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