]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/llvm/tools/clang/lib/Index/IndexSymbol.cpp
Merge compiler-rt trunk r291476.
[FreeBSD/FreeBSD.git] / contrib / llvm / tools / clang / lib / Index / IndexSymbol.cpp
1 //===--- IndexSymbol.cpp - Types and functions for indexing symbols -------===//
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 #include "clang/Index/IndexSymbol.h"
11 #include "clang/AST/DeclCXX.h"
12 #include "clang/AST/DeclObjC.h"
13 #include "clang/AST/DeclTemplate.h"
14 #include "clang/AST/PrettyPrinter.h"
15
16 using namespace clang;
17 using namespace clang::index;
18
19 /// \returns true if \c D is a subclass of 'XCTestCase'.
20 static bool isUnitTestCase(const ObjCInterfaceDecl *D) {
21   if (!D)
22     return false;
23   while (const ObjCInterfaceDecl *SuperD = D->getSuperClass()) {
24     if (SuperD->getName() == "XCTestCase")
25       return true;
26     D = SuperD;
27   }
28   return false;
29 }
30
31 /// \returns true if \c D is in a subclass of 'XCTestCase', returns void, has
32 /// no parameters, and its name starts with 'test'.
33 static bool isUnitTest(const ObjCMethodDecl *D) {
34   if (!D->parameters().empty())
35     return false;
36   if (!D->getReturnType()->isVoidType())
37     return false;
38   if (!D->getSelector().getNameForSlot(0).startswith("test"))
39     return false;
40   return isUnitTestCase(D->getClassInterface());
41 }
42
43 static void checkForIBOutlets(const Decl *D, SymbolPropertySet &PropSet) {
44   if (D->hasAttr<IBOutletAttr>()) {
45     PropSet |= (unsigned)SymbolProperty::IBAnnotated;
46   } else if (D->hasAttr<IBOutletCollectionAttr>()) {
47     PropSet |= (unsigned)SymbolProperty::IBAnnotated;
48     PropSet |= (unsigned)SymbolProperty::IBOutletCollection;
49   }
50 }
51
52 SymbolInfo index::getSymbolInfo(const Decl *D) {
53   assert(D);
54   SymbolInfo Info;
55   Info.Kind = SymbolKind::Unknown;
56   Info.SubKind = SymbolSubKind::None;
57   Info.Properties = SymbolPropertySet();
58   Info.Lang = SymbolLanguage::C;
59
60   if (const TagDecl *TD = dyn_cast<TagDecl>(D)) {
61     switch (TD->getTagKind()) {
62     case TTK_Struct:
63       Info.Kind = SymbolKind::Struct; break;
64     case TTK_Union:
65       Info.Kind = SymbolKind::Union; break;
66     case TTK_Class:
67       Info.Kind = SymbolKind::Class;
68       Info.Lang = SymbolLanguage::CXX;
69       break;
70     case TTK_Interface:
71       Info.Kind = SymbolKind::Protocol;
72       Info.Lang = SymbolLanguage::CXX;
73       break;
74     case TTK_Enum:
75       Info.Kind = SymbolKind::Enum; break;
76     }
77
78     if (const CXXRecordDecl *CXXRec = dyn_cast<CXXRecordDecl>(D)) {
79       if (!CXXRec->isCLike()) {
80         Info.Lang = SymbolLanguage::CXX;
81         if (CXXRec->getDescribedClassTemplate()) {
82           Info.Properties |= (unsigned)SymbolProperty::Generic;
83         }
84       }
85     }
86
87     if (isa<ClassTemplatePartialSpecializationDecl>(D)) {
88       Info.Properties |= (unsigned)SymbolProperty::Generic;
89       Info.Properties |= (unsigned)SymbolProperty::TemplatePartialSpecialization;
90     } else if (isa<ClassTemplateSpecializationDecl>(D)) {
91       Info.Properties |= (unsigned)SymbolProperty::Generic;
92       Info.Properties |= (unsigned)SymbolProperty::TemplateSpecialization;
93     }
94
95   } else if (auto *VD = dyn_cast<VarDecl>(D)) {
96     Info.Kind = SymbolKind::Variable;
97     if (isa<CXXRecordDecl>(D->getDeclContext())) {
98       Info.Kind = SymbolKind::StaticProperty;
99       Info.Lang = SymbolLanguage::CXX;
100     }
101     if (isa<VarTemplatePartialSpecializationDecl>(D)) {
102       Info.Lang = SymbolLanguage::CXX;
103       Info.Properties |= (unsigned)SymbolProperty::Generic;
104       Info.Properties |= (unsigned)SymbolProperty::TemplatePartialSpecialization;
105     } else if (isa<VarTemplateSpecializationDecl>(D)) {
106       Info.Lang = SymbolLanguage::CXX;
107       Info.Properties |= (unsigned)SymbolProperty::Generic;
108       Info.Properties |= (unsigned)SymbolProperty::TemplateSpecialization;
109     } else if (VD->getDescribedVarTemplate()) {
110       Info.Lang = SymbolLanguage::CXX;
111       Info.Properties |= (unsigned)SymbolProperty::Generic;
112     }
113
114   } else {
115     switch (D->getKind()) {
116     case Decl::Import:
117       Info.Kind = SymbolKind::Module;
118       break;
119     case Decl::Typedef:
120       Info.Kind = SymbolKind::TypeAlias; break; // Lang = C
121     case Decl::Function:
122       Info.Kind = SymbolKind::Function;
123       break;
124     case Decl::Field:
125       Info.Kind = SymbolKind::Field;
126       if (const CXXRecordDecl *
127             CXXRec = dyn_cast<CXXRecordDecl>(D->getDeclContext())) {
128         if (!CXXRec->isCLike())
129           Info.Lang = SymbolLanguage::CXX;
130       }
131       break;
132     case Decl::EnumConstant:
133       Info.Kind = SymbolKind::EnumConstant; break;
134     case Decl::ObjCInterface:
135     case Decl::ObjCImplementation: {
136       Info.Kind = SymbolKind::Class;
137       Info.Lang = SymbolLanguage::ObjC;
138       const ObjCInterfaceDecl *ClsD = dyn_cast<ObjCInterfaceDecl>(D);
139       if (!ClsD)
140         ClsD = cast<ObjCImplementationDecl>(D)->getClassInterface();
141       if (isUnitTestCase(ClsD))
142         Info.Properties |= (unsigned)SymbolProperty::UnitTest;
143       break;
144     }
145     case Decl::ObjCProtocol:
146       Info.Kind = SymbolKind::Protocol;
147       Info.Lang = SymbolLanguage::ObjC;
148       break;
149     case Decl::ObjCCategory:
150     case Decl::ObjCCategoryImpl:
151       Info.Kind = SymbolKind::Extension;
152       Info.Lang = SymbolLanguage::ObjC;
153       break;
154     case Decl::ObjCMethod:
155       if (cast<ObjCMethodDecl>(D)->isInstanceMethod())
156         Info.Kind = SymbolKind::InstanceMethod;
157       else
158         Info.Kind = SymbolKind::ClassMethod;
159       Info.Lang = SymbolLanguage::ObjC;
160       if (isUnitTest(cast<ObjCMethodDecl>(D)))
161         Info.Properties |= (unsigned)SymbolProperty::UnitTest;
162       if (D->hasAttr<IBActionAttr>())
163         Info.Properties |= (unsigned)SymbolProperty::IBAnnotated;
164       break;
165     case Decl::ObjCProperty:
166       Info.Kind = SymbolKind::InstanceProperty;
167       Info.Lang = SymbolLanguage::ObjC;
168       checkForIBOutlets(D, Info.Properties);
169       if (auto *Annot = D->getAttr<AnnotateAttr>()) {
170         if (Annot->getAnnotation() == "gk_inspectable")
171           Info.Properties |= (unsigned)SymbolProperty::GKInspectable;
172       }
173       break;
174     case Decl::ObjCIvar:
175       Info.Kind = SymbolKind::Field;
176       Info.Lang = SymbolLanguage::ObjC;
177       checkForIBOutlets(D, Info.Properties);
178       break;
179     case Decl::Namespace:
180       Info.Kind = SymbolKind::Namespace;
181       Info.Lang = SymbolLanguage::CXX;
182       break;
183     case Decl::NamespaceAlias:
184       Info.Kind = SymbolKind::NamespaceAlias;
185       Info.Lang = SymbolLanguage::CXX;
186       break;
187     case Decl::CXXConstructor: {
188       Info.Kind = SymbolKind::Constructor;
189       Info.Lang = SymbolLanguage::CXX;
190       auto *CD = cast<CXXConstructorDecl>(D);
191       if (CD->isCopyConstructor())
192         Info.SubKind = SymbolSubKind::CXXCopyConstructor;
193       else if (CD->isMoveConstructor())
194         Info.SubKind = SymbolSubKind::CXXMoveConstructor;
195       break;
196     }
197     case Decl::CXXDestructor:
198       Info.Kind = SymbolKind::Destructor;
199       Info.Lang = SymbolLanguage::CXX;
200       break;
201     case Decl::CXXConversion:
202       Info.Kind = SymbolKind::ConversionFunction;
203       Info.Lang = SymbolLanguage::CXX;
204       break;
205     case Decl::CXXMethod: {
206       const CXXMethodDecl *MD = cast<CXXMethodDecl>(D);
207       if (MD->isStatic())
208         Info.Kind = SymbolKind::StaticMethod;
209       else
210         Info.Kind = SymbolKind::InstanceMethod;
211       Info.Lang = SymbolLanguage::CXX;
212       break;
213     }
214     case Decl::ClassTemplate:
215       Info.Kind = SymbolKind::Class;
216       Info.Properties |= (unsigned)SymbolProperty::Generic;
217       Info.Lang = SymbolLanguage::CXX;
218       break;
219     case Decl::FunctionTemplate:
220       Info.Kind = SymbolKind::Function;
221       Info.Properties |= (unsigned)SymbolProperty::Generic;
222       Info.Lang = SymbolLanguage::CXX;
223       if (const CXXMethodDecl *MD = dyn_cast_or_null<CXXMethodDecl>(
224                            cast<FunctionTemplateDecl>(D)->getTemplatedDecl())) {
225         if (isa<CXXConstructorDecl>(MD))
226           Info.Kind = SymbolKind::Constructor;
227         else if (isa<CXXDestructorDecl>(MD))
228           Info.Kind = SymbolKind::Destructor;
229         else if (isa<CXXConversionDecl>(MD))
230           Info.Kind = SymbolKind::ConversionFunction;
231         else {
232           if (MD->isStatic())
233             Info.Kind = SymbolKind::StaticMethod;
234           else
235             Info.Kind = SymbolKind::InstanceMethod;
236         }
237       }
238       break;
239     case Decl::TypeAliasTemplate:
240       Info.Kind = SymbolKind::TypeAlias;
241       Info.Lang = SymbolLanguage::CXX;
242       Info.Properties |= (unsigned)SymbolProperty::Generic;
243       break;
244     case Decl::TypeAlias:
245       Info.Kind = SymbolKind::TypeAlias;
246       Info.Lang = SymbolLanguage::CXX;
247       break;
248     default:
249       break;
250     }
251   }
252
253   if (Info.Kind == SymbolKind::Unknown)
254     return Info;
255
256   if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
257     if (FD->getTemplatedKind() ==
258           FunctionDecl::TK_FunctionTemplateSpecialization) {
259       Info.Properties |= (unsigned)SymbolProperty::Generic;
260       Info.Properties |= (unsigned)SymbolProperty::TemplateSpecialization;
261     }
262   }
263
264   if (Info.Properties & (unsigned)SymbolProperty::Generic)
265     Info.Lang = SymbolLanguage::CXX;
266
267   return Info;
268 }
269
270 void index::applyForEachSymbolRole(SymbolRoleSet Roles,
271                                    llvm::function_ref<void(SymbolRole)> Fn) {
272 #define APPLY_FOR_ROLE(Role) \
273   if (Roles & (unsigned)SymbolRole::Role) \
274     Fn(SymbolRole::Role)
275
276   APPLY_FOR_ROLE(Declaration);
277   APPLY_FOR_ROLE(Definition);
278   APPLY_FOR_ROLE(Reference);
279   APPLY_FOR_ROLE(Read);
280   APPLY_FOR_ROLE(Write);
281   APPLY_FOR_ROLE(Call);
282   APPLY_FOR_ROLE(Dynamic);
283   APPLY_FOR_ROLE(AddressOf);
284   APPLY_FOR_ROLE(Implicit);
285   APPLY_FOR_ROLE(RelationChildOf);
286   APPLY_FOR_ROLE(RelationBaseOf);
287   APPLY_FOR_ROLE(RelationOverrideOf);
288   APPLY_FOR_ROLE(RelationReceivedBy);
289   APPLY_FOR_ROLE(RelationCalledBy);
290   APPLY_FOR_ROLE(RelationExtendedBy);
291   APPLY_FOR_ROLE(RelationAccessorOf);
292
293 #undef APPLY_FOR_ROLE
294 }
295
296 void index::printSymbolRoles(SymbolRoleSet Roles, raw_ostream &OS) {
297   bool VisitedOnce = false;
298   applyForEachSymbolRole(Roles, [&](SymbolRole Role) {
299     if (VisitedOnce)
300       OS << ',';
301     else
302       VisitedOnce = true;
303     switch (Role) {
304     case SymbolRole::Declaration: OS << "Decl"; break;
305     case SymbolRole::Definition: OS << "Def"; break;
306     case SymbolRole::Reference: OS << "Ref"; break;
307     case SymbolRole::Read: OS << "Read"; break;
308     case SymbolRole::Write: OS << "Writ"; break;
309     case SymbolRole::Call: OS << "Call"; break;
310     case SymbolRole::Dynamic: OS << "Dyn"; break;
311     case SymbolRole::AddressOf: OS << "Addr"; break;
312     case SymbolRole::Implicit: OS << "Impl"; break;
313     case SymbolRole::RelationChildOf: OS << "RelChild"; break;
314     case SymbolRole::RelationBaseOf: OS << "RelBase"; break;
315     case SymbolRole::RelationOverrideOf: OS << "RelOver"; break;
316     case SymbolRole::RelationReceivedBy: OS << "RelRec"; break;
317     case SymbolRole::RelationCalledBy: OS << "RelCall"; break;
318     case SymbolRole::RelationExtendedBy: OS << "RelExt"; break;
319     case SymbolRole::RelationAccessorOf: OS << "RelAcc"; break;
320     }
321   });
322 }
323
324 bool index::printSymbolName(const Decl *D, const LangOptions &LO,
325                             raw_ostream &OS) {
326   if (auto *ND = dyn_cast<NamedDecl>(D)) {
327     PrintingPolicy Policy(LO);
328     // Forward references can have different template argument names. Suppress
329     // the template argument names in constructors to make their name more
330     // stable.
331     Policy.SuppressTemplateArgsInCXXConstructors = true;
332     DeclarationName DeclName = ND->getDeclName();
333     if (DeclName.isEmpty())
334       return true;
335     DeclName.print(OS, Policy);
336     return false;
337   } else {
338     return true;
339   }
340 }
341
342 StringRef index::getSymbolKindString(SymbolKind K) {
343   switch (K) {
344   case SymbolKind::Unknown: return "<unknown>";
345   case SymbolKind::Module: return "module";
346   case SymbolKind::Namespace: return "namespace";
347   case SymbolKind::NamespaceAlias: return "namespace-alias";
348   case SymbolKind::Macro: return "macro";
349   case SymbolKind::Enum: return "enum";
350   case SymbolKind::Struct: return "struct";
351   case SymbolKind::Class: return "class";
352   case SymbolKind::Protocol: return "protocol";
353   case SymbolKind::Extension: return "extension";
354   case SymbolKind::Union: return "union";
355   case SymbolKind::TypeAlias: return "type-alias";
356   case SymbolKind::Function: return "function";
357   case SymbolKind::Variable: return "variable";
358   case SymbolKind::Field: return "field";
359   case SymbolKind::EnumConstant: return "enumerator";
360   case SymbolKind::InstanceMethod: return "instance-method";
361   case SymbolKind::ClassMethod: return "class-method";
362   case SymbolKind::StaticMethod: return "static-method";
363   case SymbolKind::InstanceProperty: return "instance-property";
364   case SymbolKind::ClassProperty: return "class-property";
365   case SymbolKind::StaticProperty: return "static-property";
366   case SymbolKind::Constructor: return "constructor";
367   case SymbolKind::Destructor: return "destructor";
368   case SymbolKind::ConversionFunction: return "coversion-func";
369   }
370   llvm_unreachable("invalid symbol kind");
371 }
372
373 StringRef index::getSymbolSubKindString(SymbolSubKind K) {
374   switch (K) {
375   case SymbolSubKind::None: return "<none>";
376   case SymbolSubKind::CXXCopyConstructor: return "cxx-copy-ctor";
377   case SymbolSubKind::CXXMoveConstructor: return "cxx-move-ctor";
378   }
379   llvm_unreachable("invalid symbol subkind");
380 }
381
382 StringRef index::getSymbolLanguageString(SymbolLanguage K) {
383   switch (K) {
384   case SymbolLanguage::C: return "C";
385   case SymbolLanguage::ObjC: return "ObjC";
386   case SymbolLanguage::CXX: return "C++";
387   }
388   llvm_unreachable("invalid symbol language kind");
389 }
390
391 void index::applyForEachSymbolProperty(SymbolPropertySet Props,
392                                   llvm::function_ref<void(SymbolProperty)> Fn) {
393 #define APPLY_FOR_PROPERTY(K) \
394   if (Props & (unsigned)SymbolProperty::K) \
395     Fn(SymbolProperty::K)
396
397   APPLY_FOR_PROPERTY(Generic);
398   APPLY_FOR_PROPERTY(TemplatePartialSpecialization);
399   APPLY_FOR_PROPERTY(TemplateSpecialization);
400   APPLY_FOR_PROPERTY(UnitTest);
401   APPLY_FOR_PROPERTY(IBAnnotated);
402   APPLY_FOR_PROPERTY(IBOutletCollection);
403   APPLY_FOR_PROPERTY(GKInspectable);
404
405 #undef APPLY_FOR_PROPERTY
406 }
407
408 void index::printSymbolProperties(SymbolPropertySet Props, raw_ostream &OS) {
409   bool VisitedOnce = false;
410   applyForEachSymbolProperty(Props, [&](SymbolProperty Prop) {
411     if (VisitedOnce)
412       OS << ',';
413     else
414       VisitedOnce = true;
415     switch (Prop) {
416     case SymbolProperty::Generic: OS << "Gen"; break;
417     case SymbolProperty::TemplatePartialSpecialization: OS << "TPS"; break;
418     case SymbolProperty::TemplateSpecialization: OS << "TS"; break;
419     case SymbolProperty::UnitTest: OS << "test"; break;
420     case SymbolProperty::IBAnnotated: OS << "IB"; break;
421     case SymbolProperty::IBOutletCollection: OS << "IBColl"; break;
422     case SymbolProperty::GKInspectable: OS << "GKI"; break;
423     }
424   });
425 }