1 //===--- ASTDiagnostic.cpp - Diagnostic Printing Hooks for AST Nodes ------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file implements a diagnostic formatting hook for AST elements.
12 //===----------------------------------------------------------------------===//
13 #include "clang/AST/ASTDiagnostic.h"
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"
20 using namespace clang;
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;
28 const Type *Ty = QC.strip(QT);
30 // Don't aka just because we saw an elaborated type...
31 if (const ElaboratedType *ET = dyn_cast<ElaboratedType>(Ty)) {
35 // ... or a paren type ...
36 if (const ParenType *PT = dyn_cast<ParenType>(Ty)) {
40 // ...or a substituted template type parameter ...
41 if (const SubstTemplateTypeParmType *ST =
42 dyn_cast<SubstTemplateTypeParmType>(Ty)) {
46 // ...or an attributed type...
47 if (const AttributedType *AT = dyn_cast<AttributedType>(Ty)) {
51 // ... or an auto type.
52 if (const AutoType *AT = dyn_cast<AutoType>(Ty)) {
59 // Don't desugar template specializations.
60 if (isa<TemplateSpecializationType>(Ty))
63 // Don't desugar magic Objective-C types.
64 if (QualType(Ty,0) == Context.getObjCIdType() ||
65 QualType(Ty,0) == Context.getObjCClassType() ||
66 QualType(Ty,0) == Context.getObjCSelType() ||
67 QualType(Ty,0) == Context.getObjCProtoType())
70 // Don't desugar va_list.
71 if (QualType(Ty,0) == Context.getBuiltinVaListType())
74 // Otherwise, do a single-step desugar.
77 switch (Ty->getTypeClass()) {
78 #define ABSTRACT_TYPE(Class, Base)
79 #define TYPE(Class, Base) \
81 const Class##Type *CTy = cast<Class##Type>(Ty); \
82 if (CTy->isSugared()) { \
84 Underlying = CTy->desugar(); \
88 #include "clang/AST/TypeNodes.def"
91 // If it wasn't sugared, we're done.
95 // If the desugared type is a vector type, we don't want to expand
96 // it, it will turn into an attribute mess. People want their "vec4".
97 if (isa<VectorType>(Underlying))
100 // Don't desugar through the primary typedef of an anonymous type.
101 if (const TagType *UTT = Underlying->getAs<TagType>())
102 if (const TypedefType *QTT = dyn_cast<TypedefType>(QT))
103 if (UTT->getDecl()->getTypedefNameForAnonDecl() == QTT->getDecl())
106 // Record that we actually looked through an opaque type here.
111 // If we have a pointer-like type, desugar the pointee as well.
112 // FIXME: Handle other pointer-like types.
113 if (const PointerType *Ty = QT->getAs<PointerType>()) {
114 QT = Context.getPointerType(Desugar(Context, Ty->getPointeeType(),
116 } else if (const LValueReferenceType *Ty = QT->getAs<LValueReferenceType>()) {
117 QT = Context.getLValueReferenceType(Desugar(Context, Ty->getPointeeType(),
119 } else if (const RValueReferenceType *Ty = QT->getAs<RValueReferenceType>()) {
120 QT = Context.getRValueReferenceType(Desugar(Context, Ty->getPointeeType(),
124 return QC.apply(Context, QT);
127 /// \brief Convert the given type to a string suitable for printing as part of
130 /// There are three main criteria when determining whether we should have an
131 /// a.k.a. clause when pretty-printing a type:
133 /// 1) Some types provide very minimal sugar that doesn't impede the
134 /// user's understanding --- for example, elaborated type
135 /// specifiers. If this is all the sugar we see, we don't want an
137 /// 2) Some types are technically sugared but are much more familiar
138 /// when seen in their sugared form --- for example, va_list,
139 /// vector types, and the magic Objective C types. We don't
140 /// want to desugar these, even if we do produce an a.k.a. clause.
141 /// 3) Some types may have already been desugared previously in this diagnostic.
142 /// if this is the case, doing another "aka" would just be clutter.
144 /// \param Context the context in which the type was allocated
145 /// \param Ty the type to print
147 ConvertTypeToDiagnosticString(ASTContext &Context, QualType Ty,
148 const Diagnostic::ArgumentValue *PrevArgs,
149 unsigned NumPrevArgs) {
150 // FIXME: Playing with std::string is really slow.
151 std::string S = Ty.getAsString(Context.PrintingPolicy);
153 // Check to see if we already desugared this type in this
154 // diagnostic. If so, don't do it again.
155 bool Repeated = false;
156 for (unsigned i = 0; i != NumPrevArgs; ++i) {
157 // TODO: Handle ak_declcontext case.
158 if (PrevArgs[i].first == Diagnostic::ak_qualtype) {
159 void *Ptr = (void*)PrevArgs[i].second;
160 QualType PrevTy(QualType::getFromOpaquePtr(Ptr));
168 // Consider producing an a.k.a. clause if removing all the direct
169 // sugar gives us something "significantly different".
171 bool ShouldAKA = false;
172 QualType DesugaredTy = Desugar(Context, Ty, ShouldAKA);
174 S = "'" + S + "' (aka '";
175 S += DesugaredTy.getAsString(Context.PrintingPolicy);
185 void clang::FormatASTNodeDiagnosticArgument(Diagnostic::ArgumentKind Kind,
187 const char *Modifier,
189 const char *Argument,
191 const Diagnostic::ArgumentValue *PrevArgs,
192 unsigned NumPrevArgs,
193 llvm::SmallVectorImpl<char> &Output,
195 ASTContext &Context = *static_cast<ASTContext*>(Cookie);
198 bool NeedQuotes = true;
201 default: assert(0 && "unknown ArgumentKind");
202 case Diagnostic::ak_qualtype: {
203 assert(ModLen == 0 && ArgLen == 0 &&
204 "Invalid modifier for QualType argument");
206 QualType Ty(QualType::getFromOpaquePtr(reinterpret_cast<void*>(Val)));
207 S = ConvertTypeToDiagnosticString(Context, Ty, PrevArgs, NumPrevArgs);
211 case Diagnostic::ak_declarationname: {
212 DeclarationName N = DeclarationName::getFromOpaqueInteger(Val);
215 if (ModLen == 9 && !memcmp(Modifier, "objcclass", 9) && ArgLen == 0)
217 else if (ModLen == 12 && !memcmp(Modifier, "objcinstance", 12)
221 assert(ModLen == 0 && ArgLen == 0 &&
222 "Invalid modifier for DeclarationName argument");
225 case Diagnostic::ak_nameddecl: {
227 if (ModLen == 1 && Modifier[0] == 'q' && ArgLen == 0)
230 assert(ModLen == 0 && ArgLen == 0 &&
231 "Invalid modifier for NamedDecl* argument");
234 reinterpret_cast<NamedDecl*>(Val)->
235 getNameForDiagnostic(S, Context.PrintingPolicy, Qualified);
238 case Diagnostic::ak_nestednamespec: {
239 llvm::raw_string_ostream OS(S);
240 reinterpret_cast<NestedNameSpecifier*>(Val)->print(OS,
241 Context.PrintingPolicy);
245 case Diagnostic::ak_declcontext: {
246 DeclContext *DC = reinterpret_cast<DeclContext *> (Val);
247 assert(DC && "Should never have a null declaration context");
249 if (DC->isTranslationUnit()) {
250 // FIXME: Get these strings from some localized place
251 if (Context.getLangOptions().CPlusPlus)
252 S = "the global namespace";
254 S = "the global scope";
255 } else if (TypeDecl *Type = dyn_cast<TypeDecl>(DC)) {
256 S = ConvertTypeToDiagnosticString(Context,
257 Context.getTypeDeclType(Type),
258 PrevArgs, NumPrevArgs);
260 // FIXME: Get these strings from some localized place
261 NamedDecl *ND = cast<NamedDecl>(DC);
262 if (isa<NamespaceDecl>(ND))
264 else if (isa<ObjCMethodDecl>(ND))
266 else if (isa<FunctionDecl>(ND))
270 ND->getNameForDiagnostic(S, Context.PrintingPolicy, true);
279 Output.push_back('\'');
281 Output.append(S.begin(), S.end());
284 Output.push_back('\'');