IndexSymbol.cpp 20 KB

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