Skip to content

Commit d89f200

Browse files
authored
[clang-format] Fix a bug in formatting goto labels in macros (llvm#92494)
Fixes llvm#92300.
1 parent 74f1e29 commit d89f200

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
@@ -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

+8
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

+13
Original file line numberDiff line numberDiff line change
@@ -2579,15 +2579,28 @@ TEST_F(TokenAnnotatorTest, UnderstandsLabels) {
25792579
auto Tokens = annotate("{ x: break; }");
25802580
ASSERT_EQ(Tokens.size(), 7u) << Tokens;
25812581
EXPECT_TOKEN(Tokens[2], tok::colon, TT_GotoLabelColon);
2582+
25822583
Tokens = annotate("{ case x: break; }");
25832584
ASSERT_EQ(Tokens.size(), 8u) << Tokens;
25842585
EXPECT_TOKEN(Tokens[3], tok::colon, TT_CaseLabelColon);
2586+
25852587
Tokens = annotate("{ x: { break; } }");
25862588
ASSERT_EQ(Tokens.size(), 9u) << Tokens;
25872589
EXPECT_TOKEN(Tokens[2], tok::colon, TT_GotoLabelColon);
2590+
25882591
Tokens = annotate("{ case x: { break; } }");
25892592
ASSERT_EQ(Tokens.size(), 10u) << Tokens;
25902593
EXPECT_TOKEN(Tokens[3], tok::colon, TT_CaseLabelColon);
2594+
2595+
Tokens = annotate("#define FOO label:");
2596+
ASSERT_EQ(Tokens.size(), 6u) << Tokens;
2597+
EXPECT_TOKEN(Tokens[4], tok::colon, TT_GotoLabelColon);
2598+
2599+
Tokens = annotate("#define FOO \\\n"
2600+
"label: \\\n"
2601+
" break;");
2602+
ASSERT_EQ(Tokens.size(), 8u) << Tokens;
2603+
EXPECT_TOKEN(Tokens[4], tok::colon, TT_GotoLabelColon);
25912604
}
25922605

25932606
TEST_F(TokenAnnotatorTest, UnderstandsNestedBlocks) {

0 commit comments

Comments
 (0)