diff options
author | Anders Carlsson <andersca@mac.com> | 2010-10-22 23:37:08 +0000 |
---|---|---|
committer | Anders Carlsson <andersca@mac.com> | 2010-10-22 23:37:08 +0000 |
commit | 2127eccbe15fd3b1b29aa53ccedd2e0f55ad27f9 (patch) | |
tree | f3308f8ee6379e9247351a366149282457eb9ccb | |
parent | 5b629aa86c987f276d00453b6c9ab8424f7903fe (diff) |
Warn if a variable marked with the "unused" attribute is used. Patch by Darin Adler!
git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@117184 91177308-0d34-0410-b5e6-96231b3b80d8
-rw-r--r-- | include/clang/Basic/DiagnosticSemaKinds.td | 4 | ||||
-rw-r--r-- | lib/Sema/SemaAttr.cpp | 4 | ||||
-rw-r--r-- | lib/Sema/SemaExpr.cpp | 6 | ||||
-rw-r--r-- | test/Analysis/rdar-6442306-1.m | 2 | ||||
-rw-r--r-- | test/Sema/attr-unused.c | 22 | ||||
-rw-r--r-- | test/Sema/pragma-unused.c | 25 |
6 files changed, 56 insertions, 7 deletions
diff --git a/include/clang/Basic/DiagnosticSemaKinds.td b/include/clang/Basic/DiagnosticSemaKinds.td index 90d61dbaa1..553769cbf4 100644 --- a/include/clang/Basic/DiagnosticSemaKinds.td +++ b/include/clang/Basic/DiagnosticSemaKinds.td @@ -108,7 +108,9 @@ def warn_unused_function : Warning<"unused function %0">, InGroup<UnusedFunction>, DefaultIgnore; def warn_unused_member_function : Warning<"unused member function %0">, InGroup<UnusedMemberFunction>, DefaultIgnore; - +def warn_used_but_marked_unused: Warning<"%0 was marked unused but was used">, + InGroup<Unused>; + def warn_implicit_function_decl : Warning< "implicit declaration of function %0">, InGroup<ImplicitFunctionDeclare>, DefaultIgnore; diff --git a/lib/Sema/SemaAttr.cpp b/lib/Sema/SemaAttr.cpp index 0921156b93..d7290c3089 100644 --- a/lib/Sema/SemaAttr.cpp +++ b/lib/Sema/SemaAttr.cpp @@ -288,6 +288,10 @@ void Sema::ActOnPragmaUnused(const Token *Identifiers, unsigned NumIdentifiers, continue; } + // Warn if this was used before being marked unused. + if (VD->isUsed()) + Diag(PragmaLoc, diag::warn_used_but_marked_unused) << Name; + VD->addAttr(::new (Context) UnusedAttr(Tok.getLocation(), Context)); } } diff --git a/lib/Sema/SemaExpr.cpp b/lib/Sema/SemaExpr.cpp index 587a76ed08..ced383310f 100644 --- a/lib/Sema/SemaExpr.cpp +++ b/lib/Sema/SemaExpr.cpp @@ -97,6 +97,10 @@ bool Sema::DiagnoseUseOfDecl(NamedDecl *D, SourceLocation Loc) { } } + // Warn if this is used but marked unused. + if (D->hasAttr<UnusedAttr>()) + Diag(Loc, diag::warn_used_but_marked_unused) << D->getDeclName(); + return false; } @@ -7804,7 +7808,7 @@ void Sema::MarkDeclarationReferenced(SourceLocation Loc, Decl *D) { // -Wunused-parameters) if (isa<ParmVarDecl>(D) || (isa<VarDecl>(D) && D->getDeclContext()->isFunctionOrMethod())) { - D->setUsed(true); + D->setUsed(); return; } diff --git a/test/Analysis/rdar-6442306-1.m b/test/Analysis/rdar-6442306-1.m index dfe9b722d1..0d35f23328 100644 --- a/test/Analysis/rdar-6442306-1.m +++ b/test/Analysis/rdar-6442306-1.m @@ -16,7 +16,7 @@ typedef struct { double __Foo_READSWAP__double(double*); static __inline__ bar_return_t -__Beeble_check__Request__SetPortalSize_t(__attribute__((__unused__)) __Request__SetPortalSize_t *In0P) { +__Beeble_check__Request__SetPortalSize_t(__Request__SetPortalSize_t *In0P) { if (In0P->Foo.int_rep != Foo_record.int_rep) { do { int __i__, __C__ = (2); diff --git a/test/Sema/attr-unused.c b/test/Sema/attr-unused.c index 28715141b9..795b083123 100644 --- a/test/Sema/attr-unused.c +++ b/test/Sema/attr-unused.c @@ -1,4 +1,4 @@ -// RUN: %clang_cc1 -verify -Wunused-variable -fsyntax-only %s +// RUN: %clang_cc1 -verify -Wunused -Wunused-parameter -Wunused -fsyntax-only %s static void (*fp0)(void) __attribute__((unused)); @@ -20,8 +20,24 @@ void test0() { int x; // expected-warning {{unused variable}} Int_not_unused i0; // expected-warning {{unused variable}} - Int_unused i1; + Int_unused i1; // expected-warning {{'Int_unused' was marked unused but was used}} struct Test0_not_unused s0; // expected-warning {{unused variable}} - struct Test0_unused s1; + struct Test0_unused s1; // expected-warning {{'Test0_unused' was marked unused but was used}} +} + +int f3(int x) { // expected-warning{{unused parameter 'x'}} + return 0; +} + +int f4(int x) { + return x; +} + +int f5(int x __attribute__((__unused__))) { + return 0; +} + +int f6(int x __attribute__((__unused__))) { + return x; // expected-warning{{'x' was marked unused but was used}} } diff --git a/test/Sema/pragma-unused.c b/test/Sema/pragma-unused.c index 8a051a3ec9..272f3a2f38 100644 --- a/test/Sema/pragma-unused.c +++ b/test/Sema/pragma-unused.c @@ -1,4 +1,4 @@ -// RUN: %clang_cc1 -fsyntax-only -verify %s +// RUN: %clang_cc1 -fsyntax-only -Wunused-parameter -verify %s void f1(void) { int x, y, z; @@ -41,3 +41,26 @@ void f7() { #pragma unused(undeclared, undefined, y) // expected-warning{{undeclared variable 'undeclared' used as an argument for '#pragma unused'}} expected-warning{{undeclared variable 'undefined' used as an argument for '#pragma unused'}} } +int f8(int x) { // expected-warning{{unused parameter 'x'}} + return 0; +} + +int f9(int x) { + return x; +} + +int f10(int x) { + #pragma unused(x) + return 0; +} + +int f11(int x) { + #pragma unused(x) + return x; // expected-warning{{'x' was marked unused but was used}} +} + +int f12(int x) { + int y = x; + #pragma unused(x) // expected-warning{{'x' was marked unused but was used}} + return y; +} |