-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSourceAnalyzer.cpp
306 lines (263 loc) · 7.89 KB
/
SourceAnalyzer.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
294
295
296
297
298
299
300
301
302
303
304
305
306
//===- SourceAnalyzer.cpp ---------------------------------------------===//
//
// Source Analyzer
//
// This file is distributed under the MIT License. See LICENSE for details.
//
//===----------------------------------------------------------------------===//
//
// clang plugin that classifies nodes in the input file.
//
//===----------------------------------------------------------------------===//
#include "clang/Frontend/FrontendPluginRegistry.h"
#include "clang/AST/AST.h"
#include "clang/AST/RecursiveASTVisitor.h"
#include "clang/AST/ASTConsumer.h"
#include "clang/Frontend/CompilerInstance.h"
#include "llvm/Support/raw_ostream.h"
#include "llvm/Support/YAMLTraits.h"
using namespace clang;
using llvm::yaml::ScalarEnumerationTraits;
using llvm::yaml::MappingTraits;
using llvm::yaml::IO;
using llvm::yaml::Output;
typedef struct {
int numFunctions;
int numForStmts;
int numDoStmts;
int numGotoStmts;
int numSwitchStmts;
int numWhileStmts;
int numBreakStmts;
int numContinueStmts;
int numIndirectGotoStmts;
int numIfStmts;
int numDivOps;
int numMultOps;
int numAddSubOps;
int numCompOps;
int numShiftOps;
int numAssgnOps;
int numPtrOps;
int numLogOps;
int numArray;
int numStruct;
int numScalar;
int numAggregate;
int numUnion;
} Result;
namespace llvm {
namespace yaml {
template <>
struct MappingTraits<Result> {
static void mapping(IO &io, Result &R) {
io.mapRequired("Functions", R.numFunctions);
io.mapRequired("ForStmts", R.numForStmts);
io.mapRequired("IfStmts", R.numIfStmts);
io.mapRequired("DoStmts", R.numDoStmts);
io.mapRequired("GotoStmts", R.numGotoStmts);
io.mapRequired("SwitchStmts", R.numSwitchStmts);
io.mapRequired("WhileStmts",R.numWhileStmts);
io.mapRequired("BreakStmts",R.numBreakStmts);
io.mapRequired("ContinueStmts", R.numContinueStmts);
io.mapRequired("IndirectGotoStmts",R.numIndirectGotoStmts);
io.mapRequired("DivOps", R.numDivOps);
io.mapRequired("MultOps", R.numMultOps);
io.mapRequired("AddSubOps",R.numAddSubOps);
io.mapRequired("CompOps",R.numCompOps);
io.mapRequired("ShiftOps",R.numShiftOps );
io.mapRequired("AssgnOps", R.numAssgnOps);
io.mapRequired("PtrOps", R.numPtrOps);
io.mapRequired("LogOps",R.numLogOps);
io.mapRequired("ScalarVars",R.numScalar);
//io.mapRequired("ArrayVars", R.numArray);
//io.mapRequired("StructVars",R.numStruct);
//io.mapRequired("UnionVars", R.numUnion);
io.mapRequired("AggreVars", R.numAggregate);
}
};
}
}
//void dumpResult(const Result &R) {
// Output yout(llvm::outs());
// yout << R;
//}
namespace {
Result R = {0};
class MyASTConsumer : public ASTConsumer,
public RecursiveASTVisitor<MyASTConsumer>
{
public:
void HandleTranslationUnit (clang::ASTContext &ctx);
bool VisitFunctionDecl (clang::FunctionDecl* decl);
//bool VisitParmVarDecl (clang::ParmVarDecl* decl);
bool VisitVarDecl (clang::VarDecl* decl);
//bool VisitNamedDecl (clang::NamedDecl* decl);
bool Visit (clang::TypeLoc TL);
bool VisitStmt (clang::Stmt* stmt);
};
bool MyASTConsumer::VisitVarDecl (clang::VarDecl* decl)
{
TypeLoc TL = decl->getTypeSourceInfo()->getTypeLoc();
const Type *T = TL.getTypePtr();
if (T->isArrayType())
++R.numArray;
if(T->isStructureType())
{
++R.numStruct;
//T->dump();
}
if(T->isUnionType())
++R.numUnion;
if(T->isScalarType())
++R.numScalar;
if (T->isAggregateType())
{
//T->dump();
++R.numAggregate;
}
return true;
}
void MyASTConsumer::HandleTranslationUnit (clang::ASTContext &ctx)
{
//fprintf(stderr, "Welcome to MyASTConsumer!\n");
llvm::errs() << "Welcome to MyASTConsumer!\n";
TraverseDecl(ctx.getTranslationUnitDecl());
Output yout(llvm::errs());
yout << R;
//ctx.PrintStats();
llvm::errs() << "Goodbye!\n";
}
bool MyASTConsumer::VisitStmt (clang::Stmt* stmt)
{
if(ForStmt * s = dyn_cast<ForStmt>(stmt) )
{
++R.numForStmts;
//s->dumpColor();
s = NULL;
}
if(DoStmt *s = dyn_cast<DoStmt>(stmt))
{
++R.numDoStmts;
s = NULL;
}
if (GotoStmt *s = dyn_cast<GotoStmt>(stmt))
{
++R.numGotoStmts;
s = NULL;
}
if (SwitchStmt *s = dyn_cast<SwitchStmt>(stmt))
{
++R.numSwitchStmts;
s = NULL;
}
if(WhileStmt *s = dyn_cast<WhileStmt>(stmt))
{
++R.numWhileStmts;
s = NULL;
}
if(IfStmt *s = dyn_cast<IfStmt>(stmt))
{
++R.numIfStmts;
s = NULL;
}
if(BreakStmt *s = dyn_cast<BreakStmt>(stmt))
{
++R.numBreakStmts;
s = NULL;
}
if(ContinueStmt *s = dyn_cast<ContinueStmt>(stmt))
{
++R.numContinueStmts;
s = NULL;
}
if(IndirectGotoStmt *s = dyn_cast<IndirectGotoStmt>(stmt))
{
++R.numIndirectGotoStmts;
//s->dumpColor();
s = NULL;
}
if (BinaryOperator * binop = dyn_cast<BinaryOperator>(stmt))
{
if(binop->isAdditiveOp())
++R.numAddSubOps;
else if(binop->isAssignmentOp() )
{
++R.numAssgnOps;
//binop->dumpColor();
}
else if(binop->isLogicalOp() || binop->isBitwiseOp())
++R.numLogOps;
else if(binop->isComparisonOp() || binop->isRelationalOp() || binop->isEqualityOp())
++R.numCompOps;
else if(binop->isMultiplicativeOp())
{
if(binop->getOpcode() != BO_Mul)
{
++R.numDivOps;
//binop->dumpColor();
}
else
++R.numMultOps;
}
else if(binop->isPtrMemOp())
++R.numPtrOps;
else if (binop->isShiftOp())
++R.numShiftOps;
if(binop->isCompoundAssignmentOp())
{
++R.numAssgnOps;
if(binop->getOpcode() == BO_MulAssign)
++R.numMultOps;
else if (binop->getOpcode() == BO_DivAssign || binop->getOpcode() == BO_RemAssign)
++R.numDivOps;
else if(binop->getOpcode() == BO_OrAssign || binop->getOpcode() == BO_AndAssign
|| binop->getOpcode() == BO_OrAssign || binop->getOpcode() == BO_XorAssign )
++R.numLogOps;
else if (binop->getOpcode() == BO_ShrAssign || binop->getOpcode() == BO_ShlAssign)
++R.numShiftOps;
else
++R.numAddSubOps;
}
}
if(UnaryOperator * uniOps = dyn_cast<UnaryOperator>(stmt))
{
if(uniOps->isArithmeticOp())
++R.numAddSubOps;
}
return true;
}
bool MyASTConsumer::VisitFunctionDecl (clang::FunctionDecl* decl)
{
++R.numFunctions;
return true;
}
class MyASTAction : public PluginASTAction {
protected:
ASTConsumer *CreateASTConsumer(CompilerInstance &CI, llvm::StringRef) {
return ( new MyASTConsumer());
}
bool ParseArgs(const CompilerInstance &CI,
const std::vector<std::string>& args) {
for (unsigned i = 0, e = args.size(); i != e; ++i) {
llvm::errs() << "SourceAnalyzer arg = " << args[i] << "\n";
// Example error handling.
if (args[i] == "-an-error") {
DiagnosticsEngine &D = CI.getDiagnostics();
unsigned DiagID = D.getCustomDiagID(
DiagnosticsEngine::Error, "invalid argument '" + args[i] + "'");
D.Report(DiagID);
return false;
}
}
if (args.size() && args[0] == "help")
PrintHelp(llvm::errs());
return true;
}
void PrintHelp(llvm::raw_ostream& ros) {
ros << "Help for SourceAnalyzer plugin goes here\n";
}
};
}
static FrontendPluginRegistry::Add<MyASTAction>
X("src-anlz", "source code analyzer");