]> granicus.if.org Git - clang/commitdiff
Tweak -Wuninitialized-experimental to not emit
authorTed Kremenek <kremenek@apple.com>
Wed, 26 Jan 2011 04:49:43 +0000 (04:49 +0000)
committerTed Kremenek <kremenek@apple.com>
Wed, 26 Jan 2011 04:49:43 +0000 (04:49 +0000)
a warning for uses of an uninitialized variable
when the use is a void cast, e.g. (void) x.

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

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

index 4edc1a965bd72c7079e7fa5ade637dc99eb4e4e1..32630549599a393e08492581e138f2bdd1e6ca88 100644 (file)
@@ -291,6 +291,7 @@ class TransferFunctions : public CFGRecStmtVisitor<TransferFunctions> {
   AnalysisContext &ac;
   UninitVariablesHandler *handler;
   const DeclRefExpr *currentDR;
+  const Expr *currentVoidCast;
   const bool flagBlockUses;
 public:
   TransferFunctions(CFGBlockValues &vals, const CFG &cfg,
@@ -298,7 +299,7 @@ public:
                     UninitVariablesHandler *handler,
                     bool flagBlockUses)
     : vals(vals), cfg(cfg), ac(ac), handler(handler), currentDR(0),
-      flagBlockUses(flagBlockUses) {}
+      currentVoidCast(0), flagBlockUses(flagBlockUses) {}
   
   const CFG &getCFG() { return cfg; }
   void reportUninit(const DeclRefExpr *ex, const VarDecl *vd);
@@ -446,14 +447,23 @@ void TransferFunctions::VisitCastExpr(clang::CastExpr *ce) {
       SaveAndRestore<const DeclRefExpr*> lastDR(currentDR, 
                                                 res.getDeclRefExpr());
       Visit(ce->getSubExpr());
-      if (vals[vd] == Uninitialized) {
+      if (currentVoidCast != ce && vals[vd] == Uninitialized) {
         reportUninit(res.getDeclRefExpr(), vd);
         // Don't cascade warnings.
         vals[vd] = Initialized;
       }
       return;
     }
-  }  
+  }
+  else if (CStyleCastExpr *cse = dyn_cast<CStyleCastExpr>(ce)) {
+    if (cse->getType()->isVoidType()) {
+      // e.g. (void) x;
+      SaveAndRestore<const Expr *>
+        lastVoidCast(currentVoidCast, cse->getSubExpr()->IgnoreParens());
+      Visit(cse->getSubExpr());
+      return;
+    }
+  }
   Visit(ce->getSubExpr());
 }
 
index 200fc83b10b338612280d5d5aea0879eaea36cbf..1bcfc8ad6402ba079619bcb45187103eaa7306ff 100644 (file)
@@ -212,3 +212,14 @@ void test32() {
   (void) ^{ (void) test32_x; }; // no-warning
 }
 
+void test_33() {
+  int x; // no-warning
+  (void) x;
+}
+
+int test_34() {
+  int x; // expected-warning{{use of uninitialized variable 'x'}} expected-note{{add initialization to silence this warning}}
+  (void) x;
+  return x; // expected-note{{variable 'x' is possibly uninitialized when used here}}
+}
+