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

Rafael Espindola rafael.espindola at gmail.com
Thu Jan 5 20:54:01 PST 2012


Author: rafael
Date: Thu Jan  5 22:54:01 2012
New Revision: 147652

URL: http://llvm.org/viewvc/llvm-project?rev=147652&view=rev
Log:
Improvements to the uninitialized variable warning: Check if the constructor
call is elidable or if the constructor is trivial instead of checking if it
is user declared.

Modified:
    cfe/trunk/lib/Sema/SemaDecl.cpp
    cfe/trunk/test/SemaCXX/uninitialized.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=147652&r1=147651&r2=147652&view=diff
==============================================================================
--- cfe/trunk/lib/Sema/SemaDecl.cpp (original)
+++ cfe/trunk/lib/Sema/SemaDecl.cpp Thu Jan  5 22:54:01 2012
@@ -1107,7 +1107,7 @@
     return false;
 
   // Types of valid local variables should be complete, so this should succeed.
-  if (const ValueDecl *VD = dyn_cast<ValueDecl>(D)) {
+  if (const VarDecl *VD = dyn_cast<VarDecl>(D)) {
 
     // White-list anything with an __attribute__((unused)) type.
     QualType Ty = VD->getType();
@@ -1129,11 +1129,18 @@
         return false;
 
       if (const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(Tag)) {
-        // 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())
+        if (!RD->hasTrivialDestructor())
           return false;
+
+        if (const Expr *Init = VD->getInit()) {
+          const CXXConstructExpr *Construct =
+            dyn_cast<CXXConstructExpr>(Init);
+          if (Construct && !Construct->isElidable()) {
+            CXXConstructorDecl *CD = Construct->getConstructor();
+            if (!CD->isTrivial())
+              return false;
+          }
+        }
       }
     }
 

Modified: cfe/trunk/test/SemaCXX/uninitialized.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/SemaCXX/uninitialized.cpp?rev=147652&r1=147651&r2=147652&view=diff
==============================================================================
--- cfe/trunk/test/SemaCXX/uninitialized.cpp (original)
+++ cfe/trunk/test/SemaCXX/uninitialized.cpp Thu Jan  5 22:54:01 2012
@@ -41,6 +41,7 @@
     A(int x) {}
     A(int *x) {}
     A(A *a) {}
+    ~A();
 };
 
 A getA() { return A(); }

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=147652&r1=147651&r2=147652&view=diff
==============================================================================
--- cfe/trunk/test/SemaCXX/warn-unused-variables.cpp (original)
+++ cfe/trunk/test/SemaCXX/warn-unused-variables.cpp Thu Jan  5 22:54:01 2012
@@ -80,3 +80,45 @@
     f<char>(); // expected-note {{here}}
   }
 }
+
+namespace PR11550 {
+  struct S1 {
+    S1();
+  };
+  S1 makeS1();
+  void testS1(S1 a) {
+    // This constructor call can be elided.
+    S1 x = makeS1(); // expected-warning {{unused variable 'x'}}
+
+    // This one cannot, so no warning.
+    S1 y;
+
+    // This call cannot, but the constructor is trivial.
+    S1 z = a; // expected-warning {{unused variable 'z'}}
+  }
+
+  // The same is true even when we know thet constructor has side effects.
+  void foo();
+  struct S2 {
+    S2() {
+      foo();
+    }
+  };
+  S2 makeS2();
+  void testS2(S2 a) {
+    S2 x = makeS2(); // expected-warning {{unused variable 'x'}}
+    S2 y;
+    S2 z = a; // expected-warning {{unused variable 'z'}}
+  }
+
+  // Or when the constructor is not declared by the user.
+  struct S3 {
+    S1 m;
+  };
+  S3 makeS3();
+  void testS3(S3 a) {
+    S3 x = makeS3(); // expected-warning {{unused variable 'x'}}
+    S3 y;
+    S3 z = a; // expected-warning {{unused variable 'z'}}
+  }
+}





More information about the cfe-commits mailing list