[cfe-commits] r136939 - in /cfe/trunk: lib/Analysis/UninitializedValues.cpp test/SemaCXX/uninit-variables.cpp

Ted Kremenek kremenek at apple.com
Thu Aug 4 15:40:58 PDT 2011


Author: kremenek
Date: Thu Aug  4 17:40:57 2011
New Revision: 136939

URL: http://llvm.org/viewvc/llvm-project?rev=136939&view=rev
Log:
Fix assertion failure in -Wuninitialized involving no-op casts.  Fixes PR 10577.

Modified:
    cfe/trunk/lib/Analysis/UninitializedValues.cpp
    cfe/trunk/test/SemaCXX/uninit-variables.cpp

Modified: cfe/trunk/lib/Analysis/UninitializedValues.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Analysis/UninitializedValues.cpp?rev=136939&r1=136938&r2=136939&view=diff
==============================================================================
--- cfe/trunk/lib/Analysis/UninitializedValues.cpp (original)
+++ cfe/trunk/lib/Analysis/UninitializedValues.cpp Thu Aug  4 17:40:57 2011
@@ -352,13 +352,19 @@
   /// possible to either silence the warning in some cases, or we
   /// propagate the uninitialized value.
   CastExpr *lastLoad;
+  
+  /// For some expressions, we want to ignore any post-processing after
+  /// visitation.
+  bool skipProcessUses;
+  
 public:
   TransferFunctions(CFGBlockValues &vals, const CFG &cfg,
                     AnalysisContext &ac,
                     UninitVariablesHandler *handler,
                     bool flagBlockUses)
     : vals(vals), cfg(cfg), ac(ac), handler(handler),
-      flagBlockUses(flagBlockUses), lastDR(0), lastLoad(0) {}
+      flagBlockUses(flagBlockUses), lastDR(0), lastLoad(0),
+      skipProcessUses(false) {}
   
   const CFG &getCFG() { return cfg; }
   void reportUninit(const DeclRefExpr *ex, const VarDecl *vd,
@@ -464,8 +470,9 @@
           // appropriately, but we need to continue to analyze subsequent uses
           // of the variable.
           if (init == lastLoad) {
-            DeclRefExpr *DR =
-              cast<DeclRefExpr>(lastLoad->getSubExpr()->IgnoreParens());
+            DeclRefExpr *DR
+              = cast<DeclRefExpr>(lastLoad->
+                  getSubExpr()->IgnoreParenNoopCasts(ac.getASTContext()));
             if (DR->getDecl() == vd) {
               // int x = x;
               // Propagate uninitialized value, but don't immediately report
@@ -537,6 +544,9 @@
       }
     }
   }
+  else if (ce->getCastKind() == CK_NoOp) {
+    skipProcessUses = true;
+  }
   else if (CStyleCastExpr *cse = dyn_cast<CStyleCastExpr>(ce)) {
     if (cse->getType()->isVoidType()) {
       // e.g. (void) x;
@@ -551,8 +561,10 @@
 }
 
 void TransferFunctions::Visit(clang::Stmt *s) {
+  skipProcessUses = false;
   StmtVisitor<TransferFunctions>::Visit(s);
-  ProcessUses(s);
+  if (!skipProcessUses)
+    ProcessUses(s);
 }
 
 void TransferFunctions::ProcessUses(Stmt *s) {
@@ -568,7 +580,9 @@
     // If we reach here, we have seen a load of an uninitialized value
     // and it hasn't been casted to void or otherwise handled.  In this
     // situation, report the incident.
-    DeclRefExpr *DR = cast<DeclRefExpr>(lastLoad->getSubExpr()->IgnoreParens());
+    DeclRefExpr *DR =
+      cast<DeclRefExpr>(lastLoad->getSubExpr()->
+                          IgnoreParenNoopCasts(ac.getASTContext()));
     VarDecl *VD = cast<VarDecl>(DR->getDecl());
     reportUninit(DR, VD, isAlwaysUninit(vals[VD]));
     lastLoad = 0;

Modified: cfe/trunk/test/SemaCXX/uninit-variables.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/SemaCXX/uninit-variables.cpp?rev=136939&r1=136938&r2=136939&view=diff
==============================================================================
--- cfe/trunk/test/SemaCXX/uninit-variables.cpp (original)
+++ cfe/trunk/test/SemaCXX/uninit-variables.cpp Thu Aug  4 17:40:57 2011
@@ -118,4 +118,15 @@
   }
 }
 
+// Test handling of "no-op" casts.
+void test_noop_cast()
+{
+    int x = 1;
+    int y = (int&)x; // no-warning
+}
+
+void test_noop_cast2() {
+    int x; // expected-note {{declared here}} expected-note {{add initialization}}
+    int y = (int&)x; // expected-warning {{uninitialized when used here}}
+}
 





More information about the cfe-commits mailing list