ASTDiagnostic.cpp revision 14aa2175416f79ef17811282afbf425f87d54ebf
179a9a3417929e340e84dcbc06ed9c3a277cad959Douglas Gregor//===--- ASTDiagnostic.cpp - Diagnostic Printing Hooks for AST Nodes ------===// 279a9a3417929e340e84dcbc06ed9c3a277cad959Douglas Gregor// 379a9a3417929e340e84dcbc06ed9c3a277cad959Douglas Gregor// The LLVM Compiler Infrastructure 479a9a3417929e340e84dcbc06ed9c3a277cad959Douglas Gregor// 579a9a3417929e340e84dcbc06ed9c3a277cad959Douglas Gregor// This file is distributed under the University of Illinois Open Source 679a9a3417929e340e84dcbc06ed9c3a277cad959Douglas Gregor// License. See LICENSE.TXT for details. 779a9a3417929e340e84dcbc06ed9c3a277cad959Douglas Gregor// 879a9a3417929e340e84dcbc06ed9c3a277cad959Douglas Gregor//===----------------------------------------------------------------------===// 979a9a3417929e340e84dcbc06ed9c3a277cad959Douglas Gregor// 1079a9a3417929e340e84dcbc06ed9c3a277cad959Douglas Gregor// This file implements a diagnostic formatting hook for AST elements. 1179a9a3417929e340e84dcbc06ed9c3a277cad959Douglas Gregor// 1279a9a3417929e340e84dcbc06ed9c3a277cad959Douglas Gregor//===----------------------------------------------------------------------===// 1379a9a3417929e340e84dcbc06ed9c3a277cad959Douglas Gregor#include "clang/AST/ASTDiagnostic.h" 1479a9a3417929e340e84dcbc06ed9c3a277cad959Douglas Gregor 1579a9a3417929e340e84dcbc06ed9c3a277cad959Douglas Gregor#include "clang/AST/ASTContext.h" 1679a9a3417929e340e84dcbc06ed9c3a277cad959Douglas Gregor#include "clang/AST/DeclObjC.h" 1779a9a3417929e340e84dcbc06ed9c3a277cad959Douglas Gregor#include "clang/AST/Type.h" 1879a9a3417929e340e84dcbc06ed9c3a277cad959Douglas Gregor#include "llvm/Support/raw_ostream.h" 1979a9a3417929e340e84dcbc06ed9c3a277cad959Douglas Gregor 2079a9a3417929e340e84dcbc06ed9c3a277cad959Douglas Gregorusing namespace clang; 2179a9a3417929e340e84dcbc06ed9c3a277cad959Douglas Gregor 221733bc3747f242ddaea5b953d27f514253843e31Chandler Carruth// Returns a desugared version of the QualType, and marks ShouldAKA as true 231733bc3747f242ddaea5b953d27f514253843e31Chandler Carruth// whenever we remove significant sugar from the type. 241733bc3747f242ddaea5b953d27f514253843e31Chandler Carruthstatic QualType Desugar(ASTContext &Context, QualType QT, bool &ShouldAKA) { 251733bc3747f242ddaea5b953d27f514253843e31Chandler Carruth QualifierCollector QC; 261733bc3747f242ddaea5b953d27f514253843e31Chandler Carruth 2779a9a3417929e340e84dcbc06ed9c3a277cad959Douglas Gregor while (true) { 281733bc3747f242ddaea5b953d27f514253843e31Chandler Carruth const Type *Ty = QC.strip(QT); 291733bc3747f242ddaea5b953d27f514253843e31Chandler Carruth 3079a9a3417929e340e84dcbc06ed9c3a277cad959Douglas Gregor // Don't aka just because we saw an elaborated type... 3134b41d939a1328f484511c6002ba2456db879a29Richard Smith if (const ElaboratedType *ET = dyn_cast<ElaboratedType>(Ty)) { 3234b41d939a1328f484511c6002ba2456db879a29Richard Smith QT = ET->desugar(); 3379a9a3417929e340e84dcbc06ed9c3a277cad959Douglas Gregor continue; 3479a9a3417929e340e84dcbc06ed9c3a277cad959Douglas Gregor } 35075f8f1b6bed4d1b224c74f87508534cc6392ce6Abramo Bagnara // ... or a paren type ... 3634b41d939a1328f484511c6002ba2456db879a29Richard Smith if (const ParenType *PT = dyn_cast<ParenType>(Ty)) { 3734b41d939a1328f484511c6002ba2456db879a29Richard Smith QT = PT->desugar(); 38075f8f1b6bed4d1b224c74f87508534cc6392ce6Abramo Bagnara continue; 39075f8f1b6bed4d1b224c74f87508534cc6392ce6Abramo Bagnara } 4034b41d939a1328f484511c6002ba2456db879a29Richard Smith // ...or a substituted template type parameter ... 4134b41d939a1328f484511c6002ba2456db879a29Richard Smith if (const SubstTemplateTypeParmType *ST = 4234b41d939a1328f484511c6002ba2456db879a29Richard Smith dyn_cast<SubstTemplateTypeParmType>(Ty)) { 4334b41d939a1328f484511c6002ba2456db879a29Richard Smith QT = ST->desugar(); 4434b41d939a1328f484511c6002ba2456db879a29Richard Smith continue; 4534b41d939a1328f484511c6002ba2456db879a29Richard Smith } 4614aa2175416f79ef17811282afbf425f87d54ebfJohn McCall // ...or an attributed type... 4714aa2175416f79ef17811282afbf425f87d54ebfJohn McCall if (const AttributedType *AT = dyn_cast<AttributedType>(Ty)) { 4814aa2175416f79ef17811282afbf425f87d54ebfJohn McCall QT = AT->desugar(); 4914aa2175416f79ef17811282afbf425f87d54ebfJohn McCall continue; 5014aa2175416f79ef17811282afbf425f87d54ebfJohn McCall } 5134b41d939a1328f484511c6002ba2456db879a29Richard Smith // ... or an auto type. 5234b41d939a1328f484511c6002ba2456db879a29Richard Smith if (const AutoType *AT = dyn_cast<AutoType>(Ty)) { 5334b41d939a1328f484511c6002ba2456db879a29Richard Smith if (!AT->isSugared()) 5434b41d939a1328f484511c6002ba2456db879a29Richard Smith break; 5534b41d939a1328f484511c6002ba2456db879a29Richard Smith QT = AT->desugar(); 5679a9a3417929e340e84dcbc06ed9c3a277cad959Douglas Gregor continue; 5779a9a3417929e340e84dcbc06ed9c3a277cad959Douglas Gregor } 581733bc3747f242ddaea5b953d27f514253843e31Chandler Carruth 5979a9a3417929e340e84dcbc06ed9c3a277cad959Douglas Gregor // Don't desugar template specializations. 6079a9a3417929e340e84dcbc06ed9c3a277cad959Douglas Gregor if (isa<TemplateSpecializationType>(Ty)) 6179a9a3417929e340e84dcbc06ed9c3a277cad959Douglas Gregor break; 621733bc3747f242ddaea5b953d27f514253843e31Chandler Carruth 6379a9a3417929e340e84dcbc06ed9c3a277cad959Douglas Gregor // Don't desugar magic Objective-C types. 6479a9a3417929e340e84dcbc06ed9c3a277cad959Douglas Gregor if (QualType(Ty,0) == Context.getObjCIdType() || 6579a9a3417929e340e84dcbc06ed9c3a277cad959Douglas Gregor QualType(Ty,0) == Context.getObjCClassType() || 6679a9a3417929e340e84dcbc06ed9c3a277cad959Douglas Gregor QualType(Ty,0) == Context.getObjCSelType() || 6779a9a3417929e340e84dcbc06ed9c3a277cad959Douglas Gregor QualType(Ty,0) == Context.getObjCProtoType()) 6879a9a3417929e340e84dcbc06ed9c3a277cad959Douglas Gregor break; 691733bc3747f242ddaea5b953d27f514253843e31Chandler Carruth 7079a9a3417929e340e84dcbc06ed9c3a277cad959Douglas Gregor // Don't desugar va_list. 7179a9a3417929e340e84dcbc06ed9c3a277cad959Douglas Gregor if (QualType(Ty,0) == Context.getBuiltinVaListType()) 7279a9a3417929e340e84dcbc06ed9c3a277cad959Douglas Gregor break; 731733bc3747f242ddaea5b953d27f514253843e31Chandler Carruth 7479a9a3417929e340e84dcbc06ed9c3a277cad959Douglas Gregor // Otherwise, do a single-step desugar. 7579a9a3417929e340e84dcbc06ed9c3a277cad959Douglas Gregor QualType Underlying; 7679a9a3417929e340e84dcbc06ed9c3a277cad959Douglas Gregor bool IsSugar = false; 7779a9a3417929e340e84dcbc06ed9c3a277cad959Douglas Gregor switch (Ty->getTypeClass()) { 7879a9a3417929e340e84dcbc06ed9c3a277cad959Douglas Gregor#define ABSTRACT_TYPE(Class, Base) 7979a9a3417929e340e84dcbc06ed9c3a277cad959Douglas Gregor#define TYPE(Class, Base) \ 8079a9a3417929e340e84dcbc06ed9c3a277cad959Douglas Gregorcase Type::Class: { \ 8179a9a3417929e340e84dcbc06ed9c3a277cad959Douglas Gregorconst Class##Type *CTy = cast<Class##Type>(Ty); \ 8279a9a3417929e340e84dcbc06ed9c3a277cad959Douglas Gregorif (CTy->isSugared()) { \ 8379a9a3417929e340e84dcbc06ed9c3a277cad959Douglas GregorIsSugar = true; \ 8479a9a3417929e340e84dcbc06ed9c3a277cad959Douglas GregorUnderlying = CTy->desugar(); \ 8579a9a3417929e340e84dcbc06ed9c3a277cad959Douglas Gregor} \ 8679a9a3417929e340e84dcbc06ed9c3a277cad959Douglas Gregorbreak; \ 8779a9a3417929e340e84dcbc06ed9c3a277cad959Douglas Gregor} 8879a9a3417929e340e84dcbc06ed9c3a277cad959Douglas Gregor#include "clang/AST/TypeNodes.def" 8979a9a3417929e340e84dcbc06ed9c3a277cad959Douglas Gregor } 901733bc3747f242ddaea5b953d27f514253843e31Chandler Carruth 9179a9a3417929e340e84dcbc06ed9c3a277cad959Douglas Gregor // If it wasn't sugared, we're done. 9279a9a3417929e340e84dcbc06ed9c3a277cad959Douglas Gregor if (!IsSugar) 9379a9a3417929e340e84dcbc06ed9c3a277cad959Douglas Gregor break; 941733bc3747f242ddaea5b953d27f514253843e31Chandler Carruth 9579a9a3417929e340e84dcbc06ed9c3a277cad959Douglas Gregor // If the desugared type is a vector type, we don't want to expand 9679a9a3417929e340e84dcbc06ed9c3a277cad959Douglas Gregor // it, it will turn into an attribute mess. People want their "vec4". 9779a9a3417929e340e84dcbc06ed9c3a277cad959Douglas Gregor if (isa<VectorType>(Underlying)) 9879a9a3417929e340e84dcbc06ed9c3a277cad959Douglas Gregor break; 991733bc3747f242ddaea5b953d27f514253843e31Chandler Carruth 10079a9a3417929e340e84dcbc06ed9c3a277cad959Douglas Gregor // Don't desugar through the primary typedef of an anonymous type. 101c3f8c0731ef59ba79753f89f1c108b8134f6ae83Chris Lattner if (const TagType *UTT = Underlying->getAs<TagType>()) 102c3f8c0731ef59ba79753f89f1c108b8134f6ae83Chris Lattner if (const TypedefType *QTT = dyn_cast<TypedefType>(QT)) 103c3f8c0731ef59ba79753f89f1c108b8134f6ae83Chris Lattner if (UTT->getDecl()->getTypedefForAnonDecl() == QTT->getDecl()) 104c3f8c0731ef59ba79753f89f1c108b8134f6ae83Chris Lattner break; 1051733bc3747f242ddaea5b953d27f514253843e31Chandler Carruth 1061733bc3747f242ddaea5b953d27f514253843e31Chandler Carruth // Record that we actually looked through an opaque type here. 1071733bc3747f242ddaea5b953d27f514253843e31Chandler Carruth ShouldAKA = true; 10879a9a3417929e340e84dcbc06ed9c3a277cad959Douglas Gregor QT = Underlying; 10979a9a3417929e340e84dcbc06ed9c3a277cad959Douglas Gregor } 1101733bc3747f242ddaea5b953d27f514253843e31Chandler Carruth 1111733bc3747f242ddaea5b953d27f514253843e31Chandler Carruth // If we have a pointer-like type, desugar the pointee as well. 1121733bc3747f242ddaea5b953d27f514253843e31Chandler Carruth // FIXME: Handle other pointer-like types. 1131733bc3747f242ddaea5b953d27f514253843e31Chandler Carruth if (const PointerType *Ty = QT->getAs<PointerType>()) { 114c3f8c0731ef59ba79753f89f1c108b8134f6ae83Chris Lattner QT = Context.getPointerType(Desugar(Context, Ty->getPointeeType(), 115c3f8c0731ef59ba79753f89f1c108b8134f6ae83Chris Lattner ShouldAKA)); 1161733bc3747f242ddaea5b953d27f514253843e31Chandler Carruth } else if (const LValueReferenceType *Ty = QT->getAs<LValueReferenceType>()) { 117c3f8c0731ef59ba79753f89f1c108b8134f6ae83Chris Lattner QT = Context.getLValueReferenceType(Desugar(Context, Ty->getPointeeType(), 118c3f8c0731ef59ba79753f89f1c108b8134f6ae83Chris Lattner ShouldAKA)); 11969d831645f429d3b806d2ae220aee45ca44f8c6cDouglas Gregor } else if (const RValueReferenceType *Ty = QT->getAs<RValueReferenceType>()) { 12069d831645f429d3b806d2ae220aee45ca44f8c6cDouglas Gregor QT = Context.getRValueReferenceType(Desugar(Context, Ty->getPointeeType(), 12169d831645f429d3b806d2ae220aee45ca44f8c6cDouglas Gregor ShouldAKA)); 12279a9a3417929e340e84dcbc06ed9c3a277cad959Douglas Gregor } 1231733bc3747f242ddaea5b953d27f514253843e31Chandler Carruth 12449f4e1cbd839da27ff4814b4ea6d85a79f786cbdJohn McCall return QC.apply(Context, QT); 12579a9a3417929e340e84dcbc06ed9c3a277cad959Douglas Gregor} 12679a9a3417929e340e84dcbc06ed9c3a277cad959Douglas Gregor 12779a9a3417929e340e84dcbc06ed9c3a277cad959Douglas Gregor/// \brief Convert the given type to a string suitable for printing as part of 1281733bc3747f242ddaea5b953d27f514253843e31Chandler Carruth/// a diagnostic. 1291733bc3747f242ddaea5b953d27f514253843e31Chandler Carruth/// 1301733bc3747f242ddaea5b953d27f514253843e31Chandler Carruth/// There are three main criteria when determining whether we should have an 1311733bc3747f242ddaea5b953d27f514253843e31Chandler Carruth/// a.k.a. clause when pretty-printing a type: 1321733bc3747f242ddaea5b953d27f514253843e31Chandler Carruth/// 1331733bc3747f242ddaea5b953d27f514253843e31Chandler Carruth/// 1) Some types provide very minimal sugar that doesn't impede the 1341733bc3747f242ddaea5b953d27f514253843e31Chandler Carruth/// user's understanding --- for example, elaborated type 1351733bc3747f242ddaea5b953d27f514253843e31Chandler Carruth/// specifiers. If this is all the sugar we see, we don't want an 1361733bc3747f242ddaea5b953d27f514253843e31Chandler Carruth/// a.k.a. clause. 1371733bc3747f242ddaea5b953d27f514253843e31Chandler Carruth/// 2) Some types are technically sugared but are much more familiar 1381733bc3747f242ddaea5b953d27f514253843e31Chandler Carruth/// when seen in their sugared form --- for example, va_list, 1391733bc3747f242ddaea5b953d27f514253843e31Chandler Carruth/// vector types, and the magic Objective C types. We don't 1401733bc3747f242ddaea5b953d27f514253843e31Chandler Carruth/// want to desugar these, even if we do produce an a.k.a. clause. 1411733bc3747f242ddaea5b953d27f514253843e31Chandler Carruth/// 3) Some types may have already been desugared previously in this diagnostic. 1421733bc3747f242ddaea5b953d27f514253843e31Chandler Carruth/// if this is the case, doing another "aka" would just be clutter. 14379a9a3417929e340e84dcbc06ed9c3a277cad959Douglas Gregor/// 14479a9a3417929e340e84dcbc06ed9c3a277cad959Douglas Gregor/// \param Context the context in which the type was allocated 14579a9a3417929e340e84dcbc06ed9c3a277cad959Douglas Gregor/// \param Ty the type to print 14679a9a3417929e340e84dcbc06ed9c3a277cad959Douglas Gregorstatic std::string 14779a9a3417929e340e84dcbc06ed9c3a277cad959Douglas GregorConvertTypeToDiagnosticString(ASTContext &Context, QualType Ty, 14879a9a3417929e340e84dcbc06ed9c3a277cad959Douglas Gregor const Diagnostic::ArgumentValue *PrevArgs, 14979a9a3417929e340e84dcbc06ed9c3a277cad959Douglas Gregor unsigned NumPrevArgs) { 15079a9a3417929e340e84dcbc06ed9c3a277cad959Douglas Gregor // FIXME: Playing with std::string is really slow. 15179a9a3417929e340e84dcbc06ed9c3a277cad959Douglas Gregor std::string S = Ty.getAsString(Context.PrintingPolicy); 1521733bc3747f242ddaea5b953d27f514253843e31Chandler Carruth 1531733bc3747f242ddaea5b953d27f514253843e31Chandler Carruth // Check to see if we already desugared this type in this 1541733bc3747f242ddaea5b953d27f514253843e31Chandler Carruth // diagnostic. If so, don't do it again. 1551733bc3747f242ddaea5b953d27f514253843e31Chandler Carruth bool Repeated = false; 1561733bc3747f242ddaea5b953d27f514253843e31Chandler Carruth for (unsigned i = 0; i != NumPrevArgs; ++i) { 1571733bc3747f242ddaea5b953d27f514253843e31Chandler Carruth // TODO: Handle ak_declcontext case. 1581733bc3747f242ddaea5b953d27f514253843e31Chandler Carruth if (PrevArgs[i].first == Diagnostic::ak_qualtype) { 1591733bc3747f242ddaea5b953d27f514253843e31Chandler Carruth void *Ptr = (void*)PrevArgs[i].second; 1601733bc3747f242ddaea5b953d27f514253843e31Chandler Carruth QualType PrevTy(QualType::getFromOpaquePtr(Ptr)); 1611733bc3747f242ddaea5b953d27f514253843e31Chandler Carruth if (PrevTy == Ty) { 1621733bc3747f242ddaea5b953d27f514253843e31Chandler Carruth Repeated = true; 1631733bc3747f242ddaea5b953d27f514253843e31Chandler Carruth break; 1641733bc3747f242ddaea5b953d27f514253843e31Chandler Carruth } 1651733bc3747f242ddaea5b953d27f514253843e31Chandler Carruth } 1661733bc3747f242ddaea5b953d27f514253843e31Chandler Carruth } 1671733bc3747f242ddaea5b953d27f514253843e31Chandler Carruth 16879a9a3417929e340e84dcbc06ed9c3a277cad959Douglas Gregor // Consider producing an a.k.a. clause if removing all the direct 16979a9a3417929e340e84dcbc06ed9c3a277cad959Douglas Gregor // sugar gives us something "significantly different". 1701733bc3747f242ddaea5b953d27f514253843e31Chandler Carruth if (!Repeated) { 1711733bc3747f242ddaea5b953d27f514253843e31Chandler Carruth bool ShouldAKA = false; 1721733bc3747f242ddaea5b953d27f514253843e31Chandler Carruth QualType DesugaredTy = Desugar(Context, Ty, ShouldAKA); 1731733bc3747f242ddaea5b953d27f514253843e31Chandler Carruth if (ShouldAKA) { 174c3f8c0731ef59ba79753f89f1c108b8134f6ae83Chris Lattner S = "'" + S + "' (aka '"; 175c3f8c0731ef59ba79753f89f1c108b8134f6ae83Chris Lattner S += DesugaredTy.getAsString(Context.PrintingPolicy); 176c3f8c0731ef59ba79753f89f1c108b8134f6ae83Chris Lattner S += "')"; 177c3f8c0731ef59ba79753f89f1c108b8134f6ae83Chris Lattner return S; 1781733bc3747f242ddaea5b953d27f514253843e31Chandler Carruth } 17979a9a3417929e340e84dcbc06ed9c3a277cad959Douglas Gregor } 1801733bc3747f242ddaea5b953d27f514253843e31Chandler Carruth 18179a9a3417929e340e84dcbc06ed9c3a277cad959Douglas Gregor S = "'" + S + "'"; 18279a9a3417929e340e84dcbc06ed9c3a277cad959Douglas Gregor return S; 18379a9a3417929e340e84dcbc06ed9c3a277cad959Douglas Gregor} 18479a9a3417929e340e84dcbc06ed9c3a277cad959Douglas Gregor 18579a9a3417929e340e84dcbc06ed9c3a277cad959Douglas Gregorvoid clang::FormatASTNodeDiagnosticArgument(Diagnostic::ArgumentKind Kind, 18679a9a3417929e340e84dcbc06ed9c3a277cad959Douglas Gregor intptr_t Val, 18779a9a3417929e340e84dcbc06ed9c3a277cad959Douglas Gregor const char *Modifier, 18879a9a3417929e340e84dcbc06ed9c3a277cad959Douglas Gregor unsigned ModLen, 18979a9a3417929e340e84dcbc06ed9c3a277cad959Douglas Gregor const char *Argument, 19079a9a3417929e340e84dcbc06ed9c3a277cad959Douglas Gregor unsigned ArgLen, 19179a9a3417929e340e84dcbc06ed9c3a277cad959Douglas Gregor const Diagnostic::ArgumentValue *PrevArgs, 19279a9a3417929e340e84dcbc06ed9c3a277cad959Douglas Gregor unsigned NumPrevArgs, 19379a9a3417929e340e84dcbc06ed9c3a277cad959Douglas Gregor llvm::SmallVectorImpl<char> &Output, 19479a9a3417929e340e84dcbc06ed9c3a277cad959Douglas Gregor void *Cookie) { 19579a9a3417929e340e84dcbc06ed9c3a277cad959Douglas Gregor ASTContext &Context = *static_cast<ASTContext*>(Cookie); 19679a9a3417929e340e84dcbc06ed9c3a277cad959Douglas Gregor 19779a9a3417929e340e84dcbc06ed9c3a277cad959Douglas Gregor std::string S; 19879a9a3417929e340e84dcbc06ed9c3a277cad959Douglas Gregor bool NeedQuotes = true; 19979a9a3417929e340e84dcbc06ed9c3a277cad959Douglas Gregor 20079a9a3417929e340e84dcbc06ed9c3a277cad959Douglas Gregor switch (Kind) { 20179a9a3417929e340e84dcbc06ed9c3a277cad959Douglas Gregor default: assert(0 && "unknown ArgumentKind"); 20279a9a3417929e340e84dcbc06ed9c3a277cad959Douglas Gregor case Diagnostic::ak_qualtype: { 20379a9a3417929e340e84dcbc06ed9c3a277cad959Douglas Gregor assert(ModLen == 0 && ArgLen == 0 && 20479a9a3417929e340e84dcbc06ed9c3a277cad959Douglas Gregor "Invalid modifier for QualType argument"); 20579a9a3417929e340e84dcbc06ed9c3a277cad959Douglas Gregor 20679a9a3417929e340e84dcbc06ed9c3a277cad959Douglas Gregor QualType Ty(QualType::getFromOpaquePtr(reinterpret_cast<void*>(Val))); 20779a9a3417929e340e84dcbc06ed9c3a277cad959Douglas Gregor S = ConvertTypeToDiagnosticString(Context, Ty, PrevArgs, NumPrevArgs); 20879a9a3417929e340e84dcbc06ed9c3a277cad959Douglas Gregor NeedQuotes = false; 20979a9a3417929e340e84dcbc06ed9c3a277cad959Douglas Gregor break; 21079a9a3417929e340e84dcbc06ed9c3a277cad959Douglas Gregor } 21179a9a3417929e340e84dcbc06ed9c3a277cad959Douglas Gregor case Diagnostic::ak_declarationname: { 21279a9a3417929e340e84dcbc06ed9c3a277cad959Douglas Gregor DeclarationName N = DeclarationName::getFromOpaqueInteger(Val); 21379a9a3417929e340e84dcbc06ed9c3a277cad959Douglas Gregor S = N.getAsString(); 21479a9a3417929e340e84dcbc06ed9c3a277cad959Douglas Gregor 21579a9a3417929e340e84dcbc06ed9c3a277cad959Douglas Gregor if (ModLen == 9 && !memcmp(Modifier, "objcclass", 9) && ArgLen == 0) 21679a9a3417929e340e84dcbc06ed9c3a277cad959Douglas Gregor S = '+' + S; 21779a9a3417929e340e84dcbc06ed9c3a277cad959Douglas Gregor else if (ModLen == 12 && !memcmp(Modifier, "objcinstance", 12) 21879a9a3417929e340e84dcbc06ed9c3a277cad959Douglas Gregor && ArgLen==0) 21979a9a3417929e340e84dcbc06ed9c3a277cad959Douglas Gregor S = '-' + S; 22079a9a3417929e340e84dcbc06ed9c3a277cad959Douglas Gregor else 22179a9a3417929e340e84dcbc06ed9c3a277cad959Douglas Gregor assert(ModLen == 0 && ArgLen == 0 && 22279a9a3417929e340e84dcbc06ed9c3a277cad959Douglas Gregor "Invalid modifier for DeclarationName argument"); 22379a9a3417929e340e84dcbc06ed9c3a277cad959Douglas Gregor break; 22479a9a3417929e340e84dcbc06ed9c3a277cad959Douglas Gregor } 22579a9a3417929e340e84dcbc06ed9c3a277cad959Douglas Gregor case Diagnostic::ak_nameddecl: { 22679a9a3417929e340e84dcbc06ed9c3a277cad959Douglas Gregor bool Qualified; 22779a9a3417929e340e84dcbc06ed9c3a277cad959Douglas Gregor if (ModLen == 1 && Modifier[0] == 'q' && ArgLen == 0) 22879a9a3417929e340e84dcbc06ed9c3a277cad959Douglas Gregor Qualified = true; 22979a9a3417929e340e84dcbc06ed9c3a277cad959Douglas Gregor else { 23079a9a3417929e340e84dcbc06ed9c3a277cad959Douglas Gregor assert(ModLen == 0 && ArgLen == 0 && 23179a9a3417929e340e84dcbc06ed9c3a277cad959Douglas Gregor "Invalid modifier for NamedDecl* argument"); 23279a9a3417929e340e84dcbc06ed9c3a277cad959Douglas Gregor Qualified = false; 23379a9a3417929e340e84dcbc06ed9c3a277cad959Douglas Gregor } 23479a9a3417929e340e84dcbc06ed9c3a277cad959Douglas Gregor reinterpret_cast<NamedDecl*>(Val)-> 23579a9a3417929e340e84dcbc06ed9c3a277cad959Douglas Gregor getNameForDiagnostic(S, Context.PrintingPolicy, Qualified); 23679a9a3417929e340e84dcbc06ed9c3a277cad959Douglas Gregor break; 23779a9a3417929e340e84dcbc06ed9c3a277cad959Douglas Gregor } 23879a9a3417929e340e84dcbc06ed9c3a277cad959Douglas Gregor case Diagnostic::ak_nestednamespec: { 23979a9a3417929e340e84dcbc06ed9c3a277cad959Douglas Gregor llvm::raw_string_ostream OS(S); 24079a9a3417929e340e84dcbc06ed9c3a277cad959Douglas Gregor reinterpret_cast<NestedNameSpecifier*>(Val)->print(OS, 24179a9a3417929e340e84dcbc06ed9c3a277cad959Douglas Gregor Context.PrintingPolicy); 24279a9a3417929e340e84dcbc06ed9c3a277cad959Douglas Gregor NeedQuotes = false; 24379a9a3417929e340e84dcbc06ed9c3a277cad959Douglas Gregor break; 24479a9a3417929e340e84dcbc06ed9c3a277cad959Douglas Gregor } 24579a9a3417929e340e84dcbc06ed9c3a277cad959Douglas Gregor case Diagnostic::ak_declcontext: { 24679a9a3417929e340e84dcbc06ed9c3a277cad959Douglas Gregor DeclContext *DC = reinterpret_cast<DeclContext *> (Val); 24779a9a3417929e340e84dcbc06ed9c3a277cad959Douglas Gregor assert(DC && "Should never have a null declaration context"); 24879a9a3417929e340e84dcbc06ed9c3a277cad959Douglas Gregor 24979a9a3417929e340e84dcbc06ed9c3a277cad959Douglas Gregor if (DC->isTranslationUnit()) { 25079a9a3417929e340e84dcbc06ed9c3a277cad959Douglas Gregor // FIXME: Get these strings from some localized place 25179a9a3417929e340e84dcbc06ed9c3a277cad959Douglas Gregor if (Context.getLangOptions().CPlusPlus) 25279a9a3417929e340e84dcbc06ed9c3a277cad959Douglas Gregor S = "the global namespace"; 25379a9a3417929e340e84dcbc06ed9c3a277cad959Douglas Gregor else 25479a9a3417929e340e84dcbc06ed9c3a277cad959Douglas Gregor S = "the global scope"; 25579a9a3417929e340e84dcbc06ed9c3a277cad959Douglas Gregor } else if (TypeDecl *Type = dyn_cast<TypeDecl>(DC)) { 25679a9a3417929e340e84dcbc06ed9c3a277cad959Douglas Gregor S = ConvertTypeToDiagnosticString(Context, 25779a9a3417929e340e84dcbc06ed9c3a277cad959Douglas Gregor Context.getTypeDeclType(Type), 25879a9a3417929e340e84dcbc06ed9c3a277cad959Douglas Gregor PrevArgs, NumPrevArgs); 25979a9a3417929e340e84dcbc06ed9c3a277cad959Douglas Gregor } else { 26079a9a3417929e340e84dcbc06ed9c3a277cad959Douglas Gregor // FIXME: Get these strings from some localized place 26179a9a3417929e340e84dcbc06ed9c3a277cad959Douglas Gregor NamedDecl *ND = cast<NamedDecl>(DC); 26279a9a3417929e340e84dcbc06ed9c3a277cad959Douglas Gregor if (isa<NamespaceDecl>(ND)) 26379a9a3417929e340e84dcbc06ed9c3a277cad959Douglas Gregor S += "namespace "; 26479a9a3417929e340e84dcbc06ed9c3a277cad959Douglas Gregor else if (isa<ObjCMethodDecl>(ND)) 26579a9a3417929e340e84dcbc06ed9c3a277cad959Douglas Gregor S += "method "; 26679a9a3417929e340e84dcbc06ed9c3a277cad959Douglas Gregor else if (isa<FunctionDecl>(ND)) 26779a9a3417929e340e84dcbc06ed9c3a277cad959Douglas Gregor S += "function "; 26879a9a3417929e340e84dcbc06ed9c3a277cad959Douglas Gregor 26979a9a3417929e340e84dcbc06ed9c3a277cad959Douglas Gregor S += "'"; 27079a9a3417929e340e84dcbc06ed9c3a277cad959Douglas Gregor ND->getNameForDiagnostic(S, Context.PrintingPolicy, true); 27179a9a3417929e340e84dcbc06ed9c3a277cad959Douglas Gregor S += "'"; 27279a9a3417929e340e84dcbc06ed9c3a277cad959Douglas Gregor } 27379a9a3417929e340e84dcbc06ed9c3a277cad959Douglas Gregor NeedQuotes = false; 27479a9a3417929e340e84dcbc06ed9c3a277cad959Douglas Gregor break; 27579a9a3417929e340e84dcbc06ed9c3a277cad959Douglas Gregor } 27679a9a3417929e340e84dcbc06ed9c3a277cad959Douglas Gregor } 27779a9a3417929e340e84dcbc06ed9c3a277cad959Douglas Gregor 27879a9a3417929e340e84dcbc06ed9c3a277cad959Douglas Gregor if (NeedQuotes) 27979a9a3417929e340e84dcbc06ed9c3a277cad959Douglas Gregor Output.push_back('\''); 28079a9a3417929e340e84dcbc06ed9c3a277cad959Douglas Gregor 28179a9a3417929e340e84dcbc06ed9c3a277cad959Douglas Gregor Output.append(S.begin(), S.end()); 28279a9a3417929e340e84dcbc06ed9c3a277cad959Douglas Gregor 28379a9a3417929e340e84dcbc06ed9c3a277cad959Douglas Gregor if (NeedQuotes) 28479a9a3417929e340e84dcbc06ed9c3a277cad959Douglas Gregor Output.push_back('\''); 28579a9a3417929e340e84dcbc06ed9c3a277cad959Douglas Gregor} 286