Sema/AST support for attribute used. Patch by Anders Johnson (with small tweaks & test case)!

llvm-svn: 64478
This commit is contained in:
Daniel Dunbar 2009-02-13 19:23:53 +00:00
parent 29942a349c
commit fee07a0b47
5 changed files with 51 additions and 0 deletions

View File

@ -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) {}

View File

@ -70,6 +70,7 @@ public:
AT_transparent_union,
AT_unavailable,
AT_unused,
AT_used,
AT_vector_size,
AT_visibility,
AT_warn_unused_result,

View File

@ -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;

View File

@ -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<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 @@ 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;

View File

@ -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}}
}