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