Skip to content

Commit

Permalink
[clang][dataflow] Support StmtExpr in PropagateResultObject(). (#…
Browse files Browse the repository at this point in the history
…88872)

This patch adds a test that assert-fails without the fix.
  • Loading branch information
martinboehme committed Apr 17, 2024
1 parent 024281d commit b851c7f
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 0 deletions.
5 changes: 5 additions & 0 deletions clang/lib/Analysis/FlowSensitive/DataflowEnvironment.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -470,6 +470,11 @@ class ResultObjectVisitor : public RecursiveASTVisitor<ResultObjectVisitor> {
return;
}

if (auto *SE = dyn_cast<StmtExpr>(E)) {
PropagateResultObject(cast<Expr>(SE->getSubStmt()->body_back()), Loc);
return;
}

// All other expression nodes that propagate a record prvalue should have
// exactly one child.
SmallVector<Stmt *, 1> Children(E->child_begin(), E->child_end());
Expand Down
26 changes: 26 additions & 0 deletions clang/unittests/Analysis/FlowSensitive/TransferTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3182,6 +3182,32 @@ TEST(TransferTest, ResultObjectLocationForStdInitializerListExpr) {
});
}

TEST(TransferTest, ResultObjectLocationForStmtExpr) {
std::string Code = R"(
struct S {};
void target() {
S s = ({ S(); });
// [[p]]
}
)";
using ast_matchers::cxxConstructExpr;
using ast_matchers::match;
using ast_matchers::selectFirst;
using ast_matchers::traverse;
runDataflow(
Code,
[](const llvm::StringMap<DataflowAnalysisState<NoopLattice>> &Results,
ASTContext &ASTCtx) {
const Environment &Env = getEnvironmentAtAnnotation(Results, "p");

auto *Construct = selectFirst<CXXConstructExpr>(
"construct", match(cxxConstructExpr().bind("construct"), ASTCtx));

EXPECT_EQ(&Env.getResultObjectLocation(*Construct),
&getLocForDecl<RecordStorageLocation>(ASTCtx, Env, "s"));
});
}

TEST(TransferTest, ResultObjectLocationPropagatesThroughConditionalOperator) {
std::string Code = R"(
struct A {
Expand Down

0 comments on commit b851c7f

Please sign in to comment.