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;
100 if (const TagDecl *TD = dyn_cast<TagDecl>(D)) {
101 switch (TD->getTagKind()) {
103 Info.Kind = SymbolKind::Struct; break;
105 Info.Kind = SymbolKind::Union; break;
107 Info.Kind = SymbolKind::Class;
108 Info.Lang = SymbolLanguage::CXX;
111 Info.Kind = SymbolKind::Protocol;
112 Info.Lang = SymbolLanguage::CXX;
115 Info.Kind = SymbolKind::Enum; break;
118 if (const CXXRecordDecl *CXXRec = dyn_cast<CXXRecordDecl>(D)) {
119 if (!CXXRec->isCLike()) {
120 Info.Lang = SymbolLanguage::CXX;
121 if (CXXRec->getDescribedClassTemplate()) {
122 Info.Properties |= (SymbolPropertySet)SymbolProperty::Generic;
127 if (isa<ClassTemplatePartialSpecializationDecl>(D)) {
128 Info.Properties |= (SymbolPropertySet)SymbolProperty::Generic;
130 (SymbolPropertySet)SymbolProperty::TemplatePartialSpecialization;
131 } else if (isa<ClassTemplateSpecializationDecl>(D)) {
132 Info.Properties |= (SymbolPropertySet)SymbolProperty::Generic;
134 (SymbolPropertySet)SymbolProperty::TemplateSpecialization;
137 } else if (auto *VD = dyn_cast<VarDecl>(D)) {
138 Info.Kind = SymbolKind::Variable;
139 if (isa<ParmVarDecl>(D)) {
140 Info.Kind = SymbolKind::Parameter;
141 } else if (isa<CXXRecordDecl>(D->getDeclContext())) {
142 Info.Kind = SymbolKind::StaticProperty;
143 Info.Lang = SymbolLanguage::CXX;
146 if (isa<VarTemplatePartialSpecializationDecl>(D)) {
147 Info.Lang = SymbolLanguage::CXX;
148 Info.Properties |= (SymbolPropertySet)SymbolProperty::Generic;
150 (SymbolPropertySet)SymbolProperty::TemplatePartialSpecialization;
151 } else if (isa<VarTemplateSpecializationDecl>(D)) {
152 Info.Lang = SymbolLanguage::CXX;
153 Info.Properties |= (SymbolPropertySet)SymbolProperty::Generic;
155 (SymbolPropertySet)SymbolProperty::TemplateSpecialization;
156 } else if (VD->getDescribedVarTemplate()) {
157 Info.Lang = SymbolLanguage::CXX;
158 Info.Properties |= (SymbolPropertySet)SymbolProperty::Generic;
162 switch (D->getKind()) {
164 Info.Kind = SymbolKind::Module;
167 Info.Kind = SymbolKind::TypeAlias; break; // Lang = C
169 Info.Kind = SymbolKind::Function;
172 Info.Kind = SymbolKind::Field;
173 if (const CXXRecordDecl *
174 CXXRec = dyn_cast<CXXRecordDecl>(D->getDeclContext())) {
175 if (!CXXRec->isCLike())
176 Info.Lang = SymbolLanguage::CXX;
179 case Decl::EnumConstant:
180 Info.Kind = SymbolKind::EnumConstant; break;
181 case Decl::ObjCInterface:
182 case Decl::ObjCImplementation: {
183 Info.Kind = SymbolKind::Class;
184 Info.Lang = SymbolLanguage::ObjC;
185 const ObjCInterfaceDecl *ClsD = dyn_cast<ObjCInterfaceDecl>(D);
187 ClsD = cast<ObjCImplementationDecl>(D)->getClassInterface();
188 if (isUnitTestCase(ClsD))
189 Info.Properties |= (SymbolPropertySet)SymbolProperty::UnitTest;
192 case Decl::ObjCProtocol:
193 Info.Kind = SymbolKind::Protocol;
194 Info.Lang = SymbolLanguage::ObjC;
196 case Decl::ObjCCategory:
197 case Decl::ObjCCategoryImpl: {
198 Info.Kind = SymbolKind::Extension;
199 Info.Lang = SymbolLanguage::ObjC;
200 const ObjCInterfaceDecl *ClsD = nullptr;
201 if (auto *CatD = dyn_cast<ObjCCategoryDecl>(D))
202 ClsD = CatD->getClassInterface();
204 ClsD = cast<ObjCCategoryImplDecl>(D)->getClassInterface();
205 if (isUnitTestCase(ClsD))
206 Info.Properties |= (SymbolPropertySet)SymbolProperty::UnitTest;
209 case Decl::ObjCMethod: {
210 const ObjCMethodDecl *MD = cast<ObjCMethodDecl>(D);
211 Info.Kind = MD->isInstanceMethod() ? SymbolKind::InstanceMethod : SymbolKind::ClassMethod;
212 if (MD->isPropertyAccessor()) {
213 if (MD->param_size())
214 Info.SubKind = SymbolSubKind::AccessorSetter;
216 Info.SubKind = SymbolSubKind::AccessorGetter;
218 Info.Lang = SymbolLanguage::ObjC;
220 Info.Properties |= (SymbolPropertySet)SymbolProperty::UnitTest;
221 if (D->hasAttr<IBActionAttr>())
222 Info.Properties |= (SymbolPropertySet)SymbolProperty::IBAnnotated;
225 case Decl::ObjCProperty:
226 Info.Kind = SymbolKind::InstanceProperty;
227 Info.Lang = SymbolLanguage::ObjC;
228 checkForIBOutlets(D, Info.Properties);
229 if (auto *Annot = D->getAttr<AnnotateAttr>()) {
230 if (Annot->getAnnotation() == "gk_inspectable")
231 Info.Properties |= (SymbolPropertySet)SymbolProperty::GKInspectable;
235 Info.Kind = SymbolKind::Field;
236 Info.Lang = SymbolLanguage::ObjC;
237 checkForIBOutlets(D, Info.Properties);
239 case Decl::Namespace:
240 Info.Kind = SymbolKind::Namespace;
241 Info.Lang = SymbolLanguage::CXX;
243 case Decl::NamespaceAlias:
244 Info.Kind = SymbolKind::NamespaceAlias;
245 Info.Lang = SymbolLanguage::CXX;
247 case Decl::CXXConstructor: {
248 Info.Kind = SymbolKind::Constructor;
249 Info.Lang = SymbolLanguage::CXX;
250 auto *CD = cast<CXXConstructorDecl>(D);
251 if (CD->isCopyConstructor())
252 Info.SubKind = SymbolSubKind::CXXCopyConstructor;
253 else if (CD->isMoveConstructor())
254 Info.SubKind = SymbolSubKind::CXXMoveConstructor;
257 case Decl::CXXDestructor:
258 Info.Kind = SymbolKind::Destructor;
259 Info.Lang = SymbolLanguage::CXX;
261 case Decl::CXXConversion:
262 Info.Kind = SymbolKind::ConversionFunction;
263 Info.Lang = SymbolLanguage::CXX;
265 case Decl::CXXMethod: {
266 const CXXMethodDecl *MD = cast<CXXMethodDecl>(D);
268 Info.Kind = SymbolKind::StaticMethod;
270 Info.Kind = SymbolKind::InstanceMethod;
271 Info.Lang = SymbolLanguage::CXX;
274 case Decl::ClassTemplate:
275 Info.Kind = SymbolKind::Class;
276 Info.Properties |= (SymbolPropertySet)SymbolProperty::Generic;
277 Info.Lang = SymbolLanguage::CXX;
279 case Decl::FunctionTemplate:
280 Info.Kind = SymbolKind::Function;
281 Info.Properties |= (SymbolPropertySet)SymbolProperty::Generic;
282 Info.Lang = SymbolLanguage::CXX;
283 if (const CXXMethodDecl *MD = dyn_cast_or_null<CXXMethodDecl>(
284 cast<FunctionTemplateDecl>(D)->getTemplatedDecl())) {
285 if (isa<CXXConstructorDecl>(MD))
286 Info.Kind = SymbolKind::Constructor;
287 else if (isa<CXXDestructorDecl>(MD))
288 Info.Kind = SymbolKind::Destructor;
289 else if (isa<CXXConversionDecl>(MD))
290 Info.Kind = SymbolKind::ConversionFunction;
293 Info.Kind = SymbolKind::StaticMethod;
295 Info.Kind = SymbolKind::InstanceMethod;
299 case Decl::TypeAliasTemplate:
300 Info.Kind = SymbolKind::TypeAlias;
301 Info.Lang = SymbolLanguage::CXX;
302 Info.Properties |= (SymbolPropertySet)SymbolProperty::Generic;
304 case Decl::TypeAlias:
305 Info.Kind = SymbolKind::TypeAlias;
306 Info.Lang = SymbolLanguage::CXX;
308 case Decl::UnresolvedUsingTypename:
309 Info.Kind = SymbolKind::Using;
310 Info.SubKind = SymbolSubKind::UsingTypename;
311 Info.Lang = SymbolLanguage::CXX;
312 Info.Properties |= (SymbolPropertySet)SymbolProperty::Generic;
314 case Decl::UnresolvedUsingValue:
315 Info.Kind = SymbolKind::Using;
316 Info.SubKind = SymbolSubKind::UsingValue;
317 Info.Lang = SymbolLanguage::CXX;
318 Info.Properties |= (SymbolPropertySet)SymbolProperty::Generic;
321 Info.Kind = SymbolKind::Variable;
322 Info.Lang = SymbolLanguage::CXX;
329 if (Info.Kind == SymbolKind::Unknown)
332 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
333 if (FD->getTemplatedKind() ==
334 FunctionDecl::TK_FunctionTemplateSpecialization) {
335 Info.Properties |= (SymbolPropertySet)SymbolProperty::Generic;
337 (SymbolPropertySet)SymbolProperty::TemplateSpecialization;
341 if (Info.Properties & (SymbolPropertySet)SymbolProperty::Generic)
342 Info.Lang = SymbolLanguage::CXX;
344 if (auto *attr = D->getExternalSourceSymbolAttr()) {
345 if (attr->getLanguage() == "Swift")
346 Info.Lang = SymbolLanguage::Swift;
352 SymbolInfo index::getSymbolInfoForMacro(const MacroInfo &) {
354 Info.Kind = SymbolKind::Macro;
355 Info.SubKind = SymbolSubKind::None;
356 Info.Properties = SymbolPropertySet();
357 Info.Lang = SymbolLanguage::C;
361 bool index::applyForEachSymbolRoleInterruptible(SymbolRoleSet Roles,
362 llvm::function_ref<bool(SymbolRole)> Fn) {
363 #define APPLY_FOR_ROLE(Role) \
364 if (Roles & (unsigned)SymbolRole::Role) \
365 if (!Fn(SymbolRole::Role)) \
368 APPLY_FOR_ROLE(Declaration);
369 APPLY_FOR_ROLE(Definition);
370 APPLY_FOR_ROLE(Reference);
371 APPLY_FOR_ROLE(Read);
372 APPLY_FOR_ROLE(Write);
373 APPLY_FOR_ROLE(Call);
374 APPLY_FOR_ROLE(Dynamic);
375 APPLY_FOR_ROLE(AddressOf);
376 APPLY_FOR_ROLE(Implicit);
377 APPLY_FOR_ROLE(Undefinition);
378 APPLY_FOR_ROLE(RelationChildOf);
379 APPLY_FOR_ROLE(RelationBaseOf);
380 APPLY_FOR_ROLE(RelationOverrideOf);
381 APPLY_FOR_ROLE(RelationReceivedBy);
382 APPLY_FOR_ROLE(RelationCalledBy);
383 APPLY_FOR_ROLE(RelationExtendedBy);
384 APPLY_FOR_ROLE(RelationAccessorOf);
385 APPLY_FOR_ROLE(RelationContainedBy);
386 APPLY_FOR_ROLE(RelationIBTypeOf);
387 APPLY_FOR_ROLE(RelationSpecializationOf);
389 #undef APPLY_FOR_ROLE
394 void index::applyForEachSymbolRole(SymbolRoleSet Roles,
395 llvm::function_ref<void(SymbolRole)> Fn) {
396 applyForEachSymbolRoleInterruptible(Roles, [&](SymbolRole r) -> bool {
402 void index::printSymbolRoles(SymbolRoleSet Roles, raw_ostream &OS) {
403 bool VisitedOnce = false;
404 applyForEachSymbolRole(Roles, [&](SymbolRole Role) {
410 case SymbolRole::Declaration: OS << "Decl"; break;
411 case SymbolRole::Definition: OS << "Def"; break;
412 case SymbolRole::Reference: OS << "Ref"; break;
413 case SymbolRole::Read: OS << "Read"; break;
414 case SymbolRole::Write: OS << "Writ"; break;
415 case SymbolRole::Call: OS << "Call"; break;
416 case SymbolRole::Dynamic: OS << "Dyn"; break;
417 case SymbolRole::AddressOf: OS << "Addr"; break;
418 case SymbolRole::Implicit: OS << "Impl"; break;
419 case SymbolRole::Undefinition: OS << "Undef"; break;
420 case SymbolRole::RelationChildOf: OS << "RelChild"; break;
421 case SymbolRole::RelationBaseOf: OS << "RelBase"; break;
422 case SymbolRole::RelationOverrideOf: OS << "RelOver"; break;
423 case SymbolRole::RelationReceivedBy: OS << "RelRec"; break;
424 case SymbolRole::RelationCalledBy: OS << "RelCall"; break;
425 case SymbolRole::RelationExtendedBy: OS << "RelExt"; break;
426 case SymbolRole::RelationAccessorOf: OS << "RelAcc"; break;
427 case SymbolRole::RelationContainedBy: OS << "RelCont"; break;
428 case SymbolRole::RelationIBTypeOf: OS << "RelIBType"; break;
429 case SymbolRole::RelationSpecializationOf: OS << "RelSpecialization"; break;
434 bool index::printSymbolName(const Decl *D, const LangOptions &LO,
436 if (auto *ND = dyn_cast<NamedDecl>(D)) {
437 PrintingPolicy Policy(LO);
438 // Forward references can have different template argument names. Suppress
439 // the template argument names in constructors to make their name more
441 Policy.SuppressTemplateArgsInCXXConstructors = true;
442 DeclarationName DeclName = ND->getDeclName();
443 if (DeclName.isEmpty())
445 DeclName.print(OS, Policy);
452 StringRef index::getSymbolKindString(SymbolKind K) {
454 case SymbolKind::Unknown: return "<unknown>";
455 case SymbolKind::Module: return "module";
456 case SymbolKind::Namespace: return "namespace";
457 case SymbolKind::NamespaceAlias: return "namespace-alias";
458 case SymbolKind::Macro: return "macro";
459 case SymbolKind::Enum: return "enum";
460 case SymbolKind::Struct: return "struct";
461 case SymbolKind::Class: return "class";
462 case SymbolKind::Protocol: return "protocol";
463 case SymbolKind::Extension: return "extension";
464 case SymbolKind::Union: return "union";
465 case SymbolKind::TypeAlias: return "type-alias";
466 case SymbolKind::Function: return "function";
467 case SymbolKind::Variable: return "variable";
468 case SymbolKind::Field: return "field";
469 case SymbolKind::EnumConstant: return "enumerator";
470 case SymbolKind::InstanceMethod: return "instance-method";
471 case SymbolKind::ClassMethod: return "class-method";
472 case SymbolKind::StaticMethod: return "static-method";
473 case SymbolKind::InstanceProperty: return "instance-property";
474 case SymbolKind::ClassProperty: return "class-property";
475 case SymbolKind::StaticProperty: return "static-property";
476 case SymbolKind::Constructor: return "constructor";
477 case SymbolKind::Destructor: return "destructor";
478 case SymbolKind::ConversionFunction: return "coversion-func";
479 case SymbolKind::Parameter: return "param";
480 case SymbolKind::Using: return "using";
482 llvm_unreachable("invalid symbol kind");
485 StringRef index::getSymbolSubKindString(SymbolSubKind K) {
487 case SymbolSubKind::None: return "<none>";
488 case SymbolSubKind::CXXCopyConstructor: return "cxx-copy-ctor";
489 case SymbolSubKind::CXXMoveConstructor: return "cxx-move-ctor";
490 case SymbolSubKind::AccessorGetter: return "acc-get";
491 case SymbolSubKind::AccessorSetter: return "acc-set";
492 case SymbolSubKind::UsingTypename: return "using-typename";
493 case SymbolSubKind::UsingValue: return "using-value";
495 llvm_unreachable("invalid symbol subkind");
498 StringRef index::getSymbolLanguageString(SymbolLanguage K) {
500 case SymbolLanguage::C: return "C";
501 case SymbolLanguage::ObjC: return "ObjC";
502 case SymbolLanguage::CXX: return "C++";
503 case SymbolLanguage::Swift: return "Swift";
505 llvm_unreachable("invalid symbol language kind");
508 void index::applyForEachSymbolProperty(SymbolPropertySet Props,
509 llvm::function_ref<void(SymbolProperty)> Fn) {
510 #define APPLY_FOR_PROPERTY(K) \
511 if (Props & (SymbolPropertySet)SymbolProperty::K) \
512 Fn(SymbolProperty::K)
514 APPLY_FOR_PROPERTY(Generic);
515 APPLY_FOR_PROPERTY(TemplatePartialSpecialization);
516 APPLY_FOR_PROPERTY(TemplateSpecialization);
517 APPLY_FOR_PROPERTY(UnitTest);
518 APPLY_FOR_PROPERTY(IBAnnotated);
519 APPLY_FOR_PROPERTY(IBOutletCollection);
520 APPLY_FOR_PROPERTY(GKInspectable);
521 APPLY_FOR_PROPERTY(Local);
523 #undef APPLY_FOR_PROPERTY
526 void index::printSymbolProperties(SymbolPropertySet Props, raw_ostream &OS) {
527 bool VisitedOnce = false;
528 applyForEachSymbolProperty(Props, [&](SymbolProperty Prop) {
534 case SymbolProperty::Generic: OS << "Gen"; break;
535 case SymbolProperty::TemplatePartialSpecialization: OS << "TPS"; break;
536 case SymbolProperty::TemplateSpecialization: OS << "TS"; break;
537 case SymbolProperty::UnitTest: OS << "test"; break;
538 case SymbolProperty::IBAnnotated: OS << "IB"; break;
539 case SymbolProperty::IBOutletCollection: OS << "IBColl"; break;
540 case SymbolProperty::GKInspectable: OS << "GKI"; break;
541 case SymbolProperty::Local: OS << "local"; break;