Skip to content

Conversation

@a-tarasyuk
Copy link
Member

Fixes #79893


This PR addresses the issue of attributes being incorrectly allowed on extern template declarations

[[deprecated]] extern template struct S<int>;

@llvmbot llvmbot added clang Clang issues not falling into any other category clang:frontend Language frontend issues, e.g. anything involving "Sema" labels Apr 18, 2025
@llvmbot
Copy link
Member

llvmbot commented Apr 18, 2025

@llvm/pr-subscribers-clang

Author: Oleksandr T. (a-tarasyuk)

Changes

Fixes #79893


This PR addresses the issue of attributes being incorrectly allowed on extern template declarations

[[deprecated]] extern template struct S&lt;int&gt;;

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

3 Files Affected:

  • (modified) clang/docs/ReleaseNotes.rst (+3)
  • (modified) clang/lib/Parse/Parser.cpp (+1)
  • (added) clang/test/Parser/extern-template-attributes.cpp (+6)
diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst
index f5cd1fbeabcfe..cf0ec3e0bbf29 100644
--- a/clang/docs/ReleaseNotes.rst
+++ b/clang/docs/ReleaseNotes.rst
@@ -301,6 +301,9 @@ related warnings within the method body.
   particularly relevant for AMDGPU targets, where they map to corresponding IR
   metadata.
 
+- Clang now disallows the use of attributes applied before an
+  ``extern template`` declaration (#GH79893).
+
 Improvements to Clang's diagnostics
 -----------------------------------
 
diff --git a/clang/lib/Parse/Parser.cpp b/clang/lib/Parse/Parser.cpp
index d528664bca352..25f8ed29d15fe 100644
--- a/clang/lib/Parse/Parser.cpp
+++ b/clang/lib/Parse/Parser.cpp
@@ -1049,6 +1049,7 @@ Parser::ParseExternalDeclaration(ParsedAttributes &Attrs,
 
   case tok::kw_extern:
     if (getLangOpts().CPlusPlus && NextToken().is(tok::kw_template)) {
+      ProhibitAttributes(Attrs);
       // Extern templates
       SourceLocation ExternLoc = ConsumeToken();
       SourceLocation TemplateLoc = ConsumeToken();
diff --git a/clang/test/Parser/extern-template-attributes.cpp b/clang/test/Parser/extern-template-attributes.cpp
new file mode 100644
index 0000000000000..0912ad69806db
--- /dev/null
+++ b/clang/test/Parser/extern-template-attributes.cpp
@@ -0,0 +1,6 @@
+// RUN: %clang_cc1 -std=c++17 -verify %s
+
+template <class>
+struct S {};
+
+[[deprecated]] extern template struct S<int>; // expected-error {{an attribute list cannot appear here}}

Copy link
Collaborator

@erichkeane erichkeane left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we're ok with this now, LGTM.

@a-tarasyuk a-tarasyuk merged commit 08b0c25 into llvm:main Apr 21, 2025
12 checks passed
IanWood1 pushed a commit to IanWood1/llvm-project that referenced this pull request May 6, 2025
llvm#136328)

Fixes llvm#79893 


--- 

This PR addresses the issue of _attributes_ being incorrectly allowed on
`extern template` declarations

```cpp
[[deprecated]] extern template struct S<int>;
```
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

clang:frontend Language frontend issues, e.g. anything involving "Sema" clang Clang issues not falling into any other category

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Clang] No diagnostic for attribute in front of extern declarations

4 participants