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.SubKind = SymbolSubKind::None;
57 Info.Properties = SymbolPropertySet();
58 Info.Lang = SymbolLanguage::C;
60 if (const TagDecl *TD = dyn_cast<TagDecl>(D)) {
61 switch (TD->getTagKind()) {
63 Info.Kind = SymbolKind::Struct; break;
65 Info.Kind = SymbolKind::Union; break;
67 Info.Kind = SymbolKind::Class;
68 Info.Lang = SymbolLanguage::CXX;
71 Info.Kind = SymbolKind::Protocol;
72 Info.Lang = SymbolLanguage::CXX;
75 Info.Kind = SymbolKind::Enum; break;
78 if (const CXXRecordDecl *CXXRec = dyn_cast<CXXRecordDecl>(D)) {
79 if (!CXXRec->isCLike()) {
80 Info.Lang = SymbolLanguage::CXX;
81 if (CXXRec->getDescribedClassTemplate()) {
82 Info.Properties |= (unsigned)SymbolProperty::Generic;
87 if (isa<ClassTemplatePartialSpecializationDecl>(D)) {
88 Info.Properties |= (unsigned)SymbolProperty::Generic;
89 Info.Properties |= (unsigned)SymbolProperty::TemplatePartialSpecialization;
90 } else if (isa<ClassTemplateSpecializationDecl>(D)) {
91 Info.Properties |= (unsigned)SymbolProperty::Generic;
92 Info.Properties |= (unsigned)SymbolProperty::TemplateSpecialization;
95 } else if (auto *VD = dyn_cast<VarDecl>(D)) {
96 Info.Kind = SymbolKind::Variable;
97 if (isa<CXXRecordDecl>(D->getDeclContext())) {
98 Info.Kind = SymbolKind::StaticProperty;
99 Info.Lang = SymbolLanguage::CXX;
101 if (isa<VarTemplatePartialSpecializationDecl>(D)) {
102 Info.Lang = SymbolLanguage::CXX;
103 Info.Properties |= (unsigned)SymbolProperty::Generic;
104 Info.Properties |= (unsigned)SymbolProperty::TemplatePartialSpecialization;
105 } else if (isa<VarTemplateSpecializationDecl>(D)) {
106 Info.Lang = SymbolLanguage::CXX;
107 Info.Properties |= (unsigned)SymbolProperty::Generic;
108 Info.Properties |= (unsigned)SymbolProperty::TemplateSpecialization;
109 } else if (VD->getDescribedVarTemplate()) {
110 Info.Lang = SymbolLanguage::CXX;
111 Info.Properties |= (unsigned)SymbolProperty::Generic;
115 switch (D->getKind()) {
117 Info.Kind = SymbolKind::Module;
120 Info.Kind = SymbolKind::TypeAlias; break; // Lang = C
122 Info.Kind = SymbolKind::Function;
125 Info.Kind = SymbolKind::Field;
126 if (const CXXRecordDecl *
127 CXXRec = dyn_cast<CXXRecordDecl>(D->getDeclContext())) {
128 if (!CXXRec->isCLike())
129 Info.Lang = SymbolLanguage::CXX;
132 case Decl::EnumConstant:
133 Info.Kind = SymbolKind::EnumConstant; break;
134 case Decl::ObjCInterface:
135 case Decl::ObjCImplementation: {
136 Info.Kind = SymbolKind::Class;
137 Info.Lang = SymbolLanguage::ObjC;
138 const ObjCInterfaceDecl *ClsD = dyn_cast<ObjCInterfaceDecl>(D);
140 ClsD = cast<ObjCImplementationDecl>(D)->getClassInterface();
141 if (isUnitTestCase(ClsD))
142 Info.Properties |= (unsigned)SymbolProperty::UnitTest;
145 case Decl::ObjCProtocol:
146 Info.Kind = SymbolKind::Protocol;
147 Info.Lang = SymbolLanguage::ObjC;
149 case Decl::ObjCCategory:
150 case Decl::ObjCCategoryImpl:
151 Info.Kind = SymbolKind::Extension;
152 Info.Lang = SymbolLanguage::ObjC;
154 case Decl::ObjCMethod:
155 if (cast<ObjCMethodDecl>(D)->isInstanceMethod())
156 Info.Kind = SymbolKind::InstanceMethod;
158 Info.Kind = SymbolKind::ClassMethod;
159 Info.Lang = SymbolLanguage::ObjC;
160 if (isUnitTest(cast<ObjCMethodDecl>(D)))
161 Info.Properties |= (unsigned)SymbolProperty::UnitTest;
162 if (D->hasAttr<IBActionAttr>())
163 Info.Properties |= (unsigned)SymbolProperty::IBAnnotated;
165 case Decl::ObjCProperty:
166 Info.Kind = SymbolKind::InstanceProperty;
167 Info.Lang = SymbolLanguage::ObjC;
168 checkForIBOutlets(D, Info.Properties);
169 if (auto *Annot = D->getAttr<AnnotateAttr>()) {
170 if (Annot->getAnnotation() == "gk_inspectable")
171 Info.Properties |= (unsigned)SymbolProperty::GKInspectable;
175 Info.Kind = SymbolKind::Field;
176 Info.Lang = SymbolLanguage::ObjC;
177 checkForIBOutlets(D, Info.Properties);
179 case Decl::Namespace:
180 Info.Kind = SymbolKind::Namespace;
181 Info.Lang = SymbolLanguage::CXX;
183 case Decl::NamespaceAlias:
184 Info.Kind = SymbolKind::NamespaceAlias;
185 Info.Lang = SymbolLanguage::CXX;
187 case Decl::CXXConstructor: {
188 Info.Kind = SymbolKind::Constructor;
189 Info.Lang = SymbolLanguage::CXX;
190 auto *CD = cast<CXXConstructorDecl>(D);
191 if (CD->isCopyConstructor())
192 Info.SubKind = SymbolSubKind::CXXCopyConstructor;
193 else if (CD->isMoveConstructor())
194 Info.SubKind = SymbolSubKind::CXXMoveConstructor;
197 case Decl::CXXDestructor:
198 Info.Kind = SymbolKind::Destructor;
199 Info.Lang = SymbolLanguage::CXX;
201 case Decl::CXXConversion:
202 Info.Kind = SymbolKind::ConversionFunction;
203 Info.Lang = SymbolLanguage::CXX;
205 case Decl::CXXMethod: {
206 const CXXMethodDecl *MD = cast<CXXMethodDecl>(D);
208 Info.Kind = SymbolKind::StaticMethod;
210 Info.Kind = SymbolKind::InstanceMethod;
211 Info.Lang = SymbolLanguage::CXX;
214 case Decl::ClassTemplate:
215 Info.Kind = SymbolKind::Class;
216 Info.Properties |= (unsigned)SymbolProperty::Generic;
217 Info.Lang = SymbolLanguage::CXX;
219 case Decl::FunctionTemplate:
220 Info.Kind = SymbolKind::Function;
221 Info.Properties |= (unsigned)SymbolProperty::Generic;
222 Info.Lang = SymbolLanguage::CXX;
223 if (const CXXMethodDecl *MD = dyn_cast_or_null<CXXMethodDecl>(
224 cast<FunctionTemplateDecl>(D)->getTemplatedDecl())) {
225 if (isa<CXXConstructorDecl>(MD))
226 Info.Kind = SymbolKind::Constructor;
227 else if (isa<CXXDestructorDecl>(MD))
228 Info.Kind = SymbolKind::Destructor;
229 else if (isa<CXXConversionDecl>(MD))
230 Info.Kind = SymbolKind::ConversionFunction;
233 Info.Kind = SymbolKind::StaticMethod;
235 Info.Kind = SymbolKind::InstanceMethod;
239 case Decl::TypeAliasTemplate:
240 Info.Kind = SymbolKind::TypeAlias;
241 Info.Lang = SymbolLanguage::CXX;
242 Info.Properties |= (unsigned)SymbolProperty::Generic;
244 case Decl::TypeAlias:
245 Info.Kind = SymbolKind::TypeAlias;
246 Info.Lang = SymbolLanguage::CXX;
253 if (Info.Kind == SymbolKind::Unknown)
256 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
257 if (FD->getTemplatedKind() ==
258 FunctionDecl::TK_FunctionTemplateSpecialization) {
259 Info.Properties |= (unsigned)SymbolProperty::Generic;
260 Info.Properties |= (unsigned)SymbolProperty::TemplateSpecialization;
264 if (Info.Properties & (unsigned)SymbolProperty::Generic)
265 Info.Lang = SymbolLanguage::CXX;
270 void index::applyForEachSymbolRole(SymbolRoleSet Roles,
271 llvm::function_ref<void(SymbolRole)> Fn) {
272 #define APPLY_FOR_ROLE(Role) \
273 if (Roles & (unsigned)SymbolRole::Role) \
276 APPLY_FOR_ROLE(Declaration);
277 APPLY_FOR_ROLE(Definition);
278 APPLY_FOR_ROLE(Reference);
279 APPLY_FOR_ROLE(Read);
280 APPLY_FOR_ROLE(Write);
281 APPLY_FOR_ROLE(Call);
282 APPLY_FOR_ROLE(Dynamic);
283 APPLY_FOR_ROLE(AddressOf);
284 APPLY_FOR_ROLE(Implicit);
285 APPLY_FOR_ROLE(RelationChildOf);
286 APPLY_FOR_ROLE(RelationBaseOf);
287 APPLY_FOR_ROLE(RelationOverrideOf);
288 APPLY_FOR_ROLE(RelationReceivedBy);
289 APPLY_FOR_ROLE(RelationCalledBy);
290 APPLY_FOR_ROLE(RelationExtendedBy);
291 APPLY_FOR_ROLE(RelationAccessorOf);
293 #undef APPLY_FOR_ROLE
296 void index::printSymbolRoles(SymbolRoleSet Roles, raw_ostream &OS) {
297 bool VisitedOnce = false;
298 applyForEachSymbolRole(Roles, [&](SymbolRole Role) {
304 case SymbolRole::Declaration: OS << "Decl"; break;
305 case SymbolRole::Definition: OS << "Def"; break;
306 case SymbolRole::Reference: OS << "Ref"; break;
307 case SymbolRole::Read: OS << "Read"; break;
308 case SymbolRole::Write: OS << "Writ"; break;
309 case SymbolRole::Call: OS << "Call"; break;
310 case SymbolRole::Dynamic: OS << "Dyn"; break;
311 case SymbolRole::AddressOf: OS << "Addr"; break;
312 case SymbolRole::Implicit: OS << "Impl"; break;
313 case SymbolRole::RelationChildOf: OS << "RelChild"; break;
314 case SymbolRole::RelationBaseOf: OS << "RelBase"; break;
315 case SymbolRole::RelationOverrideOf: OS << "RelOver"; break;
316 case SymbolRole::RelationReceivedBy: OS << "RelRec"; break;
317 case SymbolRole::RelationCalledBy: OS << "RelCall"; break;
318 case SymbolRole::RelationExtendedBy: OS << "RelExt"; break;
319 case SymbolRole::RelationAccessorOf: OS << "RelAcc"; break;
324 bool index::printSymbolName(const Decl *D, const LangOptions &LO,
326 if (auto *ND = dyn_cast<NamedDecl>(D)) {
327 PrintingPolicy Policy(LO);
328 // Forward references can have different template argument names. Suppress
329 // the template argument names in constructors to make their name more
331 Policy.SuppressTemplateArgsInCXXConstructors = true;
332 DeclarationName DeclName = ND->getDeclName();
333 if (DeclName.isEmpty())
335 DeclName.print(OS, Policy);
342 StringRef index::getSymbolKindString(SymbolKind K) {
344 case SymbolKind::Unknown: return "<unknown>";
345 case SymbolKind::Module: return "module";
346 case SymbolKind::Namespace: return "namespace";
347 case SymbolKind::NamespaceAlias: return "namespace-alias";
348 case SymbolKind::Macro: return "macro";
349 case SymbolKind::Enum: return "enum";
350 case SymbolKind::Struct: return "struct";
351 case SymbolKind::Class: return "class";
352 case SymbolKind::Protocol: return "protocol";
353 case SymbolKind::Extension: return "extension";
354 case SymbolKind::Union: return "union";
355 case SymbolKind::TypeAlias: return "type-alias";
356 case SymbolKind::Function: return "function";
357 case SymbolKind::Variable: return "variable";
358 case SymbolKind::Field: return "field";
359 case SymbolKind::EnumConstant: return "enumerator";
360 case SymbolKind::InstanceMethod: return "instance-method";
361 case SymbolKind::ClassMethod: return "class-method";
362 case SymbolKind::StaticMethod: return "static-method";
363 case SymbolKind::InstanceProperty: return "instance-property";
364 case SymbolKind::ClassProperty: return "class-property";
365 case SymbolKind::StaticProperty: return "static-property";
366 case SymbolKind::Constructor: return "constructor";
367 case SymbolKind::Destructor: return "destructor";
368 case SymbolKind::ConversionFunction: return "coversion-func";
370 llvm_unreachable("invalid symbol kind");
373 StringRef index::getSymbolSubKindString(SymbolSubKind K) {
375 case SymbolSubKind::None: return "<none>";
376 case SymbolSubKind::CXXCopyConstructor: return "cxx-copy-ctor";
377 case SymbolSubKind::CXXMoveConstructor: return "cxx-move-ctor";
379 llvm_unreachable("invalid symbol subkind");
382 StringRef index::getSymbolLanguageString(SymbolLanguage K) {
384 case SymbolLanguage::C: return "C";
385 case SymbolLanguage::ObjC: return "ObjC";
386 case SymbolLanguage::CXX: return "C++";
388 llvm_unreachable("invalid symbol language kind");
391 void index::applyForEachSymbolProperty(SymbolPropertySet Props,
392 llvm::function_ref<void(SymbolProperty)> Fn) {
393 #define APPLY_FOR_PROPERTY(K) \
394 if (Props & (unsigned)SymbolProperty::K) \
395 Fn(SymbolProperty::K)
397 APPLY_FOR_PROPERTY(Generic);
398 APPLY_FOR_PROPERTY(TemplatePartialSpecialization);
399 APPLY_FOR_PROPERTY(TemplateSpecialization);
400 APPLY_FOR_PROPERTY(UnitTest);
401 APPLY_FOR_PROPERTY(IBAnnotated);
402 APPLY_FOR_PROPERTY(IBOutletCollection);
403 APPLY_FOR_PROPERTY(GKInspectable);
405 #undef APPLY_FOR_PROPERTY
408 void index::printSymbolProperties(SymbolPropertySet Props, raw_ostream &OS) {
409 bool VisitedOnce = false;
410 applyForEachSymbolProperty(Props, [&](SymbolProperty Prop) {
416 case SymbolProperty::Generic: OS << "Gen"; break;
417 case SymbolProperty::TemplatePartialSpecialization: OS << "TPS"; break;
418 case SymbolProperty::TemplateSpecialization: OS << "TS"; break;
419 case SymbolProperty::UnitTest: OS << "test"; break;
420 case SymbolProperty::IBAnnotated: OS << "IB"; break;
421 case SymbolProperty::IBOutletCollection: OS << "IBColl"; break;
422 case SymbolProperty::GKInspectable: OS << "GKI"; break;