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