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
diff --git a/include/clang/Basic/DiagnosticSemaKinds.td b/include/clang/Basic/DiagnosticSemaKinds.td
index 90d61db..553769c 100644
--- a/include/clang/Basic/DiagnosticSemaKinds.td
+++ b/include/clang/Basic/DiagnosticSemaKinds.td
@@ -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;
diff --git a/lib/Sema/SemaAttr.cpp b/lib/Sema/SemaAttr.cpp
index 0921156..d7290c3 100644
--- a/lib/Sema/SemaAttr.cpp
+++ b/lib/Sema/SemaAttr.cpp
@@ -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));
   }
 }
diff --git a/lib/Sema/SemaExpr.cpp b/lib/Sema/SemaExpr.cpp
index 587a76e..ced3833 100644
--- a/lib/Sema/SemaExpr.cpp
+++ b/lib/Sema/SemaExpr.cpp
@@ -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;
   }
 
diff --git a/test/Analysis/rdar-6442306-1.m b/test/Analysis/rdar-6442306-1.m
index dfe9b72..0d35f23 100644
--- a/test/Analysis/rdar-6442306-1.m
+++ b/test/Analysis/rdar-6442306-1.m
@@ -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);
diff --git a/test/Sema/attr-unused.c b/test/Sema/attr-unused.c
index 2871514..795b083 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 @@
   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 8a051a3..272f3a2 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 @@
   #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;
+}