Skip to content

Commit a9a8338

Browse files
authored
Reland [clang][dataflow] Fix unsupported types always being equal (llvm#131575)
Relands llvm#129502. Previously when the framework encountered unsupported values (such as enum classes), they were always treated as equal when comparing with `==`, regardless of their actual values being different. Now the two sides are only equal if there's a Value assigned to them. Added handling for the special case of `nullptr == nullptr`, to preserve the behavior of untyped `nullptr` having no value.
1 parent 29ca03f commit a9a8338

File tree

2 files changed

+43
-1
lines changed

2 files changed

+43
-1
lines changed

clang/lib/Analysis/FlowSensitive/Transfer.cpp

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,14 @@ static BoolValue &evaluateBooleanEquality(const Expr &LHS, const Expr &RHS,
6060
Value *LHSValue = Env.getValue(LHS);
6161
Value *RHSValue = Env.getValue(RHS);
6262

63-
if (LHSValue == RHSValue)
63+
// When two unsupported values are compared, both are nullptr. Only supported
64+
// values should evaluate to equal.
65+
if (LHSValue == RHSValue && LHSValue)
66+
return Env.getBoolLiteralValue(true);
67+
68+
// Special case: `NullPtrLiteralExpr == itself`. When both sides are untyped
69+
// nullptr, they do not have an assigned Value, but they compare equal.
70+
if (LHS.getType()->isNullPtrType() && RHS.getType()->isNullPtrType())
6471
return Env.getBoolLiteralValue(true);
6572

6673
if (auto *LHSBool = dyn_cast_or_null<BoolValue>(LHSValue))

clang/unittests/Analysis/FlowSensitive/TransferTest.cpp

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4974,6 +4974,41 @@ TEST(TransferTest, IntegerLiteralEquality) {
49744974
});
49754975
}
49764976

4977+
TEST(TransferTest, UnsupportedValueEquality) {
4978+
std::string Code = R"(
4979+
// An explicitly unsupported type by the framework.
4980+
enum class EC {
4981+
A,
4982+
B
4983+
};
4984+
4985+
void target() {
4986+
EC ec = EC::A;
4987+
4988+
bool unsupported_eq_same = (EC::A == EC::A);
4989+
bool unsupported_eq_other = (EC::A == EC::B);
4990+
bool unsupported_eq_var = (ec == EC::B);
4991+
4992+
(void)0; // [[p]]
4993+
}
4994+
)";
4995+
runDataflow(
4996+
Code,
4997+
[](const llvm::StringMap<DataflowAnalysisState<NoopLattice>> &Results,
4998+
ASTContext &ASTCtx) {
4999+
const Environment &Env = getEnvironmentAtAnnotation(Results, "p");
5000+
5001+
// We do not model the values of unsupported types, so this
5002+
// seemingly-trivial case will not be true either.
5003+
EXPECT_TRUE(isa<AtomicBoolValue>(
5004+
getValueForDecl<BoolValue>(ASTCtx, Env, "unsupported_eq_same")));
5005+
EXPECT_TRUE(isa<AtomicBoolValue>(
5006+
getValueForDecl<BoolValue>(ASTCtx, Env, "unsupported_eq_other")));
5007+
EXPECT_TRUE(isa<AtomicBoolValue>(
5008+
getValueForDecl<BoolValue>(ASTCtx, Env, "unsupported_eq_var")));
5009+
});
5010+
}
5011+
49775012
TEST(TransferTest, CorrelatedBranches) {
49785013
std::string Code = R"(
49795014
void target(bool B, bool C) {

0 commit comments

Comments
 (0)