Skip to content

Commit 530b15d

Browse files
committed
[clang-format] Fix a regression in annotating goto labels
Fixes #92300.
1 parent 88d351e commit 530b15d

File tree

2 files changed

+15
-7
lines changed

2 files changed

+15
-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/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)