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

Chandler Carruth chandlerc at gmail.com
Tue Apr 5 14:36:30 PDT 2011


Author: chandlerc
Date: Tue Apr  5 16:36:30 2011
New Revision: 128932

URL: http://llvm.org/viewvc/llvm-project?rev=128932&view=rev
Log:
Commit a bit of a hack to fully handle the situation where variables are
marked explicitly as uninitialized through direct self initialization:

  int x = x;

With r128894 we prevented warnings about this code, and this patch
teaches the analysis engine to continue analyzing subsequent uses of
'x'. This should wrap up PR9624.

There is still an open question of whether we should suppress the
maybe-uninitialized warnings resulting from variables initialized in
this fashion. The definitely-uninitialized uses should always be warned.

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

Modified: cfe/trunk/lib/Analysis/UninitializedValues.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Analysis/UninitializedValues.cpp?rev=128932&r1=128931&r2=128932&view=diff
==============================================================================
--- cfe/trunk/lib/Analysis/UninitializedValues.cpp (original)
+++ cfe/trunk/lib/Analysis/UninitializedValues.cpp Tue Apr  5 16:36:30 2011
@@ -472,12 +472,24 @@
        DI != DE; ++DI) {
     if (VarDecl *vd = dyn_cast<VarDecl>(*DI)) {
       if (isTrackedVar(vd)) {
-        if (Stmt *init = vd->getInit()) {
+        if (Expr *init = vd->getInit()) {
           Visit(init);
-          vals[vd] = Initialized;
+
+          // If the initializer consists solely of a reference to itself, we
+          // explicitly mark the variable as uninitialized. This allows code
+          // like the following:
+          //
+          //   int x = x;
+          //
+          // to deliberately leave a variable uninitialized. Different analysis
+          // clients can detect this pattern and adjust their reporting
+          // appropriately, but we need to continue to analyze subsequent uses
+          // of the variable.
+          DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(init->IgnoreParenImpCasts());
+          vals[vd] = (DRE && DRE->getDecl() == vd) ? Uninitialized
+                                                   : Initialized;
         }
-      }
-      else if (Stmt *init = vd->getInit()) {
+      } else if (Stmt *init = vd->getInit()) {
         Visit(init);
       }
     }

Modified: cfe/trunk/test/Sema/uninit-variables.c
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/Sema/uninit-variables.c?rev=128932&r1=128931&r2=128932&view=diff
==============================================================================
--- cfe/trunk/test/Sema/uninit-variables.c (original)
+++ cfe/trunk/test/Sema/uninit-variables.c Tue Apr  5 16:36:30 2011
@@ -91,8 +91,10 @@
   for (;;) {}
 }
 
-void test15() {
-  int x = x; // no-warning: signals intended lack of initialization.
+int test15() {
+  int x = x; // no-warning: signals intended lack of initialization. \
+             // expected-note{{variable 'x' is declared here}}
+  return x; // expected-warning{{variable 'x' is possibly uninitialized when used here}}
 }
 
 // Don't warn in the following example; shows dataflow confluence.





More information about the cfe-commits mailing list