clang 23.0.0git
EntityLinker.cpp
Go to the documentation of this file.
1//===- EntityLinker.cpp ---------------------------------------------------===//
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
16#include <cassert>
17
18using namespace clang::ssaf;
19
20//===----------------------------------------------------------------------===//
21// Error Message Constants
22//===----------------------------------------------------------------------===//
23
24namespace ErrorMessages {
25
26static constexpr const char *EntityLinkerFatalErrorPrefix =
27 "EntityLinker: Corrupted TUSummary or logic bug";
28
29static constexpr const char *EntityAlreadyExistsInLinkageTable =
30 "{0} - {1} with {2} already exists in LUSummary";
31
32static constexpr const char *MissingLinkageInformation =
33 "{0} - {1} missing linkage information in TUSummary";
34
35static constexpr const char *DuplicateEntityIdInTUSummary =
36 "{0} - Duplicate {1} in EntityResolutionTable";
37
38static constexpr const char *EntityNotFoundInResolutionTable =
39 "{0} - {1} not found in EntityResolutionTable";
40
41static constexpr const char *FailedToInsertEntityIntoOutputSummary =
42 "{0} - Failed to insert data for {1} with {2} against {3} to LUSummary";
43
44static constexpr const char *DuplicateTUNamespace =
45 "failed to link TU summary: duplicate {0}";
46
47} // namespace ErrorMessages
48
51 const NestedBuildNamespace &TUNamespace,
52 const NestedBuildNamespace &EntityNamespace,
54 switch (Linkage) {
57 // Qualify with the TU namespace first (to disambiguate across TUs),
58 // then with the LU namespace.
59 return EntityNamespace.makeQualified(TUNamespace)
60 .makeQualified(LUNamespace);
62 return NestedBuildNamespace(LUNamespace);
63 }
64
65 llvm_unreachable("Unhandled EntityLinkageType variant");
66}
67
68EntityId EntityLinker::resolveEntity(const EntityName &OldName,
70 const NestedBuildNamespace &TUNamespace) {
71 NestedBuildNamespace NewNamespace = resolveNamespace(
72 Output.LUNamespace, TUNamespace, OldName.Namespace, Linkage.getLinkage());
73
74 EntityName NewName(OldName.USR, OldName.Suffix, NewNamespace);
75
76 // NewId construction will always return a fresh id for `None` and `Internal`
77 // linkage entities since their namespaces will be different even if their
78 // names clash. For `External` linkage entities with identical names this
79 // function will return the id assigned at the first insertion.
80 EntityId NewId = Output.IdTable.getId(NewName);
81
82 auto [_, Inserted] = Output.LinkageTable.try_emplace(NewId, Linkage);
83 if (!Inserted) {
84 // Insertion failure for `None` and `Internal` linkage is a fatal error
85 // because these entities have unique namespaces and should never collide.
86 // `External` linkage entities may collide.
87 if (Linkage.getLinkage() == EntityLinkageType::None ||
88 Linkage.getLinkage() == EntityLinkageType::Internal) {
91 Linkage);
92 }
93 }
94
95 return NewId;
96}
97
98std::map<EntityId, EntityId>
99EntityLinker::resolve(const TUSummaryEncoding &Summary) {
100 std::map<EntityId, EntityId> EntityResolutionTable;
101
102 Summary.IdTable.forEach([&](const EntityName &OldName, const EntityId OldId) {
103 auto Iter = Summary.LinkageTable.find(OldId);
104 if (Iter == Summary.LinkageTable.end()) {
107 }
108
109 const EntityLinkage &Linkage = Iter->second;
110
111 EntityId NewId = resolveEntity(OldName, Linkage,
112 NestedBuildNamespace(Summary.TUNamespace));
113
114 auto [_, Inserted] = EntityResolutionTable.insert({OldId, NewId});
115 if (!Inserted) {
118 }
119 });
120
121 return EntityResolutionTable;
122}
123
124std::vector<EntitySummaryEncoding *>
125EntityLinker::merge(TUSummaryEncoding &Summary,
126 const std::map<EntityId, EntityId> &EntityResolutionTable) {
127 std::vector<EntitySummaryEncoding *> PatchTargets;
128
129 for (auto &[SN, DataMap] : Summary.Data) {
130 auto &OutputSummaryData = Output.Data[SN];
131
132 for (auto &[OldId, ES] : DataMap) {
133 auto Iter = EntityResolutionTable.find(OldId);
134 if (Iter == EntityResolutionTable.end()) {
137 }
138
139 const auto NewId = Iter->second;
140
141 auto [It, Inserted] = OutputSummaryData.try_emplace(NewId, std::move(ES));
142
143 if (Inserted) {
144 PatchTargets.push_back(It->second.get());
145 } else {
146 // Safe to retrieve linkage using .at since the resolve step ensures
147 // linkage information is always present for every OldId.
148 auto Linkage = Summary.LinkageTable.at(OldId);
149
150 // Insertion should never fail for `None` and `Internal` linkage
151 // entities because these entities will have different namespaces across
152 // TUs even if their names match.
153 if (Linkage.getLinkage() == EntityLinkageType::None ||
154 Linkage.getLinkage() == EntityLinkageType::Internal) {
158 }
159
160 // TODO: Insertion is expected to fail for duplicate occurrences of
161 // `External` linkage entities. Report these cases in a "debug" mode to
162 // help debug potential ODR violations.
163 }
164 }
165 }
166
167 return PatchTargets;
168}
169
170llvm::Error
171EntityLinker::patch(const std::vector<EntitySummaryEncoding *> &PatchTargets,
172 const std::map<EntityId, EntityId> &EntityResolutionTable) {
173 for (auto *PatchTarget : PatchTargets) {
174 assert(PatchTarget && "EntityLinker::patch: Patch target cannot be null");
175
176 if (auto Err = PatchTarget->patch(EntityResolutionTable)) {
177 return Err;
178 }
179 }
180 return llvm::Error::success();
181}
182
183llvm::Error EntityLinker::link(std::unique_ptr<TUSummaryEncoding> Summary) {
184 auto [_, Inserted] = ProcessedTUNamespaces.insert(Summary->TUNamespace);
185 if (!Inserted) {
186 return ErrorBuilder::create(std::errc::invalid_argument,
188 Summary->TUNamespace)
189 .build();
190 }
191
192 TUSummaryEncoding &SummaryRef = *Summary;
193
194 auto EntityResolutionTable = resolve(SummaryRef);
195 auto PatchTargets = merge(SummaryRef, EntityResolutionTable);
196 return patch(PatchTargets, EntityResolutionTable);
197}
static NestedBuildNamespace resolveNamespace(const NestedBuildNamespace &LUNamespace, const NestedBuildNamespace &TUNamespace, const NestedBuildNamespace &EntityNamespace, EntityLinkageType Linkage)
void forEach(llvm::function_ref< void(const EntityName &, EntityId)> Callback) const
Invokes the callback for each entity in the table.
Lightweight opaque handle representing an entity in an EntityIdTable.
Definition EntityId.h:31
Represents the linkage properties of an entity in the program model.
llvm::Error link(std::unique_ptr< TUSummaryEncoding > Summary)
Links a TU summary into a LU summary.
Uniquely identifies an entity in a program.
Definition EntityName.h:28
static ErrorBuilder create(std::error_code EC, const char *Fmt, Args &&...ArgVals)
Create an ErrorBuilder with an error code and formatted message.
static void fatal(const char *Fmt, Args &&...ArgVals)
Report a fatal error with formatted message and terminate execution.
llvm::Error build() const
Build and return the final error.
Represents a hierarchical sequence of build namespaces.
NestedBuildNamespace makeQualified(NestedBuildNamespace Namespace) const
Creates a new NestedBuildNamespace by appending additional namespace.
Represents a translation unit summary in its serialized encoding.
static constexpr const char * EntityAlreadyExistsInLinkageTable
static constexpr const char * DuplicateTUNamespace
static constexpr const char * EntityLinkerFatalErrorPrefix
static constexpr const char * DuplicateEntityIdInTUSummary
static constexpr const char * MissingLinkageInformation
static constexpr const char * FailedToInsertEntityIntoOutputSummary
static constexpr const char * EntityNotFoundInResolutionTable
@ None
local variables, function parameters
@ Internal
static functions/variables, anonymous namespace
@ External
globally visible across translation units
Linkage
Describes the different kinds of linkage (C++ [basic.link], C99 6.2.2) that an entity may have.
Definition Linkage.h:24