Skip to content

Commit d9f1131

Browse files
committed
[clang-format] Fix a bug in formatting goto labels in macros
Fixes #92300.
1 parent 88d351e commit d9f1131

File tree

3 files changed

+23
-7
lines changed

3 files changed

+23
-7
lines changed

clang/lib/Format/UnwrappedLineParser.cpp

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1189,12 +1189,6 @@ void UnwrappedLineParser::parsePPDefine() {
11891189
return;
11901190
}
11911191

1192-
if (FormatTok->is(tok::identifier) &&
1193-
Tokens->peekNextToken()->is(tok::colon)) {
1194-
nextToken();
1195-
nextToken();
1196-
}
1197-
11981192
// Errors during a preprocessor directive can only affect the layout of the
11991193
// preprocessor directive, and thus we ignore them. An alternative approach
12001194
// would be to use the same approach we use on the file level (no
@@ -1681,7 +1675,8 @@ void UnwrappedLineParser::parseStructuralElement(
16811675
if (!Style.isJavaScript() && !Style.isVerilog() && !Style.isTableGen() &&
16821676
Tokens->peekNextToken()->is(tok::colon) && !Line->MustBeDeclaration) {
16831677
nextToken();
1684-
Line->Tokens.begin()->Tok->MustBreakBefore = true;
1678+
if (!Line->InMacroBody || CurrentLines->size() > 1)
1679+
Line->Tokens.begin()->Tok->MustBreakBefore = true;
16851680
FormatTok->setFinalizedType(TT_GotoLabelColon);
16861681
parseLabel(!Style.IndentGotoLabels);
16871682
if (HasLabel)

clang/unittests/Format/FormatTest.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3124,6 +3124,7 @@ TEST_F(FormatTest, FormatsLabels) {
31243124
" g();\n"
31253125
" }\n"
31263126
"}");
3127+
31273128
FormatStyle Style = getLLVMStyle();
31283129
Style.IndentGotoLabels = false;
31293130
verifyFormat("void f() {\n"
@@ -3163,6 +3164,13 @@ TEST_F(FormatTest, FormatsLabels) {
31633164
" }\n"
31643165
"}",
31653166
Style);
3167+
3168+
Style.ColumnLimit = 15;
3169+
verifyFormat("#define FOO \\\n"
3170+
"label: \\\n"
3171+
" break;",
3172+
Style);
3173+
31663174
// The opening brace may either be on the same unwrapped line as the colon or
31673175
// on a separate one. The formatter should recognize both.
31683176
Style = getLLVMStyle();

clang/unittests/Format/TokenAnnotatorTest.cpp

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2581,15 +2581,28 @@ TEST_F(TokenAnnotatorTest, UnderstandsLabels) {
25812581
auto Tokens = annotate("{ x: break; }");
25822582
ASSERT_EQ(Tokens.size(), 7u) << Tokens;
25832583
EXPECT_TOKEN(Tokens[2], tok::colon, TT_GotoLabelColon);
2584+
25842585
Tokens = annotate("{ case x: break; }");
25852586
ASSERT_EQ(Tokens.size(), 8u) << Tokens;
25862587
EXPECT_TOKEN(Tokens[3], tok::colon, TT_CaseLabelColon);
2588+
25872589
Tokens = annotate("{ x: { break; } }");
25882590
ASSERT_EQ(Tokens.size(), 9u) << Tokens;
25892591
EXPECT_TOKEN(Tokens[2], tok::colon, TT_GotoLabelColon);
2592+
25902593
Tokens = annotate("{ case x: { break; } }");
25912594
ASSERT_EQ(Tokens.size(), 10u) << Tokens;
25922595
EXPECT_TOKEN(Tokens[3], tok::colon, TT_CaseLabelColon);
2596+
2597+
Tokens = annotate("#define FOO label:");
2598+
ASSERT_EQ(Tokens.size(), 6u) << Tokens;
2599+
EXPECT_TOKEN(Tokens[4], tok::colon, TT_GotoLabelColon);
2600+
2601+
Tokens = annotate("#define FOO \\\n"
2602+
"label: \\\n"
2603+
" break;");
2604+
ASSERT_EQ(Tokens.size(), 8u) << Tokens;
2605+
EXPECT_TOKEN(Tokens[4], tok::colon, TT_GotoLabelColon);
25932606
}
25942607

25952608
TEST_F(TokenAnnotatorTest, UnderstandsNestedBlocks) {

0 commit comments

Comments
 (0)