[cfe-commits] r99981 - in /cfe/trunk: lib/Sema/SemaDecl.cpp lib/Sema/SemaDeclAttr.cpp test/Sema/attr-unused.c
John McCall
rjmccall at apple.com
Tue Mar 30 19:47:45 PDT 2010
Author: rjmccall
Date: Tue Mar 30 21:47:45 2010
New Revision: 99981
URL: http://llvm.org/viewvc/llvm-project?rev=99981&view=rev
Log:
Support __attribute__((unused)) on types. This suddenly started firing
a lot for me on selfhosts, I dunno why.
Modified:
cfe/trunk/lib/Sema/SemaDecl.cpp
cfe/trunk/lib/Sema/SemaDeclAttr.cpp
cfe/trunk/test/Sema/attr-unused.c
Modified: cfe/trunk/lib/Sema/SemaDecl.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Sema/SemaDecl.cpp?rev=99981&r1=99980&r2=99981&view=diff
==============================================================================
--- cfe/trunk/lib/Sema/SemaDecl.cpp (original)
+++ cfe/trunk/lib/Sema/SemaDecl.cpp Tue Mar 30 21:47:45 2010
@@ -511,14 +511,30 @@
// Types of valid local variables should be complete, so this should succeed.
if (const ValueDecl *VD = dyn_cast<ValueDecl>(D)) {
- if (const RecordType *RT = VD->getType()->getAs<RecordType>()) {
- if (const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(RT->getDecl())) {
+
+ // White-list anything with an __attribute__((unused)) type.
+ QualType Ty = VD->getType();
+
+ // Only look at the outermost level of typedef.
+ if (const TypedefType *TT = dyn_cast<TypedefType>(Ty)) {
+ if (TT->getDecl()->hasAttr<UnusedAttr>())
+ return false;
+ }
+
+ if (const TagType *TT = Ty->getAs<TagType>()) {
+ const TagDecl *Tag = TT->getDecl();
+ if (Tag->hasAttr<UnusedAttr>())
+ return false;
+
+ if (const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(Tag)) {
if (!RD->hasTrivialConstructor())
return false;
if (!RD->hasTrivialDestructor())
return false;
}
}
+
+ // TODO: __attribute__((unused)) templates?
}
return true;
Modified: cfe/trunk/lib/Sema/SemaDeclAttr.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Sema/SemaDeclAttr.cpp?rev=99981&r1=99980&r2=99981&view=diff
==============================================================================
--- cfe/trunk/lib/Sema/SemaDeclAttr.cpp (original)
+++ cfe/trunk/lib/Sema/SemaDeclAttr.cpp Tue Mar 30 21:47:45 2010
@@ -524,7 +524,8 @@
return;
}
- if (!isa<VarDecl>(d) && !isa<ObjCIvarDecl>(d) && !isFunctionOrMethod(d)) {
+ if (!isa<VarDecl>(d) && !isa<ObjCIvarDecl>(d) && !isFunctionOrMethod(d) &&
+ !isa<TypeDecl>(d)) {
S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
<< Attr.getName() << 2 /*variable and function*/;
return;
Modified: cfe/trunk/test/Sema/attr-unused.c
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/Sema/attr-unused.c?rev=99981&r1=99980&r2=99981&view=diff
==============================================================================
--- cfe/trunk/test/Sema/attr-unused.c (original)
+++ cfe/trunk/test/Sema/attr-unused.c Tue Mar 30 21:47:45 2010
@@ -1,4 +1,4 @@
-// RUN: %clang_cc1 -verify -fsyntax-only %s
+// RUN: %clang_cc1 -verify -Wunused-variable -fsyntax-only %s
static void (*fp0)(void) __attribute__((unused));
@@ -10,3 +10,18 @@
int g0 __attribute__((unused));
int f2() __attribute__((unused(1, 2))); // expected-error {{attribute requires 0 argument(s)}}
+
+struct Test0_unused {} __attribute__((unused));
+struct Test0_not_unused {};
+typedef int Int_unused __attribute__((unused));
+typedef int Int_not_unused;
+
+void test0() {
+ int x; // expected-warning {{unused variable}}
+
+ Int_not_unused i0; // expected-warning {{unused variable}}
+ Int_unused i1;
+
+ struct Test0_not_unused s0; // expected-warning {{unused variable}}
+ struct Test0_unused s1;
+}
More information about the cfe-commits
mailing list