1 //===--- ASTDiagnostic.cpp - Diagnostic Printing Hooks for AST Nodes ------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file implements a diagnostic formatting hook for AST elements.
12 //===----------------------------------------------------------------------===//
13 #include "clang/AST/ASTDiagnostic.h"
14 #include "clang/AST/ASTContext.h"
15 #include "clang/AST/ASTLambda.h"
16 #include "clang/AST/Attr.h"
17 #include "clang/AST/DeclObjC.h"
18 #include "clang/AST/DeclTemplate.h"
19 #include "clang/AST/ExprCXX.h"
20 #include "clang/AST/TemplateBase.h"
21 #include "clang/AST/Type.h"
22 #include "llvm/ADT/SmallString.h"
23 #include "llvm/Support/raw_ostream.h"
25 using namespace clang;
27 // Returns a desugared version of the QualType, and marks ShouldAKA as true
28 // whenever we remove significant sugar from the type.
29 static QualType Desugar(ASTContext &Context, QualType QT, bool &ShouldAKA) {
30 QualifierCollector QC;
33 const Type *Ty = QC.strip(QT);
35 // Don't aka just because we saw an elaborated type...
36 if (const ElaboratedType *ET = dyn_cast<ElaboratedType>(Ty)) {
40 // ... or a paren type ...
41 if (const ParenType *PT = dyn_cast<ParenType>(Ty)) {
45 // ...or a substituted template type parameter ...
46 if (const SubstTemplateTypeParmType *ST =
47 dyn_cast<SubstTemplateTypeParmType>(Ty)) {
51 // ...or an attributed type...
52 if (const AttributedType *AT = dyn_cast<AttributedType>(Ty)) {
56 // ...or an adjusted type...
57 if (const AdjustedType *AT = dyn_cast<AdjustedType>(Ty)) {
61 // ... or an auto type.
62 if (const AutoType *AT = dyn_cast<AutoType>(Ty)) {
69 // Desugar FunctionType if return type or any parameter type should be
70 // desugared. Preserve nullability attribute on desugared types.
71 if (const FunctionType *FT = dyn_cast<FunctionType>(Ty)) {
72 bool DesugarReturn = false;
73 QualType SugarRT = FT->getReturnType();
74 QualType RT = Desugar(Context, SugarRT, DesugarReturn);
75 if (auto nullability = AttributedType::stripOuterNullability(SugarRT)) {
76 RT = Context.getAttributedType(
77 AttributedType::getNullabilityAttrKind(*nullability), RT, RT);
80 bool DesugarArgument = false;
81 SmallVector<QualType, 4> Args;
82 const FunctionProtoType *FPT = dyn_cast<FunctionProtoType>(FT);
84 for (QualType SugarPT : FPT->param_types()) {
85 QualType PT = Desugar(Context, SugarPT, DesugarArgument);
86 if (auto nullability =
87 AttributedType::stripOuterNullability(SugarPT)) {
88 PT = Context.getAttributedType(
89 AttributedType::getNullabilityAttrKind(*nullability), PT, PT);
95 if (DesugarReturn || DesugarArgument) {
97 QT = FPT ? Context.getFunctionType(RT, Args, FPT->getExtProtoInfo())
98 : Context.getFunctionNoProtoType(RT, FT->getExtInfo());
103 // Desugar template specializations if any template argument should be
105 if (const TemplateSpecializationType *TST =
106 dyn_cast<TemplateSpecializationType>(Ty)) {
107 if (!TST->isTypeAlias()) {
108 bool DesugarArgument = false;
109 SmallVector<TemplateArgument, 4> Args;
110 for (unsigned I = 0, N = TST->getNumArgs(); I != N; ++I) {
111 const TemplateArgument &Arg = TST->getArg(I);
112 if (Arg.getKind() == TemplateArgument::Type)
113 Args.push_back(Desugar(Context, Arg.getAsType(), DesugarArgument));
118 if (DesugarArgument) {
120 QT = Context.getTemplateSpecializationType(
121 TST->getTemplateName(), Args.data(), Args.size(), QT);
127 // Don't desugar magic Objective-C types.
128 if (QualType(Ty,0) == Context.getObjCIdType() ||
129 QualType(Ty,0) == Context.getObjCClassType() ||
130 QualType(Ty,0) == Context.getObjCSelType() ||
131 QualType(Ty,0) == Context.getObjCProtoType())
134 // Don't desugar va_list.
135 if (QualType(Ty, 0) == Context.getBuiltinVaListType() ||
136 QualType(Ty, 0) == Context.getBuiltinMSVaListType())
139 // Otherwise, do a single-step desugar.
141 bool IsSugar = false;
142 switch (Ty->getTypeClass()) {
143 #define ABSTRACT_TYPE(Class, Base)
144 #define TYPE(Class, Base) \
145 case Type::Class: { \
146 const Class##Type *CTy = cast<Class##Type>(Ty); \
147 if (CTy->isSugared()) { \
149 Underlying = CTy->desugar(); \
153 #include "clang/AST/TypeNodes.def"
156 // If it wasn't sugared, we're done.
160 // If the desugared type is a vector type, we don't want to expand
161 // it, it will turn into an attribute mess. People want their "vec4".
162 if (isa<VectorType>(Underlying))
165 // Don't desugar through the primary typedef of an anonymous type.
166 if (const TagType *UTT = Underlying->getAs<TagType>())
167 if (const TypedefType *QTT = dyn_cast<TypedefType>(QT))
168 if (UTT->getDecl()->getTypedefNameForAnonDecl() == QTT->getDecl())
171 // Record that we actually looked through an opaque type here.
176 // If we have a pointer-like type, desugar the pointee as well.
177 // FIXME: Handle other pointer-like types.
178 if (const PointerType *Ty = QT->getAs<PointerType>()) {
179 QT = Context.getPointerType(Desugar(Context, Ty->getPointeeType(),
181 } else if (const auto *Ty = QT->getAs<ObjCObjectPointerType>()) {
182 QT = Context.getObjCObjectPointerType(Desugar(Context, Ty->getPointeeType(),
184 } else if (const LValueReferenceType *Ty = QT->getAs<LValueReferenceType>()) {
185 QT = Context.getLValueReferenceType(Desugar(Context, Ty->getPointeeType(),
187 } else if (const RValueReferenceType *Ty = QT->getAs<RValueReferenceType>()) {
188 QT = Context.getRValueReferenceType(Desugar(Context, Ty->getPointeeType(),
190 } else if (const auto *Ty = QT->getAs<ObjCObjectType>()) {
191 if (Ty->getBaseType().getTypePtr() != Ty && !ShouldAKA) {
192 QualType BaseType = Desugar(Context, Ty->getBaseType(), ShouldAKA);
193 QT = Context.getObjCObjectType(BaseType, Ty->getTypeArgsAsWritten(),
194 llvm::makeArrayRef(Ty->qual_begin(),
195 Ty->getNumProtocols()),
196 Ty->isKindOfTypeAsWritten());
200 return QC.apply(Context, QT);
203 /// \brief Convert the given type to a string suitable for printing as part of
206 /// There are four main criteria when determining whether we should have an
207 /// a.k.a. clause when pretty-printing a type:
209 /// 1) Some types provide very minimal sugar that doesn't impede the
210 /// user's understanding --- for example, elaborated type
211 /// specifiers. If this is all the sugar we see, we don't want an
213 /// 2) Some types are technically sugared but are much more familiar
214 /// when seen in their sugared form --- for example, va_list,
215 /// vector types, and the magic Objective C types. We don't
216 /// want to desugar these, even if we do produce an a.k.a. clause.
217 /// 3) Some types may have already been desugared previously in this diagnostic.
218 /// if this is the case, doing another "aka" would just be clutter.
219 /// 4) Two different types within the same diagnostic have the same output
220 /// string. In this case, force an a.k.a with the desugared type when
221 /// doing so will provide additional information.
223 /// \param Context the context in which the type was allocated
224 /// \param Ty the type to print
225 /// \param QualTypeVals pointer values to QualTypes which are used in the
226 /// diagnostic message
228 ConvertTypeToDiagnosticString(ASTContext &Context, QualType Ty,
229 ArrayRef<DiagnosticsEngine::ArgumentValue> PrevArgs,
230 ArrayRef<intptr_t> QualTypeVals) {
231 // FIXME: Playing with std::string is really slow.
232 bool ForceAKA = false;
233 QualType CanTy = Ty.getCanonicalType();
234 std::string S = Ty.getAsString(Context.getPrintingPolicy());
235 std::string CanS = CanTy.getAsString(Context.getPrintingPolicy());
237 for (unsigned I = 0, E = QualTypeVals.size(); I != E; ++I) {
239 QualType::getFromOpaquePtr(reinterpret_cast<void*>(QualTypeVals[I]));
240 if (CompareTy.isNull())
243 continue; // Same types
244 QualType CompareCanTy = CompareTy.getCanonicalType();
245 if (CompareCanTy == CanTy)
246 continue; // Same canonical types
247 std::string CompareS = CompareTy.getAsString(Context.getPrintingPolicy());
248 bool ShouldAKA = false;
249 QualType CompareDesugar = Desugar(Context, CompareTy, ShouldAKA);
250 std::string CompareDesugarStr =
251 CompareDesugar.getAsString(Context.getPrintingPolicy());
252 if (CompareS != S && CompareDesugarStr != S)
253 continue; // The type string is different than the comparison string
254 // and the desugared comparison string.
255 std::string CompareCanS =
256 CompareCanTy.getAsString(Context.getPrintingPolicy());
258 if (CompareCanS == CanS)
259 continue; // No new info from canonical type
265 // Check to see if we already desugared this type in this
266 // diagnostic. If so, don't do it again.
267 bool Repeated = false;
268 for (unsigned i = 0, e = PrevArgs.size(); i != e; ++i) {
269 // TODO: Handle ak_declcontext case.
270 if (PrevArgs[i].first == DiagnosticsEngine::ak_qualtype) {
271 void *Ptr = (void*)PrevArgs[i].second;
272 QualType PrevTy(QualType::getFromOpaquePtr(Ptr));
280 // Consider producing an a.k.a. clause if removing all the direct
281 // sugar gives us something "significantly different".
283 bool ShouldAKA = false;
284 QualType DesugaredTy = Desugar(Context, Ty, ShouldAKA);
285 if (ShouldAKA || ForceAKA) {
286 if (DesugaredTy == Ty) {
287 DesugaredTy = Ty.getCanonicalType();
289 std::string akaStr = DesugaredTy.getAsString(Context.getPrintingPolicy());
291 S = "'" + S + "' (aka '" + akaStr + "')";
296 // Give some additional info on vector types. These are either not desugared
297 // or displaying complex __attribute__ expressions so add details of the
298 // type and element count.
299 if (Ty->isVectorType()) {
300 const VectorType *VTy = Ty->getAs<VectorType>();
301 std::string DecoratedString;
302 llvm::raw_string_ostream OS(DecoratedString);
303 const char *Values = VTy->getNumElements() > 1 ? "values" : "value";
304 OS << "'" << S << "' (vector of " << VTy->getNumElements() << " '"
305 << VTy->getElementType().getAsString(Context.getPrintingPolicy())
306 << "' " << Values << ")";
315 static bool FormatTemplateTypeDiff(ASTContext &Context, QualType FromType,
316 QualType ToType, bool PrintTree,
317 bool PrintFromType, bool ElideType,
318 bool ShowColors, raw_ostream &OS);
320 void clang::FormatASTNodeDiagnosticArgument(
321 DiagnosticsEngine::ArgumentKind Kind,
325 ArrayRef<DiagnosticsEngine::ArgumentValue> PrevArgs,
326 SmallVectorImpl<char> &Output,
328 ArrayRef<intptr_t> QualTypeVals) {
329 ASTContext &Context = *static_cast<ASTContext*>(Cookie);
331 size_t OldEnd = Output.size();
332 llvm::raw_svector_ostream OS(Output);
333 bool NeedQuotes = true;
336 default: llvm_unreachable("unknown ArgumentKind");
337 case DiagnosticsEngine::ak_qualtype_pair: {
338 TemplateDiffTypes &TDT = *reinterpret_cast<TemplateDiffTypes*>(Val);
340 QualType::getFromOpaquePtr(reinterpret_cast<void*>(TDT.FromType));
342 QualType::getFromOpaquePtr(reinterpret_cast<void*>(TDT.ToType));
344 if (FormatTemplateTypeDiff(Context, FromType, ToType, TDT.PrintTree,
345 TDT.PrintFromType, TDT.ElideType,
346 TDT.ShowColors, OS)) {
347 NeedQuotes = !TDT.PrintTree;
348 TDT.TemplateDiffUsed = true;
352 // Don't fall-back during tree printing. The caller will handle
357 // Attempting to do a template diff on non-templates. Set the variables
358 // and continue with regular type printing of the appropriate type.
359 Val = TDT.PrintFromType ? TDT.FromType : TDT.ToType;
360 Modifier = StringRef();
361 Argument = StringRef();
364 case DiagnosticsEngine::ak_qualtype: {
365 assert(Modifier.empty() && Argument.empty() &&
366 "Invalid modifier for QualType argument");
368 QualType Ty(QualType::getFromOpaquePtr(reinterpret_cast<void*>(Val)));
369 OS << ConvertTypeToDiagnosticString(Context, Ty, PrevArgs, QualTypeVals);
373 case DiagnosticsEngine::ak_declarationname: {
374 if (Modifier == "objcclass" && Argument.empty())
376 else if (Modifier == "objcinstance" && Argument.empty())
379 assert(Modifier.empty() && Argument.empty() &&
380 "Invalid modifier for DeclarationName argument");
382 OS << DeclarationName::getFromOpaqueInteger(Val);
385 case DiagnosticsEngine::ak_nameddecl: {
387 if (Modifier == "q" && Argument.empty())
390 assert(Modifier.empty() && Argument.empty() &&
391 "Invalid modifier for NamedDecl* argument");
394 const NamedDecl *ND = reinterpret_cast<const NamedDecl*>(Val);
395 ND->getNameForDiagnostic(OS, Context.getPrintingPolicy(), Qualified);
398 case DiagnosticsEngine::ak_nestednamespec: {
399 NestedNameSpecifier *NNS = reinterpret_cast<NestedNameSpecifier*>(Val);
400 NNS->print(OS, Context.getPrintingPolicy());
404 case DiagnosticsEngine::ak_declcontext: {
405 DeclContext *DC = reinterpret_cast<DeclContext *> (Val);
406 assert(DC && "Should never have a null declaration context");
409 // FIXME: Get the strings for DeclContext from some localized place
410 if (DC->isTranslationUnit()) {
411 if (Context.getLangOpts().CPlusPlus)
412 OS << "the global namespace";
414 OS << "the global scope";
415 } else if (DC->isClosure()) {
416 OS << "block literal";
417 } else if (isLambdaCallOperator(DC)) {
418 OS << "lambda expression";
419 } else if (TypeDecl *Type = dyn_cast<TypeDecl>(DC)) {
420 OS << ConvertTypeToDiagnosticString(Context,
421 Context.getTypeDeclType(Type),
422 PrevArgs, QualTypeVals);
424 assert(isa<NamedDecl>(DC) && "Expected a NamedDecl");
425 NamedDecl *ND = cast<NamedDecl>(DC);
426 if (isa<NamespaceDecl>(ND))
428 else if (isa<ObjCMethodDecl>(ND))
430 else if (isa<FunctionDecl>(ND))
434 ND->getNameForDiagnostic(OS, Context.getPrintingPolicy(), true);
439 case DiagnosticsEngine::ak_attr: {
440 const Attr *At = reinterpret_cast<Attr *>(Val);
441 assert(At && "Received null Attr object!");
442 OS << '\'' << At->getSpelling() << '\'';
450 Output.insert(Output.begin()+OldEnd, '\'');
451 Output.push_back('\'');
455 /// TemplateDiff - A class that constructs a pretty string for a pair of
456 /// QualTypes. For the pair of types, a diff tree will be created containing
457 /// all the information about the templates and template arguments. Afterwards,
458 /// the tree is transformed to a string according to the options passed in.
461 /// Context - The ASTContext which is used for comparing template arguments.
464 /// Policy - Used during expression printing.
465 PrintingPolicy Policy;
467 /// ElideType - Option to elide identical types.
470 /// PrintTree - Format output string as a tree.
473 /// ShowColor - Diagnostics support color, so bolding will be used.
476 /// FromTemplateType - When single type printing is selected, this is the
477 /// type to be be printed. When tree printing is selected, this type will
478 /// show up first in the tree.
479 QualType FromTemplateType;
481 /// ToTemplateType - The type that FromType is compared to. Only in tree
482 /// printing will this type be outputed.
483 QualType ToTemplateType;
485 /// OS - The stream used to construct the output strings.
488 /// IsBold - Keeps track of the bold formatting for the output string.
491 /// DiffTree - A tree representation the differences between two types.
494 /// DiffKind - The difference in a DiffNode. Fields of
495 /// TemplateArgumentInfo needed by each difference can be found in the
496 /// Set* and Get* functions.
498 /// Incomplete or invalid node.
500 /// Another level of templates, requires that
502 /// Type difference, all type differences except those falling under
503 /// the Template difference.
505 /// Expression difference, this is only when both arguments are
506 /// expressions. If one argument is an expression and the other is
507 /// Integer or Declaration, then use that diff type instead.
509 /// Template argument difference
511 /// Integer difference
513 /// Declaration difference, nullptr arguments are included here
515 /// One argument being integer and the other being declaration
516 FromIntegerAndToDeclaration,
517 FromDeclarationAndToInteger
521 /// TemplateArgumentInfo - All the information needed to pretty print
522 /// a template argument. See the Set* and Get* functions to see which
523 /// fields are used for each DiffKind.
524 struct TemplateArgumentInfo {
528 bool IsValidInt = false;
529 Expr *ArgExpr = nullptr;
530 TemplateDecl *TD = nullptr;
531 ValueDecl *VD = nullptr;
532 bool NeedAddressOf = false;
533 bool IsNullPtr = false;
534 bool IsDefault = false;
537 /// DiffNode - The root node stores the original type. Each child node
538 /// stores template arguments of their parents. For templated types, the
539 /// template decl is also stored.
541 DiffKind Kind = Invalid;
543 /// NextNode - The index of the next sibling node or 0.
544 unsigned NextNode = 0;
546 /// ChildNode - The index of the first child node or 0.
547 unsigned ChildNode = 0;
549 /// ParentNode - The index of the parent node.
550 unsigned ParentNode = 0;
552 TemplateArgumentInfo FromArgInfo, ToArgInfo;
554 /// Same - Whether the two arguments evaluate to the same value.
557 DiffNode(unsigned ParentNode = 0) : ParentNode(ParentNode) {}
560 /// FlatTree - A flattened tree used to store the DiffNodes.
561 SmallVector<DiffNode, 16> FlatTree;
563 /// CurrentNode - The index of the current node being used.
564 unsigned CurrentNode;
566 /// NextFreeNode - The index of the next unused node. Used when creating
568 unsigned NextFreeNode;
570 /// ReadNode - The index of the current node being read.
575 CurrentNode(0), NextFreeNode(1) {
576 FlatTree.push_back(DiffNode());
579 // Node writing functions, one for each valid DiffKind element.
580 void SetTemplateDiff(TemplateDecl *FromTD, TemplateDecl *ToTD,
581 Qualifiers FromQual, Qualifiers ToQual,
582 bool FromDefault, bool ToDefault) {
583 assert(FlatTree[CurrentNode].Kind == Invalid && "Node is not empty.");
584 FlatTree[CurrentNode].Kind = Template;
585 FlatTree[CurrentNode].FromArgInfo.TD = FromTD;
586 FlatTree[CurrentNode].ToArgInfo.TD = ToTD;
587 FlatTree[CurrentNode].FromArgInfo.Qual = FromQual;
588 FlatTree[CurrentNode].ToArgInfo.Qual = ToQual;
589 SetDefault(FromDefault, ToDefault);
592 void SetTypeDiff(QualType FromType, QualType ToType, bool FromDefault,
594 assert(FlatTree[CurrentNode].Kind == Invalid && "Node is not empty.");
595 FlatTree[CurrentNode].Kind = Type;
596 FlatTree[CurrentNode].FromArgInfo.ArgType = FromType;
597 FlatTree[CurrentNode].ToArgInfo.ArgType = ToType;
598 SetDefault(FromDefault, ToDefault);
601 void SetExpressionDiff(Expr *FromExpr, Expr *ToExpr, bool FromDefault,
603 assert(FlatTree[CurrentNode].Kind == Invalid && "Node is not empty.");
604 FlatTree[CurrentNode].Kind = Expression;
605 FlatTree[CurrentNode].FromArgInfo.ArgExpr = FromExpr;
606 FlatTree[CurrentNode].ToArgInfo.ArgExpr = ToExpr;
607 SetDefault(FromDefault, ToDefault);
610 void SetTemplateTemplateDiff(TemplateDecl *FromTD, TemplateDecl *ToTD,
611 bool FromDefault, bool ToDefault) {
612 assert(FlatTree[CurrentNode].Kind == Invalid && "Node is not empty.");
613 FlatTree[CurrentNode].Kind = TemplateTemplate;
614 FlatTree[CurrentNode].FromArgInfo.TD = FromTD;
615 FlatTree[CurrentNode].ToArgInfo.TD = ToTD;
616 SetDefault(FromDefault, ToDefault);
619 void SetIntegerDiff(llvm::APSInt FromInt, llvm::APSInt ToInt,
620 bool IsValidFromInt, bool IsValidToInt,
621 QualType FromIntType, QualType ToIntType,
622 Expr *FromExpr, Expr *ToExpr, bool FromDefault,
624 assert(FlatTree[CurrentNode].Kind == Invalid && "Node is not empty.");
625 FlatTree[CurrentNode].Kind = Integer;
626 FlatTree[CurrentNode].FromArgInfo.Val = FromInt;
627 FlatTree[CurrentNode].ToArgInfo.Val = ToInt;
628 FlatTree[CurrentNode].FromArgInfo.IsValidInt = IsValidFromInt;
629 FlatTree[CurrentNode].ToArgInfo.IsValidInt = IsValidToInt;
630 FlatTree[CurrentNode].FromArgInfo.ArgType = FromIntType;
631 FlatTree[CurrentNode].ToArgInfo.ArgType = ToIntType;
632 FlatTree[CurrentNode].FromArgInfo.ArgExpr = FromExpr;
633 FlatTree[CurrentNode].ToArgInfo.ArgExpr = ToExpr;
634 SetDefault(FromDefault, ToDefault);
637 void SetDeclarationDiff(ValueDecl *FromValueDecl, ValueDecl *ToValueDecl,
638 bool FromAddressOf, bool ToAddressOf,
639 bool FromNullPtr, bool ToNullPtr, Expr *FromExpr,
640 Expr *ToExpr, bool FromDefault, bool ToDefault) {
641 assert(FlatTree[CurrentNode].Kind == Invalid && "Node is not empty.");
642 FlatTree[CurrentNode].Kind = Declaration;
643 FlatTree[CurrentNode].FromArgInfo.VD = FromValueDecl;
644 FlatTree[CurrentNode].ToArgInfo.VD = ToValueDecl;
645 FlatTree[CurrentNode].FromArgInfo.NeedAddressOf = FromAddressOf;
646 FlatTree[CurrentNode].ToArgInfo.NeedAddressOf = ToAddressOf;
647 FlatTree[CurrentNode].FromArgInfo.IsNullPtr = FromNullPtr;
648 FlatTree[CurrentNode].ToArgInfo.IsNullPtr = ToNullPtr;
649 FlatTree[CurrentNode].FromArgInfo.ArgExpr = FromExpr;
650 FlatTree[CurrentNode].ToArgInfo.ArgExpr = ToExpr;
651 SetDefault(FromDefault, ToDefault);
654 void SetFromDeclarationAndToIntegerDiff(
655 ValueDecl *FromValueDecl, bool FromAddressOf, bool FromNullPtr,
656 Expr *FromExpr, llvm::APSInt ToInt, bool IsValidToInt,
657 QualType ToIntType, Expr *ToExpr, bool FromDefault, bool ToDefault) {
658 assert(FlatTree[CurrentNode].Kind == Invalid && "Node is not empty.");
659 FlatTree[CurrentNode].Kind = FromDeclarationAndToInteger;
660 FlatTree[CurrentNode].FromArgInfo.VD = FromValueDecl;
661 FlatTree[CurrentNode].FromArgInfo.NeedAddressOf = FromAddressOf;
662 FlatTree[CurrentNode].FromArgInfo.IsNullPtr = FromNullPtr;
663 FlatTree[CurrentNode].FromArgInfo.ArgExpr = FromExpr;
664 FlatTree[CurrentNode].ToArgInfo.Val = ToInt;
665 FlatTree[CurrentNode].ToArgInfo.IsValidInt = IsValidToInt;
666 FlatTree[CurrentNode].ToArgInfo.ArgType = ToIntType;
667 FlatTree[CurrentNode].ToArgInfo.ArgExpr = ToExpr;
668 SetDefault(FromDefault, ToDefault);
671 void SetFromIntegerAndToDeclarationDiff(
672 llvm::APSInt FromInt, bool IsValidFromInt, QualType FromIntType,
673 Expr *FromExpr, ValueDecl *ToValueDecl, bool ToAddressOf,
674 bool ToNullPtr, Expr *ToExpr, bool FromDefault, bool ToDefault) {
675 assert(FlatTree[CurrentNode].Kind == Invalid && "Node is not empty.");
676 FlatTree[CurrentNode].Kind = FromIntegerAndToDeclaration;
677 FlatTree[CurrentNode].FromArgInfo.Val = FromInt;
678 FlatTree[CurrentNode].FromArgInfo.IsValidInt = IsValidFromInt;
679 FlatTree[CurrentNode].FromArgInfo.ArgType = FromIntType;
680 FlatTree[CurrentNode].FromArgInfo.ArgExpr = FromExpr;
681 FlatTree[CurrentNode].ToArgInfo.VD = ToValueDecl;
682 FlatTree[CurrentNode].ToArgInfo.NeedAddressOf = ToAddressOf;
683 FlatTree[CurrentNode].ToArgInfo.IsNullPtr = ToNullPtr;
684 FlatTree[CurrentNode].ToArgInfo.ArgExpr = ToExpr;
685 SetDefault(FromDefault, ToDefault);
688 /// SetDefault - Sets FromDefault and ToDefault flags of the current node.
689 void SetDefault(bool FromDefault, bool ToDefault) {
690 assert((!FromDefault || !ToDefault) && "Both arguments cannot be default.");
691 FlatTree[CurrentNode].FromArgInfo.IsDefault = FromDefault;
692 FlatTree[CurrentNode].ToArgInfo.IsDefault = ToDefault;
695 /// SetSame - Sets the same flag of the current node.
696 void SetSame(bool Same) {
697 FlatTree[CurrentNode].Same = Same;
700 /// SetKind - Sets the current node's type.
701 void SetKind(DiffKind Kind) {
702 FlatTree[CurrentNode].Kind = Kind;
705 /// Up - Changes the node to the parent of the current node.
707 assert(FlatTree[CurrentNode].Kind != Invalid &&
708 "Cannot exit node before setting node information.");
709 CurrentNode = FlatTree[CurrentNode].ParentNode;
712 /// AddNode - Adds a child node to the current node, then sets that node
713 /// node as the current node.
715 assert(FlatTree[CurrentNode].Kind == Template &&
716 "Only Template nodes can have children nodes.");
717 FlatTree.push_back(DiffNode(CurrentNode));
718 DiffNode &Node = FlatTree[CurrentNode];
719 if (Node.ChildNode == 0) {
720 // If a child node doesn't exist, add one.
721 Node.ChildNode = NextFreeNode;
723 // If a child node exists, find the last child node and add a
726 for (i = Node.ChildNode; FlatTree[i].NextNode != 0;
727 i = FlatTree[i].NextNode) {
729 FlatTree[i].NextNode = NextFreeNode;
731 CurrentNode = NextFreeNode;
735 // Node reading functions.
736 /// StartTraverse - Prepares the tree for recursive traversal.
737 void StartTraverse() {
739 CurrentNode = NextFreeNode;
743 /// Parent - Move the current read node to its parent.
745 ReadNode = FlatTree[ReadNode].ParentNode;
748 void GetTemplateDiff(TemplateDecl *&FromTD, TemplateDecl *&ToTD,
749 Qualifiers &FromQual, Qualifiers &ToQual) {
750 assert(FlatTree[ReadNode].Kind == Template && "Unexpected kind.");
751 FromTD = FlatTree[ReadNode].FromArgInfo.TD;
752 ToTD = FlatTree[ReadNode].ToArgInfo.TD;
753 FromQual = FlatTree[ReadNode].FromArgInfo.Qual;
754 ToQual = FlatTree[ReadNode].ToArgInfo.Qual;
757 void GetTypeDiff(QualType &FromType, QualType &ToType) {
758 assert(FlatTree[ReadNode].Kind == Type && "Unexpected kind");
759 FromType = FlatTree[ReadNode].FromArgInfo.ArgType;
760 ToType = FlatTree[ReadNode].ToArgInfo.ArgType;
763 void GetExpressionDiff(Expr *&FromExpr, Expr *&ToExpr) {
764 assert(FlatTree[ReadNode].Kind == Expression && "Unexpected kind");
765 FromExpr = FlatTree[ReadNode].FromArgInfo.ArgExpr;
766 ToExpr = FlatTree[ReadNode].ToArgInfo.ArgExpr;
769 void GetTemplateTemplateDiff(TemplateDecl *&FromTD, TemplateDecl *&ToTD) {
770 assert(FlatTree[ReadNode].Kind == TemplateTemplate && "Unexpected kind.");
771 FromTD = FlatTree[ReadNode].FromArgInfo.TD;
772 ToTD = FlatTree[ReadNode].ToArgInfo.TD;
775 void GetIntegerDiff(llvm::APSInt &FromInt, llvm::APSInt &ToInt,
776 bool &IsValidFromInt, bool &IsValidToInt,
777 QualType &FromIntType, QualType &ToIntType,
778 Expr *&FromExpr, Expr *&ToExpr) {
779 assert(FlatTree[ReadNode].Kind == Integer && "Unexpected kind.");
780 FromInt = FlatTree[ReadNode].FromArgInfo.Val;
781 ToInt = FlatTree[ReadNode].ToArgInfo.Val;
782 IsValidFromInt = FlatTree[ReadNode].FromArgInfo.IsValidInt;
783 IsValidToInt = FlatTree[ReadNode].ToArgInfo.IsValidInt;
784 FromIntType = FlatTree[ReadNode].FromArgInfo.ArgType;
785 ToIntType = FlatTree[ReadNode].ToArgInfo.ArgType;
786 FromExpr = FlatTree[ReadNode].FromArgInfo.ArgExpr;
787 ToExpr = FlatTree[ReadNode].ToArgInfo.ArgExpr;
790 void GetDeclarationDiff(ValueDecl *&FromValueDecl, ValueDecl *&ToValueDecl,
791 bool &FromAddressOf, bool &ToAddressOf,
792 bool &FromNullPtr, bool &ToNullPtr, Expr *&FromExpr,
794 assert(FlatTree[ReadNode].Kind == Declaration && "Unexpected kind.");
795 FromValueDecl = FlatTree[ReadNode].FromArgInfo.VD;
796 ToValueDecl = FlatTree[ReadNode].ToArgInfo.VD;
797 FromAddressOf = FlatTree[ReadNode].FromArgInfo.NeedAddressOf;
798 ToAddressOf = FlatTree[ReadNode].ToArgInfo.NeedAddressOf;
799 FromNullPtr = FlatTree[ReadNode].FromArgInfo.IsNullPtr;
800 ToNullPtr = FlatTree[ReadNode].ToArgInfo.IsNullPtr;
801 FromExpr = FlatTree[ReadNode].FromArgInfo.ArgExpr;
802 ToExpr = FlatTree[ReadNode].ToArgInfo.ArgExpr;
805 void GetFromDeclarationAndToIntegerDiff(
806 ValueDecl *&FromValueDecl, bool &FromAddressOf, bool &FromNullPtr,
807 Expr *&FromExpr, llvm::APSInt &ToInt, bool &IsValidToInt,
808 QualType &ToIntType, Expr *&ToExpr) {
809 assert(FlatTree[ReadNode].Kind == FromDeclarationAndToInteger &&
811 FromValueDecl = FlatTree[ReadNode].FromArgInfo.VD;
812 FromAddressOf = FlatTree[ReadNode].FromArgInfo.NeedAddressOf;
813 FromNullPtr = FlatTree[ReadNode].FromArgInfo.IsNullPtr;
814 FromExpr = FlatTree[ReadNode].FromArgInfo.ArgExpr;
815 ToInt = FlatTree[ReadNode].ToArgInfo.Val;
816 IsValidToInt = FlatTree[ReadNode].ToArgInfo.IsValidInt;
817 ToIntType = FlatTree[ReadNode].ToArgInfo.ArgType;
818 ToExpr = FlatTree[ReadNode].ToArgInfo.ArgExpr;
821 void GetFromIntegerAndToDeclarationDiff(
822 llvm::APSInt &FromInt, bool &IsValidFromInt, QualType &FromIntType,
823 Expr *&FromExpr, ValueDecl *&ToValueDecl, bool &ToAddressOf,
824 bool &ToNullPtr, Expr *&ToExpr) {
825 assert(FlatTree[ReadNode].Kind == FromIntegerAndToDeclaration &&
827 FromInt = FlatTree[ReadNode].FromArgInfo.Val;
828 IsValidFromInt = FlatTree[ReadNode].FromArgInfo.IsValidInt;
829 FromIntType = FlatTree[ReadNode].FromArgInfo.ArgType;
830 FromExpr = FlatTree[ReadNode].FromArgInfo.ArgExpr;
831 ToValueDecl = FlatTree[ReadNode].ToArgInfo.VD;
832 ToAddressOf = FlatTree[ReadNode].ToArgInfo.NeedAddressOf;
833 ToNullPtr = FlatTree[ReadNode].ToArgInfo.IsNullPtr;
834 ToExpr = FlatTree[ReadNode].ToArgInfo.ArgExpr;
837 /// FromDefault - Return true if the from argument is the default.
839 return FlatTree[ReadNode].FromArgInfo.IsDefault;
842 /// ToDefault - Return true if the to argument is the default.
844 return FlatTree[ReadNode].ToArgInfo.IsDefault;
847 /// NodeIsSame - Returns true the arguments are the same.
849 return FlatTree[ReadNode].Same;
852 /// HasChildrend - Returns true if the node has children.
854 return FlatTree[ReadNode].ChildNode != 0;
857 /// MoveToChild - Moves from the current node to its child.
859 ReadNode = FlatTree[ReadNode].ChildNode;
862 /// AdvanceSibling - If there is a next sibling, advance to it and return
863 /// true. Otherwise, return false.
864 bool AdvanceSibling() {
865 if (FlatTree[ReadNode].NextNode == 0)
868 ReadNode = FlatTree[ReadNode].NextNode;
872 /// HasNextSibling - Return true if the node has a next sibling.
873 bool HasNextSibling() {
874 return FlatTree[ReadNode].NextNode != 0;
877 /// Empty - Returns true if the tree has no information.
879 return GetKind() == Invalid;
882 /// GetKind - Returns the current node's type.
884 return FlatTree[ReadNode].Kind;
890 /// TSTiterator - a pair of iterators that walks the
891 /// TemplateSpecializationType and the desugared TemplateSpecializationType.
892 /// The deseguared TemplateArgument should provide the canonical argument
895 typedef const TemplateArgument& reference;
896 typedef const TemplateArgument* pointer;
898 /// InternalIterator - an iterator that is used to enter a
899 /// TemplateSpecializationType and read TemplateArguments inside template
900 /// parameter packs in order with the rest of the TemplateArguments.
901 struct InternalIterator {
902 /// TST - the template specialization whose arguments this iterator
904 const TemplateSpecializationType *TST;
906 /// Index - the index of the template argument in TST.
909 /// CurrentTA - if CurrentTA is not the same as EndTA, then CurrentTA
910 /// points to a TemplateArgument within a parameter pack.
911 TemplateArgument::pack_iterator CurrentTA;
913 /// EndTA - the end iterator of a parameter pack
914 TemplateArgument::pack_iterator EndTA;
916 /// InternalIterator - Constructs an iterator and sets it to the first
917 /// template argument.
918 InternalIterator(const TemplateSpecializationType *TST)
919 : TST(TST), Index(0), CurrentTA(nullptr), EndTA(nullptr) {
922 // Set to first template argument. If not a parameter pack, done.
923 TemplateArgument TA = TST->getArg(0);
924 if (TA.getKind() != TemplateArgument::Pack) return;
926 // Start looking into the parameter pack.
927 CurrentTA = TA.pack_begin();
928 EndTA = TA.pack_end();
930 // Found a valid template argument.
931 if (CurrentTA != EndTA) return;
933 // Parameter pack is empty, use the increment to get to a valid
934 // template argument.
938 /// isEnd - Returns true if the iterator is one past the end.
940 return Index >= TST->getNumArgs();
943 /// &operator++ - Increment the iterator to the next template argument.
944 InternalIterator &operator++() {
949 // If in a parameter pack, advance in the parameter pack.
950 if (CurrentTA != EndTA) {
952 if (CurrentTA != EndTA)
956 // Loop until a template argument is found, or the end is reached.
958 // Advance to the next template argument. Break if reached the end.
959 if (++Index == TST->getNumArgs())
962 // If the TemplateArgument is not a parameter pack, done.
963 TemplateArgument TA = TST->getArg(Index);
964 if (TA.getKind() != TemplateArgument::Pack)
967 // Handle parameter packs.
968 CurrentTA = TA.pack_begin();
969 EndTA = TA.pack_end();
971 // If the parameter pack is empty, try to advance again.
972 if (CurrentTA != EndTA)
978 /// operator* - Returns the appropriate TemplateArgument.
979 reference operator*() const {
980 assert(!isEnd() && "Index exceeds number of arguments.");
981 if (CurrentTA == EndTA)
982 return TST->getArg(Index);
987 /// operator-> - Allow access to the underlying TemplateArgument.
988 pointer operator->() const {
993 InternalIterator SugaredIterator;
994 InternalIterator DesugaredIterator;
997 TSTiterator(ASTContext &Context, const TemplateSpecializationType *TST)
998 : SugaredIterator(TST),
1000 GetTemplateSpecializationType(Context, TST->desugar())) {}
1002 /// &operator++ - Increment the iterator to the next template argument.
1003 TSTiterator &operator++() {
1005 ++DesugaredIterator;
1009 /// operator* - Returns the appropriate TemplateArgument.
1010 reference operator*() const {
1011 return *SugaredIterator;
1014 /// operator-> - Allow access to the underlying TemplateArgument.
1015 pointer operator->() const {
1016 return &operator*();
1019 /// isEnd - Returns true if no more TemplateArguments are available.
1020 bool isEnd() const {
1021 return SugaredIterator.isEnd();
1024 /// hasDesugaredTA - Returns true if there is another TemplateArgument
1026 bool hasDesugaredTA() const {
1027 return !DesugaredIterator.isEnd();
1030 /// getDesugaredTA - Returns the desugared TemplateArgument.
1031 reference getDesugaredTA() const {
1032 return *DesugaredIterator;
1036 // These functions build up the template diff tree, including functions to
1037 // retrieve and compare template arguments.
1039 static const TemplateSpecializationType *GetTemplateSpecializationType(
1040 ASTContext &Context, QualType Ty) {
1041 if (const TemplateSpecializationType *TST =
1042 Ty->getAs<TemplateSpecializationType>())
1045 const RecordType *RT = Ty->getAs<RecordType>();
1050 const ClassTemplateSpecializationDecl *CTSD =
1051 dyn_cast<ClassTemplateSpecializationDecl>(RT->getDecl());
1056 Ty = Context.getTemplateSpecializationType(
1057 TemplateName(CTSD->getSpecializedTemplate()),
1058 CTSD->getTemplateArgs().data(),
1059 CTSD->getTemplateArgs().size(),
1060 Ty.getLocalUnqualifiedType().getCanonicalType());
1062 return Ty->getAs<TemplateSpecializationType>();
1065 /// Returns true if the DiffType is Type and false for Template.
1066 static bool OnlyPerformTypeDiff(ASTContext &Context, QualType FromType,
1068 const TemplateSpecializationType *&FromArgTST,
1069 const TemplateSpecializationType *&ToArgTST) {
1070 if (FromType.isNull() || ToType.isNull())
1073 if (Context.hasSameType(FromType, ToType))
1076 FromArgTST = GetTemplateSpecializationType(Context, FromType);
1077 ToArgTST = GetTemplateSpecializationType(Context, ToType);
1079 if (!FromArgTST || !ToArgTST)
1082 if (!hasSameTemplate(FromArgTST, ToArgTST))
1088 /// DiffTypes - Fills a DiffNode with information about a type difference.
1089 void DiffTypes(const TSTiterator &FromIter, const TSTiterator &ToIter) {
1090 QualType FromType = GetType(FromIter);
1091 QualType ToType = GetType(ToIter);
1093 bool FromDefault = FromIter.isEnd() && !FromType.isNull();
1094 bool ToDefault = ToIter.isEnd() && !ToType.isNull();
1096 const TemplateSpecializationType *FromArgTST = nullptr;
1097 const TemplateSpecializationType *ToArgTST = nullptr;
1098 if (OnlyPerformTypeDiff(Context, FromType, ToType, FromArgTST, ToArgTST)) {
1099 Tree.SetTypeDiff(FromType, ToType, FromDefault, ToDefault);
1100 Tree.SetSame(!FromType.isNull() && !ToType.isNull() &&
1101 Context.hasSameType(FromType, ToType));
1103 assert(FromArgTST && ToArgTST &&
1104 "Both template specializations need to be valid.");
1105 Qualifiers FromQual = FromType.getQualifiers(),
1106 ToQual = ToType.getQualifiers();
1107 FromQual -= QualType(FromArgTST, 0).getQualifiers();
1108 ToQual -= QualType(ToArgTST, 0).getQualifiers();
1109 Tree.SetTemplateDiff(FromArgTST->getTemplateName().getAsTemplateDecl(),
1110 ToArgTST->getTemplateName().getAsTemplateDecl(),
1111 FromQual, ToQual, FromDefault, ToDefault);
1112 DiffTemplate(FromArgTST, ToArgTST);
1116 /// DiffTemplateTemplates - Fills a DiffNode with information about a
1117 /// template template difference.
1118 void DiffTemplateTemplates(const TSTiterator &FromIter,
1119 const TSTiterator &ToIter) {
1120 TemplateDecl *FromDecl = GetTemplateDecl(FromIter);
1121 TemplateDecl *ToDecl = GetTemplateDecl(ToIter);
1122 Tree.SetTemplateTemplateDiff(FromDecl, ToDecl, FromIter.isEnd() && FromDecl,
1123 ToIter.isEnd() && ToDecl);
1124 Tree.SetSame(FromDecl && ToDecl &&
1125 FromDecl->getCanonicalDecl() == ToDecl->getCanonicalDecl());
1128 /// InitializeNonTypeDiffVariables - Helper function for DiffNonTypes
1129 static void InitializeNonTypeDiffVariables(ASTContext &Context,
1130 const TSTiterator &Iter,
1131 NonTypeTemplateParmDecl *Default,
1132 llvm::APSInt &Value, bool &HasInt,
1133 QualType &IntType, bool &IsNullPtr,
1134 Expr *&E, ValueDecl *&VD,
1135 bool &NeedAddressOf) {
1136 if (!Iter.isEnd()) {
1137 switch (Iter->getKind()) {
1139 llvm_unreachable("unknown ArgumentKind");
1140 case TemplateArgument::Integral:
1141 Value = Iter->getAsIntegral();
1143 IntType = Iter->getIntegralType();
1145 case TemplateArgument::Declaration: {
1146 VD = Iter->getAsDecl();
1147 QualType ArgType = Iter->getParamTypeForDecl();
1148 QualType VDType = VD->getType();
1149 if (ArgType->isPointerType() &&
1150 Context.hasSameType(ArgType->getPointeeType(), VDType))
1151 NeedAddressOf = true;
1154 case TemplateArgument::NullPtr:
1157 case TemplateArgument::Expression:
1158 E = Iter->getAsExpr();
1160 } else if (!Default->isParameterPack()) {
1161 E = Default->getDefaultArgument();
1164 if (!Iter.hasDesugaredTA()) return;
1166 const TemplateArgument& TA = Iter.getDesugaredTA();
1167 switch (TA.getKind()) {
1169 llvm_unreachable("unknown ArgumentKind");
1170 case TemplateArgument::Integral:
1171 Value = TA.getAsIntegral();
1173 IntType = TA.getIntegralType();
1175 case TemplateArgument::Declaration: {
1176 VD = TA.getAsDecl();
1177 QualType ArgType = TA.getParamTypeForDecl();
1178 QualType VDType = VD->getType();
1179 if (ArgType->isPointerType() &&
1180 Context.hasSameType(ArgType->getPointeeType(), VDType))
1181 NeedAddressOf = true;
1184 case TemplateArgument::NullPtr:
1187 case TemplateArgument::Expression:
1188 // TODO: Sometimes, the desugared template argument Expr differs from
1189 // the sugared template argument Expr. It may be useful in the future
1190 // but for now, it is just discarded.
1197 /// DiffNonTypes - Handles any template parameters not handled by DiffTypes
1198 /// of DiffTemplatesTemplates, such as integer and declaration parameters.
1199 void DiffNonTypes(const TSTiterator &FromIter, const TSTiterator &ToIter,
1200 NonTypeTemplateParmDecl *FromDefaultNonTypeDecl,
1201 NonTypeTemplateParmDecl *ToDefaultNonTypeDecl) {
1202 Expr *FromExpr = nullptr, *ToExpr = nullptr;
1203 llvm::APSInt FromInt, ToInt;
1204 QualType FromIntType, ToIntType;
1205 ValueDecl *FromValueDecl = nullptr, *ToValueDecl = nullptr;
1206 bool HasFromInt = false, HasToInt = false, FromNullPtr = false,
1207 ToNullPtr = false, NeedFromAddressOf = false, NeedToAddressOf = false;
1208 InitializeNonTypeDiffVariables(
1209 Context, FromIter, FromDefaultNonTypeDecl, FromInt, HasFromInt,
1210 FromIntType, FromNullPtr, FromExpr, FromValueDecl, NeedFromAddressOf);
1211 InitializeNonTypeDiffVariables(Context, ToIter, ToDefaultNonTypeDecl, ToInt,
1212 HasToInt, ToIntType, ToNullPtr, ToExpr,
1213 ToValueDecl, NeedToAddressOf);
1215 bool FromDefault = FromIter.isEnd() &&
1216 (FromExpr || FromValueDecl || HasFromInt || FromNullPtr);
1217 bool ToDefault = ToIter.isEnd() &&
1218 (ToExpr || ToValueDecl || HasToInt || ToNullPtr);
1220 bool FromDeclaration = FromValueDecl || FromNullPtr;
1221 bool ToDeclaration = ToValueDecl || ToNullPtr;
1223 if (FromDeclaration && HasToInt) {
1224 Tree.SetFromDeclarationAndToIntegerDiff(
1225 FromValueDecl, NeedFromAddressOf, FromNullPtr, FromExpr, ToInt,
1226 HasToInt, ToIntType, ToExpr, FromDefault, ToDefault);
1227 Tree.SetSame(false);
1232 if (HasFromInt && ToDeclaration) {
1233 Tree.SetFromIntegerAndToDeclarationDiff(
1234 FromInt, HasFromInt, FromIntType, FromExpr, ToValueDecl,
1235 NeedToAddressOf, ToNullPtr, ToExpr, FromDefault, ToDefault);
1236 Tree.SetSame(false);
1240 if (HasFromInt || HasToInt) {
1241 Tree.SetIntegerDiff(FromInt, ToInt, HasFromInt, HasToInt, FromIntType,
1242 ToIntType, FromExpr, ToExpr, FromDefault, ToDefault);
1243 if (HasFromInt && HasToInt) {
1244 Tree.SetSame(Context.hasSameType(FromIntType, ToIntType) &&
1250 if (FromDeclaration || ToDeclaration) {
1251 Tree.SetDeclarationDiff(FromValueDecl, ToValueDecl, NeedFromAddressOf,
1252 NeedToAddressOf, FromNullPtr, ToNullPtr, FromExpr,
1253 ToExpr, FromDefault, ToDefault);
1254 bool BothNull = FromNullPtr && ToNullPtr;
1255 bool SameValueDecl =
1256 FromValueDecl && ToValueDecl &&
1257 NeedFromAddressOf == NeedToAddressOf &&
1258 FromValueDecl->getCanonicalDecl() == ToValueDecl->getCanonicalDecl();
1259 Tree.SetSame(BothNull || SameValueDecl);
1263 assert((FromExpr || ToExpr) && "Both template arguments cannot be empty.");
1264 Tree.SetExpressionDiff(FromExpr, ToExpr, FromDefault, ToDefault);
1265 Tree.SetSame(IsEqualExpr(Context, FromExpr, ToExpr));
1268 /// DiffTemplate - recursively visits template arguments and stores the
1269 /// argument info into a tree.
1270 void DiffTemplate(const TemplateSpecializationType *FromTST,
1271 const TemplateSpecializationType *ToTST) {
1272 // Begin descent into diffing template tree.
1273 TemplateParameterList *ParamsFrom =
1274 FromTST->getTemplateName().getAsTemplateDecl()->getTemplateParameters();
1275 TemplateParameterList *ParamsTo =
1276 ToTST->getTemplateName().getAsTemplateDecl()->getTemplateParameters();
1277 unsigned TotalArgs = 0;
1278 for (TSTiterator FromIter(Context, FromTST), ToIter(Context, ToTST);
1279 !FromIter.isEnd() || !ToIter.isEnd(); ++TotalArgs) {
1282 // Get the parameter at index TotalArgs. If index is larger
1283 // than the total number of parameters, then there is an
1284 // argument pack, so re-use the last parameter.
1285 unsigned FromParamIndex = std::min(TotalArgs, ParamsFrom->size() - 1);
1286 unsigned ToParamIndex = std::min(TotalArgs, ParamsTo->size() - 1);
1287 NamedDecl *FromParamND = ParamsFrom->getParam(FromParamIndex);
1288 NamedDecl *ToParamND = ParamsTo->getParam(ToParamIndex);
1290 assert(FromParamND->getKind() == ToParamND->getKind() &&
1291 "Parameter Decl are not the same kind.");
1293 if (isa<TemplateTypeParmDecl>(FromParamND)) {
1294 DiffTypes(FromIter, ToIter);
1295 } else if (isa<TemplateTemplateParmDecl>(FromParamND)) {
1296 DiffTemplateTemplates(FromIter, ToIter);
1297 } else if (isa<NonTypeTemplateParmDecl>(FromParamND)) {
1298 NonTypeTemplateParmDecl *FromDefaultNonTypeDecl =
1299 cast<NonTypeTemplateParmDecl>(FromParamND);
1300 NonTypeTemplateParmDecl *ToDefaultNonTypeDecl =
1301 cast<NonTypeTemplateParmDecl>(ToParamND);
1302 DiffNonTypes(FromIter, ToIter, FromDefaultNonTypeDecl,
1303 ToDefaultNonTypeDecl);
1305 llvm_unreachable("Unexpected Decl type.");
1314 /// makeTemplateList - Dump every template alias into the vector.
1315 static void makeTemplateList(
1316 SmallVectorImpl<const TemplateSpecializationType *> &TemplateList,
1317 const TemplateSpecializationType *TST) {
1319 TemplateList.push_back(TST);
1320 if (!TST->isTypeAlias())
1322 TST = TST->getAliasedType()->getAs<TemplateSpecializationType>();
1326 /// hasSameBaseTemplate - Returns true when the base templates are the same,
1327 /// even if the template arguments are not.
1328 static bool hasSameBaseTemplate(const TemplateSpecializationType *FromTST,
1329 const TemplateSpecializationType *ToTST) {
1330 return FromTST->getTemplateName().getAsTemplateDecl()->getCanonicalDecl() ==
1331 ToTST->getTemplateName().getAsTemplateDecl()->getCanonicalDecl();
1334 /// hasSameTemplate - Returns true if both types are specialized from the
1335 /// same template declaration. If they come from different template aliases,
1336 /// do a parallel ascension search to determine the highest template alias in
1337 /// common and set the arguments to them.
1338 static bool hasSameTemplate(const TemplateSpecializationType *&FromTST,
1339 const TemplateSpecializationType *&ToTST) {
1340 // Check the top templates if they are the same.
1341 if (hasSameBaseTemplate(FromTST, ToTST))
1344 // Create vectors of template aliases.
1345 SmallVector<const TemplateSpecializationType*, 1> FromTemplateList,
1348 makeTemplateList(FromTemplateList, FromTST);
1349 makeTemplateList(ToTemplateList, ToTST);
1351 SmallVectorImpl<const TemplateSpecializationType *>::reverse_iterator
1352 FromIter = FromTemplateList.rbegin(), FromEnd = FromTemplateList.rend(),
1353 ToIter = ToTemplateList.rbegin(), ToEnd = ToTemplateList.rend();
1355 // Check if the lowest template types are the same. If not, return.
1356 if (!hasSameBaseTemplate(*FromIter, *ToIter))
1359 // Begin searching up the template aliases. The bottom most template
1360 // matches so move up until one pair does not match. Use the template
1361 // right before that one.
1362 for (; FromIter != FromEnd && ToIter != ToEnd; ++FromIter, ++ToIter) {
1363 if (!hasSameBaseTemplate(*FromIter, *ToIter))
1367 FromTST = FromIter[-1];
1373 /// GetType - Retrieves the template type arguments, including default
1375 static QualType GetType(const TSTiterator &Iter) {
1377 return Iter->getAsType();
1378 if (Iter.hasDesugaredTA())
1379 return Iter.getDesugaredTA().getAsType();
1383 /// GetTemplateDecl - Retrieves the template template arguments, including
1384 /// default arguments.
1385 static TemplateDecl *GetTemplateDecl(const TSTiterator &Iter) {
1387 return Iter->getAsTemplate().getAsTemplateDecl();
1388 if (Iter.hasDesugaredTA())
1389 return Iter.getDesugaredTA().getAsTemplate().getAsTemplateDecl();
1393 /// IsEqualExpr - Returns true if the expressions are the same in regards to
1394 /// template arguments. These expressions are dependent, so profile them
1395 /// instead of trying to evaluate them.
1396 static bool IsEqualExpr(ASTContext &Context, Expr *FromExpr, Expr *ToExpr) {
1397 if (FromExpr == ToExpr)
1400 if (!FromExpr || !ToExpr)
1403 llvm::FoldingSetNodeID FromID, ToID;
1404 FromExpr->Profile(FromID, Context, true);
1405 ToExpr->Profile(ToID, Context, true);
1406 return FromID == ToID;
1409 // These functions converts the tree representation of the template
1410 // differences into the internal character vector.
1412 /// TreeToString - Converts the Tree object into a character stream which
1413 /// will later be turned into the output string.
1414 void TreeToString(int Indent = 1) {
1417 OS.indent(2 * Indent);
1421 // Handle cases where the difference is not templates with different
1423 switch (Tree.GetKind()) {
1424 case DiffTree::Invalid:
1425 llvm_unreachable("Template diffing failed with bad DiffNode");
1426 case DiffTree::Type: {
1427 QualType FromType, ToType;
1428 Tree.GetTypeDiff(FromType, ToType);
1429 PrintTypeNames(FromType, ToType, Tree.FromDefault(), Tree.ToDefault(),
1433 case DiffTree::Expression: {
1434 Expr *FromExpr, *ToExpr;
1435 Tree.GetExpressionDiff(FromExpr, ToExpr);
1436 PrintExpr(FromExpr, ToExpr, Tree.FromDefault(), Tree.ToDefault(),
1440 case DiffTree::TemplateTemplate: {
1441 TemplateDecl *FromTD, *ToTD;
1442 Tree.GetTemplateTemplateDiff(FromTD, ToTD);
1443 PrintTemplateTemplate(FromTD, ToTD, Tree.FromDefault(),
1444 Tree.ToDefault(), Tree.NodeIsSame());
1447 case DiffTree::Integer: {
1448 llvm::APSInt FromInt, ToInt;
1449 Expr *FromExpr, *ToExpr;
1450 bool IsValidFromInt, IsValidToInt;
1451 QualType FromIntType, ToIntType;
1452 Tree.GetIntegerDiff(FromInt, ToInt, IsValidFromInt, IsValidToInt,
1453 FromIntType, ToIntType, FromExpr, ToExpr);
1454 PrintAPSInt(FromInt, ToInt, IsValidFromInt, IsValidToInt, FromIntType,
1455 ToIntType, FromExpr, ToExpr, Tree.FromDefault(),
1456 Tree.ToDefault(), Tree.NodeIsSame());
1459 case DiffTree::Declaration: {
1460 ValueDecl *FromValueDecl, *ToValueDecl;
1461 bool FromAddressOf, ToAddressOf;
1462 bool FromNullPtr, ToNullPtr;
1463 Expr *FromExpr, *ToExpr;
1464 Tree.GetDeclarationDiff(FromValueDecl, ToValueDecl, FromAddressOf,
1465 ToAddressOf, FromNullPtr, ToNullPtr, FromExpr,
1467 PrintValueDecl(FromValueDecl, ToValueDecl, FromAddressOf, ToAddressOf,
1468 FromNullPtr, ToNullPtr, FromExpr, ToExpr,
1469 Tree.FromDefault(), Tree.ToDefault(), Tree.NodeIsSame());
1472 case DiffTree::FromDeclarationAndToInteger: {
1473 ValueDecl *FromValueDecl;
1481 Tree.GetFromDeclarationAndToIntegerDiff(
1482 FromValueDecl, FromAddressOf, FromNullPtr, FromExpr, ToInt,
1483 IsValidToInt, ToIntType, ToExpr);
1484 assert((FromValueDecl || FromNullPtr) && IsValidToInt);
1485 PrintValueDeclAndInteger(FromValueDecl, FromAddressOf, FromNullPtr,
1486 FromExpr, Tree.FromDefault(), ToInt, ToIntType,
1487 ToExpr, Tree.ToDefault());
1490 case DiffTree::FromIntegerAndToDeclaration: {
1491 llvm::APSInt FromInt;
1492 bool IsValidFromInt;
1493 QualType FromIntType;
1495 ValueDecl *ToValueDecl;
1499 Tree.GetFromIntegerAndToDeclarationDiff(
1500 FromInt, IsValidFromInt, FromIntType, FromExpr, ToValueDecl,
1501 ToAddressOf, ToNullPtr, ToExpr);
1502 assert(IsValidFromInt && (ToValueDecl || ToNullPtr));
1503 PrintIntegerAndValueDecl(FromInt, FromIntType, FromExpr,
1504 Tree.FromDefault(), ToValueDecl, ToAddressOf,
1505 ToNullPtr, ToExpr, Tree.ToDefault());
1508 case DiffTree::Template: {
1509 // Node is root of template. Recurse on children.
1510 TemplateDecl *FromTD, *ToTD;
1511 Qualifiers FromQual, ToQual;
1512 Tree.GetTemplateDiff(FromTD, ToTD, FromQual, ToQual);
1514 PrintQualifiers(FromQual, ToQual);
1516 if (!Tree.HasChildren()) {
1517 // If we're dealing with a template specialization with zero
1518 // arguments, there are no children; special-case this.
1519 OS << FromTD->getNameAsString() << "<>";
1523 OS << FromTD->getNameAsString() << '<';
1525 unsigned NumElideArgs = 0;
1528 if (Tree.NodeIsSame()) {
1532 if (NumElideArgs > 0) {
1533 PrintElideArgs(NumElideArgs, Indent);
1538 TreeToString(Indent);
1539 if (Tree.HasNextSibling())
1541 } while (Tree.AdvanceSibling());
1542 if (NumElideArgs > 0)
1543 PrintElideArgs(NumElideArgs, Indent);
1552 // To signal to the text printer that a certain text needs to be bolded,
1553 // a special character is injected into the character stream which the
1554 // text printer will later strip out.
1556 /// Bold - Start bolding text.
1558 assert(!IsBold && "Attempting to bold text that is already bold.");
1561 OS << ToggleHighlight;
1564 /// Unbold - Stop bolding text.
1566 assert(IsBold && "Attempting to remove bold from unbold text.");
1569 OS << ToggleHighlight;
1572 // Functions to print out the arguments and highlighting the difference.
1574 /// PrintTypeNames - prints the typenames, bolding differences. Will detect
1575 /// typenames that are the same and attempt to disambiguate them by using
1576 /// canonical typenames.
1577 void PrintTypeNames(QualType FromType, QualType ToType,
1578 bool FromDefault, bool ToDefault, bool Same) {
1579 assert((!FromType.isNull() || !ToType.isNull()) &&
1580 "Only one template argument may be missing.");
1583 OS << FromType.getAsString(Policy);
1587 if (!FromType.isNull() && !ToType.isNull() &&
1588 FromType.getLocalUnqualifiedType() ==
1589 ToType.getLocalUnqualifiedType()) {
1590 Qualifiers FromQual = FromType.getLocalQualifiers(),
1591 ToQual = ToType.getLocalQualifiers();
1592 PrintQualifiers(FromQual, ToQual);
1593 FromType.getLocalUnqualifiedType().print(OS, Policy);
1597 std::string FromTypeStr = FromType.isNull() ? "(no argument)"
1598 : FromType.getAsString(Policy);
1599 std::string ToTypeStr = ToType.isNull() ? "(no argument)"
1600 : ToType.getAsString(Policy);
1601 // Switch to canonical typename if it is better.
1602 // TODO: merge this with other aka printing above.
1603 if (FromTypeStr == ToTypeStr) {
1604 std::string FromCanTypeStr =
1605 FromType.getCanonicalType().getAsString(Policy);
1606 std::string ToCanTypeStr = ToType.getCanonicalType().getAsString(Policy);
1607 if (FromCanTypeStr != ToCanTypeStr) {
1608 FromTypeStr = FromCanTypeStr;
1609 ToTypeStr = ToCanTypeStr;
1613 if (PrintTree) OS << '[';
1614 OS << (FromDefault ? "(default) " : "");
1619 OS << " != " << (ToDefault ? "(default) " : "");
1628 /// PrintExpr - Prints out the expr template arguments, highlighting argument
1630 void PrintExpr(const Expr *FromExpr, const Expr *ToExpr, bool FromDefault,
1631 bool ToDefault, bool Same) {
1632 assert((FromExpr || ToExpr) &&
1633 "Only one template argument may be missing.");
1635 PrintExpr(FromExpr);
1636 } else if (!PrintTree) {
1637 OS << (FromDefault ? "(default) " : "");
1639 PrintExpr(FromExpr);
1642 OS << (FromDefault ? "[(default) " : "[");
1644 PrintExpr(FromExpr);
1646 OS << " != " << (ToDefault ? "(default) " : "");
1654 /// PrintExpr - Actual formatting and printing of expressions.
1655 void PrintExpr(const Expr *E) {
1657 E->printPretty(OS, nullptr, Policy);
1660 OS << "(no argument)";
1663 /// PrintTemplateTemplate - Handles printing of template template arguments,
1664 /// highlighting argument differences.
1665 void PrintTemplateTemplate(TemplateDecl *FromTD, TemplateDecl *ToTD,
1666 bool FromDefault, bool ToDefault, bool Same) {
1667 assert((FromTD || ToTD) && "Only one template argument may be missing.");
1669 std::string FromName = FromTD ? FromTD->getName() : "(no argument)";
1670 std::string ToName = ToTD ? ToTD->getName() : "(no argument)";
1671 if (FromTD && ToTD && FromName == ToName) {
1672 FromName = FromTD->getQualifiedNameAsString();
1673 ToName = ToTD->getQualifiedNameAsString();
1677 OS << "template " << FromTD->getNameAsString();
1678 } else if (!PrintTree) {
1679 OS << (FromDefault ? "(default) template " : "template ");
1684 OS << (FromDefault ? "[(default) template " : "[template ");
1688 OS << " != " << (ToDefault ? "(default) template " : "template ");
1696 /// PrintAPSInt - Handles printing of integral arguments, highlighting
1697 /// argument differences.
1698 void PrintAPSInt(llvm::APSInt FromInt, llvm::APSInt ToInt,
1699 bool IsValidFromInt, bool IsValidToInt, QualType FromIntType,
1700 QualType ToIntType, Expr *FromExpr, Expr *ToExpr,
1701 bool FromDefault, bool ToDefault, bool Same) {
1702 assert((IsValidFromInt || IsValidToInt) &&
1703 "Only one integral argument may be missing.");
1706 if (FromIntType->isBooleanType()) {
1707 OS << ((FromInt == 0) ? "false" : "true");
1709 OS << FromInt.toString(10);
1714 bool PrintType = IsValidFromInt && IsValidToInt &&
1715 !Context.hasSameType(FromIntType, ToIntType);
1718 OS << (FromDefault ? "(default) " : "");
1719 PrintAPSInt(FromInt, FromExpr, IsValidFromInt, FromIntType, PrintType);
1721 OS << (FromDefault ? "[(default) " : "[");
1722 PrintAPSInt(FromInt, FromExpr, IsValidFromInt, FromIntType, PrintType);
1723 OS << " != " << (ToDefault ? "(default) " : "");
1724 PrintAPSInt(ToInt, ToExpr, IsValidToInt, ToIntType, PrintType);
1729 /// PrintAPSInt - If valid, print the APSInt. If the expression is
1730 /// gives more information, print it too.
1731 void PrintAPSInt(llvm::APSInt Val, Expr *E, bool Valid, QualType IntType,
1735 if (HasExtraInfo(E)) {
1745 IntType.print(OS, Context.getPrintingPolicy());
1750 if (IntType->isBooleanType()) {
1751 OS << ((Val == 0) ? "false" : "true");
1753 OS << Val.toString(10);
1758 OS << "(no argument)";
1763 /// HasExtraInfo - Returns true if E is not an integer literal, the
1764 /// negation of an integer literal, or a boolean literal.
1765 bool HasExtraInfo(Expr *E) {
1766 if (!E) return false;
1768 E = E->IgnoreImpCasts();
1770 if (isa<IntegerLiteral>(E)) return false;
1772 if (UnaryOperator *UO = dyn_cast<UnaryOperator>(E))
1773 if (UO->getOpcode() == UO_Minus)
1774 if (isa<IntegerLiteral>(UO->getSubExpr()))
1777 if (isa<CXXBoolLiteralExpr>(E))
1783 void PrintValueDecl(ValueDecl *VD, bool AddressOf, Expr *E, bool NullPtr) {
1787 OS << VD->getName();
1792 if (E && !isa<CXXNullPtrLiteralExpr>(E)) {
1807 OS << "(no argument)";
1810 /// PrintDecl - Handles printing of Decl arguments, highlighting
1811 /// argument differences.
1812 void PrintValueDecl(ValueDecl *FromValueDecl, ValueDecl *ToValueDecl,
1813 bool FromAddressOf, bool ToAddressOf, bool FromNullPtr,
1814 bool ToNullPtr, Expr *FromExpr, Expr *ToExpr,
1815 bool FromDefault, bool ToDefault, bool Same) {
1816 assert((FromValueDecl || FromNullPtr || ToValueDecl || ToNullPtr) &&
1817 "Only one Decl argument may be NULL");
1820 PrintValueDecl(FromValueDecl, FromAddressOf, FromExpr, FromNullPtr);
1821 } else if (!PrintTree) {
1822 OS << (FromDefault ? "(default) " : "");
1824 PrintValueDecl(FromValueDecl, FromAddressOf, FromExpr, FromNullPtr);
1827 OS << (FromDefault ? "[(default) " : "[");
1829 PrintValueDecl(FromValueDecl, FromAddressOf, FromExpr, FromNullPtr);
1831 OS << " != " << (ToDefault ? "(default) " : "");
1833 PrintValueDecl(ToValueDecl, ToAddressOf, ToExpr, ToNullPtr);
1840 /// PrintValueDeclAndInteger - Uses the print functions for ValueDecl and
1841 /// APSInt to print a mixed difference.
1842 void PrintValueDeclAndInteger(ValueDecl *VD, bool NeedAddressOf,
1843 bool IsNullPtr, Expr *VDExpr, bool DefaultDecl,
1844 llvm::APSInt Val, QualType IntType,
1845 Expr *IntExpr, bool DefaultInt) {
1847 OS << (DefaultDecl ? "(default) " : "");
1849 PrintValueDecl(VD, NeedAddressOf, VDExpr, IsNullPtr);
1852 OS << (DefaultDecl ? "[(default) " : "[");
1854 PrintValueDecl(VD, NeedAddressOf, VDExpr, IsNullPtr);
1856 OS << " != " << (DefaultInt ? "(default) " : "");
1857 PrintAPSInt(Val, IntExpr, true /*Valid*/, IntType, false /*PrintType*/);
1862 /// PrintIntegerAndValueDecl - Uses the print functions for APSInt and
1863 /// ValueDecl to print a mixed difference.
1864 void PrintIntegerAndValueDecl(llvm::APSInt Val, QualType IntType,
1865 Expr *IntExpr, bool DefaultInt, ValueDecl *VD,
1866 bool NeedAddressOf, bool IsNullPtr,
1867 Expr *VDExpr, bool DefaultDecl) {
1869 OS << (DefaultInt ? "(default) " : "");
1870 PrintAPSInt(Val, IntExpr, true /*Valid*/, IntType, false /*PrintType*/);
1872 OS << (DefaultInt ? "[(default) " : "[");
1873 PrintAPSInt(Val, IntExpr, true /*Valid*/, IntType, false /*PrintType*/);
1874 OS << " != " << (DefaultDecl ? "(default) " : "");
1876 PrintValueDecl(VD, NeedAddressOf, VDExpr, IsNullPtr);
1882 // Prints the appropriate placeholder for elided template arguments.
1883 void PrintElideArgs(unsigned NumElideArgs, unsigned Indent) {
1886 for (unsigned i = 0; i < Indent; ++i)
1889 if (NumElideArgs == 0) return;
1890 if (NumElideArgs == 1)
1893 OS << "[" << NumElideArgs << " * ...]";
1896 // Prints and highlights differences in Qualifiers.
1897 void PrintQualifiers(Qualifiers FromQual, Qualifiers ToQual) {
1898 // Both types have no qualifiers
1899 if (FromQual.empty() && ToQual.empty())
1902 // Both types have same qualifiers
1903 if (FromQual == ToQual) {
1904 PrintQualifier(FromQual, /*ApplyBold*/false);
1908 // Find common qualifiers and strip them from FromQual and ToQual.
1909 Qualifiers CommonQual = Qualifiers::removeCommonQualifiers(FromQual,
1912 // The qualifiers are printed before the template name.
1914 // The common qualifiers are printed. Then, qualifiers only in this type
1915 // are printed and highlighted. Finally, qualifiers only in the other
1916 // type are printed and highlighted inside parentheses after "missing".
1918 // Qualifiers are printed next to each other, inside brackets, and
1919 // separated by "!=". The printing order is:
1920 // common qualifiers, highlighted from qualifiers, "!=",
1921 // common qualifiers, highlighted to qualifiers
1924 if (CommonQual.empty() && FromQual.empty()) {
1926 OS << "(no qualifiers) ";
1929 PrintQualifier(CommonQual, /*ApplyBold*/false);
1930 PrintQualifier(FromQual, /*ApplyBold*/true);
1933 if (CommonQual.empty() && ToQual.empty()) {
1935 OS << "(no qualifiers)";
1938 PrintQualifier(CommonQual, /*ApplyBold*/false,
1939 /*appendSpaceIfNonEmpty*/!ToQual.empty());
1940 PrintQualifier(ToQual, /*ApplyBold*/true,
1941 /*appendSpaceIfNonEmpty*/false);
1945 PrintQualifier(CommonQual, /*ApplyBold*/false);
1946 PrintQualifier(FromQual, /*ApplyBold*/true);
1950 void PrintQualifier(Qualifiers Q, bool ApplyBold,
1951 bool AppendSpaceIfNonEmpty = true) {
1952 if (Q.empty()) return;
1953 if (ApplyBold) Bold();
1954 Q.print(OS, Policy, AppendSpaceIfNonEmpty);
1955 if (ApplyBold) Unbold();
1960 TemplateDiff(raw_ostream &OS, ASTContext &Context, QualType FromType,
1961 QualType ToType, bool PrintTree, bool PrintFromType,
1962 bool ElideType, bool ShowColor)
1964 Policy(Context.getLangOpts()),
1965 ElideType(ElideType),
1966 PrintTree(PrintTree),
1967 ShowColor(ShowColor),
1968 // When printing a single type, the FromType is the one printed.
1969 FromTemplateType(PrintFromType ? FromType : ToType),
1970 ToTemplateType(PrintFromType ? ToType : FromType),
1975 /// DiffTemplate - Start the template type diffing.
1976 void DiffTemplate() {
1977 Qualifiers FromQual = FromTemplateType.getQualifiers(),
1978 ToQual = ToTemplateType.getQualifiers();
1980 const TemplateSpecializationType *FromOrigTST =
1981 GetTemplateSpecializationType(Context, FromTemplateType);
1982 const TemplateSpecializationType *ToOrigTST =
1983 GetTemplateSpecializationType(Context, ToTemplateType);
1985 // Only checking templates.
1986 if (!FromOrigTST || !ToOrigTST)
1989 // Different base templates.
1990 if (!hasSameTemplate(FromOrigTST, ToOrigTST)) {
1994 FromQual -= QualType(FromOrigTST, 0).getQualifiers();
1995 ToQual -= QualType(ToOrigTST, 0).getQualifiers();
1997 // Same base template, but different arguments.
1998 Tree.SetTemplateDiff(FromOrigTST->getTemplateName().getAsTemplateDecl(),
1999 ToOrigTST->getTemplateName().getAsTemplateDecl(),
2000 FromQual, ToQual, false /*FromDefault*/,
2001 false /*ToDefault*/);
2003 DiffTemplate(FromOrigTST, ToOrigTST);
2006 /// Emit - When the two types given are templated types with the same
2007 /// base template, a string representation of the type difference will be
2008 /// emitted to the stream and return true. Otherwise, return false.
2010 Tree.StartTraverse();
2015 assert(!IsBold && "Bold is applied to end of string.");
2018 }; // end class TemplateDiff
2021 /// FormatTemplateTypeDiff - A helper static function to start the template
2022 /// diff and return the properly formatted string. Returns true if the diff
2024 static bool FormatTemplateTypeDiff(ASTContext &Context, QualType FromType,
2025 QualType ToType, bool PrintTree,
2026 bool PrintFromType, bool ElideType,
2027 bool ShowColors, raw_ostream &OS) {
2029 PrintFromType = true;
2030 TemplateDiff TD(OS, Context, FromType, ToType, PrintTree, PrintFromType,
2031 ElideType, ShowColors);