Skip to content

Commit a1a63d6

Browse files
committed
[clang][dataflow] Add two repros for non-convergence involving pointers in loops.
These are broken out from https://reviews.llvm.org/D156658, which it now seems obvious isn't the right way to solve the non-convergence. Instead, my plan is to address the non-convergence through pointer value widening, but the exact way this should be implemented is TBD. In the meantime, I think there's value in getting these repros submitted to record the current undesirable behavior. Reviewed By: ymandel, xazax.hun Differential Revision: https://reviews.llvm.org/D158513
1 parent ff14585 commit a1a63d6

File tree

2 files changed

+50
-12
lines changed

2 files changed

+50
-12
lines changed

clang/unittests/Analysis/FlowSensitive/TestingSupport.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -402,8 +402,8 @@ llvm::Error checkDataflowWithNoopAnalysis(
402402
std::function<
403403
void(const llvm::StringMap<DataflowAnalysisState<NoopLattice>> &,
404404
ASTContext &)>
405-
VerifyResults,
406-
DataflowAnalysisOptions Options,
405+
VerifyResults = [](const auto &, auto &) {},
406+
DataflowAnalysisOptions Options = {BuiltinOptions()},
407407
LangStandard::Kind Std = LangStandard::lang_cxx17,
408408
llvm::StringRef TargetFun = "target");
409409

clang/unittests/Analysis/FlowSensitive/TransferTest.cpp

Lines changed: 48 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2667,11 +2667,7 @@ TEST(TransferTest, CannotAnalyzeFunctionTemplate) {
26672667
void target() {}
26682668
)";
26692669
ASSERT_THAT_ERROR(
2670-
checkDataflowWithNoopAnalysis(
2671-
Code,
2672-
[](const llvm::StringMap<DataflowAnalysisState<NoopLattice>> &Results,
2673-
ASTContext &ASTCtx) {},
2674-
{BuiltinOptions()}),
2670+
checkDataflowWithNoopAnalysis(Code),
26752671
llvm::FailedWithMessage("Cannot analyze templated declarations"));
26762672
}
26772673

@@ -2683,11 +2679,7 @@ TEST(TransferTest, CannotAnalyzeMethodOfClassTemplate) {
26832679
};
26842680
)";
26852681
ASSERT_THAT_ERROR(
2686-
checkDataflowWithNoopAnalysis(
2687-
Code,
2688-
[](const llvm::StringMap<DataflowAnalysisState<NoopLattice>> &Results,
2689-
ASTContext &ASTCtx) {},
2690-
{BuiltinOptions()}),
2682+
checkDataflowWithNoopAnalysis(Code),
26912683
llvm::FailedWithMessage("Cannot analyze templated declarations"));
26922684
}
26932685

@@ -3836,6 +3828,52 @@ TEST(TransferTest, LoopWithStructReferenceAssignmentConverges) {
38363828
});
38373829
}
38383830

3831+
TEST(TransferTest, LoopDereferencingChangingPointerConverges) {
3832+
std::string Code = R"cc(
3833+
bool some_condition();
3834+
3835+
void target(int i1, int i2) {
3836+
int *p = &i1;
3837+
while (true) {
3838+
(void)*p;
3839+
if (some_condition())
3840+
p = &i1;
3841+
else
3842+
p = &i2;
3843+
}
3844+
}
3845+
)cc";
3846+
// FIXME: Implement pointer value widening to make analysis converge.
3847+
ASSERT_THAT_ERROR(
3848+
checkDataflowWithNoopAnalysis(Code),
3849+
llvm::FailedWithMessage("maximum number of iterations reached"));
3850+
}
3851+
3852+
TEST(TransferTest, LoopDereferencingChangingRecordPointerConverges) {
3853+
std::string Code = R"cc(
3854+
struct Lookup {
3855+
int x;
3856+
};
3857+
3858+
bool some_condition();
3859+
3860+
void target(Lookup l1, Lookup l2) {
3861+
Lookup *l = &l1;
3862+
while (true) {
3863+
(void)l->x;
3864+
if (some_condition())
3865+
l = &l1;
3866+
else
3867+
l = &l2;
3868+
}
3869+
}
3870+
)cc";
3871+
// FIXME: Implement pointer value widening to make analysis converge.
3872+
ASSERT_THAT_ERROR(
3873+
checkDataflowWithNoopAnalysis(Code),
3874+
llvm::FailedWithMessage("maximum number of iterations reached"));
3875+
}
3876+
38393877
TEST(TransferTest, DoesNotCrashOnUnionThisExpr) {
38403878
std::string Code = R"(
38413879
union Union {

0 commit comments

Comments
 (0)