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