blob: 9870b515c67abe2f1dc284df3eddec6dbbfa8896 [file] [log] [blame]
Douglas Gregor639cccc2010-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 Carruthd102f2d2010-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 Gregor639cccc2010-02-09 22:26:47 +000027 while (true) {
Chandler Carruthd102f2d2010-05-13 11:37:24 +000028 const Type *Ty = QC.strip(QT);
29
Douglas Gregor639cccc2010-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 Bagnara924a8f32010-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 Gregor639cccc2010-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 Carruthd102f2d2010-05-13 11:37:24 +000045
Douglas Gregor639cccc2010-02-09 22:26:47 +000046 // Don't desugar template specializations.
47 if (isa<TemplateSpecializationType>(Ty))
48 break;
Chandler Carruthd102f2d2010-05-13 11:37:24 +000049
Douglas Gregor639cccc2010-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 Carruthd102f2d2010-05-13 11:37:24 +000056
Douglas Gregor639cccc2010-02-09 22:26:47 +000057 // Don't desugar va_list.
58 if (QualType(Ty,0) == Context.getBuiltinVaListType())
59 break;
Chandler Carruthd102f2d2010-05-13 11:37:24 +000060
Douglas Gregor639cccc2010-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 Carruthd102f2d2010-05-13 11:37:24 +000077
Douglas Gregor639cccc2010-02-09 22:26:47 +000078 // If it wasn't sugared, we're done.
79 if (!IsSugar)
80 break;
Chandler Carruthd102f2d2010-05-13 11:37:24 +000081
Douglas Gregor639cccc2010-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 Carruthd102f2d2010-05-13 11:37:24 +000086
Douglas Gregor639cccc2010-02-09 22:26:47 +000087 // Don't desugar through the primary typedef of an anonymous type.
Chris Lattneredbdff62010-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 Carruthd102f2d2010-05-13 11:37:24 +000092
93 // Record that we actually looked through an opaque type here.
94 ShouldAKA = true;
Douglas Gregor639cccc2010-02-09 22:26:47 +000095 QT = Underlying;
Douglas Gregor639cccc2010-02-09 22:26:47 +000096 }
Chandler Carruthd102f2d2010-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 Lattneredbdff62010-09-04 23:16:01 +0000101 QT = Context.getPointerType(Desugar(Context, Ty->getPointeeType(),
102 ShouldAKA));
Chandler Carruthd102f2d2010-05-13 11:37:24 +0000103 } else if (const LValueReferenceType *Ty = QT->getAs<LValueReferenceType>()) {
Chris Lattneredbdff62010-09-04 23:16:01 +0000104 QT = Context.getLValueReferenceType(Desugar(Context, Ty->getPointeeType(),
105 ShouldAKA));
Douglas Gregor7a2a1162011-01-20 16:08:06 +0000106 } else if (const RValueReferenceType *Ty = QT->getAs<RValueReferenceType>()) {
107 QT = Context.getRValueReferenceType(Desugar(Context, Ty->getPointeeType(),
108 ShouldAKA));
Douglas Gregor639cccc2010-02-09 22:26:47 +0000109 }
Chandler Carruthd102f2d2010-05-13 11:37:24 +0000110
John McCall717d9b02010-12-10 11:01:00 +0000111 return QC.apply(Context, QT);
Douglas Gregor639cccc2010-02-09 22:26:47 +0000112}
113
114/// \brief Convert the given type to a string suitable for printing as part of
Chandler Carruthd102f2d2010-05-13 11:37:24 +0000115/// a diagnostic.
116///
117/// There are three main criteria when determining whether we should have an
118/// a.k.a. clause when pretty-printing a type:
119///
120/// 1) Some types provide very minimal sugar that doesn't impede the
121/// user's understanding --- for example, elaborated type
122/// specifiers. If this is all the sugar we see, we don't want an
123/// a.k.a. clause.
124/// 2) Some types are technically sugared but are much more familiar
125/// when seen in their sugared form --- for example, va_list,
126/// vector types, and the magic Objective C types. We don't
127/// want to desugar these, even if we do produce an a.k.a. clause.
128/// 3) Some types may have already been desugared previously in this diagnostic.
129/// if this is the case, doing another "aka" would just be clutter.
Douglas Gregor639cccc2010-02-09 22:26:47 +0000130///
131/// \param Context the context in which the type was allocated
132/// \param Ty the type to print
133static std::string
134ConvertTypeToDiagnosticString(ASTContext &Context, QualType Ty,
135 const Diagnostic::ArgumentValue *PrevArgs,
136 unsigned NumPrevArgs) {
137 // FIXME: Playing with std::string is really slow.
138 std::string S = Ty.getAsString(Context.PrintingPolicy);
Chandler Carruthd102f2d2010-05-13 11:37:24 +0000139
140 // Check to see if we already desugared this type in this
141 // diagnostic. If so, don't do it again.
142 bool Repeated = false;
143 for (unsigned i = 0; i != NumPrevArgs; ++i) {
144 // TODO: Handle ak_declcontext case.
145 if (PrevArgs[i].first == Diagnostic::ak_qualtype) {
146 void *Ptr = (void*)PrevArgs[i].second;
147 QualType PrevTy(QualType::getFromOpaquePtr(Ptr));
148 if (PrevTy == Ty) {
149 Repeated = true;
150 break;
151 }
152 }
153 }
154
Douglas Gregor639cccc2010-02-09 22:26:47 +0000155 // Consider producing an a.k.a. clause if removing all the direct
156 // sugar gives us something "significantly different".
Chandler Carruthd102f2d2010-05-13 11:37:24 +0000157 if (!Repeated) {
158 bool ShouldAKA = false;
159 QualType DesugaredTy = Desugar(Context, Ty, ShouldAKA);
160 if (ShouldAKA) {
Chris Lattneredbdff62010-09-04 23:16:01 +0000161 S = "'" + S + "' (aka '";
162 S += DesugaredTy.getAsString(Context.PrintingPolicy);
163 S += "')";
164 return S;
Chandler Carruthd102f2d2010-05-13 11:37:24 +0000165 }
Douglas Gregor639cccc2010-02-09 22:26:47 +0000166 }
Chandler Carruthd102f2d2010-05-13 11:37:24 +0000167
Douglas Gregor639cccc2010-02-09 22:26:47 +0000168 S = "'" + S + "'";
169 return S;
170}
171
172void clang::FormatASTNodeDiagnosticArgument(Diagnostic::ArgumentKind Kind,
173 intptr_t Val,
174 const char *Modifier,
175 unsigned ModLen,
176 const char *Argument,
177 unsigned ArgLen,
178 const Diagnostic::ArgumentValue *PrevArgs,
179 unsigned NumPrevArgs,
180 llvm::SmallVectorImpl<char> &Output,
181 void *Cookie) {
182 ASTContext &Context = *static_cast<ASTContext*>(Cookie);
183
184 std::string S;
185 bool NeedQuotes = true;
186
187 switch (Kind) {
188 default: assert(0 && "unknown ArgumentKind");
189 case Diagnostic::ak_qualtype: {
190 assert(ModLen == 0 && ArgLen == 0 &&
191 "Invalid modifier for QualType argument");
192
193 QualType Ty(QualType::getFromOpaquePtr(reinterpret_cast<void*>(Val)));
194 S = ConvertTypeToDiagnosticString(Context, Ty, PrevArgs, NumPrevArgs);
195 NeedQuotes = false;
196 break;
197 }
198 case Diagnostic::ak_declarationname: {
199 DeclarationName N = DeclarationName::getFromOpaqueInteger(Val);
200 S = N.getAsString();
201
202 if (ModLen == 9 && !memcmp(Modifier, "objcclass", 9) && ArgLen == 0)
203 S = '+' + S;
204 else if (ModLen == 12 && !memcmp(Modifier, "objcinstance", 12)
205 && ArgLen==0)
206 S = '-' + S;
207 else
208 assert(ModLen == 0 && ArgLen == 0 &&
209 "Invalid modifier for DeclarationName argument");
210 break;
211 }
212 case Diagnostic::ak_nameddecl: {
213 bool Qualified;
214 if (ModLen == 1 && Modifier[0] == 'q' && ArgLen == 0)
215 Qualified = true;
216 else {
217 assert(ModLen == 0 && ArgLen == 0 &&
218 "Invalid modifier for NamedDecl* argument");
219 Qualified = false;
220 }
221 reinterpret_cast<NamedDecl*>(Val)->
222 getNameForDiagnostic(S, Context.PrintingPolicy, Qualified);
223 break;
224 }
225 case Diagnostic::ak_nestednamespec: {
226 llvm::raw_string_ostream OS(S);
227 reinterpret_cast<NestedNameSpecifier*>(Val)->print(OS,
228 Context.PrintingPolicy);
229 NeedQuotes = false;
230 break;
231 }
232 case Diagnostic::ak_declcontext: {
233 DeclContext *DC = reinterpret_cast<DeclContext *> (Val);
234 assert(DC && "Should never have a null declaration context");
235
236 if (DC->isTranslationUnit()) {
237 // FIXME: Get these strings from some localized place
238 if (Context.getLangOptions().CPlusPlus)
239 S = "the global namespace";
240 else
241 S = "the global scope";
242 } else if (TypeDecl *Type = dyn_cast<TypeDecl>(DC)) {
243 S = ConvertTypeToDiagnosticString(Context,
244 Context.getTypeDeclType(Type),
245 PrevArgs, NumPrevArgs);
246 } else {
247 // FIXME: Get these strings from some localized place
248 NamedDecl *ND = cast<NamedDecl>(DC);
249 if (isa<NamespaceDecl>(ND))
250 S += "namespace ";
251 else if (isa<ObjCMethodDecl>(ND))
252 S += "method ";
253 else if (isa<FunctionDecl>(ND))
254 S += "function ";
255
256 S += "'";
257 ND->getNameForDiagnostic(S, Context.PrintingPolicy, true);
258 S += "'";
259 }
260 NeedQuotes = false;
261 break;
262 }
263 }
264
265 if (NeedQuotes)
266 Output.push_back('\'');
267
268 Output.append(S.begin(), S.end());
269
270 if (NeedQuotes)
271 Output.push_back('\'');
272}