Author: Owen Pan Date: 2024-05-21T01:35:31-07:00 New Revision: d89f20058b45e3836527e816af7ed7372e1d554d
URL: https://github.com/llvm/llvm-project/commit/d89f20058b45e3836527e816af7ed7372e1d554d DIFF: https://github.com/llvm/llvm-project/commit/d89f20058b45e3836527e816af7ed7372e1d554d.diff LOG: [clang-format] Fix a bug in formatting goto labels in macros (#92494) Fixes #92300. Added: Modified: clang/lib/Format/UnwrappedLineParser.cpp clang/unittests/Format/FormatTest.cpp clang/unittests/Format/TokenAnnotatorTest.cpp Removed: ################################################################################ diff --git a/clang/lib/Format/UnwrappedLineParser.cpp b/clang/lib/Format/UnwrappedLineParser.cpp index b15a87327240b..3dd10f6bd2b31 100644 --- a/clang/lib/Format/UnwrappedLineParser.cpp +++ b/clang/lib/Format/UnwrappedLineParser.cpp @@ -1189,12 +1189,6 @@ void UnwrappedLineParser::parsePPDefine() { return; } - if (FormatTok->is(tok::identifier) && - Tokens->peekNextToken()->is(tok::colon)) { - nextToken(); - nextToken(); - } - // Errors during a preprocessor directive can only affect the layout of the // preprocessor directive, and thus we ignore them. An alternative approach // would be to use the same approach we use on the file level (no @@ -1681,7 +1675,8 @@ void UnwrappedLineParser::parseStructuralElement( if (!Style.isJavaScript() && !Style.isVerilog() && !Style.isTableGen() && Tokens->peekNextToken()->is(tok::colon) && !Line->MustBeDeclaration) { nextToken(); - Line->Tokens.begin()->Tok->MustBreakBefore = true; + if (!Line->InMacroBody || CurrentLines->size() > 1) + Line->Tokens.begin()->Tok->MustBreakBefore = true; FormatTok->setFinalizedType(TT_GotoLabelColon); parseLabel(!Style.IndentGotoLabels); if (HasLabel) diff --git a/clang/unittests/Format/FormatTest.cpp b/clang/unittests/Format/FormatTest.cpp index 6f57f10e12e88..2f0c0f0266774 100644 --- a/clang/unittests/Format/FormatTest.cpp +++ b/clang/unittests/Format/FormatTest.cpp @@ -3124,6 +3124,7 @@ TEST_F(FormatTest, FormatsLabels) { " g();\n" " }\n" "}"); + FormatStyle Style = getLLVMStyle(); Style.IndentGotoLabels = false; verifyFormat("void f() {\n" @@ -3163,6 +3164,13 @@ TEST_F(FormatTest, FormatsLabels) { " }\n" "}", Style); + + Style.ColumnLimit = 15; + verifyFormat("#define FOO \\\n" + "label: \\\n" + " break;", + Style); + // The opening brace may either be on the same unwrapped line as the colon or // on a separate one. The formatter should recognize both. Style = getLLVMStyle(); diff --git a/clang/unittests/Format/TokenAnnotatorTest.cpp b/clang/unittests/Format/TokenAnnotatorTest.cpp index 7528675753047..45c1554308c9b 100644 --- a/clang/unittests/Format/TokenAnnotatorTest.cpp +++ b/clang/unittests/Format/TokenAnnotatorTest.cpp @@ -2579,15 +2579,28 @@ TEST_F(TokenAnnotatorTest, UnderstandsLabels) { auto Tokens = annotate("{ x: break; }"); ASSERT_EQ(Tokens.size(), 7u) << Tokens; EXPECT_TOKEN(Tokens[2], tok::colon, TT_GotoLabelColon); + Tokens = annotate("{ case x: break; }"); ASSERT_EQ(Tokens.size(), 8u) << Tokens; EXPECT_TOKEN(Tokens[3], tok::colon, TT_CaseLabelColon); + Tokens = annotate("{ x: { break; } }"); ASSERT_EQ(Tokens.size(), 9u) << Tokens; EXPECT_TOKEN(Tokens[2], tok::colon, TT_GotoLabelColon); + Tokens = annotate("{ case x: { break; } }"); ASSERT_EQ(Tokens.size(), 10u) << Tokens; EXPECT_TOKEN(Tokens[3], tok::colon, TT_CaseLabelColon); + + Tokens = annotate("#define FOO label:"); + ASSERT_EQ(Tokens.size(), 6u) << Tokens; + EXPECT_TOKEN(Tokens[4], tok::colon, TT_GotoLabelColon); + + Tokens = annotate("#define FOO \\\n" + "label: \\\n" + " break;"); + ASSERT_EQ(Tokens.size(), 8u) << Tokens; + EXPECT_TOKEN(Tokens[4], tok::colon, TT_GotoLabelColon); } TEST_F(TokenAnnotatorTest, UnderstandsNestedBlocks) { _______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits