Skip to content

Commit 83486a3

Browse files
committed
[clang][dataflow] Check for backedges directly (instead of loop statements).
Widen on backedge nodes, instead of nodes with a loop statement as terminator. This fixes #67834 and a precision loss from assignment in a loop condition. The commit contains tests for both of these issues.
1 parent 25da115 commit 83486a3

File tree

3 files changed

+51
-20
lines changed

3 files changed

+51
-20
lines changed

clang/lib/Analysis/FlowSensitive/TypeErasedDataflowAnalysis.cpp

Lines changed: 9 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212
//===----------------------------------------------------------------------===//
1313

1414
#include <algorithm>
15-
#include <memory>
1615
#include <optional>
1716
#include <system_error>
1817
#include <utility>
@@ -33,8 +32,8 @@
3332
#include "clang/Analysis/FlowSensitive/TypeErasedDataflowAnalysis.h"
3433
#include "clang/Analysis/FlowSensitive/Value.h"
3534
#include "llvm/ADT/ArrayRef.h"
36-
#include "llvm/ADT/DenseSet.h"
3735
#include "llvm/ADT/STLExtras.h"
36+
#include "llvm/ADT/SmallBitVector.h"
3837
#include "llvm/Support/Debug.h"
3938
#include "llvm/Support/Error.h"
4039

@@ -53,19 +52,8 @@ static int blockIndexInPredecessor(const CFGBlock &Pred,
5352
return BlockPos - Pred.succ_begin();
5453
}
5554

56-
static bool isLoopHead(const CFGBlock &B) {
57-
if (const auto *T = B.getTerminatorStmt())
58-
switch (T->getStmtClass()) {
59-
case Stmt::WhileStmtClass:
60-
case Stmt::DoStmtClass:
61-
case Stmt::ForStmtClass:
62-
case Stmt::CXXForRangeStmtClass:
63-
return true;
64-
default:
65-
return false;
66-
}
67-
68-
return false;
55+
static bool isBackedgeNode(const CFGBlock &B) {
56+
return B.getLoopTarget() != nullptr;
6957
}
7058

7159
namespace {
@@ -502,14 +490,15 @@ runTypeErasedDataflowAnalysis(
502490
PostVisitCFG) {
503491
PrettyStackTraceAnalysis CrashInfo(CFCtx, "runTypeErasedDataflowAnalysis");
504492

505-
PostOrderCFGView POV(&CFCtx.getCFG());
506-
ForwardDataflowWorklist Worklist(CFCtx.getCFG(), &POV);
493+
const clang::CFG &CFG = CFCtx.getCFG();
494+
PostOrderCFGView POV(&CFG);
495+
ForwardDataflowWorklist Worklist(CFG, &POV);
507496

508497
std::vector<std::optional<TypeErasedDataflowAnalysisState>> BlockStates(
509-
CFCtx.getCFG().size());
498+
CFG.size());
510499

511500
// The entry basic block doesn't contain statements so it can be skipped.
512-
const CFGBlock &Entry = CFCtx.getCFG().getEntry();
501+
const CFGBlock &Entry = CFG.getEntry();
513502
BlockStates[Entry.getBlockID()] = {Analysis.typeErasedInitialElement(),
514503
InitEnv.fork()};
515504
Worklist.enqueueSuccessors(&Entry);
@@ -553,7 +542,7 @@ runTypeErasedDataflowAnalysis(
553542
llvm::errs() << "Old Env:\n";
554543
OldBlockState->Env.dump();
555544
});
556-
if (isLoopHead(*Block)) {
545+
if (isBackedgeNode(*Block)) {
557546
LatticeJoinEffect Effect1 = Analysis.widenTypeErased(
558547
NewBlockState.Lattice, OldBlockState->Lattice);
559548
LatticeJoinEffect Effect2 =

clang/unittests/Analysis/FlowSensitive/TransferTest.cpp

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4099,6 +4099,20 @@ TEST(TransferTest, LoopDereferencingChangingRecordPointerConverges) {
40994099
ASSERT_THAT_ERROR(checkDataflowWithNoopAnalysis(Code), llvm::Succeeded());
41004100
}
41014101

4102+
TEST(TransferTest, LoopWithDisjunctiveConditionConverges) {
4103+
std::string Code = R"cc(
4104+
bool foo();
4105+
4106+
void target() {
4107+
bool c = false;
4108+
while (foo() || foo()) {
4109+
c = true;
4110+
}
4111+
}
4112+
)cc";
4113+
ASSERT_THAT_ERROR(checkDataflowWithNoopAnalysis(Code), llvm::Succeeded());
4114+
}
4115+
41024116
TEST(TransferTest, DoesNotCrashOnUnionThisExpr) {
41034117
std::string Code = R"(
41044118
union Union {

clang/unittests/Analysis/FlowSensitive/TypeErasedDataflowAnalysisTest.cpp

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -913,6 +913,34 @@ TEST_F(FlowConditionTest, WhileStmt) {
913913
});
914914
}
915915

916+
TEST_F(FlowConditionTest, WhileStmtWithAssignmentInCondition) {
917+
std::string Code = R"(
918+
void target(bool Foo) {
919+
// This test checks whether the analysis preserves the connection between
920+
// the value of `Foo` and the assignment expression, despite widening.
921+
// The equality operator generates a fresh boolean variable on each
922+
// interpretation, which forces use of widening.
923+
while ((Foo = (3 == 4))) {
924+
(void)0;
925+
/*[[p]]*/
926+
}
927+
}
928+
)";
929+
runDataflow(
930+
Code,
931+
[](const llvm::StringMap<DataflowAnalysisState<NoopLattice>> &Results,
932+
ASTContext &ASTCtx) {
933+
const ValueDecl *FooDecl = findValueDecl(ASTCtx, "Foo");
934+
ASSERT_THAT(FooDecl, NotNull());
935+
936+
ASSERT_THAT(Results.keys(), UnorderedElementsAre("p"));
937+
const Environment &Env = getEnvironmentAtAnnotation(Results, "p");
938+
939+
auto &FooVal = cast<BoolValue>(Env.getValue(*FooDecl))->formula();
940+
EXPECT_TRUE(Env.flowConditionImplies(FooVal));
941+
});
942+
}
943+
916944
TEST_F(FlowConditionTest, Conjunction) {
917945
std::string Code = R"(
918946
void target(bool Foo, bool Bar) {

0 commit comments

Comments
 (0)