Sema/AST support for attribute used. Patch by Anders Johnson (with small tweaks & test case)!
git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@64478 91177308-0d34-0410-b5e6-96231b3b80d8
diff --git a/include/clang/AST/Attr.h b/include/clang/AST/Attr.h
index c951ba7..09492db 100644
--- a/include/clang/AST/Attr.h
+++ b/include/clang/AST/Attr.h
@@ -50,6 +50,7 @@
TransparentUnion,
Unavailable,
Unused,
+ Used,
Visibility,
Weak,
Blocks,
@@ -266,6 +267,15 @@
static bool classof(const UnusedAttr *A) { return true; }
};
+class UsedAttr : public Attr {
+public:
+ UsedAttr() : Attr(Used) {}
+
+ // Implement isa/cast/dyncast/etc.
+ static bool classof(const Attr *A) { return A->getKind() == Used; }
+ static bool classof(const UsedAttr *A) { return true; }
+};
+
class WeakAttr : public Attr {
public:
WeakAttr() : Attr(Weak) {}
diff --git a/include/clang/Parse/AttributeList.h b/include/clang/Parse/AttributeList.h
index 2295c30..c5b0582 100644
--- a/include/clang/Parse/AttributeList.h
+++ b/include/clang/Parse/AttributeList.h
@@ -70,6 +70,7 @@
AT_transparent_union,
AT_unavailable,
AT_unused,
+ AT_used,
AT_vector_size,
AT_visibility,
AT_warn_unused_result,
diff --git a/lib/Parse/AttributeList.cpp b/lib/Parse/AttributeList.cpp
index 7f18a2c..56fd916 100644
--- a/lib/Parse/AttributeList.cpp
+++ b/lib/Parse/AttributeList.cpp
@@ -56,6 +56,7 @@
if (!memcmp(Str, "weak", 4)) return AT_weak;
if (!memcmp(Str, "pure", 4)) return AT_pure;
if (!memcmp(Str, "mode", 4)) return AT_mode;
+ if (!memcmp(Str, "used", 4)) return AT_used;
break;
case 5:
if (!memcmp(Str, "alias", 5)) return AT_alias;
diff --git a/lib/Sema/SemaDeclAttr.cpp b/lib/Sema/SemaDeclAttr.cpp
index 491579c..29f6cb1 100644
--- a/lib/Sema/SemaDeclAttr.cpp
+++ b/lib/Sema/SemaDeclAttr.cpp
@@ -431,6 +431,27 @@
d->addAttr(new UnusedAttr());
}
+static void HandleUsedAttr(Decl *d, const AttributeList &Attr, Sema &S) {
+ // check the attribute arguments.
+ if (Attr.getNumArgs() != 0) {
+ S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
+ return;
+ }
+
+ if (const VarDecl *VD = dyn_cast<VarDecl>(d)) {
+ if (VD->hasLocalStorage()) {
+ S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "used";
+ return;
+ }
+ } else if (!isFunctionOrMethod(d)) {
+ S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
+ << "used" << "variable and function";
+ return;
+ }
+
+ d->addAttr(new UsedAttr());
+}
+
static void HandleConstructorAttr(Decl *d, const AttributeList &Attr, Sema &S) {
// check the attribute arguments.
if (Attr.getNumArgs() != 0 && Attr.getNumArgs() != 1) {
@@ -1357,6 +1378,7 @@
case AttributeList::AT_stdcall: HandleStdCallAttr (D, Attr, S); break;
case AttributeList::AT_unavailable: HandleUnavailableAttr(D, Attr, S); break;
case AttributeList::AT_unused: HandleUnusedAttr (D, Attr, S); break;
+ case AttributeList::AT_used: HandleUsedAttr (D, Attr, S); break;
case AttributeList::AT_vector_size: HandleVectorSizeAttr(D, Attr, S); break;
case AttributeList::AT_visibility: HandleVisibilityAttr(D, Attr, S); break;
case AttributeList::AT_weak: HandleWeakAttr (D, Attr, S); break;
diff --git a/test/Sema/attr-used.c b/test/Sema/attr-used.c
new file mode 100644
index 0000000..7d0265e
--- /dev/null
+++ b/test/Sema/attr-used.c
@@ -0,0 +1,17 @@
+// RUN: clang -verify -fsyntax-only %s
+
+struct __attribute__((used)) s { // expected-warning {{'used' attribute only applies to variable and function types}}
+ int x;
+};
+
+int a __attribute__((used));
+
+static void __attribute__((used)) f0(void) {
+}
+
+void f1() {
+ static int a __attribute__((used));
+ int b __attribute__((used)); // expected-warning {{used attribute ignored}}
+}
+
+