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 SymbolInfo index::getSymbolInfo(const Decl *D) {
55 Info.Kind = SymbolKind::Unknown;
56 Info.Properties = SymbolPropertySet();
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;
80 if (CXXRec->getDescribedClassTemplate()) {
81 Info.Properties |= (unsigned)SymbolProperty::Generic;
86 if (isa<ClassTemplatePartialSpecializationDecl>(D)) {
87 Info.Properties |= (unsigned)SymbolProperty::Generic;
88 Info.Properties |= (unsigned)SymbolProperty::TemplatePartialSpecialization;
89 } else if (isa<ClassTemplateSpecializationDecl>(D)) {
90 Info.Properties |= (unsigned)SymbolProperty::Generic;
91 Info.Properties |= (unsigned)SymbolProperty::TemplateSpecialization;
94 } else if (auto *VD = dyn_cast<VarDecl>(D)) {
95 Info.Kind = SymbolKind::Variable;
96 if (isa<CXXRecordDecl>(D->getDeclContext())) {
97 Info.Kind = SymbolKind::StaticProperty;
98 Info.Lang = SymbolLanguage::CXX;
100 if (isa<VarTemplatePartialSpecializationDecl>(D)) {
101 Info.Lang = SymbolLanguage::CXX;
102 Info.Properties |= (unsigned)SymbolProperty::Generic;
103 Info.Properties |= (unsigned)SymbolProperty::TemplatePartialSpecialization;
104 } else if (isa<VarTemplateSpecializationDecl>(D)) {
105 Info.Lang = SymbolLanguage::CXX;
106 Info.Properties |= (unsigned)SymbolProperty::Generic;
107 Info.Properties |= (unsigned)SymbolProperty::TemplateSpecialization;
108 } else if (VD->getDescribedVarTemplate()) {
109 Info.Lang = SymbolLanguage::CXX;
110 Info.Properties |= (unsigned)SymbolProperty::Generic;
114 switch (D->getKind()) {
116 Info.Kind = SymbolKind::Module;
119 Info.Kind = SymbolKind::TypeAlias; break; // Lang = C
121 Info.Kind = SymbolKind::Function;
124 Info.Kind = SymbolKind::Field;
125 if (const CXXRecordDecl *
126 CXXRec = dyn_cast<CXXRecordDecl>(D->getDeclContext())) {
127 if (!CXXRec->isCLike())
128 Info.Lang = SymbolLanguage::CXX;
131 case Decl::EnumConstant:
132 Info.Kind = SymbolKind::EnumConstant; break;
133 case Decl::ObjCInterface:
134 case Decl::ObjCImplementation: {
135 Info.Kind = SymbolKind::Class;
136 Info.Lang = SymbolLanguage::ObjC;
137 const ObjCInterfaceDecl *ClsD = dyn_cast<ObjCInterfaceDecl>(D);
139 ClsD = cast<ObjCImplementationDecl>(D)->getClassInterface();
140 if (isUnitTestCase(ClsD))
141 Info.Properties |= (unsigned)SymbolProperty::UnitTest;
144 case Decl::ObjCProtocol:
145 Info.Kind = SymbolKind::Protocol;
146 Info.Lang = SymbolLanguage::ObjC;
148 case Decl::ObjCCategory:
149 case Decl::ObjCCategoryImpl:
150 Info.Kind = SymbolKind::Extension;
151 Info.Lang = SymbolLanguage::ObjC;
153 case Decl::ObjCMethod:
154 if (cast<ObjCMethodDecl>(D)->isInstanceMethod())
155 Info.Kind = SymbolKind::InstanceMethod;
157 Info.Kind = SymbolKind::ClassMethod;
158 Info.Lang = SymbolLanguage::ObjC;
159 if (isUnitTest(cast<ObjCMethodDecl>(D)))
160 Info.Properties |= (unsigned)SymbolProperty::UnitTest;
161 if (D->hasAttr<IBActionAttr>())
162 Info.Properties |= (unsigned)SymbolProperty::IBAnnotated;
164 case Decl::ObjCProperty:
165 Info.Kind = SymbolKind::InstanceProperty;
166 Info.Lang = SymbolLanguage::ObjC;
167 checkForIBOutlets(D, Info.Properties);
168 if (auto *Annot = D->getAttr<AnnotateAttr>()) {
169 if (Annot->getAnnotation() == "gk_inspectable")
170 Info.Properties |= (unsigned)SymbolProperty::GKInspectable;
174 Info.Kind = SymbolKind::Field;
175 Info.Lang = SymbolLanguage::ObjC;
176 checkForIBOutlets(D, Info.Properties);
178 case Decl::Namespace:
179 Info.Kind = SymbolKind::Namespace;
180 Info.Lang = SymbolLanguage::CXX;
182 case Decl::NamespaceAlias:
183 Info.Kind = SymbolKind::NamespaceAlias;
184 Info.Lang = SymbolLanguage::CXX;
186 case Decl::CXXConstructor:
187 Info.Kind = SymbolKind::Constructor;
188 Info.Lang = SymbolLanguage::CXX;
190 case Decl::CXXDestructor:
191 Info.Kind = SymbolKind::Destructor;
192 Info.Lang = SymbolLanguage::CXX;
194 case Decl::CXXConversion:
195 Info.Kind = SymbolKind::ConversionFunction;
196 Info.Lang = SymbolLanguage::CXX;
198 case Decl::CXXMethod: {
199 const CXXMethodDecl *MD = cast<CXXMethodDecl>(D);
201 Info.Kind = SymbolKind::StaticMethod;
203 Info.Kind = SymbolKind::InstanceMethod;
204 Info.Lang = SymbolLanguage::CXX;
207 case Decl::ClassTemplate:
208 Info.Kind = SymbolKind::Class;
209 Info.Properties |= (unsigned)SymbolProperty::Generic;
210 Info.Lang = SymbolLanguage::CXX;
212 case Decl::FunctionTemplate:
213 Info.Kind = SymbolKind::Function;
214 Info.Properties |= (unsigned)SymbolProperty::Generic;
215 Info.Lang = SymbolLanguage::CXX;
216 if (const CXXMethodDecl *MD = dyn_cast_or_null<CXXMethodDecl>(
217 cast<FunctionTemplateDecl>(D)->getTemplatedDecl())) {
218 if (isa<CXXConstructorDecl>(MD))
219 Info.Kind = SymbolKind::Constructor;
220 else if (isa<CXXDestructorDecl>(MD))
221 Info.Kind = SymbolKind::Destructor;
222 else if (isa<CXXConversionDecl>(MD))
223 Info.Kind = SymbolKind::ConversionFunction;
226 Info.Kind = SymbolKind::StaticMethod;
228 Info.Kind = SymbolKind::InstanceMethod;
232 case Decl::TypeAliasTemplate:
233 Info.Kind = SymbolKind::TypeAlias;
234 Info.Lang = SymbolLanguage::CXX;
235 Info.Properties |= (unsigned)SymbolProperty::Generic;
237 case Decl::TypeAlias:
238 Info.Kind = SymbolKind::TypeAlias;
239 Info.Lang = SymbolLanguage::CXX;
246 if (Info.Kind == SymbolKind::Unknown)
249 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
250 if (FD->getTemplatedKind() ==
251 FunctionDecl::TK_FunctionTemplateSpecialization) {
252 Info.Properties |= (unsigned)SymbolProperty::Generic;
253 Info.Properties |= (unsigned)SymbolProperty::TemplateSpecialization;
257 if (Info.Properties & (unsigned)SymbolProperty::Generic)
258 Info.Lang = SymbolLanguage::CXX;
263 void index::applyForEachSymbolRole(SymbolRoleSet Roles,
264 llvm::function_ref<void(SymbolRole)> Fn) {
265 #define APPLY_FOR_ROLE(Role) \
266 if (Roles & (unsigned)SymbolRole::Role) \
269 APPLY_FOR_ROLE(Declaration);
270 APPLY_FOR_ROLE(Definition);
271 APPLY_FOR_ROLE(Reference);
272 APPLY_FOR_ROLE(Read);
273 APPLY_FOR_ROLE(Write);
274 APPLY_FOR_ROLE(Call);
275 APPLY_FOR_ROLE(Dynamic);
276 APPLY_FOR_ROLE(AddressOf);
277 APPLY_FOR_ROLE(Implicit);
278 APPLY_FOR_ROLE(RelationChildOf);
279 APPLY_FOR_ROLE(RelationBaseOf);
280 APPLY_FOR_ROLE(RelationOverrideOf);
281 APPLY_FOR_ROLE(RelationReceivedBy);
282 APPLY_FOR_ROLE(RelationCalledBy);
283 APPLY_FOR_ROLE(RelationExtendedBy);
284 APPLY_FOR_ROLE(RelationAccessorOf);
286 #undef APPLY_FOR_ROLE
289 void index::printSymbolRoles(SymbolRoleSet Roles, raw_ostream &OS) {
290 bool VisitedOnce = false;
291 applyForEachSymbolRole(Roles, [&](SymbolRole Role) {
297 case SymbolRole::Declaration: OS << "Decl"; break;
298 case SymbolRole::Definition: OS << "Def"; break;
299 case SymbolRole::Reference: OS << "Ref"; break;
300 case SymbolRole::Read: OS << "Read"; break;
301 case SymbolRole::Write: OS << "Writ"; break;
302 case SymbolRole::Call: OS << "Call"; break;
303 case SymbolRole::Dynamic: OS << "Dyn"; break;
304 case SymbolRole::AddressOf: OS << "Addr"; break;
305 case SymbolRole::Implicit: OS << "Impl"; break;
306 case SymbolRole::RelationChildOf: OS << "RelChild"; break;
307 case SymbolRole::RelationBaseOf: OS << "RelBase"; break;
308 case SymbolRole::RelationOverrideOf: OS << "RelOver"; break;
309 case SymbolRole::RelationReceivedBy: OS << "RelRec"; break;
310 case SymbolRole::RelationCalledBy: OS << "RelCall"; break;
311 case SymbolRole::RelationExtendedBy: OS << "RelExt"; break;
312 case SymbolRole::RelationAccessorOf: OS << "RelAcc"; break;
317 bool index::printSymbolName(const Decl *D, const LangOptions &LO,
319 if (auto *ND = dyn_cast<NamedDecl>(D)) {
320 PrintingPolicy Policy(LO);
321 // Forward references can have different template argument names. Suppress
322 // the template argument names in constructors to make their name more
324 Policy.SuppressTemplateArgsInCXXConstructors = true;
325 DeclarationName DeclName = ND->getDeclName();
326 if (DeclName.isEmpty())
328 DeclName.print(OS, Policy);
335 StringRef index::getSymbolKindString(SymbolKind K) {
337 case SymbolKind::Unknown: return "<unknown>";
338 case SymbolKind::Module: return "module";
339 case SymbolKind::Namespace: return "namespace";
340 case SymbolKind::NamespaceAlias: return "namespace-alias";
341 case SymbolKind::Macro: return "macro";
342 case SymbolKind::Enum: return "enum";
343 case SymbolKind::Struct: return "struct";
344 case SymbolKind::Class: return "class";
345 case SymbolKind::Protocol: return "protocol";
346 case SymbolKind::Extension: return "extension";
347 case SymbolKind::Union: return "union";
348 case SymbolKind::TypeAlias: return "type-alias";
349 case SymbolKind::Function: return "function";
350 case SymbolKind::Variable: return "variable";
351 case SymbolKind::Field: return "field";
352 case SymbolKind::EnumConstant: return "enumerator";
353 case SymbolKind::InstanceMethod: return "instance-method";
354 case SymbolKind::ClassMethod: return "class-method";
355 case SymbolKind::StaticMethod: return "static-method";
356 case SymbolKind::InstanceProperty: return "instance-property";
357 case SymbolKind::ClassProperty: return "class-property";
358 case SymbolKind::StaticProperty: return "static-property";
359 case SymbolKind::Constructor: return "constructor";
360 case SymbolKind::Destructor: return "destructor";
361 case SymbolKind::ConversionFunction: return "coversion-func";
363 llvm_unreachable("invalid symbol kind");
366 StringRef index::getSymbolLanguageString(SymbolLanguage K) {
368 case SymbolLanguage::C: return "C";
369 case SymbolLanguage::ObjC: return "ObjC";
370 case SymbolLanguage::CXX: return "C++";
372 llvm_unreachable("invalid symbol language kind");
375 void index::applyForEachSymbolProperty(SymbolPropertySet Props,
376 llvm::function_ref<void(SymbolProperty)> Fn) {
377 #define APPLY_FOR_PROPERTY(K) \
378 if (Props & (unsigned)SymbolProperty::K) \
379 Fn(SymbolProperty::K)
381 APPLY_FOR_PROPERTY(Generic);
382 APPLY_FOR_PROPERTY(TemplatePartialSpecialization);
383 APPLY_FOR_PROPERTY(TemplateSpecialization);
384 APPLY_FOR_PROPERTY(UnitTest);
385 APPLY_FOR_PROPERTY(IBAnnotated);
386 APPLY_FOR_PROPERTY(IBOutletCollection);
387 APPLY_FOR_PROPERTY(GKInspectable);
389 #undef APPLY_FOR_PROPERTY
392 void index::printSymbolProperties(SymbolPropertySet Props, raw_ostream &OS) {
393 bool VisitedOnce = false;
394 applyForEachSymbolProperty(Props, [&](SymbolProperty Prop) {
400 case SymbolProperty::Generic: OS << "Gen"; break;
401 case SymbolProperty::TemplatePartialSpecialization: OS << "TPS"; break;
402 case SymbolProperty::TemplateSpecialization: OS << "TS"; break;
403 case SymbolProperty::UnitTest: OS << "test"; break;
404 case SymbolProperty::IBAnnotated: OS << "IB"; break;
405 case SymbolProperty::IBOutletCollection: OS << "IBColl"; break;
406 case SymbolProperty::GKInspectable: OS << "GKI"; break;