| 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 | |
| 9 | #include "JSONEntitySummaryEncoding.h" |
| 10 | #include "JSONFormatImpl.h" |
| 11 | |
| 12 | namespace clang::ssaf { |
| 13 | |
| 14 | llvm::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(EC: std::errc::invalid_argument, |
| 20 | Fmt: ErrorMessages::FailedToReadEntityIdObject, |
| 21 | ArgVals: JSONEntityIdKey) |
| 22 | .build(); |
| 23 | } |
| 24 | |
| 25 | std::optional<uint64_t> OptEntityIdIndex = AtVal->getAsUINT64(); |
| 26 | if (!OptEntityIdIndex) { |
| 27 | return ErrorBuilder::create(EC: std::errc::invalid_argument, |
| 28 | Fmt: ErrorMessages::FailedToReadEntityIdObject, |
| 29 | ArgVals: JSONEntityIdKey) |
| 30 | .build(); |
| 31 | } |
| 32 | |
| 33 | auto OldId = JSONFormat::makeEntityId(Index: *OptEntityIdIndex); |
| 34 | auto It = Table.find(x: OldId); |
| 35 | if (It == Table.end()) { |
| 36 | return ErrorBuilder::create(EC: std::errc::invalid_argument, |
| 37 | Fmt: ErrorMessages::FailedToPatchEntityIdNotInTable, |
| 38 | ArgVals&: OldId) |
| 39 | .build(); |
| 40 | } |
| 41 | |
| 42 | *AtVal = static_cast<uint64_t>(JSONFormat::getIndex(X: It->second)); |
| 43 | |
| 44 | return llvm::Error::success(); |
| 45 | } |
| 46 | |
| 47 | llvm::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(V&: Val, Table)) { |
| 51 | return Err; |
| 52 | } |
| 53 | } |
| 54 | return llvm::Error::success(); |
| 55 | } |
| 56 | |
| 57 | llvm::Error JSONEntitySummaryEncoding::patchObject( |
| 58 | llvm::json::Object &Obj, const std::map<EntityId, EntityId> &Table) { |
| 59 | |
| 60 | llvm::json::Value *AtVal = Obj.get(K: JSONEntityIdKey); |
| 61 | return AtVal ? patchEntityIdObject(Obj, Table, AtVal) |
| 62 | : patchRegularObject(Obj, Table); |
| 63 | } |
| 64 | |
| 65 | llvm::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&: *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(V&: Val, Table)) { |
| 74 | return Err; |
| 75 | } |
| 76 | } |
| 77 | } |
| 78 | return llvm::Error::success(); |
| 79 | } |
| 80 | |
| 81 | llvm::Error JSONEntitySummaryEncoding::patch( |
| 82 | const std::map<EntityId, EntityId> &EntityResolutionTable) { |
| 83 | return patchValue(V&: Data, Table: EntityResolutionTable); |
| 84 | } |
| 85 | |
| 86 | } // namespace clang::ssaf |
| 87 | |