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 getSummaryName() 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 getResult() const & (read-only) and
68/// getResult() & (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 getAnalysisName() const final { return ResultT::analysisName(); }
86
88 return EntitySummaryT::summaryName();
89 }
90
91 const std::vector<AnalysisName> &getDependencyNames() const final {
92 static const std::vector<AnalysisName> Empty;
93 return Empty;
94 }
95
96 /// Called once per matching entity. Implement to accumulate data.
97 virtual llvm::Error add(EntityId Id, const EntitySummaryT &Summary) = 0;
98
99protected:
100 /// Read-only access to the result being built.
101 const ResultT &getResult() const & { return *Result; }
102
103 /// Mutable access to the result being built.
104 ResultT &getResult() & { return *Result; }
105
106private:
107 /// Seals the type-erased base overload, downcasts, and dispatches to the
108 /// typed add().
109 llvm::Error add(EntityId Id, const EntitySummary &Summary) final {
110 return add(Id, static_cast<const EntitySummaryT &>(Summary));
111 }
112
113 /// Type-erased result extraction for the driver.
114 std::unique_ptr<AnalysisResult> takeResult() && final {
115 return std::move(Result);
116 }
117};
118
119} // namespace clang::ssaf
120
121#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 getSummaryName() const =0
SummaryName of the EntitySummary type this analysis consumes.
Typed intermediate that concrete summary analyses inherit from.
const std::vector< AnalysisName > & getDependencyNames() const final
AnalysisNames of all AnalysisResult dependencies.
const ResultT & getResult() const &
Read-only access to the result being built.
AnalysisName getAnalysisName() const final
Used by AnalysisRegistry::Add to derive the registry entry name.
virtual llvm::Error add(EntityId Id, const EntitySummaryT &Summary)=0
Called once per matching entity. Implement to accumulate data.
ResultT & getResult() &
Mutable access to the result being built.
SummaryName getSummaryName() 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)