[clang] [Clang] disallow use of attributes before extern template declarations (PR #136328)
Oleksandr T. via cfe-commits
cfe-commits at lists.llvm.org
Fri Apr 18 15:29:13 PDT 2025
https://github.com/a-tarasyuk updated https://github.com/llvm/llvm-project/pull/136328
>From 0729ae4d4a971905aefc8f1c1ec2ec81a0f149e9 Mon Sep 17 00:00:00 2001
From: Oleksandr Tarasiuk <oleksandr.tarasiuk at outlook.com>
Date: Fri, 18 Apr 2025 20:09:56 +0300
Subject: [PATCH 1/2] [Clang] disallow use of attributes before extern template
declarations
---
clang/docs/ReleaseNotes.rst | 3 +++
clang/lib/Parse/Parser.cpp | 1 +
clang/test/Parser/extern-template-attributes.cpp | 6 ++++++
3 files changed, 10 insertions(+)
create mode 100644 clang/test/Parser/extern-template-attributes.cpp
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}}
>From a6ad92ed6491b27fa6c30cd1c9aa8ed9bec13b94 Mon Sep 17 00:00:00 2001
From: Oleksandr Tarasiuk <oleksandr.tarasiuk at outlook.com>
Date: Sat, 19 Apr 2025 01:27:00 +0300
Subject: [PATCH 2/2] add more test cases
---
clang/lib/Parse/Parser.cpp | 1 +
clang/test/Parser/extern-template-attributes.cpp | 6 ++++--
2 files changed, 5 insertions(+), 2 deletions(-)
diff --git a/clang/lib/Parse/Parser.cpp b/clang/lib/Parse/Parser.cpp
index 25f8ed29d15fe..a82aea162716a 100644
--- a/clang/lib/Parse/Parser.cpp
+++ b/clang/lib/Parse/Parser.cpp
@@ -1050,6 +1050,7 @@ Parser::ParseExternalDeclaration(ParsedAttributes &Attrs,
case tok::kw_extern:
if (getLangOpts().CPlusPlus && NextToken().is(tok::kw_template)) {
ProhibitAttributes(Attrs);
+ ProhibitAttributes(DeclSpecAttrs);
// 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
index 0912ad69806db..1301f2f680ccb 100644
--- a/clang/test/Parser/extern-template-attributes.cpp
+++ b/clang/test/Parser/extern-template-attributes.cpp
@@ -1,6 +1,8 @@
-// RUN: %clang_cc1 -std=c++17 -verify %s
+// RUN: %clang_cc1 -std=c++17 -fms-extensions -verify %s
template <class>
struct S {};
-[[deprecated]] extern template struct S<int>; // expected-error {{an attribute list cannot appear here}}
+[[deprecated]] extern template struct S<int>; // expected-error {{an attribute list cannot appear here}}
+__attribute__((deprecated)) extern template struct S<int>; // expected-error {{an attribute list cannot appear here}}
+__declspec(deprecated) extern template struct S<int>; // expected-error {{expected unqualified-id}}
More information about the cfe-commits
mailing list