blob: d6de4548466c99682e6f3da7d14353bdcd4c09bd [file] [log] [blame]
Douglas Gregor639cccc2010-02-09 22:26:47 +00001//===--- ASTDiagnostic.cpp - Diagnostic Printing Hooks for AST Nodes ------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file implements a diagnostic formatting hook for AST elements.
11//
12//===----------------------------------------------------------------------===//
13#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"
Richard Trieu91844232012-06-26 18:18:47 +000022#include "llvm/ADT/SmallString.h"
Douglas Gregor639cccc2010-02-09 22:26:47 +000023#include "llvm/Support/raw_ostream.h"
24
25using namespace clang;
26
Chandler Carruthd102f2d2010-05-13 11:37:24 +000027// Returns a desugared version of the QualType, and marks ShouldAKA as true
28// whenever we remove significant sugar from the type.
29static QualType Desugar(ASTContext &Context, QualType QT, bool &ShouldAKA) {
30 QualifierCollector QC;
31
Douglas Gregor639cccc2010-02-09 22:26:47 +000032 while (true) {
Chandler Carruthd102f2d2010-05-13 11:37:24 +000033 const Type *Ty = QC.strip(QT);
34
Douglas Gregor639cccc2010-02-09 22:26:47 +000035 // Don't aka just because we saw an elaborated type...
Richard Smith30482bc2011-02-20 03:19:35 +000036 if (const ElaboratedType *ET = dyn_cast<ElaboratedType>(Ty)) {
37 QT = ET->desugar();
Douglas Gregor639cccc2010-02-09 22:26:47 +000038 continue;
39 }
Abramo Bagnara924a8f32010-12-10 16:29:40 +000040 // ... or a paren type ...
Richard Smith30482bc2011-02-20 03:19:35 +000041 if (const ParenType *PT = dyn_cast<ParenType>(Ty)) {
42 QT = PT->desugar();
Abramo Bagnara924a8f32010-12-10 16:29:40 +000043 continue;
44 }
Richard Smith30482bc2011-02-20 03:19:35 +000045 // ...or a substituted template type parameter ...
46 if (const SubstTemplateTypeParmType *ST =
47 dyn_cast<SubstTemplateTypeParmType>(Ty)) {
48 QT = ST->desugar();
49 continue;
50 }
John McCall4223a9e2011-03-04 04:00:19 +000051 // ...or an attributed type...
52 if (const AttributedType *AT = dyn_cast<AttributedType>(Ty)) {
53 QT = AT->desugar();
54 continue;
55 }
Reid Kleckner0503a872013-12-05 01:23:43 +000056 // ...or an adjusted type...
57 if (const AdjustedType *AT = dyn_cast<AdjustedType>(Ty)) {
58 QT = AT->desugar();
59 continue;
60 }
Richard Smith30482bc2011-02-20 03:19:35 +000061 // ... or an auto type.
62 if (const AutoType *AT = dyn_cast<AutoType>(Ty)) {
63 if (!AT->isSugared())
64 break;
65 QT = AT->desugar();
Douglas Gregor639cccc2010-02-09 22:26:47 +000066 continue;
67 }
Chandler Carruthd102f2d2010-05-13 11:37:24 +000068
Richard Smith3f1b5d02011-05-05 21:57:07 +000069 // Don't desugar template specializations, unless it's an alias template.
70 if (const TemplateSpecializationType *TST
71 = dyn_cast<TemplateSpecializationType>(Ty))
72 if (!TST->isTypeAlias())
73 break;
Chandler Carruthd102f2d2010-05-13 11:37:24 +000074
Douglas Gregor639cccc2010-02-09 22:26:47 +000075 // Don't desugar magic Objective-C types.
76 if (QualType(Ty,0) == Context.getObjCIdType() ||
77 QualType(Ty,0) == Context.getObjCClassType() ||
78 QualType(Ty,0) == Context.getObjCSelType() ||
79 QualType(Ty,0) == Context.getObjCProtoType())
80 break;
Chandler Carruthd102f2d2010-05-13 11:37:24 +000081
Douglas Gregor639cccc2010-02-09 22:26:47 +000082 // Don't desugar va_list.
83 if (QualType(Ty,0) == Context.getBuiltinVaListType())
84 break;
Chandler Carruthd102f2d2010-05-13 11:37:24 +000085
Douglas Gregor639cccc2010-02-09 22:26:47 +000086 // Otherwise, do a single-step desugar.
87 QualType Underlying;
88 bool IsSugar = false;
89 switch (Ty->getTypeClass()) {
90#define ABSTRACT_TYPE(Class, Base)
91#define TYPE(Class, Base) \
92case Type::Class: { \
93const Class##Type *CTy = cast<Class##Type>(Ty); \
94if (CTy->isSugared()) { \
95IsSugar = true; \
96Underlying = CTy->desugar(); \
97} \
98break; \
99}
100#include "clang/AST/TypeNodes.def"
101 }
Chandler Carruthd102f2d2010-05-13 11:37:24 +0000102
Douglas Gregor639cccc2010-02-09 22:26:47 +0000103 // If it wasn't sugared, we're done.
104 if (!IsSugar)
105 break;
Chandler Carruthd102f2d2010-05-13 11:37:24 +0000106
Douglas Gregor639cccc2010-02-09 22:26:47 +0000107 // If the desugared type is a vector type, we don't want to expand
108 // it, it will turn into an attribute mess. People want their "vec4".
109 if (isa<VectorType>(Underlying))
110 break;
Chandler Carruthd102f2d2010-05-13 11:37:24 +0000111
Douglas Gregor639cccc2010-02-09 22:26:47 +0000112 // Don't desugar through the primary typedef of an anonymous type.
Chris Lattneredbdff62010-09-04 23:16:01 +0000113 if (const TagType *UTT = Underlying->getAs<TagType>())
114 if (const TypedefType *QTT = dyn_cast<TypedefType>(QT))
Richard Smithdda56e42011-04-15 14:24:37 +0000115 if (UTT->getDecl()->getTypedefNameForAnonDecl() == QTT->getDecl())
Chris Lattneredbdff62010-09-04 23:16:01 +0000116 break;
Chandler Carruthd102f2d2010-05-13 11:37:24 +0000117
118 // Record that we actually looked through an opaque type here.
119 ShouldAKA = true;
Douglas Gregor639cccc2010-02-09 22:26:47 +0000120 QT = Underlying;
Douglas Gregor639cccc2010-02-09 22:26:47 +0000121 }
Chandler Carruthd102f2d2010-05-13 11:37:24 +0000122
123 // If we have a pointer-like type, desugar the pointee as well.
124 // FIXME: Handle other pointer-like types.
125 if (const PointerType *Ty = QT->getAs<PointerType>()) {
Chris Lattneredbdff62010-09-04 23:16:01 +0000126 QT = Context.getPointerType(Desugar(Context, Ty->getPointeeType(),
127 ShouldAKA));
Douglas Gregore9d95f12015-07-07 03:57:35 +0000128 } else if (const auto *Ty = QT->getAs<ObjCObjectPointerType>()) {
129 QT = Context.getObjCObjectPointerType(Desugar(Context, Ty->getPointeeType(),
130 ShouldAKA));
Chandler Carruthd102f2d2010-05-13 11:37:24 +0000131 } else if (const LValueReferenceType *Ty = QT->getAs<LValueReferenceType>()) {
Chris Lattneredbdff62010-09-04 23:16:01 +0000132 QT = Context.getLValueReferenceType(Desugar(Context, Ty->getPointeeType(),
133 ShouldAKA));
Douglas Gregor7a2a1162011-01-20 16:08:06 +0000134 } else if (const RValueReferenceType *Ty = QT->getAs<RValueReferenceType>()) {
135 QT = Context.getRValueReferenceType(Desugar(Context, Ty->getPointeeType(),
136 ShouldAKA));
Douglas Gregore9d95f12015-07-07 03:57:35 +0000137 } else if (const auto *Ty = QT->getAs<ObjCObjectType>()) {
138 if (Ty->getBaseType().getTypePtr() != Ty && !ShouldAKA) {
139 QualType BaseType = Desugar(Context, Ty->getBaseType(), ShouldAKA);
140 QT = Context.getObjCObjectType(BaseType, Ty->getTypeArgsAsWritten(),
141 llvm::makeArrayRef(Ty->qual_begin(),
142 Ty->getNumProtocols()));
143 }
Douglas Gregor639cccc2010-02-09 22:26:47 +0000144 }
Chandler Carruthd102f2d2010-05-13 11:37:24 +0000145
John McCall717d9b02010-12-10 11:01:00 +0000146 return QC.apply(Context, QT);
Douglas Gregor639cccc2010-02-09 22:26:47 +0000147}
148
149/// \brief Convert the given type to a string suitable for printing as part of
Chandler Carruthd102f2d2010-05-13 11:37:24 +0000150/// a diagnostic.
151///
Chandler Carruthd5173952011-07-11 17:49:21 +0000152/// There are four main criteria when determining whether we should have an
Chandler Carruthd102f2d2010-05-13 11:37:24 +0000153/// a.k.a. clause when pretty-printing a type:
154///
155/// 1) Some types provide very minimal sugar that doesn't impede the
156/// user's understanding --- for example, elaborated type
157/// specifiers. If this is all the sugar we see, we don't want an
158/// a.k.a. clause.
159/// 2) Some types are technically sugared but are much more familiar
160/// when seen in their sugared form --- for example, va_list,
161/// vector types, and the magic Objective C types. We don't
162/// want to desugar these, even if we do produce an a.k.a. clause.
163/// 3) Some types may have already been desugared previously in this diagnostic.
164/// if this is the case, doing another "aka" would just be clutter.
Chandler Carruthd5173952011-07-11 17:49:21 +0000165/// 4) Two different types within the same diagnostic have the same output
166/// string. In this case, force an a.k.a with the desugared type when
167/// doing so will provide additional information.
Douglas Gregor639cccc2010-02-09 22:26:47 +0000168///
169/// \param Context the context in which the type was allocated
170/// \param Ty the type to print
Chandler Carruthd5173952011-07-11 17:49:21 +0000171/// \param QualTypeVals pointer values to QualTypes which are used in the
172/// diagnostic message
Douglas Gregor639cccc2010-02-09 22:26:47 +0000173static std::string
174ConvertTypeToDiagnosticString(ASTContext &Context, QualType Ty,
Craig Toppere4753502014-06-12 05:32:27 +0000175 ArrayRef<DiagnosticsEngine::ArgumentValue> PrevArgs,
176 ArrayRef<intptr_t> QualTypeVals) {
Douglas Gregor639cccc2010-02-09 22:26:47 +0000177 // FIXME: Playing with std::string is really slow.
Chandler Carruthd5173952011-07-11 17:49:21 +0000178 bool ForceAKA = false;
179 QualType CanTy = Ty.getCanonicalType();
Douglas Gregorc0b07282011-09-27 22:38:19 +0000180 std::string S = Ty.getAsString(Context.getPrintingPolicy());
181 std::string CanS = CanTy.getAsString(Context.getPrintingPolicy());
Chandler Carruthd5173952011-07-11 17:49:21 +0000182
Bill Wendling8eb771d2012-02-22 09:51:33 +0000183 for (unsigned I = 0, E = QualTypeVals.size(); I != E; ++I) {
Chandler Carruthd5173952011-07-11 17:49:21 +0000184 QualType CompareTy =
Bill Wendling8eb771d2012-02-22 09:51:33 +0000185 QualType::getFromOpaquePtr(reinterpret_cast<void*>(QualTypeVals[I]));
Richard Smithbcc22fc2012-03-09 08:00:36 +0000186 if (CompareTy.isNull())
187 continue;
Chandler Carruthd5173952011-07-11 17:49:21 +0000188 if (CompareTy == Ty)
189 continue; // Same types
190 QualType CompareCanTy = CompareTy.getCanonicalType();
191 if (CompareCanTy == CanTy)
192 continue; // Same canonical types
Douglas Gregorc0b07282011-09-27 22:38:19 +0000193 std::string CompareS = CompareTy.getAsString(Context.getPrintingPolicy());
Richard Trieu5d1aff02011-11-14 19:39:25 +0000194 bool aka;
195 QualType CompareDesugar = Desugar(Context, CompareTy, aka);
196 std::string CompareDesugarStr =
197 CompareDesugar.getAsString(Context.getPrintingPolicy());
198 if (CompareS != S && CompareDesugarStr != S)
199 continue; // The type string is different than the comparison string
200 // and the desugared comparison string.
201 std::string CompareCanS =
202 CompareCanTy.getAsString(Context.getPrintingPolicy());
203
Chandler Carruthd5173952011-07-11 17:49:21 +0000204 if (CompareCanS == CanS)
205 continue; // No new info from canonical type
206
207 ForceAKA = true;
208 break;
209 }
Chandler Carruthd102f2d2010-05-13 11:37:24 +0000210
211 // Check to see if we already desugared this type in this
212 // diagnostic. If so, don't do it again.
213 bool Repeated = false;
Craig Toppere4753502014-06-12 05:32:27 +0000214 for (unsigned i = 0, e = PrevArgs.size(); i != e; ++i) {
Chandler Carruthd102f2d2010-05-13 11:37:24 +0000215 // TODO: Handle ak_declcontext case.
David Blaikie9c902b52011-09-25 23:23:43 +0000216 if (PrevArgs[i].first == DiagnosticsEngine::ak_qualtype) {
Chandler Carruthd102f2d2010-05-13 11:37:24 +0000217 void *Ptr = (void*)PrevArgs[i].second;
218 QualType PrevTy(QualType::getFromOpaquePtr(Ptr));
219 if (PrevTy == Ty) {
220 Repeated = true;
221 break;
222 }
223 }
224 }
225
Douglas Gregor639cccc2010-02-09 22:26:47 +0000226 // Consider producing an a.k.a. clause if removing all the direct
227 // sugar gives us something "significantly different".
Chandler Carruthd102f2d2010-05-13 11:37:24 +0000228 if (!Repeated) {
229 bool ShouldAKA = false;
230 QualType DesugaredTy = Desugar(Context, Ty, ShouldAKA);
Chandler Carruthd5173952011-07-11 17:49:21 +0000231 if (ShouldAKA || ForceAKA) {
232 if (DesugaredTy == Ty) {
233 DesugaredTy = Ty.getCanonicalType();
234 }
Douglas Gregorc0b07282011-09-27 22:38:19 +0000235 std::string akaStr = DesugaredTy.getAsString(Context.getPrintingPolicy());
Chandler Carruthd5173952011-07-11 17:49:21 +0000236 if (akaStr != S) {
237 S = "'" + S + "' (aka '" + akaStr + "')";
238 return S;
239 }
Chandler Carruthd102f2d2010-05-13 11:37:24 +0000240 }
Benjamin Kramer1adc8c32014-04-25 20:41:38 +0000241
242 // Give some additional info on vector types. These are either not desugared
243 // or displaying complex __attribute__ expressions so add details of the
244 // type and element count.
245 if (Ty->isVectorType()) {
246 const VectorType *VTy = Ty->getAs<VectorType>();
247 std::string DecoratedString;
248 llvm::raw_string_ostream OS(DecoratedString);
249 const char *Values = VTy->getNumElements() > 1 ? "values" : "value";
250 OS << "'" << S << "' (vector of " << VTy->getNumElements() << " '"
251 << VTy->getElementType().getAsString(Context.getPrintingPolicy())
252 << "' " << Values << ")";
253 return OS.str();
254 }
Douglas Gregor639cccc2010-02-09 22:26:47 +0000255 }
Chandler Carruthd102f2d2010-05-13 11:37:24 +0000256
Douglas Gregor639cccc2010-02-09 22:26:47 +0000257 S = "'" + S + "'";
258 return S;
259}
260
Richard Trieu91844232012-06-26 18:18:47 +0000261static bool FormatTemplateTypeDiff(ASTContext &Context, QualType FromType,
262 QualType ToType, bool PrintTree,
263 bool PrintFromType, bool ElideType,
Benjamin Kramer8de90462013-02-22 16:08:12 +0000264 bool ShowColors, raw_ostream &OS);
Richard Trieu91844232012-06-26 18:18:47 +0000265
Chandler Carruthd5173952011-07-11 17:49:21 +0000266void clang::FormatASTNodeDiagnosticArgument(
David Blaikie9c902b52011-09-25 23:23:43 +0000267 DiagnosticsEngine::ArgumentKind Kind,
Chandler Carruthd5173952011-07-11 17:49:21 +0000268 intptr_t Val,
Craig Topper3aa4fb32014-06-12 05:32:35 +0000269 StringRef Modifier,
270 StringRef Argument,
Craig Toppere4753502014-06-12 05:32:27 +0000271 ArrayRef<DiagnosticsEngine::ArgumentValue> PrevArgs,
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000272 SmallVectorImpl<char> &Output,
Chandler Carruthd5173952011-07-11 17:49:21 +0000273 void *Cookie,
Bill Wendling8eb771d2012-02-22 09:51:33 +0000274 ArrayRef<intptr_t> QualTypeVals) {
Douglas Gregor639cccc2010-02-09 22:26:47 +0000275 ASTContext &Context = *static_cast<ASTContext*>(Cookie);
276
Benjamin Kramer7192c9e2013-02-22 15:46:08 +0000277 size_t OldEnd = Output.size();
278 llvm::raw_svector_ostream OS(Output);
Douglas Gregor639cccc2010-02-09 22:26:47 +0000279 bool NeedQuotes = true;
280
281 switch (Kind) {
David Blaikie83d382b2011-09-23 05:06:16 +0000282 default: llvm_unreachable("unknown ArgumentKind");
Richard Trieu91844232012-06-26 18:18:47 +0000283 case DiagnosticsEngine::ak_qualtype_pair: {
Richard Trieu50f5f462012-07-10 01:46:04 +0000284 TemplateDiffTypes &TDT = *reinterpret_cast<TemplateDiffTypes*>(Val);
Richard Trieu91844232012-06-26 18:18:47 +0000285 QualType FromType =
286 QualType::getFromOpaquePtr(reinterpret_cast<void*>(TDT.FromType));
287 QualType ToType =
288 QualType::getFromOpaquePtr(reinterpret_cast<void*>(TDT.ToType));
289
290 if (FormatTemplateTypeDiff(Context, FromType, ToType, TDT.PrintTree,
291 TDT.PrintFromType, TDT.ElideType,
Benjamin Kramer8de90462013-02-22 16:08:12 +0000292 TDT.ShowColors, OS)) {
Richard Trieu91844232012-06-26 18:18:47 +0000293 NeedQuotes = !TDT.PrintTree;
Richard Trieu50f5f462012-07-10 01:46:04 +0000294 TDT.TemplateDiffUsed = true;
Richard Trieu91844232012-06-26 18:18:47 +0000295 break;
296 }
297
298 // Don't fall-back during tree printing. The caller will handle
299 // this case.
300 if (TDT.PrintTree)
301 return;
302
Benjamin Kramer7192c9e2013-02-22 15:46:08 +0000303 // Attempting to do a template diff on non-templates. Set the variables
Richard Trieu91844232012-06-26 18:18:47 +0000304 // and continue with regular type printing of the appropriate type.
305 Val = TDT.PrintFromType ? TDT.FromType : TDT.ToType;
Craig Topper3aa4fb32014-06-12 05:32:35 +0000306 Modifier = StringRef();
307 Argument = StringRef();
Richard Trieu91844232012-06-26 18:18:47 +0000308 // Fall through
309 }
David Blaikie9c902b52011-09-25 23:23:43 +0000310 case DiagnosticsEngine::ak_qualtype: {
Craig Topper3aa4fb32014-06-12 05:32:35 +0000311 assert(Modifier.empty() && Argument.empty() &&
Douglas Gregor639cccc2010-02-09 22:26:47 +0000312 "Invalid modifier for QualType argument");
313
314 QualType Ty(QualType::getFromOpaquePtr(reinterpret_cast<void*>(Val)));
Craig Toppere4753502014-06-12 05:32:27 +0000315 OS << ConvertTypeToDiagnosticString(Context, Ty, PrevArgs, QualTypeVals);
Douglas Gregor639cccc2010-02-09 22:26:47 +0000316 NeedQuotes = false;
317 break;
318 }
David Blaikie9c902b52011-09-25 23:23:43 +0000319 case DiagnosticsEngine::ak_declarationname: {
Craig Topper3aa4fb32014-06-12 05:32:35 +0000320 if (Modifier == "objcclass" && Argument.empty())
Benjamin Kramer7192c9e2013-02-22 15:46:08 +0000321 OS << '+';
Craig Topper3aa4fb32014-06-12 05:32:35 +0000322 else if (Modifier == "objcinstance" && Argument.empty())
Benjamin Kramer7192c9e2013-02-22 15:46:08 +0000323 OS << '-';
Douglas Gregor639cccc2010-02-09 22:26:47 +0000324 else
Craig Topper3aa4fb32014-06-12 05:32:35 +0000325 assert(Modifier.empty() && Argument.empty() &&
Douglas Gregor639cccc2010-02-09 22:26:47 +0000326 "Invalid modifier for DeclarationName argument");
Benjamin Kramer7192c9e2013-02-22 15:46:08 +0000327
David Blaikied4da8722013-05-14 21:04:00 +0000328 OS << DeclarationName::getFromOpaqueInteger(Val);
Douglas Gregor639cccc2010-02-09 22:26:47 +0000329 break;
330 }
David Blaikie9c902b52011-09-25 23:23:43 +0000331 case DiagnosticsEngine::ak_nameddecl: {
Douglas Gregor639cccc2010-02-09 22:26:47 +0000332 bool Qualified;
Craig Topper3aa4fb32014-06-12 05:32:35 +0000333 if (Modifier == "q" && Argument.empty())
Douglas Gregor639cccc2010-02-09 22:26:47 +0000334 Qualified = true;
335 else {
Craig Topper3aa4fb32014-06-12 05:32:35 +0000336 assert(Modifier.empty() && Argument.empty() &&
Douglas Gregor639cccc2010-02-09 22:26:47 +0000337 "Invalid modifier for NamedDecl* argument");
338 Qualified = false;
339 }
Chandler Carruthc841b6e2011-08-31 09:01:53 +0000340 const NamedDecl *ND = reinterpret_cast<const NamedDecl*>(Val);
Benjamin Kramer9170e912013-02-22 15:46:01 +0000341 ND->getNameForDiagnostic(OS, Context.getPrintingPolicy(), Qualified);
Douglas Gregor639cccc2010-02-09 22:26:47 +0000342 break;
343 }
David Blaikie9c902b52011-09-25 23:23:43 +0000344 case DiagnosticsEngine::ak_nestednamespec: {
Benjamin Kramer7192c9e2013-02-22 15:46:08 +0000345 NestedNameSpecifier *NNS = reinterpret_cast<NestedNameSpecifier*>(Val);
346 NNS->print(OS, Context.getPrintingPolicy());
Douglas Gregor639cccc2010-02-09 22:26:47 +0000347 NeedQuotes = false;
348 break;
349 }
David Blaikie9c902b52011-09-25 23:23:43 +0000350 case DiagnosticsEngine::ak_declcontext: {
Douglas Gregor639cccc2010-02-09 22:26:47 +0000351 DeclContext *DC = reinterpret_cast<DeclContext *> (Val);
352 assert(DC && "Should never have a null declaration context");
Alp Tokerfb8d02b2014-06-05 22:10:59 +0000353 NeedQuotes = false;
354
Richard Trieu3af6c102014-08-27 03:05:19 +0000355 // FIXME: Get the strings for DeclContext from some localized place
Douglas Gregor639cccc2010-02-09 22:26:47 +0000356 if (DC->isTranslationUnit()) {
David Blaikiebbafb8a2012-03-11 07:00:24 +0000357 if (Context.getLangOpts().CPlusPlus)
Benjamin Kramer7192c9e2013-02-22 15:46:08 +0000358 OS << "the global namespace";
Douglas Gregor639cccc2010-02-09 22:26:47 +0000359 else
Benjamin Kramer7192c9e2013-02-22 15:46:08 +0000360 OS << "the global scope";
Richard Trieu3af6c102014-08-27 03:05:19 +0000361 } else if (DC->isClosure()) {
362 OS << "block literal";
363 } else if (isLambdaCallOperator(DC)) {
364 OS << "lambda expression";
Douglas Gregor639cccc2010-02-09 22:26:47 +0000365 } else if (TypeDecl *Type = dyn_cast<TypeDecl>(DC)) {
Benjamin Kramer7192c9e2013-02-22 15:46:08 +0000366 OS << ConvertTypeToDiagnosticString(Context,
367 Context.getTypeDeclType(Type),
Craig Toppere4753502014-06-12 05:32:27 +0000368 PrevArgs, QualTypeVals);
Douglas Gregor639cccc2010-02-09 22:26:47 +0000369 } else {
Richard Trieu3af6c102014-08-27 03:05:19 +0000370 assert(isa<NamedDecl>(DC) && "Expected a NamedDecl");
Douglas Gregor639cccc2010-02-09 22:26:47 +0000371 NamedDecl *ND = cast<NamedDecl>(DC);
372 if (isa<NamespaceDecl>(ND))
Benjamin Kramer7192c9e2013-02-22 15:46:08 +0000373 OS << "namespace ";
Douglas Gregor639cccc2010-02-09 22:26:47 +0000374 else if (isa<ObjCMethodDecl>(ND))
Benjamin Kramer7192c9e2013-02-22 15:46:08 +0000375 OS << "method ";
Douglas Gregor639cccc2010-02-09 22:26:47 +0000376 else if (isa<FunctionDecl>(ND))
Benjamin Kramer7192c9e2013-02-22 15:46:08 +0000377 OS << "function ";
378
379 OS << '\'';
380 ND->getNameForDiagnostic(OS, Context.getPrintingPolicy(), true);
381 OS << '\'';
Douglas Gregor639cccc2010-02-09 22:26:47 +0000382 }
Douglas Gregor639cccc2010-02-09 22:26:47 +0000383 break;
384 }
David Majnemerb1004102014-03-02 18:46:05 +0000385 case DiagnosticsEngine::ak_attr: {
386 const Attr *At = reinterpret_cast<Attr *>(Val);
387 assert(At && "Received null Attr object!");
388 OS << '\'' << At->getSpelling() << '\'';
389 NeedQuotes = false;
390 break;
391 }
Aaron Ballman3e424b52013-12-26 18:30:57 +0000392
Douglas Gregor639cccc2010-02-09 22:26:47 +0000393 }
Benjamin Kramer7192c9e2013-02-22 15:46:08 +0000394
395 OS.flush();
396
397 if (NeedQuotes) {
398 Output.insert(Output.begin()+OldEnd, '\'');
Douglas Gregor639cccc2010-02-09 22:26:47 +0000399 Output.push_back('\'');
Benjamin Kramer7192c9e2013-02-22 15:46:08 +0000400 }
Douglas Gregor639cccc2010-02-09 22:26:47 +0000401}
Richard Trieu91844232012-06-26 18:18:47 +0000402
403/// TemplateDiff - A class that constructs a pretty string for a pair of
404/// QualTypes. For the pair of types, a diff tree will be created containing
405/// all the information about the templates and template arguments. Afterwards,
406/// the tree is transformed to a string according to the options passed in.
407namespace {
408class TemplateDiff {
409 /// Context - The ASTContext which is used for comparing template arguments.
410 ASTContext &Context;
411
412 /// Policy - Used during expression printing.
413 PrintingPolicy Policy;
414
415 /// ElideType - Option to elide identical types.
416 bool ElideType;
417
418 /// PrintTree - Format output string as a tree.
419 bool PrintTree;
420
421 /// ShowColor - Diagnostics support color, so bolding will be used.
422 bool ShowColor;
423
424 /// FromType - When single type printing is selected, this is the type to be
425 /// be printed. When tree printing is selected, this type will show up first
426 /// in the tree.
427 QualType FromType;
428
429 /// ToType - The type that FromType is compared to. Only in tree printing
430 /// will this type be outputed.
431 QualType ToType;
432
Richard Trieu91844232012-06-26 18:18:47 +0000433 /// OS - The stream used to construct the output strings.
Benjamin Kramer8de90462013-02-22 16:08:12 +0000434 raw_ostream &OS;
Richard Trieu91844232012-06-26 18:18:47 +0000435
436 /// IsBold - Keeps track of the bold formatting for the output string.
437 bool IsBold;
438
439 /// DiffTree - A tree representation the differences between two types.
440 class DiffTree {
Richard Trieub4cff112013-03-15 20:35:18 +0000441 public:
442 /// DiffKind - The difference in a DiffNode and which fields are used.
443 enum DiffKind {
444 /// Incomplete or invalid node.
445 Invalid,
446 /// Another level of templates, uses TemplateDecl and Qualifiers
447 Template,
448 /// Type difference, uses QualType
449 Type,
450 /// Expression difference, uses Expr
451 Expression,
452 /// Template argument difference, uses TemplateDecl
453 TemplateTemplate,
454 /// Integer difference, uses APSInt and Expr
455 Integer,
456 /// Declaration difference, uses ValueDecl
457 Declaration
458 };
459 private:
Richard Trieu91844232012-06-26 18:18:47 +0000460 /// DiffNode - The root node stores the original type. Each child node
461 /// stores template arguments of their parents. For templated types, the
462 /// template decl is also stored.
463 struct DiffNode {
Richard Trieub4cff112013-03-15 20:35:18 +0000464 DiffKind Kind;
465
Richard Trieu91844232012-06-26 18:18:47 +0000466 /// NextNode - The index of the next sibling node or 0.
467 unsigned NextNode;
468
469 /// ChildNode - The index of the first child node or 0.
470 unsigned ChildNode;
471
472 /// ParentNode - The index of the parent node.
473 unsigned ParentNode;
474
475 /// FromType, ToType - The type arguments.
476 QualType FromType, ToType;
477
478 /// FromExpr, ToExpr - The expression arguments.
479 Expr *FromExpr, *ToExpr;
480
Richard Trieu38800892014-07-24 04:24:50 +0000481 /// FromNullPtr, ToNullPtr - If the template argument is a nullptr
482 bool FromNullPtr, ToNullPtr;
483
Richard Trieu91844232012-06-26 18:18:47 +0000484 /// FromTD, ToTD - The template decl for template template
485 /// arguments or the type arguments that are templates.
486 TemplateDecl *FromTD, *ToTD;
487
Richard Trieub7243852012-09-28 20:32:51 +0000488 /// FromQual, ToQual - Qualifiers for template types.
489 Qualifiers FromQual, ToQual;
490
Richard Trieu6df89452012-11-01 21:29:28 +0000491 /// FromInt, ToInt - APSInt's for integral arguments.
492 llvm::APSInt FromInt, ToInt;
493
494 /// IsValidFromInt, IsValidToInt - Whether the APSInt's are valid.
495 bool IsValidFromInt, IsValidToInt;
496
Richard Trieu954aaaf2013-02-27 01:41:53 +0000497 /// FromValueDecl, ToValueDecl - Whether the argument is a decl.
498 ValueDecl *FromValueDecl, *ToValueDecl;
499
Richard Trieu091872c52013-05-07 21:36:24 +0000500 /// FromAddressOf, ToAddressOf - Whether the ValueDecl needs an address of
501 /// operator before it.
502 bool FromAddressOf, ToAddressOf;
503
Richard Trieu91844232012-06-26 18:18:47 +0000504 /// FromDefault, ToDefault - Whether the argument is a default argument.
505 bool FromDefault, ToDefault;
506
507 /// Same - Whether the two arguments evaluate to the same value.
508 bool Same;
509
510 DiffNode(unsigned ParentNode = 0)
Richard Trieub4cff112013-03-15 20:35:18 +0000511 : Kind(Invalid), NextNode(0), ChildNode(0), ParentNode(ParentNode),
Craig Topper36250ad2014-05-12 05:36:57 +0000512 FromType(), ToType(), FromExpr(nullptr), ToExpr(nullptr),
Richard Trieu38800892014-07-24 04:24:50 +0000513 FromNullPtr(false), ToNullPtr(false),
Craig Topper36250ad2014-05-12 05:36:57 +0000514 FromTD(nullptr), ToTD(nullptr), IsValidFromInt(false),
515 IsValidToInt(false), FromValueDecl(nullptr), ToValueDecl(nullptr),
516 FromAddressOf(false), ToAddressOf(false), FromDefault(false),
517 ToDefault(false), Same(false) {}
Richard Trieu91844232012-06-26 18:18:47 +0000518 };
519
520 /// FlatTree - A flattened tree used to store the DiffNodes.
Dmitri Gribenkof8579502013-01-12 19:30:44 +0000521 SmallVector<DiffNode, 16> FlatTree;
Richard Trieu91844232012-06-26 18:18:47 +0000522
523 /// CurrentNode - The index of the current node being used.
524 unsigned CurrentNode;
525
526 /// NextFreeNode - The index of the next unused node. Used when creating
527 /// child nodes.
528 unsigned NextFreeNode;
529
530 /// ReadNode - The index of the current node being read.
531 unsigned ReadNode;
532
533 public:
534 DiffTree() :
535 CurrentNode(0), NextFreeNode(1) {
536 FlatTree.push_back(DiffNode());
537 }
538
539 // Node writing functions.
540 /// SetNode - Sets FromTD and ToTD of the current node.
541 void SetNode(TemplateDecl *FromTD, TemplateDecl *ToTD) {
542 FlatTree[CurrentNode].FromTD = FromTD;
543 FlatTree[CurrentNode].ToTD = ToTD;
544 }
545
546 /// SetNode - Sets FromType and ToType of the current node.
547 void SetNode(QualType FromType, QualType ToType) {
548 FlatTree[CurrentNode].FromType = FromType;
549 FlatTree[CurrentNode].ToType = ToType;
550 }
551
552 /// SetNode - Set FromExpr and ToExpr of the current node.
553 void SetNode(Expr *FromExpr, Expr *ToExpr) {
554 FlatTree[CurrentNode].FromExpr = FromExpr;
555 FlatTree[CurrentNode].ToExpr = ToExpr;
556 }
557
Richard Trieu6df89452012-11-01 21:29:28 +0000558 /// SetNode - Set FromInt and ToInt of the current node.
559 void SetNode(llvm::APSInt FromInt, llvm::APSInt ToInt,
560 bool IsValidFromInt, bool IsValidToInt) {
561 FlatTree[CurrentNode].FromInt = FromInt;
562 FlatTree[CurrentNode].ToInt = ToInt;
563 FlatTree[CurrentNode].IsValidFromInt = IsValidFromInt;
564 FlatTree[CurrentNode].IsValidToInt = IsValidToInt;
565 }
566
Richard Trieub7243852012-09-28 20:32:51 +0000567 /// SetNode - Set FromQual and ToQual of the current node.
568 void SetNode(Qualifiers FromQual, Qualifiers ToQual) {
569 FlatTree[CurrentNode].FromQual = FromQual;
570 FlatTree[CurrentNode].ToQual = ToQual;
571 }
572
Richard Trieu954aaaf2013-02-27 01:41:53 +0000573 /// SetNode - Set FromValueDecl and ToValueDecl of the current node.
Richard Trieu091872c52013-05-07 21:36:24 +0000574 void SetNode(ValueDecl *FromValueDecl, ValueDecl *ToValueDecl,
575 bool FromAddressOf, bool ToAddressOf) {
Richard Trieu954aaaf2013-02-27 01:41:53 +0000576 FlatTree[CurrentNode].FromValueDecl = FromValueDecl;
577 FlatTree[CurrentNode].ToValueDecl = ToValueDecl;
Richard Trieu091872c52013-05-07 21:36:24 +0000578 FlatTree[CurrentNode].FromAddressOf = FromAddressOf;
579 FlatTree[CurrentNode].ToAddressOf = ToAddressOf;
Richard Trieu954aaaf2013-02-27 01:41:53 +0000580 }
581
Richard Trieu91844232012-06-26 18:18:47 +0000582 /// SetSame - Sets the same flag of the current node.
583 void SetSame(bool Same) {
584 FlatTree[CurrentNode].Same = Same;
585 }
586
Richard Trieu38800892014-07-24 04:24:50 +0000587 /// SetNullPtr - Sets the NullPtr flags of the current node.
588 void SetNullPtr(bool FromNullPtr, bool ToNullPtr) {
589 FlatTree[CurrentNode].FromNullPtr = FromNullPtr;
590 FlatTree[CurrentNode].ToNullPtr = ToNullPtr;
591 }
592
Richard Trieu91844232012-06-26 18:18:47 +0000593 /// SetDefault - Sets FromDefault and ToDefault flags of the current node.
594 void SetDefault(bool FromDefault, bool ToDefault) {
595 FlatTree[CurrentNode].FromDefault = FromDefault;
596 FlatTree[CurrentNode].ToDefault = ToDefault;
597 }
598
Richard Trieub4cff112013-03-15 20:35:18 +0000599 /// SetKind - Sets the current node's type.
600 void SetKind(DiffKind Kind) {
601 FlatTree[CurrentNode].Kind = Kind;
602 }
603
Richard Trieu91844232012-06-26 18:18:47 +0000604 /// Up - Changes the node to the parent of the current node.
605 void Up() {
606 CurrentNode = FlatTree[CurrentNode].ParentNode;
607 }
608
609 /// AddNode - Adds a child node to the current node, then sets that node
610 /// node as the current node.
611 void AddNode() {
612 FlatTree.push_back(DiffNode(CurrentNode));
613 DiffNode &Node = FlatTree[CurrentNode];
614 if (Node.ChildNode == 0) {
615 // If a child node doesn't exist, add one.
616 Node.ChildNode = NextFreeNode;
617 } else {
618 // If a child node exists, find the last child node and add a
619 // next node to it.
620 unsigned i;
621 for (i = Node.ChildNode; FlatTree[i].NextNode != 0;
622 i = FlatTree[i].NextNode) {
623 }
624 FlatTree[i].NextNode = NextFreeNode;
625 }
626 CurrentNode = NextFreeNode;
627 ++NextFreeNode;
628 }
629
630 // Node reading functions.
631 /// StartTraverse - Prepares the tree for recursive traversal.
632 void StartTraverse() {
633 ReadNode = 0;
634 CurrentNode = NextFreeNode;
635 NextFreeNode = 0;
636 }
637
638 /// Parent - Move the current read node to its parent.
639 void Parent() {
640 ReadNode = FlatTree[ReadNode].ParentNode;
641 }
642
Richard Trieu91844232012-06-26 18:18:47 +0000643 /// GetNode - Gets the FromType and ToType.
644 void GetNode(QualType &FromType, QualType &ToType) {
645 FromType = FlatTree[ReadNode].FromType;
646 ToType = FlatTree[ReadNode].ToType;
647 }
648
649 /// GetNode - Gets the FromExpr and ToExpr.
650 void GetNode(Expr *&FromExpr, Expr *&ToExpr) {
651 FromExpr = FlatTree[ReadNode].FromExpr;
652 ToExpr = FlatTree[ReadNode].ToExpr;
653 }
654
655 /// GetNode - Gets the FromTD and ToTD.
656 void GetNode(TemplateDecl *&FromTD, TemplateDecl *&ToTD) {
657 FromTD = FlatTree[ReadNode].FromTD;
658 ToTD = FlatTree[ReadNode].ToTD;
659 }
660
Richard Trieu6df89452012-11-01 21:29:28 +0000661 /// GetNode - Gets the FromInt and ToInt.
662 void GetNode(llvm::APSInt &FromInt, llvm::APSInt &ToInt,
663 bool &IsValidFromInt, bool &IsValidToInt) {
664 FromInt = FlatTree[ReadNode].FromInt;
665 ToInt = FlatTree[ReadNode].ToInt;
666 IsValidFromInt = FlatTree[ReadNode].IsValidFromInt;
667 IsValidToInt = FlatTree[ReadNode].IsValidToInt;
668 }
669
Richard Trieub7243852012-09-28 20:32:51 +0000670 /// GetNode - Gets the FromQual and ToQual.
671 void GetNode(Qualifiers &FromQual, Qualifiers &ToQual) {
672 FromQual = FlatTree[ReadNode].FromQual;
673 ToQual = FlatTree[ReadNode].ToQual;
674 }
675
Richard Trieu954aaaf2013-02-27 01:41:53 +0000676 /// GetNode - Gets the FromValueDecl and ToValueDecl.
Richard Trieu091872c52013-05-07 21:36:24 +0000677 void GetNode(ValueDecl *&FromValueDecl, ValueDecl *&ToValueDecl,
678 bool &FromAddressOf, bool &ToAddressOf) {
Richard Trieu954aaaf2013-02-27 01:41:53 +0000679 FromValueDecl = FlatTree[ReadNode].FromValueDecl;
680 ToValueDecl = FlatTree[ReadNode].ToValueDecl;
Richard Trieu091872c52013-05-07 21:36:24 +0000681 FromAddressOf = FlatTree[ReadNode].FromAddressOf;
682 ToAddressOf = FlatTree[ReadNode].ToAddressOf;
Richard Trieu954aaaf2013-02-27 01:41:53 +0000683 }
684
Richard Trieu91844232012-06-26 18:18:47 +0000685 /// NodeIsSame - Returns true the arguments are the same.
686 bool NodeIsSame() {
687 return FlatTree[ReadNode].Same;
688 }
689
690 /// HasChildrend - Returns true if the node has children.
691 bool HasChildren() {
692 return FlatTree[ReadNode].ChildNode != 0;
693 }
694
695 /// MoveToChild - Moves from the current node to its child.
696 void MoveToChild() {
697 ReadNode = FlatTree[ReadNode].ChildNode;
698 }
699
700 /// AdvanceSibling - If there is a next sibling, advance to it and return
701 /// true. Otherwise, return false.
702 bool AdvanceSibling() {
703 if (FlatTree[ReadNode].NextNode == 0)
704 return false;
705
706 ReadNode = FlatTree[ReadNode].NextNode;
707 return true;
708 }
709
710 /// HasNextSibling - Return true if the node has a next sibling.
711 bool HasNextSibling() {
712 return FlatTree[ReadNode].NextNode != 0;
713 }
714
Richard Trieu38800892014-07-24 04:24:50 +0000715 /// FromNullPtr - Returns true if the from argument is null.
716 bool FromNullPtr() {
717 return FlatTree[ReadNode].FromNullPtr;
718 }
719
720 /// ToNullPtr - Returns true if the to argument is null.
721 bool ToNullPtr() {
722 return FlatTree[ReadNode].ToNullPtr;
723 }
724
Richard Trieu91844232012-06-26 18:18:47 +0000725 /// FromDefault - Return true if the from argument is the default.
726 bool FromDefault() {
727 return FlatTree[ReadNode].FromDefault;
728 }
729
730 /// ToDefault - Return true if the to argument is the default.
731 bool ToDefault() {
732 return FlatTree[ReadNode].ToDefault;
733 }
734
735 /// Empty - Returns true if the tree has no information.
736 bool Empty() {
Richard Trieub4cff112013-03-15 20:35:18 +0000737 return GetKind() == Invalid;
738 }
739
740 /// GetKind - Returns the current node's type.
741 DiffKind GetKind() {
742 return FlatTree[ReadNode].Kind;
Richard Trieu91844232012-06-26 18:18:47 +0000743 }
744 };
745
746 DiffTree Tree;
747
748 /// TSTiterator - an iterator that is used to enter a
749 /// TemplateSpecializationType and read TemplateArguments inside template
750 /// parameter packs in order with the rest of the TemplateArguments.
751 struct TSTiterator {
752 typedef const TemplateArgument& reference;
753 typedef const TemplateArgument* pointer;
754
755 /// TST - the template specialization whose arguments this iterator
756 /// traverse over.
757 const TemplateSpecializationType *TST;
758
Richard Trieu64ab30392013-03-15 23:55:09 +0000759 /// DesugarTST - desugared template specialization used to extract
760 /// default argument information
761 const TemplateSpecializationType *DesugarTST;
762
Richard Trieu91844232012-06-26 18:18:47 +0000763 /// Index - the index of the template argument in TST.
764 unsigned Index;
765
766 /// CurrentTA - if CurrentTA is not the same as EndTA, then CurrentTA
767 /// points to a TemplateArgument within a parameter pack.
768 TemplateArgument::pack_iterator CurrentTA;
769
770 /// EndTA - the end iterator of a parameter pack
771 TemplateArgument::pack_iterator EndTA;
772
773 /// TSTiterator - Constructs an iterator and sets it to the first template
774 /// argument.
Richard Trieu64ab30392013-03-15 23:55:09 +0000775 TSTiterator(ASTContext &Context, const TemplateSpecializationType *TST)
776 : TST(TST),
777 DesugarTST(GetTemplateSpecializationType(Context, TST->desugar())),
Craig Topper36250ad2014-05-12 05:36:57 +0000778 Index(0), CurrentTA(nullptr), EndTA(nullptr) {
Richard Trieu91844232012-06-26 18:18:47 +0000779 if (isEnd()) return;
780
781 // Set to first template argument. If not a parameter pack, done.
782 TemplateArgument TA = TST->getArg(0);
783 if (TA.getKind() != TemplateArgument::Pack) return;
784
785 // Start looking into the parameter pack.
786 CurrentTA = TA.pack_begin();
787 EndTA = TA.pack_end();
788
789 // Found a valid template argument.
790 if (CurrentTA != EndTA) return;
791
792 // Parameter pack is empty, use the increment to get to a valid
793 // template argument.
794 ++(*this);
795 }
796
797 /// isEnd - Returns true if the iterator is one past the end.
798 bool isEnd() const {
Richard Trieu64ab30392013-03-15 23:55:09 +0000799 return Index >= TST->getNumArgs();
Richard Trieu91844232012-06-26 18:18:47 +0000800 }
801
802 /// &operator++ - Increment the iterator to the next template argument.
803 TSTiterator &operator++() {
Richard Trieu64ab30392013-03-15 23:55:09 +0000804 // After the end, Index should be the default argument position in
805 // DesugarTST, if it exists.
806 if (isEnd()) {
807 ++Index;
808 return *this;
809 }
Richard Trieu91844232012-06-26 18:18:47 +0000810
811 // If in a parameter pack, advance in the parameter pack.
812 if (CurrentTA != EndTA) {
813 ++CurrentTA;
814 if (CurrentTA != EndTA)
815 return *this;
816 }
817
818 // Loop until a template argument is found, or the end is reached.
819 while (true) {
820 // Advance to the next template argument. Break if reached the end.
821 if (++Index == TST->getNumArgs()) break;
822
823 // If the TemplateArgument is not a parameter pack, done.
824 TemplateArgument TA = TST->getArg(Index);
825 if (TA.getKind() != TemplateArgument::Pack) break;
826
827 // Handle parameter packs.
828 CurrentTA = TA.pack_begin();
829 EndTA = TA.pack_end();
830
831 // If the parameter pack is empty, try to advance again.
832 if (CurrentTA != EndTA) break;
833 }
834 return *this;
835 }
836
837 /// operator* - Returns the appropriate TemplateArgument.
838 reference operator*() const {
839 assert(!isEnd() && "Index exceeds number of arguments.");
840 if (CurrentTA == EndTA)
841 return TST->getArg(Index);
842 else
843 return *CurrentTA;
844 }
845
846 /// operator-> - Allow access to the underlying TemplateArgument.
847 pointer operator->() const {
848 return &operator*();
849 }
Richard Trieu64ab30392013-03-15 23:55:09 +0000850
851 /// getDesugar - Returns the deduced template argument from DesguarTST
852 reference getDesugar() const {
853 return DesugarTST->getArg(Index);
854 }
Richard Trieu91844232012-06-26 18:18:47 +0000855 };
856
857 // These functions build up the template diff tree, including functions to
858 // retrieve and compare template arguments.
859
860 static const TemplateSpecializationType * GetTemplateSpecializationType(
861 ASTContext &Context, QualType Ty) {
862 if (const TemplateSpecializationType *TST =
863 Ty->getAs<TemplateSpecializationType>())
864 return TST;
865
866 const RecordType *RT = Ty->getAs<RecordType>();
867
868 if (!RT)
Craig Topper36250ad2014-05-12 05:36:57 +0000869 return nullptr;
Richard Trieu91844232012-06-26 18:18:47 +0000870
871 const ClassTemplateSpecializationDecl *CTSD =
872 dyn_cast<ClassTemplateSpecializationDecl>(RT->getDecl());
873
874 if (!CTSD)
Craig Topper36250ad2014-05-12 05:36:57 +0000875 return nullptr;
Richard Trieu91844232012-06-26 18:18:47 +0000876
877 Ty = Context.getTemplateSpecializationType(
878 TemplateName(CTSD->getSpecializedTemplate()),
879 CTSD->getTemplateArgs().data(),
880 CTSD->getTemplateArgs().size(),
Richard Trieu3cee4132013-03-23 01:38:36 +0000881 Ty.getLocalUnqualifiedType().getCanonicalType());
Richard Trieu91844232012-06-26 18:18:47 +0000882
883 return Ty->getAs<TemplateSpecializationType>();
884 }
885
Richard Trieu9fee0b72014-08-27 06:24:47 +0000886 /// DiffTypes - Fills a DiffNode with information about a type difference.
887 void DiffTypes(const TSTiterator &FromIter, const TSTiterator &ToIter,
888 TemplateTypeParmDecl *FromDefaultTypeDecl,
889 TemplateTypeParmDecl *ToDefaultTypeDecl) {
890 QualType FromType = GetType(FromIter, FromDefaultTypeDecl);
891 QualType ToType = GetType(ToIter, ToDefaultTypeDecl);
892
893 Tree.SetNode(FromType, ToType);
894 Tree.SetDefault(FromIter.isEnd() && !FromType.isNull(),
895 ToIter.isEnd() && !ToType.isNull());
896 Tree.SetKind(DiffTree::Type);
897 if (FromType.isNull() || ToType.isNull())
898 return;
899
900 if (Context.hasSameType(FromType, ToType)) {
901 Tree.SetSame(true);
902 return;
903 }
904
905 const TemplateSpecializationType *FromArgTST =
906 GetTemplateSpecializationType(Context, FromType);
907 if (!FromArgTST)
908 return;
909
910 const TemplateSpecializationType *ToArgTST =
911 GetTemplateSpecializationType(Context, ToType);
912 if (!ToArgTST)
913 return;
914
915 if (!hasSameTemplate(FromArgTST, ToArgTST))
916 return;
917
918 Qualifiers FromQual = FromType.getQualifiers(),
919 ToQual = ToType.getQualifiers();
920 FromQual -= QualType(FromArgTST, 0).getQualifiers();
921 ToQual -= QualType(ToArgTST, 0).getQualifiers();
922 Tree.SetNode(FromArgTST->getTemplateName().getAsTemplateDecl(),
923 ToArgTST->getTemplateName().getAsTemplateDecl());
924 Tree.SetNode(FromQual, ToQual);
925 Tree.SetKind(DiffTree::Template);
926 DiffTemplate(FromArgTST, ToArgTST);
927 }
928
929 /// DiffTemplateTemplates - Fills a DiffNode with information about a
930 /// template template difference.
931 void DiffTemplateTemplates(const TSTiterator &FromIter,
932 const TSTiterator &ToIter,
933 TemplateTemplateParmDecl *FromDefaultTemplateDecl,
934 TemplateTemplateParmDecl *ToDefaultTemplateDecl) {
935 TemplateDecl *FromDecl = GetTemplateDecl(FromIter, FromDefaultTemplateDecl);
936 TemplateDecl *ToDecl = GetTemplateDecl(ToIter, ToDefaultTemplateDecl);
937 Tree.SetNode(FromDecl, ToDecl);
938 Tree.SetSame(FromDecl && ToDecl &&
939 FromDecl->getCanonicalDecl() == ToDecl->getCanonicalDecl());
940 Tree.SetDefault(FromIter.isEnd() && FromDecl, ToIter.isEnd() && ToDecl);
941 Tree.SetKind(DiffTree::TemplateTemplate);
942 }
943
944 /// InitializeNonTypeDiffVariables - Helper function for DiffNonTypes
945 static void InitializeNonTypeDiffVariables(
946 ASTContext &Context, const TSTiterator &Iter,
947 NonTypeTemplateParmDecl *Default, bool &HasInt, bool &HasValueDecl,
948 bool &IsNullPtr, Expr *&E, llvm::APSInt &Value, ValueDecl *&VD) {
949 HasInt = !Iter.isEnd() && Iter->getKind() == TemplateArgument::Integral;
950
951 HasValueDecl =
952 !Iter.isEnd() && Iter->getKind() == TemplateArgument::Declaration;
953
954 IsNullPtr = !Iter.isEnd() && Iter->getKind() == TemplateArgument::NullPtr;
955
956 if (HasInt)
957 Value = Iter->getAsIntegral();
958 else if (HasValueDecl)
959 VD = Iter->getAsDecl();
960 else if (!IsNullPtr)
961 E = GetExpr(Iter, Default);
962
963 if (E && Default->getType()->isPointerType())
964 IsNullPtr = CheckForNullPtr(Context, E);
965 }
966
967 /// NeedsAddressOf - Helper function for DiffNonTypes. Returns true if the
968 /// ValueDecl needs a '&' when printed.
969 static bool NeedsAddressOf(ValueDecl *VD, Expr *E,
970 NonTypeTemplateParmDecl *Default) {
971 if (!VD)
972 return false;
973
974 if (E) {
975 if (UnaryOperator *UO = dyn_cast<UnaryOperator>(E->IgnoreParens())) {
976 if (UO->getOpcode() == UO_AddrOf) {
977 return true;
978 }
979 }
980 return false;
981 }
982
983 if (!Default->getType()->isReferenceType()) {
984 return true;
985 }
986
987 return false;
988 }
989
990 /// DiffNonTypes - Handles any template parameters not handled by DiffTypes
991 /// of DiffTemplatesTemplates, such as integer and declaration parameters.
992 void DiffNonTypes(const TSTiterator &FromIter, const TSTiterator &ToIter,
993 NonTypeTemplateParmDecl *FromDefaultNonTypeDecl,
994 NonTypeTemplateParmDecl *ToDefaultNonTypeDecl) {
995 Expr *FromExpr = nullptr, *ToExpr = nullptr;
996 llvm::APSInt FromInt, ToInt;
997 ValueDecl *FromValueDecl = nullptr, *ToValueDecl = nullptr;
998 bool HasFromInt = false, HasToInt = false, HasFromValueDecl = false,
999 HasToValueDecl = false, FromNullPtr = false, ToNullPtr = false;
1000 InitializeNonTypeDiffVariables(Context, FromIter, FromDefaultNonTypeDecl,
1001 HasFromInt, HasFromValueDecl, FromNullPtr,
1002 FromExpr, FromInt, FromValueDecl);
1003 InitializeNonTypeDiffVariables(Context, ToIter, ToDefaultNonTypeDecl,
1004 HasToInt, HasToValueDecl, ToNullPtr,
1005 ToExpr, ToInt, ToValueDecl);
1006
1007 assert(((!HasFromInt && !HasToInt) ||
1008 (!HasFromValueDecl && !HasToValueDecl)) &&
1009 "Template argument cannot be both integer and declaration");
1010
Richard Trieu9fee0b72014-08-27 06:24:47 +00001011 if (!HasFromInt && !HasToInt && !HasFromValueDecl && !HasToValueDecl) {
1012 Tree.SetNode(FromExpr, ToExpr);
1013 Tree.SetDefault(FromIter.isEnd() && FromExpr, ToIter.isEnd() && ToExpr);
1014 if (FromDefaultNonTypeDecl->getType()->isIntegralOrEnumerationType()) {
1015 if (FromExpr)
Richard Trieu555c9672015-02-26 02:40:48 +00001016 HasFromInt = GetInt(Context, FromIter, FromExpr, FromInt,
1017 FromDefaultNonTypeDecl->getType());
Richard Trieu9fee0b72014-08-27 06:24:47 +00001018 if (ToExpr)
Richard Trieu555c9672015-02-26 02:40:48 +00001019 HasToInt = GetInt(Context, ToIter, ToExpr, ToInt,
1020 ToDefaultNonTypeDecl->getType());
Richard Trieu9fee0b72014-08-27 06:24:47 +00001021 }
1022 if (HasFromInt && HasToInt) {
1023 Tree.SetNode(FromInt, ToInt, HasFromInt, HasToInt);
Richard Trieu15b66532015-01-24 02:48:32 +00001024 Tree.SetSame(FromInt == ToInt);
Richard Trieu9fee0b72014-08-27 06:24:47 +00001025 Tree.SetKind(DiffTree::Integer);
1026 } else if (HasFromInt || HasToInt) {
1027 Tree.SetNode(FromInt, ToInt, HasFromInt, HasToInt);
1028 Tree.SetSame(false);
1029 Tree.SetKind(DiffTree::Integer);
1030 } else {
Richard Trieu15b66532015-01-24 02:48:32 +00001031 Tree.SetSame(IsEqualExpr(Context, FromExpr, ToExpr) ||
Richard Trieu9fee0b72014-08-27 06:24:47 +00001032 (FromNullPtr && ToNullPtr));
1033 Tree.SetNullPtr(FromNullPtr, ToNullPtr);
1034 Tree.SetKind(DiffTree::Expression);
1035 }
1036 return;
1037 }
1038
1039 if (HasFromInt || HasToInt) {
1040 if (!HasFromInt && FromExpr)
Richard Trieu555c9672015-02-26 02:40:48 +00001041 HasFromInt = GetInt(Context, FromIter, FromExpr, FromInt,
1042 FromDefaultNonTypeDecl->getType());
Richard Trieu9fee0b72014-08-27 06:24:47 +00001043 if (!HasToInt && ToExpr)
Richard Trieu555c9672015-02-26 02:40:48 +00001044 HasToInt = GetInt(Context, ToIter, ToExpr, ToInt,
1045 ToDefaultNonTypeDecl->getType());
Richard Trieu9fee0b72014-08-27 06:24:47 +00001046 Tree.SetNode(FromInt, ToInt, HasFromInt, HasToInt);
Richard Trieu15b66532015-01-24 02:48:32 +00001047 if (HasFromInt && HasToInt) {
1048 Tree.SetSame(FromInt == ToInt);
1049 } else {
1050 Tree.SetSame(false);
1051 }
Richard Trieu9fee0b72014-08-27 06:24:47 +00001052 Tree.SetDefault(FromIter.isEnd() && HasFromInt,
1053 ToIter.isEnd() && HasToInt);
1054 Tree.SetKind(DiffTree::Integer);
1055 return;
1056 }
1057
1058 if (!HasFromValueDecl && FromExpr)
1059 FromValueDecl = GetValueDecl(FromIter, FromExpr);
1060 if (!HasToValueDecl && ToExpr)
1061 ToValueDecl = GetValueDecl(ToIter, ToExpr);
1062
1063 bool FromAddressOf =
1064 NeedsAddressOf(FromValueDecl, FromExpr, FromDefaultNonTypeDecl);
1065 bool ToAddressOf =
1066 NeedsAddressOf(ToValueDecl, ToExpr, ToDefaultNonTypeDecl);
1067
1068 Tree.SetNullPtr(FromNullPtr, ToNullPtr);
1069 Tree.SetNode(FromValueDecl, ToValueDecl, FromAddressOf, ToAddressOf);
1070 Tree.SetSame(FromValueDecl && ToValueDecl &&
1071 FromValueDecl->getCanonicalDecl() ==
1072 ToValueDecl->getCanonicalDecl());
1073 Tree.SetDefault(FromIter.isEnd() && FromValueDecl,
1074 ToIter.isEnd() && ToValueDecl);
1075 Tree.SetKind(DiffTree::Declaration);
1076 }
1077
Richard Trieu91844232012-06-26 18:18:47 +00001078 /// DiffTemplate - recursively visits template arguments and stores the
1079 /// argument info into a tree.
1080 void DiffTemplate(const TemplateSpecializationType *FromTST,
1081 const TemplateSpecializationType *ToTST) {
1082 // Begin descent into diffing template tree.
Benjamin Kramer3b05e202013-10-08 16:58:52 +00001083 TemplateParameterList *ParamsFrom =
Richard Trieu91844232012-06-26 18:18:47 +00001084 FromTST->getTemplateName().getAsTemplateDecl()->getTemplateParameters();
Benjamin Kramer3b05e202013-10-08 16:58:52 +00001085 TemplateParameterList *ParamsTo =
1086 ToTST->getTemplateName().getAsTemplateDecl()->getTemplateParameters();
Richard Trieu91844232012-06-26 18:18:47 +00001087 unsigned TotalArgs = 0;
Richard Trieu64ab30392013-03-15 23:55:09 +00001088 for (TSTiterator FromIter(Context, FromTST), ToIter(Context, ToTST);
Richard Trieu91844232012-06-26 18:18:47 +00001089 !FromIter.isEnd() || !ToIter.isEnd(); ++TotalArgs) {
1090 Tree.AddNode();
1091
1092 // Get the parameter at index TotalArgs. If index is larger
1093 // than the total number of parameters, then there is an
1094 // argument pack, so re-use the last parameter.
Richard Trieu9fee0b72014-08-27 06:24:47 +00001095 unsigned FromParamIndex = std::min(TotalArgs, ParamsFrom->size() - 1);
1096 unsigned ToParamIndex = std::min(TotalArgs, ParamsTo->size() - 1);
1097 NamedDecl *FromParamND = ParamsFrom->getParam(FromParamIndex);
1098 NamedDecl *ToParamND = ParamsTo->getParam(ToParamIndex);
Benjamin Kramer3b05e202013-10-08 16:58:52 +00001099
Richard Trieu9fee0b72014-08-27 06:24:47 +00001100 TemplateTypeParmDecl *FromDefaultTypeDecl =
1101 dyn_cast<TemplateTypeParmDecl>(FromParamND);
1102 TemplateTypeParmDecl *ToDefaultTypeDecl =
1103 dyn_cast<TemplateTypeParmDecl>(ToParamND);
1104 if (FromDefaultTypeDecl && ToDefaultTypeDecl)
1105 DiffTypes(FromIter, ToIter, FromDefaultTypeDecl, ToDefaultTypeDecl);
Richard Trieu91844232012-06-26 18:18:47 +00001106
Richard Trieu9fee0b72014-08-27 06:24:47 +00001107 TemplateTemplateParmDecl *FromDefaultTemplateDecl =
1108 dyn_cast<TemplateTemplateParmDecl>(FromParamND);
1109 TemplateTemplateParmDecl *ToDefaultTemplateDecl =
1110 dyn_cast<TemplateTemplateParmDecl>(ToParamND);
1111 if (FromDefaultTemplateDecl && ToDefaultTemplateDecl)
1112 DiffTemplateTemplates(FromIter, ToIter, FromDefaultTemplateDecl,
1113 ToDefaultTemplateDecl);
Richard Trieu91844232012-06-26 18:18:47 +00001114
Richard Trieu9fee0b72014-08-27 06:24:47 +00001115 NonTypeTemplateParmDecl *FromDefaultNonTypeDecl =
1116 dyn_cast<NonTypeTemplateParmDecl>(FromParamND);
1117 NonTypeTemplateParmDecl *ToDefaultNonTypeDecl =
1118 dyn_cast<NonTypeTemplateParmDecl>(ToParamND);
1119 if (FromDefaultNonTypeDecl && ToDefaultNonTypeDecl)
1120 DiffNonTypes(FromIter, ToIter, FromDefaultNonTypeDecl,
1121 ToDefaultNonTypeDecl);
Richard Trieu91844232012-06-26 18:18:47 +00001122
Richard Trieu64ab30392013-03-15 23:55:09 +00001123 ++FromIter;
1124 ++ToIter;
Richard Trieu91844232012-06-26 18:18:47 +00001125 Tree.Up();
1126 }
1127 }
1128
Richard Trieu8e14cac2012-09-28 19:51:57 +00001129 /// makeTemplateList - Dump every template alias into the vector.
1130 static void makeTemplateList(
Craig Topper5603df42013-07-05 19:34:19 +00001131 SmallVectorImpl<const TemplateSpecializationType *> &TemplateList,
Richard Trieu8e14cac2012-09-28 19:51:57 +00001132 const TemplateSpecializationType *TST) {
1133 while (TST) {
1134 TemplateList.push_back(TST);
1135 if (!TST->isTypeAlias())
1136 return;
1137 TST = TST->getAliasedType()->getAs<TemplateSpecializationType>();
1138 }
1139 }
1140
1141 /// hasSameBaseTemplate - Returns true when the base templates are the same,
1142 /// even if the template arguments are not.
1143 static bool hasSameBaseTemplate(const TemplateSpecializationType *FromTST,
1144 const TemplateSpecializationType *ToTST) {
Douglas Gregor8e9f55f2013-01-31 01:08:35 +00001145 return FromTST->getTemplateName().getAsTemplateDecl()->getCanonicalDecl() ==
1146 ToTST->getTemplateName().getAsTemplateDecl()->getCanonicalDecl();
Richard Trieu8e14cac2012-09-28 19:51:57 +00001147 }
1148
Richard Trieu91844232012-06-26 18:18:47 +00001149 /// hasSameTemplate - Returns true if both types are specialized from the
1150 /// same template declaration. If they come from different template aliases,
1151 /// do a parallel ascension search to determine the highest template alias in
1152 /// common and set the arguments to them.
1153 static bool hasSameTemplate(const TemplateSpecializationType *&FromTST,
1154 const TemplateSpecializationType *&ToTST) {
1155 // Check the top templates if they are the same.
Richard Trieu8e14cac2012-09-28 19:51:57 +00001156 if (hasSameBaseTemplate(FromTST, ToTST))
Richard Trieu91844232012-06-26 18:18:47 +00001157 return true;
1158
1159 // Create vectors of template aliases.
1160 SmallVector<const TemplateSpecializationType*, 1> FromTemplateList,
1161 ToTemplateList;
1162
Richard Trieu8e14cac2012-09-28 19:51:57 +00001163 makeTemplateList(FromTemplateList, FromTST);
1164 makeTemplateList(ToTemplateList, ToTST);
Richard Trieu91844232012-06-26 18:18:47 +00001165
Craig Topper61ac9062013-07-08 03:55:09 +00001166 SmallVectorImpl<const TemplateSpecializationType *>::reverse_iterator
Richard Trieu91844232012-06-26 18:18:47 +00001167 FromIter = FromTemplateList.rbegin(), FromEnd = FromTemplateList.rend(),
1168 ToIter = ToTemplateList.rbegin(), ToEnd = ToTemplateList.rend();
1169
1170 // Check if the lowest template types are the same. If not, return.
Richard Trieu8e14cac2012-09-28 19:51:57 +00001171 if (!hasSameBaseTemplate(*FromIter, *ToIter))
Richard Trieu91844232012-06-26 18:18:47 +00001172 return false;
1173
1174 // Begin searching up the template aliases. The bottom most template
1175 // matches so move up until one pair does not match. Use the template
1176 // right before that one.
1177 for (; FromIter != FromEnd && ToIter != ToEnd; ++FromIter, ++ToIter) {
Richard Trieu8e14cac2012-09-28 19:51:57 +00001178 if (!hasSameBaseTemplate(*FromIter, *ToIter))
Richard Trieu91844232012-06-26 18:18:47 +00001179 break;
1180 }
1181
1182 FromTST = FromIter[-1];
1183 ToTST = ToIter[-1];
1184
1185 return true;
1186 }
1187
1188 /// GetType - Retrieves the template type arguments, including default
1189 /// arguments.
Richard Trieu88d786e2014-08-27 04:45:30 +00001190 static QualType GetType(const TSTiterator &Iter,
1191 TemplateTypeParmDecl *DefaultTTPD) {
Richard Trieu91844232012-06-26 18:18:47 +00001192 bool isVariadic = DefaultTTPD->isParameterPack();
1193
1194 if (!Iter.isEnd())
Richard Trieu17f13ec2013-04-03 03:06:48 +00001195 return Iter->getAsType();
Richard Trieu8ed6f2a2013-07-20 03:49:02 +00001196 if (isVariadic)
1197 return QualType();
Richard Trieu17f13ec2013-04-03 03:06:48 +00001198
Richard Trieu8ed6f2a2013-07-20 03:49:02 +00001199 QualType ArgType = DefaultTTPD->getDefaultArgument();
1200 if (ArgType->isDependentType())
1201 return Iter.getDesugar().getAsType();
1202
1203 return ArgType;
David Blaikie47e45182012-06-26 18:52:09 +00001204 }
Richard Trieu91844232012-06-26 18:18:47 +00001205
1206 /// GetExpr - Retrieves the template expression argument, including default
1207 /// arguments.
Richard Trieu88d786e2014-08-27 04:45:30 +00001208 static Expr *GetExpr(const TSTiterator &Iter,
1209 NonTypeTemplateParmDecl *DefaultNTTPD) {
Craig Topper36250ad2014-05-12 05:36:57 +00001210 Expr *ArgExpr = nullptr;
Richard Trieu91844232012-06-26 18:18:47 +00001211 bool isVariadic = DefaultNTTPD->isParameterPack();
1212
1213 if (!Iter.isEnd())
1214 ArgExpr = Iter->getAsExpr();
1215 else if (!isVariadic)
1216 ArgExpr = DefaultNTTPD->getDefaultArgument();
1217
1218 if (ArgExpr)
1219 while (SubstNonTypeTemplateParmExpr *SNTTPE =
1220 dyn_cast<SubstNonTypeTemplateParmExpr>(ArgExpr))
1221 ArgExpr = SNTTPE->getReplacement();
Richard Trieu17f13ec2013-04-03 03:06:48 +00001222
1223 return ArgExpr;
Richard Trieu91844232012-06-26 18:18:47 +00001224 }
1225
Richard Trieu981de5c2013-04-03 02:11:36 +00001226 /// GetInt - Retrieves the template integer argument, including evaluating
Richard Trieu555c9672015-02-26 02:40:48 +00001227 /// default arguments. If the value comes from an expression, extend the
1228 /// APSInt to size of IntegerType to match the behavior in
1229 /// Sema::CheckTemplateArgument
Richard Trieu88d786e2014-08-27 04:45:30 +00001230 static bool GetInt(ASTContext &Context, const TSTiterator &Iter,
Richard Trieu555c9672015-02-26 02:40:48 +00001231 Expr *ArgExpr, llvm::APSInt &Int, QualType IntegerType) {
Richard Trieu981de5c2013-04-03 02:11:36 +00001232 // Default, value-depenedent expressions require fetching
Nikola Smiljanic3fe1e092014-07-01 04:17:53 +00001233 // from the desugared TemplateArgument, otherwise expression needs to
1234 // be evaluatable.
1235 if (Iter.isEnd() && ArgExpr->isValueDependent()) {
Richard Trieu981de5c2013-04-03 02:11:36 +00001236 switch (Iter.getDesugar().getKind()) {
1237 case TemplateArgument::Integral:
Nikola Smiljanic3fe1e092014-07-01 04:17:53 +00001238 Int = Iter.getDesugar().getAsIntegral();
1239 return true;
Richard Trieu981de5c2013-04-03 02:11:36 +00001240 case TemplateArgument::Expression:
1241 ArgExpr = Iter.getDesugar().getAsExpr();
Nikola Smiljanic3fe1e092014-07-01 04:17:53 +00001242 Int = ArgExpr->EvaluateKnownConstInt(Context);
Richard Trieu555c9672015-02-26 02:40:48 +00001243 Int = Int.extOrTrunc(Context.getTypeSize(IntegerType));
Nikola Smiljanic3fe1e092014-07-01 04:17:53 +00001244 return true;
Richard Trieu981de5c2013-04-03 02:11:36 +00001245 default:
Craig Topperd8d43192014-06-18 05:13:13 +00001246 llvm_unreachable("Unexpected template argument kind");
Richard Trieu981de5c2013-04-03 02:11:36 +00001247 }
Nikola Smiljanic3fe1e092014-07-01 04:17:53 +00001248 } else if (ArgExpr->isEvaluatable(Context)) {
1249 Int = ArgExpr->EvaluateKnownConstInt(Context);
Richard Trieu555c9672015-02-26 02:40:48 +00001250 Int = Int.extOrTrunc(Context.getTypeSize(IntegerType));
Nikola Smiljanic3fe1e092014-07-01 04:17:53 +00001251 return true;
1252 }
1253
1254 return false;
Richard Trieu981de5c2013-04-03 02:11:36 +00001255 }
1256
Richard Trieu091872c52013-05-07 21:36:24 +00001257 /// GetValueDecl - Retrieves the template Decl argument, including
Richard Trieu981de5c2013-04-03 02:11:36 +00001258 /// default expression argument.
Richard Trieu88d786e2014-08-27 04:45:30 +00001259 static ValueDecl *GetValueDecl(const TSTiterator &Iter, Expr *ArgExpr) {
Richard Trieu981de5c2013-04-03 02:11:36 +00001260 // Default, value-depenedent expressions require fetching
1261 // from the desugared TemplateArgument
1262 if (Iter.isEnd() && ArgExpr->isValueDependent())
1263 switch (Iter.getDesugar().getKind()) {
1264 case TemplateArgument::Declaration:
1265 return Iter.getDesugar().getAsDecl();
1266 case TemplateArgument::Expression:
1267 ArgExpr = Iter.getDesugar().getAsExpr();
1268 return cast<DeclRefExpr>(ArgExpr)->getDecl();
1269 default:
Craig Topperd8d43192014-06-18 05:13:13 +00001270 llvm_unreachable("Unexpected template argument kind");
Richard Trieu981de5c2013-04-03 02:11:36 +00001271 }
Richard Trieu091872c52013-05-07 21:36:24 +00001272 DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(ArgExpr);
1273 if (!DRE) {
Richard Trieu38800892014-07-24 04:24:50 +00001274 UnaryOperator *UO = dyn_cast<UnaryOperator>(ArgExpr->IgnoreParens());
1275 if (!UO)
1276 return nullptr;
1277 DRE = cast<DeclRefExpr>(UO->getSubExpr());
Richard Trieu091872c52013-05-07 21:36:24 +00001278 }
1279
1280 return DRE->getDecl();
Richard Trieu981de5c2013-04-03 02:11:36 +00001281 }
1282
Richard Trieu38800892014-07-24 04:24:50 +00001283 /// CheckForNullPtr - returns true if the expression can be evaluated as
1284 /// a null pointer
Richard Trieu88d786e2014-08-27 04:45:30 +00001285 static bool CheckForNullPtr(ASTContext &Context, Expr *E) {
Richard Trieu38800892014-07-24 04:24:50 +00001286 assert(E && "Expected expression");
1287
1288 E = E->IgnoreParenCasts();
1289 if (E->isNullPointerConstant(Context, Expr::NPC_ValueDependentIsNull))
1290 return true;
1291
1292 DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(E);
1293 if (!DRE)
1294 return false;
1295
1296 VarDecl *VD = dyn_cast<VarDecl>(DRE->getDecl());
1297 if (!VD || !VD->hasInit())
1298 return false;
1299
1300 return VD->getInit()->IgnoreParenCasts()->isNullPointerConstant(
1301 Context, Expr::NPC_ValueDependentIsNull);
1302 }
1303
Richard Trieu91844232012-06-26 18:18:47 +00001304 /// GetTemplateDecl - Retrieves the template template arguments, including
1305 /// default arguments.
Richard Trieu88d786e2014-08-27 04:45:30 +00001306 static TemplateDecl *GetTemplateDecl(const TSTiterator &Iter,
Richard Trieu17f13ec2013-04-03 03:06:48 +00001307 TemplateTemplateParmDecl *DefaultTTPD) {
Richard Trieu91844232012-06-26 18:18:47 +00001308 bool isVariadic = DefaultTTPD->isParameterPack();
1309
1310 TemplateArgument TA = DefaultTTPD->getDefaultArgument().getArgument();
Craig Topper36250ad2014-05-12 05:36:57 +00001311 TemplateDecl *DefaultTD = nullptr;
Eli Friedmanb826a002012-09-26 02:36:12 +00001312 if (TA.getKind() != TemplateArgument::Null)
1313 DefaultTD = TA.getAsTemplate().getAsTemplateDecl();
Richard Trieu91844232012-06-26 18:18:47 +00001314
1315 if (!Iter.isEnd())
Richard Trieu17f13ec2013-04-03 03:06:48 +00001316 return Iter->getAsTemplate().getAsTemplateDecl();
1317 if (!isVariadic)
1318 return DefaultTD;
1319
Craig Topper36250ad2014-05-12 05:36:57 +00001320 return nullptr;
Richard Trieu91844232012-06-26 18:18:47 +00001321 }
1322
1323 /// IsEqualExpr - Returns true if the expressions evaluate to the same value.
Richard Trieu15b66532015-01-24 02:48:32 +00001324 static bool IsEqualExpr(ASTContext &Context, Expr *FromExpr, Expr *ToExpr) {
Richard Trieu91844232012-06-26 18:18:47 +00001325 if (FromExpr == ToExpr)
1326 return true;
1327
1328 if (!FromExpr || !ToExpr)
1329 return false;
1330
Richard Trieu97bacec2014-07-26 02:10:52 +00001331 DeclRefExpr *FromDRE = dyn_cast<DeclRefExpr>(FromExpr->IgnoreParens()),
1332 *ToDRE = dyn_cast<DeclRefExpr>(ToExpr->IgnoreParens());
Richard Trieu91844232012-06-26 18:18:47 +00001333
1334 if (FromDRE || ToDRE) {
1335 if (!FromDRE || !ToDRE)
1336 return false;
1337 return FromDRE->getDecl() == ToDRE->getDecl();
1338 }
1339
1340 Expr::EvalResult FromResult, ToResult;
1341 if (!FromExpr->EvaluateAsRValue(FromResult, Context) ||
Richard Trieu97bacec2014-07-26 02:10:52 +00001342 !ToExpr->EvaluateAsRValue(ToResult, Context)) {
1343 llvm::FoldingSetNodeID FromID, ToID;
1344 FromExpr->Profile(FromID, Context, true);
1345 ToExpr->Profile(ToID, Context, true);
1346 return FromID == ToID;
1347 }
Richard Trieu91844232012-06-26 18:18:47 +00001348
1349 APValue &FromVal = FromResult.Val;
1350 APValue &ToVal = ToResult.Val;
1351
1352 if (FromVal.getKind() != ToVal.getKind()) return false;
1353
1354 switch (FromVal.getKind()) {
1355 case APValue::Int:
Richard Trieu15b66532015-01-24 02:48:32 +00001356 return FromVal.getInt() == ToVal.getInt();
Richard Trieu91844232012-06-26 18:18:47 +00001357 case APValue::LValue: {
1358 APValue::LValueBase FromBase = FromVal.getLValueBase();
1359 APValue::LValueBase ToBase = ToVal.getLValueBase();
1360 if (FromBase.isNull() && ToBase.isNull())
1361 return true;
1362 if (FromBase.isNull() || ToBase.isNull())
1363 return false;
1364 return FromBase.get<const ValueDecl*>() ==
1365 ToBase.get<const ValueDecl*>();
1366 }
1367 case APValue::MemberPointer:
1368 return FromVal.getMemberPointerDecl() == ToVal.getMemberPointerDecl();
1369 default:
1370 llvm_unreachable("Unknown template argument expression.");
1371 }
1372 }
1373
1374 // These functions converts the tree representation of the template
1375 // differences into the internal character vector.
1376
1377 /// TreeToString - Converts the Tree object into a character stream which
1378 /// will later be turned into the output string.
1379 void TreeToString(int Indent = 1) {
1380 if (PrintTree) {
1381 OS << '\n';
Benjamin Kramer6582c362013-02-22 16:13:34 +00001382 OS.indent(2 * Indent);
Richard Trieu91844232012-06-26 18:18:47 +00001383 ++Indent;
1384 }
1385
1386 // Handle cases where the difference is not templates with different
1387 // arguments.
Richard Trieub4cff112013-03-15 20:35:18 +00001388 switch (Tree.GetKind()) {
Richard Trieub4cff112013-03-15 20:35:18 +00001389 case DiffTree::Invalid:
1390 llvm_unreachable("Template diffing failed with bad DiffNode");
1391 case DiffTree::Type: {
Richard Trieu91844232012-06-26 18:18:47 +00001392 QualType FromType, ToType;
1393 Tree.GetNode(FromType, ToType);
1394 PrintTypeNames(FromType, ToType, Tree.FromDefault(), Tree.ToDefault(),
1395 Tree.NodeIsSame());
1396 return;
1397 }
Richard Trieub4cff112013-03-15 20:35:18 +00001398 case DiffTree::Expression: {
Richard Trieu91844232012-06-26 18:18:47 +00001399 Expr *FromExpr, *ToExpr;
1400 Tree.GetNode(FromExpr, ToExpr);
Richard Trieu38800892014-07-24 04:24:50 +00001401 PrintExpr(FromExpr, ToExpr, Tree.FromNullPtr(), Tree.ToNullPtr(),
1402 Tree.FromDefault(), Tree.ToDefault(), Tree.NodeIsSame());
Richard Trieu91844232012-06-26 18:18:47 +00001403 return;
1404 }
Richard Trieub4cff112013-03-15 20:35:18 +00001405 case DiffTree::TemplateTemplate: {
Richard Trieu91844232012-06-26 18:18:47 +00001406 TemplateDecl *FromTD, *ToTD;
1407 Tree.GetNode(FromTD, ToTD);
1408 PrintTemplateTemplate(FromTD, ToTD, Tree.FromDefault(),
1409 Tree.ToDefault(), Tree.NodeIsSame());
1410 return;
1411 }
Richard Trieub4cff112013-03-15 20:35:18 +00001412 case DiffTree::Integer: {
Richard Trieu6df89452012-11-01 21:29:28 +00001413 llvm::APSInt FromInt, ToInt;
Richard Trieu64ab30392013-03-15 23:55:09 +00001414 Expr *FromExpr, *ToExpr;
Richard Trieu6df89452012-11-01 21:29:28 +00001415 bool IsValidFromInt, IsValidToInt;
Richard Trieu64ab30392013-03-15 23:55:09 +00001416 Tree.GetNode(FromExpr, ToExpr);
Richard Trieu6df89452012-11-01 21:29:28 +00001417 Tree.GetNode(FromInt, ToInt, IsValidFromInt, IsValidToInt);
1418 PrintAPSInt(FromInt, ToInt, IsValidFromInt, IsValidToInt,
Richard Trieu64ab30392013-03-15 23:55:09 +00001419 FromExpr, ToExpr, Tree.FromDefault(), Tree.ToDefault(),
1420 Tree.NodeIsSame());
Richard Trieu6df89452012-11-01 21:29:28 +00001421 return;
1422 }
Richard Trieub4cff112013-03-15 20:35:18 +00001423 case DiffTree::Declaration: {
Richard Trieu954aaaf2013-02-27 01:41:53 +00001424 ValueDecl *FromValueDecl, *ToValueDecl;
Richard Trieu091872c52013-05-07 21:36:24 +00001425 bool FromAddressOf, ToAddressOf;
1426 Tree.GetNode(FromValueDecl, ToValueDecl, FromAddressOf, ToAddressOf);
1427 PrintValueDecl(FromValueDecl, ToValueDecl, FromAddressOf, ToAddressOf,
Richard Trieu38800892014-07-24 04:24:50 +00001428 Tree.FromNullPtr(), Tree.ToNullPtr(), Tree.FromDefault(),
1429 Tree.ToDefault(), Tree.NodeIsSame());
Richard Trieu954aaaf2013-02-27 01:41:53 +00001430 return;
1431 }
Richard Trieub4cff112013-03-15 20:35:18 +00001432 case DiffTree::Template: {
1433 // Node is root of template. Recurse on children.
1434 TemplateDecl *FromTD, *ToTD;
1435 Tree.GetNode(FromTD, ToTD);
Richard Trieu954aaaf2013-02-27 01:41:53 +00001436
Richard Trieub4cff112013-03-15 20:35:18 +00001437 if (!Tree.HasChildren()) {
1438 // If we're dealing with a template specialization with zero
1439 // arguments, there are no children; special-case this.
1440 OS << FromTD->getNameAsString() << "<>";
1441 return;
Richard Trieu91844232012-06-26 18:18:47 +00001442 }
Richard Trieub4cff112013-03-15 20:35:18 +00001443
1444 Qualifiers FromQual, ToQual;
1445 Tree.GetNode(FromQual, ToQual);
1446 PrintQualifiers(FromQual, ToQual);
1447
1448 OS << FromTD->getNameAsString() << '<';
1449 Tree.MoveToChild();
1450 unsigned NumElideArgs = 0;
1451 do {
1452 if (ElideType) {
1453 if (Tree.NodeIsSame()) {
1454 ++NumElideArgs;
1455 continue;
1456 }
1457 if (NumElideArgs > 0) {
1458 PrintElideArgs(NumElideArgs, Indent);
1459 NumElideArgs = 0;
1460 OS << ", ";
1461 }
1462 }
1463 TreeToString(Indent);
1464 if (Tree.HasNextSibling())
1465 OS << ", ";
1466 } while (Tree.AdvanceSibling());
1467 if (NumElideArgs > 0)
Richard Trieu91844232012-06-26 18:18:47 +00001468 PrintElideArgs(NumElideArgs, Indent);
Richard Trieu91844232012-06-26 18:18:47 +00001469
Richard Trieub4cff112013-03-15 20:35:18 +00001470 Tree.Parent();
1471 OS << ">";
1472 return;
1473 }
1474 }
Richard Trieu91844232012-06-26 18:18:47 +00001475 }
1476
1477 // To signal to the text printer that a certain text needs to be bolded,
1478 // a special character is injected into the character stream which the
1479 // text printer will later strip out.
1480
1481 /// Bold - Start bolding text.
1482 void Bold() {
1483 assert(!IsBold && "Attempting to bold text that is already bold.");
1484 IsBold = true;
1485 if (ShowColor)
1486 OS << ToggleHighlight;
1487 }
1488
1489 /// Unbold - Stop bolding text.
1490 void Unbold() {
1491 assert(IsBold && "Attempting to remove bold from unbold text.");
1492 IsBold = false;
1493 if (ShowColor)
1494 OS << ToggleHighlight;
1495 }
1496
1497 // Functions to print out the arguments and highlighting the difference.
1498
1499 /// PrintTypeNames - prints the typenames, bolding differences. Will detect
1500 /// typenames that are the same and attempt to disambiguate them by using
1501 /// canonical typenames.
1502 void PrintTypeNames(QualType FromType, QualType ToType,
1503 bool FromDefault, bool ToDefault, bool Same) {
1504 assert((!FromType.isNull() || !ToType.isNull()) &&
1505 "Only one template argument may be missing.");
1506
1507 if (Same) {
Richard Trieud86c9012014-07-25 00:24:02 +00001508 OS << FromType.getAsString(Policy);
Richard Trieu91844232012-06-26 18:18:47 +00001509 return;
1510 }
1511
Richard Trieub7243852012-09-28 20:32:51 +00001512 if (!FromType.isNull() && !ToType.isNull() &&
1513 FromType.getLocalUnqualifiedType() ==
1514 ToType.getLocalUnqualifiedType()) {
1515 Qualifiers FromQual = FromType.getLocalQualifiers(),
Alp Tokeraced95a2013-11-26 02:52:41 +00001516 ToQual = ToType.getLocalQualifiers();
Richard Trieub7243852012-09-28 20:32:51 +00001517 PrintQualifiers(FromQual, ToQual);
1518 FromType.getLocalUnqualifiedType().print(OS, Policy);
1519 return;
1520 }
1521
Richard Trieu91844232012-06-26 18:18:47 +00001522 std::string FromTypeStr = FromType.isNull() ? "(no argument)"
Richard Trieud86c9012014-07-25 00:24:02 +00001523 : FromType.getAsString(Policy);
Richard Trieu91844232012-06-26 18:18:47 +00001524 std::string ToTypeStr = ToType.isNull() ? "(no argument)"
Richard Trieud86c9012014-07-25 00:24:02 +00001525 : ToType.getAsString(Policy);
Richard Trieu91844232012-06-26 18:18:47 +00001526 // Switch to canonical typename if it is better.
1527 // TODO: merge this with other aka printing above.
1528 if (FromTypeStr == ToTypeStr) {
Richard Trieud86c9012014-07-25 00:24:02 +00001529 std::string FromCanTypeStr =
1530 FromType.getCanonicalType().getAsString(Policy);
1531 std::string ToCanTypeStr = ToType.getCanonicalType().getAsString(Policy);
Richard Trieu91844232012-06-26 18:18:47 +00001532 if (FromCanTypeStr != ToCanTypeStr) {
1533 FromTypeStr = FromCanTypeStr;
1534 ToTypeStr = ToCanTypeStr;
1535 }
1536 }
1537
1538 if (PrintTree) OS << '[';
1539 OS << (FromDefault ? "(default) " : "");
1540 Bold();
1541 OS << FromTypeStr;
1542 Unbold();
1543 if (PrintTree) {
1544 OS << " != " << (ToDefault ? "(default) " : "");
1545 Bold();
1546 OS << ToTypeStr;
1547 Unbold();
1548 OS << "]";
1549 }
1550 return;
1551 }
1552
1553 /// PrintExpr - Prints out the expr template arguments, highlighting argument
1554 /// differences.
Richard Trieu38800892014-07-24 04:24:50 +00001555 void PrintExpr(const Expr *FromExpr, const Expr *ToExpr, bool FromNullPtr,
1556 bool ToNullPtr, bool FromDefault, bool ToDefault, bool Same) {
Richard Trieu91844232012-06-26 18:18:47 +00001557 assert((FromExpr || ToExpr) &&
1558 "Only one template argument may be missing.");
1559 if (Same) {
Richard Trieu38800892014-07-24 04:24:50 +00001560 PrintExpr(FromExpr, FromNullPtr);
Richard Trieu91844232012-06-26 18:18:47 +00001561 } else if (!PrintTree) {
1562 OS << (FromDefault ? "(default) " : "");
1563 Bold();
Richard Trieu38800892014-07-24 04:24:50 +00001564 PrintExpr(FromExpr, FromNullPtr);
Richard Trieu91844232012-06-26 18:18:47 +00001565 Unbold();
1566 } else {
1567 OS << (FromDefault ? "[(default) " : "[");
1568 Bold();
Richard Trieu38800892014-07-24 04:24:50 +00001569 PrintExpr(FromExpr, FromNullPtr);
Richard Trieu91844232012-06-26 18:18:47 +00001570 Unbold();
1571 OS << " != " << (ToDefault ? "(default) " : "");
1572 Bold();
Richard Trieu38800892014-07-24 04:24:50 +00001573 PrintExpr(ToExpr, ToNullPtr);
Richard Trieu91844232012-06-26 18:18:47 +00001574 Unbold();
1575 OS << ']';
1576 }
1577 }
1578
1579 /// PrintExpr - Actual formatting and printing of expressions.
Richard Trieu38800892014-07-24 04:24:50 +00001580 void PrintExpr(const Expr *E, bool NullPtr = false) {
1581 if (E) {
Craig Topper36250ad2014-05-12 05:36:57 +00001582 E->printPretty(OS, nullptr, Policy);
Richard Trieu38800892014-07-24 04:24:50 +00001583 return;
1584 }
1585 if (NullPtr) {
1586 OS << "nullptr";
1587 return;
1588 }
1589 OS << "(no argument)";
Richard Trieu91844232012-06-26 18:18:47 +00001590 }
1591
1592 /// PrintTemplateTemplate - Handles printing of template template arguments,
1593 /// highlighting argument differences.
1594 void PrintTemplateTemplate(TemplateDecl *FromTD, TemplateDecl *ToTD,
1595 bool FromDefault, bool ToDefault, bool Same) {
1596 assert((FromTD || ToTD) && "Only one template argument may be missing.");
Richard Trieue673d71a2013-01-31 02:47:46 +00001597
1598 std::string FromName = FromTD ? FromTD->getName() : "(no argument)";
1599 std::string ToName = ToTD ? ToTD->getName() : "(no argument)";
1600 if (FromTD && ToTD && FromName == ToName) {
1601 FromName = FromTD->getQualifiedNameAsString();
1602 ToName = ToTD->getQualifiedNameAsString();
1603 }
1604
Richard Trieu91844232012-06-26 18:18:47 +00001605 if (Same) {
1606 OS << "template " << FromTD->getNameAsString();
1607 } else if (!PrintTree) {
1608 OS << (FromDefault ? "(default) template " : "template ");
1609 Bold();
Richard Trieue673d71a2013-01-31 02:47:46 +00001610 OS << FromName;
Richard Trieu91844232012-06-26 18:18:47 +00001611 Unbold();
1612 } else {
1613 OS << (FromDefault ? "[(default) template " : "[template ");
1614 Bold();
Richard Trieue673d71a2013-01-31 02:47:46 +00001615 OS << FromName;
Richard Trieu91844232012-06-26 18:18:47 +00001616 Unbold();
1617 OS << " != " << (ToDefault ? "(default) template " : "template ");
1618 Bold();
Richard Trieue673d71a2013-01-31 02:47:46 +00001619 OS << ToName;
Richard Trieu91844232012-06-26 18:18:47 +00001620 Unbold();
1621 OS << ']';
1622 }
1623 }
1624
Richard Trieu6df89452012-11-01 21:29:28 +00001625 /// PrintAPSInt - Handles printing of integral arguments, highlighting
1626 /// argument differences.
1627 void PrintAPSInt(llvm::APSInt FromInt, llvm::APSInt ToInt,
Richard Trieu64ab30392013-03-15 23:55:09 +00001628 bool IsValidFromInt, bool IsValidToInt, Expr *FromExpr,
1629 Expr *ToExpr, bool FromDefault, bool ToDefault, bool Same) {
Richard Trieu6df89452012-11-01 21:29:28 +00001630 assert((IsValidFromInt || IsValidToInt) &&
1631 "Only one integral argument may be missing.");
1632
1633 if (Same) {
1634 OS << FromInt.toString(10);
1635 } else if (!PrintTree) {
1636 OS << (FromDefault ? "(default) " : "");
Richard Trieu64ab30392013-03-15 23:55:09 +00001637 PrintAPSInt(FromInt, FromExpr, IsValidFromInt);
Richard Trieu6df89452012-11-01 21:29:28 +00001638 } else {
1639 OS << (FromDefault ? "[(default) " : "[");
Richard Trieu64ab30392013-03-15 23:55:09 +00001640 PrintAPSInt(FromInt, FromExpr, IsValidFromInt);
Richard Trieu6df89452012-11-01 21:29:28 +00001641 OS << " != " << (ToDefault ? "(default) " : "");
Richard Trieu64ab30392013-03-15 23:55:09 +00001642 PrintAPSInt(ToInt, ToExpr, IsValidToInt);
Richard Trieu6df89452012-11-01 21:29:28 +00001643 OS << ']';
1644 }
1645 }
1646
Richard Trieu64ab30392013-03-15 23:55:09 +00001647 /// PrintAPSInt - If valid, print the APSInt. If the expression is
1648 /// gives more information, print it too.
1649 void PrintAPSInt(llvm::APSInt Val, Expr *E, bool Valid) {
1650 Bold();
1651 if (Valid) {
1652 if (HasExtraInfo(E)) {
1653 PrintExpr(E);
1654 Unbold();
1655 OS << " aka ";
1656 Bold();
1657 }
1658 OS << Val.toString(10);
Nikola Smiljanic3fe1e092014-07-01 04:17:53 +00001659 } else if (E) {
1660 PrintExpr(E);
Richard Trieu64ab30392013-03-15 23:55:09 +00001661 } else {
1662 OS << "(no argument)";
1663 }
1664 Unbold();
1665 }
Richard Trieu15b66532015-01-24 02:48:32 +00001666
Richard Trieu64ab30392013-03-15 23:55:09 +00001667 /// HasExtraInfo - Returns true if E is not an integer literal or the
1668 /// negation of an integer literal
1669 bool HasExtraInfo(Expr *E) {
1670 if (!E) return false;
Richard Trieu15b66532015-01-24 02:48:32 +00001671
1672 E = E->IgnoreImpCasts();
1673
Richard Trieu64ab30392013-03-15 23:55:09 +00001674 if (isa<IntegerLiteral>(E)) return false;
1675
1676 if (UnaryOperator *UO = dyn_cast<UnaryOperator>(E))
1677 if (UO->getOpcode() == UO_Minus)
1678 if (isa<IntegerLiteral>(UO->getSubExpr()))
1679 return false;
1680
1681 return true;
1682 }
1683
Richard Trieu38800892014-07-24 04:24:50 +00001684 void PrintValueDecl(ValueDecl *VD, bool AddressOf, bool NullPtr) {
1685 if (VD) {
1686 if (AddressOf)
1687 OS << "&";
1688 OS << VD->getName();
1689 return;
1690 }
1691
1692 if (NullPtr) {
1693 OS << "nullptr";
1694 return;
1695 }
1696
1697 OS << "(no argument)";
1698 }
1699
Richard Trieu954aaaf2013-02-27 01:41:53 +00001700 /// PrintDecl - Handles printing of Decl arguments, highlighting
1701 /// argument differences.
1702 void PrintValueDecl(ValueDecl *FromValueDecl, ValueDecl *ToValueDecl,
Richard Trieu38800892014-07-24 04:24:50 +00001703 bool FromAddressOf, bool ToAddressOf, bool FromNullPtr,
1704 bool ToNullPtr, bool FromDefault, bool ToDefault,
1705 bool Same) {
1706 assert((FromValueDecl || FromNullPtr || ToValueDecl || ToNullPtr) &&
Richard Trieu954aaaf2013-02-27 01:41:53 +00001707 "Only one Decl argument may be NULL");
1708
1709 if (Same) {
Richard Trieu38800892014-07-24 04:24:50 +00001710 PrintValueDecl(FromValueDecl, FromAddressOf, FromNullPtr);
Richard Trieu954aaaf2013-02-27 01:41:53 +00001711 } else if (!PrintTree) {
1712 OS << (FromDefault ? "(default) " : "");
1713 Bold();
Richard Trieu38800892014-07-24 04:24:50 +00001714 PrintValueDecl(FromValueDecl, FromAddressOf, FromNullPtr);
Richard Trieu954aaaf2013-02-27 01:41:53 +00001715 Unbold();
1716 } else {
1717 OS << (FromDefault ? "[(default) " : "[");
1718 Bold();
Richard Trieu38800892014-07-24 04:24:50 +00001719 PrintValueDecl(FromValueDecl, FromAddressOf, FromNullPtr);
Richard Trieu954aaaf2013-02-27 01:41:53 +00001720 Unbold();
1721 OS << " != " << (ToDefault ? "(default) " : "");
1722 Bold();
Richard Trieu38800892014-07-24 04:24:50 +00001723 PrintValueDecl(ToValueDecl, ToAddressOf, ToNullPtr);
Richard Trieu954aaaf2013-02-27 01:41:53 +00001724 Unbold();
1725 OS << ']';
1726 }
1727
1728 }
1729
Richard Trieu91844232012-06-26 18:18:47 +00001730 // Prints the appropriate placeholder for elided template arguments.
1731 void PrintElideArgs(unsigned NumElideArgs, unsigned Indent) {
1732 if (PrintTree) {
1733 OS << '\n';
1734 for (unsigned i = 0; i < Indent; ++i)
1735 OS << " ";
1736 }
1737 if (NumElideArgs == 0) return;
1738 if (NumElideArgs == 1)
1739 OS << "[...]";
1740 else
1741 OS << "[" << NumElideArgs << " * ...]";
1742 }
1743
Richard Trieub7243852012-09-28 20:32:51 +00001744 // Prints and highlights differences in Qualifiers.
1745 void PrintQualifiers(Qualifiers FromQual, Qualifiers ToQual) {
1746 // Both types have no qualifiers
1747 if (FromQual.empty() && ToQual.empty())
1748 return;
1749
1750 // Both types have same qualifiers
1751 if (FromQual == ToQual) {
1752 PrintQualifier(FromQual, /*ApplyBold*/false);
1753 return;
1754 }
1755
1756 // Find common qualifiers and strip them from FromQual and ToQual.
1757 Qualifiers CommonQual = Qualifiers::removeCommonQualifiers(FromQual,
1758 ToQual);
1759
1760 // The qualifiers are printed before the template name.
1761 // Inline printing:
1762 // The common qualifiers are printed. Then, qualifiers only in this type
1763 // are printed and highlighted. Finally, qualifiers only in the other
1764 // type are printed and highlighted inside parentheses after "missing".
1765 // Tree printing:
1766 // Qualifiers are printed next to each other, inside brackets, and
1767 // separated by "!=". The printing order is:
1768 // common qualifiers, highlighted from qualifiers, "!=",
1769 // common qualifiers, highlighted to qualifiers
1770 if (PrintTree) {
1771 OS << "[";
1772 if (CommonQual.empty() && FromQual.empty()) {
1773 Bold();
1774 OS << "(no qualifiers) ";
1775 Unbold();
1776 } else {
1777 PrintQualifier(CommonQual, /*ApplyBold*/false);
1778 PrintQualifier(FromQual, /*ApplyBold*/true);
1779 }
1780 OS << "!= ";
1781 if (CommonQual.empty() && ToQual.empty()) {
1782 Bold();
1783 OS << "(no qualifiers)";
1784 Unbold();
1785 } else {
1786 PrintQualifier(CommonQual, /*ApplyBold*/false,
1787 /*appendSpaceIfNonEmpty*/!ToQual.empty());
1788 PrintQualifier(ToQual, /*ApplyBold*/true,
1789 /*appendSpaceIfNonEmpty*/false);
1790 }
1791 OS << "] ";
1792 } else {
1793 PrintQualifier(CommonQual, /*ApplyBold*/false);
1794 PrintQualifier(FromQual, /*ApplyBold*/true);
1795 }
1796 }
1797
1798 void PrintQualifier(Qualifiers Q, bool ApplyBold,
1799 bool AppendSpaceIfNonEmpty = true) {
1800 if (Q.empty()) return;
1801 if (ApplyBold) Bold();
1802 Q.print(OS, Policy, AppendSpaceIfNonEmpty);
1803 if (ApplyBold) Unbold();
1804 }
1805
Richard Trieu91844232012-06-26 18:18:47 +00001806public:
1807
Benjamin Kramer8de90462013-02-22 16:08:12 +00001808 TemplateDiff(raw_ostream &OS, ASTContext &Context, QualType FromType,
1809 QualType ToType, bool PrintTree, bool PrintFromType,
1810 bool ElideType, bool ShowColor)
Richard Trieu91844232012-06-26 18:18:47 +00001811 : Context(Context),
1812 Policy(Context.getLangOpts()),
1813 ElideType(ElideType),
1814 PrintTree(PrintTree),
1815 ShowColor(ShowColor),
1816 // When printing a single type, the FromType is the one printed.
1817 FromType(PrintFromType ? FromType : ToType),
1818 ToType(PrintFromType ? ToType : FromType),
Benjamin Kramer8de90462013-02-22 16:08:12 +00001819 OS(OS),
Richard Trieu91844232012-06-26 18:18:47 +00001820 IsBold(false) {
1821 }
1822
1823 /// DiffTemplate - Start the template type diffing.
1824 void DiffTemplate() {
Richard Trieub7243852012-09-28 20:32:51 +00001825 Qualifiers FromQual = FromType.getQualifiers(),
1826 ToQual = ToType.getQualifiers();
1827
Richard Trieu91844232012-06-26 18:18:47 +00001828 const TemplateSpecializationType *FromOrigTST =
1829 GetTemplateSpecializationType(Context, FromType);
1830 const TemplateSpecializationType *ToOrigTST =
1831 GetTemplateSpecializationType(Context, ToType);
1832
1833 // Only checking templates.
1834 if (!FromOrigTST || !ToOrigTST)
1835 return;
1836
1837 // Different base templates.
1838 if (!hasSameTemplate(FromOrigTST, ToOrigTST)) {
1839 return;
1840 }
1841
Richard Trieub7243852012-09-28 20:32:51 +00001842 FromQual -= QualType(FromOrigTST, 0).getQualifiers();
1843 ToQual -= QualType(ToOrigTST, 0).getQualifiers();
Richard Trieu91844232012-06-26 18:18:47 +00001844 Tree.SetNode(FromType, ToType);
Richard Trieub7243852012-09-28 20:32:51 +00001845 Tree.SetNode(FromQual, ToQual);
Richard Trieub4cff112013-03-15 20:35:18 +00001846 Tree.SetKind(DiffTree::Template);
Richard Trieu91844232012-06-26 18:18:47 +00001847
1848 // Same base template, but different arguments.
1849 Tree.SetNode(FromOrigTST->getTemplateName().getAsTemplateDecl(),
1850 ToOrigTST->getTemplateName().getAsTemplateDecl());
1851
1852 DiffTemplate(FromOrigTST, ToOrigTST);
David Blaikie47e45182012-06-26 18:52:09 +00001853 }
Richard Trieu91844232012-06-26 18:18:47 +00001854
Benjamin Kramer6582c362013-02-22 16:13:34 +00001855 /// Emit - When the two types given are templated types with the same
Richard Trieu91844232012-06-26 18:18:47 +00001856 /// base template, a string representation of the type difference will be
Benjamin Kramer6582c362013-02-22 16:13:34 +00001857 /// emitted to the stream and return true. Otherwise, return false.
Benjamin Kramer8de90462013-02-22 16:08:12 +00001858 bool Emit() {
Richard Trieu91844232012-06-26 18:18:47 +00001859 Tree.StartTraverse();
1860 if (Tree.Empty())
1861 return false;
1862
1863 TreeToString();
1864 assert(!IsBold && "Bold is applied to end of string.");
Richard Trieu91844232012-06-26 18:18:47 +00001865 return true;
1866 }
1867}; // end class TemplateDiff
1868} // end namespace
1869
1870/// FormatTemplateTypeDiff - A helper static function to start the template
1871/// diff and return the properly formatted string. Returns true if the diff
1872/// is successful.
1873static bool FormatTemplateTypeDiff(ASTContext &Context, QualType FromType,
1874 QualType ToType, bool PrintTree,
1875 bool PrintFromType, bool ElideType,
Benjamin Kramer8de90462013-02-22 16:08:12 +00001876 bool ShowColors, raw_ostream &OS) {
Richard Trieu91844232012-06-26 18:18:47 +00001877 if (PrintTree)
1878 PrintFromType = true;
Benjamin Kramer8de90462013-02-22 16:08:12 +00001879 TemplateDiff TD(OS, Context, FromType, ToType, PrintTree, PrintFromType,
Richard Trieu91844232012-06-26 18:18:47 +00001880 ElideType, ShowColors);
1881 TD.DiffTemplate();
Benjamin Kramer8de90462013-02-22 16:08:12 +00001882 return TD.Emit();
Richard Trieu91844232012-06-26 18:18:47 +00001883}