clang 23.0.0git
PointerFlowAnalysis.cpp
Go to the documentation of this file.
1//===- PointerFlowAnalysis.cpp - WPA for PointerFlow ----------------------===//
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// PointerFlowAnalysis is a noop analysis.
9//
10// PointerFlowAnalysisResult is a map from EntityIds to
11// EdgeSets.
12//===----------------------------------------------------------------------===//
13
15#include "SSAFAnalysesCommon.h"
21#include "llvm/Support/Error.h"
22#include "llvm/Support/JSON.h"
23#include <memory>
24
25using namespace clang::ssaf;
26using namespace llvm;
27
28namespace {
29
30// Serialized as a flat array of alternating [EntityId, EdgesArray, ...] pairs.
31json::Object
32serializePointerFlowAnalysisResult(const PointerFlowAnalysisResult &R,
34 json::Array Content;
35
36 for (const auto &[Id, EntityEdges] : R.Edges) {
37 Content.push_back(IdToJSON(Id));
38 Content.push_back(json::Value(edgeSetToJSON(EntityEdges, IdToJSON)));
39 }
40
41 json::Object Result;
42
43 Result[PointerFlowAnalysisResultName] = std::move(Content);
44 return Result;
45}
46
47Expected<std::unique_ptr<AnalysisResult>> deserializePointerFlowAnalysisResult(
48 const json::Object &Obj, JSONFormat::EntityIdFromJSONFn IdFromJSON) {
49 const json::Array *Content = Obj.getArray(PointerFlowAnalysisResultName);
50
51 if (!Content)
52 return makeSawButExpectedError(Obj, "an object with a key %s",
54
55 if (Content->size() % 2 != 0)
57 *Content, "an even number of elements, got %lu", Content->size());
58
59 std::map<EntityId, EdgeSet> Edges;
60
61 for (size_t I = 0; I < Content->size(); I += 2) {
62 const json::Object *IdData = (*Content)[I].getAsObject();
63
64 if (!IdData)
65 return makeSawButExpectedError((*Content)[I],
66 "an object representing EntityId");
67
68 auto Id = IdFromJSON(*IdData);
69
70 if (!Id)
71 return Id.takeError();
72
73 const json::Array *EdgesData = (*Content)[I + 1].getAsArray();
74
75 if (!EdgesData)
76 return makeSawButExpectedError((*Content)[I + 1],
77 "an array of arrays representing EdgeSet");
78
79 auto EntityEdges = edgeSetFromJSON(*EdgesData, IdFromJSON);
80
81 if (!EntityEdges)
82 return EntityEdges.takeError();
83 Edges[*Id] = std::move(*EntityEdges);
84 }
85
86 auto Ret = std::make_unique<PointerFlowAnalysisResult>();
87
88 Ret->Edges = std::move(Edges);
89 return Ret;
90}
91
92JSONFormat::AnalysisResultRegistry::Add<PointerFlowAnalysisResult>
93 RegisterPointerFlowResultForJSON(serializePointerFlowAnalysisResult,
94 deserializePointerFlowAnalysisResult);
95
96class PointerFlowAnalysis final
97 : public SummaryAnalysis<PointerFlowAnalysisResult,
98 PointerFlowEntitySummary> {
99public:
100 llvm::Error add(EntityId Id,
101 const PointerFlowEntitySummary &Summary) override {
102 auto EdgesOfEntity = getEdges(Summary);
103
104 getResult().Edges[Id] = EdgeSet(EdgesOfEntity.begin(), EdgesOfEntity.end());
105 return llvm::Error::success();
106 }
107};
108
110 RegisterPointerFlowAnalysis("Whole-program pointer flow analysis");
111
112} // namespace
113
114// NOLINTNEXTLINE(misc-use-internal-linkage)
Result
Implement __builtin_bit_cast and related operations.
volatile int PointerFlowAnalysisAnchorSource
llvm::function_ref< llvm::Expected< EntityId >(const Object &)> EntityIdFromJSONFn
Definition JSONFormat.h:71
llvm::function_ref< Object(EntityId)> EntityIdToJSONFn
Definition JSONFormat.h:70
Typed intermediate that concrete summary analyses inherit from.
PRESERVE_NONE bool Ret(InterpState &S, CodePtr &PC)
Definition Interp.h:258
llvm::json::Array edgeSetToJSON(llvm::iterator_range< EdgeSet::const_iterator > Edges, JSONFormat::EntityIdToJSONFn IdToJSON)
Serialize an EdgeSet {(src1, dest1), (src1, dest2), (src2, dest3), (src2, dest4), ....
std::map< EntityPointerLevel, EntityPointerLevelSet > EdgeSet
Maps each source node to its destination nodes:
Definition PointerFlow.h:23
llvm::Error makeSawButExpectedError(const JSONTy &Saw, llvm::StringRef Expected, const Ts &...ExpectedArgs)
llvm::Expected< EdgeSet > edgeSetFromJSON(const llvm::json::Array &EdgesData, JSONFormat::EntityIdFromJSONFn IdFromJSON)
Deserialize an EdgeSet from the array format produced by edgeSetToJSON.
constexpr llvm::StringLiteral PointerFlowAnalysisResultName
Diagnostic wrappers for TextAPI types for error reporting.
Definition Dominators.h:30
Registers AnalysisT with the unified registry.