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