[clang] [clang-format] Fix a bug in annotating struct braces (PR #90555)
via cfe-commits
cfe-commits at lists.llvm.org
Mon Apr 29 21:15:47 PDT 2024
llvmbot wrote:
<!--LLVM PR SUMMARY COMMENT-->
@llvm/pr-subscribers-clang-format
Author: Owen Pan (owenca)
<details>
<summary>Changes</summary>
Fixes #<!-- -->60040.
---
Full diff: https://github.com/llvm/llvm-project/pull/90555.diff
2 Files Affected:
- (modified) clang/lib/Format/UnwrappedLineParser.cpp (+4-1)
- (modified) clang/unittests/Format/TokenAnnotatorTest.cpp (+13)
``````````diff
diff --git a/clang/lib/Format/UnwrappedLineParser.cpp b/clang/lib/Format/UnwrappedLineParser.cpp
index 3a263955a6a8fe..854428389740d8 100644
--- a/clang/lib/Format/UnwrappedLineParser.cpp
+++ b/clang/lib/Format/UnwrappedLineParser.cpp
@@ -3944,8 +3944,11 @@ void UnwrappedLineParser::parseRecord(bool ParseAsExpr) {
switch (FormatTok->Tok.getKind()) {
case tok::l_paren:
// We can have macros in between 'class' and the class name.
- if (!IsNonMacroIdentifier(Previous))
+ if (!IsNonMacroIdentifier(Previous) ||
+ // e.g. `struct macro(a) S { int i; };`
+ Previous->Previous == &InitialToken) {
parseParens();
+ }
break;
case tok::coloncolon:
break;
diff --git a/clang/unittests/Format/TokenAnnotatorTest.cpp b/clang/unittests/Format/TokenAnnotatorTest.cpp
index dff5251d2e9406..01daf8dee505bc 100644
--- a/clang/unittests/Format/TokenAnnotatorTest.cpp
+++ b/clang/unittests/Format/TokenAnnotatorTest.cpp
@@ -438,6 +438,11 @@ TEST_F(TokenAnnotatorTest, UnderstandsStructs) {
EXPECT_TOKEN(Tokens[2], tok::l_brace, TT_StructLBrace);
EXPECT_TOKEN(Tokens[3], tok::r_brace, TT_StructRBrace);
+ Tokens = annotate("struct macro(a) S {};");
+ ASSERT_EQ(Tokens.size(), 10u) << Tokens;
+ EXPECT_TOKEN(Tokens[6], tok::l_brace, TT_StructLBrace);
+ EXPECT_TOKEN(Tokens[7], tok::r_brace, TT_StructRBrace);
+
Tokens = annotate("struct EXPORT_MACRO [[nodiscard]] C { int i; };");
ASSERT_EQ(Tokens.size(), 15u) << Tokens;
EXPECT_TOKEN(Tokens[8], tok::l_brace, TT_StructLBrace);
@@ -448,6 +453,14 @@ TEST_F(TokenAnnotatorTest, UnderstandsStructs) {
EXPECT_TOKEN(Tokens[12], tok::l_brace, TT_StructLBrace);
EXPECT_TOKEN(Tokens[16], tok::r_brace, TT_StructRBrace);
+ Tokens = annotate("struct macro(a) S {\n"
+ " void f(T &t);\n"
+ "};");
+ ASSERT_EQ(Tokens.size(), 18u) << Tokens;
+ EXPECT_TOKEN(Tokens[6], tok::l_brace, TT_StructLBrace);
+ EXPECT_TOKEN(Tokens[11], tok::amp, TT_PointerOrReference);
+ EXPECT_TOKEN(Tokens[15], tok::r_brace, TT_StructRBrace);
+
Tokens = annotate("template <typename T> struct S<const T[N]> {};");
ASSERT_EQ(Tokens.size(), 18u) << Tokens;
EXPECT_TOKEN(Tokens[7], tok::less, TT_TemplateOpener);
``````````
</details>
https://github.com/llvm/llvm-project/pull/90555
More information about the cfe-commits
mailing list