Skip to content

Commit a2caa49

Browse files
authored
[clang][dataflow] Treat comma operator correctly in getResultObjectLocation(). (#78427)
1 parent 3b943c0 commit a2caa49

File tree

2 files changed

+23
-8
lines changed

2 files changed

+23
-8
lines changed

clang/lib/Analysis/FlowSensitive/DataflowEnvironment.cpp

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -782,8 +782,13 @@ Environment::getResultObjectLocation(const Expr &RecordPRValue) const {
782782
return Val->getLoc();
783783
}
784784

785-
// Expression nodes that propagate a record prvalue should have exactly one
786-
// child.
785+
if (auto *Op = dyn_cast<BinaryOperator>(&RecordPRValue);
786+
Op && Op->isCommaOp()) {
787+
return getResultObjectLocation(*Op->getRHS());
788+
}
789+
790+
// All other expression nodes that propagate a record prvalue should have
791+
// exactly one child.
787792
llvm::SmallVector<const Stmt *> children(RecordPRValue.child_begin(),
788793
RecordPRValue.child_end());
789794
assert(children.size() == 1);

clang/unittests/Analysis/FlowSensitive/TransferTest.cpp

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2642,14 +2642,17 @@ TEST(TransferTest, ResultObjectLocation) {
26422642
};
26432643
26442644
void target() {
2645-
A();
2645+
0, A();
26462646
(void)0; // [[p]]
26472647
}
26482648
)";
2649+
using ast_matchers::binaryOperator;
26492650
using ast_matchers::cxxBindTemporaryExpr;
26502651
using ast_matchers::cxxTemporaryObjectExpr;
26512652
using ast_matchers::exprWithCleanups;
26522653
using ast_matchers::has;
2654+
using ast_matchers::hasOperatorName;
2655+
using ast_matchers::hasRHS;
26532656
using ast_matchers::match;
26542657
using ast_matchers::selectFirst;
26552658
using ast_matchers::traverse;
@@ -2659,26 +2662,33 @@ TEST(TransferTest, ResultObjectLocation) {
26592662
ASTContext &ASTCtx) {
26602663
const Environment &Env = getEnvironmentAtAnnotation(Results, "p");
26612664

2662-
// The expresssion `A()` in the code above produces the following
2663-
// structure, consisting of three prvalues of record type.
2665+
// The expression `0, A()` in the code above produces the following
2666+
// structure, consisting of four prvalues of record type.
26642667
// `Env.getResultObjectLocation()` should return the same location for
26652668
// all of these.
26662669
auto MatchResult = match(
26672670
traverse(TK_AsIs,
26682671
exprWithCleanups(
2669-
has(cxxBindTemporaryExpr(
2670-
has(cxxTemporaryObjectExpr().bind("toe")))
2671-
.bind("bte")))
2672+
has(binaryOperator(
2673+
hasOperatorName(","),
2674+
hasRHS(cxxBindTemporaryExpr(
2675+
has(cxxTemporaryObjectExpr().bind(
2676+
"toe")))
2677+
.bind("bte")))
2678+
.bind("comma")))
26722679
.bind("ewc")),
26732680
ASTCtx);
26742681
auto *TOE = selectFirst<CXXTemporaryObjectExpr>("toe", MatchResult);
26752682
ASSERT_NE(TOE, nullptr);
2683+
auto *Comma = selectFirst<BinaryOperator>("comma", MatchResult);
2684+
ASSERT_NE(Comma, nullptr);
26762685
auto *EWC = selectFirst<ExprWithCleanups>("ewc", MatchResult);
26772686
ASSERT_NE(EWC, nullptr);
26782687
auto *BTE = selectFirst<CXXBindTemporaryExpr>("bte", MatchResult);
26792688
ASSERT_NE(BTE, nullptr);
26802689

26812690
RecordStorageLocation &Loc = Env.getResultObjectLocation(*TOE);
2691+
EXPECT_EQ(&Loc, &Env.getResultObjectLocation(*Comma));
26822692
EXPECT_EQ(&Loc, &Env.getResultObjectLocation(*EWC));
26832693
EXPECT_EQ(&Loc, &Env.getResultObjectLocation(*BTE));
26842694
});

0 commit comments

Comments
 (0)