From: Ted Kremenek Date: Fri, 25 Jul 2008 04:39:19 +0000 (+0000) Subject: Implemented Sema support for attribute "unused". X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=73798892751e378cbcdef43579c1d41685091fd0;p=clang Implemented Sema support for attribute "unused". git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@54008 91177308-0d34-0410-b5e6-96231b3b80d8 --- diff --git a/include/clang/AST/Attr.h b/include/clang/AST/Attr.h index dd6f28f85e..0ae0acb878 100644 --- a/include/clang/AST/Attr.h +++ b/include/clang/AST/Attr.h @@ -27,22 +27,22 @@ public: enum Kind { Alias, Aligned, - Packed, Annotate, - NonNull, - NoReturn, Deprecated, - Weak, DLLImport, DLLExport, - NoThrow, + FastCall, Format, - Visibility, - FastCall, + IBOutletKind, // Clang-specific. Use "Kind" suffix to not conflict with + NonNull, + NoReturn, + NoThrow, + Packed, StdCall, TransparentUnion, - IBOutletKind // Clang-specific. Use "Kind" suffix to not conflict with - // the IBOutlet macro. + Unused, + Visibility, + Weak }; private: @@ -157,6 +157,15 @@ public: static bool classof(const DeprecatedAttr *A) { return true; } }; +class UnusedAttr : public Attr { +public: + UnusedAttr() : Attr(Unused) {} + + // Implement isa/cast/dyncast/etc. + static bool classof(const Attr *A) { return A->getKind() == Unused; } + static bool classof(const UnusedAttr *A) { return true; } +}; + class WeakAttr : public Attr { public: WeakAttr() : Attr(Weak) {} diff --git a/lib/Sema/SemaDeclAttr.cpp b/lib/Sema/SemaDeclAttr.cpp index 04e3f0557b..9a38f6cf35 100644 --- a/lib/Sema/SemaDeclAttr.cpp +++ b/lib/Sema/SemaDeclAttr.cpp @@ -339,6 +339,25 @@ static void HandleNoReturnAttr(Decl *d, const AttributeList &Attr, Sema &S) { d->addAttr(new NoReturnAttr()); } +static void HandleUnusedAttr(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, + std::string("0")); + return; + } + + VarDecl *VD = dyn_cast(d); + + if (!VD) { + S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type, + "unused", "variable"); + return; + } + + d->addAttr(new UnusedAttr()); +} + static void HandleDeprecatedAttr(Decl *d, const AttributeList &Attr, Sema &S) { // check the attribute arguments. if (Attr.getNumArgs() != 0) { @@ -826,6 +845,7 @@ static void ProcessDeclAttribute(Decl *D, const AttributeList &Attr, Sema &S) { case AttributeList::AT_format: HandleFormatAttr (D, Attr, S); break; case AttributeList::AT_IBOutlet: HandleIBOutletAttr (D, Attr, S); break; case AttributeList::AT_nonnull: HandleNonNullAttr (D, Attr, S); break; + case AttributeList::AT_unused: HandleUnusedAttr (D, Attr, S); break; case AttributeList::AT_transparent_union: HandleTransparentUnionAttr(D, Attr, S); break;