[clang] [clang][dataflow] Allow `DataflowAnalysisContext` to use a non-owned `Solver`. (PR #91316)
via cfe-commits
cfe-commits at lists.llvm.org
Tue May 7 05:53:46 PDT 2024
https://github.com/martinboehme updated https://github.com/llvm/llvm-project/pull/91316
>From abb7d53778394a220353deeeb86fbd06bf4352c2 Mon Sep 17 00:00:00 2001
From: Martin Braenne <mboehme at google.com>
Date: Tue, 7 May 2024 10:23:16 +0000
Subject: [PATCH 1/2] [clang][dataflow] Allow `DataflowAnalysisContext` to use
a non-owned `Solver`.
For some callers (see change in DataflowAnalysis.h), this is more convenient.
---
.../Analysis/FlowSensitive/DataflowAnalysis.h | 5 ++---
.../FlowSensitive/DataflowAnalysisContext.h | 20 +++++++++++++++++--
.../FlowSensitive/DataflowAnalysisContext.cpp | 10 +++++-----
3 files changed, 25 insertions(+), 10 deletions(-)
diff --git a/clang/include/clang/Analysis/FlowSensitive/DataflowAnalysis.h b/clang/include/clang/Analysis/FlowSensitive/DataflowAnalysis.h
index 67eccdd030dcdd..763af244547647 100644
--- a/clang/include/clang/Analysis/FlowSensitive/DataflowAnalysis.h
+++ b/clang/include/clang/Analysis/FlowSensitive/DataflowAnalysis.h
@@ -283,9 +283,8 @@ llvm::Expected<llvm::SmallVector<Diagnostic>> diagnoseFunction(
if (!Context)
return Context.takeError();
- auto OwnedSolver = std::make_unique<WatchedLiteralsSolver>(MaxSATIterations);
- const WatchedLiteralsSolver *Solver = OwnedSolver.get();
- DataflowAnalysisContext AnalysisContext(std::move(OwnedSolver));
+ auto Solver = std::make_unique<WatchedLiteralsSolver>(MaxSATIterations);
+ DataflowAnalysisContext AnalysisContext(*Solver);
Environment Env(AnalysisContext, FuncDecl);
AnalysisT Analysis = createAnalysis<AnalysisT>(ASTCtx, Env);
llvm::SmallVector<Diagnostic> Diagnostics;
diff --git a/clang/include/clang/Analysis/FlowSensitive/DataflowAnalysisContext.h b/clang/include/clang/Analysis/FlowSensitive/DataflowAnalysisContext.h
index aa2c366cb164a9..13a74281e02b5d 100644
--- a/clang/include/clang/Analysis/FlowSensitive/DataflowAnalysisContext.h
+++ b/clang/include/clang/Analysis/FlowSensitive/DataflowAnalysisContext.h
@@ -67,7 +67,19 @@ class DataflowAnalysisContext {
DataflowAnalysisContext(std::unique_ptr<Solver> S,
Options Opts = Options{
/*ContextSensitiveOpts=*/std::nullopt,
- /*Logger=*/nullptr});
+ /*Logger=*/nullptr})
+ : DataflowAnalysisContext(*S, std::move(S), Opts) {}
+
+ /// Constructs a dataflow analysis context.
+ ///
+ /// Requirements:
+ ///
+ /// `S` must outlive the `DataflowAnalysisContext`.
+ DataflowAnalysisContext(Solver &S, Options Opts = Options{
+ /*ContextSensitiveOpts=*/std::nullopt,
+ /*Logger=*/nullptr})
+ : DataflowAnalysisContext(S, nullptr, Opts) {}
+
~DataflowAnalysisContext();
/// Sets a callback that returns the names and types of the synthetic fields
@@ -209,6 +221,9 @@ class DataflowAnalysisContext {
using DenseMapInfo::isEqual;
};
+ DataflowAnalysisContext(Solver &S, std::unique_ptr<Solver> OwnedSolver,
+ Options Opts);
+
// Extends the set of modeled field declarations.
void addModeledFields(const FieldSet &Fields);
@@ -232,7 +247,8 @@ class DataflowAnalysisContext {
Solver::Result::Status::Unsatisfiable;
}
- std::unique_ptr<Solver> S;
+ Solver &S;
+ std::unique_ptr<Solver> OwnedSolver;
std::unique_ptr<Arena> A;
// Maps from program declarations and statements to storage locations that are
diff --git a/clang/lib/Analysis/FlowSensitive/DataflowAnalysisContext.cpp b/clang/lib/Analysis/FlowSensitive/DataflowAnalysisContext.cpp
index e94fd39c45dc15..3041dcf6d500c5 100644
--- a/clang/lib/Analysis/FlowSensitive/DataflowAnalysisContext.cpp
+++ b/clang/lib/Analysis/FlowSensitive/DataflowAnalysisContext.cpp
@@ -170,7 +170,7 @@ DataflowAnalysisContext::joinFlowConditions(Atom FirstToken,
Solver::Result DataflowAnalysisContext::querySolver(
llvm::SetVector<const Formula *> Constraints) {
- return S->solve(Constraints.getArrayRef());
+ return S.solve(Constraints.getArrayRef());
}
bool DataflowAnalysisContext::flowConditionImplies(Atom Token,
@@ -338,10 +338,10 @@ static std::unique_ptr<Logger> makeLoggerFromCommandLine() {
return Logger::html(std::move(StreamFactory));
}
-DataflowAnalysisContext::DataflowAnalysisContext(std::unique_ptr<Solver> S,
- Options Opts)
- : S(std::move(S)), A(std::make_unique<Arena>()), Opts(Opts) {
- assert(this->S != nullptr);
+DataflowAnalysisContext::DataflowAnalysisContext(
+ Solver &S, std::unique_ptr<Solver> OwnedSolver, Options Opts)
+ : S(S), OwnedSolver(std::move(OwnedSolver)), A(std::make_unique<Arena>()),
+ Opts(Opts) {
// If the -dataflow-log command-line flag was set, synthesize a logger.
// This is ugly but provides a uniform method for ad-hoc debugging dataflow-
// based tools.
>From 6112536c0f400c10b919532d2b171c37d0d435ac Mon Sep 17 00:00:00 2001
From: Martin Braenne <mboehme at google.com>
Date: Tue, 7 May 2024 12:53:16 +0000
Subject: [PATCH 2/2] fixup! [clang][dataflow] Allow `DataflowAnalysisContext`
to use a non-owned `Solver`.
---
.../clang/Analysis/FlowSensitive/DataflowAnalysisContext.h | 6 +++++-
.../lib/Analysis/FlowSensitive/DataflowAnalysisContext.cpp | 2 +-
2 files changed, 6 insertions(+), 2 deletions(-)
diff --git a/clang/include/clang/Analysis/FlowSensitive/DataflowAnalysisContext.h b/clang/include/clang/Analysis/FlowSensitive/DataflowAnalysisContext.h
index 13a74281e02b5d..5be4a1145f40d7 100644
--- a/clang/include/clang/Analysis/FlowSensitive/DataflowAnalysisContext.h
+++ b/clang/include/clang/Analysis/FlowSensitive/DataflowAnalysisContext.h
@@ -221,7 +221,11 @@ class DataflowAnalysisContext {
using DenseMapInfo::isEqual;
};
- DataflowAnalysisContext(Solver &S, std::unique_ptr<Solver> OwnedSolver,
+ /// `S` is the solver to use. `OwnedSolver` may be:
+ /// * Null (in which case `S` is non-onwed and must outlive this object), or
+ /// * Non-null (in which case it must refer to `S`, and the
+ /// `DataflowAnalysisContext will take ownership of `OwnedSolver`).
+ DataflowAnalysisContext(Solver &S, std::unique_ptr<Solver> &&OwnedSolver,
Options Opts);
// Extends the set of modeled field declarations.
diff --git a/clang/lib/Analysis/FlowSensitive/DataflowAnalysisContext.cpp b/clang/lib/Analysis/FlowSensitive/DataflowAnalysisContext.cpp
index 3041dcf6d500c5..4b86daa56d7b52 100644
--- a/clang/lib/Analysis/FlowSensitive/DataflowAnalysisContext.cpp
+++ b/clang/lib/Analysis/FlowSensitive/DataflowAnalysisContext.cpp
@@ -339,7 +339,7 @@ static std::unique_ptr<Logger> makeLoggerFromCommandLine() {
}
DataflowAnalysisContext::DataflowAnalysisContext(
- Solver &S, std::unique_ptr<Solver> OwnedSolver, Options Opts)
+ Solver &S, std::unique_ptr<Solver> &&OwnedSolver, Options Opts)
: S(S), OwnedSolver(std::move(OwnedSolver)), A(std::make_unique<Arena>()),
Opts(Opts) {
// If the -dataflow-log command-line flag was set, synthesize a logger.
More information about the cfe-commits
mailing list