llvmbot wrote:

<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-clang

Author: Carson Radtke (carsonRadtke)

<details>
<summary>Changes</summary>

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.
```

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


1 Files Affected:

- (modified) clang/lib/Parse/Parser.cpp (+10-1) 


``````````diff
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;

``````````

</details>


https://github.com/llvm/llvm-project/pull/124122
_______________________________________________
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to