IndexSymbol.cpp 21 KB

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