As reported in PR 10358, code such as:

void foo(int y) {
  int x; // Undefined.
  do {
    y+=x;
  } while (1);
}

will only trigger a warning about x being uninitialized when
-Wconditional-uninitialized because the uninitialized variables are
mistakenly marked as possibly being assigned to before use. The attached
patch makes a few tiny changes to lib/Analysis/UninitializedValues.cpp to
not  mark uninitialized variables as Initialized just to avoid cascaded
warnings. Instead it marks them as Unknown (part of the local Value enum) so
that isUninitialized() will still return false but variables don't
accidentally get marked as maybe unitialized by calls to
CFGBlockValues::mergeIntoScratch when they are definitely uninitialized
(through it being marked as Uninitialized in one packed vector and
Initialized in the other, and the two values being or'ed together to become
MayUninitialized).

The patch also fixes up the four cases in test/Sema/uninit-variables.c where
a "may be uninitialized" should rightfully be "is uninitialized".

Cheers,
Kaelyn
diff --git a/lib/Analysis/UninitializedValues.cpp b/lib/Analysis/UninitializedValues.cpp
index 1d6959d..5e00eeb 100644
--- a/lib/Analysis/UninitializedValues.cpp
+++ b/lib/Analysis/UninitializedValues.cpp
@@ -495,9 +495,11 @@ void TransferFunctions::VisitBinaryOperator(clang::BinaryOperator *bo) {
 
       ValueVector::reference val = vals[vd];
       if (isUninitialized(val)) {
-        if (bo->getOpcode() != BO_Assign)
+        if (bo->getOpcode() != BO_Assign) {
           reportUninit(res.getDeclRefExpr(), vd, isAlwaysUninit(val));
-        val = Initialized;
+          val = Unknown;
+        } else
+          val = Initialized;
       }
       return;
     }
@@ -526,7 +528,7 @@ void TransferFunctions::VisitUnaryOperator(clang::UnaryOperator *uo) {
         if (isUninitialized(val)) {
           reportUninit(res.getDeclRefExpr(), vd, isAlwaysUninit(val));
           // Don't cascade warnings.
-          val = Initialized;
+          val = Unknown;
         }
         return;
       }
@@ -558,7 +560,7 @@ void TransferFunctions::VisitCastExpr(clang::CastExpr *ce) {
         if (isUninitialized(val)) {
           reportUninit(res.getDeclRefExpr(), vd, isAlwaysUninit(val));
           // Don't cascade warnings.
-          vals[vd] = Initialized;
+          vals[vd] = Unknown;
         }
       }
       return;
diff --git a/test/Sema/uninit-variables.c b/test/Sema/uninit-variables.c
index b70a295..06abc76 100644
--- a/test/Sema/uninit-variables.c
+++ b/test/Sema/uninit-variables.c
@@ -77,7 +77,7 @@ int test11(unsigned n) {
 }
 
 void test12(unsigned n) {
-  for (unsigned i ; n ; ++i) ; // expected-warning{{variable 'i' may be uninitialized when used here}} expected-note{{variable 'i' is declared here}} expected-note{{add initialization to silence this warning}}
+  for (unsigned i ; n ; ++i) ; // expected-warning{{variable 'i' is uninitialized when used here}} expected-note{{variable 'i' is declared here}} expected-note{{add initialization to silence this warning}}
 }
 
 int test13() {
@@ -237,7 +237,7 @@ void test36()
   void **pc; // expected-note{{variable 'pc' is declared here}} expected-note{{add initialization to silence this warning}}
   void *dummy[] = { &&L1, &&L2 };
  L1:
-    goto *pc; // expected-warning{{variable 'pc' may be uninitialized when used here}}
+    goto *pc; // expected-warning{{variable 'pc' is uninitialized when used here}}
  L2:
     goto *pc;
 }
@@ -289,7 +289,7 @@ void test43_aux(int x);
 void test43(int i) {
   int x; // expected-note {{variable 'x' is declared here}} expected-note{{add initialization to silence this warning}}
   for (i = 0 ; i < 10; i++)
-    test43_aux(x++); // expected-warning {{variable 'x' may be uninitialized when used here}}
+    test43_aux(x++); // expected-warning {{variable 'x' is uninitialized when used here}}
 }
 
 void test44(int i) {
@@ -297,7 +297,7 @@ void test44(int i) {
   int y; // expected-note {{variable 'y' is declared here}} expected-note{{add initialization to silence this warning}}
   for (i = 0; i < 10; i++ ) {
     test43_aux(x++); // no-warning
-    x += y; // expected-warning {{variable 'y' may be uninitialized when used here}}
+    x += y; // expected-warning {{variable 'y' is uninitialized when used here}}
   }
 }
 
_______________________________________________
cfe-commits mailing list
[email protected]
http://lists.cs.uiuc.edu/mailman/listinfo/cfe-commits

Reply via email to