blob: c93bbe60abb080d38866a5a8ee727cc295120d6d [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
Richard Smith3e4c6c42011-05-05 21:57:07 +000059 // Don't desugar template specializations, unless it's an alias template.
60 if (const TemplateSpecializationType *TST
61 = dyn_cast<TemplateSpecializationType>(Ty))
62 if (!TST->isTypeAlias())
63 break;
Chandler Carruth1733bc32010-05-13 11:37:24 +000064
Douglas Gregor79a9a342010-02-09 22:26:47 +000065 // Don't desugar magic Objective-C types.
66 if (QualType(Ty,0) == Context.getObjCIdType() ||
67 QualType(Ty,0) == Context.getObjCClassType() ||
68 QualType(Ty,0) == Context.getObjCSelType() ||
69 QualType(Ty,0) == Context.getObjCProtoType())
70 break;
Chandler Carruth1733bc32010-05-13 11:37:24 +000071
Douglas Gregor79a9a342010-02-09 22:26:47 +000072 // Don't desugar va_list.
73 if (QualType(Ty,0) == Context.getBuiltinVaListType())
74 break;
Chandler Carruth1733bc32010-05-13 11:37:24 +000075
Douglas Gregor79a9a342010-02-09 22:26:47 +000076 // Otherwise, do a single-step desugar.
77 QualType Underlying;
78 bool IsSugar = false;
79 switch (Ty->getTypeClass()) {
80#define ABSTRACT_TYPE(Class, Base)
81#define TYPE(Class, Base) \
82case Type::Class: { \
83const Class##Type *CTy = cast<Class##Type>(Ty); \
84if (CTy->isSugared()) { \
85IsSugar = true; \
86Underlying = CTy->desugar(); \
87} \
88break; \
89}
90#include "clang/AST/TypeNodes.def"
91 }
Chandler Carruth1733bc32010-05-13 11:37:24 +000092
Douglas Gregor79a9a342010-02-09 22:26:47 +000093 // If it wasn't sugared, we're done.
94 if (!IsSugar)
95 break;
Chandler Carruth1733bc32010-05-13 11:37:24 +000096
Douglas Gregor79a9a342010-02-09 22:26:47 +000097 // If the desugared type is a vector type, we don't want to expand
98 // it, it will turn into an attribute mess. People want their "vec4".
99 if (isa<VectorType>(Underlying))
100 break;
Chandler Carruth1733bc32010-05-13 11:37:24 +0000101
Douglas Gregor79a9a342010-02-09 22:26:47 +0000102 // Don't desugar through the primary typedef of an anonymous type.
Chris Lattnerc3f8c072010-09-04 23:16:01 +0000103 if (const TagType *UTT = Underlying->getAs<TagType>())
104 if (const TypedefType *QTT = dyn_cast<TypedefType>(QT))
Richard Smith162e1c12011-04-15 14:24:37 +0000105 if (UTT->getDecl()->getTypedefNameForAnonDecl() == QTT->getDecl())
Chris Lattnerc3f8c072010-09-04 23:16:01 +0000106 break;
Chandler Carruth1733bc32010-05-13 11:37:24 +0000107
108 // Record that we actually looked through an opaque type here.
109 ShouldAKA = true;
Douglas Gregor79a9a342010-02-09 22:26:47 +0000110 QT = Underlying;
Douglas Gregor79a9a342010-02-09 22:26:47 +0000111 }
Chandler Carruth1733bc32010-05-13 11:37:24 +0000112
113 // If we have a pointer-like type, desugar the pointee as well.
114 // FIXME: Handle other pointer-like types.
115 if (const PointerType *Ty = QT->getAs<PointerType>()) {
Chris Lattnerc3f8c072010-09-04 23:16:01 +0000116 QT = Context.getPointerType(Desugar(Context, Ty->getPointeeType(),
117 ShouldAKA));
Chandler Carruth1733bc32010-05-13 11:37:24 +0000118 } else if (const LValueReferenceType *Ty = QT->getAs<LValueReferenceType>()) {
Chris Lattnerc3f8c072010-09-04 23:16:01 +0000119 QT = Context.getLValueReferenceType(Desugar(Context, Ty->getPointeeType(),
120 ShouldAKA));
Douglas Gregor69d83162011-01-20 16:08:06 +0000121 } else if (const RValueReferenceType *Ty = QT->getAs<RValueReferenceType>()) {
122 QT = Context.getRValueReferenceType(Desugar(Context, Ty->getPointeeType(),
123 ShouldAKA));
Douglas Gregor79a9a342010-02-09 22:26:47 +0000124 }
Chandler Carruth1733bc32010-05-13 11:37:24 +0000125
John McCall49f4e1c2010-12-10 11:01:00 +0000126 return QC.apply(Context, QT);
Douglas Gregor79a9a342010-02-09 22:26:47 +0000127}
128
129/// \brief Convert the given type to a string suitable for printing as part of
Chandler Carruth1733bc32010-05-13 11:37:24 +0000130/// a diagnostic.
131///
Chandler Carruth0673cb32011-07-11 17:49:21 +0000132/// There are four main criteria when determining whether we should have an
Chandler Carruth1733bc32010-05-13 11:37:24 +0000133/// a.k.a. clause when pretty-printing a type:
134///
135/// 1) Some types provide very minimal sugar that doesn't impede the
136/// user's understanding --- for example, elaborated type
137/// specifiers. If this is all the sugar we see, we don't want an
138/// a.k.a. clause.
139/// 2) Some types are technically sugared but are much more familiar
140/// when seen in their sugared form --- for example, va_list,
141/// vector types, and the magic Objective C types. We don't
142/// want to desugar these, even if we do produce an a.k.a. clause.
143/// 3) Some types may have already been desugared previously in this diagnostic.
144/// if this is the case, doing another "aka" would just be clutter.
Chandler Carruth0673cb32011-07-11 17:49:21 +0000145/// 4) Two different types within the same diagnostic have the same output
146/// string. In this case, force an a.k.a with the desugared type when
147/// doing so will provide additional information.
Douglas Gregor79a9a342010-02-09 22:26:47 +0000148///
149/// \param Context the context in which the type was allocated
150/// \param Ty the type to print
Chandler Carruth0673cb32011-07-11 17:49:21 +0000151/// \param QualTypeVals pointer values to QualTypes which are used in the
152/// diagnostic message
Douglas Gregor79a9a342010-02-09 22:26:47 +0000153static std::string
154ConvertTypeToDiagnosticString(ASTContext &Context, QualType Ty,
155 const Diagnostic::ArgumentValue *PrevArgs,
Chandler Carruth0673cb32011-07-11 17:49:21 +0000156 unsigned NumPrevArgs,
Chris Lattner5f9e2722011-07-23 10:55:15 +0000157 SmallVectorImpl<intptr_t> &QualTypeVals) {
Douglas Gregor79a9a342010-02-09 22:26:47 +0000158 // FIXME: Playing with std::string is really slow.
Chandler Carruth0673cb32011-07-11 17:49:21 +0000159 bool ForceAKA = false;
160 QualType CanTy = Ty.getCanonicalType();
Douglas Gregor79a9a342010-02-09 22:26:47 +0000161 std::string S = Ty.getAsString(Context.PrintingPolicy);
Chandler Carruth0673cb32011-07-11 17:49:21 +0000162 std::string CanS = CanTy.getAsString(Context.PrintingPolicy);
163
Chris Lattner5f9e2722011-07-23 10:55:15 +0000164 for (SmallVectorImpl<intptr_t>::iterator I = QualTypeVals.begin(),
Chandler Carruth0673cb32011-07-11 17:49:21 +0000165 E = QualTypeVals.end(); I != E; ++I) {
166 QualType CompareTy =
167 QualType::getFromOpaquePtr(reinterpret_cast<void*>(*I));
168 if (CompareTy == Ty)
169 continue; // Same types
170 QualType CompareCanTy = CompareTy.getCanonicalType();
171 if (CompareCanTy == CanTy)
172 continue; // Same canonical types
173 std::string CompareS = CompareTy.getAsString(Context.PrintingPolicy);
174 if (CompareS != S)
175 continue; // Original strings are different
176 std::string CompareCanS = CompareCanTy.getAsString(Context.PrintingPolicy);
177 if (CompareCanS == CanS)
178 continue; // No new info from canonical type
179
180 ForceAKA = true;
181 break;
182 }
Chandler Carruth1733bc32010-05-13 11:37:24 +0000183
184 // Check to see if we already desugared this type in this
185 // diagnostic. If so, don't do it again.
186 bool Repeated = false;
187 for (unsigned i = 0; i != NumPrevArgs; ++i) {
188 // TODO: Handle ak_declcontext case.
189 if (PrevArgs[i].first == Diagnostic::ak_qualtype) {
190 void *Ptr = (void*)PrevArgs[i].second;
191 QualType PrevTy(QualType::getFromOpaquePtr(Ptr));
192 if (PrevTy == Ty) {
193 Repeated = true;
194 break;
195 }
196 }
197 }
198
Douglas Gregor79a9a342010-02-09 22:26:47 +0000199 // Consider producing an a.k.a. clause if removing all the direct
200 // sugar gives us something "significantly different".
Chandler Carruth1733bc32010-05-13 11:37:24 +0000201 if (!Repeated) {
202 bool ShouldAKA = false;
203 QualType DesugaredTy = Desugar(Context, Ty, ShouldAKA);
Chandler Carruth0673cb32011-07-11 17:49:21 +0000204 if (ShouldAKA || ForceAKA) {
205 if (DesugaredTy == Ty) {
206 DesugaredTy = Ty.getCanonicalType();
207 }
208 std::string akaStr = DesugaredTy.getAsString(Context.PrintingPolicy);
209 if (akaStr != S) {
210 S = "'" + S + "' (aka '" + akaStr + "')";
211 return S;
212 }
Chandler Carruth1733bc32010-05-13 11:37:24 +0000213 }
Douglas Gregor79a9a342010-02-09 22:26:47 +0000214 }
Chandler Carruth1733bc32010-05-13 11:37:24 +0000215
Douglas Gregor79a9a342010-02-09 22:26:47 +0000216 S = "'" + S + "'";
217 return S;
218}
219
Chandler Carruth0673cb32011-07-11 17:49:21 +0000220void clang::FormatASTNodeDiagnosticArgument(
221 Diagnostic::ArgumentKind Kind,
222 intptr_t Val,
223 const char *Modifier,
224 unsigned ModLen,
225 const char *Argument,
226 unsigned ArgLen,
227 const Diagnostic::ArgumentValue *PrevArgs,
228 unsigned NumPrevArgs,
Chris Lattner5f9e2722011-07-23 10:55:15 +0000229 SmallVectorImpl<char> &Output,
Chandler Carruth0673cb32011-07-11 17:49:21 +0000230 void *Cookie,
Chris Lattner5f9e2722011-07-23 10:55:15 +0000231 SmallVectorImpl<intptr_t> &QualTypeVals) {
Douglas Gregor79a9a342010-02-09 22:26:47 +0000232 ASTContext &Context = *static_cast<ASTContext*>(Cookie);
233
234 std::string S;
235 bool NeedQuotes = true;
236
237 switch (Kind) {
238 default: assert(0 && "unknown ArgumentKind");
239 case Diagnostic::ak_qualtype: {
240 assert(ModLen == 0 && ArgLen == 0 &&
241 "Invalid modifier for QualType argument");
242
243 QualType Ty(QualType::getFromOpaquePtr(reinterpret_cast<void*>(Val)));
Chandler Carruth0673cb32011-07-11 17:49:21 +0000244 S = ConvertTypeToDiagnosticString(Context, Ty, PrevArgs, NumPrevArgs,
245 QualTypeVals);
Douglas Gregor79a9a342010-02-09 22:26:47 +0000246 NeedQuotes = false;
247 break;
248 }
249 case Diagnostic::ak_declarationname: {
250 DeclarationName N = DeclarationName::getFromOpaqueInteger(Val);
251 S = N.getAsString();
252
253 if (ModLen == 9 && !memcmp(Modifier, "objcclass", 9) && ArgLen == 0)
254 S = '+' + S;
255 else if (ModLen == 12 && !memcmp(Modifier, "objcinstance", 12)
256 && ArgLen==0)
257 S = '-' + S;
258 else
259 assert(ModLen == 0 && ArgLen == 0 &&
260 "Invalid modifier for DeclarationName argument");
261 break;
262 }
263 case Diagnostic::ak_nameddecl: {
264 bool Qualified;
265 if (ModLen == 1 && Modifier[0] == 'q' && ArgLen == 0)
266 Qualified = true;
267 else {
268 assert(ModLen == 0 && ArgLen == 0 &&
269 "Invalid modifier for NamedDecl* argument");
270 Qualified = false;
271 }
272 reinterpret_cast<NamedDecl*>(Val)->
273 getNameForDiagnostic(S, Context.PrintingPolicy, Qualified);
274 break;
275 }
276 case Diagnostic::ak_nestednamespec: {
277 llvm::raw_string_ostream OS(S);
278 reinterpret_cast<NestedNameSpecifier*>(Val)->print(OS,
279 Context.PrintingPolicy);
280 NeedQuotes = false;
281 break;
282 }
283 case Diagnostic::ak_declcontext: {
284 DeclContext *DC = reinterpret_cast<DeclContext *> (Val);
285 assert(DC && "Should never have a null declaration context");
286
287 if (DC->isTranslationUnit()) {
288 // FIXME: Get these strings from some localized place
289 if (Context.getLangOptions().CPlusPlus)
290 S = "the global namespace";
291 else
292 S = "the global scope";
293 } else if (TypeDecl *Type = dyn_cast<TypeDecl>(DC)) {
294 S = ConvertTypeToDiagnosticString(Context,
295 Context.getTypeDeclType(Type),
Chandler Carruth0673cb32011-07-11 17:49:21 +0000296 PrevArgs, NumPrevArgs, QualTypeVals);
Douglas Gregor79a9a342010-02-09 22:26:47 +0000297 } else {
298 // FIXME: Get these strings from some localized place
299 NamedDecl *ND = cast<NamedDecl>(DC);
300 if (isa<NamespaceDecl>(ND))
301 S += "namespace ";
302 else if (isa<ObjCMethodDecl>(ND))
303 S += "method ";
304 else if (isa<FunctionDecl>(ND))
305 S += "function ";
306
307 S += "'";
308 ND->getNameForDiagnostic(S, Context.PrintingPolicy, true);
309 S += "'";
310 }
311 NeedQuotes = false;
312 break;
313 }
314 }
315
316 if (NeedQuotes)
317 Output.push_back('\'');
318
319 Output.append(S.begin(), S.end());
320
321 if (NeedQuotes)
322 Output.push_back('\'');
323}