]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/llvm/tools/clang/lib/AST/ASTDiagnostic.cpp
Upgrade our copy of llvm/clang to r132879, from upstream's trunk.
[FreeBSD/FreeBSD.git] / contrib / llvm / tools / clang / lib / AST / ASTDiagnostic.cpp
1 //===--- 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
20 using namespace clang;
21
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;
26
27   while (true) {
28     const Type *Ty = QC.strip(QT);
29
30     // Don't aka just because we saw an elaborated type...
31     if (const ElaboratedType *ET = dyn_cast<ElaboratedType>(Ty)) {
32       QT = ET->desugar();
33       continue;
34     }
35     // ... or a paren type ...
36     if (const ParenType *PT = dyn_cast<ParenType>(Ty)) {
37       QT = PT->desugar();
38       continue;
39     }
40     // ...or a substituted template type parameter ...
41     if (const SubstTemplateTypeParmType *ST =
42           dyn_cast<SubstTemplateTypeParmType>(Ty)) {
43       QT = ST->desugar();
44       continue;
45     }
46     // ...or an attributed type...
47     if (const AttributedType *AT = dyn_cast<AttributedType>(Ty)) {
48       QT = AT->desugar();
49       continue;
50     }
51     // ... or an auto type.
52     if (const AutoType *AT = dyn_cast<AutoType>(Ty)) {
53       if (!AT->isSugared())
54         break;
55       QT = AT->desugar();
56       continue;
57     }
58
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())
63         break;
64
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())
70       break;
71
72     // Don't desugar va_list.
73     if (QualType(Ty,0) == Context.getBuiltinVaListType())
74       break;
75
76     // 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) \
82 case Type::Class: { \
83 const Class##Type *CTy = cast<Class##Type>(Ty); \
84 if (CTy->isSugared()) { \
85 IsSugar = true; \
86 Underlying = CTy->desugar(); \
87 } \
88 break; \
89 }
90 #include "clang/AST/TypeNodes.def"
91     }
92
93     // If it wasn't sugared, we're done.
94     if (!IsSugar)
95       break;
96
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))
100       break;
101
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())
106           break;
107
108     // Record that we actually looked through an opaque type here.
109     ShouldAKA = true;
110     QT = Underlying;
111   }
112
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(),
117                                         ShouldAKA));
118   } else if (const LValueReferenceType *Ty = QT->getAs<LValueReferenceType>()) {
119     QT = Context.getLValueReferenceType(Desugar(Context, Ty->getPointeeType(),
120                                                 ShouldAKA));
121   } else if (const RValueReferenceType *Ty = QT->getAs<RValueReferenceType>()) {
122     QT = Context.getRValueReferenceType(Desugar(Context, Ty->getPointeeType(),
123                                                 ShouldAKA));
124   }
125
126   return QC.apply(Context, QT);
127 }
128
129 /// \brief Convert the given type to a string suitable for printing as part of 
130 /// a diagnostic.
131 ///
132 /// There are three main criteria when determining whether we should have an
133 /// 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.
145 ///
146 /// \param Context the context in which the type was allocated
147 /// \param Ty the type to print
148 static std::string
149 ConvertTypeToDiagnosticString(ASTContext &Context, QualType Ty,
150                               const Diagnostic::ArgumentValue *PrevArgs,
151                               unsigned NumPrevArgs) {
152   // FIXME: Playing with std::string is really slow.
153   std::string S = Ty.getAsString(Context.PrintingPolicy);
154
155   // Check to see if we already desugared this type in this
156   // diagnostic.  If so, don't do it again.
157   bool Repeated = false;
158   for (unsigned i = 0; i != NumPrevArgs; ++i) {
159     // TODO: Handle ak_declcontext case.
160     if (PrevArgs[i].first == Diagnostic::ak_qualtype) {
161       void *Ptr = (void*)PrevArgs[i].second;
162       QualType PrevTy(QualType::getFromOpaquePtr(Ptr));
163       if (PrevTy == Ty) {
164         Repeated = true;
165         break;
166       }
167     }
168   }
169
170   // Consider producing an a.k.a. clause if removing all the direct
171   // sugar gives us something "significantly different".
172   if (!Repeated) {
173     bool ShouldAKA = false;
174     QualType DesugaredTy = Desugar(Context, Ty, ShouldAKA);
175     if (ShouldAKA) {
176       S = "'" + S + "' (aka '";
177       S += DesugaredTy.getAsString(Context.PrintingPolicy);
178       S += "')";
179       return S;
180     }
181   }
182
183   S = "'" + S + "'";
184   return S;
185 }
186
187 void clang::FormatASTNodeDiagnosticArgument(Diagnostic::ArgumentKind Kind, 
188                                             intptr_t Val,
189                                             const char *Modifier, 
190                                             unsigned ModLen,
191                                             const char *Argument, 
192                                             unsigned ArgLen,
193                                     const Diagnostic::ArgumentValue *PrevArgs,
194                                             unsigned NumPrevArgs,
195                                             llvm::SmallVectorImpl<char> &Output,
196                                             void *Cookie) {
197   ASTContext &Context = *static_cast<ASTContext*>(Cookie);
198   
199   std::string S;
200   bool NeedQuotes = true;
201   
202   switch (Kind) {
203     default: assert(0 && "unknown ArgumentKind");
204     case Diagnostic::ak_qualtype: {
205       assert(ModLen == 0 && ArgLen == 0 &&
206              "Invalid modifier for QualType argument");
207       
208       QualType Ty(QualType::getFromOpaquePtr(reinterpret_cast<void*>(Val)));
209       S = ConvertTypeToDiagnosticString(Context, Ty, PrevArgs, NumPrevArgs);
210       NeedQuotes = false;
211       break;
212     }
213     case Diagnostic::ak_declarationname: {
214       DeclarationName N = DeclarationName::getFromOpaqueInteger(Val);
215       S = N.getAsString();
216       
217       if (ModLen == 9 && !memcmp(Modifier, "objcclass", 9) && ArgLen == 0)
218         S = '+' + S;
219       else if (ModLen == 12 && !memcmp(Modifier, "objcinstance", 12)
220                 && ArgLen==0)
221         S = '-' + S;
222       else
223         assert(ModLen == 0 && ArgLen == 0 &&
224                "Invalid modifier for DeclarationName argument");
225       break;
226     }
227     case Diagnostic::ak_nameddecl: {
228       bool Qualified;
229       if (ModLen == 1 && Modifier[0] == 'q' && ArgLen == 0)
230         Qualified = true;
231       else {
232         assert(ModLen == 0 && ArgLen == 0 &&
233                "Invalid modifier for NamedDecl* argument");
234         Qualified = false;
235       }
236       reinterpret_cast<NamedDecl*>(Val)->
237       getNameForDiagnostic(S, Context.PrintingPolicy, Qualified);
238       break;
239     }
240     case Diagnostic::ak_nestednamespec: {
241       llvm::raw_string_ostream OS(S);
242       reinterpret_cast<NestedNameSpecifier*>(Val)->print(OS,
243                                                         Context.PrintingPolicy);
244       NeedQuotes = false;
245       break;
246     }
247     case Diagnostic::ak_declcontext: {
248       DeclContext *DC = reinterpret_cast<DeclContext *> (Val);
249       assert(DC && "Should never have a null declaration context");
250       
251       if (DC->isTranslationUnit()) {
252         // FIXME: Get these strings from some localized place
253         if (Context.getLangOptions().CPlusPlus)
254           S = "the global namespace";
255         else
256           S = "the global scope";
257       } else if (TypeDecl *Type = dyn_cast<TypeDecl>(DC)) {
258         S = ConvertTypeToDiagnosticString(Context, 
259                                           Context.getTypeDeclType(Type),
260                                           PrevArgs, NumPrevArgs);
261       } else {
262         // FIXME: Get these strings from some localized place
263         NamedDecl *ND = cast<NamedDecl>(DC);
264         if (isa<NamespaceDecl>(ND))
265           S += "namespace ";
266         else if (isa<ObjCMethodDecl>(ND))
267           S += "method ";
268         else if (isa<FunctionDecl>(ND))
269           S += "function ";
270         
271         S += "'";
272         ND->getNameForDiagnostic(S, Context.PrintingPolicy, true);
273         S += "'";
274       }
275       NeedQuotes = false;
276       break;
277     }
278   }
279   
280   if (NeedQuotes)
281     Output.push_back('\'');
282   
283   Output.append(S.begin(), S.end());
284   
285   if (NeedQuotes)
286     Output.push_back('\'');
287 }