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"
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<TemplateTemplateParmDecl>(D))
62 if (isa<ObjCTypeParamDecl>(D))
65 if (isa<UsingDirectiveDecl>(D))
67 if (!D->getParentFunctionOrMethod())
70 if (const NamedDecl *ND = dyn_cast<NamedDecl>(D)) {
71 switch (ND->getFormalLinkage()) {
75 case VisibleNoLinkage:
76 case UniqueExternalLinkage:
77 case ModuleInternalLinkage:
78 llvm_unreachable("Not a sema linkage");
88 SymbolInfo index::getSymbolInfo(const Decl *D) {
91 Info.Kind = SymbolKind::Unknown;
92 Info.SubKind = SymbolSubKind::None;
93 Info.Properties = SymbolPropertySet();
94 Info.Lang = SymbolLanguage::C;
96 if (isFunctionLocalSymbol(D)) {
97 Info.Properties |= (SymbolPropertySet)SymbolProperty::Local;
99 if (isa<ObjCProtocolDecl>(D->getDeclContext())) {
100 Info.Properties |= (SymbolPropertySet)SymbolProperty::ProtocolInterface;
103 if (const TagDecl *TD = dyn_cast<TagDecl>(D)) {
104 switch (TD->getTagKind()) {
106 Info.Kind = SymbolKind::Struct; break;
108 Info.Kind = SymbolKind::Union; break;
110 Info.Kind = SymbolKind::Class;
111 Info.Lang = SymbolLanguage::CXX;
114 Info.Kind = SymbolKind::Protocol;
115 Info.Lang = SymbolLanguage::CXX;
118 Info.Kind = SymbolKind::Enum; break;
121 if (const CXXRecordDecl *CXXRec = dyn_cast<CXXRecordDecl>(D)) {
122 if (!CXXRec->isCLike()) {
123 Info.Lang = SymbolLanguage::CXX;
124 if (CXXRec->getDescribedClassTemplate()) {
125 Info.Properties |= (SymbolPropertySet)SymbolProperty::Generic;
130 if (isa<ClassTemplatePartialSpecializationDecl>(D)) {
131 Info.Properties |= (SymbolPropertySet)SymbolProperty::Generic;
133 (SymbolPropertySet)SymbolProperty::TemplatePartialSpecialization;
134 } else if (isa<ClassTemplateSpecializationDecl>(D)) {
135 Info.Properties |= (SymbolPropertySet)SymbolProperty::Generic;
137 (SymbolPropertySet)SymbolProperty::TemplateSpecialization;
140 } else if (auto *VD = dyn_cast<VarDecl>(D)) {
141 Info.Kind = SymbolKind::Variable;
142 if (isa<ParmVarDecl>(D)) {
143 Info.Kind = SymbolKind::Parameter;
144 } else if (isa<CXXRecordDecl>(D->getDeclContext())) {
145 Info.Kind = SymbolKind::StaticProperty;
146 Info.Lang = SymbolLanguage::CXX;
149 if (isa<VarTemplatePartialSpecializationDecl>(D)) {
150 Info.Lang = SymbolLanguage::CXX;
151 Info.Properties |= (SymbolPropertySet)SymbolProperty::Generic;
153 (SymbolPropertySet)SymbolProperty::TemplatePartialSpecialization;
154 } else if (isa<VarTemplateSpecializationDecl>(D)) {
155 Info.Lang = SymbolLanguage::CXX;
156 Info.Properties |= (SymbolPropertySet)SymbolProperty::Generic;
158 (SymbolPropertySet)SymbolProperty::TemplateSpecialization;
159 } else if (VD->getDescribedVarTemplate()) {
160 Info.Lang = SymbolLanguage::CXX;
161 Info.Properties |= (SymbolPropertySet)SymbolProperty::Generic;
165 switch (D->getKind()) {
167 Info.Kind = SymbolKind::Module;
170 Info.Kind = SymbolKind::TypeAlias; break; // Lang = C
172 Info.Kind = SymbolKind::Function;
175 Info.Kind = SymbolKind::Field;
176 if (const CXXRecordDecl *
177 CXXRec = dyn_cast<CXXRecordDecl>(D->getDeclContext())) {
178 if (!CXXRec->isCLike())
179 Info.Lang = SymbolLanguage::CXX;
182 case Decl::EnumConstant:
183 Info.Kind = SymbolKind::EnumConstant; break;
184 case Decl::ObjCInterface:
185 case Decl::ObjCImplementation: {
186 Info.Kind = SymbolKind::Class;
187 Info.Lang = SymbolLanguage::ObjC;
188 const ObjCInterfaceDecl *ClsD = dyn_cast<ObjCInterfaceDecl>(D);
190 ClsD = cast<ObjCImplementationDecl>(D)->getClassInterface();
191 if (isUnitTestCase(ClsD))
192 Info.Properties |= (SymbolPropertySet)SymbolProperty::UnitTest;
195 case Decl::ObjCProtocol:
196 Info.Kind = SymbolKind::Protocol;
197 Info.Lang = SymbolLanguage::ObjC;
199 case Decl::ObjCCategory:
200 case Decl::ObjCCategoryImpl: {
201 Info.Kind = SymbolKind::Extension;
202 Info.Lang = SymbolLanguage::ObjC;
203 const ObjCInterfaceDecl *ClsD = nullptr;
204 if (auto *CatD = dyn_cast<ObjCCategoryDecl>(D))
205 ClsD = CatD->getClassInterface();
207 ClsD = cast<ObjCCategoryImplDecl>(D)->getClassInterface();
208 if (isUnitTestCase(ClsD))
209 Info.Properties |= (SymbolPropertySet)SymbolProperty::UnitTest;
212 case Decl::ObjCMethod: {
213 const ObjCMethodDecl *MD = cast<ObjCMethodDecl>(D);
214 Info.Kind = MD->isInstanceMethod() ? SymbolKind::InstanceMethod : SymbolKind::ClassMethod;
215 if (MD->isPropertyAccessor()) {
216 if (MD->param_size())
217 Info.SubKind = SymbolSubKind::AccessorSetter;
219 Info.SubKind = SymbolSubKind::AccessorGetter;
221 Info.Lang = SymbolLanguage::ObjC;
223 Info.Properties |= (SymbolPropertySet)SymbolProperty::UnitTest;
224 if (D->hasAttr<IBActionAttr>())
225 Info.Properties |= (SymbolPropertySet)SymbolProperty::IBAnnotated;
228 case Decl::ObjCProperty:
229 Info.Kind = SymbolKind::InstanceProperty;
230 Info.Lang = SymbolLanguage::ObjC;
231 checkForIBOutlets(D, Info.Properties);
232 if (auto *Annot = D->getAttr<AnnotateAttr>()) {
233 if (Annot->getAnnotation() == "gk_inspectable")
234 Info.Properties |= (SymbolPropertySet)SymbolProperty::GKInspectable;
238 Info.Kind = SymbolKind::Field;
239 Info.Lang = SymbolLanguage::ObjC;
240 checkForIBOutlets(D, Info.Properties);
242 case Decl::Namespace:
243 Info.Kind = SymbolKind::Namespace;
244 Info.Lang = SymbolLanguage::CXX;
246 case Decl::NamespaceAlias:
247 Info.Kind = SymbolKind::NamespaceAlias;
248 Info.Lang = SymbolLanguage::CXX;
250 case Decl::CXXConstructor: {
251 Info.Kind = SymbolKind::Constructor;
252 Info.Lang = SymbolLanguage::CXX;
253 auto *CD = cast<CXXConstructorDecl>(D);
254 if (CD->isCopyConstructor())
255 Info.SubKind = SymbolSubKind::CXXCopyConstructor;
256 else if (CD->isMoveConstructor())
257 Info.SubKind = SymbolSubKind::CXXMoveConstructor;
260 case Decl::CXXDestructor:
261 Info.Kind = SymbolKind::Destructor;
262 Info.Lang = SymbolLanguage::CXX;
264 case Decl::CXXConversion:
265 Info.Kind = SymbolKind::ConversionFunction;
266 Info.Lang = SymbolLanguage::CXX;
268 case Decl::CXXMethod: {
269 const CXXMethodDecl *MD = cast<CXXMethodDecl>(D);
271 Info.Kind = SymbolKind::StaticMethod;
273 Info.Kind = SymbolKind::InstanceMethod;
274 Info.Lang = SymbolLanguage::CXX;
277 case Decl::ClassTemplate:
278 Info.Kind = SymbolKind::Class;
279 Info.Properties |= (SymbolPropertySet)SymbolProperty::Generic;
280 Info.Lang = SymbolLanguage::CXX;
282 case Decl::FunctionTemplate:
283 Info.Kind = SymbolKind::Function;
284 Info.Properties |= (SymbolPropertySet)SymbolProperty::Generic;
285 Info.Lang = SymbolLanguage::CXX;
286 if (const CXXMethodDecl *MD = dyn_cast_or_null<CXXMethodDecl>(
287 cast<FunctionTemplateDecl>(D)->getTemplatedDecl())) {
288 if (isa<CXXConstructorDecl>(MD))
289 Info.Kind = SymbolKind::Constructor;
290 else if (isa<CXXDestructorDecl>(MD))
291 Info.Kind = SymbolKind::Destructor;
292 else if (isa<CXXConversionDecl>(MD))
293 Info.Kind = SymbolKind::ConversionFunction;
296 Info.Kind = SymbolKind::StaticMethod;
298 Info.Kind = SymbolKind::InstanceMethod;
302 case Decl::TypeAliasTemplate:
303 Info.Kind = SymbolKind::TypeAlias;
304 Info.Lang = SymbolLanguage::CXX;
305 Info.Properties |= (SymbolPropertySet)SymbolProperty::Generic;
307 case Decl::TypeAlias:
308 Info.Kind = SymbolKind::TypeAlias;
309 Info.Lang = SymbolLanguage::CXX;
311 case Decl::UnresolvedUsingTypename:
312 Info.Kind = SymbolKind::Using;
313 Info.SubKind = SymbolSubKind::UsingTypename;
314 Info.Lang = SymbolLanguage::CXX;
315 Info.Properties |= (SymbolPropertySet)SymbolProperty::Generic;
317 case Decl::UnresolvedUsingValue:
318 Info.Kind = SymbolKind::Using;
319 Info.SubKind = SymbolSubKind::UsingValue;
320 Info.Lang = SymbolLanguage::CXX;
321 Info.Properties |= (SymbolPropertySet)SymbolProperty::Generic;
324 Info.Kind = SymbolKind::Variable;
325 Info.Lang = SymbolLanguage::CXX;
332 if (Info.Kind == SymbolKind::Unknown)
335 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
336 if (FD->getTemplatedKind() ==
337 FunctionDecl::TK_FunctionTemplateSpecialization) {
338 Info.Properties |= (SymbolPropertySet)SymbolProperty::Generic;
340 (SymbolPropertySet)SymbolProperty::TemplateSpecialization;
344 if (Info.Properties & (SymbolPropertySet)SymbolProperty::Generic)
345 Info.Lang = SymbolLanguage::CXX;
347 if (auto *attr = D->getExternalSourceSymbolAttr()) {
348 if (attr->getLanguage() == "Swift")
349 Info.Lang = SymbolLanguage::Swift;
355 SymbolInfo index::getSymbolInfoForMacro(const MacroInfo &) {
357 Info.Kind = SymbolKind::Macro;
358 Info.SubKind = SymbolSubKind::None;
359 Info.Properties = SymbolPropertySet();
360 Info.Lang = SymbolLanguage::C;
364 bool index::applyForEachSymbolRoleInterruptible(SymbolRoleSet Roles,
365 llvm::function_ref<bool(SymbolRole)> Fn) {
366 #define APPLY_FOR_ROLE(Role) \
367 if (Roles & (unsigned)SymbolRole::Role) \
368 if (!Fn(SymbolRole::Role)) \
371 APPLY_FOR_ROLE(Declaration);
372 APPLY_FOR_ROLE(Definition);
373 APPLY_FOR_ROLE(Reference);
374 APPLY_FOR_ROLE(Read);
375 APPLY_FOR_ROLE(Write);
376 APPLY_FOR_ROLE(Call);
377 APPLY_FOR_ROLE(Dynamic);
378 APPLY_FOR_ROLE(AddressOf);
379 APPLY_FOR_ROLE(Implicit);
380 APPLY_FOR_ROLE(Undefinition);
381 APPLY_FOR_ROLE(RelationChildOf);
382 APPLY_FOR_ROLE(RelationBaseOf);
383 APPLY_FOR_ROLE(RelationOverrideOf);
384 APPLY_FOR_ROLE(RelationReceivedBy);
385 APPLY_FOR_ROLE(RelationCalledBy);
386 APPLY_FOR_ROLE(RelationExtendedBy);
387 APPLY_FOR_ROLE(RelationAccessorOf);
388 APPLY_FOR_ROLE(RelationContainedBy);
389 APPLY_FOR_ROLE(RelationIBTypeOf);
390 APPLY_FOR_ROLE(RelationSpecializationOf);
392 #undef APPLY_FOR_ROLE
397 void index::applyForEachSymbolRole(SymbolRoleSet Roles,
398 llvm::function_ref<void(SymbolRole)> Fn) {
399 applyForEachSymbolRoleInterruptible(Roles, [&](SymbolRole r) -> bool {
405 void index::printSymbolRoles(SymbolRoleSet Roles, raw_ostream &OS) {
406 bool VisitedOnce = false;
407 applyForEachSymbolRole(Roles, [&](SymbolRole Role) {
413 case SymbolRole::Declaration: OS << "Decl"; break;
414 case SymbolRole::Definition: OS << "Def"; break;
415 case SymbolRole::Reference: OS << "Ref"; break;
416 case SymbolRole::Read: OS << "Read"; break;
417 case SymbolRole::Write: OS << "Writ"; break;
418 case SymbolRole::Call: OS << "Call"; break;
419 case SymbolRole::Dynamic: OS << "Dyn"; break;
420 case SymbolRole::AddressOf: OS << "Addr"; break;
421 case SymbolRole::Implicit: OS << "Impl"; break;
422 case SymbolRole::Undefinition: OS << "Undef"; break;
423 case SymbolRole::RelationChildOf: OS << "RelChild"; break;
424 case SymbolRole::RelationBaseOf: OS << "RelBase"; break;
425 case SymbolRole::RelationOverrideOf: OS << "RelOver"; break;
426 case SymbolRole::RelationReceivedBy: OS << "RelRec"; break;
427 case SymbolRole::RelationCalledBy: OS << "RelCall"; break;
428 case SymbolRole::RelationExtendedBy: OS << "RelExt"; break;
429 case SymbolRole::RelationAccessorOf: OS << "RelAcc"; break;
430 case SymbolRole::RelationContainedBy: OS << "RelCont"; break;
431 case SymbolRole::RelationIBTypeOf: OS << "RelIBType"; break;
432 case SymbolRole::RelationSpecializationOf: OS << "RelSpecialization"; break;
437 bool index::printSymbolName(const Decl *D, const LangOptions &LO,
439 if (auto *ND = dyn_cast<NamedDecl>(D)) {
440 PrintingPolicy Policy(LO);
441 // Forward references can have different template argument names. Suppress
442 // the template argument names in constructors to make their name more
444 Policy.SuppressTemplateArgsInCXXConstructors = true;
445 DeclarationName DeclName = ND->getDeclName();
446 if (DeclName.isEmpty())
448 DeclName.print(OS, Policy);
455 StringRef index::getSymbolKindString(SymbolKind K) {
457 case SymbolKind::Unknown: return "<unknown>";
458 case SymbolKind::Module: return "module";
459 case SymbolKind::Namespace: return "namespace";
460 case SymbolKind::NamespaceAlias: return "namespace-alias";
461 case SymbolKind::Macro: return "macro";
462 case SymbolKind::Enum: return "enum";
463 case SymbolKind::Struct: return "struct";
464 case SymbolKind::Class: return "class";
465 case SymbolKind::Protocol: return "protocol";
466 case SymbolKind::Extension: return "extension";
467 case SymbolKind::Union: return "union";
468 case SymbolKind::TypeAlias: return "type-alias";
469 case SymbolKind::Function: return "function";
470 case SymbolKind::Variable: return "variable";
471 case SymbolKind::Field: return "field";
472 case SymbolKind::EnumConstant: return "enumerator";
473 case SymbolKind::InstanceMethod: return "instance-method";
474 case SymbolKind::ClassMethod: return "class-method";
475 case SymbolKind::StaticMethod: return "static-method";
476 case SymbolKind::InstanceProperty: return "instance-property";
477 case SymbolKind::ClassProperty: return "class-property";
478 case SymbolKind::StaticProperty: return "static-property";
479 case SymbolKind::Constructor: return "constructor";
480 case SymbolKind::Destructor: return "destructor";
481 case SymbolKind::ConversionFunction: return "coversion-func";
482 case SymbolKind::Parameter: return "param";
483 case SymbolKind::Using: return "using";
485 llvm_unreachable("invalid symbol kind");
488 StringRef index::getSymbolSubKindString(SymbolSubKind 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";
498 llvm_unreachable("invalid symbol subkind");
501 StringRef index::getSymbolLanguageString(SymbolLanguage K) {
503 case SymbolLanguage::C: return "C";
504 case SymbolLanguage::ObjC: return "ObjC";
505 case SymbolLanguage::CXX: return "C++";
506 case SymbolLanguage::Swift: return "Swift";
508 llvm_unreachable("invalid symbol language kind");
511 void index::applyForEachSymbolProperty(SymbolPropertySet Props,
512 llvm::function_ref<void(SymbolProperty)> Fn) {
513 #define APPLY_FOR_PROPERTY(K) \
514 if (Props & (SymbolPropertySet)SymbolProperty::K) \
515 Fn(SymbolProperty::K)
517 APPLY_FOR_PROPERTY(Generic);
518 APPLY_FOR_PROPERTY(TemplatePartialSpecialization);
519 APPLY_FOR_PROPERTY(TemplateSpecialization);
520 APPLY_FOR_PROPERTY(UnitTest);
521 APPLY_FOR_PROPERTY(IBAnnotated);
522 APPLY_FOR_PROPERTY(IBOutletCollection);
523 APPLY_FOR_PROPERTY(GKInspectable);
524 APPLY_FOR_PROPERTY(Local);
525 APPLY_FOR_PROPERTY(ProtocolInterface);
527 #undef APPLY_FOR_PROPERTY
530 void index::printSymbolProperties(SymbolPropertySet Props, raw_ostream &OS) {
531 bool VisitedOnce = false;
532 applyForEachSymbolProperty(Props, [&](SymbolProperty Prop) {
538 case SymbolProperty::Generic: OS << "Gen"; break;
539 case SymbolProperty::TemplatePartialSpecialization: OS << "TPS"; break;
540 case SymbolProperty::TemplateSpecialization: OS << "TS"; break;
541 case SymbolProperty::UnitTest: OS << "test"; break;
542 case SymbolProperty::IBAnnotated: OS << "IB"; break;
543 case SymbolProperty::IBOutletCollection: OS << "IBColl"; break;
544 case SymbolProperty::GKInspectable: OS << "GKI"; break;
545 case SymbolProperty::Local: OS << "local"; break;
546 case SymbolProperty::ProtocolInterface: OS << "protocol"; break;