]> granicus.if.org Git - clang/commitdiff
-Wuninitialized: don't warn about uninitialized variables in unreachable code.
authorTed Kremenek <kremenek@apple.com>
Mon, 4 Apr 2011 20:30:58 +0000 (20:30 +0000)
committerTed Kremenek <kremenek@apple.com>
Mon, 4 Apr 2011 20:30:58 +0000 (20:30 +0000)
git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@128840 91177308-0d34-0410-b5e6-96231b3b80d8

lib/Analysis/CFG.cpp
lib/Analysis/UninitializedValues.cpp
test/SemaCXX/uninit-variables.cpp

index c193112131ff1fd4ffb30301ca39859e43a08434..e91de0366c605ff89c5ba653004ab4e654d1f5c2 100644 (file)
@@ -428,8 +428,8 @@ private:
     if (!BuildOpts.PruneTriviallyFalseEdges)
       return false;
     return !S->isTypeDependent() && 
-    !S->isValueDependent() &&
-    S->Evaluate(outResult, *Context);
+           !S->isValueDependent() &&
+           S->Evaluate(outResult, *Context);
   }
 
   /// tryEvaluateBool - Try and evaluate the Stmt and return 0 or 1
index 58191ec0b042ead5d8f406258f776f2d2e0c3f90..f775cc5b500208b54df4d87323d0d9d70173c674 100644 (file)
@@ -156,6 +156,8 @@ public:
     return declToIndex.getValueIndex(vd).hasValue();
   }
   
+  bool hasValues(const CFGBlock *block);
+  
   void resetScratch();
   ValueVector &getScratch() { return scratch; }
   
@@ -231,6 +233,11 @@ ValueVector &CFGBlockValues::getValueVector(const CFGBlock *block,
   return lazyCreate(vals[idx].first);
 }
 
+bool CFGBlockValues::hasValues(const CFGBlock *block) {
+  unsigned idx = block->getBlockID();
+  return vals[idx].second != 0;  
+}
+
 BVPair &CFGBlockValues::getValueVectors(const clang::CFGBlock *block,
                                         bool shouldLazyCreate) {
   unsigned idx = block->getBlockID();
@@ -603,9 +610,12 @@ void TransferFunctions::VisitUnaryExprOrTypeTraitExpr(
 
 static bool runOnBlock(const CFGBlock *block, const CFG &cfg,
                        AnalysisContext &ac, CFGBlockValues &vals,
+                       llvm::BitVector &wasAnalyzed,
                        UninitVariablesHandler *handler = 0,
                        bool flagBlockUses = false) {
   
+  wasAnalyzed[block->getBlockID()] = true;
+  
   if (const BinaryOperator *b = getLogicalOperatorInChain(block)) {
     CFGBlock::const_pred_iterator itr = block->pred_begin();
     BVPair vA = vals.getValueVectors(*itr, false);
@@ -663,10 +673,11 @@ void clang::runUninitializedVariablesAnalysis(const DeclContext &dc,
   llvm::BitVector previouslyVisited(cfg.getNumBlockIDs());
   
   worklist.enqueueSuccessors(&cfg.getEntry());
+  llvm::BitVector wasAnalyzed(cfg.getNumBlockIDs(), false);
 
   while (const CFGBlock *block = worklist.dequeue()) {
     // Did the block change?
-    bool changed = runOnBlock(block, cfg, ac, vals);    
+    bool changed = runOnBlock(block, cfg, ac, vals, wasAnalyzed);    
     if (changed || !previouslyVisited[block->getBlockID()])
       worklist.enqueueSuccessors(block);    
     previouslyVisited[block->getBlockID()] = true;
@@ -674,7 +685,9 @@ void clang::runUninitializedVariablesAnalysis(const DeclContext &dc,
   
   // Run through the blocks one more time, and report uninitialized variabes.
   for (CFG::const_iterator BI = cfg.begin(), BE = cfg.end(); BI != BE; ++BI) {
-    runOnBlock(*BI, cfg, ac, vals, &handler, /* flagBlockUses */ true);
+    if (wasAnalyzed[(*BI)->getBlockID()])
+      runOnBlock(*BI, cfg, ac, vals, wasAnalyzed, &handler,
+                 /* flagBlockUses */ true);
   }
 }
 
index 77cbcf72a3d9af0a4bb07294852fc9cfa924ee58..6fa59fe2a15e544f7211fbccce4b34fdbc85ba28 100644 (file)
@@ -70,3 +70,11 @@ class Rdar9188004C : public Rdar9188004B<Rdar9188004A> {
   virtual void bar(void) const;
 };
 void Rdar9188004C::bar(void) const {}
+
+// Don't warn about uninitialized variables in unreachable code.
+void PR9625() {
+  if (false) {
+    int x;
+    (void)static_cast<float>(x); // no-warning
+  }
+}