1 //===--- ASTDiagnostic.cpp - Diagnostic Printing Hooks for AST Nodes ------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file implements a diagnostic formatting hook for AST elements.
12 //===----------------------------------------------------------------------===//
13 #include "clang/AST/ASTDiagnostic.h"
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"
20 using namespace clang;
22 // Returns a desugared version of the QualType, and marks ShouldAKA as true
23 // whenever we remove significant sugar from the type.
24 static QualType Desugar(ASTContext &Context, QualType QT, bool &ShouldAKA) {
25 QualifierCollector QC;
28 const Type *Ty = QC.strip(QT);
30 // Don't aka just because we saw an elaborated type...
31 if (const ElaboratedType *ET = dyn_cast<ElaboratedType>(Ty)) {
35 // ... or a paren type ...
36 if (const ParenType *PT = dyn_cast<ParenType>(Ty)) {
40 // ...or a substituted template type parameter ...
41 if (const SubstTemplateTypeParmType *ST =
42 dyn_cast<SubstTemplateTypeParmType>(Ty)) {
46 // ...or an attributed type...
47 if (const AttributedType *AT = dyn_cast<AttributedType>(Ty)) {
51 // ... or an auto type.
52 if (const AutoType *AT = dyn_cast<AutoType>(Ty)) {
59 // 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())
65 // 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())
72 // Don't desugar va_list.
73 if (QualType(Ty,0) == Context.getBuiltinVaListType())
76 // Otherwise, do a single-step desugar.
79 switch (Ty->getTypeClass()) {
80 #define ABSTRACT_TYPE(Class, Base)
81 #define TYPE(Class, Base) \
83 const Class##Type *CTy = cast<Class##Type>(Ty); \
84 if (CTy->isSugared()) { \
86 Underlying = CTy->desugar(); \
90 #include "clang/AST/TypeNodes.def"
93 // If it wasn't sugared, we're done.
97 // 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))
102 // Don't desugar through the primary typedef of an anonymous type.
103 if (const TagType *UTT = Underlying->getAs<TagType>())
104 if (const TypedefType *QTT = dyn_cast<TypedefType>(QT))
105 if (UTT->getDecl()->getTypedefNameForAnonDecl() == QTT->getDecl())
108 // Record that we actually looked through an opaque type here.
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>()) {
116 QT = Context.getPointerType(Desugar(Context, Ty->getPointeeType(),
118 } else if (const LValueReferenceType *Ty = QT->getAs<LValueReferenceType>()) {
119 QT = Context.getLValueReferenceType(Desugar(Context, Ty->getPointeeType(),
121 } else if (const RValueReferenceType *Ty = QT->getAs<RValueReferenceType>()) {
122 QT = Context.getRValueReferenceType(Desugar(Context, Ty->getPointeeType(),
126 return QC.apply(Context, QT);
129 /// \brief Convert the given type to a string suitable for printing as part of
132 /// There are four main criteria when determining whether we should have an
133 /// a.k.a. clause when pretty-printing a type:
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
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.
145 /// 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.
149 /// \param Context the context in which the type was allocated
150 /// \param Ty the type to print
151 /// \param QualTypeVals pointer values to QualTypes which are used in the
152 /// diagnostic message
154 ConvertTypeToDiagnosticString(ASTContext &Context, QualType Ty,
155 const DiagnosticsEngine::ArgumentValue *PrevArgs,
156 unsigned NumPrevArgs,
157 SmallVectorImpl<intptr_t> &QualTypeVals) {
158 // FIXME: Playing with std::string is really slow.
159 bool ForceAKA = false;
160 QualType CanTy = Ty.getCanonicalType();
161 std::string S = Ty.getAsString(Context.getPrintingPolicy());
162 std::string CanS = CanTy.getAsString(Context.getPrintingPolicy());
164 for (SmallVectorImpl<intptr_t>::iterator I = QualTypeVals.begin(),
165 E = QualTypeVals.end(); I != E; ++I) {
167 QualType::getFromOpaquePtr(reinterpret_cast<void*>(*I));
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.getPrintingPolicy());
175 continue; // Original strings are different
176 std::string CompareCanS = CompareCanTy.getAsString(Context.getPrintingPolicy());
177 if (CompareCanS == CanS)
178 continue; // No new info from canonical type
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 == DiagnosticsEngine::ak_qualtype) {
190 void *Ptr = (void*)PrevArgs[i].second;
191 QualType PrevTy(QualType::getFromOpaquePtr(Ptr));
199 // Consider producing an a.k.a. clause if removing all the direct
200 // sugar gives us something "significantly different".
202 bool ShouldAKA = false;
203 QualType DesugaredTy = Desugar(Context, Ty, ShouldAKA);
204 if (ShouldAKA || ForceAKA) {
205 if (DesugaredTy == Ty) {
206 DesugaredTy = Ty.getCanonicalType();
208 std::string akaStr = DesugaredTy.getAsString(Context.getPrintingPolicy());
210 S = "'" + S + "' (aka '" + akaStr + "')";
220 void clang::FormatASTNodeDiagnosticArgument(
221 DiagnosticsEngine::ArgumentKind Kind,
223 const char *Modifier,
225 const char *Argument,
227 const DiagnosticsEngine::ArgumentValue *PrevArgs,
228 unsigned NumPrevArgs,
229 SmallVectorImpl<char> &Output,
231 SmallVectorImpl<intptr_t> &QualTypeVals) {
232 ASTContext &Context = *static_cast<ASTContext*>(Cookie);
235 bool NeedQuotes = true;
238 default: llvm_unreachable("unknown ArgumentKind");
239 case DiagnosticsEngine::ak_qualtype: {
240 assert(ModLen == 0 && ArgLen == 0 &&
241 "Invalid modifier for QualType argument");
243 QualType Ty(QualType::getFromOpaquePtr(reinterpret_cast<void*>(Val)));
244 S = ConvertTypeToDiagnosticString(Context, Ty, PrevArgs, NumPrevArgs,
249 case DiagnosticsEngine::ak_declarationname: {
250 DeclarationName N = DeclarationName::getFromOpaqueInteger(Val);
253 if (ModLen == 9 && !memcmp(Modifier, "objcclass", 9) && ArgLen == 0)
255 else if (ModLen == 12 && !memcmp(Modifier, "objcinstance", 12)
259 assert(ModLen == 0 && ArgLen == 0 &&
260 "Invalid modifier for DeclarationName argument");
263 case DiagnosticsEngine::ak_nameddecl: {
265 if (ModLen == 1 && Modifier[0] == 'q' && ArgLen == 0)
268 assert(ModLen == 0 && ArgLen == 0 &&
269 "Invalid modifier for NamedDecl* argument");
272 const NamedDecl *ND = reinterpret_cast<const NamedDecl*>(Val);
273 ND->getNameForDiagnostic(S, Context.getPrintingPolicy(), Qualified);
276 case DiagnosticsEngine::ak_nestednamespec: {
277 llvm::raw_string_ostream OS(S);
278 reinterpret_cast<NestedNameSpecifier*>(Val)->print(OS,
279 Context.getPrintingPolicy());
283 case DiagnosticsEngine::ak_declcontext: {
284 DeclContext *DC = reinterpret_cast<DeclContext *> (Val);
285 assert(DC && "Should never have a null declaration context");
287 if (DC->isTranslationUnit()) {
288 // FIXME: Get these strings from some localized place
289 if (Context.getLangOptions().CPlusPlus)
290 S = "the global namespace";
292 S = "the global scope";
293 } else if (TypeDecl *Type = dyn_cast<TypeDecl>(DC)) {
294 S = ConvertTypeToDiagnosticString(Context,
295 Context.getTypeDeclType(Type),
296 PrevArgs, NumPrevArgs, QualTypeVals);
298 // FIXME: Get these strings from some localized place
299 NamedDecl *ND = cast<NamedDecl>(DC);
300 if (isa<NamespaceDecl>(ND))
302 else if (isa<ObjCMethodDecl>(ND))
304 else if (isa<FunctionDecl>(ND))
308 ND->getNameForDiagnostic(S, Context.getPrintingPolicy(), true);
317 Output.push_back('\'');
319 Output.append(S.begin(), S.end());
322 Output.push_back('\'');