xref: /openbsd/gnu/llvm/clang/lib/Index/IndexSymbol.cpp (revision 12c85518)
1 //===--- IndexSymbol.cpp - Types and functions for indexing symbols -------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 
9 #include "clang/Index/IndexSymbol.h"
10 #include "clang/AST/Attr.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 #include "clang/Lex/MacroInfo.h"
16 
17 using namespace clang;
18 using namespace clang::index;
19 
20 /// \returns true if \c D is a subclass of 'XCTestCase'.
isUnitTestCase(const ObjCInterfaceDecl * D)21 static bool isUnitTestCase(const ObjCInterfaceDecl *D) {
22   if (!D)
23     return false;
24   while (const ObjCInterfaceDecl *SuperD = D->getSuperClass()) {
25     if (SuperD->getName() == "XCTestCase")
26       return true;
27     D = SuperD;
28   }
29   return false;
30 }
31 
32 /// \returns true if \c D is in a subclass of 'XCTestCase', returns void, has
33 /// no parameters, and its name starts with 'test'.
isUnitTest(const ObjCMethodDecl * D)34 static bool isUnitTest(const ObjCMethodDecl *D) {
35   if (!D->parameters().empty())
36     return false;
37   if (!D->getReturnType()->isVoidType())
38     return false;
39   if (!D->getSelector().getNameForSlot(0).startswith("test"))
40     return false;
41   return isUnitTestCase(D->getClassInterface());
42 }
43 
checkForIBOutlets(const Decl * D,SymbolPropertySet & PropSet)44 static void checkForIBOutlets(const Decl *D, SymbolPropertySet &PropSet) {
45   if (D->hasAttr<IBOutletAttr>()) {
46     PropSet |= (SymbolPropertySet)SymbolProperty::IBAnnotated;
47   } else if (D->hasAttr<IBOutletCollectionAttr>()) {
48     PropSet |= (SymbolPropertySet)SymbolProperty::IBAnnotated;
49     PropSet |= (SymbolPropertySet)SymbolProperty::IBOutletCollection;
50   }
51 }
52 
isFunctionLocalSymbol(const Decl * D)53 bool index::isFunctionLocalSymbol(const Decl *D) {
54   assert(D);
55 
56   if (isa<ParmVarDecl>(D))
57     return true;
58 
59   if (isa<ObjCTypeParamDecl>(D))
60     return true;
61 
62   if (isa<UsingDirectiveDecl>(D))
63     return false;
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 InternalLinkage:
71         return true;
72       case VisibleNoLinkage:
73       case UniqueExternalLinkage:
74       case ModuleInternalLinkage:
75         llvm_unreachable("Not a sema linkage");
76       case ModuleLinkage:
77       case ExternalLinkage:
78         return false;
79     }
80   }
81 
82   return true;
83 }
84 
getSymbolInfo(const Decl * D)85 SymbolInfo index::getSymbolInfo(const Decl *D) {
86   assert(D);
87   SymbolInfo Info;
88   Info.Kind = SymbolKind::Unknown;
89   Info.SubKind = SymbolSubKind::None;
90   Info.Properties = SymbolPropertySet();
91   Info.Lang = SymbolLanguage::C;
92 
93   if (isFunctionLocalSymbol(D)) {
94     Info.Properties |= (SymbolPropertySet)SymbolProperty::Local;
95   }
96   if (isa<ObjCProtocolDecl>(D->getDeclContext())) {
97     Info.Properties |= (SymbolPropertySet)SymbolProperty::ProtocolInterface;
98   }
99 
100   if (auto *VT = dyn_cast<VarTemplateDecl>(D)) {
101     Info.Properties |= (SymbolPropertySet)SymbolProperty::Generic;
102     Info.Lang = SymbolLanguage::CXX;
103     // All other fields are filled from the templated decl.
104     D = VT->getTemplatedDecl();
105   }
106 
107   if (const TagDecl *TD = dyn_cast<TagDecl>(D)) {
108     switch (TD->getTagKind()) {
109     case TTK_Struct:
110       Info.Kind = SymbolKind::Struct; break;
111     case TTK_Union:
112       Info.Kind = SymbolKind::Union; break;
113     case TTK_Class:
114       Info.Kind = SymbolKind::Class;
115       Info.Lang = SymbolLanguage::CXX;
116       break;
117     case TTK_Interface:
118       Info.Kind = SymbolKind::Protocol;
119       Info.Lang = SymbolLanguage::CXX;
120       break;
121     case TTK_Enum:
122       Info.Kind = SymbolKind::Enum; break;
123     }
124 
125     if (const CXXRecordDecl *CXXRec = dyn_cast<CXXRecordDecl>(D)) {
126       if (!CXXRec->isCLike()) {
127         Info.Lang = SymbolLanguage::CXX;
128         if (CXXRec->getDescribedClassTemplate()) {
129           Info.Properties |= (SymbolPropertySet)SymbolProperty::Generic;
130         }
131       }
132     }
133 
134     if (isa<ClassTemplatePartialSpecializationDecl>(D)) {
135       Info.Properties |= (SymbolPropertySet)SymbolProperty::Generic;
136       Info.Properties |=
137           (SymbolPropertySet)SymbolProperty::TemplatePartialSpecialization;
138     } else if (isa<ClassTemplateSpecializationDecl>(D)) {
139       Info.Properties |= (SymbolPropertySet)SymbolProperty::Generic;
140       Info.Properties |=
141           (SymbolPropertySet)SymbolProperty::TemplateSpecialization;
142     }
143 
144   } else if (auto *VD = dyn_cast<VarDecl>(D)) {
145     Info.Kind = SymbolKind::Variable;
146     if (isa<ParmVarDecl>(D)) {
147       Info.Kind = SymbolKind::Parameter;
148     } else if (isa<CXXRecordDecl>(D->getDeclContext())) {
149       Info.Kind = SymbolKind::StaticProperty;
150       Info.Lang = SymbolLanguage::CXX;
151     }
152 
153     if (isa<VarTemplatePartialSpecializationDecl>(D)) {
154       Info.Lang = SymbolLanguage::CXX;
155       Info.Properties |= (SymbolPropertySet)SymbolProperty::Generic;
156       Info.Properties |=
157           (SymbolPropertySet)SymbolProperty::TemplatePartialSpecialization;
158     } else if (isa<VarTemplateSpecializationDecl>(D)) {
159       Info.Lang = SymbolLanguage::CXX;
160       Info.Properties |= (SymbolPropertySet)SymbolProperty::Generic;
161       Info.Properties |=
162           (SymbolPropertySet)SymbolProperty::TemplateSpecialization;
163     } else if (VD->getDescribedVarTemplate()) {
164       Info.Lang = SymbolLanguage::CXX;
165       Info.Properties |= (SymbolPropertySet)SymbolProperty::Generic;
166     }
167 
168   } else {
169     switch (D->getKind()) {
170     case Decl::Import:
171       Info.Kind = SymbolKind::Module;
172       break;
173     case Decl::Typedef:
174       Info.Kind = SymbolKind::TypeAlias; break; // Lang = C
175     case Decl::Function:
176       Info.Kind = SymbolKind::Function;
177       break;
178     case Decl::Field:
179     case Decl::IndirectField:
180       Info.Kind = SymbolKind::Field;
181       if (const CXXRecordDecl *
182             CXXRec = dyn_cast<CXXRecordDecl>(D->getDeclContext())) {
183         if (!CXXRec->isCLike())
184           Info.Lang = SymbolLanguage::CXX;
185       }
186       break;
187     case Decl::EnumConstant:
188       Info.Kind = SymbolKind::EnumConstant; break;
189     case Decl::ObjCInterface:
190     case Decl::ObjCImplementation: {
191       Info.Kind = SymbolKind::Class;
192       Info.Lang = SymbolLanguage::ObjC;
193       const ObjCInterfaceDecl *ClsD = dyn_cast<ObjCInterfaceDecl>(D);
194       if (!ClsD)
195         ClsD = cast<ObjCImplementationDecl>(D)->getClassInterface();
196       if (isUnitTestCase(ClsD))
197         Info.Properties |= (SymbolPropertySet)SymbolProperty::UnitTest;
198       break;
199     }
200     case Decl::ObjCProtocol:
201       Info.Kind = SymbolKind::Protocol;
202       Info.Lang = SymbolLanguage::ObjC;
203       break;
204     case Decl::ObjCCategory:
205     case Decl::ObjCCategoryImpl: {
206       Info.Kind = SymbolKind::Extension;
207       Info.Lang = SymbolLanguage::ObjC;
208       const ObjCInterfaceDecl *ClsD = nullptr;
209       if (auto *CatD = dyn_cast<ObjCCategoryDecl>(D))
210         ClsD = CatD->getClassInterface();
211       else
212         ClsD = cast<ObjCCategoryImplDecl>(D)->getClassInterface();
213       if (isUnitTestCase(ClsD))
214         Info.Properties |= (SymbolPropertySet)SymbolProperty::UnitTest;
215       break;
216     }
217     case Decl::ObjCMethod: {
218       const ObjCMethodDecl *MD = cast<ObjCMethodDecl>(D);
219       Info.Kind = MD->isInstanceMethod() ? SymbolKind::InstanceMethod : SymbolKind::ClassMethod;
220       if (MD->isPropertyAccessor()) {
221         if (MD->param_size())
222           Info.SubKind = SymbolSubKind::AccessorSetter;
223         else
224           Info.SubKind = SymbolSubKind::AccessorGetter;
225       }
226       Info.Lang = SymbolLanguage::ObjC;
227       if (isUnitTest(MD))
228         Info.Properties |= (SymbolPropertySet)SymbolProperty::UnitTest;
229       if (D->hasAttr<IBActionAttr>())
230         Info.Properties |= (SymbolPropertySet)SymbolProperty::IBAnnotated;
231       break;
232     }
233     case Decl::ObjCProperty:
234       Info.Kind = SymbolKind::InstanceProperty;
235       Info.Lang = SymbolLanguage::ObjC;
236       checkForIBOutlets(D, Info.Properties);
237       if (auto *Annot = D->getAttr<AnnotateAttr>()) {
238         if (Annot->getAnnotation() == "gk_inspectable")
239           Info.Properties |= (SymbolPropertySet)SymbolProperty::GKInspectable;
240       }
241       break;
242     case Decl::ObjCIvar:
243       Info.Kind = SymbolKind::Field;
244       Info.Lang = SymbolLanguage::ObjC;
245       checkForIBOutlets(D, Info.Properties);
246       break;
247     case Decl::Namespace:
248       Info.Kind = SymbolKind::Namespace;
249       Info.Lang = SymbolLanguage::CXX;
250       break;
251     case Decl::NamespaceAlias:
252       Info.Kind = SymbolKind::NamespaceAlias;
253       Info.Lang = SymbolLanguage::CXX;
254       break;
255     case Decl::CXXConstructor: {
256       Info.Kind = SymbolKind::Constructor;
257       Info.Lang = SymbolLanguage::CXX;
258       auto *CD = cast<CXXConstructorDecl>(D);
259       if (CD->isCopyConstructor())
260         Info.SubKind = SymbolSubKind::CXXCopyConstructor;
261       else if (CD->isMoveConstructor())
262         Info.SubKind = SymbolSubKind::CXXMoveConstructor;
263       break;
264     }
265     case Decl::CXXDestructor:
266       Info.Kind = SymbolKind::Destructor;
267       Info.Lang = SymbolLanguage::CXX;
268       break;
269     case Decl::CXXConversion:
270       Info.Kind = SymbolKind::ConversionFunction;
271       Info.Lang = SymbolLanguage::CXX;
272       break;
273     case Decl::CXXMethod: {
274       const CXXMethodDecl *MD = cast<CXXMethodDecl>(D);
275       if (MD->isStatic())
276         Info.Kind = SymbolKind::StaticMethod;
277       else
278         Info.Kind = SymbolKind::InstanceMethod;
279       Info.Lang = SymbolLanguage::CXX;
280       break;
281     }
282     case Decl::ClassTemplate:
283       Info.Kind = SymbolKind::Class;
284       Info.Properties |= (SymbolPropertySet)SymbolProperty::Generic;
285       Info.Lang = SymbolLanguage::CXX;
286       break;
287     case Decl::FunctionTemplate:
288       Info.Kind = SymbolKind::Function;
289       Info.Properties |= (SymbolPropertySet)SymbolProperty::Generic;
290       Info.Lang = SymbolLanguage::CXX;
291       if (const CXXMethodDecl *MD = dyn_cast_or_null<CXXMethodDecl>(
292                            cast<FunctionTemplateDecl>(D)->getTemplatedDecl())) {
293         if (isa<CXXConstructorDecl>(MD))
294           Info.Kind = SymbolKind::Constructor;
295         else if (isa<CXXDestructorDecl>(MD))
296           Info.Kind = SymbolKind::Destructor;
297         else if (isa<CXXConversionDecl>(MD))
298           Info.Kind = SymbolKind::ConversionFunction;
299         else {
300           if (MD->isStatic())
301             Info.Kind = SymbolKind::StaticMethod;
302           else
303             Info.Kind = SymbolKind::InstanceMethod;
304         }
305       }
306       break;
307     case Decl::TypeAliasTemplate:
308       Info.Kind = SymbolKind::TypeAlias;
309       Info.Lang = SymbolLanguage::CXX;
310       Info.Properties |= (SymbolPropertySet)SymbolProperty::Generic;
311       break;
312     case Decl::TypeAlias:
313       Info.Kind = SymbolKind::TypeAlias;
314       Info.Lang = SymbolLanguage::CXX;
315       break;
316     case Decl::UnresolvedUsingTypename:
317       Info.Kind = SymbolKind::Using;
318       Info.SubKind = SymbolSubKind::UsingTypename;
319       Info.Lang = SymbolLanguage::CXX;
320       Info.Properties |= (SymbolPropertySet)SymbolProperty::Generic;
321       break;
322     case Decl::UnresolvedUsingValue:
323       Info.Kind = SymbolKind::Using;
324       Info.SubKind = SymbolSubKind::UsingValue;
325       Info.Lang = SymbolLanguage::CXX;
326       Info.Properties |= (SymbolPropertySet)SymbolProperty::Generic;
327       break;
328     case Decl::Using:
329       Info.Kind = SymbolKind::Using;
330       Info.Lang = SymbolLanguage::CXX;
331       break;
332     case Decl::UsingEnum:
333       Info.Kind = SymbolKind::Using;
334       Info.Lang = SymbolLanguage::CXX;
335       Info.SubKind = SymbolSubKind::UsingEnum;
336       break;
337     case Decl::Binding:
338       Info.Kind = SymbolKind::Variable;
339       Info.Lang = SymbolLanguage::CXX;
340       break;
341     case Decl::MSProperty:
342       Info.Kind = SymbolKind::InstanceProperty;
343       if (const CXXRecordDecl *CXXRec =
344               dyn_cast<CXXRecordDecl>(D->getDeclContext())) {
345         if (!CXXRec->isCLike())
346           Info.Lang = SymbolLanguage::CXX;
347       }
348       break;
349     case Decl::ClassTemplatePartialSpecialization:
350     case Decl::ClassScopeFunctionSpecialization:
351     case Decl::ClassTemplateSpecialization:
352     case Decl::CXXRecord:
353     case Decl::Enum:
354     case Decl::Record:
355       llvm_unreachable("records handled before");
356       break;
357     case Decl::VarTemplateSpecialization:
358     case Decl::VarTemplatePartialSpecialization:
359     case Decl::ImplicitParam:
360     case Decl::ParmVar:
361     case Decl::Var:
362     case Decl::VarTemplate:
363       llvm_unreachable("variables handled before");
364       break;
365     case Decl::TemplateTypeParm:
366       Info.Kind = SymbolKind::TemplateTypeParm;
367       break;
368     case Decl::TemplateTemplateParm:
369       Info.Kind = SymbolKind::TemplateTemplateParm;
370       break;
371     case Decl::NonTypeTemplateParm:
372       Info.Kind = SymbolKind::NonTypeTemplateParm;
373       break;
374     case Decl::Concept:
375       Info.Kind = SymbolKind::Concept;
376       break;
377     // Other decls get the 'unknown' kind.
378     default:
379       break;
380     }
381   }
382 
383   if (Info.Kind == SymbolKind::Unknown)
384     return Info;
385 
386   if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
387     if (FD->getTemplatedKind() ==
388           FunctionDecl::TK_FunctionTemplateSpecialization) {
389       Info.Properties |= (SymbolPropertySet)SymbolProperty::Generic;
390       Info.Properties |=
391           (SymbolPropertySet)SymbolProperty::TemplateSpecialization;
392     }
393   }
394 
395   if (Info.Properties & (SymbolPropertySet)SymbolProperty::Generic)
396     Info.Lang = SymbolLanguage::CXX;
397 
398   if (auto *attr = D->getExternalSourceSymbolAttr()) {
399     if (attr->getLanguage() == "Swift")
400       Info.Lang = SymbolLanguage::Swift;
401   }
402 
403   return Info;
404 }
405 
getSymbolInfoForMacro(const MacroInfo &)406 SymbolInfo index::getSymbolInfoForMacro(const MacroInfo &) {
407   SymbolInfo Info;
408   Info.Kind = SymbolKind::Macro;
409   Info.SubKind = SymbolSubKind::None;
410   Info.Properties = SymbolPropertySet();
411   Info.Lang = SymbolLanguage::C;
412   return Info;
413 }
414 
applyForEachSymbolRoleInterruptible(SymbolRoleSet Roles,llvm::function_ref<bool (SymbolRole)> Fn)415 bool index::applyForEachSymbolRoleInterruptible(SymbolRoleSet Roles,
416                                    llvm::function_ref<bool(SymbolRole)> Fn) {
417 #define APPLY_FOR_ROLE(Role) \
418   if (Roles & (unsigned)SymbolRole::Role) \
419     if (!Fn(SymbolRole::Role)) \
420       return false;
421 
422   APPLY_FOR_ROLE(Declaration);
423   APPLY_FOR_ROLE(Definition);
424   APPLY_FOR_ROLE(Reference);
425   APPLY_FOR_ROLE(Read);
426   APPLY_FOR_ROLE(Write);
427   APPLY_FOR_ROLE(Call);
428   APPLY_FOR_ROLE(Dynamic);
429   APPLY_FOR_ROLE(AddressOf);
430   APPLY_FOR_ROLE(Implicit);
431   APPLY_FOR_ROLE(Undefinition);
432   APPLY_FOR_ROLE(RelationChildOf);
433   APPLY_FOR_ROLE(RelationBaseOf);
434   APPLY_FOR_ROLE(RelationOverrideOf);
435   APPLY_FOR_ROLE(RelationReceivedBy);
436   APPLY_FOR_ROLE(RelationCalledBy);
437   APPLY_FOR_ROLE(RelationExtendedBy);
438   APPLY_FOR_ROLE(RelationAccessorOf);
439   APPLY_FOR_ROLE(RelationContainedBy);
440   APPLY_FOR_ROLE(RelationIBTypeOf);
441   APPLY_FOR_ROLE(RelationSpecializationOf);
442   APPLY_FOR_ROLE(NameReference);
443 
444 #undef APPLY_FOR_ROLE
445 
446   return true;
447 }
448 
applyForEachSymbolRole(SymbolRoleSet Roles,llvm::function_ref<void (SymbolRole)> Fn)449 void index::applyForEachSymbolRole(SymbolRoleSet Roles,
450                                    llvm::function_ref<void(SymbolRole)> Fn) {
451   applyForEachSymbolRoleInterruptible(Roles, [&](SymbolRole r) -> bool {
452     Fn(r);
453     return true;
454   });
455 }
456 
printSymbolRoles(SymbolRoleSet Roles,raw_ostream & OS)457 void index::printSymbolRoles(SymbolRoleSet Roles, raw_ostream &OS) {
458   bool VisitedOnce = false;
459   applyForEachSymbolRole(Roles, [&](SymbolRole Role) {
460     if (VisitedOnce)
461       OS << ',';
462     else
463       VisitedOnce = true;
464     switch (Role) {
465     case SymbolRole::Declaration: OS << "Decl"; break;
466     case SymbolRole::Definition: OS << "Def"; break;
467     case SymbolRole::Reference: OS << "Ref"; break;
468     case SymbolRole::Read: OS << "Read"; break;
469     case SymbolRole::Write: OS << "Writ"; break;
470     case SymbolRole::Call: OS << "Call"; break;
471     case SymbolRole::Dynamic: OS << "Dyn"; break;
472     case SymbolRole::AddressOf: OS << "Addr"; break;
473     case SymbolRole::Implicit: OS << "Impl"; break;
474     case SymbolRole::Undefinition: OS << "Undef"; break;
475     case SymbolRole::RelationChildOf: OS << "RelChild"; break;
476     case SymbolRole::RelationBaseOf: OS << "RelBase"; break;
477     case SymbolRole::RelationOverrideOf: OS << "RelOver"; break;
478     case SymbolRole::RelationReceivedBy: OS << "RelRec"; break;
479     case SymbolRole::RelationCalledBy: OS << "RelCall"; break;
480     case SymbolRole::RelationExtendedBy: OS << "RelExt"; break;
481     case SymbolRole::RelationAccessorOf: OS << "RelAcc"; break;
482     case SymbolRole::RelationContainedBy: OS << "RelCont"; break;
483     case SymbolRole::RelationIBTypeOf: OS << "RelIBType"; break;
484     case SymbolRole::RelationSpecializationOf: OS << "RelSpecialization"; break;
485     case SymbolRole::NameReference: OS << "NameReference"; break;
486     }
487   });
488 }
489 
printSymbolName(const Decl * D,const LangOptions & LO,raw_ostream & OS)490 bool index::printSymbolName(const Decl *D, const LangOptions &LO,
491                             raw_ostream &OS) {
492   if (auto *ND = dyn_cast<NamedDecl>(D)) {
493     PrintingPolicy Policy(LO);
494     // Forward references can have different template argument names. Suppress
495     // the template argument names in constructors to make their name more
496     // stable.
497     Policy.SuppressTemplateArgsInCXXConstructors = true;
498     DeclarationName DeclName = ND->getDeclName();
499     if (DeclName.isEmpty())
500       return true;
501     DeclName.print(OS, Policy);
502     return false;
503   } else {
504     return true;
505   }
506 }
507 
getSymbolKindString(SymbolKind K)508 StringRef index::getSymbolKindString(SymbolKind K) {
509   switch (K) {
510   case SymbolKind::Unknown: return "<unknown>";
511   case SymbolKind::Module: return "module";
512   case SymbolKind::Namespace: return "namespace";
513   case SymbolKind::NamespaceAlias: return "namespace-alias";
514   case SymbolKind::Macro: return "macro";
515   case SymbolKind::Enum: return "enum";
516   case SymbolKind::Struct: return "struct";
517   case SymbolKind::Class: return "class";
518   case SymbolKind::Protocol: return "protocol";
519   case SymbolKind::Extension: return "extension";
520   case SymbolKind::Union: return "union";
521   case SymbolKind::TypeAlias: return "type-alias";
522   case SymbolKind::Function: return "function";
523   case SymbolKind::Variable: return "variable";
524   case SymbolKind::Field: return "field";
525   case SymbolKind::EnumConstant: return "enumerator";
526   case SymbolKind::InstanceMethod: return "instance-method";
527   case SymbolKind::ClassMethod: return "class-method";
528   case SymbolKind::StaticMethod: return "static-method";
529   case SymbolKind::InstanceProperty: return "instance-property";
530   case SymbolKind::ClassProperty: return "class-property";
531   case SymbolKind::StaticProperty: return "static-property";
532   case SymbolKind::Constructor: return "constructor";
533   case SymbolKind::Destructor: return "destructor";
534   case SymbolKind::ConversionFunction: return "conversion-func";
535   case SymbolKind::Parameter: return "param";
536   case SymbolKind::Using: return "using";
537   case SymbolKind::TemplateTypeParm: return "template-type-param";
538   case SymbolKind::TemplateTemplateParm: return "template-template-param";
539   case SymbolKind::NonTypeTemplateParm: return "non-type-template-param";
540   case SymbolKind::Concept:
541     return "concept";
542   }
543   llvm_unreachable("invalid symbol kind");
544 }
545 
getSymbolSubKindString(SymbolSubKind K)546 StringRef index::getSymbolSubKindString(SymbolSubKind K) {
547   switch (K) {
548   case SymbolSubKind::None: return "<none>";
549   case SymbolSubKind::CXXCopyConstructor: return "cxx-copy-ctor";
550   case SymbolSubKind::CXXMoveConstructor: return "cxx-move-ctor";
551   case SymbolSubKind::AccessorGetter: return "acc-get";
552   case SymbolSubKind::AccessorSetter: return "acc-set";
553   case SymbolSubKind::UsingTypename: return "using-typename";
554   case SymbolSubKind::UsingValue: return "using-value";
555   case SymbolSubKind::UsingEnum:
556     return "using-enum";
557   }
558   llvm_unreachable("invalid symbol subkind");
559 }
560 
getSymbolLanguageString(SymbolLanguage K)561 StringRef index::getSymbolLanguageString(SymbolLanguage K) {
562   switch (K) {
563   case SymbolLanguage::C: return "C";
564   case SymbolLanguage::ObjC: return "ObjC";
565   case SymbolLanguage::CXX: return "C++";
566   case SymbolLanguage::Swift: return "Swift";
567   }
568   llvm_unreachable("invalid symbol language kind");
569 }
570 
applyForEachSymbolProperty(SymbolPropertySet Props,llvm::function_ref<void (SymbolProperty)> Fn)571 void index::applyForEachSymbolProperty(SymbolPropertySet Props,
572                                   llvm::function_ref<void(SymbolProperty)> Fn) {
573 #define APPLY_FOR_PROPERTY(K)                                                  \
574   if (Props & (SymbolPropertySet)SymbolProperty::K)                            \
575   Fn(SymbolProperty::K)
576 
577   APPLY_FOR_PROPERTY(Generic);
578   APPLY_FOR_PROPERTY(TemplatePartialSpecialization);
579   APPLY_FOR_PROPERTY(TemplateSpecialization);
580   APPLY_FOR_PROPERTY(UnitTest);
581   APPLY_FOR_PROPERTY(IBAnnotated);
582   APPLY_FOR_PROPERTY(IBOutletCollection);
583   APPLY_FOR_PROPERTY(GKInspectable);
584   APPLY_FOR_PROPERTY(Local);
585   APPLY_FOR_PROPERTY(ProtocolInterface);
586 
587 #undef APPLY_FOR_PROPERTY
588 }
589 
printSymbolProperties(SymbolPropertySet Props,raw_ostream & OS)590 void index::printSymbolProperties(SymbolPropertySet Props, raw_ostream &OS) {
591   bool VisitedOnce = false;
592   applyForEachSymbolProperty(Props, [&](SymbolProperty Prop) {
593     if (VisitedOnce)
594       OS << ',';
595     else
596       VisitedOnce = true;
597     switch (Prop) {
598     case SymbolProperty::Generic: OS << "Gen"; break;
599     case SymbolProperty::TemplatePartialSpecialization: OS << "TPS"; break;
600     case SymbolProperty::TemplateSpecialization: OS << "TS"; break;
601     case SymbolProperty::UnitTest: OS << "test"; break;
602     case SymbolProperty::IBAnnotated: OS << "IB"; break;
603     case SymbolProperty::IBOutletCollection: OS << "IBColl"; break;
604     case SymbolProperty::GKInspectable: OS << "GKI"; break;
605     case SymbolProperty::Local: OS << "local"; break;
606     case SymbolProperty::ProtocolInterface: OS << "protocol"; break;
607     }
608   });
609 }
610