https://github.com/carsonRadtke created https://github.com/llvm/llvm-project/pull/124122
Attribute locations for scoped enums should appear immediately after 'class' or 'struct'. Consider a user trying to add attributes to a scoped enum declaration: ```c++ [[attr]] enum class X { A }; ``` The attribute is in the incorrect location and clang ([before this change](https://godbolt.org/z/9GjfWc5Tf)) suggests putting it after 'enum'. This is incorrect for scoped enum as the attribute must following 'class'. After this change, the warning looks like: ``` $ ./bin/clang -c -std=c++20 ../repro.cpp ../repro.cpp:1:20: error: misplaced attributes; expected attributes here 1 | [[attr]] enum class E { A }; | ~~~~~~~~ ^ | [[attr]] 1 error generated. ``` >From 8e92e07c226bc78474254a5c7be0e1fd9fb69735 Mon Sep 17 00:00:00 2001 From: carson radtke <nosrac...@gmail.com> Date: Thu, 23 Jan 2025 08:30:33 -0600 Subject: [PATCH] [clang] improve suggested attribute location for scoped enums Attribute locations for scoped enums should appear immediately after 'class' or 'struct'. Consider a user trying to add attributes to a scoped enum declaration: ```c++ [[attr]] enum class X { A }; ``` The attribute is in the incorrect location and clang (before this change) suggests putting it after 'enum'. This is incorrect for scoped enum as the attribute must following 'class'. --- clang/lib/Parse/Parser.cpp | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/clang/lib/Parse/Parser.cpp b/clang/lib/Parse/Parser.cpp index 0710542f5e938e..4c5bcbd8df6bb5 100644 --- a/clang/lib/Parse/Parser.cpp +++ b/clang/lib/Parse/Parser.cpp @@ -1175,12 +1175,21 @@ Parser::DeclGroupPtrTy Parser::ParseDeclOrFunctionDefInternal( } }; - // Suggest correct location to fix '[[attrib]] struct' to 'struct [[attrib]]' + // Suggest correct location to fix '[[attrib]] struct' to 'struct [[attrib]]'... SourceLocation CorrectLocationForAttributes = DeclSpec::isDeclRep(DS.getTypeSpecType()) ? DS.getTypeSpecTypeLoc().getLocWithOffset( LengthOfTSTToken(DS.getTypeSpecType())) : SourceLocation(); + // ... suggested location should be after 'class/struct' for scoped enums + if (DS.getTypeSpecType() == DeclSpec::TST_enum) { + const EnumDecl *ED = cast<EnumDecl>(DS.getRepAsDecl()); + if (ED->isScoped()) { + // FIXME: offset is dependent on number of spaces after 'enum' + CorrectLocationForAttributes = + CorrectLocationForAttributes.getLocWithOffset(6); + } + } ProhibitAttributes(Attrs, CorrectLocationForAttributes); ConsumeToken(); RecordDecl *AnonRecord = nullptr; _______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits