blob: d7df0976b078421a44920b873cfff43d11a7fa27 [file] [log] [blame]
Argyrios Kyrtzidisf4fb85b2016-02-12 23:10:59 +00001//===--- IndexSymbol.cpp - Types and functions for indexing symbols -------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
10#include "clang/Index/IndexSymbol.h"
11#include "clang/AST/DeclCXX.h"
12#include "clang/AST/DeclObjC.h"
13#include "clang/AST/DeclTemplate.h"
Argyrios Kyrtzidisd5719082016-02-15 01:32:36 +000014#include "clang/AST/PrettyPrinter.h"
Argyrios Kyrtzidisf4fb85b2016-02-12 23:10:59 +000015
16using namespace clang;
17using namespace clang::index;
18
Argyrios Kyrtzidisf30c8c62016-04-22 07:21:10 +000019/// \returns true if \c D is a subclass of 'XCTestCase'.
20static bool isUnitTestCase(const ObjCInterfaceDecl *D) {
21 if (!D)
22 return false;
23 while (const ObjCInterfaceDecl *SuperD = D->getSuperClass()) {
24 if (SuperD->getName() == "XCTestCase")
25 return true;
26 D = SuperD;
27 }
28 return false;
29}
30
31/// \returns true if \c D is in a subclass of 'XCTestCase', returns void, has
32/// no parameters, and its name starts with 'test'.
33static bool isUnitTest(const ObjCMethodDecl *D) {
34 if (!D->parameters().empty())
35 return false;
36 if (!D->getReturnType()->isVoidType())
37 return false;
38 if (!D->getSelector().getNameForSlot(0).startswith("test"))
39 return false;
40 return isUnitTestCase(D->getClassInterface());
41}
42
Argyrios Kyrtzidisdb469832016-11-11 23:49:55 +000043static void checkForIBOutlets(const Decl *D, SymbolPropertySet &PropSet) {
Argyrios Kyrtzidis0642a212016-04-22 07:21:16 +000044 if (D->hasAttr<IBOutletAttr>()) {
Argyrios Kyrtzidisdb469832016-11-11 23:49:55 +000045 PropSet |= (unsigned)SymbolProperty::IBAnnotated;
Argyrios Kyrtzidis0642a212016-04-22 07:21:16 +000046 } else if (D->hasAttr<IBOutletCollectionAttr>()) {
Argyrios Kyrtzidisdb469832016-11-11 23:49:55 +000047 PropSet |= (unsigned)SymbolProperty::IBAnnotated;
48 PropSet |= (unsigned)SymbolProperty::IBOutletCollection;
Argyrios Kyrtzidis0642a212016-04-22 07:21:16 +000049 }
50}
51
Argyrios Kyrtzidisf4fb85b2016-02-12 23:10:59 +000052SymbolInfo index::getSymbolInfo(const Decl *D) {
53 assert(D);
54 SymbolInfo Info;
55 Info.Kind = SymbolKind::Unknown;
Argyrios Kyrtzidise24f5e22017-01-08 23:21:35 +000056 Info.SubKind = SymbolSubKind::None;
Argyrios Kyrtzidisdb469832016-11-11 23:49:55 +000057 Info.Properties = SymbolPropertySet();
Argyrios Kyrtzidisf4fb85b2016-02-12 23:10:59 +000058 Info.Lang = SymbolLanguage::C;
59
60 if (const TagDecl *TD = dyn_cast<TagDecl>(D)) {
61 switch (TD->getTagKind()) {
62 case TTK_Struct:
63 Info.Kind = SymbolKind::Struct; break;
64 case TTK_Union:
65 Info.Kind = SymbolKind::Union; break;
66 case TTK_Class:
Ben Langmuir443913f2016-03-25 17:01:59 +000067 Info.Kind = SymbolKind::Class;
Argyrios Kyrtzidisf4fb85b2016-02-12 23:10:59 +000068 Info.Lang = SymbolLanguage::CXX;
69 break;
70 case TTK_Interface:
Ben Langmuir443913f2016-03-25 17:01:59 +000071 Info.Kind = SymbolKind::Protocol;
Argyrios Kyrtzidisf4fb85b2016-02-12 23:10:59 +000072 Info.Lang = SymbolLanguage::CXX;
73 break;
74 case TTK_Enum:
75 Info.Kind = SymbolKind::Enum; break;
76 }
77
Argyrios Kyrtzidis3e5b61f2016-11-07 21:20:08 +000078 if (const CXXRecordDecl *CXXRec = dyn_cast<CXXRecordDecl>(D)) {
79 if (!CXXRec->isCLike()) {
Argyrios Kyrtzidisf4fb85b2016-02-12 23:10:59 +000080 Info.Lang = SymbolLanguage::CXX;
Argyrios Kyrtzidis3e5b61f2016-11-07 21:20:08 +000081 if (CXXRec->getDescribedClassTemplate()) {
Argyrios Kyrtzidisdb469832016-11-11 23:49:55 +000082 Info.Properties |= (unsigned)SymbolProperty::Generic;
Argyrios Kyrtzidis3e5b61f2016-11-07 21:20:08 +000083 }
84 }
85 }
Argyrios Kyrtzidisf4fb85b2016-02-12 23:10:59 +000086
87 if (isa<ClassTemplatePartialSpecializationDecl>(D)) {
Argyrios Kyrtzidisdb469832016-11-11 23:49:55 +000088 Info.Properties |= (unsigned)SymbolProperty::Generic;
89 Info.Properties |= (unsigned)SymbolProperty::TemplatePartialSpecialization;
Argyrios Kyrtzidisf4fb85b2016-02-12 23:10:59 +000090 } else if (isa<ClassTemplateSpecializationDecl>(D)) {
Argyrios Kyrtzidisdb469832016-11-11 23:49:55 +000091 Info.Properties |= (unsigned)SymbolProperty::Generic;
92 Info.Properties |= (unsigned)SymbolProperty::TemplateSpecialization;
Argyrios Kyrtzidisf4fb85b2016-02-12 23:10:59 +000093 }
94
Argyrios Kyrtzidis9d8ab722016-11-07 21:20:15 +000095 } else if (auto *VD = dyn_cast<VarDecl>(D)) {
96 Info.Kind = SymbolKind::Variable;
97 if (isa<CXXRecordDecl>(D->getDeclContext())) {
98 Info.Kind = SymbolKind::StaticProperty;
99 Info.Lang = SymbolLanguage::CXX;
100 }
101 if (isa<VarTemplatePartialSpecializationDecl>(D)) {
102 Info.Lang = SymbolLanguage::CXX;
Argyrios Kyrtzidisdb469832016-11-11 23:49:55 +0000103 Info.Properties |= (unsigned)SymbolProperty::Generic;
104 Info.Properties |= (unsigned)SymbolProperty::TemplatePartialSpecialization;
Argyrios Kyrtzidis9d8ab722016-11-07 21:20:15 +0000105 } else if (isa<VarTemplateSpecializationDecl>(D)) {
106 Info.Lang = SymbolLanguage::CXX;
Argyrios Kyrtzidisdb469832016-11-11 23:49:55 +0000107 Info.Properties |= (unsigned)SymbolProperty::Generic;
108 Info.Properties |= (unsigned)SymbolProperty::TemplateSpecialization;
Argyrios Kyrtzidis9d8ab722016-11-07 21:20:15 +0000109 } else if (VD->getDescribedVarTemplate()) {
110 Info.Lang = SymbolLanguage::CXX;
Argyrios Kyrtzidisdb469832016-11-11 23:49:55 +0000111 Info.Properties |= (unsigned)SymbolProperty::Generic;
Argyrios Kyrtzidis9d8ab722016-11-07 21:20:15 +0000112 }
113
Argyrios Kyrtzidisf4fb85b2016-02-12 23:10:59 +0000114 } else {
115 switch (D->getKind()) {
Argyrios Kyrtzidis113387e2016-02-29 07:56:07 +0000116 case Decl::Import:
117 Info.Kind = SymbolKind::Module;
118 break;
Argyrios Kyrtzidisf4fb85b2016-02-12 23:10:59 +0000119 case Decl::Typedef:
Ben Langmuir443913f2016-03-25 17:01:59 +0000120 Info.Kind = SymbolKind::TypeAlias; break; // Lang = C
Argyrios Kyrtzidisf4fb85b2016-02-12 23:10:59 +0000121 case Decl::Function:
122 Info.Kind = SymbolKind::Function;
123 break;
Argyrios Kyrtzidisf4fb85b2016-02-12 23:10:59 +0000124 case Decl::Field:
125 Info.Kind = SymbolKind::Field;
126 if (const CXXRecordDecl *
127 CXXRec = dyn_cast<CXXRecordDecl>(D->getDeclContext())) {
128 if (!CXXRec->isCLike())
129 Info.Lang = SymbolLanguage::CXX;
130 }
131 break;
132 case Decl::EnumConstant:
133 Info.Kind = SymbolKind::EnumConstant; break;
134 case Decl::ObjCInterface:
Argyrios Kyrtzidisf30c8c62016-04-22 07:21:10 +0000135 case Decl::ObjCImplementation: {
Ben Langmuir443913f2016-03-25 17:01:59 +0000136 Info.Kind = SymbolKind::Class;
Argyrios Kyrtzidisf4fb85b2016-02-12 23:10:59 +0000137 Info.Lang = SymbolLanguage::ObjC;
Argyrios Kyrtzidisf30c8c62016-04-22 07:21:10 +0000138 const ObjCInterfaceDecl *ClsD = dyn_cast<ObjCInterfaceDecl>(D);
139 if (!ClsD)
140 ClsD = cast<ObjCImplementationDecl>(D)->getClassInterface();
141 if (isUnitTestCase(ClsD))
Argyrios Kyrtzidisdb469832016-11-11 23:49:55 +0000142 Info.Properties |= (unsigned)SymbolProperty::UnitTest;
Argyrios Kyrtzidisf4fb85b2016-02-12 23:10:59 +0000143 break;
Argyrios Kyrtzidisf30c8c62016-04-22 07:21:10 +0000144 }
Argyrios Kyrtzidisf4fb85b2016-02-12 23:10:59 +0000145 case Decl::ObjCProtocol:
Ben Langmuir443913f2016-03-25 17:01:59 +0000146 Info.Kind = SymbolKind::Protocol;
Argyrios Kyrtzidisf4fb85b2016-02-12 23:10:59 +0000147 Info.Lang = SymbolLanguage::ObjC;
148 break;
149 case Decl::ObjCCategory:
150 case Decl::ObjCCategoryImpl:
Ben Langmuir443913f2016-03-25 17:01:59 +0000151 Info.Kind = SymbolKind::Extension;
Argyrios Kyrtzidisf4fb85b2016-02-12 23:10:59 +0000152 Info.Lang = SymbolLanguage::ObjC;
153 break;
154 case Decl::ObjCMethod:
155 if (cast<ObjCMethodDecl>(D)->isInstanceMethod())
Ben Langmuir443913f2016-03-25 17:01:59 +0000156 Info.Kind = SymbolKind::InstanceMethod;
Argyrios Kyrtzidisf4fb85b2016-02-12 23:10:59 +0000157 else
Ben Langmuir443913f2016-03-25 17:01:59 +0000158 Info.Kind = SymbolKind::ClassMethod;
Argyrios Kyrtzidisf4fb85b2016-02-12 23:10:59 +0000159 Info.Lang = SymbolLanguage::ObjC;
Argyrios Kyrtzidisf30c8c62016-04-22 07:21:10 +0000160 if (isUnitTest(cast<ObjCMethodDecl>(D)))
Argyrios Kyrtzidisdb469832016-11-11 23:49:55 +0000161 Info.Properties |= (unsigned)SymbolProperty::UnitTest;
Argyrios Kyrtzidis0642a212016-04-22 07:21:16 +0000162 if (D->hasAttr<IBActionAttr>())
Argyrios Kyrtzidisdb469832016-11-11 23:49:55 +0000163 Info.Properties |= (unsigned)SymbolProperty::IBAnnotated;
Argyrios Kyrtzidisf4fb85b2016-02-12 23:10:59 +0000164 break;
165 case Decl::ObjCProperty:
Ben Langmuir443913f2016-03-25 17:01:59 +0000166 Info.Kind = SymbolKind::InstanceProperty;
Argyrios Kyrtzidisf4fb85b2016-02-12 23:10:59 +0000167 Info.Lang = SymbolLanguage::ObjC;
Argyrios Kyrtzidisdb469832016-11-11 23:49:55 +0000168 checkForIBOutlets(D, Info.Properties);
Argyrios Kyrtzidisf42032a2016-11-10 23:27:11 +0000169 if (auto *Annot = D->getAttr<AnnotateAttr>()) {
170 if (Annot->getAnnotation() == "gk_inspectable")
Argyrios Kyrtzidisdb469832016-11-11 23:49:55 +0000171 Info.Properties |= (unsigned)SymbolProperty::GKInspectable;
Argyrios Kyrtzidisf42032a2016-11-10 23:27:11 +0000172 }
Argyrios Kyrtzidisf4fb85b2016-02-12 23:10:59 +0000173 break;
174 case Decl::ObjCIvar:
Ben Langmuir443913f2016-03-25 17:01:59 +0000175 Info.Kind = SymbolKind::Field;
Argyrios Kyrtzidisf4fb85b2016-02-12 23:10:59 +0000176 Info.Lang = SymbolLanguage::ObjC;
Argyrios Kyrtzidisdb469832016-11-11 23:49:55 +0000177 checkForIBOutlets(D, Info.Properties);
Argyrios Kyrtzidisf4fb85b2016-02-12 23:10:59 +0000178 break;
179 case Decl::Namespace:
Ben Langmuir443913f2016-03-25 17:01:59 +0000180 Info.Kind = SymbolKind::Namespace;
Argyrios Kyrtzidisf4fb85b2016-02-12 23:10:59 +0000181 Info.Lang = SymbolLanguage::CXX;
182 break;
183 case Decl::NamespaceAlias:
Ben Langmuir443913f2016-03-25 17:01:59 +0000184 Info.Kind = SymbolKind::NamespaceAlias;
Argyrios Kyrtzidisf4fb85b2016-02-12 23:10:59 +0000185 Info.Lang = SymbolLanguage::CXX;
186 break;
Argyrios Kyrtzidise24f5e22017-01-08 23:21:35 +0000187 case Decl::CXXConstructor: {
Ben Langmuir443913f2016-03-25 17:01:59 +0000188 Info.Kind = SymbolKind::Constructor;
Argyrios Kyrtzidisf4fb85b2016-02-12 23:10:59 +0000189 Info.Lang = SymbolLanguage::CXX;
Argyrios Kyrtzidise24f5e22017-01-08 23:21:35 +0000190 auto *CD = cast<CXXConstructorDecl>(D);
191 if (CD->isCopyConstructor())
192 Info.SubKind = SymbolSubKind::CXXCopyConstructor;
193 else if (CD->isMoveConstructor())
194 Info.SubKind = SymbolSubKind::CXXMoveConstructor;
Argyrios Kyrtzidisf4fb85b2016-02-12 23:10:59 +0000195 break;
Argyrios Kyrtzidise24f5e22017-01-08 23:21:35 +0000196 }
Argyrios Kyrtzidisf4fb85b2016-02-12 23:10:59 +0000197 case Decl::CXXDestructor:
Ben Langmuir443913f2016-03-25 17:01:59 +0000198 Info.Kind = SymbolKind::Destructor;
Argyrios Kyrtzidisf4fb85b2016-02-12 23:10:59 +0000199 Info.Lang = SymbolLanguage::CXX;
200 break;
201 case Decl::CXXConversion:
Ben Langmuir443913f2016-03-25 17:01:59 +0000202 Info.Kind = SymbolKind::ConversionFunction;
Argyrios Kyrtzidisf4fb85b2016-02-12 23:10:59 +0000203 Info.Lang = SymbolLanguage::CXX;
204 break;
205 case Decl::CXXMethod: {
206 const CXXMethodDecl *MD = cast<CXXMethodDecl>(D);
207 if (MD->isStatic())
Ben Langmuir443913f2016-03-25 17:01:59 +0000208 Info.Kind = SymbolKind::StaticMethod;
Argyrios Kyrtzidisf4fb85b2016-02-12 23:10:59 +0000209 else
Ben Langmuir443913f2016-03-25 17:01:59 +0000210 Info.Kind = SymbolKind::InstanceMethod;
Argyrios Kyrtzidisf4fb85b2016-02-12 23:10:59 +0000211 Info.Lang = SymbolLanguage::CXX;
212 break;
213 }
214 case Decl::ClassTemplate:
Ben Langmuir443913f2016-03-25 17:01:59 +0000215 Info.Kind = SymbolKind::Class;
Argyrios Kyrtzidisdb469832016-11-11 23:49:55 +0000216 Info.Properties |= (unsigned)SymbolProperty::Generic;
Ben Langmuir443913f2016-03-25 17:01:59 +0000217 Info.Lang = SymbolLanguage::CXX;
Argyrios Kyrtzidisf4fb85b2016-02-12 23:10:59 +0000218 break;
219 case Decl::FunctionTemplate:
220 Info.Kind = SymbolKind::Function;
Argyrios Kyrtzidisdb469832016-11-11 23:49:55 +0000221 Info.Properties |= (unsigned)SymbolProperty::Generic;
Ben Langmuir443913f2016-03-25 17:01:59 +0000222 Info.Lang = SymbolLanguage::CXX;
Argyrios Kyrtzidisf4fb85b2016-02-12 23:10:59 +0000223 if (const CXXMethodDecl *MD = dyn_cast_or_null<CXXMethodDecl>(
224 cast<FunctionTemplateDecl>(D)->getTemplatedDecl())) {
225 if (isa<CXXConstructorDecl>(MD))
Ben Langmuir443913f2016-03-25 17:01:59 +0000226 Info.Kind = SymbolKind::Constructor;
Argyrios Kyrtzidisf4fb85b2016-02-12 23:10:59 +0000227 else if (isa<CXXDestructorDecl>(MD))
Ben Langmuir443913f2016-03-25 17:01:59 +0000228 Info.Kind = SymbolKind::Destructor;
Argyrios Kyrtzidisf4fb85b2016-02-12 23:10:59 +0000229 else if (isa<CXXConversionDecl>(MD))
Ben Langmuir443913f2016-03-25 17:01:59 +0000230 Info.Kind = SymbolKind::ConversionFunction;
Argyrios Kyrtzidisf4fb85b2016-02-12 23:10:59 +0000231 else {
232 if (MD->isStatic())
Ben Langmuir443913f2016-03-25 17:01:59 +0000233 Info.Kind = SymbolKind::StaticMethod;
Argyrios Kyrtzidisf4fb85b2016-02-12 23:10:59 +0000234 else
Ben Langmuir443913f2016-03-25 17:01:59 +0000235 Info.Kind = SymbolKind::InstanceMethod;
Argyrios Kyrtzidisf4fb85b2016-02-12 23:10:59 +0000236 }
237 }
238 break;
239 case Decl::TypeAliasTemplate:
Ben Langmuir443913f2016-03-25 17:01:59 +0000240 Info.Kind = SymbolKind::TypeAlias;
241 Info.Lang = SymbolLanguage::CXX;
Argyrios Kyrtzidisdb469832016-11-11 23:49:55 +0000242 Info.Properties |= (unsigned)SymbolProperty::Generic;
Argyrios Kyrtzidisf4fb85b2016-02-12 23:10:59 +0000243 break;
244 case Decl::TypeAlias:
Ben Langmuir443913f2016-03-25 17:01:59 +0000245 Info.Kind = SymbolKind::TypeAlias;
Argyrios Kyrtzidisf4fb85b2016-02-12 23:10:59 +0000246 Info.Lang = SymbolLanguage::CXX;
247 break;
248 default:
249 break;
250 }
251 }
252
253 if (Info.Kind == SymbolKind::Unknown)
254 return Info;
255
256 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
257 if (FD->getTemplatedKind() ==
Argyrios Kyrtzidisf2142cb2016-04-22 07:21:04 +0000258 FunctionDecl::TK_FunctionTemplateSpecialization) {
Argyrios Kyrtzidisdb469832016-11-11 23:49:55 +0000259 Info.Properties |= (unsigned)SymbolProperty::Generic;
260 Info.Properties |= (unsigned)SymbolProperty::TemplateSpecialization;
Argyrios Kyrtzidisf2142cb2016-04-22 07:21:04 +0000261 }
Argyrios Kyrtzidisf4fb85b2016-02-12 23:10:59 +0000262 }
263
Argyrios Kyrtzidisdb469832016-11-11 23:49:55 +0000264 if (Info.Properties & (unsigned)SymbolProperty::Generic)
Argyrios Kyrtzidisf4fb85b2016-02-12 23:10:59 +0000265 Info.Lang = SymbolLanguage::CXX;
266
267 return Info;
268}
Argyrios Kyrtzidis6fdcb9c2016-02-14 06:39:11 +0000269
270void index::applyForEachSymbolRole(SymbolRoleSet Roles,
271 llvm::function_ref<void(SymbolRole)> Fn) {
272#define APPLY_FOR_ROLE(Role) \
273 if (Roles & (unsigned)SymbolRole::Role) \
274 Fn(SymbolRole::Role)
275
276 APPLY_FOR_ROLE(Declaration);
277 APPLY_FOR_ROLE(Definition);
278 APPLY_FOR_ROLE(Reference);
279 APPLY_FOR_ROLE(Read);
280 APPLY_FOR_ROLE(Write);
281 APPLY_FOR_ROLE(Call);
282 APPLY_FOR_ROLE(Dynamic);
283 APPLY_FOR_ROLE(AddressOf);
284 APPLY_FOR_ROLE(Implicit);
285 APPLY_FOR_ROLE(RelationChildOf);
286 APPLY_FOR_ROLE(RelationBaseOf);
287 APPLY_FOR_ROLE(RelationOverrideOf);
288 APPLY_FOR_ROLE(RelationReceivedBy);
Argyrios Kyrtzidisa8b51c12016-02-29 07:56:00 +0000289 APPLY_FOR_ROLE(RelationCalledBy);
Argyrios Kyrtzidis806faaf2016-10-25 21:11:22 +0000290 APPLY_FOR_ROLE(RelationExtendedBy);
291 APPLY_FOR_ROLE(RelationAccessorOf);
Argyrios Kyrtzidisdf60aa82017-01-11 20:51:10 +0000292 APPLY_FOR_ROLE(RelationContainedBy);
Argyrios Kyrtzidis6fdcb9c2016-02-14 06:39:11 +0000293
294#undef APPLY_FOR_ROLE
295}
296
297void index::printSymbolRoles(SymbolRoleSet Roles, raw_ostream &OS) {
298 bool VisitedOnce = false;
299 applyForEachSymbolRole(Roles, [&](SymbolRole Role) {
300 if (VisitedOnce)
Argyrios Kyrtzidisd97ec182016-02-29 07:55:51 +0000301 OS << ',';
Argyrios Kyrtzidis6fdcb9c2016-02-14 06:39:11 +0000302 else
303 VisitedOnce = true;
304 switch (Role) {
305 case SymbolRole::Declaration: OS << "Decl"; break;
306 case SymbolRole::Definition: OS << "Def"; break;
307 case SymbolRole::Reference: OS << "Ref"; break;
308 case SymbolRole::Read: OS << "Read"; break;
309 case SymbolRole::Write: OS << "Writ"; break;
310 case SymbolRole::Call: OS << "Call"; break;
311 case SymbolRole::Dynamic: OS << "Dyn"; break;
312 case SymbolRole::AddressOf: OS << "Addr"; break;
313 case SymbolRole::Implicit: OS << "Impl"; break;
314 case SymbolRole::RelationChildOf: OS << "RelChild"; break;
315 case SymbolRole::RelationBaseOf: OS << "RelBase"; break;
316 case SymbolRole::RelationOverrideOf: OS << "RelOver"; break;
317 case SymbolRole::RelationReceivedBy: OS << "RelRec"; break;
Argyrios Kyrtzidisa8b51c12016-02-29 07:56:00 +0000318 case SymbolRole::RelationCalledBy: OS << "RelCall"; break;
Argyrios Kyrtzidis806faaf2016-10-25 21:11:22 +0000319 case SymbolRole::RelationExtendedBy: OS << "RelExt"; break;
320 case SymbolRole::RelationAccessorOf: OS << "RelAcc"; break;
Argyrios Kyrtzidisdf60aa82017-01-11 20:51:10 +0000321 case SymbolRole::RelationContainedBy: OS << "RelCont"; break;
Argyrios Kyrtzidis6fdcb9c2016-02-14 06:39:11 +0000322 }
323 });
324}
325
Argyrios Kyrtzidisd5719082016-02-15 01:32:36 +0000326bool index::printSymbolName(const Decl *D, const LangOptions &LO,
327 raw_ostream &OS) {
328 if (auto *ND = dyn_cast<NamedDecl>(D)) {
329 PrintingPolicy Policy(LO);
330 // Forward references can have different template argument names. Suppress
331 // the template argument names in constructors to make their name more
332 // stable.
333 Policy.SuppressTemplateArgsInCXXConstructors = true;
334 DeclarationName DeclName = ND->getDeclName();
335 if (DeclName.isEmpty())
336 return true;
337 DeclName.print(OS, Policy);
338 return false;
339 } else {
340 return true;
341 }
342}
343
Argyrios Kyrtzidis6fdcb9c2016-02-14 06:39:11 +0000344StringRef index::getSymbolKindString(SymbolKind K) {
345 switch (K) {
346 case SymbolKind::Unknown: return "<unknown>";
347 case SymbolKind::Module: return "module";
Ben Langmuir443913f2016-03-25 17:01:59 +0000348 case SymbolKind::Namespace: return "namespace";
349 case SymbolKind::NamespaceAlias: return "namespace-alias";
Argyrios Kyrtzidis6fdcb9c2016-02-14 06:39:11 +0000350 case SymbolKind::Macro: return "macro";
351 case SymbolKind::Enum: return "enum";
352 case SymbolKind::Struct: return "struct";
Ben Langmuir443913f2016-03-25 17:01:59 +0000353 case SymbolKind::Class: return "class";
354 case SymbolKind::Protocol: return "protocol";
355 case SymbolKind::Extension: return "extension";
Argyrios Kyrtzidis6fdcb9c2016-02-14 06:39:11 +0000356 case SymbolKind::Union: return "union";
Ben Langmuir443913f2016-03-25 17:01:59 +0000357 case SymbolKind::TypeAlias: return "type-alias";
Argyrios Kyrtzidis6fdcb9c2016-02-14 06:39:11 +0000358 case SymbolKind::Function: return "function";
359 case SymbolKind::Variable: return "variable";
360 case SymbolKind::Field: return "field";
361 case SymbolKind::EnumConstant: return "enumerator";
Ben Langmuir443913f2016-03-25 17:01:59 +0000362 case SymbolKind::InstanceMethod: return "instance-method";
363 case SymbolKind::ClassMethod: return "class-method";
364 case SymbolKind::StaticMethod: return "static-method";
365 case SymbolKind::InstanceProperty: return "instance-property";
366 case SymbolKind::ClassProperty: return "class-property";
367 case SymbolKind::StaticProperty: return "static-property";
368 case SymbolKind::Constructor: return "constructor";
369 case SymbolKind::Destructor: return "destructor";
370 case SymbolKind::ConversionFunction: return "coversion-func";
Argyrios Kyrtzidis6fdcb9c2016-02-14 06:39:11 +0000371 }
Saleem Abdulrasool9b0ac332016-02-15 00:36:52 +0000372 llvm_unreachable("invalid symbol kind");
Argyrios Kyrtzidis6fdcb9c2016-02-14 06:39:11 +0000373}
374
Argyrios Kyrtzidise24f5e22017-01-08 23:21:35 +0000375StringRef index::getSymbolSubKindString(SymbolSubKind K) {
376 switch (K) {
377 case SymbolSubKind::None: return "<none>";
378 case SymbolSubKind::CXXCopyConstructor: return "cxx-copy-ctor";
379 case SymbolSubKind::CXXMoveConstructor: return "cxx-move-ctor";
380 }
381 llvm_unreachable("invalid symbol subkind");
382}
383
Argyrios Kyrtzidis6fdcb9c2016-02-14 06:39:11 +0000384StringRef index::getSymbolLanguageString(SymbolLanguage K) {
385 switch (K) {
386 case SymbolLanguage::C: return "C";
387 case SymbolLanguage::ObjC: return "ObjC";
388 case SymbolLanguage::CXX: return "C++";
389 }
Saleem Abdulrasool9b0ac332016-02-15 00:36:52 +0000390 llvm_unreachable("invalid symbol language kind");
Argyrios Kyrtzidis6fdcb9c2016-02-14 06:39:11 +0000391}
Argyrios Kyrtzidisf2142cb2016-04-22 07:21:04 +0000392
Argyrios Kyrtzidisdb469832016-11-11 23:49:55 +0000393void index::applyForEachSymbolProperty(SymbolPropertySet Props,
394 llvm::function_ref<void(SymbolProperty)> Fn) {
395#define APPLY_FOR_PROPERTY(K) \
396 if (Props & (unsigned)SymbolProperty::K) \
397 Fn(SymbolProperty::K)
Argyrios Kyrtzidisf2142cb2016-04-22 07:21:04 +0000398
Argyrios Kyrtzidisdb469832016-11-11 23:49:55 +0000399 APPLY_FOR_PROPERTY(Generic);
400 APPLY_FOR_PROPERTY(TemplatePartialSpecialization);
401 APPLY_FOR_PROPERTY(TemplateSpecialization);
402 APPLY_FOR_PROPERTY(UnitTest);
403 APPLY_FOR_PROPERTY(IBAnnotated);
404 APPLY_FOR_PROPERTY(IBOutletCollection);
405 APPLY_FOR_PROPERTY(GKInspectable);
Argyrios Kyrtzidisf2142cb2016-04-22 07:21:04 +0000406
Argyrios Kyrtzidisdb469832016-11-11 23:49:55 +0000407#undef APPLY_FOR_PROPERTY
Argyrios Kyrtzidisf2142cb2016-04-22 07:21:04 +0000408}
409
Argyrios Kyrtzidisdb469832016-11-11 23:49:55 +0000410void index::printSymbolProperties(SymbolPropertySet Props, raw_ostream &OS) {
Argyrios Kyrtzidisf2142cb2016-04-22 07:21:04 +0000411 bool VisitedOnce = false;
Argyrios Kyrtzidisdb469832016-11-11 23:49:55 +0000412 applyForEachSymbolProperty(Props, [&](SymbolProperty Prop) {
Argyrios Kyrtzidisf2142cb2016-04-22 07:21:04 +0000413 if (VisitedOnce)
414 OS << ',';
415 else
416 VisitedOnce = true;
Argyrios Kyrtzidisdb469832016-11-11 23:49:55 +0000417 switch (Prop) {
418 case SymbolProperty::Generic: OS << "Gen"; break;
419 case SymbolProperty::TemplatePartialSpecialization: OS << "TPS"; break;
420 case SymbolProperty::TemplateSpecialization: OS << "TS"; break;
421 case SymbolProperty::UnitTest: OS << "test"; break;
422 case SymbolProperty::IBAnnotated: OS << "IB"; break;
423 case SymbolProperty::IBOutletCollection: OS << "IBColl"; break;
424 case SymbolProperty::GKInspectable: OS << "GKI"; break;
Argyrios Kyrtzidisf2142cb2016-04-22 07:21:04 +0000425 }
426 });
427}