Skip to content

Commit

Permalink
[clang][dataflow] Create Values for integer literals.
Browse files Browse the repository at this point in the history
This patch includes a test that fails without the fix.

I discovered that we weren't creating `Value`s for integer literals when, in a
different patch, I tried to overwrite the value of a struct field with a literal
for the purposes of a test and was surprised to find that the struct compared
the same before and after the assignment.

This functionality therefore seems useful at least for tests, but is probably
also useful for actual analysis of code.

Reviewed By: ymandel, xazax.hun, gribozavr2

Differential Revision: https://reviews.llvm.org/D152813
  • Loading branch information
martinboehme committed Jun 19, 2023
1 parent 5a8fc41 commit 762cb1d
Show file tree
Hide file tree
Showing 5 changed files with 79 additions and 5 deletions.
11 changes: 9 additions & 2 deletions clang/include/clang/Analysis/FlowSensitive/Arena.h
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,12 @@ class Arena {
/// will be a value that represents the true boolean literal.
BoolValue &makeEquals(BoolValue &LHS, BoolValue &RHS);

/// Returns a symbolic integer value that models an integer literal equal to
/// `Value`. These literals are the same every time.
/// Integer literals are not typed; the type is determined by the `Expr` that
/// an integer literal is associated with.
IntegerValue &makeIntLiteral(llvm::APInt Value);

/// Returns a symbolic boolean value that models a boolean literal equal to
/// `Value`. These literals are the same every time.
AtomicBoolValue &makeLiteral(bool Value) const {
Expand All @@ -103,8 +109,9 @@ class Arena {
std::vector<std::unique_ptr<StorageLocation>> Locs;
std::vector<std::unique_ptr<Value>> Vals;

// Indices that are used to avoid recreating the same composite boolean
// values.
// Indices that are used to avoid recreating the same integer literals and
// composite boolean values.
llvm::DenseMap<llvm::APInt, IntegerValue *> IntegerLiterals;
llvm::DenseMap<std::pair<BoolValue *, BoolValue *>, ConjunctionValue *>
ConjunctionVals;
llvm::DenseMap<std::pair<BoolValue *, BoolValue *>, DisjunctionValue *>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -448,6 +448,12 @@ class Environment {
return DACtx->arena().create<T>(std::forward<Args>(args)...);
}

/// Returns a symbolic integer value that models an integer literal equal to
/// `Value`
IntegerValue &getIntLiteralValue(llvm::APInt Value) const {
return DACtx->arena().makeIntLiteral(Value);
}

/// Returns a symbolic boolean value that models a boolean literal equal to
/// `Value`
AtomicBoolValue &getBoolLiteralValue(bool Value) const {
Expand Down
8 changes: 8 additions & 0 deletions clang/lib/Analysis/FlowSensitive/Arena.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -68,4 +68,12 @@ BoolValue &Arena::makeEquals(BoolValue &LHS, BoolValue &RHS) {
return *Res.first->second;
}

IntegerValue &Arena::makeIntLiteral(llvm::APInt Value) {
auto [It, Inserted] = IntegerLiterals.try_emplace(Value, nullptr);

if (Inserted)
It->second = &create<IntegerValue>();
return *It->second;
}

} // namespace clang::dataflow
16 changes: 13 additions & 3 deletions clang/lib/Analysis/FlowSensitive/Transfer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,15 @@ const Environment *StmtToEnvMap::getEnvironment(const Stmt &S) const {

static BoolValue &evaluateBooleanEquality(const Expr &LHS, const Expr &RHS,
Environment &Env) {
if (auto *LHSValue = dyn_cast_or_null<BoolValue>(Env.getValueStrict(LHS)))
if (auto *RHSValue = dyn_cast_or_null<BoolValue>(Env.getValueStrict(RHS)))
return Env.makeIff(*LHSValue, *RHSValue);
Value *LHSValue = Env.getValueStrict(LHS);
Value *RHSValue = Env.getValueStrict(RHS);

if (LHSValue == RHSValue)
return Env.getBoolLiteralValue(true);

if (auto *LHSBool = dyn_cast_or_null<BoolValue>(LHSValue))
if (auto *RHSBool = dyn_cast_or_null<BoolValue>(RHSValue))
return Env.makeIff(*LHSBool, *RHSBool);

return Env.makeAtomicBoolValue();
}
Expand Down Expand Up @@ -776,6 +782,10 @@ class TransferVisitor : public ConstStmtVisitor<TransferVisitor> {
Env.setValueStrict(*S, Env.getBoolLiteralValue(S->getValue()));
}

void VisitIntegerLiteral(const IntegerLiteral *S) {
Env.setValueStrict(*S, Env.getIntLiteralValue(S->getValue()));
}

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
43 changes: 43 additions & 0 deletions clang/unittests/Analysis/FlowSensitive/TransferTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -811,6 +811,31 @@ TEST(TransferTest, BinaryOperatorAssign) {
});
}

TEST(TransferTest, BinaryOperatorAssignIntegerLiteral) {
std::string Code = R"(
void target() {
int Foo = 1;
// [[before]]
Foo = 2;
// [[after]]
}
)";
runDataflow(
Code,
[](const llvm::StringMap<DataflowAnalysisState<NoopLattice>> &Results,
ASTContext &ASTCtx) {
const Environment &Before =
getEnvironmentAtAnnotation(Results, "before");
const Environment &After = getEnvironmentAtAnnotation(Results, "after");

const auto &ValBefore =
getValueForDecl<IntegerValue>(ASTCtx, Before, "Foo");
const auto &ValAfter =
getValueForDecl<IntegerValue>(ASTCtx, After, "Foo");
EXPECT_NE(&ValBefore, &ValAfter);
});
}

TEST(TransferTest, VarDeclInitAssign) {
std::string Code = R"(
void target() {
Expand Down Expand Up @@ -3441,6 +3466,24 @@ TEST(TransferTest, BooleanInequality) {
});
}

TEST(TransferTest, IntegerLiteralEquality) {
std::string Code = R"(
void target() {
bool equal = (42 == 42);
// [[p]]
}
)";
runDataflow(
Code,
[](const llvm::StringMap<DataflowAnalysisState<NoopLattice>> &Results,
ASTContext &ASTCtx) {
const Environment &Env = getEnvironmentAtAnnotation(Results, "p");

auto &Equal = getValueForDecl<BoolValue>(ASTCtx, Env, "equal");
EXPECT_TRUE(Env.flowConditionImplies(Equal));
});
}

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

0 comments on commit 762cb1d

Please sign in to comment.