]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/llvm/tools/clang/lib/AST/ASTDiagnostic.cpp
Copy the v4l2 header unchanged from the vendor branch.
[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. 
60     if (isa<TemplateSpecializationType>(Ty))
61       break;
62
63     // Don't desugar magic Objective-C types.
64     if (QualType(Ty,0) == Context.getObjCIdType() ||
65         QualType(Ty,0) == Context.getObjCClassType() ||
66         QualType(Ty,0) == Context.getObjCSelType() ||
67         QualType(Ty,0) == Context.getObjCProtoType())
68       break;
69
70     // Don't desugar va_list.
71     if (QualType(Ty,0) == Context.getBuiltinVaListType())
72       break;
73
74     // Otherwise, do a single-step desugar.
75     QualType Underlying;
76     bool IsSugar = false;
77     switch (Ty->getTypeClass()) {
78 #define ABSTRACT_TYPE(Class, Base)
79 #define TYPE(Class, Base) \
80 case Type::Class: { \
81 const Class##Type *CTy = cast<Class##Type>(Ty); \
82 if (CTy->isSugared()) { \
83 IsSugar = true; \
84 Underlying = CTy->desugar(); \
85 } \
86 break; \
87 }
88 #include "clang/AST/TypeNodes.def"
89     }
90
91     // If it wasn't sugared, we're done.
92     if (!IsSugar)
93       break;
94
95     // If the desugared type is a vector type, we don't want to expand
96     // it, it will turn into an attribute mess. People want their "vec4".
97     if (isa<VectorType>(Underlying))
98       break;
99
100     // Don't desugar through the primary typedef of an anonymous type.
101     if (const TagType *UTT = Underlying->getAs<TagType>())
102       if (const TypedefType *QTT = dyn_cast<TypedefType>(QT))
103         if (UTT->getDecl()->getTypedefNameForAnonDecl() == QTT->getDecl())
104           break;
105
106     // Record that we actually looked through an opaque type here.
107     ShouldAKA = true;
108     QT = Underlying;
109   }
110
111   // If we have a pointer-like type, desugar the pointee as well.
112   // FIXME: Handle other pointer-like types.
113   if (const PointerType *Ty = QT->getAs<PointerType>()) {
114     QT = Context.getPointerType(Desugar(Context, Ty->getPointeeType(),
115                                         ShouldAKA));
116   } else if (const LValueReferenceType *Ty = QT->getAs<LValueReferenceType>()) {
117     QT = Context.getLValueReferenceType(Desugar(Context, Ty->getPointeeType(),
118                                                 ShouldAKA));
119   } else if (const RValueReferenceType *Ty = QT->getAs<RValueReferenceType>()) {
120     QT = Context.getRValueReferenceType(Desugar(Context, Ty->getPointeeType(),
121                                                 ShouldAKA));
122   }
123
124   return QC.apply(Context, QT);
125 }
126
127 /// \brief Convert the given type to a string suitable for printing as part of 
128 /// a diagnostic.
129 ///
130 /// There are three main criteria when determining whether we should have an
131 /// a.k.a. clause when pretty-printing a type:
132 ///
133 /// 1) Some types provide very minimal sugar that doesn't impede the
134 ///    user's understanding --- for example, elaborated type
135 ///    specifiers.  If this is all the sugar we see, we don't want an
136 ///    a.k.a. clause.
137 /// 2) Some types are technically sugared but are much more familiar
138 ///    when seen in their sugared form --- for example, va_list,
139 ///    vector types, and the magic Objective C types.  We don't
140 ///    want to desugar these, even if we do produce an a.k.a. clause.
141 /// 3) Some types may have already been desugared previously in this diagnostic.
142 ///    if this is the case, doing another "aka" would just be clutter.
143 ///
144 /// \param Context the context in which the type was allocated
145 /// \param Ty the type to print
146 static std::string
147 ConvertTypeToDiagnosticString(ASTContext &Context, QualType Ty,
148                               const Diagnostic::ArgumentValue *PrevArgs,
149                               unsigned NumPrevArgs) {
150   // FIXME: Playing with std::string is really slow.
151   std::string S = Ty.getAsString(Context.PrintingPolicy);
152
153   // Check to see if we already desugared this type in this
154   // diagnostic.  If so, don't do it again.
155   bool Repeated = false;
156   for (unsigned i = 0; i != NumPrevArgs; ++i) {
157     // TODO: Handle ak_declcontext case.
158     if (PrevArgs[i].first == Diagnostic::ak_qualtype) {
159       void *Ptr = (void*)PrevArgs[i].second;
160       QualType PrevTy(QualType::getFromOpaquePtr(Ptr));
161       if (PrevTy == Ty) {
162         Repeated = true;
163         break;
164       }
165     }
166   }
167
168   // Consider producing an a.k.a. clause if removing all the direct
169   // sugar gives us something "significantly different".
170   if (!Repeated) {
171     bool ShouldAKA = false;
172     QualType DesugaredTy = Desugar(Context, Ty, ShouldAKA);
173     if (ShouldAKA) {
174       S = "'" + S + "' (aka '";
175       S += DesugaredTy.getAsString(Context.PrintingPolicy);
176       S += "')";
177       return S;
178     }
179   }
180
181   S = "'" + S + "'";
182   return S;
183 }
184
185 void clang::FormatASTNodeDiagnosticArgument(Diagnostic::ArgumentKind Kind, 
186                                             intptr_t Val,
187                                             const char *Modifier, 
188                                             unsigned ModLen,
189                                             const char *Argument, 
190                                             unsigned ArgLen,
191                                     const Diagnostic::ArgumentValue *PrevArgs,
192                                             unsigned NumPrevArgs,
193                                             llvm::SmallVectorImpl<char> &Output,
194                                             void *Cookie) {
195   ASTContext &Context = *static_cast<ASTContext*>(Cookie);
196   
197   std::string S;
198   bool NeedQuotes = true;
199   
200   switch (Kind) {
201     default: assert(0 && "unknown ArgumentKind");
202     case Diagnostic::ak_qualtype: {
203       assert(ModLen == 0 && ArgLen == 0 &&
204              "Invalid modifier for QualType argument");
205       
206       QualType Ty(QualType::getFromOpaquePtr(reinterpret_cast<void*>(Val)));
207       S = ConvertTypeToDiagnosticString(Context, Ty, PrevArgs, NumPrevArgs);
208       NeedQuotes = false;
209       break;
210     }
211     case Diagnostic::ak_declarationname: {
212       DeclarationName N = DeclarationName::getFromOpaqueInteger(Val);
213       S = N.getAsString();
214       
215       if (ModLen == 9 && !memcmp(Modifier, "objcclass", 9) && ArgLen == 0)
216         S = '+' + S;
217       else if (ModLen == 12 && !memcmp(Modifier, "objcinstance", 12)
218                 && ArgLen==0)
219         S = '-' + S;
220       else
221         assert(ModLen == 0 && ArgLen == 0 &&
222                "Invalid modifier for DeclarationName argument");
223       break;
224     }
225     case Diagnostic::ak_nameddecl: {
226       bool Qualified;
227       if (ModLen == 1 && Modifier[0] == 'q' && ArgLen == 0)
228         Qualified = true;
229       else {
230         assert(ModLen == 0 && ArgLen == 0 &&
231                "Invalid modifier for NamedDecl* argument");
232         Qualified = false;
233       }
234       reinterpret_cast<NamedDecl*>(Val)->
235       getNameForDiagnostic(S, Context.PrintingPolicy, Qualified);
236       break;
237     }
238     case Diagnostic::ak_nestednamespec: {
239       llvm::raw_string_ostream OS(S);
240       reinterpret_cast<NestedNameSpecifier*>(Val)->print(OS,
241                                                         Context.PrintingPolicy);
242       NeedQuotes = false;
243       break;
244     }
245     case Diagnostic::ak_declcontext: {
246       DeclContext *DC = reinterpret_cast<DeclContext *> (Val);
247       assert(DC && "Should never have a null declaration context");
248       
249       if (DC->isTranslationUnit()) {
250         // FIXME: Get these strings from some localized place
251         if (Context.getLangOptions().CPlusPlus)
252           S = "the global namespace";
253         else
254           S = "the global scope";
255       } else if (TypeDecl *Type = dyn_cast<TypeDecl>(DC)) {
256         S = ConvertTypeToDiagnosticString(Context, 
257                                           Context.getTypeDeclType(Type),
258                                           PrevArgs, NumPrevArgs);
259       } else {
260         // FIXME: Get these strings from some localized place
261         NamedDecl *ND = cast<NamedDecl>(DC);
262         if (isa<NamespaceDecl>(ND))
263           S += "namespace ";
264         else if (isa<ObjCMethodDecl>(ND))
265           S += "method ";
266         else if (isa<FunctionDecl>(ND))
267           S += "function ";
268         
269         S += "'";
270         ND->getNameForDiagnostic(S, Context.PrintingPolicy, true);
271         S += "'";
272       }
273       NeedQuotes = false;
274       break;
275     }
276   }
277   
278   if (NeedQuotes)
279     Output.push_back('\'');
280   
281   Output.append(S.begin(), S.end());
282   
283   if (NeedQuotes)
284     Output.push_back('\'');
285 }