Skip to content

[clang][dataflow] Treat comma operator correctly in getResultObjectLocation(). #78427

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
merged 1 commit into from
Jan 22, 2024
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
9 changes: 7 additions & 2 deletions clang/lib/Analysis/FlowSensitive/DataflowEnvironment.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -781,8 +781,13 @@ Environment::getResultObjectLocation(const Expr &RecordPRValue) const {
return Val->getLoc();
}

// Expression nodes that propagate a record prvalue should have exactly one
// child.
if (auto *Op = dyn_cast<BinaryOperator>(&RecordPRValue);
Op && Op->isCommaOp()) {
return getResultObjectLocation(*Op->getRHS());
}

// All other expression nodes that propagate a record prvalue should have
// exactly one child.
llvm::SmallVector<const Stmt *> children(RecordPRValue.child_begin(),
RecordPRValue.child_end());
assert(children.size() == 1);
Expand Down
22 changes: 16 additions & 6 deletions clang/unittests/Analysis/FlowSensitive/TransferTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2642,14 +2642,17 @@ TEST(TransferTest, ResultObjectLocation) {
};

void target() {
A();
0, A();
(void)0; // [[p]]
}
)";
using ast_matchers::binaryOperator;
using ast_matchers::cxxBindTemporaryExpr;
using ast_matchers::cxxTemporaryObjectExpr;
using ast_matchers::exprWithCleanups;
using ast_matchers::has;
using ast_matchers::hasOperatorName;
using ast_matchers::hasRHS;
using ast_matchers::match;
using ast_matchers::selectFirst;
using ast_matchers::traverse;
Expand All @@ -2659,26 +2662,33 @@ TEST(TransferTest, ResultObjectLocation) {
ASTContext &ASTCtx) {
const Environment &Env = getEnvironmentAtAnnotation(Results, "p");

// The expresssion `A()` in the code above produces the following
// structure, consisting of three prvalues of record type.
// The expression `0, A()` in the code above produces the following
// structure, consisting of four prvalues of record type.
// `Env.getResultObjectLocation()` should return the same location for
// all of these.
auto MatchResult = match(
traverse(TK_AsIs,
exprWithCleanups(
has(cxxBindTemporaryExpr(
has(cxxTemporaryObjectExpr().bind("toe")))
.bind("bte")))
has(binaryOperator(
hasOperatorName(","),
hasRHS(cxxBindTemporaryExpr(
has(cxxTemporaryObjectExpr().bind(
"toe")))
.bind("bte")))
.bind("comma")))
.bind("ewc")),
ASTCtx);
auto *TOE = selectFirst<CXXTemporaryObjectExpr>("toe", MatchResult);
ASSERT_NE(TOE, nullptr);
auto *Comma = selectFirst<BinaryOperator>("comma", MatchResult);
ASSERT_NE(Comma, nullptr);
auto *EWC = selectFirst<ExprWithCleanups>("ewc", MatchResult);
ASSERT_NE(EWC, nullptr);
auto *BTE = selectFirst<CXXBindTemporaryExpr>("bte", MatchResult);
ASSERT_NE(BTE, nullptr);

RecordStorageLocation &Loc = Env.getResultObjectLocation(*TOE);
EXPECT_EQ(&Loc, &Env.getResultObjectLocation(*Comma));
EXPECT_EQ(&Loc, &Env.getResultObjectLocation(*EWC));
EXPECT_EQ(&Loc, &Env.getResultObjectLocation(*BTE));
});
Expand Down