Skip to content

Commit 768118d

Browse files
owencatstellar
authored andcommitted
[clang-format] Fix a bug in formatting goto labels in macros (#92494)
Fixes #92300. (cherry picked from commit d89f200)
1 parent 8c0fe0d commit 768118d

File tree

3 files changed

+23
-7
lines changed

3 files changed

+23
-7
lines changed

clang/lib/Format/UnwrappedLineParser.cpp

+2-7
Original file line numberDiff line numberDiff line change
@@ -1185,12 +1185,6 @@ void UnwrappedLineParser::parsePPDefine() {
11851185
return;
11861186
}
11871187

1188-
if (FormatTok->is(tok::identifier) &&
1189-
Tokens->peekNextToken()->is(tok::colon)) {
1190-
nextToken();
1191-
nextToken();
1192-
}
1193-
11941188
// Errors during a preprocessor directive can only affect the layout of the
11951189
// preprocessor directive, and thus we ignore them. An alternative approach
11961190
// would be to use the same approach we use on the file level (no
@@ -1671,7 +1665,8 @@ void UnwrappedLineParser::parseStructuralElement(
16711665
if (!Style.isJavaScript() && !Style.isVerilog() && !Style.isTableGen() &&
16721666
Tokens->peekNextToken()->is(tok::colon) && !Line->MustBeDeclaration) {
16731667
nextToken();
1674-
Line->Tokens.begin()->Tok->MustBreakBefore = true;
1668+
if (!Line->InMacroBody || CurrentLines->size() > 1)
1669+
Line->Tokens.begin()->Tok->MustBreakBefore = true;
16751670
FormatTok->setFinalizedType(TT_GotoLabelColon);
16761671
parseLabel(!Style.IndentGotoLabels);
16771672
if (HasLabel)

clang/unittests/Format/FormatTest.cpp

+8
Original file line numberDiff line numberDiff line change
@@ -3118,6 +3118,7 @@ TEST_F(FormatTest, FormatsLabels) {
31183118
" g();\n"
31193119
" }\n"
31203120
"}");
3121+
31213122
FormatStyle Style = getLLVMStyle();
31223123
Style.IndentGotoLabels = false;
31233124
verifyFormat("void f() {\n"
@@ -3157,6 +3158,13 @@ TEST_F(FormatTest, FormatsLabels) {
31573158
" }\n"
31583159
"}",
31593160
Style);
3161+
3162+
Style.ColumnLimit = 15;
3163+
verifyFormat("#define FOO \\\n"
3164+
"label: \\\n"
3165+
" break;",
3166+
Style);
3167+
31603168
// The opening brace may either be on the same unwrapped line as the colon or
31613169
// on a separate one. The formatter should recognize both.
31623170
Style = getLLVMStyle();

clang/unittests/Format/TokenAnnotatorTest.cpp

+13
Original file line numberDiff line numberDiff line change
@@ -2353,15 +2353,28 @@ TEST_F(TokenAnnotatorTest, UnderstandsLabels) {
23532353
auto Tokens = annotate("{ x: break; }");
23542354
ASSERT_EQ(Tokens.size(), 7u) << Tokens;
23552355
EXPECT_TOKEN(Tokens[2], tok::colon, TT_GotoLabelColon);
2356+
23562357
Tokens = annotate("{ case x: break; }");
23572358
ASSERT_EQ(Tokens.size(), 8u) << Tokens;
23582359
EXPECT_TOKEN(Tokens[3], tok::colon, TT_CaseLabelColon);
2360+
23592361
Tokens = annotate("{ x: { break; } }");
23602362
ASSERT_EQ(Tokens.size(), 9u) << Tokens;
23612363
EXPECT_TOKEN(Tokens[2], tok::colon, TT_GotoLabelColon);
2364+
23622365
Tokens = annotate("{ case x: { break; } }");
23632366
ASSERT_EQ(Tokens.size(), 10u) << Tokens;
23642367
EXPECT_TOKEN(Tokens[3], tok::colon, TT_CaseLabelColon);
2368+
2369+
Tokens = annotate("#define FOO label:");
2370+
ASSERT_EQ(Tokens.size(), 6u) << Tokens;
2371+
EXPECT_TOKEN(Tokens[4], tok::colon, TT_GotoLabelColon);
2372+
2373+
Tokens = annotate("#define FOO \\\n"
2374+
"label: \\\n"
2375+
" break;");
2376+
ASSERT_EQ(Tokens.size(), 8u) << Tokens;
2377+
EXPECT_TOKEN(Tokens[4], tok::colon, TT_GotoLabelColon);
23652378
}
23662379

23672380
TEST_F(TokenAnnotatorTest, UnderstandsNestedBlocks) {

0 commit comments

Comments
 (0)