[cfe-commits] r156512 - in /cfe/trunk: lib/Sema/AnalysisBasedWarnings.cpp test/SemaCXX/uninitialized.cpp

Richard Trieu rtrieu at google.com
Wed May 9 14:08:23 PDT 2012


Author: rtrieu
Date: Wed May  9 16:08:22 2012
New Revision: 156512

URL: http://llvm.org/viewvc/llvm-project?rev=156512&view=rev
Log:
Pull some cases of initialization with self-reference warnings out of
-Wconditional-uninitialized into -Wuninitialized.

Modified:
    cfe/trunk/lib/Sema/AnalysisBasedWarnings.cpp
    cfe/trunk/test/SemaCXX/uninitialized.cpp

Modified: cfe/trunk/lib/Sema/AnalysisBasedWarnings.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Sema/AnalysisBasedWarnings.cpp?rev=156512&r1=156511&r2=156512&view=diff
==============================================================================
--- cfe/trunk/lib/Sema/AnalysisBasedWarnings.cpp (original)
+++ cfe/trunk/lib/Sema/AnalysisBasedWarnings.cpp Wed May  9 16:08:22 2012
@@ -464,44 +464,36 @@
 static bool DiagnoseUninitializedUse(Sema &S, const VarDecl *VD,
                                      const Expr *E, bool isAlwaysUninit,
                                      bool alwaysReportSelfInit = false) {
-  bool isSelfInit = false;
 
   if (const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(E)) {
-    if (isAlwaysUninit) {
-      // Inspect the initializer of the variable declaration which is
-      // being referenced prior to its initialization. We emit
-      // specialized diagnostics for self-initialization, and we
-      // specifically avoid warning about self references which take the
-      // form of:
-      //
-      //   int x = x;
-      //
-      // This is used to indicate to GCC that 'x' is intentionally left
-      // uninitialized. Proven code paths which access 'x' in
-      // an uninitialized state after this will still warn.
-      //
-      // TODO: Should we suppress maybe-uninitialized warnings for
-      // variables initialized in this way?
-      if (const Expr *Initializer = VD->getInit()) {
-        if (!alwaysReportSelfInit && DRE == Initializer->IgnoreParenImpCasts())
-          return false;
-
-        ContainsReference CR(S.Context, DRE);
-        CR.Visit(const_cast<Expr*>(Initializer));
-        isSelfInit = CR.doesContainReference();
-      }
-      if (isSelfInit) {
+    // Inspect the initializer of the variable declaration which is
+    // being referenced prior to its initialization. We emit
+    // specialized diagnostics for self-initialization, and we
+    // specifically avoid warning about self references which take the
+    // form of:
+    //
+    //   int x = x;
+    //
+    // This is used to indicate to GCC that 'x' is intentionally left
+    // uninitialized. Proven code paths which access 'x' in
+    // an uninitialized state after this will still warn.
+    if (const Expr *Initializer = VD->getInit()) {
+      if (!alwaysReportSelfInit && DRE == Initializer->IgnoreParenImpCasts())
+        return false;
+
+      ContainsReference CR(S.Context, DRE);
+      CR.Visit(const_cast<Expr*>(Initializer));
+      if (CR.doesContainReference()) {
         S.Diag(DRE->getLocStart(),
                diag::warn_uninit_self_reference_in_init)
-        << VD->getDeclName() << VD->getLocation() << DRE->getSourceRange();
-      } else {
-        S.Diag(DRE->getLocStart(), diag::warn_uninit_var)
-          << VD->getDeclName() << DRE->getSourceRange();
+          << VD->getDeclName() << VD->getLocation() << DRE->getSourceRange();
+        return true;
       }
-    } else {
-      S.Diag(DRE->getLocStart(), diag::warn_maybe_uninit_var)
-        << VD->getDeclName() << DRE->getSourceRange();
     }
+
+    S.Diag(DRE->getLocStart(), isAlwaysUninit ? diag::warn_uninit_var
+                                              : diag::warn_maybe_uninit_var)
+      << VD->getDeclName() << DRE->getSourceRange();
   } else {
     const BlockExpr *BE = cast<BlockExpr>(E);
     if (VD->getType()->isBlockPointerType() &&
@@ -518,7 +510,7 @@
   // Report where the variable was declared when the use wasn't within
   // the initializer of that declaration & we didn't already suggest
   // an initialization fixit.
-  if (!isSelfInit && !SuggestInitializationFixit(S, VD))
+  if (!SuggestInitializationFixit(S, VD))
     S.Diag(VD->getLocStart(), diag::note_uninit_var_def)
       << VD->getDeclName();
 

Modified: cfe/trunk/test/SemaCXX/uninitialized.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/SemaCXX/uninitialized.cpp?rev=156512&r1=156511&r2=156512&view=diff
==============================================================================
--- cfe/trunk/test/SemaCXX/uninitialized.cpp (original)
+++ cfe/trunk/test/SemaCXX/uninitialized.cpp Wed May  9 16:08:22 2012
@@ -10,9 +10,6 @@
 int a = a; // no-warning: used to signal intended lack of initialization.
 int b = b + 1; // expected-warning {{variable 'b' is uninitialized when used within its own initialization}}
 int c = (c + c); // expected-warning 2 {{variable 'c' is uninitialized when used within its own initialization}}
-void test() {
-  int d = ({ d + d ;}); // expected-warning {{variable 'd' is uninitialized when used within its own initialization}}
-}
 int e = static_cast<long>(e) + 1; // expected-warning {{variable 'e' is uninitialized when used within its own initialization}}
 int f = foo(f); // expected-warning {{variable 'f' is uninitialized when used within its own initialization}}
 
@@ -28,6 +25,48 @@
 int m = 1 + (k ? m : m);  // expected-warning 2{{variable 'm' is uninitialized when used within its own initialization}}
 int n = -n;  // expected-warning {{variable 'n' is uninitialized when used within its own initialization}}
 
+void test () {
+  int a = a; // no-warning: used to signal intended lack of initialization.
+  int b = b + 1; // expected-warning {{variable 'b' is uninitialized when used within its own initialization}}
+  int c = (c + c); // expected-warning {{variable 'c' is uninitialized when used within its own initialization}}
+  int d = ({ d + d ;}); // expected-warning {{variable 'd' is uninitialized when used within its own initialization}}
+  int e = static_cast<long>(e) + 1; // expected-warning {{variable 'e' is uninitialized when used within its own initialization}}
+  int f = foo(f); // expected-warning {{variable 'f' is uninitialized when used within its own initialization}}
+
+  // Thes don't warn as they don't require the value.
+  int g = sizeof(g);
+  void* ptr = &ptr;
+  int h = bar(&h);
+  int i = boo(i);
+  int j = far(j);
+  int k = __alignof__(k);
+
+  int l = k ? l : l;  // FIXME: warn here
+  int m = 1 + (k ? m : m);  // FIXME: warn here
+  int n = -n;  // expected-warning {{variable 'n' is uninitialized when used within its own initialization}}
+
+  for (;;) {
+    int a = a; // no-warning: used to signal intended lack of initialization.
+    int b = b + 1; // expected-warning {{variable 'b' is uninitialized when used within its own initialization}}
+    int c = (c + c); // expected-warning {{variable 'c' is uninitialized when used within its own initialization}}
+    int d = ({ d + d ;}); // expected-warning {{variable 'd' is uninitialized when used within its own initialization}}
+    int e = static_cast<long>(e) + 1; // expected-warning {{variable 'e' is uninitialized when used within its own initialization}}
+    int f = foo(f); // expected-warning {{variable 'f' is uninitialized when used within its own initialization}}
+
+    // Thes don't warn as they don't require the value.
+    int g = sizeof(g);
+    void* ptr = &ptr;
+    int h = bar(&h);
+    int i = boo(i);
+    int j = far(j);
+    int k = __alignof__(k);
+
+    int l = k ? l : l;  // FIXME: warn here
+    int m = 1 + (k ? m : m);  // FIXME: warn here
+    int n = -n;  // expected-warning {{variable 'n' is uninitialized when used within its own initialization}}
+  }
+}
+
 // Test self-references with record types.
 class A {
   // Non-POD class.





More information about the cfe-commits mailing list