[cfe-commits] r117184 - in /cfe/trunk: include/clang/Basic/DiagnosticSemaKinds.td lib/Sema/SemaAttr.cpp lib/Sema/SemaExpr.cpp test/Analysis/rdar-6442306-1.m test/Sema/attr-unused.c test/Sema/pragma-unused.c
Anders Carlsson
andersca at mac.com
Fri Oct 22 16:37:08 PDT 2010
Author: andersca
Date: Fri Oct 22 18:37:08 2010
New Revision: 117184
URL: http://llvm.org/viewvc/llvm-project?rev=117184&view=rev
Log:
Warn if a variable marked with the "unused" attribute is used. Patch by Darin Adler!
Modified:
cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.td
cfe/trunk/lib/Sema/SemaAttr.cpp
cfe/trunk/lib/Sema/SemaExpr.cpp
cfe/trunk/test/Analysis/rdar-6442306-1.m
cfe/trunk/test/Sema/attr-unused.c
cfe/trunk/test/Sema/pragma-unused.c
Modified: cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.td
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.td?rev=117184&r1=117183&r2=117184&view=diff
==============================================================================
--- cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.td (original)
+++ cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.td Fri Oct 22 18:37:08 2010
@@ -108,7 +108,9 @@
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;
Modified: cfe/trunk/lib/Sema/SemaAttr.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Sema/SemaAttr.cpp?rev=117184&r1=117183&r2=117184&view=diff
==============================================================================
--- cfe/trunk/lib/Sema/SemaAttr.cpp (original)
+++ cfe/trunk/lib/Sema/SemaAttr.cpp Fri Oct 22 18:37:08 2010
@@ -288,6 +288,10 @@
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));
}
}
Modified: cfe/trunk/lib/Sema/SemaExpr.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Sema/SemaExpr.cpp?rev=117184&r1=117183&r2=117184&view=diff
==============================================================================
--- cfe/trunk/lib/Sema/SemaExpr.cpp (original)
+++ cfe/trunk/lib/Sema/SemaExpr.cpp Fri Oct 22 18:37:08 2010
@@ -97,6 +97,10 @@
}
}
+ // 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 @@
// -Wunused-parameters)
if (isa<ParmVarDecl>(D) ||
(isa<VarDecl>(D) && D->getDeclContext()->isFunctionOrMethod())) {
- D->setUsed(true);
+ D->setUsed();
return;
}
Modified: cfe/trunk/test/Analysis/rdar-6442306-1.m
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/Analysis/rdar-6442306-1.m?rev=117184&r1=117183&r2=117184&view=diff
==============================================================================
--- cfe/trunk/test/Analysis/rdar-6442306-1.m (original)
+++ cfe/trunk/test/Analysis/rdar-6442306-1.m Fri Oct 22 18:37:08 2010
@@ -16,7 +16,7 @@
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);
Modified: cfe/trunk/test/Sema/attr-unused.c
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/Sema/attr-unused.c?rev=117184&r1=117183&r2=117184&view=diff
==============================================================================
--- cfe/trunk/test/Sema/attr-unused.c (original)
+++ cfe/trunk/test/Sema/attr-unused.c Fri Oct 22 18:37:08 2010
@@ -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 @@
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}}
}
Modified: cfe/trunk/test/Sema/pragma-unused.c
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/Sema/pragma-unused.c?rev=117184&r1=117183&r2=117184&view=diff
==============================================================================
--- cfe/trunk/test/Sema/pragma-unused.c (original)
+++ cfe/trunk/test/Sema/pragma-unused.c Fri Oct 22 18:37:08 2010
@@ -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 @@
#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;
+}
More information about the cfe-commits
mailing list