clang 23.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_SCALABLESTATICANALYSISFRAMEWORK_CORE_MODEL_BUILDNAMESPACE_H
19#define LLVM_CLANG_SCALABLESTATICANALYSISFRAMEWORK_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
30enum class BuildNamespaceKind : unsigned short { CompilationUnit, LinkUnit };
31
32/// Represents a single namespace in the build process.
33///
34/// A BuildNamespace groups program entities, such as those belonging to a
35/// compilation unit or link unit (e.g., a shared library). Each namespace has a
36/// kind (CompilationUnit or LinkUnit) and a unique identifier name within that
37/// kind.
38///
39/// BuildNamespaces can be composed into NestedBuildNamespace to represent
40/// hierarchical namespace structures that model how software is constructed
41/// from its components.
44 std::string Name;
45
46 auto asTuple() const { return std::tie(Kind, Name); }
47
48public:
49 BuildNamespace(BuildNamespaceKind Kind, llvm::StringRef Name)
50 : Kind(Kind), Name(Name.str()) {}
51
52 /// Creates a BuildNamespace representing a compilation unit.
53 ///
54 /// \param CompilationId The unique identifier for the compilation unit.
55 /// \returns A BuildNamespace with CompilationUnit kind.
56 static BuildNamespace makeCompilationUnit(llvm::StringRef CompilationId);
57
58 bool operator==(const BuildNamespace &Other) const;
59 bool operator!=(const BuildNamespace &Other) const;
60 bool operator<(const BuildNamespace &Other) const;
61
62 friend class EntityLinker;
63 friend class SerializationFormat;
64 friend class TestFixture;
65 friend llvm::raw_ostream &operator<<(llvm::raw_ostream &OS,
66 const BuildNamespace &BN);
67};
68
69/// Represents a hierarchical sequence of build namespaces.
70///
71/// A NestedBuildNamespace captures namespace qualification for program entities
72/// by maintaining an ordered sequence of BuildNamespace steps. This models how
73/// entities are organized through multiple steps of the build process, such as
74/// first being part of a compilation unit, then incorporated into a link unit.
75///
76/// For example, an entity might be qualified by a compilation unit namespace
77/// followed by a shared library namespace.
79 std::vector<BuildNamespace> Namespaces;
80
81public:
83
84 explicit NestedBuildNamespace(const std::vector<BuildNamespace> &Namespaces)
85 : Namespaces(Namespaces) {}
86
88 Namespaces.push_back(N);
89 }
90
91 /// Creates a NestedBuildNamespace representing a compilation unit.
92 ///
93 /// \param CompilationId The unique identifier for the compilation unit.
94 /// \returns A NestedBuildNamespace containing a single CompilationUnit
95 /// BuildNamespace.
97 makeCompilationUnit(llvm::StringRef CompilationId);
98
99 /// Creates a new NestedBuildNamespace by appending additional namespace.
100 ///
101 /// \param Namespace The namespace to append.
103 auto Copy = *this;
104 Copy.Namespaces.reserve(Copy.Namespaces.size() +
105 Namespace.Namespaces.size());
106 llvm::append_range(Copy.Namespaces, Namespace.Namespaces);
107 return Copy;
108 }
109
110 bool empty() const;
111
112 bool operator==(const NestedBuildNamespace &Other) const;
113 bool operator!=(const NestedBuildNamespace &Other) const;
114 bool operator<(const NestedBuildNamespace &Other) const;
115
117 friend class TestFixture;
118 friend llvm::raw_ostream &operator<<(llvm::raw_ostream &OS,
119 const NestedBuildNamespace &NBN);
120};
121
122llvm::raw_ostream &operator<<(llvm::raw_ostream &OS, BuildNamespaceKind BNK);
123llvm::raw_ostream &operator<<(llvm::raw_ostream &OS, const BuildNamespace &BN);
124llvm::raw_ostream &operator<<(llvm::raw_ostream &OS,
125 const NestedBuildNamespace &NBN);
126
127} // namespace clang::ssaf
128
129#endif // LLVM_CLANG_SCALABLESTATICANALYSISFRAMEWORK_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
friend llvm::raw_ostream & operator<<(llvm::raw_ostream &OS, const BuildNamespace &BN)
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)
bool operator==(const NestedBuildNamespace &Other) const
llvm::raw_ostream & operator<<(llvm::raw_ostream &OS, BuildNamespaceKind BNK)
@ Other
Other implicit parameter.
Definition Decl.h:1746