clang 23.0.0git
TagsPairsAnalysis.cpp
Go to the documentation of this file.
1//===- TagsPairsAnalysis.cpp - Derived analysis for ExamplePlugin ---------===//
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 "AnalysisResults.h"
13#include "llvm/Support/Error.h"
14#include "llvm/Support/JSON.h"
15#include "llvm/Support/Registry.h"
16#include <memory>
17
18using namespace clang::ssaf;
19using namespace llvm;
22
23namespace {
24
25//===----------------------------------------------------------------------===//
26// TagsPairsAnalysisResult
27//
28// Aggregate statistics derived from TagsAnalysisResult and
29// PairsAnalysisResult. Serialized as:
30// { "unique_tag_count": N, "entity_count": N,
31// "total_pair_count": N, "max_pairs_per_entity": N }
32//===----------------------------------------------------------------------===//
33
34struct TagsPairsAnalysisResult final : AnalysisResult {
35 static AnalysisName analysisName() {
36 return AnalysisName("TagsPairsAnalysisResult");
37 }
38
39 int UniqueTagCount = 0;
40 int EntityCount = 0;
41 int TotalPairCount = 0;
42 int MaxPairsPerEntity = 0;
43};
44
45json::Object serializeTagsPairsAnalysisResult(const TagsPairsAnalysisResult &R,
47 return json::Object{{"unique_tag_count", R.UniqueTagCount},
48 {"entity_count", R.EntityCount},
49 {"total_pair_count", R.TotalPairCount},
50 {"max_pairs_per_entity", R.MaxPairsPerEntity}};
51}
52
54deserializeTagsPairsAnalysisResult(const json::Object &Obj,
56 auto R = std::make_unique<TagsPairsAnalysisResult>();
57
58 auto UTC = Obj.getInteger("unique_tag_count");
59 if (!UTC) {
60 return createStringError(inconvertibleErrorCode(),
61 "missing or invalid 'unique_tag_count'");
62 }
63 R->UniqueTagCount = static_cast<int>(*UTC);
64
65 auto EC = Obj.getInteger("entity_count");
66 if (!EC) {
67 return createStringError(inconvertibleErrorCode(),
68 "missing or invalid 'entity_count'");
69 }
70 R->EntityCount = static_cast<int>(*EC);
71
72 auto TPC = Obj.getInteger("total_pair_count");
73 if (!TPC) {
74 return createStringError(inconvertibleErrorCode(),
75 "missing or invalid 'total_pair_count'");
76 }
77 R->TotalPairCount = static_cast<int>(*TPC);
78
79 auto MPE = Obj.getInteger("max_pairs_per_entity");
80 if (!MPE) {
81 return createStringError(inconvertibleErrorCode(),
82 "missing or invalid 'max_pairs_per_entity'");
83 }
84 R->MaxPairsPerEntity = static_cast<int>(*MPE);
85
86 return std::move(R);
87}
88
89JSONFormat::AnalysisResultRegistry::Add<TagsPairsAnalysisResult>
90 RegisterSummaryResultForJSON(serializeTagsPairsAnalysisResult,
91 deserializeTagsPairsAnalysisResult);
92
93//===----------------------------------------------------------------------===//
94// TagsPairsAnalysis
95//
96// DerivedAnalysis that depends on TagsAnalysisResult and PairsAnalysisResult.
97// Computes aggregate statistics:
98// - unique_tag_count: number of distinct tags
99// - entity_count: number of entities with pair data
100// - total_pair_count: sum of all per-entity pair counts
101// - max_pairs_per_entity: maximum pairs on any single entity
102//===----------------------------------------------------------------------===//
103
104class TagsPairsAnalysis final
105 : public DerivedAnalysis<TagsPairsAnalysisResult, TagsAnalysisResult,
106 PairsAnalysisResult> {
107public:
108 using ResultType = TagsPairsAnalysisResult;
109
110 llvm::Error initialize(const TagsAnalysisResult &Tags,
111 const PairsAnalysisResult &Pairs) override {
112 result().UniqueTagCount = static_cast<int>(Tags.Tags.size());
113 result().EntityCount = static_cast<int>(Pairs.PairCounts.size());
114
115 int Total = 0;
116 int Max = 0;
117 for (const auto &[Id, Count] : Pairs.PairCounts) {
118 Total += Count;
119 if (Count > Max)
120 Max = Count;
121 }
122 result().TotalPairCount = Total;
123 result().MaxPairsPerEntity = Max;
124
125 return llvm::Error::success();
126 }
127
128 llvm::Expected<bool> step() override {
129 return false; // converged in initialize
130 }
131};
132
134 RegisterTagsPairsAnalysis("Aggregate tag and pair statistics");
135
136} // namespace
Base class for whole-program analysis results.
Typed intermediate that concrete derived analyses inherit from.
llvm::function_ref< llvm::Expected< EntityId >(const Object &)> EntityIdFromJSONFn
Definition JSONFormat.h:71
llvm::function_ref< Object(EntityId)> EntityIdToJSONFn
Definition JSONFormat.h:70
void initialize(TemplateInstantiationCallbackPtrs &Callbacks, const Sema &TheSema)
Diagnostic wrappers for TextAPI types for error reporting.
Definition Dominators.h:30
float __ovld __cnfn step(float, float)
Returns 0.0 if x < edge, otherwise it returns 1.0.
Registers AnalysisT with the unified registry.
std::vector< std::pair< clang::ssaf::EntityId, int > > PairCounts
std::vector< std::string > Tags