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, SymbolSubKindSet &SubKindSet) {
44 if (D->hasAttr<IBOutletAttr>()) {
45 SubKindSet |= (unsigned)SymbolSubKind::IBAnnotated;
46 } else if (D->hasAttr<IBOutletCollectionAttr>()) {
47 SubKindSet |= (unsigned)SymbolSubKind::IBAnnotated;
48 SubKindSet |= (unsigned)SymbolSubKind::IBOutletCollection;
52 SymbolInfo index::getSymbolInfo(const Decl *D) {
55 Info.Kind = SymbolKind::Unknown;
56 Info.SubKinds = SymbolSubKindSet();
57 Info.Lang = SymbolLanguage::C;
59 if (const TagDecl *TD = dyn_cast<TagDecl>(D)) {
60 switch (TD->getTagKind()) {
62 Info.Kind = SymbolKind::Struct; break;
64 Info.Kind = SymbolKind::Union; break;
66 Info.Kind = SymbolKind::Class;
67 Info.Lang = SymbolLanguage::CXX;
70 Info.Kind = SymbolKind::Protocol;
71 Info.Lang = SymbolLanguage::CXX;
74 Info.Kind = SymbolKind::Enum; break;
77 if (const CXXRecordDecl *CXXRec = dyn_cast<CXXRecordDecl>(D))
78 if (!CXXRec->isCLike())
79 Info.Lang = SymbolLanguage::CXX;
81 if (isa<ClassTemplatePartialSpecializationDecl>(D)) {
82 Info.SubKinds |= (unsigned)SymbolSubKind::Generic;
83 Info.SubKinds |= (unsigned)SymbolSubKind::TemplatePartialSpecialization;
84 } else if (isa<ClassTemplateSpecializationDecl>(D)) {
85 Info.SubKinds |= (unsigned)SymbolSubKind::Generic;
86 Info.SubKinds |= (unsigned)SymbolSubKind::TemplateSpecialization;
90 switch (D->getKind()) {
92 Info.Kind = SymbolKind::Module;
95 Info.Kind = SymbolKind::TypeAlias; break; // Lang = C
97 Info.Kind = SymbolKind::Function;
100 Info.Kind = SymbolKind::Variable;
103 Info.Kind = SymbolKind::Variable;
104 if (isa<CXXRecordDecl>(D->getDeclContext())) {
105 Info.Kind = SymbolKind::StaticProperty;
106 Info.Lang = SymbolLanguage::CXX;
110 Info.Kind = SymbolKind::Field;
111 if (const CXXRecordDecl *
112 CXXRec = dyn_cast<CXXRecordDecl>(D->getDeclContext())) {
113 if (!CXXRec->isCLike())
114 Info.Lang = SymbolLanguage::CXX;
117 case Decl::EnumConstant:
118 Info.Kind = SymbolKind::EnumConstant; break;
119 case Decl::ObjCInterface:
120 case Decl::ObjCImplementation: {
121 Info.Kind = SymbolKind::Class;
122 Info.Lang = SymbolLanguage::ObjC;
123 const ObjCInterfaceDecl *ClsD = dyn_cast<ObjCInterfaceDecl>(D);
125 ClsD = cast<ObjCImplementationDecl>(D)->getClassInterface();
126 if (isUnitTestCase(ClsD))
127 Info.SubKinds |= (unsigned)SymbolSubKind::UnitTest;
130 case Decl::ObjCProtocol:
131 Info.Kind = SymbolKind::Protocol;
132 Info.Lang = SymbolLanguage::ObjC;
134 case Decl::ObjCCategory:
135 case Decl::ObjCCategoryImpl:
136 Info.Kind = SymbolKind::Extension;
137 Info.Lang = SymbolLanguage::ObjC;
139 case Decl::ObjCMethod:
140 if (cast<ObjCMethodDecl>(D)->isInstanceMethod())
141 Info.Kind = SymbolKind::InstanceMethod;
143 Info.Kind = SymbolKind::ClassMethod;
144 Info.Lang = SymbolLanguage::ObjC;
145 if (isUnitTest(cast<ObjCMethodDecl>(D)))
146 Info.SubKinds |= (unsigned)SymbolSubKind::UnitTest;
147 if (D->hasAttr<IBActionAttr>())
148 Info.SubKinds |= (unsigned)SymbolSubKind::IBAnnotated;
150 case Decl::ObjCProperty:
151 Info.Kind = SymbolKind::InstanceProperty;
152 Info.Lang = SymbolLanguage::ObjC;
153 checkForIBOutlets(D, Info.SubKinds);
156 Info.Kind = SymbolKind::Field;
157 Info.Lang = SymbolLanguage::ObjC;
158 checkForIBOutlets(D, Info.SubKinds);
160 case Decl::Namespace:
161 Info.Kind = SymbolKind::Namespace;
162 Info.Lang = SymbolLanguage::CXX;
164 case Decl::NamespaceAlias:
165 Info.Kind = SymbolKind::NamespaceAlias;
166 Info.Lang = SymbolLanguage::CXX;
168 case Decl::CXXConstructor:
169 Info.Kind = SymbolKind::Constructor;
170 Info.Lang = SymbolLanguage::CXX;
172 case Decl::CXXDestructor:
173 Info.Kind = SymbolKind::Destructor;
174 Info.Lang = SymbolLanguage::CXX;
176 case Decl::CXXConversion:
177 Info.Kind = SymbolKind::ConversionFunction;
178 Info.Lang = SymbolLanguage::CXX;
180 case Decl::CXXMethod: {
181 const CXXMethodDecl *MD = cast<CXXMethodDecl>(D);
183 Info.Kind = SymbolKind::StaticMethod;
185 Info.Kind = SymbolKind::InstanceMethod;
186 Info.Lang = SymbolLanguage::CXX;
189 case Decl::ClassTemplate:
190 Info.Kind = SymbolKind::Class;
191 Info.SubKinds |= (unsigned)SymbolSubKind::Generic;
192 Info.Lang = SymbolLanguage::CXX;
194 case Decl::FunctionTemplate:
195 Info.Kind = SymbolKind::Function;
196 Info.SubKinds |= (unsigned)SymbolSubKind::Generic;
197 Info.Lang = SymbolLanguage::CXX;
198 if (const CXXMethodDecl *MD = dyn_cast_or_null<CXXMethodDecl>(
199 cast<FunctionTemplateDecl>(D)->getTemplatedDecl())) {
200 if (isa<CXXConstructorDecl>(MD))
201 Info.Kind = SymbolKind::Constructor;
202 else if (isa<CXXDestructorDecl>(MD))
203 Info.Kind = SymbolKind::Destructor;
204 else if (isa<CXXConversionDecl>(MD))
205 Info.Kind = SymbolKind::ConversionFunction;
208 Info.Kind = SymbolKind::StaticMethod;
210 Info.Kind = SymbolKind::InstanceMethod;
214 case Decl::TypeAliasTemplate:
215 Info.Kind = SymbolKind::TypeAlias;
216 Info.Lang = SymbolLanguage::CXX;
217 Info.SubKinds |= (unsigned)SymbolSubKind::Generic;
219 case Decl::TypeAlias:
220 Info.Kind = SymbolKind::TypeAlias;
221 Info.Lang = SymbolLanguage::CXX;
228 if (Info.Kind == SymbolKind::Unknown)
231 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
232 if (FD->getTemplatedKind() ==
233 FunctionDecl::TK_FunctionTemplateSpecialization) {
234 Info.SubKinds |= (unsigned)SymbolSubKind::Generic;
235 Info.SubKinds |= (unsigned)SymbolSubKind::TemplateSpecialization;
239 if (Info.SubKinds & (unsigned)SymbolSubKind::Generic)
240 Info.Lang = SymbolLanguage::CXX;
245 void index::applyForEachSymbolRole(SymbolRoleSet Roles,
246 llvm::function_ref<void(SymbolRole)> Fn) {
247 #define APPLY_FOR_ROLE(Role) \
248 if (Roles & (unsigned)SymbolRole::Role) \
251 APPLY_FOR_ROLE(Declaration);
252 APPLY_FOR_ROLE(Definition);
253 APPLY_FOR_ROLE(Reference);
254 APPLY_FOR_ROLE(Read);
255 APPLY_FOR_ROLE(Write);
256 APPLY_FOR_ROLE(Call);
257 APPLY_FOR_ROLE(Dynamic);
258 APPLY_FOR_ROLE(AddressOf);
259 APPLY_FOR_ROLE(Implicit);
260 APPLY_FOR_ROLE(RelationChildOf);
261 APPLY_FOR_ROLE(RelationBaseOf);
262 APPLY_FOR_ROLE(RelationOverrideOf);
263 APPLY_FOR_ROLE(RelationReceivedBy);
264 APPLY_FOR_ROLE(RelationCalledBy);
266 #undef APPLY_FOR_ROLE
269 void index::printSymbolRoles(SymbolRoleSet Roles, raw_ostream &OS) {
270 bool VisitedOnce = false;
271 applyForEachSymbolRole(Roles, [&](SymbolRole Role) {
277 case SymbolRole::Declaration: OS << "Decl"; break;
278 case SymbolRole::Definition: OS << "Def"; break;
279 case SymbolRole::Reference: OS << "Ref"; break;
280 case SymbolRole::Read: OS << "Read"; break;
281 case SymbolRole::Write: OS << "Writ"; break;
282 case SymbolRole::Call: OS << "Call"; break;
283 case SymbolRole::Dynamic: OS << "Dyn"; break;
284 case SymbolRole::AddressOf: OS << "Addr"; break;
285 case SymbolRole::Implicit: OS << "Impl"; break;
286 case SymbolRole::RelationChildOf: OS << "RelChild"; break;
287 case SymbolRole::RelationBaseOf: OS << "RelBase"; break;
288 case SymbolRole::RelationOverrideOf: OS << "RelOver"; break;
289 case SymbolRole::RelationReceivedBy: OS << "RelRec"; break;
290 case SymbolRole::RelationCalledBy: OS << "RelCall"; break;
295 bool index::printSymbolName(const Decl *D, const LangOptions &LO,
297 if (auto *ND = dyn_cast<NamedDecl>(D)) {
298 PrintingPolicy Policy(LO);
299 // Forward references can have different template argument names. Suppress
300 // the template argument names in constructors to make their name more
302 Policy.SuppressTemplateArgsInCXXConstructors = true;
303 DeclarationName DeclName = ND->getDeclName();
304 if (DeclName.isEmpty())
306 DeclName.print(OS, Policy);
313 StringRef index::getSymbolKindString(SymbolKind K) {
315 case SymbolKind::Unknown: return "<unknown>";
316 case SymbolKind::Module: return "module";
317 case SymbolKind::Namespace: return "namespace";
318 case SymbolKind::NamespaceAlias: return "namespace-alias";
319 case SymbolKind::Macro: return "macro";
320 case SymbolKind::Enum: return "enum";
321 case SymbolKind::Struct: return "struct";
322 case SymbolKind::Class: return "class";
323 case SymbolKind::Protocol: return "protocol";
324 case SymbolKind::Extension: return "extension";
325 case SymbolKind::Union: return "union";
326 case SymbolKind::TypeAlias: return "type-alias";
327 case SymbolKind::Function: return "function";
328 case SymbolKind::Variable: return "variable";
329 case SymbolKind::Field: return "field";
330 case SymbolKind::EnumConstant: return "enumerator";
331 case SymbolKind::InstanceMethod: return "instance-method";
332 case SymbolKind::ClassMethod: return "class-method";
333 case SymbolKind::StaticMethod: return "static-method";
334 case SymbolKind::InstanceProperty: return "instance-property";
335 case SymbolKind::ClassProperty: return "class-property";
336 case SymbolKind::StaticProperty: return "static-property";
337 case SymbolKind::Constructor: return "constructor";
338 case SymbolKind::Destructor: return "destructor";
339 case SymbolKind::ConversionFunction: return "coversion-func";
341 llvm_unreachable("invalid symbol kind");
344 StringRef index::getSymbolLanguageString(SymbolLanguage K) {
346 case SymbolLanguage::C: return "C";
347 case SymbolLanguage::ObjC: return "ObjC";
348 case SymbolLanguage::CXX: return "C++";
350 llvm_unreachable("invalid symbol language kind");
353 void index::applyForEachSymbolSubKind(SymbolSubKindSet SubKinds,
354 llvm::function_ref<void(SymbolSubKind)> Fn) {
355 #define APPLY_FOR_SUBKIND(K) \
356 if (SubKinds & (unsigned)SymbolSubKind::K) \
359 APPLY_FOR_SUBKIND(Generic);
360 APPLY_FOR_SUBKIND(TemplatePartialSpecialization);
361 APPLY_FOR_SUBKIND(TemplateSpecialization);
362 APPLY_FOR_SUBKIND(UnitTest);
363 APPLY_FOR_SUBKIND(IBAnnotated);
364 APPLY_FOR_SUBKIND(IBOutletCollection);
366 #undef APPLY_FOR_SUBKIND
369 void index::printSymbolSubKinds(SymbolSubKindSet SubKinds, raw_ostream &OS) {
370 bool VisitedOnce = false;
371 applyForEachSymbolSubKind(SubKinds, [&](SymbolSubKind SubKind) {
377 case SymbolSubKind::Generic: OS << "Gen"; break;
378 case SymbolSubKind::TemplatePartialSpecialization: OS << "TPS"; break;
379 case SymbolSubKind::TemplateSpecialization: OS << "TS"; break;
380 case SymbolSubKind::UnitTest: OS << "test"; break;
381 case SymbolSubKind::IBAnnotated: OS << "IB"; break;
382 case SymbolSubKind::IBOutletCollection: OS << "IBColl"; break;