diff --git a/clang/lib/Analysis/FlowSensitive/Transfer.cpp b/clang/lib/Analysis/FlowSensitive/Transfer.cpp index 2271a75fbcaf7..bb3aec763c29c 100644 --- a/clang/lib/Analysis/FlowSensitive/Transfer.cpp +++ b/clang/lib/Analysis/FlowSensitive/Transfer.cpp @@ -536,7 +536,13 @@ class TransferVisitor : public ConstStmtVisitor { 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) { diff --git a/clang/unittests/Analysis/FlowSensitive/TransferTest.cpp b/clang/unittests/Analysis/FlowSensitive/TransferTest.cpp index 268ea4c7431f6..8bbb04024dcce 100644 --- a/clang/unittests/Analysis/FlowSensitive/TransferTest.cpp +++ b/clang/unittests/Analysis/FlowSensitive/TransferTest.cpp @@ -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> &Results, + ASTContext &ASTCtx) { + const Environment &Env = getEnvironmentAtAnnotation(Results, "p"); + + auto *CallExpr = selectFirst( + "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) {