clang 23.0.0git
TUSummaryBuilder.h
Go to the documentation of this file.
1//===- TUSummaryBuilder.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#ifndef LLVM_CLANG_SCALABLESTATICANALYSIS_CORE_TUSUMMARY_TUSUMMARYBUILDER_H
10#define LLVM_CLANG_SCALABLESTATICANALYSIS_CORE_TUSUMMARY_TUSUMMARYBUILDER_H
11
15#include <memory>
16#include <utility>
17
18namespace clang::ssaf {
19
20class EntityName;
21class SSAFOptions;
22class TUSummary;
23
25public:
26 TUSummaryBuilder(TUSummary &Summary, const SSAFOptions &Options)
27 : Summary(Summary), Options(Options) {}
28
30
31 /// Associate the \p Data \c EntitySummary with the \p Entity.
32 /// This consumes the \p Data only if \p Entity wasn't associated yet with the
33 /// same kind of \c EntitySummary.
34 /// \returns a pointer to the \c EntitySummary and whether it inserted or not.
35 template <typename ConcreteEntitySummary,
37 std::pair<EntitySummary *, bool>
38 addSummary(EntityId Entity, std::unique_ptr<ConcreteEntitySummary> &&Data);
39
40 /// \returns the \c SSAFOptions of this builder.
41 const SSAFOptions &getOptions() const { return Options; }
42
43private:
44 TUSummary &Summary;
45 const SSAFOptions &Options;
46
47 std::pair<EntitySummary *, bool>
48 addSummaryImpl(EntityId Entity, std::unique_ptr<EntitySummary> &&Data);
49};
50
51// Why is this a template?
52//
53// We use template here to avoid an implicit conversion from
54// `std::unique_ptr<ConcreteEntitySummary>` to `std::unique_ptr<EntitySummary>`
55// because constructing that implicit temporary would unconditionally "consume"
56// the Data. This would make it impossible to recover from the call-site the
57// Data you pass in even if no insertion happens.
58template <typename ConcreteEntitySummary,
60std::pair<EntitySummary *, bool>
62 std::unique_ptr<ConcreteEntitySummary> &&Data) {
63 // Prepare a unique_ptr of the base type to avoid implicit conversions at the
64 // call-site.
65 std::unique_ptr<EntitySummary> TypeErasedData = std::move(Data);
66
67 // This only moves (consumes) TypeErasedData if insertion happened.
68 // Otherwise it doesn't touch the `TypeErasedData`.
69 auto [It, Inserted] = addSummaryImpl(Entity, std::move(TypeErasedData));
70
71 // Move it back on failure to keep the `Data` unconsumed.
72 if (!Inserted) {
73 Data = std::unique_ptr<ConcreteEntitySummary>(
74 static_cast<ConcreteEntitySummary *>(TypeErasedData.release()));
75 }
76 return {It, Inserted};
77}
78
79} // namespace clang::ssaf
80
81#endif // LLVM_CLANG_SCALABLESTATICANALYSIS_CORE_TUSUMMARY_TUSUMMARYBUILDER_H
Lightweight opaque handle representing an entity in an EntityIdTable.
Definition EntityId.h:31
Uniquely identifies an entity in a program.
Definition EntityName.h:28
std::pair< EntitySummary *, bool > addSummary(EntityId Entity, std::unique_ptr< ConcreteEntitySummary > &&Data)
Associate the Data EntitySummary with the Entity.
EntityId addEntity(const EntityName &EN, EntityLinkageType Linkage)
TUSummaryBuilder(TUSummary &Summary, const SSAFOptions &Options)
const SSAFOptions & getOptions() const
Data extracted for a given translation unit and for a given set of analyses.
Definition TUSummary.h:25
std::enable_if_t< std::is_base_of_v< EntitySummary, Derived > > DerivesFromEntitySummary
Linkage
Describes the different kinds of linkage (C++ [basic.link], C99 6.2.2) that an entity may have.
Definition Linkage.h:24