[clang] [clang][ssaf] Introduce entity abstraction for SSAF (PR #169131)

via cfe-commits cfe-commits at lists.llvm.org
Fri Nov 21 16:06:05 PST 2025


github-actions[bot] wrote:

<!--LLVM CODE FORMAT COMMENT: {clang-format}-->


:warning: C/C++ code formatter, clang-format found issues in your code. :warning:

<details>
<summary>
You can test this locally with the following command:
</summary>

``````````bash
git-clang-format --diff origin/main HEAD --extensions h,cpp -- clang/include/clang/Analysis/Scalable/ASTEntityMapping.h clang/include/clang/Analysis/Scalable/Model/BuildNamespace.h clang/include/clang/Analysis/Scalable/Model/EntityName.h clang/lib/Analysis/Scalable/ASTEntityMapping.cpp clang/lib/Analysis/Scalable/Model/BuildNamespace.cpp clang/lib/Analysis/Scalable/Model/EntityName.cpp clang/unittests/Analysis/Scalable/ASTEntityMappingTest.cpp clang/unittests/Analysis/Scalable/BuildNamespaceTest.cpp clang/unittests/Analysis/Scalable/EntityNameTest.cpp --diff_from_common_commit
``````````

:warning:
The reproduction instructions above might return results for more than one PR
in a stack if you are using a stacked PR workflow. You can limit the results by
changing `origin/main` to the base branch/commit you want to compare against.
:warning:

</details>

<details>
<summary>
View the diff from clang-format here.
</summary>

``````````diff
diff --git a/clang/include/clang/Analysis/Scalable/ASTEntityMapping.h b/clang/include/clang/Analysis/Scalable/ASTEntityMapping.h
index a137e8b74..435b3afdb 100644
--- a/clang/include/clang/Analysis/Scalable/ASTEntityMapping.h
+++ b/clang/include/clang/Analysis/Scalable/ASTEntityMapping.h
@@ -9,8 +9,8 @@
 #ifndef LLVM_CLANG_ANALYSIS_SCALABLE_ASTMAPPING_H
 #define LLVM_CLANG_ANALYSIS_SCALABLE_ASTMAPPING_H
 
-#include "clang/Analysis/Scalable/Model/EntityName.h"
 #include "clang/AST/Decl.h"
+#include "clang/Analysis/Scalable/Model/EntityName.h"
 #include "llvm/ADT/StringRef.h"
 #include <optional>
 
@@ -30,15 +30,17 @@ namespace ssaf {
 ///
 /// \param D The declaration to map. Must not be null.
 ///
-/// \return An EntityName if the declaration can be mapped, std::nullopt otherwise.
-std::optional<EntityName> getLocalEntityNameForDecl(const Decl* D);
+/// \return An EntityName if the declaration can be mapped, std::nullopt
+/// otherwise.
+std::optional<EntityName> getLocalEntityNameForDecl(const Decl *D);
 
 /// Maps a function return type to an EntityName.
 ///
 /// \param FD The function declaration. Must not be null.
 ///
 /// \return An EntityName for the function's return type.
-std::optional<EntityName> getLocalEntityNameForFunctionReturn(const FunctionDecl* FD);
+std::optional<EntityName>
+getLocalEntityNameForFunctionReturn(const FunctionDecl *FD);
 
 } // namespace ssaf
 } // namespace clang
