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