]> granicus.if.org Git - clang/commitdiff
Don't treat variables with non-trivial ctors or dtors as unused. Fixes PR5407.
authorAnders Carlsson <andersca@mac.com>
Sat, 7 Nov 2009 07:26:56 +0000 (07:26 +0000)
committerAnders Carlsson <andersca@mac.com>
Sat, 7 Nov 2009 07:26:56 +0000 (07:26 +0000)
git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@86352 91177308-0d34-0410-b5e6-96231b3b80d8

lib/Sema/SemaDecl.cpp
test/SemaCXX/warn-unused-variables.cpp

index df7be1022353f5c0df9f7acfbb889777764d0cb6..8e1a19cbe5c9d3e0c55cb19071e7cc81e3a5a9ba 100644 (file)
@@ -385,8 +385,22 @@ bool Sema::isDeclInScope(NamedDecl *&D, DeclContext *Ctx, Scope *S) {
 }
 
 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());
 }
 
index d8b9a00ad6deb4a3074875ce304bfe1c9612a471..001048910ce9236df979f1eb48419746c11e5fc5 100644 (file)
@@ -4,3 +4,11 @@ template<typename T> void f() {
        T t;
        t = 17;
 }
+
+struct A { A(); };
+struct B { ~B(); };
+
+void f() {
+  A a;
+  B b;
+}
\ No newline at end of file