clang 23.0.0git
JSONFormatImpl.h
Go to the documentation of this file.
1//===- JSONFormatImpl.h -----------------------------------------*- C++ -*-===//
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// Internal implementation header shared by all translation units in the
10// JSONFormat subdirectory. Not part of the public API.
11//
12//===----------------------------------------------------------------------===//
13
14#ifndef LLVM_CLANG_LIB_ScalableStaticAnalysis_CORE_SERIALIZATION_JSONFORMAT_JSONFORMATIMPL_H
15#define LLVM_CLANG_LIB_ScalableStaticAnalysis_CORE_SERIALIZATION_JSONFORMAT_JSONFORMATIMPL_H
16
25#include "llvm/ADT/STLExtras.h"
26#include "llvm/ADT/StringExtras.h"
27#include "llvm/Support/ErrorHandling.h"
28#include "llvm/Support/FileSystem.h"
29#include "llvm/Support/FormatVariadic.h"
30#include "llvm/Support/JSON.h"
31#include "llvm/Support/MemoryBuffer.h"
32#include "llvm/Support/Path.h"
33#include "llvm/Support/Registry.h"
34
35namespace clang::ssaf {
36
37using Array = llvm::json::Array;
38using Object = llvm::json::Object;
39using Value = llvm::json::Value;
40
41//----------------------------------------------------------------------------
42// File Format Constant
43//----------------------------------------------------------------------------
44
45inline constexpr const char *JSONFormatFileExtension = ".json";
46
47//----------------------------------------------------------------------------
48// Error Message Constants
49//----------------------------------------------------------------------------
50
51namespace ErrorMessages {
52
53inline constexpr const char *FailedToReadFile =
54 "failed to read file '{0}': {1}";
55inline constexpr const char *FailedToWriteFile =
56 "failed to write file '{0}': {1}";
57inline constexpr const char *FileNotFound = "file does not exist";
58inline constexpr const char *FileIsDirectory =
59 "path is a directory, not a file";
60inline constexpr const char *FileIsNotJSON =
61 "file does not end with '{0}' extension";
62inline constexpr const char *FileExists = "file already exists";
63inline constexpr const char *ParentDirectoryNotFound =
64 "parent directory does not exist";
65
66inline constexpr const char *ReadingFromField = "reading {0} from field '{1}'";
67inline constexpr const char *WritingToField = "writing {0} to field '{1}'";
68inline constexpr const char *ReadingFromIndex = "reading {0} from index '{1}'";
69inline constexpr const char *WritingToIndex = "writing {0} to index '{1}'";
70inline constexpr const char *ReadingFromFile = "reading {0} from file '{1}'";
71inline constexpr const char *WritingToFile = "writing {0} to file '{1}'";
72
73inline constexpr const char *FailedInsertionOnDuplication =
74 "failed to insert {0} at index '{1}': encountered duplicate '{2}'";
75
76inline constexpr const char *FailedToReadObject =
77 "failed to read {0}: expected JSON {1}";
78inline constexpr const char *FailedToReadObjectAtField =
79 "failed to read {0} from field '{1}': expected JSON {2}";
80inline constexpr const char *FailedToReadObjectAtIndex =
81 "failed to read {0} from index '{1}': expected JSON {2}";
82
83inline constexpr const char *MismatchedSummaryType =
84 "expected '{0}' for field '{1}' but got '{2}'";
85inline constexpr const char *MismatchedNestedNamespace =
86 "field '{0}' does not equal the enclosing artifact's namespace";
87inline constexpr const char *UnknownArtifactType =
88 "unknown value '{0}' for field '{1}': expected '{2}', '{3}', or '{4}'";
89inline constexpr const char *UnknownArtifactEncodingType =
90 "unknown value '{0}' for field '{1}': expected '{2}', '{3}', '{4}', '{5}', "
91 "or '{6}'";
92
94 "failed to deserialize EntitySummary: no FormatInfo registered for '{0}'";
95inline constexpr const char *FailedToSerializeEntitySummaryNoFormatInfo =
96 "failed to serialize EntitySummary: no FormatInfo registered for '{0}'";
97
98inline constexpr const char *FailedToDeserializeEntitySummaryMissingData =
99 "failed to deserialize EntitySummary: null EntitySummary data for '{0}'";
100inline constexpr const char *FailedToSerializeEntitySummaryMissingData =
101 "JSONFormat - null EntitySummary data for '{0}'";
102
103inline constexpr const char
105 "failed to deserialize EntitySummary: EntitySummary data for '{0}' "
106 "reports mismatched '{1}'";
107inline constexpr const char
109 "JSONFormat - EntitySummary data for '{0}' reports mismatched '{1}'";
110
111inline constexpr const char *InvalidBuildNamespaceKind =
112 "invalid BuildNamespaceKind value '{0}' for field 'kind'";
113
114inline constexpr const char *InvalidEntityLinkageType =
115 "invalid EntityLinkageType value '{0}' for field 'type'";
116
117inline constexpr const char *FailedToDeserializeLinkageTableExtraId =
118 "failed to deserialize LinkageTable: extra '{0}' not present in IdTable";
119
120inline constexpr const char *FailedToDeserializeLinkageTableMissingId =
121 "failed to deserialize LinkageTable: missing '{0}' present in IdTable";
122
123inline constexpr const char *FailedToReadEntityIdObject =
124 "failed to read EntityId: expected JSON object with a single '{0}' key "
125 "mapped to a number (unsigned 64-bit integer)";
126
127inline constexpr const char *FailedToPatchEntityIdNotInTable =
128 "failed to patch EntityId: '{0}' not found in entity resolution table";
129
130inline constexpr const char *TargetTripleNotNormalized =
131 "target triple '{0}' is not in normalized form (expected '{1}')";
132
133} // namespace ErrorMessages
134
135//----------------------------------------------------------------------------
136// Entity Id JSON Representation
137//----------------------------------------------------------------------------
138
139/// An entity ID is encoded as the single-key object {"@": <index>}.
140inline constexpr const char *JSONEntityIdKey = "@";
141
142//----------------------------------------------------------------------------
143// Summary Type JSON Representation
144//----------------------------------------------------------------------------
145
146/// Root-object key naming the summary kind so files are self-describing.
147inline constexpr const char *JSONTypeKey = "type";
148
149/// Value written to \c JSONTypeKey for serialized \c TUSummary files.
150inline constexpr const char *JSONTypeValueTUSummary = "TUSummary";
151
152/// Value written to \c JSONTypeKey for serialized \c LUSummary files.
153inline constexpr const char *JSONTypeValueLUSummary = "LUSummary";
154
155/// Value written to \c JSONTypeKey for serialized \c StaticLibrary files.
156inline constexpr const char *JSONTypeValueStaticLibrary = "StaticLibrary";
157
158/// Value written to \c JSONTypeKey for serialized \c MultiArchStaticLibrary
159/// files.
160inline constexpr const char *JSONTypeValueMultiArchStaticLibrary =
161 "MultiArchStaticLibrary";
162
163/// Value written to \c JSONTypeKey for serialized \c MultiArchSharedLibrary
164/// files.
165inline constexpr const char *JSONTypeValueMultiArchSharedLibrary =
166 "MultiArchSharedLibrary";
167
168/// Value written to \c JSONTypeKey for serialized \c WPASuite files.
169inline constexpr const char *JSONTypeValueWPASuite = "WPASuite";
170
171/// Reads the \c JSONTypeKey field from the root object and verifies it
172/// equals \p ExpectedType. Returns success or an error with the field
173/// missing/mismatch detail.
174llvm::Error checkSummaryType(const Object &RootObject,
175 llvm::StringRef ExpectedType);
176
177/// Reads the \c JSONTypeKey field from the root object as a string.
178/// Returns the string on success; on error, the message is suitable for
179/// callers that want to report the missing field themselves.
181
182//----------------------------------------------------------------------------
183// JSON Reader and Writer
184//----------------------------------------------------------------------------
185
186llvm::Expected<Value> readJSON(llvm::StringRef Path);
187llvm::Error writeJSON(Value &&V, llvm::StringRef Path);
188
189//----------------------------------------------------------------------------
190// SummaryName helpers (free functions, anonymous-namespace in .cpp)
191//----------------------------------------------------------------------------
192
193SummaryName summaryNameFromJSON(llvm::StringRef SummaryNameStr);
194llvm::StringRef summaryNameToJSON(const SummaryName &SN);
195
196//----------------------------------------------------------------------------
197// AnalysisName helpers
198//----------------------------------------------------------------------------
199
200AnalysisName analysisNameFromJSON(llvm::StringRef AnalysisNameStr);
201llvm::StringRef analysisNameToJSON(const AnalysisName &AN);
202
203//----------------------------------------------------------------------------
204// BuildNamespaceKind helpers
205//----------------------------------------------------------------------------
206
208buildNamespaceKindFromJSON(llvm::StringRef BuildNamespaceKindStr);
209
210// Provided for consistency with respect to rest of the codebase.
212
213//----------------------------------------------------------------------------
214// EntityLinkageType helpers
215//----------------------------------------------------------------------------
216
218entityLinkageTypeFromJSON(llvm::StringRef EntityLinkageTypeStr);
219
220// Provided for consistency with respect to rest of the codebase.
222
223//----------------------------------------------------------------------------
224// TargetTriple helpers
225//----------------------------------------------------------------------------
226
227/// Validates that \p Triple is a target triple string in normalized form.
228/// Returns success if \p Triple equals \c llvm::Triple::normalize(Triple),
229/// otherwise returns an \c invalid_argument error describing the expected
230/// normalized form.
231llvm::Error validateNormalizedTargetTriple(llvm::StringRef Triple);
232
233} // namespace clang::ssaf
234
235#endif // LLVM_CLANG_LIB_ScalableStaticAnalysis_CORE_SERIALIZATION_JSONFORMAT_JSONFORMATIMPL_H
#define V(N, I)
Uniquely identifies a whole-program analysis and the AnalysisResult it produces.
Uniquely identifies an analysis summary.
Definition SummaryName.h:22
constexpr const char * FailedToSerializeEntitySummaryMismatchedSummaryName
constexpr const char * FailedToPatchEntityIdNotInTable
constexpr const char * InvalidBuildNamespaceKind
constexpr const char * ReadingFromFile
constexpr const char * ReadingFromField
constexpr const char * MismatchedSummaryType
constexpr const char * FailedToReadObjectAtIndex
constexpr const char * FailedToDeserializeEntitySummaryNoFormatInfo
constexpr const char * InvalidEntityLinkageType
constexpr const char * FileIsDirectory
constexpr const char * FailedToDeserializeEntitySummaryMismatchedSummaryName
constexpr const char * FileNotFound
constexpr const char * FailedToReadObjectAtField
constexpr const char * FileExists
constexpr const char * FailedToReadEntityIdObject
constexpr const char * MismatchedNestedNamespace
constexpr const char * WritingToFile
constexpr const char * ReadingFromIndex
constexpr const char * WritingToField
constexpr const char * TargetTripleNotNormalized
constexpr const char * FailedInsertionOnDuplication
constexpr const char * FailedToDeserializeLinkageTableExtraId
constexpr const char * FailedToSerializeEntitySummaryMissingData
constexpr const char * UnknownArtifactEncodingType
constexpr const char * FailedToDeserializeEntitySummaryMissingData
constexpr const char * FailedToWriteFile
constexpr const char * FailedToDeserializeLinkageTableMissingId
constexpr const char * FileIsNotJSON
constexpr const char * FailedToReadFile
constexpr const char * FailedToReadObject
constexpr const char * ParentDirectoryNotFound
constexpr const char * UnknownArtifactType
constexpr const char * FailedToSerializeEntitySummaryNoFormatInfo
constexpr const char * WritingToIndex
SummaryName summaryNameFromJSON(llvm::StringRef SummaryNameStr)
llvm::StringRef summaryNameToJSON(const SummaryName &SN)
constexpr const char * JSONTypeValueMultiArchSharedLibrary
Value written to JSONTypeKey for serialized MultiArchSharedLibrary files.
llvm::json::Array Array
constexpr const char * JSONTypeKey
Root-object key naming the summary kind so files are self-describing.
llvm::Expected< llvm::StringRef > readSummaryType(const Object &RootObject)
Reads the JSONTypeKey field from the root object as a string.
constexpr const char * JSONTypeValueWPASuite
Value written to JSONTypeKey for serialized WPASuite files.
llvm::Expected< EntityLinkageType > entityLinkageTypeFromJSON(llvm::StringRef EntityLinkageTypeStr)
constexpr const char * JSONTypeValueTUSummary
Value written to JSONTypeKey for serialized TUSummary files.
constexpr const char * JSONEntityIdKey
An entity ID is encoded as the single-key object {"@": <index>}.
llvm::StringRef analysisNameToJSON(const AnalysisName &AN)
llvm::json::Value Value
constexpr const char * JSONTypeValueLUSummary
Value written to JSONTypeKey for serialized LUSummary files.
AnalysisName analysisNameFromJSON(llvm::StringRef AnalysisNameStr)
llvm::Error validateNormalizedTargetTriple(llvm::StringRef Triple)
Validates that Triple is a target triple string in normalized form.
constexpr const char * JSONTypeValueStaticLibrary
Value written to JSONTypeKey for serialized StaticLibrary files.
llvm::Expected< BuildNamespaceKind > buildNamespaceKindFromJSON(llvm::StringRef BuildNamespaceKindStr)
llvm::json::Object Object
llvm::Expected< Value > readJSON(llvm::StringRef Path)
constexpr const char * JSONTypeValueMultiArchStaticLibrary
Value written to JSONTypeKey for serialized MultiArchStaticLibrary files.
llvm::Error checkSummaryType(const Object &RootObject, llvm::StringRef ExpectedType)
Reads the JSONTypeKey field from the root object and verifies it equals ExpectedType.
constexpr const char * JSONFormatFileExtension
llvm::StringRef buildNamespaceKindToJSON(BuildNamespaceKind BNK)
llvm::StringRef entityLinkageTypeToJSON(EntityLinkageType LT)
llvm::Error writeJSON(Value &&V, llvm::StringRef Path)
llvm::Expected< QualType > ExpectedType