[PATCH] D125848: [clang-format] Handle attributes in enum declaration.
tchatow created this revision. Herald added a project: All. tchatow requested review of this revision. Herald added a project: clang. 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 }; Repository: rG LLVM Github Monorepo https://reviews.llvm.org/D125848 Files: clang/lib/Format/UnwrappedLineParser.cpp clang/unittests/Format/FormatTest.cpp Index: clang/unittests/Format/FormatTest.cpp === --- clang/unittests/Format/FormatTest.cpp +++ clang/unittests/Format/FormatTest.cpp @@ -3565,6 +3565,7 @@ 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::value\n" "};", @@ -3658,6 +3659,7 @@ 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}"); } @@ -3675,6 +3677,7 @@ 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}"); } Index: clang/lib/Format/UnwrappedLineParser.cpp === --- clang/lib/Format/UnwrappedLineParser.cpp +++ clang/lib/Format/UnwrappedLineParser.cpp @@ -3360,11 +3360,18 @@ 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 Index: clang/unittests/Format/FormatTest.cpp === --- clang/unittests/Format/FormatTest.cpp +++ clang/unittests/Format/FormatTest.cpp @@ -3565,6 +3565,7 @@ 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::value\n" "};", @@ -3658,6 +3659,7 @@ 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}"); } @@ -3675,6 +3677,7 @@ 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}"); } Index: clang/lib/Format/UnwrappedLineParser.cpp === --- clang/lib/Format/UnwrappedLineParser.cpp +++ clang/lib/Format/UnwrappedLineParser.cpp @@ -3360,11 +3360,18 @@ 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 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[PATCH] D125848: [clang-format] Handle attributes in enum declaration.
tchatow updated this revision to Diff 431489. tchatow added a comment. Added tests for non-empty enums Repository: rG LLVM Github Monorepo CHANGES SINCE LAST ACTION https://reviews.llvm.org/D125848/new/ https://reviews.llvm.org/D125848 Files: clang/lib/Format/UnwrappedLineParser.cpp clang/unittests/Format/FormatTest.cpp Index: clang/unittests/Format/FormatTest.cpp === --- clang/unittests/Format/FormatTest.cpp +++ clang/unittests/Format/FormatTest.cpp @@ -3565,6 +3565,7 @@ 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::value\n" "};", @@ -3611,6 +3612,17 @@ "};", 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" @@ -3658,7 +3670,19 @@ 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) { @@ -3675,7 +3699,19 @@ 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) { Index: clang/lib/Format/UnwrappedLineParser.cpp === --- clang/lib/Format/UnwrappedLineParser.cpp +++ clang/lib/Format/UnwrappedLineParser.cpp @@ -3360,11 +3360,18 @@ 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 Index: clang/unittests/Format/FormatTest.cpp === --- clang/unittests/Format/FormatTest.cpp +++ clang/unittests/Format/FormatTest.cpp @@ -3565,6 +3565,7 @@ 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::value\n" "};", @@ -3611,6 +3612,17 @@ "};", 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" @@ -3658,7 +3670,19 @@ 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
[PATCH] D125848: [clang-format] Handle attributes in enum declaration.
tchatow added a comment. No, I'll need someone to commit it on my behalf. Repository: rG LLVM Github Monorepo CHANGES SINCE LAST ACTION https://reviews.llvm.org/D125848/new/ https://reviews.llvm.org/D125848 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits