blob: 7402b7dda4ecfaf973724ce57f53dc9e3dd84a8c [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
22/// Determines whether we should have an a.k.a. clause when
23/// pretty-printing a type. There are three main criteria:
24///
25/// 1) Some types provide very minimal sugar that doesn't impede the
26/// user's understanding --- for example, elaborated type
27/// specifiers. If this is all the sugar we see, we don't want an
28/// a.k.a. clause.
29/// 2) Some types are technically sugared but are much more familiar
30/// when seen in their sugared form --- for example, va_list,
31/// vector types, and the magic Objective C types. We don't
32/// want to desugar these, even if we do produce an a.k.a. clause.
33/// 3) Some types may have already been desugared previously in this diagnostic.
34/// if this is the case, doing another "aka" would just be clutter.
35///
36static bool ShouldAKA(ASTContext &Context, QualType QT,
37 const Diagnostic::ArgumentValue *PrevArgs,
38 unsigned NumPrevArgs,
39 QualType &DesugaredQT) {
40 QualType InputTy = QT;
41
42 bool AKA = false;
43 QualifierCollector Qc;
44
45 while (true) {
46 const Type *Ty = Qc.strip(QT);
47
48 // Don't aka just because we saw an elaborated type...
49 if (isa<ElaboratedType>(Ty)) {
50 QT = cast<ElaboratedType>(Ty)->desugar();
51 continue;
52 }
53
54 // ...or a qualified name type...
55 if (isa<QualifiedNameType>(Ty)) {
56 QT = cast<QualifiedNameType>(Ty)->desugar();
57 continue;
58 }
59
60 // ...or a substituted template type parameter.
61 if (isa<SubstTemplateTypeParmType>(Ty)) {
62 QT = cast<SubstTemplateTypeParmType>(Ty)->desugar();
63 continue;
64 }
65
66 // Don't desugar template specializations.
67 if (isa<TemplateSpecializationType>(Ty))
68 break;
69
70 // Don't desugar magic Objective-C types.
71 if (QualType(Ty,0) == Context.getObjCIdType() ||
72 QualType(Ty,0) == Context.getObjCClassType() ||
73 QualType(Ty,0) == Context.getObjCSelType() ||
74 QualType(Ty,0) == Context.getObjCProtoType())
75 break;
76
77 // Don't desugar va_list.
78 if (QualType(Ty,0) == Context.getBuiltinVaListType())
79 break;
80
81 // Otherwise, do a single-step desugar.
82 QualType Underlying;
83 bool IsSugar = false;
84 switch (Ty->getTypeClass()) {
85#define ABSTRACT_TYPE(Class, Base)
86#define TYPE(Class, Base) \
87case Type::Class: { \
88const Class##Type *CTy = cast<Class##Type>(Ty); \
89if (CTy->isSugared()) { \
90IsSugar = true; \
91Underlying = CTy->desugar(); \
92} \
93break; \
94}
95#include "clang/AST/TypeNodes.def"
96 }
97
98 // If it wasn't sugared, we're done.
99 if (!IsSugar)
100 break;
101
102 // If the desugared type is a vector type, we don't want to expand
103 // it, it will turn into an attribute mess. People want their "vec4".
104 if (isa<VectorType>(Underlying))
105 break;
106
107 // Don't desugar through the primary typedef of an anonymous type.
108 if (isa<TagType>(Underlying) && isa<TypedefType>(QT))
109 if (cast<TagType>(Underlying)->getDecl()->getTypedefForAnonDecl() ==
110 cast<TypedefType>(QT)->getDecl())
111 break;
112
113 // Otherwise, we're tearing through something opaque; note that
114 // we'll eventually need an a.k.a. clause and keep going.
115 AKA = true;
116 QT = Underlying;
117 continue;
118 }
119
120 // If we never tore through opaque sugar, don't print aka.
121 if (!AKA) return false;
122
123 // If we did, check to see if we already desugared this type in this
124 // diagnostic. If so, don't do it again.
125 for (unsigned i = 0; i != NumPrevArgs; ++i) {
126 // TODO: Handle ak_declcontext case.
127 if (PrevArgs[i].first == Diagnostic::ak_qualtype) {
128 void *Ptr = (void*)PrevArgs[i].second;
129 QualType PrevTy(QualType::getFromOpaquePtr(Ptr));
130 if (PrevTy == InputTy)
131 return false;
132 }
133 }
134
135 DesugaredQT = Qc.apply(QT);
136 return true;
137}
138
139/// \brief Convert the given type to a string suitable for printing as part of
140/// a diagnostic.
141///
142/// \param Context the context in which the type was allocated
143/// \param Ty the type to print
144static std::string
145ConvertTypeToDiagnosticString(ASTContext &Context, QualType Ty,
146 const Diagnostic::ArgumentValue *PrevArgs,
147 unsigned NumPrevArgs) {
148 // FIXME: Playing with std::string is really slow.
149 std::string S = Ty.getAsString(Context.PrintingPolicy);
150
151 // Consider producing an a.k.a. clause if removing all the direct
152 // sugar gives us something "significantly different".
153
154 QualType DesugaredTy;
155 if (ShouldAKA(Context, Ty, PrevArgs, NumPrevArgs, DesugaredTy)) {
156 S = "'"+S+"' (aka '";
157 S += DesugaredTy.getAsString(Context.PrintingPolicy);
158 S += "')";
159 return S;
160 }
161
162 S = "'" + S + "'";
163 return S;
164}
165
166void clang::FormatASTNodeDiagnosticArgument(Diagnostic::ArgumentKind Kind,
167 intptr_t Val,
168 const char *Modifier,
169 unsigned ModLen,
170 const char *Argument,
171 unsigned ArgLen,
172 const Diagnostic::ArgumentValue *PrevArgs,
173 unsigned NumPrevArgs,
174 llvm::SmallVectorImpl<char> &Output,
175 void *Cookie) {
176 ASTContext &Context = *static_cast<ASTContext*>(Cookie);
177
178 std::string S;
179 bool NeedQuotes = true;
180
181 switch (Kind) {
182 default: assert(0 && "unknown ArgumentKind");
183 case Diagnostic::ak_qualtype: {
184 assert(ModLen == 0 && ArgLen == 0 &&
185 "Invalid modifier for QualType argument");
186
187 QualType Ty(QualType::getFromOpaquePtr(reinterpret_cast<void*>(Val)));
188 S = ConvertTypeToDiagnosticString(Context, Ty, PrevArgs, NumPrevArgs);
189 NeedQuotes = false;
190 break;
191 }
192 case Diagnostic::ak_declarationname: {
193 DeclarationName N = DeclarationName::getFromOpaqueInteger(Val);
194 S = N.getAsString();
195
196 if (ModLen == 9 && !memcmp(Modifier, "objcclass", 9) && ArgLen == 0)
197 S = '+' + S;
198 else if (ModLen == 12 && !memcmp(Modifier, "objcinstance", 12)
199 && ArgLen==0)
200 S = '-' + S;
201 else
202 assert(ModLen == 0 && ArgLen == 0 &&
203 "Invalid modifier for DeclarationName argument");
204 break;
205 }
206 case Diagnostic::ak_nameddecl: {
207 bool Qualified;
208 if (ModLen == 1 && Modifier[0] == 'q' && ArgLen == 0)
209 Qualified = true;
210 else {
211 assert(ModLen == 0 && ArgLen == 0 &&
212 "Invalid modifier for NamedDecl* argument");
213 Qualified = false;
214 }
215 reinterpret_cast<NamedDecl*>(Val)->
216 getNameForDiagnostic(S, Context.PrintingPolicy, Qualified);
217 break;
218 }
219 case Diagnostic::ak_nestednamespec: {
220 llvm::raw_string_ostream OS(S);
221 reinterpret_cast<NestedNameSpecifier*>(Val)->print(OS,
222 Context.PrintingPolicy);
223 NeedQuotes = false;
224 break;
225 }
226 case Diagnostic::ak_declcontext: {
227 DeclContext *DC = reinterpret_cast<DeclContext *> (Val);
228 assert(DC && "Should never have a null declaration context");
229
230 if (DC->isTranslationUnit()) {
231 // FIXME: Get these strings from some localized place
232 if (Context.getLangOptions().CPlusPlus)
233 S = "the global namespace";
234 else
235 S = "the global scope";
236 } else if (TypeDecl *Type = dyn_cast<TypeDecl>(DC)) {
237 S = ConvertTypeToDiagnosticString(Context,
238 Context.getTypeDeclType(Type),
239 PrevArgs, NumPrevArgs);
240 } else {
241 // FIXME: Get these strings from some localized place
242 NamedDecl *ND = cast<NamedDecl>(DC);
243 if (isa<NamespaceDecl>(ND))
244 S += "namespace ";
245 else if (isa<ObjCMethodDecl>(ND))
246 S += "method ";
247 else if (isa<FunctionDecl>(ND))
248 S += "function ";
249
250 S += "'";
251 ND->getNameForDiagnostic(S, Context.PrintingPolicy, true);
252 S += "'";
253 }
254 NeedQuotes = false;
255 break;
256 }
257 }
258
259 if (NeedQuotes)
260 Output.push_back('\'');
261
262 Output.append(S.begin(), S.end());
263
264 if (NeedQuotes)
265 Output.push_back('\'');
266}