[clang] 49ea702 - [clang-format] Improve attribute (macro) handling for enums (#227461)
via cfe-commits
cfe-commits at lists.llvm.org
Mon Nov 9 12:53:54 PST 2026
Author: Björn Schäpers
Date: 2026-11-09T20:53:49Z
New Revision: 49ea702ff5d5e0fbae67e535565d9dc5dddbbc98
URL: https://github.com/llvm/llvm-project/commit/49ea702ff5d5e0fbae67e535565d9dc5dddbbc98
DIFF: https://github.com/llvm/llvm-project/commit/49ea702ff5d5e0fbae67e535565d9dc5dddbbc98.diff
LOG: [clang-format] Improve attribute (macro) handling for enums (#227461)
Fixes #227244
Added:
Modified:
clang/lib/Format/UnwrappedLineParser.cpp
clang/unittests/Format/TokenAnnotatorTest.cpp
Removed:
################################################################################
diff --git a/clang/lib/Format/UnwrappedLineParser.cpp b/clang/lib/Format/UnwrappedLineParser.cpp
index ae507a0e96a57..9832dd48f832b 100644
--- a/clang/lib/Format/UnwrappedLineParser.cpp
+++ b/clang/lib/Format/UnwrappedLineParser.cpp
@@ -4011,19 +4011,23 @@ bool UnwrappedLineParser::parseEnum() {
if (Style.Language == FormatStyle::LK_Proto && FormatTok->is(tok::equal))
return false;
- if (IsCpp) {
- // Eat up enum class ...
- if (FormatTok->isOneOf(tok::kw_class, tok::kw_struct))
- nextToken();
- while (FormatTok->is(tok::l_square))
- if (!handleCppAttributes())
- return false;
- }
+ // Eat up enum class ...
+ if (IsCpp && FormatTok->isOneOf(tok::kw_class, tok::kw_struct))
+ nextToken();
while (FormatTok->Tok.getIdentifierInfo() ||
FormatTok->isOneOf(tok::colon, tok::coloncolon, tok::less,
tok::greater, tok::comma, tok::question,
tok::l_square)) {
+ if (FormatTok->is(TT_AttributeMacro)) {
+ nextToken();
+ continue;
+ }
+ if (IsCpp && FormatTok->is(tok::l_square)) {
+ if (handleCppAttributes())
+ continue;
+ return false;
+ }
if (FormatTok->is(tok::colon))
FormatTok->setFinalizedType(TT_EnumUnderlyingTypeColon);
if (Style.isVerilog()) {
@@ -4036,8 +4040,10 @@ bool UnwrappedLineParser::parseEnum() {
nextToken();
}
// We can have macros or attributes in between 'enum' and the enum name.
- if (FormatTok->is(tok::l_paren))
+ if (FormatTok->is(tok::l_paren)) {
parseParens();
+ continue;
+ }
if (FormatTok->is(tok::identifier)) {
nextToken();
// If there are two identifiers in a row, this is likely an elaborate
diff --git a/clang/unittests/Format/TokenAnnotatorTest.cpp b/clang/unittests/Format/TokenAnnotatorTest.cpp
index 138486071b330..2534dba281bd8 100644
--- a/clang/unittests/Format/TokenAnnotatorTest.cpp
+++ b/clang/unittests/Format/TokenAnnotatorTest.cpp
@@ -692,6 +692,36 @@ TEST_F(TokenAnnotatorTest, UnderstandsEnums) {
ASSERT_EQ(Tokens.size(), 6u) << Tokens;
EXPECT_TOKEN(Tokens[2], tok::l_brace, TT_EnumLBrace);
EXPECT_TOKEN(Tokens[3], tok::r_brace, TT_EnumRBrace);
+
+ auto Style = getLLVMStyle();
+ Style.AttributeMacros = {"AttributeA", "AttributeB"};
+ Tokens = annotate("enum class AttributeA AttributeB Enum {};", Style);
+ ASSERT_EQ(Tokens.size(), 9u) << Tokens;
+ EXPECT_TOKEN(Tokens[5], tok::l_brace, TT_EnumLBrace);
+ EXPECT_TOKEN(Tokens[6], tok::r_brace, TT_EnumRBrace);
+
+ Tokens = annotate("enum class AttributeA [[nodiscard]] AttributeB Enum {};",
+ Style);
+ ASSERT_EQ(Tokens.size(), 14u) << Tokens;
+ EXPECT_TOKEN(Tokens[10], tok::l_brace, TT_EnumLBrace);
+ EXPECT_TOKEN(Tokens[11], tok::r_brace, TT_EnumRBrace);
+
+ Tokens = annotate(
+ "enum class AttributeA __attribute__((nodiscard)) AttributeB Enum {};",
+ Style);
+ ASSERT_EQ(Tokens.size(), 15u) << Tokens;
+ EXPECT_TOKEN(Tokens[11], tok::l_brace, TT_EnumLBrace);
+ EXPECT_TOKEN(Tokens[12], tok::r_brace, TT_EnumRBrace);
+
+ Tokens = annotate("enum __attribute__((deprecated)) Foo {};");
+ ASSERT_EQ(Tokens.size(), 12u) << Tokens;
+ EXPECT_TOKEN(Tokens[8], tok::l_brace, TT_EnumLBrace);
+ EXPECT_TOKEN(Tokens[9], tok::r_brace, TT_EnumRBrace);
+
+ Tokens = annotate("enum __declspec(deprecated) Foo {};");
+ ASSERT_EQ(Tokens.size(), 10u) << Tokens;
+ EXPECT_TOKEN(Tokens[6], tok::l_brace, TT_EnumLBrace);
+ EXPECT_TOKEN(Tokens[7], tok::r_brace, TT_EnumRBrace);
}
TEST_F(TokenAnnotatorTest, UnderstandsExportBlock) {
More information about the cfe-commits
mailing list