-
Notifications
You must be signed in to change notification settings - Fork 3
/
HPCPatternInstrASTTraversal.cpp
522 lines (434 loc) · 16.6 KB
/
HPCPatternInstrASTTraversal.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
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
#include "HPCPatternInstrASTTraversal.h"
#include "Debug.h"
#include "HPCParallelPattern.h"
#include <string>
#include <exception>
#include <iostream>
#include "clang/AST/RawCommentList.h"
#include "llvm/ADT/StringRef.h"
#include "clang/Basic/SourceManager.h"
#include "clang/Basic/SourceLocation.h"
#include <string>
#include "llvm/ADT/SmallVector.h"
#include "HPCRunningStats.h"
#ifndef HPCERROR_H
#include "HPCError.h"
#endif
/**
* @brief If a function declaration is encountered, look up the corresponding database entry.
* We set helper variables in the PatternBeginHandler and PatternEndHandler to the FunctionNode for correct parent-child-relations.
*
* @param Decl The clang object encountered by the visitor.
*
* @return Always true to signal that the tree traversal should be continued.
**/
bool HPCPatternInstrVisitor::VisitFunctionDecl(clang::FunctionDecl *Decl)
{
clang::SourceManager& SourceMan = Context->getSourceManager();
if(SourceMan.isInMainFile(Decl->getBeginLoc()))
{
CurrentFn = Decl;
clang::SourceLocation beginLoc = Decl->getBeginLoc();
clang::SourceManager& SourceMan = Context->getSourceManager();
clang::FullSourceLoc SourceLoc(beginLoc, SourceMan);
CallTreeNode* Node;
if ((CurrentFnEntry = PatternGraph::GetInstance()->GetFunctionNode(Decl)) == NULL)
{
PatternGraph::GetInstance()->RegisterFunction(Decl);
CurrentFnEntry = PatternGraph::GetInstance()->GetFunctionNode(Decl);
}
if(Decl->isMain()){
Node = ClTre->registerNode(Root, CurrentFnEntry, LastNodeType, GetTopPatternStack(), CurrentFnEntry);
ClTre->setRootNode(Node);
}
else
Node = ClTre->registerNode(Function_Decl, CurrentFnEntry, LastNodeType, GetTopPatternStack(), CurrentFnEntry);
Node->SetLineNumber(SourceLoc.getLineNumber());
#ifdef LOCDEBUG
std::cout << "setted LineNumber of: "<< *Node->GetID()<<" to "<< SourceLoc.getLineNumber()<<" verification: "<<Node->getLineNumber()<< '\n';
#endif
#ifdef PRINT_DEBUG
std::cout << CurrentFnEntry->GetFnName() << " (" << CurrentFnEntry->GetHash() << ")" << std::endl;
#endif
PatternBeginHandler.SetCurrentFnEntry(CurrentFnEntry);
PatternEndHandler.SetCurrentFnEntry(CurrentFnEntry);
}
LastNodeType = Function_Decl;
return true;
}
/**
* @brief When we encounter a call expression, we look up the declaration of the function called.
* If it is one of our instrumentation functions, we extract the information from the string argument with ASTMatchers.
* A PatternCodeRegion object is created if this is the start of a region or the current region is closed.
* For a non-instrumentation function, the function is added to the pattern which surraunds it as a child.
* If there is no pattern, the function is a direct child of the calling function.
*
* @param CallExpr The clang object containing information about the call expression.
*
* @return True to signal continuing the traversal.
**/
bool HPCPatternInstrVisitor::VisitCallExpr(clang::CallExpr *CallExpr)
{
clang::SourceManager& SourceMan = Context->getSourceManager();
/*If Clause is used to make shure that only the code in compile_commands.json is traversed
and no used libraries*/
if(SourceMan.isInMainFile(CallExpr->getBeginLoc()))
{
if (!CallExpr->getBuiltinCallee() && CallExpr->getDirectCallee() && !CallExpr->getDirectCallee()->isInStdNamespace())
{
clang::FunctionDecl* Callee = CallExpr->getDirectCallee();
#ifdef PRINT_DEBUG
std::cout << Callee->getNameInfo().getName().getAsString() << std::endl;
#endif
std::string FnName = Callee->getNameInfo().getName().getAsString();
// If the CallExpr is a pattern-begin expression
if (!FnName.compare(PATTERN_BEGIN_CXX_FNNAME) || !FnName.compare(PATTERN_BEGIN_C_FNNAME))
{
/*Delivers the children of the current node*/
clang::Expr** Args = CallExpr->getArgs();
#ifdef PRINT_DEBUG
Args[0]->dump();
#endif
/*calls all registered callbacks on all matches on the given node.
this call is pretty stong. It creates the patternCodeRegion and if there is no mathing PatternOccurrence it
is creating one. Also are the Child parent relations set with this call*/
//PatternBeginFinder is a clang::ast_matchers::MatchFinder which uses a HPCPatternBeginInstrHandler to set everything up and the Pattern is also registered in the patternStack.
PatternCodeRegion* PatBeforethisPat = PatternBeginHandler.GetLastPattern();
PatternBeginFinder.match(*Args[0], *Context);
PatternCodeRegion* PatternCodeReg = PatternBeginHandler.GetLastPattern();
/* Store this PatternCodeRegion Begin in the CallTree (ClTre)*/
CallTreeNode* BeginNode = ClTre->registerNode(Pattern_Begin, PatternCodeReg, LastNodeType, PatBeforethisPat, CurrentFnEntry);
/* Get the location of the fn call which denotes the beginning of this pattern */
clang::SourceLocation LocStart = CallExpr->getBeginLoc();
clang::FullSourceLoc SourceLoc(LocStart, SourceMan);
BeginNode->SetLineNumber(SourceLoc.getLineNumber());
#ifdef LOCDEBUG
std::cout << "setted LineNumber of: "<< *BeginNode->GetID()<<" to "<< SourceLoc.getLineNumber()<<" verification: "<<BeginNode->getLineNumber()<< '\n';
#endif
PatternCodeReg->SetFirstLine(SourceLoc.getLineNumber());
PatternCodeReg->SetStartSourceLoc(LocStart);
PatternCodeReg->isInMain = SourceMan.isInMainFile(LocStart);
LastNodeType = Pattern_Begin;
}
else if (!FnName.compare(PATTERN_END_CXX_FNNAME) || !FnName.compare(PATTERN_END_C_FNNAME))
{
clang::Expr** Args = CallExpr->getArgs();
#ifdef PRINT_DEBUG
std::cout << "Degub dump of Args before matching" << '\n';
Args[0]->dump();
#endif
PatternCodeRegion* PatternCodeReg;
try{
PatternEndFinder.match(*Args[0], *Context);
PatternCodeReg = PatternEndHandler.GetLastPattern();
}
catch(TooManyEndsException& e){
e.what();
throw TerminateEarlyException();
}
#ifdef PRINT_DEBUG
std::cout << "Degub dump of Args after matching" << '\n';
Args[0]->dump();
#endif
/* Get the location of the fn call which denotes the end of this pattern */
clang::SourceManager& SourceMan = Context->getSourceManager();
clang::SourceLocation LocEnd = CallExpr->getEndLoc();
clang::FullSourceLoc SourceLoc(LocEnd, SourceMan);
CallTreeNode* EndNode = ClTre->registerEndNode(Pattern_End, PatternEndHandler.GetLastPatternID(), LastNodeType, PatternCodeReg, CurrentFnEntry);
EndNode->SetLineNumber(SourceLoc.getLineNumber());
#ifdef LOCDEBUG
std::cout << "setted LineNumber of: "<< *EndNode->GetID()<<" to "<< SourceLoc.getLineNumber()<<" verification: "<<EndNode->getLineNumber()<< '\n';
#endif
}
// If no: search the called function for patterns
else
{
/* Look up the database entry for the function in which the current callExpr is within*/
FunctionNode* Func;
/*if the function is not registered register*/
if ((Func = PatternGraph::GetInstance()->GetFunctionNode(Callee)) == NULL)
{
PatternGraph::GetInstance()->RegisterFunction(Callee);
}
Func = PatternGraph::GetInstance()->GetFunctionNode(Callee);
#ifdef PRINT_DEBUG
std::cout << Func->GetFnName() << " (" << Func->GetHash() << ")" << std::endl;
#endif
/* Store this function call in the CallTree (ClTre)*/
CallTreeNode* FuncNode = ClTre->registerNode(Function, Func, LastNodeType, GetTopPatternStack(), CurrentFnEntry);
clang::SourceManager& SourceMan = Context->getSourceManager();
clang::SourceLocation LocStart = CallExpr->getBeginLoc();
clang::FullSourceLoc SourceLoc(LocStart, SourceMan);
FuncNode->SetLineNumber(SourceLoc.getLineNumber());
PatternCodeRegion* Top;
/* if we are within a Pattern -> register this Functon as a child of the pattern etc. */
if ((Top = GetTopPatternStack()) != NULL)
{
Top->AddChild(Func);
Func->AddParent(Top);
Func->AddPatternParent(Top);
#ifdef DEBUG_J
std::cout << Func->GetFnName()<< " hat als PatternParent: " << Top->GetID()<< '\n';
#endif
}
else
{/*if not register this function as a child for the function in which we currenty are
(because we are always inside a function this is possible)
*/
CurrentFnEntry->AddChild(Func);
Func->AddParent(CurrentFnEntry);
/*if the parent of this function has a PatternParent, the function inherits it to its child (Func) */
if(!CurrentFnEntry->HasNoPatternParents()){
//function has PatternParents too
Func->AddPatternParents(CurrentFnEntry->GetPatternParents());
/*If the function has PatternParents AND PatternChildre, we register the the GetPatternChildren
as Children of the PatternParents vice versa*/
if(!Func->HasNoPatternChildren()){
Func->registerPatChildrenToPatParents();
}
}
}
}
}
}
return true;
}
HPCPatternInstrVisitor::HPCPatternInstrVisitor (clang::ASTContext* Context) : Context(Context)
{
using namespace clang::ast_matchers;
StatementMatcher StringArgumentMatcher = hasDescendant(stringLiteral().bind("patternstr"));
PatternBeginFinder.addMatcher(StringArgumentMatcher, &PatternBeginHandler);
PatternEndFinder.addMatcher(StringArgumentMatcher, &PatternEndHandler);
}
Halstead* currentHlst;
HalsteadVisitor::HalsteadVisitor(clang::ASTContext *Context) : Context(Context)
{
actHalstead = getActualHalstead();
}
/* Consumer function implementations
*/
void HPCPatternInstrConsumer::HandleTranslationUnit(clang::ASTContext &Context)
{
/* Traverse the AST for comments and parse them */
DEBUG_MESSAGE("Using Visitor to traverse from top translation declaration unit");
Visitor.TraverseDecl(Context.getTranslationUnitDecl());
}
bool HalsteadVisitor::VisitBinaryOperator(clang::BinaryOperator *BinarOp){
std::vector<HPCParallelPattern*> isInPatterns;
IsStmtInAPatt(BinarOp, &isInPatterns);
if(isInPatterns.empty()){
return true;
}
else{
for(HPCParallelPattern* Pat : isInPatterns){
Pat->incrementNumOfOperators();
}
}
return true;
}
bool HalsteadVisitor::VisitDeclStmt(clang::DeclStmt *DclStmt){
std::vector<HPCParallelPattern*> isInPatterns;
IsStmtInAPatt(DclStmt, &isInPatterns);
if(isInPatterns.empty()){
return true;
}
else{
for(HPCParallelPattern* Pat : isInPatterns){
Pat->incrementNumOfOperators();
}
}
return true;
}
bool HalsteadVisitor::VisitCallExpr(clang::CallExpr *CallExpr){
std::vector<HPCParallelPattern*> isInPatterns;
IsStmtInAPatt(CallExpr, &isInPatterns);
clang::Decl* CalleeDecl = CallExpr->getCalleeDecl();
clang::SourceManager& SurMan = Context->getSourceManager();
if(SurMan.isInMainFile(CallExpr->getExprLoc())){
/* CalleeDecl->dump();
std::cout << "STATEMENT DUMP: " << '\n';
clang::Stmt* CalleeStmt = CalleeDecl->getBody();
CalleeStmt->dump();*/
}
if(isInPatterns.empty()){
return true;
}
else{
for(HPCParallelPattern* Pat : isInPatterns){
Pat->incrementNumOfOperators();
}
}
return true;
}
bool HalsteadVisitor::VisitUnaryOperator(clang::UnaryOperator *UnaryOp){
std::vector<HPCParallelPattern*> isInPatterns;
IsStmtInAPatt(UnaryOp, &isInPatterns);
if(isInPatterns.empty()){
return true;
}
else{
for(HPCParallelPattern* Pat : isInPatterns){
Pat->incrementNumOfOperators();
}
}
return true;
}
bool HalsteadVisitor::VisitCompoundAssignOperator(clang::CompoundAssignOperator *CompAsOp){
std::vector<HPCParallelPattern*> isInPatterns;
IsStmtInAPatt(CompAsOp, &isInPatterns);
if(isInPatterns.empty()){
return true;
}
else{
for(HPCParallelPattern* Pat : isInPatterns){
Pat->incrementNumOfOperators();
}
}
return true;
}
bool HalsteadVisitor::VisitMemberExpr(clang::MemberExpr *MemExpr){
std::vector<HPCParallelPattern*> isInPatterns;
IsStmtInAPatt(MemExpr, &isInPatterns);
if(isInPatterns.empty()){
return true;
}
else{
for(HPCParallelPattern* Pat : isInPatterns){
Pat->incrementNumOfOperators();
}
}
return true;
}
bool HalsteadVisitor::VisitStringLiteral(clang::StringLiteral *StrgLit){
std::vector<HPCParallelPattern*> isInPatterns;
IsStmtInAPatt(StrgLit, &isInPatterns);
if(isInPatterns.empty()){
return true;
}
else{
for(HPCParallelPattern* Pat : isInPatterns){
Pat->incrementNumOfOperators();
}
}
return true;
}
bool HalsteadVisitor::VisitCharacterLiteral(clang::CharacterLiteral *CharLit){
std::vector<HPCParallelPattern*> isInPatterns;
IsStmtInAPatt(CharLit, &isInPatterns);
if(isInPatterns.empty()){
return true;
}
else{
for(HPCParallelPattern* Pat : isInPatterns){
Pat->incrementNumOfOperators();
}
}
return true;
}
/*bisher werden nur die TypeQualifiers gezählt. will man noch einmal die Decl selbst mit zählen muss man plus 1 rechnen*/
bool HalsteadVisitor::VisitVarDecl(clang::VarDecl *VrDcl){
std::vector<HPCParallelPattern*> isInPatterns;
IsDeclInAPatt(VrDcl, &isInPatterns);
if(isInPatterns.empty()){
return true;
}
else{
int numTypeQual = HalsteadVisitor::countQual(VrDcl);
/*
countQual return the number of TypeQualifiers excluding the default qualifier */
for(HPCParallelPattern* Pat : isInPatterns){
for (size_t i = 0; i < numTypeQual; i++) {
Pat->incrementNumOfOperators();
}
/*is the variable mot only declared but also initialized, than we have one operator more
in the patterns */
if(VrDcl->hasInit()){
Pat->incrementNumOfOperators();
}
/*count the storage class specifiers*/
if(VrDcl->getStorageClass()!= clang::StorageClass::SC_None){
Pat->incrementNumOfOperators();
}
}
}
return true;
}
bool HalsteadVisitor::VisitFunctionDecl(clang::FunctionDecl *FctDecl){
std::vector<HPCParallelPattern*> isInPatterns;
IsDeclInAPatt(FctDecl, &isInPatterns);
if(isInPatterns.empty()){
return true;
}
else{
for(HPCParallelPattern* Pat : isInPatterns){
if(FctDecl->getStorageClass() != clang::StorageClass::SC_None ) Pat->incrementNumOfOperators();
}
}
return true;
}
void HalsteadVisitor::IsStmtInAPatt(clang::Stmt *Stm, std::vector<HPCParallelPattern*> *isInPatterns){
std::vector<PatternOccurrence*> WorkOccStackForHalstead(OccStackForHalstead.begin(), OccStackForHalstead.end()) ;
// WorkOccStackForHalstead is correctly initialized
clang::SourceManager& SourceMan = Context->getSourceManager();
for (int i = 0; i < WorkOccStackForHalstead.size(); i++){
PatternOccurrence* PatOcc = WorkOccStackForHalstead[i];
std::vector<PatternCodeRegion*> CodeRegions = PatOcc->GetCodeRegions();
getActualHalstead()->insertPattern(PatOcc->GetPattern());
for(int i = 0; i < CodeRegions.size(); i++){
PatternCodeRegion* CodeReg = CodeRegions[i];
bool ExprAfterBegStmt = SourceMan.isPointWithin (Stm->getEndLoc(),CodeReg->GetStartLoc(), CodeReg->GetEndLoc());
if(ExprAfterBegStmt){
isInPatterns->push_back(PatOcc->GetPattern());
}
}
}
}
void HalsteadVisitor::IsDeclInAPatt(clang::Decl *Dcl, std::vector<HPCParallelPattern*> *isInPatterns){
std::vector<PatternOccurrence*> WorkOccStackForHalstead(OccStackForHalstead.begin(), OccStackForHalstead.end()) ;
clang::SourceManager& SourceMan = Context->getSourceManager();
for (int i = 0; i < WorkOccStackForHalstead.size(); i++){
PatternOccurrence* PatOcc = WorkOccStackForHalstead[i];
std::vector<PatternCodeRegion*> CodeRegions = PatOcc->GetCodeRegions();
getActualHalstead()->insertPattern(PatOcc->GetPattern());
for(int i = 0; i < CodeRegions.size(); i++){
PatternCodeRegion* CodeReg = CodeRegions[i];
bool ExprAfterBegStmt = SourceMan.isPointWithin (Dcl->getLocation(),CodeReg->GetStartLoc(), CodeReg->GetEndLoc());
if(ExprAfterBegStmt){
isInPatterns->push_back(PatOcc->GetPattern());
}
}
}
}
int HalsteadVisitor::countQual(clang::VarDecl* VDecl){
llvm::SmallVector<clang::Attr*, 4> Attributes;
clang::QualType type = VDecl->getType();
std::string typ = type.getAsString();
int wc = 0;
for(int i =0 ; i < typ.length(); i++){
char actchar = typ[i];
if(!((actchar <= 'z' && actchar >= 'a') || (actchar <= 'Z' && actchar >= 'A') || (actchar == '_'))){
if(actchar == ' '){
/*we only count one more if the next character is a letter or _*/
if( ((i+1) < typ.length()) && ( (typ[i+1] <= 'z' && typ[i+1] >= 'a') || (typ[i+1] <= 'Z' && typ[i+1] >= 'A') || (typ[i+1] == '_') ) ){
wc++;
}
else{
return wc;
}
}
}
}
return wc;
}
/*
* Frontend action function implementations
*/
std::unique_ptr<clang::ASTConsumer> HPCPatternInstrAction::CreateASTConsumer(clang::CompilerInstance &Compiler, llvm::StringRef InFile)
{
DEBUG_MESSAGE("Creating consumer object!")
return std::unique_ptr<clang::ASTConsumer>(new HPCPatternInstrConsumer(&Compiler.getASTContext()));
}
std::unique_ptr<clang::ASTConsumer> HalsteadClassAction::CreateASTConsumer(clang::CompilerInstance &Compiler, llvm::StringRef InFile)
{
return std::unique_ptr<clang::ASTConsumer>(new HalsteadConsumer(&Compiler.getASTContext()));
}