blob: bd128f0514a5cf4178663e811151d0316798c52c [file] [log] [blame]
Douglas Gregor79a9a342010-02-09 22:26:47 +00001//===--- 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
20using namespace clang;
21
Chandler Carruth1733bc32010-05-13 11:37:24 +000022// Returns a desugared version of the QualType, and marks ShouldAKA as true
23// whenever we remove significant sugar from the type.
24static QualType Desugar(ASTContext &Context, QualType QT, bool &ShouldAKA) {
25 QualifierCollector QC;
26
Douglas Gregor79a9a342010-02-09 22:26:47 +000027 while (true) {
Chandler Carruth1733bc32010-05-13 11:37:24 +000028 const Type *Ty = QC.strip(QT);
29
Douglas Gregor79a9a342010-02-09 22:26:47 +000030 // Don't aka just because we saw an elaborated type...
31 if (isa<ElaboratedType>(Ty)) {
32 QT = cast<ElaboratedType>(Ty)->desugar();
33 continue;
34 }
Abramo Bagnara075f8f12010-12-10 16:29:40 +000035 // ... or a paren type ...
36 if (isa<ParenType>(Ty)) {
37 QT = cast<ParenType>(Ty)->desugar();
38 continue;
39 }
Douglas Gregor79a9a342010-02-09 22:26:47 +000040 // ...or a substituted template type parameter.
41 if (isa<SubstTemplateTypeParmType>(Ty)) {
42 QT = cast<SubstTemplateTypeParmType>(Ty)->desugar();
43 continue;
44 }
Chandler Carruth1733bc32010-05-13 11:37:24 +000045
Douglas Gregor79a9a342010-02-09 22:26:47 +000046 // Don't desugar template specializations.
47 if (isa<TemplateSpecializationType>(Ty))
48 break;
Chandler Carruth1733bc32010-05-13 11:37:24 +000049
Douglas Gregor79a9a342010-02-09 22:26:47 +000050 // Don't desugar magic Objective-C types.
51 if (QualType(Ty,0) == Context.getObjCIdType() ||
52 QualType(Ty,0) == Context.getObjCClassType() ||
53 QualType(Ty,0) == Context.getObjCSelType() ||
54 QualType(Ty,0) == Context.getObjCProtoType())
55 break;
Chandler Carruth1733bc32010-05-13 11:37:24 +000056
Douglas Gregor79a9a342010-02-09 22:26:47 +000057 // Don't desugar va_list.
58 if (QualType(Ty,0) == Context.getBuiltinVaListType())
59 break;
Chandler Carruth1733bc32010-05-13 11:37:24 +000060
Douglas Gregor79a9a342010-02-09 22:26:47 +000061 // Otherwise, do a single-step desugar.
62 QualType Underlying;
63 bool IsSugar = false;
64 switch (Ty->getTypeClass()) {
65#define ABSTRACT_TYPE(Class, Base)
66#define TYPE(Class, Base) \
67case Type::Class: { \
68const Class##Type *CTy = cast<Class##Type>(Ty); \
69if (CTy->isSugared()) { \
70IsSugar = true; \
71Underlying = CTy->desugar(); \
72} \
73break; \
74}
75#include "clang/AST/TypeNodes.def"
76 }
Chandler Carruth1733bc32010-05-13 11:37:24 +000077
Douglas Gregor79a9a342010-02-09 22:26:47 +000078 // If it wasn't sugared, we're done.
79 if (!IsSugar)
80 break;
Chandler Carruth1733bc32010-05-13 11:37:24 +000081
Douglas Gregor79a9a342010-02-09 22:26:47 +000082 // If the desugared type is a vector type, we don't want to expand
83 // it, it will turn into an attribute mess. People want their "vec4".
84 if (isa<VectorType>(Underlying))
85 break;
Chandler Carruth1733bc32010-05-13 11:37:24 +000086
Douglas Gregor79a9a342010-02-09 22:26:47 +000087 // Don't desugar through the primary typedef of an anonymous type.
Chris Lattnerc3f8c072010-09-04 23:16:01 +000088 if (const TagType *UTT = Underlying->getAs<TagType>())
89 if (const TypedefType *QTT = dyn_cast<TypedefType>(QT))
90 if (UTT->getDecl()->getTypedefForAnonDecl() == QTT->getDecl())
91 break;
Chandler Carruth1733bc32010-05-13 11:37:24 +000092
93 // Record that we actually looked through an opaque type here.
94 ShouldAKA = true;
Douglas Gregor79a9a342010-02-09 22:26:47 +000095 QT = Underlying;
Douglas Gregor79a9a342010-02-09 22:26:47 +000096 }
Chandler Carruth1733bc32010-05-13 11:37:24 +000097
98 // If we have a pointer-like type, desugar the pointee as well.
99 // FIXME: Handle other pointer-like types.
100 if (const PointerType *Ty = QT->getAs<PointerType>()) {
Chris Lattnerc3f8c072010-09-04 23:16:01 +0000101 QT = Context.getPointerType(Desugar(Context, Ty->getPointeeType(),
102 ShouldAKA));
Chandler Carruth1733bc32010-05-13 11:37:24 +0000103 } else if (const LValueReferenceType *Ty = QT->getAs<LValueReferenceType>()) {
Chris Lattnerc3f8c072010-09-04 23:16:01 +0000104 QT = Context.getLValueReferenceType(Desugar(Context, Ty->getPointeeType(),
105 ShouldAKA));
Douglas Gregor79a9a342010-02-09 22:26:47 +0000106 }
Chandler Carruth1733bc32010-05-13 11:37:24 +0000107
John McCall49f4e1c2010-12-10 11:01:00 +0000108 return QC.apply(Context, QT);
Douglas Gregor79a9a342010-02-09 22:26:47 +0000109}
110
111/// \brief Convert the given type to a string suitable for printing as part of
Chandler Carruth1733bc32010-05-13 11:37:24 +0000112/// a diagnostic.
113///
114/// There are three main criteria when determining whether we should have an
115/// a.k.a. clause when pretty-printing a type:
116///
117/// 1) Some types provide very minimal sugar that doesn't impede the
118/// user's understanding --- for example, elaborated type
119/// specifiers. If this is all the sugar we see, we don't want an
120/// a.k.a. clause.
121/// 2) Some types are technically sugared but are much more familiar
122/// when seen in their sugared form --- for example, va_list,
123/// vector types, and the magic Objective C types. We don't
124/// want to desugar these, even if we do produce an a.k.a. clause.
125/// 3) Some types may have already been desugared previously in this diagnostic.
126/// if this is the case, doing another "aka" would just be clutter.
Douglas Gregor79a9a342010-02-09 22:26:47 +0000127///
128/// \param Context the context in which the type was allocated
129/// \param Ty the type to print
130static std::string
131ConvertTypeToDiagnosticString(ASTContext &Context, QualType Ty,
132 const Diagnostic::ArgumentValue *PrevArgs,
133 unsigned NumPrevArgs) {
134 // FIXME: Playing with std::string is really slow.
135 std::string S = Ty.getAsString(Context.PrintingPolicy);
Chandler Carruth1733bc32010-05-13 11:37:24 +0000136
137 // Check to see if we already desugared this type in this
138 // diagnostic. If so, don't do it again.
139 bool Repeated = false;
140 for (unsigned i = 0; i != NumPrevArgs; ++i) {
141 // TODO: Handle ak_declcontext case.
142 if (PrevArgs[i].first == Diagnostic::ak_qualtype) {
143 void *Ptr = (void*)PrevArgs[i].second;
144 QualType PrevTy(QualType::getFromOpaquePtr(Ptr));
145 if (PrevTy == Ty) {
146 Repeated = true;
147 break;
148 }
149 }
150 }
151
Douglas Gregor79a9a342010-02-09 22:26:47 +0000152 // Consider producing an a.k.a. clause if removing all the direct
153 // sugar gives us something "significantly different".
Chandler Carruth1733bc32010-05-13 11:37:24 +0000154 if (!Repeated) {
155 bool ShouldAKA = false;
156 QualType DesugaredTy = Desugar(Context, Ty, ShouldAKA);
157 if (ShouldAKA) {
Chris Lattnerc3f8c072010-09-04 23:16:01 +0000158 S = "'" + S + "' (aka '";
159 S += DesugaredTy.getAsString(Context.PrintingPolicy);
160 S += "')";
161 return S;
Chandler Carruth1733bc32010-05-13 11:37:24 +0000162 }
Douglas Gregor79a9a342010-02-09 22:26:47 +0000163 }
Chandler Carruth1733bc32010-05-13 11:37:24 +0000164
Douglas Gregor79a9a342010-02-09 22:26:47 +0000165 S = "'" + S + "'";
166 return S;
167}
168
169void clang::FormatASTNodeDiagnosticArgument(Diagnostic::ArgumentKind Kind,
170 intptr_t Val,
171 const char *Modifier,
172 unsigned ModLen,
173 const char *Argument,
174 unsigned ArgLen,
175 const Diagnostic::ArgumentValue *PrevArgs,
176 unsigned NumPrevArgs,
177 llvm::SmallVectorImpl<char> &Output,
178 void *Cookie) {
179 ASTContext &Context = *static_cast<ASTContext*>(Cookie);
180
181 std::string S;
182 bool NeedQuotes = true;
183
184 switch (Kind) {
185 default: assert(0 && "unknown ArgumentKind");
186 case Diagnostic::ak_qualtype: {
187 assert(ModLen == 0 && ArgLen == 0 &&
188 "Invalid modifier for QualType argument");
189
190 QualType Ty(QualType::getFromOpaquePtr(reinterpret_cast<void*>(Val)));
191 S = ConvertTypeToDiagnosticString(Context, Ty, PrevArgs, NumPrevArgs);
192 NeedQuotes = false;
193 break;
194 }
195 case Diagnostic::ak_declarationname: {
196 DeclarationName N = DeclarationName::getFromOpaqueInteger(Val);
197 S = N.getAsString();
198
199 if (ModLen == 9 && !memcmp(Modifier, "objcclass", 9) && ArgLen == 0)
200 S = '+' + S;
201 else if (ModLen == 12 && !memcmp(Modifier, "objcinstance", 12)
202 && ArgLen==0)
203 S = '-' + S;
204 else
205 assert(ModLen == 0 && ArgLen == 0 &&
206 "Invalid modifier for DeclarationName argument");
207 break;
208 }
209 case Diagnostic::ak_nameddecl: {
210 bool Qualified;
211 if (ModLen == 1 && Modifier[0] == 'q' && ArgLen == 0)
212 Qualified = true;
213 else {
214 assert(ModLen == 0 && ArgLen == 0 &&
215 "Invalid modifier for NamedDecl* argument");
216 Qualified = false;
217 }
218 reinterpret_cast<NamedDecl*>(Val)->
219 getNameForDiagnostic(S, Context.PrintingPolicy, Qualified);
220 break;
221 }
222 case Diagnostic::ak_nestednamespec: {
223 llvm::raw_string_ostream OS(S);
224 reinterpret_cast<NestedNameSpecifier*>(Val)->print(OS,
225 Context.PrintingPolicy);
226 NeedQuotes = false;
227 break;
228 }
229 case Diagnostic::ak_declcontext: {
230 DeclContext *DC = reinterpret_cast<DeclContext *> (Val);
231 assert(DC && "Should never have a null declaration context");
232
233 if (DC->isTranslationUnit()) {
234 // FIXME: Get these strings from some localized place
235 if (Context.getLangOptions().CPlusPlus)
236 S = "the global namespace";
237 else
238 S = "the global scope";
239 } else if (TypeDecl *Type = dyn_cast<TypeDecl>(DC)) {
240 S = ConvertTypeToDiagnosticString(Context,
241 Context.getTypeDeclType(Type),
242 PrevArgs, NumPrevArgs);
243 } else {
244 // FIXME: Get these strings from some localized place
245 NamedDecl *ND = cast<NamedDecl>(DC);
246 if (isa<NamespaceDecl>(ND))
247 S += "namespace ";
248 else if (isa<ObjCMethodDecl>(ND))
249 S += "method ";
250 else if (isa<FunctionDecl>(ND))
251 S += "function ";
252
253 S += "'";
254 ND->getNameForDiagnostic(S, Context.PrintingPolicy, true);
255 S += "'";
256 }
257 NeedQuotes = false;
258 break;
259 }
260 }
261
262 if (NeedQuotes)
263 Output.push_back('\'');
264
265 Output.append(S.begin(), S.end());
266
267 if (NeedQuotes)
268 Output.push_back('\'');
269}