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_SCALABLESTATICANALYSISFRAMEWORK_CORE_TUSUMMARY_TUSUMMARYBUILDER_H
10#define LLVM_CLANG_SCALABLESTATICANALYSISFRAMEWORK_CORE_TUSUMMARY_TUSUMMARYBUILDER_H
11
15#include <memory>
16#include <utility>
17
18namespace clang::ssaf {
19
20class EntityName;
21class TUSummary;
22
24public:
25 explicit TUSummaryBuilder(TUSummary &Summary) : Summary(Summary) {}
26
28
29 /// Associate the \p Data \c EntitySummary with the \p Entity.
30 /// This consumes the \p Data only if \p Entity wasn't associated yet with the
31 /// same kind of \c EntitySummary.
32 /// \returns a pointer to the \c EntitySummary and whether it inserted or not.
33 template <typename ConcreteEntitySummary,
35 std::pair<EntitySummary *, bool>
36 addSummary(EntityId Entity, std::unique_ptr<ConcreteEntitySummary> &&Data);
37
38private:
39 TUSummary &Summary;
40
41 std::pair<EntitySummary *, bool>
42 addSummaryImpl(EntityId Entity, std::unique_ptr<EntitySummary> &&Data);
43};
44
45// Why is this a template?
46//
47// We use template here to avoid an implicit conversion from
48// `std::unique_ptr<ConcreteEntitySummary>` to `std::unique_ptr<EntitySummary>`
49// because constructing that implicit temporary would unconditionally "consume"
50// the Data. This would make it impossible to recover from the call-site the
51// Data you pass in even if no insertion happens.
52template <typename ConcreteEntitySummary,
54std::pair<EntitySummary *, bool>
56 std::unique_ptr<ConcreteEntitySummary> &&Data) {
57 // Prepare a unique_ptr of the base type to avoid implicit conversions at the
58 // call-site.
59 std::unique_ptr<EntitySummary> TypeErasedData = std::move(Data);
60
61 // This only moves (consumes) TypeErasedData if insertion happened.
62 // Otherwise it doesn't touch the `TypeErasedData`.
63 auto [It, Inserted] = addSummaryImpl(Entity, std::move(TypeErasedData));
64
65 // Move it back on failure to keep the `Data` unconsumed.
66 if (!Inserted) {
67 Data = std::unique_ptr<ConcreteEntitySummary>(
68 static_cast<ConcreteEntitySummary *>(TypeErasedData.release()));
69 }
70 return {It, Inserted};
71}
72
73} // namespace clang::ssaf
74
75#endif // LLVM_CLANG_SCALABLESTATICANALYSISFRAMEWORK_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)
Data extracted for a given translation unit and for a given set of analyses.
Definition TUSummary.h:24
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