-Wuninitialized: don't warn about uninitialized variables in unreachable code.

llvm-svn: 128840
This commit is contained in:
Ted Kremenek 2011-04-04 20:30:58 +00:00
parent 0abc463f5b
commit 352a7081a8
3 changed files with 25 additions and 4 deletions

View File

@ -156,6 +156,8 @@ public:
return declToIndex.getValueIndex(vd).hasValue(); return declToIndex.getValueIndex(vd).hasValue();
} }
bool hasValues(const CFGBlock *block);
void resetScratch(); void resetScratch();
ValueVector &getScratch() { return scratch; } ValueVector &getScratch() { return scratch; }
@ -231,6 +233,11 @@ ValueVector &CFGBlockValues::getValueVector(const CFGBlock *block,
return lazyCreate(vals[idx].first); 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, BVPair &CFGBlockValues::getValueVectors(const clang::CFGBlock *block,
bool shouldLazyCreate) { bool shouldLazyCreate) {
unsigned idx = block->getBlockID(); unsigned idx = block->getBlockID();
@ -603,9 +610,12 @@ void TransferFunctions::VisitUnaryExprOrTypeTraitExpr(
static bool runOnBlock(const CFGBlock *block, const CFG &cfg, static bool runOnBlock(const CFGBlock *block, const CFG &cfg,
AnalysisContext &ac, CFGBlockValues &vals, AnalysisContext &ac, CFGBlockValues &vals,
llvm::BitVector &wasAnalyzed,
UninitVariablesHandler *handler = 0, UninitVariablesHandler *handler = 0,
bool flagBlockUses = false) { bool flagBlockUses = false) {
wasAnalyzed[block->getBlockID()] = true;
if (const BinaryOperator *b = getLogicalOperatorInChain(block)) { if (const BinaryOperator *b = getLogicalOperatorInChain(block)) {
CFGBlock::const_pred_iterator itr = block->pred_begin(); CFGBlock::const_pred_iterator itr = block->pred_begin();
BVPair vA = vals.getValueVectors(*itr, false); BVPair vA = vals.getValueVectors(*itr, false);
@ -663,10 +673,11 @@ void clang::runUninitializedVariablesAnalysis(const DeclContext &dc,
llvm::BitVector previouslyVisited(cfg.getNumBlockIDs()); llvm::BitVector previouslyVisited(cfg.getNumBlockIDs());
worklist.enqueueSuccessors(&cfg.getEntry()); worklist.enqueueSuccessors(&cfg.getEntry());
llvm::BitVector wasAnalyzed(cfg.getNumBlockIDs(), false);
while (const CFGBlock *block = worklist.dequeue()) { while (const CFGBlock *block = worklist.dequeue()) {
// Did the block change? // Did the block change?
bool changed = runOnBlock(block, cfg, ac, vals); bool changed = runOnBlock(block, cfg, ac, vals, wasAnalyzed);
if (changed || !previouslyVisited[block->getBlockID()]) if (changed || !previouslyVisited[block->getBlockID()])
worklist.enqueueSuccessors(block); worklist.enqueueSuccessors(block);
previouslyVisited[block->getBlockID()] = true; 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. // Run through the blocks one more time, and report uninitialized variabes.
for (CFG::const_iterator BI = cfg.begin(), BE = cfg.end(); BI != BE; ++BI) { 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);
} }
} }

View File

@ -70,3 +70,11 @@ class Rdar9188004C : public Rdar9188004B<Rdar9188004A> {
virtual void bar(void) const; virtual void bar(void) const;
}; };
void Rdar9188004C::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
}
}