clang 24.0.0git
SharedLexicalRepresentationFormat.cpp
Go to the documentation of this file.
1//===- SharedLexicalRepresentationFormat.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
12#include "llvm/Support/Error.h"
13#include "llvm/Support/JSON.h"
14#include "llvm/Support/Registry.h"
15
16#include <memory>
17#include <utility>
18#include <vector>
19
20using namespace clang;
21using namespace ssaf;
22using Array = llvm::json::Array;
23using Object = llvm::json::Object;
24using Value = llvm::json::Value;
25
26namespace clang::ssaf {
27
29buildEntitySourceLocationsSummary(std::vector<SourceLocationRecord> Locs) {
30 return EntitySourceLocationsSummary(std::move(Locs));
31}
32
34getDeclLocations(const EntitySourceLocationsSummary &S) {
35 return S.DeclLocations;
36}
37
38} // namespace clang::ssaf
39
40static constexpr llvm::StringLiteral DeclLocationsKey = "decl_locations";
41static constexpr llvm::StringLiteral FilePathKey = "file_path";
42static constexpr llvm::StringLiteral LineKey = "line";
43static constexpr llvm::StringLiteral ColumnKey = "column";
44
46 return Object{{FilePathKey.data(), R.FilePath},
47 {LineKey.data(), static_cast<int64_t>(R.Line)},
48 {ColumnKey.data(), static_cast<int64_t>(R.Column)}};
49}
50
53 const Object *Obj = V.getAsObject();
54 if (!Obj)
55 return makeSawButExpectedError(V, "an object {%s, %s, %s}",
56 FilePathKey.data(), LineKey.data(),
57 ColumnKey.data());
58
59 auto FilePath = Obj->getString(FilePathKey);
60 if (!FilePath)
61 return makeSawButExpectedError(*Obj, "an object with a string field %s",
62 FilePathKey.data());
63
64 auto Line = Obj->getInteger(LineKey);
65 if (!Line)
66 return makeSawButExpectedError(*Obj, "an object with an integer field %s",
67 LineKey.data());
68
69 auto Column = Obj->getInteger(ColumnKey);
70 if (!Column)
71 return makeSawButExpectedError(*Obj, "an object with an integer field %s",
72 ColumnKey.data());
73
75 R.FilePath = FilePath->str();
76 R.Line = static_cast<unsigned>(*Line);
77 R.Column = static_cast<unsigned>(*Column);
78 return R;
79}
80
82 const auto &S = static_cast<const EntitySourceLocationsSummary &>(ES);
83 Array Locs;
84 for (const auto &R : getDeclLocations(S))
85 Locs.push_back(sourceLocationRecordToJSON(R));
86 return Object{{DeclLocationsKey.data(), std::move(Locs)}};
87}
88
92 const Array *Locs = Data.getArray(DeclLocationsKey);
93 if (!Locs)
95 "an Object with an array field %s",
96 DeclLocationsKey.data());
97
98 std::vector<SourceLocationRecord> Records;
99 Records.reserve(Locs->size());
100 for (const Value &V : *Locs) {
102 if (!R)
103 return R.takeError();
104 Records.push_back(std::move(*R));
105 }
106 return std::make_unique<EntitySourceLocationsSummary>(
108}
109
110namespace {
111struct SharedLexicalRepresentationJSONFormatInfo final
113 SharedLexicalRepresentationJSONFormatInfo()
114 : JSONFormat::FormatInfo(EntitySourceLocationsSummary::summaryName(),
116};
117} // namespace
118
119static llvm::Registry<JSONFormat::FormatInfo>::Add<
120 SharedLexicalRepresentationJSONFormatInfo>
123 "JSON Format info for EntitySourceLocationsSummary");
124
125namespace clang::ssaf {
126// NOLINTNEXTLINE(misc-use-internal-linkage)
128} // namespace clang::ssaf
#define V(N, I)
llvm::MachO::Records Records
Definition MachO.h:40
llvm::json::Object Object
llvm::json::Array Array
static Object sourceLocationRecordToJSON(const SourceLocationRecord &R)
static llvm::Registry< JSONFormat::FormatInfo >::Add< SharedLexicalRepresentationJSONFormatInfo > RegisterSharedLexicalRepresentationJSONFormatInfo(EntitySourceLocationsSummary::Name, "JSON Format info for EntitySourceLocationsSummary")
static llvm::Expected< std::unique_ptr< EntitySummary > > deserialize(const Object &Data, EntityIdTable &, JSONFormat::EntityIdFromJSONFn)
static constexpr llvm::StringLiteral FilePathKey
static constexpr llvm::StringLiteral DeclLocationsKey
static llvm::Expected< SourceLocationRecord > sourceLocationRecordFromJSON(const Value &V)
static Object serialize(const EntitySummary &ES, JSONFormat::EntityIdToJSONFn)
static constexpr llvm::StringLiteral LineKey
static constexpr llvm::StringLiteral ColumnKey
Manages entity name interning and provides efficient EntityId handles.
An EntitySourceLocationsSummary contains one SourceLocationRecord per declaration site contributed by...
Base class for analysis-specific summary data.
llvm::function_ref< llvm::Expected< EntityId >(const Object &)> EntityIdFromJSONFn
Definition JSONFormat.h:99
FormatInfoEntry< SerializerFn, DeserializerFn > FormatInfo
Definition JSONFormat.h:108
llvm::function_ref< Object(EntityId)> EntityIdToJSONFn
Definition JSONFormat.h:98
EntitySourceLocationsSummary buildEntitySourceLocationsSummary(std::vector< SourceLocationRecord > Locs)
llvm::ArrayRef< SourceLocationRecord > getDeclLocations(const EntitySourceLocationsSummary &S)
llvm::Error makeSawButExpectedError(const JSONTy &Saw, llvm::StringRef Expected, const Ts &...ExpectedArgs)
volatile int SharedLexicalRepresentationJSONFormatAnchorSource
llvm::json::Object Object
The JSON file list parser is used to communicate input to InstallAPI.
A canonical (file, line, column) triple for one declaration site.