ASTDiagnostic.cpp revision c3f8c0731ef59ba79753f89f1c108b8134f6ae83
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...
3179a9a3417929e340e84dcbc06ed9c3a277cad959Douglas Gregor    if (isa<ElaboratedType>(Ty)) {
3279a9a3417929e340e84dcbc06ed9c3a277cad959Douglas Gregor      QT = cast<ElaboratedType>(Ty)->desugar();
3379a9a3417929e340e84dcbc06ed9c3a277cad959Douglas Gregor      continue;
3479a9a3417929e340e84dcbc06ed9c3a277cad959Douglas Gregor    }
353cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall
3679a9a3417929e340e84dcbc06ed9c3a277cad959Douglas Gregor    // ...or a substituted template type parameter.
3779a9a3417929e340e84dcbc06ed9c3a277cad959Douglas Gregor    if (isa<SubstTemplateTypeParmType>(Ty)) {
3879a9a3417929e340e84dcbc06ed9c3a277cad959Douglas Gregor      QT = cast<SubstTemplateTypeParmType>(Ty)->desugar();
3979a9a3417929e340e84dcbc06ed9c3a277cad959Douglas Gregor      continue;
4079a9a3417929e340e84dcbc06ed9c3a277cad959Douglas Gregor    }
411733bc3747f242ddaea5b953d27f514253843e31Chandler Carruth
4279a9a3417929e340e84dcbc06ed9c3a277cad959Douglas Gregor    // Don't desugar template specializations.
4379a9a3417929e340e84dcbc06ed9c3a277cad959Douglas Gregor    if (isa<TemplateSpecializationType>(Ty))
4479a9a3417929e340e84dcbc06ed9c3a277cad959Douglas Gregor      break;
451733bc3747f242ddaea5b953d27f514253843e31Chandler Carruth
4679a9a3417929e340e84dcbc06ed9c3a277cad959Douglas Gregor    // Don't desugar magic Objective-C types.
4779a9a3417929e340e84dcbc06ed9c3a277cad959Douglas Gregor    if (QualType(Ty,0) == Context.getObjCIdType() ||
4879a9a3417929e340e84dcbc06ed9c3a277cad959Douglas Gregor        QualType(Ty,0) == Context.getObjCClassType() ||
4979a9a3417929e340e84dcbc06ed9c3a277cad959Douglas Gregor        QualType(Ty,0) == Context.getObjCSelType() ||
5079a9a3417929e340e84dcbc06ed9c3a277cad959Douglas Gregor        QualType(Ty,0) == Context.getObjCProtoType())
5179a9a3417929e340e84dcbc06ed9c3a277cad959Douglas Gregor      break;
521733bc3747f242ddaea5b953d27f514253843e31Chandler Carruth
5379a9a3417929e340e84dcbc06ed9c3a277cad959Douglas Gregor    // Don't desugar va_list.
5479a9a3417929e340e84dcbc06ed9c3a277cad959Douglas Gregor    if (QualType(Ty,0) == Context.getBuiltinVaListType())
5579a9a3417929e340e84dcbc06ed9c3a277cad959Douglas Gregor      break;
561733bc3747f242ddaea5b953d27f514253843e31Chandler Carruth
5779a9a3417929e340e84dcbc06ed9c3a277cad959Douglas Gregor    // Otherwise, do a single-step desugar.
5879a9a3417929e340e84dcbc06ed9c3a277cad959Douglas Gregor    QualType Underlying;
5979a9a3417929e340e84dcbc06ed9c3a277cad959Douglas Gregor    bool IsSugar = false;
6079a9a3417929e340e84dcbc06ed9c3a277cad959Douglas Gregor    switch (Ty->getTypeClass()) {
6179a9a3417929e340e84dcbc06ed9c3a277cad959Douglas Gregor#define ABSTRACT_TYPE(Class, Base)
6279a9a3417929e340e84dcbc06ed9c3a277cad959Douglas Gregor#define TYPE(Class, Base) \
6379a9a3417929e340e84dcbc06ed9c3a277cad959Douglas Gregorcase Type::Class: { \
6479a9a3417929e340e84dcbc06ed9c3a277cad959Douglas Gregorconst Class##Type *CTy = cast<Class##Type>(Ty); \
6579a9a3417929e340e84dcbc06ed9c3a277cad959Douglas Gregorif (CTy->isSugared()) { \
6679a9a3417929e340e84dcbc06ed9c3a277cad959Douglas GregorIsSugar = true; \
6779a9a3417929e340e84dcbc06ed9c3a277cad959Douglas GregorUnderlying = CTy->desugar(); \
6879a9a3417929e340e84dcbc06ed9c3a277cad959Douglas Gregor} \
6979a9a3417929e340e84dcbc06ed9c3a277cad959Douglas Gregorbreak; \
7079a9a3417929e340e84dcbc06ed9c3a277cad959Douglas Gregor}
7179a9a3417929e340e84dcbc06ed9c3a277cad959Douglas Gregor#include "clang/AST/TypeNodes.def"
7279a9a3417929e340e84dcbc06ed9c3a277cad959Douglas Gregor    }
731733bc3747f242ddaea5b953d27f514253843e31Chandler Carruth
7479a9a3417929e340e84dcbc06ed9c3a277cad959Douglas Gregor    // If it wasn't sugared, we're done.
7579a9a3417929e340e84dcbc06ed9c3a277cad959Douglas Gregor    if (!IsSugar)
7679a9a3417929e340e84dcbc06ed9c3a277cad959Douglas Gregor      break;
771733bc3747f242ddaea5b953d27f514253843e31Chandler Carruth
7879a9a3417929e340e84dcbc06ed9c3a277cad959Douglas Gregor    // If the desugared type is a vector type, we don't want to expand
7979a9a3417929e340e84dcbc06ed9c3a277cad959Douglas Gregor    // it, it will turn into an attribute mess. People want their "vec4".
8079a9a3417929e340e84dcbc06ed9c3a277cad959Douglas Gregor    if (isa<VectorType>(Underlying))
8179a9a3417929e340e84dcbc06ed9c3a277cad959Douglas Gregor      break;
821733bc3747f242ddaea5b953d27f514253843e31Chandler Carruth
8379a9a3417929e340e84dcbc06ed9c3a277cad959Douglas Gregor    // Don't desugar through the primary typedef of an anonymous type.
84c3f8c0731ef59ba79753f89f1c108b8134f6ae83Chris Lattner    if (const TagType *UTT = Underlying->getAs<TagType>())
85c3f8c0731ef59ba79753f89f1c108b8134f6ae83Chris Lattner      if (const TypedefType *QTT = dyn_cast<TypedefType>(QT))
86c3f8c0731ef59ba79753f89f1c108b8134f6ae83Chris Lattner        if (UTT->getDecl()->getTypedefForAnonDecl() == QTT->getDecl())
87c3f8c0731ef59ba79753f89f1c108b8134f6ae83Chris Lattner          break;
881733bc3747f242ddaea5b953d27f514253843e31Chandler Carruth
891733bc3747f242ddaea5b953d27f514253843e31Chandler Carruth    // Record that we actually looked through an opaque type here.
901733bc3747f242ddaea5b953d27f514253843e31Chandler Carruth    ShouldAKA = true;
9179a9a3417929e340e84dcbc06ed9c3a277cad959Douglas Gregor    QT = Underlying;
9279a9a3417929e340e84dcbc06ed9c3a277cad959Douglas Gregor  }
931733bc3747f242ddaea5b953d27f514253843e31Chandler Carruth
941733bc3747f242ddaea5b953d27f514253843e31Chandler Carruth  // If we have a pointer-like type, desugar the pointee as well.
951733bc3747f242ddaea5b953d27f514253843e31Chandler Carruth  // FIXME: Handle other pointer-like types.
961733bc3747f242ddaea5b953d27f514253843e31Chandler Carruth  if (const PointerType *Ty = QT->getAs<PointerType>()) {
97c3f8c0731ef59ba79753f89f1c108b8134f6ae83Chris Lattner    QT = Context.getPointerType(Desugar(Context, Ty->getPointeeType(),
98c3f8c0731ef59ba79753f89f1c108b8134f6ae83Chris Lattner                                        ShouldAKA));
991733bc3747f242ddaea5b953d27f514253843e31Chandler Carruth  } else if (const LValueReferenceType *Ty = QT->getAs<LValueReferenceType>()) {
100c3f8c0731ef59ba79753f89f1c108b8134f6ae83Chris Lattner    QT = Context.getLValueReferenceType(Desugar(Context, Ty->getPointeeType(),
101c3f8c0731ef59ba79753f89f1c108b8134f6ae83Chris Lattner                                                ShouldAKA));
10279a9a3417929e340e84dcbc06ed9c3a277cad959Douglas Gregor  }
1031733bc3747f242ddaea5b953d27f514253843e31Chandler Carruth
1041733bc3747f242ddaea5b953d27f514253843e31Chandler Carruth  return QC.apply(QT);
10579a9a3417929e340e84dcbc06ed9c3a277cad959Douglas Gregor}
10679a9a3417929e340e84dcbc06ed9c3a277cad959Douglas Gregor
10779a9a3417929e340e84dcbc06ed9c3a277cad959Douglas Gregor/// \brief Convert the given type to a string suitable for printing as part of
1081733bc3747f242ddaea5b953d27f514253843e31Chandler Carruth/// a diagnostic.
1091733bc3747f242ddaea5b953d27f514253843e31Chandler Carruth///
1101733bc3747f242ddaea5b953d27f514253843e31Chandler Carruth/// There are three main criteria when determining whether we should have an
1111733bc3747f242ddaea5b953d27f514253843e31Chandler Carruth/// a.k.a. clause when pretty-printing a type:
1121733bc3747f242ddaea5b953d27f514253843e31Chandler Carruth///
1131733bc3747f242ddaea5b953d27f514253843e31Chandler Carruth/// 1) Some types provide very minimal sugar that doesn't impede the
1141733bc3747f242ddaea5b953d27f514253843e31Chandler Carruth///    user's understanding --- for example, elaborated type
1151733bc3747f242ddaea5b953d27f514253843e31Chandler Carruth///    specifiers.  If this is all the sugar we see, we don't want an
1161733bc3747f242ddaea5b953d27f514253843e31Chandler Carruth///    a.k.a. clause.
1171733bc3747f242ddaea5b953d27f514253843e31Chandler Carruth/// 2) Some types are technically sugared but are much more familiar
1181733bc3747f242ddaea5b953d27f514253843e31Chandler Carruth///    when seen in their sugared form --- for example, va_list,
1191733bc3747f242ddaea5b953d27f514253843e31Chandler Carruth///    vector types, and the magic Objective C types.  We don't
1201733bc3747f242ddaea5b953d27f514253843e31Chandler Carruth///    want to desugar these, even if we do produce an a.k.a. clause.
1211733bc3747f242ddaea5b953d27f514253843e31Chandler Carruth/// 3) Some types may have already been desugared previously in this diagnostic.
1221733bc3747f242ddaea5b953d27f514253843e31Chandler Carruth///    if this is the case, doing another "aka" would just be clutter.
12379a9a3417929e340e84dcbc06ed9c3a277cad959Douglas Gregor///
12479a9a3417929e340e84dcbc06ed9c3a277cad959Douglas Gregor/// \param Context the context in which the type was allocated
12579a9a3417929e340e84dcbc06ed9c3a277cad959Douglas Gregor/// \param Ty the type to print
12679a9a3417929e340e84dcbc06ed9c3a277cad959Douglas Gregorstatic std::string
12779a9a3417929e340e84dcbc06ed9c3a277cad959Douglas GregorConvertTypeToDiagnosticString(ASTContext &Context, QualType Ty,
12879a9a3417929e340e84dcbc06ed9c3a277cad959Douglas Gregor                              const Diagnostic::ArgumentValue *PrevArgs,
12979a9a3417929e340e84dcbc06ed9c3a277cad959Douglas Gregor                              unsigned NumPrevArgs) {
13079a9a3417929e340e84dcbc06ed9c3a277cad959Douglas Gregor  // FIXME: Playing with std::string is really slow.
13179a9a3417929e340e84dcbc06ed9c3a277cad959Douglas Gregor  std::string S = Ty.getAsString(Context.PrintingPolicy);
1321733bc3747f242ddaea5b953d27f514253843e31Chandler Carruth
1331733bc3747f242ddaea5b953d27f514253843e31Chandler Carruth  // Check to see if we already desugared this type in this
1341733bc3747f242ddaea5b953d27f514253843e31Chandler Carruth  // diagnostic.  If so, don't do it again.
1351733bc3747f242ddaea5b953d27f514253843e31Chandler Carruth  bool Repeated = false;
1361733bc3747f242ddaea5b953d27f514253843e31Chandler Carruth  for (unsigned i = 0; i != NumPrevArgs; ++i) {
1371733bc3747f242ddaea5b953d27f514253843e31Chandler Carruth    // TODO: Handle ak_declcontext case.
1381733bc3747f242ddaea5b953d27f514253843e31Chandler Carruth    if (PrevArgs[i].first == Diagnostic::ak_qualtype) {
1391733bc3747f242ddaea5b953d27f514253843e31Chandler Carruth      void *Ptr = (void*)PrevArgs[i].second;
1401733bc3747f242ddaea5b953d27f514253843e31Chandler Carruth      QualType PrevTy(QualType::getFromOpaquePtr(Ptr));
1411733bc3747f242ddaea5b953d27f514253843e31Chandler Carruth      if (PrevTy == Ty) {
1421733bc3747f242ddaea5b953d27f514253843e31Chandler Carruth        Repeated = true;
1431733bc3747f242ddaea5b953d27f514253843e31Chandler Carruth        break;
1441733bc3747f242ddaea5b953d27f514253843e31Chandler Carruth      }
1451733bc3747f242ddaea5b953d27f514253843e31Chandler Carruth    }
1461733bc3747f242ddaea5b953d27f514253843e31Chandler Carruth  }
1471733bc3747f242ddaea5b953d27f514253843e31Chandler Carruth
14879a9a3417929e340e84dcbc06ed9c3a277cad959Douglas Gregor  // Consider producing an a.k.a. clause if removing all the direct
14979a9a3417929e340e84dcbc06ed9c3a277cad959Douglas Gregor  // sugar gives us something "significantly different".
1501733bc3747f242ddaea5b953d27f514253843e31Chandler Carruth  if (!Repeated) {
1511733bc3747f242ddaea5b953d27f514253843e31Chandler Carruth    bool ShouldAKA = false;
1521733bc3747f242ddaea5b953d27f514253843e31Chandler Carruth    QualType DesugaredTy = Desugar(Context, Ty, ShouldAKA);
1531733bc3747f242ddaea5b953d27f514253843e31Chandler Carruth    if (ShouldAKA) {
154c3f8c0731ef59ba79753f89f1c108b8134f6ae83Chris Lattner      S = "'" + S + "' (aka '";
155c3f8c0731ef59ba79753f89f1c108b8134f6ae83Chris Lattner      S += DesugaredTy.getAsString(Context.PrintingPolicy);
156c3f8c0731ef59ba79753f89f1c108b8134f6ae83Chris Lattner      S += "')";
157c3f8c0731ef59ba79753f89f1c108b8134f6ae83Chris Lattner      return S;
1581733bc3747f242ddaea5b953d27f514253843e31Chandler Carruth    }
15979a9a3417929e340e84dcbc06ed9c3a277cad959Douglas Gregor  }
1601733bc3747f242ddaea5b953d27f514253843e31Chandler Carruth
16179a9a3417929e340e84dcbc06ed9c3a277cad959Douglas Gregor  S = "'" + S + "'";
16279a9a3417929e340e84dcbc06ed9c3a277cad959Douglas Gregor  return S;
16379a9a3417929e340e84dcbc06ed9c3a277cad959Douglas Gregor}
16479a9a3417929e340e84dcbc06ed9c3a277cad959Douglas Gregor
16579a9a3417929e340e84dcbc06ed9c3a277cad959Douglas Gregorvoid clang::FormatASTNodeDiagnosticArgument(Diagnostic::ArgumentKind Kind,
16679a9a3417929e340e84dcbc06ed9c3a277cad959Douglas Gregor                                            intptr_t Val,
16779a9a3417929e340e84dcbc06ed9c3a277cad959Douglas Gregor                                            const char *Modifier,
16879a9a3417929e340e84dcbc06ed9c3a277cad959Douglas Gregor                                            unsigned ModLen,
16979a9a3417929e340e84dcbc06ed9c3a277cad959Douglas Gregor                                            const char *Argument,
17079a9a3417929e340e84dcbc06ed9c3a277cad959Douglas Gregor                                            unsigned ArgLen,
17179a9a3417929e340e84dcbc06ed9c3a277cad959Douglas Gregor                                    const Diagnostic::ArgumentValue *PrevArgs,
17279a9a3417929e340e84dcbc06ed9c3a277cad959Douglas Gregor                                            unsigned NumPrevArgs,
17379a9a3417929e340e84dcbc06ed9c3a277cad959Douglas Gregor                                            llvm::SmallVectorImpl<char> &Output,
17479a9a3417929e340e84dcbc06ed9c3a277cad959Douglas Gregor                                            void *Cookie) {
17579a9a3417929e340e84dcbc06ed9c3a277cad959Douglas Gregor  ASTContext &Context = *static_cast<ASTContext*>(Cookie);
17679a9a3417929e340e84dcbc06ed9c3a277cad959Douglas Gregor
17779a9a3417929e340e84dcbc06ed9c3a277cad959Douglas Gregor  std::string S;
17879a9a3417929e340e84dcbc06ed9c3a277cad959Douglas Gregor  bool NeedQuotes = true;
17979a9a3417929e340e84dcbc06ed9c3a277cad959Douglas Gregor
18079a9a3417929e340e84dcbc06ed9c3a277cad959Douglas Gregor  switch (Kind) {
18179a9a3417929e340e84dcbc06ed9c3a277cad959Douglas Gregor    default: assert(0 && "unknown ArgumentKind");
18279a9a3417929e340e84dcbc06ed9c3a277cad959Douglas Gregor    case Diagnostic::ak_qualtype: {
18379a9a3417929e340e84dcbc06ed9c3a277cad959Douglas Gregor      assert(ModLen == 0 && ArgLen == 0 &&
18479a9a3417929e340e84dcbc06ed9c3a277cad959Douglas Gregor             "Invalid modifier for QualType argument");
18579a9a3417929e340e84dcbc06ed9c3a277cad959Douglas Gregor
18679a9a3417929e340e84dcbc06ed9c3a277cad959Douglas Gregor      QualType Ty(QualType::getFromOpaquePtr(reinterpret_cast<void*>(Val)));
18779a9a3417929e340e84dcbc06ed9c3a277cad959Douglas Gregor      S = ConvertTypeToDiagnosticString(Context, Ty, PrevArgs, NumPrevArgs);
18879a9a3417929e340e84dcbc06ed9c3a277cad959Douglas Gregor      NeedQuotes = false;
18979a9a3417929e340e84dcbc06ed9c3a277cad959Douglas Gregor      break;
19079a9a3417929e340e84dcbc06ed9c3a277cad959Douglas Gregor    }
19179a9a3417929e340e84dcbc06ed9c3a277cad959Douglas Gregor    case Diagnostic::ak_declarationname: {
19279a9a3417929e340e84dcbc06ed9c3a277cad959Douglas Gregor      DeclarationName N = DeclarationName::getFromOpaqueInteger(Val);
19379a9a3417929e340e84dcbc06ed9c3a277cad959Douglas Gregor      S = N.getAsString();
19479a9a3417929e340e84dcbc06ed9c3a277cad959Douglas Gregor
19579a9a3417929e340e84dcbc06ed9c3a277cad959Douglas Gregor      if (ModLen == 9 && !memcmp(Modifier, "objcclass", 9) && ArgLen == 0)
19679a9a3417929e340e84dcbc06ed9c3a277cad959Douglas Gregor        S = '+' + S;
19779a9a3417929e340e84dcbc06ed9c3a277cad959Douglas Gregor      else if (ModLen == 12 && !memcmp(Modifier, "objcinstance", 12)
19879a9a3417929e340e84dcbc06ed9c3a277cad959Douglas Gregor                && ArgLen==0)
19979a9a3417929e340e84dcbc06ed9c3a277cad959Douglas Gregor        S = '-' + S;
20079a9a3417929e340e84dcbc06ed9c3a277cad959Douglas Gregor      else
20179a9a3417929e340e84dcbc06ed9c3a277cad959Douglas Gregor        assert(ModLen == 0 && ArgLen == 0 &&
20279a9a3417929e340e84dcbc06ed9c3a277cad959Douglas Gregor               "Invalid modifier for DeclarationName argument");
20379a9a3417929e340e84dcbc06ed9c3a277cad959Douglas Gregor      break;
20479a9a3417929e340e84dcbc06ed9c3a277cad959Douglas Gregor    }
20579a9a3417929e340e84dcbc06ed9c3a277cad959Douglas Gregor    case Diagnostic::ak_nameddecl: {
20679a9a3417929e340e84dcbc06ed9c3a277cad959Douglas Gregor      bool Qualified;
20779a9a3417929e340e84dcbc06ed9c3a277cad959Douglas Gregor      if (ModLen == 1 && Modifier[0] == 'q' && ArgLen == 0)
20879a9a3417929e340e84dcbc06ed9c3a277cad959Douglas Gregor        Qualified = true;
20979a9a3417929e340e84dcbc06ed9c3a277cad959Douglas Gregor      else {
21079a9a3417929e340e84dcbc06ed9c3a277cad959Douglas Gregor        assert(ModLen == 0 && ArgLen == 0 &&
21179a9a3417929e340e84dcbc06ed9c3a277cad959Douglas Gregor               "Invalid modifier for NamedDecl* argument");
21279a9a3417929e340e84dcbc06ed9c3a277cad959Douglas Gregor        Qualified = false;
21379a9a3417929e340e84dcbc06ed9c3a277cad959Douglas Gregor      }
21479a9a3417929e340e84dcbc06ed9c3a277cad959Douglas Gregor      reinterpret_cast<NamedDecl*>(Val)->
21579a9a3417929e340e84dcbc06ed9c3a277cad959Douglas Gregor      getNameForDiagnostic(S, Context.PrintingPolicy, Qualified);
21679a9a3417929e340e84dcbc06ed9c3a277cad959Douglas Gregor      break;
21779a9a3417929e340e84dcbc06ed9c3a277cad959Douglas Gregor    }
21879a9a3417929e340e84dcbc06ed9c3a277cad959Douglas Gregor    case Diagnostic::ak_nestednamespec: {
21979a9a3417929e340e84dcbc06ed9c3a277cad959Douglas Gregor      llvm::raw_string_ostream OS(S);
22079a9a3417929e340e84dcbc06ed9c3a277cad959Douglas Gregor      reinterpret_cast<NestedNameSpecifier*>(Val)->print(OS,
22179a9a3417929e340e84dcbc06ed9c3a277cad959Douglas Gregor                                                        Context.PrintingPolicy);
22279a9a3417929e340e84dcbc06ed9c3a277cad959Douglas Gregor      NeedQuotes = false;
22379a9a3417929e340e84dcbc06ed9c3a277cad959Douglas Gregor      break;
22479a9a3417929e340e84dcbc06ed9c3a277cad959Douglas Gregor    }
22579a9a3417929e340e84dcbc06ed9c3a277cad959Douglas Gregor    case Diagnostic::ak_declcontext: {
22679a9a3417929e340e84dcbc06ed9c3a277cad959Douglas Gregor      DeclContext *DC = reinterpret_cast<DeclContext *> (Val);
22779a9a3417929e340e84dcbc06ed9c3a277cad959Douglas Gregor      assert(DC && "Should never have a null declaration context");
22879a9a3417929e340e84dcbc06ed9c3a277cad959Douglas Gregor
22979a9a3417929e340e84dcbc06ed9c3a277cad959Douglas Gregor      if (DC->isTranslationUnit()) {
23079a9a3417929e340e84dcbc06ed9c3a277cad959Douglas Gregor        // FIXME: Get these strings from some localized place
23179a9a3417929e340e84dcbc06ed9c3a277cad959Douglas Gregor        if (Context.getLangOptions().CPlusPlus)
23279a9a3417929e340e84dcbc06ed9c3a277cad959Douglas Gregor          S = "the global namespace";
23379a9a3417929e340e84dcbc06ed9c3a277cad959Douglas Gregor        else
23479a9a3417929e340e84dcbc06ed9c3a277cad959Douglas Gregor          S = "the global scope";
23579a9a3417929e340e84dcbc06ed9c3a277cad959Douglas Gregor      } else if (TypeDecl *Type = dyn_cast<TypeDecl>(DC)) {
23679a9a3417929e340e84dcbc06ed9c3a277cad959Douglas Gregor        S = ConvertTypeToDiagnosticString(Context,
23779a9a3417929e340e84dcbc06ed9c3a277cad959Douglas Gregor                                          Context.getTypeDeclType(Type),
23879a9a3417929e340e84dcbc06ed9c3a277cad959Douglas Gregor                                          PrevArgs, NumPrevArgs);
23979a9a3417929e340e84dcbc06ed9c3a277cad959Douglas Gregor      } else {
24079a9a3417929e340e84dcbc06ed9c3a277cad959Douglas Gregor        // FIXME: Get these strings from some localized place
24179a9a3417929e340e84dcbc06ed9c3a277cad959Douglas Gregor        NamedDecl *ND = cast<NamedDecl>(DC);
24279a9a3417929e340e84dcbc06ed9c3a277cad959Douglas Gregor        if (isa<NamespaceDecl>(ND))
24379a9a3417929e340e84dcbc06ed9c3a277cad959Douglas Gregor          S += "namespace ";
24479a9a3417929e340e84dcbc06ed9c3a277cad959Douglas Gregor        else if (isa<ObjCMethodDecl>(ND))
24579a9a3417929e340e84dcbc06ed9c3a277cad959Douglas Gregor          S += "method ";
24679a9a3417929e340e84dcbc06ed9c3a277cad959Douglas Gregor        else if (isa<FunctionDecl>(ND))
24779a9a3417929e340e84dcbc06ed9c3a277cad959Douglas Gregor          S += "function ";
24879a9a3417929e340e84dcbc06ed9c3a277cad959Douglas Gregor
24979a9a3417929e340e84dcbc06ed9c3a277cad959Douglas Gregor        S += "'";
25079a9a3417929e340e84dcbc06ed9c3a277cad959Douglas Gregor        ND->getNameForDiagnostic(S, Context.PrintingPolicy, true);
25179a9a3417929e340e84dcbc06ed9c3a277cad959Douglas Gregor        S += "'";
25279a9a3417929e340e84dcbc06ed9c3a277cad959Douglas Gregor      }
25379a9a3417929e340e84dcbc06ed9c3a277cad959Douglas Gregor      NeedQuotes = false;
25479a9a3417929e340e84dcbc06ed9c3a277cad959Douglas Gregor      break;
25579a9a3417929e340e84dcbc06ed9c3a277cad959Douglas Gregor    }
25679a9a3417929e340e84dcbc06ed9c3a277cad959Douglas Gregor  }
25779a9a3417929e340e84dcbc06ed9c3a277cad959Douglas Gregor
25879a9a3417929e340e84dcbc06ed9c3a277cad959Douglas Gregor  if (NeedQuotes)
25979a9a3417929e340e84dcbc06ed9c3a277cad959Douglas Gregor    Output.push_back('\'');
26079a9a3417929e340e84dcbc06ed9c3a277cad959Douglas Gregor
26179a9a3417929e340e84dcbc06ed9c3a277cad959Douglas Gregor  Output.append(S.begin(), S.end());
26279a9a3417929e340e84dcbc06ed9c3a277cad959Douglas Gregor
26379a9a3417929e340e84dcbc06ed9c3a277cad959Douglas Gregor  if (NeedQuotes)
26479a9a3417929e340e84dcbc06ed9c3a277cad959Douglas Gregor    Output.push_back('\'');
26579a9a3417929e340e84dcbc06ed9c3a277cad959Douglas Gregor}
266