| 1 | //===- SummaryDataBuilderRegistry.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 | // Registry for SummaryDataBuilders. |
| 10 | // |
| 11 | // To register a builder, add a static Add<BuilderT> in the builder's |
| 12 | // translation unit: |
| 13 | // |
| 14 | // static SummaryDataBuilderRegistry::Add<MyDataBuilder> |
| 15 | // Registered("Data builder for MyAnalysis"); |
| 16 | // |
| 17 | // The registry entry name is derived automatically from |
| 18 | // MyDataBuilder::summaryName(), which returns MyData::summaryName(). |
| 19 | // |
| 20 | //===----------------------------------------------------------------------===// |
| 21 | |
| 22 | #ifndef LLVM_CLANG_SCALABLESTATICANALYSISFRAMEWORK_CORE_SUMMARYDATA_SUMMARYDATABUILDERREGISTRY_H |
| 23 | #define LLVM_CLANG_SCALABLESTATICANALYSISFRAMEWORK_CORE_SUMMARYDATA_SUMMARYDATABUILDERREGISTRY_H |
| 24 | |
| 25 | #include "clang/ScalableStaticAnalysisFramework/Core/SummaryData/SummaryDataBuilder.h" |
| 26 | #include "llvm/Support/Registry.h" |
| 27 | #include <memory> |
| 28 | #include <string> |
| 29 | |
| 30 | namespace clang::ssaf { |
| 31 | |
| 32 | /// Registry for SummaryDataBuilder implementations. |
| 33 | /// |
| 34 | /// Provides an Add helper that derives the registry entry name from |
| 35 | /// BuilderT::summaryName(), eliminating the possibility of registering a |
| 36 | /// builder under the wrong name. |
| 37 | class SummaryDataBuilderRegistry { |
| 38 | using RegistryT = llvm::Registry<SummaryDataBuilderBase>; |
| 39 | |
| 40 | SummaryDataBuilderRegistry() = delete; |
| 41 | |
| 42 | public: |
| 43 | /// Registers \p BuilderT under the name returned by |
| 44 | /// \c BuilderT::summaryName(). Only a description is required. |
| 45 | /// |
| 46 | /// \c Add objects must be declared \c static at namespace scope — they |
| 47 | /// register an entry in a global linked list on construction and are |
| 48 | /// not copyable or movable. |
| 49 | template <typename BuilderT> struct Add { |
| 50 | explicit Add(llvm::StringRef Desc) |
| 51 | : Name(BuilderT::summaryName().str().str()), Node(Name, Desc) {} |
| 52 | |
| 53 | Add(const Add &) = delete; |
| 54 | Add &operator=(const Add &) = delete; |
| 55 | |
| 56 | private: |
| 57 | std::string Name; |
| 58 | RegistryT::Add<BuilderT> Node; |
| 59 | }; |
| 60 | |
| 61 | /// Returns true if a builder is registered under \p Name. |
| 62 | static bool contains(llvm::StringRef Name); |
| 63 | |
| 64 | /// Instantiates the builder registered under \p Name, or returns nullptr |
| 65 | /// if no such builder is registered. |
| 66 | static std::unique_ptr<SummaryDataBuilderBase> |
| 67 | instantiate(llvm::StringRef Name); |
| 68 | }; |
| 69 | |
| 70 | } // namespace clang::ssaf |
| 71 | |
| 72 | #endif // LLVM_CLANG_SCALABLESTATICANALYSISFRAMEWORK_CORE_SUMMARYDATA_SUMMARYDATABUILDERREGISTRY_H |
| 73 | |