This repository was archived by the owner on Nov 1, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 69
/
Copy pathASTDiagnostic.cpp
2051 lines (1818 loc) · 74.8 KB
/
ASTDiagnostic.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
//===--- ASTDiagnostic.cpp - Diagnostic Printing Hooks for AST Nodes ------===//
//
// The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
//
// This file implements a diagnostic formatting hook for AST elements.
//
//===----------------------------------------------------------------------===//
#include "clang/AST/ASTDiagnostic.h"
#include "clang/AST/ASTContext.h"
#include "clang/AST/ASTLambda.h"
#include "clang/AST/Attr.h"
#include "clang/AST/DeclObjC.h"
#include "clang/AST/DeclTemplate.h"
#include "clang/AST/ExprCXX.h"
#include "clang/AST/TemplateBase.h"
#include "clang/AST/Type.h"
#include "llvm/Support/raw_ostream.h"
using namespace clang;
// Returns a desugared version of the QualType, and marks ShouldAKA as true
// whenever we remove significant sugar from the type.
static QualType Desugar(ASTContext &Context, QualType QT, bool &ShouldAKA) {
QualifierCollector QC;
while (true) {
const Type *Ty = QC.strip(QT);
// Don't aka just because we saw an elaborated type...
if (const ElaboratedType *ET = dyn_cast<ElaboratedType>(Ty)) {
QT = ET->desugar();
continue;
}
// ... or a paren type ...
if (const ParenType *PT = dyn_cast<ParenType>(Ty)) {
QT = PT->desugar();
continue;
}
// ...or a substituted template type parameter ...
if (const SubstTemplateTypeParmType *ST =
dyn_cast<SubstTemplateTypeParmType>(Ty)) {
QT = ST->desugar();
continue;
}
// ...or an attributed type...
if (const AttributedType *AT = dyn_cast<AttributedType>(Ty)) {
QT = AT->desugar();
continue;
}
// ...or an adjusted type...
if (const AdjustedType *AT = dyn_cast<AdjustedType>(Ty)) {
QT = AT->desugar();
continue;
}
// ... or an auto type.
if (const AutoType *AT = dyn_cast<AutoType>(Ty)) {
if (!AT->isSugared())
break;
QT = AT->desugar();
continue;
}
// Desugar FunctionType if return type or any parameter type should be
// desugared. Preserve nullability attribute on desugared types.
if (const FunctionType *FT = dyn_cast<FunctionType>(Ty)) {
bool DesugarReturn = false;
QualType SugarRT = FT->getReturnType();
QualType RT = Desugar(Context, SugarRT, DesugarReturn);
if (auto nullability = AttributedType::stripOuterNullability(SugarRT)) {
RT = Context.getAttributedType(
AttributedType::getNullabilityAttrKind(*nullability), RT, RT);
}
bool DesugarArgument = false;
SmallVector<QualType, 4> Args;
const FunctionProtoType *FPT = dyn_cast<FunctionProtoType>(FT);
if (FPT) {
for (QualType SugarPT : FPT->param_types()) {
QualType PT = Desugar(Context, SugarPT, DesugarArgument);
if (auto nullability =
AttributedType::stripOuterNullability(SugarPT)) {
PT = Context.getAttributedType(
AttributedType::getNullabilityAttrKind(*nullability), PT, PT);
}
Args.push_back(PT);
}
}
if (DesugarReturn || DesugarArgument) {
ShouldAKA = true;
QT = FPT ? Context.getFunctionType(RT, Args, FPT->getExtProtoInfo())
: Context.getFunctionNoProtoType(RT, FT->getExtInfo());
break;
}
}
// Desugar template specializations if any template argument should be
// desugared.
if (const TemplateSpecializationType *TST =
dyn_cast<TemplateSpecializationType>(Ty)) {
if (!TST->isTypeAlias()) {
bool DesugarArgument = false;
SmallVector<TemplateArgument, 4> Args;
for (unsigned I = 0, N = TST->getNumArgs(); I != N; ++I) {
const TemplateArgument &Arg = TST->getArg(I);
if (Arg.getKind() == TemplateArgument::Type)
Args.push_back(Desugar(Context, Arg.getAsType(), DesugarArgument));
else
Args.push_back(Arg);
}
if (DesugarArgument) {
ShouldAKA = true;
QT = Context.getTemplateSpecializationType(
TST->getTemplateName(), Args, QT);
}
break;
}
}
// Don't desugar magic Objective-C types.
if (QualType(Ty,0) == Context.getObjCIdType() ||
QualType(Ty,0) == Context.getObjCClassType() ||
QualType(Ty,0) == Context.getObjCSelType() ||
QualType(Ty,0) == Context.getObjCProtoType())
break;
// Don't desugar va_list.
if (QualType(Ty, 0) == Context.getBuiltinVaListType() ||
QualType(Ty, 0) == Context.getBuiltinMSVaListType())
break;
// Otherwise, do a single-step desugar.
QualType Underlying;
bool IsSugar = false;
switch (Ty->getTypeClass()) {
#define ABSTRACT_TYPE(Class, Base)
#define TYPE(Class, Base) \
case Type::Class: { \
const Class##Type *CTy = cast<Class##Type>(Ty); \
if (CTy->isSugared()) { \
IsSugar = true; \
Underlying = CTy->desugar(); \
} \
break; \
}
#include "clang/AST/TypeNodes.def"
}
// If it wasn't sugared, we're done.
if (!IsSugar)
break;
// If the desugared type is a vector type, we don't want to expand
// it, it will turn into an attribute mess. People want their "vec4".
if (isa<VectorType>(Underlying))
break;
// Don't desugar through the primary typedef of an anonymous type.
if (const TagType *UTT = Underlying->getAs<TagType>())
if (const TypedefType *QTT = dyn_cast<TypedefType>(QT))
if (UTT->getDecl()->getTypedefNameForAnonDecl() == QTT->getDecl())
break;
// Record that we actually looked through an opaque type here.
ShouldAKA = true;
QT = Underlying;
}
// If we have a pointer-like type, desugar the pointee as well.
// FIXME: Handle other pointer-like types.
if (const PointerType *Ty = QT->getAs<PointerType>()) {
QT = Context.getPointerType(Desugar(Context, Ty->getPointeeType(),
ShouldAKA));
} else if (const auto *Ty = QT->getAs<ObjCObjectPointerType>()) {
QT = Context.getObjCObjectPointerType(Desugar(Context, Ty->getPointeeType(),
ShouldAKA));
} else if (const LValueReferenceType *Ty = QT->getAs<LValueReferenceType>()) {
QT = Context.getLValueReferenceType(Desugar(Context, Ty->getPointeeType(),
ShouldAKA));
} else if (const RValueReferenceType *Ty = QT->getAs<RValueReferenceType>()) {
QT = Context.getRValueReferenceType(Desugar(Context, Ty->getPointeeType(),
ShouldAKA));
} else if (const auto *Ty = QT->getAs<ObjCObjectType>()) {
if (Ty->getBaseType().getTypePtr() != Ty && !ShouldAKA) {
QualType BaseType = Desugar(Context, Ty->getBaseType(), ShouldAKA);
QT = Context.getObjCObjectType(BaseType, Ty->getTypeArgsAsWritten(),
llvm::makeArrayRef(Ty->qual_begin(),
Ty->getNumProtocols()),
Ty->isKindOfTypeAsWritten());
}
}
return QC.apply(Context, QT);
}
/// \brief Convert the given type to a string suitable for printing as part of
/// a diagnostic.
///
/// There are four main criteria when determining whether we should have an
/// a.k.a. clause when pretty-printing a type:
///
/// 1) Some types provide very minimal sugar that doesn't impede the
/// user's understanding --- for example, elaborated type
/// specifiers. If this is all the sugar we see, we don't want an
/// a.k.a. clause.
/// 2) Some types are technically sugared but are much more familiar
/// when seen in their sugared form --- for example, va_list,
/// vector types, and the magic Objective C types. We don't
/// want to desugar these, even if we do produce an a.k.a. clause.
/// 3) Some types may have already been desugared previously in this diagnostic.
/// if this is the case, doing another "aka" would just be clutter.
/// 4) Two different types within the same diagnostic have the same output
/// string. In this case, force an a.k.a with the desugared type when
/// doing so will provide additional information.
///
/// \param Context the context in which the type was allocated
/// \param Ty the type to print
/// \param QualTypeVals pointer values to QualTypes which are used in the
/// diagnostic message
static std::string
ConvertTypeToDiagnosticString(ASTContext &Context, QualType Ty,
ArrayRef<DiagnosticsEngine::ArgumentValue> PrevArgs,
ArrayRef<intptr_t> QualTypeVals) {
// FIXME: Playing with std::string is really slow.
bool ForceAKA = false;
QualType CanTy = Ty.getCanonicalType();
std::string S = Ty.getAsString(Context.getPrintingPolicy());
std::string CanS = CanTy.getAsString(Context.getPrintingPolicy());
for (unsigned I = 0, E = QualTypeVals.size(); I != E; ++I) {
QualType CompareTy =
QualType::getFromOpaquePtr(reinterpret_cast<void*>(QualTypeVals[I]));
if (CompareTy.isNull())
continue;
if (CompareTy == Ty)
continue; // Same types
QualType CompareCanTy = CompareTy.getCanonicalType();
if (CompareCanTy == CanTy)
continue; // Same canonical types
std::string CompareS = CompareTy.getAsString(Context.getPrintingPolicy());
bool ShouldAKA = false;
QualType CompareDesugar = Desugar(Context, CompareTy, ShouldAKA);
std::string CompareDesugarStr =
CompareDesugar.getAsString(Context.getPrintingPolicy());
if (CompareS != S && CompareDesugarStr != S)
continue; // The type string is different than the comparison string
// and the desugared comparison string.
std::string CompareCanS =
CompareCanTy.getAsString(Context.getPrintingPolicy());
if (CompareCanS == CanS)
continue; // No new info from canonical type
ForceAKA = true;
break;
}
// Check to see if we already desugared this type in this
// diagnostic. If so, don't do it again.
bool Repeated = false;
for (unsigned i = 0, e = PrevArgs.size(); i != e; ++i) {
// TODO: Handle ak_declcontext case.
if (PrevArgs[i].first == DiagnosticsEngine::ak_qualtype) {
void *Ptr = (void*)PrevArgs[i].second;
QualType PrevTy(QualType::getFromOpaquePtr(Ptr));
if (PrevTy == Ty) {
Repeated = true;
break;
}
}
}
// Consider producing an a.k.a. clause if removing all the direct
// sugar gives us something "significantly different".
if (!Repeated) {
bool ShouldAKA = false;
QualType DesugaredTy = Desugar(Context, Ty, ShouldAKA);
if (ShouldAKA || ForceAKA) {
if (DesugaredTy == Ty) {
DesugaredTy = Ty.getCanonicalType();
}
std::string akaStr = DesugaredTy.getAsString(Context.getPrintingPolicy());
if (akaStr != S) {
S = "'" + S + "' (aka '" + akaStr + "')";
return S;
}
}
// Give some additional info on vector types. These are either not desugared
// or displaying complex __attribute__ expressions so add details of the
// type and element count.
if (Ty->isVectorType()) {
const VectorType *VTy = Ty->getAs<VectorType>();
std::string DecoratedString;
llvm::raw_string_ostream OS(DecoratedString);
const char *Values = VTy->getNumElements() > 1 ? "values" : "value";
OS << "'" << S << "' (vector of " << VTy->getNumElements() << " '"
<< VTy->getElementType().getAsString(Context.getPrintingPolicy())
<< "' " << Values << ")";
return OS.str();
}
}
S = "'" + S + "'";
return S;
}
static bool FormatTemplateTypeDiff(ASTContext &Context, QualType FromType,
QualType ToType, bool PrintTree,
bool PrintFromType, bool ElideType,
bool ShowColors, raw_ostream &OS);
void clang::FormatASTNodeDiagnosticArgument(
DiagnosticsEngine::ArgumentKind Kind,
intptr_t Val,
StringRef Modifier,
StringRef Argument,
ArrayRef<DiagnosticsEngine::ArgumentValue> PrevArgs,
SmallVectorImpl<char> &Output,
void *Cookie,
ArrayRef<intptr_t> QualTypeVals) {
ASTContext &Context = *static_cast<ASTContext*>(Cookie);
size_t OldEnd = Output.size();
llvm::raw_svector_ostream OS(Output);
bool NeedQuotes = true;
switch (Kind) {
default: llvm_unreachable("unknown ArgumentKind");
case DiagnosticsEngine::ak_qualtype_pair: {
TemplateDiffTypes &TDT = *reinterpret_cast<TemplateDiffTypes*>(Val);
QualType FromType =
QualType::getFromOpaquePtr(reinterpret_cast<void*>(TDT.FromType));
QualType ToType =
QualType::getFromOpaquePtr(reinterpret_cast<void*>(TDT.ToType));
if (FormatTemplateTypeDiff(Context, FromType, ToType, TDT.PrintTree,
TDT.PrintFromType, TDT.ElideType,
TDT.ShowColors, OS)) {
NeedQuotes = !TDT.PrintTree;
TDT.TemplateDiffUsed = true;
break;
}
// Don't fall-back during tree printing. The caller will handle
// this case.
if (TDT.PrintTree)
return;
// Attempting to do a template diff on non-templates. Set the variables
// and continue with regular type printing of the appropriate type.
Val = TDT.PrintFromType ? TDT.FromType : TDT.ToType;
Modifier = StringRef();
Argument = StringRef();
// Fall through
LLVM_FALLTHROUGH;
}
case DiagnosticsEngine::ak_qualtype: {
assert(Modifier.empty() && Argument.empty() &&
"Invalid modifier for QualType argument");
QualType Ty(QualType::getFromOpaquePtr(reinterpret_cast<void*>(Val)));
OS << ConvertTypeToDiagnosticString(Context, Ty, PrevArgs, QualTypeVals);
NeedQuotes = false;
break;
}
case DiagnosticsEngine::ak_declarationname: {
if (Modifier == "objcclass" && Argument.empty())
OS << '+';
else if (Modifier == "objcinstance" && Argument.empty())
OS << '-';
else
assert(Modifier.empty() && Argument.empty() &&
"Invalid modifier for DeclarationName argument");
OS << DeclarationName::getFromOpaqueInteger(Val);
break;
}
case DiagnosticsEngine::ak_nameddecl: {
bool Qualified;
if (Modifier == "q" && Argument.empty())
Qualified = true;
else {
assert(Modifier.empty() && Argument.empty() &&
"Invalid modifier for NamedDecl* argument");
Qualified = false;
}
const NamedDecl *ND = reinterpret_cast<const NamedDecl*>(Val);
ND->getNameForDiagnostic(OS, Context.getPrintingPolicy(), Qualified);
break;
}
case DiagnosticsEngine::ak_nestednamespec: {
NestedNameSpecifier *NNS = reinterpret_cast<NestedNameSpecifier*>(Val);
NNS->print(OS, Context.getPrintingPolicy());
NeedQuotes = false;
break;
}
case DiagnosticsEngine::ak_declcontext: {
DeclContext *DC = reinterpret_cast<DeclContext *> (Val);
assert(DC && "Should never have a null declaration context");
NeedQuotes = false;
// FIXME: Get the strings for DeclContext from some localized place
if (DC->isTranslationUnit()) {
if (Context.getLangOpts().CPlusPlus)
OS << "the global namespace";
else
OS << "the global scope";
} else if (DC->isClosure()) {
OS << "block literal";
} else if (isLambdaCallOperator(DC)) {
OS << "lambda expression";
} else if (TypeDecl *Type = dyn_cast<TypeDecl>(DC)) {
OS << ConvertTypeToDiagnosticString(Context,
Context.getTypeDeclType(Type),
PrevArgs, QualTypeVals);
} else {
assert(isa<NamedDecl>(DC) && "Expected a NamedDecl");
NamedDecl *ND = cast<NamedDecl>(DC);
if (isa<NamespaceDecl>(ND))
OS << "namespace ";
else if (isa<ObjCMethodDecl>(ND))
OS << "method ";
else if (isa<FunctionDecl>(ND))
OS << "function ";
OS << '\'';
ND->getNameForDiagnostic(OS, Context.getPrintingPolicy(), true);
OS << '\'';
}
break;
}
case DiagnosticsEngine::ak_attr: {
const Attr *At = reinterpret_cast<Attr *>(Val);
assert(At && "Received null Attr object!");
OS << '\'' << At->getSpelling() << '\'';
NeedQuotes = false;
break;
}
}
if (NeedQuotes) {
Output.insert(Output.begin()+OldEnd, '\'');
Output.push_back('\'');
}
}
/// TemplateDiff - A class that constructs a pretty string for a pair of
/// QualTypes. For the pair of types, a diff tree will be created containing
/// all the information about the templates and template arguments. Afterwards,
/// the tree is transformed to a string according to the options passed in.
namespace {
class TemplateDiff {
/// Context - The ASTContext which is used for comparing template arguments.
ASTContext &Context;
/// Policy - Used during expression printing.
PrintingPolicy Policy;
/// ElideType - Option to elide identical types.
bool ElideType;
/// PrintTree - Format output string as a tree.
bool PrintTree;
/// ShowColor - Diagnostics support color, so bolding will be used.
bool ShowColor;
/// FromTemplateType - When single type printing is selected, this is the
/// type to be be printed. When tree printing is selected, this type will
/// show up first in the tree.
QualType FromTemplateType;
/// ToTemplateType - The type that FromType is compared to. Only in tree
/// printing will this type be outputed.
QualType ToTemplateType;
/// OS - The stream used to construct the output strings.
raw_ostream &OS;
/// IsBold - Keeps track of the bold formatting for the output string.
bool IsBold;
/// DiffTree - A tree representation the differences between two types.
class DiffTree {
public:
/// DiffKind - The difference in a DiffNode. Fields of
/// TemplateArgumentInfo needed by each difference can be found in the
/// Set* and Get* functions.
enum DiffKind {
/// Incomplete or invalid node.
Invalid,
/// Another level of templates
Template,
/// Type difference, all type differences except those falling under
/// the Template difference.
Type,
/// Expression difference, this is only when both arguments are
/// expressions. If one argument is an expression and the other is
/// Integer or Declaration, then use that diff type instead.
Expression,
/// Template argument difference
TemplateTemplate,
/// Integer difference
Integer,
/// Declaration difference, nullptr arguments are included here
Declaration,
/// One argument being integer and the other being declaration
FromIntegerAndToDeclaration,
FromDeclarationAndToInteger
};
private:
/// TemplateArgumentInfo - All the information needed to pretty print
/// a template argument. See the Set* and Get* functions to see which
/// fields are used for each DiffKind.
struct TemplateArgumentInfo {
QualType ArgType;
Qualifiers Qual;
llvm::APSInt Val;
bool IsValidInt = false;
Expr *ArgExpr = nullptr;
TemplateDecl *TD = nullptr;
ValueDecl *VD = nullptr;
bool NeedAddressOf = false;
bool IsNullPtr = false;
bool IsDefault = false;
};
/// DiffNode - The root node stores the original type. Each child node
/// stores template arguments of their parents. For templated types, the
/// template decl is also stored.
struct DiffNode {
DiffKind Kind = Invalid;
/// NextNode - The index of the next sibling node or 0.
unsigned NextNode = 0;
/// ChildNode - The index of the first child node or 0.
unsigned ChildNode = 0;
/// ParentNode - The index of the parent node.
unsigned ParentNode = 0;
TemplateArgumentInfo FromArgInfo, ToArgInfo;
/// Same - Whether the two arguments evaluate to the same value.
bool Same = false;
DiffNode(unsigned ParentNode = 0) : ParentNode(ParentNode) {}
};
/// FlatTree - A flattened tree used to store the DiffNodes.
SmallVector<DiffNode, 16> FlatTree;
/// CurrentNode - The index of the current node being used.
unsigned CurrentNode;
/// NextFreeNode - The index of the next unused node. Used when creating
/// child nodes.
unsigned NextFreeNode;
/// ReadNode - The index of the current node being read.
unsigned ReadNode;
public:
DiffTree() :
CurrentNode(0), NextFreeNode(1) {
FlatTree.push_back(DiffNode());
}
// Node writing functions, one for each valid DiffKind element.
void SetTemplateDiff(TemplateDecl *FromTD, TemplateDecl *ToTD,
Qualifiers FromQual, Qualifiers ToQual,
bool FromDefault, bool ToDefault) {
assert(FlatTree[CurrentNode].Kind == Invalid && "Node is not empty.");
FlatTree[CurrentNode].Kind = Template;
FlatTree[CurrentNode].FromArgInfo.TD = FromTD;
FlatTree[CurrentNode].ToArgInfo.TD = ToTD;
FlatTree[CurrentNode].FromArgInfo.Qual = FromQual;
FlatTree[CurrentNode].ToArgInfo.Qual = ToQual;
SetDefault(FromDefault, ToDefault);
}
void SetTypeDiff(QualType FromType, QualType ToType, bool FromDefault,
bool ToDefault) {
assert(FlatTree[CurrentNode].Kind == Invalid && "Node is not empty.");
FlatTree[CurrentNode].Kind = Type;
FlatTree[CurrentNode].FromArgInfo.ArgType = FromType;
FlatTree[CurrentNode].ToArgInfo.ArgType = ToType;
SetDefault(FromDefault, ToDefault);
}
void SetExpressionDiff(Expr *FromExpr, Expr *ToExpr, bool FromDefault,
bool ToDefault) {
assert(FlatTree[CurrentNode].Kind == Invalid && "Node is not empty.");
FlatTree[CurrentNode].Kind = Expression;
FlatTree[CurrentNode].FromArgInfo.ArgExpr = FromExpr;
FlatTree[CurrentNode].ToArgInfo.ArgExpr = ToExpr;
SetDefault(FromDefault, ToDefault);
}
void SetTemplateTemplateDiff(TemplateDecl *FromTD, TemplateDecl *ToTD,
bool FromDefault, bool ToDefault) {
assert(FlatTree[CurrentNode].Kind == Invalid && "Node is not empty.");
FlatTree[CurrentNode].Kind = TemplateTemplate;
FlatTree[CurrentNode].FromArgInfo.TD = FromTD;
FlatTree[CurrentNode].ToArgInfo.TD = ToTD;
SetDefault(FromDefault, ToDefault);
}
void SetIntegerDiff(const llvm::APSInt &FromInt, const llvm::APSInt &ToInt,
bool IsValidFromInt, bool IsValidToInt,
QualType FromIntType, QualType ToIntType,
Expr *FromExpr, Expr *ToExpr, bool FromDefault,
bool ToDefault) {
assert(FlatTree[CurrentNode].Kind == Invalid && "Node is not empty.");
FlatTree[CurrentNode].Kind = Integer;
FlatTree[CurrentNode].FromArgInfo.Val = FromInt;
FlatTree[CurrentNode].ToArgInfo.Val = ToInt;
FlatTree[CurrentNode].FromArgInfo.IsValidInt = IsValidFromInt;
FlatTree[CurrentNode].ToArgInfo.IsValidInt = IsValidToInt;
FlatTree[CurrentNode].FromArgInfo.ArgType = FromIntType;
FlatTree[CurrentNode].ToArgInfo.ArgType = ToIntType;
FlatTree[CurrentNode].FromArgInfo.ArgExpr = FromExpr;
FlatTree[CurrentNode].ToArgInfo.ArgExpr = ToExpr;
SetDefault(FromDefault, ToDefault);
}
void SetDeclarationDiff(ValueDecl *FromValueDecl, ValueDecl *ToValueDecl,
bool FromAddressOf, bool ToAddressOf,
bool FromNullPtr, bool ToNullPtr, Expr *FromExpr,
Expr *ToExpr, bool FromDefault, bool ToDefault) {
assert(FlatTree[CurrentNode].Kind == Invalid && "Node is not empty.");
FlatTree[CurrentNode].Kind = Declaration;
FlatTree[CurrentNode].FromArgInfo.VD = FromValueDecl;
FlatTree[CurrentNode].ToArgInfo.VD = ToValueDecl;
FlatTree[CurrentNode].FromArgInfo.NeedAddressOf = FromAddressOf;
FlatTree[CurrentNode].ToArgInfo.NeedAddressOf = ToAddressOf;
FlatTree[CurrentNode].FromArgInfo.IsNullPtr = FromNullPtr;
FlatTree[CurrentNode].ToArgInfo.IsNullPtr = ToNullPtr;
FlatTree[CurrentNode].FromArgInfo.ArgExpr = FromExpr;
FlatTree[CurrentNode].ToArgInfo.ArgExpr = ToExpr;
SetDefault(FromDefault, ToDefault);
}
void SetFromDeclarationAndToIntegerDiff(
ValueDecl *FromValueDecl, bool FromAddressOf, bool FromNullPtr,
Expr *FromExpr, const llvm::APSInt &ToInt, bool IsValidToInt,
QualType ToIntType, Expr *ToExpr, bool FromDefault, bool ToDefault) {
assert(FlatTree[CurrentNode].Kind == Invalid && "Node is not empty.");
FlatTree[CurrentNode].Kind = FromDeclarationAndToInteger;
FlatTree[CurrentNode].FromArgInfo.VD = FromValueDecl;
FlatTree[CurrentNode].FromArgInfo.NeedAddressOf = FromAddressOf;
FlatTree[CurrentNode].FromArgInfo.IsNullPtr = FromNullPtr;
FlatTree[CurrentNode].FromArgInfo.ArgExpr = FromExpr;
FlatTree[CurrentNode].ToArgInfo.Val = ToInt;
FlatTree[CurrentNode].ToArgInfo.IsValidInt = IsValidToInt;
FlatTree[CurrentNode].ToArgInfo.ArgType = ToIntType;
FlatTree[CurrentNode].ToArgInfo.ArgExpr = ToExpr;
SetDefault(FromDefault, ToDefault);
}
void SetFromIntegerAndToDeclarationDiff(
const llvm::APSInt &FromInt, bool IsValidFromInt, QualType FromIntType,
Expr *FromExpr, ValueDecl *ToValueDecl, bool ToAddressOf,
bool ToNullPtr, Expr *ToExpr, bool FromDefault, bool ToDefault) {
assert(FlatTree[CurrentNode].Kind == Invalid && "Node is not empty.");
FlatTree[CurrentNode].Kind = FromIntegerAndToDeclaration;
FlatTree[CurrentNode].FromArgInfo.Val = FromInt;
FlatTree[CurrentNode].FromArgInfo.IsValidInt = IsValidFromInt;
FlatTree[CurrentNode].FromArgInfo.ArgType = FromIntType;
FlatTree[CurrentNode].FromArgInfo.ArgExpr = FromExpr;
FlatTree[CurrentNode].ToArgInfo.VD = ToValueDecl;
FlatTree[CurrentNode].ToArgInfo.NeedAddressOf = ToAddressOf;
FlatTree[CurrentNode].ToArgInfo.IsNullPtr = ToNullPtr;
FlatTree[CurrentNode].ToArgInfo.ArgExpr = ToExpr;
SetDefault(FromDefault, ToDefault);
}
/// SetDefault - Sets FromDefault and ToDefault flags of the current node.
void SetDefault(bool FromDefault, bool ToDefault) {
assert((!FromDefault || !ToDefault) && "Both arguments cannot be default.");
FlatTree[CurrentNode].FromArgInfo.IsDefault = FromDefault;
FlatTree[CurrentNode].ToArgInfo.IsDefault = ToDefault;
}
/// SetSame - Sets the same flag of the current node.
void SetSame(bool Same) {
FlatTree[CurrentNode].Same = Same;
}
/// SetKind - Sets the current node's type.
void SetKind(DiffKind Kind) {
FlatTree[CurrentNode].Kind = Kind;
}
/// Up - Changes the node to the parent of the current node.
void Up() {
assert(FlatTree[CurrentNode].Kind != Invalid &&
"Cannot exit node before setting node information.");
CurrentNode = FlatTree[CurrentNode].ParentNode;
}
/// AddNode - Adds a child node to the current node, then sets that node
/// node as the current node.
void AddNode() {
assert(FlatTree[CurrentNode].Kind == Template &&
"Only Template nodes can have children nodes.");
FlatTree.push_back(DiffNode(CurrentNode));
DiffNode &Node = FlatTree[CurrentNode];
if (Node.ChildNode == 0) {
// If a child node doesn't exist, add one.
Node.ChildNode = NextFreeNode;
} else {
// If a child node exists, find the last child node and add a
// next node to it.
unsigned i;
for (i = Node.ChildNode; FlatTree[i].NextNode != 0;
i = FlatTree[i].NextNode) {
}
FlatTree[i].NextNode = NextFreeNode;
}
CurrentNode = NextFreeNode;
++NextFreeNode;
}
// Node reading functions.
/// StartTraverse - Prepares the tree for recursive traversal.
void StartTraverse() {
ReadNode = 0;
CurrentNode = NextFreeNode;
NextFreeNode = 0;
}
/// Parent - Move the current read node to its parent.
void Parent() {
ReadNode = FlatTree[ReadNode].ParentNode;
}
void GetTemplateDiff(TemplateDecl *&FromTD, TemplateDecl *&ToTD,
Qualifiers &FromQual, Qualifiers &ToQual) {
assert(FlatTree[ReadNode].Kind == Template && "Unexpected kind.");
FromTD = FlatTree[ReadNode].FromArgInfo.TD;
ToTD = FlatTree[ReadNode].ToArgInfo.TD;
FromQual = FlatTree[ReadNode].FromArgInfo.Qual;
ToQual = FlatTree[ReadNode].ToArgInfo.Qual;
}
void GetTypeDiff(QualType &FromType, QualType &ToType) {
assert(FlatTree[ReadNode].Kind == Type && "Unexpected kind");
FromType = FlatTree[ReadNode].FromArgInfo.ArgType;
ToType = FlatTree[ReadNode].ToArgInfo.ArgType;
}
void GetExpressionDiff(Expr *&FromExpr, Expr *&ToExpr) {
assert(FlatTree[ReadNode].Kind == Expression && "Unexpected kind");
FromExpr = FlatTree[ReadNode].FromArgInfo.ArgExpr;
ToExpr = FlatTree[ReadNode].ToArgInfo.ArgExpr;
}
void GetTemplateTemplateDiff(TemplateDecl *&FromTD, TemplateDecl *&ToTD) {
assert(FlatTree[ReadNode].Kind == TemplateTemplate && "Unexpected kind.");
FromTD = FlatTree[ReadNode].FromArgInfo.TD;
ToTD = FlatTree[ReadNode].ToArgInfo.TD;
}
void GetIntegerDiff(llvm::APSInt &FromInt, llvm::APSInt &ToInt,
bool &IsValidFromInt, bool &IsValidToInt,
QualType &FromIntType, QualType &ToIntType,
Expr *&FromExpr, Expr *&ToExpr) {
assert(FlatTree[ReadNode].Kind == Integer && "Unexpected kind.");
FromInt = FlatTree[ReadNode].FromArgInfo.Val;
ToInt = FlatTree[ReadNode].ToArgInfo.Val;
IsValidFromInt = FlatTree[ReadNode].FromArgInfo.IsValidInt;
IsValidToInt = FlatTree[ReadNode].ToArgInfo.IsValidInt;
FromIntType = FlatTree[ReadNode].FromArgInfo.ArgType;
ToIntType = FlatTree[ReadNode].ToArgInfo.ArgType;
FromExpr = FlatTree[ReadNode].FromArgInfo.ArgExpr;
ToExpr = FlatTree[ReadNode].ToArgInfo.ArgExpr;
}
void GetDeclarationDiff(ValueDecl *&FromValueDecl, ValueDecl *&ToValueDecl,
bool &FromAddressOf, bool &ToAddressOf,
bool &FromNullPtr, bool &ToNullPtr, Expr *&FromExpr,
Expr *&ToExpr) {
assert(FlatTree[ReadNode].Kind == Declaration && "Unexpected kind.");
FromValueDecl = FlatTree[ReadNode].FromArgInfo.VD;
ToValueDecl = FlatTree[ReadNode].ToArgInfo.VD;
FromAddressOf = FlatTree[ReadNode].FromArgInfo.NeedAddressOf;
ToAddressOf = FlatTree[ReadNode].ToArgInfo.NeedAddressOf;
FromNullPtr = FlatTree[ReadNode].FromArgInfo.IsNullPtr;
ToNullPtr = FlatTree[ReadNode].ToArgInfo.IsNullPtr;
FromExpr = FlatTree[ReadNode].FromArgInfo.ArgExpr;
ToExpr = FlatTree[ReadNode].ToArgInfo.ArgExpr;
}
void GetFromDeclarationAndToIntegerDiff(
ValueDecl *&FromValueDecl, bool &FromAddressOf, bool &FromNullPtr,
Expr *&FromExpr, llvm::APSInt &ToInt, bool &IsValidToInt,
QualType &ToIntType, Expr *&ToExpr) {
assert(FlatTree[ReadNode].Kind == FromDeclarationAndToInteger &&
"Unexpected kind.");
FromValueDecl = FlatTree[ReadNode].FromArgInfo.VD;
FromAddressOf = FlatTree[ReadNode].FromArgInfo.NeedAddressOf;
FromNullPtr = FlatTree[ReadNode].FromArgInfo.IsNullPtr;
FromExpr = FlatTree[ReadNode].FromArgInfo.ArgExpr;
ToInt = FlatTree[ReadNode].ToArgInfo.Val;
IsValidToInt = FlatTree[ReadNode].ToArgInfo.IsValidInt;
ToIntType = FlatTree[ReadNode].ToArgInfo.ArgType;
ToExpr = FlatTree[ReadNode].ToArgInfo.ArgExpr;
}
void GetFromIntegerAndToDeclarationDiff(
llvm::APSInt &FromInt, bool &IsValidFromInt, QualType &FromIntType,
Expr *&FromExpr, ValueDecl *&ToValueDecl, bool &ToAddressOf,
bool &ToNullPtr, Expr *&ToExpr) {
assert(FlatTree[ReadNode].Kind == FromIntegerAndToDeclaration &&
"Unexpected kind.");
FromInt = FlatTree[ReadNode].FromArgInfo.Val;
IsValidFromInt = FlatTree[ReadNode].FromArgInfo.IsValidInt;
FromIntType = FlatTree[ReadNode].FromArgInfo.ArgType;
FromExpr = FlatTree[ReadNode].FromArgInfo.ArgExpr;
ToValueDecl = FlatTree[ReadNode].ToArgInfo.VD;
ToAddressOf = FlatTree[ReadNode].ToArgInfo.NeedAddressOf;
ToNullPtr = FlatTree[ReadNode].ToArgInfo.IsNullPtr;
ToExpr = FlatTree[ReadNode].ToArgInfo.ArgExpr;
}
/// FromDefault - Return true if the from argument is the default.
bool FromDefault() {
return FlatTree[ReadNode].FromArgInfo.IsDefault;
}
/// ToDefault - Return true if the to argument is the default.
bool ToDefault() {
return FlatTree[ReadNode].ToArgInfo.IsDefault;
}
/// NodeIsSame - Returns true the arguments are the same.
bool NodeIsSame() {
return FlatTree[ReadNode].Same;
}
/// HasChildrend - Returns true if the node has children.
bool HasChildren() {
return FlatTree[ReadNode].ChildNode != 0;
}
/// MoveToChild - Moves from the current node to its child.
void MoveToChild() {
ReadNode = FlatTree[ReadNode].ChildNode;
}
/// AdvanceSibling - If there is a next sibling, advance to it and return
/// true. Otherwise, return false.
bool AdvanceSibling() {
if (FlatTree[ReadNode].NextNode == 0)
return false;
ReadNode = FlatTree[ReadNode].NextNode;
return true;
}
/// HasNextSibling - Return true if the node has a next sibling.
bool HasNextSibling() {
return FlatTree[ReadNode].NextNode != 0;
}
/// Empty - Returns true if the tree has no information.
bool Empty() {
return GetKind() == Invalid;
}
/// GetKind - Returns the current node's type.
DiffKind GetKind() {
return FlatTree[ReadNode].Kind;
}
};
DiffTree Tree;
/// TSTiterator - a pair of iterators that walks the
/// TemplateSpecializationType and the desugared TemplateSpecializationType.
/// The deseguared TemplateArgument should provide the canonical argument
/// for comparisons.
class TSTiterator {
typedef const TemplateArgument& reference;
typedef const TemplateArgument* pointer;
/// InternalIterator - an iterator that is used to enter a
/// TemplateSpecializationType and read TemplateArguments inside template
/// parameter packs in order with the rest of the TemplateArguments.
struct InternalIterator {
/// TST - the template specialization whose arguments this iterator
/// traverse over.
const TemplateSpecializationType *TST;
/// Index - the index of the template argument in TST.
unsigned Index;
/// CurrentTA - if CurrentTA is not the same as EndTA, then CurrentTA
/// points to a TemplateArgument within a parameter pack.
TemplateArgument::pack_iterator CurrentTA;
/// EndTA - the end iterator of a parameter pack
TemplateArgument::pack_iterator EndTA;
/// InternalIterator - Constructs an iterator and sets it to the first
/// template argument.
InternalIterator(const TemplateSpecializationType *TST)
: TST(TST), Index(0), CurrentTA(nullptr), EndTA(nullptr) {
if (!TST) return;
if (isEnd()) return;
// Set to first template argument. If not a parameter pack, done.
TemplateArgument TA = TST->getArg(0);
if (TA.getKind() != TemplateArgument::Pack) return;
// Start looking into the parameter pack.
CurrentTA = TA.pack_begin();
EndTA = TA.pack_end();
// Found a valid template argument.
if (CurrentTA != EndTA) return;
// Parameter pack is empty, use the increment to get to a valid
// template argument.
++(*this);
}
/// Return true if the iterator is non-singular.
bool isValid() const { return TST; }
/// isEnd - Returns true if the iterator is one past the end.
bool isEnd() const {
assert(TST && "InternalIterator is invalid with a null TST.");
return Index >= TST->getNumArgs();
}
/// &operator++ - Increment the iterator to the next template argument.
InternalIterator &operator++() {
assert(TST && "InternalIterator is invalid with a null TST.");
if (isEnd()) {
return *this;
}
// If in a parameter pack, advance in the parameter pack.
if (CurrentTA != EndTA) {
++CurrentTA;
if (CurrentTA != EndTA)
return *this;
}
// Loop until a template argument is found, or the end is reached.
while (true) {
// Advance to the next template argument. Break if reached the end.
if (++Index == TST->getNumArgs())
break;
// If the TemplateArgument is not a parameter pack, done.
TemplateArgument TA = TST->getArg(Index);
if (TA.getKind() != TemplateArgument::Pack)
break;
// Handle parameter packs.
CurrentTA = TA.pack_begin();
EndTA = TA.pack_end();
// If the parameter pack is empty, try to advance again.
if (CurrentTA != EndTA)
break;
}
return *this;
}
/// operator* - Returns the appropriate TemplateArgument.
reference operator*() const {
assert(TST && "InternalIterator is invalid with a null TST.");
assert(!isEnd() && "Index exceeds number of arguments.");
if (CurrentTA == EndTA)
return TST->getArg(Index);
else
return *CurrentTA;
}
/// operator-> - Allow access to the underlying TemplateArgument.
pointer operator->() const {
assert(TST && "InternalIterator is invalid with a null TST.");
return &operator*();
}
};