[clang] [clang][ssaf] Implement JSONFormat (PR #180021)
Balázs Benics via cfe-commits
cfe-commits at lists.llvm.org
Mon Feb 16 06:45:52 PST 2026
================
@@ -0,0 +1,131 @@
+//===- 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 EntityIdTable;
+class EntitySummary;
+class SummaryName;
+
+class JSONFormat final : public SerializationFormat {
+ using Array = llvm::json::Array;
+ using Object = llvm::json::Object;
+
+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;
+ };
+
+ JSONFormat();
+
+ ~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<Object(const EntitySummary &,
+ const EntityIdConverter &)>;
+ using DeserializerFn =
+ llvm::function_ref<llvm::Expected<std::unique_ptr<EntitySummary>>(
+ const Object &, EntityIdTable &, const EntityIdConverter &)>;
+
+ using FormatInfo = FormatInfoEntry<SerializerFn, DeserializerFn>;
+
+private:
+ std::map<SummaryName, FormatInfo> FormatInfos;
----------------
steakhal wrote:
WDYT of making this const so that we definitely not end up "default constructing" entries into the map by incidentally using `operator[]`.
```suggestion
static std::map<SummaryName, FormatInfo> initFormatInfos();
const std::map<SummaryName, FormatInfo> FormatInfos = initFormatInfos();
```
This would mean that we no longer need to declare an actor (because what gets generated would do the right thing). That said, we don't need the `~JSONFormat() = default;` which is implied by default.
Of course, the `initFormatInfos()` would need to fill up a map and return it by value for RVO instead of directly filling the field non-static data member.
https://github.com/llvm/llvm-project/pull/180021
More information about the cfe-commits
mailing list