Skip to content

[clang-format] Correctly annotate braces in macro definition #107352

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Sep 6, 2024

Conversation

owenca
Copy link
Contributor

@owenca owenca commented Sep 5, 2024

Also add a test case for #107096.

Fixes #106418.

@llvmbot
Copy link
Member

llvmbot commented Sep 5, 2024

@llvm/pr-subscribers-clang-format

Author: Owen Pan (owenca)

Changes

Also add a test case for #106418.

Fixes #107096.


Full diff: https://github.com/llvm/llvm-project/pull/107352.diff

2 Files Affected:

  • (modified) clang/lib/Format/UnwrappedLineParser.cpp (+2-1)
  • (modified) clang/unittests/Format/TokenAnnotatorTest.cpp (+20)
diff --git a/clang/lib/Format/UnwrappedLineParser.cpp b/clang/lib/Format/UnwrappedLineParser.cpp
index 246b29d308bfaf..1727ed93822b1b 100644
--- a/clang/lib/Format/UnwrappedLineParser.cpp
+++ b/clang/lib/Format/UnwrappedLineParser.cpp
@@ -570,7 +570,8 @@ void UnwrappedLineParser::calculateBraceTypes(bool ExpectClassBody) {
                                 NextTok->isOneOf(Keywords.kw_of, Keywords.kw_in,
                                                  Keywords.kw_as));
           ProbablyBracedList =
-              ProbablyBracedList || (IsCpp && NextTok->is(tok::l_paren));
+              ProbablyBracedList || (IsCpp && (PrevTok->Tok.isLiteral() ||
+                                               NextTok->is(tok::l_paren)));
 
           // If there is a comma, semicolon or right paren after the closing
           // brace, we assume this is a braced initializer list.
diff --git a/clang/unittests/Format/TokenAnnotatorTest.cpp b/clang/unittests/Format/TokenAnnotatorTest.cpp
index c0436d8a2e180a..1bb796fd6f5ee9 100644
--- a/clang/unittests/Format/TokenAnnotatorTest.cpp
+++ b/clang/unittests/Format/TokenAnnotatorTest.cpp
@@ -3278,6 +3278,26 @@ TEST_F(TokenAnnotatorTest, BraceKind) {
   EXPECT_BRACE_KIND(Tokens[10], BK_Block);
   EXPECT_TOKEN(Tokens[11], tok::r_brace, TT_StructRBrace);
   EXPECT_BRACE_KIND(Tokens[11], BK_Block);
+
+  Tokens = annotate("#define MACRO            \\\n"
+                    "  struct hash<type> {    \\\n"
+                    "    void f() { return; } \\\n"
+                    "  };");
+  ASSERT_EQ(Tokens.size(), 20u) << Tokens;
+  EXPECT_TOKEN(Tokens[8], tok::l_brace, TT_StructLBrace);
+  EXPECT_BRACE_KIND(Tokens[8], BK_Block);
+  EXPECT_TOKEN(Tokens[10], tok::identifier, TT_FunctionDeclarationName);
+  EXPECT_TOKEN(Tokens[11], tok::l_paren, TT_FunctionDeclarationLParen);
+  EXPECT_TOKEN(Tokens[13], tok::l_brace, TT_FunctionLBrace);
+  EXPECT_BRACE_KIND(Tokens[13], BK_Block);
+  EXPECT_BRACE_KIND(Tokens[16], BK_Block);
+  EXPECT_TOKEN(Tokens[17], tok::r_brace, TT_StructRBrace);
+  EXPECT_BRACE_KIND(Tokens[17], BK_Block);
+
+  Tokens = annotate("#define MEMBER(NAME) NAME{\"\"}");
+  ASSERT_EQ(Tokens.size(), 11u) << Tokens;
+  EXPECT_BRACE_KIND(Tokens[7], BK_BracedInit);
+  EXPECT_BRACE_KIND(Tokens[9], BK_BracedInit);
 }
 
 TEST_F(TokenAnnotatorTest, UnderstandsElaboratedTypeSpecifier) {

@owenca owenca merged commit 616a8ce into llvm:main Sep 6, 2024
10 checks passed
@owenca owenca deleted the braces-in-macro branch September 6, 2024 01:45
@prj-
Copy link

prj- commented Sep 6, 2024

Could you please back-port this to llvm:release/19.x while reverting the previous fix?

@owenca owenca added this to the LLVM 19.X Release milestone Sep 6, 2024
@owenca
Copy link
Contributor Author

owenca commented Sep 6, 2024

/cherry-pick 616a8ce

@llvmbot
Copy link
Member

llvmbot commented Sep 6, 2024

Failed to cherry-pick: 616a8ce

https://github.com/llvm/llvm-project/actions/runs/10733540294

Please manually backport the fix and push it to your github fork. Once this is done, please create a pull request

owenca added a commit to owenca/llvm-project that referenced this pull request Sep 6, 2024
@owenca
Copy link
Contributor Author

owenca commented Sep 6, 2024

Done in #107531.

tru pushed a commit to owenca/llvm-project that referenced this pull request Sep 10, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
Development

Successfully merging this pull request may close these issues.

[clang-format] [regression] formatting of macro not stable; oscillates between two ways
5 participants