[cfe-commits] r103362 - in /cfe/trunk: lib/Sema/Sema.h lib/Sema/SemaDecl.cpp lib/Sema/SemaTemplateInstantiateDecl.cpp test/SemaTemplate/unused-variables.cpp

Douglas Gregor dgregor at apple.com
Sat May 8 16:05:03 PDT 2010


Author: dgregor
Date: Sat May  8 18:05:03 2010
New Revision: 103362

URL: http://llvm.org/viewvc/llvm-project?rev=103362&view=rev
Log:
Improve our handling of the -Wunused-variable warning in templates. In
particular, don't complain about unused variables that have dependent
type until instantiation time, so that we can look at the type of the
variable. Moreover, only complain about unused variables that have
neither a user-declared constructor nor a non-trivial destructor.

Added:
    cfe/trunk/test/SemaTemplate/unused-variables.cpp   (with props)
Modified:
    cfe/trunk/lib/Sema/Sema.h
    cfe/trunk/lib/Sema/SemaDecl.cpp
    cfe/trunk/lib/Sema/SemaTemplateInstantiateDecl.cpp

Modified: cfe/trunk/lib/Sema/Sema.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Sema/Sema.h?rev=103362&r1=103361&r2=103362&view=diff
==============================================================================
--- cfe/trunk/lib/Sema/Sema.h (original)
+++ cfe/trunk/lib/Sema/Sema.h Sat May  8 18:05:03 2010
@@ -1761,7 +1761,8 @@
   /// DiagnoseUnusedExprResult - If the statement passed in is an expression
   /// whose result is unused, warn.
   void DiagnoseUnusedExprResult(const Stmt *S);
-
+  void DiagnoseUnusedDecl(const NamedDecl *ND);
+  
   ParsingDeclStackState PushParsingDeclaration();
   void PopParsingDeclaration(ParsingDeclStackState S, DeclPtrTy D);
   void EmitDeprecationWarning(NamedDecl *D, SourceLocation Loc);

Modified: cfe/trunk/lib/Sema/SemaDecl.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Sema/SemaDecl.cpp?rev=103362&r1=103361&r2=103362&view=diff
==============================================================================
--- cfe/trunk/lib/Sema/SemaDecl.cpp (original)
+++ cfe/trunk/lib/Sema/SemaDecl.cpp Sat May  8 18:05:03 2010
@@ -544,9 +544,9 @@
         return false;
     }
 
-    // If we failed to complete the type for some reason, don't
-    // diagnose the variable.
-    if (Ty->isIncompleteType())
+    // If we failed to complete the type for some reason, or if the type is
+    // dependent, don't diagnose the variable. 
+    if (Ty->isIncompleteType() || Ty->isDependentType())
       return false;
 
     if (const TagType *TT = Ty->getAs<TagType>()) {
@@ -555,9 +555,10 @@
         return false;
 
       if (const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(Tag)) {
-        if (!RD->hasTrivialConstructor())
-          return false;
-        if (!RD->hasTrivialDestructor())
+        // FIXME: Checking for the presence of a user-declared constructor
+        // isn't completely accurate; we'd prefer to check that the initializer
+        // has no side effects.
+        if (RD->hasUserDeclaredConstructor() || !RD->hasTrivialDestructor())
           return false;
       }
     }
@@ -568,6 +569,18 @@
   return true;
 }
 
+void Sema::DiagnoseUnusedDecl(const NamedDecl *D) {
+  if (!ShouldDiagnoseUnusedDecl(D))
+    return;
+  
+  if (isa<VarDecl>(D) && cast<VarDecl>(D)->isExceptionVariable())
+    Diag(D->getLocation(), diag::warn_unused_exception_param)
+    << D->getDeclName();
+  else
+    Diag(D->getLocation(), diag::warn_unused_variable) 
+    << D->getDeclName();
+}
+
 void Sema::ActOnPopScope(SourceLocation Loc, Scope *S) {
   if (S->decl_empty()) return;
   assert((S->getFlags() & (Scope::DeclScope | Scope::TemplateParamScope)) &&
@@ -584,15 +597,9 @@
     if (!D->getDeclName()) continue;
 
     // Diagnose unused variables in this scope.
-    if (ShouldDiagnoseUnusedDecl(D) && 
-        S->getNumErrorsAtStart() == getDiagnostics().getNumErrors()) {
-      if (isa<VarDecl>(D) && cast<VarDecl>(D)->isExceptionVariable())
-        Diag(D->getLocation(), diag::warn_unused_exception_param)
-          << D->getDeclName();
-      else
-        Diag(D->getLocation(), diag::warn_unused_variable) 
-          << D->getDeclName();
-    }
+    if (S->getNumErrorsAtStart() == getDiagnostics().getNumErrors())
+      DiagnoseUnusedDecl(D);
+    
     // Remove this name from our lexical scope.
     IdResolver.RemoveDecl(D);
   }

Modified: cfe/trunk/lib/Sema/SemaTemplateInstantiateDecl.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Sema/SemaTemplateInstantiateDecl.cpp?rev=103362&r1=103361&r2=103362&view=diff
==============================================================================
--- cfe/trunk/lib/Sema/SemaTemplateInstantiateDecl.cpp (original)
+++ cfe/trunk/lib/Sema/SemaTemplateInstantiateDecl.cpp Sat May  8 18:05:03 2010
@@ -351,7 +351,8 @@
     Var->setLexicalDeclContext(D->getLexicalDeclContext());
 
   Var->setAccess(D->getAccess());
-
+  Var->setUsed(D->isUsed());
+  
   // FIXME: In theory, we could have a previous declaration for variables that
   // are not static data members.
   bool Redeclaration = false;
@@ -419,6 +420,10 @@
   } else if (!Var->isStaticDataMember() || Var->isOutOfLine())
     SemaRef.ActOnUninitializedDecl(Sema::DeclPtrTy::make(Var), false);
 
+  // Diagnose unused local variables.
+  if (!Var->isInvalidDecl() && Owner->isFunctionOrMethod() && !Var->isUsed())
+    SemaRef.DiagnoseUnusedDecl(Var);
+  
   return Var;
 }
 

Added: cfe/trunk/test/SemaTemplate/unused-variables.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/SemaTemplate/unused-variables.cpp?rev=103362&view=auto
==============================================================================
--- cfe/trunk/test/SemaTemplate/unused-variables.cpp (added)
+++ cfe/trunk/test/SemaTemplate/unused-variables.cpp Sat May  8 18:05:03 2010
@@ -0,0 +1,21 @@
+// RUN: %clang_cc1 -fsyntax-only -Wunused -verify %s
+
+struct X0 {
+  ~X0();
+};
+
+struct X1 { };
+
+template<typename T>
+void f() {
+  X0 x0;
+  X1 x1; // expected-warning{{unused variable 'x1'}}
+}
+
+template<typename T, typename U>
+void g() {
+  T t;
+  U u; // expected-warning{{unused variable 'u'}}
+}
+
+template void g<X0, X1>(); // expected-note{{in instantiation of}}

Propchange: cfe/trunk/test/SemaTemplate/unused-variables.cpp
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: cfe/trunk/test/SemaTemplate/unused-variables.cpp
------------------------------------------------------------------------------
    svn:keywords = Id

Propchange: cfe/trunk/test/SemaTemplate/unused-variables.cpp
------------------------------------------------------------------------------
    svn:mime-type = text/plain





More information about the cfe-commits mailing list