clang 23.0.0git
JSONEntitySummaryEncoding.cpp
Go to the documentation of this file.
1//===- JSONEntitySummaryEncoding.cpp --------------------------------------===//
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
10#include "JSONFormatImpl.h"
11
12namespace clang::ssaf {
13
14llvm::Error JSONEntitySummaryEncoding::patchEntityIdObject(
15 llvm::json::Object &Obj, const std::map<EntityId, EntityId> &Table,
16 llvm::json::Value *AtVal) {
17
18 if (Obj.size() != 1) {
19 return ErrorBuilder::create(std::errc::invalid_argument,
22 .build();
23 }
24
25 std::optional<uint64_t> OptEntityIdIndex = AtVal->getAsUINT64();
26 if (!OptEntityIdIndex) {
27 return ErrorBuilder::create(std::errc::invalid_argument,
30 .build();
31 }
32
33 auto OldId = JSONFormat::makeEntityId(*OptEntityIdIndex);
34 auto It = Table.find(OldId);
35 if (It == Table.end()) {
36 return ErrorBuilder::create(std::errc::invalid_argument,
38 OldId)
39 .build();
40 }
41
42 *AtVal = static_cast<uint64_t>(JSONFormat::getIndex(It->second));
43
44 return llvm::Error::success();
45}
46
47llvm::Error JSONEntitySummaryEncoding::patchRegularObject(
48 llvm::json::Object &Obj, const std::map<EntityId, EntityId> &Table) {
49 for (auto &[Key, Val] : Obj) {
50 if (auto Err = patchValue(Val, Table)) {
51 return Err;
52 }
53 }
54 return llvm::Error::success();
55}
56
57llvm::Error JSONEntitySummaryEncoding::patchObject(
58 llvm::json::Object &Obj, const std::map<EntityId, EntityId> &Table) {
59
60 llvm::json::Value *AtVal = Obj.get(JSONEntityIdKey);
61 return AtVal ? patchEntityIdObject(Obj, Table, AtVal)
62 : patchRegularObject(Obj, Table);
63}
64
65llvm::Error JSONEntitySummaryEncoding::patchValue(
66 llvm::json::Value &V, const std::map<EntityId, EntityId> &Table) {
67 if (llvm::json::Object *Obj = V.getAsObject()) {
68 if (auto Err = patchObject(*Obj, Table)) {
69 return Err;
70 }
71 } else if (llvm::json::Array *Arr = V.getAsArray()) {
72 for (auto &Val : *Arr) {
73 if (auto Err = patchValue(Val, Table)) {
74 return Err;
75 }
76 }
77 }
78 return llvm::Error::success();
79}
80
82 const std::map<EntityId, EntityId> &EntityResolutionTable) {
83 return patchValue(Data, EntityResolutionTable);
84}
85
86} // namespace clang::ssaf
#define V(N, I)
static ErrorBuilder create(std::error_code EC, const char *Fmt, Args &&...ArgVals)
Create an ErrorBuilder with an error code and formatted message.
llvm::Error build() const
Build and return the final error.
llvm::Error patch(const std::map< EntityId, EntityId > &EntityResolutionTable) override
Updates EntityId references in the encoded data.
static EntityId makeEntityId(const size_t Index)
constexpr const char * FailedToPatchEntityIdNotInTable
constexpr const char * FailedToReadEntityIdObject
constexpr const char * JSONEntityIdKey
An entity ID is encoded as the single-key object {"@": <index>}.
unsigned long uint64_t