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... |
| 31 | if (isa<ElaboratedType>(Ty)) { |
| 32 | QT = cast<ElaboratedType>(Ty)->desugar(); |
| 33 | continue; |
| 34 | } |
John McCall | 3cb0ebd | 2010-03-10 03:28:59 +0000 | [diff] [blame] | 35 | |
Douglas Gregor | 79a9a34 | 2010-02-09 22:26:47 +0000 | [diff] [blame] | 36 | // ...or a substituted template type parameter. |
| 37 | if (isa<SubstTemplateTypeParmType>(Ty)) { |
| 38 | QT = cast<SubstTemplateTypeParmType>(Ty)->desugar(); |
| 39 | continue; |
| 40 | } |
Chandler Carruth | 1733bc3 | 2010-05-13 11:37:24 +0000 | [diff] [blame] | 41 | |
Douglas Gregor | 79a9a34 | 2010-02-09 22:26:47 +0000 | [diff] [blame] | 42 | // Don't desugar template specializations. |
| 43 | if (isa<TemplateSpecializationType>(Ty)) |
| 44 | break; |
Chandler Carruth | 1733bc3 | 2010-05-13 11:37:24 +0000 | [diff] [blame] | 45 | |
Douglas Gregor | 79a9a34 | 2010-02-09 22:26:47 +0000 | [diff] [blame] | 46 | // Don't desugar magic Objective-C types. |
| 47 | if (QualType(Ty,0) == Context.getObjCIdType() || |
| 48 | QualType(Ty,0) == Context.getObjCClassType() || |
| 49 | QualType(Ty,0) == Context.getObjCSelType() || |
| 50 | QualType(Ty,0) == Context.getObjCProtoType()) |
| 51 | break; |
Chandler Carruth | 1733bc3 | 2010-05-13 11:37:24 +0000 | [diff] [blame] | 52 | |
Douglas Gregor | 79a9a34 | 2010-02-09 22:26:47 +0000 | [diff] [blame] | 53 | // Don't desugar va_list. |
| 54 | if (QualType(Ty,0) == Context.getBuiltinVaListType()) |
| 55 | break; |
Chandler Carruth | 1733bc3 | 2010-05-13 11:37:24 +0000 | [diff] [blame] | 56 | |
Douglas Gregor | 79a9a34 | 2010-02-09 22:26:47 +0000 | [diff] [blame] | 57 | // Otherwise, do a single-step desugar. |
| 58 | QualType Underlying; |
| 59 | bool IsSugar = false; |
| 60 | switch (Ty->getTypeClass()) { |
| 61 | #define ABSTRACT_TYPE(Class, Base) |
| 62 | #define TYPE(Class, Base) \ |
| 63 | case Type::Class: { \ |
| 64 | const Class##Type *CTy = cast<Class##Type>(Ty); \ |
| 65 | if (CTy->isSugared()) { \ |
| 66 | IsSugar = true; \ |
| 67 | Underlying = CTy->desugar(); \ |
| 68 | } \ |
| 69 | break; \ |
| 70 | } |
| 71 | #include "clang/AST/TypeNodes.def" |
| 72 | } |
Chandler Carruth | 1733bc3 | 2010-05-13 11:37:24 +0000 | [diff] [blame] | 73 | |
Douglas Gregor | 79a9a34 | 2010-02-09 22:26:47 +0000 | [diff] [blame] | 74 | // If it wasn't sugared, we're done. |
| 75 | if (!IsSugar) |
| 76 | break; |
Chandler Carruth | 1733bc3 | 2010-05-13 11:37:24 +0000 | [diff] [blame] | 77 | |
Douglas Gregor | 79a9a34 | 2010-02-09 22:26:47 +0000 | [diff] [blame] | 78 | // If the desugared type is a vector type, we don't want to expand |
| 79 | // it, it will turn into an attribute mess. People want their "vec4". |
| 80 | if (isa<VectorType>(Underlying)) |
| 81 | break; |
Chandler Carruth | 1733bc3 | 2010-05-13 11:37:24 +0000 | [diff] [blame] | 82 | |
Douglas Gregor | 79a9a34 | 2010-02-09 22:26:47 +0000 | [diff] [blame] | 83 | // Don't desugar through the primary typedef of an anonymous type. |
| 84 | if (isa<TagType>(Underlying) && isa<TypedefType>(QT)) |
| 85 | if (cast<TagType>(Underlying)->getDecl()->getTypedefForAnonDecl() == |
| 86 | cast<TypedefType>(QT)->getDecl()) |
| 87 | break; |
Chandler Carruth | 1733bc3 | 2010-05-13 11:37:24 +0000 | [diff] [blame] | 88 | |
| 89 | // Record that we actually looked through an opaque type here. |
| 90 | ShouldAKA = true; |
Douglas Gregor | 79a9a34 | 2010-02-09 22:26:47 +0000 | [diff] [blame] | 91 | QT = Underlying; |
Douglas Gregor | 79a9a34 | 2010-02-09 22:26:47 +0000 | [diff] [blame] | 92 | } |
Chandler Carruth | 1733bc3 | 2010-05-13 11:37:24 +0000 | [diff] [blame] | 93 | |
| 94 | // If we have a pointer-like type, desugar the pointee as well. |
| 95 | // FIXME: Handle other pointer-like types. |
| 96 | if (const PointerType *Ty = QT->getAs<PointerType>()) { |
| 97 | QT = Context.getPointerType(Desugar(Context, Ty->getPointeeType(), |
| 98 | ShouldAKA)); |
| 99 | } else if (const LValueReferenceType *Ty = QT->getAs<LValueReferenceType>()) { |
| 100 | QT = Context.getLValueReferenceType(Desugar(Context, Ty->getPointeeType(), |
| 101 | ShouldAKA)); |
Douglas Gregor | 79a9a34 | 2010-02-09 22:26:47 +0000 | [diff] [blame] | 102 | } |
Chandler Carruth | 1733bc3 | 2010-05-13 11:37:24 +0000 | [diff] [blame] | 103 | |
| 104 | return QC.apply(QT); |
Douglas Gregor | 79a9a34 | 2010-02-09 22:26:47 +0000 | [diff] [blame] | 105 | } |
| 106 | |
| 107 | /// \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] | 108 | /// a diagnostic. |
| 109 | /// |
| 110 | /// There are three main criteria when determining whether we should have an |
| 111 | /// a.k.a. clause when pretty-printing a type: |
| 112 | /// |
| 113 | /// 1) Some types provide very minimal sugar that doesn't impede the |
| 114 | /// user's understanding --- for example, elaborated type |
| 115 | /// specifiers. If this is all the sugar we see, we don't want an |
| 116 | /// a.k.a. clause. |
| 117 | /// 2) Some types are technically sugared but are much more familiar |
| 118 | /// when seen in their sugared form --- for example, va_list, |
| 119 | /// vector types, and the magic Objective C types. We don't |
| 120 | /// want to desugar these, even if we do produce an a.k.a. clause. |
| 121 | /// 3) Some types may have already been desugared previously in this diagnostic. |
| 122 | /// if this is the case, doing another "aka" would just be clutter. |
Douglas Gregor | 79a9a34 | 2010-02-09 22:26:47 +0000 | [diff] [blame] | 123 | /// |
| 124 | /// \param Context the context in which the type was allocated |
| 125 | /// \param Ty the type to print |
| 126 | static std::string |
| 127 | ConvertTypeToDiagnosticString(ASTContext &Context, QualType Ty, |
| 128 | const Diagnostic::ArgumentValue *PrevArgs, |
| 129 | unsigned NumPrevArgs) { |
| 130 | // FIXME: Playing with std::string is really slow. |
| 131 | std::string S = Ty.getAsString(Context.PrintingPolicy); |
Chandler Carruth | 1733bc3 | 2010-05-13 11:37:24 +0000 | [diff] [blame] | 132 | |
| 133 | // Check to see if we already desugared this type in this |
| 134 | // diagnostic. If so, don't do it again. |
| 135 | bool Repeated = false; |
| 136 | for (unsigned i = 0; i != NumPrevArgs; ++i) { |
| 137 | // TODO: Handle ak_declcontext case. |
| 138 | if (PrevArgs[i].first == Diagnostic::ak_qualtype) { |
| 139 | void *Ptr = (void*)PrevArgs[i].second; |
| 140 | QualType PrevTy(QualType::getFromOpaquePtr(Ptr)); |
| 141 | if (PrevTy == Ty) { |
| 142 | Repeated = true; |
| 143 | break; |
| 144 | } |
| 145 | } |
| 146 | } |
| 147 | |
Douglas Gregor | 79a9a34 | 2010-02-09 22:26:47 +0000 | [diff] [blame] | 148 | // Consider producing an a.k.a. clause if removing all the direct |
| 149 | // sugar gives us something "significantly different". |
Chandler Carruth | 1733bc3 | 2010-05-13 11:37:24 +0000 | [diff] [blame] | 150 | if (!Repeated) { |
| 151 | bool ShouldAKA = false; |
| 152 | QualType DesugaredTy = Desugar(Context, Ty, ShouldAKA); |
| 153 | if (ShouldAKA) { |
| 154 | S = "'"+S+"' (aka '"; |
| 155 | S += DesugaredTy.getAsString(Context.PrintingPolicy); |
| 156 | S += "')"; |
| 157 | return S; |
| 158 | } |
Douglas Gregor | 79a9a34 | 2010-02-09 22:26:47 +0000 | [diff] [blame] | 159 | } |
Chandler Carruth | 1733bc3 | 2010-05-13 11:37:24 +0000 | [diff] [blame] | 160 | |
Douglas Gregor | 79a9a34 | 2010-02-09 22:26:47 +0000 | [diff] [blame] | 161 | S = "'" + S + "'"; |
| 162 | return S; |
| 163 | } |
| 164 | |
| 165 | void clang::FormatASTNodeDiagnosticArgument(Diagnostic::ArgumentKind Kind, |
| 166 | intptr_t Val, |
| 167 | const char *Modifier, |
| 168 | unsigned ModLen, |
| 169 | const char *Argument, |
| 170 | unsigned ArgLen, |
| 171 | const Diagnostic::ArgumentValue *PrevArgs, |
| 172 | unsigned NumPrevArgs, |
| 173 | llvm::SmallVectorImpl<char> &Output, |
| 174 | void *Cookie) { |
| 175 | ASTContext &Context = *static_cast<ASTContext*>(Cookie); |
| 176 | |
| 177 | std::string S; |
| 178 | bool NeedQuotes = true; |
| 179 | |
| 180 | switch (Kind) { |
| 181 | default: assert(0 && "unknown ArgumentKind"); |
| 182 | case Diagnostic::ak_qualtype: { |
| 183 | assert(ModLen == 0 && ArgLen == 0 && |
| 184 | "Invalid modifier for QualType argument"); |
| 185 | |
| 186 | QualType Ty(QualType::getFromOpaquePtr(reinterpret_cast<void*>(Val))); |
| 187 | S = ConvertTypeToDiagnosticString(Context, Ty, PrevArgs, NumPrevArgs); |
| 188 | NeedQuotes = false; |
| 189 | break; |
| 190 | } |
| 191 | case Diagnostic::ak_declarationname: { |
| 192 | DeclarationName N = DeclarationName::getFromOpaqueInteger(Val); |
| 193 | S = N.getAsString(); |
| 194 | |
| 195 | if (ModLen == 9 && !memcmp(Modifier, "objcclass", 9) && ArgLen == 0) |
| 196 | S = '+' + S; |
| 197 | else if (ModLen == 12 && !memcmp(Modifier, "objcinstance", 12) |
| 198 | && ArgLen==0) |
| 199 | S = '-' + S; |
| 200 | else |
| 201 | assert(ModLen == 0 && ArgLen == 0 && |
| 202 | "Invalid modifier for DeclarationName argument"); |
| 203 | break; |
| 204 | } |
| 205 | case Diagnostic::ak_nameddecl: { |
| 206 | bool Qualified; |
| 207 | if (ModLen == 1 && Modifier[0] == 'q' && ArgLen == 0) |
| 208 | Qualified = true; |
| 209 | else { |
| 210 | assert(ModLen == 0 && ArgLen == 0 && |
| 211 | "Invalid modifier for NamedDecl* argument"); |
| 212 | Qualified = false; |
| 213 | } |
| 214 | reinterpret_cast<NamedDecl*>(Val)-> |
| 215 | getNameForDiagnostic(S, Context.PrintingPolicy, Qualified); |
| 216 | break; |
| 217 | } |
| 218 | case Diagnostic::ak_nestednamespec: { |
| 219 | llvm::raw_string_ostream OS(S); |
| 220 | reinterpret_cast<NestedNameSpecifier*>(Val)->print(OS, |
| 221 | Context.PrintingPolicy); |
| 222 | NeedQuotes = false; |
| 223 | break; |
| 224 | } |
| 225 | case Diagnostic::ak_declcontext: { |
| 226 | DeclContext *DC = reinterpret_cast<DeclContext *> (Val); |
| 227 | assert(DC && "Should never have a null declaration context"); |
| 228 | |
| 229 | if (DC->isTranslationUnit()) { |
| 230 | // FIXME: Get these strings from some localized place |
| 231 | if (Context.getLangOptions().CPlusPlus) |
| 232 | S = "the global namespace"; |
| 233 | else |
| 234 | S = "the global scope"; |
| 235 | } else if (TypeDecl *Type = dyn_cast<TypeDecl>(DC)) { |
| 236 | S = ConvertTypeToDiagnosticString(Context, |
| 237 | Context.getTypeDeclType(Type), |
| 238 | PrevArgs, NumPrevArgs); |
| 239 | } else { |
| 240 | // FIXME: Get these strings from some localized place |
| 241 | NamedDecl *ND = cast<NamedDecl>(DC); |
| 242 | if (isa<NamespaceDecl>(ND)) |
| 243 | S += "namespace "; |
| 244 | else if (isa<ObjCMethodDecl>(ND)) |
| 245 | S += "method "; |
| 246 | else if (isa<FunctionDecl>(ND)) |
| 247 | S += "function "; |
| 248 | |
| 249 | S += "'"; |
| 250 | ND->getNameForDiagnostic(S, Context.PrintingPolicy, true); |
| 251 | S += "'"; |
| 252 | } |
| 253 | NeedQuotes = false; |
| 254 | break; |
| 255 | } |
| 256 | } |
| 257 | |
| 258 | if (NeedQuotes) |
| 259 | Output.push_back('\''); |
| 260 | |
| 261 | Output.append(S.begin(), S.end()); |
| 262 | |
| 263 | if (NeedQuotes) |
| 264 | Output.push_back('\''); |
| 265 | } |