Skip to content

[clang][dataflow] fix assert in Environment::getResultObjectLocation #79608

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions clang/lib/Analysis/FlowSensitive/Transfer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -536,7 +536,13 @@ class TransferVisitor : public ConstStmtVisitor<TransferVisitor> {

copyRecord(*LocSrc, *LocDst, Env);
Env.setStorageLocation(*S, *LocDst);
return;
}

// CXXOperatorCallExpr can be prvalues. Call `VisitCallExpr`() to create
// a `RecordValue` for them so that `Environment::getResultObjectLocation()`
// can return a value.
VisitCallExpr(S);
}

void VisitCXXFunctionalCastExpr(const CXXFunctionalCastExpr *S) {
Expand Down
33 changes: 33 additions & 0 deletions clang/unittests/Analysis/FlowSensitive/TransferTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2735,6 +2735,39 @@ TEST(TransferTest, ResultObjectLocationForDefaultInitExpr) {
});
}

// This test ensures that CXXOperatorCallExpr returning prvalues are correctly
// handled by the transfer functions, especially that `getResultObjectLocation`
// correctly returns a storage location for those.
TEST(TransferTest, ResultObjectLocationForCXXOperatorCallExpr) {
std::string Code = R"(
struct A {
A operator+(int);
};

void target() {
A a;
a + 3;
(void)0; // [[p]]
}
)";
using ast_matchers::cxxOperatorCallExpr;
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 *CallExpr = selectFirst<CXXOperatorCallExpr>(
"call_expr",
match(cxxOperatorCallExpr().bind("call_expr"), ASTCtx));

EXPECT_NE(&Env.getResultObjectLocation(*CallExpr), nullptr);
});
}

TEST(TransferTest, StaticCast) {
std::string Code = R"(
void target(int Foo) {
Expand Down