-
Notifications
You must be signed in to change notification settings - Fork 7
/
visit.cpp
2129 lines (1938 loc) · 68.9 KB
/
visit.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
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include "main.hpp"
#include "dbjson.hpp"
#include "compat.h"
#include "clang/AST/RecordLayout.h"
// Decl visitors
bool DbJSONClassVisitor::TraverseDecl(Decl *D) {
bool TraverseResult = RecursiveASTVisitor<DbJSONClassVisitor>::TraverseDecl(D);
if (D) {
switch (D->getKind()) {
case Decl::Function:
case Decl::CXXMethod:
case Decl::CXXDestructor:
case Decl::CXXConstructor:
case Decl::CXXConversion:
case Decl::CXXDeductionGuide:
{
if (!VisitFunctionDeclComplete(static_cast<FunctionDecl*>(D))) return false;
break;
}
case Decl::Var:
{
if (!VisitVarDeclComplete(static_cast<VarDecl*>(D))) return false;
break;
}
case Decl::Record:
{
if (!VisitRecordDeclComplete(static_cast<RecordDecl*>(D))) return false;
}
}
}
return TraverseResult;
}
// Track declaration context in some cases
bool DbJSONClassVisitor::VisitDecl(Decl *D) {
switch (D->getKind()) {
case Decl::Function:
case Decl::CXXMethod:
case Decl::CXXDestructor:
case Decl::CXXConstructor:
case Decl::CXXConversion:
case Decl::CXXDeductionGuide:
{
FunctionDecl* FD = static_cast<FunctionDecl*>(D);
return VisitFunctionDeclStart(FD);
}
case Decl::Var:
{
VarDecl* VD = static_cast<VarDecl*>(D);
return VisitVarDeclStart(VD);
}
case Decl::Record:
{
RecordDecl* RD = static_cast<RecordDecl*>(D);
return VisitRecordDeclStart(RD);
}
}
return true;
}
// Variables
bool DbJSONClassVisitor::VisitVarDeclStart(const VarDecl *D) {
fopsVarDecl.push_back(D);
if (D->isDefinedOutsideFunctionOrMethod() && !D->isStaticDataMember()
&& D->hasGlobalStorage() && !D->isStaticLocal()) {
// We're are taking care of global variable
lastGlobalVarDecl = D;
assert(D->getNameAsString().size() && "unnamed variable");
}
inVarDecl.push_back(true);
return true;
}
bool DbJSONClassVisitor::VisitVarDeclComplete(const VarDecl *D) {
fopsVarDecl.pop_back();
lastGlobalVarDecl = nullptr;
inVarDecl.pop_back();
return true;
}
unsigned long NumInitializedElements(const Expr* E) {
unsigned long init_count = 0;
if (E->getStmtClass()==Stmt::InitListExprClass) {
const InitListExpr* ILE = static_cast<const InitListExpr*>(E);
for (unsigned i=0; i<ILE->getNumInits(); ++i) {
const Expr* IE = ILE->getInit(i);
init_count+=NumInitializedElements(IE);
}
}
else if (E->getStmtClass()==Stmt::ImplicitValueInitExprClass) {
}
else {
init_count+=1;
}
return init_count;
}
bool DbJSONClassVisitor::VisitVarDecl(const VarDecl *D) {
DBG(DEBUG_NOTICE, llvm::outs() << "@notice VisitVarDecl() [" << D << ":" << D->getKind() <<"]\n"; D->dumpColor(); );
if (opts.debugME) {
llvm::outs() << "@VisitVarDecl()\n";
D->dumpColor();
}
if ((D->getKind() == Decl::Kind::Var)&&(D->getInit())) {
DbJSONClassVisitor::DREMap_t DREMap;
std::vector<CStyleCastOrType> castVec;
const Expr* E = stripCastsEx(D->getInit(),castVec);
bool isAddress = false;
if (castVec.size()>0) {
if (castVec.front().getFinalType()->getTypeClass()==Type::Pointer) {
isAddress = true;
}
}
// Check if there's implicit (or explicit) cast from the initializer
// Handle void* as a special case and allow casting in opposite way to the initializer
// Also ignore implicit casts for string literals
QualType castType;
if (getFirstCast(castVec)) {
castType = getFirstCast(castVec)->getType();
}
else {
if (D->getInit()->getStmtClass()==Stmt::ImplicitCastExprClass) {
const ImplicitCastExpr* ICE = static_cast<const ImplicitCastExpr*>(D->getInit());
if (ICE->getSubExpr()->getType()!=D->getType()) {
if (isPtrToVoid(D->getType())) {
castType = ICE->getSubExpr()->getType();
}
else {
if (E->getStmtClass()!=Stmt::StringLiteralClass) {
castType = D->getType();
}
}
}
}
}
if (!castType.isNull()) {
noticeTypeClass(castType);
}
// Check if init can be evaluated as a constant expression
Expr::EvalResult Res;
if((!E->isValueDependent()) && E->isEvaluatable(Context) && tryEvaluateIntegerConstantExpr(E,Res)) {
int64_t i = Res.Val.getInt().extOrTrunc(63).getExtValue();
ValueDeclOrCallExprOrAddressOrMEOrUnaryOrAS v;
CStyleCastOrType valuecast;
if (!castType.isNull()) {
valuecast.setType(castType);
}
if (isAddress) {
v.setAddress(i,valuecast);
}
else {
v.setInteger(i,valuecast);
}
vMCtuple_t vMCtuple;
v.setPrimaryFlag(false);
DREMap_add(DREMap,v,vMCtuple);
}
else {
DbJSONClassVisitor::lookup_cache_t cache;
bool compundStmtSeen = false;
unsigned MECnt = 0;
lookForDeclRefWithMemberExprsInternal(D->getInit(),D->getInit(),DREMap,cache,&compundStmtSeen,
0,&MECnt,0,true,false,false,castType);
}
VarRef_t VR;
VR.VDCAMUAS.setValue(D);
std::vector<VarRef_t> vVR;
vVR.push_back(VR);
unsigned size = DREMap.size();
for (DbJSONClassVisitor::DREMap_t::iterator i = DREMap.begin(); i!=DREMap.end(); ++i) {
VarRef_t iVR;
iVR.VDCAMUAS = (*i).first;
vVR.push_back(iVR);
}
if (!D->isDefinedOutsideFunctionOrMethod()) {
if (lastFunctionDef) {
std::pair<std::set<DereferenceInfo_t>::iterator,bool> rv =
lastFunctionDef->derefList.insert(DereferenceInfo_t(VR,NumInitializedElements(D->getInit()),vVR,"",
getCurrentCSPtr(),DereferenceInit));
const_cast<DbJSONClassVisitor::DereferenceInfo_t*>(&(*rv.first))->addOrd(exprOrd++);
const_cast<DbJSONClassVisitor::DereferenceInfo_t*>(&(*rv.first))->evalExprInner =
[D,this](const DereferenceInfo_t *d){
llvm::raw_string_ostream exprstream(d->Expr);
exprstream << "[" << getAbsoluteLocation(D->getBeginLoc()) << "]: ";
D->print(exprstream);
exprstream.flush();
};
}
}
}
if (opts.cstmt && (!D->isDefinedOutsideFunctionOrMethod())) {
if (lastFunctionDef) {
struct DbJSONClassVisitor::VarInfo_t vi = {0,0,0,0};
if (currentWithinCS()) {
vi.CSPtr = getCurrentCSPtr();
if (hasParentCS()) {
vi.parentCSPtr = getParentCSPtr();
}
}
vi.varId = lastFunctionDef->varId++;
if(D->getKind() == Decl::Kind::Var){
vi.VD = D;
if (opts.debug3) {
llvm::outs() << "VarDecl: " << D->getName().str() << " (" << lastFunctionDef->this_func->getName().str() << ")["
<< lastFunctionDef->csIdMap[vi.CSPtr] << "] " << D->getLocation().printToString(Context.getSourceManager()) << "\n";
}
}
else if(D->getKind() == Decl::Kind::ParmVar){
vi.PVD = cast<const ParmVarDecl>(D);
if (opts.debug3) {
llvm::outs() << "ParmVarDecl: " << D->getName().str() << " (" << lastFunctionDef->this_func->getName().str() << ") "
<< D->getLocation().printToString(Context.getSourceManager()) << "\n";
}
}
else if (D->getKind() == Decl::Kind::Decomposition) {
// Do nothing (for now)
}
else {
if (opts.exit_on_error) {
llvm::outs() << "\nERROR: Unsupported declaration kind: " << D->getDeclKindName() << "\n";
D->dump(llvm::outs());
llvm::outs() << D->getLocation().printToString(Context.getSourceManager()) << "\n";
exit(EXIT_FAILURE);
}
}
lastFunctionDef->varMap.insert(std::pair<const VarDecl*,VarInfo_t>(D,vi));
}
}
if (lastFunctionDef) {
const FunctionDecl* FD = lastFunctionDef->this_func;
DBG(DEBUG_NOTICE, FD->dumpColor(); llvm::errs() << "isMember: " << int(FD->isCXXClassMember()) << "\n"; );
const CXXRecordDecl* RD = 0;
if (FD->isCXXClassMember()) {
const CXXMethodDecl* MD = static_cast<const CXXMethodDecl*>(FD);
RD = MD->getParent();
}
DBG(DEBUG_NOTICE, llvm::errs() << "RD: " << RD << "\n"; if (RD) RD->dumpColor(); );
noticeTypeClass(D->getType());
lastFunctionDef->refTypes.insert(D->getType());
}
if(D->getKind() == Decl::Kind::Var){
if(D->isStaticDataMember()) return true;
if(!D->hasGlobalStorage()) return true;
if(D->isStaticLocal()) return true;
std::string name = D->getNameAsString();
if(!unique_name.insert(name).second) return true;
int linkage = D->isExternallyVisible();
int def_kind = D->hasDefinition();
for( const VarDecl *RD : D->getCanonicalDecl()->redecls()){
if(RD->isThisDeclarationADefinition() == def_kind) {
D=RD;
break;
}
}
switch(def_kind){
case 2:
{
const VarDecl *DD = D->getDefinition();
QualType ST = DD->getTypeSourceInfo() ? DD->getTypeSourceInfo()->getType() : DD->getType();
if (isOwnedTagDeclType(ST)) {
noticeTypeClass(ST);
}
noticeTypeClass(DD->getType());
VarMap.insert({VarForMap(DD),{{},DD}});
VarMap.at(VarForMap(DD)).id.setID(VarNum++);
break;
}
case 1:
{
const VarDecl *TD = D->getActingDefinition();
QualType ST = TD->getTypeSourceInfo() ? TD->getTypeSourceInfo()->getType() : TD->getType();
if (isOwnedTagDeclType(ST)) {
noticeTypeClass(ST);
}
noticeTypeClass(TD->getType());
VarMap.insert({VarForMap(TD),{{},TD}});
VarMap.at(VarForMap(TD)).id.setID(VarNum++);
break;
}
case 0:
{
assert(linkage && "Static variable not defined");
const VarDecl *CD = D->getCanonicalDecl();
QualType ST = CD->getTypeSourceInfo() ? CD->getTypeSourceInfo()->getType() : CD->getType();
if (isOwnedTagDeclType(ST)) {
noticeTypeClass(ST);
}
noticeTypeClass(CD->getType());
VarMap.insert({VarForMap(CD),{{},CD}});
VarMap.at(VarForMap(CD)).id.setID(VarNum++);
break;
}
default: {
llvm::outs() << "WARNING: Invalid def_kind (" << (int)def_kind << ")\n";
}
}
}
return true;
}
// Types
bool DbJSONClassVisitor::VisitRecordDeclStart(const RecordDecl *D) {
recordDeclStack.push(D);
return true;
}
bool DbJSONClassVisitor::VisitRecordDeclComplete(const RecordDecl *D) {
recordDeclStack.pop();
return true;
}
bool DbJSONClassVisitor::VisitRecordDecl(const RecordDecl *D) {
QualType T = Context.getRecordType(D);
DBG(DEBUG_NOTICE, llvm::outs() << "@notice VisitRecordDecl(" << D << ")\n"; D->dump(llvm::outs()); T.dump() );
noticeTypeClass(T);
if (opts.cstmt) {
if (currentWithinCS()) {
if (recordCSMap.find(D)==recordCSMap.end()) {
recordCSMap.insert(std::pair<const RecordDecl*,const CompoundStmt*>(D,getCurrentCSPtr()));
}
}
}
return true;
}
bool DbJSONClassVisitor::VisitEnumDecl(const EnumDecl *D) {
DBG(DEBUG_NOTICE, llvm::outs() << "@notice VisitEnumDecl()\n"; D->dump(llvm::outs()) );
QualType T = Context.getEnumType(D);
noticeTypeClass(T);
return true;
}
bool DbJSONClassVisitor::VisitTypedefDecl(TypedefDecl *D) {
DBG(DEBUG_NOTICE, llvm::outs() << "@notice VisitTypedefDecl()\n"; D->dump(llvm::outs()) );
QualType T = Context.getTypeDeclType(D);
noticeTypeClass(T);
QualType tT = D->getTypeSourceInfo()->getType();
if (tT->getTypeClass()==Type::Elaborated) {
TagDecl *OwnedTagDecl = cast<ElaboratedType>(tT)->getOwnedTagDecl();
if (OwnedTagDecl && OwnedTagDecl->isCompleteDefinition()){
TypedefRecords.insert(OwnedTagDecl);
}
}
return true;
}
// Types - c++ only
bool DbJSONClassVisitor::VisitCXXRecordDecl(const CXXRecordDecl* D) {
return true;
}
bool DbJSONClassVisitor::VisitClassTemplateDecl(const ClassTemplateDecl *D) {
DBG(DEBUG_NOTICE, llvm::outs() << "@notice VisitClassTemplateDecl()\n"; D->dump(llvm::outs()) );
CXXRecordDecl* RD = D->getTemplatedDecl();
if (classTemplateMap.find(RD)==classTemplateMap.end()) {
classTemplateMap.insert(std::pair<CXXRecordDecl*,const ClassTemplateDecl*>(RD,D));
}
QualType T = Context.getRecordType(static_cast<const RecordDecl*>(RD));
noticeTypeClass(T);
bool rv = true;
for (auto i = D->spec_begin(); i!=D->spec_end(); ++i) {
if (!VisitClassTemplateSpecializationDecl(*i)) rv=false;
}
// Notice template parameters
const TemplateParameterList * Params = D->getTemplateParameters();
for (unsigned i = 0, e = Params->size(); i != e; ++i) {
const Decl *Param = Params->getParam(i);
if (auto TTP = dyn_cast<TemplateTypeParmDecl>(Param)) {
noticeTypeClass(Context.getTypeDeclType(TTP));
}
else if (auto NTTP = dyn_cast<NonTypeTemplateParmDecl>(Param)) {
noticeTypeClass(NTTP->getType());
}
else if (auto TTPD = dyn_cast<TemplateTemplateParmDecl>(Param)) {
/* Not supported */
}
}
if (opts.debug) {
std::string _class;
QualType RT = Context.getRecordType(RD);
_class = RT.getAsString();
llvm::outs() << "notice classTemplate: " << _class << RD << "\n";
}
return rv;
}
bool DbJSONClassVisitor::VisitClassTemplatePartialSpecializationDecl(const ClassTemplatePartialSpecializationDecl *D) {
const CXXRecordDecl* RD = static_cast<const CXXRecordDecl*>(D);
DBG(DEBUG_NOTICE, llvm::outs() << "@notice VisitClassTemplatePartialSpecializationDecl(" << D << ")\n";
D->dump(llvm::outs()) );
if (classTemplatePartialSpecializationMap.find(RD)==classTemplatePartialSpecializationMap.end()) {
classTemplatePartialSpecializationMap.insert(std::pair<const CXXRecordDecl*,
const ClassTemplatePartialSpecializationDecl*>(RD,D));
}
QualType T = Context.getRecordType(static_cast<const RecordDecl*>(D));
noticeTypeClass(T);
// Notice template parameters
const TemplateParameterList * Params = D->getTemplateParameters();
for (unsigned i = 0, e = Params->size(); i != e; ++i) {
const Decl *Param = Params->getParam(i);
if (auto TTP = dyn_cast<TemplateTypeParmDecl>(Param)) {
noticeTypeClass(Context.getTypeDeclType(TTP));
}
else if (auto NTTP = dyn_cast<NonTypeTemplateParmDecl>(Param)) {
noticeTypeClass(NTTP->getType());
}
else if (auto TTPD = dyn_cast<TemplateTemplateParmDecl>(Param)) {
/* Not supported */
}
}
if (opts.debug) {
std::string _class;
QualType RT = Context.getRecordType(RD);
_class = RT.getAsString();
std::string templatePars;
llvm::raw_string_ostream tpstream(templatePars);
D->getTemplateParameters()->print(tpstream,Context);
tpstream.flush();
llvm::outs() << "notice classTemplatePartialSpecialization: [" << _class << "] "
<< templatePars << " " << D << "\n";
}
return true;
}
bool DbJSONClassVisitor::VisitClassTemplateSpecializationDecl(const ClassTemplateSpecializationDecl *D) {
DBG(DEBUG_NOTICE, llvm::outs() << "@notice VisitClassTemplateSpecializationDecl(" << D << ")\n"; D->dump(llvm::outs()) );
const CXXRecordDecl* RD = static_cast<const CXXRecordDecl*>(D);
if (classTemplateSpecializationMap.find(RD)==classTemplateSpecializationMap.end()) {
classTemplateSpecializationMap.insert(std::pair<const CXXRecordDecl*,
const ClassTemplateSpecializationDecl*>(RD,D));
}
QualType RT = Context.getRecordType(RD);
std::string _class = RT.getAsString();
QualType T = Context.getRecordType(static_cast<const RecordDecl*>(D));
noticeTypeClass(T);
if (opts.debug) {
std::string _class;
QualType RT = Context.getRecordType(RD);
_class = RT.getAsString();
std::string templatePars;
llvm::raw_string_ostream tpstream(templatePars);
printTemplateArgumentList(tpstream,D->getTemplateArgs().asArray(),Context.getPrintingPolicy());
tpstream.flush();
llvm::outs() << "notice classTemplateSpecialization: [" << _class << "] "
<< templatePars << " " << D << "\n";
}
return true;
}
bool DbJSONClassVisitor::VisitTypeAliasDecl(TypeAliasDecl *D) {
DBG(DEBUG_NOTICE, llvm::errs() << "@notice VisitTypeAliasDecl()\n";
if (D->getDescribedAliasTemplate()) D->getDescribedAliasTemplate()->dump(llvm::outs()); else D->dump(llvm::outs()); );
TypeAliasTemplateDecl* TATD = D->getDescribedAliasTemplate();
if (TATD) {
noticeTemplateParameters(TATD->getTemplateParameters());
}
QualType T = Context.getTypeDeclType(D);
noticeTypeClass(T);
return true;
}
bool DbJSONClassVisitor::VisitTypeAliasDeclFromClass(TypeAliasDecl *D) {
DBG(DEBUG_NOTICE, llvm::errs() << "@notice VisitTypeAliasDeclFromClass()\n";
if (D->getDescribedAliasTemplate()) D->getDescribedAliasTemplate()->dump(llvm::outs()); else D->dump(llvm::outs()); );
TypeAliasTemplateDecl* TATD = D->getDescribedAliasTemplate();
if (TATD) {
noticeTemplateParameters(TATD->getTemplateParameters());
}
QualType T = Context.getTypeDeclType(D);
assert(T->getTypeClass()==Type::Typedef && "Invalid TypeClass for TypeAlias type");
const TypedefType *ttp = cast<TypedefType>(T);
TypedefNameDecl* TPD = ttp->getDecl();
QualType UT = TPD->getUnderlyingType();
const TemplateSpecializationType* tp = LookForTemplateSpecializationType(UT);
if (TATD && tp) {
templateSpecializationTypeAliasMap.insert(std::pair<const TemplateSpecializationType*,TypeAliasDecl*>(tp,D));
}
noticeTypeClass(T);
return true;
}
bool DbJSONClassVisitor::VisitTypedefDeclFromClass(TypedefDecl *D) {
DBG(DEBUG_NOTICE, llvm::outs() << "@notice VisitTypedefDeclFromClass()\n"; D->dump(llvm::outs()) );
QualType T = Context.getTypeDeclType(D);
noticeTypeClass(T);
return true;
}
// Functions
bool DbJSONClassVisitor::VisitFunctionDeclStart(const FunctionDecl *D) {
DBG(DEBUG_NOTICE, llvm::outs() << "@notice VisitFunctionDeclStart(" << D << ")\n"; D->dump(llvm::outs()) );
if (!D->getIdentifier()) {
std::stringstream DN;
DN << static_cast<const Decl*>(D)->getDeclKindName() << "Decl";
unsupportedFuncClass.insert(std::pair<Decl::Kind,std::string>(D->getDeclKind(),DN.str()));
return true;
}
if (D->hasBody()) {
if (FuncMap.find(D)!=FuncMap.end()) {
DBG(DEBUG_NOTICE, llvm::outs() << "@notice VisitFunctionDeclStart(): present\n"; );
lastFunctionDef = &FuncMap[D];
return true;
}
}
else {
lastFunctionDefCache = lastFunctionDef;
lastFunctionDef = nullptr;
if ((getFuncDeclMap().find(D)!=getFuncDeclMap().end())||(
(opts.assert)&&(CTAList.find(D)!=CTAList.end())
)) {
DBG(DEBUG_NOTICE, llvm::outs() << "@notice VisitFunctionDeclStart(): present\n"; );
return true;
}
}
QualType rT = D->getReturnType();
std::stringstream className;
std::stringstream funcName;
const CXXRecordDecl* RD = 0;
if (D->isCXXClassMember()) {
const CXXMethodDecl* MD = static_cast<const CXXMethodDecl*>(D);
RD = MD->getParent();
funcName << MD->getNameAsString();
className << RD->getNameAsString() << "::";
}
else {
funcName << D->getName().str();
}
bool funcSaved = false;
if (D->hasBody()) {
const FunctionDecl * defdecl = D->getDefinition();
if (defdecl==D) {
assert(FuncMap.find(D)==FuncMap.end() && "Multiple definitions of Function body");
FuncMap.insert({D,{}});
FuncMap.at(D).id.setID(FuncNum++);
functionStack.push_back(&FuncMap[D]);
funcSaved = true;
lastFunctionDef = &FuncMap[D];
lastFunctionDef->this_func = D;
lastFunctionDef->CSId = 0;
lastFunctionDef->varId = 0;
if(opts.taint){
FuncMap[D].declcount = internal_declcount(D);
taint_params(D,FuncMap[D]);
}
}
}
else {
D = D->getCanonicalDecl();
if (getFuncDeclMap().find(D)==getFuncDeclMap().end()) {
if (friendDeclMap.find(D)==friendDeclMap.end()) {
// Ignored friend function declarations when arrived here
funcSaved = true;
if (opts.assert) {
if (is_compiletime_assert_decl(D,Context)) {
/* Save only first encounter of the "void __compiletime_assert_N()" function declaration
* Later resolve all references to __compiletime_assert_M() to the first seen declaration
*/
if (CTA) {
funcSaved = false;
}
else {
CTA = D;
}
CTAList.insert(D);
}
}
if (funcSaved) {
getFuncDeclMap().insert(std::pair<const FunctionDecl*,FuncDeclData>(D,{{},D}));
getFuncDeclMap().at(D).id.setID(FuncNum++);
}
}
}
}
if ((D->hasBody() && (D->getDefinition()==D)))
DBG(opts.debug, llvm::outs() << "notice Function: " << className.str() <<
funcName.str() << "() [" << D->getNumParams() << "] ("
<< D->getLocation().printToString(Context.getSourceManager()) << ")"
<< "(" << D->hasBody() << ")" << "(" << (D->hasBody() && (D->getDefinition()==D)) << ") "
<< (const void*)D << "\n" );
//D->dumpColor();
if (funcSaved) {
const TemplateSpecializationType* tp = LookForTemplateSpecializationType(rT);
noticeTypeClass(rT);
for(unsigned long i=0; i<D->getNumParams(); i++) {
std::string TypeS;
const ParmVarDecl* p = D->getParamDecl(i);
QualType T = p->getTypeSourceInfo() ? p->getTypeSourceInfo()->getType() : p->getType();
const TemplateSpecializationType* tp = LookForTemplateSpecializationType(T);
noticeTypeClass(T);
}
if (D->getTemplatedKind()==FunctionDecl::TK_NonTemplate) {
}
else if (D->getTemplatedKind()==FunctionDecl::TK_FunctionTemplate) {
}
else if (D->getTemplatedKind()==FunctionDecl::TK_MemberSpecialization) {
}
else if (D->getTemplatedKind()==FunctionDecl::TK_FunctionTemplateSpecialization) {
}
else if (D->getTemplatedKind()==FunctionDecl::TK_DependentFunctionTemplateSpecialization) {
}
DBG(DEBUG_NOTICE, llvm::outs() << "notice Function: " << className.str() <<
funcName.str() << "() [" << D->getNumParams() << "]: DONE\n"; );
}
return true;
}
bool DbJSONClassVisitor::VisitFunctionDeclComplete(const FunctionDecl *D) {
// get deref expr strings
if(lastFunctionDef){
for(auto &deref : lastFunctionDef->derefList){
deref.evalExpr();
}
}
if (!D->getIdentifier()) {
return true;
}
if (D->hasBody()) {
const FunctionDecl * defdecl = D->getDefinition();
if (defdecl==D) {
functionStack.pop_back();
if (functionStack.size()>0) {
lastFunctionDef = functionStack.back();
}
else {
lastFunctionDef = 0;
}
}
}
else{
lastFunctionDef = lastFunctionDefCache;
lastFunctionDefCache = 0;
}
return true;
}
// Functions - c++ only
bool DbJSONClassVisitor::VisitCXXMethodDecl(const CXXMethodDecl* D) {
//llvm::outs() << "@DbJSONClassVisitor::VisitCXXMethodDecl(" << D << ")\n";
return true;
}
bool DbJSONClassVisitor::VisitFriendDecl(const FriendDecl *D) {
if (!D->getFriendType()) {
if (friendDeclMap.find(D->getFriendDecl())==friendDeclMap.end()) {
friendDeclMap.insert(std::pair<const void*,const FriendDecl*>(D->getFriendDecl(),D));
}
}
else {
if (friendDeclMap.find(D->getFriendType())==friendDeclMap.end()) {
friendDeclMap.insert(std::pair<const void*,const FriendDecl*>(D->getFriendType(),D));
}
}
return true;
}
bool DbJSONClassVisitor::VisitFunctionTemplateDecl(const FunctionTemplateDecl *D) {
const FunctionDecl* FD = D->getTemplatedDecl();
DBG(DEBUG_NOTICE, llvm::outs() << "@notice VisitFunctionTemplateDecl(" << D << "," << FD << ")\n"; D->dump(llvm::outs()) );
for (auto i = D->spec_begin(); i!=D->spec_end(); ++i) {
const FunctionDecl* sFD = static_cast<FunctionDecl*>(*i);
if (functionTemplateMap.find(sFD)==functionTemplateMap.end()) {
functionTemplateMap.insert(std::pair<const FunctionDecl*,const FunctionTemplateDecl*>(sFD,D));
}
}
if (!FD) return true;
if (opts.debug) {
std::string funcName = D->getName().str();
std::string _class;
if (D->isCXXClassMember()) {
const CXXMethodDecl* MD = static_cast<const CXXMethodDecl*>(FD);
const CXXRecordDecl* RD = MD->getParent();
QualType RT = Context.getRecordType(RD);
_class = RT.getAsString();
}
llvm::outs() << "notice FunctionTemplate: " << funcName << " [" << _class << "] " << FD << "\n";
}
if (functionTemplateMap.find(FD)==functionTemplateMap.end()) {
functionTemplateMap.insert(std::pair<const FunctionDecl*,const FunctionTemplateDecl*>(FD,D));
}
return true;
}
// Stmt visitors
bool DbJSONClassVisitor::TraverseStmt(Stmt *S) {
bool TraverseResult = RecursiveASTVisitor<DbJSONClassVisitor>::TraverseStmt(S);
if (S && (S->getStmtClass()==Stmt::CompoundStmtClass)) {
if (!VisitCompoundStmtComplete(static_cast<CompoundStmt*>(S))) return false;
}
return TraverseResult;
}
// bypass the traversal of semantic form introduced by shouldVisitImplicitCode
bool DbJSONClassVisitor::TraverseInitListExpr(InitListExpr *S){
S = S->isSyntacticForm()? S : S->getSyntacticForm();
return Base::TraverseSynOrSemInitListExpr(S);
}
// Track compound statements
bool DbJSONClassVisitor::VisitStmt(Stmt *Node) {
if (( (Node->getStmtClass()==Stmt::NullStmtClass)||
(Node->getStmtClass()==Stmt::IfStmtClass)||
(Node->getStmtClass()==Stmt::SwitchStmtClass)||
(Node->getStmtClass()==Stmt::WhileStmtClass)||
(Node->getStmtClass()==Stmt::DoStmtClass)||
(Node->getStmtClass()==Stmt::ForStmtClass)||
(Node->getStmtClass()==Stmt::IndirectGotoStmtClass)||
(Node->getStmtClass()==Stmt::ReturnStmtClass)||
(Node->getStmtClass()==Stmt::GCCAsmStmtClass)||
(Node->getStmtClass()==Stmt::MSAsmStmtClass)||
(Node->getStmtClass()==Stmt::GotoStmtClass)||
(Node->getStmtClass()==Stmt::BinaryOperatorClass)||
(Node->getStmtClass()==Stmt::BinaryConditionalOperatorClass)||
(Node->getStmtClass()==Stmt::ConditionalOperatorClass)||
(Node->getStmtClass()==Stmt::CallExprClass)
) && (inVarDecl.size()==0)) {
/* Get the location of first non-decl statement in the function body */
if (lastFunctionDef) {
if (lastFunctionDef->firstNonDeclStmtLoc=="") {
SourceLocation sloc = Node->getSourceRange().getBegin();
if (!sloc.isMacroID()) {
lastFunctionDef->firstNonDeclStmtLoc = getAbsoluteLocation(sloc);
}
}
}
}
if (Node->getStmtClass()==Stmt::CompoundStmtClass) {
CompoundStmt* cs = static_cast<CompoundStmt*>(Node);
return VisitCompoundStmtStart(cs);
}
return true;
}
bool DbJSONClassVisitor::VisitCompoundStmtStart(const CompoundStmt *CS) {
if (opts.cstmt) {
const CompoundStmt* parentCS = 0;
if (csStack.size()>0) {
parentCS = csStack.back();
}
csStack.push_back(CS);
if (lastFunctionDef) {
lastFunctionDef->csIdMap.insert(std::pair<const CompoundStmt*,long>(CS,lastFunctionDef->CSId++));
lastFunctionDef->csParentMap.insert(std::pair<const CompoundStmt*,const CompoundStmt*>(CS,parentCS));
}
}
return true;
}
bool DbJSONClassVisitor::VisitCompoundStmtComplete(const CompoundStmt *CS) {
if (opts.cstmt) {
assert(csStack.back()==CS);
csStack.pop_back();
}
return true;
}
void DbJSONClassVisitor::handleConditionDeref(Expr *Cond,size_t cf_id){
if(!Cond) return;
QualType CT = Cond->getType();
VarRef_t VR;
VR.VDCAMUAS.setCond(Cond,cf_id);
std::vector<VarRef_t> vVR;
DbJSONClassVisitor::DREMap_t DREMap;
std::vector<CStyleCastOrType> castVec;
const class Expr* E = stripCastsEx(Cond,castVec);
bool isAddress = false;
/* We might have address constant if we cast the value to pointer type or the return type is a pointer */
if (castVec.size()>0) {
if (castVec.front().getFinalType()->getTypeClass()==Type::Pointer) {
isAddress = true;
}
}
if (!isAddress) {
if (CT->getTypeClass()==Type::Pointer) {
isAddress = true;
}
}
// Check if there's implicit (or explicit) cast to the return value
QualType castType;
if (Cond->getStmtClass()==Stmt::ImplicitCastExprClass) {
const ImplicitCastExpr* ICE = static_cast<const ImplicitCastExpr*>(Cond);
castType = ICE->getSubExpr()->getType();
}
else if (Cond->getStmtClass()==Stmt::CStyleCastExprClass) {
const CStyleCastExpr* CSCE = static_cast<const CStyleCastExpr*>(Cond);
castType = CSCE->getType();
}
if (!castType.isNull()) {
noticeTypeClass(castType);
}
// Check if return expression can be evaluated as a constant expression
Expr::EvalResult Res;
if((!E->isValueDependent()) && E->isEvaluatable(Context) && tryEvaluateIntegerConstantExpr(E,Res)) {
int64_t i = Res.Val.getInt().extOrTrunc(63).getExtValue();
ValueDeclOrCallExprOrAddressOrMEOrUnaryOrAS v;
CStyleCastOrType valuecast;
if (!castType.isNull()) {
valuecast.setType(castType);
}
if (isAddress) {
v.setAddress(i,valuecast);
}
else {
v.setInteger(i,valuecast);
}
vMCtuple_t vMCtuple;
v.setPrimaryFlag(false);
DREMap_add(DREMap,v,vMCtuple);
}
else {
lookup_cache_t cache;
bool compundStmtSeen = false;
unsigned MECnt = 0;
lookForDeclRefWithMemberExprsInternal(E,E,DREMap,cache,&compundStmtSeen,0,&MECnt,0,true,false,false,castType);
}
for (DbJSONClassVisitor::DREMap_t::iterator i = DREMap.begin(); i!=DREMap.end(); ++i) {
VarRef_t iVR;
iVR.VDCAMUAS = (*i).first;
vVR.push_back(iVR);
}
std::pair<std::set<DereferenceInfo_t>::iterator,bool> rv =
lastFunctionDef->derefList.insert(DereferenceInfo_t(VR,cf_id,vVR,"",getCurrentCSPtr(),DereferenceCond));
const_cast<DbJSONClassVisitor::DereferenceInfo_t*>(&(*rv.first))->addOrd(exprOrd++);
const_cast<DbJSONClassVisitor::DereferenceInfo_t*>(&(*rv.first))->evalExprInner =
[Cond,this](const DereferenceInfo_t *d){
llvm::raw_string_ostream exprstream(d->Expr);
exprstream << "[" << getAbsoluteLocation(Cond->getBeginLoc()) << "]: ";
Cond->printPretty(exprstream,nullptr,Context.getPrintingPolicy());
exprstream.flush();
};
}
bool DbJSONClassVisitor::VisitSwitchStmt(SwitchStmt *S){
// add compound statement if not present(should never happen)
if(S->getBody()->getStmtClass() != Stmt::CompoundStmtClass){
CompoundStmt *CS = compatibility::createEmptyCompoundStmt(Context);
CS->body_begin()[0] = S->getBody();
S->setBody(CS);
}
// add control flow info
CompoundStmt *CS = static_cast<CompoundStmt*>(S->getBody());
size_t cf_id = lastFunctionDef->cfData.size();
lastFunctionDef->cfData.push_back({cf_switch,CS});
lastFunctionDef->csInfoMap.insert({CS,cf_id});
// add condition deref
handleConditionDeref(S->getCond(),cf_id);
if (opts.switchopt) {
const Expr* cond = S->getCond();
std::vector<std::pair<DbJSONClassVisitor::caseinfo_t,DbJSONClassVisitor::caseinfo_t>> caselst;
const SwitchCase* cse = S->getSwitchCaseList();
while (cse) {
if (cse->getStmtClass()==Stmt::CaseStmtClass) {
const CaseStmt* ccse = static_cast<const CaseStmt*>(cse);
const Expr* LHS = ccse->getLHS();
int64_t enumvLHS = 0;
std::string enumstrLHS;
std::string macroValueLHS;
std::string raw_codeLHS;
int64_t exprValLHS = 0;
if (LHS) {
setSwitchData(LHS,&enumvLHS,&enumstrLHS,¯oValueLHS,&raw_codeLHS,&exprValLHS);
}
const Expr* RHS = ccse->getRHS();
int64_t enumvRHS = 0;
std::string enumstrRHS;
std::string macroValueRHS;
std::string raw_codeRHS;
int64_t exprValRHS = 0;
if (RHS) {
setSwitchData(RHS,&enumvRHS,&enumstrRHS,¯oValueRHS,&raw_codeRHS,&exprValRHS);
}
caselst.push_back(
std::pair<DbJSONClassVisitor::caseinfo_t,DbJSONClassVisitor::caseinfo_t>(
DbJSONClassVisitor::caseinfo_t(DbJSONClassVisitor::caseenum_t(enumvLHS,enumstrLHS),macroValueLHS,raw_codeLHS,exprValLHS),
DbJSONClassVisitor::caseinfo_t(DbJSONClassVisitor::caseenum_t(enumvRHS,enumstrRHS),macroValueRHS,raw_codeRHS,exprValRHS)
)
);
}
else if (cse->getStmtClass()==Stmt::DefaultStmtClass) {
const DefaultStmt* dsce = static_cast<const DefaultStmt*>(cse);
}
cse = cse->getNextSwitchCase();
}
if (caselst.size()>0) {
/* We might have switch statement in implicit (e.g. operator()) function */
if (lastFunctionDef) {
lastFunctionDef->switch_map.insert(std::pair<const Expr*,std::vector<std::pair<DbJSONClassVisitor::caseinfo_t,
DbJSONClassVisitor::caseinfo_t>>>(cond,caselst));
}
}
}
return true;
}
bool DbJSONClassVisitor::VisitIfStmt(IfStmt *S){
if (!lastFunctionDef) return true;
// add compound statement if not present
if(S->getThen()->getStmtClass() != Stmt::CompoundStmtClass){
CompoundStmt *CS = compatibility::createEmptyCompoundStmt(Context);
CS->body_begin()[0] = S->getThen();
S->setThen(CS);
}
if(S->getElse() && S->getElse()->getStmtClass() != Stmt::CompoundStmtClass){
CompoundStmt *CS = compatibility::createEmptyCompoundStmt(Context);
CS->body_begin()[0] = S->getElse();
S->setElse(CS);
}
// add control flow info
CompoundStmt *CS = static_cast<CompoundStmt*>(S->getThen());
size_t cf_id = lastFunctionDef->cfData.size();
lastFunctionDef->cfData.push_back({cf_if,CS});
lastFunctionDef->csInfoMap.insert({CS,cf_id});
handleConditionDeref(S->getCond(),cf_id);
if(S->getElse()){
CS = static_cast<CompoundStmt*>(S->getElse());
cf_id = lastFunctionDef->cfData.size();
lastFunctionDef->cfData.push_back({cf_else,CS});
lastFunctionDef->csInfoMap.insert({CS,cf_id});
handleConditionDeref(S->getCond(),cf_id);
}
struct DbJSONClassVisitor::IfInfo_t ii = {0,0,0};
if (currentWithinCS()) {
ii.CSPtr = getCurrentCSPtr();
if (hasParentCS()) {
ii.parentCSPtr = getParentCSPtr();
}
}
ii.ifstmt = S;
if (opts.debug3) {
llvm::outs() << "IfStmt: " << "" << " (" << lastFunctionDef->this_func->getName().str() << ")["