Skip to content

[clang][dataflow] Fix crash when ConstantExpr is used in conditional operator. #90112

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
Apr 26, 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
16 changes: 12 additions & 4 deletions clang/lib/Analysis/FlowSensitive/ASTOps.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,20 @@ namespace clang::dataflow {

const Expr &ignoreCFGOmittedNodes(const Expr &E) {
const Expr *Current = &E;
if (auto *EWC = dyn_cast<ExprWithCleanups>(Current)) {
Current = EWC->getSubExpr();
const Expr *Last = nullptr;
while (Current != Last) {
Last = Current;
if (auto *EWC = dyn_cast<ExprWithCleanups>(Current)) {
Current = EWC->getSubExpr();
assert(Current != nullptr);
}
if (auto *CE = dyn_cast<ConstantExpr>(Current)) {
Current = CE->getSubExpr();
assert(Current != nullptr);
}
Current = Current->IgnoreParens();
assert(Current != nullptr);
}
Current = Current->IgnoreParens();
assert(Current != nullptr);
return *Current;
}

Expand Down
6 changes: 5 additions & 1 deletion clang/lib/Analysis/FlowSensitive/Transfer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,11 @@ namespace dataflow {

const Environment *StmtToEnvMap::getEnvironment(const Stmt &S) const {
auto BlockIt = ACFG.getStmtToBlock().find(&ignoreCFGOmittedNodes(S));
assert(BlockIt != ACFG.getStmtToBlock().end());
if (BlockIt == ACFG.getStmtToBlock().end()) {
assert(false);
// Return null to avoid dereferencing the end iterator in non-assert builds.
return nullptr;
}
if (!ACFG.isBlockReachable(*BlockIt->getSecond()))
return nullptr;
if (BlockIt->getSecond()->getBlockID() == CurBlockID)
Expand Down
32 changes: 32 additions & 0 deletions clang/unittests/Analysis/FlowSensitive/TransferTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5357,6 +5357,38 @@ TEST(TransferTest, ConditionalOperatorLocation) {
});
}

TEST(TransferTest, ConditionalOperatorOnConstantExpr) {
// This is a regression test: We used to crash when a `ConstantExpr` was used
// in the branches of a conditional operator.
std::string Code = R"cc(
consteval bool identity(bool B) { return B; }
void target(bool Cond) {
bool JoinTrueTrue = Cond ? identity(true) : identity(true);
bool JoinTrueFalse = Cond ? identity(true) : identity(false);
// [[p]]
}
)cc";
runDataflow(
Code,
[](const llvm::StringMap<DataflowAnalysisState<NoopLattice>> &Results,
ASTContext &ASTCtx) {
Environment Env = getEnvironmentAtAnnotation(Results, "p").fork();

auto &JoinTrueTrue =
getValueForDecl<BoolValue>(ASTCtx, Env, "JoinTrueTrue");
// FIXME: This test documents the current behavior, namely that we
// don't actually use the constant result of the `ConstantExpr` and
// instead treat it like a normal function call.
EXPECT_EQ(JoinTrueTrue.formula().kind(), Formula::Kind::AtomRef);
// EXPECT_TRUE(JoinTrueTrue.formula().literal());

auto &JoinTrueFalse =
getValueForDecl<BoolValue>(ASTCtx, Env, "JoinTrueFalse");
EXPECT_EQ(JoinTrueFalse.formula().kind(), Formula::Kind::AtomRef);
},
LangStandard::lang_cxx20);
}

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