[clang] a1a63d6 - [clang][dataflow] Add two repros for non-convergence involving pointers in loops.
Martin Braenne via cfe-commits
cfe-commits at lists.llvm.org
Wed Aug 23 00:03:23 PDT 2023
Author: Martin Braenne
Date: 2023-08-23T07:03:16Z
New Revision: a1a63d68a46882e051eedcb632723e15f2ee331b
URL: https://github.com/llvm/llvm-project/commit/a1a63d68a46882e051eedcb632723e15f2ee331b
DIFF: https://github.com/llvm/llvm-project/commit/a1a63d68a46882e051eedcb632723e15f2ee331b.diff
LOG: [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
Added:
Modified:
clang/unittests/Analysis/FlowSensitive/TestingSupport.h
clang/unittests/Analysis/FlowSensitive/TransferTest.cpp
Removed:
################################################################################
diff --git a/clang/unittests/Analysis/FlowSensitive/TestingSupport.h b/clang/unittests/Analysis/FlowSensitive/TestingSupport.h
index 11dc4ed76aa4fc..44dbf27a745867 100644
--- a/clang/unittests/Analysis/FlowSensitive/TestingSupport.h
+++ b/clang/unittests/Analysis/FlowSensitive/TestingSupport.h
@@ -402,8 +402,8 @@ llvm::Error checkDataflowWithNoopAnalysis(
std::function<
void(const llvm::StringMap<DataflowAnalysisState<NoopLattice>> &,
ASTContext &)>
- VerifyResults,
- DataflowAnalysisOptions Options,
+ VerifyResults = [](const auto &, auto &) {},
+ DataflowAnalysisOptions Options = {BuiltinOptions()},
LangStandard::Kind Std = LangStandard::lang_cxx17,
llvm::StringRef TargetFun = "target");
diff --git a/clang/unittests/Analysis/FlowSensitive/TransferTest.cpp b/clang/unittests/Analysis/FlowSensitive/TransferTest.cpp
index 117230e14aa9e9..4c31de3c8085bd 100644
--- a/clang/unittests/Analysis/FlowSensitive/TransferTest.cpp
+++ b/clang/unittests/Analysis/FlowSensitive/TransferTest.cpp
@@ -2667,11 +2667,7 @@ TEST(TransferTest, CannotAnalyzeFunctionTemplate) {
void target() {}
)";
ASSERT_THAT_ERROR(
- checkDataflowWithNoopAnalysis(
- Code,
- [](const llvm::StringMap<DataflowAnalysisState<NoopLattice>> &Results,
- ASTContext &ASTCtx) {},
- {BuiltinOptions()}),
+ checkDataflowWithNoopAnalysis(Code),
llvm::FailedWithMessage("Cannot analyze templated declarations"));
}
@@ -2683,11 +2679,7 @@ TEST(TransferTest, CannotAnalyzeMethodOfClassTemplate) {
};
)";
ASSERT_THAT_ERROR(
- checkDataflowWithNoopAnalysis(
- Code,
- [](const llvm::StringMap<DataflowAnalysisState<NoopLattice>> &Results,
- ASTContext &ASTCtx) {},
- {BuiltinOptions()}),
+ checkDataflowWithNoopAnalysis(Code),
llvm::FailedWithMessage("Cannot analyze templated declarations"));
}
@@ -3836,6 +3828,52 @@ TEST(TransferTest, LoopWithStructReferenceAssignmentConverges) {
});
}
+TEST(TransferTest, LoopDereferencingChangingPointerConverges) {
+ std::string Code = R"cc(
+ bool some_condition();
+
+ void target(int i1, int i2) {
+ int *p = &i1;
+ while (true) {
+ (void)*p;
+ if (some_condition())
+ p = &i1;
+ else
+ p = &i2;
+ }
+ }
+ )cc";
+ // FIXME: Implement pointer value widening to make analysis converge.
+ ASSERT_THAT_ERROR(
+ checkDataflowWithNoopAnalysis(Code),
+ llvm::FailedWithMessage("maximum number of iterations reached"));
+}
+
+TEST(TransferTest, LoopDereferencingChangingRecordPointerConverges) {
+ std::string Code = R"cc(
+ struct Lookup {
+ int x;
+ };
+
+ bool some_condition();
+
+ void target(Lookup l1, Lookup l2) {
+ Lookup *l = &l1;
+ while (true) {
+ (void)l->x;
+ if (some_condition())
+ l = &l1;
+ else
+ l = &l2;
+ }
+ }
+ )cc";
+ // FIXME: Implement pointer value widening to make analysis converge.
+ ASSERT_THAT_ERROR(
+ checkDataflowWithNoopAnalysis(Code),
+ llvm::FailedWithMessage("maximum number of iterations reached"));
+}
+
TEST(TransferTest, DoesNotCrashOnUnionThisExpr) {
std::string Code = R"(
union Union {
More information about the cfe-commits
mailing list