clang 23.0.0git
UnsafeBufferUsageFormat.cpp
Go to the documentation of this file.
1//===- UnsafeBufferUsageFormat.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
14#include "llvm/Support/Error.h"
15#include "llvm/Support/JSON.h"
16
17using namespace clang;
18using namespace ssaf;
19using Array = llvm::json::Array;
20using Object = llvm::json::Object;
21
22static constexpr llvm::StringLiteral SummarySerializationKey = "UnsafeBuffers";
23
25ssaf::buildUnsafeBufferUsageEntitySummary(EntityPointerLevelSet UnsafeBuffers);
26
27extern llvm::iterator_range<EntityPointerLevelSet::const_iterator>
28ssaf::getUnsafeBuffers(const UnsafeBufferUsageEntitySummary &S);
29
32 const auto &SS = static_cast<const UnsafeBufferUsageEntitySummary &>(S);
33 Array UnsafeBuffersData;
34
35 for (const auto &EPL : getUnsafeBuffers(SS))
36 UnsafeBuffersData.push_back(entityPointerLevelToJSON(EPL, Fn));
37 return Object{{SummarySerializationKey.data(), std::move(UnsafeBuffersData)}};
38}
39
42 const Array *UnsafeBuffersData =
43 Data.getArray(SummarySerializationKey.data());
44
45 if (!UnsafeBuffersData)
46 return makeSawButExpectedError(Object(Data), "an Object with a key %s",
48
49 EntityPointerLevelSet EPLs;
50
51 for (const auto &EltData : *UnsafeBuffersData) {
53 entityPointerLevelFromJSON(EltData, Fn);
54
55 if (!EPL)
56 return EPL.takeError();
57 EPLs.insert(*EPL);
58 }
59 return std::make_unique<UnsafeBufferUsageEntitySummary>(
60 buildUnsafeBufferUsageEntitySummary(std::move(EPLs)));
61}
62
68
69namespace {
70struct UnsafeBufferUsageJSONFormatInfo final : JSONFormat::FormatInfo {
71 UnsafeBufferUsageJSONFormatInfo()
72 : JSONFormat::FormatInfo(UnsafeBufferUsageEntitySummary::summaryName(),
74};
75} // namespace
76
77static llvm::Registry<JSONFormat::FormatInfo>::Add<
78 UnsafeBufferUsageJSONFormatInfo>
81 "JSON Format info for UnsafeBufferUsageEntitySummary");
82
83namespace clang::ssaf {
84// NOLINTNEXTLINE(misc-use-internal-linkage)
86} // namespace clang::ssaf
llvm::json::Object Object
llvm::json::Array Array
static llvm::Expected< std::unique_ptr< EntitySummary > > deserialize(const Object &Data, EntityIdTable &, JSONFormat::EntityIdFromJSONFn Fn)
static llvm::Expected< std::unique_ptr< EntitySummary > > deserializeImpl(const Object &Data, JSONFormat::EntityIdFromJSONFn Fn)
static llvm::Registry< JSONFormat::FormatInfo >::Add< UnsafeBufferUsageJSONFormatInfo > RegisterUnsafeBufferUsageJSONFormatInfo(UnsafeBufferUsageEntitySummary::Name, "JSON Format info for UnsafeBufferUsageEntitySummary")
static Object serialize(const EntitySummary &S, JSONFormat::EntityIdToJSONFn Fn)
static constexpr llvm::StringLiteral SummarySerializationKey
Manages entity name interning and provides efficient EntityId handles.
Base class for analysis-specific summary data.
llvm::function_ref< llvm::Expected< EntityId >(const Object &)> EntityIdFromJSONFn
Definition JSONFormat.h:71
FormatInfoEntry< SerializerFn, DeserializerFn > FormatInfo
Definition JSONFormat.h:80
llvm::function_ref< Object(EntityId)> EntityIdToJSONFn
Definition JSONFormat.h:70
An UnsafeBufferUsageEntitySummary contains a set of EntityPointerLevels extracted from unsafe buffer ...
static constexpr llvm::StringLiteral Name
llvm::json::Value entityPointerLevelToJSON(const EntityPointerLevel &EPL, JSONFormat::EntityIdToJSONFn EntityId2JSON)
llvm::Error makeSawButExpectedError(const JSONTy &Saw, llvm::StringRef Expected, const Ts &...ExpectedArgs)
llvm::json::Object Object
Expected< EntityPointerLevel > entityPointerLevelFromJSON(const llvm::json::Value &EPLData, JSONFormat::EntityIdFromJSONFn EntityIdFromJSON)
volatile int UnsafeBufferUsageJSONFormatAnchorSource
The JSON file list parser is used to communicate input to InstallAPI.