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 (!D->getParentFunctionOrMethod())
67 if (const NamedDecl *ND = dyn_cast<NamedDecl>(D)) {
68 switch (ND->getFormalLinkage()) {
70 case VisibleNoLinkage:
73 case UniqueExternalLinkage:
74 llvm_unreachable("Not a sema linkage");
83 SymbolInfo index::getSymbolInfo(const Decl *D) {
86 Info.Kind = SymbolKind::Unknown;
87 Info.SubKind = SymbolSubKind::None;
88 Info.Properties = SymbolPropertySet();
89 Info.Lang = SymbolLanguage::C;
91 if (isFunctionLocalSymbol(D)) {
92 Info.Properties |= (unsigned)SymbolProperty::Local;
95 if (const TagDecl *TD = dyn_cast<TagDecl>(D)) {
96 switch (TD->getTagKind()) {
98 Info.Kind = SymbolKind::Struct; break;
100 Info.Kind = SymbolKind::Union; break;
102 Info.Kind = SymbolKind::Class;
103 Info.Lang = SymbolLanguage::CXX;
106 Info.Kind = SymbolKind::Protocol;
107 Info.Lang = SymbolLanguage::CXX;
110 Info.Kind = SymbolKind::Enum; break;
113 if (const CXXRecordDecl *CXXRec = dyn_cast<CXXRecordDecl>(D)) {
114 if (!CXXRec->isCLike()) {
115 Info.Lang = SymbolLanguage::CXX;
116 if (CXXRec->getDescribedClassTemplate()) {
117 Info.Properties |= (unsigned)SymbolProperty::Generic;
122 if (isa<ClassTemplatePartialSpecializationDecl>(D)) {
123 Info.Properties |= (unsigned)SymbolProperty::Generic;
124 Info.Properties |= (unsigned)SymbolProperty::TemplatePartialSpecialization;
125 } else if (isa<ClassTemplateSpecializationDecl>(D)) {
126 Info.Properties |= (unsigned)SymbolProperty::Generic;
127 Info.Properties |= (unsigned)SymbolProperty::TemplateSpecialization;
130 } else if (auto *VD = dyn_cast<VarDecl>(D)) {
131 Info.Kind = SymbolKind::Variable;
132 if (isa<ParmVarDecl>(D)) {
133 Info.Kind = SymbolKind::Parameter;
134 } else if (isa<CXXRecordDecl>(D->getDeclContext())) {
135 Info.Kind = SymbolKind::StaticProperty;
136 Info.Lang = SymbolLanguage::CXX;
139 if (isa<VarTemplatePartialSpecializationDecl>(D)) {
140 Info.Lang = SymbolLanguage::CXX;
141 Info.Properties |= (unsigned)SymbolProperty::Generic;
142 Info.Properties |= (unsigned)SymbolProperty::TemplatePartialSpecialization;
143 } else if (isa<VarTemplateSpecializationDecl>(D)) {
144 Info.Lang = SymbolLanguage::CXX;
145 Info.Properties |= (unsigned)SymbolProperty::Generic;
146 Info.Properties |= (unsigned)SymbolProperty::TemplateSpecialization;
147 } else if (VD->getDescribedVarTemplate()) {
148 Info.Lang = SymbolLanguage::CXX;
149 Info.Properties |= (unsigned)SymbolProperty::Generic;
153 switch (D->getKind()) {
155 Info.Kind = SymbolKind::Module;
158 Info.Kind = SymbolKind::TypeAlias; break; // Lang = C
160 Info.Kind = SymbolKind::Function;
163 Info.Kind = SymbolKind::Field;
164 if (const CXXRecordDecl *
165 CXXRec = dyn_cast<CXXRecordDecl>(D->getDeclContext())) {
166 if (!CXXRec->isCLike())
167 Info.Lang = SymbolLanguage::CXX;
170 case Decl::EnumConstant:
171 Info.Kind = SymbolKind::EnumConstant; break;
172 case Decl::ObjCInterface:
173 case Decl::ObjCImplementation: {
174 Info.Kind = SymbolKind::Class;
175 Info.Lang = SymbolLanguage::ObjC;
176 const ObjCInterfaceDecl *ClsD = dyn_cast<ObjCInterfaceDecl>(D);
178 ClsD = cast<ObjCImplementationDecl>(D)->getClassInterface();
179 if (isUnitTestCase(ClsD))
180 Info.Properties |= (unsigned)SymbolProperty::UnitTest;
183 case Decl::ObjCProtocol:
184 Info.Kind = SymbolKind::Protocol;
185 Info.Lang = SymbolLanguage::ObjC;
187 case Decl::ObjCCategory:
188 case Decl::ObjCCategoryImpl: {
189 Info.Kind = SymbolKind::Extension;
190 Info.Lang = SymbolLanguage::ObjC;
191 const ObjCInterfaceDecl *ClsD = nullptr;
192 if (auto *CatD = dyn_cast<ObjCCategoryDecl>(D))
193 ClsD = CatD->getClassInterface();
195 ClsD = cast<ObjCCategoryImplDecl>(D)->getClassInterface();
196 if (isUnitTestCase(ClsD))
197 Info.Properties |= (unsigned)SymbolProperty::UnitTest;
200 case Decl::ObjCMethod:
201 if (cast<ObjCMethodDecl>(D)->isInstanceMethod()) {
202 const ObjCMethodDecl *MD = cast<ObjCMethodDecl>(D);
203 Info.Kind = SymbolKind::InstanceMethod;
204 if (MD->isPropertyAccessor()) {
205 if (MD->param_size())
206 Info.SubKind = SymbolSubKind::AccessorSetter;
208 Info.SubKind = SymbolSubKind::AccessorGetter;
211 Info.Kind = SymbolKind::ClassMethod;
213 Info.Lang = SymbolLanguage::ObjC;
214 if (isUnitTest(cast<ObjCMethodDecl>(D)))
215 Info.Properties |= (unsigned)SymbolProperty::UnitTest;
216 if (D->hasAttr<IBActionAttr>())
217 Info.Properties |= (unsigned)SymbolProperty::IBAnnotated;
219 case Decl::ObjCProperty:
220 Info.Kind = SymbolKind::InstanceProperty;
221 Info.Lang = SymbolLanguage::ObjC;
222 checkForIBOutlets(D, Info.Properties);
223 if (auto *Annot = D->getAttr<AnnotateAttr>()) {
224 if (Annot->getAnnotation() == "gk_inspectable")
225 Info.Properties |= (unsigned)SymbolProperty::GKInspectable;
229 Info.Kind = SymbolKind::Field;
230 Info.Lang = SymbolLanguage::ObjC;
231 checkForIBOutlets(D, Info.Properties);
233 case Decl::Namespace:
234 Info.Kind = SymbolKind::Namespace;
235 Info.Lang = SymbolLanguage::CXX;
237 case Decl::NamespaceAlias:
238 Info.Kind = SymbolKind::NamespaceAlias;
239 Info.Lang = SymbolLanguage::CXX;
241 case Decl::CXXConstructor: {
242 Info.Kind = SymbolKind::Constructor;
243 Info.Lang = SymbolLanguage::CXX;
244 auto *CD = cast<CXXConstructorDecl>(D);
245 if (CD->isCopyConstructor())
246 Info.SubKind = SymbolSubKind::CXXCopyConstructor;
247 else if (CD->isMoveConstructor())
248 Info.SubKind = SymbolSubKind::CXXMoveConstructor;
251 case Decl::CXXDestructor:
252 Info.Kind = SymbolKind::Destructor;
253 Info.Lang = SymbolLanguage::CXX;
255 case Decl::CXXConversion:
256 Info.Kind = SymbolKind::ConversionFunction;
257 Info.Lang = SymbolLanguage::CXX;
259 case Decl::CXXMethod: {
260 const CXXMethodDecl *MD = cast<CXXMethodDecl>(D);
262 Info.Kind = SymbolKind::StaticMethod;
264 Info.Kind = SymbolKind::InstanceMethod;
265 Info.Lang = SymbolLanguage::CXX;
268 case Decl::ClassTemplate:
269 Info.Kind = SymbolKind::Class;
270 Info.Properties |= (unsigned)SymbolProperty::Generic;
271 Info.Lang = SymbolLanguage::CXX;
273 case Decl::FunctionTemplate:
274 Info.Kind = SymbolKind::Function;
275 Info.Properties |= (unsigned)SymbolProperty::Generic;
276 Info.Lang = SymbolLanguage::CXX;
277 if (const CXXMethodDecl *MD = dyn_cast_or_null<CXXMethodDecl>(
278 cast<FunctionTemplateDecl>(D)->getTemplatedDecl())) {
279 if (isa<CXXConstructorDecl>(MD))
280 Info.Kind = SymbolKind::Constructor;
281 else if (isa<CXXDestructorDecl>(MD))
282 Info.Kind = SymbolKind::Destructor;
283 else if (isa<CXXConversionDecl>(MD))
284 Info.Kind = SymbolKind::ConversionFunction;
287 Info.Kind = SymbolKind::StaticMethod;
289 Info.Kind = SymbolKind::InstanceMethod;
293 case Decl::TypeAliasTemplate:
294 Info.Kind = SymbolKind::TypeAlias;
295 Info.Lang = SymbolLanguage::CXX;
296 Info.Properties |= (unsigned)SymbolProperty::Generic;
298 case Decl::TypeAlias:
299 Info.Kind = SymbolKind::TypeAlias;
300 Info.Lang = SymbolLanguage::CXX;
307 if (Info.Kind == SymbolKind::Unknown)
310 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
311 if (FD->getTemplatedKind() ==
312 FunctionDecl::TK_FunctionTemplateSpecialization) {
313 Info.Properties |= (unsigned)SymbolProperty::Generic;
314 Info.Properties |= (unsigned)SymbolProperty::TemplateSpecialization;
318 if (Info.Properties & (unsigned)SymbolProperty::Generic)
319 Info.Lang = SymbolLanguage::CXX;
324 bool index::applyForEachSymbolRoleInterruptible(SymbolRoleSet Roles,
325 llvm::function_ref<bool(SymbolRole)> Fn) {
326 #define APPLY_FOR_ROLE(Role) \
327 if (Roles & (unsigned)SymbolRole::Role) \
328 if (!Fn(SymbolRole::Role)) \
331 APPLY_FOR_ROLE(Declaration);
332 APPLY_FOR_ROLE(Definition);
333 APPLY_FOR_ROLE(Reference);
334 APPLY_FOR_ROLE(Read);
335 APPLY_FOR_ROLE(Write);
336 APPLY_FOR_ROLE(Call);
337 APPLY_FOR_ROLE(Dynamic);
338 APPLY_FOR_ROLE(AddressOf);
339 APPLY_FOR_ROLE(Implicit);
340 APPLY_FOR_ROLE(RelationChildOf);
341 APPLY_FOR_ROLE(RelationBaseOf);
342 APPLY_FOR_ROLE(RelationOverrideOf);
343 APPLY_FOR_ROLE(RelationReceivedBy);
344 APPLY_FOR_ROLE(RelationCalledBy);
345 APPLY_FOR_ROLE(RelationExtendedBy);
346 APPLY_FOR_ROLE(RelationAccessorOf);
347 APPLY_FOR_ROLE(RelationContainedBy);
348 APPLY_FOR_ROLE(RelationIBTypeOf);
350 #undef APPLY_FOR_ROLE
355 void index::applyForEachSymbolRole(SymbolRoleSet Roles,
356 llvm::function_ref<void(SymbolRole)> Fn) {
357 applyForEachSymbolRoleInterruptible(Roles, [&](SymbolRole r) -> bool {
363 void index::printSymbolRoles(SymbolRoleSet Roles, raw_ostream &OS) {
364 bool VisitedOnce = false;
365 applyForEachSymbolRole(Roles, [&](SymbolRole Role) {
371 case SymbolRole::Declaration: OS << "Decl"; break;
372 case SymbolRole::Definition: OS << "Def"; break;
373 case SymbolRole::Reference: OS << "Ref"; break;
374 case SymbolRole::Read: OS << "Read"; break;
375 case SymbolRole::Write: OS << "Writ"; break;
376 case SymbolRole::Call: OS << "Call"; break;
377 case SymbolRole::Dynamic: OS << "Dyn"; break;
378 case SymbolRole::AddressOf: OS << "Addr"; break;
379 case SymbolRole::Implicit: OS << "Impl"; break;
380 case SymbolRole::RelationChildOf: OS << "RelChild"; break;
381 case SymbolRole::RelationBaseOf: OS << "RelBase"; break;
382 case SymbolRole::RelationOverrideOf: OS << "RelOver"; break;
383 case SymbolRole::RelationReceivedBy: OS << "RelRec"; break;
384 case SymbolRole::RelationCalledBy: OS << "RelCall"; break;
385 case SymbolRole::RelationExtendedBy: OS << "RelExt"; break;
386 case SymbolRole::RelationAccessorOf: OS << "RelAcc"; break;
387 case SymbolRole::RelationContainedBy: OS << "RelCont"; break;
388 case SymbolRole::RelationIBTypeOf: OS << "RelIBType"; break;
393 bool index::printSymbolName(const Decl *D, const LangOptions &LO,
395 if (auto *ND = dyn_cast<NamedDecl>(D)) {
396 PrintingPolicy Policy(LO);
397 // Forward references can have different template argument names. Suppress
398 // the template argument names in constructors to make their name more
400 Policy.SuppressTemplateArgsInCXXConstructors = true;
401 DeclarationName DeclName = ND->getDeclName();
402 if (DeclName.isEmpty())
404 DeclName.print(OS, Policy);
411 StringRef index::getSymbolKindString(SymbolKind K) {
413 case SymbolKind::Unknown: return "<unknown>";
414 case SymbolKind::Module: return "module";
415 case SymbolKind::Namespace: return "namespace";
416 case SymbolKind::NamespaceAlias: return "namespace-alias";
417 case SymbolKind::Macro: return "macro";
418 case SymbolKind::Enum: return "enum";
419 case SymbolKind::Struct: return "struct";
420 case SymbolKind::Class: return "class";
421 case SymbolKind::Protocol: return "protocol";
422 case SymbolKind::Extension: return "extension";
423 case SymbolKind::Union: return "union";
424 case SymbolKind::TypeAlias: return "type-alias";
425 case SymbolKind::Function: return "function";
426 case SymbolKind::Variable: return "variable";
427 case SymbolKind::Field: return "field";
428 case SymbolKind::EnumConstant: return "enumerator";
429 case SymbolKind::InstanceMethod: return "instance-method";
430 case SymbolKind::ClassMethod: return "class-method";
431 case SymbolKind::StaticMethod: return "static-method";
432 case SymbolKind::InstanceProperty: return "instance-property";
433 case SymbolKind::ClassProperty: return "class-property";
434 case SymbolKind::StaticProperty: return "static-property";
435 case SymbolKind::Constructor: return "constructor";
436 case SymbolKind::Destructor: return "destructor";
437 case SymbolKind::ConversionFunction: return "coversion-func";
438 case SymbolKind::Parameter: return "param";
440 llvm_unreachable("invalid symbol kind");
443 StringRef index::getSymbolSubKindString(SymbolSubKind K) {
445 case SymbolSubKind::None: return "<none>";
446 case SymbolSubKind::CXXCopyConstructor: return "cxx-copy-ctor";
447 case SymbolSubKind::CXXMoveConstructor: return "cxx-move-ctor";
448 case SymbolSubKind::AccessorGetter: return "acc-get";
449 case SymbolSubKind::AccessorSetter: return "acc-set";
451 llvm_unreachable("invalid symbol subkind");
454 StringRef index::getSymbolLanguageString(SymbolLanguage K) {
456 case SymbolLanguage::C: return "C";
457 case SymbolLanguage::ObjC: return "ObjC";
458 case SymbolLanguage::CXX: return "C++";
460 llvm_unreachable("invalid symbol language kind");
463 void index::applyForEachSymbolProperty(SymbolPropertySet Props,
464 llvm::function_ref<void(SymbolProperty)> Fn) {
465 #define APPLY_FOR_PROPERTY(K) \
466 if (Props & (unsigned)SymbolProperty::K) \
467 Fn(SymbolProperty::K)
469 APPLY_FOR_PROPERTY(Generic);
470 APPLY_FOR_PROPERTY(TemplatePartialSpecialization);
471 APPLY_FOR_PROPERTY(TemplateSpecialization);
472 APPLY_FOR_PROPERTY(UnitTest);
473 APPLY_FOR_PROPERTY(IBAnnotated);
474 APPLY_FOR_PROPERTY(IBOutletCollection);
475 APPLY_FOR_PROPERTY(GKInspectable);
476 APPLY_FOR_PROPERTY(Local);
478 #undef APPLY_FOR_PROPERTY
481 void index::printSymbolProperties(SymbolPropertySet Props, raw_ostream &OS) {
482 bool VisitedOnce = false;
483 applyForEachSymbolProperty(Props, [&](SymbolProperty Prop) {
489 case SymbolProperty::Generic: OS << "Gen"; break;
490 case SymbolProperty::TemplatePartialSpecialization: OS << "TPS"; break;
491 case SymbolProperty::TemplateSpecialization: OS << "TS"; break;
492 case SymbolProperty::UnitTest: OS << "test"; break;
493 case SymbolProperty::IBAnnotated: OS << "IB"; break;
494 case SymbolProperty::IBOutletCollection: OS << "IBColl"; break;
495 case SymbolProperty::GKInspectable: OS << "GKI"; break;
496 case SymbolProperty::Local: OS << "local"; break;