clang 23.0.0git
SummaryAnalysis.h
Go to the documentation of this file.
1//===- SummaryAnalysis.h ----------------------------------------*- C++ -*-===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8//
9// Defines SummaryAnalysisBase (type-erased base known to AnalysisDriver) and
10// the typed intermediate SummaryAnalysis<ResultT, EntitySummaryT> that
11// concrete analyses inherit from.
12//
13//===----------------------------------------------------------------------===//
14
15#ifndef LLVM_CLANG_SCALABLESTATICANALYSISFRAMEWORK_CORE_WHOLEPROGRAMANALYSIS_SUMMARYANALYSIS_H
16#define LLVM_CLANG_SCALABLESTATICANALYSISFRAMEWORK_CORE_WHOLEPROGRAMANALYSIS_SUMMARYANALYSIS_H
17
24#include "llvm/Support/Error.h"
25#include <memory>
26
27namespace clang::ssaf {
28
29class AnalysisDriver;
31
32/// Type-erased base for summary analyses. Known to AnalysisDriver.
33///
34/// Not subclassed directly -- use SummaryAnalysis<ResultT, EntitySummaryT>.
35/// A summary analysis processes per-entity EntitySummary objects from the
36/// LUSummary one at a time, accumulating whole-program data into an
37/// AnalysisResult.
39 friend class AnalysisDriver;
40
41protected:
43
44public:
45 /// SummaryName of the EntitySummary type this analysis consumes.
46 /// Used by the driver to route entities from the LUSummary.
47 virtual SummaryName summaryName() const = 0;
48
49private:
50 /// Called once before any add() calls. Default is a no-op.
51 virtual llvm::Error initialize() { return llvm::Error::success(); }
52
53 /// Called once per matching entity. The driver retains ownership of the
54 /// summary; multiple SummaryAnalysis instances may receive the same entity.
55 virtual llvm::Error add(EntityId Id, const EntitySummary &Summary) = 0;
56
57 /// Called after all entities have been processed. Default is a no-op.
58 virtual llvm::Error finalize() { return llvm::Error::success(); }
59};
60
61/// Typed intermediate that concrete summary analyses inherit from.
62///
63/// Concrete analyses must implement:
64/// llvm::Error add(EntityId Id, const EntitySummaryT &Summary) override;
65/// and may override initialize() and finalize().
66///
67/// The result being built is accessible via result() const & (read-only) and
68/// result() & (mutable) within the analysis implementation.
69template <typename ResultT, typename EntitySummaryT>
71 static_assert(std::is_base_of_v<AnalysisResult, ResultT>,
72 "ResultT must derive from AnalysisResult");
73 static_assert(HasAnalysisName_v<ResultT>,
74 "ResultT must have a static analysisName() method");
75 static_assert(std::is_base_of_v<EntitySummary, EntitySummaryT>,
76 "EntitySummaryT must derive from EntitySummary");
77
78 friend class AnalysisRegistry;
79 using ResultType = ResultT;
80
81 std::unique_ptr<ResultT> Result = std::make_unique<ResultT>();
82
83public:
84 /// Used by AnalysisRegistry::Add to derive the registry entry name.
85 AnalysisName analysisName() const final { return ResultT::analysisName(); }
86
87 SummaryName summaryName() const final {
88 return EntitySummaryT::summaryName();
89 }
90
91 const std::vector<AnalysisName> &dependencyNames() const final {
92 static const std::vector<AnalysisName> Empty;
93 return Empty;
94 }
95
96 /// Called once before the first add() call. Override for initialization.
97 virtual llvm::Error initialize() override { return llvm::Error::success(); }
98
99 /// Called once per matching entity. Implement to accumulate data.
100 virtual llvm::Error add(EntityId Id, const EntitySummaryT &Summary) = 0;
101
102 /// Called after all entities have been processed.
103 /// Override for post-processing.
104 virtual llvm::Error finalize() override { return llvm::Error::success(); }
105
106protected:
107 /// Read-only access to the result being built.
108 const ResultT &result() const & { return *Result; }
109
110 /// Mutable access to the result being built.
111 ResultT &result() & { return *Result; }
112
113private:
114 /// Seals the type-erased base overload, downcasts, and dispatches to the
115 /// typed add().
116 llvm::Error add(EntityId Id, const EntitySummary &Summary) final {
117 return add(Id, static_cast<const EntitySummaryT &>(Summary));
118 }
119
120 /// Type-erased result extraction for the driver.
121 std::unique_ptr<AnalysisResult> result() && final {
122 return std::move(Result);
123 }
124};
125
126} // namespace clang::ssaf
127
128#endif // LLVM_CLANG_SCALABLESTATICANALYSISFRAMEWORK_CORE_WHOLEPROGRAMANALYSIS_SUMMARYANALYSIS_H
Orchestrates whole-program analysis over an LUSummary.
Uniquely identifies a whole-program analysis and the AnalysisResult it produces.
Unified registry for SummaryAnalysis and DerivedAnalysis implementations.
Lightweight opaque handle representing an entity in an EntityIdTable.
Definition EntityId.h:31
Base class for analysis-specific summary data.
virtual SummaryName summaryName() const =0
SummaryName of the EntitySummary type this analysis consumes.
Typed intermediate that concrete summary analyses inherit from.
virtual llvm::Error initialize() override
Called once before the first add() call. Override for initialization.
const ResultT & result() const &
Read-only access to the result being built.
const std::vector< AnalysisName > & dependencyNames() const final
AnalysisNames of all AnalysisResult dependencies.
ResultT & result() &
Mutable access to the result being built.
virtual llvm::Error add(EntityId Id, const EntitySummaryT &Summary)=0
Called once per matching entity. Implement to accumulate data.
virtual llvm::Error finalize() override
Called after all entities have been processed.
AnalysisName analysisName() const final
Used by AnalysisRegistry::Add to derive the registry entry name.
SummaryName summaryName() const final
SummaryName of the EntitySummary type this analysis consumes.
Uniquely identifies an analysis summary.
Definition SummaryName.h:22
constexpr bool HasAnalysisName_v
@ Result
The result type of a method or function.
Definition TypeBase.h:905
void finalize(TemplateInstantiationCallbackPtrs &Callbacks, const Sema &TheSema)