From: Daniel Dunbar Date: Fri, 13 Feb 2009 19:23:53 +0000 (+0000) Subject: Sema/AST support for attribute used. Patch by Anders Johnson (with small tweaks ... X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=b805dad4aa386aeae0f72512895bd238678d37a5;p=clang 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 c951ba712e..09492dba66 100644 --- a/include/clang/AST/Attr.h +++ b/include/clang/AST/Attr.h @@ -50,6 +50,7 @@ public: TransparentUnion, Unavailable, Unused, + Used, Visibility, Weak, Blocks, @@ -266,6 +267,15 @@ public: 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 2295c30c90..c5b058202c 100644 --- a/include/clang/Parse/AttributeList.h +++ b/include/clang/Parse/AttributeList.h @@ -70,6 +70,7 @@ public: 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 7f18a2c325..56fd916607 100644 --- a/lib/Parse/AttributeList.cpp +++ b/lib/Parse/AttributeList.cpp @@ -56,6 +56,7 @@ AttributeList::Kind AttributeList::getKind(const IdentifierInfo *Name) { 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 491579c813..29f6cb18a0 100644 --- a/lib/Sema/SemaDeclAttr.cpp +++ b/lib/Sema/SemaDeclAttr.cpp @@ -431,6 +431,27 @@ static void HandleUnusedAttr(Decl *d, const AttributeList &Attr, Sema &S) { 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(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 @@ static void ProcessDeclAttribute(Decl *D, const AttributeList &Attr, Sema &S) { 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 0000000000..7d0265ef32 --- /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}} +} + +