Skip to content

Commit b851c7f

Browse files
authored
[clang][dataflow] Support StmtExpr in PropagateResultObject(). (#88872)
This patch adds a test that assert-fails without the fix.
1 parent 024281d commit b851c7f

File tree

2 files changed

+31
-0
lines changed

2 files changed

+31
-0
lines changed

clang/lib/Analysis/FlowSensitive/DataflowEnvironment.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -470,6 +470,11 @@ class ResultObjectVisitor : public RecursiveASTVisitor<ResultObjectVisitor> {
470470
return;
471471
}
472472

473+
if (auto *SE = dyn_cast<StmtExpr>(E)) {
474+
PropagateResultObject(cast<Expr>(SE->getSubStmt()->body_back()), Loc);
475+
return;
476+
}
477+
473478
// All other expression nodes that propagate a record prvalue should have
474479
// exactly one child.
475480
SmallVector<Stmt *, 1> Children(E->child_begin(), E->child_end());

clang/unittests/Analysis/FlowSensitive/TransferTest.cpp

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3182,6 +3182,32 @@ TEST(TransferTest, ResultObjectLocationForStdInitializerListExpr) {
31823182
});
31833183
}
31843184

3185+
TEST(TransferTest, ResultObjectLocationForStmtExpr) {
3186+
std::string Code = R"(
3187+
struct S {};
3188+
void target() {
3189+
S s = ({ S(); });
3190+
// [[p]]
3191+
}
3192+
)";
3193+
using ast_matchers::cxxConstructExpr;
3194+
using ast_matchers::match;
3195+
using ast_matchers::selectFirst;
3196+
using ast_matchers::traverse;
3197+
runDataflow(
3198+
Code,
3199+
[](const llvm::StringMap<DataflowAnalysisState<NoopLattice>> &Results,
3200+
ASTContext &ASTCtx) {
3201+
const Environment &Env = getEnvironmentAtAnnotation(Results, "p");
3202+
3203+
auto *Construct = selectFirst<CXXConstructExpr>(
3204+
"construct", match(cxxConstructExpr().bind("construct"), ASTCtx));
3205+
3206+
EXPECT_EQ(&Env.getResultObjectLocation(*Construct),
3207+
&getLocForDecl<RecordStorageLocation>(ASTCtx, Env, "s"));
3208+
});
3209+
}
3210+
31853211
TEST(TransferTest, ResultObjectLocationPropagatesThroughConditionalOperator) {
31863212
std::string Code = R"(
31873213
struct A {

0 commit comments

Comments
 (0)