1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
|
>From 092a55679cf2a27b286118c8e425248a0c52dc90 Mon Sep 17 00:00:00 2001
From: Stephan Bergmann <sbergman@redhat.com>
Date: Wed, 9 Dec 2015 11:33:35 +0100
Subject: [PATCH 2/2] Fix handling of abi_tag attribute on namespaces
...to match GCC behavior:
* Forbid the attribute on unnamed namespaces. (GCC merely produces a warning
instead of an error for unnamed or non-inline namespaces, though.)
* When no tags are given for a (named, inline) namespace, use the namespace's
name as tag.
---
include/clang/Basic/DiagnosticSemaKinds.td | 2 ++
lib/Sema/SemaDeclAttr.cpp | 24 ++++++++++++++++--------
test/SemaCXX/attr-abi-tag-syntax.cpp | 20 ++++++++++++++++++++
test/SemaCXX/attr-abi-tag.cpp | 13 +++++++++++++
4 files changed, 51 insertions(+), 8 deletions(-)
create mode 100644 test/SemaCXX/attr-abi-tag-syntax.cpp
create mode 100644 test/SemaCXX/attr-abi-tag.cpp
diff --git a/include/clang/Basic/DiagnosticSemaKinds.td b/include/clang/Basic/DiagnosticSemaKinds.td
index c1931f6..2bda8c0 100644
--- a/include/clang/Basic/DiagnosticSemaKinds.td
+++ b/include/clang/Basic/DiagnosticSemaKinds.td
@@ -4137,6 +4137,8 @@ def err_redefinition_extern_inline : Error<
"%select{C99 mode|C++}1">;
def err_attr_abi_tag_only_on_inline_namespace :
Error<"abi_tag attribute only allowed on inline namespaces">;
+def err_attr_abi_tag_only_on_named_namespace :
+ Error<"abi_tag attribute only allowed on named namespaces">;
def err_abi_tag_on_redeclaration :
Error<"cannot add abi_tag attribute in redeclaration">;
def err_new_abi_tag_on_redeclaration :
diff --git a/lib/Sema/SemaDeclAttr.cpp b/lib/Sema/SemaDeclAttr.cpp
index e3bc97b..0fc1384 100644
--- a/lib/Sema/SemaDeclAttr.cpp
+++ b/lib/Sema/SemaDeclAttr.cpp
@@ -4423,7 +4423,9 @@ static void handleDeclspecThreadAttr(Sema &S, Decl *D,
static void handleAbiTagAttr(Sema &S, Decl *D,
const AttributeList &Attr) {
- if (!checkAttributeAtLeastNumArgs(S, Attr, 1))
+ const auto *NS = dyn_cast<NamespaceDecl>(D);
+
+ if (!checkAttributeAtLeastNumArgs(S, Attr, NS ? 0 : 1))
return;
SmallVector<std::string, 4> Tags;
@@ -4436,17 +4438,23 @@ static void handleAbiTagAttr(Sema &S, Decl *D,
Tags.push_back(Tag);
}
+
+ if (NS && !NS->isInline()) {
+ S.Diag(Attr.getLoc(), diag::err_attr_abi_tag_only_on_inline_namespace);
+ return;
+ }
+ if (NS && NS->isAnonymousNamespace()) {
+ S.Diag(Attr.getLoc(), diag::err_attr_abi_tag_only_on_named_namespace);
+ return;
+ }
+ if (NS && Attr.getNumArgs() == 0) {
+ Tags.push_back(NS->getName());
+ }
+
// store tags sorted and without duplicates
std::sort(Tags.begin(), Tags.end());
Tags.erase(std::unique(Tags.begin(), Tags.end()), Tags.end());
- if (const auto *NS = dyn_cast<NamespaceDecl>(D)) {
- if (!NS->isInline()) {
- S.Diag(Attr.getLoc(), diag::err_attr_abi_tag_only_on_inline_namespace);
- return;
- }
- }
-
const auto *CD = D->getCanonicalDecl();
if (CD != D) {
// redeclarations must not add new abi tags, or abi tags in the first place
diff --git a/test/SemaCXX/attr-abi-tag-syntax.cpp b/test/SemaCXX/attr-abi-tag-syntax.cpp
new file mode 100644
index 0000000..31d900d
--- /dev/null
+++ b/test/SemaCXX/attr-abi-tag-syntax.cpp
@@ -0,0 +1,20 @@
+// RUN: %clang_cc1 -std=c++11 -fsyntax-only -verify %s
+
+namespace N1 {
+
+namespace __attribute__((__abi_tag__)) {} // \
+ // expected-error {{abi_tag attribute only allowed on inline namespaces}}
+
+namespace N __attribute__((__abi_tag__)) {} // \
+ // expected-error {{abi_tag attribute only allowed on inline namespaces}}
+
+}
+
+namespace N2 {
+
+inline namespace __attribute__((__abi_tag__)) {} // \
+ // expected-error {{abi_tag attribute only allowed on named namespaces}}
+
+inline namespace N __attribute__((__abi_tag__)) {}
+
+}
diff --git a/test/SemaCXX/attr-abi-tag.cpp b/test/SemaCXX/attr-abi-tag.cpp
new file mode 100644
index 0000000..098ce86
--- /dev/null
+++ b/test/SemaCXX/attr-abi-tag.cpp
@@ -0,0 +1,13 @@
+// RUN: %clang_cc1 -x c++ -std=c++11 -triple x86_64-unknown-linux -emit-llvm < %s | FileCheck %s
+
+// CHECK: @_Z5Func1B6Names1v()
+inline namespace Names1 __attribute__((__abi_tag__)) {
+ class C1 {};
+}
+C1 Func1() { return C1(); }
+
+// CHECK: @_Z5Func2B4Tag1B4Tag2v()
+inline namespace Names2 __attribute__((__abi_tag__("Tag1", "Tag2"))) {
+ class C2 {};
+}
+C2 Func2() { return C2(); }
--
2.5.0
|