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