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