| 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_SCALABLESTATICANALYSIS_CORE_SUMMARYDATA_SUMMARYDATABUILDER_H |
| 16 | #define LLVM_CLANG_SCALABLESTATICANALYSIS_CORE_SUMMARYDATA_SUMMARYDATABUILDER_H |
| 17 | |
| 18 | #include "clang/ScalableStaticAnalysis/Core/Model/EntityId.h" |
| 19 | #include "clang/ScalableStaticAnalysis/Core/Model/SummaryName.h" |
| 20 | #include "clang/ScalableStaticAnalysis/Core/SummaryData/SummaryData.h" |
| 21 | #include "clang/ScalableStaticAnalysis/Core/SummaryData/SummaryDataTraits.h" |
| 22 | #include "clang/ScalableStaticAnalysis/Core/TUSummary/EntitySummary.h" |
| 23 | #include <memory> |
| 24 | |
| 25 | namespace clang::ssaf { |
| 26 | |
| 27 | class LUSummaryConsumer; |
| 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 takeData(). |
| 34 | class SummaryDataBuilderBase { |
| 35 | friend class LUSummaryConsumer; |
| 36 | |
| 37 | public: |
| 38 | virtual ~SummaryDataBuilderBase() = default; |
| 39 | |
| 40 | private: |
| 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> takeData() && = 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. |
| 59 | template <typename DataT, typename SummaryT> |
| 60 | class SummaryDataBuilder : public SummaryDataBuilderBase { |
| 61 | static_assert(std::is_base_of_v<SummaryData, DataT>, |
| 62 | "DataT must derive from SummaryData" ); |
| 63 | static_assert(HasSummaryName_v<DataT>, |
| 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 | |
| 70 | public: |
| 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 | |
| 77 | protected: |
| 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 | |
| 83 | private: |
| 84 | std::unique_ptr<SummaryData> takeData() && override { |
| 85 | return std::move(Data); |
| 86 | } |
| 87 | |
| 88 | /// Seals the base overload, downcasts, and dispatches to the typed overload. |
| 89 | void addSummary(EntityId Id, std::unique_ptr<EntitySummary> Summary) final { |
| 90 | addSummary(Id, std::unique_ptr<SummaryT>( |
| 91 | static_cast<SummaryT *>(Summary.release()))); |
| 92 | } |
| 93 | }; |
| 94 | |
| 95 | } // namespace clang::ssaf |
| 96 | |
| 97 | #endif // LLVM_CLANG_SCALABLESTATICANALYSIS_CORE_SUMMARYDATA_SUMMARYDATABUILDER_H |
| 98 | |