Skip to content

Commit 680391f

Browse files
authored
[clang][dataflow] Fix unsupported types always being equal (#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 a Value assignment for `nullptr`, to handle the special case of `nullptr == nullptr`.
1 parent e27b8b2 commit 680391f

File tree

2 files changed

+46
-1
lines changed

2 files changed

+46
-1
lines changed

clang/lib/Analysis/FlowSensitive/Transfer.cpp

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,9 @@ 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)
6466
return Env.getBoolLiteralValue(true);
6567

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

803+
// Untyped nullptr's aren't handled by NullToPointer casts, so they need to be
804+
// handled separately.
805+
void VisitCXXNullPtrLiteralExpr(const CXXNullPtrLiteralExpr *S) {
806+
auto &NullPointerVal =
807+
Env.getOrCreateNullPointerValue(S->getType()->getPointeeType());
808+
Env.setValue(*S, NullPointerVal);
809+
}
810+
801811
void VisitParenExpr(const ParenExpr *S) {
802812
// The CFG does not contain `ParenExpr` as top-level statements in basic
803813
// blocks, however manual traversal to sub-expressions may encounter them.

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)