Skip to content

[clang][dataflow] Fix unsupported types always being equal #129502

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
Mar 4, 2025
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
12 changes: 11 additions & 1 deletion clang/lib/Analysis/FlowSensitive/Transfer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,9 @@ static BoolValue &evaluateBooleanEquality(const Expr &LHS, const Expr &RHS,
Value *LHSValue = Env.getValue(LHS);
Value *RHSValue = Env.getValue(RHS);

if (LHSValue == RHSValue)
// When two unsupported values are compared, both are nullptr. Only supported
// values should evaluate to equal.
if (LHSValue == RHSValue && LHSValue)
return Env.getBoolLiteralValue(true);

if (auto *LHSBool = dyn_cast_or_null<BoolValue>(LHSValue))
Expand Down Expand Up @@ -798,6 +800,14 @@ class TransferVisitor : public ConstStmtVisitor<TransferVisitor> {
Env.setValue(*S, Env.getIntLiteralValue(S->getValue()));
}

// Untyped nullptr's aren't handled by NullToPointer casts, so they need to be
// handled separately.
void VisitCXXNullPtrLiteralExpr(const CXXNullPtrLiteralExpr *S) {
auto &NullPointerVal =
Env.getOrCreateNullPointerValue(S->getType()->getPointeeType());
Env.setValue(*S, NullPointerVal);
}

void VisitParenExpr(const ParenExpr *S) {
// The CFG does not contain `ParenExpr` as top-level statements in basic
// blocks, however manual traversal to sub-expressions may encounter them.
Expand Down
35 changes: 35 additions & 0 deletions clang/unittests/Analysis/FlowSensitive/TransferTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4974,6 +4974,41 @@ TEST(TransferTest, IntegerLiteralEquality) {
});
}

TEST(TransferTest, UnsupportedValueEquality) {
std::string Code = R"(
// An explicitly unsupported type by the framework.
enum class EC {
A,
B
};

void target() {
EC ec = EC::A;

bool unsupported_eq_same = (EC::A == EC::A);
bool unsupported_eq_other = (EC::A == EC::B);
bool unsupported_eq_var = (ec == EC::B);

(void)0; // [[p]]
}
)";
runDataflow(
Code,
[](const llvm::StringMap<DataflowAnalysisState<NoopLattice>> &Results,
ASTContext &ASTCtx) {
const Environment &Env = getEnvironmentAtAnnotation(Results, "p");

// We do not model the values of unsupported types, so this
// seemingly-trivial case will not be true either.
EXPECT_TRUE(isa<AtomicBoolValue>(
getValueForDecl<BoolValue>(ASTCtx, Env, "unsupported_eq_same")));
EXPECT_TRUE(isa<AtomicBoolValue>(
getValueForDecl<BoolValue>(ASTCtx, Env, "unsupported_eq_other")));
EXPECT_TRUE(isa<AtomicBoolValue>(
getValueForDecl<BoolValue>(ASTCtx, Env, "unsupported_eq_var")));
});
}

TEST(TransferTest, CorrelatedBranches) {
std::string Code = R"(
void target(bool B, bool C) {
Expand Down