[clang] 8f70d16 - [clang-format] Handle attributes in enum declaration.
Marek Kurdej via cfe-commits
cfe-commits at lists.llvm.org
Thu May 26 06:44:01 PDT 2022
Author: Tyler Chatow
Date: 2022-05-26T15:43:57+02:00
New Revision: 8f70d16c9ab2d7c060f5c92a31a0fc4a82349897
URL: https://github.com/llvm/llvm-project/commit/8f70d16c9ab2d7c060f5c92a31a0fc4a82349897
DIFF: https://github.com/llvm/llvm-project/commit/8f70d16c9ab2d7c060f5c92a31a0fc4a82349897.diff
LOG: [clang-format] Handle attributes in enum declaration.
Fixes https://github.com/llvm/llvm-project/issues/55457
Ensures that attributes in the enum declaration are interpreted
correctly, for instance:
```
enum class [[nodiscard]] E {
a,
b
};
```
Reviewed By: MyDeveloperDay, curdeius
Differential Revision: https://reviews.llvm.org/D125848
Added:
Modified:
clang/lib/Format/UnwrappedLineParser.cpp
clang/unittests/Format/FormatTest.cpp
Removed:
################################################################################
diff --git a/clang/lib/Format/UnwrappedLineParser.cpp b/clang/lib/Format/UnwrappedLineParser.cpp
index a20562dd77a4..0611e9eace47 100644
--- a/clang/lib/Format/UnwrappedLineParser.cpp
+++ b/clang/lib/Format/UnwrappedLineParser.cpp
@@ -3419,11 +3419,18 @@ bool UnwrappedLineParser::parseEnum() {
while (FormatTok->Tok.getIdentifierInfo() ||
FormatTok->isOneOf(tok::colon, tok::coloncolon, tok::less,
- tok::greater, tok::comma, tok::question)) {
+ tok::greater, tok::comma, tok::question,
+ tok::l_square, tok::r_square)) {
nextToken();
// We can have macros or attributes in between 'enum' and the enum name.
if (FormatTok->is(tok::l_paren))
parseParens();
+ if (FormatTok->is(TT_AttributeSquare)) {
+ parseSquare();
+ // Consume the closing TT_AttributeSquare.
+ if (FormatTok->Next && FormatTok->is(TT_AttributeSquare))
+ nextToken();
+ }
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/FormatTest.cpp b/clang/unittests/Format/FormatTest.cpp
index 3621ba667818..44ef882fe7db 100644
--- a/clang/unittests/Format/FormatTest.cpp
+++ b/clang/unittests/Format/FormatTest.cpp
@@ -3573,6 +3573,7 @@ TEST_F(FormatTest, FormatsEnum) {
verifyFormat("enum X E {} d;");
verifyFormat("enum __attribute__((...)) E {} d;");
verifyFormat("enum __declspec__((...)) E {} d;");
+ verifyFormat("enum [[nodiscard]] E {} d;");
verifyFormat("enum {\n"
" Bar = Foo<int, int>::value\n"
"};",
@@ -3619,6 +3620,17 @@ TEST_F(FormatTest, FormatsEnum) {
"};",
EightIndent);
+ verifyFormat("enum [[nodiscard]] E {\n"
+ " ONE,\n"
+ " TWO,\n"
+ "};");
+ verifyFormat("enum [[nodiscard]] E {\n"
+ " // Comment 1\n"
+ " ONE,\n"
+ " // Comment 2\n"
+ " TWO,\n"
+ "};");
+
// Not enums.
verifyFormat("enum X f() {\n"
" a();\n"
@@ -3666,7 +3678,19 @@ TEST_F(FormatTest, FormatsEnumStruct) {
verifyFormat("enum struct X E {} d;");
verifyFormat("enum struct __attribute__((...)) E {} d;");
verifyFormat("enum struct __declspec__((...)) E {} d;");
+ verifyFormat("enum struct [[nodiscard]] E {} d;");
verifyFormat("enum struct X f() {\n a();\n return 42;\n}");
+
+ verifyFormat("enum struct [[nodiscard]] E {\n"
+ " ONE,\n"
+ " TWO,\n"
+ "};");
+ verifyFormat("enum struct [[nodiscard]] E {\n"
+ " // Comment 1\n"
+ " ONE,\n"
+ " // Comment 2\n"
+ " TWO,\n"
+ "};");
}
TEST_F(FormatTest, FormatsEnumClass) {
@@ -3683,7 +3707,19 @@ TEST_F(FormatTest, FormatsEnumClass) {
verifyFormat("enum class X E {} d;");
verifyFormat("enum class __attribute__((...)) E {} d;");
verifyFormat("enum class __declspec__((...)) E {} d;");
+ verifyFormat("enum class [[nodiscard]] E {} d;");
verifyFormat("enum class X f() {\n a();\n return 42;\n}");
+
+ verifyFormat("enum class [[nodiscard]] E {\n"
+ " ONE,\n"
+ " TWO,\n"
+ "};");
+ verifyFormat("enum class [[nodiscard]] E {\n"
+ " // Comment 1\n"
+ " ONE,\n"
+ " // Comment 2\n"
+ " TWO,\n"
+ "};");
}
TEST_F(FormatTest, FormatsEnumTypes) {
More information about the cfe-commits
mailing list