[cfe-commits] r123733 - in /cfe/trunk: lib/Analysis/UninitializedValuesV2.cpp test/Sema/uninit-variables.c

Ted Kremenek kremenek at apple.com
Mon Jan 17 20:53:26 PST 2011


Author: kremenek
Date: Mon Jan 17 22:53:25 2011
New Revision: 123733

URL: http://llvm.org/viewvc/llvm-project?rev=123733&view=rev
Log:
Teach UninitializedValuesV2 about "int x = x" and
also properly handle confluence of loops.

Modified:
    cfe/trunk/lib/Analysis/UninitializedValuesV2.cpp
    cfe/trunk/test/Sema/uninit-variables.c

Modified: cfe/trunk/lib/Analysis/UninitializedValuesV2.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Analysis/UninitializedValuesV2.cpp?rev=123733&r1=123732&r2=123733&view=diff
==============================================================================
--- cfe/trunk/lib/Analysis/UninitializedValuesV2.cpp (original)
+++ cfe/trunk/lib/Analysis/UninitializedValuesV2.cpp Mon Jan 17 22:53:25 2011
@@ -22,6 +22,11 @@
 
 using namespace clang;
 
+static bool isTrackedVar(const VarDecl *vd) {
+  return vd->isLocalVarDecl() && !vd->hasGlobalStorage() && 
+         vd->getType()->isScalarType();
+}
+
 //------------------------------------------------------------------------====//
 // DeclToBit: a mapping from Decls we track to bitvector indices.
 //====------------------------------------------------------------------------//
@@ -49,7 +54,7 @@
                                                E(dc.decls_end());
   for ( ; I != E; ++I) {
     const VarDecl *vd = *I;
-    if (vd->isLocalVarDecl() && !vd->hasGlobalStorage())
+    if (isTrackedVar(vd))
       map[vd] = count++;
   }
 }
@@ -126,7 +131,7 @@
   if (isFirst)
     scratch = source;
   else
-    scratch &= source;  
+    scratch |= source;  
 }
 
 bool CFGBlockValues::updateBitVectorWithScratch(const CFGBlock *block) {
@@ -166,6 +171,8 @@
 }
 
 void DataflowWorklist::enqueue(const CFGBlock *block) {
+  if (!block)
+    return;
   unsigned idx = block->getBlockID();
   if (enqueuedBlocks[idx])
     return;
@@ -193,8 +200,8 @@
 // Transfer function for uninitialized values analysis.
 //====------------------------------------------------------------------------//
 
-static const bool Initialized = true;
-static const bool Uninitialized = false;
+static const bool Initialized = false;
+static const bool Uninitialized = true;
 
 namespace {
 class FindVarResult {
@@ -235,12 +242,11 @@
   for (DeclStmt::decl_iterator DI = ds->decl_begin(), DE = ds->decl_end();
        DI != DE; ++DI) {
     if (VarDecl *vd = dyn_cast<VarDecl>(*DI)) {
-      if (vd->isLocalVarDecl() && !vd->hasGlobalStorage()) {
+      if (isTrackedVar(vd))
         if (Stmt *init = vd->getInit()) {
-          vals[vd] = Initialized;
           Visit(init);
+          vals[vd] = Initialized;
         }
-      }
     }
   }
 }
@@ -248,7 +254,7 @@
 static FindVarResult findBlockVarDecl(Expr* ex) {
   if (DeclRefExpr* dr = dyn_cast<DeclRefExpr>(ex->IgnoreParenCasts()))
     if (VarDecl *vd = dyn_cast<VarDecl>(dr->getDecl()))
-      if (vd->isLocalVarDecl() && !vd->hasGlobalStorage())
+      if (isTrackedVar(vd))
         return FindVarResult(vd, dr);
 
   return FindVarResult(0, 0);

Modified: cfe/trunk/test/Sema/uninit-variables.c
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/Sema/uninit-variables.c?rev=123733&r1=123732&r2=123733&view=diff
==============================================================================
--- cfe/trunk/test/Sema/uninit-variables.c (original)
+++ cfe/trunk/test/Sema/uninit-variables.c Mon Jan 17 22:53:25 2011
@@ -85,4 +85,20 @@
   return i; // no-warning
 }
 
+// Simply don't crash on this test case.
+void test14() {
+  const char *p = 0;
+  for (;;) {}
+}
+
+void test15() {
+  int x = x; // expected-warning{{use of uninitialized variable 'x'}}
+}
 
+// Don't warn in the following example; shows dataflow confluence.
+char *test16_aux();
+void test16() {
+  char *p = test16_aux();
+  for (unsigned i = 0 ; i < 100 ; i++)
+    p[i] = 'a'; // no-warning
+}





More information about the cfe-commits mailing list