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