ASTDiagnostic.cpp 78 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141
  1. //===--- ASTDiagnostic.cpp - Diagnostic Printing Hooks for AST Nodes ------===//
  2. //
  3. // 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
  6. //
  7. //===----------------------------------------------------------------------===//
  8. //
  9. // This file implements a diagnostic formatting hook for AST elements.
  10. //
  11. //===----------------------------------------------------------------------===//
  12. #include "clang/AST/ASTDiagnostic.h"
  13. #include "clang/AST/ASTContext.h"
  14. #include "clang/AST/ASTLambda.h"
  15. #include "clang/AST/Attr.h"
  16. #include "clang/AST/DeclObjC.h"
  17. #include "clang/AST/DeclTemplate.h"
  18. #include "clang/AST/ExprCXX.h"
  19. #include "clang/AST/TemplateBase.h"
  20. #include "clang/AST/Type.h"
  21. #include "llvm/ADT/StringExtras.h"
  22. #include "llvm/Support/raw_ostream.h"
  23. using namespace clang;
  24. // Returns a desugared version of the QualType, and marks ShouldAKA as true
  25. // whenever we remove significant sugar from the type.
  26. QualType clang::desugarForDiagnostic(ASTContext &Context, QualType QT,
  27. bool &ShouldAKA) {
  28. QualifierCollector QC;
  29. while (true) {
  30. const Type *Ty = QC.strip(QT);
  31. // Don't aka just because we saw an elaborated type...
  32. if (const ElaboratedType *ET = dyn_cast<ElaboratedType>(Ty)) {
  33. QT = ET->desugar();
  34. continue;
  35. }
  36. // ... or a using type ...
  37. if (const UsingType *UT = dyn_cast<UsingType>(Ty)) {
  38. QT = UT->desugar();
  39. continue;
  40. }
  41. // ... or a paren type ...
  42. if (const ParenType *PT = dyn_cast<ParenType>(Ty)) {
  43. QT = PT->desugar();
  44. continue;
  45. }
  46. // ... or a macro defined type ...
  47. if (const MacroQualifiedType *MDT = dyn_cast<MacroQualifiedType>(Ty)) {
  48. QT = MDT->desugar();
  49. continue;
  50. }
  51. // ...or a substituted template type parameter ...
  52. if (const SubstTemplateTypeParmType *ST =
  53. dyn_cast<SubstTemplateTypeParmType>(Ty)) {
  54. QT = ST->desugar();
  55. continue;
  56. }
  57. // ...or an attributed type...
  58. if (const AttributedType *AT = dyn_cast<AttributedType>(Ty)) {
  59. QT = AT->desugar();
  60. continue;
  61. }
  62. // ...or an adjusted type...
  63. if (const AdjustedType *AT = dyn_cast<AdjustedType>(Ty)) {
  64. QT = AT->desugar();
  65. continue;
  66. }
  67. // ... or an auto type.
  68. if (const AutoType *AT = dyn_cast<AutoType>(Ty)) {
  69. if (!AT->isSugared())
  70. break;
  71. QT = AT->desugar();
  72. continue;
  73. }
  74. // Desugar FunctionType if return type or any parameter type should be
  75. // desugared. Preserve nullability attribute on desugared types.
  76. if (const FunctionType *FT = dyn_cast<FunctionType>(Ty)) {
  77. bool DesugarReturn = false;
  78. QualType SugarRT = FT->getReturnType();
  79. QualType RT = desugarForDiagnostic(Context, SugarRT, DesugarReturn);
  80. if (auto nullability = AttributedType::stripOuterNullability(SugarRT)) {
  81. RT = Context.getAttributedType(
  82. AttributedType::getNullabilityAttrKind(*nullability), RT, RT);
  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 = desugarForDiagnostic(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. if (DesugarReturn || DesugarArgument) {
  99. ShouldAKA = true;
  100. QT = FPT ? Context.getFunctionType(RT, Args, FPT->getExtProtoInfo())
  101. : Context.getFunctionNoProtoType(RT, FT->getExtInfo());
  102. break;
  103. }
  104. }
  105. // Desugar template specializations if any template argument should be
  106. // desugared.
  107. if (const TemplateSpecializationType *TST =
  108. dyn_cast<TemplateSpecializationType>(Ty)) {
  109. if (!TST->isTypeAlias()) {
  110. bool DesugarArgument = false;
  111. SmallVector<TemplateArgument, 4> Args;
  112. for (const TemplateArgument &Arg : TST->template_arguments()) {
  113. if (Arg.getKind() == TemplateArgument::Type)
  114. Args.push_back(desugarForDiagnostic(Context, Arg.getAsType(),
  115. DesugarArgument));
  116. else
  117. Args.push_back(Arg);
  118. }
  119. if (DesugarArgument) {
  120. ShouldAKA = true;
  121. QT = Context.getTemplateSpecializationType(
  122. TST->getTemplateName(), Args, QT);
  123. }
  124. break;
  125. }
  126. }
  127. if (const auto *AT = dyn_cast<ArrayType>(Ty)) {
  128. QualType ElementTy =
  129. desugarForDiagnostic(Context, AT->getElementType(), ShouldAKA);
  130. if (const auto *CAT = dyn_cast<ConstantArrayType>(AT))
  131. QT = Context.getConstantArrayType(
  132. ElementTy, CAT->getSize(), CAT->getSizeExpr(),
  133. CAT->getSizeModifier(), CAT->getIndexTypeCVRQualifiers());
  134. else if (const auto *VAT = dyn_cast<VariableArrayType>(AT))
  135. QT = Context.getVariableArrayType(
  136. ElementTy, VAT->getSizeExpr(), VAT->getSizeModifier(),
  137. VAT->getIndexTypeCVRQualifiers(), VAT->getBracketsRange());
  138. else if (const auto *DSAT = dyn_cast<DependentSizedArrayType>(AT))
  139. QT = Context.getDependentSizedArrayType(
  140. ElementTy, DSAT->getSizeExpr(), DSAT->getSizeModifier(),
  141. DSAT->getIndexTypeCVRQualifiers(), DSAT->getBracketsRange());
  142. else if (const auto *IAT = dyn_cast<IncompleteArrayType>(AT))
  143. QT = Context.getIncompleteArrayType(ElementTy, IAT->getSizeModifier(),
  144. IAT->getIndexTypeCVRQualifiers());
  145. else
  146. llvm_unreachable("Unhandled array type");
  147. break;
  148. }
  149. // Don't desugar magic Objective-C types.
  150. if (QualType(Ty,0) == Context.getObjCIdType() ||
  151. QualType(Ty,0) == Context.getObjCClassType() ||
  152. QualType(Ty,0) == Context.getObjCSelType() ||
  153. QualType(Ty,0) == Context.getObjCProtoType())
  154. break;
  155. // Don't desugar va_list.
  156. if (QualType(Ty, 0) == Context.getBuiltinVaListType() ||
  157. QualType(Ty, 0) == Context.getBuiltinMSVaListType())
  158. break;
  159. // Otherwise, do a single-step desugar.
  160. QualType Underlying;
  161. bool IsSugar = false;
  162. switch (Ty->getTypeClass()) {
  163. #define ABSTRACT_TYPE(Class, Base)
  164. #define TYPE(Class, Base) \
  165. case Type::Class: { \
  166. const Class##Type *CTy = cast<Class##Type>(Ty); \
  167. if (CTy->isSugared()) { \
  168. IsSugar = true; \
  169. Underlying = CTy->desugar(); \
  170. } \
  171. break; \
  172. }
  173. #include "clang/AST/TypeNodes.inc"
  174. }
  175. // If it wasn't sugared, we're done.
  176. if (!IsSugar)
  177. break;
  178. // If the desugared type is a vector type, we don't want to expand
  179. // it, it will turn into an attribute mess. People want their "vec4".
  180. if (isa<VectorType>(Underlying))
  181. break;
  182. // Don't desugar through the primary typedef of an anonymous type.
  183. if (const TagType *UTT = Underlying->getAs<TagType>())
  184. if (const TypedefType *QTT = dyn_cast<TypedefType>(QT))
  185. if (UTT->getDecl()->getTypedefNameForAnonDecl() == QTT->getDecl())
  186. break;
  187. // Record that we actually looked through an opaque type here.
  188. ShouldAKA = true;
  189. QT = Underlying;
  190. }
  191. // If we have a pointer-like type, desugar the pointee as well.
  192. // FIXME: Handle other pointer-like types.
  193. if (const PointerType *Ty = QT->getAs<PointerType>()) {
  194. QT = Context.getPointerType(
  195. desugarForDiagnostic(Context, Ty->getPointeeType(), ShouldAKA));
  196. } else if (const auto *Ty = QT->getAs<ObjCObjectPointerType>()) {
  197. QT = Context.getObjCObjectPointerType(
  198. desugarForDiagnostic(Context, Ty->getPointeeType(), ShouldAKA));
  199. } else if (const LValueReferenceType *Ty = QT->getAs<LValueReferenceType>()) {
  200. QT = Context.getLValueReferenceType(
  201. desugarForDiagnostic(Context, Ty->getPointeeType(), ShouldAKA));
  202. } else if (const RValueReferenceType *Ty = QT->getAs<RValueReferenceType>()) {
  203. QT = Context.getRValueReferenceType(
  204. desugarForDiagnostic(Context, Ty->getPointeeType(), ShouldAKA));
  205. } else if (const auto *Ty = QT->getAs<ObjCObjectType>()) {
  206. if (Ty->getBaseType().getTypePtr() != Ty && !ShouldAKA) {
  207. QualType BaseType =
  208. desugarForDiagnostic(Context, Ty->getBaseType(), ShouldAKA);
  209. QT = Context.getObjCObjectType(
  210. BaseType, Ty->getTypeArgsAsWritten(),
  211. llvm::ArrayRef(Ty->qual_begin(), Ty->getNumProtocols()),
  212. Ty->isKindOfTypeAsWritten());
  213. }
  214. }
  215. return QC.apply(Context, QT);
  216. }
  217. /// Convert the given type to a string suitable for printing as part of
  218. /// a diagnostic.
  219. ///
  220. /// There are four main criteria when determining whether we should have an
  221. /// a.k.a. clause when pretty-printing a type:
  222. ///
  223. /// 1) Some types provide very minimal sugar that doesn't impede the
  224. /// user's understanding --- for example, elaborated type
  225. /// specifiers. If this is all the sugar we see, we don't want an
  226. /// a.k.a. clause.
  227. /// 2) Some types are technically sugared but are much more familiar
  228. /// when seen in their sugared form --- for example, va_list,
  229. /// vector types, and the magic Objective C types. We don't
  230. /// want to desugar these, even if we do produce an a.k.a. clause.
  231. /// 3) Some types may have already been desugared previously in this diagnostic.
  232. /// if this is the case, doing another "aka" would just be clutter.
  233. /// 4) Two different types within the same diagnostic have the same output
  234. /// string. In this case, force an a.k.a with the desugared type when
  235. /// doing so will provide additional information.
  236. ///
  237. /// \param Context the context in which the type was allocated
  238. /// \param Ty the type to print
  239. /// \param QualTypeVals pointer values to QualTypes which are used in the
  240. /// diagnostic message
  241. static std::string
  242. ConvertTypeToDiagnosticString(ASTContext &Context, QualType Ty,
  243. ArrayRef<DiagnosticsEngine::ArgumentValue> PrevArgs,
  244. ArrayRef<intptr_t> QualTypeVals) {
  245. // FIXME: Playing with std::string is really slow.
  246. bool ForceAKA = false;
  247. QualType CanTy = Ty.getCanonicalType();
  248. std::string S = Ty.getAsString(Context.getPrintingPolicy());
  249. std::string CanS = CanTy.getAsString(Context.getPrintingPolicy());
  250. for (const intptr_t &QualTypeVal : QualTypeVals) {
  251. QualType CompareTy =
  252. QualType::getFromOpaquePtr(reinterpret_cast<void *>(QualTypeVal));
  253. if (CompareTy.isNull())
  254. continue;
  255. if (CompareTy == Ty)
  256. continue; // Same types
  257. QualType CompareCanTy = CompareTy.getCanonicalType();
  258. if (CompareCanTy == CanTy)
  259. continue; // Same canonical types
  260. std::string CompareS = CompareTy.getAsString(Context.getPrintingPolicy());
  261. bool ShouldAKA = false;
  262. QualType CompareDesugar =
  263. desugarForDiagnostic(Context, CompareTy, ShouldAKA);
  264. std::string CompareDesugarStr =
  265. CompareDesugar.getAsString(Context.getPrintingPolicy());
  266. if (CompareS != S && CompareDesugarStr != S)
  267. continue; // The type string is different than the comparison string
  268. // and the desugared comparison string.
  269. std::string CompareCanS =
  270. CompareCanTy.getAsString(Context.getPrintingPolicy());
  271. if (CompareCanS == CanS)
  272. continue; // No new info from canonical type
  273. ForceAKA = true;
  274. break;
  275. }
  276. // Check to see if we already desugared this type in this
  277. // diagnostic. If so, don't do it again.
  278. bool Repeated = false;
  279. for (const auto &PrevArg : PrevArgs) {
  280. // TODO: Handle ak_declcontext case.
  281. if (PrevArg.first == DiagnosticsEngine::ak_qualtype) {
  282. QualType PrevTy(
  283. QualType::getFromOpaquePtr(reinterpret_cast<void *>(PrevArg.second)));
  284. if (PrevTy == Ty) {
  285. Repeated = true;
  286. break;
  287. }
  288. }
  289. }
  290. // Consider producing an a.k.a. clause if removing all the direct
  291. // sugar gives us something "significantly different".
  292. if (!Repeated) {
  293. bool ShouldAKA = false;
  294. QualType DesugaredTy = desugarForDiagnostic(Context, Ty, ShouldAKA);
  295. if (ShouldAKA || ForceAKA) {
  296. if (DesugaredTy == Ty) {
  297. DesugaredTy = Ty.getCanonicalType();
  298. }
  299. std::string akaStr = DesugaredTy.getAsString(Context.getPrintingPolicy());
  300. if (akaStr != S) {
  301. S = "'" + S + "' (aka '" + akaStr + "')";
  302. return S;
  303. }
  304. }
  305. // Give some additional info on vector types. These are either not desugared
  306. // or displaying complex __attribute__ expressions so add details of the
  307. // type and element count.
  308. if (const auto *VTy = Ty->getAs<VectorType>()) {
  309. std::string DecoratedString;
  310. llvm::raw_string_ostream OS(DecoratedString);
  311. const char *Values = VTy->getNumElements() > 1 ? "values" : "value";
  312. OS << "'" << S << "' (vector of " << VTy->getNumElements() << " '"
  313. << VTy->getElementType().getAsString(Context.getPrintingPolicy())
  314. << "' " << Values << ")";
  315. return DecoratedString;
  316. }
  317. }
  318. S = "'" + S + "'";
  319. return S;
  320. }
  321. static bool FormatTemplateTypeDiff(ASTContext &Context, QualType FromType,
  322. QualType ToType, bool PrintTree,
  323. bool PrintFromType, bool ElideType,
  324. bool ShowColors, raw_ostream &OS);
  325. void clang::FormatASTNodeDiagnosticArgument(
  326. DiagnosticsEngine::ArgumentKind Kind,
  327. intptr_t Val,
  328. StringRef Modifier,
  329. StringRef Argument,
  330. ArrayRef<DiagnosticsEngine::ArgumentValue> PrevArgs,
  331. SmallVectorImpl<char> &Output,
  332. void *Cookie,
  333. ArrayRef<intptr_t> QualTypeVals) {
  334. ASTContext &Context = *static_cast<ASTContext*>(Cookie);
  335. size_t OldEnd = Output.size();
  336. llvm::raw_svector_ostream OS(Output);
  337. bool NeedQuotes = true;
  338. switch (Kind) {
  339. default: llvm_unreachable("unknown ArgumentKind");
  340. case DiagnosticsEngine::ak_addrspace: {
  341. assert(Modifier.empty() && Argument.empty() &&
  342. "Invalid modifier for Qualifiers argument");
  343. auto S = Qualifiers::getAddrSpaceAsString(static_cast<LangAS>(Val));
  344. if (S.empty()) {
  345. OS << (Context.getLangOpts().OpenCL ? "default" : "generic");
  346. OS << " address space";
  347. } else {
  348. OS << "address space";
  349. OS << " '" << S << "'";
  350. }
  351. NeedQuotes = false;
  352. break;
  353. }
  354. case DiagnosticsEngine::ak_qual: {
  355. assert(Modifier.empty() && Argument.empty() &&
  356. "Invalid modifier for Qualifiers argument");
  357. Qualifiers Q(Qualifiers::fromOpaqueValue(Val));
  358. auto S = Q.getAsString();
  359. if (S.empty()) {
  360. OS << "unqualified";
  361. NeedQuotes = false;
  362. } else {
  363. OS << S;
  364. }
  365. break;
  366. }
  367. case DiagnosticsEngine::ak_qualtype_pair: {
  368. TemplateDiffTypes &TDT = *reinterpret_cast<TemplateDiffTypes*>(Val);
  369. QualType FromType =
  370. QualType::getFromOpaquePtr(reinterpret_cast<void*>(TDT.FromType));
  371. QualType ToType =
  372. QualType::getFromOpaquePtr(reinterpret_cast<void*>(TDT.ToType));
  373. if (FormatTemplateTypeDiff(Context, FromType, ToType, TDT.PrintTree,
  374. TDT.PrintFromType, TDT.ElideType,
  375. TDT.ShowColors, OS)) {
  376. NeedQuotes = !TDT.PrintTree;
  377. TDT.TemplateDiffUsed = true;
  378. break;
  379. }
  380. // Don't fall-back during tree printing. The caller will handle
  381. // this case.
  382. if (TDT.PrintTree)
  383. return;
  384. // Attempting to do a template diff on non-templates. Set the variables
  385. // and continue with regular type printing of the appropriate type.
  386. Val = TDT.PrintFromType ? TDT.FromType : TDT.ToType;
  387. Modifier = StringRef();
  388. Argument = StringRef();
  389. // Fall through
  390. [[fallthrough]];
  391. }
  392. case DiagnosticsEngine::ak_qualtype: {
  393. assert(Modifier.empty() && Argument.empty() &&
  394. "Invalid modifier for QualType argument");
  395. QualType Ty(QualType::getFromOpaquePtr(reinterpret_cast<void*>(Val)));
  396. OS << ConvertTypeToDiagnosticString(Context, Ty, PrevArgs, QualTypeVals);
  397. NeedQuotes = false;
  398. break;
  399. }
  400. case DiagnosticsEngine::ak_declarationname: {
  401. if (Modifier == "objcclass" && Argument.empty())
  402. OS << '+';
  403. else if (Modifier == "objcinstance" && Argument.empty())
  404. OS << '-';
  405. else
  406. assert(Modifier.empty() && Argument.empty() &&
  407. "Invalid modifier for DeclarationName argument");
  408. OS << DeclarationName::getFromOpaqueInteger(Val);
  409. break;
  410. }
  411. case DiagnosticsEngine::ak_nameddecl: {
  412. bool Qualified;
  413. if (Modifier == "q" && Argument.empty())
  414. Qualified = true;
  415. else {
  416. assert(Modifier.empty() && Argument.empty() &&
  417. "Invalid modifier for NamedDecl* argument");
  418. Qualified = false;
  419. }
  420. const NamedDecl *ND = reinterpret_cast<const NamedDecl*>(Val);
  421. ND->getNameForDiagnostic(OS, Context.getPrintingPolicy(), Qualified);
  422. break;
  423. }
  424. case DiagnosticsEngine::ak_nestednamespec: {
  425. NestedNameSpecifier *NNS = reinterpret_cast<NestedNameSpecifier*>(Val);
  426. NNS->print(OS, Context.getPrintingPolicy());
  427. NeedQuotes = false;
  428. break;
  429. }
  430. case DiagnosticsEngine::ak_declcontext: {
  431. DeclContext *DC = reinterpret_cast<DeclContext *> (Val);
  432. assert(DC && "Should never have a null declaration context");
  433. NeedQuotes = false;
  434. // FIXME: Get the strings for DeclContext from some localized place
  435. if (DC->isTranslationUnit()) {
  436. if (Context.getLangOpts().CPlusPlus)
  437. OS << "the global namespace";
  438. else
  439. OS << "the global scope";
  440. } else if (DC->isClosure()) {
  441. OS << "block literal";
  442. } else if (isLambdaCallOperator(DC)) {
  443. OS << "lambda expression";
  444. } else if (TypeDecl *Type = dyn_cast<TypeDecl>(DC)) {
  445. OS << ConvertTypeToDiagnosticString(Context,
  446. Context.getTypeDeclType(Type),
  447. PrevArgs, QualTypeVals);
  448. } else {
  449. assert(isa<NamedDecl>(DC) && "Expected a NamedDecl");
  450. NamedDecl *ND = cast<NamedDecl>(DC);
  451. if (isa<NamespaceDecl>(ND))
  452. OS << "namespace ";
  453. else if (isa<ObjCMethodDecl>(ND))
  454. OS << "method ";
  455. else if (isa<FunctionDecl>(ND))
  456. OS << "function ";
  457. OS << '\'';
  458. ND->getNameForDiagnostic(OS, Context.getPrintingPolicy(), true);
  459. OS << '\'';
  460. }
  461. break;
  462. }
  463. case DiagnosticsEngine::ak_attr: {
  464. const Attr *At = reinterpret_cast<Attr *>(Val);
  465. assert(At && "Received null Attr object!");
  466. OS << '\'' << At->getSpelling() << '\'';
  467. NeedQuotes = false;
  468. break;
  469. }
  470. }
  471. if (NeedQuotes) {
  472. Output.insert(Output.begin()+OldEnd, '\'');
  473. Output.push_back('\'');
  474. }
  475. }
  476. /// TemplateDiff - A class that constructs a pretty string for a pair of
  477. /// QualTypes. For the pair of types, a diff tree will be created containing
  478. /// all the information about the templates and template arguments. Afterwards,
  479. /// the tree is transformed to a string according to the options passed in.
  480. namespace {
  481. class TemplateDiff {
  482. /// Context - The ASTContext which is used for comparing template arguments.
  483. ASTContext &Context;
  484. /// Policy - Used during expression printing.
  485. PrintingPolicy Policy;
  486. /// ElideType - Option to elide identical types.
  487. bool ElideType;
  488. /// PrintTree - Format output string as a tree.
  489. bool PrintTree;
  490. /// ShowColor - Diagnostics support color, so bolding will be used.
  491. bool ShowColor;
  492. /// FromTemplateType - When single type printing is selected, this is the
  493. /// type to be printed. When tree printing is selected, this type will
  494. /// show up first in the tree.
  495. QualType FromTemplateType;
  496. /// ToTemplateType - The type that FromType is compared to. Only in tree
  497. /// printing will this type be outputed.
  498. QualType ToTemplateType;
  499. /// OS - The stream used to construct the output strings.
  500. raw_ostream &OS;
  501. /// IsBold - Keeps track of the bold formatting for the output string.
  502. bool IsBold;
  503. /// DiffTree - A tree representation the differences between two types.
  504. class DiffTree {
  505. public:
  506. /// DiffKind - The difference in a DiffNode. Fields of
  507. /// TemplateArgumentInfo needed by each difference can be found in the
  508. /// Set* and Get* functions.
  509. enum DiffKind {
  510. /// Incomplete or invalid node.
  511. Invalid,
  512. /// Another level of templates
  513. Template,
  514. /// Type difference, all type differences except those falling under
  515. /// the Template difference.
  516. Type,
  517. /// Expression difference, this is only when both arguments are
  518. /// expressions. If one argument is an expression and the other is
  519. /// Integer or Declaration, then use that diff type instead.
  520. Expression,
  521. /// Template argument difference
  522. TemplateTemplate,
  523. /// Integer difference
  524. Integer,
  525. /// Declaration difference, nullptr arguments are included here
  526. Declaration,
  527. /// One argument being integer and the other being declaration
  528. FromIntegerAndToDeclaration,
  529. FromDeclarationAndToInteger
  530. };
  531. private:
  532. /// TemplateArgumentInfo - All the information needed to pretty print
  533. /// a template argument. See the Set* and Get* functions to see which
  534. /// fields are used for each DiffKind.
  535. struct TemplateArgumentInfo {
  536. QualType ArgType;
  537. Qualifiers Qual;
  538. llvm::APSInt Val;
  539. bool IsValidInt = false;
  540. Expr *ArgExpr = nullptr;
  541. TemplateDecl *TD = nullptr;
  542. ValueDecl *VD = nullptr;
  543. bool NeedAddressOf = false;
  544. bool IsNullPtr = false;
  545. bool IsDefault = false;
  546. };
  547. /// DiffNode - The root node stores the original type. Each child node
  548. /// stores template arguments of their parents. For templated types, the
  549. /// template decl is also stored.
  550. struct DiffNode {
  551. DiffKind Kind = Invalid;
  552. /// NextNode - The index of the next sibling node or 0.
  553. unsigned NextNode = 0;
  554. /// ChildNode - The index of the first child node or 0.
  555. unsigned ChildNode = 0;
  556. /// ParentNode - The index of the parent node.
  557. unsigned ParentNode = 0;
  558. TemplateArgumentInfo FromArgInfo, ToArgInfo;
  559. /// Same - Whether the two arguments evaluate to the same value.
  560. bool Same = false;
  561. DiffNode(unsigned ParentNode = 0) : ParentNode(ParentNode) {}
  562. };
  563. /// FlatTree - A flattened tree used to store the DiffNodes.
  564. SmallVector<DiffNode, 16> FlatTree;
  565. /// CurrentNode - The index of the current node being used.
  566. unsigned CurrentNode;
  567. /// NextFreeNode - The index of the next unused node. Used when creating
  568. /// child nodes.
  569. unsigned NextFreeNode;
  570. /// ReadNode - The index of the current node being read.
  571. unsigned ReadNode;
  572. public:
  573. DiffTree() : CurrentNode(0), NextFreeNode(1), ReadNode(0) {
  574. FlatTree.push_back(DiffNode());
  575. }
  576. // Node writing functions, one for each valid DiffKind element.
  577. void SetTemplateDiff(TemplateDecl *FromTD, TemplateDecl *ToTD,
  578. Qualifiers FromQual, Qualifiers ToQual,
  579. bool FromDefault, bool ToDefault) {
  580. assert(FlatTree[CurrentNode].Kind == Invalid && "Node is not empty.");
  581. FlatTree[CurrentNode].Kind = Template;
  582. FlatTree[CurrentNode].FromArgInfo.TD = FromTD;
  583. FlatTree[CurrentNode].ToArgInfo.TD = ToTD;
  584. FlatTree[CurrentNode].FromArgInfo.Qual = FromQual;
  585. FlatTree[CurrentNode].ToArgInfo.Qual = ToQual;
  586. SetDefault(FromDefault, ToDefault);
  587. }
  588. void SetTypeDiff(QualType FromType, QualType ToType, bool FromDefault,
  589. bool ToDefault) {
  590. assert(FlatTree[CurrentNode].Kind == Invalid && "Node is not empty.");
  591. FlatTree[CurrentNode].Kind = Type;
  592. FlatTree[CurrentNode].FromArgInfo.ArgType = FromType;
  593. FlatTree[CurrentNode].ToArgInfo.ArgType = ToType;
  594. SetDefault(FromDefault, ToDefault);
  595. }
  596. void SetExpressionDiff(Expr *FromExpr, Expr *ToExpr, bool FromDefault,
  597. bool ToDefault) {
  598. assert(FlatTree[CurrentNode].Kind == Invalid && "Node is not empty.");
  599. FlatTree[CurrentNode].Kind = Expression;
  600. FlatTree[CurrentNode].FromArgInfo.ArgExpr = FromExpr;
  601. FlatTree[CurrentNode].ToArgInfo.ArgExpr = ToExpr;
  602. SetDefault(FromDefault, ToDefault);
  603. }
  604. void SetTemplateTemplateDiff(TemplateDecl *FromTD, TemplateDecl *ToTD,
  605. bool FromDefault, bool ToDefault) {
  606. assert(FlatTree[CurrentNode].Kind == Invalid && "Node is not empty.");
  607. FlatTree[CurrentNode].Kind = TemplateTemplate;
  608. FlatTree[CurrentNode].FromArgInfo.TD = FromTD;
  609. FlatTree[CurrentNode].ToArgInfo.TD = ToTD;
  610. SetDefault(FromDefault, ToDefault);
  611. }
  612. void SetIntegerDiff(const llvm::APSInt &FromInt, const llvm::APSInt &ToInt,
  613. bool IsValidFromInt, bool IsValidToInt,
  614. QualType FromIntType, QualType ToIntType,
  615. Expr *FromExpr, Expr *ToExpr, bool FromDefault,
  616. bool ToDefault) {
  617. assert(FlatTree[CurrentNode].Kind == Invalid && "Node is not empty.");
  618. FlatTree[CurrentNode].Kind = Integer;
  619. FlatTree[CurrentNode].FromArgInfo.Val = FromInt;
  620. FlatTree[CurrentNode].ToArgInfo.Val = ToInt;
  621. FlatTree[CurrentNode].FromArgInfo.IsValidInt = IsValidFromInt;
  622. FlatTree[CurrentNode].ToArgInfo.IsValidInt = IsValidToInt;
  623. FlatTree[CurrentNode].FromArgInfo.ArgType = FromIntType;
  624. FlatTree[CurrentNode].ToArgInfo.ArgType = ToIntType;
  625. FlatTree[CurrentNode].FromArgInfo.ArgExpr = FromExpr;
  626. FlatTree[CurrentNode].ToArgInfo.ArgExpr = ToExpr;
  627. SetDefault(FromDefault, ToDefault);
  628. }
  629. void SetDeclarationDiff(ValueDecl *FromValueDecl, ValueDecl *ToValueDecl,
  630. bool FromAddressOf, bool ToAddressOf,
  631. bool FromNullPtr, bool ToNullPtr, Expr *FromExpr,
  632. Expr *ToExpr, bool FromDefault, bool ToDefault) {
  633. assert(FlatTree[CurrentNode].Kind == Invalid && "Node is not empty.");
  634. FlatTree[CurrentNode].Kind = Declaration;
  635. FlatTree[CurrentNode].FromArgInfo.VD = FromValueDecl;
  636. FlatTree[CurrentNode].ToArgInfo.VD = ToValueDecl;
  637. FlatTree[CurrentNode].FromArgInfo.NeedAddressOf = FromAddressOf;
  638. FlatTree[CurrentNode].ToArgInfo.NeedAddressOf = ToAddressOf;
  639. FlatTree[CurrentNode].FromArgInfo.IsNullPtr = FromNullPtr;
  640. FlatTree[CurrentNode].ToArgInfo.IsNullPtr = ToNullPtr;
  641. FlatTree[CurrentNode].FromArgInfo.ArgExpr = FromExpr;
  642. FlatTree[CurrentNode].ToArgInfo.ArgExpr = ToExpr;
  643. SetDefault(FromDefault, ToDefault);
  644. }
  645. void SetFromDeclarationAndToIntegerDiff(
  646. ValueDecl *FromValueDecl, bool FromAddressOf, bool FromNullPtr,
  647. Expr *FromExpr, const llvm::APSInt &ToInt, bool IsValidToInt,
  648. QualType ToIntType, Expr *ToExpr, bool FromDefault, bool ToDefault) {
  649. assert(FlatTree[CurrentNode].Kind == Invalid && "Node is not empty.");
  650. FlatTree[CurrentNode].Kind = FromDeclarationAndToInteger;
  651. FlatTree[CurrentNode].FromArgInfo.VD = FromValueDecl;
  652. FlatTree[CurrentNode].FromArgInfo.NeedAddressOf = FromAddressOf;
  653. FlatTree[CurrentNode].FromArgInfo.IsNullPtr = FromNullPtr;
  654. FlatTree[CurrentNode].FromArgInfo.ArgExpr = FromExpr;
  655. FlatTree[CurrentNode].ToArgInfo.Val = ToInt;
  656. FlatTree[CurrentNode].ToArgInfo.IsValidInt = IsValidToInt;
  657. FlatTree[CurrentNode].ToArgInfo.ArgType = ToIntType;
  658. FlatTree[CurrentNode].ToArgInfo.ArgExpr = ToExpr;
  659. SetDefault(FromDefault, ToDefault);
  660. }
  661. void SetFromIntegerAndToDeclarationDiff(
  662. const llvm::APSInt &FromInt, bool IsValidFromInt, QualType FromIntType,
  663. Expr *FromExpr, ValueDecl *ToValueDecl, bool ToAddressOf,
  664. bool ToNullPtr, Expr *ToExpr, bool FromDefault, bool ToDefault) {
  665. assert(FlatTree[CurrentNode].Kind == Invalid && "Node is not empty.");
  666. FlatTree[CurrentNode].Kind = FromIntegerAndToDeclaration;
  667. FlatTree[CurrentNode].FromArgInfo.Val = FromInt;
  668. FlatTree[CurrentNode].FromArgInfo.IsValidInt = IsValidFromInt;
  669. FlatTree[CurrentNode].FromArgInfo.ArgType = FromIntType;
  670. FlatTree[CurrentNode].FromArgInfo.ArgExpr = FromExpr;
  671. FlatTree[CurrentNode].ToArgInfo.VD = ToValueDecl;
  672. FlatTree[CurrentNode].ToArgInfo.NeedAddressOf = ToAddressOf;
  673. FlatTree[CurrentNode].ToArgInfo.IsNullPtr = ToNullPtr;
  674. FlatTree[CurrentNode].ToArgInfo.ArgExpr = ToExpr;
  675. SetDefault(FromDefault, ToDefault);
  676. }
  677. /// SetDefault - Sets FromDefault and ToDefault flags of the current node.
  678. void SetDefault(bool FromDefault, bool ToDefault) {
  679. assert((!FromDefault || !ToDefault) && "Both arguments cannot be default.");
  680. FlatTree[CurrentNode].FromArgInfo.IsDefault = FromDefault;
  681. FlatTree[CurrentNode].ToArgInfo.IsDefault = ToDefault;
  682. }
  683. /// SetSame - Sets the same flag of the current node.
  684. void SetSame(bool Same) {
  685. FlatTree[CurrentNode].Same = Same;
  686. }
  687. /// SetKind - Sets the current node's type.
  688. void SetKind(DiffKind Kind) {
  689. FlatTree[CurrentNode].Kind = Kind;
  690. }
  691. /// Up - Changes the node to the parent of the current node.
  692. void Up() {
  693. assert(FlatTree[CurrentNode].Kind != Invalid &&
  694. "Cannot exit node before setting node information.");
  695. CurrentNode = FlatTree[CurrentNode].ParentNode;
  696. }
  697. /// AddNode - Adds a child node to the current node, then sets that node
  698. /// node as the current node.
  699. void AddNode() {
  700. assert(FlatTree[CurrentNode].Kind == Template &&
  701. "Only Template nodes can have children nodes.");
  702. FlatTree.push_back(DiffNode(CurrentNode));
  703. DiffNode &Node = FlatTree[CurrentNode];
  704. if (Node.ChildNode == 0) {
  705. // If a child node doesn't exist, add one.
  706. Node.ChildNode = NextFreeNode;
  707. } else {
  708. // If a child node exists, find the last child node and add a
  709. // next node to it.
  710. unsigned i;
  711. for (i = Node.ChildNode; FlatTree[i].NextNode != 0;
  712. i = FlatTree[i].NextNode) {
  713. }
  714. FlatTree[i].NextNode = NextFreeNode;
  715. }
  716. CurrentNode = NextFreeNode;
  717. ++NextFreeNode;
  718. }
  719. // Node reading functions.
  720. /// StartTraverse - Prepares the tree for recursive traversal.
  721. void StartTraverse() {
  722. ReadNode = 0;
  723. CurrentNode = NextFreeNode;
  724. NextFreeNode = 0;
  725. }
  726. /// Parent - Move the current read node to its parent.
  727. void Parent() {
  728. ReadNode = FlatTree[ReadNode].ParentNode;
  729. }
  730. void GetTemplateDiff(TemplateDecl *&FromTD, TemplateDecl *&ToTD,
  731. Qualifiers &FromQual, Qualifiers &ToQual) {
  732. assert(FlatTree[ReadNode].Kind == Template && "Unexpected kind.");
  733. FromTD = FlatTree[ReadNode].FromArgInfo.TD;
  734. ToTD = FlatTree[ReadNode].ToArgInfo.TD;
  735. FromQual = FlatTree[ReadNode].FromArgInfo.Qual;
  736. ToQual = FlatTree[ReadNode].ToArgInfo.Qual;
  737. }
  738. void GetTypeDiff(QualType &FromType, QualType &ToType) {
  739. assert(FlatTree[ReadNode].Kind == Type && "Unexpected kind");
  740. FromType = FlatTree[ReadNode].FromArgInfo.ArgType;
  741. ToType = FlatTree[ReadNode].ToArgInfo.ArgType;
  742. }
  743. void GetExpressionDiff(Expr *&FromExpr, Expr *&ToExpr) {
  744. assert(FlatTree[ReadNode].Kind == Expression && "Unexpected kind");
  745. FromExpr = FlatTree[ReadNode].FromArgInfo.ArgExpr;
  746. ToExpr = FlatTree[ReadNode].ToArgInfo.ArgExpr;
  747. }
  748. void GetTemplateTemplateDiff(TemplateDecl *&FromTD, TemplateDecl *&ToTD) {
  749. assert(FlatTree[ReadNode].Kind == TemplateTemplate && "Unexpected kind.");
  750. FromTD = FlatTree[ReadNode].FromArgInfo.TD;
  751. ToTD = FlatTree[ReadNode].ToArgInfo.TD;
  752. }
  753. void GetIntegerDiff(llvm::APSInt &FromInt, llvm::APSInt &ToInt,
  754. bool &IsValidFromInt, bool &IsValidToInt,
  755. QualType &FromIntType, QualType &ToIntType,
  756. Expr *&FromExpr, Expr *&ToExpr) {
  757. assert(FlatTree[ReadNode].Kind == Integer && "Unexpected kind.");
  758. FromInt = FlatTree[ReadNode].FromArgInfo.Val;
  759. ToInt = FlatTree[ReadNode].ToArgInfo.Val;
  760. IsValidFromInt = FlatTree[ReadNode].FromArgInfo.IsValidInt;
  761. IsValidToInt = FlatTree[ReadNode].ToArgInfo.IsValidInt;
  762. FromIntType = FlatTree[ReadNode].FromArgInfo.ArgType;
  763. ToIntType = FlatTree[ReadNode].ToArgInfo.ArgType;
  764. FromExpr = FlatTree[ReadNode].FromArgInfo.ArgExpr;
  765. ToExpr = FlatTree[ReadNode].ToArgInfo.ArgExpr;
  766. }
  767. void GetDeclarationDiff(ValueDecl *&FromValueDecl, ValueDecl *&ToValueDecl,
  768. bool &FromAddressOf, bool &ToAddressOf,
  769. bool &FromNullPtr, bool &ToNullPtr, Expr *&FromExpr,
  770. Expr *&ToExpr) {
  771. assert(FlatTree[ReadNode].Kind == Declaration && "Unexpected kind.");
  772. FromValueDecl = FlatTree[ReadNode].FromArgInfo.VD;
  773. ToValueDecl = FlatTree[ReadNode].ToArgInfo.VD;
  774. FromAddressOf = FlatTree[ReadNode].FromArgInfo.NeedAddressOf;
  775. ToAddressOf = FlatTree[ReadNode].ToArgInfo.NeedAddressOf;
  776. FromNullPtr = FlatTree[ReadNode].FromArgInfo.IsNullPtr;
  777. ToNullPtr = FlatTree[ReadNode].ToArgInfo.IsNullPtr;
  778. FromExpr = FlatTree[ReadNode].FromArgInfo.ArgExpr;
  779. ToExpr = FlatTree[ReadNode].ToArgInfo.ArgExpr;
  780. }
  781. void GetFromDeclarationAndToIntegerDiff(
  782. ValueDecl *&FromValueDecl, bool &FromAddressOf, bool &FromNullPtr,
  783. Expr *&FromExpr, llvm::APSInt &ToInt, bool &IsValidToInt,
  784. QualType &ToIntType, Expr *&ToExpr) {
  785. assert(FlatTree[ReadNode].Kind == FromDeclarationAndToInteger &&
  786. "Unexpected kind.");
  787. FromValueDecl = FlatTree[ReadNode].FromArgInfo.VD;
  788. FromAddressOf = FlatTree[ReadNode].FromArgInfo.NeedAddressOf;
  789. FromNullPtr = FlatTree[ReadNode].FromArgInfo.IsNullPtr;
  790. FromExpr = FlatTree[ReadNode].FromArgInfo.ArgExpr;
  791. ToInt = FlatTree[ReadNode].ToArgInfo.Val;
  792. IsValidToInt = FlatTree[ReadNode].ToArgInfo.IsValidInt;
  793. ToIntType = FlatTree[ReadNode].ToArgInfo.ArgType;
  794. ToExpr = FlatTree[ReadNode].ToArgInfo.ArgExpr;
  795. }
  796. void GetFromIntegerAndToDeclarationDiff(
  797. llvm::APSInt &FromInt, bool &IsValidFromInt, QualType &FromIntType,
  798. Expr *&FromExpr, ValueDecl *&ToValueDecl, bool &ToAddressOf,
  799. bool &ToNullPtr, Expr *&ToExpr) {
  800. assert(FlatTree[ReadNode].Kind == FromIntegerAndToDeclaration &&
  801. "Unexpected kind.");
  802. FromInt = FlatTree[ReadNode].FromArgInfo.Val;
  803. IsValidFromInt = FlatTree[ReadNode].FromArgInfo.IsValidInt;
  804. FromIntType = FlatTree[ReadNode].FromArgInfo.ArgType;
  805. FromExpr = FlatTree[ReadNode].FromArgInfo.ArgExpr;
  806. ToValueDecl = FlatTree[ReadNode].ToArgInfo.VD;
  807. ToAddressOf = FlatTree[ReadNode].ToArgInfo.NeedAddressOf;
  808. ToNullPtr = FlatTree[ReadNode].ToArgInfo.IsNullPtr;
  809. ToExpr = FlatTree[ReadNode].ToArgInfo.ArgExpr;
  810. }
  811. /// FromDefault - Return true if the from argument is the default.
  812. bool FromDefault() {
  813. return FlatTree[ReadNode].FromArgInfo.IsDefault;
  814. }
  815. /// ToDefault - Return true if the to argument is the default.
  816. bool ToDefault() {
  817. return FlatTree[ReadNode].ToArgInfo.IsDefault;
  818. }
  819. /// NodeIsSame - Returns true the arguments are the same.
  820. bool NodeIsSame() {
  821. return FlatTree[ReadNode].Same;
  822. }
  823. /// HasChildrend - Returns true if the node has children.
  824. bool HasChildren() {
  825. return FlatTree[ReadNode].ChildNode != 0;
  826. }
  827. /// MoveToChild - Moves from the current node to its child.
  828. void MoveToChild() {
  829. ReadNode = FlatTree[ReadNode].ChildNode;
  830. }
  831. /// AdvanceSibling - If there is a next sibling, advance to it and return
  832. /// true. Otherwise, return false.
  833. bool AdvanceSibling() {
  834. if (FlatTree[ReadNode].NextNode == 0)
  835. return false;
  836. ReadNode = FlatTree[ReadNode].NextNode;
  837. return true;
  838. }
  839. /// HasNextSibling - Return true if the node has a next sibling.
  840. bool HasNextSibling() {
  841. return FlatTree[ReadNode].NextNode != 0;
  842. }
  843. /// Empty - Returns true if the tree has no information.
  844. bool Empty() {
  845. return GetKind() == Invalid;
  846. }
  847. /// GetKind - Returns the current node's type.
  848. DiffKind GetKind() {
  849. return FlatTree[ReadNode].Kind;
  850. }
  851. };
  852. DiffTree Tree;
  853. /// TSTiterator - a pair of iterators that walks the
  854. /// TemplateSpecializationType and the desugared TemplateSpecializationType.
  855. /// The deseguared TemplateArgument should provide the canonical argument
  856. /// for comparisons.
  857. class TSTiterator {
  858. typedef const TemplateArgument& reference;
  859. typedef const TemplateArgument* pointer;
  860. /// InternalIterator - an iterator that is used to enter a
  861. /// TemplateSpecializationType and read TemplateArguments inside template
  862. /// parameter packs in order with the rest of the TemplateArguments.
  863. struct InternalIterator {
  864. /// TST - the template specialization whose arguments this iterator
  865. /// traverse over.
  866. const TemplateSpecializationType *TST;
  867. /// Index - the index of the template argument in TST.
  868. unsigned Index;
  869. /// CurrentTA - if CurrentTA is not the same as EndTA, then CurrentTA
  870. /// points to a TemplateArgument within a parameter pack.
  871. TemplateArgument::pack_iterator CurrentTA;
  872. /// EndTA - the end iterator of a parameter pack
  873. TemplateArgument::pack_iterator EndTA;
  874. /// InternalIterator - Constructs an iterator and sets it to the first
  875. /// template argument.
  876. InternalIterator(const TemplateSpecializationType *TST)
  877. : TST(TST), Index(0), CurrentTA(nullptr), EndTA(nullptr) {
  878. if (!TST) return;
  879. if (isEnd()) return;
  880. // Set to first template argument. If not a parameter pack, done.
  881. TemplateArgument TA = TST->template_arguments()[0];
  882. if (TA.getKind() != TemplateArgument::Pack) return;
  883. // Start looking into the parameter pack.
  884. CurrentTA = TA.pack_begin();
  885. EndTA = TA.pack_end();
  886. // Found a valid template argument.
  887. if (CurrentTA != EndTA) return;
  888. // Parameter pack is empty, use the increment to get to a valid
  889. // template argument.
  890. ++(*this);
  891. }
  892. /// Return true if the iterator is non-singular.
  893. bool isValid() const { return TST; }
  894. /// isEnd - Returns true if the iterator is one past the end.
  895. bool isEnd() const {
  896. assert(TST && "InternalIterator is invalid with a null TST.");
  897. return Index >= TST->template_arguments().size();
  898. }
  899. /// &operator++ - Increment the iterator to the next template argument.
  900. InternalIterator &operator++() {
  901. assert(TST && "InternalIterator is invalid with a null TST.");
  902. if (isEnd()) {
  903. return *this;
  904. }
  905. // If in a parameter pack, advance in the parameter pack.
  906. if (CurrentTA != EndTA) {
  907. ++CurrentTA;
  908. if (CurrentTA != EndTA)
  909. return *this;
  910. }
  911. // Loop until a template argument is found, or the end is reached.
  912. while (true) {
  913. // Advance to the next template argument. Break if reached the end.
  914. if (++Index == TST->template_arguments().size())
  915. break;
  916. // If the TemplateArgument is not a parameter pack, done.
  917. TemplateArgument TA = TST->template_arguments()[Index];
  918. if (TA.getKind() != TemplateArgument::Pack)
  919. break;
  920. // Handle parameter packs.
  921. CurrentTA = TA.pack_begin();
  922. EndTA = TA.pack_end();
  923. // If the parameter pack is empty, try to advance again.
  924. if (CurrentTA != EndTA)
  925. break;
  926. }
  927. return *this;
  928. }
  929. /// operator* - Returns the appropriate TemplateArgument.
  930. reference operator*() const {
  931. assert(TST && "InternalIterator is invalid with a null TST.");
  932. assert(!isEnd() && "Index exceeds number of arguments.");
  933. if (CurrentTA == EndTA)
  934. return TST->template_arguments()[Index];
  935. else
  936. return *CurrentTA;
  937. }
  938. /// operator-> - Allow access to the underlying TemplateArgument.
  939. pointer operator->() const {
  940. assert(TST && "InternalIterator is invalid with a null TST.");
  941. return &operator*();
  942. }
  943. };
  944. InternalIterator SugaredIterator;
  945. InternalIterator DesugaredIterator;
  946. public:
  947. TSTiterator(ASTContext &Context, const TemplateSpecializationType *TST)
  948. : SugaredIterator(TST),
  949. DesugaredIterator(
  950. (TST->isSugared() && !TST->isTypeAlias())
  951. ? GetTemplateSpecializationType(Context, TST->desugar())
  952. : nullptr) {}
  953. /// &operator++ - Increment the iterator to the next template argument.
  954. TSTiterator &operator++() {
  955. ++SugaredIterator;
  956. if (DesugaredIterator.isValid())
  957. ++DesugaredIterator;
  958. return *this;
  959. }
  960. /// operator* - Returns the appropriate TemplateArgument.
  961. reference operator*() const {
  962. return *SugaredIterator;
  963. }
  964. /// operator-> - Allow access to the underlying TemplateArgument.
  965. pointer operator->() const {
  966. return &operator*();
  967. }
  968. /// isEnd - Returns true if no more TemplateArguments are available.
  969. bool isEnd() const {
  970. return SugaredIterator.isEnd();
  971. }
  972. /// hasDesugaredTA - Returns true if there is another TemplateArgument
  973. /// available.
  974. bool hasDesugaredTA() const {
  975. return DesugaredIterator.isValid() && !DesugaredIterator.isEnd();
  976. }
  977. /// getDesugaredTA - Returns the desugared TemplateArgument.
  978. reference getDesugaredTA() const {
  979. assert(DesugaredIterator.isValid() &&
  980. "Desugared TemplateArgument should not be used.");
  981. return *DesugaredIterator;
  982. }
  983. };
  984. // These functions build up the template diff tree, including functions to
  985. // retrieve and compare template arguments.
  986. static const TemplateSpecializationType *GetTemplateSpecializationType(
  987. ASTContext &Context, QualType Ty) {
  988. if (const TemplateSpecializationType *TST =
  989. Ty->getAs<TemplateSpecializationType>())
  990. return TST;
  991. if (const auto* SubstType = Ty->getAs<SubstTemplateTypeParmType>())
  992. Ty = SubstType->getReplacementType();
  993. const RecordType *RT = Ty->getAs<RecordType>();
  994. if (!RT)
  995. return nullptr;
  996. const ClassTemplateSpecializationDecl *CTSD =
  997. dyn_cast<ClassTemplateSpecializationDecl>(RT->getDecl());
  998. if (!CTSD)
  999. return nullptr;
  1000. Ty = Context.getTemplateSpecializationType(
  1001. TemplateName(CTSD->getSpecializedTemplate()),
  1002. CTSD->getTemplateArgs().asArray(),
  1003. Ty.getLocalUnqualifiedType().getCanonicalType());
  1004. return Ty->getAs<TemplateSpecializationType>();
  1005. }
  1006. /// Returns true if the DiffType is Type and false for Template.
  1007. static bool OnlyPerformTypeDiff(ASTContext &Context, QualType FromType,
  1008. QualType ToType,
  1009. const TemplateSpecializationType *&FromArgTST,
  1010. const TemplateSpecializationType *&ToArgTST) {
  1011. if (FromType.isNull() || ToType.isNull())
  1012. return true;
  1013. if (Context.hasSameType(FromType, ToType))
  1014. return true;
  1015. FromArgTST = GetTemplateSpecializationType(Context, FromType);
  1016. ToArgTST = GetTemplateSpecializationType(Context, ToType);
  1017. if (!FromArgTST || !ToArgTST)
  1018. return true;
  1019. if (!hasSameTemplate(FromArgTST, ToArgTST))
  1020. return true;
  1021. return false;
  1022. }
  1023. /// DiffTypes - Fills a DiffNode with information about a type difference.
  1024. void DiffTypes(const TSTiterator &FromIter, const TSTiterator &ToIter) {
  1025. QualType FromType = GetType(FromIter);
  1026. QualType ToType = GetType(ToIter);
  1027. bool FromDefault = FromIter.isEnd() && !FromType.isNull();
  1028. bool ToDefault = ToIter.isEnd() && !ToType.isNull();
  1029. const TemplateSpecializationType *FromArgTST = nullptr;
  1030. const TemplateSpecializationType *ToArgTST = nullptr;
  1031. if (OnlyPerformTypeDiff(Context, FromType, ToType, FromArgTST, ToArgTST)) {
  1032. Tree.SetTypeDiff(FromType, ToType, FromDefault, ToDefault);
  1033. Tree.SetSame(!FromType.isNull() && !ToType.isNull() &&
  1034. Context.hasSameType(FromType, ToType));
  1035. } else {
  1036. assert(FromArgTST && ToArgTST &&
  1037. "Both template specializations need to be valid.");
  1038. Qualifiers FromQual = FromType.getQualifiers(),
  1039. ToQual = ToType.getQualifiers();
  1040. FromQual -= QualType(FromArgTST, 0).getQualifiers();
  1041. ToQual -= QualType(ToArgTST, 0).getQualifiers();
  1042. Tree.SetTemplateDiff(FromArgTST->getTemplateName().getAsTemplateDecl(),
  1043. ToArgTST->getTemplateName().getAsTemplateDecl(),
  1044. FromQual, ToQual, FromDefault, ToDefault);
  1045. DiffTemplate(FromArgTST, ToArgTST);
  1046. }
  1047. }
  1048. /// DiffTemplateTemplates - Fills a DiffNode with information about a
  1049. /// template template difference.
  1050. void DiffTemplateTemplates(const TSTiterator &FromIter,
  1051. const TSTiterator &ToIter) {
  1052. TemplateDecl *FromDecl = GetTemplateDecl(FromIter);
  1053. TemplateDecl *ToDecl = GetTemplateDecl(ToIter);
  1054. Tree.SetTemplateTemplateDiff(FromDecl, ToDecl, FromIter.isEnd() && FromDecl,
  1055. ToIter.isEnd() && ToDecl);
  1056. Tree.SetSame(FromDecl && ToDecl &&
  1057. FromDecl->getCanonicalDecl() == ToDecl->getCanonicalDecl());
  1058. }
  1059. /// InitializeNonTypeDiffVariables - Helper function for DiffNonTypes
  1060. static void InitializeNonTypeDiffVariables(ASTContext &Context,
  1061. const TSTiterator &Iter,
  1062. NonTypeTemplateParmDecl *Default,
  1063. llvm::APSInt &Value, bool &HasInt,
  1064. QualType &IntType, bool &IsNullPtr,
  1065. Expr *&E, ValueDecl *&VD,
  1066. bool &NeedAddressOf) {
  1067. if (!Iter.isEnd()) {
  1068. switch (Iter->getKind()) {
  1069. default:
  1070. llvm_unreachable("unknown ArgumentKind");
  1071. case TemplateArgument::Integral:
  1072. Value = Iter->getAsIntegral();
  1073. HasInt = true;
  1074. IntType = Iter->getIntegralType();
  1075. return;
  1076. case TemplateArgument::Declaration: {
  1077. VD = Iter->getAsDecl();
  1078. QualType ArgType = Iter->getParamTypeForDecl();
  1079. QualType VDType = VD->getType();
  1080. if (ArgType->isPointerType() &&
  1081. Context.hasSameType(ArgType->getPointeeType(), VDType))
  1082. NeedAddressOf = true;
  1083. return;
  1084. }
  1085. case TemplateArgument::NullPtr:
  1086. IsNullPtr = true;
  1087. return;
  1088. case TemplateArgument::Expression:
  1089. E = Iter->getAsExpr();
  1090. }
  1091. } else if (!Default->isParameterPack()) {
  1092. E = Default->getDefaultArgument();
  1093. }
  1094. if (!Iter.hasDesugaredTA()) return;
  1095. const TemplateArgument& TA = Iter.getDesugaredTA();
  1096. switch (TA.getKind()) {
  1097. default:
  1098. llvm_unreachable("unknown ArgumentKind");
  1099. case TemplateArgument::Integral:
  1100. Value = TA.getAsIntegral();
  1101. HasInt = true;
  1102. IntType = TA.getIntegralType();
  1103. return;
  1104. case TemplateArgument::Declaration: {
  1105. VD = TA.getAsDecl();
  1106. QualType ArgType = TA.getParamTypeForDecl();
  1107. QualType VDType = VD->getType();
  1108. if (ArgType->isPointerType() &&
  1109. Context.hasSameType(ArgType->getPointeeType(), VDType))
  1110. NeedAddressOf = true;
  1111. return;
  1112. }
  1113. case TemplateArgument::NullPtr:
  1114. IsNullPtr = true;
  1115. return;
  1116. case TemplateArgument::Expression:
  1117. // TODO: Sometimes, the desugared template argument Expr differs from
  1118. // the sugared template argument Expr. It may be useful in the future
  1119. // but for now, it is just discarded.
  1120. if (!E)
  1121. E = TA.getAsExpr();
  1122. return;
  1123. }
  1124. }
  1125. /// DiffNonTypes - Handles any template parameters not handled by DiffTypes
  1126. /// of DiffTemplatesTemplates, such as integer and declaration parameters.
  1127. void DiffNonTypes(const TSTiterator &FromIter, const TSTiterator &ToIter,
  1128. NonTypeTemplateParmDecl *FromDefaultNonTypeDecl,
  1129. NonTypeTemplateParmDecl *ToDefaultNonTypeDecl) {
  1130. Expr *FromExpr = nullptr, *ToExpr = nullptr;
  1131. llvm::APSInt FromInt, ToInt;
  1132. QualType FromIntType, ToIntType;
  1133. ValueDecl *FromValueDecl = nullptr, *ToValueDecl = nullptr;
  1134. bool HasFromInt = false, HasToInt = false, FromNullPtr = false,
  1135. ToNullPtr = false, NeedFromAddressOf = false, NeedToAddressOf = false;
  1136. InitializeNonTypeDiffVariables(
  1137. Context, FromIter, FromDefaultNonTypeDecl, FromInt, HasFromInt,
  1138. FromIntType, FromNullPtr, FromExpr, FromValueDecl, NeedFromAddressOf);
  1139. InitializeNonTypeDiffVariables(Context, ToIter, ToDefaultNonTypeDecl, ToInt,
  1140. HasToInt, ToIntType, ToNullPtr, ToExpr,
  1141. ToValueDecl, NeedToAddressOf);
  1142. bool FromDefault = FromIter.isEnd() &&
  1143. (FromExpr || FromValueDecl || HasFromInt || FromNullPtr);
  1144. bool ToDefault = ToIter.isEnd() &&
  1145. (ToExpr || ToValueDecl || HasToInt || ToNullPtr);
  1146. bool FromDeclaration = FromValueDecl || FromNullPtr;
  1147. bool ToDeclaration = ToValueDecl || ToNullPtr;
  1148. if (FromDeclaration && HasToInt) {
  1149. Tree.SetFromDeclarationAndToIntegerDiff(
  1150. FromValueDecl, NeedFromAddressOf, FromNullPtr, FromExpr, ToInt,
  1151. HasToInt, ToIntType, ToExpr, FromDefault, ToDefault);
  1152. Tree.SetSame(false);
  1153. return;
  1154. }
  1155. if (HasFromInt && ToDeclaration) {
  1156. Tree.SetFromIntegerAndToDeclarationDiff(
  1157. FromInt, HasFromInt, FromIntType, FromExpr, ToValueDecl,
  1158. NeedToAddressOf, ToNullPtr, ToExpr, FromDefault, ToDefault);
  1159. Tree.SetSame(false);
  1160. return;
  1161. }
  1162. if (HasFromInt || HasToInt) {
  1163. Tree.SetIntegerDiff(FromInt, ToInt, HasFromInt, HasToInt, FromIntType,
  1164. ToIntType, FromExpr, ToExpr, FromDefault, ToDefault);
  1165. if (HasFromInt && HasToInt) {
  1166. Tree.SetSame(Context.hasSameType(FromIntType, ToIntType) &&
  1167. FromInt == ToInt);
  1168. }
  1169. return;
  1170. }
  1171. if (FromDeclaration || ToDeclaration) {
  1172. Tree.SetDeclarationDiff(FromValueDecl, ToValueDecl, NeedFromAddressOf,
  1173. NeedToAddressOf, FromNullPtr, ToNullPtr, FromExpr,
  1174. ToExpr, FromDefault, ToDefault);
  1175. bool BothNull = FromNullPtr && ToNullPtr;
  1176. bool SameValueDecl =
  1177. FromValueDecl && ToValueDecl &&
  1178. NeedFromAddressOf == NeedToAddressOf &&
  1179. FromValueDecl->getCanonicalDecl() == ToValueDecl->getCanonicalDecl();
  1180. Tree.SetSame(BothNull || SameValueDecl);
  1181. return;
  1182. }
  1183. assert((FromExpr || ToExpr) && "Both template arguments cannot be empty.");
  1184. Tree.SetExpressionDiff(FromExpr, ToExpr, FromDefault, ToDefault);
  1185. Tree.SetSame(IsEqualExpr(Context, FromExpr, ToExpr));
  1186. }
  1187. /// DiffTemplate - recursively visits template arguments and stores the
  1188. /// argument info into a tree.
  1189. void DiffTemplate(const TemplateSpecializationType *FromTST,
  1190. const TemplateSpecializationType *ToTST) {
  1191. // Begin descent into diffing template tree.
  1192. TemplateParameterList *ParamsFrom =
  1193. FromTST->getTemplateName().getAsTemplateDecl()->getTemplateParameters();
  1194. TemplateParameterList *ParamsTo =
  1195. ToTST->getTemplateName().getAsTemplateDecl()->getTemplateParameters();
  1196. unsigned TotalArgs = 0;
  1197. for (TSTiterator FromIter(Context, FromTST), ToIter(Context, ToTST);
  1198. !FromIter.isEnd() || !ToIter.isEnd(); ++TotalArgs) {
  1199. Tree.AddNode();
  1200. // Get the parameter at index TotalArgs. If index is larger
  1201. // than the total number of parameters, then there is an
  1202. // argument pack, so re-use the last parameter.
  1203. unsigned FromParamIndex = std::min(TotalArgs, ParamsFrom->size() - 1);
  1204. unsigned ToParamIndex = std::min(TotalArgs, ParamsTo->size() - 1);
  1205. NamedDecl *FromParamND = ParamsFrom->getParam(FromParamIndex);
  1206. NamedDecl *ToParamND = ParamsTo->getParam(ToParamIndex);
  1207. assert(FromParamND->getKind() == ToParamND->getKind() &&
  1208. "Parameter Decl are not the same kind.");
  1209. if (isa<TemplateTypeParmDecl>(FromParamND)) {
  1210. DiffTypes(FromIter, ToIter);
  1211. } else if (isa<TemplateTemplateParmDecl>(FromParamND)) {
  1212. DiffTemplateTemplates(FromIter, ToIter);
  1213. } else if (isa<NonTypeTemplateParmDecl>(FromParamND)) {
  1214. NonTypeTemplateParmDecl *FromDefaultNonTypeDecl =
  1215. cast<NonTypeTemplateParmDecl>(FromParamND);
  1216. NonTypeTemplateParmDecl *ToDefaultNonTypeDecl =
  1217. cast<NonTypeTemplateParmDecl>(ToParamND);
  1218. DiffNonTypes(FromIter, ToIter, FromDefaultNonTypeDecl,
  1219. ToDefaultNonTypeDecl);
  1220. } else {
  1221. llvm_unreachable("Unexpected Decl type.");
  1222. }
  1223. ++FromIter;
  1224. ++ToIter;
  1225. Tree.Up();
  1226. }
  1227. }
  1228. /// makeTemplateList - Dump every template alias into the vector.
  1229. static void makeTemplateList(
  1230. SmallVectorImpl<const TemplateSpecializationType *> &TemplateList,
  1231. const TemplateSpecializationType *TST) {
  1232. while (TST) {
  1233. TemplateList.push_back(TST);
  1234. if (!TST->isTypeAlias())
  1235. return;
  1236. TST = TST->getAliasedType()->getAs<TemplateSpecializationType>();
  1237. }
  1238. }
  1239. /// hasSameBaseTemplate - Returns true when the base templates are the same,
  1240. /// even if the template arguments are not.
  1241. static bool hasSameBaseTemplate(const TemplateSpecializationType *FromTST,
  1242. const TemplateSpecializationType *ToTST) {
  1243. return FromTST->getTemplateName().getAsTemplateDecl()->getCanonicalDecl() ==
  1244. ToTST->getTemplateName().getAsTemplateDecl()->getCanonicalDecl();
  1245. }
  1246. /// hasSameTemplate - Returns true if both types are specialized from the
  1247. /// same template declaration. If they come from different template aliases,
  1248. /// do a parallel ascension search to determine the highest template alias in
  1249. /// common and set the arguments to them.
  1250. static bool hasSameTemplate(const TemplateSpecializationType *&FromTST,
  1251. const TemplateSpecializationType *&ToTST) {
  1252. // Check the top templates if they are the same.
  1253. if (hasSameBaseTemplate(FromTST, ToTST))
  1254. return true;
  1255. // Create vectors of template aliases.
  1256. SmallVector<const TemplateSpecializationType*, 1> FromTemplateList,
  1257. ToTemplateList;
  1258. makeTemplateList(FromTemplateList, FromTST);
  1259. makeTemplateList(ToTemplateList, ToTST);
  1260. SmallVectorImpl<const TemplateSpecializationType *>::reverse_iterator
  1261. FromIter = FromTemplateList.rbegin(), FromEnd = FromTemplateList.rend(),
  1262. ToIter = ToTemplateList.rbegin(), ToEnd = ToTemplateList.rend();
  1263. // Check if the lowest template types are the same. If not, return.
  1264. if (!hasSameBaseTemplate(*FromIter, *ToIter))
  1265. return false;
  1266. // Begin searching up the template aliases. The bottom most template
  1267. // matches so move up until one pair does not match. Use the template
  1268. // right before that one.
  1269. for (; FromIter != FromEnd && ToIter != ToEnd; ++FromIter, ++ToIter) {
  1270. if (!hasSameBaseTemplate(*FromIter, *ToIter))
  1271. break;
  1272. }
  1273. FromTST = FromIter[-1];
  1274. ToTST = ToIter[-1];
  1275. return true;
  1276. }
  1277. /// GetType - Retrieves the template type arguments, including default
  1278. /// arguments.
  1279. static QualType GetType(const TSTiterator &Iter) {
  1280. if (!Iter.isEnd())
  1281. return Iter->getAsType();
  1282. if (Iter.hasDesugaredTA())
  1283. return Iter.getDesugaredTA().getAsType();
  1284. return QualType();
  1285. }
  1286. /// GetTemplateDecl - Retrieves the template template arguments, including
  1287. /// default arguments.
  1288. static TemplateDecl *GetTemplateDecl(const TSTiterator &Iter) {
  1289. if (!Iter.isEnd())
  1290. return Iter->getAsTemplate().getAsTemplateDecl();
  1291. if (Iter.hasDesugaredTA())
  1292. return Iter.getDesugaredTA().getAsTemplate().getAsTemplateDecl();
  1293. return nullptr;
  1294. }
  1295. /// IsEqualExpr - Returns true if the expressions are the same in regards to
  1296. /// template arguments. These expressions are dependent, so profile them
  1297. /// instead of trying to evaluate them.
  1298. static bool IsEqualExpr(ASTContext &Context, Expr *FromExpr, Expr *ToExpr) {
  1299. if (FromExpr == ToExpr)
  1300. return true;
  1301. if (!FromExpr || !ToExpr)
  1302. return false;
  1303. llvm::FoldingSetNodeID FromID, ToID;
  1304. FromExpr->Profile(FromID, Context, true);
  1305. ToExpr->Profile(ToID, Context, true);
  1306. return FromID == ToID;
  1307. }
  1308. // These functions converts the tree representation of the template
  1309. // differences into the internal character vector.
  1310. /// TreeToString - Converts the Tree object into a character stream which
  1311. /// will later be turned into the output string.
  1312. void TreeToString(int Indent = 1) {
  1313. if (PrintTree) {
  1314. OS << '\n';
  1315. OS.indent(2 * Indent);
  1316. ++Indent;
  1317. }
  1318. // Handle cases where the difference is not templates with different
  1319. // arguments.
  1320. switch (Tree.GetKind()) {
  1321. case DiffTree::Invalid:
  1322. llvm_unreachable("Template diffing failed with bad DiffNode");
  1323. case DiffTree::Type: {
  1324. QualType FromType, ToType;
  1325. Tree.GetTypeDiff(FromType, ToType);
  1326. PrintTypeNames(FromType, ToType, Tree.FromDefault(), Tree.ToDefault(),
  1327. Tree.NodeIsSame());
  1328. return;
  1329. }
  1330. case DiffTree::Expression: {
  1331. Expr *FromExpr, *ToExpr;
  1332. Tree.GetExpressionDiff(FromExpr, ToExpr);
  1333. PrintExpr(FromExpr, ToExpr, Tree.FromDefault(), Tree.ToDefault(),
  1334. Tree.NodeIsSame());
  1335. return;
  1336. }
  1337. case DiffTree::TemplateTemplate: {
  1338. TemplateDecl *FromTD, *ToTD;
  1339. Tree.GetTemplateTemplateDiff(FromTD, ToTD);
  1340. PrintTemplateTemplate(FromTD, ToTD, Tree.FromDefault(),
  1341. Tree.ToDefault(), Tree.NodeIsSame());
  1342. return;
  1343. }
  1344. case DiffTree::Integer: {
  1345. llvm::APSInt FromInt, ToInt;
  1346. Expr *FromExpr, *ToExpr;
  1347. bool IsValidFromInt, IsValidToInt;
  1348. QualType FromIntType, ToIntType;
  1349. Tree.GetIntegerDiff(FromInt, ToInt, IsValidFromInt, IsValidToInt,
  1350. FromIntType, ToIntType, FromExpr, ToExpr);
  1351. PrintAPSInt(FromInt, ToInt, IsValidFromInt, IsValidToInt, FromIntType,
  1352. ToIntType, FromExpr, ToExpr, Tree.FromDefault(),
  1353. Tree.ToDefault(), Tree.NodeIsSame());
  1354. return;
  1355. }
  1356. case DiffTree::Declaration: {
  1357. ValueDecl *FromValueDecl, *ToValueDecl;
  1358. bool FromAddressOf, ToAddressOf;
  1359. bool FromNullPtr, ToNullPtr;
  1360. Expr *FromExpr, *ToExpr;
  1361. Tree.GetDeclarationDiff(FromValueDecl, ToValueDecl, FromAddressOf,
  1362. ToAddressOf, FromNullPtr, ToNullPtr, FromExpr,
  1363. ToExpr);
  1364. PrintValueDecl(FromValueDecl, ToValueDecl, FromAddressOf, ToAddressOf,
  1365. FromNullPtr, ToNullPtr, FromExpr, ToExpr,
  1366. Tree.FromDefault(), Tree.ToDefault(), Tree.NodeIsSame());
  1367. return;
  1368. }
  1369. case DiffTree::FromDeclarationAndToInteger: {
  1370. ValueDecl *FromValueDecl;
  1371. bool FromAddressOf;
  1372. bool FromNullPtr;
  1373. Expr *FromExpr;
  1374. llvm::APSInt ToInt;
  1375. bool IsValidToInt;
  1376. QualType ToIntType;
  1377. Expr *ToExpr;
  1378. Tree.GetFromDeclarationAndToIntegerDiff(
  1379. FromValueDecl, FromAddressOf, FromNullPtr, FromExpr, ToInt,
  1380. IsValidToInt, ToIntType, ToExpr);
  1381. assert((FromValueDecl || FromNullPtr) && IsValidToInt);
  1382. PrintValueDeclAndInteger(FromValueDecl, FromAddressOf, FromNullPtr,
  1383. FromExpr, Tree.FromDefault(), ToInt, ToIntType,
  1384. ToExpr, Tree.ToDefault());
  1385. return;
  1386. }
  1387. case DiffTree::FromIntegerAndToDeclaration: {
  1388. llvm::APSInt FromInt;
  1389. bool IsValidFromInt;
  1390. QualType FromIntType;
  1391. Expr *FromExpr;
  1392. ValueDecl *ToValueDecl;
  1393. bool ToAddressOf;
  1394. bool ToNullPtr;
  1395. Expr *ToExpr;
  1396. Tree.GetFromIntegerAndToDeclarationDiff(
  1397. FromInt, IsValidFromInt, FromIntType, FromExpr, ToValueDecl,
  1398. ToAddressOf, ToNullPtr, ToExpr);
  1399. assert(IsValidFromInt && (ToValueDecl || ToNullPtr));
  1400. PrintIntegerAndValueDecl(FromInt, FromIntType, FromExpr,
  1401. Tree.FromDefault(), ToValueDecl, ToAddressOf,
  1402. ToNullPtr, ToExpr, Tree.ToDefault());
  1403. return;
  1404. }
  1405. case DiffTree::Template: {
  1406. // Node is root of template. Recurse on children.
  1407. TemplateDecl *FromTD, *ToTD;
  1408. Qualifiers FromQual, ToQual;
  1409. Tree.GetTemplateDiff(FromTD, ToTD, FromQual, ToQual);
  1410. PrintQualifiers(FromQual, ToQual);
  1411. if (!Tree.HasChildren()) {
  1412. // If we're dealing with a template specialization with zero
  1413. // arguments, there are no children; special-case this.
  1414. OS << FromTD->getDeclName() << "<>";
  1415. return;
  1416. }
  1417. OS << FromTD->getDeclName() << '<';
  1418. Tree.MoveToChild();
  1419. unsigned NumElideArgs = 0;
  1420. bool AllArgsElided = true;
  1421. do {
  1422. if (ElideType) {
  1423. if (Tree.NodeIsSame()) {
  1424. ++NumElideArgs;
  1425. continue;
  1426. }
  1427. AllArgsElided = false;
  1428. if (NumElideArgs > 0) {
  1429. PrintElideArgs(NumElideArgs, Indent);
  1430. NumElideArgs = 0;
  1431. OS << ", ";
  1432. }
  1433. }
  1434. TreeToString(Indent);
  1435. if (Tree.HasNextSibling())
  1436. OS << ", ";
  1437. } while (Tree.AdvanceSibling());
  1438. if (NumElideArgs > 0) {
  1439. if (AllArgsElided)
  1440. OS << "...";
  1441. else
  1442. PrintElideArgs(NumElideArgs, Indent);
  1443. }
  1444. Tree.Parent();
  1445. OS << ">";
  1446. return;
  1447. }
  1448. }
  1449. }
  1450. // To signal to the text printer that a certain text needs to be bolded,
  1451. // a special character is injected into the character stream which the
  1452. // text printer will later strip out.
  1453. /// Bold - Start bolding text.
  1454. void Bold() {
  1455. assert(!IsBold && "Attempting to bold text that is already bold.");
  1456. IsBold = true;
  1457. if (ShowColor)
  1458. OS << ToggleHighlight;
  1459. }
  1460. /// Unbold - Stop bolding text.
  1461. void Unbold() {
  1462. assert(IsBold && "Attempting to remove bold from unbold text.");
  1463. IsBold = false;
  1464. if (ShowColor)
  1465. OS << ToggleHighlight;
  1466. }
  1467. // Functions to print out the arguments and highlighting the difference.
  1468. /// PrintTypeNames - prints the typenames, bolding differences. Will detect
  1469. /// typenames that are the same and attempt to disambiguate them by using
  1470. /// canonical typenames.
  1471. void PrintTypeNames(QualType FromType, QualType ToType,
  1472. bool FromDefault, bool ToDefault, bool Same) {
  1473. assert((!FromType.isNull() || !ToType.isNull()) &&
  1474. "Only one template argument may be missing.");
  1475. if (Same) {
  1476. OS << FromType.getAsString(Policy);
  1477. return;
  1478. }
  1479. if (!FromType.isNull() && !ToType.isNull() &&
  1480. FromType.getLocalUnqualifiedType() ==
  1481. ToType.getLocalUnqualifiedType()) {
  1482. Qualifiers FromQual = FromType.getLocalQualifiers(),
  1483. ToQual = ToType.getLocalQualifiers();
  1484. PrintQualifiers(FromQual, ToQual);
  1485. FromType.getLocalUnqualifiedType().print(OS, Policy);
  1486. return;
  1487. }
  1488. std::string FromTypeStr = FromType.isNull() ? "(no argument)"
  1489. : FromType.getAsString(Policy);
  1490. std::string ToTypeStr = ToType.isNull() ? "(no argument)"
  1491. : ToType.getAsString(Policy);
  1492. // Print without ElaboratedType sugar if it is better.
  1493. // TODO: merge this with other aka printing above.
  1494. if (FromTypeStr == ToTypeStr) {
  1495. const auto *FromElTy = dyn_cast<ElaboratedType>(FromType),
  1496. *ToElTy = dyn_cast<ElaboratedType>(ToType);
  1497. if (FromElTy || ToElTy) {
  1498. std::string FromNamedTypeStr =
  1499. FromElTy ? FromElTy->getNamedType().getAsString(Policy)
  1500. : FromTypeStr;
  1501. std::string ToNamedTypeStr =
  1502. ToElTy ? ToElTy->getNamedType().getAsString(Policy) : ToTypeStr;
  1503. if (FromNamedTypeStr != ToNamedTypeStr) {
  1504. FromTypeStr = FromNamedTypeStr;
  1505. ToTypeStr = ToNamedTypeStr;
  1506. goto PrintTypes;
  1507. }
  1508. }
  1509. // Switch to canonical typename if it is better.
  1510. std::string FromCanTypeStr =
  1511. FromType.getCanonicalType().getAsString(Policy);
  1512. std::string ToCanTypeStr = ToType.getCanonicalType().getAsString(Policy);
  1513. if (FromCanTypeStr != ToCanTypeStr) {
  1514. FromTypeStr = FromCanTypeStr;
  1515. ToTypeStr = ToCanTypeStr;
  1516. }
  1517. }
  1518. PrintTypes:
  1519. if (PrintTree) OS << '[';
  1520. OS << (FromDefault ? "(default) " : "");
  1521. Bold();
  1522. OS << FromTypeStr;
  1523. Unbold();
  1524. if (PrintTree) {
  1525. OS << " != " << (ToDefault ? "(default) " : "");
  1526. Bold();
  1527. OS << ToTypeStr;
  1528. Unbold();
  1529. OS << "]";
  1530. }
  1531. }
  1532. /// PrintExpr - Prints out the expr template arguments, highlighting argument
  1533. /// differences.
  1534. void PrintExpr(const Expr *FromExpr, const Expr *ToExpr, bool FromDefault,
  1535. bool ToDefault, bool Same) {
  1536. assert((FromExpr || ToExpr) &&
  1537. "Only one template argument may be missing.");
  1538. if (Same) {
  1539. PrintExpr(FromExpr);
  1540. } else if (!PrintTree) {
  1541. OS << (FromDefault ? "(default) " : "");
  1542. Bold();
  1543. PrintExpr(FromExpr);
  1544. Unbold();
  1545. } else {
  1546. OS << (FromDefault ? "[(default) " : "[");
  1547. Bold();
  1548. PrintExpr(FromExpr);
  1549. Unbold();
  1550. OS << " != " << (ToDefault ? "(default) " : "");
  1551. Bold();
  1552. PrintExpr(ToExpr);
  1553. Unbold();
  1554. OS << ']';
  1555. }
  1556. }
  1557. /// PrintExpr - Actual formatting and printing of expressions.
  1558. void PrintExpr(const Expr *E) {
  1559. if (E) {
  1560. E->printPretty(OS, nullptr, Policy);
  1561. return;
  1562. }
  1563. OS << "(no argument)";
  1564. }
  1565. /// PrintTemplateTemplate - Handles printing of template template arguments,
  1566. /// highlighting argument differences.
  1567. void PrintTemplateTemplate(TemplateDecl *FromTD, TemplateDecl *ToTD,
  1568. bool FromDefault, bool ToDefault, bool Same) {
  1569. assert((FromTD || ToTD) && "Only one template argument may be missing.");
  1570. std::string FromName =
  1571. std::string(FromTD ? FromTD->getName() : "(no argument)");
  1572. std::string ToName = std::string(ToTD ? ToTD->getName() : "(no argument)");
  1573. if (FromTD && ToTD && FromName == ToName) {
  1574. FromName = FromTD->getQualifiedNameAsString();
  1575. ToName = ToTD->getQualifiedNameAsString();
  1576. }
  1577. if (Same) {
  1578. OS << "template " << FromTD->getDeclName();
  1579. } else if (!PrintTree) {
  1580. OS << (FromDefault ? "(default) template " : "template ");
  1581. Bold();
  1582. OS << FromName;
  1583. Unbold();
  1584. } else {
  1585. OS << (FromDefault ? "[(default) template " : "[template ");
  1586. Bold();
  1587. OS << FromName;
  1588. Unbold();
  1589. OS << " != " << (ToDefault ? "(default) template " : "template ");
  1590. Bold();
  1591. OS << ToName;
  1592. Unbold();
  1593. OS << ']';
  1594. }
  1595. }
  1596. /// PrintAPSInt - Handles printing of integral arguments, highlighting
  1597. /// argument differences.
  1598. void PrintAPSInt(const llvm::APSInt &FromInt, const llvm::APSInt &ToInt,
  1599. bool IsValidFromInt, bool IsValidToInt, QualType FromIntType,
  1600. QualType ToIntType, Expr *FromExpr, Expr *ToExpr,
  1601. bool FromDefault, bool ToDefault, bool Same) {
  1602. assert((IsValidFromInt || IsValidToInt) &&
  1603. "Only one integral argument may be missing.");
  1604. if (Same) {
  1605. if (FromIntType->isBooleanType()) {
  1606. OS << ((FromInt == 0) ? "false" : "true");
  1607. } else {
  1608. OS << toString(FromInt, 10);
  1609. }
  1610. return;
  1611. }
  1612. bool PrintType = IsValidFromInt && IsValidToInt &&
  1613. !Context.hasSameType(FromIntType, ToIntType);
  1614. if (!PrintTree) {
  1615. OS << (FromDefault ? "(default) " : "");
  1616. PrintAPSInt(FromInt, FromExpr, IsValidFromInt, FromIntType, PrintType);
  1617. } else {
  1618. OS << (FromDefault ? "[(default) " : "[");
  1619. PrintAPSInt(FromInt, FromExpr, IsValidFromInt, FromIntType, PrintType);
  1620. OS << " != " << (ToDefault ? "(default) " : "");
  1621. PrintAPSInt(ToInt, ToExpr, IsValidToInt, ToIntType, PrintType);
  1622. OS << ']';
  1623. }
  1624. }
  1625. /// PrintAPSInt - If valid, print the APSInt. If the expression is
  1626. /// gives more information, print it too.
  1627. void PrintAPSInt(const llvm::APSInt &Val, Expr *E, bool Valid,
  1628. QualType IntType, bool PrintType) {
  1629. Bold();
  1630. if (Valid) {
  1631. if (HasExtraInfo(E)) {
  1632. PrintExpr(E);
  1633. Unbold();
  1634. OS << " aka ";
  1635. Bold();
  1636. }
  1637. if (PrintType) {
  1638. Unbold();
  1639. OS << "(";
  1640. Bold();
  1641. IntType.print(OS, Context.getPrintingPolicy());
  1642. Unbold();
  1643. OS << ") ";
  1644. Bold();
  1645. }
  1646. if (IntType->isBooleanType()) {
  1647. OS << ((Val == 0) ? "false" : "true");
  1648. } else {
  1649. OS << toString(Val, 10);
  1650. }
  1651. } else if (E) {
  1652. PrintExpr(E);
  1653. } else {
  1654. OS << "(no argument)";
  1655. }
  1656. Unbold();
  1657. }
  1658. /// HasExtraInfo - Returns true if E is not an integer literal, the
  1659. /// negation of an integer literal, or a boolean literal.
  1660. bool HasExtraInfo(Expr *E) {
  1661. if (!E) return false;
  1662. E = E->IgnoreImpCasts();
  1663. if (isa<IntegerLiteral>(E)) return false;
  1664. if (UnaryOperator *UO = dyn_cast<UnaryOperator>(E))
  1665. if (UO->getOpcode() == UO_Minus)
  1666. if (isa<IntegerLiteral>(UO->getSubExpr()))
  1667. return false;
  1668. if (isa<CXXBoolLiteralExpr>(E))
  1669. return false;
  1670. return true;
  1671. }
  1672. void PrintValueDecl(ValueDecl *VD, bool AddressOf, Expr *E, bool NullPtr) {
  1673. if (VD) {
  1674. if (AddressOf)
  1675. OS << "&";
  1676. else if (auto *TPO = dyn_cast<TemplateParamObjectDecl>(VD)) {
  1677. // FIXME: Diffing the APValue would be neat.
  1678. // FIXME: Suppress this and use the full name of the declaration if the
  1679. // parameter is a pointer or reference.
  1680. TPO->printAsInit(OS, Policy);
  1681. return;
  1682. }
  1683. VD->printName(OS, Policy);
  1684. return;
  1685. }
  1686. if (NullPtr) {
  1687. if (E && !isa<CXXNullPtrLiteralExpr>(E)) {
  1688. PrintExpr(E);
  1689. if (IsBold) {
  1690. Unbold();
  1691. OS << " aka ";
  1692. Bold();
  1693. } else {
  1694. OS << " aka ";
  1695. }
  1696. }
  1697. OS << "nullptr";
  1698. return;
  1699. }
  1700. OS << "(no argument)";
  1701. }
  1702. /// PrintDecl - Handles printing of Decl arguments, highlighting
  1703. /// argument differences.
  1704. void PrintValueDecl(ValueDecl *FromValueDecl, ValueDecl *ToValueDecl,
  1705. bool FromAddressOf, bool ToAddressOf, bool FromNullPtr,
  1706. bool ToNullPtr, Expr *FromExpr, Expr *ToExpr,
  1707. bool FromDefault, bool ToDefault, bool Same) {
  1708. assert((FromValueDecl || FromNullPtr || ToValueDecl || ToNullPtr) &&
  1709. "Only one Decl argument may be NULL");
  1710. if (Same) {
  1711. PrintValueDecl(FromValueDecl, FromAddressOf, FromExpr, FromNullPtr);
  1712. } else if (!PrintTree) {
  1713. OS << (FromDefault ? "(default) " : "");
  1714. Bold();
  1715. PrintValueDecl(FromValueDecl, FromAddressOf, FromExpr, FromNullPtr);
  1716. Unbold();
  1717. } else {
  1718. OS << (FromDefault ? "[(default) " : "[");
  1719. Bold();
  1720. PrintValueDecl(FromValueDecl, FromAddressOf, FromExpr, FromNullPtr);
  1721. Unbold();
  1722. OS << " != " << (ToDefault ? "(default) " : "");
  1723. Bold();
  1724. PrintValueDecl(ToValueDecl, ToAddressOf, ToExpr, ToNullPtr);
  1725. Unbold();
  1726. OS << ']';
  1727. }
  1728. }
  1729. /// PrintValueDeclAndInteger - Uses the print functions for ValueDecl and
  1730. /// APSInt to print a mixed difference.
  1731. void PrintValueDeclAndInteger(ValueDecl *VD, bool NeedAddressOf,
  1732. bool IsNullPtr, Expr *VDExpr, bool DefaultDecl,
  1733. const llvm::APSInt &Val, QualType IntType,
  1734. Expr *IntExpr, bool DefaultInt) {
  1735. if (!PrintTree) {
  1736. OS << (DefaultDecl ? "(default) " : "");
  1737. Bold();
  1738. PrintValueDecl(VD, NeedAddressOf, VDExpr, IsNullPtr);
  1739. Unbold();
  1740. } else {
  1741. OS << (DefaultDecl ? "[(default) " : "[");
  1742. Bold();
  1743. PrintValueDecl(VD, NeedAddressOf, VDExpr, IsNullPtr);
  1744. Unbold();
  1745. OS << " != " << (DefaultInt ? "(default) " : "");
  1746. PrintAPSInt(Val, IntExpr, true /*Valid*/, IntType, false /*PrintType*/);
  1747. OS << ']';
  1748. }
  1749. }
  1750. /// PrintIntegerAndValueDecl - Uses the print functions for APSInt and
  1751. /// ValueDecl to print a mixed difference.
  1752. void PrintIntegerAndValueDecl(const llvm::APSInt &Val, QualType IntType,
  1753. Expr *IntExpr, bool DefaultInt, ValueDecl *VD,
  1754. bool NeedAddressOf, bool IsNullPtr,
  1755. Expr *VDExpr, bool DefaultDecl) {
  1756. if (!PrintTree) {
  1757. OS << (DefaultInt ? "(default) " : "");
  1758. PrintAPSInt(Val, IntExpr, true /*Valid*/, IntType, false /*PrintType*/);
  1759. } else {
  1760. OS << (DefaultInt ? "[(default) " : "[");
  1761. PrintAPSInt(Val, IntExpr, true /*Valid*/, IntType, false /*PrintType*/);
  1762. OS << " != " << (DefaultDecl ? "(default) " : "");
  1763. Bold();
  1764. PrintValueDecl(VD, NeedAddressOf, VDExpr, IsNullPtr);
  1765. Unbold();
  1766. OS << ']';
  1767. }
  1768. }
  1769. // Prints the appropriate placeholder for elided template arguments.
  1770. void PrintElideArgs(unsigned NumElideArgs, unsigned Indent) {
  1771. if (PrintTree) {
  1772. OS << '\n';
  1773. for (unsigned i = 0; i < Indent; ++i)
  1774. OS << " ";
  1775. }
  1776. if (NumElideArgs == 0) return;
  1777. if (NumElideArgs == 1)
  1778. OS << "[...]";
  1779. else
  1780. OS << "[" << NumElideArgs << " * ...]";
  1781. }
  1782. // Prints and highlights differences in Qualifiers.
  1783. void PrintQualifiers(Qualifiers FromQual, Qualifiers ToQual) {
  1784. // Both types have no qualifiers
  1785. if (FromQual.empty() && ToQual.empty())
  1786. return;
  1787. // Both types have same qualifiers
  1788. if (FromQual == ToQual) {
  1789. PrintQualifier(FromQual, /*ApplyBold*/false);
  1790. return;
  1791. }
  1792. // Find common qualifiers and strip them from FromQual and ToQual.
  1793. Qualifiers CommonQual = Qualifiers::removeCommonQualifiers(FromQual,
  1794. ToQual);
  1795. // The qualifiers are printed before the template name.
  1796. // Inline printing:
  1797. // The common qualifiers are printed. Then, qualifiers only in this type
  1798. // are printed and highlighted. Finally, qualifiers only in the other
  1799. // type are printed and highlighted inside parentheses after "missing".
  1800. // Tree printing:
  1801. // Qualifiers are printed next to each other, inside brackets, and
  1802. // separated by "!=". The printing order is:
  1803. // common qualifiers, highlighted from qualifiers, "!=",
  1804. // common qualifiers, highlighted to qualifiers
  1805. if (PrintTree) {
  1806. OS << "[";
  1807. if (CommonQual.empty() && FromQual.empty()) {
  1808. Bold();
  1809. OS << "(no qualifiers) ";
  1810. Unbold();
  1811. } else {
  1812. PrintQualifier(CommonQual, /*ApplyBold*/false);
  1813. PrintQualifier(FromQual, /*ApplyBold*/true);
  1814. }
  1815. OS << "!= ";
  1816. if (CommonQual.empty() && ToQual.empty()) {
  1817. Bold();
  1818. OS << "(no qualifiers)";
  1819. Unbold();
  1820. } else {
  1821. PrintQualifier(CommonQual, /*ApplyBold*/false,
  1822. /*appendSpaceIfNonEmpty*/!ToQual.empty());
  1823. PrintQualifier(ToQual, /*ApplyBold*/true,
  1824. /*appendSpaceIfNonEmpty*/false);
  1825. }
  1826. OS << "] ";
  1827. } else {
  1828. PrintQualifier(CommonQual, /*ApplyBold*/false);
  1829. PrintQualifier(FromQual, /*ApplyBold*/true);
  1830. }
  1831. }
  1832. void PrintQualifier(Qualifiers Q, bool ApplyBold,
  1833. bool AppendSpaceIfNonEmpty = true) {
  1834. if (Q.empty()) return;
  1835. if (ApplyBold) Bold();
  1836. Q.print(OS, Policy, AppendSpaceIfNonEmpty);
  1837. if (ApplyBold) Unbold();
  1838. }
  1839. public:
  1840. TemplateDiff(raw_ostream &OS, ASTContext &Context, QualType FromType,
  1841. QualType ToType, bool PrintTree, bool PrintFromType,
  1842. bool ElideType, bool ShowColor)
  1843. : Context(Context),
  1844. Policy(Context.getLangOpts()),
  1845. ElideType(ElideType),
  1846. PrintTree(PrintTree),
  1847. ShowColor(ShowColor),
  1848. // When printing a single type, the FromType is the one printed.
  1849. FromTemplateType(PrintFromType ? FromType : ToType),
  1850. ToTemplateType(PrintFromType ? ToType : FromType),
  1851. OS(OS),
  1852. IsBold(false) {
  1853. }
  1854. /// DiffTemplate - Start the template type diffing.
  1855. void DiffTemplate() {
  1856. Qualifiers FromQual = FromTemplateType.getQualifiers(),
  1857. ToQual = ToTemplateType.getQualifiers();
  1858. const TemplateSpecializationType *FromOrigTST =
  1859. GetTemplateSpecializationType(Context, FromTemplateType);
  1860. const TemplateSpecializationType *ToOrigTST =
  1861. GetTemplateSpecializationType(Context, ToTemplateType);
  1862. // Only checking templates.
  1863. if (!FromOrigTST || !ToOrigTST)
  1864. return;
  1865. // Different base templates.
  1866. if (!hasSameTemplate(FromOrigTST, ToOrigTST)) {
  1867. return;
  1868. }
  1869. FromQual -= QualType(FromOrigTST, 0).getQualifiers();
  1870. ToQual -= QualType(ToOrigTST, 0).getQualifiers();
  1871. // Same base template, but different arguments.
  1872. Tree.SetTemplateDiff(FromOrigTST->getTemplateName().getAsTemplateDecl(),
  1873. ToOrigTST->getTemplateName().getAsTemplateDecl(),
  1874. FromQual, ToQual, false /*FromDefault*/,
  1875. false /*ToDefault*/);
  1876. DiffTemplate(FromOrigTST, ToOrigTST);
  1877. }
  1878. /// Emit - When the two types given are templated types with the same
  1879. /// base template, a string representation of the type difference will be
  1880. /// emitted to the stream and return true. Otherwise, return false.
  1881. bool Emit() {
  1882. Tree.StartTraverse();
  1883. if (Tree.Empty())
  1884. return false;
  1885. TreeToString();
  1886. assert(!IsBold && "Bold is applied to end of string.");
  1887. return true;
  1888. }
  1889. }; // end class TemplateDiff
  1890. } // end anonymous namespace
  1891. /// FormatTemplateTypeDiff - A helper static function to start the template
  1892. /// diff and return the properly formatted string. Returns true if the diff
  1893. /// is successful.
  1894. static bool FormatTemplateTypeDiff(ASTContext &Context, QualType FromType,
  1895. QualType ToType, bool PrintTree,
  1896. bool PrintFromType, bool ElideType,
  1897. bool ShowColors, raw_ostream &OS) {
  1898. if (PrintTree)
  1899. PrintFromType = true;
  1900. TemplateDiff TD(OS, Context, FromType, ToType, PrintTree, PrintFromType,
  1901. ElideType, ShowColors);
  1902. TD.DiffTemplate();
  1903. return TD.Emit();
  1904. }