clang 23.0.0git
SummaryDataBuilder.h
Go to the documentation of this file.
1//===- SummaryDataBuilder.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// This file defines SummaryDataBuilderBase (abstract base known to the
10// registry and LUSummaryConsumer) and the typed intermediate template
11// SummaryDataBuilder<DataT, SummaryT> that concrete builders inherit from.
12//
13//===----------------------------------------------------------------------===//
14
15#ifndef LLVM_CLANG_SCALABLESTATICANALYSISFRAMEWORK_CORE_SUMMARYDATA_SUMMARYDATABUILDER_H
16#define LLVM_CLANG_SCALABLESTATICANALYSISFRAMEWORK_CORE_SUMMARYDATA_SUMMARYDATABUILDER_H
17
23#include <memory>
24
25namespace clang::ssaf {
26
28
29/// Abstract base class for all summary data builders.
30///
31/// Known to the registry and LUSummaryConsumer. Receives entities one at a
32/// time via \c addSummary(), is finalized via \c finalize(), and transfers
33/// ownership of the built data via \c getData().
35 friend class LUSummaryConsumer;
36
37public:
38 virtual ~SummaryDataBuilderBase() = default;
39
40private:
41 /// Called once per entity belonging to this builder's analysis.
42 /// Takes ownership of the summary data.
43 virtual void addSummary(EntityId Id,
44 std::unique_ptr<EntitySummary> Summary) = 0;
45
46 /// Called after all entities have been added.
47 virtual void finalize() {}
48
49 /// Transfers ownership of the built data. Called by LUSummaryConsumer after
50 /// finalize(). The rvalue ref-qualifier enforces single use — the builder
51 /// cannot be accessed after this call.
52 virtual std::unique_ptr<SummaryData> getData() && = 0;
53};
54
55/// Typed intermediate template that concrete builders inherit from.
56/// Concrete builders must implement the typed
57/// \c addSummary(EntityId, unique_ptr<SummaryT>) overload, and may override
58/// \c finalize() for any post-processing needed after all entities are added.
59template <typename DataT, typename SummaryT>
61 static_assert(std::is_base_of_v<SummaryData, DataT>,
62 "DataT must derive from SummaryData");
63 static_assert(HasSummaryName<DataT>::value,
64 "DataT must have a static summaryName() method");
65 static_assert(std::is_base_of_v<EntitySummary, SummaryT>,
66 "SummaryT must derive from EntitySummary");
67
68 std::unique_ptr<DataT> Data;
69
70public:
71 SummaryDataBuilder() : Data(std::make_unique<DataT>()) {}
72
73 /// Returns the SummaryName of the data this builder produces.
74 /// Used by SummaryDataBuilderRegistry::Add to derive the registry entry name.
75 static SummaryName summaryName() { return DataT::summaryName(); }
76
77protected:
78 /// Typed customization point — concrete builders override this.
79 virtual void addSummary(EntityId Id, std::unique_ptr<SummaryT> Summary) = 0;
80
81 DataT &getData() & { return *Data; }
82
83private:
84 std::unique_ptr<SummaryData> getData() && override { return std::move(Data); }
85
86 /// Seals the base overload, downcasts, and dispatches to the typed overload.
87 void addSummary(EntityId Id, std::unique_ptr<EntitySummary> Summary) final {
88 addSummary(Id, std::unique_ptr<SummaryT>(
89 static_cast<SummaryT *>(Summary.release())));
90 }
91};
92
93} // namespace clang::ssaf
94
95#endif // LLVM_CLANG_SCALABLESTATICANALYSISFRAMEWORK_CORE_SUMMARYDATA_SUMMARYDATABUILDER_H
Lightweight opaque handle representing an entity in an EntityIdTable.
Definition EntityId.h:31
Consumes a LUSummary by dispatching its entity data to registered SummaryDataBuilders and returning t...
Abstract base class for all summary data builders.
virtual ~SummaryDataBuilderBase()=default
static SummaryName summaryName()
Returns the SummaryName of the data this builder produces.
virtual void addSummary(EntityId Id, std::unique_ptr< SummaryT > Summary)=0
Typed customization point — concrete builders override this.
Uniquely identifies an analysis summary.
Definition SummaryName.h:22
Type trait that checks whether T has a static summaryName() method returning SummaryName.