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