blob: b7b9df55e3d1cd183dbafb94d502ea22241c3e62 [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...
Richard Smith34b41d92011-02-20 03:19:35 +000031 if (const ElaboratedType *ET = dyn_cast<ElaboratedType>(Ty)) {
32 QT = ET->desugar();
Douglas Gregor79a9a342010-02-09 22:26:47 +000033 continue;
34 }
Abramo Bagnara075f8f12010-12-10 16:29:40 +000035 // ... or a paren type ...
Richard Smith34b41d92011-02-20 03:19:35 +000036 if (const ParenType *PT = dyn_cast<ParenType>(Ty)) {
37 QT = PT->desugar();
Abramo Bagnara075f8f12010-12-10 16:29:40 +000038 continue;
39 }
Richard Smith34b41d92011-02-20 03:19:35 +000040 // ...or a substituted template type parameter ...
41 if (const SubstTemplateTypeParmType *ST =
42 dyn_cast<SubstTemplateTypeParmType>(Ty)) {
43 QT = ST->desugar();
44 continue;
45 }
John McCall14aa2172011-03-04 04:00:19 +000046 // ...or an attributed type...
47 if (const AttributedType *AT = dyn_cast<AttributedType>(Ty)) {
48 QT = AT->desugar();
49 continue;
50 }
Richard Smith34b41d92011-02-20 03:19:35 +000051 // ... or an auto type.
52 if (const AutoType *AT = dyn_cast<AutoType>(Ty)) {
53 if (!AT->isSugared())
54 break;
55 QT = AT->desugar();
Douglas Gregor79a9a342010-02-09 22:26:47 +000056 continue;
57 }
Chandler Carruth1733bc32010-05-13 11:37:24 +000058
Douglas Gregor79a9a342010-02-09 22:26:47 +000059 // Don't desugar template specializations.
60 if (isa<TemplateSpecializationType>(Ty))
61 break;
Chandler Carruth1733bc32010-05-13 11:37:24 +000062
Douglas Gregor79a9a342010-02-09 22:26:47 +000063 // 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())
68 break;
Chandler Carruth1733bc32010-05-13 11:37:24 +000069
Douglas Gregor79a9a342010-02-09 22:26:47 +000070 // Don't desugar va_list.
71 if (QualType(Ty,0) == Context.getBuiltinVaListType())
72 break;
Chandler Carruth1733bc32010-05-13 11:37:24 +000073
Douglas Gregor79a9a342010-02-09 22:26:47 +000074 // Otherwise, do a single-step desugar.
75 QualType Underlying;
76 bool IsSugar = false;
77 switch (Ty->getTypeClass()) {
78#define ABSTRACT_TYPE(Class, Base)
79#define TYPE(Class, Base) \
80case Type::Class: { \
81const Class##Type *CTy = cast<Class##Type>(Ty); \
82if (CTy->isSugared()) { \
83IsSugar = true; \
84Underlying = CTy->desugar(); \
85} \
86break; \
87}
88#include "clang/AST/TypeNodes.def"
89 }
Chandler Carruth1733bc32010-05-13 11:37:24 +000090
Douglas Gregor79a9a342010-02-09 22:26:47 +000091 // If it wasn't sugared, we're done.
92 if (!IsSugar)
93 break;
Chandler Carruth1733bc32010-05-13 11:37:24 +000094
Douglas Gregor79a9a342010-02-09 22:26:47 +000095 // 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))
98 break;
Chandler Carruth1733bc32010-05-13 11:37:24 +000099
Douglas Gregor79a9a342010-02-09 22:26:47 +0000100 // Don't desugar through the primary typedef of an anonymous type.
Chris Lattnerc3f8c072010-09-04 23:16:01 +0000101 if (const TagType *UTT = Underlying->getAs<TagType>())
102 if (const TypedefType *QTT = dyn_cast<TypedefType>(QT))
103 if (UTT->getDecl()->getTypedefForAnonDecl() == QTT->getDecl())
104 break;
Chandler Carruth1733bc32010-05-13 11:37:24 +0000105
106 // Record that we actually looked through an opaque type here.
107 ShouldAKA = true;
Douglas Gregor79a9a342010-02-09 22:26:47 +0000108 QT = Underlying;
Douglas Gregor79a9a342010-02-09 22:26:47 +0000109 }
Chandler Carruth1733bc32010-05-13 11:37:24 +0000110
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>()) {
Chris Lattnerc3f8c072010-09-04 23:16:01 +0000114 QT = Context.getPointerType(Desugar(Context, Ty->getPointeeType(),
115 ShouldAKA));
Chandler Carruth1733bc32010-05-13 11:37:24 +0000116 } else if (const LValueReferenceType *Ty = QT->getAs<LValueReferenceType>()) {
Chris Lattnerc3f8c072010-09-04 23:16:01 +0000117 QT = Context.getLValueReferenceType(Desugar(Context, Ty->getPointeeType(),
118 ShouldAKA));
Douglas Gregor69d83162011-01-20 16:08:06 +0000119 } else if (const RValueReferenceType *Ty = QT->getAs<RValueReferenceType>()) {
120 QT = Context.getRValueReferenceType(Desugar(Context, Ty->getPointeeType(),
121 ShouldAKA));
Douglas Gregor79a9a342010-02-09 22:26:47 +0000122 }
Chandler Carruth1733bc32010-05-13 11:37:24 +0000123
John McCall49f4e1c2010-12-10 11:01:00 +0000124 return QC.apply(Context, QT);
Douglas Gregor79a9a342010-02-09 22:26:47 +0000125}
126
127/// \brief Convert the given type to a string suitable for printing as part of
Chandler Carruth1733bc32010-05-13 11:37:24 +0000128/// a diagnostic.
129///
130/// There are three main criteria when determining whether we should have an
131/// a.k.a. clause when pretty-printing a type:
132///
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
136/// a.k.a. clause.
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.
Douglas Gregor79a9a342010-02-09 22:26:47 +0000143///
144/// \param Context the context in which the type was allocated
145/// \param Ty the type to print
146static std::string
147ConvertTypeToDiagnosticString(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);
Chandler Carruth1733bc32010-05-13 11:37:24 +0000152
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));
161 if (PrevTy == Ty) {
162 Repeated = true;
163 break;
164 }
165 }
166 }
167
Douglas Gregor79a9a342010-02-09 22:26:47 +0000168 // Consider producing an a.k.a. clause if removing all the direct
169 // sugar gives us something "significantly different".
Chandler Carruth1733bc32010-05-13 11:37:24 +0000170 if (!Repeated) {
171 bool ShouldAKA = false;
172 QualType DesugaredTy = Desugar(Context, Ty, ShouldAKA);
173 if (ShouldAKA) {
Chris Lattnerc3f8c072010-09-04 23:16:01 +0000174 S = "'" + S + "' (aka '";
175 S += DesugaredTy.getAsString(Context.PrintingPolicy);
176 S += "')";
177 return S;
Chandler Carruth1733bc32010-05-13 11:37:24 +0000178 }
Douglas Gregor79a9a342010-02-09 22:26:47 +0000179 }
Chandler Carruth1733bc32010-05-13 11:37:24 +0000180
Douglas Gregor79a9a342010-02-09 22:26:47 +0000181 S = "'" + S + "'";
182 return S;
183}
184
185void clang::FormatASTNodeDiagnosticArgument(Diagnostic::ArgumentKind Kind,
186 intptr_t Val,
187 const char *Modifier,
188 unsigned ModLen,
189 const char *Argument,
190 unsigned ArgLen,
191 const Diagnostic::ArgumentValue *PrevArgs,
192 unsigned NumPrevArgs,
193 llvm::SmallVectorImpl<char> &Output,
194 void *Cookie) {
195 ASTContext &Context = *static_cast<ASTContext*>(Cookie);
196
197 std::string S;
198 bool NeedQuotes = true;
199
200 switch (Kind) {
201 default: assert(0 && "unknown ArgumentKind");
202 case Diagnostic::ak_qualtype: {
203 assert(ModLen == 0 && ArgLen == 0 &&
204 "Invalid modifier for QualType argument");
205
206 QualType Ty(QualType::getFromOpaquePtr(reinterpret_cast<void*>(Val)));
207 S = ConvertTypeToDiagnosticString(Context, Ty, PrevArgs, NumPrevArgs);
208 NeedQuotes = false;
209 break;
210 }
211 case Diagnostic::ak_declarationname: {
212 DeclarationName N = DeclarationName::getFromOpaqueInteger(Val);
213 S = N.getAsString();
214
215 if (ModLen == 9 && !memcmp(Modifier, "objcclass", 9) && ArgLen == 0)
216 S = '+' + S;
217 else if (ModLen == 12 && !memcmp(Modifier, "objcinstance", 12)
218 && ArgLen==0)
219 S = '-' + S;
220 else
221 assert(ModLen == 0 && ArgLen == 0 &&
222 "Invalid modifier for DeclarationName argument");
223 break;
224 }
225 case Diagnostic::ak_nameddecl: {
226 bool Qualified;
227 if (ModLen == 1 && Modifier[0] == 'q' && ArgLen == 0)
228 Qualified = true;
229 else {
230 assert(ModLen == 0 && ArgLen == 0 &&
231 "Invalid modifier for NamedDecl* argument");
232 Qualified = false;
233 }
234 reinterpret_cast<NamedDecl*>(Val)->
235 getNameForDiagnostic(S, Context.PrintingPolicy, Qualified);
236 break;
237 }
238 case Diagnostic::ak_nestednamespec: {
239 llvm::raw_string_ostream OS(S);
240 reinterpret_cast<NestedNameSpecifier*>(Val)->print(OS,
241 Context.PrintingPolicy);
242 NeedQuotes = false;
243 break;
244 }
245 case Diagnostic::ak_declcontext: {
246 DeclContext *DC = reinterpret_cast<DeclContext *> (Val);
247 assert(DC && "Should never have a null declaration context");
248
249 if (DC->isTranslationUnit()) {
250 // FIXME: Get these strings from some localized place
251 if (Context.getLangOptions().CPlusPlus)
252 S = "the global namespace";
253 else
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);
259 } else {
260 // FIXME: Get these strings from some localized place
261 NamedDecl *ND = cast<NamedDecl>(DC);
262 if (isa<NamespaceDecl>(ND))
263 S += "namespace ";
264 else if (isa<ObjCMethodDecl>(ND))
265 S += "method ";
266 else if (isa<FunctionDecl>(ND))
267 S += "function ";
268
269 S += "'";
270 ND->getNameForDiagnostic(S, Context.PrintingPolicy, true);
271 S += "'";
272 }
273 NeedQuotes = false;
274 break;
275 }
276 }
277
278 if (NeedQuotes)
279 Output.push_back('\'');
280
281 Output.append(S.begin(), S.end());
282
283 if (NeedQuotes)
284 Output.push_back('\'');
285}