Skip to content

[clang-format] Handle AttributeMacros in parseRecord() #94189

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
Jun 3, 2024

Conversation

owenca
Copy link
Contributor

@owenca owenca commented Jun 3, 2024

Fixes #94184.

@llvmbot
Copy link
Member

llvmbot commented Jun 3, 2024

@llvm/pr-subscribers-clang-format

Author: Owen Pan (owenca)

Changes

Fixes #94184.


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

2 Files Affected:

  • (modified) clang/lib/Format/UnwrappedLineParser.cpp (+3-1)
  • (modified) clang/unittests/Format/TokenAnnotatorTest.cpp (+8)
diff --git a/clang/lib/Format/UnwrappedLineParser.cpp b/clang/lib/Format/UnwrappedLineParser.cpp
index 053fd3d4df559..d6061c2666c2a 100644
--- a/clang/lib/Format/UnwrappedLineParser.cpp
+++ b/clang/lib/Format/UnwrappedLineParser.cpp
@@ -3993,8 +3993,10 @@ void UnwrappedLineParser::parseRecord(bool ParseAsExpr) {
     case tok::coloncolon:
       break;
     default:
-      if (!ClassName && Previous->is(tok::identifier))
+      if (!ClassName && Previous->is(tok::identifier) &&
+          Previous->isNot(TT_AttributeMacro)) {
         ClassName = Previous;
+      }
     }
   }
 
diff --git a/clang/unittests/Format/TokenAnnotatorTest.cpp b/clang/unittests/Format/TokenAnnotatorTest.cpp
index a3b2569d80633..3d609a1f041bc 100644
--- a/clang/unittests/Format/TokenAnnotatorTest.cpp
+++ b/clang/unittests/Format/TokenAnnotatorTest.cpp
@@ -493,6 +493,14 @@ TEST_F(TokenAnnotatorTest, UnderstandsStructs) {
   Tokens = annotate("template <typename T, enum E e> struct S {};");
   ASSERT_EQ(Tokens.size(), 15u) << Tokens;
   EXPECT_TOKEN(Tokens[11], tok::l_brace, TT_StructLBrace);
+
+  auto Style = getLLVMStyle();
+  Style.AttributeMacros.push_back("EXPORT");
+  Tokens = annotate("struct EXPORT StructName {};", Style);
+  ASSERT_EQ(Tokens.size(), 7u) << Tokens;
+  EXPECT_TOKEN(Tokens[1], tok::identifier, TT_AttributeMacro);
+  EXPECT_TOKEN(Tokens[3], tok::l_brace, TT_StructLBrace);
+  EXPECT_TOKEN(Tokens[4], tok::r_brace, TT_StructRBrace);
 }
 
 TEST_F(TokenAnnotatorTest, UnderstandsUnions) {

Copy link
Contributor

@HazardyKnusperkeks HazardyKnusperkeks left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As noted in the issue, I think it should also keep working without the attribute macro. (I'd be fine with the all upper case is a macro heuristic.)

@owenca owenca merged commit 13f6797 into llvm:main Jun 3, 2024
9 checks passed
@owenca owenca deleted the 94184 branch June 3, 2024 19:55
@owenca
Copy link
Contributor Author

owenca commented Jun 4, 2024

As noted in the issue, I think it should also keep working without the attribute macro. (I'd be fine with the all upper case is a macro heuristic.)

See #94184 (comment).

@owenca
Copy link
Contributor Author

owenca commented Jan 25, 2025

As noted in the issue, I think it should also keep working without the attribute macro. (I'd be fine with the all upper case is a macro heuristic.)

See #124397.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[clang-format] Struct/class declaration with export macro treated as initialization in clang-format 19
4 participants