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 | |
9 | #include "clang/StaticAnalyzer/Core/PathSensitive/EntryPointStats.h" |
10 | #include "clang/AST/DeclBase.h" |
11 | #include "clang/Analysis/AnalysisDeclContext.h" |
12 | #include "llvm/ADT/STLExtras.h" |
13 | #include "llvm/ADT/StringExtras.h" |
14 | #include "llvm/ADT/StringRef.h" |
15 | #include "llvm/Support/FileSystem.h" |
16 | #include "llvm/Support/ManagedStatic.h" |
17 | #include "llvm/Support/raw_ostream.h" |
18 | #include <iterator> |
19 | |
20 | using namespace clang; |
21 | using namespace ento; |
22 | |
23 | namespace { |
24 | struct Registry { |
25 | std::vector<BoolEPStat *> BoolStats; |
26 | std::vector<CounterEPStat *> CounterStats; |
27 | std::vector<UnsignedMaxEPStat *> UnsignedMaxStats; |
28 | std::vector<UnsignedEPStat *> UnsignedStats; |
29 | |
30 | bool IsLocked = false; |
31 | |
32 | struct Snapshot { |
33 | const Decl *EntryPoint; |
34 | std::vector<bool> BoolStatValues; |
35 | std::vector<unsigned> UnsignedStatValues; |
36 | |
37 | void dumpAsCSV(llvm::raw_ostream &OS) const; |
38 | }; |
39 | |
40 | std::vector<Snapshot> Snapshots; |
41 | }; |
42 | } // namespace |
43 | |
44 | static llvm::ManagedStatic<Registry> StatsRegistry; |
45 | |
46 | namespace { |
47 | template <typename Callback> void enumerateStatVectors(const Callback &Fn) { |
48 | Fn(StatsRegistry->BoolStats); |
49 | Fn(StatsRegistry->CounterStats); |
50 | Fn(StatsRegistry->UnsignedMaxStats); |
51 | Fn(StatsRegistry->UnsignedStats); |
52 | } |
53 | } // namespace |
54 | |
55 | static void checkStatName(const EntryPointStat *M) { |
56 | #ifdef NDEBUG |
57 | return; |
58 | #endif // NDEBUG |
59 | constexpr std::array AllowedSpecialChars = { |
60 | '+', '-', '_', '=', ':', '(', ')', '@', '!', '~', |
61 | '$', '%', '^', '&', '*', '\'', ';', '<', '>', '/'}; |
62 | for (unsigned char C : M->name()) { |
63 | if (!std::isalnum(C) && !llvm::is_contained(Range: AllowedSpecialChars, Element: C)) { |
64 | llvm::errs() << "Stat name \"" << M->name() << "\" contains character '" |
65 | << C << "' (" << static_cast<int>(C) |
66 | << ") that is not allowed." ; |
67 | assert(false && "The Stat name contains unallowed character" ); |
68 | } |
69 | } |
70 | } |
71 | |
72 | void EntryPointStat::lockRegistry() { |
73 | auto CmpByNames = [](const EntryPointStat *L, const EntryPointStat *R) { |
74 | return L->name() < R->name(); |
75 | }; |
76 | enumerateStatVectors( |
77 | Fn: [CmpByNames](auto &Stats) { llvm::sort(Stats, CmpByNames); }); |
78 | enumerateStatVectors( |
79 | Fn: [](const auto &Stats) { llvm::for_each(Stats, checkStatName); }); |
80 | StatsRegistry->IsLocked = true; |
81 | } |
82 | |
83 | [[maybe_unused]] static bool isRegistered(llvm::StringLiteral Name) { |
84 | auto ByName = [Name](const EntryPointStat *M) { return M->name() == Name; }; |
85 | bool Result = false; |
86 | enumerateStatVectors(Fn: [ByName, &Result](const auto &Stats) { |
87 | Result = Result || llvm::any_of(Stats, ByName); |
88 | }); |
89 | return Result; |
90 | } |
91 | |
92 | BoolEPStat::BoolEPStat(llvm::StringLiteral Name) : EntryPointStat(Name) { |
93 | assert(!StatsRegistry->IsLocked); |
94 | assert(!isRegistered(Name)); |
95 | StatsRegistry->BoolStats.push_back(x: this); |
96 | } |
97 | |
98 | CounterEPStat::CounterEPStat(llvm::StringLiteral Name) : EntryPointStat(Name) { |
99 | assert(!StatsRegistry->IsLocked); |
100 | assert(!isRegistered(Name)); |
101 | StatsRegistry->CounterStats.push_back(x: this); |
102 | } |
103 | |
104 | UnsignedMaxEPStat::UnsignedMaxEPStat(llvm::StringLiteral Name) |
105 | : EntryPointStat(Name) { |
106 | assert(!StatsRegistry->IsLocked); |
107 | assert(!isRegistered(Name)); |
108 | StatsRegistry->UnsignedMaxStats.push_back(x: this); |
109 | } |
110 | |
111 | UnsignedEPStat::UnsignedEPStat(llvm::StringLiteral Name) |
112 | : EntryPointStat(Name) { |
113 | assert(!StatsRegistry->IsLocked); |
114 | assert(!isRegistered(Name)); |
115 | StatsRegistry->UnsignedStats.push_back(x: this); |
116 | } |
117 | |
118 | static std::vector<unsigned> consumeUnsignedStats() { |
119 | std::vector<unsigned> Result; |
120 | Result.reserve(n: StatsRegistry->CounterStats.size() + |
121 | StatsRegistry->UnsignedMaxStats.size() + |
122 | StatsRegistry->UnsignedStats.size()); |
123 | for (auto *M : StatsRegistry->CounterStats) { |
124 | Result.push_back(x: M->value()); |
125 | M->reset(); |
126 | } |
127 | for (auto *M : StatsRegistry->UnsignedMaxStats) { |
128 | Result.push_back(x: M->value()); |
129 | M->reset(); |
130 | } |
131 | for (auto *M : StatsRegistry->UnsignedStats) { |
132 | Result.push_back(x: M->value()); |
133 | M->reset(); |
134 | } |
135 | return Result; |
136 | } |
137 | |
138 | static std::vector<llvm::StringLiteral> getStatNames() { |
139 | std::vector<llvm::StringLiteral> Ret; |
140 | auto GetName = [](const EntryPointStat *M) { return M->name(); }; |
141 | enumerateStatVectors(Fn: [GetName, &Ret](const auto &Stats) { |
142 | transform(Stats, std::back_inserter(x&: Ret), GetName); |
143 | }); |
144 | return Ret; |
145 | } |
146 | |
147 | void Registry::Snapshot::dumpAsCSV(llvm::raw_ostream &OS) const { |
148 | OS << '"'; |
149 | llvm::printEscapedString( |
150 | Name: clang::AnalysisDeclContext::getFunctionName(D: EntryPoint), Out&: OS); |
151 | OS << "\", " ; |
152 | auto PrintAsBool = [&OS](bool B) { OS << (B ? "true" : "false" ); }; |
153 | llvm::interleaveComma(c: BoolStatValues, os&: OS, each_fn: PrintAsBool); |
154 | OS << ((BoolStatValues.empty() || UnsignedStatValues.empty()) ? "" : ", " ); |
155 | llvm::interleaveComma(c: UnsignedStatValues, os&: OS); |
156 | } |
157 | |
158 | static std::vector<bool> consumeBoolStats() { |
159 | std::vector<bool> Result; |
160 | Result.reserve(n: StatsRegistry->BoolStats.size()); |
161 | for (auto *M : StatsRegistry->BoolStats) { |
162 | Result.push_back(x: M->value()); |
163 | M->reset(); |
164 | } |
165 | return Result; |
166 | } |
167 | |
168 | void EntryPointStat::takeSnapshot(const Decl *EntryPoint) { |
169 | auto BoolValues = consumeBoolStats(); |
170 | auto UnsignedValues = consumeUnsignedStats(); |
171 | StatsRegistry->Snapshots.push_back( |
172 | x: {.EntryPoint: EntryPoint, .BoolStatValues: std::move(BoolValues), .UnsignedStatValues: std::move(UnsignedValues)}); |
173 | } |
174 | |
175 | void EntryPointStat::dumpStatsAsCSV(llvm::StringRef FileName) { |
176 | std::error_code EC; |
177 | llvm::raw_fd_ostream File(FileName, EC, llvm::sys::fs::OF_Text); |
178 | if (EC) |
179 | return; |
180 | dumpStatsAsCSV(OS&: File); |
181 | } |
182 | |
183 | void EntryPointStat::dumpStatsAsCSV(llvm::raw_ostream &OS) { |
184 | OS << "EntryPoint, " ; |
185 | llvm::interleaveComma(c: getStatNames(), os&: OS); |
186 | OS << "\n" ; |
187 | |
188 | std::vector<std::string> Rows; |
189 | Rows.reserve(n: StatsRegistry->Snapshots.size()); |
190 | for (const auto &Snapshot : StatsRegistry->Snapshots) { |
191 | std::string Row; |
192 | llvm::raw_string_ostream RowOs(Row); |
193 | Snapshot.dumpAsCSV(OS&: RowOs); |
194 | RowOs << "\n" ; |
195 | Rows.push_back(x: RowOs.str()); |
196 | } |
197 | llvm::sort(C&: Rows); |
198 | for (const auto &Row : Rows) { |
199 | OS << Row; |
200 | } |
201 | } |
202 | |