clang 24.0.0git
EntityLinker.h
Go to the documentation of this file.
1//===- EntityLinker.h - Class for linking entities --------------*- 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// This file defines the EntityLinker class that combines multiple TU summaries
10// into a unified LU summary by deduplicating entities and patching summaries.
11// TU summaries may be supplied individually, bundled in a static library, or
12// bundled in one architecture member of a multi-architecture static library.
13//
14//===----------------------------------------------------------------------===//
15
16#ifndef LLVM_CLANG_SCALABLESTATICANALYSIS_CORE_ENTITYLINKER_ENTITYLINKER_H
17#define LLVM_CLANG_SCALABLESTATICANALYSIS_CORE_ENTITYLINKER_ENTITYLINKER_H
18
21#include "llvm/Support/Error.h"
22#include "llvm/TargetParser/Triple.h"
23#include <cstddef>
24#include <map>
25#include <memory>
26#include <set>
27#include <vector>
28
29namespace clang::ssaf {
30
32class StaticLibrary;
34
35/// Computes the build namespace an entity should carry after linking, given its
36/// linkage.
38resolveNamespace(const NestedBuildNamespace &LUNamespace,
39 const NestedBuildNamespace &TUNamespace,
40 const NestedBuildNamespace &EntityNamespace,
42
44 LUSummaryEncoding Output;
45
46 // Namespaces of the TU summaries folded in, supplied directly or as members
47 // of a library.
48 std::set<BuildNamespace> ProcessedTUNamespaces;
49
50public:
51 /// Constructs an EntityLinker to link TU summaries into a LU summary.
52 ///
53 /// \param TargetTriple The target triple of the link unit. Every linked
54 /// input must report the same triple.
55 /// \param LUNamespace The namespace identifying this link unit.
56 EntityLinker(llvm::Triple TargetTriple, NestedBuildNamespace LUNamespace)
57 : Output(std::move(TargetTriple), std::move(LUNamespace)) {}
58
59 /// Links a TU summary into a LU summary.
60 ///
61 /// Deduplicates entities, patches entity ID references in the entity summary,
62 /// and merges them into a single data store.
63 ///
64 /// \param Summary The TU summary to link. Ownership is transferred.
65 /// \returns Error if \p Summary reports a different target triple than this
66 /// link unit, if its TU namespace has already been linked, or if
67 /// patching fails; success otherwise. Corrupted summary data
68 /// (missing linkage information, duplicate entity IDs, etc.)
69 /// triggers a fatal error.
70 llvm::Error link(std::unique_ptr<TUSummaryEncoding> Summary);
71
72 /// Links every member of a static library into the LU summary.
73 ///
74 /// Members are folded in unconditionally, in an unspecified order, exactly as
75 /// if each had been passed as an individual TU summary.
76 ///
77 /// \param Library The static library to link. Ownership is transferred.
78 /// \returns Error if \p Library reports a different target triple than this
79 /// link unit or if any member fails to link, success otherwise.
80 llvm::Error link(std::unique_ptr<StaticLibrary> Library);
81
82 /// Links the architecture member matching this link unit into the LU summary.
83 ///
84 /// Members for other architectures are discarded.
85 ///
86 /// \param Library The multi-arch static library to link. Ownership is
87 /// transferred.
88 /// \returns Error if \p Library has no member whose target triple equals this
89 /// link unit's, or if the selected member fails to link; success
90 /// otherwise.
91 llvm::Error link(std::unique_ptr<MultiArchStaticLibrary> Library);
92
93 /// Returns the number of TU summaries folded in so far.
94 ///
95 /// Counts members expanded from libraries as well as TU summaries linked
96 /// directly, so it is not the number of link() calls.
97 size_t getLinkedTUCount() const { return ProcessedTUNamespaces.size(); }
98
99 /// Returns the accumulated LU summary.
100 ///
101 /// \returns LU summary containing all the deduplicated and patched entity
102 /// summaries.
103 LUSummaryEncoding takeOutput() && { return std::move(Output); }
104
105private:
106 /// Checks that an input belongs to this link unit's target.
107 ///
108 /// \param TargetTriple The triple of the input being linked.
109 /// \param InputNamespace The namespace naming that input in the diagnostic.
110 /// \returns Error if \p TargetTriple differs from this link unit's, success
111 /// otherwise.
112 llvm::Error checkTargetTriple(const llvm::Triple &TargetTriple,
113 const BuildNamespace &InputNamespace) const;
114
115 /// Resolves a TU entity name to an LU entity name and ID.
116 ///
117 /// \param OldName The entity name in the TU namespace.
118 /// \param Linkage The linkage determining namespace resolution strategy.
119 /// \returns The resolved LU EntityId.
120 EntityId resolveEntity(const EntityName &OldName,
121 const EntityLinkage &Linkage,
122 const NestedBuildNamespace &TUNamespace);
123
124 /// Resolves each TU EntityId to its corresponding LU EntityId.
125 ///
126 /// \param Summary The TU summary whose entities are being resolved.
127 /// \returns A map from TU EntityIds to their corresponding LU EntityIds.
128 std::map<EntityId, EntityId> resolve(const TUSummaryEncoding &Summary);
129
130 /// Merges all summary data from a TU summary into the LU Summary.
131 ///
132 /// \param Summary The TU summary whose data is being merged.
133 /// \param EntityResolutionTable Map from TU EntityIds to LU EntityIds.
134 /// \returns Pointers to each EntitySummaryEncoding successfully merged.
135 std::vector<EntitySummaryEncoding *>
136 merge(TUSummaryEncoding &Summary,
137 const std::map<EntityId, EntityId> &EntityResolutionTable);
138
139 /// Patches EntityId references in merged summary data.
140 ///
141 /// \param PatchTargets Vector of summary encodings that need patching.
142 /// \param EntityResolutionTable Map from TU EntityIds to LU EntityIds.
143 /// \returns Error if patching any encoding fails, success otherwise.
144 llvm::Error patch(const std::vector<EntitySummaryEncoding *> &PatchTargets,
145 const std::map<EntityId, EntityId> &EntityResolutionTable);
146};
147
148} // namespace clang::ssaf
149
150#endif // LLVM_CLANG_SCALABLESTATICANALYSIS_CORE_ENTITYLINKER_ENTITYLINKER_H
Represents a single namespace in the build process.
Lightweight opaque handle representing an entity in an EntityIdTable.
Definition EntityId.h:31
Represents the linkage properties of an entity in the program model.
EntityLinker(llvm::Triple TargetTriple, NestedBuildNamespace LUNamespace)
Constructs an EntityLinker to link TU summaries into a LU summary.
llvm::Error link(std::unique_ptr< TUSummaryEncoding > Summary)
Links a TU summary into a LU summary.
LUSummaryEncoding takeOutput() &&
Returns the accumulated LU summary.
size_t getLinkedTUCount() const
Returns the number of TU summaries folded in so far.
Uniquely identifies an entity in a program.
Definition EntityName.h:28
Represents a link unit summary in its serialized encoding.
Represents a multi-architecture static library.
Represents a hierarchical sequence of build namespaces.
Represents a static library of translation unit summary encodings.
Represents a translation unit summary in its serialized encoding.
NestedBuildNamespace resolveNamespace(const NestedBuildNamespace &LUNamespace, const NestedBuildNamespace &TUNamespace, const NestedBuildNamespace &EntityNamespace, EntityLinkageType Linkage)
Computes the build namespace an entity should carry after linking, given its linkage.
Linkage
Describes the different kinds of linkage (C++ [basic.link], C99 6.2.2) that an entity may have.
Definition Linkage.h:24