]> granicus.if.org Git - clang/commitdiff
Implement support for -Wunused-variable, from Oscar Bonilla!
authorDouglas Gregor <dgregor@apple.com>
Thu, 8 Oct 2009 21:35:42 +0000 (21:35 +0000)
committerDouglas Gregor <dgregor@apple.com>
Thu, 8 Oct 2009 21:35:42 +0000 (21:35 +0000)
git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@83577 91177308-0d34-0410-b5e6-96231b3b80d8

include/clang/Basic/DiagnosticSemaKinds.td
lib/Sema/SemaDecl.cpp
lib/Sema/SemaExpr.cpp
test/Sema/warn-unused-variables.c [new file with mode: 0644]
test/SemaCXX/warn-unused-variables.cpp [new file with mode: 0644]

index 0a267091f577550c1659100cd3515bfec9c21bce..9d39389f519b7b99dae1e660ce0c31d4419b6705 100644 (file)
@@ -77,6 +77,8 @@ def err_bad_variable_name : Error<
 def err_parameter_name_omitted : Error<"parameter name omitted">;
 def warn_unused_parameter : Warning<"unused parameter %0">,
   InGroup<UnusedParameter>, DefaultIgnore;
+def warn_unused_variable : Warning<"unused variable %0">,
+  InGroup<UnusedVariable>, DefaultIgnore;
 def warn_decl_in_param_list : Warning<
   "declaration of %0 will not be visible outside of this function">;
 
index fa6d623ae3d932e0bf5ab1ab4ec7240d51d9d544..c2a83cdc6f5bc8663ada7bfcaedc20fdbfc12842 100644 (file)
@@ -434,6 +434,12 @@ void Sema::ActOnPopScope(SourceLocation Loc, Scope *S) {
 
     if (!D->getDeclName()) continue;
 
+    // Diagnose unused variables in this scope.
+    if (!D->isUsed() && !D->hasAttr<UnusedAttr>() && isa<VarDecl>(D) && 
+        !isa<ParmVarDecl>(D) && !isa<ImplicitParamDecl>(D) && 
+        D->getDeclContext()->isFunctionOrMethod())
+           Diag(D->getLocation(), diag::warn_unused_variable) << D->getDeclName();
+    
     // Remove this name from our lexical scope.
     IdResolver.RemoveDecl(D);
   }
index c9525f39e5568f6c8830554f0e25253c1b10441d..c689a7ac2a5ea805c30c3bcf2d63c0313bf7398b 100644 (file)
@@ -6145,9 +6145,12 @@ void Sema::MarkDeclarationReferenced(SourceLocation Loc, Decl *D) {
   if (D->isUsed())
     return;
 
-  // Mark a parameter declaration "used", regardless of whether we're in a
-  // template or not.
-  if (isa<ParmVarDecl>(D))
+  // Mark a parameter or variable declaration "used", regardless of whether we're in a
+  // template or not. The reason for this is that unevaluated expressions
+  // (e.g. (void)sizeof()) constitute a use for warning purposes (-Wunused-variables and
+  // -Wunused-parameters)
+  if (isa<ParmVarDecl>(D) || 
+      (isa<VarDecl>(D) && D->getDeclContext()->isFunctionOrMethod()))
     D->setUsed(true);
 
   // Do not mark anything as "used" within a dependent context; wait for
diff --git a/test/Sema/warn-unused-variables.c b/test/Sema/warn-unused-variables.c
new file mode 100644 (file)
index 0000000..fd22543
--- /dev/null
@@ -0,0 +1,19 @@
+// RUN: clang-cc -fsyntax-only -Wunused-variable -verify %s
+
+struct s0 {
+       unsigned int    i;
+};
+
+int proto(int a, int b);
+
+void f0(void) {
+       int     a __attribute__((unused)),
+               b; // expected-warning{{unused}}
+       return;
+}
+
+void f1(void) {
+       int     i;
+       (void)sizeof(i);
+       return;
+}
diff --git a/test/SemaCXX/warn-unused-variables.cpp b/test/SemaCXX/warn-unused-variables.cpp
new file mode 100644 (file)
index 0000000..d8b9a00
--- /dev/null
@@ -0,0 +1,6 @@
+// RUN: clang -fsyntax-only -Wunused-variable -verify %s
+
+template<typename T> void f() {
+       T t;
+       t = 17;
+}