Douglas Gregor | 79a9a34 | 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" |
| 14 | |
| 15 | #include "clang/AST/ASTContext.h" |
| 16 | #include "clang/AST/DeclObjC.h" |
| 17 | #include "clang/AST/Type.h" |
| 18 | #include "llvm/Support/raw_ostream.h" |
| 19 | |
| 20 | using namespace clang; |
| 21 | |
Chandler Carruth | 1733bc3 | 2010-05-13 11:37:24 +0000 | [diff] [blame] | 22 | // Returns a desugared version of the QualType, and marks ShouldAKA as true |
| 23 | // whenever we remove significant sugar from the type. |
| 24 | static QualType Desugar(ASTContext &Context, QualType QT, bool &ShouldAKA) { |
| 25 | QualifierCollector QC; |
| 26 | |
Douglas Gregor | 79a9a34 | 2010-02-09 22:26:47 +0000 | [diff] [blame] | 27 | while (true) { |
Chandler Carruth | 1733bc3 | 2010-05-13 11:37:24 +0000 | [diff] [blame] | 28 | const Type *Ty = QC.strip(QT); |
| 29 | |
Douglas Gregor | 79a9a34 | 2010-02-09 22:26:47 +0000 | [diff] [blame] | 30 | // Don't aka just because we saw an elaborated type... |
Richard Smith | 34b41d9 | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 31 | if (const ElaboratedType *ET = dyn_cast<ElaboratedType>(Ty)) { |
| 32 | QT = ET->desugar(); |
Douglas Gregor | 79a9a34 | 2010-02-09 22:26:47 +0000 | [diff] [blame] | 33 | continue; |
| 34 | } |
Abramo Bagnara | 075f8f1 | 2010-12-10 16:29:40 +0000 | [diff] [blame] | 35 | // ... or a paren type ... |
Richard Smith | 34b41d9 | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 36 | if (const ParenType *PT = dyn_cast<ParenType>(Ty)) { |
| 37 | QT = PT->desugar(); |
Abramo Bagnara | 075f8f1 | 2010-12-10 16:29:40 +0000 | [diff] [blame] | 38 | continue; |
| 39 | } |
Richard Smith | 34b41d9 | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 40 | // ...or a substituted template type parameter ... |
| 41 | if (const SubstTemplateTypeParmType *ST = |
| 42 | dyn_cast<SubstTemplateTypeParmType>(Ty)) { |
| 43 | QT = ST->desugar(); |
| 44 | continue; |
| 45 | } |
John McCall | 14aa217 | 2011-03-04 04:00:19 +0000 | [diff] [blame] | 46 | // ...or an attributed type... |
| 47 | if (const AttributedType *AT = dyn_cast<AttributedType>(Ty)) { |
| 48 | QT = AT->desugar(); |
| 49 | continue; |
| 50 | } |
Richard Smith | 34b41d9 | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 51 | // ... or an auto type. |
| 52 | if (const AutoType *AT = dyn_cast<AutoType>(Ty)) { |
| 53 | if (!AT->isSugared()) |
| 54 | break; |
| 55 | QT = AT->desugar(); |
Douglas Gregor | 79a9a34 | 2010-02-09 22:26:47 +0000 | [diff] [blame] | 56 | continue; |
| 57 | } |
Chandler Carruth | 1733bc3 | 2010-05-13 11:37:24 +0000 | [diff] [blame] | 58 | |
Richard Smith | 3e4c6c4 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 59 | // Don't desugar template specializations, unless it's an alias template. |
| 60 | if (const TemplateSpecializationType *TST |
| 61 | = dyn_cast<TemplateSpecializationType>(Ty)) |
| 62 | if (!TST->isTypeAlias()) |
| 63 | break; |
Chandler Carruth | 1733bc3 | 2010-05-13 11:37:24 +0000 | [diff] [blame] | 64 | |
Douglas Gregor | 79a9a34 | 2010-02-09 22:26:47 +0000 | [diff] [blame] | 65 | // Don't desugar magic Objective-C types. |
| 66 | if (QualType(Ty,0) == Context.getObjCIdType() || |
| 67 | QualType(Ty,0) == Context.getObjCClassType() || |
| 68 | QualType(Ty,0) == Context.getObjCSelType() || |
| 69 | QualType(Ty,0) == Context.getObjCProtoType()) |
| 70 | break; |
Chandler Carruth | 1733bc3 | 2010-05-13 11:37:24 +0000 | [diff] [blame] | 71 | |
Douglas Gregor | 79a9a34 | 2010-02-09 22:26:47 +0000 | [diff] [blame] | 72 | // Don't desugar va_list. |
| 73 | if (QualType(Ty,0) == Context.getBuiltinVaListType()) |
| 74 | break; |
Chandler Carruth | 1733bc3 | 2010-05-13 11:37:24 +0000 | [diff] [blame] | 75 | |
Douglas Gregor | 79a9a34 | 2010-02-09 22:26:47 +0000 | [diff] [blame] | 76 | // Otherwise, do a single-step desugar. |
| 77 | QualType Underlying; |
| 78 | bool IsSugar = false; |
| 79 | switch (Ty->getTypeClass()) { |
| 80 | #define ABSTRACT_TYPE(Class, Base) |
| 81 | #define TYPE(Class, Base) \ |
| 82 | case Type::Class: { \ |
| 83 | const Class##Type *CTy = cast<Class##Type>(Ty); \ |
| 84 | if (CTy->isSugared()) { \ |
| 85 | IsSugar = true; \ |
| 86 | Underlying = CTy->desugar(); \ |
| 87 | } \ |
| 88 | break; \ |
| 89 | } |
| 90 | #include "clang/AST/TypeNodes.def" |
| 91 | } |
Chandler Carruth | 1733bc3 | 2010-05-13 11:37:24 +0000 | [diff] [blame] | 92 | |
Douglas Gregor | 79a9a34 | 2010-02-09 22:26:47 +0000 | [diff] [blame] | 93 | // If it wasn't sugared, we're done. |
| 94 | if (!IsSugar) |
| 95 | break; |
Chandler Carruth | 1733bc3 | 2010-05-13 11:37:24 +0000 | [diff] [blame] | 96 | |
Douglas Gregor | 79a9a34 | 2010-02-09 22:26:47 +0000 | [diff] [blame] | 97 | // If the desugared type is a vector type, we don't want to expand |
| 98 | // it, it will turn into an attribute mess. People want their "vec4". |
| 99 | if (isa<VectorType>(Underlying)) |
| 100 | break; |
Chandler Carruth | 1733bc3 | 2010-05-13 11:37:24 +0000 | [diff] [blame] | 101 | |
Douglas Gregor | 79a9a34 | 2010-02-09 22:26:47 +0000 | [diff] [blame] | 102 | // Don't desugar through the primary typedef of an anonymous type. |
Chris Lattner | c3f8c07 | 2010-09-04 23:16:01 +0000 | [diff] [blame] | 103 | if (const TagType *UTT = Underlying->getAs<TagType>()) |
| 104 | if (const TypedefType *QTT = dyn_cast<TypedefType>(QT)) |
Richard Smith | 162e1c1 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 105 | if (UTT->getDecl()->getTypedefNameForAnonDecl() == QTT->getDecl()) |
Chris Lattner | c3f8c07 | 2010-09-04 23:16:01 +0000 | [diff] [blame] | 106 | break; |
Chandler Carruth | 1733bc3 | 2010-05-13 11:37:24 +0000 | [diff] [blame] | 107 | |
| 108 | // Record that we actually looked through an opaque type here. |
| 109 | ShouldAKA = true; |
Douglas Gregor | 79a9a34 | 2010-02-09 22:26:47 +0000 | [diff] [blame] | 110 | QT = Underlying; |
Douglas Gregor | 79a9a34 | 2010-02-09 22:26:47 +0000 | [diff] [blame] | 111 | } |
Chandler Carruth | 1733bc3 | 2010-05-13 11:37:24 +0000 | [diff] [blame] | 112 | |
| 113 | // If we have a pointer-like type, desugar the pointee as well. |
| 114 | // FIXME: Handle other pointer-like types. |
| 115 | if (const PointerType *Ty = QT->getAs<PointerType>()) { |
Chris Lattner | c3f8c07 | 2010-09-04 23:16:01 +0000 | [diff] [blame] | 116 | QT = Context.getPointerType(Desugar(Context, Ty->getPointeeType(), |
| 117 | ShouldAKA)); |
Chandler Carruth | 1733bc3 | 2010-05-13 11:37:24 +0000 | [diff] [blame] | 118 | } else if (const LValueReferenceType *Ty = QT->getAs<LValueReferenceType>()) { |
Chris Lattner | c3f8c07 | 2010-09-04 23:16:01 +0000 | [diff] [blame] | 119 | QT = Context.getLValueReferenceType(Desugar(Context, Ty->getPointeeType(), |
| 120 | ShouldAKA)); |
Douglas Gregor | 69d8316 | 2011-01-20 16:08:06 +0000 | [diff] [blame] | 121 | } else if (const RValueReferenceType *Ty = QT->getAs<RValueReferenceType>()) { |
| 122 | QT = Context.getRValueReferenceType(Desugar(Context, Ty->getPointeeType(), |
| 123 | ShouldAKA)); |
Douglas Gregor | 79a9a34 | 2010-02-09 22:26:47 +0000 | [diff] [blame] | 124 | } |
Chandler Carruth | 1733bc3 | 2010-05-13 11:37:24 +0000 | [diff] [blame] | 125 | |
John McCall | 49f4e1c | 2010-12-10 11:01:00 +0000 | [diff] [blame] | 126 | return QC.apply(Context, QT); |
Douglas Gregor | 79a9a34 | 2010-02-09 22:26:47 +0000 | [diff] [blame] | 127 | } |
| 128 | |
| 129 | /// \brief Convert the given type to a string suitable for printing as part of |
Chandler Carruth | 1733bc3 | 2010-05-13 11:37:24 +0000 | [diff] [blame] | 130 | /// a diagnostic. |
| 131 | /// |
Chandler Carruth | 0673cb3 | 2011-07-11 17:49:21 +0000 | [diff] [blame] | 132 | /// There are four main criteria when determining whether we should have an |
Chandler Carruth | 1733bc3 | 2010-05-13 11:37:24 +0000 | [diff] [blame] | 133 | /// a.k.a. clause when pretty-printing a type: |
| 134 | /// |
| 135 | /// 1) Some types provide very minimal sugar that doesn't impede the |
| 136 | /// user's understanding --- for example, elaborated type |
| 137 | /// specifiers. If this is all the sugar we see, we don't want an |
| 138 | /// a.k.a. clause. |
| 139 | /// 2) Some types are technically sugared but are much more familiar |
| 140 | /// when seen in their sugared form --- for example, va_list, |
| 141 | /// vector types, and the magic Objective C types. We don't |
| 142 | /// want to desugar these, even if we do produce an a.k.a. clause. |
| 143 | /// 3) Some types may have already been desugared previously in this diagnostic. |
| 144 | /// if this is the case, doing another "aka" would just be clutter. |
Chandler Carruth | 0673cb3 | 2011-07-11 17:49:21 +0000 | [diff] [blame] | 145 | /// 4) Two different types within the same diagnostic have the same output |
| 146 | /// string. In this case, force an a.k.a with the desugared type when |
| 147 | /// doing so will provide additional information. |
Douglas Gregor | 79a9a34 | 2010-02-09 22:26:47 +0000 | [diff] [blame] | 148 | /// |
| 149 | /// \param Context the context in which the type was allocated |
| 150 | /// \param Ty the type to print |
Chandler Carruth | 0673cb3 | 2011-07-11 17:49:21 +0000 | [diff] [blame] | 151 | /// \param QualTypeVals pointer values to QualTypes which are used in the |
| 152 | /// diagnostic message |
Douglas Gregor | 79a9a34 | 2010-02-09 22:26:47 +0000 | [diff] [blame] | 153 | static std::string |
| 154 | ConvertTypeToDiagnosticString(ASTContext &Context, QualType Ty, |
David Blaikie | d6471f7 | 2011-09-25 23:23:43 +0000 | [diff] [blame] | 155 | const DiagnosticsEngine::ArgumentValue *PrevArgs, |
Chandler Carruth | 0673cb3 | 2011-07-11 17:49:21 +0000 | [diff] [blame] | 156 | unsigned NumPrevArgs, |
Bill Wendling | 341785e | 2012-02-22 09:51:33 +0000 | [diff] [blame] | 157 | ArrayRef<intptr_t> QualTypeVals) { |
Douglas Gregor | 79a9a34 | 2010-02-09 22:26:47 +0000 | [diff] [blame] | 158 | // FIXME: Playing with std::string is really slow. |
Chandler Carruth | 0673cb3 | 2011-07-11 17:49:21 +0000 | [diff] [blame] | 159 | bool ForceAKA = false; |
| 160 | QualType CanTy = Ty.getCanonicalType(); |
Douglas Gregor | 30c4240 | 2011-09-27 22:38:19 +0000 | [diff] [blame] | 161 | std::string S = Ty.getAsString(Context.getPrintingPolicy()); |
| 162 | std::string CanS = CanTy.getAsString(Context.getPrintingPolicy()); |
Chandler Carruth | 0673cb3 | 2011-07-11 17:49:21 +0000 | [diff] [blame] | 163 | |
Bill Wendling | 341785e | 2012-02-22 09:51:33 +0000 | [diff] [blame] | 164 | for (unsigned I = 0, E = QualTypeVals.size(); I != E; ++I) { |
Chandler Carruth | 0673cb3 | 2011-07-11 17:49:21 +0000 | [diff] [blame] | 165 | QualType CompareTy = |
Bill Wendling | 341785e | 2012-02-22 09:51:33 +0000 | [diff] [blame] | 166 | QualType::getFromOpaquePtr(reinterpret_cast<void*>(QualTypeVals[I])); |
Richard Smith | 36f5cfe | 2012-03-09 08:00:36 +0000 | [diff] [blame^] | 167 | if (CompareTy.isNull()) |
| 168 | continue; |
Chandler Carruth | 0673cb3 | 2011-07-11 17:49:21 +0000 | [diff] [blame] | 169 | if (CompareTy == Ty) |
| 170 | continue; // Same types |
| 171 | QualType CompareCanTy = CompareTy.getCanonicalType(); |
| 172 | if (CompareCanTy == CanTy) |
| 173 | continue; // Same canonical types |
Douglas Gregor | 30c4240 | 2011-09-27 22:38:19 +0000 | [diff] [blame] | 174 | std::string CompareS = CompareTy.getAsString(Context.getPrintingPolicy()); |
Richard Trieu | ecb912e | 2011-11-14 19:39:25 +0000 | [diff] [blame] | 175 | bool aka; |
| 176 | QualType CompareDesugar = Desugar(Context, CompareTy, aka); |
| 177 | std::string CompareDesugarStr = |
| 178 | CompareDesugar.getAsString(Context.getPrintingPolicy()); |
| 179 | if (CompareS != S && CompareDesugarStr != S) |
| 180 | continue; // The type string is different than the comparison string |
| 181 | // and the desugared comparison string. |
| 182 | std::string CompareCanS = |
| 183 | CompareCanTy.getAsString(Context.getPrintingPolicy()); |
| 184 | |
Chandler Carruth | 0673cb3 | 2011-07-11 17:49:21 +0000 | [diff] [blame] | 185 | if (CompareCanS == CanS) |
| 186 | continue; // No new info from canonical type |
| 187 | |
| 188 | ForceAKA = true; |
| 189 | break; |
| 190 | } |
Chandler Carruth | 1733bc3 | 2010-05-13 11:37:24 +0000 | [diff] [blame] | 191 | |
| 192 | // Check to see if we already desugared this type in this |
| 193 | // diagnostic. If so, don't do it again. |
| 194 | bool Repeated = false; |
| 195 | for (unsigned i = 0; i != NumPrevArgs; ++i) { |
| 196 | // TODO: Handle ak_declcontext case. |
David Blaikie | d6471f7 | 2011-09-25 23:23:43 +0000 | [diff] [blame] | 197 | if (PrevArgs[i].first == DiagnosticsEngine::ak_qualtype) { |
Chandler Carruth | 1733bc3 | 2010-05-13 11:37:24 +0000 | [diff] [blame] | 198 | void *Ptr = (void*)PrevArgs[i].second; |
| 199 | QualType PrevTy(QualType::getFromOpaquePtr(Ptr)); |
| 200 | if (PrevTy == Ty) { |
| 201 | Repeated = true; |
| 202 | break; |
| 203 | } |
| 204 | } |
| 205 | } |
| 206 | |
Douglas Gregor | 79a9a34 | 2010-02-09 22:26:47 +0000 | [diff] [blame] | 207 | // Consider producing an a.k.a. clause if removing all the direct |
| 208 | // sugar gives us something "significantly different". |
Chandler Carruth | 1733bc3 | 2010-05-13 11:37:24 +0000 | [diff] [blame] | 209 | if (!Repeated) { |
| 210 | bool ShouldAKA = false; |
| 211 | QualType DesugaredTy = Desugar(Context, Ty, ShouldAKA); |
Chandler Carruth | 0673cb3 | 2011-07-11 17:49:21 +0000 | [diff] [blame] | 212 | if (ShouldAKA || ForceAKA) { |
| 213 | if (DesugaredTy == Ty) { |
| 214 | DesugaredTy = Ty.getCanonicalType(); |
| 215 | } |
Douglas Gregor | 30c4240 | 2011-09-27 22:38:19 +0000 | [diff] [blame] | 216 | std::string akaStr = DesugaredTy.getAsString(Context.getPrintingPolicy()); |
Chandler Carruth | 0673cb3 | 2011-07-11 17:49:21 +0000 | [diff] [blame] | 217 | if (akaStr != S) { |
| 218 | S = "'" + S + "' (aka '" + akaStr + "')"; |
| 219 | return S; |
| 220 | } |
Chandler Carruth | 1733bc3 | 2010-05-13 11:37:24 +0000 | [diff] [blame] | 221 | } |
Douglas Gregor | 79a9a34 | 2010-02-09 22:26:47 +0000 | [diff] [blame] | 222 | } |
Chandler Carruth | 1733bc3 | 2010-05-13 11:37:24 +0000 | [diff] [blame] | 223 | |
Douglas Gregor | 79a9a34 | 2010-02-09 22:26:47 +0000 | [diff] [blame] | 224 | S = "'" + S + "'"; |
| 225 | return S; |
| 226 | } |
| 227 | |
Chandler Carruth | 0673cb3 | 2011-07-11 17:49:21 +0000 | [diff] [blame] | 228 | void clang::FormatASTNodeDiagnosticArgument( |
David Blaikie | d6471f7 | 2011-09-25 23:23:43 +0000 | [diff] [blame] | 229 | DiagnosticsEngine::ArgumentKind Kind, |
Chandler Carruth | 0673cb3 | 2011-07-11 17:49:21 +0000 | [diff] [blame] | 230 | intptr_t Val, |
| 231 | const char *Modifier, |
| 232 | unsigned ModLen, |
| 233 | const char *Argument, |
| 234 | unsigned ArgLen, |
David Blaikie | d6471f7 | 2011-09-25 23:23:43 +0000 | [diff] [blame] | 235 | const DiagnosticsEngine::ArgumentValue *PrevArgs, |
Chandler Carruth | 0673cb3 | 2011-07-11 17:49:21 +0000 | [diff] [blame] | 236 | unsigned NumPrevArgs, |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 237 | SmallVectorImpl<char> &Output, |
Chandler Carruth | 0673cb3 | 2011-07-11 17:49:21 +0000 | [diff] [blame] | 238 | void *Cookie, |
Bill Wendling | 341785e | 2012-02-22 09:51:33 +0000 | [diff] [blame] | 239 | ArrayRef<intptr_t> QualTypeVals) { |
Douglas Gregor | 79a9a34 | 2010-02-09 22:26:47 +0000 | [diff] [blame] | 240 | ASTContext &Context = *static_cast<ASTContext*>(Cookie); |
| 241 | |
| 242 | std::string S; |
| 243 | bool NeedQuotes = true; |
| 244 | |
| 245 | switch (Kind) { |
David Blaikie | b219cfc | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 246 | default: llvm_unreachable("unknown ArgumentKind"); |
David Blaikie | d6471f7 | 2011-09-25 23:23:43 +0000 | [diff] [blame] | 247 | case DiagnosticsEngine::ak_qualtype: { |
Douglas Gregor | 79a9a34 | 2010-02-09 22:26:47 +0000 | [diff] [blame] | 248 | assert(ModLen == 0 && ArgLen == 0 && |
| 249 | "Invalid modifier for QualType argument"); |
| 250 | |
| 251 | QualType Ty(QualType::getFromOpaquePtr(reinterpret_cast<void*>(Val))); |
Chandler Carruth | 0673cb3 | 2011-07-11 17:49:21 +0000 | [diff] [blame] | 252 | S = ConvertTypeToDiagnosticString(Context, Ty, PrevArgs, NumPrevArgs, |
| 253 | QualTypeVals); |
Douglas Gregor | 79a9a34 | 2010-02-09 22:26:47 +0000 | [diff] [blame] | 254 | NeedQuotes = false; |
| 255 | break; |
| 256 | } |
David Blaikie | d6471f7 | 2011-09-25 23:23:43 +0000 | [diff] [blame] | 257 | case DiagnosticsEngine::ak_declarationname: { |
Douglas Gregor | 79a9a34 | 2010-02-09 22:26:47 +0000 | [diff] [blame] | 258 | DeclarationName N = DeclarationName::getFromOpaqueInteger(Val); |
| 259 | S = N.getAsString(); |
| 260 | |
| 261 | if (ModLen == 9 && !memcmp(Modifier, "objcclass", 9) && ArgLen == 0) |
| 262 | S = '+' + S; |
| 263 | else if (ModLen == 12 && !memcmp(Modifier, "objcinstance", 12) |
| 264 | && ArgLen==0) |
| 265 | S = '-' + S; |
| 266 | else |
| 267 | assert(ModLen == 0 && ArgLen == 0 && |
| 268 | "Invalid modifier for DeclarationName argument"); |
| 269 | break; |
| 270 | } |
David Blaikie | d6471f7 | 2011-09-25 23:23:43 +0000 | [diff] [blame] | 271 | case DiagnosticsEngine::ak_nameddecl: { |
Douglas Gregor | 79a9a34 | 2010-02-09 22:26:47 +0000 | [diff] [blame] | 272 | bool Qualified; |
| 273 | if (ModLen == 1 && Modifier[0] == 'q' && ArgLen == 0) |
| 274 | Qualified = true; |
| 275 | else { |
| 276 | assert(ModLen == 0 && ArgLen == 0 && |
| 277 | "Invalid modifier for NamedDecl* argument"); |
| 278 | Qualified = false; |
| 279 | } |
Chandler Carruth | b0656ec | 2011-08-31 09:01:53 +0000 | [diff] [blame] | 280 | const NamedDecl *ND = reinterpret_cast<const NamedDecl*>(Val); |
Douglas Gregor | 30c4240 | 2011-09-27 22:38:19 +0000 | [diff] [blame] | 281 | ND->getNameForDiagnostic(S, Context.getPrintingPolicy(), Qualified); |
Douglas Gregor | 79a9a34 | 2010-02-09 22:26:47 +0000 | [diff] [blame] | 282 | break; |
| 283 | } |
David Blaikie | d6471f7 | 2011-09-25 23:23:43 +0000 | [diff] [blame] | 284 | case DiagnosticsEngine::ak_nestednamespec: { |
Douglas Gregor | 79a9a34 | 2010-02-09 22:26:47 +0000 | [diff] [blame] | 285 | llvm::raw_string_ostream OS(S); |
| 286 | reinterpret_cast<NestedNameSpecifier*>(Val)->print(OS, |
Douglas Gregor | 30c4240 | 2011-09-27 22:38:19 +0000 | [diff] [blame] | 287 | Context.getPrintingPolicy()); |
Douglas Gregor | 79a9a34 | 2010-02-09 22:26:47 +0000 | [diff] [blame] | 288 | NeedQuotes = false; |
| 289 | break; |
| 290 | } |
David Blaikie | d6471f7 | 2011-09-25 23:23:43 +0000 | [diff] [blame] | 291 | case DiagnosticsEngine::ak_declcontext: { |
Douglas Gregor | 79a9a34 | 2010-02-09 22:26:47 +0000 | [diff] [blame] | 292 | DeclContext *DC = reinterpret_cast<DeclContext *> (Val); |
| 293 | assert(DC && "Should never have a null declaration context"); |
| 294 | |
| 295 | if (DC->isTranslationUnit()) { |
| 296 | // FIXME: Get these strings from some localized place |
| 297 | if (Context.getLangOptions().CPlusPlus) |
| 298 | S = "the global namespace"; |
| 299 | else |
| 300 | S = "the global scope"; |
| 301 | } else if (TypeDecl *Type = dyn_cast<TypeDecl>(DC)) { |
| 302 | S = ConvertTypeToDiagnosticString(Context, |
| 303 | Context.getTypeDeclType(Type), |
Chandler Carruth | 0673cb3 | 2011-07-11 17:49:21 +0000 | [diff] [blame] | 304 | PrevArgs, NumPrevArgs, QualTypeVals); |
Douglas Gregor | 79a9a34 | 2010-02-09 22:26:47 +0000 | [diff] [blame] | 305 | } else { |
| 306 | // FIXME: Get these strings from some localized place |
| 307 | NamedDecl *ND = cast<NamedDecl>(DC); |
| 308 | if (isa<NamespaceDecl>(ND)) |
| 309 | S += "namespace "; |
| 310 | else if (isa<ObjCMethodDecl>(ND)) |
| 311 | S += "method "; |
| 312 | else if (isa<FunctionDecl>(ND)) |
| 313 | S += "function "; |
| 314 | |
| 315 | S += "'"; |
Douglas Gregor | 30c4240 | 2011-09-27 22:38:19 +0000 | [diff] [blame] | 316 | ND->getNameForDiagnostic(S, Context.getPrintingPolicy(), true); |
Douglas Gregor | 79a9a34 | 2010-02-09 22:26:47 +0000 | [diff] [blame] | 317 | S += "'"; |
| 318 | } |
| 319 | NeedQuotes = false; |
| 320 | break; |
| 321 | } |
| 322 | } |
| 323 | |
| 324 | if (NeedQuotes) |
| 325 | Output.push_back('\''); |
| 326 | |
| 327 | Output.append(S.begin(), S.end()); |
| 328 | |
| 329 | if (NeedQuotes) |
| 330 | Output.push_back('\''); |
| 331 | } |