1 //===--- IndexSymbol.cpp - Types and functions for indexing symbols -------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #include "clang/Index/IndexSymbol.h"
11 #include "clang/AST/DeclCXX.h"
12 #include "clang/AST/DeclObjC.h"
13 #include "clang/AST/DeclTemplate.h"
14 #include "clang/AST/PrettyPrinter.h"
16 using namespace clang;
17 using namespace clang::index;
19 /// \returns true if \c D is a subclass of 'XCTestCase'.
20 static bool isUnitTestCase(const ObjCInterfaceDecl *D) {
23 while (const ObjCInterfaceDecl *SuperD = D->getSuperClass()) {
24 if (SuperD->getName() == "XCTestCase")
31 /// \returns true if \c D is in a subclass of 'XCTestCase', returns void, has
32 /// no parameters, and its name starts with 'test'.
33 static bool isUnitTest(const ObjCMethodDecl *D) {
34 if (!D->parameters().empty())
36 if (!D->getReturnType()->isVoidType())
38 if (!D->getSelector().getNameForSlot(0).startswith("test"))
40 return isUnitTestCase(D->getClassInterface());
43 static void checkForIBOutlets(const Decl *D, SymbolPropertySet &PropSet) {
44 if (D->hasAttr<IBOutletAttr>()) {
45 PropSet |= (unsigned)SymbolProperty::IBAnnotated;
46 } else if (D->hasAttr<IBOutletCollectionAttr>()) {
47 PropSet |= (unsigned)SymbolProperty::IBAnnotated;
48 PropSet |= (unsigned)SymbolProperty::IBOutletCollection;
52 bool index::isFunctionLocalSymbol(const Decl *D) {
55 if (isa<ParmVarDecl>(D))
58 if (isa<TemplateTemplateParmDecl>(D))
61 if (isa<ObjCTypeParamDecl>(D))
64 if (!D->getParentFunctionOrMethod())
67 if (const NamedDecl *ND = dyn_cast<NamedDecl>(D)) {
68 switch (ND->getFormalLinkage()) {
70 case VisibleNoLinkage:
73 case UniqueExternalLinkage:
74 llvm_unreachable("Not a sema linkage");
83 SymbolInfo index::getSymbolInfo(const Decl *D) {
86 Info.Kind = SymbolKind::Unknown;
87 Info.SubKind = SymbolSubKind::None;
88 Info.Properties = SymbolPropertySet();
89 Info.Lang = SymbolLanguage::C;
91 if (isFunctionLocalSymbol(D)) {
92 Info.Properties |= (unsigned)SymbolProperty::Local;
95 if (const TagDecl *TD = dyn_cast<TagDecl>(D)) {
96 switch (TD->getTagKind()) {
98 Info.Kind = SymbolKind::Struct; break;
100 Info.Kind = SymbolKind::Union; break;
102 Info.Kind = SymbolKind::Class;
103 Info.Lang = SymbolLanguage::CXX;
106 Info.Kind = SymbolKind::Protocol;
107 Info.Lang = SymbolLanguage::CXX;
110 Info.Kind = SymbolKind::Enum; break;
113 if (const CXXRecordDecl *CXXRec = dyn_cast<CXXRecordDecl>(D)) {
114 if (!CXXRec->isCLike()) {
115 Info.Lang = SymbolLanguage::CXX;
116 if (CXXRec->getDescribedClassTemplate()) {
117 Info.Properties |= (unsigned)SymbolProperty::Generic;
122 if (isa<ClassTemplatePartialSpecializationDecl>(D)) {
123 Info.Properties |= (unsigned)SymbolProperty::Generic;
124 Info.Properties |= (unsigned)SymbolProperty::TemplatePartialSpecialization;
125 } else if (isa<ClassTemplateSpecializationDecl>(D)) {
126 Info.Properties |= (unsigned)SymbolProperty::Generic;
127 Info.Properties |= (unsigned)SymbolProperty::TemplateSpecialization;
130 } else if (auto *VD = dyn_cast<VarDecl>(D)) {
131 Info.Kind = SymbolKind::Variable;
132 if (isa<ParmVarDecl>(D)) {
133 Info.Kind = SymbolKind::Parameter;
134 } else if (isa<CXXRecordDecl>(D->getDeclContext())) {
135 Info.Kind = SymbolKind::StaticProperty;
136 Info.Lang = SymbolLanguage::CXX;
139 if (isa<VarTemplatePartialSpecializationDecl>(D)) {
140 Info.Lang = SymbolLanguage::CXX;
141 Info.Properties |= (unsigned)SymbolProperty::Generic;
142 Info.Properties |= (unsigned)SymbolProperty::TemplatePartialSpecialization;
143 } else if (isa<VarTemplateSpecializationDecl>(D)) {
144 Info.Lang = SymbolLanguage::CXX;
145 Info.Properties |= (unsigned)SymbolProperty::Generic;
146 Info.Properties |= (unsigned)SymbolProperty::TemplateSpecialization;
147 } else if (VD->getDescribedVarTemplate()) {
148 Info.Lang = SymbolLanguage::CXX;
149 Info.Properties |= (unsigned)SymbolProperty::Generic;
153 switch (D->getKind()) {
155 Info.Kind = SymbolKind::Module;
158 Info.Kind = SymbolKind::TypeAlias; break; // Lang = C
160 Info.Kind = SymbolKind::Function;
163 Info.Kind = SymbolKind::Field;
164 if (const CXXRecordDecl *
165 CXXRec = dyn_cast<CXXRecordDecl>(D->getDeclContext())) {
166 if (!CXXRec->isCLike())
167 Info.Lang = SymbolLanguage::CXX;
170 case Decl::EnumConstant:
171 Info.Kind = SymbolKind::EnumConstant; break;
172 case Decl::ObjCInterface:
173 case Decl::ObjCImplementation: {
174 Info.Kind = SymbolKind::Class;
175 Info.Lang = SymbolLanguage::ObjC;
176 const ObjCInterfaceDecl *ClsD = dyn_cast<ObjCInterfaceDecl>(D);
178 ClsD = cast<ObjCImplementationDecl>(D)->getClassInterface();
179 if (isUnitTestCase(ClsD))
180 Info.Properties |= (unsigned)SymbolProperty::UnitTest;
183 case Decl::ObjCProtocol:
184 Info.Kind = SymbolKind::Protocol;
185 Info.Lang = SymbolLanguage::ObjC;
187 case Decl::ObjCCategory:
188 case Decl::ObjCCategoryImpl: {
189 Info.Kind = SymbolKind::Extension;
190 Info.Lang = SymbolLanguage::ObjC;
191 const ObjCInterfaceDecl *ClsD = nullptr;
192 if (auto *CatD = dyn_cast<ObjCCategoryDecl>(D))
193 ClsD = CatD->getClassInterface();
195 ClsD = cast<ObjCCategoryImplDecl>(D)->getClassInterface();
196 if (isUnitTestCase(ClsD))
197 Info.Properties |= (unsigned)SymbolProperty::UnitTest;
200 case Decl::ObjCMethod:
201 if (cast<ObjCMethodDecl>(D)->isInstanceMethod()) {
202 const ObjCMethodDecl *MD = cast<ObjCMethodDecl>(D);
203 Info.Kind = SymbolKind::InstanceMethod;
204 if (MD->isPropertyAccessor()) {
205 if (MD->param_size())
206 Info.SubKind = SymbolSubKind::AccessorSetter;
208 Info.SubKind = SymbolSubKind::AccessorGetter;
211 Info.Kind = SymbolKind::ClassMethod;
213 Info.Lang = SymbolLanguage::ObjC;
214 if (isUnitTest(cast<ObjCMethodDecl>(D)))
215 Info.Properties |= (unsigned)SymbolProperty::UnitTest;
216 if (D->hasAttr<IBActionAttr>())
217 Info.Properties |= (unsigned)SymbolProperty::IBAnnotated;
219 case Decl::ObjCProperty:
220 Info.Kind = SymbolKind::InstanceProperty;
221 Info.Lang = SymbolLanguage::ObjC;
222 checkForIBOutlets(D, Info.Properties);
223 if (auto *Annot = D->getAttr<AnnotateAttr>()) {
224 if (Annot->getAnnotation() == "gk_inspectable")
225 Info.Properties |= (unsigned)SymbolProperty::GKInspectable;
229 Info.Kind = SymbolKind::Field;
230 Info.Lang = SymbolLanguage::ObjC;
231 checkForIBOutlets(D, Info.Properties);
233 case Decl::Namespace:
234 Info.Kind = SymbolKind::Namespace;
235 Info.Lang = SymbolLanguage::CXX;
237 case Decl::NamespaceAlias:
238 Info.Kind = SymbolKind::NamespaceAlias;
239 Info.Lang = SymbolLanguage::CXX;
241 case Decl::CXXConstructor: {
242 Info.Kind = SymbolKind::Constructor;
243 Info.Lang = SymbolLanguage::CXX;
244 auto *CD = cast<CXXConstructorDecl>(D);
245 if (CD->isCopyConstructor())
246 Info.SubKind = SymbolSubKind::CXXCopyConstructor;
247 else if (CD->isMoveConstructor())
248 Info.SubKind = SymbolSubKind::CXXMoveConstructor;
251 case Decl::CXXDestructor:
252 Info.Kind = SymbolKind::Destructor;
253 Info.Lang = SymbolLanguage::CXX;
255 case Decl::CXXConversion:
256 Info.Kind = SymbolKind::ConversionFunction;
257 Info.Lang = SymbolLanguage::CXX;
259 case Decl::CXXMethod: {
260 const CXXMethodDecl *MD = cast<CXXMethodDecl>(D);
262 Info.Kind = SymbolKind::StaticMethod;
264 Info.Kind = SymbolKind::InstanceMethod;
265 Info.Lang = SymbolLanguage::CXX;
268 case Decl::ClassTemplate:
269 Info.Kind = SymbolKind::Class;
270 Info.Properties |= (unsigned)SymbolProperty::Generic;
271 Info.Lang = SymbolLanguage::CXX;
273 case Decl::FunctionTemplate:
274 Info.Kind = SymbolKind::Function;
275 Info.Properties |= (unsigned)SymbolProperty::Generic;
276 Info.Lang = SymbolLanguage::CXX;
277 if (const CXXMethodDecl *MD = dyn_cast_or_null<CXXMethodDecl>(
278 cast<FunctionTemplateDecl>(D)->getTemplatedDecl())) {
279 if (isa<CXXConstructorDecl>(MD))
280 Info.Kind = SymbolKind::Constructor;
281 else if (isa<CXXDestructorDecl>(MD))
282 Info.Kind = SymbolKind::Destructor;
283 else if (isa<CXXConversionDecl>(MD))
284 Info.Kind = SymbolKind::ConversionFunction;
287 Info.Kind = SymbolKind::StaticMethod;
289 Info.Kind = SymbolKind::InstanceMethod;
293 case Decl::TypeAliasTemplate:
294 Info.Kind = SymbolKind::TypeAlias;
295 Info.Lang = SymbolLanguage::CXX;
296 Info.Properties |= (unsigned)SymbolProperty::Generic;
298 case Decl::TypeAlias:
299 Info.Kind = SymbolKind::TypeAlias;
300 Info.Lang = SymbolLanguage::CXX;
307 if (Info.Kind == SymbolKind::Unknown)
310 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
311 if (FD->getTemplatedKind() ==
312 FunctionDecl::TK_FunctionTemplateSpecialization) {
313 Info.Properties |= (unsigned)SymbolProperty::Generic;
314 Info.Properties |= (unsigned)SymbolProperty::TemplateSpecialization;
318 if (Info.Properties & (unsigned)SymbolProperty::Generic)
319 Info.Lang = SymbolLanguage::CXX;
321 auto getExternalSymAttr = [](const Decl *D) -> ExternalSourceSymbolAttr* {
322 if (auto *attr = D->getAttr<ExternalSourceSymbolAttr>())
324 if (auto *dcd = dyn_cast<Decl>(D->getDeclContext())) {
325 if (auto *attr = dcd->getAttr<ExternalSourceSymbolAttr>())
330 if (auto *attr = getExternalSymAttr(D)) {
331 if (attr->getLanguage() == "Swift")
332 Info.Lang = SymbolLanguage::Swift;
338 bool index::applyForEachSymbolRoleInterruptible(SymbolRoleSet Roles,
339 llvm::function_ref<bool(SymbolRole)> Fn) {
340 #define APPLY_FOR_ROLE(Role) \
341 if (Roles & (unsigned)SymbolRole::Role) \
342 if (!Fn(SymbolRole::Role)) \
345 APPLY_FOR_ROLE(Declaration);
346 APPLY_FOR_ROLE(Definition);
347 APPLY_FOR_ROLE(Reference);
348 APPLY_FOR_ROLE(Read);
349 APPLY_FOR_ROLE(Write);
350 APPLY_FOR_ROLE(Call);
351 APPLY_FOR_ROLE(Dynamic);
352 APPLY_FOR_ROLE(AddressOf);
353 APPLY_FOR_ROLE(Implicit);
354 APPLY_FOR_ROLE(RelationChildOf);
355 APPLY_FOR_ROLE(RelationBaseOf);
356 APPLY_FOR_ROLE(RelationOverrideOf);
357 APPLY_FOR_ROLE(RelationReceivedBy);
358 APPLY_FOR_ROLE(RelationCalledBy);
359 APPLY_FOR_ROLE(RelationExtendedBy);
360 APPLY_FOR_ROLE(RelationAccessorOf);
361 APPLY_FOR_ROLE(RelationContainedBy);
362 APPLY_FOR_ROLE(RelationIBTypeOf);
363 APPLY_FOR_ROLE(RelationSpecializationOf);
365 #undef APPLY_FOR_ROLE
370 void index::applyForEachSymbolRole(SymbolRoleSet Roles,
371 llvm::function_ref<void(SymbolRole)> Fn) {
372 applyForEachSymbolRoleInterruptible(Roles, [&](SymbolRole r) -> bool {
378 void index::printSymbolRoles(SymbolRoleSet Roles, raw_ostream &OS) {
379 bool VisitedOnce = false;
380 applyForEachSymbolRole(Roles, [&](SymbolRole Role) {
386 case SymbolRole::Declaration: OS << "Decl"; break;
387 case SymbolRole::Definition: OS << "Def"; break;
388 case SymbolRole::Reference: OS << "Ref"; break;
389 case SymbolRole::Read: OS << "Read"; break;
390 case SymbolRole::Write: OS << "Writ"; break;
391 case SymbolRole::Call: OS << "Call"; break;
392 case SymbolRole::Dynamic: OS << "Dyn"; break;
393 case SymbolRole::AddressOf: OS << "Addr"; break;
394 case SymbolRole::Implicit: OS << "Impl"; break;
395 case SymbolRole::RelationChildOf: OS << "RelChild"; break;
396 case SymbolRole::RelationBaseOf: OS << "RelBase"; break;
397 case SymbolRole::RelationOverrideOf: OS << "RelOver"; break;
398 case SymbolRole::RelationReceivedBy: OS << "RelRec"; break;
399 case SymbolRole::RelationCalledBy: OS << "RelCall"; break;
400 case SymbolRole::RelationExtendedBy: OS << "RelExt"; break;
401 case SymbolRole::RelationAccessorOf: OS << "RelAcc"; break;
402 case SymbolRole::RelationContainedBy: OS << "RelCont"; break;
403 case SymbolRole::RelationIBTypeOf: OS << "RelIBType"; break;
404 case SymbolRole::RelationSpecializationOf: OS << "RelSpecialization"; break;
409 bool index::printSymbolName(const Decl *D, const LangOptions &LO,
411 if (auto *ND = dyn_cast<NamedDecl>(D)) {
412 PrintingPolicy Policy(LO);
413 // Forward references can have different template argument names. Suppress
414 // the template argument names in constructors to make their name more
416 Policy.SuppressTemplateArgsInCXXConstructors = true;
417 DeclarationName DeclName = ND->getDeclName();
418 if (DeclName.isEmpty())
420 DeclName.print(OS, Policy);
427 StringRef index::getSymbolKindString(SymbolKind K) {
429 case SymbolKind::Unknown: return "<unknown>";
430 case SymbolKind::Module: return "module";
431 case SymbolKind::Namespace: return "namespace";
432 case SymbolKind::NamespaceAlias: return "namespace-alias";
433 case SymbolKind::Macro: return "macro";
434 case SymbolKind::Enum: return "enum";
435 case SymbolKind::Struct: return "struct";
436 case SymbolKind::Class: return "class";
437 case SymbolKind::Protocol: return "protocol";
438 case SymbolKind::Extension: return "extension";
439 case SymbolKind::Union: return "union";
440 case SymbolKind::TypeAlias: return "type-alias";
441 case SymbolKind::Function: return "function";
442 case SymbolKind::Variable: return "variable";
443 case SymbolKind::Field: return "field";
444 case SymbolKind::EnumConstant: return "enumerator";
445 case SymbolKind::InstanceMethod: return "instance-method";
446 case SymbolKind::ClassMethod: return "class-method";
447 case SymbolKind::StaticMethod: return "static-method";
448 case SymbolKind::InstanceProperty: return "instance-property";
449 case SymbolKind::ClassProperty: return "class-property";
450 case SymbolKind::StaticProperty: return "static-property";
451 case SymbolKind::Constructor: return "constructor";
452 case SymbolKind::Destructor: return "destructor";
453 case SymbolKind::ConversionFunction: return "coversion-func";
454 case SymbolKind::Parameter: return "param";
456 llvm_unreachable("invalid symbol kind");
459 StringRef index::getSymbolSubKindString(SymbolSubKind K) {
461 case SymbolSubKind::None: return "<none>";
462 case SymbolSubKind::CXXCopyConstructor: return "cxx-copy-ctor";
463 case SymbolSubKind::CXXMoveConstructor: return "cxx-move-ctor";
464 case SymbolSubKind::AccessorGetter: return "acc-get";
465 case SymbolSubKind::AccessorSetter: return "acc-set";
467 llvm_unreachable("invalid symbol subkind");
470 StringRef index::getSymbolLanguageString(SymbolLanguage K) {
472 case SymbolLanguage::C: return "C";
473 case SymbolLanguage::ObjC: return "ObjC";
474 case SymbolLanguage::CXX: return "C++";
475 case SymbolLanguage::Swift: return "Swift";
477 llvm_unreachable("invalid symbol language kind");
480 void index::applyForEachSymbolProperty(SymbolPropertySet Props,
481 llvm::function_ref<void(SymbolProperty)> Fn) {
482 #define APPLY_FOR_PROPERTY(K) \
483 if (Props & (unsigned)SymbolProperty::K) \
484 Fn(SymbolProperty::K)
486 APPLY_FOR_PROPERTY(Generic);
487 APPLY_FOR_PROPERTY(TemplatePartialSpecialization);
488 APPLY_FOR_PROPERTY(TemplateSpecialization);
489 APPLY_FOR_PROPERTY(UnitTest);
490 APPLY_FOR_PROPERTY(IBAnnotated);
491 APPLY_FOR_PROPERTY(IBOutletCollection);
492 APPLY_FOR_PROPERTY(GKInspectable);
493 APPLY_FOR_PROPERTY(Local);
495 #undef APPLY_FOR_PROPERTY
498 void index::printSymbolProperties(SymbolPropertySet Props, raw_ostream &OS) {
499 bool VisitedOnce = false;
500 applyForEachSymbolProperty(Props, [&](SymbolProperty Prop) {
506 case SymbolProperty::Generic: OS << "Gen"; break;
507 case SymbolProperty::TemplatePartialSpecialization: OS << "TPS"; break;
508 case SymbolProperty::TemplateSpecialization: OS << "TS"; break;
509 case SymbolProperty::UnitTest: OS << "test"; break;
510 case SymbolProperty::IBAnnotated: OS << "IB"; break;
511 case SymbolProperty::IBOutletCollection: OS << "IBColl"; break;
512 case SymbolProperty::GKInspectable: OS << "GKI"; break;
513 case SymbolProperty::Local: OS << "local"; break;