Skip to content

Commit 62b2a47

Browse files
committed
[clang][dataflow] Only skip ExprWithCleanups when visiting terminators
`IgnoreParenImpCasts` will remove implicit casts to bool (e.g. `PointerToBoolean`), such that the resulting expression may not be of the `bool` type. The `cast_or_null<BoolValue>` in `extendFlowCondition` will then trigger an assert, as the pointer expression will not have a `BoolValue`. Instead, we only skip `ExprWithCleanups` and `ParenExpr` nodes, as the CFG does not emit them. Differential Revision: https://reviews.llvm.org/D124807
1 parent 7aadfc5 commit 62b2a47

File tree

6 files changed

+73
-21
lines changed

6 files changed

+73
-21
lines changed

clang/include/clang/Analysis/FlowSensitive/DataflowEnvironment.h

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,10 @@ class Environment {
172172
/// Creates a storage location for `E`. Does not assign the returned storage
173173
/// location to `E` in the environment. Does not assign a value to the
174174
/// returned storage location in the environment.
175+
///
176+
/// Requirements:
177+
///
178+
/// `E` must not be a `ExprWithCleanups`.
175179
StorageLocation &createStorageLocation(const Expr &E);
176180

177181
/// Assigns `Loc` as the storage location of `D` in the environment.
@@ -191,11 +195,16 @@ class Environment {
191195
/// Requirements:
192196
///
193197
/// `E` must not be assigned a storage location in the environment.
198+
/// `E` must not be a `ExprWithCleanups`.
194199
void setStorageLocation(const Expr &E, StorageLocation &Loc);
195200

196201
/// Returns the storage location assigned to `E` in the environment, applying
197202
/// the `SP` policy for skipping past indirections, or null if `E` isn't
198203
/// assigned a storage location in the environment.
204+
///
205+
/// Requirements:
206+
///
207+
/// `E` must not be a `ExprWithCleanups`.
199208
StorageLocation *getStorageLocation(const Expr &E, SkipPast SP) const;
200209

201210
/// Returns the storage location assigned to the `this` pointee in the
@@ -226,6 +235,12 @@ class Environment {
226235

227236
/// Equivalent to `getValue(getStorageLocation(E, SP), SkipPast::None)` if `E`
228237
/// is assigned a storage location in the environment, otherwise returns null.
238+
///
239+
/// Requirements:
240+
///
241+
/// `E` must not be a `ExprWithCleanups`.
242+
///
243+
/// FIXME: `Environment` should ignore any `ExprWithCleanups` it sees.
229244
Value *getValue(const Expr &E, SkipPast SP) const;
230245

231246
/// Transfers ownership of `Loc` to the analysis context and returns a

clang/include/clang/Analysis/FlowSensitive/Transfer.h

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,19 @@ class StmtToEnvMap {
3535
///
3636
/// Requirements:
3737
///
38-
/// The type of `S` must not be `ParenExpr`.
38+
/// `S` must not be `ParenExpr` or `ExprWithCleanups`.
3939
void transfer(const StmtToEnvMap &StmtToEnv, const Stmt &S, Environment &Env);
4040

41+
/// Skip past a `ExprWithCleanups` which might surround `E`. Returns null if `E`
42+
/// is null.
43+
///
44+
/// The CFG omits `ExprWithCleanups` nodes (as it does with `ParenExpr`), and so
45+
/// the transfer function doesn't accept them as valid input. Manual traversal
46+
/// of the AST should skip and unwrap any `ExprWithCleanups` it might expect to
47+
/// see. They are safe to skip, as the CFG will emit calls to destructors as
48+
/// appropriate.
49+
const Expr *ignoreExprWithCleanups(const Expr *E);
50+
4151
} // namespace dataflow
4252
} // namespace clang
4353

clang/lib/Analysis/FlowSensitive/DataflowEnvironment.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
#include "clang/Analysis/FlowSensitive/DataflowEnvironment.h"
1616
#include "clang/AST/Decl.h"
1717
#include "clang/AST/DeclCXX.h"
18+
#include "clang/AST/ExprCXX.h"
1819
#include "clang/AST/Type.h"
1920
#include "clang/Analysis/FlowSensitive/DataflowLattice.h"
2021
#include "clang/Analysis/FlowSensitive/StorageLocation.h"
@@ -342,6 +343,7 @@ StorageLocation &Environment::createStorageLocation(const VarDecl &D) {
342343
}
343344

344345
StorageLocation &Environment::createStorageLocation(const Expr &E) {
346+
assert(!isa<ExprWithCleanups>(&E));
345347
// Evaluated expressions are always assigned the same storage locations to
346348
// ensure that the environment stabilizes across loop iterations. Storage
347349
// locations for evaluated expressions are stored in the analysis context.
@@ -364,12 +366,14 @@ StorageLocation *Environment::getStorageLocation(const ValueDecl &D,
364366
}
365367

366368
void Environment::setStorageLocation(const Expr &E, StorageLocation &Loc) {
369+
assert(!isa<ExprWithCleanups>(&E));
367370
assert(ExprToLoc.find(&E) == ExprToLoc.end());
368371
ExprToLoc[&E] = &Loc;
369372
}
370373

371374
StorageLocation *Environment::getStorageLocation(const Expr &E,
372375
SkipPast SP) const {
376+
assert(!isa<ExprWithCleanups>(&E));
373377
// FIXME: Add a test with parens.
374378
auto It = ExprToLoc.find(E.IgnoreParens());
375379
return It == ExprToLoc.end() ? nullptr : &skip(*It->second, SP);

clang/lib/Analysis/FlowSensitive/Transfer.cpp

Lines changed: 5 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@
3333
namespace clang {
3434
namespace dataflow {
3535

36-
static const Expr *skipExprWithCleanups(const Expr *E) {
36+
const Expr *ignoreExprWithCleanups(const Expr *E) {
3737
if (auto *C = dyn_cast_or_null<ExprWithCleanups>(E))
3838
return C->getSubExpr();
3939
return E;
@@ -155,9 +155,7 @@ class TransferVisitor : public ConstStmtVisitor<TransferVisitor> {
155155
return;
156156
}
157157

158-
// The CFG does not contain `ParenExpr` as top-level statements in basic
159-
// blocks, however sub-expressions can still be of that type.
160-
InitExpr = skipExprWithCleanups(D.getInit()->IgnoreParens());
158+
InitExpr = ignoreExprWithCleanups(D.getInit());
161159
assert(InitExpr != nullptr);
162160

163161
if (D.getType()->isReferenceType()) {
@@ -190,10 +188,7 @@ class TransferVisitor : public ConstStmtVisitor<TransferVisitor> {
190188
}
191189

192190
void VisitImplicitCastExpr(const ImplicitCastExpr *S) {
193-
// The CFG does not contain `ParenExpr` as top-level statements in basic
194-
// blocks, however sub-expressions can still be of that type.
195-
assert(S->getSubExpr() != nullptr);
196-
const Expr *SubExpr = S->getSubExpr()->IgnoreParens();
191+
const Expr *SubExpr = S->getSubExpr();
197192
assert(SubExpr != nullptr);
198193

199194
switch (S->getCastKind()) {
@@ -252,10 +247,7 @@ class TransferVisitor : public ConstStmtVisitor<TransferVisitor> {
252247
}
253248

254249
void VisitUnaryOperator(const UnaryOperator *S) {
255-
// The CFG does not contain `ParenExpr` as top-level statements in basic
256-
// blocks, however sub-expressions can still be of that type.
257-
assert(S->getSubExpr() != nullptr);
258-
const Expr *SubExpr = S->getSubExpr()->IgnoreParens();
250+
const Expr *SubExpr = S->getSubExpr();
259251
assert(SubExpr != nullptr);
260252

261253
switch (S->getOpcode()) {
@@ -444,9 +436,6 @@ class TransferVisitor : public ConstStmtVisitor<TransferVisitor> {
444436

445437
void VisitCXXFunctionalCastExpr(const CXXFunctionalCastExpr *S) {
446438
if (S->getCastKind() == CK_ConstructorConversion) {
447-
// The CFG does not contain `ParenExpr` as top-level statements in basic
448-
// blocks, however sub-expressions can still be of that type.
449-
assert(S->getSubExpr() != nullptr);
450439
const Expr *SubExpr = S->getSubExpr();
451440
assert(SubExpr != nullptr);
452441

@@ -604,7 +593,7 @@ class TransferVisitor : public ConstStmtVisitor<TransferVisitor> {
604593
};
605594

606595
void transfer(const StmtToEnvMap &StmtToEnv, const Stmt &S, Environment &Env) {
607-
assert(!isa<ParenExpr>(&S));
596+
assert(!(isa<ParenExpr, ExprWithCleanups>(&S)));
608597
TransferVisitor(StmtToEnv, Env).Visit(&S);
609598
}
610599

clang/lib/Analysis/FlowSensitive/TypeErasedDataflowAnalysis.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -77,26 +77,26 @@ class TerminatorVisitor : public ConstStmtVisitor<TerminatorVisitor> {
7777
: StmtToEnv(StmtToEnv), Env(Env), BlockSuccIdx(BlockSuccIdx) {}
7878

7979
void VisitIfStmt(const IfStmt *S) {
80-
auto *Cond = S->getCond()->IgnoreParenImpCasts();
80+
auto *Cond = ignoreExprWithCleanups(S->getCond())->IgnoreParens();
8181
assert(Cond != nullptr);
8282
extendFlowCondition(*Cond);
8383
}
8484

8585
void VisitWhileStmt(const WhileStmt *S) {
86-
auto *Cond = S->getCond()->IgnoreParenImpCasts();
86+
auto *Cond = ignoreExprWithCleanups(S->getCond())->IgnoreParens();
8787
assert(Cond != nullptr);
8888
extendFlowCondition(*Cond);
8989
}
9090

9191
void VisitBinaryOperator(const BinaryOperator *S) {
9292
assert(S->getOpcode() == BO_LAnd || S->getOpcode() == BO_LOr);
93-
auto *LHS = S->getLHS()->IgnoreParenImpCasts();
93+
auto *LHS = ignoreExprWithCleanups(S->getLHS())->IgnoreParens();
9494
assert(LHS != nullptr);
9595
extendFlowCondition(*LHS);
9696
}
9797

9898
void VisitConditionalOperator(const ConditionalOperator *S) {
99-
auto *Cond = S->getCond()->IgnoreParenImpCasts();
99+
auto *Cond = ignoreExprWithCleanups(S->getCond())->IgnoreParens();
100100
assert(Cond != nullptr);
101101
extendFlowCondition(*Cond);
102102
}

clang/unittests/Analysis/FlowSensitive/TypeErasedDataflowAnalysisTest.cpp

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1152,4 +1152,38 @@ TEST_F(FlowConditionTest, OpaqueFlowConditionInsideBranchMergesToOpaqueBool) {
11521152
});
11531153
}
11541154

1155+
TEST_F(FlowConditionTest, PointerToBoolImplicitCast) {
1156+
std::string Code = R"(
1157+
void target(int *Ptr) {
1158+
bool Foo = false;
1159+
if (Ptr) {
1160+
Foo = true;
1161+
/*[[p1]]*/
1162+
}
1163+
1164+
(void)0;
1165+
/*[[p2]]*/
1166+
}
1167+
)";
1168+
runDataflow(
1169+
Code, [](llvm::ArrayRef<
1170+
std::pair<std::string, DataflowAnalysisState<NoopLattice>>>
1171+
Results,
1172+
ASTContext &ASTCtx) {
1173+
ASSERT_THAT(Results, ElementsAre(Pair("p2", _), Pair("p1", _)));
1174+
const ValueDecl *FooDecl = findValueDecl(ASTCtx, "Foo");
1175+
ASSERT_THAT(FooDecl, NotNull());
1176+
1177+
const Environment &Env1 = Results[1].second.Env;
1178+
auto &FooVal1 =
1179+
*cast<BoolValue>(Env1.getValue(*FooDecl, SkipPast::Reference));
1180+
EXPECT_TRUE(Env1.flowConditionImplies(FooVal1));
1181+
1182+
const Environment &Env2 = Results[0].second.Env;
1183+
auto &FooVal2 =
1184+
*cast<BoolValue>(Env2.getValue(*FooDecl, SkipPast::Reference));
1185+
EXPECT_FALSE(Env2.flowConditionImplies(FooVal2));
1186+
});
1187+
}
1188+
11551189
} // namespace

0 commit comments

Comments
 (0)