blob: 8ff3cebb1d83f6c80b4b958e3556a602a4c8ecf1 [file] [log] [blame]
Douglas Gregor639cccc2010-02-09 22:26:47 +00001//===--- ASTDiagnostic.cpp - Diagnostic Printing Hooks for AST Nodes ------===//
2//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
Douglas Gregor639cccc2010-02-09 22:26:47 +00006//
7//===----------------------------------------------------------------------===//
8//
9// This file implements a diagnostic formatting hook for AST elements.
10//
11//===----------------------------------------------------------------------===//
Eugene Zelenko0a4f3f42016-02-10 19:11:58 +000012
Douglas Gregor639cccc2010-02-09 22:26:47 +000013#include "clang/AST/ASTDiagnostic.h"
Douglas Gregor639cccc2010-02-09 22:26:47 +000014#include "clang/AST/ASTContext.h"
Alp Tokerfb8d02b2014-06-05 22:10:59 +000015#include "clang/AST/ASTLambda.h"
Aaron Ballman3e424b52013-12-26 18:30:57 +000016#include "clang/AST/Attr.h"
Douglas Gregor639cccc2010-02-09 22:26:47 +000017#include "clang/AST/DeclObjC.h"
Richard Trieu91844232012-06-26 18:18:47 +000018#include "clang/AST/DeclTemplate.h"
Chandler Carruth3a022472012-12-04 09:13:33 +000019#include "clang/AST/ExprCXX.h"
20#include "clang/AST/TemplateBase.h"
Douglas Gregor639cccc2010-02-09 22:26:47 +000021#include "clang/AST/Type.h"
22#include "llvm/Support/raw_ostream.h"
23
24using namespace clang;
25
Chandler Carruthd102f2d2010-05-13 11:37:24 +000026// Returns a desugared version of the QualType, and marks ShouldAKA as true
27// whenever we remove significant sugar from the type.
28static QualType Desugar(ASTContext &Context, QualType QT, bool &ShouldAKA) {
29 QualifierCollector QC;
30
Douglas Gregor639cccc2010-02-09 22:26:47 +000031 while (true) {
Chandler Carruthd102f2d2010-05-13 11:37:24 +000032 const Type *Ty = QC.strip(QT);
33
Douglas Gregor639cccc2010-02-09 22:26:47 +000034 // Don't aka just because we saw an elaborated type...
Richard Smith30482bc2011-02-20 03:19:35 +000035 if (const ElaboratedType *ET = dyn_cast<ElaboratedType>(Ty)) {
36 QT = ET->desugar();
Douglas Gregor639cccc2010-02-09 22:26:47 +000037 continue;
38 }
Abramo Bagnara924a8f32010-12-10 16:29:40 +000039 // ... or a paren type ...
Richard Smith30482bc2011-02-20 03:19:35 +000040 if (const ParenType *PT = dyn_cast<ParenType>(Ty)) {
41 QT = PT->desugar();
Abramo Bagnara924a8f32010-12-10 16:29:40 +000042 continue;
43 }
Leonard Chanc72aaf62019-05-07 03:20:17 +000044 // ... or a macro defined type ...
45 if (const MacroQualifiedType *MDT = dyn_cast<MacroQualifiedType>(Ty)) {
46 QT = MDT->desugar();
47 continue;
48 }
Richard Smith30482bc2011-02-20 03:19:35 +000049 // ...or a substituted template type parameter ...
50 if (const SubstTemplateTypeParmType *ST =
51 dyn_cast<SubstTemplateTypeParmType>(Ty)) {
52 QT = ST->desugar();
53 continue;
54 }
John McCall4223a9e2011-03-04 04:00:19 +000055 // ...or an attributed type...
56 if (const AttributedType *AT = dyn_cast<AttributedType>(Ty)) {
57 QT = AT->desugar();
58 continue;
59 }
Reid Kleckner0503a872013-12-05 01:23:43 +000060 // ...or an adjusted type...
61 if (const AdjustedType *AT = dyn_cast<AdjustedType>(Ty)) {
62 QT = AT->desugar();
63 continue;
64 }
Richard Smith30482bc2011-02-20 03:19:35 +000065 // ... or an auto type.
66 if (const AutoType *AT = dyn_cast<AutoType>(Ty)) {
67 if (!AT->isSugared())
68 break;
69 QT = AT->desugar();
Douglas Gregor639cccc2010-02-09 22:26:47 +000070 continue;
71 }
Chandler Carruthd102f2d2010-05-13 11:37:24 +000072
Nikola Smiljanicfa007282015-07-16 01:06:17 +000073 // Desugar FunctionType if return type or any parameter type should be
74 // desugared. Preserve nullability attribute on desugared types.
75 if (const FunctionType *FT = dyn_cast<FunctionType>(Ty)) {
76 bool DesugarReturn = false;
77 QualType SugarRT = FT->getReturnType();
78 QualType RT = Desugar(Context, SugarRT, DesugarReturn);
79 if (auto nullability = AttributedType::stripOuterNullability(SugarRT)) {
80 RT = Context.getAttributedType(
81 AttributedType::getNullabilityAttrKind(*nullability), RT, RT);
82 }
83
84 bool DesugarArgument = false;
85 SmallVector<QualType, 4> Args;
86 const FunctionProtoType *FPT = dyn_cast<FunctionProtoType>(FT);
87 if (FPT) {
88 for (QualType SugarPT : FPT->param_types()) {
89 QualType PT = Desugar(Context, SugarPT, DesugarArgument);
90 if (auto nullability =
91 AttributedType::stripOuterNullability(SugarPT)) {
92 PT = Context.getAttributedType(
93 AttributedType::getNullabilityAttrKind(*nullability), PT, PT);
94 }
95 Args.push_back(PT);
96 }
97 }
98
99 if (DesugarReturn || DesugarArgument) {
100 ShouldAKA = true;
101 QT = FPT ? Context.getFunctionType(RT, Args, FPT->getExtProtoInfo())
102 : Context.getFunctionNoProtoType(RT, FT->getExtInfo());
Richard Smith3f1b5d02011-05-05 21:57:07 +0000103 break;
Nikola Smiljanicfa007282015-07-16 01:06:17 +0000104 }
105 }
106
107 // Desugar template specializations if any template argument should be
108 // desugared.
109 if (const TemplateSpecializationType *TST =
110 dyn_cast<TemplateSpecializationType>(Ty)) {
111 if (!TST->isTypeAlias()) {
112 bool DesugarArgument = false;
113 SmallVector<TemplateArgument, 4> Args;
114 for (unsigned I = 0, N = TST->getNumArgs(); I != N; ++I) {
115 const TemplateArgument &Arg = TST->getArg(I);
116 if (Arg.getKind() == TemplateArgument::Type)
117 Args.push_back(Desugar(Context, Arg.getAsType(), DesugarArgument));
118 else
119 Args.push_back(Arg);
120 }
121
122 if (DesugarArgument) {
123 ShouldAKA = true;
124 QT = Context.getTemplateSpecializationType(
David Majnemer6fbeee32016-07-07 04:43:07 +0000125 TST->getTemplateName(), Args, QT);
Nikola Smiljanicfa007282015-07-16 01:06:17 +0000126 }
127 break;
128 }
129 }
Chandler Carruthd102f2d2010-05-13 11:37:24 +0000130
Douglas Gregor639cccc2010-02-09 22:26:47 +0000131 // Don't desugar magic Objective-C types.
132 if (QualType(Ty,0) == Context.getObjCIdType() ||
133 QualType(Ty,0) == Context.getObjCClassType() ||
134 QualType(Ty,0) == Context.getObjCSelType() ||
135 QualType(Ty,0) == Context.getObjCProtoType())
136 break;
Chandler Carruthd102f2d2010-05-13 11:37:24 +0000137
Douglas Gregor639cccc2010-02-09 22:26:47 +0000138 // Don't desugar va_list.
Charles Davisc7d5c942015-09-17 20:55:33 +0000139 if (QualType(Ty, 0) == Context.getBuiltinVaListType() ||
140 QualType(Ty, 0) == Context.getBuiltinMSVaListType())
Douglas Gregor639cccc2010-02-09 22:26:47 +0000141 break;
Chandler Carruthd102f2d2010-05-13 11:37:24 +0000142
Douglas Gregor639cccc2010-02-09 22:26:47 +0000143 // Otherwise, do a single-step desugar.
144 QualType Underlying;
145 bool IsSugar = false;
146 switch (Ty->getTypeClass()) {
147#define ABSTRACT_TYPE(Class, Base)
148#define TYPE(Class, Base) \
149case Type::Class: { \
150const Class##Type *CTy = cast<Class##Type>(Ty); \
151if (CTy->isSugared()) { \
152IsSugar = true; \
153Underlying = CTy->desugar(); \
154} \
155break; \
156}
John McCall36b12a82019-10-02 06:35:23 +0000157#include "clang/AST/TypeNodes.inc"
Douglas Gregor639cccc2010-02-09 22:26:47 +0000158 }
Chandler Carruthd102f2d2010-05-13 11:37:24 +0000159
Douglas Gregor639cccc2010-02-09 22:26:47 +0000160 // If it wasn't sugared, we're done.
161 if (!IsSugar)
162 break;
Chandler Carruthd102f2d2010-05-13 11:37:24 +0000163
Douglas Gregor639cccc2010-02-09 22:26:47 +0000164 // If the desugared type is a vector type, we don't want to expand
165 // it, it will turn into an attribute mess. People want their "vec4".
166 if (isa<VectorType>(Underlying))
167 break;
Chandler Carruthd102f2d2010-05-13 11:37:24 +0000168
Douglas Gregor639cccc2010-02-09 22:26:47 +0000169 // Don't desugar through the primary typedef of an anonymous type.
Chris Lattneredbdff62010-09-04 23:16:01 +0000170 if (const TagType *UTT = Underlying->getAs<TagType>())
171 if (const TypedefType *QTT = dyn_cast<TypedefType>(QT))
Richard Smithdda56e42011-04-15 14:24:37 +0000172 if (UTT->getDecl()->getTypedefNameForAnonDecl() == QTT->getDecl())
Chris Lattneredbdff62010-09-04 23:16:01 +0000173 break;
Chandler Carruthd102f2d2010-05-13 11:37:24 +0000174
175 // Record that we actually looked through an opaque type here.
176 ShouldAKA = true;
Douglas Gregor639cccc2010-02-09 22:26:47 +0000177 QT = Underlying;
Douglas Gregor639cccc2010-02-09 22:26:47 +0000178 }
Chandler Carruthd102f2d2010-05-13 11:37:24 +0000179
180 // If we have a pointer-like type, desugar the pointee as well.
181 // FIXME: Handle other pointer-like types.
182 if (const PointerType *Ty = QT->getAs<PointerType>()) {
Chris Lattneredbdff62010-09-04 23:16:01 +0000183 QT = Context.getPointerType(Desugar(Context, Ty->getPointeeType(),
184 ShouldAKA));
Douglas Gregore9d95f12015-07-07 03:57:35 +0000185 } else if (const auto *Ty = QT->getAs<ObjCObjectPointerType>()) {
186 QT = Context.getObjCObjectPointerType(Desugar(Context, Ty->getPointeeType(),
187 ShouldAKA));
Chandler Carruthd102f2d2010-05-13 11:37:24 +0000188 } else if (const LValueReferenceType *Ty = QT->getAs<LValueReferenceType>()) {
Chris Lattneredbdff62010-09-04 23:16:01 +0000189 QT = Context.getLValueReferenceType(Desugar(Context, Ty->getPointeeType(),
190 ShouldAKA));
Douglas Gregor7a2a1162011-01-20 16:08:06 +0000191 } else if (const RValueReferenceType *Ty = QT->getAs<RValueReferenceType>()) {
192 QT = Context.getRValueReferenceType(Desugar(Context, Ty->getPointeeType(),
193 ShouldAKA));
Douglas Gregore9d95f12015-07-07 03:57:35 +0000194 } else if (const auto *Ty = QT->getAs<ObjCObjectType>()) {
195 if (Ty->getBaseType().getTypePtr() != Ty && !ShouldAKA) {
196 QualType BaseType = Desugar(Context, Ty->getBaseType(), ShouldAKA);
197 QT = Context.getObjCObjectType(BaseType, Ty->getTypeArgsAsWritten(),
198 llvm::makeArrayRef(Ty->qual_begin(),
Douglas Gregorab209d82015-07-07 03:58:42 +0000199 Ty->getNumProtocols()),
200 Ty->isKindOfTypeAsWritten());
Douglas Gregore9d95f12015-07-07 03:57:35 +0000201 }
Douglas Gregor639cccc2010-02-09 22:26:47 +0000202 }
Chandler Carruthd102f2d2010-05-13 11:37:24 +0000203
John McCall717d9b02010-12-10 11:01:00 +0000204 return QC.apply(Context, QT);
Douglas Gregor639cccc2010-02-09 22:26:47 +0000205}
206
Fangrui Song6907ce22018-07-30 19:24:48 +0000207/// Convert the given type to a string suitable for printing as part of
Chandler Carruthd102f2d2010-05-13 11:37:24 +0000208/// a diagnostic.
209///
Chandler Carruthd5173952011-07-11 17:49:21 +0000210/// There are four main criteria when determining whether we should have an
Chandler Carruthd102f2d2010-05-13 11:37:24 +0000211/// a.k.a. clause when pretty-printing a type:
212///
213/// 1) Some types provide very minimal sugar that doesn't impede the
214/// user's understanding --- for example, elaborated type
215/// specifiers. If this is all the sugar we see, we don't want an
216/// a.k.a. clause.
217/// 2) Some types are technically sugared but are much more familiar
218/// when seen in their sugared form --- for example, va_list,
219/// vector types, and the magic Objective C types. We don't
220/// want to desugar these, even if we do produce an a.k.a. clause.
221/// 3) Some types may have already been desugared previously in this diagnostic.
222/// if this is the case, doing another "aka" would just be clutter.
Chandler Carruthd5173952011-07-11 17:49:21 +0000223/// 4) Two different types within the same diagnostic have the same output
224/// string. In this case, force an a.k.a with the desugared type when
225/// doing so will provide additional information.
Douglas Gregor639cccc2010-02-09 22:26:47 +0000226///
227/// \param Context the context in which the type was allocated
228/// \param Ty the type to print
Chandler Carruthd5173952011-07-11 17:49:21 +0000229/// \param QualTypeVals pointer values to QualTypes which are used in the
230/// diagnostic message
Douglas Gregor639cccc2010-02-09 22:26:47 +0000231static std::string
232ConvertTypeToDiagnosticString(ASTContext &Context, QualType Ty,
Craig Toppere4753502014-06-12 05:32:27 +0000233 ArrayRef<DiagnosticsEngine::ArgumentValue> PrevArgs,
234 ArrayRef<intptr_t> QualTypeVals) {
Douglas Gregor639cccc2010-02-09 22:26:47 +0000235 // FIXME: Playing with std::string is really slow.
Chandler Carruthd5173952011-07-11 17:49:21 +0000236 bool ForceAKA = false;
237 QualType CanTy = Ty.getCanonicalType();
Douglas Gregorc0b07282011-09-27 22:38:19 +0000238 std::string S = Ty.getAsString(Context.getPrintingPolicy());
239 std::string CanS = CanTy.getAsString(Context.getPrintingPolicy());
Chandler Carruthd5173952011-07-11 17:49:21 +0000240
Bill Wendling8eb771d2012-02-22 09:51:33 +0000241 for (unsigned I = 0, E = QualTypeVals.size(); I != E; ++I) {
Chandler Carruthd5173952011-07-11 17:49:21 +0000242 QualType CompareTy =
Bill Wendling8eb771d2012-02-22 09:51:33 +0000243 QualType::getFromOpaquePtr(reinterpret_cast<void*>(QualTypeVals[I]));
Richard Smithbcc22fc2012-03-09 08:00:36 +0000244 if (CompareTy.isNull())
245 continue;
Chandler Carruthd5173952011-07-11 17:49:21 +0000246 if (CompareTy == Ty)
247 continue; // Same types
248 QualType CompareCanTy = CompareTy.getCanonicalType();
249 if (CompareCanTy == CanTy)
250 continue; // Same canonical types
Douglas Gregorc0b07282011-09-27 22:38:19 +0000251 std::string CompareS = CompareTy.getAsString(Context.getPrintingPolicy());
Justin Bogner25bf8cb2015-07-08 18:32:26 +0000252 bool ShouldAKA = false;
253 QualType CompareDesugar = Desugar(Context, CompareTy, ShouldAKA);
Richard Trieu5d1aff02011-11-14 19:39:25 +0000254 std::string CompareDesugarStr =
255 CompareDesugar.getAsString(Context.getPrintingPolicy());
256 if (CompareS != S && CompareDesugarStr != S)
257 continue; // The type string is different than the comparison string
258 // and the desugared comparison string.
259 std::string CompareCanS =
260 CompareCanTy.getAsString(Context.getPrintingPolicy());
Fangrui Song6907ce22018-07-30 19:24:48 +0000261
Chandler Carruthd5173952011-07-11 17:49:21 +0000262 if (CompareCanS == CanS)
263 continue; // No new info from canonical type
264
265 ForceAKA = true;
266 break;
267 }
Chandler Carruthd102f2d2010-05-13 11:37:24 +0000268
269 // Check to see if we already desugared this type in this
270 // diagnostic. If so, don't do it again.
271 bool Repeated = false;
Craig Toppere4753502014-06-12 05:32:27 +0000272 for (unsigned i = 0, e = PrevArgs.size(); i != e; ++i) {
Chandler Carruthd102f2d2010-05-13 11:37:24 +0000273 // TODO: Handle ak_declcontext case.
David Blaikie9c902b52011-09-25 23:23:43 +0000274 if (PrevArgs[i].first == DiagnosticsEngine::ak_qualtype) {
Chandler Carruthd102f2d2010-05-13 11:37:24 +0000275 void *Ptr = (void*)PrevArgs[i].second;
276 QualType PrevTy(QualType::getFromOpaquePtr(Ptr));
277 if (PrevTy == Ty) {
278 Repeated = true;
279 break;
280 }
281 }
282 }
283
Douglas Gregor639cccc2010-02-09 22:26:47 +0000284 // Consider producing an a.k.a. clause if removing all the direct
285 // sugar gives us something "significantly different".
Chandler Carruthd102f2d2010-05-13 11:37:24 +0000286 if (!Repeated) {
287 bool ShouldAKA = false;
288 QualType DesugaredTy = Desugar(Context, Ty, ShouldAKA);
Chandler Carruthd5173952011-07-11 17:49:21 +0000289 if (ShouldAKA || ForceAKA) {
290 if (DesugaredTy == Ty) {
291 DesugaredTy = Ty.getCanonicalType();
292 }
Douglas Gregorc0b07282011-09-27 22:38:19 +0000293 std::string akaStr = DesugaredTy.getAsString(Context.getPrintingPolicy());
Chandler Carruthd5173952011-07-11 17:49:21 +0000294 if (akaStr != S) {
295 S = "'" + S + "' (aka '" + akaStr + "')";
296 return S;
297 }
Chandler Carruthd102f2d2010-05-13 11:37:24 +0000298 }
Benjamin Kramer1adc8c32014-04-25 20:41:38 +0000299
300 // Give some additional info on vector types. These are either not desugared
301 // or displaying complex __attribute__ expressions so add details of the
302 // type and element count.
303 if (Ty->isVectorType()) {
304 const VectorType *VTy = Ty->getAs<VectorType>();
305 std::string DecoratedString;
306 llvm::raw_string_ostream OS(DecoratedString);
307 const char *Values = VTy->getNumElements() > 1 ? "values" : "value";
308 OS << "'" << S << "' (vector of " << VTy->getNumElements() << " '"
309 << VTy->getElementType().getAsString(Context.getPrintingPolicy())
310 << "' " << Values << ")";
311 return OS.str();
312 }
Douglas Gregor639cccc2010-02-09 22:26:47 +0000313 }
Chandler Carruthd102f2d2010-05-13 11:37:24 +0000314
Douglas Gregor639cccc2010-02-09 22:26:47 +0000315 S = "'" + S + "'";
316 return S;
317}
318
Richard Trieu91844232012-06-26 18:18:47 +0000319static bool FormatTemplateTypeDiff(ASTContext &Context, QualType FromType,
320 QualType ToType, bool PrintTree,
321 bool PrintFromType, bool ElideType,
Benjamin Kramer8de90462013-02-22 16:08:12 +0000322 bool ShowColors, raw_ostream &OS);
Richard Trieu91844232012-06-26 18:18:47 +0000323
Chandler Carruthd5173952011-07-11 17:49:21 +0000324void clang::FormatASTNodeDiagnosticArgument(
David Blaikie9c902b52011-09-25 23:23:43 +0000325 DiagnosticsEngine::ArgumentKind Kind,
Chandler Carruthd5173952011-07-11 17:49:21 +0000326 intptr_t Val,
Craig Topper3aa4fb32014-06-12 05:32:35 +0000327 StringRef Modifier,
328 StringRef Argument,
Craig Toppere4753502014-06-12 05:32:27 +0000329 ArrayRef<DiagnosticsEngine::ArgumentValue> PrevArgs,
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000330 SmallVectorImpl<char> &Output,
Chandler Carruthd5173952011-07-11 17:49:21 +0000331 void *Cookie,
Bill Wendling8eb771d2012-02-22 09:51:33 +0000332 ArrayRef<intptr_t> QualTypeVals) {
Douglas Gregor639cccc2010-02-09 22:26:47 +0000333 ASTContext &Context = *static_cast<ASTContext*>(Cookie);
Fangrui Song6907ce22018-07-30 19:24:48 +0000334
Benjamin Kramer7192c9e2013-02-22 15:46:08 +0000335 size_t OldEnd = Output.size();
336 llvm::raw_svector_ostream OS(Output);
Douglas Gregor639cccc2010-02-09 22:26:47 +0000337 bool NeedQuotes = true;
Fangrui Song6907ce22018-07-30 19:24:48 +0000338
Douglas Gregor639cccc2010-02-09 22:26:47 +0000339 switch (Kind) {
David Blaikie83d382b2011-09-23 05:06:16 +0000340 default: llvm_unreachable("unknown ArgumentKind");
Anastasia Stulovaed8dadb2019-12-13 12:30:59 +0000341 case DiagnosticsEngine::ak_addrspace: {
342 assert(Modifier.empty() && Argument.empty() &&
343 "Invalid modifier for Qualfiers argument");
344
345 auto S = Qualifiers::getAddrSpaceAsString(static_cast<LangAS>(Val));
346 if (S.empty()) {
347 OS << (Context.getLangOpts().OpenCL ? "default" : "generic");
348 OS << " address space";
349 } else {
350 OS << "address space";
351 OS << " '" << S << "'";
352 }
353 NeedQuotes = false;
354 break;
355 }
Anastasia Stulova4cebc9d2019-01-04 11:50:36 +0000356 case DiagnosticsEngine::ak_qual: {
357 assert(Modifier.empty() && Argument.empty() &&
358 "Invalid modifier for Qualfiers argument");
359
360 Qualifiers Q(Qualifiers::fromOpaqueValue(Val));
361 auto S = Q.getAsString();
362 if (S.empty()) {
363 OS << "unqualified";
364 NeedQuotes = false;
365 } else {
Anastasia Stulovaed8dadb2019-12-13 12:30:59 +0000366 OS << S;
Anastasia Stulova4cebc9d2019-01-04 11:50:36 +0000367 }
368 break;
369 }
Richard Trieu91844232012-06-26 18:18:47 +0000370 case DiagnosticsEngine::ak_qualtype_pair: {
Richard Trieu50f5f462012-07-10 01:46:04 +0000371 TemplateDiffTypes &TDT = *reinterpret_cast<TemplateDiffTypes*>(Val);
Richard Trieu91844232012-06-26 18:18:47 +0000372 QualType FromType =
373 QualType::getFromOpaquePtr(reinterpret_cast<void*>(TDT.FromType));
374 QualType ToType =
375 QualType::getFromOpaquePtr(reinterpret_cast<void*>(TDT.ToType));
376
377 if (FormatTemplateTypeDiff(Context, FromType, ToType, TDT.PrintTree,
378 TDT.PrintFromType, TDT.ElideType,
Benjamin Kramer8de90462013-02-22 16:08:12 +0000379 TDT.ShowColors, OS)) {
Richard Trieu91844232012-06-26 18:18:47 +0000380 NeedQuotes = !TDT.PrintTree;
Richard Trieu50f5f462012-07-10 01:46:04 +0000381 TDT.TemplateDiffUsed = true;
Richard Trieu91844232012-06-26 18:18:47 +0000382 break;
383 }
384
385 // Don't fall-back during tree printing. The caller will handle
386 // this case.
387 if (TDT.PrintTree)
388 return;
389
Benjamin Kramer7192c9e2013-02-22 15:46:08 +0000390 // Attempting to do a template diff on non-templates. Set the variables
Richard Trieu91844232012-06-26 18:18:47 +0000391 // and continue with regular type printing of the appropriate type.
392 Val = TDT.PrintFromType ? TDT.FromType : TDT.ToType;
Craig Topper3aa4fb32014-06-12 05:32:35 +0000393 Modifier = StringRef();
394 Argument = StringRef();
Richard Trieu91844232012-06-26 18:18:47 +0000395 // Fall through
Galina Kistanovaf87496d2017-06-03 06:31:42 +0000396 LLVM_FALLTHROUGH;
Richard Trieu91844232012-06-26 18:18:47 +0000397 }
David Blaikie9c902b52011-09-25 23:23:43 +0000398 case DiagnosticsEngine::ak_qualtype: {
Craig Topper3aa4fb32014-06-12 05:32:35 +0000399 assert(Modifier.empty() && Argument.empty() &&
Douglas Gregor639cccc2010-02-09 22:26:47 +0000400 "Invalid modifier for QualType argument");
Fangrui Song6907ce22018-07-30 19:24:48 +0000401
Douglas Gregor639cccc2010-02-09 22:26:47 +0000402 QualType Ty(QualType::getFromOpaquePtr(reinterpret_cast<void*>(Val)));
Craig Toppere4753502014-06-12 05:32:27 +0000403 OS << ConvertTypeToDiagnosticString(Context, Ty, PrevArgs, QualTypeVals);
Douglas Gregor639cccc2010-02-09 22:26:47 +0000404 NeedQuotes = false;
405 break;
406 }
David Blaikie9c902b52011-09-25 23:23:43 +0000407 case DiagnosticsEngine::ak_declarationname: {
Craig Topper3aa4fb32014-06-12 05:32:35 +0000408 if (Modifier == "objcclass" && Argument.empty())
Benjamin Kramer7192c9e2013-02-22 15:46:08 +0000409 OS << '+';
Craig Topper3aa4fb32014-06-12 05:32:35 +0000410 else if (Modifier == "objcinstance" && Argument.empty())
Benjamin Kramer7192c9e2013-02-22 15:46:08 +0000411 OS << '-';
Douglas Gregor639cccc2010-02-09 22:26:47 +0000412 else
Craig Topper3aa4fb32014-06-12 05:32:35 +0000413 assert(Modifier.empty() && Argument.empty() &&
Douglas Gregor639cccc2010-02-09 22:26:47 +0000414 "Invalid modifier for DeclarationName argument");
Benjamin Kramer7192c9e2013-02-22 15:46:08 +0000415
David Blaikied4da8722013-05-14 21:04:00 +0000416 OS << DeclarationName::getFromOpaqueInteger(Val);
Douglas Gregor639cccc2010-02-09 22:26:47 +0000417 break;
418 }
David Blaikie9c902b52011-09-25 23:23:43 +0000419 case DiagnosticsEngine::ak_nameddecl: {
Douglas Gregor639cccc2010-02-09 22:26:47 +0000420 bool Qualified;
Craig Topper3aa4fb32014-06-12 05:32:35 +0000421 if (Modifier == "q" && Argument.empty())
Douglas Gregor639cccc2010-02-09 22:26:47 +0000422 Qualified = true;
423 else {
Craig Topper3aa4fb32014-06-12 05:32:35 +0000424 assert(Modifier.empty() && Argument.empty() &&
Douglas Gregor639cccc2010-02-09 22:26:47 +0000425 "Invalid modifier for NamedDecl* argument");
426 Qualified = false;
427 }
Chandler Carruthc841b6e2011-08-31 09:01:53 +0000428 const NamedDecl *ND = reinterpret_cast<const NamedDecl*>(Val);
Benjamin Kramer9170e912013-02-22 15:46:01 +0000429 ND->getNameForDiagnostic(OS, Context.getPrintingPolicy(), Qualified);
Douglas Gregor639cccc2010-02-09 22:26:47 +0000430 break;
431 }
David Blaikie9c902b52011-09-25 23:23:43 +0000432 case DiagnosticsEngine::ak_nestednamespec: {
Benjamin Kramer7192c9e2013-02-22 15:46:08 +0000433 NestedNameSpecifier *NNS = reinterpret_cast<NestedNameSpecifier*>(Val);
434 NNS->print(OS, Context.getPrintingPolicy());
Douglas Gregor639cccc2010-02-09 22:26:47 +0000435 NeedQuotes = false;
436 break;
437 }
David Blaikie9c902b52011-09-25 23:23:43 +0000438 case DiagnosticsEngine::ak_declcontext: {
Douglas Gregor639cccc2010-02-09 22:26:47 +0000439 DeclContext *DC = reinterpret_cast<DeclContext *> (Val);
440 assert(DC && "Should never have a null declaration context");
Alp Tokerfb8d02b2014-06-05 22:10:59 +0000441 NeedQuotes = false;
442
Richard Trieu3af6c102014-08-27 03:05:19 +0000443 // FIXME: Get the strings for DeclContext from some localized place
Douglas Gregor639cccc2010-02-09 22:26:47 +0000444 if (DC->isTranslationUnit()) {
David Blaikiebbafb8a2012-03-11 07:00:24 +0000445 if (Context.getLangOpts().CPlusPlus)
Benjamin Kramer7192c9e2013-02-22 15:46:08 +0000446 OS << "the global namespace";
Douglas Gregor639cccc2010-02-09 22:26:47 +0000447 else
Benjamin Kramer7192c9e2013-02-22 15:46:08 +0000448 OS << "the global scope";
Richard Trieu3af6c102014-08-27 03:05:19 +0000449 } else if (DC->isClosure()) {
450 OS << "block literal";
451 } else if (isLambdaCallOperator(DC)) {
452 OS << "lambda expression";
Douglas Gregor639cccc2010-02-09 22:26:47 +0000453 } else if (TypeDecl *Type = dyn_cast<TypeDecl>(DC)) {
Benjamin Kramer7192c9e2013-02-22 15:46:08 +0000454 OS << ConvertTypeToDiagnosticString(Context,
455 Context.getTypeDeclType(Type),
Craig Toppere4753502014-06-12 05:32:27 +0000456 PrevArgs, QualTypeVals);
Douglas Gregor639cccc2010-02-09 22:26:47 +0000457 } else {
Richard Trieu3af6c102014-08-27 03:05:19 +0000458 assert(isa<NamedDecl>(DC) && "Expected a NamedDecl");
Douglas Gregor639cccc2010-02-09 22:26:47 +0000459 NamedDecl *ND = cast<NamedDecl>(DC);
460 if (isa<NamespaceDecl>(ND))
Benjamin Kramer7192c9e2013-02-22 15:46:08 +0000461 OS << "namespace ";
Douglas Gregor639cccc2010-02-09 22:26:47 +0000462 else if (isa<ObjCMethodDecl>(ND))
Benjamin Kramer7192c9e2013-02-22 15:46:08 +0000463 OS << "method ";
Douglas Gregor639cccc2010-02-09 22:26:47 +0000464 else if (isa<FunctionDecl>(ND))
Benjamin Kramer7192c9e2013-02-22 15:46:08 +0000465 OS << "function ";
466
467 OS << '\'';
468 ND->getNameForDiagnostic(OS, Context.getPrintingPolicy(), true);
469 OS << '\'';
Douglas Gregor639cccc2010-02-09 22:26:47 +0000470 }
Douglas Gregor639cccc2010-02-09 22:26:47 +0000471 break;
472 }
David Majnemerb1004102014-03-02 18:46:05 +0000473 case DiagnosticsEngine::ak_attr: {
474 const Attr *At = reinterpret_cast<Attr *>(Val);
475 assert(At && "Received null Attr object!");
476 OS << '\'' << At->getSpelling() << '\'';
477 NeedQuotes = false;
478 break;
479 }
Douglas Gregor639cccc2010-02-09 22:26:47 +0000480 }
Benjamin Kramer7192c9e2013-02-22 15:46:08 +0000481
Benjamin Kramer7192c9e2013-02-22 15:46:08 +0000482 if (NeedQuotes) {
483 Output.insert(Output.begin()+OldEnd, '\'');
Douglas Gregor639cccc2010-02-09 22:26:47 +0000484 Output.push_back('\'');
Benjamin Kramer7192c9e2013-02-22 15:46:08 +0000485 }
Douglas Gregor639cccc2010-02-09 22:26:47 +0000486}
Richard Trieu91844232012-06-26 18:18:47 +0000487
488/// TemplateDiff - A class that constructs a pretty string for a pair of
489/// QualTypes. For the pair of types, a diff tree will be created containing
490/// all the information about the templates and template arguments. Afterwards,
491/// the tree is transformed to a string according to the options passed in.
492namespace {
493class TemplateDiff {
494 /// Context - The ASTContext which is used for comparing template arguments.
495 ASTContext &Context;
496
497 /// Policy - Used during expression printing.
498 PrintingPolicy Policy;
499
500 /// ElideType - Option to elide identical types.
501 bool ElideType;
502
503 /// PrintTree - Format output string as a tree.
504 bool PrintTree;
505
506 /// ShowColor - Diagnostics support color, so bolding will be used.
507 bool ShowColor;
508
Richard Trieu2331c8b2016-01-15 05:48:38 +0000509 /// FromTemplateType - When single type printing is selected, this is the
510 /// type to be be printed. When tree printing is selected, this type will
511 /// show up first in the tree.
512 QualType FromTemplateType;
Richard Trieu91844232012-06-26 18:18:47 +0000513
Richard Trieu2331c8b2016-01-15 05:48:38 +0000514 /// ToTemplateType - The type that FromType is compared to. Only in tree
515 /// printing will this type be outputed.
516 QualType ToTemplateType;
Richard Trieu91844232012-06-26 18:18:47 +0000517
Richard Trieu91844232012-06-26 18:18:47 +0000518 /// OS - The stream used to construct the output strings.
Benjamin Kramer8de90462013-02-22 16:08:12 +0000519 raw_ostream &OS;
Richard Trieu91844232012-06-26 18:18:47 +0000520
521 /// IsBold - Keeps track of the bold formatting for the output string.
522 bool IsBold;
523
524 /// DiffTree - A tree representation the differences between two types.
525 class DiffTree {
Richard Trieub4cff112013-03-15 20:35:18 +0000526 public:
Richard Trieu14714c42016-01-14 22:56:39 +0000527 /// DiffKind - The difference in a DiffNode. Fields of
528 /// TemplateArgumentInfo needed by each difference can be found in the
529 /// Set* and Get* functions.
Richard Trieub4cff112013-03-15 20:35:18 +0000530 enum DiffKind {
531 /// Incomplete or invalid node.
532 Invalid,
Richard Trieu12074502016-02-02 00:36:59 +0000533 /// Another level of templates
Richard Trieub4cff112013-03-15 20:35:18 +0000534 Template,
Richard Trieu14714c42016-01-14 22:56:39 +0000535 /// Type difference, all type differences except those falling under
536 /// the Template difference.
Richard Trieub4cff112013-03-15 20:35:18 +0000537 Type,
Richard Trieu14714c42016-01-14 22:56:39 +0000538 /// Expression difference, this is only when both arguments are
539 /// expressions. If one argument is an expression and the other is
540 /// Integer or Declaration, then use that diff type instead.
Richard Trieub4cff112013-03-15 20:35:18 +0000541 Expression,
Richard Trieu14714c42016-01-14 22:56:39 +0000542 /// Template argument difference
Richard Trieub4cff112013-03-15 20:35:18 +0000543 TemplateTemplate,
Richard Trieu14714c42016-01-14 22:56:39 +0000544 /// Integer difference
Richard Trieub4cff112013-03-15 20:35:18 +0000545 Integer,
Richard Trieu14714c42016-01-14 22:56:39 +0000546 /// Declaration difference, nullptr arguments are included here
Richard Trieu9213ce52016-01-15 05:01:53 +0000547 Declaration,
548 /// One argument being integer and the other being declaration
549 FromIntegerAndToDeclaration,
550 FromDeclarationAndToInteger
Richard Trieub4cff112013-03-15 20:35:18 +0000551 };
Richard Trieu14714c42016-01-14 22:56:39 +0000552
Richard Trieub4cff112013-03-15 20:35:18 +0000553 private:
Richard Trieu14714c42016-01-14 22:56:39 +0000554 /// TemplateArgumentInfo - All the information needed to pretty print
555 /// a template argument. See the Set* and Get* functions to see which
556 /// fields are used for each DiffKind.
557 struct TemplateArgumentInfo {
558 QualType ArgType;
559 Qualifiers Qual;
560 llvm::APSInt Val;
561 bool IsValidInt = false;
562 Expr *ArgExpr = nullptr;
563 TemplateDecl *TD = nullptr;
564 ValueDecl *VD = nullptr;
565 bool NeedAddressOf = false;
566 bool IsNullPtr = false;
567 bool IsDefault = false;
568 };
569
Richard Trieu91844232012-06-26 18:18:47 +0000570 /// DiffNode - The root node stores the original type. Each child node
571 /// stores template arguments of their parents. For templated types, the
572 /// template decl is also stored.
573 struct DiffNode {
Richard Trieu14714c42016-01-14 22:56:39 +0000574 DiffKind Kind = Invalid;
Richard Trieub4cff112013-03-15 20:35:18 +0000575
Richard Trieu91844232012-06-26 18:18:47 +0000576 /// NextNode - The index of the next sibling node or 0.
Richard Trieu14714c42016-01-14 22:56:39 +0000577 unsigned NextNode = 0;
Richard Trieu91844232012-06-26 18:18:47 +0000578
579 /// ChildNode - The index of the first child node or 0.
Richard Trieu14714c42016-01-14 22:56:39 +0000580 unsigned ChildNode = 0;
Richard Trieu91844232012-06-26 18:18:47 +0000581
582 /// ParentNode - The index of the parent node.
Richard Trieu14714c42016-01-14 22:56:39 +0000583 unsigned ParentNode = 0;
Richard Trieu91844232012-06-26 18:18:47 +0000584
Richard Trieu14714c42016-01-14 22:56:39 +0000585 TemplateArgumentInfo FromArgInfo, ToArgInfo;
Richard Trieu91844232012-06-26 18:18:47 +0000586
587 /// Same - Whether the two arguments evaluate to the same value.
Richard Trieu14714c42016-01-14 22:56:39 +0000588 bool Same = false;
Richard Trieu91844232012-06-26 18:18:47 +0000589
Richard Trieu14714c42016-01-14 22:56:39 +0000590 DiffNode(unsigned ParentNode = 0) : ParentNode(ParentNode) {}
Richard Trieu91844232012-06-26 18:18:47 +0000591 };
592
593 /// FlatTree - A flattened tree used to store the DiffNodes.
Dmitri Gribenkof8579502013-01-12 19:30:44 +0000594 SmallVector<DiffNode, 16> FlatTree;
Richard Trieu91844232012-06-26 18:18:47 +0000595
596 /// CurrentNode - The index of the current node being used.
597 unsigned CurrentNode;
598
599 /// NextFreeNode - The index of the next unused node. Used when creating
600 /// child nodes.
601 unsigned NextFreeNode;
602
603 /// ReadNode - The index of the current node being read.
604 unsigned ReadNode;
Richard Trieu14714c42016-01-14 22:56:39 +0000605
Richard Trieu91844232012-06-26 18:18:47 +0000606 public:
607 DiffTree() :
608 CurrentNode(0), NextFreeNode(1) {
609 FlatTree.push_back(DiffNode());
610 }
611
Richard Trieu14714c42016-01-14 22:56:39 +0000612 // Node writing functions, one for each valid DiffKind element.
613 void SetTemplateDiff(TemplateDecl *FromTD, TemplateDecl *ToTD,
614 Qualifiers FromQual, Qualifiers ToQual,
615 bool FromDefault, bool ToDefault) {
616 assert(FlatTree[CurrentNode].Kind == Invalid && "Node is not empty.");
617 FlatTree[CurrentNode].Kind = Template;
618 FlatTree[CurrentNode].FromArgInfo.TD = FromTD;
619 FlatTree[CurrentNode].ToArgInfo.TD = ToTD;
620 FlatTree[CurrentNode].FromArgInfo.Qual = FromQual;
621 FlatTree[CurrentNode].ToArgInfo.Qual = ToQual;
622 SetDefault(FromDefault, ToDefault);
Richard Trieu91844232012-06-26 18:18:47 +0000623 }
624
Richard Trieu14714c42016-01-14 22:56:39 +0000625 void SetTypeDiff(QualType FromType, QualType ToType, bool FromDefault,
626 bool ToDefault) {
627 assert(FlatTree[CurrentNode].Kind == Invalid && "Node is not empty.");
628 FlatTree[CurrentNode].Kind = Type;
629 FlatTree[CurrentNode].FromArgInfo.ArgType = FromType;
630 FlatTree[CurrentNode].ToArgInfo.ArgType = ToType;
631 SetDefault(FromDefault, ToDefault);
Richard Trieu91844232012-06-26 18:18:47 +0000632 }
633
Richard Trieu2c22a862016-01-15 01:08:56 +0000634 void SetExpressionDiff(Expr *FromExpr, Expr *ToExpr, bool FromDefault,
635 bool ToDefault) {
Richard Trieu14714c42016-01-14 22:56:39 +0000636 assert(FlatTree[CurrentNode].Kind == Invalid && "Node is not empty.");
637 FlatTree[CurrentNode].Kind = Expression;
638 FlatTree[CurrentNode].FromArgInfo.ArgExpr = FromExpr;
639 FlatTree[CurrentNode].ToArgInfo.ArgExpr = ToExpr;
Richard Trieu14714c42016-01-14 22:56:39 +0000640 SetDefault(FromDefault, ToDefault);
Richard Trieu91844232012-06-26 18:18:47 +0000641 }
642
Richard Trieu14714c42016-01-14 22:56:39 +0000643 void SetTemplateTemplateDiff(TemplateDecl *FromTD, TemplateDecl *ToTD,
644 bool FromDefault, bool ToDefault) {
645 assert(FlatTree[CurrentNode].Kind == Invalid && "Node is not empty.");
646 FlatTree[CurrentNode].Kind = TemplateTemplate;
647 FlatTree[CurrentNode].FromArgInfo.TD = FromTD;
648 FlatTree[CurrentNode].ToArgInfo.TD = ToTD;
649 SetDefault(FromDefault, ToDefault);
Richard Trieu6df89452012-11-01 21:29:28 +0000650 }
651
Benjamin Kramer7320b992016-06-15 14:20:56 +0000652 void SetIntegerDiff(const llvm::APSInt &FromInt, const llvm::APSInt &ToInt,
Richard Trieu14714c42016-01-14 22:56:39 +0000653 bool IsValidFromInt, bool IsValidToInt,
Richard Trieud5c73782016-01-15 02:55:17 +0000654 QualType FromIntType, QualType ToIntType,
Richard Trieu14714c42016-01-14 22:56:39 +0000655 Expr *FromExpr, Expr *ToExpr, bool FromDefault,
656 bool ToDefault) {
657 assert(FlatTree[CurrentNode].Kind == Invalid && "Node is not empty.");
658 FlatTree[CurrentNode].Kind = Integer;
659 FlatTree[CurrentNode].FromArgInfo.Val = FromInt;
660 FlatTree[CurrentNode].ToArgInfo.Val = ToInt;
661 FlatTree[CurrentNode].FromArgInfo.IsValidInt = IsValidFromInt;
662 FlatTree[CurrentNode].ToArgInfo.IsValidInt = IsValidToInt;
Richard Trieud5c73782016-01-15 02:55:17 +0000663 FlatTree[CurrentNode].FromArgInfo.ArgType = FromIntType;
664 FlatTree[CurrentNode].ToArgInfo.ArgType = ToIntType;
Richard Trieu14714c42016-01-14 22:56:39 +0000665 FlatTree[CurrentNode].FromArgInfo.ArgExpr = FromExpr;
666 FlatTree[CurrentNode].ToArgInfo.ArgExpr = ToExpr;
667 SetDefault(FromDefault, ToDefault);
Richard Trieub7243852012-09-28 20:32:51 +0000668 }
669
Richard Trieu14714c42016-01-14 22:56:39 +0000670 void SetDeclarationDiff(ValueDecl *FromValueDecl, ValueDecl *ToValueDecl,
671 bool FromAddressOf, bool ToAddressOf,
Richard Trieu2c22a862016-01-15 01:08:56 +0000672 bool FromNullPtr, bool ToNullPtr, Expr *FromExpr,
673 Expr *ToExpr, bool FromDefault, bool ToDefault) {
Richard Trieu14714c42016-01-14 22:56:39 +0000674 assert(FlatTree[CurrentNode].Kind == Invalid && "Node is not empty.");
675 FlatTree[CurrentNode].Kind = Declaration;
676 FlatTree[CurrentNode].FromArgInfo.VD = FromValueDecl;
677 FlatTree[CurrentNode].ToArgInfo.VD = ToValueDecl;
678 FlatTree[CurrentNode].FromArgInfo.NeedAddressOf = FromAddressOf;
679 FlatTree[CurrentNode].ToArgInfo.NeedAddressOf = ToAddressOf;
680 FlatTree[CurrentNode].FromArgInfo.IsNullPtr = FromNullPtr;
681 FlatTree[CurrentNode].ToArgInfo.IsNullPtr = ToNullPtr;
Richard Trieu2c22a862016-01-15 01:08:56 +0000682 FlatTree[CurrentNode].FromArgInfo.ArgExpr = FromExpr;
683 FlatTree[CurrentNode].ToArgInfo.ArgExpr = ToExpr;
Richard Trieu14714c42016-01-14 22:56:39 +0000684 SetDefault(FromDefault, ToDefault);
685 }
686
Richard Trieu9213ce52016-01-15 05:01:53 +0000687 void SetFromDeclarationAndToIntegerDiff(
688 ValueDecl *FromValueDecl, bool FromAddressOf, bool FromNullPtr,
Benjamin Kramer7320b992016-06-15 14:20:56 +0000689 Expr *FromExpr, const llvm::APSInt &ToInt, bool IsValidToInt,
Richard Trieu9213ce52016-01-15 05:01:53 +0000690 QualType ToIntType, Expr *ToExpr, bool FromDefault, bool ToDefault) {
691 assert(FlatTree[CurrentNode].Kind == Invalid && "Node is not empty.");
692 FlatTree[CurrentNode].Kind = FromDeclarationAndToInteger;
693 FlatTree[CurrentNode].FromArgInfo.VD = FromValueDecl;
694 FlatTree[CurrentNode].FromArgInfo.NeedAddressOf = FromAddressOf;
695 FlatTree[CurrentNode].FromArgInfo.IsNullPtr = FromNullPtr;
696 FlatTree[CurrentNode].FromArgInfo.ArgExpr = FromExpr;
697 FlatTree[CurrentNode].ToArgInfo.Val = ToInt;
698 FlatTree[CurrentNode].ToArgInfo.IsValidInt = IsValidToInt;
699 FlatTree[CurrentNode].ToArgInfo.ArgType = ToIntType;
700 FlatTree[CurrentNode].ToArgInfo.ArgExpr = ToExpr;
701 SetDefault(FromDefault, ToDefault);
702 }
703
704 void SetFromIntegerAndToDeclarationDiff(
Benjamin Kramer7320b992016-06-15 14:20:56 +0000705 const llvm::APSInt &FromInt, bool IsValidFromInt, QualType FromIntType,
Richard Trieu9213ce52016-01-15 05:01:53 +0000706 Expr *FromExpr, ValueDecl *ToValueDecl, bool ToAddressOf,
707 bool ToNullPtr, Expr *ToExpr, bool FromDefault, bool ToDefault) {
708 assert(FlatTree[CurrentNode].Kind == Invalid && "Node is not empty.");
709 FlatTree[CurrentNode].Kind = FromIntegerAndToDeclaration;
710 FlatTree[CurrentNode].FromArgInfo.Val = FromInt;
711 FlatTree[CurrentNode].FromArgInfo.IsValidInt = IsValidFromInt;
712 FlatTree[CurrentNode].FromArgInfo.ArgType = FromIntType;
713 FlatTree[CurrentNode].FromArgInfo.ArgExpr = FromExpr;
714 FlatTree[CurrentNode].ToArgInfo.VD = ToValueDecl;
715 FlatTree[CurrentNode].ToArgInfo.NeedAddressOf = ToAddressOf;
716 FlatTree[CurrentNode].ToArgInfo.IsNullPtr = ToNullPtr;
717 FlatTree[CurrentNode].ToArgInfo.ArgExpr = ToExpr;
718 SetDefault(FromDefault, ToDefault);
719 }
720
Richard Trieu14714c42016-01-14 22:56:39 +0000721 /// SetDefault - Sets FromDefault and ToDefault flags of the current node.
722 void SetDefault(bool FromDefault, bool ToDefault) {
James Y Knight925d60e2016-01-15 05:57:41 +0000723 assert((!FromDefault || !ToDefault) && "Both arguments cannot be default.");
Richard Trieu14714c42016-01-14 22:56:39 +0000724 FlatTree[CurrentNode].FromArgInfo.IsDefault = FromDefault;
725 FlatTree[CurrentNode].ToArgInfo.IsDefault = ToDefault;
Richard Trieu954aaaf2013-02-27 01:41:53 +0000726 }
727
Richard Trieu91844232012-06-26 18:18:47 +0000728 /// SetSame - Sets the same flag of the current node.
729 void SetSame(bool Same) {
730 FlatTree[CurrentNode].Same = Same;
731 }
732
Richard Trieub4cff112013-03-15 20:35:18 +0000733 /// SetKind - Sets the current node's type.
734 void SetKind(DiffKind Kind) {
735 FlatTree[CurrentNode].Kind = Kind;
736 }
737
Richard Trieu91844232012-06-26 18:18:47 +0000738 /// Up - Changes the node to the parent of the current node.
739 void Up() {
Richard Trieu2331c8b2016-01-15 05:48:38 +0000740 assert(FlatTree[CurrentNode].Kind != Invalid &&
741 "Cannot exit node before setting node information.");
Richard Trieu91844232012-06-26 18:18:47 +0000742 CurrentNode = FlatTree[CurrentNode].ParentNode;
743 }
744
745 /// AddNode - Adds a child node to the current node, then sets that node
746 /// node as the current node.
747 void AddNode() {
Richard Trieu2331c8b2016-01-15 05:48:38 +0000748 assert(FlatTree[CurrentNode].Kind == Template &&
749 "Only Template nodes can have children nodes.");
Richard Trieu91844232012-06-26 18:18:47 +0000750 FlatTree.push_back(DiffNode(CurrentNode));
751 DiffNode &Node = FlatTree[CurrentNode];
752 if (Node.ChildNode == 0) {
753 // If a child node doesn't exist, add one.
754 Node.ChildNode = NextFreeNode;
755 } else {
756 // If a child node exists, find the last child node and add a
757 // next node to it.
758 unsigned i;
759 for (i = Node.ChildNode; FlatTree[i].NextNode != 0;
760 i = FlatTree[i].NextNode) {
761 }
762 FlatTree[i].NextNode = NextFreeNode;
763 }
764 CurrentNode = NextFreeNode;
765 ++NextFreeNode;
766 }
767
768 // Node reading functions.
769 /// StartTraverse - Prepares the tree for recursive traversal.
770 void StartTraverse() {
771 ReadNode = 0;
772 CurrentNode = NextFreeNode;
773 NextFreeNode = 0;
774 }
775
776 /// Parent - Move the current read node to its parent.
777 void Parent() {
778 ReadNode = FlatTree[ReadNode].ParentNode;
779 }
780
Richard Trieu14714c42016-01-14 22:56:39 +0000781 void GetTemplateDiff(TemplateDecl *&FromTD, TemplateDecl *&ToTD,
782 Qualifiers &FromQual, Qualifiers &ToQual) {
783 assert(FlatTree[ReadNode].Kind == Template && "Unexpected kind.");
784 FromTD = FlatTree[ReadNode].FromArgInfo.TD;
785 ToTD = FlatTree[ReadNode].ToArgInfo.TD;
786 FromQual = FlatTree[ReadNode].FromArgInfo.Qual;
787 ToQual = FlatTree[ReadNode].ToArgInfo.Qual;
Richard Trieu91844232012-06-26 18:18:47 +0000788 }
789
Richard Trieu14714c42016-01-14 22:56:39 +0000790 void GetTypeDiff(QualType &FromType, QualType &ToType) {
791 assert(FlatTree[ReadNode].Kind == Type && "Unexpected kind");
792 FromType = FlatTree[ReadNode].FromArgInfo.ArgType;
793 ToType = FlatTree[ReadNode].ToArgInfo.ArgType;
Richard Trieu91844232012-06-26 18:18:47 +0000794 }
795
Richard Trieu2c22a862016-01-15 01:08:56 +0000796 void GetExpressionDiff(Expr *&FromExpr, Expr *&ToExpr) {
Richard Trieu14714c42016-01-14 22:56:39 +0000797 assert(FlatTree[ReadNode].Kind == Expression && "Unexpected kind");
798 FromExpr = FlatTree[ReadNode].FromArgInfo.ArgExpr;
799 ToExpr = FlatTree[ReadNode].ToArgInfo.ArgExpr;
Richard Trieu91844232012-06-26 18:18:47 +0000800 }
801
Richard Trieu14714c42016-01-14 22:56:39 +0000802 void GetTemplateTemplateDiff(TemplateDecl *&FromTD, TemplateDecl *&ToTD) {
803 assert(FlatTree[ReadNode].Kind == TemplateTemplate && "Unexpected kind.");
804 FromTD = FlatTree[ReadNode].FromArgInfo.TD;
805 ToTD = FlatTree[ReadNode].ToArgInfo.TD;
Richard Trieu6df89452012-11-01 21:29:28 +0000806 }
807
Richard Trieu14714c42016-01-14 22:56:39 +0000808 void GetIntegerDiff(llvm::APSInt &FromInt, llvm::APSInt &ToInt,
809 bool &IsValidFromInt, bool &IsValidToInt,
Richard Trieud5c73782016-01-15 02:55:17 +0000810 QualType &FromIntType, QualType &ToIntType,
Richard Trieu14714c42016-01-14 22:56:39 +0000811 Expr *&FromExpr, Expr *&ToExpr) {
812 assert(FlatTree[ReadNode].Kind == Integer && "Unexpected kind.");
813 FromInt = FlatTree[ReadNode].FromArgInfo.Val;
814 ToInt = FlatTree[ReadNode].ToArgInfo.Val;
815 IsValidFromInt = FlatTree[ReadNode].FromArgInfo.IsValidInt;
816 IsValidToInt = FlatTree[ReadNode].ToArgInfo.IsValidInt;
Richard Trieud5c73782016-01-15 02:55:17 +0000817 FromIntType = FlatTree[ReadNode].FromArgInfo.ArgType;
818 ToIntType = FlatTree[ReadNode].ToArgInfo.ArgType;
Richard Trieu14714c42016-01-14 22:56:39 +0000819 FromExpr = FlatTree[ReadNode].FromArgInfo.ArgExpr;
820 ToExpr = FlatTree[ReadNode].ToArgInfo.ArgExpr;
Richard Trieub7243852012-09-28 20:32:51 +0000821 }
822
Richard Trieu14714c42016-01-14 22:56:39 +0000823 void GetDeclarationDiff(ValueDecl *&FromValueDecl, ValueDecl *&ToValueDecl,
824 bool &FromAddressOf, bool &ToAddressOf,
Richard Trieu2c22a862016-01-15 01:08:56 +0000825 bool &FromNullPtr, bool &ToNullPtr, Expr *&FromExpr,
826 Expr *&ToExpr) {
Richard Trieu14714c42016-01-14 22:56:39 +0000827 assert(FlatTree[ReadNode].Kind == Declaration && "Unexpected kind.");
828 FromValueDecl = FlatTree[ReadNode].FromArgInfo.VD;
829 ToValueDecl = FlatTree[ReadNode].ToArgInfo.VD;
830 FromAddressOf = FlatTree[ReadNode].FromArgInfo.NeedAddressOf;
831 ToAddressOf = FlatTree[ReadNode].ToArgInfo.NeedAddressOf;
832 FromNullPtr = FlatTree[ReadNode].FromArgInfo.IsNullPtr;
833 ToNullPtr = FlatTree[ReadNode].ToArgInfo.IsNullPtr;
Richard Trieu2c22a862016-01-15 01:08:56 +0000834 FromExpr = FlatTree[ReadNode].FromArgInfo.ArgExpr;
835 ToExpr = FlatTree[ReadNode].ToArgInfo.ArgExpr;
Richard Trieu14714c42016-01-14 22:56:39 +0000836 }
837
Richard Trieu9213ce52016-01-15 05:01:53 +0000838 void GetFromDeclarationAndToIntegerDiff(
839 ValueDecl *&FromValueDecl, bool &FromAddressOf, bool &FromNullPtr,
840 Expr *&FromExpr, llvm::APSInt &ToInt, bool &IsValidToInt,
841 QualType &ToIntType, Expr *&ToExpr) {
842 assert(FlatTree[ReadNode].Kind == FromDeclarationAndToInteger &&
843 "Unexpected kind.");
844 FromValueDecl = FlatTree[ReadNode].FromArgInfo.VD;
845 FromAddressOf = FlatTree[ReadNode].FromArgInfo.NeedAddressOf;
846 FromNullPtr = FlatTree[ReadNode].FromArgInfo.IsNullPtr;
847 FromExpr = FlatTree[ReadNode].FromArgInfo.ArgExpr;
848 ToInt = FlatTree[ReadNode].ToArgInfo.Val;
849 IsValidToInt = FlatTree[ReadNode].ToArgInfo.IsValidInt;
850 ToIntType = FlatTree[ReadNode].ToArgInfo.ArgType;
851 ToExpr = FlatTree[ReadNode].ToArgInfo.ArgExpr;
852 }
853
854 void GetFromIntegerAndToDeclarationDiff(
855 llvm::APSInt &FromInt, bool &IsValidFromInt, QualType &FromIntType,
856 Expr *&FromExpr, ValueDecl *&ToValueDecl, bool &ToAddressOf,
857 bool &ToNullPtr, Expr *&ToExpr) {
858 assert(FlatTree[ReadNode].Kind == FromIntegerAndToDeclaration &&
859 "Unexpected kind.");
860 FromInt = FlatTree[ReadNode].FromArgInfo.Val;
861 IsValidFromInt = FlatTree[ReadNode].FromArgInfo.IsValidInt;
862 FromIntType = FlatTree[ReadNode].FromArgInfo.ArgType;
863 FromExpr = FlatTree[ReadNode].FromArgInfo.ArgExpr;
864 ToValueDecl = FlatTree[ReadNode].ToArgInfo.VD;
865 ToAddressOf = FlatTree[ReadNode].ToArgInfo.NeedAddressOf;
866 ToNullPtr = FlatTree[ReadNode].ToArgInfo.IsNullPtr;
867 ToExpr = FlatTree[ReadNode].ToArgInfo.ArgExpr;
868 }
869
Richard Trieu14714c42016-01-14 22:56:39 +0000870 /// FromDefault - Return true if the from argument is the default.
871 bool FromDefault() {
872 return FlatTree[ReadNode].FromArgInfo.IsDefault;
873 }
874
875 /// ToDefault - Return true if the to argument is the default.
876 bool ToDefault() {
877 return FlatTree[ReadNode].ToArgInfo.IsDefault;
Richard Trieu954aaaf2013-02-27 01:41:53 +0000878 }
879
Richard Trieu91844232012-06-26 18:18:47 +0000880 /// NodeIsSame - Returns true the arguments are the same.
881 bool NodeIsSame() {
882 return FlatTree[ReadNode].Same;
883 }
884
885 /// HasChildrend - Returns true if the node has children.
886 bool HasChildren() {
887 return FlatTree[ReadNode].ChildNode != 0;
888 }
889
890 /// MoveToChild - Moves from the current node to its child.
891 void MoveToChild() {
892 ReadNode = FlatTree[ReadNode].ChildNode;
893 }
894
895 /// AdvanceSibling - If there is a next sibling, advance to it and return
896 /// true. Otherwise, return false.
897 bool AdvanceSibling() {
898 if (FlatTree[ReadNode].NextNode == 0)
899 return false;
900
901 ReadNode = FlatTree[ReadNode].NextNode;
902 return true;
903 }
904
905 /// HasNextSibling - Return true if the node has a next sibling.
906 bool HasNextSibling() {
907 return FlatTree[ReadNode].NextNode != 0;
908 }
909
Richard Trieu91844232012-06-26 18:18:47 +0000910 /// Empty - Returns true if the tree has no information.
911 bool Empty() {
Richard Trieub4cff112013-03-15 20:35:18 +0000912 return GetKind() == Invalid;
913 }
914
915 /// GetKind - Returns the current node's type.
916 DiffKind GetKind() {
917 return FlatTree[ReadNode].Kind;
Richard Trieu91844232012-06-26 18:18:47 +0000918 }
919 };
920
921 DiffTree Tree;
922
Richard Trieuac1e2f82016-01-14 23:30:12 +0000923 /// TSTiterator - a pair of iterators that walks the
924 /// TemplateSpecializationType and the desugared TemplateSpecializationType.
925 /// The deseguared TemplateArgument should provide the canonical argument
926 /// for comparisons.
927 class TSTiterator {
Richard Trieu91844232012-06-26 18:18:47 +0000928 typedef const TemplateArgument& reference;
929 typedef const TemplateArgument* pointer;
930
Richard Trieuac1e2f82016-01-14 23:30:12 +0000931 /// InternalIterator - an iterator that is used to enter a
932 /// TemplateSpecializationType and read TemplateArguments inside template
933 /// parameter packs in order with the rest of the TemplateArguments.
934 struct InternalIterator {
935 /// TST - the template specialization whose arguments this iterator
936 /// traverse over.
937 const TemplateSpecializationType *TST;
Richard Trieu91844232012-06-26 18:18:47 +0000938
Richard Trieuac1e2f82016-01-14 23:30:12 +0000939 /// Index - the index of the template argument in TST.
940 unsigned Index;
Richard Trieu64ab30392013-03-15 23:55:09 +0000941
Richard Trieuac1e2f82016-01-14 23:30:12 +0000942 /// CurrentTA - if CurrentTA is not the same as EndTA, then CurrentTA
943 /// points to a TemplateArgument within a parameter pack.
944 TemplateArgument::pack_iterator CurrentTA;
Richard Trieu91844232012-06-26 18:18:47 +0000945
Richard Trieuac1e2f82016-01-14 23:30:12 +0000946 /// EndTA - the end iterator of a parameter pack
947 TemplateArgument::pack_iterator EndTA;
Richard Trieu91844232012-06-26 18:18:47 +0000948
Richard Trieuac1e2f82016-01-14 23:30:12 +0000949 /// InternalIterator - Constructs an iterator and sets it to the first
950 /// template argument.
951 InternalIterator(const TemplateSpecializationType *TST)
952 : TST(TST), Index(0), CurrentTA(nullptr), EndTA(nullptr) {
Richard Trieue1a6a7d2016-08-05 03:16:36 +0000953 if (!TST) return;
954
Richard Trieuac1e2f82016-01-14 23:30:12 +0000955 if (isEnd()) return;
Richard Trieu91844232012-06-26 18:18:47 +0000956
Richard Trieuac1e2f82016-01-14 23:30:12 +0000957 // Set to first template argument. If not a parameter pack, done.
958 TemplateArgument TA = TST->getArg(0);
959 if (TA.getKind() != TemplateArgument::Pack) return;
Richard Trieu91844232012-06-26 18:18:47 +0000960
Richard Trieuac1e2f82016-01-14 23:30:12 +0000961 // Start looking into the parameter pack.
Richard Trieu91844232012-06-26 18:18:47 +0000962 CurrentTA = TA.pack_begin();
963 EndTA = TA.pack_end();
964
Richard Trieuac1e2f82016-01-14 23:30:12 +0000965 // Found a valid template argument.
966 if (CurrentTA != EndTA) return;
967
968 // Parameter pack is empty, use the increment to get to a valid
969 // template argument.
970 ++(*this);
Richard Trieu91844232012-06-26 18:18:47 +0000971 }
Richard Trieuac1e2f82016-01-14 23:30:12 +0000972
Richard Smithe0ab8732016-10-28 19:54:43 +0000973 /// Return true if the iterator is non-singular.
974 bool isValid() const { return TST; }
975
Richard Trieuac1e2f82016-01-14 23:30:12 +0000976 /// isEnd - Returns true if the iterator is one past the end.
977 bool isEnd() const {
Richard Trieue056aee2016-08-06 01:44:06 +0000978 assert(TST && "InternalIterator is invalid with a null TST.");
Richard Trieuac1e2f82016-01-14 23:30:12 +0000979 return Index >= TST->getNumArgs();
980 }
981
982 /// &operator++ - Increment the iterator to the next template argument.
983 InternalIterator &operator++() {
Richard Trieue056aee2016-08-06 01:44:06 +0000984 assert(TST && "InternalIterator is invalid with a null TST.");
Richard Trieuac1e2f82016-01-14 23:30:12 +0000985 if (isEnd()) {
986 return *this;
987 }
988
989 // If in a parameter pack, advance in the parameter pack.
990 if (CurrentTA != EndTA) {
991 ++CurrentTA;
992 if (CurrentTA != EndTA)
993 return *this;
994 }
995
996 // Loop until a template argument is found, or the end is reached.
997 while (true) {
998 // Advance to the next template argument. Break if reached the end.
999 if (++Index == TST->getNumArgs())
1000 break;
1001
1002 // If the TemplateArgument is not a parameter pack, done.
1003 TemplateArgument TA = TST->getArg(Index);
1004 if (TA.getKind() != TemplateArgument::Pack)
1005 break;
1006
1007 // Handle parameter packs.
1008 CurrentTA = TA.pack_begin();
1009 EndTA = TA.pack_end();
1010
1011 // If the parameter pack is empty, try to advance again.
1012 if (CurrentTA != EndTA)
1013 break;
1014 }
1015 return *this;
1016 }
1017
1018 /// operator* - Returns the appropriate TemplateArgument.
1019 reference operator*() const {
Richard Trieue056aee2016-08-06 01:44:06 +00001020 assert(TST && "InternalIterator is invalid with a null TST.");
Richard Trieuac1e2f82016-01-14 23:30:12 +00001021 assert(!isEnd() && "Index exceeds number of arguments.");
1022 if (CurrentTA == EndTA)
1023 return TST->getArg(Index);
1024 else
1025 return *CurrentTA;
1026 }
1027
1028 /// operator-> - Allow access to the underlying TemplateArgument.
1029 pointer operator->() const {
Richard Trieue056aee2016-08-06 01:44:06 +00001030 assert(TST && "InternalIterator is invalid with a null TST.");
Richard Trieuac1e2f82016-01-14 23:30:12 +00001031 return &operator*();
1032 }
1033 };
1034
1035 InternalIterator SugaredIterator;
1036 InternalIterator DesugaredIterator;
1037
1038 public:
1039 TSTiterator(ASTContext &Context, const TemplateSpecializationType *TST)
Richard Smithe0ab8732016-10-28 19:54:43 +00001040 : SugaredIterator(TST),
Richard Trieuac1e2f82016-01-14 23:30:12 +00001041 DesugaredIterator(
Richard Smithe0ab8732016-10-28 19:54:43 +00001042 (TST->isSugared() && !TST->isTypeAlias())
1043 ? GetTemplateSpecializationType(Context, TST->desugar())
1044 : nullptr) {}
Richard Trieuac1e2f82016-01-14 23:30:12 +00001045
1046 /// &operator++ - Increment the iterator to the next template argument.
1047 TSTiterator &operator++() {
1048 ++SugaredIterator;
Richard Smithe0ab8732016-10-28 19:54:43 +00001049 if (DesugaredIterator.isValid())
Richard Trieua7564d72016-03-30 22:23:00 +00001050 ++DesugaredIterator;
Richard Trieu91844232012-06-26 18:18:47 +00001051 return *this;
1052 }
1053
1054 /// operator* - Returns the appropriate TemplateArgument.
1055 reference operator*() const {
Richard Trieuac1e2f82016-01-14 23:30:12 +00001056 return *SugaredIterator;
Richard Trieu91844232012-06-26 18:18:47 +00001057 }
1058
1059 /// operator-> - Allow access to the underlying TemplateArgument.
1060 pointer operator->() const {
1061 return &operator*();
1062 }
Richard Trieu64ab30392013-03-15 23:55:09 +00001063
Richard Trieuac1e2f82016-01-14 23:30:12 +00001064 /// isEnd - Returns true if no more TemplateArguments are available.
1065 bool isEnd() const {
1066 return SugaredIterator.isEnd();
1067 }
1068
1069 /// hasDesugaredTA - Returns true if there is another TemplateArgument
1070 /// available.
1071 bool hasDesugaredTA() const {
Richard Smithe0ab8732016-10-28 19:54:43 +00001072 return DesugaredIterator.isValid() && !DesugaredIterator.isEnd();
Richard Trieuac1e2f82016-01-14 23:30:12 +00001073 }
1074
1075 /// getDesugaredTA - Returns the desugared TemplateArgument.
1076 reference getDesugaredTA() const {
Richard Smithe0ab8732016-10-28 19:54:43 +00001077 assert(DesugaredIterator.isValid() &&
Richard Trieua7564d72016-03-30 22:23:00 +00001078 "Desugared TemplateArgument should not be used.");
Richard Trieuac1e2f82016-01-14 23:30:12 +00001079 return *DesugaredIterator;
Richard Trieu64ab30392013-03-15 23:55:09 +00001080 }
Richard Trieu91844232012-06-26 18:18:47 +00001081 };
1082
1083 // These functions build up the template diff tree, including functions to
Richard Trieuac1e2f82016-01-14 23:30:12 +00001084 // retrieve and compare template arguments.
Richard Trieu91844232012-06-26 18:18:47 +00001085
Richard Trieu2331c8b2016-01-15 05:48:38 +00001086 static const TemplateSpecializationType *GetTemplateSpecializationType(
Richard Trieu91844232012-06-26 18:18:47 +00001087 ASTContext &Context, QualType Ty) {
1088 if (const TemplateSpecializationType *TST =
1089 Ty->getAs<TemplateSpecializationType>())
1090 return TST;
1091
1092 const RecordType *RT = Ty->getAs<RecordType>();
1093
1094 if (!RT)
Craig Topper36250ad2014-05-12 05:36:57 +00001095 return nullptr;
Richard Trieu91844232012-06-26 18:18:47 +00001096
1097 const ClassTemplateSpecializationDecl *CTSD =
1098 dyn_cast<ClassTemplateSpecializationDecl>(RT->getDecl());
1099
1100 if (!CTSD)
Craig Topper36250ad2014-05-12 05:36:57 +00001101 return nullptr;
Richard Trieu91844232012-06-26 18:18:47 +00001102
1103 Ty = Context.getTemplateSpecializationType(
1104 TemplateName(CTSD->getSpecializedTemplate()),
David Majnemer6fbeee32016-07-07 04:43:07 +00001105 CTSD->getTemplateArgs().asArray(),
Richard Trieu3cee4132013-03-23 01:38:36 +00001106 Ty.getLocalUnqualifiedType().getCanonicalType());
Richard Trieu91844232012-06-26 18:18:47 +00001107
1108 return Ty->getAs<TemplateSpecializationType>();
1109 }
1110
Richard Trieu14714c42016-01-14 22:56:39 +00001111 /// Returns true if the DiffType is Type and false for Template.
1112 static bool OnlyPerformTypeDiff(ASTContext &Context, QualType FromType,
1113 QualType ToType,
1114 const TemplateSpecializationType *&FromArgTST,
1115 const TemplateSpecializationType *&ToArgTST) {
1116 if (FromType.isNull() || ToType.isNull())
1117 return true;
1118
1119 if (Context.hasSameType(FromType, ToType))
1120 return true;
1121
1122 FromArgTST = GetTemplateSpecializationType(Context, FromType);
1123 ToArgTST = GetTemplateSpecializationType(Context, ToType);
1124
1125 if (!FromArgTST || !ToArgTST)
1126 return true;
1127
1128 if (!hasSameTemplate(FromArgTST, ToArgTST))
1129 return true;
1130
1131 return false;
1132 }
1133
Richard Trieu9fee0b72014-08-27 06:24:47 +00001134 /// DiffTypes - Fills a DiffNode with information about a type difference.
Richard Trieu2c22a862016-01-15 01:08:56 +00001135 void DiffTypes(const TSTiterator &FromIter, const TSTiterator &ToIter) {
1136 QualType FromType = GetType(FromIter);
1137 QualType ToType = GetType(ToIter);
Richard Trieu9fee0b72014-08-27 06:24:47 +00001138
Richard Trieu14714c42016-01-14 22:56:39 +00001139 bool FromDefault = FromIter.isEnd() && !FromType.isNull();
1140 bool ToDefault = ToIter.isEnd() && !ToType.isNull();
Richard Trieu9fee0b72014-08-27 06:24:47 +00001141
Richard Trieu14714c42016-01-14 22:56:39 +00001142 const TemplateSpecializationType *FromArgTST = nullptr;
1143 const TemplateSpecializationType *ToArgTST = nullptr;
1144 if (OnlyPerformTypeDiff(Context, FromType, ToType, FromArgTST, ToArgTST)) {
1145 Tree.SetTypeDiff(FromType, ToType, FromDefault, ToDefault);
1146 Tree.SetSame(!FromType.isNull() && !ToType.isNull() &&
1147 Context.hasSameType(FromType, ToType));
1148 } else {
1149 assert(FromArgTST && ToArgTST &&
1150 "Both template specializations need to be valid.");
1151 Qualifiers FromQual = FromType.getQualifiers(),
1152 ToQual = ToType.getQualifiers();
1153 FromQual -= QualType(FromArgTST, 0).getQualifiers();
1154 ToQual -= QualType(ToArgTST, 0).getQualifiers();
1155 Tree.SetTemplateDiff(FromArgTST->getTemplateName().getAsTemplateDecl(),
1156 ToArgTST->getTemplateName().getAsTemplateDecl(),
1157 FromQual, ToQual, FromDefault, ToDefault);
1158 DiffTemplate(FromArgTST, ToArgTST);
Richard Trieu9fee0b72014-08-27 06:24:47 +00001159 }
Richard Trieu9fee0b72014-08-27 06:24:47 +00001160 }
1161
1162 /// DiffTemplateTemplates - Fills a DiffNode with information about a
1163 /// template template difference.
1164 void DiffTemplateTemplates(const TSTiterator &FromIter,
Richard Trieu2c22a862016-01-15 01:08:56 +00001165 const TSTiterator &ToIter) {
1166 TemplateDecl *FromDecl = GetTemplateDecl(FromIter);
1167 TemplateDecl *ToDecl = GetTemplateDecl(ToIter);
Richard Trieu14714c42016-01-14 22:56:39 +00001168 Tree.SetTemplateTemplateDiff(FromDecl, ToDecl, FromIter.isEnd() && FromDecl,
1169 ToIter.isEnd() && ToDecl);
Richard Trieu9fee0b72014-08-27 06:24:47 +00001170 Tree.SetSame(FromDecl && ToDecl &&
1171 FromDecl->getCanonicalDecl() == ToDecl->getCanonicalDecl());
Richard Trieu9fee0b72014-08-27 06:24:47 +00001172 }
1173
1174 /// InitializeNonTypeDiffVariables - Helper function for DiffNonTypes
Richard Trieud5c73782016-01-15 02:55:17 +00001175 static void InitializeNonTypeDiffVariables(ASTContext &Context,
1176 const TSTiterator &Iter,
1177 NonTypeTemplateParmDecl *Default,
1178 llvm::APSInt &Value, bool &HasInt,
1179 QualType &IntType, bool &IsNullPtr,
1180 Expr *&E, ValueDecl *&VD,
1181 bool &NeedAddressOf) {
Richard Trieu2c22a862016-01-15 01:08:56 +00001182 if (!Iter.isEnd()) {
1183 switch (Iter->getKind()) {
1184 default:
1185 llvm_unreachable("unknown ArgumentKind");
1186 case TemplateArgument::Integral:
1187 Value = Iter->getAsIntegral();
1188 HasInt = true;
Richard Trieud5c73782016-01-15 02:55:17 +00001189 IntType = Iter->getIntegralType();
Richard Trieu2c22a862016-01-15 01:08:56 +00001190 return;
1191 case TemplateArgument::Declaration: {
1192 VD = Iter->getAsDecl();
1193 QualType ArgType = Iter->getParamTypeForDecl();
1194 QualType VDType = VD->getType();
1195 if (ArgType->isPointerType() &&
1196 Context.hasSameType(ArgType->getPointeeType(), VDType))
1197 NeedAddressOf = true;
1198 return;
Richard Trieu9fee0b72014-08-27 06:24:47 +00001199 }
Richard Trieu2c22a862016-01-15 01:08:56 +00001200 case TemplateArgument::NullPtr:
1201 IsNullPtr = true;
1202 return;
1203 case TemplateArgument::Expression:
1204 E = Iter->getAsExpr();
Richard Trieu9fee0b72014-08-27 06:24:47 +00001205 }
Richard Trieu2c22a862016-01-15 01:08:56 +00001206 } else if (!Default->isParameterPack()) {
1207 E = Default->getDefaultArgument();
Richard Trieu9fee0b72014-08-27 06:24:47 +00001208 }
1209
Richard Trieu2c22a862016-01-15 01:08:56 +00001210 if (!Iter.hasDesugaredTA()) return;
Richard Trieu9fee0b72014-08-27 06:24:47 +00001211
Richard Trieu2c22a862016-01-15 01:08:56 +00001212 const TemplateArgument& TA = Iter.getDesugaredTA();
1213 switch (TA.getKind()) {
1214 default:
1215 llvm_unreachable("unknown ArgumentKind");
1216 case TemplateArgument::Integral:
1217 Value = TA.getAsIntegral();
1218 HasInt = true;
Richard Trieud5c73782016-01-15 02:55:17 +00001219 IntType = TA.getIntegralType();
Richard Trieu2c22a862016-01-15 01:08:56 +00001220 return;
1221 case TemplateArgument::Declaration: {
1222 VD = TA.getAsDecl();
1223 QualType ArgType = TA.getParamTypeForDecl();
1224 QualType VDType = VD->getType();
1225 if (ArgType->isPointerType() &&
1226 Context.hasSameType(ArgType->getPointeeType(), VDType))
1227 NeedAddressOf = true;
1228 return;
1229 }
1230 case TemplateArgument::NullPtr:
1231 IsNullPtr = true;
1232 return;
1233 case TemplateArgument::Expression:
1234 // TODO: Sometimes, the desugared template argument Expr differs from
1235 // the sugared template argument Expr. It may be useful in the future
1236 // but for now, it is just discarded.
1237 if (!E)
1238 E = TA.getAsExpr();
1239 return;
1240 }
Richard Trieu9fee0b72014-08-27 06:24:47 +00001241 }
1242
1243 /// DiffNonTypes - Handles any template parameters not handled by DiffTypes
1244 /// of DiffTemplatesTemplates, such as integer and declaration parameters.
1245 void DiffNonTypes(const TSTiterator &FromIter, const TSTiterator &ToIter,
1246 NonTypeTemplateParmDecl *FromDefaultNonTypeDecl,
1247 NonTypeTemplateParmDecl *ToDefaultNonTypeDecl) {
1248 Expr *FromExpr = nullptr, *ToExpr = nullptr;
1249 llvm::APSInt FromInt, ToInt;
Richard Trieud5c73782016-01-15 02:55:17 +00001250 QualType FromIntType, ToIntType;
Richard Trieu9fee0b72014-08-27 06:24:47 +00001251 ValueDecl *FromValueDecl = nullptr, *ToValueDecl = nullptr;
Richard Trieu2c22a862016-01-15 01:08:56 +00001252 bool HasFromInt = false, HasToInt = false, FromNullPtr = false,
1253 ToNullPtr = false, NeedFromAddressOf = false, NeedToAddressOf = false;
Richard Trieud5c73782016-01-15 02:55:17 +00001254 InitializeNonTypeDiffVariables(
1255 Context, FromIter, FromDefaultNonTypeDecl, FromInt, HasFromInt,
1256 FromIntType, FromNullPtr, FromExpr, FromValueDecl, NeedFromAddressOf);
Richard Trieu2c22a862016-01-15 01:08:56 +00001257 InitializeNonTypeDiffVariables(Context, ToIter, ToDefaultNonTypeDecl, ToInt,
Richard Trieud5c73782016-01-15 02:55:17 +00001258 HasToInt, ToIntType, ToNullPtr, ToExpr,
1259 ToValueDecl, NeedToAddressOf);
Richard Trieu2c22a862016-01-15 01:08:56 +00001260
Richard Trieu14714c42016-01-14 22:56:39 +00001261 bool FromDefault = FromIter.isEnd() &&
1262 (FromExpr || FromValueDecl || HasFromInt || FromNullPtr);
1263 bool ToDefault = ToIter.isEnd() &&
1264 (ToExpr || ToValueDecl || HasToInt || ToNullPtr);
1265
Richard Trieu9213ce52016-01-15 05:01:53 +00001266 bool FromDeclaration = FromValueDecl || FromNullPtr;
1267 bool ToDeclaration = ToValueDecl || ToNullPtr;
1268
1269 if (FromDeclaration && HasToInt) {
1270 Tree.SetFromDeclarationAndToIntegerDiff(
1271 FromValueDecl, NeedFromAddressOf, FromNullPtr, FromExpr, ToInt,
1272 HasToInt, ToIntType, ToExpr, FromDefault, ToDefault);
1273 Tree.SetSame(false);
1274 return;
1275
1276 }
1277
1278 if (HasFromInt && ToDeclaration) {
1279 Tree.SetFromIntegerAndToDeclarationDiff(
1280 FromInt, HasFromInt, FromIntType, FromExpr, ToValueDecl,
1281 NeedToAddressOf, ToNullPtr, ToExpr, FromDefault, ToDefault);
1282 Tree.SetSame(false);
1283 return;
1284 }
1285
Richard Trieu9fee0b72014-08-27 06:24:47 +00001286 if (HasFromInt || HasToInt) {
Richard Trieud5c73782016-01-15 02:55:17 +00001287 Tree.SetIntegerDiff(FromInt, ToInt, HasFromInt, HasToInt, FromIntType,
1288 ToIntType, FromExpr, ToExpr, FromDefault, ToDefault);
Richard Trieu15b66532015-01-24 02:48:32 +00001289 if (HasFromInt && HasToInt) {
Richard Trieud5c73782016-01-15 02:55:17 +00001290 Tree.SetSame(Context.hasSameType(FromIntType, ToIntType) &&
1291 FromInt == ToInt);
Richard Trieu15b66532015-01-24 02:48:32 +00001292 }
Richard Trieu9fee0b72014-08-27 06:24:47 +00001293 return;
1294 }
1295
Richard Trieu2c22a862016-01-15 01:08:56 +00001296 if (FromDeclaration || ToDeclaration) {
1297 Tree.SetDeclarationDiff(FromValueDecl, ToValueDecl, NeedFromAddressOf,
1298 NeedToAddressOf, FromNullPtr, ToNullPtr, FromExpr,
1299 ToExpr, FromDefault, ToDefault);
1300 bool BothNull = FromNullPtr && ToNullPtr;
1301 bool SameValueDecl =
1302 FromValueDecl && ToValueDecl &&
1303 NeedFromAddressOf == NeedToAddressOf &&
1304 FromValueDecl->getCanonicalDecl() == ToValueDecl->getCanonicalDecl();
1305 Tree.SetSame(BothNull || SameValueDecl);
1306 return;
1307 }
Richard Trieu9fee0b72014-08-27 06:24:47 +00001308
Richard Trieu2c22a862016-01-15 01:08:56 +00001309 assert((FromExpr || ToExpr) && "Both template arguments cannot be empty.");
1310 Tree.SetExpressionDiff(FromExpr, ToExpr, FromDefault, ToDefault);
1311 Tree.SetSame(IsEqualExpr(Context, FromExpr, ToExpr));
Richard Trieu9fee0b72014-08-27 06:24:47 +00001312 }
1313
Richard Trieu91844232012-06-26 18:18:47 +00001314 /// DiffTemplate - recursively visits template arguments and stores the
1315 /// argument info into a tree.
1316 void DiffTemplate(const TemplateSpecializationType *FromTST,
1317 const TemplateSpecializationType *ToTST) {
1318 // Begin descent into diffing template tree.
Benjamin Kramer3b05e202013-10-08 16:58:52 +00001319 TemplateParameterList *ParamsFrom =
Richard Trieu91844232012-06-26 18:18:47 +00001320 FromTST->getTemplateName().getAsTemplateDecl()->getTemplateParameters();
Benjamin Kramer3b05e202013-10-08 16:58:52 +00001321 TemplateParameterList *ParamsTo =
1322 ToTST->getTemplateName().getAsTemplateDecl()->getTemplateParameters();
Richard Trieu91844232012-06-26 18:18:47 +00001323 unsigned TotalArgs = 0;
Richard Trieu64ab30392013-03-15 23:55:09 +00001324 for (TSTiterator FromIter(Context, FromTST), ToIter(Context, ToTST);
Richard Trieu91844232012-06-26 18:18:47 +00001325 !FromIter.isEnd() || !ToIter.isEnd(); ++TotalArgs) {
1326 Tree.AddNode();
1327
1328 // Get the parameter at index TotalArgs. If index is larger
1329 // than the total number of parameters, then there is an
1330 // argument pack, so re-use the last parameter.
Richard Trieu9fee0b72014-08-27 06:24:47 +00001331 unsigned FromParamIndex = std::min(TotalArgs, ParamsFrom->size() - 1);
1332 unsigned ToParamIndex = std::min(TotalArgs, ParamsTo->size() - 1);
1333 NamedDecl *FromParamND = ParamsFrom->getParam(FromParamIndex);
1334 NamedDecl *ToParamND = ParamsTo->getParam(ToParamIndex);
Benjamin Kramer3b05e202013-10-08 16:58:52 +00001335
Richard Trieu2c22a862016-01-15 01:08:56 +00001336 assert(FromParamND->getKind() == ToParamND->getKind() &&
1337 "Parameter Decl are not the same kind.");
Richard Trieu91844232012-06-26 18:18:47 +00001338
Richard Trieu2c22a862016-01-15 01:08:56 +00001339 if (isa<TemplateTypeParmDecl>(FromParamND)) {
1340 DiffTypes(FromIter, ToIter);
1341 } else if (isa<TemplateTemplateParmDecl>(FromParamND)) {
1342 DiffTemplateTemplates(FromIter, ToIter);
1343 } else if (isa<NonTypeTemplateParmDecl>(FromParamND)) {
1344 NonTypeTemplateParmDecl *FromDefaultNonTypeDecl =
1345 cast<NonTypeTemplateParmDecl>(FromParamND);
1346 NonTypeTemplateParmDecl *ToDefaultNonTypeDecl =
1347 cast<NonTypeTemplateParmDecl>(ToParamND);
Richard Trieu9fee0b72014-08-27 06:24:47 +00001348 DiffNonTypes(FromIter, ToIter, FromDefaultNonTypeDecl,
1349 ToDefaultNonTypeDecl);
Richard Trieu2c22a862016-01-15 01:08:56 +00001350 } else {
1351 llvm_unreachable("Unexpected Decl type.");
1352 }
Richard Trieu91844232012-06-26 18:18:47 +00001353
Richard Trieu64ab30392013-03-15 23:55:09 +00001354 ++FromIter;
1355 ++ToIter;
Richard Trieu91844232012-06-26 18:18:47 +00001356 Tree.Up();
1357 }
1358 }
1359
Richard Trieu8e14cac2012-09-28 19:51:57 +00001360 /// makeTemplateList - Dump every template alias into the vector.
1361 static void makeTemplateList(
Craig Topper5603df42013-07-05 19:34:19 +00001362 SmallVectorImpl<const TemplateSpecializationType *> &TemplateList,
Richard Trieu8e14cac2012-09-28 19:51:57 +00001363 const TemplateSpecializationType *TST) {
1364 while (TST) {
1365 TemplateList.push_back(TST);
1366 if (!TST->isTypeAlias())
1367 return;
1368 TST = TST->getAliasedType()->getAs<TemplateSpecializationType>();
1369 }
1370 }
1371
1372 /// hasSameBaseTemplate - Returns true when the base templates are the same,
1373 /// even if the template arguments are not.
1374 static bool hasSameBaseTemplate(const TemplateSpecializationType *FromTST,
1375 const TemplateSpecializationType *ToTST) {
Douglas Gregor8e9f55f2013-01-31 01:08:35 +00001376 return FromTST->getTemplateName().getAsTemplateDecl()->getCanonicalDecl() ==
1377 ToTST->getTemplateName().getAsTemplateDecl()->getCanonicalDecl();
Richard Trieu8e14cac2012-09-28 19:51:57 +00001378 }
1379
Richard Trieu91844232012-06-26 18:18:47 +00001380 /// hasSameTemplate - Returns true if both types are specialized from the
1381 /// same template declaration. If they come from different template aliases,
1382 /// do a parallel ascension search to determine the highest template alias in
1383 /// common and set the arguments to them.
1384 static bool hasSameTemplate(const TemplateSpecializationType *&FromTST,
1385 const TemplateSpecializationType *&ToTST) {
1386 // Check the top templates if they are the same.
Richard Trieu8e14cac2012-09-28 19:51:57 +00001387 if (hasSameBaseTemplate(FromTST, ToTST))
Richard Trieu91844232012-06-26 18:18:47 +00001388 return true;
1389
1390 // Create vectors of template aliases.
1391 SmallVector<const TemplateSpecializationType*, 1> FromTemplateList,
1392 ToTemplateList;
1393
Richard Trieu8e14cac2012-09-28 19:51:57 +00001394 makeTemplateList(FromTemplateList, FromTST);
1395 makeTemplateList(ToTemplateList, ToTST);
Richard Trieu91844232012-06-26 18:18:47 +00001396
Craig Topper61ac9062013-07-08 03:55:09 +00001397 SmallVectorImpl<const TemplateSpecializationType *>::reverse_iterator
Richard Trieu91844232012-06-26 18:18:47 +00001398 FromIter = FromTemplateList.rbegin(), FromEnd = FromTemplateList.rend(),
1399 ToIter = ToTemplateList.rbegin(), ToEnd = ToTemplateList.rend();
1400
1401 // Check if the lowest template types are the same. If not, return.
Richard Trieu8e14cac2012-09-28 19:51:57 +00001402 if (!hasSameBaseTemplate(*FromIter, *ToIter))
Richard Trieu91844232012-06-26 18:18:47 +00001403 return false;
1404
1405 // Begin searching up the template aliases. The bottom most template
1406 // matches so move up until one pair does not match. Use the template
1407 // right before that one.
1408 for (; FromIter != FromEnd && ToIter != ToEnd; ++FromIter, ++ToIter) {
Richard Trieu8e14cac2012-09-28 19:51:57 +00001409 if (!hasSameBaseTemplate(*FromIter, *ToIter))
Richard Trieu91844232012-06-26 18:18:47 +00001410 break;
1411 }
1412
1413 FromTST = FromIter[-1];
1414 ToTST = ToIter[-1];
1415
1416 return true;
1417 }
1418
1419 /// GetType - Retrieves the template type arguments, including default
1420 /// arguments.
Richard Trieu2c22a862016-01-15 01:08:56 +00001421 static QualType GetType(const TSTiterator &Iter) {
Richard Trieu91844232012-06-26 18:18:47 +00001422 if (!Iter.isEnd())
Richard Trieu17f13ec2013-04-03 03:06:48 +00001423 return Iter->getAsType();
Richard Trieu2c22a862016-01-15 01:08:56 +00001424 if (Iter.hasDesugaredTA())
Richard Trieuac1e2f82016-01-14 23:30:12 +00001425 return Iter.getDesugaredTA().getAsType();
Richard Trieu2c22a862016-01-15 01:08:56 +00001426 return QualType();
Richard Trieu38800892014-07-24 04:24:50 +00001427 }
1428
Richard Trieu91844232012-06-26 18:18:47 +00001429 /// GetTemplateDecl - Retrieves the template template arguments, including
1430 /// default arguments.
Richard Trieu2c22a862016-01-15 01:08:56 +00001431 static TemplateDecl *GetTemplateDecl(const TSTiterator &Iter) {
Richard Trieu91844232012-06-26 18:18:47 +00001432 if (!Iter.isEnd())
Richard Trieu17f13ec2013-04-03 03:06:48 +00001433 return Iter->getAsTemplate().getAsTemplateDecl();
Richard Trieu2c22a862016-01-15 01:08:56 +00001434 if (Iter.hasDesugaredTA())
1435 return Iter.getDesugaredTA().getAsTemplate().getAsTemplateDecl();
Craig Topper36250ad2014-05-12 05:36:57 +00001436 return nullptr;
Richard Trieu91844232012-06-26 18:18:47 +00001437 }
1438
Richard Trieu2c22a862016-01-15 01:08:56 +00001439 /// IsEqualExpr - Returns true if the expressions are the same in regards to
1440 /// template arguments. These expressions are dependent, so profile them
1441 /// instead of trying to evaluate them.
Richard Trieu15b66532015-01-24 02:48:32 +00001442 static bool IsEqualExpr(ASTContext &Context, Expr *FromExpr, Expr *ToExpr) {
Richard Trieu91844232012-06-26 18:18:47 +00001443 if (FromExpr == ToExpr)
1444 return true;
1445
1446 if (!FromExpr || !ToExpr)
1447 return false;
1448
Richard Trieu2c22a862016-01-15 01:08:56 +00001449 llvm::FoldingSetNodeID FromID, ToID;
1450 FromExpr->Profile(FromID, Context, true);
1451 ToExpr->Profile(ToID, Context, true);
1452 return FromID == ToID;
Richard Trieu91844232012-06-26 18:18:47 +00001453 }
1454
1455 // These functions converts the tree representation of the template
1456 // differences into the internal character vector.
1457
1458 /// TreeToString - Converts the Tree object into a character stream which
1459 /// will later be turned into the output string.
1460 void TreeToString(int Indent = 1) {
1461 if (PrintTree) {
1462 OS << '\n';
Benjamin Kramer6582c362013-02-22 16:13:34 +00001463 OS.indent(2 * Indent);
Richard Trieu91844232012-06-26 18:18:47 +00001464 ++Indent;
1465 }
1466
1467 // Handle cases where the difference is not templates with different
1468 // arguments.
Richard Trieub4cff112013-03-15 20:35:18 +00001469 switch (Tree.GetKind()) {
Richard Trieub4cff112013-03-15 20:35:18 +00001470 case DiffTree::Invalid:
1471 llvm_unreachable("Template diffing failed with bad DiffNode");
1472 case DiffTree::Type: {
Richard Trieu91844232012-06-26 18:18:47 +00001473 QualType FromType, ToType;
Richard Trieu14714c42016-01-14 22:56:39 +00001474 Tree.GetTypeDiff(FromType, ToType);
Richard Trieu91844232012-06-26 18:18:47 +00001475 PrintTypeNames(FromType, ToType, Tree.FromDefault(), Tree.ToDefault(),
1476 Tree.NodeIsSame());
1477 return;
1478 }
Richard Trieub4cff112013-03-15 20:35:18 +00001479 case DiffTree::Expression: {
Richard Trieu91844232012-06-26 18:18:47 +00001480 Expr *FromExpr, *ToExpr;
Richard Trieu2c22a862016-01-15 01:08:56 +00001481 Tree.GetExpressionDiff(FromExpr, ToExpr);
1482 PrintExpr(FromExpr, ToExpr, Tree.FromDefault(), Tree.ToDefault(),
1483 Tree.NodeIsSame());
Richard Trieu91844232012-06-26 18:18:47 +00001484 return;
1485 }
Richard Trieub4cff112013-03-15 20:35:18 +00001486 case DiffTree::TemplateTemplate: {
Richard Trieu91844232012-06-26 18:18:47 +00001487 TemplateDecl *FromTD, *ToTD;
Richard Trieu14714c42016-01-14 22:56:39 +00001488 Tree.GetTemplateTemplateDiff(FromTD, ToTD);
Richard Trieu91844232012-06-26 18:18:47 +00001489 PrintTemplateTemplate(FromTD, ToTD, Tree.FromDefault(),
1490 Tree.ToDefault(), Tree.NodeIsSame());
1491 return;
1492 }
Richard Trieub4cff112013-03-15 20:35:18 +00001493 case DiffTree::Integer: {
Richard Trieu6df89452012-11-01 21:29:28 +00001494 llvm::APSInt FromInt, ToInt;
Richard Trieu64ab30392013-03-15 23:55:09 +00001495 Expr *FromExpr, *ToExpr;
Richard Trieu6df89452012-11-01 21:29:28 +00001496 bool IsValidFromInt, IsValidToInt;
Richard Trieud5c73782016-01-15 02:55:17 +00001497 QualType FromIntType, ToIntType;
Richard Trieu14714c42016-01-14 22:56:39 +00001498 Tree.GetIntegerDiff(FromInt, ToInt, IsValidFromInt, IsValidToInt,
Richard Trieud5c73782016-01-15 02:55:17 +00001499 FromIntType, ToIntType, FromExpr, ToExpr);
1500 PrintAPSInt(FromInt, ToInt, IsValidFromInt, IsValidToInt, FromIntType,
1501 ToIntType, FromExpr, ToExpr, Tree.FromDefault(),
1502 Tree.ToDefault(), Tree.NodeIsSame());
Richard Trieu6df89452012-11-01 21:29:28 +00001503 return;
1504 }
Richard Trieub4cff112013-03-15 20:35:18 +00001505 case DiffTree::Declaration: {
Richard Trieu954aaaf2013-02-27 01:41:53 +00001506 ValueDecl *FromValueDecl, *ToValueDecl;
Richard Trieu091872c52013-05-07 21:36:24 +00001507 bool FromAddressOf, ToAddressOf;
Richard Trieu14714c42016-01-14 22:56:39 +00001508 bool FromNullPtr, ToNullPtr;
Richard Trieu2c22a862016-01-15 01:08:56 +00001509 Expr *FromExpr, *ToExpr;
Richard Trieu14714c42016-01-14 22:56:39 +00001510 Tree.GetDeclarationDiff(FromValueDecl, ToValueDecl, FromAddressOf,
Richard Trieu2c22a862016-01-15 01:08:56 +00001511 ToAddressOf, FromNullPtr, ToNullPtr, FromExpr,
1512 ToExpr);
Richard Trieu091872c52013-05-07 21:36:24 +00001513 PrintValueDecl(FromValueDecl, ToValueDecl, FromAddressOf, ToAddressOf,
Richard Trieu2c22a862016-01-15 01:08:56 +00001514 FromNullPtr, ToNullPtr, FromExpr, ToExpr,
1515 Tree.FromDefault(), Tree.ToDefault(), Tree.NodeIsSame());
Richard Trieu954aaaf2013-02-27 01:41:53 +00001516 return;
1517 }
Richard Trieu9213ce52016-01-15 05:01:53 +00001518 case DiffTree::FromDeclarationAndToInteger: {
1519 ValueDecl *FromValueDecl;
1520 bool FromAddressOf;
1521 bool FromNullPtr;
1522 Expr *FromExpr;
1523 llvm::APSInt ToInt;
1524 bool IsValidToInt;
1525 QualType ToIntType;
1526 Expr *ToExpr;
1527 Tree.GetFromDeclarationAndToIntegerDiff(
1528 FromValueDecl, FromAddressOf, FromNullPtr, FromExpr, ToInt,
1529 IsValidToInt, ToIntType, ToExpr);
1530 assert((FromValueDecl || FromNullPtr) && IsValidToInt);
1531 PrintValueDeclAndInteger(FromValueDecl, FromAddressOf, FromNullPtr,
1532 FromExpr, Tree.FromDefault(), ToInt, ToIntType,
1533 ToExpr, Tree.ToDefault());
1534 return;
1535 }
1536 case DiffTree::FromIntegerAndToDeclaration: {
1537 llvm::APSInt FromInt;
1538 bool IsValidFromInt;
1539 QualType FromIntType;
1540 Expr *FromExpr;
1541 ValueDecl *ToValueDecl;
1542 bool ToAddressOf;
1543 bool ToNullPtr;
1544 Expr *ToExpr;
1545 Tree.GetFromIntegerAndToDeclarationDiff(
1546 FromInt, IsValidFromInt, FromIntType, FromExpr, ToValueDecl,
1547 ToAddressOf, ToNullPtr, ToExpr);
1548 assert(IsValidFromInt && (ToValueDecl || ToNullPtr));
1549 PrintIntegerAndValueDecl(FromInt, FromIntType, FromExpr,
1550 Tree.FromDefault(), ToValueDecl, ToAddressOf,
1551 ToNullPtr, ToExpr, Tree.ToDefault());
1552 return;
1553 }
Richard Trieub4cff112013-03-15 20:35:18 +00001554 case DiffTree::Template: {
1555 // Node is root of template. Recurse on children.
1556 TemplateDecl *FromTD, *ToTD;
Richard Trieu14714c42016-01-14 22:56:39 +00001557 Qualifiers FromQual, ToQual;
1558 Tree.GetTemplateDiff(FromTD, ToTD, FromQual, ToQual);
Richard Trieu954aaaf2013-02-27 01:41:53 +00001559
Richard Trieu2331c8b2016-01-15 05:48:38 +00001560 PrintQualifiers(FromQual, ToQual);
1561
Richard Trieub4cff112013-03-15 20:35:18 +00001562 if (!Tree.HasChildren()) {
1563 // If we're dealing with a template specialization with zero
1564 // arguments, there are no children; special-case this.
1565 OS << FromTD->getNameAsString() << "<>";
1566 return;
Richard Trieu91844232012-06-26 18:18:47 +00001567 }
Richard Trieub4cff112013-03-15 20:35:18 +00001568
Richard Trieu14714c42016-01-14 22:56:39 +00001569 OS << FromTD->getNameAsString() << '<';
Richard Trieub4cff112013-03-15 20:35:18 +00001570 Tree.MoveToChild();
1571 unsigned NumElideArgs = 0;
Richard Trieu12074502016-02-02 00:36:59 +00001572 bool AllArgsElided = true;
Richard Trieub4cff112013-03-15 20:35:18 +00001573 do {
1574 if (ElideType) {
1575 if (Tree.NodeIsSame()) {
1576 ++NumElideArgs;
1577 continue;
1578 }
Richard Trieu12074502016-02-02 00:36:59 +00001579 AllArgsElided = false;
Richard Trieub4cff112013-03-15 20:35:18 +00001580 if (NumElideArgs > 0) {
1581 PrintElideArgs(NumElideArgs, Indent);
1582 NumElideArgs = 0;
1583 OS << ", ";
1584 }
1585 }
1586 TreeToString(Indent);
1587 if (Tree.HasNextSibling())
1588 OS << ", ";
1589 } while (Tree.AdvanceSibling());
Richard Trieu12074502016-02-02 00:36:59 +00001590 if (NumElideArgs > 0) {
1591 if (AllArgsElided)
1592 OS << "...";
1593 else
1594 PrintElideArgs(NumElideArgs, Indent);
1595 }
Richard Trieu91844232012-06-26 18:18:47 +00001596
Richard Trieub4cff112013-03-15 20:35:18 +00001597 Tree.Parent();
1598 OS << ">";
1599 return;
1600 }
1601 }
Richard Trieu91844232012-06-26 18:18:47 +00001602 }
1603
1604 // To signal to the text printer that a certain text needs to be bolded,
1605 // a special character is injected into the character stream which the
1606 // text printer will later strip out.
1607
1608 /// Bold - Start bolding text.
1609 void Bold() {
1610 assert(!IsBold && "Attempting to bold text that is already bold.");
1611 IsBold = true;
1612 if (ShowColor)
1613 OS << ToggleHighlight;
1614 }
1615
1616 /// Unbold - Stop bolding text.
1617 void Unbold() {
1618 assert(IsBold && "Attempting to remove bold from unbold text.");
1619 IsBold = false;
1620 if (ShowColor)
1621 OS << ToggleHighlight;
1622 }
1623
1624 // Functions to print out the arguments and highlighting the difference.
1625
1626 /// PrintTypeNames - prints the typenames, bolding differences. Will detect
1627 /// typenames that are the same and attempt to disambiguate them by using
1628 /// canonical typenames.
1629 void PrintTypeNames(QualType FromType, QualType ToType,
1630 bool FromDefault, bool ToDefault, bool Same) {
1631 assert((!FromType.isNull() || !ToType.isNull()) &&
1632 "Only one template argument may be missing.");
1633
1634 if (Same) {
Richard Trieud86c9012014-07-25 00:24:02 +00001635 OS << FromType.getAsString(Policy);
Richard Trieu91844232012-06-26 18:18:47 +00001636 return;
1637 }
1638
Richard Trieub7243852012-09-28 20:32:51 +00001639 if (!FromType.isNull() && !ToType.isNull() &&
1640 FromType.getLocalUnqualifiedType() ==
1641 ToType.getLocalUnqualifiedType()) {
1642 Qualifiers FromQual = FromType.getLocalQualifiers(),
Alp Tokeraced95a2013-11-26 02:52:41 +00001643 ToQual = ToType.getLocalQualifiers();
Richard Trieub7243852012-09-28 20:32:51 +00001644 PrintQualifiers(FromQual, ToQual);
1645 FromType.getLocalUnqualifiedType().print(OS, Policy);
1646 return;
1647 }
1648
Richard Trieu91844232012-06-26 18:18:47 +00001649 std::string FromTypeStr = FromType.isNull() ? "(no argument)"
Richard Trieud86c9012014-07-25 00:24:02 +00001650 : FromType.getAsString(Policy);
Richard Trieu91844232012-06-26 18:18:47 +00001651 std::string ToTypeStr = ToType.isNull() ? "(no argument)"
Richard Trieud86c9012014-07-25 00:24:02 +00001652 : ToType.getAsString(Policy);
Richard Trieu91844232012-06-26 18:18:47 +00001653 // Switch to canonical typename if it is better.
1654 // TODO: merge this with other aka printing above.
1655 if (FromTypeStr == ToTypeStr) {
Richard Trieud86c9012014-07-25 00:24:02 +00001656 std::string FromCanTypeStr =
1657 FromType.getCanonicalType().getAsString(Policy);
1658 std::string ToCanTypeStr = ToType.getCanonicalType().getAsString(Policy);
Richard Trieu91844232012-06-26 18:18:47 +00001659 if (FromCanTypeStr != ToCanTypeStr) {
1660 FromTypeStr = FromCanTypeStr;
1661 ToTypeStr = ToCanTypeStr;
1662 }
1663 }
1664
1665 if (PrintTree) OS << '[';
1666 OS << (FromDefault ? "(default) " : "");
1667 Bold();
1668 OS << FromTypeStr;
1669 Unbold();
1670 if (PrintTree) {
1671 OS << " != " << (ToDefault ? "(default) " : "");
1672 Bold();
1673 OS << ToTypeStr;
1674 Unbold();
1675 OS << "]";
1676 }
Richard Trieu91844232012-06-26 18:18:47 +00001677 }
1678
1679 /// PrintExpr - Prints out the expr template arguments, highlighting argument
1680 /// differences.
Richard Trieu2c22a862016-01-15 01:08:56 +00001681 void PrintExpr(const Expr *FromExpr, const Expr *ToExpr, bool FromDefault,
1682 bool ToDefault, bool Same) {
Richard Trieu91844232012-06-26 18:18:47 +00001683 assert((FromExpr || ToExpr) &&
1684 "Only one template argument may be missing.");
1685 if (Same) {
Richard Trieu2c22a862016-01-15 01:08:56 +00001686 PrintExpr(FromExpr);
Richard Trieu91844232012-06-26 18:18:47 +00001687 } else if (!PrintTree) {
1688 OS << (FromDefault ? "(default) " : "");
1689 Bold();
Richard Trieu2c22a862016-01-15 01:08:56 +00001690 PrintExpr(FromExpr);
Richard Trieu91844232012-06-26 18:18:47 +00001691 Unbold();
1692 } else {
1693 OS << (FromDefault ? "[(default) " : "[");
1694 Bold();
Richard Trieu2c22a862016-01-15 01:08:56 +00001695 PrintExpr(FromExpr);
Richard Trieu91844232012-06-26 18:18:47 +00001696 Unbold();
1697 OS << " != " << (ToDefault ? "(default) " : "");
1698 Bold();
Richard Trieu2c22a862016-01-15 01:08:56 +00001699 PrintExpr(ToExpr);
Richard Trieu91844232012-06-26 18:18:47 +00001700 Unbold();
1701 OS << ']';
1702 }
1703 }
1704
1705 /// PrintExpr - Actual formatting and printing of expressions.
Richard Trieu2c22a862016-01-15 01:08:56 +00001706 void PrintExpr(const Expr *E) {
Richard Trieu38800892014-07-24 04:24:50 +00001707 if (E) {
Craig Topper36250ad2014-05-12 05:36:57 +00001708 E->printPretty(OS, nullptr, Policy);
Richard Trieu38800892014-07-24 04:24:50 +00001709 return;
1710 }
Richard Trieu38800892014-07-24 04:24:50 +00001711 OS << "(no argument)";
Richard Trieu91844232012-06-26 18:18:47 +00001712 }
1713
1714 /// PrintTemplateTemplate - Handles printing of template template arguments,
1715 /// highlighting argument differences.
1716 void PrintTemplateTemplate(TemplateDecl *FromTD, TemplateDecl *ToTD,
1717 bool FromDefault, bool ToDefault, bool Same) {
1718 assert((FromTD || ToTD) && "Only one template argument may be missing.");
Richard Trieue673d71a2013-01-31 02:47:46 +00001719
1720 std::string FromName = FromTD ? FromTD->getName() : "(no argument)";
1721 std::string ToName = ToTD ? ToTD->getName() : "(no argument)";
1722 if (FromTD && ToTD && FromName == ToName) {
1723 FromName = FromTD->getQualifiedNameAsString();
1724 ToName = ToTD->getQualifiedNameAsString();
1725 }
1726
Richard Trieu91844232012-06-26 18:18:47 +00001727 if (Same) {
1728 OS << "template " << FromTD->getNameAsString();
1729 } else if (!PrintTree) {
1730 OS << (FromDefault ? "(default) template " : "template ");
1731 Bold();
Richard Trieue673d71a2013-01-31 02:47:46 +00001732 OS << FromName;
Richard Trieu91844232012-06-26 18:18:47 +00001733 Unbold();
1734 } else {
1735 OS << (FromDefault ? "[(default) template " : "[template ");
1736 Bold();
Richard Trieue673d71a2013-01-31 02:47:46 +00001737 OS << FromName;
Richard Trieu91844232012-06-26 18:18:47 +00001738 Unbold();
1739 OS << " != " << (ToDefault ? "(default) template " : "template ");
1740 Bold();
Richard Trieue673d71a2013-01-31 02:47:46 +00001741 OS << ToName;
Richard Trieu91844232012-06-26 18:18:47 +00001742 Unbold();
1743 OS << ']';
1744 }
1745 }
1746
Richard Trieu6df89452012-11-01 21:29:28 +00001747 /// PrintAPSInt - Handles printing of integral arguments, highlighting
1748 /// argument differences.
Benjamin Kramer7320b992016-06-15 14:20:56 +00001749 void PrintAPSInt(const llvm::APSInt &FromInt, const llvm::APSInt &ToInt,
Richard Trieud5c73782016-01-15 02:55:17 +00001750 bool IsValidFromInt, bool IsValidToInt, QualType FromIntType,
1751 QualType ToIntType, Expr *FromExpr, Expr *ToExpr,
1752 bool FromDefault, bool ToDefault, bool Same) {
Richard Trieu6df89452012-11-01 21:29:28 +00001753 assert((IsValidFromInt || IsValidToInt) &&
1754 "Only one integral argument may be missing.");
1755
1756 if (Same) {
Richard Trieud5c73782016-01-15 02:55:17 +00001757 if (FromIntType->isBooleanType()) {
1758 OS << ((FromInt == 0) ? "false" : "true");
1759 } else {
1760 OS << FromInt.toString(10);
1761 }
1762 return;
1763 }
1764
1765 bool PrintType = IsValidFromInt && IsValidToInt &&
1766 !Context.hasSameType(FromIntType, ToIntType);
1767
1768 if (!PrintTree) {
Richard Trieu6df89452012-11-01 21:29:28 +00001769 OS << (FromDefault ? "(default) " : "");
Richard Trieud5c73782016-01-15 02:55:17 +00001770 PrintAPSInt(FromInt, FromExpr, IsValidFromInt, FromIntType, PrintType);
Richard Trieu6df89452012-11-01 21:29:28 +00001771 } else {
1772 OS << (FromDefault ? "[(default) " : "[");
Richard Trieud5c73782016-01-15 02:55:17 +00001773 PrintAPSInt(FromInt, FromExpr, IsValidFromInt, FromIntType, PrintType);
Richard Trieu6df89452012-11-01 21:29:28 +00001774 OS << " != " << (ToDefault ? "(default) " : "");
Richard Trieud5c73782016-01-15 02:55:17 +00001775 PrintAPSInt(ToInt, ToExpr, IsValidToInt, ToIntType, PrintType);
Richard Trieu6df89452012-11-01 21:29:28 +00001776 OS << ']';
1777 }
1778 }
1779
Richard Trieu64ab30392013-03-15 23:55:09 +00001780 /// PrintAPSInt - If valid, print the APSInt. If the expression is
1781 /// gives more information, print it too.
Benjamin Kramer7320b992016-06-15 14:20:56 +00001782 void PrintAPSInt(const llvm::APSInt &Val, Expr *E, bool Valid,
1783 QualType IntType, bool PrintType) {
Richard Trieu64ab30392013-03-15 23:55:09 +00001784 Bold();
1785 if (Valid) {
1786 if (HasExtraInfo(E)) {
1787 PrintExpr(E);
1788 Unbold();
1789 OS << " aka ";
1790 Bold();
1791 }
Richard Trieud5c73782016-01-15 02:55:17 +00001792 if (PrintType) {
1793 Unbold();
1794 OS << "(";
1795 Bold();
1796 IntType.print(OS, Context.getPrintingPolicy());
1797 Unbold();
1798 OS << ") ";
1799 Bold();
1800 }
1801 if (IntType->isBooleanType()) {
1802 OS << ((Val == 0) ? "false" : "true");
1803 } else {
1804 OS << Val.toString(10);
1805 }
Nikola Smiljanic3fe1e092014-07-01 04:17:53 +00001806 } else if (E) {
1807 PrintExpr(E);
Richard Trieu64ab30392013-03-15 23:55:09 +00001808 } else {
1809 OS << "(no argument)";
1810 }
1811 Unbold();
1812 }
Richard Trieu15b66532015-01-24 02:48:32 +00001813
Richard Trieu14714c42016-01-14 22:56:39 +00001814 /// HasExtraInfo - Returns true if E is not an integer literal, the
1815 /// negation of an integer literal, or a boolean literal.
Richard Trieu64ab30392013-03-15 23:55:09 +00001816 bool HasExtraInfo(Expr *E) {
1817 if (!E) return false;
Richard Trieu15b66532015-01-24 02:48:32 +00001818
1819 E = E->IgnoreImpCasts();
1820
Richard Trieu64ab30392013-03-15 23:55:09 +00001821 if (isa<IntegerLiteral>(E)) return false;
1822
1823 if (UnaryOperator *UO = dyn_cast<UnaryOperator>(E))
1824 if (UO->getOpcode() == UO_Minus)
1825 if (isa<IntegerLiteral>(UO->getSubExpr()))
1826 return false;
1827
Richard Trieu14714c42016-01-14 22:56:39 +00001828 if (isa<CXXBoolLiteralExpr>(E))
1829 return false;
1830
Richard Trieu64ab30392013-03-15 23:55:09 +00001831 return true;
1832 }
1833
Richard Trieu2c22a862016-01-15 01:08:56 +00001834 void PrintValueDecl(ValueDecl *VD, bool AddressOf, Expr *E, bool NullPtr) {
Richard Trieu38800892014-07-24 04:24:50 +00001835 if (VD) {
1836 if (AddressOf)
1837 OS << "&";
1838 OS << VD->getName();
1839 return;
1840 }
1841
1842 if (NullPtr) {
Richard Trieu2c22a862016-01-15 01:08:56 +00001843 if (E && !isa<CXXNullPtrLiteralExpr>(E)) {
1844 PrintExpr(E);
1845 if (IsBold) {
1846 Unbold();
1847 OS << " aka ";
1848 Bold();
1849 } else {
1850 OS << " aka ";
1851 }
1852 }
1853
Richard Trieu38800892014-07-24 04:24:50 +00001854 OS << "nullptr";
1855 return;
1856 }
1857
1858 OS << "(no argument)";
1859 }
1860
Richard Trieu954aaaf2013-02-27 01:41:53 +00001861 /// PrintDecl - Handles printing of Decl arguments, highlighting
1862 /// argument differences.
1863 void PrintValueDecl(ValueDecl *FromValueDecl, ValueDecl *ToValueDecl,
Richard Trieu38800892014-07-24 04:24:50 +00001864 bool FromAddressOf, bool ToAddressOf, bool FromNullPtr,
Richard Trieu2c22a862016-01-15 01:08:56 +00001865 bool ToNullPtr, Expr *FromExpr, Expr *ToExpr,
1866 bool FromDefault, bool ToDefault, bool Same) {
Richard Trieu38800892014-07-24 04:24:50 +00001867 assert((FromValueDecl || FromNullPtr || ToValueDecl || ToNullPtr) &&
Richard Trieu954aaaf2013-02-27 01:41:53 +00001868 "Only one Decl argument may be NULL");
1869
1870 if (Same) {
Richard Trieu2c22a862016-01-15 01:08:56 +00001871 PrintValueDecl(FromValueDecl, FromAddressOf, FromExpr, FromNullPtr);
Richard Trieu954aaaf2013-02-27 01:41:53 +00001872 } else if (!PrintTree) {
1873 OS << (FromDefault ? "(default) " : "");
1874 Bold();
Richard Trieu2c22a862016-01-15 01:08:56 +00001875 PrintValueDecl(FromValueDecl, FromAddressOf, FromExpr, FromNullPtr);
Richard Trieu954aaaf2013-02-27 01:41:53 +00001876 Unbold();
1877 } else {
1878 OS << (FromDefault ? "[(default) " : "[");
1879 Bold();
Richard Trieu2c22a862016-01-15 01:08:56 +00001880 PrintValueDecl(FromValueDecl, FromAddressOf, FromExpr, FromNullPtr);
Richard Trieu954aaaf2013-02-27 01:41:53 +00001881 Unbold();
1882 OS << " != " << (ToDefault ? "(default) " : "");
1883 Bold();
Richard Trieu2c22a862016-01-15 01:08:56 +00001884 PrintValueDecl(ToValueDecl, ToAddressOf, ToExpr, ToNullPtr);
Richard Trieu954aaaf2013-02-27 01:41:53 +00001885 Unbold();
1886 OS << ']';
1887 }
Richard Trieu954aaaf2013-02-27 01:41:53 +00001888 }
1889
Richard Trieu9213ce52016-01-15 05:01:53 +00001890 /// PrintValueDeclAndInteger - Uses the print functions for ValueDecl and
1891 /// APSInt to print a mixed difference.
1892 void PrintValueDeclAndInteger(ValueDecl *VD, bool NeedAddressOf,
1893 bool IsNullPtr, Expr *VDExpr, bool DefaultDecl,
Benjamin Kramer7320b992016-06-15 14:20:56 +00001894 const llvm::APSInt &Val, QualType IntType,
Richard Trieu9213ce52016-01-15 05:01:53 +00001895 Expr *IntExpr, bool DefaultInt) {
1896 if (!PrintTree) {
1897 OS << (DefaultDecl ? "(default) " : "");
1898 Bold();
1899 PrintValueDecl(VD, NeedAddressOf, VDExpr, IsNullPtr);
1900 Unbold();
1901 } else {
1902 OS << (DefaultDecl ? "[(default) " : "[");
1903 Bold();
1904 PrintValueDecl(VD, NeedAddressOf, VDExpr, IsNullPtr);
1905 Unbold();
1906 OS << " != " << (DefaultInt ? "(default) " : "");
1907 PrintAPSInt(Val, IntExpr, true /*Valid*/, IntType, false /*PrintType*/);
1908 OS << ']';
1909 }
1910 }
1911
1912 /// PrintIntegerAndValueDecl - Uses the print functions for APSInt and
1913 /// ValueDecl to print a mixed difference.
Benjamin Kramer7320b992016-06-15 14:20:56 +00001914 void PrintIntegerAndValueDecl(const llvm::APSInt &Val, QualType IntType,
Richard Trieu9213ce52016-01-15 05:01:53 +00001915 Expr *IntExpr, bool DefaultInt, ValueDecl *VD,
1916 bool NeedAddressOf, bool IsNullPtr,
1917 Expr *VDExpr, bool DefaultDecl) {
1918 if (!PrintTree) {
1919 OS << (DefaultInt ? "(default) " : "");
1920 PrintAPSInt(Val, IntExpr, true /*Valid*/, IntType, false /*PrintType*/);
1921 } else {
1922 OS << (DefaultInt ? "[(default) " : "[");
1923 PrintAPSInt(Val, IntExpr, true /*Valid*/, IntType, false /*PrintType*/);
1924 OS << " != " << (DefaultDecl ? "(default) " : "");
1925 Bold();
1926 PrintValueDecl(VD, NeedAddressOf, VDExpr, IsNullPtr);
1927 Unbold();
1928 OS << ']';
1929 }
1930 }
1931
Richard Trieu91844232012-06-26 18:18:47 +00001932 // Prints the appropriate placeholder for elided template arguments.
1933 void PrintElideArgs(unsigned NumElideArgs, unsigned Indent) {
1934 if (PrintTree) {
1935 OS << '\n';
1936 for (unsigned i = 0; i < Indent; ++i)
1937 OS << " ";
1938 }
1939 if (NumElideArgs == 0) return;
1940 if (NumElideArgs == 1)
1941 OS << "[...]";
1942 else
1943 OS << "[" << NumElideArgs << " * ...]";
1944 }
1945
Richard Trieub7243852012-09-28 20:32:51 +00001946 // Prints and highlights differences in Qualifiers.
1947 void PrintQualifiers(Qualifiers FromQual, Qualifiers ToQual) {
1948 // Both types have no qualifiers
1949 if (FromQual.empty() && ToQual.empty())
1950 return;
1951
1952 // Both types have same qualifiers
1953 if (FromQual == ToQual) {
1954 PrintQualifier(FromQual, /*ApplyBold*/false);
1955 return;
1956 }
1957
1958 // Find common qualifiers and strip them from FromQual and ToQual.
1959 Qualifiers CommonQual = Qualifiers::removeCommonQualifiers(FromQual,
1960 ToQual);
1961
1962 // The qualifiers are printed before the template name.
1963 // Inline printing:
1964 // The common qualifiers are printed. Then, qualifiers only in this type
1965 // are printed and highlighted. Finally, qualifiers only in the other
1966 // type are printed and highlighted inside parentheses after "missing".
1967 // Tree printing:
1968 // Qualifiers are printed next to each other, inside brackets, and
1969 // separated by "!=". The printing order is:
1970 // common qualifiers, highlighted from qualifiers, "!=",
1971 // common qualifiers, highlighted to qualifiers
1972 if (PrintTree) {
1973 OS << "[";
1974 if (CommonQual.empty() && FromQual.empty()) {
1975 Bold();
1976 OS << "(no qualifiers) ";
1977 Unbold();
1978 } else {
1979 PrintQualifier(CommonQual, /*ApplyBold*/false);
1980 PrintQualifier(FromQual, /*ApplyBold*/true);
1981 }
1982 OS << "!= ";
1983 if (CommonQual.empty() && ToQual.empty()) {
1984 Bold();
1985 OS << "(no qualifiers)";
1986 Unbold();
1987 } else {
1988 PrintQualifier(CommonQual, /*ApplyBold*/false,
1989 /*appendSpaceIfNonEmpty*/!ToQual.empty());
1990 PrintQualifier(ToQual, /*ApplyBold*/true,
1991 /*appendSpaceIfNonEmpty*/false);
1992 }
1993 OS << "] ";
1994 } else {
1995 PrintQualifier(CommonQual, /*ApplyBold*/false);
1996 PrintQualifier(FromQual, /*ApplyBold*/true);
1997 }
1998 }
1999
2000 void PrintQualifier(Qualifiers Q, bool ApplyBold,
2001 bool AppendSpaceIfNonEmpty = true) {
2002 if (Q.empty()) return;
2003 if (ApplyBold) Bold();
2004 Q.print(OS, Policy, AppendSpaceIfNonEmpty);
2005 if (ApplyBold) Unbold();
2006 }
2007
Richard Trieu91844232012-06-26 18:18:47 +00002008public:
2009
Benjamin Kramer8de90462013-02-22 16:08:12 +00002010 TemplateDiff(raw_ostream &OS, ASTContext &Context, QualType FromType,
2011 QualType ToType, bool PrintTree, bool PrintFromType,
2012 bool ElideType, bool ShowColor)
Richard Trieu91844232012-06-26 18:18:47 +00002013 : Context(Context),
2014 Policy(Context.getLangOpts()),
2015 ElideType(ElideType),
2016 PrintTree(PrintTree),
2017 ShowColor(ShowColor),
2018 // When printing a single type, the FromType is the one printed.
Richard Trieu2331c8b2016-01-15 05:48:38 +00002019 FromTemplateType(PrintFromType ? FromType : ToType),
2020 ToTemplateType(PrintFromType ? ToType : FromType),
Benjamin Kramer8de90462013-02-22 16:08:12 +00002021 OS(OS),
Richard Trieu91844232012-06-26 18:18:47 +00002022 IsBold(false) {
2023 }
2024
2025 /// DiffTemplate - Start the template type diffing.
2026 void DiffTemplate() {
Richard Trieu2331c8b2016-01-15 05:48:38 +00002027 Qualifiers FromQual = FromTemplateType.getQualifiers(),
2028 ToQual = ToTemplateType.getQualifiers();
Richard Trieub7243852012-09-28 20:32:51 +00002029
Richard Trieu91844232012-06-26 18:18:47 +00002030 const TemplateSpecializationType *FromOrigTST =
Richard Trieu2331c8b2016-01-15 05:48:38 +00002031 GetTemplateSpecializationType(Context, FromTemplateType);
Richard Trieu91844232012-06-26 18:18:47 +00002032 const TemplateSpecializationType *ToOrigTST =
Richard Trieu2331c8b2016-01-15 05:48:38 +00002033 GetTemplateSpecializationType(Context, ToTemplateType);
Richard Trieu91844232012-06-26 18:18:47 +00002034
2035 // Only checking templates.
2036 if (!FromOrigTST || !ToOrigTST)
2037 return;
2038
2039 // Different base templates.
2040 if (!hasSameTemplate(FromOrigTST, ToOrigTST)) {
2041 return;
2042 }
2043
Richard Trieub7243852012-09-28 20:32:51 +00002044 FromQual -= QualType(FromOrigTST, 0).getQualifiers();
2045 ToQual -= QualType(ToOrigTST, 0).getQualifiers();
Richard Trieu91844232012-06-26 18:18:47 +00002046
2047 // Same base template, but different arguments.
Richard Trieu14714c42016-01-14 22:56:39 +00002048 Tree.SetTemplateDiff(FromOrigTST->getTemplateName().getAsTemplateDecl(),
2049 ToOrigTST->getTemplateName().getAsTemplateDecl(),
2050 FromQual, ToQual, false /*FromDefault*/,
2051 false /*ToDefault*/);
Richard Trieu91844232012-06-26 18:18:47 +00002052
2053 DiffTemplate(FromOrigTST, ToOrigTST);
David Blaikie47e45182012-06-26 18:52:09 +00002054 }
Richard Trieu91844232012-06-26 18:18:47 +00002055
Benjamin Kramer6582c362013-02-22 16:13:34 +00002056 /// Emit - When the two types given are templated types with the same
Richard Trieu91844232012-06-26 18:18:47 +00002057 /// base template, a string representation of the type difference will be
Benjamin Kramer6582c362013-02-22 16:13:34 +00002058 /// emitted to the stream and return true. Otherwise, return false.
Benjamin Kramer8de90462013-02-22 16:08:12 +00002059 bool Emit() {
Richard Trieu91844232012-06-26 18:18:47 +00002060 Tree.StartTraverse();
2061 if (Tree.Empty())
2062 return false;
2063
2064 TreeToString();
2065 assert(!IsBold && "Bold is applied to end of string.");
Richard Trieu91844232012-06-26 18:18:47 +00002066 return true;
2067 }
2068}; // end class TemplateDiff
Eugene Zelenko0a4f3f42016-02-10 19:11:58 +00002069} // end anonymous namespace
Richard Trieu91844232012-06-26 18:18:47 +00002070
2071/// FormatTemplateTypeDiff - A helper static function to start the template
2072/// diff and return the properly formatted string. Returns true if the diff
2073/// is successful.
2074static bool FormatTemplateTypeDiff(ASTContext &Context, QualType FromType,
2075 QualType ToType, bool PrintTree,
Fangrui Song6907ce22018-07-30 19:24:48 +00002076 bool PrintFromType, bool ElideType,
Benjamin Kramer8de90462013-02-22 16:08:12 +00002077 bool ShowColors, raw_ostream &OS) {
Richard Trieu91844232012-06-26 18:18:47 +00002078 if (PrintTree)
2079 PrintFromType = true;
Benjamin Kramer8de90462013-02-22 16:08:12 +00002080 TemplateDiff TD(OS, Context, FromType, ToType, PrintTree, PrintFromType,
Richard Trieu91844232012-06-26 18:18:47 +00002081 ElideType, ShowColors);
2082 TD.DiffTemplate();
Benjamin Kramer8de90462013-02-22 16:08:12 +00002083 return TD.Emit();
Richard Trieu91844232012-06-26 18:18:47 +00002084}