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 |= (SymbolPropertySet)SymbolProperty::IBAnnotated;
46 } else if (D->hasAttr<IBOutletCollectionAttr>()) {
47 PropSet |= (SymbolPropertySet)SymbolProperty::IBAnnotated;
48 PropSet |= (SymbolPropertySet)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 (isa<UsingDirectiveDecl>(D))
66 if (!D->getParentFunctionOrMethod())
69 if (const NamedDecl *ND = dyn_cast<NamedDecl>(D)) {
70 switch (ND->getFormalLinkage()) {
74 case VisibleNoLinkage:
75 case UniqueExternalLinkage:
76 case ModuleInternalLinkage:
77 llvm_unreachable("Not a sema linkage");
87 SymbolInfo index::getSymbolInfo(const Decl *D) {
90 Info.Kind = SymbolKind::Unknown;
91 Info.SubKind = SymbolSubKind::None;
92 Info.Properties = SymbolPropertySet();
93 Info.Lang = SymbolLanguage::C;
95 if (isFunctionLocalSymbol(D)) {
96 Info.Properties |= (SymbolPropertySet)SymbolProperty::Local;
99 if (const TagDecl *TD = dyn_cast<TagDecl>(D)) {
100 switch (TD->getTagKind()) {
102 Info.Kind = SymbolKind::Struct; break;
104 Info.Kind = SymbolKind::Union; break;
106 Info.Kind = SymbolKind::Class;
107 Info.Lang = SymbolLanguage::CXX;
110 Info.Kind = SymbolKind::Protocol;
111 Info.Lang = SymbolLanguage::CXX;
114 Info.Kind = SymbolKind::Enum; break;
117 if (const CXXRecordDecl *CXXRec = dyn_cast<CXXRecordDecl>(D)) {
118 if (!CXXRec->isCLike()) {
119 Info.Lang = SymbolLanguage::CXX;
120 if (CXXRec->getDescribedClassTemplate()) {
121 Info.Properties |= (SymbolPropertySet)SymbolProperty::Generic;
126 if (isa<ClassTemplatePartialSpecializationDecl>(D)) {
127 Info.Properties |= (SymbolPropertySet)SymbolProperty::Generic;
129 (SymbolPropertySet)SymbolProperty::TemplatePartialSpecialization;
130 } else if (isa<ClassTemplateSpecializationDecl>(D)) {
131 Info.Properties |= (SymbolPropertySet)SymbolProperty::Generic;
133 (SymbolPropertySet)SymbolProperty::TemplateSpecialization;
136 } else if (auto *VD = dyn_cast<VarDecl>(D)) {
137 Info.Kind = SymbolKind::Variable;
138 if (isa<ParmVarDecl>(D)) {
139 Info.Kind = SymbolKind::Parameter;
140 } else if (isa<CXXRecordDecl>(D->getDeclContext())) {
141 Info.Kind = SymbolKind::StaticProperty;
142 Info.Lang = SymbolLanguage::CXX;
145 if (isa<VarTemplatePartialSpecializationDecl>(D)) {
146 Info.Lang = SymbolLanguage::CXX;
147 Info.Properties |= (SymbolPropertySet)SymbolProperty::Generic;
149 (SymbolPropertySet)SymbolProperty::TemplatePartialSpecialization;
150 } else if (isa<VarTemplateSpecializationDecl>(D)) {
151 Info.Lang = SymbolLanguage::CXX;
152 Info.Properties |= (SymbolPropertySet)SymbolProperty::Generic;
154 (SymbolPropertySet)SymbolProperty::TemplateSpecialization;
155 } else if (VD->getDescribedVarTemplate()) {
156 Info.Lang = SymbolLanguage::CXX;
157 Info.Properties |= (SymbolPropertySet)SymbolProperty::Generic;
161 switch (D->getKind()) {
163 Info.Kind = SymbolKind::Module;
166 Info.Kind = SymbolKind::TypeAlias; break; // Lang = C
168 Info.Kind = SymbolKind::Function;
171 Info.Kind = SymbolKind::Field;
172 if (const CXXRecordDecl *
173 CXXRec = dyn_cast<CXXRecordDecl>(D->getDeclContext())) {
174 if (!CXXRec->isCLike())
175 Info.Lang = SymbolLanguage::CXX;
178 case Decl::EnumConstant:
179 Info.Kind = SymbolKind::EnumConstant; break;
180 case Decl::ObjCInterface:
181 case Decl::ObjCImplementation: {
182 Info.Kind = SymbolKind::Class;
183 Info.Lang = SymbolLanguage::ObjC;
184 const ObjCInterfaceDecl *ClsD = dyn_cast<ObjCInterfaceDecl>(D);
186 ClsD = cast<ObjCImplementationDecl>(D)->getClassInterface();
187 if (isUnitTestCase(ClsD))
188 Info.Properties |= (SymbolPropertySet)SymbolProperty::UnitTest;
191 case Decl::ObjCProtocol:
192 Info.Kind = SymbolKind::Protocol;
193 Info.Lang = SymbolLanguage::ObjC;
195 case Decl::ObjCCategory:
196 case Decl::ObjCCategoryImpl: {
197 Info.Kind = SymbolKind::Extension;
198 Info.Lang = SymbolLanguage::ObjC;
199 const ObjCInterfaceDecl *ClsD = nullptr;
200 if (auto *CatD = dyn_cast<ObjCCategoryDecl>(D))
201 ClsD = CatD->getClassInterface();
203 ClsD = cast<ObjCCategoryImplDecl>(D)->getClassInterface();
204 if (isUnitTestCase(ClsD))
205 Info.Properties |= (SymbolPropertySet)SymbolProperty::UnitTest;
208 case Decl::ObjCMethod: {
209 const ObjCMethodDecl *MD = cast<ObjCMethodDecl>(D);
210 Info.Kind = MD->isInstanceMethod() ? SymbolKind::InstanceMethod : SymbolKind::ClassMethod;
211 if (MD->isPropertyAccessor()) {
212 if (MD->param_size())
213 Info.SubKind = SymbolSubKind::AccessorSetter;
215 Info.SubKind = SymbolSubKind::AccessorGetter;
217 Info.Lang = SymbolLanguage::ObjC;
219 Info.Properties |= (SymbolPropertySet)SymbolProperty::UnitTest;
220 if (D->hasAttr<IBActionAttr>())
221 Info.Properties |= (SymbolPropertySet)SymbolProperty::IBAnnotated;
224 case Decl::ObjCProperty:
225 Info.Kind = SymbolKind::InstanceProperty;
226 Info.Lang = SymbolLanguage::ObjC;
227 checkForIBOutlets(D, Info.Properties);
228 if (auto *Annot = D->getAttr<AnnotateAttr>()) {
229 if (Annot->getAnnotation() == "gk_inspectable")
230 Info.Properties |= (SymbolPropertySet)SymbolProperty::GKInspectable;
234 Info.Kind = SymbolKind::Field;
235 Info.Lang = SymbolLanguage::ObjC;
236 checkForIBOutlets(D, Info.Properties);
238 case Decl::Namespace:
239 Info.Kind = SymbolKind::Namespace;
240 Info.Lang = SymbolLanguage::CXX;
242 case Decl::NamespaceAlias:
243 Info.Kind = SymbolKind::NamespaceAlias;
244 Info.Lang = SymbolLanguage::CXX;
246 case Decl::CXXConstructor: {
247 Info.Kind = SymbolKind::Constructor;
248 Info.Lang = SymbolLanguage::CXX;
249 auto *CD = cast<CXXConstructorDecl>(D);
250 if (CD->isCopyConstructor())
251 Info.SubKind = SymbolSubKind::CXXCopyConstructor;
252 else if (CD->isMoveConstructor())
253 Info.SubKind = SymbolSubKind::CXXMoveConstructor;
256 case Decl::CXXDestructor:
257 Info.Kind = SymbolKind::Destructor;
258 Info.Lang = SymbolLanguage::CXX;
260 case Decl::CXXConversion:
261 Info.Kind = SymbolKind::ConversionFunction;
262 Info.Lang = SymbolLanguage::CXX;
264 case Decl::CXXMethod: {
265 const CXXMethodDecl *MD = cast<CXXMethodDecl>(D);
267 Info.Kind = SymbolKind::StaticMethod;
269 Info.Kind = SymbolKind::InstanceMethod;
270 Info.Lang = SymbolLanguage::CXX;
273 case Decl::ClassTemplate:
274 Info.Kind = SymbolKind::Class;
275 Info.Properties |= (SymbolPropertySet)SymbolProperty::Generic;
276 Info.Lang = SymbolLanguage::CXX;
278 case Decl::FunctionTemplate:
279 Info.Kind = SymbolKind::Function;
280 Info.Properties |= (SymbolPropertySet)SymbolProperty::Generic;
281 Info.Lang = SymbolLanguage::CXX;
282 if (const CXXMethodDecl *MD = dyn_cast_or_null<CXXMethodDecl>(
283 cast<FunctionTemplateDecl>(D)->getTemplatedDecl())) {
284 if (isa<CXXConstructorDecl>(MD))
285 Info.Kind = SymbolKind::Constructor;
286 else if (isa<CXXDestructorDecl>(MD))
287 Info.Kind = SymbolKind::Destructor;
288 else if (isa<CXXConversionDecl>(MD))
289 Info.Kind = SymbolKind::ConversionFunction;
292 Info.Kind = SymbolKind::StaticMethod;
294 Info.Kind = SymbolKind::InstanceMethod;
298 case Decl::TypeAliasTemplate:
299 Info.Kind = SymbolKind::TypeAlias;
300 Info.Lang = SymbolLanguage::CXX;
301 Info.Properties |= (SymbolPropertySet)SymbolProperty::Generic;
303 case Decl::TypeAlias:
304 Info.Kind = SymbolKind::TypeAlias;
305 Info.Lang = SymbolLanguage::CXX;
307 case Decl::UnresolvedUsingTypename:
308 Info.Kind = SymbolKind::Using;
309 Info.SubKind = SymbolSubKind::UsingTypename;
310 Info.Lang = SymbolLanguage::CXX;
311 Info.Properties |= (SymbolPropertySet)SymbolProperty::Generic;
313 case Decl::UnresolvedUsingValue:
314 Info.Kind = SymbolKind::Using;
315 Info.SubKind = SymbolSubKind::UsingValue;
316 Info.Lang = SymbolLanguage::CXX;
317 Info.Properties |= (SymbolPropertySet)SymbolProperty::Generic;
320 Info.Kind = SymbolKind::Variable;
321 Info.Lang = SymbolLanguage::CXX;
328 if (Info.Kind == SymbolKind::Unknown)
331 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
332 if (FD->getTemplatedKind() ==
333 FunctionDecl::TK_FunctionTemplateSpecialization) {
334 Info.Properties |= (SymbolPropertySet)SymbolProperty::Generic;
336 (SymbolPropertySet)SymbolProperty::TemplateSpecialization;
340 if (Info.Properties & (SymbolPropertySet)SymbolProperty::Generic)
341 Info.Lang = SymbolLanguage::CXX;
343 if (auto *attr = D->getExternalSourceSymbolAttr()) {
344 if (attr->getLanguage() == "Swift")
345 Info.Lang = SymbolLanguage::Swift;
351 bool index::applyForEachSymbolRoleInterruptible(SymbolRoleSet Roles,
352 llvm::function_ref<bool(SymbolRole)> Fn) {
353 #define APPLY_FOR_ROLE(Role) \
354 if (Roles & (unsigned)SymbolRole::Role) \
355 if (!Fn(SymbolRole::Role)) \
358 APPLY_FOR_ROLE(Declaration);
359 APPLY_FOR_ROLE(Definition);
360 APPLY_FOR_ROLE(Reference);
361 APPLY_FOR_ROLE(Read);
362 APPLY_FOR_ROLE(Write);
363 APPLY_FOR_ROLE(Call);
364 APPLY_FOR_ROLE(Dynamic);
365 APPLY_FOR_ROLE(AddressOf);
366 APPLY_FOR_ROLE(Implicit);
367 APPLY_FOR_ROLE(RelationChildOf);
368 APPLY_FOR_ROLE(RelationBaseOf);
369 APPLY_FOR_ROLE(RelationOverrideOf);
370 APPLY_FOR_ROLE(RelationReceivedBy);
371 APPLY_FOR_ROLE(RelationCalledBy);
372 APPLY_FOR_ROLE(RelationExtendedBy);
373 APPLY_FOR_ROLE(RelationAccessorOf);
374 APPLY_FOR_ROLE(RelationContainedBy);
375 APPLY_FOR_ROLE(RelationIBTypeOf);
376 APPLY_FOR_ROLE(RelationSpecializationOf);
378 #undef APPLY_FOR_ROLE
383 void index::applyForEachSymbolRole(SymbolRoleSet Roles,
384 llvm::function_ref<void(SymbolRole)> Fn) {
385 applyForEachSymbolRoleInterruptible(Roles, [&](SymbolRole r) -> bool {
391 void index::printSymbolRoles(SymbolRoleSet Roles, raw_ostream &OS) {
392 bool VisitedOnce = false;
393 applyForEachSymbolRole(Roles, [&](SymbolRole Role) {
399 case SymbolRole::Declaration: OS << "Decl"; break;
400 case SymbolRole::Definition: OS << "Def"; break;
401 case SymbolRole::Reference: OS << "Ref"; break;
402 case SymbolRole::Read: OS << "Read"; break;
403 case SymbolRole::Write: OS << "Writ"; break;
404 case SymbolRole::Call: OS << "Call"; break;
405 case SymbolRole::Dynamic: OS << "Dyn"; break;
406 case SymbolRole::AddressOf: OS << "Addr"; break;
407 case SymbolRole::Implicit: OS << "Impl"; break;
408 case SymbolRole::RelationChildOf: OS << "RelChild"; break;
409 case SymbolRole::RelationBaseOf: OS << "RelBase"; break;
410 case SymbolRole::RelationOverrideOf: OS << "RelOver"; break;
411 case SymbolRole::RelationReceivedBy: OS << "RelRec"; break;
412 case SymbolRole::RelationCalledBy: OS << "RelCall"; break;
413 case SymbolRole::RelationExtendedBy: OS << "RelExt"; break;
414 case SymbolRole::RelationAccessorOf: OS << "RelAcc"; break;
415 case SymbolRole::RelationContainedBy: OS << "RelCont"; break;
416 case SymbolRole::RelationIBTypeOf: OS << "RelIBType"; break;
417 case SymbolRole::RelationSpecializationOf: OS << "RelSpecialization"; break;
422 bool index::printSymbolName(const Decl *D, const LangOptions &LO,
424 if (auto *ND = dyn_cast<NamedDecl>(D)) {
425 PrintingPolicy Policy(LO);
426 // Forward references can have different template argument names. Suppress
427 // the template argument names in constructors to make their name more
429 Policy.SuppressTemplateArgsInCXXConstructors = true;
430 DeclarationName DeclName = ND->getDeclName();
431 if (DeclName.isEmpty())
433 DeclName.print(OS, Policy);
440 StringRef index::getSymbolKindString(SymbolKind K) {
442 case SymbolKind::Unknown: return "<unknown>";
443 case SymbolKind::Module: return "module";
444 case SymbolKind::Namespace: return "namespace";
445 case SymbolKind::NamespaceAlias: return "namespace-alias";
446 case SymbolKind::Macro: return "macro";
447 case SymbolKind::Enum: return "enum";
448 case SymbolKind::Struct: return "struct";
449 case SymbolKind::Class: return "class";
450 case SymbolKind::Protocol: return "protocol";
451 case SymbolKind::Extension: return "extension";
452 case SymbolKind::Union: return "union";
453 case SymbolKind::TypeAlias: return "type-alias";
454 case SymbolKind::Function: return "function";
455 case SymbolKind::Variable: return "variable";
456 case SymbolKind::Field: return "field";
457 case SymbolKind::EnumConstant: return "enumerator";
458 case SymbolKind::InstanceMethod: return "instance-method";
459 case SymbolKind::ClassMethod: return "class-method";
460 case SymbolKind::StaticMethod: return "static-method";
461 case SymbolKind::InstanceProperty: return "instance-property";
462 case SymbolKind::ClassProperty: return "class-property";
463 case SymbolKind::StaticProperty: return "static-property";
464 case SymbolKind::Constructor: return "constructor";
465 case SymbolKind::Destructor: return "destructor";
466 case SymbolKind::ConversionFunction: return "coversion-func";
467 case SymbolKind::Parameter: return "param";
468 case SymbolKind::Using: return "using";
470 llvm_unreachable("invalid symbol kind");
473 StringRef index::getSymbolSubKindString(SymbolSubKind K) {
475 case SymbolSubKind::None: return "<none>";
476 case SymbolSubKind::CXXCopyConstructor: return "cxx-copy-ctor";
477 case SymbolSubKind::CXXMoveConstructor: return "cxx-move-ctor";
478 case SymbolSubKind::AccessorGetter: return "acc-get";
479 case SymbolSubKind::AccessorSetter: return "acc-set";
480 case SymbolSubKind::UsingTypename: return "using-typename";
481 case SymbolSubKind::UsingValue: return "using-value";
483 llvm_unreachable("invalid symbol subkind");
486 StringRef index::getSymbolLanguageString(SymbolLanguage K) {
488 case SymbolLanguage::C: return "C";
489 case SymbolLanguage::ObjC: return "ObjC";
490 case SymbolLanguage::CXX: return "C++";
491 case SymbolLanguage::Swift: return "Swift";
493 llvm_unreachable("invalid symbol language kind");
496 void index::applyForEachSymbolProperty(SymbolPropertySet Props,
497 llvm::function_ref<void(SymbolProperty)> Fn) {
498 #define APPLY_FOR_PROPERTY(K) \
499 if (Props & (SymbolPropertySet)SymbolProperty::K) \
500 Fn(SymbolProperty::K)
502 APPLY_FOR_PROPERTY(Generic);
503 APPLY_FOR_PROPERTY(TemplatePartialSpecialization);
504 APPLY_FOR_PROPERTY(TemplateSpecialization);
505 APPLY_FOR_PROPERTY(UnitTest);
506 APPLY_FOR_PROPERTY(IBAnnotated);
507 APPLY_FOR_PROPERTY(IBOutletCollection);
508 APPLY_FOR_PROPERTY(GKInspectable);
509 APPLY_FOR_PROPERTY(Local);
511 #undef APPLY_FOR_PROPERTY
514 void index::printSymbolProperties(SymbolPropertySet Props, raw_ostream &OS) {
515 bool VisitedOnce = false;
516 applyForEachSymbolProperty(Props, [&](SymbolProperty Prop) {
522 case SymbolProperty::Generic: OS << "Gen"; break;
523 case SymbolProperty::TemplatePartialSpecialization: OS << "TPS"; break;
524 case SymbolProperty::TemplateSpecialization: OS << "TS"; break;
525 case SymbolProperty::UnitTest: OS << "test"; break;
526 case SymbolProperty::IBAnnotated: OS << "IB"; break;
527 case SymbolProperty::IBOutletCollection: OS << "IBColl"; break;
528 case SymbolProperty::GKInspectable: OS << "GKI"; break;
529 case SymbolProperty::Local: OS << "local"; break;