]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/llvm/tools/clang/lib/Index/IndexSymbol.cpp
Merge llvm, clang, lld, lldb, compiler-rt and libc++ r301441, and update
[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 bool index::isFunctionLocalSymbol(const Decl *D) {
53   assert(D);
54
55   if (isa<ParmVarDecl>(D))
56     return true;
57
58   if (isa<TemplateTemplateParmDecl>(D))
59     return true;
60
61   if (isa<ObjCTypeParamDecl>(D))
62     return true;
63
64   if (!D->getParentFunctionOrMethod())
65     return false;
66
67   if (const NamedDecl *ND = dyn_cast<NamedDecl>(D)) {
68     switch (ND->getFormalLinkage()) {
69       case NoLinkage:
70       case VisibleNoLinkage:
71       case InternalLinkage:
72         return true;
73       case UniqueExternalLinkage:
74         llvm_unreachable("Not a sema linkage");
75       case ExternalLinkage:
76         return false;
77     }
78   }
79
80   return true;
81 }
82
83 SymbolInfo index::getSymbolInfo(const Decl *D) {
84   assert(D);
85   SymbolInfo Info;
86   Info.Kind = SymbolKind::Unknown;
87   Info.SubKind = SymbolSubKind::None;
88   Info.Properties = SymbolPropertySet();
89   Info.Lang = SymbolLanguage::C;
90
91   if (isFunctionLocalSymbol(D)) {
92     Info.Properties |= (unsigned)SymbolProperty::Local;
93   }
94
95   if (const TagDecl *TD = dyn_cast<TagDecl>(D)) {
96     switch (TD->getTagKind()) {
97     case TTK_Struct:
98       Info.Kind = SymbolKind::Struct; break;
99     case TTK_Union:
100       Info.Kind = SymbolKind::Union; break;
101     case TTK_Class:
102       Info.Kind = SymbolKind::Class;
103       Info.Lang = SymbolLanguage::CXX;
104       break;
105     case TTK_Interface:
106       Info.Kind = SymbolKind::Protocol;
107       Info.Lang = SymbolLanguage::CXX;
108       break;
109     case TTK_Enum:
110       Info.Kind = SymbolKind::Enum; break;
111     }
112
113     if (const CXXRecordDecl *CXXRec = dyn_cast<CXXRecordDecl>(D)) {
114       if (!CXXRec->isCLike()) {
115         Info.Lang = SymbolLanguage::CXX;
116         if (CXXRec->getDescribedClassTemplate()) {
117           Info.Properties |= (unsigned)SymbolProperty::Generic;
118         }
119       }
120     }
121
122     if (isa<ClassTemplatePartialSpecializationDecl>(D)) {
123       Info.Properties |= (unsigned)SymbolProperty::Generic;
124       Info.Properties |= (unsigned)SymbolProperty::TemplatePartialSpecialization;
125     } else if (isa<ClassTemplateSpecializationDecl>(D)) {
126       Info.Properties |= (unsigned)SymbolProperty::Generic;
127       Info.Properties |= (unsigned)SymbolProperty::TemplateSpecialization;
128     }
129
130   } else if (auto *VD = dyn_cast<VarDecl>(D)) {
131     Info.Kind = SymbolKind::Variable;
132     if (isa<ParmVarDecl>(D)) {
133       Info.Kind = SymbolKind::Parameter;
134     } else if (isa<CXXRecordDecl>(D->getDeclContext())) {
135       Info.Kind = SymbolKind::StaticProperty;
136       Info.Lang = SymbolLanguage::CXX;
137     }
138
139     if (isa<VarTemplatePartialSpecializationDecl>(D)) {
140       Info.Lang = SymbolLanguage::CXX;
141       Info.Properties |= (unsigned)SymbolProperty::Generic;
142       Info.Properties |= (unsigned)SymbolProperty::TemplatePartialSpecialization;
143     } else if (isa<VarTemplateSpecializationDecl>(D)) {
144       Info.Lang = SymbolLanguage::CXX;
145       Info.Properties |= (unsigned)SymbolProperty::Generic;
146       Info.Properties |= (unsigned)SymbolProperty::TemplateSpecialization;
147     } else if (VD->getDescribedVarTemplate()) {
148       Info.Lang = SymbolLanguage::CXX;
149       Info.Properties |= (unsigned)SymbolProperty::Generic;
150     }
151
152   } else {
153     switch (D->getKind()) {
154     case Decl::Import:
155       Info.Kind = SymbolKind::Module;
156       break;
157     case Decl::Typedef:
158       Info.Kind = SymbolKind::TypeAlias; break; // Lang = C
159     case Decl::Function:
160       Info.Kind = SymbolKind::Function;
161       break;
162     case Decl::Field:
163       Info.Kind = SymbolKind::Field;
164       if (const CXXRecordDecl *
165             CXXRec = dyn_cast<CXXRecordDecl>(D->getDeclContext())) {
166         if (!CXXRec->isCLike())
167           Info.Lang = SymbolLanguage::CXX;
168       }
169       break;
170     case Decl::EnumConstant:
171       Info.Kind = SymbolKind::EnumConstant; break;
172     case Decl::ObjCInterface:
173     case Decl::ObjCImplementation: {
174       Info.Kind = SymbolKind::Class;
175       Info.Lang = SymbolLanguage::ObjC;
176       const ObjCInterfaceDecl *ClsD = dyn_cast<ObjCInterfaceDecl>(D);
177       if (!ClsD)
178         ClsD = cast<ObjCImplementationDecl>(D)->getClassInterface();
179       if (isUnitTestCase(ClsD))
180         Info.Properties |= (unsigned)SymbolProperty::UnitTest;
181       break;
182     }
183     case Decl::ObjCProtocol:
184       Info.Kind = SymbolKind::Protocol;
185       Info.Lang = SymbolLanguage::ObjC;
186       break;
187     case Decl::ObjCCategory:
188     case Decl::ObjCCategoryImpl: {
189       Info.Kind = SymbolKind::Extension;
190       Info.Lang = SymbolLanguage::ObjC;
191       const ObjCInterfaceDecl *ClsD = nullptr;
192       if (auto *CatD = dyn_cast<ObjCCategoryDecl>(D))
193         ClsD = CatD->getClassInterface();
194       else
195         ClsD = cast<ObjCCategoryImplDecl>(D)->getClassInterface();
196       if (isUnitTestCase(ClsD))
197         Info.Properties |= (unsigned)SymbolProperty::UnitTest;
198       break;
199     }
200     case Decl::ObjCMethod:
201       if (cast<ObjCMethodDecl>(D)->isInstanceMethod()) {
202         const ObjCMethodDecl *MD = cast<ObjCMethodDecl>(D);
203         Info.Kind = SymbolKind::InstanceMethod;
204         if (MD->isPropertyAccessor()) {
205           if (MD->param_size())
206             Info.SubKind = SymbolSubKind::AccessorSetter;
207           else
208             Info.SubKind = SymbolSubKind::AccessorGetter;
209         }
210       } else {
211         Info.Kind = SymbolKind::ClassMethod;
212       }
213       Info.Lang = SymbolLanguage::ObjC;
214       if (isUnitTest(cast<ObjCMethodDecl>(D)))
215         Info.Properties |= (unsigned)SymbolProperty::UnitTest;
216       if (D->hasAttr<IBActionAttr>())
217         Info.Properties |= (unsigned)SymbolProperty::IBAnnotated;
218       break;
219     case Decl::ObjCProperty:
220       Info.Kind = SymbolKind::InstanceProperty;
221       Info.Lang = SymbolLanguage::ObjC;
222       checkForIBOutlets(D, Info.Properties);
223       if (auto *Annot = D->getAttr<AnnotateAttr>()) {
224         if (Annot->getAnnotation() == "gk_inspectable")
225           Info.Properties |= (unsigned)SymbolProperty::GKInspectable;
226       }
227       break;
228     case Decl::ObjCIvar:
229       Info.Kind = SymbolKind::Field;
230       Info.Lang = SymbolLanguage::ObjC;
231       checkForIBOutlets(D, Info.Properties);
232       break;
233     case Decl::Namespace:
234       Info.Kind = SymbolKind::Namespace;
235       Info.Lang = SymbolLanguage::CXX;
236       break;
237     case Decl::NamespaceAlias:
238       Info.Kind = SymbolKind::NamespaceAlias;
239       Info.Lang = SymbolLanguage::CXX;
240       break;
241     case Decl::CXXConstructor: {
242       Info.Kind = SymbolKind::Constructor;
243       Info.Lang = SymbolLanguage::CXX;
244       auto *CD = cast<CXXConstructorDecl>(D);
245       if (CD->isCopyConstructor())
246         Info.SubKind = SymbolSubKind::CXXCopyConstructor;
247       else if (CD->isMoveConstructor())
248         Info.SubKind = SymbolSubKind::CXXMoveConstructor;
249       break;
250     }
251     case Decl::CXXDestructor:
252       Info.Kind = SymbolKind::Destructor;
253       Info.Lang = SymbolLanguage::CXX;
254       break;
255     case Decl::CXXConversion:
256       Info.Kind = SymbolKind::ConversionFunction;
257       Info.Lang = SymbolLanguage::CXX;
258       break;
259     case Decl::CXXMethod: {
260       const CXXMethodDecl *MD = cast<CXXMethodDecl>(D);
261       if (MD->isStatic())
262         Info.Kind = SymbolKind::StaticMethod;
263       else
264         Info.Kind = SymbolKind::InstanceMethod;
265       Info.Lang = SymbolLanguage::CXX;
266       break;
267     }
268     case Decl::ClassTemplate:
269       Info.Kind = SymbolKind::Class;
270       Info.Properties |= (unsigned)SymbolProperty::Generic;
271       Info.Lang = SymbolLanguage::CXX;
272       break;
273     case Decl::FunctionTemplate:
274       Info.Kind = SymbolKind::Function;
275       Info.Properties |= (unsigned)SymbolProperty::Generic;
276       Info.Lang = SymbolLanguage::CXX;
277       if (const CXXMethodDecl *MD = dyn_cast_or_null<CXXMethodDecl>(
278                            cast<FunctionTemplateDecl>(D)->getTemplatedDecl())) {
279         if (isa<CXXConstructorDecl>(MD))
280           Info.Kind = SymbolKind::Constructor;
281         else if (isa<CXXDestructorDecl>(MD))
282           Info.Kind = SymbolKind::Destructor;
283         else if (isa<CXXConversionDecl>(MD))
284           Info.Kind = SymbolKind::ConversionFunction;
285         else {
286           if (MD->isStatic())
287             Info.Kind = SymbolKind::StaticMethod;
288           else
289             Info.Kind = SymbolKind::InstanceMethod;
290         }
291       }
292       break;
293     case Decl::TypeAliasTemplate:
294       Info.Kind = SymbolKind::TypeAlias;
295       Info.Lang = SymbolLanguage::CXX;
296       Info.Properties |= (unsigned)SymbolProperty::Generic;
297       break;
298     case Decl::TypeAlias:
299       Info.Kind = SymbolKind::TypeAlias;
300       Info.Lang = SymbolLanguage::CXX;
301       break;
302     default:
303       break;
304     }
305   }
306
307   if (Info.Kind == SymbolKind::Unknown)
308     return Info;
309
310   if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
311     if (FD->getTemplatedKind() ==
312           FunctionDecl::TK_FunctionTemplateSpecialization) {
313       Info.Properties |= (unsigned)SymbolProperty::Generic;
314       Info.Properties |= (unsigned)SymbolProperty::TemplateSpecialization;
315     }
316   }
317
318   if (Info.Properties & (unsigned)SymbolProperty::Generic)
319     Info.Lang = SymbolLanguage::CXX;
320
321   auto getExternalSymAttr = [](const Decl *D) -> ExternalSourceSymbolAttr* {
322     if (auto *attr = D->getAttr<ExternalSourceSymbolAttr>())
323       return attr;
324     if (auto *dcd = dyn_cast<Decl>(D->getDeclContext())) {
325       if (auto *attr = dcd->getAttr<ExternalSourceSymbolAttr>())
326         return attr;
327     }
328     return nullptr;
329   };
330   if (auto *attr = getExternalSymAttr(D)) {
331     if (attr->getLanguage() == "Swift")
332       Info.Lang = SymbolLanguage::Swift;
333   }
334
335   return Info;
336 }
337
338 bool index::applyForEachSymbolRoleInterruptible(SymbolRoleSet Roles,
339                                    llvm::function_ref<bool(SymbolRole)> Fn) {
340 #define APPLY_FOR_ROLE(Role) \
341   if (Roles & (unsigned)SymbolRole::Role) \
342     if (!Fn(SymbolRole::Role)) \
343       return false;
344
345   APPLY_FOR_ROLE(Declaration);
346   APPLY_FOR_ROLE(Definition);
347   APPLY_FOR_ROLE(Reference);
348   APPLY_FOR_ROLE(Read);
349   APPLY_FOR_ROLE(Write);
350   APPLY_FOR_ROLE(Call);
351   APPLY_FOR_ROLE(Dynamic);
352   APPLY_FOR_ROLE(AddressOf);
353   APPLY_FOR_ROLE(Implicit);
354   APPLY_FOR_ROLE(RelationChildOf);
355   APPLY_FOR_ROLE(RelationBaseOf);
356   APPLY_FOR_ROLE(RelationOverrideOf);
357   APPLY_FOR_ROLE(RelationReceivedBy);
358   APPLY_FOR_ROLE(RelationCalledBy);
359   APPLY_FOR_ROLE(RelationExtendedBy);
360   APPLY_FOR_ROLE(RelationAccessorOf);
361   APPLY_FOR_ROLE(RelationContainedBy);
362   APPLY_FOR_ROLE(RelationIBTypeOf);
363   APPLY_FOR_ROLE(RelationSpecializationOf);
364
365 #undef APPLY_FOR_ROLE
366
367   return true;
368 }
369
370 void index::applyForEachSymbolRole(SymbolRoleSet Roles,
371                                    llvm::function_ref<void(SymbolRole)> Fn) {
372   applyForEachSymbolRoleInterruptible(Roles, [&](SymbolRole r) -> bool {
373     Fn(r);
374     return true;
375   });
376 }
377
378 void index::printSymbolRoles(SymbolRoleSet Roles, raw_ostream &OS) {
379   bool VisitedOnce = false;
380   applyForEachSymbolRole(Roles, [&](SymbolRole Role) {
381     if (VisitedOnce)
382       OS << ',';
383     else
384       VisitedOnce = true;
385     switch (Role) {
386     case SymbolRole::Declaration: OS << "Decl"; break;
387     case SymbolRole::Definition: OS << "Def"; break;
388     case SymbolRole::Reference: OS << "Ref"; break;
389     case SymbolRole::Read: OS << "Read"; break;
390     case SymbolRole::Write: OS << "Writ"; break;
391     case SymbolRole::Call: OS << "Call"; break;
392     case SymbolRole::Dynamic: OS << "Dyn"; break;
393     case SymbolRole::AddressOf: OS << "Addr"; break;
394     case SymbolRole::Implicit: OS << "Impl"; break;
395     case SymbolRole::RelationChildOf: OS << "RelChild"; break;
396     case SymbolRole::RelationBaseOf: OS << "RelBase"; break;
397     case SymbolRole::RelationOverrideOf: OS << "RelOver"; break;
398     case SymbolRole::RelationReceivedBy: OS << "RelRec"; break;
399     case SymbolRole::RelationCalledBy: OS << "RelCall"; break;
400     case SymbolRole::RelationExtendedBy: OS << "RelExt"; break;
401     case SymbolRole::RelationAccessorOf: OS << "RelAcc"; break;
402     case SymbolRole::RelationContainedBy: OS << "RelCont"; break;
403     case SymbolRole::RelationIBTypeOf: OS << "RelIBType"; break;
404     case SymbolRole::RelationSpecializationOf: OS << "RelSpecialization"; break;
405     }
406   });
407 }
408
409 bool index::printSymbolName(const Decl *D, const LangOptions &LO,
410                             raw_ostream &OS) {
411   if (auto *ND = dyn_cast<NamedDecl>(D)) {
412     PrintingPolicy Policy(LO);
413     // Forward references can have different template argument names. Suppress
414     // the template argument names in constructors to make their name more
415     // stable.
416     Policy.SuppressTemplateArgsInCXXConstructors = true;
417     DeclarationName DeclName = ND->getDeclName();
418     if (DeclName.isEmpty())
419       return true;
420     DeclName.print(OS, Policy);
421     return false;
422   } else {
423     return true;
424   }
425 }
426
427 StringRef index::getSymbolKindString(SymbolKind K) {
428   switch (K) {
429   case SymbolKind::Unknown: return "<unknown>";
430   case SymbolKind::Module: return "module";
431   case SymbolKind::Namespace: return "namespace";
432   case SymbolKind::NamespaceAlias: return "namespace-alias";
433   case SymbolKind::Macro: return "macro";
434   case SymbolKind::Enum: return "enum";
435   case SymbolKind::Struct: return "struct";
436   case SymbolKind::Class: return "class";
437   case SymbolKind::Protocol: return "protocol";
438   case SymbolKind::Extension: return "extension";
439   case SymbolKind::Union: return "union";
440   case SymbolKind::TypeAlias: return "type-alias";
441   case SymbolKind::Function: return "function";
442   case SymbolKind::Variable: return "variable";
443   case SymbolKind::Field: return "field";
444   case SymbolKind::EnumConstant: return "enumerator";
445   case SymbolKind::InstanceMethod: return "instance-method";
446   case SymbolKind::ClassMethod: return "class-method";
447   case SymbolKind::StaticMethod: return "static-method";
448   case SymbolKind::InstanceProperty: return "instance-property";
449   case SymbolKind::ClassProperty: return "class-property";
450   case SymbolKind::StaticProperty: return "static-property";
451   case SymbolKind::Constructor: return "constructor";
452   case SymbolKind::Destructor: return "destructor";
453   case SymbolKind::ConversionFunction: return "coversion-func";
454   case SymbolKind::Parameter: return "param";
455   }
456   llvm_unreachable("invalid symbol kind");
457 }
458
459 StringRef index::getSymbolSubKindString(SymbolSubKind K) {
460   switch (K) {
461   case SymbolSubKind::None: return "<none>";
462   case SymbolSubKind::CXXCopyConstructor: return "cxx-copy-ctor";
463   case SymbolSubKind::CXXMoveConstructor: return "cxx-move-ctor";
464   case SymbolSubKind::AccessorGetter: return "acc-get";
465   case SymbolSubKind::AccessorSetter: return "acc-set";
466   }
467   llvm_unreachable("invalid symbol subkind");
468 }
469
470 StringRef index::getSymbolLanguageString(SymbolLanguage K) {
471   switch (K) {
472   case SymbolLanguage::C: return "C";
473   case SymbolLanguage::ObjC: return "ObjC";
474   case SymbolLanguage::CXX: return "C++";
475   case SymbolLanguage::Swift: return "Swift";
476   }
477   llvm_unreachable("invalid symbol language kind");
478 }
479
480 void index::applyForEachSymbolProperty(SymbolPropertySet Props,
481                                   llvm::function_ref<void(SymbolProperty)> Fn) {
482 #define APPLY_FOR_PROPERTY(K) \
483   if (Props & (unsigned)SymbolProperty::K) \
484     Fn(SymbolProperty::K)
485
486   APPLY_FOR_PROPERTY(Generic);
487   APPLY_FOR_PROPERTY(TemplatePartialSpecialization);
488   APPLY_FOR_PROPERTY(TemplateSpecialization);
489   APPLY_FOR_PROPERTY(UnitTest);
490   APPLY_FOR_PROPERTY(IBAnnotated);
491   APPLY_FOR_PROPERTY(IBOutletCollection);
492   APPLY_FOR_PROPERTY(GKInspectable);
493   APPLY_FOR_PROPERTY(Local);
494
495 #undef APPLY_FOR_PROPERTY
496 }
497
498 void index::printSymbolProperties(SymbolPropertySet Props, raw_ostream &OS) {
499   bool VisitedOnce = false;
500   applyForEachSymbolProperty(Props, [&](SymbolProperty Prop) {
501     if (VisitedOnce)
502       OS << ',';
503     else
504       VisitedOnce = true;
505     switch (Prop) {
506     case SymbolProperty::Generic: OS << "Gen"; break;
507     case SymbolProperty::TemplatePartialSpecialization: OS << "TPS"; break;
508     case SymbolProperty::TemplateSpecialization: OS << "TS"; break;
509     case SymbolProperty::UnitTest: OS << "test"; break;
510     case SymbolProperty::IBAnnotated: OS << "IB"; break;
511     case SymbolProperty::IBOutletCollection: OS << "IBColl"; break;
512     case SymbolProperty::GKInspectable: OS << "GKI"; break;
513     case SymbolProperty::Local: OS << "local"; break;
514     }
515   });
516 }