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 (isa<UsingDirectiveDecl>(D))
66 if (!D->getParentFunctionOrMethod())
69 if (const NamedDecl *ND = dyn_cast<NamedDecl>(D)) {
70 switch (ND->getFormalLinkage()) {
72 case VisibleNoLinkage:
75 case UniqueExternalLinkage:
76 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 |= (unsigned)SymbolProperty::Local;
97 if (const TagDecl *TD = dyn_cast<TagDecl>(D)) {
98 switch (TD->getTagKind()) {
100 Info.Kind = SymbolKind::Struct; break;
102 Info.Kind = SymbolKind::Union; break;
104 Info.Kind = SymbolKind::Class;
105 Info.Lang = SymbolLanguage::CXX;
108 Info.Kind = SymbolKind::Protocol;
109 Info.Lang = SymbolLanguage::CXX;
112 Info.Kind = SymbolKind::Enum; break;
115 if (const CXXRecordDecl *CXXRec = dyn_cast<CXXRecordDecl>(D)) {
116 if (!CXXRec->isCLike()) {
117 Info.Lang = SymbolLanguage::CXX;
118 if (CXXRec->getDescribedClassTemplate()) {
119 Info.Properties |= (unsigned)SymbolProperty::Generic;
124 if (isa<ClassTemplatePartialSpecializationDecl>(D)) {
125 Info.Properties |= (unsigned)SymbolProperty::Generic;
126 Info.Properties |= (unsigned)SymbolProperty::TemplatePartialSpecialization;
127 } else if (isa<ClassTemplateSpecializationDecl>(D)) {
128 Info.Properties |= (unsigned)SymbolProperty::Generic;
129 Info.Properties |= (unsigned)SymbolProperty::TemplateSpecialization;
132 } else if (auto *VD = dyn_cast<VarDecl>(D)) {
133 Info.Kind = SymbolKind::Variable;
134 if (isa<ParmVarDecl>(D)) {
135 Info.Kind = SymbolKind::Parameter;
136 } else if (isa<CXXRecordDecl>(D->getDeclContext())) {
137 Info.Kind = SymbolKind::StaticProperty;
138 Info.Lang = SymbolLanguage::CXX;
141 if (isa<VarTemplatePartialSpecializationDecl>(D)) {
142 Info.Lang = SymbolLanguage::CXX;
143 Info.Properties |= (unsigned)SymbolProperty::Generic;
144 Info.Properties |= (unsigned)SymbolProperty::TemplatePartialSpecialization;
145 } else if (isa<VarTemplateSpecializationDecl>(D)) {
146 Info.Lang = SymbolLanguage::CXX;
147 Info.Properties |= (unsigned)SymbolProperty::Generic;
148 Info.Properties |= (unsigned)SymbolProperty::TemplateSpecialization;
149 } else if (VD->getDescribedVarTemplate()) {
150 Info.Lang = SymbolLanguage::CXX;
151 Info.Properties |= (unsigned)SymbolProperty::Generic;
155 switch (D->getKind()) {
157 Info.Kind = SymbolKind::Module;
160 Info.Kind = SymbolKind::TypeAlias; break; // Lang = C
162 Info.Kind = SymbolKind::Function;
165 Info.Kind = SymbolKind::Field;
166 if (const CXXRecordDecl *
167 CXXRec = dyn_cast<CXXRecordDecl>(D->getDeclContext())) {
168 if (!CXXRec->isCLike())
169 Info.Lang = SymbolLanguage::CXX;
172 case Decl::EnumConstant:
173 Info.Kind = SymbolKind::EnumConstant; break;
174 case Decl::ObjCInterface:
175 case Decl::ObjCImplementation: {
176 Info.Kind = SymbolKind::Class;
177 Info.Lang = SymbolLanguage::ObjC;
178 const ObjCInterfaceDecl *ClsD = dyn_cast<ObjCInterfaceDecl>(D);
180 ClsD = cast<ObjCImplementationDecl>(D)->getClassInterface();
181 if (isUnitTestCase(ClsD))
182 Info.Properties |= (unsigned)SymbolProperty::UnitTest;
185 case Decl::ObjCProtocol:
186 Info.Kind = SymbolKind::Protocol;
187 Info.Lang = SymbolLanguage::ObjC;
189 case Decl::ObjCCategory:
190 case Decl::ObjCCategoryImpl: {
191 Info.Kind = SymbolKind::Extension;
192 Info.Lang = SymbolLanguage::ObjC;
193 const ObjCInterfaceDecl *ClsD = nullptr;
194 if (auto *CatD = dyn_cast<ObjCCategoryDecl>(D))
195 ClsD = CatD->getClassInterface();
197 ClsD = cast<ObjCCategoryImplDecl>(D)->getClassInterface();
198 if (isUnitTestCase(ClsD))
199 Info.Properties |= (unsigned)SymbolProperty::UnitTest;
202 case Decl::ObjCMethod:
203 if (cast<ObjCMethodDecl>(D)->isInstanceMethod()) {
204 const ObjCMethodDecl *MD = cast<ObjCMethodDecl>(D);
205 Info.Kind = SymbolKind::InstanceMethod;
206 if (MD->isPropertyAccessor()) {
207 if (MD->param_size())
208 Info.SubKind = SymbolSubKind::AccessorSetter;
210 Info.SubKind = SymbolSubKind::AccessorGetter;
213 Info.Kind = SymbolKind::ClassMethod;
215 Info.Lang = SymbolLanguage::ObjC;
216 if (isUnitTest(cast<ObjCMethodDecl>(D)))
217 Info.Properties |= (unsigned)SymbolProperty::UnitTest;
218 if (D->hasAttr<IBActionAttr>())
219 Info.Properties |= (unsigned)SymbolProperty::IBAnnotated;
221 case Decl::ObjCProperty:
222 Info.Kind = SymbolKind::InstanceProperty;
223 Info.Lang = SymbolLanguage::ObjC;
224 checkForIBOutlets(D, Info.Properties);
225 if (auto *Annot = D->getAttr<AnnotateAttr>()) {
226 if (Annot->getAnnotation() == "gk_inspectable")
227 Info.Properties |= (unsigned)SymbolProperty::GKInspectable;
231 Info.Kind = SymbolKind::Field;
232 Info.Lang = SymbolLanguage::ObjC;
233 checkForIBOutlets(D, Info.Properties);
235 case Decl::Namespace:
236 Info.Kind = SymbolKind::Namespace;
237 Info.Lang = SymbolLanguage::CXX;
239 case Decl::NamespaceAlias:
240 Info.Kind = SymbolKind::NamespaceAlias;
241 Info.Lang = SymbolLanguage::CXX;
243 case Decl::CXXConstructor: {
244 Info.Kind = SymbolKind::Constructor;
245 Info.Lang = SymbolLanguage::CXX;
246 auto *CD = cast<CXXConstructorDecl>(D);
247 if (CD->isCopyConstructor())
248 Info.SubKind = SymbolSubKind::CXXCopyConstructor;
249 else if (CD->isMoveConstructor())
250 Info.SubKind = SymbolSubKind::CXXMoveConstructor;
253 case Decl::CXXDestructor:
254 Info.Kind = SymbolKind::Destructor;
255 Info.Lang = SymbolLanguage::CXX;
257 case Decl::CXXConversion:
258 Info.Kind = SymbolKind::ConversionFunction;
259 Info.Lang = SymbolLanguage::CXX;
261 case Decl::CXXMethod: {
262 const CXXMethodDecl *MD = cast<CXXMethodDecl>(D);
264 Info.Kind = SymbolKind::StaticMethod;
266 Info.Kind = SymbolKind::InstanceMethod;
267 Info.Lang = SymbolLanguage::CXX;
270 case Decl::ClassTemplate:
271 Info.Kind = SymbolKind::Class;
272 Info.Properties |= (unsigned)SymbolProperty::Generic;
273 Info.Lang = SymbolLanguage::CXX;
275 case Decl::FunctionTemplate:
276 Info.Kind = SymbolKind::Function;
277 Info.Properties |= (unsigned)SymbolProperty::Generic;
278 Info.Lang = SymbolLanguage::CXX;
279 if (const CXXMethodDecl *MD = dyn_cast_or_null<CXXMethodDecl>(
280 cast<FunctionTemplateDecl>(D)->getTemplatedDecl())) {
281 if (isa<CXXConstructorDecl>(MD))
282 Info.Kind = SymbolKind::Constructor;
283 else if (isa<CXXDestructorDecl>(MD))
284 Info.Kind = SymbolKind::Destructor;
285 else if (isa<CXXConversionDecl>(MD))
286 Info.Kind = SymbolKind::ConversionFunction;
289 Info.Kind = SymbolKind::StaticMethod;
291 Info.Kind = SymbolKind::InstanceMethod;
295 case Decl::TypeAliasTemplate:
296 Info.Kind = SymbolKind::TypeAlias;
297 Info.Lang = SymbolLanguage::CXX;
298 Info.Properties |= (unsigned)SymbolProperty::Generic;
300 case Decl::TypeAlias:
301 Info.Kind = SymbolKind::TypeAlias;
302 Info.Lang = SymbolLanguage::CXX;
305 Info.Kind = SymbolKind::Variable;
306 Info.Lang = SymbolLanguage::CXX;
313 if (Info.Kind == SymbolKind::Unknown)
316 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
317 if (FD->getTemplatedKind() ==
318 FunctionDecl::TK_FunctionTemplateSpecialization) {
319 Info.Properties |= (unsigned)SymbolProperty::Generic;
320 Info.Properties |= (unsigned)SymbolProperty::TemplateSpecialization;
324 if (Info.Properties & (unsigned)SymbolProperty::Generic)
325 Info.Lang = SymbolLanguage::CXX;
327 if (auto *attr = D->getExternalSourceSymbolAttr()) {
328 if (attr->getLanguage() == "Swift")
329 Info.Lang = SymbolLanguage::Swift;
335 bool index::applyForEachSymbolRoleInterruptible(SymbolRoleSet Roles,
336 llvm::function_ref<bool(SymbolRole)> Fn) {
337 #define APPLY_FOR_ROLE(Role) \
338 if (Roles & (unsigned)SymbolRole::Role) \
339 if (!Fn(SymbolRole::Role)) \
342 APPLY_FOR_ROLE(Declaration);
343 APPLY_FOR_ROLE(Definition);
344 APPLY_FOR_ROLE(Reference);
345 APPLY_FOR_ROLE(Read);
346 APPLY_FOR_ROLE(Write);
347 APPLY_FOR_ROLE(Call);
348 APPLY_FOR_ROLE(Dynamic);
349 APPLY_FOR_ROLE(AddressOf);
350 APPLY_FOR_ROLE(Implicit);
351 APPLY_FOR_ROLE(RelationChildOf);
352 APPLY_FOR_ROLE(RelationBaseOf);
353 APPLY_FOR_ROLE(RelationOverrideOf);
354 APPLY_FOR_ROLE(RelationReceivedBy);
355 APPLY_FOR_ROLE(RelationCalledBy);
356 APPLY_FOR_ROLE(RelationExtendedBy);
357 APPLY_FOR_ROLE(RelationAccessorOf);
358 APPLY_FOR_ROLE(RelationContainedBy);
359 APPLY_FOR_ROLE(RelationIBTypeOf);
360 APPLY_FOR_ROLE(RelationSpecializationOf);
362 #undef APPLY_FOR_ROLE
367 void index::applyForEachSymbolRole(SymbolRoleSet Roles,
368 llvm::function_ref<void(SymbolRole)> Fn) {
369 applyForEachSymbolRoleInterruptible(Roles, [&](SymbolRole r) -> bool {
375 void index::printSymbolRoles(SymbolRoleSet Roles, raw_ostream &OS) {
376 bool VisitedOnce = false;
377 applyForEachSymbolRole(Roles, [&](SymbolRole Role) {
383 case SymbolRole::Declaration: OS << "Decl"; break;
384 case SymbolRole::Definition: OS << "Def"; break;
385 case SymbolRole::Reference: OS << "Ref"; break;
386 case SymbolRole::Read: OS << "Read"; break;
387 case SymbolRole::Write: OS << "Writ"; break;
388 case SymbolRole::Call: OS << "Call"; break;
389 case SymbolRole::Dynamic: OS << "Dyn"; break;
390 case SymbolRole::AddressOf: OS << "Addr"; break;
391 case SymbolRole::Implicit: OS << "Impl"; break;
392 case SymbolRole::RelationChildOf: OS << "RelChild"; break;
393 case SymbolRole::RelationBaseOf: OS << "RelBase"; break;
394 case SymbolRole::RelationOverrideOf: OS << "RelOver"; break;
395 case SymbolRole::RelationReceivedBy: OS << "RelRec"; break;
396 case SymbolRole::RelationCalledBy: OS << "RelCall"; break;
397 case SymbolRole::RelationExtendedBy: OS << "RelExt"; break;
398 case SymbolRole::RelationAccessorOf: OS << "RelAcc"; break;
399 case SymbolRole::RelationContainedBy: OS << "RelCont"; break;
400 case SymbolRole::RelationIBTypeOf: OS << "RelIBType"; break;
401 case SymbolRole::RelationSpecializationOf: OS << "RelSpecialization"; break;
406 bool index::printSymbolName(const Decl *D, const LangOptions &LO,
408 if (auto *ND = dyn_cast<NamedDecl>(D)) {
409 PrintingPolicy Policy(LO);
410 // Forward references can have different template argument names. Suppress
411 // the template argument names in constructors to make their name more
413 Policy.SuppressTemplateArgsInCXXConstructors = true;
414 DeclarationName DeclName = ND->getDeclName();
415 if (DeclName.isEmpty())
417 DeclName.print(OS, Policy);
424 StringRef index::getSymbolKindString(SymbolKind K) {
426 case SymbolKind::Unknown: return "<unknown>";
427 case SymbolKind::Module: return "module";
428 case SymbolKind::Namespace: return "namespace";
429 case SymbolKind::NamespaceAlias: return "namespace-alias";
430 case SymbolKind::Macro: return "macro";
431 case SymbolKind::Enum: return "enum";
432 case SymbolKind::Struct: return "struct";
433 case SymbolKind::Class: return "class";
434 case SymbolKind::Protocol: return "protocol";
435 case SymbolKind::Extension: return "extension";
436 case SymbolKind::Union: return "union";
437 case SymbolKind::TypeAlias: return "type-alias";
438 case SymbolKind::Function: return "function";
439 case SymbolKind::Variable: return "variable";
440 case SymbolKind::Field: return "field";
441 case SymbolKind::EnumConstant: return "enumerator";
442 case SymbolKind::InstanceMethod: return "instance-method";
443 case SymbolKind::ClassMethod: return "class-method";
444 case SymbolKind::StaticMethod: return "static-method";
445 case SymbolKind::InstanceProperty: return "instance-property";
446 case SymbolKind::ClassProperty: return "class-property";
447 case SymbolKind::StaticProperty: return "static-property";
448 case SymbolKind::Constructor: return "constructor";
449 case SymbolKind::Destructor: return "destructor";
450 case SymbolKind::ConversionFunction: return "coversion-func";
451 case SymbolKind::Parameter: return "param";
453 llvm_unreachable("invalid symbol kind");
456 StringRef index::getSymbolSubKindString(SymbolSubKind K) {
458 case SymbolSubKind::None: return "<none>";
459 case SymbolSubKind::CXXCopyConstructor: return "cxx-copy-ctor";
460 case SymbolSubKind::CXXMoveConstructor: return "cxx-move-ctor";
461 case SymbolSubKind::AccessorGetter: return "acc-get";
462 case SymbolSubKind::AccessorSetter: return "acc-set";
464 llvm_unreachable("invalid symbol subkind");
467 StringRef index::getSymbolLanguageString(SymbolLanguage K) {
469 case SymbolLanguage::C: return "C";
470 case SymbolLanguage::ObjC: return "ObjC";
471 case SymbolLanguage::CXX: return "C++";
472 case SymbolLanguage::Swift: return "Swift";
474 llvm_unreachable("invalid symbol language kind");
477 void index::applyForEachSymbolProperty(SymbolPropertySet Props,
478 llvm::function_ref<void(SymbolProperty)> Fn) {
479 #define APPLY_FOR_PROPERTY(K) \
480 if (Props & (unsigned)SymbolProperty::K) \
481 Fn(SymbolProperty::K)
483 APPLY_FOR_PROPERTY(Generic);
484 APPLY_FOR_PROPERTY(TemplatePartialSpecialization);
485 APPLY_FOR_PROPERTY(TemplateSpecialization);
486 APPLY_FOR_PROPERTY(UnitTest);
487 APPLY_FOR_PROPERTY(IBAnnotated);
488 APPLY_FOR_PROPERTY(IBOutletCollection);
489 APPLY_FOR_PROPERTY(GKInspectable);
490 APPLY_FOR_PROPERTY(Local);
492 #undef APPLY_FOR_PROPERTY
495 void index::printSymbolProperties(SymbolPropertySet Props, raw_ostream &OS) {
496 bool VisitedOnce = false;
497 applyForEachSymbolProperty(Props, [&](SymbolProperty Prop) {
503 case SymbolProperty::Generic: OS << "Gen"; break;
504 case SymbolProperty::TemplatePartialSpecialization: OS << "TPS"; break;
505 case SymbolProperty::TemplateSpecialization: OS << "TS"; break;
506 case SymbolProperty::UnitTest: OS << "test"; break;
507 case SymbolProperty::IBAnnotated: OS << "IB"; break;
508 case SymbolProperty::IBOutletCollection: OS << "IBColl"; break;
509 case SymbolProperty::GKInspectable: OS << "GKI"; break;
510 case SymbolProperty::Local: OS << "local"; break;