[clang] [llvm] [clang][ssaf] Implement JSON format for CallGraph summary (PR #189681)
Balázs Benics via llvm-commits
llvm-commits at lists.llvm.org
Wed Apr 1 07:30:52 PDT 2026
================
@@ -0,0 +1,142 @@
+//===- CallGraphJSONFormat.cpp --------------------------------------------===//
+//
+// 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
+//
+//===----------------------------------------------------------------------===//
+
+#include "clang/ScalableStaticAnalysisFramework/Analyses/CallGraph/CallGraphSummary.h"
+#include "clang/ScalableStaticAnalysisFramework/Core/Model/EntityId.h"
+#include "clang/ScalableStaticAnalysisFramework/Core/Serialization/JSONFormat.h"
+#include "llvm/ADT/STLExtras.h"
+#include "llvm/Support/JSON.h"
+#include <memory>
+
+using namespace llvm;
+using namespace clang;
+using namespace ssaf;
+
+static json::Object serialize(const EntitySummary &Summary,
+ JSONFormat::EntityIdToJSONFn ToJSON) {
+ const auto &S = static_cast<const CallGraphSummary &>(Summary);
+
+ json::Array DirectCalleesArray;
+ DirectCalleesArray.reserve(S.DirectCallees.size());
+ append_range(DirectCalleesArray, map_range(S.DirectCallees, ToJSON));
+
+ json::Array VirtualCalleesArray;
+ VirtualCalleesArray.reserve(S.VirtualCallees.size());
+ append_range(VirtualCalleesArray, map_range(S.VirtualCallees, ToJSON));
+
+ return json::Object{
+ {"pretty_name", json::Value(S.PrettyName)},
+ {"direct_callees", std::move(DirectCalleesArray)},
+ {"virtual_callees", std::move(VirtualCalleesArray)},
+ {"def",
+ json::Object{
+ {"file", json::Value(S.Definition.File)},
+ {"line", json::Value(S.Definition.Line)},
+ {"col", json::Value(S.Definition.Column)},
+ }},
+ };
+}
+
+static Expected<std::unique_ptr<EntitySummary>>
+deserialize(const json::Object &Obj, EntityIdTable &IdTable,
+ JSONFormat::EntityIdFromJSONFn FromJSON) {
+ auto Result = std::make_unique<CallGraphSummary>();
+
+ auto PrettyName = Obj.getString("pretty_name");
+ if (!PrettyName) {
+ return createStringError(inconvertibleErrorCode(),
----------------
steakhal wrote:
I've reworked it to use ErrorBuilder, and building contexts.
Given the added complexity I've also opted for writing some tests for the error cases, and also expanded the CallGraph summary test to be more precise.
See d0b7ef88b0650fa47c3d7f3bb270f72f1097ac84
https://github.com/llvm/llvm-project/pull/189681
More information about the llvm-commits
mailing list