diff --git a/clang/include/clang/Analysis/Scalable/Model/BuildNamespace.h b/clang/include/clang/Analysis/Scalable/Model/BuildNamespace.h
index c4bf7146e..b77fa0501 100644
--- a/clang/include/clang/Analysis/Scalable/Model/BuildNamespace.h
+++ b/clang/include/clang/Analysis/Scalable/Model/BuildNamespace.h
@@ -17,10 +17,7 @@
 namespace clang {
 namespace ssaf {
 
-enum class BuildNamespaceKind : unsigned short {
-  CompilationUnit,
-  LinkUnit
-};
+enum class BuildNamespaceKind : unsigned short { CompilationUnit, LinkUnit };
 
 std::string toString(BuildNamespaceKind BNK);
 
@@ -30,15 +27,16 @@ std::optional<BuildNamespaceKind> parseBuildNamespaceKind(llvm::StringRef Str);
 class BuildNamespace {
   BuildNamespaceKind Kind;
   std::string Name;
+
 public:
   BuildNamespace(BuildNamespaceKind Kind, llvm::StringRef Name)
-    : Kind(Kind), Name(Name.str()) {}
+      : Kind(Kind), Name(Name.str()) {}
 
   static BuildNamespace makeTU(llvm::StringRef CompilationId);
 
-  bool operator==(const BuildNamespace& Other) const;
-  bool operator!=(const BuildNamespace& Other) const;
-  bool operator<(const BuildNamespace& Other) const;
+  bool operator==(const BuildNamespace &Other) const;
+  bool operator!=(const BuildNamespace &Other) const;
+  bool operator<(const BuildNamespace &Other) const;
 
   friend class SerializationFormat;
 };
@@ -52,10 +50,10 @@ class NestedBuildNamespace {
 public:
   NestedBuildNamespace() = default;
 
-  explicit NestedBuildNamespace(const std::vector<BuildNamespace>& Namespaces)
-    : Namespaces(Namespaces) {}
+  explicit NestedBuildNamespace(const std::vector<BuildNamespace> &Namespaces)
+      : Namespaces(Namespaces) {}
 
-  explicit NestedBuildNamespace(const BuildNamespace& N) {
+  explicit NestedBuildNamespace(const BuildNamespace &N) {
     Namespaces.push_back(N);
   }
 
@@ -63,16 +61,16 @@ public:
 
   NestedBuildNamespace makeQualified(NestedBuildNamespace Namespace) {
     auto Copy = *this;
-    for (const auto& N : Namespace.Namespaces)
+    for (const auto &N : Namespace.Namespaces)
       Copy.Namespaces.push_back(N);
     return Copy;
   }
 
   bool empty() const;
 
-  bool operator==(const NestedBuildNamespace& Other) const;
-  bool operator!=(const NestedBuildNamespace& Other) const;
-  bool operator<(const NestedBuildNamespace& Other) const;
+  bool operator==(const NestedBuildNamespace &Other) const;
+  bool operator!=(const NestedBuildNamespace &Other) const;
+  bool operator<(const NestedBuildNamespace &Other) const;
 
   friend class JSONWriter;
   friend class LinkUnitResolution;
diff --git a/clang/include/clang/Analysis/Scalable/Model/EntityName.h b/clang/include/clang/Analysis/Scalable/Model/EntityName.h
index 7f11ef058..b43440168 100644
--- a/clang/include/clang/Analysis/Scalable/Model/EntityName.h
+++ b/clang/include/clang/Analysis/Scalable/Model/EntityName.h
@@ -19,9 +19,10 @@ namespace ssaf {
 
 /// Uniquely identifies an entity in a program.
 ///
-/// EntityName provides a globally unique identifier for program entities that remains
-/// stable across compilation boundaries. This enables whole-program analysis to track
-/// and relate entities across separately compiled translation units.
+/// EntityName provides a globally unique identifier for program entities that
+/// remains stable across compilation boundaries. This enables whole-program
+/// analysis to track and relate entities across separately compiled translation
+/// units.
 class EntityName {
   std::string USR;
   llvm::SmallString<16> Suffix;
@@ -31,9 +32,9 @@ public:
   EntityName(llvm::StringRef USR, llvm::StringRef Suffix,
              NestedBuildNamespace Namespace);
 
-  bool operator==(const EntityName& Other) const;
-  bool operator!=(const EntityName& Other) const;
-  bool operator<(const EntityName& Other) const;
+  bool operator==(const EntityName &Other) const;
+  bool operator!=(const EntityName &Other) const;
+  bool operator<(const EntityName &Other) const;
 
   EntityName makeQualified(NestedBuildNamespace Namespace);
 
diff --git a/clang/lib/Analysis/Scalable/ASTEntityMapping.cpp b/clang/lib/Analysis/Scalable/ASTEntityMapping.cpp
index 87d05e8aa..e74d74c6a 100644
--- a/clang/lib/Analysis/Scalable/ASTEntityMapping.cpp
+++ b/clang/lib/Analysis/Scalable/ASTEntityMapping.cpp
@@ -19,7 +19,7 @@
 namespace clang {
 namespace ssaf {
 
-std::optional<EntityName> getLocalEntityNameForDecl(const Decl* D) {
+std::optional<EntityName> getLocalEntityNameForDecl(const Decl *D) {
   if (!D)
     return std::nullopt;
 
@@ -36,9 +36,11 @@ std::optional<EntityName> getLocalEntityNameForDecl(const Decl* D) {
   llvm::SmallString<16> Suffix;
   const Decl *USRDecl = D;
 
-  // For parameters, use the parent function's USR with parameter index as suffix
-  if (const auto * PVD = dyn_cast<ParmVarDecl>(D)) {
-    const auto *FD = dyn_cast_or_null<FunctionDecl>(PVD->getParentFunctionOrMethod());
+  // For parameters, use the parent function's USR with parameter index as
+  // suffix
+  if (const auto *PVD = dyn_cast<ParmVarDecl>(D)) {
+    const auto *FD =
+        dyn_cast_or_null<FunctionDecl>(PVD->getParentFunctionOrMethod());
     if (!FD)
       return std::nullopt;
     USRDecl = FD;
@@ -60,7 +62,8 @@ std::optional<EntityName> getLocalEntityNameForDecl(const Decl* D) {
   return EntityName(USRBuf.str(), Suffix, {});
 }
 
-std::optional<EntityName> getLocalEntityNameForFunctionReturn(const FunctionDecl* FD) {
+std::optional<EntityName>
+getLocalEntityNameForFunctionReturn(const FunctionDecl *FD) {
   if (!FD)
     return std::nullopt;
 
diff --git a/clang/lib/Analysis/Scalable/Model/BuildNamespace.cpp b/clang/lib/Analysis/Scalable/Model/BuildNamespace.cpp
index 5284a9a87..8111a30a1 100644
--- a/clang/lib/Analysis/Scalable/Model/BuildNamespace.cpp
+++ b/clang/lib/Analysis/Scalable/Model/BuildNamespace.cpp
@@ -13,9 +13,11 @@ namespace clang {
 namespace ssaf {
 
 std::string toString(BuildNamespaceKind BNK) {
-  switch(BNK) {
-    case BuildNamespaceKind::CompilationUnit: return "compilation_unit";
-    case BuildNamespaceKind::LinkUnit: return "link_unit";
+  switch (BNK) {
+  case BuildNamespaceKind::CompilationUnit:
+    return "compilation_unit";
+  case BuildNamespaceKind::LinkUnit:
+    return "link_unit";
   }
   llvm_unreachable("Unknown BuildNamespaceKind");
 }
@@ -29,42 +31,42 @@ std::optional<BuildNamespaceKind> parseBuildNamespaceKind(llvm::StringRef Str) {
 }
 
 BuildNamespace BuildNamespace::makeTU(llvm::StringRef CompilationId) {
-  return BuildNamespace{BuildNamespaceKind::CompilationUnit, CompilationId.str()};
+  return BuildNamespace{BuildNamespaceKind::CompilationUnit,
+                        CompilationId.str()};
 }
 
-bool BuildNamespace::operator==(const BuildNamespace& Other) const {
+bool BuildNamespace::operator==(const BuildNamespace &Other) const {
   return Kind == Other.Kind && Name == Other.Name;
 }
 
-bool BuildNamespace::operator!=(const BuildNamespace& Other) const {
+bool BuildNamespace::operator!=(const BuildNamespace &Other) const {
   return !(*this == Other);
 }
 
-bool BuildNamespace::operator<(const BuildNamespace& Other) const {
+bool BuildNamespace::operator<(const BuildNamespace &Other) const {
   if (Kind != Other.Kind)
     return Kind < Other.Kind;
   return Name < Other.Name;
 }
 
-NestedBuildNamespace NestedBuildNamespace::makeTU(llvm::StringRef CompilationId) {
+NestedBuildNamespace
+NestedBuildNamespace::makeTU(llvm::StringRef CompilationId) {
   NestedBuildNamespace Result;
   Result.Namespaces.push_back(BuildNamespace::makeTU(CompilationId));
   return Result;
 }
 
-bool NestedBuildNamespace::empty() const {
-  return Namespaces.empty();
-}
+bool NestedBuildNamespace::empty() const { return Namespaces.empty(); }
 
-bool NestedBuildNamespace::operator==(const NestedBuildNamespace& Other) const {
+bool NestedBuildNamespace::operator==(const NestedBuildNamespace &Other) const {
   return Namespaces == Other.Namespaces;
 }
 
-bool NestedBuildNamespace::operator!=(const NestedBuildNamespace& Other) const {
+bool NestedBuildNamespace::operator!=(const NestedBuildNamespace &Other) const {
   return !(*this == Other);
 }
 
-bool NestedBuildNamespace::operator<(const NestedBuildNamespace& Other) const {
+bool NestedBuildNamespace::operator<(const NestedBuildNamespace &Other) const {
   return Namespaces < Other.Namespaces;
 }
 
diff --git a/clang/lib/Analysis/Scalable/Model/EntityName.cpp b/clang/lib/Analysis/Scalable/Model/EntityName.cpp
index 3404ecc58..6b9e8d805 100644
--- a/clang/lib/Analysis/Scalable/Model/EntityName.cpp
+++ b/clang/lib/Analysis/Scalable/Model/EntityName.cpp
@@ -13,19 +13,18 @@ namespace ssaf {
 
 EntityName::EntityName(llvm::StringRef USR, llvm::StringRef Suffix,
                        NestedBuildNamespace Namespace)
-  : USR(USR.str()), Suffix(Suffix), Namespace(std::move(Namespace)) {}
+    : USR(USR.str()), Suffix(Suffix), Namespace(std::move(Namespace)) {}
 
-bool EntityName::operator==(const EntityName& Other) const {
-  return USR == Other.USR &&
-         Suffix == Other.Suffix &&
+bool EntityName::operator==(const EntityName &Other) const {
+  return USR == Other.USR && Suffix == Other.Suffix &&
          Namespace == Other.Namespace;
 }
 
-bool EntityName::operator!=(const EntityName& Other) const {
+bool EntityName::operator!=(const EntityName &Other) const {
   return !(*this == Other);
 }
 
-bool EntityName::operator<(const EntityName& Other) const {
+bool EntityName::operator<(const EntityName &Other) const {
   if (USR != Other.USR)
     return USR < Other.USR;
   if (Suffix != Other.Suffix)
diff --git a/clang/unittests/Analysis/Scalable/ASTEntityMappingTest.cpp b/clang/unittests/Analysis/Scalable/ASTEntityMappingTest.cpp
index 8de0df246..4cece2fbd 100644
--- a/clang/unittests/Analysis/Scalable/ASTEntityMappingTest.cpp
+++ b/clang/unittests/Analysis/Scalable/ASTEntityMappingTest.cpp
@@ -95,11 +95,13 @@ TEST(ASTEntityMappingTest, NullDecl) {
 }
 
 TEST(ASTEntityMappingTest, ImplicitDecl) {
-  auto AST = tooling::buildASTFromCode(R"(
+  auto AST = tooling::buildASTFromCode(
+      R"(
     struct S {
       S() = default;
     };
-  )", "test.cpp", std::make_shared<PCHContainerOperations>());
+  )",
+      "test.cpp", std::make_shared<PCHContainerOperations>());
   auto &Ctx = AST->getASTContext();
 
   const auto *RD = findDecl<CXXRecordDecl>(Ctx, "S");

``````````

</details>


https://github.com/llvm/llvm-project/pull/169131


More information about the cfe-commits mailing list