[cfe-commits] r83577 - in /cfe/trunk: include/clang/Basic/DiagnosticSemaKinds.td lib/Sema/SemaDecl.cpp lib/Sema/SemaExpr.cpp test/Sema/warn-unused-variables.c test/SemaCXX/warn-unused-variables.cpp

Douglas Gregor dgregor at apple.com
Thu Oct 8 14:35:42 PDT 2009


Author: dgregor
Date: Thu Oct  8 16:35:42 2009
New Revision: 83577

URL: http://llvm.org/viewvc/llvm-project?rev=83577&view=rev
Log:
Implement support for -Wunused-variable, from Oscar Bonilla!

Added:
    cfe/trunk/test/Sema/warn-unused-variables.c
    cfe/trunk/test/SemaCXX/warn-unused-variables.cpp
Modified:
    cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.td
    cfe/trunk/lib/Sema/SemaDecl.cpp
    cfe/trunk/lib/Sema/SemaExpr.cpp

Modified: cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.td
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.td?rev=83577&r1=83576&r2=83577&view=diff

==============================================================================
--- cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.td (original)
+++ cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.td Thu Oct  8 16:35:42 2009
@@ -77,6 +77,8 @@
 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">;
 

Modified: cfe/trunk/lib/Sema/SemaDecl.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Sema/SemaDecl.cpp?rev=83577&r1=83576&r2=83577&view=diff

==============================================================================
--- cfe/trunk/lib/Sema/SemaDecl.cpp (original)
+++ cfe/trunk/lib/Sema/SemaDecl.cpp Thu Oct  8 16:35:42 2009
@@ -434,6 +434,12 @@
 
     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);
   }

Modified: cfe/trunk/lib/Sema/SemaExpr.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Sema/SemaExpr.cpp?rev=83577&r1=83576&r2=83577&view=diff

==============================================================================
--- cfe/trunk/lib/Sema/SemaExpr.cpp (original)
+++ cfe/trunk/lib/Sema/SemaExpr.cpp Thu Oct  8 16:35:42 2009
@@ -6145,9 +6145,12 @@
   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

Added: cfe/trunk/test/Sema/warn-unused-variables.c
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/Sema/warn-unused-variables.c?rev=83577&view=auto

==============================================================================
--- cfe/trunk/test/Sema/warn-unused-variables.c (added)
+++ cfe/trunk/test/Sema/warn-unused-variables.c Thu Oct  8 16:35:42 2009
@@ -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;
+}

Added: cfe/trunk/test/SemaCXX/warn-unused-variables.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/SemaCXX/warn-unused-variables.cpp?rev=83577&view=auto

==============================================================================
--- cfe/trunk/test/SemaCXX/warn-unused-variables.cpp (added)
+++ cfe/trunk/test/SemaCXX/warn-unused-variables.cpp Thu Oct  8 16:35:42 2009
@@ -0,0 +1,6 @@
+// RUN: clang -fsyntax-only -Wunused-variable -verify %s
+
+template<typename T> void f() {
+	T t;
+	t = 17;
+}





More information about the cfe-commits mailing list