[cfe-commits] r86352 - in /cfe/trunk: lib/Sema/SemaDecl.cpp test/SemaCXX/warn-unused-variables.cpp

Anders Carlsson andersca at mac.com
Fri Nov 6 23:26:56 PST 2009


Author: andersca
Date: Sat Nov  7 01:26:56 2009
New Revision: 86352

URL: http://llvm.org/viewvc/llvm-project?rev=86352&view=rev
Log:
Don't treat variables with non-trivial ctors or dtors as unused. Fixes PR5407.

Modified:
    cfe/trunk/lib/Sema/SemaDecl.cpp
    cfe/trunk/test/SemaCXX/warn-unused-variables.cpp

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

==============================================================================
--- cfe/trunk/lib/Sema/SemaDecl.cpp (original)
+++ cfe/trunk/lib/Sema/SemaDecl.cpp Sat Nov  7 01:26:56 2009
@@ -385,8 +385,22 @@
 }
 
 static bool ShouldDiagnoseUnusedDecl(const NamedDecl *D) {
-  return (!D->isUsed() && !D->hasAttr<UnusedAttr>() && isa<VarDecl>(D) && 
-          !isa<ParmVarDecl>(D) && !isa<ImplicitParamDecl>(D) && 
+  if (D->isUsed() || D->hasAttr<UnusedAttr>())
+    return false;
+  
+  if (const ValueDecl *VD = dyn_cast<ValueDecl>(D)) {
+    if (const RecordType *RT = VD->getType()->getAs<RecordType>()) {
+      if (const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(RT->getDecl())) {
+        if (!RD->hasTrivialConstructor())
+          return false;
+        if (!RD->hasTrivialDestructor())
+          return false;
+      }
+    }
+  }
+  
+  return (isa<VarDecl>(D) && !isa<ParmVarDecl>(D) && 
+          !isa<ImplicitParamDecl>(D) && 
           D->getDeclContext()->isFunctionOrMethod());
 }
 

Modified: cfe/trunk/test/SemaCXX/warn-unused-variables.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/SemaCXX/warn-unused-variables.cpp?rev=86352&r1=86351&r2=86352&view=diff

==============================================================================
--- cfe/trunk/test/SemaCXX/warn-unused-variables.cpp (original)
+++ cfe/trunk/test/SemaCXX/warn-unused-variables.cpp Sat Nov  7 01:26:56 2009
@@ -4,3 +4,11 @@
 	T t;
 	t = 17;
 }
+
+struct A { A(); };
+struct B { ~B(); };
+
+void f() {
+  A a;
+  B b;
+}
\ No newline at end of file





More information about the cfe-commits mailing list