clang 22.0.0git
EntryPointStats.cpp
Go to the documentation of this file.
1//===- EntryPointStats.cpp --------------------------------------*- 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
10#include "clang/AST/DeclBase.h"
13#include "llvm/ADT/STLExtras.h"
14#include "llvm/ADT/SmallVector.h"
15#include "llvm/ADT/StringExtras.h"
16#include "llvm/ADT/StringRef.h"
17#include "llvm/Support/FileSystem.h"
18#include "llvm/Support/ManagedStatic.h"
19#include "llvm/Support/raw_ostream.h"
20#include <iterator>
21
22using namespace clang;
23using namespace ento;
24
25namespace {
26struct Registry {
27 std::vector<BoolEPStat *> BoolStats;
28 std::vector<CounterEPStat *> CounterStats;
29 std::vector<UnsignedMaxEPStat *> UnsignedMaxStats;
30 std::vector<UnsignedEPStat *> UnsignedStats;
31
32 bool IsLocked = false;
33
34 struct Snapshot {
35 const Decl *EntryPoint;
36 std::vector<bool> BoolStatValues;
37 std::vector<unsigned> UnsignedStatValues;
38
39 void dumpAsCSV(llvm::raw_ostream &OS) const;
40 };
41
42 std::vector<Snapshot> Snapshots;
43 std::string EscapedCPPFileName;
44};
45} // namespace
46
47static llvm::ManagedStatic<Registry> StatsRegistry;
48
49namespace {
50template <typename Callback> void enumerateStatVectors(const Callback &Fn) {
51 Fn(StatsRegistry->BoolStats);
52 Fn(StatsRegistry->CounterStats);
53 Fn(StatsRegistry->UnsignedMaxStats);
54 Fn(StatsRegistry->UnsignedStats);
55}
56} // namespace
57
58static void checkStatName(const EntryPointStat *M) {
59#ifdef NDEBUG
60 return;
61#endif // NDEBUG
62 constexpr std::array AllowedSpecialChars = {
63 '+', '-', '_', '=', ':', '(', ')', '@', '!', '~',
64 '$', '%', '^', '&', '*', '\'', ';', '<', '>', '/'};
65 for (unsigned char C : M->name()) {
66 if (!std::isalnum(C) && !llvm::is_contained(AllowedSpecialChars, C)) {
67 llvm::errs() << "Stat name \"" << M->name() << "\" contains character '"
68 << C << "' (" << static_cast<int>(C)
69 << ") that is not allowed.";
70 assert(false && "The Stat name contains unallowed character");
71 }
72 }
73}
74
75void EntryPointStat::lockRegistry(llvm::StringRef CPPFileName) {
76 auto CmpByNames = [](const EntryPointStat *L, const EntryPointStat *R) {
77 return L->name() < R->name();
78 };
79 enumerateStatVectors(
80 [CmpByNames](auto &Stats) { llvm::sort(Stats, CmpByNames); });
81 enumerateStatVectors(
82 [](const auto &Stats) { llvm::for_each(Stats, checkStatName); });
83 StatsRegistry->IsLocked = true;
84 llvm::raw_string_ostream OS(StatsRegistry->EscapedCPPFileName);
85 llvm::printEscapedString(CPPFileName, OS);
86}
87
88[[maybe_unused]] static bool isRegistered(llvm::StringLiteral Name) {
89 auto ByName = [Name](const EntryPointStat *M) { return M->name() == Name; };
90 bool Result = false;
91 enumerateStatVectors([ByName, &Result](const auto &Stats) {
92 Result = Result || llvm::any_of(Stats, ByName);
93 });
94 return Result;
95}
96
97BoolEPStat::BoolEPStat(llvm::StringLiteral Name) : EntryPointStat(Name) {
98 assert(!StatsRegistry->IsLocked);
99 assert(!isRegistered(Name));
100 StatsRegistry->BoolStats.push_back(this);
101}
102
103CounterEPStat::CounterEPStat(llvm::StringLiteral Name) : EntryPointStat(Name) {
104 assert(!StatsRegistry->IsLocked);
105 assert(!isRegistered(Name));
106 StatsRegistry->CounterStats.push_back(this);
107}
108
110 : EntryPointStat(Name) {
111 assert(!StatsRegistry->IsLocked);
112 assert(!isRegistered(Name));
113 StatsRegistry->UnsignedMaxStats.push_back(this);
114}
115
116UnsignedEPStat::UnsignedEPStat(llvm::StringLiteral Name)
117 : EntryPointStat(Name) {
118 assert(!StatsRegistry->IsLocked);
119 assert(!isRegistered(Name));
120 StatsRegistry->UnsignedStats.push_back(this);
121}
122
123static std::vector<unsigned> consumeUnsignedStats() {
124 std::vector<unsigned> Result;
125 Result.reserve(StatsRegistry->CounterStats.size() +
126 StatsRegistry->UnsignedMaxStats.size() +
127 StatsRegistry->UnsignedStats.size());
128 for (auto *M : StatsRegistry->CounterStats) {
129 Result.push_back(M->value());
130 M->reset();
131 }
132 for (auto *M : StatsRegistry->UnsignedMaxStats) {
133 Result.push_back(M->value());
134 M->reset();
135 }
136 for (auto *M : StatsRegistry->UnsignedStats) {
137 Result.push_back(M->value());
138 M->reset();
139 }
140 return Result;
141}
142
143static std::vector<llvm::StringLiteral> getStatNames() {
144 std::vector<llvm::StringLiteral> Ret;
145 auto GetName = [](const EntryPointStat *M) { return M->name(); };
146 enumerateStatVectors([GetName, &Ret](const auto &Stats) {
147 transform(Stats, std::back_inserter(Ret), GetName);
148 });
149 return Ret;
150}
151
152static std::string getUSR(const Decl *D) {
154 if (index::generateUSRForDecl(D, Buf)) {
155 assert(false && "This should never fail");
157 }
158 return llvm::toStringRef(Buf).str();
159}
160
161void Registry::Snapshot::dumpAsCSV(llvm::raw_ostream &OS) const {
162 OS << '"';
163 llvm::printEscapedString(getUSR(EntryPoint), OS);
164 OS << "\",\"";
165 OS << StatsRegistry->EscapedCPPFileName << "\",\"";
166 llvm::printEscapedString(
168 OS << "\",";
169 auto PrintAsBool = [&OS](bool B) { OS << (B ? "true" : "false"); };
170 llvm::interleave(BoolStatValues, OS, PrintAsBool, ",");
171 OS << ((BoolStatValues.empty() || UnsignedStatValues.empty()) ? "" : ",");
172 llvm::interleave(UnsignedStatValues, OS, [&OS](unsigned U) { OS << U; }, ",");
173}
174
175static std::vector<bool> consumeBoolStats() {
176 std::vector<bool> Result;
177 Result.reserve(StatsRegistry->BoolStats.size());
178 for (auto *M : StatsRegistry->BoolStats) {
179 Result.push_back(M->value());
180 M->reset();
181 }
182 return Result;
183}
184
185void EntryPointStat::takeSnapshot(const Decl *EntryPoint) {
186 auto BoolValues = consumeBoolStats();
187 auto UnsignedValues = consumeUnsignedStats();
188 StatsRegistry->Snapshots.push_back(
189 {EntryPoint, std::move(BoolValues), std::move(UnsignedValues)});
190}
191
193 std::error_code EC;
194 llvm::raw_fd_ostream File(FileName, EC, llvm::sys::fs::OF_Text);
195 if (EC)
196 return;
198}
199
200void EntryPointStat::dumpStatsAsCSV(llvm::raw_ostream &OS) {
201 OS << "USR,File,DebugName,";
202 llvm::interleave(getStatNames(), OS, [&OS](const auto &a) { OS << a; }, ",");
203 OS << "\n";
204
205 std::vector<std::string> Rows;
206 Rows.reserve(StatsRegistry->Snapshots.size());
207 for (const auto &Snapshot : StatsRegistry->Snapshots) {
208 std::string Row;
209 llvm::raw_string_ostream RowOs(Row);
210 Snapshot.dumpAsCSV(RowOs);
211 RowOs << "\n";
212 Rows.push_back(RowOs.str());
213 }
214 llvm::sort(Rows);
215 for (const auto &Row : Rows) {
216 OS << Row;
217 }
218}
This file defines AnalysisDeclContext, a class that manages the analysis context data for context sen...
static void checkStatName(const EntryPointStat *M)
static std::vector< unsigned > consumeUnsignedStats()
static llvm::ManagedStatic< Registry > StatsRegistry
static std::vector< llvm::StringLiteral > getStatNames()
static bool isRegistered(llvm::StringLiteral Name)
static std::vector< bool > consumeBoolStats()
static std::string getUSR(const Decl *D)
static std::string getFunctionName(const Decl *D)
Decl - This represents one declaration (or definition), e.g.
Definition DeclBase.h:86
BoolEPStat(llvm::StringLiteral Name)
CounterEPStat(llvm::StringLiteral Name)
static void lockRegistry(llvm::StringRef CPPFileName)
static void dumpStatsAsCSV(llvm::raw_ostream &OS)
static void takeSnapshot(const Decl *EntryPoint)
llvm::StringLiteral name() const
EntryPointStat(llvm::StringLiteral Name)
UnsignedEPStat(llvm::StringLiteral Name)
UnsignedMaxEPStat(llvm::StringLiteral Name)
@ OS
Indicates that the tracking object is a descendant of a referenced-counted OSObject,...
bool generateUSRForDecl(const Decl *D, SmallVectorImpl< char > &Buf)
Generate a USR for a Decl, including the USR prefix.
std::variant< struct RequiresDecl, struct HeaderDecl, struct UmbrellaDirDecl, struct ModuleDecl, struct ExcludeDecl, struct ExportDecl, struct ExportAsDecl, struct ExternModuleDecl, struct UseDecl, struct LinkDecl, struct ConfigMacrosDecl, struct ConflictDecl > Decl
All declarations that can appear in a module declaration.
The JSON file list parser is used to communicate input to InstallAPI.