1 //===--- IndexSymbol.cpp - Types and functions for indexing symbols -------===//
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
7 //===----------------------------------------------------------------------===//
9 #include "clang/Index/IndexSymbol.h"
10 #include "clang/AST/Attr.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"
15 #include "clang/Lex/MacroInfo.h"
17 using namespace clang;
18 using namespace clang::index;
20 /// \returns true if \c D is a subclass of 'XCTestCase'.
21 static bool isUnitTestCase(const ObjCInterfaceDecl *D) {
24 while (const ObjCInterfaceDecl *SuperD = D->getSuperClass()) {
25 if (SuperD->getName() == "XCTestCase")
32 /// \returns true if \c D is in a subclass of 'XCTestCase', returns void, has
33 /// no parameters, and its name starts with 'test'.
34 static bool isUnitTest(const ObjCMethodDecl *D) {
35 if (!D->parameters().empty())
37 if (!D->getReturnType()->isVoidType())
39 if (!D->getSelector().getNameForSlot(0).startswith("test"))
41 return isUnitTestCase(D->getClassInterface());
44 static void checkForIBOutlets(const Decl *D, SymbolPropertySet &PropSet) {
45 if (D->hasAttr<IBOutletAttr>()) {
46 PropSet |= (SymbolPropertySet)SymbolProperty::IBAnnotated;
47 } else if (D->hasAttr<IBOutletCollectionAttr>()) {
48 PropSet |= (SymbolPropertySet)SymbolProperty::IBAnnotated;
49 PropSet |= (SymbolPropertySet)SymbolProperty::IBOutletCollection;
53 bool index::isFunctionLocalSymbol(const Decl *D) {
56 if (isa<ParmVarDecl>(D))
59 if (isa<ObjCTypeParamDecl>(D))
62 if (isa<UsingDirectiveDecl>(D))
64 if (!D->getParentFunctionOrMethod())
67 if (const NamedDecl *ND = dyn_cast<NamedDecl>(D)) {
68 switch (ND->getFormalLinkage()) {
72 case VisibleNoLinkage:
73 case UniqueExternalLinkage:
74 case ModuleInternalLinkage:
75 llvm_unreachable("Not a sema linkage");
85 SymbolInfo index::getSymbolInfo(const Decl *D) {
88 Info.Kind = SymbolKind::Unknown;
89 Info.SubKind = SymbolSubKind::None;
90 Info.Properties = SymbolPropertySet();
91 Info.Lang = SymbolLanguage::C;
93 if (isFunctionLocalSymbol(D)) {
94 Info.Properties |= (SymbolPropertySet)SymbolProperty::Local;
96 if (isa<ObjCProtocolDecl>(D->getDeclContext())) {
97 Info.Properties |= (SymbolPropertySet)SymbolProperty::ProtocolInterface;
100 if (auto *VT = dyn_cast<VarTemplateDecl>(D)) {
101 Info.Properties |= (SymbolPropertySet)SymbolProperty::Generic;
102 Info.Lang = SymbolLanguage::CXX;
103 // All other fields are filled from the templated decl.
104 D = VT->getTemplatedDecl();
107 if (const TagDecl *TD = dyn_cast<TagDecl>(D)) {
108 switch (TD->getTagKind()) {
110 Info.Kind = SymbolKind::Struct; break;
112 Info.Kind = SymbolKind::Union; break;
114 Info.Kind = SymbolKind::Class;
115 Info.Lang = SymbolLanguage::CXX;
118 Info.Kind = SymbolKind::Protocol;
119 Info.Lang = SymbolLanguage::CXX;
122 Info.Kind = SymbolKind::Enum; break;
125 if (const CXXRecordDecl *CXXRec = dyn_cast<CXXRecordDecl>(D)) {
126 if (!CXXRec->isCLike()) {
127 Info.Lang = SymbolLanguage::CXX;
128 if (CXXRec->getDescribedClassTemplate()) {
129 Info.Properties |= (SymbolPropertySet)SymbolProperty::Generic;
134 if (isa<ClassTemplatePartialSpecializationDecl>(D)) {
135 Info.Properties |= (SymbolPropertySet)SymbolProperty::Generic;
137 (SymbolPropertySet)SymbolProperty::TemplatePartialSpecialization;
138 } else if (isa<ClassTemplateSpecializationDecl>(D)) {
139 Info.Properties |= (SymbolPropertySet)SymbolProperty::Generic;
141 (SymbolPropertySet)SymbolProperty::TemplateSpecialization;
144 } else if (auto *VD = dyn_cast<VarDecl>(D)) {
145 Info.Kind = SymbolKind::Variable;
146 if (isa<ParmVarDecl>(D)) {
147 Info.Kind = SymbolKind::Parameter;
148 } else if (isa<CXXRecordDecl>(D->getDeclContext())) {
149 Info.Kind = SymbolKind::StaticProperty;
150 Info.Lang = SymbolLanguage::CXX;
153 if (isa<VarTemplatePartialSpecializationDecl>(D)) {
154 Info.Lang = SymbolLanguage::CXX;
155 Info.Properties |= (SymbolPropertySet)SymbolProperty::Generic;
157 (SymbolPropertySet)SymbolProperty::TemplatePartialSpecialization;
158 } else if (isa<VarTemplateSpecializationDecl>(D)) {
159 Info.Lang = SymbolLanguage::CXX;
160 Info.Properties |= (SymbolPropertySet)SymbolProperty::Generic;
162 (SymbolPropertySet)SymbolProperty::TemplateSpecialization;
163 } else if (VD->getDescribedVarTemplate()) {
164 Info.Lang = SymbolLanguage::CXX;
165 Info.Properties |= (SymbolPropertySet)SymbolProperty::Generic;
169 switch (D->getKind()) {
171 Info.Kind = SymbolKind::Module;
174 Info.Kind = SymbolKind::TypeAlias; break; // Lang = C
176 Info.Kind = SymbolKind::Function;
179 case Decl::IndirectField:
180 Info.Kind = SymbolKind::Field;
181 if (const CXXRecordDecl *
182 CXXRec = dyn_cast<CXXRecordDecl>(D->getDeclContext())) {
183 if (!CXXRec->isCLike())
184 Info.Lang = SymbolLanguage::CXX;
187 case Decl::EnumConstant:
188 Info.Kind = SymbolKind::EnumConstant; break;
189 case Decl::ObjCInterface:
190 case Decl::ObjCImplementation: {
191 Info.Kind = SymbolKind::Class;
192 Info.Lang = SymbolLanguage::ObjC;
193 const ObjCInterfaceDecl *ClsD = dyn_cast<ObjCInterfaceDecl>(D);
195 ClsD = cast<ObjCImplementationDecl>(D)->getClassInterface();
196 if (isUnitTestCase(ClsD))
197 Info.Properties |= (SymbolPropertySet)SymbolProperty::UnitTest;
200 case Decl::ObjCProtocol:
201 Info.Kind = SymbolKind::Protocol;
202 Info.Lang = SymbolLanguage::ObjC;
204 case Decl::ObjCCategory:
205 case Decl::ObjCCategoryImpl: {
206 Info.Kind = SymbolKind::Extension;
207 Info.Lang = SymbolLanguage::ObjC;
208 const ObjCInterfaceDecl *ClsD = nullptr;
209 if (auto *CatD = dyn_cast<ObjCCategoryDecl>(D))
210 ClsD = CatD->getClassInterface();
212 ClsD = cast<ObjCCategoryImplDecl>(D)->getClassInterface();
213 if (isUnitTestCase(ClsD))
214 Info.Properties |= (SymbolPropertySet)SymbolProperty::UnitTest;
217 case Decl::ObjCMethod: {
218 const ObjCMethodDecl *MD = cast<ObjCMethodDecl>(D);
219 Info.Kind = MD->isInstanceMethod() ? SymbolKind::InstanceMethod : SymbolKind::ClassMethod;
220 if (MD->isPropertyAccessor()) {
221 if (MD->param_size())
222 Info.SubKind = SymbolSubKind::AccessorSetter;
224 Info.SubKind = SymbolSubKind::AccessorGetter;
226 Info.Lang = SymbolLanguage::ObjC;
228 Info.Properties |= (SymbolPropertySet)SymbolProperty::UnitTest;
229 if (D->hasAttr<IBActionAttr>())
230 Info.Properties |= (SymbolPropertySet)SymbolProperty::IBAnnotated;
233 case Decl::ObjCProperty:
234 Info.Kind = SymbolKind::InstanceProperty;
235 Info.Lang = SymbolLanguage::ObjC;
236 checkForIBOutlets(D, Info.Properties);
237 if (auto *Annot = D->getAttr<AnnotateAttr>()) {
238 if (Annot->getAnnotation() == "gk_inspectable")
239 Info.Properties |= (SymbolPropertySet)SymbolProperty::GKInspectable;
243 Info.Kind = SymbolKind::Field;
244 Info.Lang = SymbolLanguage::ObjC;
245 checkForIBOutlets(D, Info.Properties);
247 case Decl::Namespace:
248 Info.Kind = SymbolKind::Namespace;
249 Info.Lang = SymbolLanguage::CXX;
251 case Decl::NamespaceAlias:
252 Info.Kind = SymbolKind::NamespaceAlias;
253 Info.Lang = SymbolLanguage::CXX;
255 case Decl::CXXConstructor: {
256 Info.Kind = SymbolKind::Constructor;
257 Info.Lang = SymbolLanguage::CXX;
258 auto *CD = cast<CXXConstructorDecl>(D);
259 if (CD->isCopyConstructor())
260 Info.SubKind = SymbolSubKind::CXXCopyConstructor;
261 else if (CD->isMoveConstructor())
262 Info.SubKind = SymbolSubKind::CXXMoveConstructor;
265 case Decl::CXXDestructor:
266 Info.Kind = SymbolKind::Destructor;
267 Info.Lang = SymbolLanguage::CXX;
269 case Decl::CXXConversion:
270 Info.Kind = SymbolKind::ConversionFunction;
271 Info.Lang = SymbolLanguage::CXX;
273 case Decl::CXXMethod: {
274 const CXXMethodDecl *MD = cast<CXXMethodDecl>(D);
276 Info.Kind = SymbolKind::StaticMethod;
278 Info.Kind = SymbolKind::InstanceMethod;
279 Info.Lang = SymbolLanguage::CXX;
282 case Decl::ClassTemplate:
283 Info.Kind = SymbolKind::Class;
284 Info.Properties |= (SymbolPropertySet)SymbolProperty::Generic;
285 Info.Lang = SymbolLanguage::CXX;
287 case Decl::FunctionTemplate:
288 Info.Kind = SymbolKind::Function;
289 Info.Properties |= (SymbolPropertySet)SymbolProperty::Generic;
290 Info.Lang = SymbolLanguage::CXX;
291 if (const CXXMethodDecl *MD = dyn_cast_or_null<CXXMethodDecl>(
292 cast<FunctionTemplateDecl>(D)->getTemplatedDecl())) {
293 if (isa<CXXConstructorDecl>(MD))
294 Info.Kind = SymbolKind::Constructor;
295 else if (isa<CXXDestructorDecl>(MD))
296 Info.Kind = SymbolKind::Destructor;
297 else if (isa<CXXConversionDecl>(MD))
298 Info.Kind = SymbolKind::ConversionFunction;
301 Info.Kind = SymbolKind::StaticMethod;
303 Info.Kind = SymbolKind::InstanceMethod;
307 case Decl::TypeAliasTemplate:
308 Info.Kind = SymbolKind::TypeAlias;
309 Info.Lang = SymbolLanguage::CXX;
310 Info.Properties |= (SymbolPropertySet)SymbolProperty::Generic;
312 case Decl::TypeAlias:
313 Info.Kind = SymbolKind::TypeAlias;
314 Info.Lang = SymbolLanguage::CXX;
316 case Decl::UnresolvedUsingTypename:
317 Info.Kind = SymbolKind::Using;
318 Info.SubKind = SymbolSubKind::UsingTypename;
319 Info.Lang = SymbolLanguage::CXX;
320 Info.Properties |= (SymbolPropertySet)SymbolProperty::Generic;
322 case Decl::UnresolvedUsingValue:
323 Info.Kind = SymbolKind::Using;
324 Info.SubKind = SymbolSubKind::UsingValue;
325 Info.Lang = SymbolLanguage::CXX;
326 Info.Properties |= (SymbolPropertySet)SymbolProperty::Generic;
329 Info.Kind = SymbolKind::Using;
330 Info.Lang = SymbolLanguage::CXX;
333 Info.Kind = SymbolKind::Variable;
334 Info.Lang = SymbolLanguage::CXX;
336 case Decl::MSProperty:
337 Info.Kind = SymbolKind::InstanceProperty;
338 if (const CXXRecordDecl *CXXRec =
339 dyn_cast<CXXRecordDecl>(D->getDeclContext())) {
340 if (!CXXRec->isCLike())
341 Info.Lang = SymbolLanguage::CXX;
344 case Decl::ClassTemplatePartialSpecialization:
345 case Decl::ClassScopeFunctionSpecialization:
346 case Decl::ClassTemplateSpecialization:
347 case Decl::CXXRecord:
350 llvm_unreachable("records handled before");
352 case Decl::VarTemplateSpecialization:
353 case Decl::VarTemplatePartialSpecialization:
354 case Decl::ImplicitParam:
357 case Decl::VarTemplate:
358 llvm_unreachable("variables handled before");
360 // Other decls get the 'unknown' kind.
366 if (Info.Kind == SymbolKind::Unknown)
369 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
370 if (FD->getTemplatedKind() ==
371 FunctionDecl::TK_FunctionTemplateSpecialization) {
372 Info.Properties |= (SymbolPropertySet)SymbolProperty::Generic;
374 (SymbolPropertySet)SymbolProperty::TemplateSpecialization;
378 if (Info.Properties & (SymbolPropertySet)SymbolProperty::Generic)
379 Info.Lang = SymbolLanguage::CXX;
381 if (auto *attr = D->getExternalSourceSymbolAttr()) {
382 if (attr->getLanguage() == "Swift")
383 Info.Lang = SymbolLanguage::Swift;
389 SymbolInfo index::getSymbolInfoForMacro(const MacroInfo &) {
391 Info.Kind = SymbolKind::Macro;
392 Info.SubKind = SymbolSubKind::None;
393 Info.Properties = SymbolPropertySet();
394 Info.Lang = SymbolLanguage::C;
398 bool index::applyForEachSymbolRoleInterruptible(SymbolRoleSet Roles,
399 llvm::function_ref<bool(SymbolRole)> Fn) {
400 #define APPLY_FOR_ROLE(Role) \
401 if (Roles & (unsigned)SymbolRole::Role) \
402 if (!Fn(SymbolRole::Role)) \
405 APPLY_FOR_ROLE(Declaration);
406 APPLY_FOR_ROLE(Definition);
407 APPLY_FOR_ROLE(Reference);
408 APPLY_FOR_ROLE(Read);
409 APPLY_FOR_ROLE(Write);
410 APPLY_FOR_ROLE(Call);
411 APPLY_FOR_ROLE(Dynamic);
412 APPLY_FOR_ROLE(AddressOf);
413 APPLY_FOR_ROLE(Implicit);
414 APPLY_FOR_ROLE(Undefinition);
415 APPLY_FOR_ROLE(RelationChildOf);
416 APPLY_FOR_ROLE(RelationBaseOf);
417 APPLY_FOR_ROLE(RelationOverrideOf);
418 APPLY_FOR_ROLE(RelationReceivedBy);
419 APPLY_FOR_ROLE(RelationCalledBy);
420 APPLY_FOR_ROLE(RelationExtendedBy);
421 APPLY_FOR_ROLE(RelationAccessorOf);
422 APPLY_FOR_ROLE(RelationContainedBy);
423 APPLY_FOR_ROLE(RelationIBTypeOf);
424 APPLY_FOR_ROLE(RelationSpecializationOf);
425 APPLY_FOR_ROLE(NameReference);
427 #undef APPLY_FOR_ROLE
432 void index::applyForEachSymbolRole(SymbolRoleSet Roles,
433 llvm::function_ref<void(SymbolRole)> Fn) {
434 applyForEachSymbolRoleInterruptible(Roles, [&](SymbolRole r) -> bool {
440 void index::printSymbolRoles(SymbolRoleSet Roles, raw_ostream &OS) {
441 bool VisitedOnce = false;
442 applyForEachSymbolRole(Roles, [&](SymbolRole Role) {
448 case SymbolRole::Declaration: OS << "Decl"; break;
449 case SymbolRole::Definition: OS << "Def"; break;
450 case SymbolRole::Reference: OS << "Ref"; break;
451 case SymbolRole::Read: OS << "Read"; break;
452 case SymbolRole::Write: OS << "Writ"; break;
453 case SymbolRole::Call: OS << "Call"; break;
454 case SymbolRole::Dynamic: OS << "Dyn"; break;
455 case SymbolRole::AddressOf: OS << "Addr"; break;
456 case SymbolRole::Implicit: OS << "Impl"; break;
457 case SymbolRole::Undefinition: OS << "Undef"; break;
458 case SymbolRole::RelationChildOf: OS << "RelChild"; break;
459 case SymbolRole::RelationBaseOf: OS << "RelBase"; break;
460 case SymbolRole::RelationOverrideOf: OS << "RelOver"; break;
461 case SymbolRole::RelationReceivedBy: OS << "RelRec"; break;
462 case SymbolRole::RelationCalledBy: OS << "RelCall"; break;
463 case SymbolRole::RelationExtendedBy: OS << "RelExt"; break;
464 case SymbolRole::RelationAccessorOf: OS << "RelAcc"; break;
465 case SymbolRole::RelationContainedBy: OS << "RelCont"; break;
466 case SymbolRole::RelationIBTypeOf: OS << "RelIBType"; break;
467 case SymbolRole::RelationSpecializationOf: OS << "RelSpecialization"; break;
468 case SymbolRole::NameReference: OS << "NameReference"; break;
473 bool index::printSymbolName(const Decl *D, const LangOptions &LO,
475 if (auto *ND = dyn_cast<NamedDecl>(D)) {
476 PrintingPolicy Policy(LO);
477 // Forward references can have different template argument names. Suppress
478 // the template argument names in constructors to make their name more
480 Policy.SuppressTemplateArgsInCXXConstructors = true;
481 DeclarationName DeclName = ND->getDeclName();
482 if (DeclName.isEmpty())
484 DeclName.print(OS, Policy);
491 StringRef index::getSymbolKindString(SymbolKind K) {
493 case SymbolKind::Unknown: return "<unknown>";
494 case SymbolKind::Module: return "module";
495 case SymbolKind::Namespace: return "namespace";
496 case SymbolKind::NamespaceAlias: return "namespace-alias";
497 case SymbolKind::Macro: return "macro";
498 case SymbolKind::Enum: return "enum";
499 case SymbolKind::Struct: return "struct";
500 case SymbolKind::Class: return "class";
501 case SymbolKind::Protocol: return "protocol";
502 case SymbolKind::Extension: return "extension";
503 case SymbolKind::Union: return "union";
504 case SymbolKind::TypeAlias: return "type-alias";
505 case SymbolKind::Function: return "function";
506 case SymbolKind::Variable: return "variable";
507 case SymbolKind::Field: return "field";
508 case SymbolKind::EnumConstant: return "enumerator";
509 case SymbolKind::InstanceMethod: return "instance-method";
510 case SymbolKind::ClassMethod: return "class-method";
511 case SymbolKind::StaticMethod: return "static-method";
512 case SymbolKind::InstanceProperty: return "instance-property";
513 case SymbolKind::ClassProperty: return "class-property";
514 case SymbolKind::StaticProperty: return "static-property";
515 case SymbolKind::Constructor: return "constructor";
516 case SymbolKind::Destructor: return "destructor";
517 case SymbolKind::ConversionFunction: return "conversion-func";
518 case SymbolKind::Parameter: return "param";
519 case SymbolKind::Using: return "using";
521 llvm_unreachable("invalid symbol kind");
524 StringRef index::getSymbolSubKindString(SymbolSubKind K) {
526 case SymbolSubKind::None: return "<none>";
527 case SymbolSubKind::CXXCopyConstructor: return "cxx-copy-ctor";
528 case SymbolSubKind::CXXMoveConstructor: return "cxx-move-ctor";
529 case SymbolSubKind::AccessorGetter: return "acc-get";
530 case SymbolSubKind::AccessorSetter: return "acc-set";
531 case SymbolSubKind::UsingTypename: return "using-typename";
532 case SymbolSubKind::UsingValue: return "using-value";
534 llvm_unreachable("invalid symbol subkind");
537 StringRef index::getSymbolLanguageString(SymbolLanguage K) {
539 case SymbolLanguage::C: return "C";
540 case SymbolLanguage::ObjC: return "ObjC";
541 case SymbolLanguage::CXX: return "C++";
542 case SymbolLanguage::Swift: return "Swift";
544 llvm_unreachable("invalid symbol language kind");
547 void index::applyForEachSymbolProperty(SymbolPropertySet Props,
548 llvm::function_ref<void(SymbolProperty)> Fn) {
549 #define APPLY_FOR_PROPERTY(K) \
550 if (Props & (SymbolPropertySet)SymbolProperty::K) \
551 Fn(SymbolProperty::K)
553 APPLY_FOR_PROPERTY(Generic);
554 APPLY_FOR_PROPERTY(TemplatePartialSpecialization);
555 APPLY_FOR_PROPERTY(TemplateSpecialization);
556 APPLY_FOR_PROPERTY(UnitTest);
557 APPLY_FOR_PROPERTY(IBAnnotated);
558 APPLY_FOR_PROPERTY(IBOutletCollection);
559 APPLY_FOR_PROPERTY(GKInspectable);
560 APPLY_FOR_PROPERTY(Local);
561 APPLY_FOR_PROPERTY(ProtocolInterface);
563 #undef APPLY_FOR_PROPERTY
566 void index::printSymbolProperties(SymbolPropertySet Props, raw_ostream &OS) {
567 bool VisitedOnce = false;
568 applyForEachSymbolProperty(Props, [&](SymbolProperty Prop) {
574 case SymbolProperty::Generic: OS << "Gen"; break;
575 case SymbolProperty::TemplatePartialSpecialization: OS << "TPS"; break;
576 case SymbolProperty::TemplateSpecialization: OS << "TS"; break;
577 case SymbolProperty::UnitTest: OS << "test"; break;
578 case SymbolProperty::IBAnnotated: OS << "IB"; break;
579 case SymbolProperty::IBOutletCollection: OS << "IBColl"; break;
580 case SymbolProperty::GKInspectable: OS << "GKI"; break;
581 case SymbolProperty::Local: OS << "local"; break;
582 case SymbolProperty::ProtocolInterface: OS << "protocol"; break;