clang 24.0.0git
BuildNamespace.h
Go to the documentation of this file.
1//===- BuildNamespace.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// This file defines BuildNamespace and NestedBuildNamespace classes that
10// represent build namespaces in the Scalable Static Analysis Framework.
11//
12// Build namespaces provide an abstraction for grouping program entities (such
13// as those in a shared library or compilation unit) to enable analysis of
14// software projects constructed from individual components.
15//
16//===----------------------------------------------------------------------===//
17
18#ifndef LLVM_CLANG_SCALABLESTATICANALYSIS_CORE_MODEL_BUILDNAMESPACE_H
19#define LLVM_CLANG_SCALABLESTATICANALYSIS_CORE_MODEL_BUILDNAMESPACE_H
20
21#include "llvm/ADT/STLExtras.h"
22#include "llvm/ADT/StringRef.h"
23#include "llvm/Support/raw_ostream.h"
24#include <optional>
25#include <string>
26#include <vector>
27
28namespace clang::ssaf {
29
36
37/// Represents a single namespace in the build process.
38///
39/// A BuildNamespace groups program entities, such as those belonging to a
40/// compilation unit or link unit (e.g., a shared library). Each namespace has a
41/// kind (CompilationUnit or LinkUnit) and a unique identifier name within that
42/// kind.
43///
44/// BuildNamespaces can be composed into NestedBuildNamespace to represent
45/// hierarchical namespace structures that model how software is constructed
46/// from its components.
49 std::string Name;
50
51 auto asTuple() const { return std::tie(Kind, Name); }
52
53public:
54 BuildNamespace(BuildNamespaceKind Kind, llvm::StringRef Name)
55 : Kind(Kind), Name(Name.str()) {}
56
57 /// Creates a BuildNamespace representing a compilation unit.
58 ///
59 /// \param CompilationId The unique identifier for the compilation unit.
60 /// \returns A BuildNamespace with CompilationUnit kind.
61 static BuildNamespace makeCompilationUnit(llvm::StringRef CompilationId);
62
63 /// Returns a copy of this namespace with its kind replaced by \p Kind,
64 /// preserving the name.
66
67 bool operator==(const BuildNamespace &Other) const;
68 bool operator!=(const BuildNamespace &Other) const;
69 bool operator<(const BuildNamespace &Other) const;
70
71 friend class EntityLinker;
72 friend class SerializationFormat;
73 friend class TestFixture;
74 friend llvm::raw_ostream &operator<<(llvm::raw_ostream &OS,
75 const BuildNamespace &BN);
76};
77
78/// Represents a hierarchical sequence of build namespaces.
79///
80/// A NestedBuildNamespace captures namespace qualification for program entities
81/// by maintaining an ordered sequence of BuildNamespace steps. This models how
82/// entities are organized through multiple steps of the build process, such as
83/// first being part of a compilation unit, then incorporated into a link unit.
84///
85/// For example, an entity might be qualified by a compilation unit namespace
86/// followed by a shared library namespace.
88 std::vector<BuildNamespace> Namespaces;
89
90public:
92
93 explicit NestedBuildNamespace(const std::vector<BuildNamespace> &Namespaces)
94 : Namespaces(Namespaces) {}
95
97 Namespaces.push_back(N);
98 }
99
100 /// Creates a NestedBuildNamespace representing a compilation unit.
101 ///
102 /// \param CompilationId The unique identifier for the compilation unit.
103 /// \returns A NestedBuildNamespace containing a single CompilationUnit
104 /// BuildNamespace.
106 makeCompilationUnit(llvm::StringRef CompilationId);
107
108 /// Creates a NestedBuildNamespace representing a link unit.
109 ///
110 /// \param LinkUnitId The unique identifier for the link unit.
111 /// \returns A NestedBuildNamespace containing a single LinkUnit
112 /// BuildNamespace.
113 static NestedBuildNamespace makeLinkUnit(llvm::StringRef LinkUnitId);
114
115 /// Creates a new NestedBuildNamespace by appending additional namespace.
116 ///
117 /// \param Namespace The namespace to append.
119 auto Copy = *this;
120 Copy.Namespaces.reserve(Copy.Namespaces.size() +
121 Namespace.Namespaces.size());
122 llvm::append_range(Copy.Namespaces, Namespace.Namespaces);
123 return Copy;
124 }
125
126 bool empty() const;
127
128 bool operator==(const NestedBuildNamespace &Other) const;
129 bool operator!=(const NestedBuildNamespace &Other) const;
130 bool operator<(const NestedBuildNamespace &Other) const;
131
133 friend class TestFixture;
134 friend llvm::raw_ostream &operator<<(llvm::raw_ostream &OS,
135 const NestedBuildNamespace &NBN);
136};
137
138llvm::raw_ostream &operator<<(llvm::raw_ostream &OS, BuildNamespaceKind BNK);
139llvm::raw_ostream &operator<<(llvm::raw_ostream &OS, const BuildNamespace &BN);
140llvm::raw_ostream &operator<<(llvm::raw_ostream &OS,
141 const NestedBuildNamespace &NBN);
142
143} // namespace clang::ssaf
144
145#endif // LLVM_CLANG_SCALABLESTATICANALYSIS_CORE_MODEL_BUILDNAMESPACE_H
Represents a single namespace in the build process.
BuildNamespace(BuildNamespaceKind Kind, llvm::StringRef Name)
bool operator<(const BuildNamespace &Other) const
bool operator!=(const BuildNamespace &Other) const
static BuildNamespace makeCompilationUnit(llvm::StringRef CompilationId)
Creates a BuildNamespace representing a compilation unit.
bool operator==(const BuildNamespace &Other) const
BuildNamespace withKind(BuildNamespaceKind Kind) const
Returns a copy of this namespace with its kind replaced by Kind, preserving the name.
friend llvm::raw_ostream & operator<<(llvm::raw_ostream &OS, const BuildNamespace &BN)
Represents a multi-architecture static library.
Represents a hierarchical sequence of build namespaces.
NestedBuildNamespace(const std::vector< BuildNamespace > &Namespaces)
static NestedBuildNamespace makeCompilationUnit(llvm::StringRef CompilationId)
Creates a NestedBuildNamespace representing a compilation unit.
NestedBuildNamespace(const BuildNamespace &N)
bool operator<(const NestedBuildNamespace &Other) const
NestedBuildNamespace makeQualified(NestedBuildNamespace Namespace) const
Creates a new NestedBuildNamespace by appending additional namespace.
bool operator!=(const NestedBuildNamespace &Other) const
friend llvm::raw_ostream & operator<<(llvm::raw_ostream &OS, const NestedBuildNamespace &NBN)
static NestedBuildNamespace makeLinkUnit(llvm::StringRef LinkUnitId)
Creates a NestedBuildNamespace representing a link unit.
bool operator==(const NestedBuildNamespace &Other) const
Represents a static library of translation unit summary encodings.
llvm::raw_ostream & operator<<(llvm::raw_ostream &OS, BuildNamespaceKind BNK)
@ Other
Other implicit parameter.
Definition Decl.h:1775