PR31631: fix bad CFG (and bogus warnings) when an if-statement has an init-statement and has binary operator as its condition.

llvm-svn: 291964
This commit is contained in:
Richard Smith 2017-01-13 22:16:41 +00:00
parent 251099171f
commit 509bbd1a66
2 changed files with 47 additions and 42 deletions

View File

@ -2175,19 +2175,15 @@ CFGBlock *CFGBuilder::VisitIfStmt(IfStmt *I) {
SaveAndRestore<LocalScope::const_iterator> save_scope_pos(ScopePos); SaveAndRestore<LocalScope::const_iterator> save_scope_pos(ScopePos);
// Create local scope for C++17 if init-stmt if one exists. // Create local scope for C++17 if init-stmt if one exists.
if (Stmt *Init = I->getInit()) { if (Stmt *Init = I->getInit())
LocalScope::const_iterator BeginScopePos = ScopePos;
addLocalScopeForStmt(Init); addLocalScopeForStmt(Init);
addAutomaticObjDtors(ScopePos, BeginScopePos, I);
}
// Create local scope for possible condition variable. // Create local scope for possible condition variable.
// Store scope position. Add implicit destructor. // Store scope position. Add implicit destructor.
if (VarDecl *VD = I->getConditionVariable()) { if (VarDecl *VD = I->getConditionVariable())
LocalScope::const_iterator BeginScopePos = ScopePos;
addLocalScopeForVarDecl(VD); addLocalScopeForVarDecl(VD);
addAutomaticObjDtors(ScopePos, BeginScopePos, I);
} addAutomaticObjDtors(ScopePos, save_scope_pos.get(), I);
// The block we were processing is now finished. Make it the successor // The block we were processing is now finished. Make it the successor
// block. // block.
@ -2256,12 +2252,14 @@ CFGBlock *CFGBuilder::VisitIfStmt(IfStmt *I) {
// removes infeasible paths from the control-flow graph by having the // removes infeasible paths from the control-flow graph by having the
// control-flow transfer of '&&' or '||' go directly into the then/else // control-flow transfer of '&&' or '||' go directly into the then/else
// blocks directly. // blocks directly.
if (!I->getConditionVariable()) BinaryOperator *Cond =
if (BinaryOperator *Cond = I->getConditionVariable()
dyn_cast<BinaryOperator>(I->getCond()->IgnoreParens())) ? nullptr
if (Cond->isLogicalOp()) : dyn_cast<BinaryOperator>(I->getCond()->IgnoreParens());
return VisitLogicalOperator(Cond, I, ThenBlock, ElseBlock).first; CFGBlock *LastBlock;
if (Cond && Cond->isLogicalOp())
LastBlock = VisitLogicalOperator(Cond, I, ThenBlock, ElseBlock).first;
else {
// Now create a new block containing the if statement. // Now create a new block containing the if statement.
Block = createBlock(false); Block = createBlock(false);
@ -2276,10 +2274,10 @@ CFGBlock *CFGBuilder::VisitIfStmt(IfStmt *I) {
addSuccessor(Block, ThenBlock, /* isReachable = */ !KnownVal.isFalse()); addSuccessor(Block, ThenBlock, /* isReachable = */ !KnownVal.isFalse());
addSuccessor(Block, ElseBlock, /* isReachable = */ !KnownVal.isTrue()); addSuccessor(Block, ElseBlock, /* isReachable = */ !KnownVal.isTrue());
// Add the condition as the last statement in the new block. This may create // Add the condition as the last statement in the new block. This may
// new blocks as the condition may contain control-flow. Any newly created // create new blocks as the condition may contain control-flow. Any newly
// blocks will be pointed to be "Block". // created blocks will be pointed to be "Block".
CFGBlock *LastBlock = addStmt(I->getCond()); LastBlock = addStmt(I->getCond());
// If the IfStmt contains a condition variable, add it and its // If the IfStmt contains a condition variable, add it and its
// initializer to the CFG. // initializer to the CFG.
@ -2287,6 +2285,7 @@ CFGBlock *CFGBuilder::VisitIfStmt(IfStmt *I) {
autoCreateBlock(); autoCreateBlock();
LastBlock = addStmt(const_cast<DeclStmt *>(DS)); LastBlock = addStmt(const_cast<DeclStmt *>(DS));
} }
}
// Finally, if the IfStmt contains a C++17 init-stmt, add it to the CFG. // Finally, if the IfStmt contains a C++17 init-stmt, add it to the CFG.
if (Stmt *Init = I->getInit()) { if (Stmt *Init = I->getInit()) {
@ -3078,19 +3077,15 @@ CFGBlock *CFGBuilder::VisitSwitchStmt(SwitchStmt *Terminator) {
SaveAndRestore<LocalScope::const_iterator> save_scope_pos(ScopePos); SaveAndRestore<LocalScope::const_iterator> save_scope_pos(ScopePos);
// Create local scope for C++17 switch init-stmt if one exists. // Create local scope for C++17 switch init-stmt if one exists.
if (Stmt *Init = Terminator->getInit()) { if (Stmt *Init = Terminator->getInit())
LocalScope::const_iterator BeginScopePos = ScopePos;
addLocalScopeForStmt(Init); addLocalScopeForStmt(Init);
addAutomaticObjDtors(ScopePos, BeginScopePos, Terminator);
}
// Create local scope for possible condition variable. // Create local scope for possible condition variable.
// Store scope position. Add implicit destructor. // Store scope position. Add implicit destructor.
if (VarDecl *VD = Terminator->getConditionVariable()) { if (VarDecl *VD = Terminator->getConditionVariable())
LocalScope::const_iterator SwitchBeginScopePos = ScopePos;
addLocalScopeForVarDecl(VD); addLocalScopeForVarDecl(VD);
addAutomaticObjDtors(ScopePos, SwitchBeginScopePos, Terminator);
} addAutomaticObjDtors(ScopePos, save_scope_pos.get(), Terminator);
if (Block) { if (Block) {
if (badCFG) if (badCFG)

View File

@ -1,4 +1,4 @@
// RUN: %clang_cc1 -fsyntax-only -Wall -Wuninitialized -Wno-unused-value -Wno-unused-lambda-capture -std=c++11 -verify %s // RUN: %clang_cc1 -fsyntax-only -Wall -Wuninitialized -Wno-unused-value -Wno-unused-lambda-capture -std=c++1z -verify %s
// definitions for std::move // definitions for std::move
namespace std { namespace std {
@ -1437,3 +1437,13 @@ void array_capture(bool b) {
[fname]{}; [fname]{};
} }
} }
void if_switch_init_stmt(int k) {
if (int n = 0; (n == k || k > 5)) {}
if (int n; (n == k || k > 5)) {} // expected-warning {{uninitialized}} expected-note {{initialize}}
switch (int n = 0; (n == k || k > 5)) {} // expected-warning {{boolean}}
switch (int n; (n == k || k > 5)) {} // expected-warning {{uninitialized}} expected-note {{initialize}} expected-warning {{boolean}}
}