Skip to content

Commit

Permalink
[CFG] [analyzer] Add construction contexts for loop condition variables.
Browse files Browse the repository at this point in the history
Loop condition variables, eg.

  while (shared_ptr<int> P = getIntPtr()) { ... })

weren't handled in r324794 because they don't go through the common
CFGBuilder::VisitDeclStmt method. Which means that they regressed
after r324800.

Fix the regression by duplicating the necessary construction context scan in
the loop visiting code.

Differential Revision: https://reviews.llvm.org/D45706

llvm-svn: 330382
  • Loading branch information
haoNoQ committed Apr 19, 2018
1 parent 468bc0d commit ab9b78b
Show file tree
Hide file tree
Showing 4 changed files with 105 additions and 10 deletions.
16 changes: 14 additions & 2 deletions clang/lib/Analysis/CFG.cpp
Expand Up @@ -3169,7 +3169,13 @@ CFGBlock *CFGBuilder::VisitForStmt(ForStmt *F) {
if (VarDecl *VD = F->getConditionVariable()) {
if (Expr *Init = VD->getInit()) {
autoCreateBlock();
appendStmt(Block, F->getConditionVariableDeclStmt());
const DeclStmt *DS = F->getConditionVariableDeclStmt();
assert(DS->isSingleDecl());
findConstructionContexts(
ConstructionContextLayer::create(cfg->getBumpVectorContext(),
const_cast<DeclStmt *>(DS)),
Init);
appendStmt(Block, DS);
EntryConditionBlock = addStmt(Init);
assert(Block == EntryConditionBlock);
maybeAddScopeBeginForVarDecl(EntryConditionBlock, VD, C);
Expand Down Expand Up @@ -3494,7 +3500,13 @@ CFGBlock *CFGBuilder::VisitWhileStmt(WhileStmt *W) {
if (VarDecl *VD = W->getConditionVariable()) {
if (Expr *Init = VD->getInit()) {
autoCreateBlock();
appendStmt(Block, W->getConditionVariableDeclStmt());
const DeclStmt *DS = W->getConditionVariableDeclStmt();
assert(DS->isSingleDecl());
findConstructionContexts(
ConstructionContextLayer::create(cfg->getBumpVectorContext(),
const_cast<DeclStmt *>(DS)),
Init);
appendStmt(Block, DS);
EntryConditionBlock = addStmt(Init);
assert(Block == EntryConditionBlock);
maybeAddScopeBeginForVarDecl(EntryConditionBlock, VD, C);
Expand Down
12 changes: 8 additions & 4 deletions clang/test/Analysis/auto-obj-dtors-cfg-output.cpp
Expand Up @@ -388,7 +388,8 @@ void test_if_jumps() {
// CHECK: [B4]
// CHECK-NEXT: 1: a
// CHECK-NEXT: 2: [B4.1] (ImplicitCastExpr, NoOp, const class A)
// CHECK-NEXT: 3: [B4.2] (CXXConstructExpr, class A)
// WARNINGS-NEXT: 3: [B4.2] (CXXConstructExpr, class A)
// ANALYZER-NEXT: 3: [B4.2] (CXXConstructExpr, [B4.4], class A)
// CHECK-NEXT: 4: A b = a;
// CHECK-NEXT: 5: b
// CHECK-NEXT: 6: [B4.5] (ImplicitCastExpr, NoOp, const class A)
Expand Down Expand Up @@ -478,7 +479,8 @@ void test_while_implicit_scope() {
// CHECK: [B10]
// CHECK-NEXT: 1: a
// CHECK-NEXT: 2: [B10.1] (ImplicitCastExpr, NoOp, const class A)
// CHECK-NEXT: 3: [B10.2] (CXXConstructExpr, class A)
// WARNINGS-NEXT: 3: [B10.2] (CXXConstructExpr, class A)
// ANALYZER-NEXT: 3: [B10.2] (CXXConstructExpr, [B10.4], class A)
// CHECK-NEXT: 4: A b = a;
// CHECK-NEXT: 5: b
// CHECK-NEXT: 6: [B10.5] (ImplicitCastExpr, NoOp, const class A)
Expand Down Expand Up @@ -761,7 +763,8 @@ void test_switch_jumps() {
// CHECK: [B4]
// CHECK-NEXT: 1: a
// CHECK-NEXT: 2: [B4.1] (ImplicitCastExpr, NoOp, const class A)
// CHECK-NEXT: 3: [B4.2] (CXXConstructExpr, class A)
// WARNINGS-NEXT: 3: [B4.2] (CXXConstructExpr, class A)
// ANALYZER-NEXT: 3: [B4.2] (CXXConstructExpr, [B4.4], class A)
// CHECK-NEXT: 4: A b = a;
// CHECK-NEXT: 5: b
// CHECK-NEXT: 6: [B4.5] (ImplicitCastExpr, NoOp, const class A)
Expand Down Expand Up @@ -851,7 +854,8 @@ void test_for_implicit_scope() {
// CHECK: [B10]
// CHECK-NEXT: 1: b
// CHECK-NEXT: 2: [B10.1] (ImplicitCastExpr, NoOp, const class A)
// CHECK-NEXT: 3: [B10.2] (CXXConstructExpr, class A)
// WARNINGS-NEXT: 3: [B10.2] (CXXConstructExpr, class A)
// ANALYZER-NEXT: 3: [B10.2] (CXXConstructExpr, [B10.4], class A)
// CHECK-NEXT: 4: A c = b;
// CHECK-NEXT: 5: c
// CHECK-NEXT: 6: [B10.5] (ImplicitCastExpr, NoOp, const class A)
Expand Down
79 changes: 79 additions & 0 deletions clang/test/Analysis/cfg-rich-constructors.cpp
Expand Up @@ -450,6 +450,85 @@ void temporaryInCondition() {
if (C());
}

// CHECK: void temporaryInConditionVariable()
// CHECK: 1: C() (CXXConstructExpr, [B2.2], class C)
// CXX11-NEXT: 2: [B2.1]
// CXX11-NEXT: 3: [B2.2] (CXXConstructExpr, [B2.4], class C)
// CXX11-NEXT: 4: C c = C();
// CXX11-NEXT: 5: c
// CXX11-NEXT: 6: [B2.5] (ImplicitCastExpr, NoOp, const class C)
// CXX11-NEXT: 7: [B2.6].operator bool
// CXX11-NEXT: 8: [B2.6]
// CXX11-NEXT: 9: [B2.8] (ImplicitCastExpr, UserDefinedConversion, _Bool)
// CXX11-NEXT: T: if [B2.9]
// CXX17-NEXT: 2: C c = C();
// CXX17-NEXT: 3: c
// CXX17-NEXT: 4: [B2.3] (ImplicitCastExpr, NoOp, const class C)
// CXX17-NEXT: 5: [B2.4].operator bool
// CXX17-NEXT: 6: [B2.4]
// CXX17-NEXT: 7: [B2.6] (ImplicitCastExpr, UserDefinedConversion, _Bool)
// CXX17-NEXT: T: if [B2.7]
void temporaryInConditionVariable() {
if (C c = C());
}


// CHECK: void temporaryInForLoopConditionVariable()
// CHECK: [B2]
// CXX11-NEXT: 1: C() (CXXConstructExpr, [B2.2], class C)
// CXX11-NEXT: 2: [B2.1]
// CXX11-NEXT: 3: [B2.2] (CXXConstructExpr, [B2.4], class C)
// CXX11-NEXT: 4: C c2 = C();
// CXX11-NEXT: 5: c2
// CXX11-NEXT: 6: [B2.5] (ImplicitCastExpr, NoOp, const class C)
// CXX11-NEXT: 7: [B2.6].operator bool
// CXX11-NEXT: 8: [B2.6]
// CXX11-NEXT: 9: [B2.8] (ImplicitCastExpr, UserDefinedConversion, _Bool)
// CXX11-NEXT: T: for (...; [B2.9]; )
// CXX17-NEXT: 1: C() (CXXConstructExpr, [B2.2], class C)
// CXX17-NEXT: 2: C c2 = C();
// CXX17-NEXT: 3: c2
// CXX17-NEXT: 4: [B2.3] (ImplicitCastExpr, NoOp, const class C)
// CXX17-NEXT: 5: [B2.4].operator bool
// CXX17-NEXT: 6: [B2.4]
// CXX17-NEXT: 7: [B2.6] (ImplicitCastExpr, UserDefinedConversion, _Bool)
// CXX17-NEXT: T: for (...; [B2.7]; )
// CHECK: [B3]
// CXX11-NEXT: 1: C() (CXXConstructExpr, [B3.2], class C)
// CXX11-NEXT: 2: [B3.1]
// CXX11-NEXT: 3: [B3.2] (CXXConstructExpr, [B3.4], class C)
// CXX11-NEXT: 4: C c1 = C();
// CXX17-NEXT: 1: C() (CXXConstructExpr, [B3.2], class C)
// CXX17-NEXT: 2: C c1 = C();
void temporaryInForLoopConditionVariable() {
for (C c1 = C(); C c2 = C(); );
}


// FIXME: Find construction context for the loop condition variable.
// CHECK: void temporaryInWhileLoopConditionVariable()
// CXX11: 1: C() (CXXConstructExpr, [B2.2], class C)
// CXX11-NEXT: 2: [B2.1]
// CXX11-NEXT: 3: [B2.2] (CXXConstructExpr, [B2.4], class C)
// CXX11-NEXT: 4: C c = C();
// CXX11-NEXT: 5: c
// CXX11-NEXT: 6: [B2.5] (ImplicitCastExpr, NoOp, const class C)
// CXX11-NEXT: 7: [B2.6].operator bool
// CXX11-NEXT: 8: [B2.6]
// CXX11-NEXT: 9: [B2.8] (ImplicitCastExpr, UserDefinedConversion, _Bool)
// CXX11-NEXT: T: while [B2.9]
// CXX17: 1: C() (CXXConstructExpr, [B2.2], class C)
// CXX17-NEXT: 2: C c = C();
// CXX17-NEXT: 3: c
// CXX17-NEXT: 4: [B2.3] (ImplicitCastExpr, NoOp, const class C)
// CXX17-NEXT: 5: [B2.4].operator bool
// CXX17-NEXT: 6: [B2.4]
// CXX17-NEXT: 7: [B2.6] (ImplicitCastExpr, UserDefinedConversion, _Bool)
// CXX17-NEXT: T: while [B2.7]
void temporaryInWhileLoopConditionVariable() {
while (C c = C());
}

} // end namespace temporary_object_expr_without_dtors

namespace temporary_object_expr_with_dtors {
Expand Down
8 changes: 4 additions & 4 deletions clang/test/Analysis/scopes-cfg-output.cpp
Expand Up @@ -296,7 +296,7 @@ void test_if_jumps() {
// CHECK-NEXT: 1: CFGScopeBegin(b)
// CHECK-NEXT: 2: a
// CHECK-NEXT: 3: [B4.2] (ImplicitCastExpr, NoOp, const class A)
// CHECK-NEXT: 4: [B4.3] (CXXConstructExpr, class A)
// CHECK-NEXT: 4: [B4.3] (CXXConstructExpr, [B4.5], class A)
// CHECK-NEXT: 5: A b = a;
// CHECK-NEXT: 6: b
// CHECK-NEXT: 7: [B4.6] (ImplicitCastExpr, NoOp, const class A)
Expand Down Expand Up @@ -395,7 +395,7 @@ void test_while_implicit_scope() {
// CHECK-NEXT: 1: CFGScopeBegin(b)
// CHECK-NEXT: 2: a
// CHECK-NEXT: 3: [B10.2] (ImplicitCastExpr, NoOp, const class A)
// CHECK-NEXT: 4: [B10.3] (CXXConstructExpr, class A)
// CHECK-NEXT: 4: [B10.3] (CXXConstructExpr, [B10.5], class A)
// CHECK-NEXT: 5: A b = a;
// CHECK-NEXT: 6: b
// CHECK-NEXT: 7: [B10.6] (ImplicitCastExpr, NoOp, const class A)
Expand Down Expand Up @@ -540,7 +540,7 @@ void test_do_jumps() {
// CHECK-NEXT: 1: CFGScopeBegin(b)
// CHECK-NEXT: 2: a
// CHECK-NEXT: 3: [B4.2] (ImplicitCastExpr, NoOp, const class A)
// CHECK-NEXT: 4: [B4.3] (CXXConstructExpr, class A)
// CHECK-NEXT: 4: [B4.3] (CXXConstructExpr, [B4.5], class A)
// CHECK-NEXT: 5: A b = a;
// CHECK-NEXT: 6: b
// CHECK-NEXT: 7: [B4.6] (ImplicitCastExpr, NoOp, const class A)
Expand Down Expand Up @@ -640,7 +640,7 @@ void test_for_implicit_scope() {
// CHECK-NEXT: 1: CFGScopeBegin(c)
// CHECK-NEXT: 2: b
// CHECK-NEXT: 3: [B10.2] (ImplicitCastExpr, NoOp, const class A)
// CHECK-NEXT: 4: [B10.3] (CXXConstructExpr, class A)
// CHECK-NEXT: 4: [B10.3] (CXXConstructExpr, [B10.5], class A)
// CHECK-NEXT: 5: A c = b;
// CHECK-NEXT: 6: c
// CHECK-NEXT: 7: [B10.6] (ImplicitCastExpr, NoOp, const class A)
Expand Down

0 comments on commit ab9b78b

Please sign in to comment.