xref: /freebsd/contrib/llvm-project/clang/lib/StaticAnalyzer/Core/EntryPointStats.cpp (revision 700637cbb5e582861067a11aaca4d053546871d2)
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 {
enumerateStatVectors(const Callback & Fn)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 
checkStatName(const EntryPointStat * M)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(AllowedSpecialChars, 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 
lockRegistry()72 void EntryPointStat::lockRegistry() {
73   auto CmpByNames = [](const EntryPointStat *L, const EntryPointStat *R) {
74     return L->name() < R->name();
75   };
76   enumerateStatVectors(
77       [CmpByNames](auto &Stats) { llvm::sort(Stats, CmpByNames); });
78   enumerateStatVectors(
79       [](const auto &Stats) { llvm::for_each(Stats, checkStatName); });
80   StatsRegistry->IsLocked = true;
81 }
82 
isRegistered(llvm::StringLiteral Name)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([ByName, &Result](const auto &Stats) {
87     Result = Result || llvm::any_of(Stats, ByName);
88   });
89   return Result;
90 }
91 
BoolEPStat(llvm::StringLiteral Name)92 BoolEPStat::BoolEPStat(llvm::StringLiteral Name) : EntryPointStat(Name) {
93   assert(!StatsRegistry->IsLocked);
94   assert(!isRegistered(Name));
95   StatsRegistry->BoolStats.push_back(this);
96 }
97 
CounterEPStat(llvm::StringLiteral Name)98 CounterEPStat::CounterEPStat(llvm::StringLiteral Name) : EntryPointStat(Name) {
99   assert(!StatsRegistry->IsLocked);
100   assert(!isRegistered(Name));
101   StatsRegistry->CounterStats.push_back(this);
102 }
103 
UnsignedMaxEPStat(llvm::StringLiteral Name)104 UnsignedMaxEPStat::UnsignedMaxEPStat(llvm::StringLiteral Name)
105     : EntryPointStat(Name) {
106   assert(!StatsRegistry->IsLocked);
107   assert(!isRegistered(Name));
108   StatsRegistry->UnsignedMaxStats.push_back(this);
109 }
110 
UnsignedEPStat(llvm::StringLiteral Name)111 UnsignedEPStat::UnsignedEPStat(llvm::StringLiteral Name)
112     : EntryPointStat(Name) {
113   assert(!StatsRegistry->IsLocked);
114   assert(!isRegistered(Name));
115   StatsRegistry->UnsignedStats.push_back(this);
116 }
117 
consumeUnsignedStats()118 static std::vector<unsigned> consumeUnsignedStats() {
119   std::vector<unsigned> Result;
120   Result.reserve(StatsRegistry->CounterStats.size() +
121                  StatsRegistry->UnsignedMaxStats.size() +
122                  StatsRegistry->UnsignedStats.size());
123   for (auto *M : StatsRegistry->CounterStats) {
124     Result.push_back(M->value());
125     M->reset();
126   }
127   for (auto *M : StatsRegistry->UnsignedMaxStats) {
128     Result.push_back(M->value());
129     M->reset();
130   }
131   for (auto *M : StatsRegistry->UnsignedStats) {
132     Result.push_back(M->value());
133     M->reset();
134   }
135   return Result;
136 }
137 
getStatNames()138 static std::vector<llvm::StringLiteral> getStatNames() {
139   std::vector<llvm::StringLiteral> Ret;
140   auto GetName = [](const EntryPointStat *M) { return M->name(); };
141   enumerateStatVectors([GetName, &Ret](const auto &Stats) {
142     transform(Stats, std::back_inserter(Ret), GetName);
143   });
144   return Ret;
145 }
146 
dumpAsCSV(llvm::raw_ostream & OS) const147 void Registry::Snapshot::dumpAsCSV(llvm::raw_ostream &OS) const {
148   OS << '"';
149   llvm::printEscapedString(
150       clang::AnalysisDeclContext::getFunctionName(EntryPoint), OS);
151   OS << "\", ";
152   auto PrintAsBool = [&OS](bool B) { OS << (B ? "true" : "false"); };
153   llvm::interleaveComma(BoolStatValues, OS, PrintAsBool);
154   OS << ((BoolStatValues.empty() || UnsignedStatValues.empty()) ? "" : ", ");
155   llvm::interleaveComma(UnsignedStatValues, OS);
156 }
157 
consumeBoolStats()158 static std::vector<bool> consumeBoolStats() {
159   std::vector<bool> Result;
160   Result.reserve(StatsRegistry->BoolStats.size());
161   for (auto *M : StatsRegistry->BoolStats) {
162     Result.push_back(M->value());
163     M->reset();
164   }
165   return Result;
166 }
167 
takeSnapshot(const Decl * EntryPoint)168 void EntryPointStat::takeSnapshot(const Decl *EntryPoint) {
169   auto BoolValues = consumeBoolStats();
170   auto UnsignedValues = consumeUnsignedStats();
171   StatsRegistry->Snapshots.push_back(
172       {EntryPoint, std::move(BoolValues), std::move(UnsignedValues)});
173 }
174 
dumpStatsAsCSV(llvm::StringRef FileName)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(File);
181 }
182 
dumpStatsAsCSV(llvm::raw_ostream & OS)183 void EntryPointStat::dumpStatsAsCSV(llvm::raw_ostream &OS) {
184   OS << "EntryPoint, ";
185   llvm::interleaveComma(getStatNames(), OS);
186   OS << "\n";
187 
188   std::vector<std::string> Rows;
189   Rows.reserve(StatsRegistry->Snapshots.size());
190   for (const auto &Snapshot : StatsRegistry->Snapshots) {
191     std::string Row;
192     llvm::raw_string_ostream RowOs(Row);
193     Snapshot.dumpAsCSV(RowOs);
194     RowOs << "\n";
195     Rows.push_back(RowOs.str());
196   }
197   llvm::sort(Rows);
198   for (const auto &Row : Rows) {
199     OS << Row;
200   }
201 }
202