summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorOwen Pan <owenpiano@gmail.com>2024-05-21 01:35:31 -0700
committerTom Stellard <tstellar@redhat.com>2024-06-05 14:56:32 -0700
commit768118d1ad38bf13c545828f67bd6b474d61fc55 (patch)
tree185d93fc1486681c0e6319502032232219299a25
parent8c0fe0d65ed85966c0ac075e896620c55ca95227 (diff)
[clang-format] Fix a bug in formatting goto labels in macros (#92494)llvmorg-18.1.7upstream/release/18.x
Fixes #92300. (cherry picked from commit d89f20058b45e3836527e816af7ed7372e1d554d)
-rw-r--r--clang/lib/Format/UnwrappedLineParser.cpp9
-rw-r--r--clang/unittests/Format/FormatTest.cpp8
-rw-r--r--clang/unittests/Format/TokenAnnotatorTest.cpp13
3 files changed, 23 insertions, 7 deletions
diff --git a/clang/lib/Format/UnwrappedLineParser.cpp b/clang/lib/Format/UnwrappedLineParser.cpp
index f70affb732a0..179d77bf0049 100644
--- a/clang/lib/Format/UnwrappedLineParser.cpp
+++ b/clang/lib/Format/UnwrappedLineParser.cpp
@@ -1185,12 +1185,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
@@ -1671,7 +1665,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 d69632f7f0f8..11ae41bc78ac 100644
--- a/clang/unittests/Format/FormatTest.cpp
+++ b/clang/unittests/Format/FormatTest.cpp
@@ -3118,6 +3118,7 @@ TEST_F(FormatTest, FormatsLabels) {
" g();\n"
" }\n"
"}");
+
FormatStyle Style = getLLVMStyle();
Style.IndentGotoLabels = false;
verifyFormat("void f() {\n"
@@ -3157,6 +3158,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 44ebad9d5a87..dfa9c22430f3 100644
--- a/clang/unittests/Format/TokenAnnotatorTest.cpp
+++ b/clang/unittests/Format/TokenAnnotatorTest.cpp
@@ -2353,15 +2353,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) {