[clang] [clang][ssaf] Implement JSONFormat (PR #180021)

Balázs Benics via cfe-commits cfe-commits at lists.llvm.org
Wed Feb 11 08:00:09 PST 2026


================
@@ -0,0 +1,132 @@
+//===- JSONFormat.h ---------------------------------------------*- C++ -*-===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+//
+// JSON serialization format implementation
+//
+//===----------------------------------------------------------------------===//
+
+#ifndef CLANG_ANALYSIS_SCALABLE_SERIALIZATION_JSONFORMAT_H
+#define CLANG_ANALYSIS_SCALABLE_SERIALIZATION_JSONFORMAT_H
+
+#include "clang/Analysis/Scalable/Serialization/SerializationFormat.h"
+#include "llvm/ADT/STLFunctionalExtras.h"
+#include "llvm/Support/JSON.h"
+
+namespace clang::ssaf {
+
+class EntitySummary;
+class EntityIdTable;
+class SummaryName;
+
+class JSONFormat : public SerializationFormat {
+public:
+  // Helper class to provide limited access to EntityId conversion methods
+  // Only exposes EntityId serialization/deserialization to format handlers
+  class EntityIdConverter {
+  public:
+    EntityId fromJSON(uint64_t EntityIdIndex) const {
+      return Format.entityIdFromJSON(EntityIdIndex);
+    }
+
+    uint64_t toJSON(EntityId EI) const { return Format.entityIdToJSON(EI); }
+
+  private:
+    friend class JSONFormat;
+    EntityIdConverter(const JSONFormat &Format) : Format(Format) {}
+    const JSONFormat &Format;
+  };
+
+  explicit JSONFormat(llvm::IntrusiveRefCntPtr<llvm::vfs::FileSystem> FS);
+
+  ~JSONFormat() = default;
+
+  llvm::Expected<TUSummary> readTUSummary(llvm::StringRef Path) override;
+
+  llvm::Error writeTUSummary(const TUSummary &Summary,
+                             llvm::StringRef Path) override;
+
+  using SerializerFn = llvm::function_ref<llvm::json::Object(
+      const EntitySummary &, const EntityIdConverter &)>;
+  using DeserializerFn =
+      llvm::function_ref<llvm::Expected<std::unique_ptr<EntitySummary>>(
+          const llvm::json::Object &, EntityIdTable &,
+          const EntityIdConverter &)>;
+
+  using FormatInfo = FormatInfoEntry<SerializerFn, DeserializerFn>;
+
+private:
+  std::map<SummaryName, FormatInfo> FormatInfos;
+
+  EntityId entityIdFromJSON(const uint64_t EntityIdIndex) const;
+  uint64_t entityIdToJSON(EntityId EI) const;
+
+  llvm::Expected<BuildNamespaceKind>
+  buildNamespaceKindFromJSON(llvm::StringRef BuildNamespaceKindStr) const;
+
+  llvm::Expected<BuildNamespace>
+  buildNamespaceFromJSON(const llvm::json::Object &BuildNamespaceObject) const;
----------------
steakhal wrote:

I'd expect that the qualification `llvm::json`  would repeat quite a bit in this header and the implementation.
I think it's safe include those names within the `JSONFormat` decl context by `using llvm::json;`
This should improve readability.

Or at least do this selectively for the most common types, like `json::Object`, `json::Array`. Maybe this approach would be even better, so we have precise control over what names are brought into this decl context.

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


More information about the cfe-commits mailing list