-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.cpp
294 lines (257 loc) · 10.4 KB
/
main.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
#include <sstream> // to print results in percentage
// Declares clang::SyntaxOnlyAction.
#include "clang/Frontend/FrontendActions.h"
#include "clang/Tooling/CommonOptionsParser.h"
#include "clang/Tooling/Tooling.h"
// Declares llvm::cl::extrahelp.
#include "llvm/Support/CommandLine.h"
#include "FriendStats.hpp"
#include "DataCrunching.hpp"
#include "DataIO.hpp"
using namespace clang::tooling;
using namespace llvm;
using namespace clang;
// Apply a custom category to all command-line options so that they are the
// only ones displayed.
static llvm::cl::OptionCategory MyToolCategory("friend-stats-tool options");
// CommonOptionsParser declares HelpMessage with a description of the common
// command-line options related to the compilation database and input files.
// It's nice to have this help message in all tools.
static cl::extrahelp CommonHelp(CommonOptionsParser::HelpMessage);
// A help message for this specific tool can be added afterwards.
static cl::extrahelp
MoreHelp("\nThis tool collects statistics about friend declarations");
static cl::opt<bool> UseCompilationDbFiles(
"db", cl::desc("Run the tool on all files of the compilation db"),
cl::ValueOptional, cl::cat(MyToolCategory));
static cl::opt<bool> PrintZeroPrivInHost(
"zh", cl::desc("Print friend function instances whose "
"befriending class has no private entities"),
cl::ValueOptional, cl::cat(MyToolCategory));
static cl::opt<bool> PrintZeroPrivInFriend(
"zf", cl::desc("Print friend function instances "
"who does not access any private entities."),
cl::ValueOptional, cl::cat(MyToolCategory));
static cl::opt<bool> NoStatistics(
"no_stats", cl::desc("Do not print out statistics about friend usage."),
cl::ValueOptional, cl::cat(MyToolCategory));
static cl::opt<bool> PrintMeyersCandidates(
"meyers_candidates",
cl::desc("Print Meyers candidate friend function instances."),
cl::ValueOptional, cl::cat(MyToolCategory));
static cl::opt<bool> PrintPossiblyIncorrectFriend(
"if", cl::desc("Print friend function instances "
"which are possible not correct."),
cl::ValueOptional, cl::cat(MyToolCategory));
static cl::opt<bool> PrintHostClassesWithZeroPrivate(
"host_classes_with_zero_priv",
cl::desc(
"Print befriending classes which have only public members and types."),
cl::ValueOptional, cl::cat(MyToolCategory));
static cl::opt<bool> PrintIncorrectFriendClasses(
"incorrect_friend_classes",
cl::desc("Print friend classes which don't use any private entities."),
cl::ValueOptional, cl::cat(MyToolCategory));
class ProgressIndicator : public SourceFileCallbacks {
const std::size_t numFiles = 0;
std::size_t processedFiles = 0;
public:
ProgressIndicator(std::size_t numFiles) : numFiles(numFiles) {}
virtual bool handleBeginSource(CompilerInstance &CI,
StringRef Filename) override {
++processedFiles;
llvm::outs() << Filename << " [" << processedFiles << "/" << numFiles << "]"
<< "\n";
llvm::outs().flush();
return true;
}
};
inline std::string to_percentage(double d) {
std::stringstream ss;
ss << std::fixed << d * 100 << " %";
return ss.str();
}
class DataTraversal {
public:
DataTraversal(const Result &result) : result(result) {}
void operator()() {
traverseFriendFuncData();
traverseFriendClassData();
if (PrintHostClassesWithZeroPrivate)
printHostClassesWithZeroPrivate();
if (!NoStatistics)
conclusion();
}
private:
const Result &result;
SelfDiagnostics diags;
HostClassesWithZeroPrivate hostClassesWithZeroPriv;
BefriendingClassesAllFriendsMC befriendingClassesAllFriendsMC;
struct Func {
Average average;
PercentageDistribution percentageDist;
NumberOfUsedPrivsDistribution usedPrivsDistribution;
ZeroPrivInHost zeroPrivInHost;
ZeroPrivInFriend zeroPrivInFriend;
MeyersCandidate meyersCandidate;
PossiblyIncorrectFriend possiblyIncorrect;
} func;
struct Class {
Average average;
PercentageDistribution percentageDist;
NumberOfUsedPrivsDistribution usedPrivsDistribution;
} clazz;
void traverseFriendFuncData() {
for (const auto &v : result.FuncResults) {
for (const auto funcResPair : v.second) {
const auto &funcRes = funcResPair.second;
if (diags(funcRes)) {
func.average(funcRes);
func.percentageDist(funcRes);
func.usedPrivsDistribution(funcRes);
if (PrintZeroPrivInHost && func.zeroPrivInHost(funcRes)) {
print(funcResPair);
}
if (PrintZeroPrivInFriend && func.zeroPrivInFriend(funcRes)) {
print(funcResPair);
}
auto mc = func.meyersCandidate(funcResPair);
if (PrintMeyersCandidates && mc) {
llvm::outs() << "Meyers candidate:\n";
print(funcResPair);
}
if (PrintPossiblyIncorrectFriend &&
func.possiblyIncorrect(funcResPair)) {
llvm::outs()
<< "Warning: possibly incorrect friend function instance:\n";
print(funcResPair);
}
hostClassesWithZeroPriv(funcRes);
befriendingClassesAllFriendsMC.functionInstance(funcResPair);
} else {
llvm::outs() << "WRONG MEASURE here:\n" << funcRes.friendDeclLocStr
<< "\n";
print(funcResPair);
llvm::outs() << "SKIPPING ENTRY FROM STATISTICS\n\n";
}
}
}
}
void traverseFriendClassData() {
for (const auto &friendDecls : result.ClassResults) {
for (const auto &classSpecs : friendDecls.second) {
IncorrectFriendClass incorrectFriendClass;
for (const auto &funcResPair : classSpecs.second.memberFuncResults) {
const auto &funcRes = funcResPair.second;
if (diags(funcRes)) {
clazz.average(funcRes);
clazz.percentageDist(funcRes);
clazz.usedPrivsDistribution(funcRes);
hostClassesWithZeroPriv(funcRes);
befriendingClassesAllFriendsMC.classFunctionInstance(funcRes);
incorrectFriendClass(funcRes);
} else {
llvm::outs() << "WRONG MEASURE here:\n" << funcRes.friendDeclLocStr
<< "\n";
print(funcResPair);
llvm::outs() << "SKIPPING ENTRY FROM STATISTICS\n\n";
}
}
if (PrintIncorrectFriendClasses && incorrectFriendClass.result) {
printIncorrectFriendClass(classSpecs.second);
}
}
}
}
void printHostClassesWithZeroPrivate() {
auto befrClassWithAllMC = befriendingClassesAllFriendsMC.getResult();
for (const auto &cip : hostClassesWithZeroPriv.classes) {
// This is not a class with just MC friend functions
if (befrClassWithAllMC.count(cip) == 0) {
llvm::outs()
<< "Warning: befriending class with zero private entities:\n";
print(*cip);
}
}
}
void printIncorrectFriendClass(const Result::ClassResult &classResult) {
llvm::outs() << "Warning: possibly incorrect friend class:\n";
llvm::outs() << "diagName: " << classResult.diagName << "\n";
llvm::outs() << "defLoc: " << classResult.defLocStr << "\n";
llvm::outs() << "friendDeclLoc: " << classResult.friendDeclLocStr << "\n";
llvm::outs()
<< "============================================================"
"================\n";
}
void conclusion() {
llvm::outs() << "########## Friend FUNCTIONS ##########"
<< "\n";
llvm::outs() << "Number of available friend function definitions: "
<< func.average.num << "\n";
llvm::outs() << "Number of friend function declarations with zero priv "
"entity declared in host class: "
<< func.average.numZeroDenom
<< "\n";
double sum = func.average.get();
llvm::outs() << "Average usage of priv entities (vars, funcs, types) in "
"friend functions: "
<< to_percentage(sum)
<< "\n";
llvm::outs()
<< "Friend functions private usage (in percentage) distribution:"
<< "\n";
llvm::outs() << func.percentageDist.dist;
llvm::outs() << "Friend functions private usage (by piece) distribution:"
<< "\n";
llvm::outs() << func.usedPrivsDistribution.dist;
llvm::outs() << "Number of Meyers candidates: "
<< func.meyersCandidate.count << "\n";
llvm::outs() << "\n";
llvm::outs() << "########## Friend CLASSES ##########"
<< "\n";
llvm::outs()
<< "Number of available function definitions in friend classes: "
<< clazz.average.num << "\n";
llvm::outs()
<< "Number of function declarations of friend classes with zero priv "
"entity declared in host class: "
<< clazz.average.numZeroDenom
<< "\n";
sum = clazz.average.get();
llvm::outs() << "Average usage of priv entities (vars, funcs, types) in "
"friend classes: "
<< to_percentage(sum)
<< "\n";
llvm::outs() << R"("Indirect friend")"
" functions private usage (in percentage) distribution:"
<< "\n";
llvm::outs() << clazz.percentageDist.dist;
llvm::outs() << R"("Indirect friend")"
" functions private usage (by piece) distribution:"
<< "\n";
llvm::outs() << clazz.usedPrivsDistribution.dist;
}
};
int main(int argc, const char **argv) {
CommonOptionsParser OptionsParser(argc, argv, MyToolCategory);
auto files = OptionsParser.getSourcePathList();
if (UseCompilationDbFiles.getNumOccurrences() > 0) {
files = OptionsParser.getCompilations().getAllFiles();
}
ClangTool Tool(OptionsParser.getCompilations(), files);
FriendHandler Handler;
MatchFinder Finder;
Finder.addMatcher(FriendMatcher, &Handler);
ProgressIndicator progressIndicator{files.size()};
auto ret =
Tool.run(newFrontendActionFactory(&Finder, &progressIndicator).get());
llvm::outs() << "\n";
llvm::outs() << "Number of processed friend function declarations: "
<< Handler.getResult().friendFuncDeclCount << "\n";
llvm::outs() << "Number of processed friend class declarations: "
<< Handler.getResult().friendClassDeclCount << "\n";
llvm::outs() << "\n";
DataTraversal traversal{Handler.getResult()};
traversal();
return ret;
}