Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 14 additions & 9 deletions clang/lib/Format/UnwrappedLineParser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -491,11 +491,19 @@ void UnwrappedLineParser::calculateBraceTypes(bool ExpectClassBody) {
SmallVector<StackEntry, 8> LBraceStack;
assert(Tok->is(tok::l_brace));
do {
// Get next non-comment token.
FormatToken *NextTok;
do {
NextTok = Tokens->getNextToken();
} while (NextTok->is(tok::comment));
// Get next non-comment, non-preprocessor token.
FormatToken *NextTok = Tokens->getNextToken();
while (NextTok->is(tok::comment) ||
(NextTok->is(tok::hash) && isOnNewLine(*NextTok))) {
while (NextTok->is(tok::comment))
NextTok = Tokens->getNextToken();
while (NextTok->is(tok::hash) && isOnNewLine(*NextTok)) {
ScopedMacroState MacroState(*Line, Tokens, NextTok);
do {
NextTok = Tokens->getNextToken();
} while (NextTok->isNot(tok::eof));
}
}

switch (Tok->Tok.getKind()) {
case tok::l_brace:
Expand Down Expand Up @@ -611,12 +619,9 @@ void UnwrappedLineParser::calculateBraceTypes(bool ExpectClassBody) {
if (Tok->isNot(TT_StatementMacro))
break;
[[fallthrough]];
case tok::kw_if:
if (PrevTok->is(tok::hash))
break;
[[fallthrough]];
case tok::at:
case tok::semi:
case tok::kw_if:
case tok::kw_while:
case tok::kw_for:
case tok::kw_switch:
Expand Down
15 changes: 15 additions & 0 deletions clang/unittests/Format/TokenAnnotatorTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2134,6 +2134,7 @@ TEST_F(TokenAnnotatorTest, UnderstandConstructors) {
EXPECT_TOKEN(Tokens[11], tok::l_paren, TT_Unknown);
EXPECT_TOKEN(Tokens[12], tok::r_paren, TT_Unknown);
EXPECT_TOKEN(Tokens[13], tok::l_brace, TT_FunctionLBrace);
EXPECT_BRACE_KIND(Tokens[13], BK_Block);

Tokens = annotate("Class::Class() : BaseClass{}, Member{} {}");
ASSERT_EQ(Tokens.size(), 16u) << Tokens;
Expand All @@ -2146,6 +2147,20 @@ TEST_F(TokenAnnotatorTest, UnderstandConstructors) {
EXPECT_TOKEN(Tokens[11], tok::l_brace, TT_Unknown);
EXPECT_TOKEN(Tokens[12], tok::r_brace, TT_Unknown);
EXPECT_TOKEN(Tokens[13], tok::l_brace, TT_FunctionLBrace);
EXPECT_BRACE_KIND(Tokens[13], BK_Block);

Tokens = annotate("class Class {\n"
" Class() : BaseClass() {\n"
"#if 0\n"
" // comment\n"
"#endif\n"
" }\n"
" Class f();\n"
"}");
ASSERT_EQ(Tokens.size(), 25u) << Tokens;
EXPECT_TOKEN(Tokens[6], tok::colon, TT_CtorInitializerColon);
EXPECT_TOKEN(Tokens[10], tok::l_brace, TT_FunctionLBrace);
EXPECT_BRACE_KIND(Tokens[10], BK_Block);
}

TEST_F(TokenAnnotatorTest, UnderstandsConditionParens) {
Expand Down