]> granicus.if.org Git - clang/commitdiff
-Wuninitialized bugfix: when entering the scope of a variable with no
authorRichard Smith <richard-llvm@metafoo.co.uk>
Sat, 16 Jun 2012 23:34:14 +0000 (23:34 +0000)
committerRichard Smith <richard-llvm@metafoo.co.uk>
Sat, 16 Jun 2012 23:34:14 +0000 (23:34 +0000)
initializer, it is uninitialized, even if we may be coming from somewhere where
it was initialized.

git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@158611 91177308-0d34-0410-b5e6-96231b3b80d8

lib/Analysis/UninitializedValues.cpp
test/Sema/uninit-variables.c

index 57dfab36fddefb664de35f52938e77902377d725..fbfa46c443c76a851e278a2a801c687a275b48a6 100644 (file)
@@ -625,6 +625,18 @@ void TransferFunctions::VisitDeclStmt(DeclStmt *ds) {
           // the use of the uninitialized value (which visiting the
           // initializer).
           vals[vd] = Initialized;
+        } else {
+          // No initializer: the variable is now uninitialized. This matters
+          // for cases like:
+          //   while (...) {
+          //     int n;
+          //     use(n);
+          //     n = 0;
+          //   }
+          // FIXME: Mark the variable as uninitialized whenever its scope is
+          // left, since its scope could be re-entered by a jump over the
+          // declaration.
+          vals[vd] = Uninitialized;
         }
       }
     }
index ef03d44d97b01cdf7a81b8560ee76c5f7dec1037..180d60cbce7631f0ff0a3a2632dec24cfb5d3867 100644 (file)
@@ -437,3 +437,28 @@ void test54() {
   int c;  // expected-note {{initialize the variable 'c' to silence this warning}}
   ASSIGN(int, c, d);  // expected-warning {{variable 'c' is uninitialized when used here}}
 }
+
+void uninit_in_loop() {
+  int produce(void);
+  void consume(int);
+  for (int n = 0; n < 100; ++n) {
+    int k; // expected-note {{initialize}}
+    consume(k); // expected-warning {{variable 'k' is uninitialized}}
+    k = produce();
+  }
+}
+
+void uninit_in_loop_goto() {
+  int produce(void);
+  void consume(int);
+  for (int n = 0; n < 100; ++n) {
+    goto skip_decl;
+    int k; // expected-note {{initialize}}
+skip_decl:
+    // FIXME: This should produce the 'is uninitialized' diagnostic, but we
+    // don't have enough information in the CFG to easily tell that the
+    // variable's scope has been left and re-entered.
+    consume(k); // expected-warning {{variable 'k' may be uninitialized}}
+    k = produce();
+  }
+}