Skip to content

Commit 7b6ee6e

Browse files
owencatru
authored andcommitted
[clang-format] Fix a regression in parsing switch in macro call (#114506)
Fixes #114408. (cherry picked from commit 6ca816f)
1 parent 430c495 commit 7b6ee6e

File tree

2 files changed

+13
-2
lines changed

2 files changed

+13
-2
lines changed

clang/lib/Format/UnwrappedLineParser.cpp

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2086,7 +2086,8 @@ void UnwrappedLineParser::parseStructuralElement(
20862086
case tok::kw_switch:
20872087
if (Style.Language == FormatStyle::LK_Java)
20882088
parseSwitch(/*IsExpr=*/true);
2089-
nextToken();
2089+
else
2090+
nextToken();
20902091
break;
20912092
case tok::kw_case:
20922093
// Proto: there are no switch/case statements.
@@ -2637,7 +2638,10 @@ bool UnwrappedLineParser::parseParens(TokenType AmpAmpTokenType) {
26372638
nextToken();
26382639
break;
26392640
case tok::kw_switch:
2640-
parseSwitch(/*IsExpr=*/true);
2641+
if (Style.Language == FormatStyle::LK_Java)
2642+
parseSwitch(/*IsExpr=*/true);
2643+
else
2644+
nextToken();
26412645
break;
26422646
case tok::kw_requires: {
26432647
auto RequiresToken = FormatTok;

clang/unittests/Format/TokenAnnotatorTest.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3412,6 +3412,13 @@ TEST_F(TokenAnnotatorTest, TemplateInstantiation) {
34123412
EXPECT_TOKEN(Tokens[18], tok::greater, TT_TemplateCloser);
34133413
}
34143414

3415+
TEST_F(TokenAnnotatorTest, SwitchInMacroArgument) {
3416+
auto Tokens = annotate("FOOBAR(switch);\n"
3417+
"void f() {}");
3418+
ASSERT_EQ(Tokens.size(), 12u) << Tokens;
3419+
EXPECT_TOKEN(Tokens[9], tok::l_brace, TT_FunctionLBrace);
3420+
}
3421+
34153422
} // namespace
34163423
} // namespace format
34173424
} // namespace clang

0 commit comments

Comments
 (0)