Skip to content

Commit 23ae482

Browse files
authored
[clang][dataflow] Allow DataflowAnalysisContext to use a non-owned Solver. (#91316)
For some callers (see change in DataflowAnalysis.h), this is more convenient.
1 parent 8755d24 commit 23ae482

File tree

3 files changed

+29
-10
lines changed

3 files changed

+29
-10
lines changed

clang/include/clang/Analysis/FlowSensitive/DataflowAnalysis.h

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -283,9 +283,8 @@ llvm::Expected<llvm::SmallVector<Diagnostic>> diagnoseFunction(
283283
if (!Context)
284284
return Context.takeError();
285285

286-
auto OwnedSolver = std::make_unique<WatchedLiteralsSolver>(MaxSATIterations);
287-
const WatchedLiteralsSolver *Solver = OwnedSolver.get();
288-
DataflowAnalysisContext AnalysisContext(std::move(OwnedSolver));
286+
auto Solver = std::make_unique<WatchedLiteralsSolver>(MaxSATIterations);
287+
DataflowAnalysisContext AnalysisContext(*Solver);
289288
Environment Env(AnalysisContext, FuncDecl);
290289
AnalysisT Analysis = createAnalysis<AnalysisT>(ASTCtx, Env);
291290
llvm::SmallVector<Diagnostic> Diagnostics;

clang/include/clang/Analysis/FlowSensitive/DataflowAnalysisContext.h

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,19 @@ class DataflowAnalysisContext {
6767
DataflowAnalysisContext(std::unique_ptr<Solver> S,
6868
Options Opts = Options{
6969
/*ContextSensitiveOpts=*/std::nullopt,
70-
/*Logger=*/nullptr});
70+
/*Logger=*/nullptr})
71+
: DataflowAnalysisContext(*S, std::move(S), Opts) {}
72+
73+
/// Constructs a dataflow analysis context.
74+
///
75+
/// Requirements:
76+
///
77+
/// `S` must outlive the `DataflowAnalysisContext`.
78+
DataflowAnalysisContext(Solver &S, Options Opts = Options{
79+
/*ContextSensitiveOpts=*/std::nullopt,
80+
/*Logger=*/nullptr})
81+
: DataflowAnalysisContext(S, nullptr, Opts) {}
82+
7183
~DataflowAnalysisContext();
7284

7385
/// Sets a callback that returns the names and types of the synthetic fields
@@ -209,6 +221,13 @@ class DataflowAnalysisContext {
209221
using DenseMapInfo::isEqual;
210222
};
211223

224+
/// `S` is the solver to use. `OwnedSolver` may be:
225+
/// * Null (in which case `S` is non-onwed and must outlive this object), or
226+
/// * Non-null (in which case it must refer to `S`, and the
227+
/// `DataflowAnalysisContext will take ownership of `OwnedSolver`).
228+
DataflowAnalysisContext(Solver &S, std::unique_ptr<Solver> &&OwnedSolver,
229+
Options Opts);
230+
212231
// Extends the set of modeled field declarations.
213232
void addModeledFields(const FieldSet &Fields);
214233

@@ -232,7 +251,8 @@ class DataflowAnalysisContext {
232251
Solver::Result::Status::Unsatisfiable;
233252
}
234253

235-
std::unique_ptr<Solver> S;
254+
Solver &S;
255+
std::unique_ptr<Solver> OwnedSolver;
236256
std::unique_ptr<Arena> A;
237257

238258
// Maps from program declarations and statements to storage locations that are

clang/lib/Analysis/FlowSensitive/DataflowAnalysisContext.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,7 @@ DataflowAnalysisContext::joinFlowConditions(Atom FirstToken,
170170

171171
Solver::Result DataflowAnalysisContext::querySolver(
172172
llvm::SetVector<const Formula *> Constraints) {
173-
return S->solve(Constraints.getArrayRef());
173+
return S.solve(Constraints.getArrayRef());
174174
}
175175

176176
bool DataflowAnalysisContext::flowConditionImplies(Atom Token,
@@ -338,10 +338,10 @@ static std::unique_ptr<Logger> makeLoggerFromCommandLine() {
338338
return Logger::html(std::move(StreamFactory));
339339
}
340340

341-
DataflowAnalysisContext::DataflowAnalysisContext(std::unique_ptr<Solver> S,
342-
Options Opts)
343-
: S(std::move(S)), A(std::make_unique<Arena>()), Opts(Opts) {
344-
assert(this->S != nullptr);
341+
DataflowAnalysisContext::DataflowAnalysisContext(
342+
Solver &S, std::unique_ptr<Solver> &&OwnedSolver, Options Opts)
343+
: S(S), OwnedSolver(std::move(OwnedSolver)), A(std::make_unique<Arena>()),
344+
Opts(Opts) {
345345
// If the -dataflow-log command-line flag was set, synthesize a logger.
346346
// This is ugly but provides a uniform method for ad-hoc debugging dataflow-
347347
// based tools.

0 commit comments

Comments
 (0)