blob: f49612507313aa64114697a2a6a06622ab876b38 [file] [log] [blame]
Douglas Gregor639cccc2010-02-09 22:26:47 +00001//===--- ASTDiagnostic.cpp - Diagnostic Printing Hooks for AST Nodes ------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file implements a diagnostic formatting hook for AST elements.
11//
12//===----------------------------------------------------------------------===//
Eugene Zelenko0a4f3f42016-02-10 19:11:58 +000013
Douglas Gregor639cccc2010-02-09 22:26:47 +000014#include "clang/AST/ASTDiagnostic.h"
Douglas Gregor639cccc2010-02-09 22:26:47 +000015#include "clang/AST/ASTContext.h"
Alp Tokerfb8d02b2014-06-05 22:10:59 +000016#include "clang/AST/ASTLambda.h"
Aaron Ballman3e424b52013-12-26 18:30:57 +000017#include "clang/AST/Attr.h"
Douglas Gregor639cccc2010-02-09 22:26:47 +000018#include "clang/AST/DeclObjC.h"
Richard Trieu91844232012-06-26 18:18:47 +000019#include "clang/AST/DeclTemplate.h"
Chandler Carruth3a022472012-12-04 09:13:33 +000020#include "clang/AST/ExprCXX.h"
21#include "clang/AST/TemplateBase.h"
Douglas Gregor639cccc2010-02-09 22:26:47 +000022#include "clang/AST/Type.h"
Richard Trieu91844232012-06-26 18:18:47 +000023#include "llvm/ADT/SmallString.h"
Douglas Gregor639cccc2010-02-09 22:26:47 +000024#include "llvm/Support/raw_ostream.h"
25
26using namespace clang;
27
Chandler Carruthd102f2d2010-05-13 11:37:24 +000028// Returns a desugared version of the QualType, and marks ShouldAKA as true
29// whenever we remove significant sugar from the type.
30static QualType Desugar(ASTContext &Context, QualType QT, bool &ShouldAKA) {
31 QualifierCollector QC;
32
Douglas Gregor639cccc2010-02-09 22:26:47 +000033 while (true) {
Chandler Carruthd102f2d2010-05-13 11:37:24 +000034 const Type *Ty = QC.strip(QT);
35
Douglas Gregor639cccc2010-02-09 22:26:47 +000036 // Don't aka just because we saw an elaborated type...
Richard Smith30482bc2011-02-20 03:19:35 +000037 if (const ElaboratedType *ET = dyn_cast<ElaboratedType>(Ty)) {
38 QT = ET->desugar();
Douglas Gregor639cccc2010-02-09 22:26:47 +000039 continue;
40 }
Abramo Bagnara924a8f32010-12-10 16:29:40 +000041 // ... or a paren type ...
Richard Smith30482bc2011-02-20 03:19:35 +000042 if (const ParenType *PT = dyn_cast<ParenType>(Ty)) {
43 QT = PT->desugar();
Abramo Bagnara924a8f32010-12-10 16:29:40 +000044 continue;
45 }
Richard Smith30482bc2011-02-20 03:19:35 +000046 // ...or a substituted template type parameter ...
47 if (const SubstTemplateTypeParmType *ST =
48 dyn_cast<SubstTemplateTypeParmType>(Ty)) {
49 QT = ST->desugar();
50 continue;
51 }
John McCall4223a9e2011-03-04 04:00:19 +000052 // ...or an attributed type...
53 if (const AttributedType *AT = dyn_cast<AttributedType>(Ty)) {
54 QT = AT->desugar();
55 continue;
56 }
Reid Kleckner0503a872013-12-05 01:23:43 +000057 // ...or an adjusted type...
58 if (const AdjustedType *AT = dyn_cast<AdjustedType>(Ty)) {
59 QT = AT->desugar();
60 continue;
61 }
Richard Smith30482bc2011-02-20 03:19:35 +000062 // ... or an auto type.
63 if (const AutoType *AT = dyn_cast<AutoType>(Ty)) {
64 if (!AT->isSugared())
65 break;
66 QT = AT->desugar();
Douglas Gregor639cccc2010-02-09 22:26:47 +000067 continue;
68 }
Chandler Carruthd102f2d2010-05-13 11:37:24 +000069
Nikola Smiljanicfa007282015-07-16 01:06:17 +000070 // Desugar FunctionType if return type or any parameter type should be
71 // desugared. Preserve nullability attribute on desugared types.
72 if (const FunctionType *FT = dyn_cast<FunctionType>(Ty)) {
73 bool DesugarReturn = false;
74 QualType SugarRT = FT->getReturnType();
75 QualType RT = Desugar(Context, SugarRT, DesugarReturn);
76 if (auto nullability = AttributedType::stripOuterNullability(SugarRT)) {
77 RT = Context.getAttributedType(
78 AttributedType::getNullabilityAttrKind(*nullability), RT, RT);
79 }
80
81 bool DesugarArgument = false;
82 SmallVector<QualType, 4> Args;
83 const FunctionProtoType *FPT = dyn_cast<FunctionProtoType>(FT);
84 if (FPT) {
85 for (QualType SugarPT : FPT->param_types()) {
86 QualType PT = Desugar(Context, SugarPT, DesugarArgument);
87 if (auto nullability =
88 AttributedType::stripOuterNullability(SugarPT)) {
89 PT = Context.getAttributedType(
90 AttributedType::getNullabilityAttrKind(*nullability), PT, PT);
91 }
92 Args.push_back(PT);
93 }
94 }
95
96 if (DesugarReturn || DesugarArgument) {
97 ShouldAKA = true;
98 QT = FPT ? Context.getFunctionType(RT, Args, FPT->getExtProtoInfo())
99 : Context.getFunctionNoProtoType(RT, FT->getExtInfo());
Richard Smith3f1b5d02011-05-05 21:57:07 +0000100 break;
Nikola Smiljanicfa007282015-07-16 01:06:17 +0000101 }
102 }
103
104 // Desugar template specializations if any template argument should be
105 // desugared.
106 if (const TemplateSpecializationType *TST =
107 dyn_cast<TemplateSpecializationType>(Ty)) {
108 if (!TST->isTypeAlias()) {
109 bool DesugarArgument = false;
110 SmallVector<TemplateArgument, 4> Args;
111 for (unsigned I = 0, N = TST->getNumArgs(); I != N; ++I) {
112 const TemplateArgument &Arg = TST->getArg(I);
113 if (Arg.getKind() == TemplateArgument::Type)
114 Args.push_back(Desugar(Context, Arg.getAsType(), DesugarArgument));
115 else
116 Args.push_back(Arg);
117 }
118
119 if (DesugarArgument) {
120 ShouldAKA = true;
121 QT = Context.getTemplateSpecializationType(
122 TST->getTemplateName(), Args.data(), Args.size(), QT);
123 }
124 break;
125 }
126 }
Chandler Carruthd102f2d2010-05-13 11:37:24 +0000127
Douglas Gregor639cccc2010-02-09 22:26:47 +0000128 // Don't desugar magic Objective-C types.
129 if (QualType(Ty,0) == Context.getObjCIdType() ||
130 QualType(Ty,0) == Context.getObjCClassType() ||
131 QualType(Ty,0) == Context.getObjCSelType() ||
132 QualType(Ty,0) == Context.getObjCProtoType())
133 break;
Chandler Carruthd102f2d2010-05-13 11:37:24 +0000134
Douglas Gregor639cccc2010-02-09 22:26:47 +0000135 // Don't desugar va_list.
Charles Davisc7d5c942015-09-17 20:55:33 +0000136 if (QualType(Ty, 0) == Context.getBuiltinVaListType() ||
137 QualType(Ty, 0) == Context.getBuiltinMSVaListType())
Douglas Gregor639cccc2010-02-09 22:26:47 +0000138 break;
Chandler Carruthd102f2d2010-05-13 11:37:24 +0000139
Douglas Gregor639cccc2010-02-09 22:26:47 +0000140 // Otherwise, do a single-step desugar.
141 QualType Underlying;
142 bool IsSugar = false;
143 switch (Ty->getTypeClass()) {
144#define ABSTRACT_TYPE(Class, Base)
145#define TYPE(Class, Base) \
146case Type::Class: { \
147const Class##Type *CTy = cast<Class##Type>(Ty); \
148if (CTy->isSugared()) { \
149IsSugar = true; \
150Underlying = CTy->desugar(); \
151} \
152break; \
153}
154#include "clang/AST/TypeNodes.def"
155 }
Chandler Carruthd102f2d2010-05-13 11:37:24 +0000156
Douglas Gregor639cccc2010-02-09 22:26:47 +0000157 // If it wasn't sugared, we're done.
158 if (!IsSugar)
159 break;
Chandler Carruthd102f2d2010-05-13 11:37:24 +0000160
Douglas Gregor639cccc2010-02-09 22:26:47 +0000161 // If the desugared type is a vector type, we don't want to expand
162 // it, it will turn into an attribute mess. People want their "vec4".
163 if (isa<VectorType>(Underlying))
164 break;
Chandler Carruthd102f2d2010-05-13 11:37:24 +0000165
Douglas Gregor639cccc2010-02-09 22:26:47 +0000166 // Don't desugar through the primary typedef of an anonymous type.
Chris Lattneredbdff62010-09-04 23:16:01 +0000167 if (const TagType *UTT = Underlying->getAs<TagType>())
168 if (const TypedefType *QTT = dyn_cast<TypedefType>(QT))
Richard Smithdda56e42011-04-15 14:24:37 +0000169 if (UTT->getDecl()->getTypedefNameForAnonDecl() == QTT->getDecl())
Chris Lattneredbdff62010-09-04 23:16:01 +0000170 break;
Chandler Carruthd102f2d2010-05-13 11:37:24 +0000171
172 // Record that we actually looked through an opaque type here.
173 ShouldAKA = true;
Douglas Gregor639cccc2010-02-09 22:26:47 +0000174 QT = Underlying;
Douglas Gregor639cccc2010-02-09 22:26:47 +0000175 }
Chandler Carruthd102f2d2010-05-13 11:37:24 +0000176
177 // If we have a pointer-like type, desugar the pointee as well.
178 // FIXME: Handle other pointer-like types.
179 if (const PointerType *Ty = QT->getAs<PointerType>()) {
Chris Lattneredbdff62010-09-04 23:16:01 +0000180 QT = Context.getPointerType(Desugar(Context, Ty->getPointeeType(),
181 ShouldAKA));
Douglas Gregore9d95f12015-07-07 03:57:35 +0000182 } else if (const auto *Ty = QT->getAs<ObjCObjectPointerType>()) {
183 QT = Context.getObjCObjectPointerType(Desugar(Context, Ty->getPointeeType(),
184 ShouldAKA));
Chandler Carruthd102f2d2010-05-13 11:37:24 +0000185 } else if (const LValueReferenceType *Ty = QT->getAs<LValueReferenceType>()) {
Chris Lattneredbdff62010-09-04 23:16:01 +0000186 QT = Context.getLValueReferenceType(Desugar(Context, Ty->getPointeeType(),
187 ShouldAKA));
Douglas Gregor7a2a1162011-01-20 16:08:06 +0000188 } else if (const RValueReferenceType *Ty = QT->getAs<RValueReferenceType>()) {
189 QT = Context.getRValueReferenceType(Desugar(Context, Ty->getPointeeType(),
190 ShouldAKA));
Douglas Gregore9d95f12015-07-07 03:57:35 +0000191 } else if (const auto *Ty = QT->getAs<ObjCObjectType>()) {
192 if (Ty->getBaseType().getTypePtr() != Ty && !ShouldAKA) {
193 QualType BaseType = Desugar(Context, Ty->getBaseType(), ShouldAKA);
194 QT = Context.getObjCObjectType(BaseType, Ty->getTypeArgsAsWritten(),
195 llvm::makeArrayRef(Ty->qual_begin(),
Douglas Gregorab209d82015-07-07 03:58:42 +0000196 Ty->getNumProtocols()),
197 Ty->isKindOfTypeAsWritten());
Douglas Gregore9d95f12015-07-07 03:57:35 +0000198 }
Douglas Gregor639cccc2010-02-09 22:26:47 +0000199 }
Chandler Carruthd102f2d2010-05-13 11:37:24 +0000200
John McCall717d9b02010-12-10 11:01:00 +0000201 return QC.apply(Context, QT);
Douglas Gregor639cccc2010-02-09 22:26:47 +0000202}
203
204/// \brief Convert the given type to a string suitable for printing as part of
Chandler Carruthd102f2d2010-05-13 11:37:24 +0000205/// a diagnostic.
206///
Chandler Carruthd5173952011-07-11 17:49:21 +0000207/// There are four main criteria when determining whether we should have an
Chandler Carruthd102f2d2010-05-13 11:37:24 +0000208/// a.k.a. clause when pretty-printing a type:
209///
210/// 1) Some types provide very minimal sugar that doesn't impede the
211/// user's understanding --- for example, elaborated type
212/// specifiers. If this is all the sugar we see, we don't want an
213/// a.k.a. clause.
214/// 2) Some types are technically sugared but are much more familiar
215/// when seen in their sugared form --- for example, va_list,
216/// vector types, and the magic Objective C types. We don't
217/// want to desugar these, even if we do produce an a.k.a. clause.
218/// 3) Some types may have already been desugared previously in this diagnostic.
219/// if this is the case, doing another "aka" would just be clutter.
Chandler Carruthd5173952011-07-11 17:49:21 +0000220/// 4) Two different types within the same diagnostic have the same output
221/// string. In this case, force an a.k.a with the desugared type when
222/// doing so will provide additional information.
Douglas Gregor639cccc2010-02-09 22:26:47 +0000223///
224/// \param Context the context in which the type was allocated
225/// \param Ty the type to print
Chandler Carruthd5173952011-07-11 17:49:21 +0000226/// \param QualTypeVals pointer values to QualTypes which are used in the
227/// diagnostic message
Douglas Gregor639cccc2010-02-09 22:26:47 +0000228static std::string
229ConvertTypeToDiagnosticString(ASTContext &Context, QualType Ty,
Craig Toppere4753502014-06-12 05:32:27 +0000230 ArrayRef<DiagnosticsEngine::ArgumentValue> PrevArgs,
231 ArrayRef<intptr_t> QualTypeVals) {
Douglas Gregor639cccc2010-02-09 22:26:47 +0000232 // FIXME: Playing with std::string is really slow.
Chandler Carruthd5173952011-07-11 17:49:21 +0000233 bool ForceAKA = false;
234 QualType CanTy = Ty.getCanonicalType();
Douglas Gregorc0b07282011-09-27 22:38:19 +0000235 std::string S = Ty.getAsString(Context.getPrintingPolicy());
236 std::string CanS = CanTy.getAsString(Context.getPrintingPolicy());
Chandler Carruthd5173952011-07-11 17:49:21 +0000237
Bill Wendling8eb771d2012-02-22 09:51:33 +0000238 for (unsigned I = 0, E = QualTypeVals.size(); I != E; ++I) {
Chandler Carruthd5173952011-07-11 17:49:21 +0000239 QualType CompareTy =
Bill Wendling8eb771d2012-02-22 09:51:33 +0000240 QualType::getFromOpaquePtr(reinterpret_cast<void*>(QualTypeVals[I]));
Richard Smithbcc22fc2012-03-09 08:00:36 +0000241 if (CompareTy.isNull())
242 continue;
Chandler Carruthd5173952011-07-11 17:49:21 +0000243 if (CompareTy == Ty)
244 continue; // Same types
245 QualType CompareCanTy = CompareTy.getCanonicalType();
246 if (CompareCanTy == CanTy)
247 continue; // Same canonical types
Douglas Gregorc0b07282011-09-27 22:38:19 +0000248 std::string CompareS = CompareTy.getAsString(Context.getPrintingPolicy());
Justin Bogner25bf8cb2015-07-08 18:32:26 +0000249 bool ShouldAKA = false;
250 QualType CompareDesugar = Desugar(Context, CompareTy, ShouldAKA);
Richard Trieu5d1aff02011-11-14 19:39:25 +0000251 std::string CompareDesugarStr =
252 CompareDesugar.getAsString(Context.getPrintingPolicy());
253 if (CompareS != S && CompareDesugarStr != S)
254 continue; // The type string is different than the comparison string
255 // and the desugared comparison string.
256 std::string CompareCanS =
257 CompareCanTy.getAsString(Context.getPrintingPolicy());
258
Chandler Carruthd5173952011-07-11 17:49:21 +0000259 if (CompareCanS == CanS)
260 continue; // No new info from canonical type
261
262 ForceAKA = true;
263 break;
264 }
Chandler Carruthd102f2d2010-05-13 11:37:24 +0000265
266 // Check to see if we already desugared this type in this
267 // diagnostic. If so, don't do it again.
268 bool Repeated = false;
Craig Toppere4753502014-06-12 05:32:27 +0000269 for (unsigned i = 0, e = PrevArgs.size(); i != e; ++i) {
Chandler Carruthd102f2d2010-05-13 11:37:24 +0000270 // TODO: Handle ak_declcontext case.
David Blaikie9c902b52011-09-25 23:23:43 +0000271 if (PrevArgs[i].first == DiagnosticsEngine::ak_qualtype) {
Chandler Carruthd102f2d2010-05-13 11:37:24 +0000272 void *Ptr = (void*)PrevArgs[i].second;
273 QualType PrevTy(QualType::getFromOpaquePtr(Ptr));
274 if (PrevTy == Ty) {
275 Repeated = true;
276 break;
277 }
278 }
279 }
280
Douglas Gregor639cccc2010-02-09 22:26:47 +0000281 // Consider producing an a.k.a. clause if removing all the direct
282 // sugar gives us something "significantly different".
Chandler Carruthd102f2d2010-05-13 11:37:24 +0000283 if (!Repeated) {
284 bool ShouldAKA = false;
285 QualType DesugaredTy = Desugar(Context, Ty, ShouldAKA);
Chandler Carruthd5173952011-07-11 17:49:21 +0000286 if (ShouldAKA || ForceAKA) {
287 if (DesugaredTy == Ty) {
288 DesugaredTy = Ty.getCanonicalType();
289 }
Douglas Gregorc0b07282011-09-27 22:38:19 +0000290 std::string akaStr = DesugaredTy.getAsString(Context.getPrintingPolicy());
Chandler Carruthd5173952011-07-11 17:49:21 +0000291 if (akaStr != S) {
292 S = "'" + S + "' (aka '" + akaStr + "')";
293 return S;
294 }
Chandler Carruthd102f2d2010-05-13 11:37:24 +0000295 }
Benjamin Kramer1adc8c32014-04-25 20:41:38 +0000296
297 // Give some additional info on vector types. These are either not desugared
298 // or displaying complex __attribute__ expressions so add details of the
299 // type and element count.
300 if (Ty->isVectorType()) {
301 const VectorType *VTy = Ty->getAs<VectorType>();
302 std::string DecoratedString;
303 llvm::raw_string_ostream OS(DecoratedString);
304 const char *Values = VTy->getNumElements() > 1 ? "values" : "value";
305 OS << "'" << S << "' (vector of " << VTy->getNumElements() << " '"
306 << VTy->getElementType().getAsString(Context.getPrintingPolicy())
307 << "' " << Values << ")";
308 return OS.str();
309 }
Douglas Gregor639cccc2010-02-09 22:26:47 +0000310 }
Chandler Carruthd102f2d2010-05-13 11:37:24 +0000311
Douglas Gregor639cccc2010-02-09 22:26:47 +0000312 S = "'" + S + "'";
313 return S;
314}
315
Richard Trieu91844232012-06-26 18:18:47 +0000316static bool FormatTemplateTypeDiff(ASTContext &Context, QualType FromType,
317 QualType ToType, bool PrintTree,
318 bool PrintFromType, bool ElideType,
Benjamin Kramer8de90462013-02-22 16:08:12 +0000319 bool ShowColors, raw_ostream &OS);
Richard Trieu91844232012-06-26 18:18:47 +0000320
Chandler Carruthd5173952011-07-11 17:49:21 +0000321void clang::FormatASTNodeDiagnosticArgument(
David Blaikie9c902b52011-09-25 23:23:43 +0000322 DiagnosticsEngine::ArgumentKind Kind,
Chandler Carruthd5173952011-07-11 17:49:21 +0000323 intptr_t Val,
Craig Topper3aa4fb32014-06-12 05:32:35 +0000324 StringRef Modifier,
325 StringRef Argument,
Craig Toppere4753502014-06-12 05:32:27 +0000326 ArrayRef<DiagnosticsEngine::ArgumentValue> PrevArgs,
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000327 SmallVectorImpl<char> &Output,
Chandler Carruthd5173952011-07-11 17:49:21 +0000328 void *Cookie,
Bill Wendling8eb771d2012-02-22 09:51:33 +0000329 ArrayRef<intptr_t> QualTypeVals) {
Douglas Gregor639cccc2010-02-09 22:26:47 +0000330 ASTContext &Context = *static_cast<ASTContext*>(Cookie);
331
Benjamin Kramer7192c9e2013-02-22 15:46:08 +0000332 size_t OldEnd = Output.size();
333 llvm::raw_svector_ostream OS(Output);
Douglas Gregor639cccc2010-02-09 22:26:47 +0000334 bool NeedQuotes = true;
335
336 switch (Kind) {
David Blaikie83d382b2011-09-23 05:06:16 +0000337 default: llvm_unreachable("unknown ArgumentKind");
Richard Trieu91844232012-06-26 18:18:47 +0000338 case DiagnosticsEngine::ak_qualtype_pair: {
Richard Trieu50f5f462012-07-10 01:46:04 +0000339 TemplateDiffTypes &TDT = *reinterpret_cast<TemplateDiffTypes*>(Val);
Richard Trieu91844232012-06-26 18:18:47 +0000340 QualType FromType =
341 QualType::getFromOpaquePtr(reinterpret_cast<void*>(TDT.FromType));
342 QualType ToType =
343 QualType::getFromOpaquePtr(reinterpret_cast<void*>(TDT.ToType));
344
345 if (FormatTemplateTypeDiff(Context, FromType, ToType, TDT.PrintTree,
346 TDT.PrintFromType, TDT.ElideType,
Benjamin Kramer8de90462013-02-22 16:08:12 +0000347 TDT.ShowColors, OS)) {
Richard Trieu91844232012-06-26 18:18:47 +0000348 NeedQuotes = !TDT.PrintTree;
Richard Trieu50f5f462012-07-10 01:46:04 +0000349 TDT.TemplateDiffUsed = true;
Richard Trieu91844232012-06-26 18:18:47 +0000350 break;
351 }
352
353 // Don't fall-back during tree printing. The caller will handle
354 // this case.
355 if (TDT.PrintTree)
356 return;
357
Benjamin Kramer7192c9e2013-02-22 15:46:08 +0000358 // Attempting to do a template diff on non-templates. Set the variables
Richard Trieu91844232012-06-26 18:18:47 +0000359 // and continue with regular type printing of the appropriate type.
360 Val = TDT.PrintFromType ? TDT.FromType : TDT.ToType;
Craig Topper3aa4fb32014-06-12 05:32:35 +0000361 Modifier = StringRef();
362 Argument = StringRef();
Richard Trieu91844232012-06-26 18:18:47 +0000363 // Fall through
364 }
David Blaikie9c902b52011-09-25 23:23:43 +0000365 case DiagnosticsEngine::ak_qualtype: {
Craig Topper3aa4fb32014-06-12 05:32:35 +0000366 assert(Modifier.empty() && Argument.empty() &&
Douglas Gregor639cccc2010-02-09 22:26:47 +0000367 "Invalid modifier for QualType argument");
368
369 QualType Ty(QualType::getFromOpaquePtr(reinterpret_cast<void*>(Val)));
Craig Toppere4753502014-06-12 05:32:27 +0000370 OS << ConvertTypeToDiagnosticString(Context, Ty, PrevArgs, QualTypeVals);
Douglas Gregor639cccc2010-02-09 22:26:47 +0000371 NeedQuotes = false;
372 break;
373 }
David Blaikie9c902b52011-09-25 23:23:43 +0000374 case DiagnosticsEngine::ak_declarationname: {
Craig Topper3aa4fb32014-06-12 05:32:35 +0000375 if (Modifier == "objcclass" && Argument.empty())
Benjamin Kramer7192c9e2013-02-22 15:46:08 +0000376 OS << '+';
Craig Topper3aa4fb32014-06-12 05:32:35 +0000377 else if (Modifier == "objcinstance" && Argument.empty())
Benjamin Kramer7192c9e2013-02-22 15:46:08 +0000378 OS << '-';
Douglas Gregor639cccc2010-02-09 22:26:47 +0000379 else
Craig Topper3aa4fb32014-06-12 05:32:35 +0000380 assert(Modifier.empty() && Argument.empty() &&
Douglas Gregor639cccc2010-02-09 22:26:47 +0000381 "Invalid modifier for DeclarationName argument");
Benjamin Kramer7192c9e2013-02-22 15:46:08 +0000382
David Blaikied4da8722013-05-14 21:04:00 +0000383 OS << DeclarationName::getFromOpaqueInteger(Val);
Douglas Gregor639cccc2010-02-09 22:26:47 +0000384 break;
385 }
David Blaikie9c902b52011-09-25 23:23:43 +0000386 case DiagnosticsEngine::ak_nameddecl: {
Douglas Gregor639cccc2010-02-09 22:26:47 +0000387 bool Qualified;
Craig Topper3aa4fb32014-06-12 05:32:35 +0000388 if (Modifier == "q" && Argument.empty())
Douglas Gregor639cccc2010-02-09 22:26:47 +0000389 Qualified = true;
390 else {
Craig Topper3aa4fb32014-06-12 05:32:35 +0000391 assert(Modifier.empty() && Argument.empty() &&
Douglas Gregor639cccc2010-02-09 22:26:47 +0000392 "Invalid modifier for NamedDecl* argument");
393 Qualified = false;
394 }
Chandler Carruthc841b6e2011-08-31 09:01:53 +0000395 const NamedDecl *ND = reinterpret_cast<const NamedDecl*>(Val);
Benjamin Kramer9170e912013-02-22 15:46:01 +0000396 ND->getNameForDiagnostic(OS, Context.getPrintingPolicy(), Qualified);
Douglas Gregor639cccc2010-02-09 22:26:47 +0000397 break;
398 }
David Blaikie9c902b52011-09-25 23:23:43 +0000399 case DiagnosticsEngine::ak_nestednamespec: {
Benjamin Kramer7192c9e2013-02-22 15:46:08 +0000400 NestedNameSpecifier *NNS = reinterpret_cast<NestedNameSpecifier*>(Val);
401 NNS->print(OS, Context.getPrintingPolicy());
Douglas Gregor639cccc2010-02-09 22:26:47 +0000402 NeedQuotes = false;
403 break;
404 }
David Blaikie9c902b52011-09-25 23:23:43 +0000405 case DiagnosticsEngine::ak_declcontext: {
Douglas Gregor639cccc2010-02-09 22:26:47 +0000406 DeclContext *DC = reinterpret_cast<DeclContext *> (Val);
407 assert(DC && "Should never have a null declaration context");
Alp Tokerfb8d02b2014-06-05 22:10:59 +0000408 NeedQuotes = false;
409
Richard Trieu3af6c102014-08-27 03:05:19 +0000410 // FIXME: Get the strings for DeclContext from some localized place
Douglas Gregor639cccc2010-02-09 22:26:47 +0000411 if (DC->isTranslationUnit()) {
David Blaikiebbafb8a2012-03-11 07:00:24 +0000412 if (Context.getLangOpts().CPlusPlus)
Benjamin Kramer7192c9e2013-02-22 15:46:08 +0000413 OS << "the global namespace";
Douglas Gregor639cccc2010-02-09 22:26:47 +0000414 else
Benjamin Kramer7192c9e2013-02-22 15:46:08 +0000415 OS << "the global scope";
Richard Trieu3af6c102014-08-27 03:05:19 +0000416 } else if (DC->isClosure()) {
417 OS << "block literal";
418 } else if (isLambdaCallOperator(DC)) {
419 OS << "lambda expression";
Douglas Gregor639cccc2010-02-09 22:26:47 +0000420 } else if (TypeDecl *Type = dyn_cast<TypeDecl>(DC)) {
Benjamin Kramer7192c9e2013-02-22 15:46:08 +0000421 OS << ConvertTypeToDiagnosticString(Context,
422 Context.getTypeDeclType(Type),
Craig Toppere4753502014-06-12 05:32:27 +0000423 PrevArgs, QualTypeVals);
Douglas Gregor639cccc2010-02-09 22:26:47 +0000424 } else {
Richard Trieu3af6c102014-08-27 03:05:19 +0000425 assert(isa<NamedDecl>(DC) && "Expected a NamedDecl");
Douglas Gregor639cccc2010-02-09 22:26:47 +0000426 NamedDecl *ND = cast<NamedDecl>(DC);
427 if (isa<NamespaceDecl>(ND))
Benjamin Kramer7192c9e2013-02-22 15:46:08 +0000428 OS << "namespace ";
Douglas Gregor639cccc2010-02-09 22:26:47 +0000429 else if (isa<ObjCMethodDecl>(ND))
Benjamin Kramer7192c9e2013-02-22 15:46:08 +0000430 OS << "method ";
Douglas Gregor639cccc2010-02-09 22:26:47 +0000431 else if (isa<FunctionDecl>(ND))
Benjamin Kramer7192c9e2013-02-22 15:46:08 +0000432 OS << "function ";
433
434 OS << '\'';
435 ND->getNameForDiagnostic(OS, Context.getPrintingPolicy(), true);
436 OS << '\'';
Douglas Gregor639cccc2010-02-09 22:26:47 +0000437 }
Douglas Gregor639cccc2010-02-09 22:26:47 +0000438 break;
439 }
David Majnemerb1004102014-03-02 18:46:05 +0000440 case DiagnosticsEngine::ak_attr: {
441 const Attr *At = reinterpret_cast<Attr *>(Val);
442 assert(At && "Received null Attr object!");
443 OS << '\'' << At->getSpelling() << '\'';
444 NeedQuotes = false;
445 break;
446 }
Douglas Gregor639cccc2010-02-09 22:26:47 +0000447 }
Benjamin Kramer7192c9e2013-02-22 15:46:08 +0000448
Benjamin Kramer7192c9e2013-02-22 15:46:08 +0000449 if (NeedQuotes) {
450 Output.insert(Output.begin()+OldEnd, '\'');
Douglas Gregor639cccc2010-02-09 22:26:47 +0000451 Output.push_back('\'');
Benjamin Kramer7192c9e2013-02-22 15:46:08 +0000452 }
Douglas Gregor639cccc2010-02-09 22:26:47 +0000453}
Richard Trieu91844232012-06-26 18:18:47 +0000454
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.
459namespace {
460class TemplateDiff {
461 /// Context - The ASTContext which is used for comparing template arguments.
462 ASTContext &Context;
463
464 /// Policy - Used during expression printing.
465 PrintingPolicy Policy;
466
467 /// ElideType - Option to elide identical types.
468 bool ElideType;
469
470 /// PrintTree - Format output string as a tree.
471 bool PrintTree;
472
473 /// ShowColor - Diagnostics support color, so bolding will be used.
474 bool ShowColor;
475
Richard Trieu2331c8b2016-01-15 05:48:38 +0000476 /// 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;
Richard Trieu91844232012-06-26 18:18:47 +0000480
Richard Trieu2331c8b2016-01-15 05:48:38 +0000481 /// ToTemplateType - The type that FromType is compared to. Only in tree
482 /// printing will this type be outputed.
483 QualType ToTemplateType;
Richard Trieu91844232012-06-26 18:18:47 +0000484
Richard Trieu91844232012-06-26 18:18:47 +0000485 /// OS - The stream used to construct the output strings.
Benjamin Kramer8de90462013-02-22 16:08:12 +0000486 raw_ostream &OS;
Richard Trieu91844232012-06-26 18:18:47 +0000487
488 /// IsBold - Keeps track of the bold formatting for the output string.
489 bool IsBold;
490
491 /// DiffTree - A tree representation the differences between two types.
492 class DiffTree {
Richard Trieub4cff112013-03-15 20:35:18 +0000493 public:
Richard Trieu14714c42016-01-14 22:56:39 +0000494 /// DiffKind - The difference in a DiffNode. Fields of
495 /// TemplateArgumentInfo needed by each difference can be found in the
496 /// Set* and Get* functions.
Richard Trieub4cff112013-03-15 20:35:18 +0000497 enum DiffKind {
498 /// Incomplete or invalid node.
499 Invalid,
Richard Trieu12074502016-02-02 00:36:59 +0000500 /// Another level of templates
Richard Trieub4cff112013-03-15 20:35:18 +0000501 Template,
Richard Trieu14714c42016-01-14 22:56:39 +0000502 /// Type difference, all type differences except those falling under
503 /// the Template difference.
Richard Trieub4cff112013-03-15 20:35:18 +0000504 Type,
Richard Trieu14714c42016-01-14 22:56:39 +0000505 /// 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.
Richard Trieub4cff112013-03-15 20:35:18 +0000508 Expression,
Richard Trieu14714c42016-01-14 22:56:39 +0000509 /// Template argument difference
Richard Trieub4cff112013-03-15 20:35:18 +0000510 TemplateTemplate,
Richard Trieu14714c42016-01-14 22:56:39 +0000511 /// Integer difference
Richard Trieub4cff112013-03-15 20:35:18 +0000512 Integer,
Richard Trieu14714c42016-01-14 22:56:39 +0000513 /// Declaration difference, nullptr arguments are included here
Richard Trieu9213ce52016-01-15 05:01:53 +0000514 Declaration,
515 /// One argument being integer and the other being declaration
516 FromIntegerAndToDeclaration,
517 FromDeclarationAndToInteger
Richard Trieub4cff112013-03-15 20:35:18 +0000518 };
Richard Trieu14714c42016-01-14 22:56:39 +0000519
Richard Trieub4cff112013-03-15 20:35:18 +0000520 private:
Richard Trieu14714c42016-01-14 22:56:39 +0000521 /// 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 {
525 QualType ArgType;
526 Qualifiers Qual;
527 llvm::APSInt Val;
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;
535 };
536
Richard Trieu91844232012-06-26 18:18:47 +0000537 /// 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.
540 struct DiffNode {
Richard Trieu14714c42016-01-14 22:56:39 +0000541 DiffKind Kind = Invalid;
Richard Trieub4cff112013-03-15 20:35:18 +0000542
Richard Trieu91844232012-06-26 18:18:47 +0000543 /// NextNode - The index of the next sibling node or 0.
Richard Trieu14714c42016-01-14 22:56:39 +0000544 unsigned NextNode = 0;
Richard Trieu91844232012-06-26 18:18:47 +0000545
546 /// ChildNode - The index of the first child node or 0.
Richard Trieu14714c42016-01-14 22:56:39 +0000547 unsigned ChildNode = 0;
Richard Trieu91844232012-06-26 18:18:47 +0000548
549 /// ParentNode - The index of the parent node.
Richard Trieu14714c42016-01-14 22:56:39 +0000550 unsigned ParentNode = 0;
Richard Trieu91844232012-06-26 18:18:47 +0000551
Richard Trieu14714c42016-01-14 22:56:39 +0000552 TemplateArgumentInfo FromArgInfo, ToArgInfo;
Richard Trieu91844232012-06-26 18:18:47 +0000553
554 /// Same - Whether the two arguments evaluate to the same value.
Richard Trieu14714c42016-01-14 22:56:39 +0000555 bool Same = false;
Richard Trieu91844232012-06-26 18:18:47 +0000556
Richard Trieu14714c42016-01-14 22:56:39 +0000557 DiffNode(unsigned ParentNode = 0) : ParentNode(ParentNode) {}
Richard Trieu91844232012-06-26 18:18:47 +0000558 };
559
560 /// FlatTree - A flattened tree used to store the DiffNodes.
Dmitri Gribenkof8579502013-01-12 19:30:44 +0000561 SmallVector<DiffNode, 16> FlatTree;
Richard Trieu91844232012-06-26 18:18:47 +0000562
563 /// CurrentNode - The index of the current node being used.
564 unsigned CurrentNode;
565
566 /// NextFreeNode - The index of the next unused node. Used when creating
567 /// child nodes.
568 unsigned NextFreeNode;
569
570 /// ReadNode - The index of the current node being read.
571 unsigned ReadNode;
Richard Trieu14714c42016-01-14 22:56:39 +0000572
Richard Trieu91844232012-06-26 18:18:47 +0000573 public:
574 DiffTree() :
575 CurrentNode(0), NextFreeNode(1) {
576 FlatTree.push_back(DiffNode());
577 }
578
Richard Trieu14714c42016-01-14 22:56:39 +0000579 // 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);
Richard Trieu91844232012-06-26 18:18:47 +0000590 }
591
Richard Trieu14714c42016-01-14 22:56:39 +0000592 void SetTypeDiff(QualType FromType, QualType ToType, bool FromDefault,
593 bool ToDefault) {
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);
Richard Trieu91844232012-06-26 18:18:47 +0000599 }
600
Richard Trieu2c22a862016-01-15 01:08:56 +0000601 void SetExpressionDiff(Expr *FromExpr, Expr *ToExpr, bool FromDefault,
602 bool ToDefault) {
Richard Trieu14714c42016-01-14 22:56:39 +0000603 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;
Richard Trieu14714c42016-01-14 22:56:39 +0000607 SetDefault(FromDefault, ToDefault);
Richard Trieu91844232012-06-26 18:18:47 +0000608 }
609
Richard Trieu14714c42016-01-14 22:56:39 +0000610 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);
Richard Trieu6df89452012-11-01 21:29:28 +0000617 }
618
Benjamin Kramer7320b992016-06-15 14:20:56 +0000619 void SetIntegerDiff(const llvm::APSInt &FromInt, const llvm::APSInt &ToInt,
Richard Trieu14714c42016-01-14 22:56:39 +0000620 bool IsValidFromInt, bool IsValidToInt,
Richard Trieud5c73782016-01-15 02:55:17 +0000621 QualType FromIntType, QualType ToIntType,
Richard Trieu14714c42016-01-14 22:56:39 +0000622 Expr *FromExpr, Expr *ToExpr, bool FromDefault,
623 bool ToDefault) {
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;
Richard Trieud5c73782016-01-15 02:55:17 +0000630 FlatTree[CurrentNode].FromArgInfo.ArgType = FromIntType;
631 FlatTree[CurrentNode].ToArgInfo.ArgType = ToIntType;
Richard Trieu14714c42016-01-14 22:56:39 +0000632 FlatTree[CurrentNode].FromArgInfo.ArgExpr = FromExpr;
633 FlatTree[CurrentNode].ToArgInfo.ArgExpr = ToExpr;
634 SetDefault(FromDefault, ToDefault);
Richard Trieub7243852012-09-28 20:32:51 +0000635 }
636
Richard Trieu14714c42016-01-14 22:56:39 +0000637 void SetDeclarationDiff(ValueDecl *FromValueDecl, ValueDecl *ToValueDecl,
638 bool FromAddressOf, bool ToAddressOf,
Richard Trieu2c22a862016-01-15 01:08:56 +0000639 bool FromNullPtr, bool ToNullPtr, Expr *FromExpr,
640 Expr *ToExpr, bool FromDefault, bool ToDefault) {
Richard Trieu14714c42016-01-14 22:56:39 +0000641 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;
Richard Trieu2c22a862016-01-15 01:08:56 +0000649 FlatTree[CurrentNode].FromArgInfo.ArgExpr = FromExpr;
650 FlatTree[CurrentNode].ToArgInfo.ArgExpr = ToExpr;
Richard Trieu14714c42016-01-14 22:56:39 +0000651 SetDefault(FromDefault, ToDefault);
652 }
653
Richard Trieu9213ce52016-01-15 05:01:53 +0000654 void SetFromDeclarationAndToIntegerDiff(
655 ValueDecl *FromValueDecl, bool FromAddressOf, bool FromNullPtr,
Benjamin Kramer7320b992016-06-15 14:20:56 +0000656 Expr *FromExpr, const llvm::APSInt &ToInt, bool IsValidToInt,
Richard Trieu9213ce52016-01-15 05:01:53 +0000657 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);
669 }
670
671 void SetFromIntegerAndToDeclarationDiff(
Benjamin Kramer7320b992016-06-15 14:20:56 +0000672 const llvm::APSInt &FromInt, bool IsValidFromInt, QualType FromIntType,
Richard Trieu9213ce52016-01-15 05:01:53 +0000673 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);
686 }
687
Richard Trieu14714c42016-01-14 22:56:39 +0000688 /// SetDefault - Sets FromDefault and ToDefault flags of the current node.
689 void SetDefault(bool FromDefault, bool ToDefault) {
James Y Knight925d60e2016-01-15 05:57:41 +0000690 assert((!FromDefault || !ToDefault) && "Both arguments cannot be default.");
Richard Trieu14714c42016-01-14 22:56:39 +0000691 FlatTree[CurrentNode].FromArgInfo.IsDefault = FromDefault;
692 FlatTree[CurrentNode].ToArgInfo.IsDefault = ToDefault;
Richard Trieu954aaaf2013-02-27 01:41:53 +0000693 }
694
Richard Trieu91844232012-06-26 18:18:47 +0000695 /// SetSame - Sets the same flag of the current node.
696 void SetSame(bool Same) {
697 FlatTree[CurrentNode].Same = Same;
698 }
699
Richard Trieub4cff112013-03-15 20:35:18 +0000700 /// SetKind - Sets the current node's type.
701 void SetKind(DiffKind Kind) {
702 FlatTree[CurrentNode].Kind = Kind;
703 }
704
Richard Trieu91844232012-06-26 18:18:47 +0000705 /// Up - Changes the node to the parent of the current node.
706 void Up() {
Richard Trieu2331c8b2016-01-15 05:48:38 +0000707 assert(FlatTree[CurrentNode].Kind != Invalid &&
708 "Cannot exit node before setting node information.");
Richard Trieu91844232012-06-26 18:18:47 +0000709 CurrentNode = FlatTree[CurrentNode].ParentNode;
710 }
711
712 /// AddNode - Adds a child node to the current node, then sets that node
713 /// node as the current node.
714 void AddNode() {
Richard Trieu2331c8b2016-01-15 05:48:38 +0000715 assert(FlatTree[CurrentNode].Kind == Template &&
716 "Only Template nodes can have children nodes.");
Richard Trieu91844232012-06-26 18:18:47 +0000717 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;
722 } else {
723 // If a child node exists, find the last child node and add a
724 // next node to it.
725 unsigned i;
726 for (i = Node.ChildNode; FlatTree[i].NextNode != 0;
727 i = FlatTree[i].NextNode) {
728 }
729 FlatTree[i].NextNode = NextFreeNode;
730 }
731 CurrentNode = NextFreeNode;
732 ++NextFreeNode;
733 }
734
735 // Node reading functions.
736 /// StartTraverse - Prepares the tree for recursive traversal.
737 void StartTraverse() {
738 ReadNode = 0;
739 CurrentNode = NextFreeNode;
740 NextFreeNode = 0;
741 }
742
743 /// Parent - Move the current read node to its parent.
744 void Parent() {
745 ReadNode = FlatTree[ReadNode].ParentNode;
746 }
747
Richard Trieu14714c42016-01-14 22:56:39 +0000748 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;
Richard Trieu91844232012-06-26 18:18:47 +0000755 }
756
Richard Trieu14714c42016-01-14 22:56:39 +0000757 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;
Richard Trieu91844232012-06-26 18:18:47 +0000761 }
762
Richard Trieu2c22a862016-01-15 01:08:56 +0000763 void GetExpressionDiff(Expr *&FromExpr, Expr *&ToExpr) {
Richard Trieu14714c42016-01-14 22:56:39 +0000764 assert(FlatTree[ReadNode].Kind == Expression && "Unexpected kind");
765 FromExpr = FlatTree[ReadNode].FromArgInfo.ArgExpr;
766 ToExpr = FlatTree[ReadNode].ToArgInfo.ArgExpr;
Richard Trieu91844232012-06-26 18:18:47 +0000767 }
768
Richard Trieu14714c42016-01-14 22:56:39 +0000769 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;
Richard Trieu6df89452012-11-01 21:29:28 +0000773 }
774
Richard Trieu14714c42016-01-14 22:56:39 +0000775 void GetIntegerDiff(llvm::APSInt &FromInt, llvm::APSInt &ToInt,
776 bool &IsValidFromInt, bool &IsValidToInt,
Richard Trieud5c73782016-01-15 02:55:17 +0000777 QualType &FromIntType, QualType &ToIntType,
Richard Trieu14714c42016-01-14 22:56:39 +0000778 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;
Richard Trieud5c73782016-01-15 02:55:17 +0000784 FromIntType = FlatTree[ReadNode].FromArgInfo.ArgType;
785 ToIntType = FlatTree[ReadNode].ToArgInfo.ArgType;
Richard Trieu14714c42016-01-14 22:56:39 +0000786 FromExpr = FlatTree[ReadNode].FromArgInfo.ArgExpr;
787 ToExpr = FlatTree[ReadNode].ToArgInfo.ArgExpr;
Richard Trieub7243852012-09-28 20:32:51 +0000788 }
789
Richard Trieu14714c42016-01-14 22:56:39 +0000790 void GetDeclarationDiff(ValueDecl *&FromValueDecl, ValueDecl *&ToValueDecl,
791 bool &FromAddressOf, bool &ToAddressOf,
Richard Trieu2c22a862016-01-15 01:08:56 +0000792 bool &FromNullPtr, bool &ToNullPtr, Expr *&FromExpr,
793 Expr *&ToExpr) {
Richard Trieu14714c42016-01-14 22:56:39 +0000794 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;
Richard Trieu2c22a862016-01-15 01:08:56 +0000801 FromExpr = FlatTree[ReadNode].FromArgInfo.ArgExpr;
802 ToExpr = FlatTree[ReadNode].ToArgInfo.ArgExpr;
Richard Trieu14714c42016-01-14 22:56:39 +0000803 }
804
Richard Trieu9213ce52016-01-15 05:01:53 +0000805 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 &&
810 "Unexpected kind.");
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;
819 }
820
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 &&
826 "Unexpected kind.");
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;
835 }
836
Richard Trieu14714c42016-01-14 22:56:39 +0000837 /// FromDefault - Return true if the from argument is the default.
838 bool FromDefault() {
839 return FlatTree[ReadNode].FromArgInfo.IsDefault;
840 }
841
842 /// ToDefault - Return true if the to argument is the default.
843 bool ToDefault() {
844 return FlatTree[ReadNode].ToArgInfo.IsDefault;
Richard Trieu954aaaf2013-02-27 01:41:53 +0000845 }
846
Richard Trieu91844232012-06-26 18:18:47 +0000847 /// NodeIsSame - Returns true the arguments are the same.
848 bool NodeIsSame() {
849 return FlatTree[ReadNode].Same;
850 }
851
852 /// HasChildrend - Returns true if the node has children.
853 bool HasChildren() {
854 return FlatTree[ReadNode].ChildNode != 0;
855 }
856
857 /// MoveToChild - Moves from the current node to its child.
858 void MoveToChild() {
859 ReadNode = FlatTree[ReadNode].ChildNode;
860 }
861
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)
866 return false;
867
868 ReadNode = FlatTree[ReadNode].NextNode;
869 return true;
870 }
871
872 /// HasNextSibling - Return true if the node has a next sibling.
873 bool HasNextSibling() {
874 return FlatTree[ReadNode].NextNode != 0;
875 }
876
Richard Trieu91844232012-06-26 18:18:47 +0000877 /// Empty - Returns true if the tree has no information.
878 bool Empty() {
Richard Trieub4cff112013-03-15 20:35:18 +0000879 return GetKind() == Invalid;
880 }
881
882 /// GetKind - Returns the current node's type.
883 DiffKind GetKind() {
884 return FlatTree[ReadNode].Kind;
Richard Trieu91844232012-06-26 18:18:47 +0000885 }
886 };
887
888 DiffTree Tree;
889
Richard Trieuac1e2f82016-01-14 23:30:12 +0000890 /// TSTiterator - a pair of iterators that walks the
891 /// TemplateSpecializationType and the desugared TemplateSpecializationType.
892 /// The deseguared TemplateArgument should provide the canonical argument
893 /// for comparisons.
894 class TSTiterator {
Richard Trieu91844232012-06-26 18:18:47 +0000895 typedef const TemplateArgument& reference;
896 typedef const TemplateArgument* pointer;
897
Richard Trieuac1e2f82016-01-14 23:30:12 +0000898 /// 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
903 /// traverse over.
904 const TemplateSpecializationType *TST;
Richard Trieu91844232012-06-26 18:18:47 +0000905
Richard Trieuac1e2f82016-01-14 23:30:12 +0000906 /// Index - the index of the template argument in TST.
907 unsigned Index;
Richard Trieu64ab30392013-03-15 23:55:09 +0000908
Richard Trieuac1e2f82016-01-14 23:30:12 +0000909 /// 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;
Richard Trieu91844232012-06-26 18:18:47 +0000912
Richard Trieuac1e2f82016-01-14 23:30:12 +0000913 /// EndTA - the end iterator of a parameter pack
914 TemplateArgument::pack_iterator EndTA;
Richard Trieu91844232012-06-26 18:18:47 +0000915
Richard Trieuac1e2f82016-01-14 23:30:12 +0000916 /// 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) {
920 if (isEnd()) return;
Richard Trieu91844232012-06-26 18:18:47 +0000921
Richard Trieuac1e2f82016-01-14 23:30:12 +0000922 // Set to first template argument. If not a parameter pack, done.
923 TemplateArgument TA = TST->getArg(0);
924 if (TA.getKind() != TemplateArgument::Pack) return;
Richard Trieu91844232012-06-26 18:18:47 +0000925
Richard Trieuac1e2f82016-01-14 23:30:12 +0000926 // Start looking into the parameter pack.
Richard Trieu91844232012-06-26 18:18:47 +0000927 CurrentTA = TA.pack_begin();
928 EndTA = TA.pack_end();
929
Richard Trieuac1e2f82016-01-14 23:30:12 +0000930 // Found a valid template argument.
931 if (CurrentTA != EndTA) return;
932
933 // Parameter pack is empty, use the increment to get to a valid
934 // template argument.
935 ++(*this);
Richard Trieu91844232012-06-26 18:18:47 +0000936 }
Richard Trieuac1e2f82016-01-14 23:30:12 +0000937
938 /// isEnd - Returns true if the iterator is one past the end.
939 bool isEnd() const {
940 return Index >= TST->getNumArgs();
941 }
942
943 /// &operator++ - Increment the iterator to the next template argument.
944 InternalIterator &operator++() {
945 if (isEnd()) {
946 return *this;
947 }
948
949 // If in a parameter pack, advance in the parameter pack.
950 if (CurrentTA != EndTA) {
951 ++CurrentTA;
952 if (CurrentTA != EndTA)
953 return *this;
954 }
955
956 // Loop until a template argument is found, or the end is reached.
957 while (true) {
958 // Advance to the next template argument. Break if reached the end.
959 if (++Index == TST->getNumArgs())
960 break;
961
962 // If the TemplateArgument is not a parameter pack, done.
963 TemplateArgument TA = TST->getArg(Index);
964 if (TA.getKind() != TemplateArgument::Pack)
965 break;
966
967 // Handle parameter packs.
968 CurrentTA = TA.pack_begin();
969 EndTA = TA.pack_end();
970
971 // If the parameter pack is empty, try to advance again.
972 if (CurrentTA != EndTA)
973 break;
974 }
975 return *this;
976 }
977
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);
983 else
984 return *CurrentTA;
985 }
986
987 /// operator-> - Allow access to the underlying TemplateArgument.
988 pointer operator->() const {
989 return &operator*();
990 }
991 };
992
Richard Trieua7564d72016-03-30 22:23:00 +0000993 bool UseDesugaredIterator;
Richard Trieuac1e2f82016-01-14 23:30:12 +0000994 InternalIterator SugaredIterator;
995 InternalIterator DesugaredIterator;
996
997 public:
998 TSTiterator(ASTContext &Context, const TemplateSpecializationType *TST)
Richard Trieua7564d72016-03-30 22:23:00 +0000999 : UseDesugaredIterator(TST->isSugared() && !TST->isTypeAlias()),
1000 SugaredIterator(TST),
Richard Trieuac1e2f82016-01-14 23:30:12 +00001001 DesugaredIterator(
1002 GetTemplateSpecializationType(Context, TST->desugar())) {}
1003
1004 /// &operator++ - Increment the iterator to the next template argument.
1005 TSTiterator &operator++() {
1006 ++SugaredIterator;
Richard Trieua7564d72016-03-30 22:23:00 +00001007 if (UseDesugaredIterator)
1008 ++DesugaredIterator;
Richard Trieu91844232012-06-26 18:18:47 +00001009 return *this;
1010 }
1011
1012 /// operator* - Returns the appropriate TemplateArgument.
1013 reference operator*() const {
Richard Trieuac1e2f82016-01-14 23:30:12 +00001014 return *SugaredIterator;
Richard Trieu91844232012-06-26 18:18:47 +00001015 }
1016
1017 /// operator-> - Allow access to the underlying TemplateArgument.
1018 pointer operator->() const {
1019 return &operator*();
1020 }
Richard Trieu64ab30392013-03-15 23:55:09 +00001021
Richard Trieuac1e2f82016-01-14 23:30:12 +00001022 /// isEnd - Returns true if no more TemplateArguments are available.
1023 bool isEnd() const {
1024 return SugaredIterator.isEnd();
1025 }
1026
1027 /// hasDesugaredTA - Returns true if there is another TemplateArgument
1028 /// available.
1029 bool hasDesugaredTA() const {
Richard Trieua7564d72016-03-30 22:23:00 +00001030 return UseDesugaredIterator && !DesugaredIterator.isEnd();
Richard Trieuac1e2f82016-01-14 23:30:12 +00001031 }
1032
1033 /// getDesugaredTA - Returns the desugared TemplateArgument.
1034 reference getDesugaredTA() const {
Richard Trieua7564d72016-03-30 22:23:00 +00001035 assert(UseDesugaredIterator &&
1036 "Desugared TemplateArgument should not be used.");
Richard Trieuac1e2f82016-01-14 23:30:12 +00001037 return *DesugaredIterator;
Richard Trieu64ab30392013-03-15 23:55:09 +00001038 }
Richard Trieu91844232012-06-26 18:18:47 +00001039 };
1040
1041 // These functions build up the template diff tree, including functions to
Richard Trieuac1e2f82016-01-14 23:30:12 +00001042 // retrieve and compare template arguments.
Richard Trieu91844232012-06-26 18:18:47 +00001043
Richard Trieu2331c8b2016-01-15 05:48:38 +00001044 static const TemplateSpecializationType *GetTemplateSpecializationType(
Richard Trieu91844232012-06-26 18:18:47 +00001045 ASTContext &Context, QualType Ty) {
1046 if (const TemplateSpecializationType *TST =
1047 Ty->getAs<TemplateSpecializationType>())
1048 return TST;
1049
1050 const RecordType *RT = Ty->getAs<RecordType>();
1051
1052 if (!RT)
Craig Topper36250ad2014-05-12 05:36:57 +00001053 return nullptr;
Richard Trieu91844232012-06-26 18:18:47 +00001054
1055 const ClassTemplateSpecializationDecl *CTSD =
1056 dyn_cast<ClassTemplateSpecializationDecl>(RT->getDecl());
1057
1058 if (!CTSD)
Craig Topper36250ad2014-05-12 05:36:57 +00001059 return nullptr;
Richard Trieu91844232012-06-26 18:18:47 +00001060
1061 Ty = Context.getTemplateSpecializationType(
1062 TemplateName(CTSD->getSpecializedTemplate()),
1063 CTSD->getTemplateArgs().data(),
1064 CTSD->getTemplateArgs().size(),
Richard Trieu3cee4132013-03-23 01:38:36 +00001065 Ty.getLocalUnqualifiedType().getCanonicalType());
Richard Trieu91844232012-06-26 18:18:47 +00001066
1067 return Ty->getAs<TemplateSpecializationType>();
1068 }
1069
Richard Trieu14714c42016-01-14 22:56:39 +00001070 /// Returns true if the DiffType is Type and false for Template.
1071 static bool OnlyPerformTypeDiff(ASTContext &Context, QualType FromType,
1072 QualType ToType,
1073 const TemplateSpecializationType *&FromArgTST,
1074 const TemplateSpecializationType *&ToArgTST) {
1075 if (FromType.isNull() || ToType.isNull())
1076 return true;
1077
1078 if (Context.hasSameType(FromType, ToType))
1079 return true;
1080
1081 FromArgTST = GetTemplateSpecializationType(Context, FromType);
1082 ToArgTST = GetTemplateSpecializationType(Context, ToType);
1083
1084 if (!FromArgTST || !ToArgTST)
1085 return true;
1086
1087 if (!hasSameTemplate(FromArgTST, ToArgTST))
1088 return true;
1089
1090 return false;
1091 }
1092
Richard Trieu9fee0b72014-08-27 06:24:47 +00001093 /// DiffTypes - Fills a DiffNode with information about a type difference.
Richard Trieu2c22a862016-01-15 01:08:56 +00001094 void DiffTypes(const TSTiterator &FromIter, const TSTiterator &ToIter) {
1095 QualType FromType = GetType(FromIter);
1096 QualType ToType = GetType(ToIter);
Richard Trieu9fee0b72014-08-27 06:24:47 +00001097
Richard Trieu14714c42016-01-14 22:56:39 +00001098 bool FromDefault = FromIter.isEnd() && !FromType.isNull();
1099 bool ToDefault = ToIter.isEnd() && !ToType.isNull();
Richard Trieu9fee0b72014-08-27 06:24:47 +00001100
Richard Trieu14714c42016-01-14 22:56:39 +00001101 const TemplateSpecializationType *FromArgTST = nullptr;
1102 const TemplateSpecializationType *ToArgTST = nullptr;
1103 if (OnlyPerformTypeDiff(Context, FromType, ToType, FromArgTST, ToArgTST)) {
1104 Tree.SetTypeDiff(FromType, ToType, FromDefault, ToDefault);
1105 Tree.SetSame(!FromType.isNull() && !ToType.isNull() &&
1106 Context.hasSameType(FromType, ToType));
1107 } else {
1108 assert(FromArgTST && ToArgTST &&
1109 "Both template specializations need to be valid.");
1110 Qualifiers FromQual = FromType.getQualifiers(),
1111 ToQual = ToType.getQualifiers();
1112 FromQual -= QualType(FromArgTST, 0).getQualifiers();
1113 ToQual -= QualType(ToArgTST, 0).getQualifiers();
1114 Tree.SetTemplateDiff(FromArgTST->getTemplateName().getAsTemplateDecl(),
1115 ToArgTST->getTemplateName().getAsTemplateDecl(),
1116 FromQual, ToQual, FromDefault, ToDefault);
1117 DiffTemplate(FromArgTST, ToArgTST);
Richard Trieu9fee0b72014-08-27 06:24:47 +00001118 }
Richard Trieu9fee0b72014-08-27 06:24:47 +00001119 }
1120
1121 /// DiffTemplateTemplates - Fills a DiffNode with information about a
1122 /// template template difference.
1123 void DiffTemplateTemplates(const TSTiterator &FromIter,
Richard Trieu2c22a862016-01-15 01:08:56 +00001124 const TSTiterator &ToIter) {
1125 TemplateDecl *FromDecl = GetTemplateDecl(FromIter);
1126 TemplateDecl *ToDecl = GetTemplateDecl(ToIter);
Richard Trieu14714c42016-01-14 22:56:39 +00001127 Tree.SetTemplateTemplateDiff(FromDecl, ToDecl, FromIter.isEnd() && FromDecl,
1128 ToIter.isEnd() && ToDecl);
Richard Trieu9fee0b72014-08-27 06:24:47 +00001129 Tree.SetSame(FromDecl && ToDecl &&
1130 FromDecl->getCanonicalDecl() == ToDecl->getCanonicalDecl());
Richard Trieu9fee0b72014-08-27 06:24:47 +00001131 }
1132
1133 /// InitializeNonTypeDiffVariables - Helper function for DiffNonTypes
Richard Trieud5c73782016-01-15 02:55:17 +00001134 static void InitializeNonTypeDiffVariables(ASTContext &Context,
1135 const TSTiterator &Iter,
1136 NonTypeTemplateParmDecl *Default,
1137 llvm::APSInt &Value, bool &HasInt,
1138 QualType &IntType, bool &IsNullPtr,
1139 Expr *&E, ValueDecl *&VD,
1140 bool &NeedAddressOf) {
Richard Trieu2c22a862016-01-15 01:08:56 +00001141 if (!Iter.isEnd()) {
1142 switch (Iter->getKind()) {
1143 default:
1144 llvm_unreachable("unknown ArgumentKind");
1145 case TemplateArgument::Integral:
1146 Value = Iter->getAsIntegral();
1147 HasInt = true;
Richard Trieud5c73782016-01-15 02:55:17 +00001148 IntType = Iter->getIntegralType();
Richard Trieu2c22a862016-01-15 01:08:56 +00001149 return;
1150 case TemplateArgument::Declaration: {
1151 VD = Iter->getAsDecl();
1152 QualType ArgType = Iter->getParamTypeForDecl();
1153 QualType VDType = VD->getType();
1154 if (ArgType->isPointerType() &&
1155 Context.hasSameType(ArgType->getPointeeType(), VDType))
1156 NeedAddressOf = true;
1157 return;
Richard Trieu9fee0b72014-08-27 06:24:47 +00001158 }
Richard Trieu2c22a862016-01-15 01:08:56 +00001159 case TemplateArgument::NullPtr:
1160 IsNullPtr = true;
1161 return;
1162 case TemplateArgument::Expression:
1163 E = Iter->getAsExpr();
Richard Trieu9fee0b72014-08-27 06:24:47 +00001164 }
Richard Trieu2c22a862016-01-15 01:08:56 +00001165 } else if (!Default->isParameterPack()) {
1166 E = Default->getDefaultArgument();
Richard Trieu9fee0b72014-08-27 06:24:47 +00001167 }
1168
Richard Trieu2c22a862016-01-15 01:08:56 +00001169 if (!Iter.hasDesugaredTA()) return;
Richard Trieu9fee0b72014-08-27 06:24:47 +00001170
Richard Trieu2c22a862016-01-15 01:08:56 +00001171 const TemplateArgument& TA = Iter.getDesugaredTA();
1172 switch (TA.getKind()) {
1173 default:
1174 llvm_unreachable("unknown ArgumentKind");
1175 case TemplateArgument::Integral:
1176 Value = TA.getAsIntegral();
1177 HasInt = true;
Richard Trieud5c73782016-01-15 02:55:17 +00001178 IntType = TA.getIntegralType();
Richard Trieu2c22a862016-01-15 01:08:56 +00001179 return;
1180 case TemplateArgument::Declaration: {
1181 VD = TA.getAsDecl();
1182 QualType ArgType = TA.getParamTypeForDecl();
1183 QualType VDType = VD->getType();
1184 if (ArgType->isPointerType() &&
1185 Context.hasSameType(ArgType->getPointeeType(), VDType))
1186 NeedAddressOf = true;
1187 return;
1188 }
1189 case TemplateArgument::NullPtr:
1190 IsNullPtr = true;
1191 return;
1192 case TemplateArgument::Expression:
1193 // TODO: Sometimes, the desugared template argument Expr differs from
1194 // the sugared template argument Expr. It may be useful in the future
1195 // but for now, it is just discarded.
1196 if (!E)
1197 E = TA.getAsExpr();
1198 return;
1199 }
Richard Trieu9fee0b72014-08-27 06:24:47 +00001200 }
1201
1202 /// DiffNonTypes - Handles any template parameters not handled by DiffTypes
1203 /// of DiffTemplatesTemplates, such as integer and declaration parameters.
1204 void DiffNonTypes(const TSTiterator &FromIter, const TSTiterator &ToIter,
1205 NonTypeTemplateParmDecl *FromDefaultNonTypeDecl,
1206 NonTypeTemplateParmDecl *ToDefaultNonTypeDecl) {
1207 Expr *FromExpr = nullptr, *ToExpr = nullptr;
1208 llvm::APSInt FromInt, ToInt;
Richard Trieud5c73782016-01-15 02:55:17 +00001209 QualType FromIntType, ToIntType;
Richard Trieu9fee0b72014-08-27 06:24:47 +00001210 ValueDecl *FromValueDecl = nullptr, *ToValueDecl = nullptr;
Richard Trieu2c22a862016-01-15 01:08:56 +00001211 bool HasFromInt = false, HasToInt = false, FromNullPtr = false,
1212 ToNullPtr = false, NeedFromAddressOf = false, NeedToAddressOf = false;
Richard Trieud5c73782016-01-15 02:55:17 +00001213 InitializeNonTypeDiffVariables(
1214 Context, FromIter, FromDefaultNonTypeDecl, FromInt, HasFromInt,
1215 FromIntType, FromNullPtr, FromExpr, FromValueDecl, NeedFromAddressOf);
Richard Trieu2c22a862016-01-15 01:08:56 +00001216 InitializeNonTypeDiffVariables(Context, ToIter, ToDefaultNonTypeDecl, ToInt,
Richard Trieud5c73782016-01-15 02:55:17 +00001217 HasToInt, ToIntType, ToNullPtr, ToExpr,
1218 ToValueDecl, NeedToAddressOf);
Richard Trieu2c22a862016-01-15 01:08:56 +00001219
Richard Trieu14714c42016-01-14 22:56:39 +00001220 bool FromDefault = FromIter.isEnd() &&
1221 (FromExpr || FromValueDecl || HasFromInt || FromNullPtr);
1222 bool ToDefault = ToIter.isEnd() &&
1223 (ToExpr || ToValueDecl || HasToInt || ToNullPtr);
1224
Richard Trieu9213ce52016-01-15 05:01:53 +00001225 bool FromDeclaration = FromValueDecl || FromNullPtr;
1226 bool ToDeclaration = ToValueDecl || ToNullPtr;
1227
1228 if (FromDeclaration && HasToInt) {
1229 Tree.SetFromDeclarationAndToIntegerDiff(
1230 FromValueDecl, NeedFromAddressOf, FromNullPtr, FromExpr, ToInt,
1231 HasToInt, ToIntType, ToExpr, FromDefault, ToDefault);
1232 Tree.SetSame(false);
1233 return;
1234
1235 }
1236
1237 if (HasFromInt && ToDeclaration) {
1238 Tree.SetFromIntegerAndToDeclarationDiff(
1239 FromInt, HasFromInt, FromIntType, FromExpr, ToValueDecl,
1240 NeedToAddressOf, ToNullPtr, ToExpr, FromDefault, ToDefault);
1241 Tree.SetSame(false);
1242 return;
1243 }
1244
Richard Trieu9fee0b72014-08-27 06:24:47 +00001245 if (HasFromInt || HasToInt) {
Richard Trieud5c73782016-01-15 02:55:17 +00001246 Tree.SetIntegerDiff(FromInt, ToInt, HasFromInt, HasToInt, FromIntType,
1247 ToIntType, FromExpr, ToExpr, FromDefault, ToDefault);
Richard Trieu15b66532015-01-24 02:48:32 +00001248 if (HasFromInt && HasToInt) {
Richard Trieud5c73782016-01-15 02:55:17 +00001249 Tree.SetSame(Context.hasSameType(FromIntType, ToIntType) &&
1250 FromInt == ToInt);
Richard Trieu15b66532015-01-24 02:48:32 +00001251 }
Richard Trieu9fee0b72014-08-27 06:24:47 +00001252 return;
1253 }
1254
Richard Trieu2c22a862016-01-15 01:08:56 +00001255 if (FromDeclaration || ToDeclaration) {
1256 Tree.SetDeclarationDiff(FromValueDecl, ToValueDecl, NeedFromAddressOf,
1257 NeedToAddressOf, FromNullPtr, ToNullPtr, FromExpr,
1258 ToExpr, FromDefault, ToDefault);
1259 bool BothNull = FromNullPtr && ToNullPtr;
1260 bool SameValueDecl =
1261 FromValueDecl && ToValueDecl &&
1262 NeedFromAddressOf == NeedToAddressOf &&
1263 FromValueDecl->getCanonicalDecl() == ToValueDecl->getCanonicalDecl();
1264 Tree.SetSame(BothNull || SameValueDecl);
1265 return;
1266 }
Richard Trieu9fee0b72014-08-27 06:24:47 +00001267
Richard Trieu2c22a862016-01-15 01:08:56 +00001268 assert((FromExpr || ToExpr) && "Both template arguments cannot be empty.");
1269 Tree.SetExpressionDiff(FromExpr, ToExpr, FromDefault, ToDefault);
1270 Tree.SetSame(IsEqualExpr(Context, FromExpr, ToExpr));
Richard Trieu9fee0b72014-08-27 06:24:47 +00001271 }
1272
Richard Trieu91844232012-06-26 18:18:47 +00001273 /// DiffTemplate - recursively visits template arguments and stores the
1274 /// argument info into a tree.
1275 void DiffTemplate(const TemplateSpecializationType *FromTST,
1276 const TemplateSpecializationType *ToTST) {
1277 // Begin descent into diffing template tree.
Benjamin Kramer3b05e202013-10-08 16:58:52 +00001278 TemplateParameterList *ParamsFrom =
Richard Trieu91844232012-06-26 18:18:47 +00001279 FromTST->getTemplateName().getAsTemplateDecl()->getTemplateParameters();
Benjamin Kramer3b05e202013-10-08 16:58:52 +00001280 TemplateParameterList *ParamsTo =
1281 ToTST->getTemplateName().getAsTemplateDecl()->getTemplateParameters();
Richard Trieu91844232012-06-26 18:18:47 +00001282 unsigned TotalArgs = 0;
Richard Trieu64ab30392013-03-15 23:55:09 +00001283 for (TSTiterator FromIter(Context, FromTST), ToIter(Context, ToTST);
Richard Trieu91844232012-06-26 18:18:47 +00001284 !FromIter.isEnd() || !ToIter.isEnd(); ++TotalArgs) {
1285 Tree.AddNode();
1286
1287 // Get the parameter at index TotalArgs. If index is larger
1288 // than the total number of parameters, then there is an
1289 // argument pack, so re-use the last parameter.
Richard Trieu9fee0b72014-08-27 06:24:47 +00001290 unsigned FromParamIndex = std::min(TotalArgs, ParamsFrom->size() - 1);
1291 unsigned ToParamIndex = std::min(TotalArgs, ParamsTo->size() - 1);
1292 NamedDecl *FromParamND = ParamsFrom->getParam(FromParamIndex);
1293 NamedDecl *ToParamND = ParamsTo->getParam(ToParamIndex);
Benjamin Kramer3b05e202013-10-08 16:58:52 +00001294
Richard Trieu2c22a862016-01-15 01:08:56 +00001295 assert(FromParamND->getKind() == ToParamND->getKind() &&
1296 "Parameter Decl are not the same kind.");
Richard Trieu91844232012-06-26 18:18:47 +00001297
Richard Trieu2c22a862016-01-15 01:08:56 +00001298 if (isa<TemplateTypeParmDecl>(FromParamND)) {
1299 DiffTypes(FromIter, ToIter);
1300 } else if (isa<TemplateTemplateParmDecl>(FromParamND)) {
1301 DiffTemplateTemplates(FromIter, ToIter);
1302 } else if (isa<NonTypeTemplateParmDecl>(FromParamND)) {
1303 NonTypeTemplateParmDecl *FromDefaultNonTypeDecl =
1304 cast<NonTypeTemplateParmDecl>(FromParamND);
1305 NonTypeTemplateParmDecl *ToDefaultNonTypeDecl =
1306 cast<NonTypeTemplateParmDecl>(ToParamND);
Richard Trieu9fee0b72014-08-27 06:24:47 +00001307 DiffNonTypes(FromIter, ToIter, FromDefaultNonTypeDecl,
1308 ToDefaultNonTypeDecl);
Richard Trieu2c22a862016-01-15 01:08:56 +00001309 } else {
1310 llvm_unreachable("Unexpected Decl type.");
1311 }
Richard Trieu91844232012-06-26 18:18:47 +00001312
Richard Trieu64ab30392013-03-15 23:55:09 +00001313 ++FromIter;
1314 ++ToIter;
Richard Trieu91844232012-06-26 18:18:47 +00001315 Tree.Up();
1316 }
1317 }
1318
Richard Trieu8e14cac2012-09-28 19:51:57 +00001319 /// makeTemplateList - Dump every template alias into the vector.
1320 static void makeTemplateList(
Craig Topper5603df42013-07-05 19:34:19 +00001321 SmallVectorImpl<const TemplateSpecializationType *> &TemplateList,
Richard Trieu8e14cac2012-09-28 19:51:57 +00001322 const TemplateSpecializationType *TST) {
1323 while (TST) {
1324 TemplateList.push_back(TST);
1325 if (!TST->isTypeAlias())
1326 return;
1327 TST = TST->getAliasedType()->getAs<TemplateSpecializationType>();
1328 }
1329 }
1330
1331 /// hasSameBaseTemplate - Returns true when the base templates are the same,
1332 /// even if the template arguments are not.
1333 static bool hasSameBaseTemplate(const TemplateSpecializationType *FromTST,
1334 const TemplateSpecializationType *ToTST) {
Douglas Gregor8e9f55f2013-01-31 01:08:35 +00001335 return FromTST->getTemplateName().getAsTemplateDecl()->getCanonicalDecl() ==
1336 ToTST->getTemplateName().getAsTemplateDecl()->getCanonicalDecl();
Richard Trieu8e14cac2012-09-28 19:51:57 +00001337 }
1338
Richard Trieu91844232012-06-26 18:18:47 +00001339 /// hasSameTemplate - Returns true if both types are specialized from the
1340 /// same template declaration. If they come from different template aliases,
1341 /// do a parallel ascension search to determine the highest template alias in
1342 /// common and set the arguments to them.
1343 static bool hasSameTemplate(const TemplateSpecializationType *&FromTST,
1344 const TemplateSpecializationType *&ToTST) {
1345 // Check the top templates if they are the same.
Richard Trieu8e14cac2012-09-28 19:51:57 +00001346 if (hasSameBaseTemplate(FromTST, ToTST))
Richard Trieu91844232012-06-26 18:18:47 +00001347 return true;
1348
1349 // Create vectors of template aliases.
1350 SmallVector<const TemplateSpecializationType*, 1> FromTemplateList,
1351 ToTemplateList;
1352
Richard Trieu8e14cac2012-09-28 19:51:57 +00001353 makeTemplateList(FromTemplateList, FromTST);
1354 makeTemplateList(ToTemplateList, ToTST);
Richard Trieu91844232012-06-26 18:18:47 +00001355
Craig Topper61ac9062013-07-08 03:55:09 +00001356 SmallVectorImpl<const TemplateSpecializationType *>::reverse_iterator
Richard Trieu91844232012-06-26 18:18:47 +00001357 FromIter = FromTemplateList.rbegin(), FromEnd = FromTemplateList.rend(),
1358 ToIter = ToTemplateList.rbegin(), ToEnd = ToTemplateList.rend();
1359
1360 // Check if the lowest template types are the same. If not, return.
Richard Trieu8e14cac2012-09-28 19:51:57 +00001361 if (!hasSameBaseTemplate(*FromIter, *ToIter))
Richard Trieu91844232012-06-26 18:18:47 +00001362 return false;
1363
1364 // Begin searching up the template aliases. The bottom most template
1365 // matches so move up until one pair does not match. Use the template
1366 // right before that one.
1367 for (; FromIter != FromEnd && ToIter != ToEnd; ++FromIter, ++ToIter) {
Richard Trieu8e14cac2012-09-28 19:51:57 +00001368 if (!hasSameBaseTemplate(*FromIter, *ToIter))
Richard Trieu91844232012-06-26 18:18:47 +00001369 break;
1370 }
1371
1372 FromTST = FromIter[-1];
1373 ToTST = ToIter[-1];
1374
1375 return true;
1376 }
1377
1378 /// GetType - Retrieves the template type arguments, including default
1379 /// arguments.
Richard Trieu2c22a862016-01-15 01:08:56 +00001380 static QualType GetType(const TSTiterator &Iter) {
Richard Trieu91844232012-06-26 18:18:47 +00001381 if (!Iter.isEnd())
Richard Trieu17f13ec2013-04-03 03:06:48 +00001382 return Iter->getAsType();
Richard Trieu2c22a862016-01-15 01:08:56 +00001383 if (Iter.hasDesugaredTA())
Richard Trieuac1e2f82016-01-14 23:30:12 +00001384 return Iter.getDesugaredTA().getAsType();
Richard Trieu2c22a862016-01-15 01:08:56 +00001385 return QualType();
Richard Trieu38800892014-07-24 04:24:50 +00001386 }
1387
Richard Trieu91844232012-06-26 18:18:47 +00001388 /// GetTemplateDecl - Retrieves the template template arguments, including
1389 /// default arguments.
Richard Trieu2c22a862016-01-15 01:08:56 +00001390 static TemplateDecl *GetTemplateDecl(const TSTiterator &Iter) {
Richard Trieu91844232012-06-26 18:18:47 +00001391 if (!Iter.isEnd())
Richard Trieu17f13ec2013-04-03 03:06:48 +00001392 return Iter->getAsTemplate().getAsTemplateDecl();
Richard Trieu2c22a862016-01-15 01:08:56 +00001393 if (Iter.hasDesugaredTA())
1394 return Iter.getDesugaredTA().getAsTemplate().getAsTemplateDecl();
Craig Topper36250ad2014-05-12 05:36:57 +00001395 return nullptr;
Richard Trieu91844232012-06-26 18:18:47 +00001396 }
1397
Richard Trieu2c22a862016-01-15 01:08:56 +00001398 /// IsEqualExpr - Returns true if the expressions are the same in regards to
1399 /// template arguments. These expressions are dependent, so profile them
1400 /// instead of trying to evaluate them.
Richard Trieu15b66532015-01-24 02:48:32 +00001401 static bool IsEqualExpr(ASTContext &Context, Expr *FromExpr, Expr *ToExpr) {
Richard Trieu91844232012-06-26 18:18:47 +00001402 if (FromExpr == ToExpr)
1403 return true;
1404
1405 if (!FromExpr || !ToExpr)
1406 return false;
1407
Richard Trieu2c22a862016-01-15 01:08:56 +00001408 llvm::FoldingSetNodeID FromID, ToID;
1409 FromExpr->Profile(FromID, Context, true);
1410 ToExpr->Profile(ToID, Context, true);
1411 return FromID == ToID;
Richard Trieu91844232012-06-26 18:18:47 +00001412 }
1413
1414 // These functions converts the tree representation of the template
1415 // differences into the internal character vector.
1416
1417 /// TreeToString - Converts the Tree object into a character stream which
1418 /// will later be turned into the output string.
1419 void TreeToString(int Indent = 1) {
1420 if (PrintTree) {
1421 OS << '\n';
Benjamin Kramer6582c362013-02-22 16:13:34 +00001422 OS.indent(2 * Indent);
Richard Trieu91844232012-06-26 18:18:47 +00001423 ++Indent;
1424 }
1425
1426 // Handle cases where the difference is not templates with different
1427 // arguments.
Richard Trieub4cff112013-03-15 20:35:18 +00001428 switch (Tree.GetKind()) {
Richard Trieub4cff112013-03-15 20:35:18 +00001429 case DiffTree::Invalid:
1430 llvm_unreachable("Template diffing failed with bad DiffNode");
1431 case DiffTree::Type: {
Richard Trieu91844232012-06-26 18:18:47 +00001432 QualType FromType, ToType;
Richard Trieu14714c42016-01-14 22:56:39 +00001433 Tree.GetTypeDiff(FromType, ToType);
Richard Trieu91844232012-06-26 18:18:47 +00001434 PrintTypeNames(FromType, ToType, Tree.FromDefault(), Tree.ToDefault(),
1435 Tree.NodeIsSame());
1436 return;
1437 }
Richard Trieub4cff112013-03-15 20:35:18 +00001438 case DiffTree::Expression: {
Richard Trieu91844232012-06-26 18:18:47 +00001439 Expr *FromExpr, *ToExpr;
Richard Trieu2c22a862016-01-15 01:08:56 +00001440 Tree.GetExpressionDiff(FromExpr, ToExpr);
1441 PrintExpr(FromExpr, ToExpr, Tree.FromDefault(), Tree.ToDefault(),
1442 Tree.NodeIsSame());
Richard Trieu91844232012-06-26 18:18:47 +00001443 return;
1444 }
Richard Trieub4cff112013-03-15 20:35:18 +00001445 case DiffTree::TemplateTemplate: {
Richard Trieu91844232012-06-26 18:18:47 +00001446 TemplateDecl *FromTD, *ToTD;
Richard Trieu14714c42016-01-14 22:56:39 +00001447 Tree.GetTemplateTemplateDiff(FromTD, ToTD);
Richard Trieu91844232012-06-26 18:18:47 +00001448 PrintTemplateTemplate(FromTD, ToTD, Tree.FromDefault(),
1449 Tree.ToDefault(), Tree.NodeIsSame());
1450 return;
1451 }
Richard Trieub4cff112013-03-15 20:35:18 +00001452 case DiffTree::Integer: {
Richard Trieu6df89452012-11-01 21:29:28 +00001453 llvm::APSInt FromInt, ToInt;
Richard Trieu64ab30392013-03-15 23:55:09 +00001454 Expr *FromExpr, *ToExpr;
Richard Trieu6df89452012-11-01 21:29:28 +00001455 bool IsValidFromInt, IsValidToInt;
Richard Trieud5c73782016-01-15 02:55:17 +00001456 QualType FromIntType, ToIntType;
Richard Trieu14714c42016-01-14 22:56:39 +00001457 Tree.GetIntegerDiff(FromInt, ToInt, IsValidFromInt, IsValidToInt,
Richard Trieud5c73782016-01-15 02:55:17 +00001458 FromIntType, ToIntType, FromExpr, ToExpr);
1459 PrintAPSInt(FromInt, ToInt, IsValidFromInt, IsValidToInt, FromIntType,
1460 ToIntType, FromExpr, ToExpr, Tree.FromDefault(),
1461 Tree.ToDefault(), Tree.NodeIsSame());
Richard Trieu6df89452012-11-01 21:29:28 +00001462 return;
1463 }
Richard Trieub4cff112013-03-15 20:35:18 +00001464 case DiffTree::Declaration: {
Richard Trieu954aaaf2013-02-27 01:41:53 +00001465 ValueDecl *FromValueDecl, *ToValueDecl;
Richard Trieu091872c52013-05-07 21:36:24 +00001466 bool FromAddressOf, ToAddressOf;
Richard Trieu14714c42016-01-14 22:56:39 +00001467 bool FromNullPtr, ToNullPtr;
Richard Trieu2c22a862016-01-15 01:08:56 +00001468 Expr *FromExpr, *ToExpr;
Richard Trieu14714c42016-01-14 22:56:39 +00001469 Tree.GetDeclarationDiff(FromValueDecl, ToValueDecl, FromAddressOf,
Richard Trieu2c22a862016-01-15 01:08:56 +00001470 ToAddressOf, FromNullPtr, ToNullPtr, FromExpr,
1471 ToExpr);
Richard Trieu091872c52013-05-07 21:36:24 +00001472 PrintValueDecl(FromValueDecl, ToValueDecl, FromAddressOf, ToAddressOf,
Richard Trieu2c22a862016-01-15 01:08:56 +00001473 FromNullPtr, ToNullPtr, FromExpr, ToExpr,
1474 Tree.FromDefault(), Tree.ToDefault(), Tree.NodeIsSame());
Richard Trieu954aaaf2013-02-27 01:41:53 +00001475 return;
1476 }
Richard Trieu9213ce52016-01-15 05:01:53 +00001477 case DiffTree::FromDeclarationAndToInteger: {
1478 ValueDecl *FromValueDecl;
1479 bool FromAddressOf;
1480 bool FromNullPtr;
1481 Expr *FromExpr;
1482 llvm::APSInt ToInt;
1483 bool IsValidToInt;
1484 QualType ToIntType;
1485 Expr *ToExpr;
1486 Tree.GetFromDeclarationAndToIntegerDiff(
1487 FromValueDecl, FromAddressOf, FromNullPtr, FromExpr, ToInt,
1488 IsValidToInt, ToIntType, ToExpr);
1489 assert((FromValueDecl || FromNullPtr) && IsValidToInt);
1490 PrintValueDeclAndInteger(FromValueDecl, FromAddressOf, FromNullPtr,
1491 FromExpr, Tree.FromDefault(), ToInt, ToIntType,
1492 ToExpr, Tree.ToDefault());
1493 return;
1494 }
1495 case DiffTree::FromIntegerAndToDeclaration: {
1496 llvm::APSInt FromInt;
1497 bool IsValidFromInt;
1498 QualType FromIntType;
1499 Expr *FromExpr;
1500 ValueDecl *ToValueDecl;
1501 bool ToAddressOf;
1502 bool ToNullPtr;
1503 Expr *ToExpr;
1504 Tree.GetFromIntegerAndToDeclarationDiff(
1505 FromInt, IsValidFromInt, FromIntType, FromExpr, ToValueDecl,
1506 ToAddressOf, ToNullPtr, ToExpr);
1507 assert(IsValidFromInt && (ToValueDecl || ToNullPtr));
1508 PrintIntegerAndValueDecl(FromInt, FromIntType, FromExpr,
1509 Tree.FromDefault(), ToValueDecl, ToAddressOf,
1510 ToNullPtr, ToExpr, Tree.ToDefault());
1511 return;
1512 }
Richard Trieub4cff112013-03-15 20:35:18 +00001513 case DiffTree::Template: {
1514 // Node is root of template. Recurse on children.
1515 TemplateDecl *FromTD, *ToTD;
Richard Trieu14714c42016-01-14 22:56:39 +00001516 Qualifiers FromQual, ToQual;
1517 Tree.GetTemplateDiff(FromTD, ToTD, FromQual, ToQual);
Richard Trieu954aaaf2013-02-27 01:41:53 +00001518
Richard Trieu2331c8b2016-01-15 05:48:38 +00001519 PrintQualifiers(FromQual, ToQual);
1520
Richard Trieub4cff112013-03-15 20:35:18 +00001521 if (!Tree.HasChildren()) {
1522 // If we're dealing with a template specialization with zero
1523 // arguments, there are no children; special-case this.
1524 OS << FromTD->getNameAsString() << "<>";
1525 return;
Richard Trieu91844232012-06-26 18:18:47 +00001526 }
Richard Trieub4cff112013-03-15 20:35:18 +00001527
Richard Trieu14714c42016-01-14 22:56:39 +00001528 OS << FromTD->getNameAsString() << '<';
Richard Trieub4cff112013-03-15 20:35:18 +00001529 Tree.MoveToChild();
1530 unsigned NumElideArgs = 0;
Richard Trieu12074502016-02-02 00:36:59 +00001531 bool AllArgsElided = true;
Richard Trieub4cff112013-03-15 20:35:18 +00001532 do {
1533 if (ElideType) {
1534 if (Tree.NodeIsSame()) {
1535 ++NumElideArgs;
1536 continue;
1537 }
Richard Trieu12074502016-02-02 00:36:59 +00001538 AllArgsElided = false;
Richard Trieub4cff112013-03-15 20:35:18 +00001539 if (NumElideArgs > 0) {
1540 PrintElideArgs(NumElideArgs, Indent);
1541 NumElideArgs = 0;
1542 OS << ", ";
1543 }
1544 }
1545 TreeToString(Indent);
1546 if (Tree.HasNextSibling())
1547 OS << ", ";
1548 } while (Tree.AdvanceSibling());
Richard Trieu12074502016-02-02 00:36:59 +00001549 if (NumElideArgs > 0) {
1550 if (AllArgsElided)
1551 OS << "...";
1552 else
1553 PrintElideArgs(NumElideArgs, Indent);
1554 }
Richard Trieu91844232012-06-26 18:18:47 +00001555
Richard Trieub4cff112013-03-15 20:35:18 +00001556 Tree.Parent();
1557 OS << ">";
1558 return;
1559 }
1560 }
Richard Trieu91844232012-06-26 18:18:47 +00001561 }
1562
1563 // To signal to the text printer that a certain text needs to be bolded,
1564 // a special character is injected into the character stream which the
1565 // text printer will later strip out.
1566
1567 /// Bold - Start bolding text.
1568 void Bold() {
1569 assert(!IsBold && "Attempting to bold text that is already bold.");
1570 IsBold = true;
1571 if (ShowColor)
1572 OS << ToggleHighlight;
1573 }
1574
1575 /// Unbold - Stop bolding text.
1576 void Unbold() {
1577 assert(IsBold && "Attempting to remove bold from unbold text.");
1578 IsBold = false;
1579 if (ShowColor)
1580 OS << ToggleHighlight;
1581 }
1582
1583 // Functions to print out the arguments and highlighting the difference.
1584
1585 /// PrintTypeNames - prints the typenames, bolding differences. Will detect
1586 /// typenames that are the same and attempt to disambiguate them by using
1587 /// canonical typenames.
1588 void PrintTypeNames(QualType FromType, QualType ToType,
1589 bool FromDefault, bool ToDefault, bool Same) {
1590 assert((!FromType.isNull() || !ToType.isNull()) &&
1591 "Only one template argument may be missing.");
1592
1593 if (Same) {
Richard Trieud86c9012014-07-25 00:24:02 +00001594 OS << FromType.getAsString(Policy);
Richard Trieu91844232012-06-26 18:18:47 +00001595 return;
1596 }
1597
Richard Trieub7243852012-09-28 20:32:51 +00001598 if (!FromType.isNull() && !ToType.isNull() &&
1599 FromType.getLocalUnqualifiedType() ==
1600 ToType.getLocalUnqualifiedType()) {
1601 Qualifiers FromQual = FromType.getLocalQualifiers(),
Alp Tokeraced95a2013-11-26 02:52:41 +00001602 ToQual = ToType.getLocalQualifiers();
Richard Trieub7243852012-09-28 20:32:51 +00001603 PrintQualifiers(FromQual, ToQual);
1604 FromType.getLocalUnqualifiedType().print(OS, Policy);
1605 return;
1606 }
1607
Richard Trieu91844232012-06-26 18:18:47 +00001608 std::string FromTypeStr = FromType.isNull() ? "(no argument)"
Richard Trieud86c9012014-07-25 00:24:02 +00001609 : FromType.getAsString(Policy);
Richard Trieu91844232012-06-26 18:18:47 +00001610 std::string ToTypeStr = ToType.isNull() ? "(no argument)"
Richard Trieud86c9012014-07-25 00:24:02 +00001611 : ToType.getAsString(Policy);
Richard Trieu91844232012-06-26 18:18:47 +00001612 // Switch to canonical typename if it is better.
1613 // TODO: merge this with other aka printing above.
1614 if (FromTypeStr == ToTypeStr) {
Richard Trieud86c9012014-07-25 00:24:02 +00001615 std::string FromCanTypeStr =
1616 FromType.getCanonicalType().getAsString(Policy);
1617 std::string ToCanTypeStr = ToType.getCanonicalType().getAsString(Policy);
Richard Trieu91844232012-06-26 18:18:47 +00001618 if (FromCanTypeStr != ToCanTypeStr) {
1619 FromTypeStr = FromCanTypeStr;
1620 ToTypeStr = ToCanTypeStr;
1621 }
1622 }
1623
1624 if (PrintTree) OS << '[';
1625 OS << (FromDefault ? "(default) " : "");
1626 Bold();
1627 OS << FromTypeStr;
1628 Unbold();
1629 if (PrintTree) {
1630 OS << " != " << (ToDefault ? "(default) " : "");
1631 Bold();
1632 OS << ToTypeStr;
1633 Unbold();
1634 OS << "]";
1635 }
Richard Trieu91844232012-06-26 18:18:47 +00001636 }
1637
1638 /// PrintExpr - Prints out the expr template arguments, highlighting argument
1639 /// differences.
Richard Trieu2c22a862016-01-15 01:08:56 +00001640 void PrintExpr(const Expr *FromExpr, const Expr *ToExpr, bool FromDefault,
1641 bool ToDefault, bool Same) {
Richard Trieu91844232012-06-26 18:18:47 +00001642 assert((FromExpr || ToExpr) &&
1643 "Only one template argument may be missing.");
1644 if (Same) {
Richard Trieu2c22a862016-01-15 01:08:56 +00001645 PrintExpr(FromExpr);
Richard Trieu91844232012-06-26 18:18:47 +00001646 } else if (!PrintTree) {
1647 OS << (FromDefault ? "(default) " : "");
1648 Bold();
Richard Trieu2c22a862016-01-15 01:08:56 +00001649 PrintExpr(FromExpr);
Richard Trieu91844232012-06-26 18:18:47 +00001650 Unbold();
1651 } else {
1652 OS << (FromDefault ? "[(default) " : "[");
1653 Bold();
Richard Trieu2c22a862016-01-15 01:08:56 +00001654 PrintExpr(FromExpr);
Richard Trieu91844232012-06-26 18:18:47 +00001655 Unbold();
1656 OS << " != " << (ToDefault ? "(default) " : "");
1657 Bold();
Richard Trieu2c22a862016-01-15 01:08:56 +00001658 PrintExpr(ToExpr);
Richard Trieu91844232012-06-26 18:18:47 +00001659 Unbold();
1660 OS << ']';
1661 }
1662 }
1663
1664 /// PrintExpr - Actual formatting and printing of expressions.
Richard Trieu2c22a862016-01-15 01:08:56 +00001665 void PrintExpr(const Expr *E) {
Richard Trieu38800892014-07-24 04:24:50 +00001666 if (E) {
Craig Topper36250ad2014-05-12 05:36:57 +00001667 E->printPretty(OS, nullptr, Policy);
Richard Trieu38800892014-07-24 04:24:50 +00001668 return;
1669 }
Richard Trieu38800892014-07-24 04:24:50 +00001670 OS << "(no argument)";
Richard Trieu91844232012-06-26 18:18:47 +00001671 }
1672
1673 /// PrintTemplateTemplate - Handles printing of template template arguments,
1674 /// highlighting argument differences.
1675 void PrintTemplateTemplate(TemplateDecl *FromTD, TemplateDecl *ToTD,
1676 bool FromDefault, bool ToDefault, bool Same) {
1677 assert((FromTD || ToTD) && "Only one template argument may be missing.");
Richard Trieue673d71a2013-01-31 02:47:46 +00001678
1679 std::string FromName = FromTD ? FromTD->getName() : "(no argument)";
1680 std::string ToName = ToTD ? ToTD->getName() : "(no argument)";
1681 if (FromTD && ToTD && FromName == ToName) {
1682 FromName = FromTD->getQualifiedNameAsString();
1683 ToName = ToTD->getQualifiedNameAsString();
1684 }
1685
Richard Trieu91844232012-06-26 18:18:47 +00001686 if (Same) {
1687 OS << "template " << FromTD->getNameAsString();
1688 } else if (!PrintTree) {
1689 OS << (FromDefault ? "(default) template " : "template ");
1690 Bold();
Richard Trieue673d71a2013-01-31 02:47:46 +00001691 OS << FromName;
Richard Trieu91844232012-06-26 18:18:47 +00001692 Unbold();
1693 } else {
1694 OS << (FromDefault ? "[(default) template " : "[template ");
1695 Bold();
Richard Trieue673d71a2013-01-31 02:47:46 +00001696 OS << FromName;
Richard Trieu91844232012-06-26 18:18:47 +00001697 Unbold();
1698 OS << " != " << (ToDefault ? "(default) template " : "template ");
1699 Bold();
Richard Trieue673d71a2013-01-31 02:47:46 +00001700 OS << ToName;
Richard Trieu91844232012-06-26 18:18:47 +00001701 Unbold();
1702 OS << ']';
1703 }
1704 }
1705
Richard Trieu6df89452012-11-01 21:29:28 +00001706 /// PrintAPSInt - Handles printing of integral arguments, highlighting
1707 /// argument differences.
Benjamin Kramer7320b992016-06-15 14:20:56 +00001708 void PrintAPSInt(const llvm::APSInt &FromInt, const llvm::APSInt &ToInt,
Richard Trieud5c73782016-01-15 02:55:17 +00001709 bool IsValidFromInt, bool IsValidToInt, QualType FromIntType,
1710 QualType ToIntType, Expr *FromExpr, Expr *ToExpr,
1711 bool FromDefault, bool ToDefault, bool Same) {
Richard Trieu6df89452012-11-01 21:29:28 +00001712 assert((IsValidFromInt || IsValidToInt) &&
1713 "Only one integral argument may be missing.");
1714
1715 if (Same) {
Richard Trieud5c73782016-01-15 02:55:17 +00001716 if (FromIntType->isBooleanType()) {
1717 OS << ((FromInt == 0) ? "false" : "true");
1718 } else {
1719 OS << FromInt.toString(10);
1720 }
1721 return;
1722 }
1723
1724 bool PrintType = IsValidFromInt && IsValidToInt &&
1725 !Context.hasSameType(FromIntType, ToIntType);
1726
1727 if (!PrintTree) {
Richard Trieu6df89452012-11-01 21:29:28 +00001728 OS << (FromDefault ? "(default) " : "");
Richard Trieud5c73782016-01-15 02:55:17 +00001729 PrintAPSInt(FromInt, FromExpr, IsValidFromInt, FromIntType, PrintType);
Richard Trieu6df89452012-11-01 21:29:28 +00001730 } else {
1731 OS << (FromDefault ? "[(default) " : "[");
Richard Trieud5c73782016-01-15 02:55:17 +00001732 PrintAPSInt(FromInt, FromExpr, IsValidFromInt, FromIntType, PrintType);
Richard Trieu6df89452012-11-01 21:29:28 +00001733 OS << " != " << (ToDefault ? "(default) " : "");
Richard Trieud5c73782016-01-15 02:55:17 +00001734 PrintAPSInt(ToInt, ToExpr, IsValidToInt, ToIntType, PrintType);
Richard Trieu6df89452012-11-01 21:29:28 +00001735 OS << ']';
1736 }
1737 }
1738
Richard Trieu64ab30392013-03-15 23:55:09 +00001739 /// PrintAPSInt - If valid, print the APSInt. If the expression is
1740 /// gives more information, print it too.
Benjamin Kramer7320b992016-06-15 14:20:56 +00001741 void PrintAPSInt(const llvm::APSInt &Val, Expr *E, bool Valid,
1742 QualType IntType, bool PrintType) {
Richard Trieu64ab30392013-03-15 23:55:09 +00001743 Bold();
1744 if (Valid) {
1745 if (HasExtraInfo(E)) {
1746 PrintExpr(E);
1747 Unbold();
1748 OS << " aka ";
1749 Bold();
1750 }
Richard Trieud5c73782016-01-15 02:55:17 +00001751 if (PrintType) {
1752 Unbold();
1753 OS << "(";
1754 Bold();
1755 IntType.print(OS, Context.getPrintingPolicy());
1756 Unbold();
1757 OS << ") ";
1758 Bold();
1759 }
1760 if (IntType->isBooleanType()) {
1761 OS << ((Val == 0) ? "false" : "true");
1762 } else {
1763 OS << Val.toString(10);
1764 }
Nikola Smiljanic3fe1e092014-07-01 04:17:53 +00001765 } else if (E) {
1766 PrintExpr(E);
Richard Trieu64ab30392013-03-15 23:55:09 +00001767 } else {
1768 OS << "(no argument)";
1769 }
1770 Unbold();
1771 }
Richard Trieu15b66532015-01-24 02:48:32 +00001772
Richard Trieu14714c42016-01-14 22:56:39 +00001773 /// HasExtraInfo - Returns true if E is not an integer literal, the
1774 /// negation of an integer literal, or a boolean literal.
Richard Trieu64ab30392013-03-15 23:55:09 +00001775 bool HasExtraInfo(Expr *E) {
1776 if (!E) return false;
Richard Trieu15b66532015-01-24 02:48:32 +00001777
1778 E = E->IgnoreImpCasts();
1779
Richard Trieu64ab30392013-03-15 23:55:09 +00001780 if (isa<IntegerLiteral>(E)) return false;
1781
1782 if (UnaryOperator *UO = dyn_cast<UnaryOperator>(E))
1783 if (UO->getOpcode() == UO_Minus)
1784 if (isa<IntegerLiteral>(UO->getSubExpr()))
1785 return false;
1786
Richard Trieu14714c42016-01-14 22:56:39 +00001787 if (isa<CXXBoolLiteralExpr>(E))
1788 return false;
1789
Richard Trieu64ab30392013-03-15 23:55:09 +00001790 return true;
1791 }
1792
Richard Trieu2c22a862016-01-15 01:08:56 +00001793 void PrintValueDecl(ValueDecl *VD, bool AddressOf, Expr *E, bool NullPtr) {
Richard Trieu38800892014-07-24 04:24:50 +00001794 if (VD) {
1795 if (AddressOf)
1796 OS << "&";
1797 OS << VD->getName();
1798 return;
1799 }
1800
1801 if (NullPtr) {
Richard Trieu2c22a862016-01-15 01:08:56 +00001802 if (E && !isa<CXXNullPtrLiteralExpr>(E)) {
1803 PrintExpr(E);
1804 if (IsBold) {
1805 Unbold();
1806 OS << " aka ";
1807 Bold();
1808 } else {
1809 OS << " aka ";
1810 }
1811 }
1812
Richard Trieu38800892014-07-24 04:24:50 +00001813 OS << "nullptr";
1814 return;
1815 }
1816
1817 OS << "(no argument)";
1818 }
1819
Richard Trieu954aaaf2013-02-27 01:41:53 +00001820 /// PrintDecl - Handles printing of Decl arguments, highlighting
1821 /// argument differences.
1822 void PrintValueDecl(ValueDecl *FromValueDecl, ValueDecl *ToValueDecl,
Richard Trieu38800892014-07-24 04:24:50 +00001823 bool FromAddressOf, bool ToAddressOf, bool FromNullPtr,
Richard Trieu2c22a862016-01-15 01:08:56 +00001824 bool ToNullPtr, Expr *FromExpr, Expr *ToExpr,
1825 bool FromDefault, bool ToDefault, bool Same) {
Richard Trieu38800892014-07-24 04:24:50 +00001826 assert((FromValueDecl || FromNullPtr || ToValueDecl || ToNullPtr) &&
Richard Trieu954aaaf2013-02-27 01:41:53 +00001827 "Only one Decl argument may be NULL");
1828
1829 if (Same) {
Richard Trieu2c22a862016-01-15 01:08:56 +00001830 PrintValueDecl(FromValueDecl, FromAddressOf, FromExpr, FromNullPtr);
Richard Trieu954aaaf2013-02-27 01:41:53 +00001831 } else if (!PrintTree) {
1832 OS << (FromDefault ? "(default) " : "");
1833 Bold();
Richard Trieu2c22a862016-01-15 01:08:56 +00001834 PrintValueDecl(FromValueDecl, FromAddressOf, FromExpr, FromNullPtr);
Richard Trieu954aaaf2013-02-27 01:41:53 +00001835 Unbold();
1836 } else {
1837 OS << (FromDefault ? "[(default) " : "[");
1838 Bold();
Richard Trieu2c22a862016-01-15 01:08:56 +00001839 PrintValueDecl(FromValueDecl, FromAddressOf, FromExpr, FromNullPtr);
Richard Trieu954aaaf2013-02-27 01:41:53 +00001840 Unbold();
1841 OS << " != " << (ToDefault ? "(default) " : "");
1842 Bold();
Richard Trieu2c22a862016-01-15 01:08:56 +00001843 PrintValueDecl(ToValueDecl, ToAddressOf, ToExpr, ToNullPtr);
Richard Trieu954aaaf2013-02-27 01:41:53 +00001844 Unbold();
1845 OS << ']';
1846 }
Richard Trieu954aaaf2013-02-27 01:41:53 +00001847 }
1848
Richard Trieu9213ce52016-01-15 05:01:53 +00001849 /// PrintValueDeclAndInteger - Uses the print functions for ValueDecl and
1850 /// APSInt to print a mixed difference.
1851 void PrintValueDeclAndInteger(ValueDecl *VD, bool NeedAddressOf,
1852 bool IsNullPtr, Expr *VDExpr, bool DefaultDecl,
Benjamin Kramer7320b992016-06-15 14:20:56 +00001853 const llvm::APSInt &Val, QualType IntType,
Richard Trieu9213ce52016-01-15 05:01:53 +00001854 Expr *IntExpr, bool DefaultInt) {
1855 if (!PrintTree) {
1856 OS << (DefaultDecl ? "(default) " : "");
1857 Bold();
1858 PrintValueDecl(VD, NeedAddressOf, VDExpr, IsNullPtr);
1859 Unbold();
1860 } else {
1861 OS << (DefaultDecl ? "[(default) " : "[");
1862 Bold();
1863 PrintValueDecl(VD, NeedAddressOf, VDExpr, IsNullPtr);
1864 Unbold();
1865 OS << " != " << (DefaultInt ? "(default) " : "");
1866 PrintAPSInt(Val, IntExpr, true /*Valid*/, IntType, false /*PrintType*/);
1867 OS << ']';
1868 }
1869 }
1870
1871 /// PrintIntegerAndValueDecl - Uses the print functions for APSInt and
1872 /// ValueDecl to print a mixed difference.
Benjamin Kramer7320b992016-06-15 14:20:56 +00001873 void PrintIntegerAndValueDecl(const llvm::APSInt &Val, QualType IntType,
Richard Trieu9213ce52016-01-15 05:01:53 +00001874 Expr *IntExpr, bool DefaultInt, ValueDecl *VD,
1875 bool NeedAddressOf, bool IsNullPtr,
1876 Expr *VDExpr, bool DefaultDecl) {
1877 if (!PrintTree) {
1878 OS << (DefaultInt ? "(default) " : "");
1879 PrintAPSInt(Val, IntExpr, true /*Valid*/, IntType, false /*PrintType*/);
1880 } else {
1881 OS << (DefaultInt ? "[(default) " : "[");
1882 PrintAPSInt(Val, IntExpr, true /*Valid*/, IntType, false /*PrintType*/);
1883 OS << " != " << (DefaultDecl ? "(default) " : "");
1884 Bold();
1885 PrintValueDecl(VD, NeedAddressOf, VDExpr, IsNullPtr);
1886 Unbold();
1887 OS << ']';
1888 }
1889 }
1890
Richard Trieu91844232012-06-26 18:18:47 +00001891 // Prints the appropriate placeholder for elided template arguments.
1892 void PrintElideArgs(unsigned NumElideArgs, unsigned Indent) {
1893 if (PrintTree) {
1894 OS << '\n';
1895 for (unsigned i = 0; i < Indent; ++i)
1896 OS << " ";
1897 }
1898 if (NumElideArgs == 0) return;
1899 if (NumElideArgs == 1)
1900 OS << "[...]";
1901 else
1902 OS << "[" << NumElideArgs << " * ...]";
1903 }
1904
Richard Trieub7243852012-09-28 20:32:51 +00001905 // Prints and highlights differences in Qualifiers.
1906 void PrintQualifiers(Qualifiers FromQual, Qualifiers ToQual) {
1907 // Both types have no qualifiers
1908 if (FromQual.empty() && ToQual.empty())
1909 return;
1910
1911 // Both types have same qualifiers
1912 if (FromQual == ToQual) {
1913 PrintQualifier(FromQual, /*ApplyBold*/false);
1914 return;
1915 }
1916
1917 // Find common qualifiers and strip them from FromQual and ToQual.
1918 Qualifiers CommonQual = Qualifiers::removeCommonQualifiers(FromQual,
1919 ToQual);
1920
1921 // The qualifiers are printed before the template name.
1922 // Inline printing:
1923 // The common qualifiers are printed. Then, qualifiers only in this type
1924 // are printed and highlighted. Finally, qualifiers only in the other
1925 // type are printed and highlighted inside parentheses after "missing".
1926 // Tree printing:
1927 // Qualifiers are printed next to each other, inside brackets, and
1928 // separated by "!=". The printing order is:
1929 // common qualifiers, highlighted from qualifiers, "!=",
1930 // common qualifiers, highlighted to qualifiers
1931 if (PrintTree) {
1932 OS << "[";
1933 if (CommonQual.empty() && FromQual.empty()) {
1934 Bold();
1935 OS << "(no qualifiers) ";
1936 Unbold();
1937 } else {
1938 PrintQualifier(CommonQual, /*ApplyBold*/false);
1939 PrintQualifier(FromQual, /*ApplyBold*/true);
1940 }
1941 OS << "!= ";
1942 if (CommonQual.empty() && ToQual.empty()) {
1943 Bold();
1944 OS << "(no qualifiers)";
1945 Unbold();
1946 } else {
1947 PrintQualifier(CommonQual, /*ApplyBold*/false,
1948 /*appendSpaceIfNonEmpty*/!ToQual.empty());
1949 PrintQualifier(ToQual, /*ApplyBold*/true,
1950 /*appendSpaceIfNonEmpty*/false);
1951 }
1952 OS << "] ";
1953 } else {
1954 PrintQualifier(CommonQual, /*ApplyBold*/false);
1955 PrintQualifier(FromQual, /*ApplyBold*/true);
1956 }
1957 }
1958
1959 void PrintQualifier(Qualifiers Q, bool ApplyBold,
1960 bool AppendSpaceIfNonEmpty = true) {
1961 if (Q.empty()) return;
1962 if (ApplyBold) Bold();
1963 Q.print(OS, Policy, AppendSpaceIfNonEmpty);
1964 if (ApplyBold) Unbold();
1965 }
1966
Richard Trieu91844232012-06-26 18:18:47 +00001967public:
1968
Benjamin Kramer8de90462013-02-22 16:08:12 +00001969 TemplateDiff(raw_ostream &OS, ASTContext &Context, QualType FromType,
1970 QualType ToType, bool PrintTree, bool PrintFromType,
1971 bool ElideType, bool ShowColor)
Richard Trieu91844232012-06-26 18:18:47 +00001972 : Context(Context),
1973 Policy(Context.getLangOpts()),
1974 ElideType(ElideType),
1975 PrintTree(PrintTree),
1976 ShowColor(ShowColor),
1977 // When printing a single type, the FromType is the one printed.
Richard Trieu2331c8b2016-01-15 05:48:38 +00001978 FromTemplateType(PrintFromType ? FromType : ToType),
1979 ToTemplateType(PrintFromType ? ToType : FromType),
Benjamin Kramer8de90462013-02-22 16:08:12 +00001980 OS(OS),
Richard Trieu91844232012-06-26 18:18:47 +00001981 IsBold(false) {
1982 }
1983
1984 /// DiffTemplate - Start the template type diffing.
1985 void DiffTemplate() {
Richard Trieu2331c8b2016-01-15 05:48:38 +00001986 Qualifiers FromQual = FromTemplateType.getQualifiers(),
1987 ToQual = ToTemplateType.getQualifiers();
Richard Trieub7243852012-09-28 20:32:51 +00001988
Richard Trieu91844232012-06-26 18:18:47 +00001989 const TemplateSpecializationType *FromOrigTST =
Richard Trieu2331c8b2016-01-15 05:48:38 +00001990 GetTemplateSpecializationType(Context, FromTemplateType);
Richard Trieu91844232012-06-26 18:18:47 +00001991 const TemplateSpecializationType *ToOrigTST =
Richard Trieu2331c8b2016-01-15 05:48:38 +00001992 GetTemplateSpecializationType(Context, ToTemplateType);
Richard Trieu91844232012-06-26 18:18:47 +00001993
1994 // Only checking templates.
1995 if (!FromOrigTST || !ToOrigTST)
1996 return;
1997
1998 // Different base templates.
1999 if (!hasSameTemplate(FromOrigTST, ToOrigTST)) {
2000 return;
2001 }
2002
Richard Trieub7243852012-09-28 20:32:51 +00002003 FromQual -= QualType(FromOrigTST, 0).getQualifiers();
2004 ToQual -= QualType(ToOrigTST, 0).getQualifiers();
Richard Trieu91844232012-06-26 18:18:47 +00002005
2006 // Same base template, but different arguments.
Richard Trieu14714c42016-01-14 22:56:39 +00002007 Tree.SetTemplateDiff(FromOrigTST->getTemplateName().getAsTemplateDecl(),
2008 ToOrigTST->getTemplateName().getAsTemplateDecl(),
2009 FromQual, ToQual, false /*FromDefault*/,
2010 false /*ToDefault*/);
Richard Trieu91844232012-06-26 18:18:47 +00002011
2012 DiffTemplate(FromOrigTST, ToOrigTST);
David Blaikie47e45182012-06-26 18:52:09 +00002013 }
Richard Trieu91844232012-06-26 18:18:47 +00002014
Benjamin Kramer6582c362013-02-22 16:13:34 +00002015 /// Emit - When the two types given are templated types with the same
Richard Trieu91844232012-06-26 18:18:47 +00002016 /// base template, a string representation of the type difference will be
Benjamin Kramer6582c362013-02-22 16:13:34 +00002017 /// emitted to the stream and return true. Otherwise, return false.
Benjamin Kramer8de90462013-02-22 16:08:12 +00002018 bool Emit() {
Richard Trieu91844232012-06-26 18:18:47 +00002019 Tree.StartTraverse();
2020 if (Tree.Empty())
2021 return false;
2022
2023 TreeToString();
2024 assert(!IsBold && "Bold is applied to end of string.");
Richard Trieu91844232012-06-26 18:18:47 +00002025 return true;
2026 }
2027}; // end class TemplateDiff
Eugene Zelenko0a4f3f42016-02-10 19:11:58 +00002028} // end anonymous namespace
Richard Trieu91844232012-06-26 18:18:47 +00002029
2030/// FormatTemplateTypeDiff - A helper static function to start the template
2031/// diff and return the properly formatted string. Returns true if the diff
2032/// is successful.
2033static bool FormatTemplateTypeDiff(ASTContext &Context, QualType FromType,
2034 QualType ToType, bool PrintTree,
2035 bool PrintFromType, bool ElideType,
Benjamin Kramer8de90462013-02-22 16:08:12 +00002036 bool ShowColors, raw_ostream &OS) {
Richard Trieu91844232012-06-26 18:18:47 +00002037 if (PrintTree)
2038 PrintFromType = true;
Benjamin Kramer8de90462013-02-22 16:08:12 +00002039 TemplateDiff TD(OS, Context, FromType, ToType, PrintTree, PrintFromType,
Richard Trieu91844232012-06-26 18:18:47 +00002040 ElideType, ShowColors);
2041 TD.DiffTemplate();
Benjamin Kramer8de90462013-02-22 16:08:12 +00002042 return TD.Emit();
Richard Trieu91844232012-06-26 18:18:47 +00002043}