clang 22.0.0git
IndexSymbol.cpp
Go to the documentation of this file.
1//===--- IndexSymbol.cpp - Types and functions for indexing symbols -------===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8
10#include "clang/AST/Attr.h"
11#include "clang/AST/DeclCXX.h"
12#include "clang/AST/DeclObjC.h"
15#include "clang/Lex/MacroInfo.h"
16
17using namespace clang;
18using namespace clang::index;
19
20/// \returns true if \c D is a subclass of 'XCTestCase'.
21static bool isUnitTestCase(const ObjCInterfaceDecl *D) {
22 if (!D)
23 return false;
24 while (const ObjCInterfaceDecl *SuperD = D->getSuperClass()) {
25 if (SuperD->getName() == "XCTestCase")
26 return true;
27 D = SuperD;
28 }
29 return false;
30}
31
32/// \returns true if \c D is in a subclass of 'XCTestCase', returns void, has
33/// no parameters, and its name starts with 'test'.
34static bool isUnitTest(const ObjCMethodDecl *D) {
35 if (!D->parameters().empty())
36 return false;
37 if (!D->getReturnType()->isVoidType())
38 return false;
39 if (!D->getSelector().getNameForSlot(0).starts_with("test"))
40 return false;
42}
43
44static void checkForIBOutlets(const Decl *D, SymbolPropertySet &PropSet) {
45 if (D->hasAttr<IBOutletAttr>()) {
47 } else if (D->hasAttr<IBOutletCollectionAttr>()) {
50 }
51}
52
54 assert(D);
55
56 if (isa<ParmVarDecl>(D))
57 return true;
58
60 return true;
61
63 return false;
65 return false;
66
67 if (const NamedDecl *ND = dyn_cast<NamedDecl>(D)) {
68 switch (ND->getFormalLinkage()) {
70 llvm_unreachable("Linkage hasn't been computed!");
71 case Linkage::None:
73 return true;
76 llvm_unreachable("Not a sema linkage");
77 case Linkage::Module:
79 return false;
80 }
81 }
82
83 return true;
84}
85
87 assert(D);
88 SymbolInfo Info;
93
94 if (isFunctionLocalSymbol(D)) {
96 }
99 }
100
101 if (auto *VT = dyn_cast<VarTemplateDecl>(D)) {
104 // All other fields are filled from the templated decl.
105 D = VT->getTemplatedDecl();
106 }
107
108 if (const TagDecl *TD = dyn_cast<TagDecl>(D)) {
109 switch (TD->getTagKind()) {
111 Info.Kind = SymbolKind::Struct; break;
113 Info.Kind = SymbolKind::Union; break;
115 Info.Kind = SymbolKind::Class;
117 break;
121 break;
123 Info.Kind = SymbolKind::Enum; break;
124 }
125
126 if (const CXXRecordDecl *CXXRec = dyn_cast<CXXRecordDecl>(D)) {
127 if (!CXXRec->isCLike()) {
129 if (CXXRec->getDescribedClassTemplate()) {
131 }
132 }
133 }
134
137 Info.Properties |=
141 Info.Properties |=
143 }
144
145 } else if (auto *VD = dyn_cast<VarDecl>(D)) {
147 if (isa<ParmVarDecl>(D)) {
149 } else if (isa<CXXRecordDecl>(D->getDeclContext())) {
152 }
153
157 Info.Properties |=
162 Info.Properties |=
164 } else if (VD->getDescribedVarTemplate()) {
167 }
168
169 } else {
170 switch (D->getKind()) {
171 case Decl::Import:
173 break;
174 case Decl::Typedef:
175 Info.Kind = SymbolKind::TypeAlias; break; // Lang = C
176 case Decl::Function:
178 break;
179 case Decl::Field:
180 case Decl::IndirectField:
181 Info.Kind = SymbolKind::Field;
182 if (const CXXRecordDecl *
183 CXXRec = dyn_cast<CXXRecordDecl>(D->getDeclContext())) {
184 if (!CXXRec->isCLike())
186 }
187 break;
188 case Decl::EnumConstant:
189 Info.Kind = SymbolKind::EnumConstant; break;
190 case Decl::ObjCInterface:
191 case Decl::ObjCImplementation: {
192 Info.Kind = SymbolKind::Class;
194 const ObjCInterfaceDecl *ClsD = dyn_cast<ObjCInterfaceDecl>(D);
195 if (!ClsD)
196 ClsD = cast<ObjCImplementationDecl>(D)->getClassInterface();
197 if (isUnitTestCase(ClsD))
199 break;
200 }
201 case Decl::ObjCProtocol:
204 break;
205 case Decl::ObjCCategory:
206 case Decl::ObjCCategoryImpl: {
209 const ObjCInterfaceDecl *ClsD = nullptr;
210 if (auto *CatD = dyn_cast<ObjCCategoryDecl>(D))
211 ClsD = CatD->getClassInterface();
212 else
213 ClsD = cast<ObjCCategoryImplDecl>(D)->getClassInterface();
214 if (isUnitTestCase(ClsD))
216 break;
217 }
218 case Decl::ObjCMethod: {
221 if (MD->isPropertyAccessor()) {
222 if (MD->param_size())
224 else
226 }
228 if (isUnitTest(MD))
230 if (D->hasAttr<IBActionAttr>())
232 break;
233 }
234 case Decl::ObjCProperty:
238 if (auto *Annot = D->getAttr<AnnotateAttr>()) {
239 if (Annot->getAnnotation() == "gk_inspectable")
241 }
242 break;
243 case Decl::ObjCIvar:
244 Info.Kind = SymbolKind::Field;
247 break;
248 case Decl::Namespace:
251 break;
252 case Decl::NamespaceAlias:
255 break;
256 case Decl::CXXConstructor: {
259 auto *CD = cast<CXXConstructorDecl>(D);
260 if (CD->isCopyConstructor())
262 else if (CD->isMoveConstructor())
264 break;
265 }
266 case Decl::CXXDestructor:
269 break;
270 case Decl::CXXConversion:
273 break;
274 case Decl::CXXMethod: {
275 const CXXMethodDecl *MD = cast<CXXMethodDecl>(D);
276 if (MD->isStatic())
278 else
281 break;
282 }
283 case Decl::ClassTemplate:
284 Info.Kind = SymbolKind::Class;
287 break;
288 case Decl::FunctionTemplate:
292 if (const CXXMethodDecl *MD = dyn_cast_or_null<CXXMethodDecl>(
293 cast<FunctionTemplateDecl>(D)->getTemplatedDecl())) {
296 else if (isa<CXXDestructorDecl>(MD))
298 else if (isa<CXXConversionDecl>(MD))
300 else {
301 if (MD->isStatic())
303 else
305 }
306 }
307 break;
308 case Decl::TypeAliasTemplate:
312 break;
313 case Decl::TypeAlias:
316 break;
317 case Decl::UnresolvedUsingTypename:
318 Info.Kind = SymbolKind::Using;
322 break;
323 case Decl::UnresolvedUsingValue:
324 Info.Kind = SymbolKind::Using;
328 break;
329 case Decl::Using:
330 Info.Kind = SymbolKind::Using;
332 break;
333 case Decl::UsingEnum:
334 Info.Kind = SymbolKind::Using;
337 break;
338 case Decl::Binding:
341 break;
342 case Decl::MSProperty:
344 if (const CXXRecordDecl *CXXRec =
345 dyn_cast<CXXRecordDecl>(D->getDeclContext())) {
346 if (!CXXRec->isCLike())
348 }
349 break;
350 case Decl::ClassTemplatePartialSpecialization:
351 case Decl::ClassTemplateSpecialization:
352 case Decl::CXXRecord:
353 case Decl::Enum:
354 case Decl::Record:
355 llvm_unreachable("records handled before");
356 break;
357 case Decl::VarTemplateSpecialization:
358 case Decl::VarTemplatePartialSpecialization:
359 case Decl::ImplicitParam:
360 case Decl::ParmVar:
361 case Decl::Var:
362 case Decl::VarTemplate:
363 llvm_unreachable("variables handled before");
364 break;
365 case Decl::TemplateTypeParm:
367 break;
368 case Decl::TemplateTemplateParm:
370 break;
371 case Decl::NonTypeTemplateParm:
373 break;
374 case Decl::Concept:
376 break;
377 // Other decls get the 'unknown' kind.
378 default:
379 break;
380 }
381 }
382
383 if (Info.Kind == SymbolKind::Unknown)
384 return Info;
385
386 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
387 if (FD->getTemplatedKind() ==
390 Info.Properties |=
392 }
393 }
394
397
398 if (auto *attr = D->getExternalSourceSymbolAttr()) {
399 if (attr->getLanguage() == "Swift")
401 }
402
403 return Info;
404}
405
407 SymbolInfo Info;
408 Info.Kind = SymbolKind::Macro;
411 Info.Lang = SymbolLanguage::C;
412 return Info;
413}
414
448
450 llvm::function_ref<void(SymbolRole)> Fn) {
451 applyForEachSymbolRoleInterruptible(Roles, [&](SymbolRole r) -> bool {
452 Fn(r);
453 return true;
454 });
455}
456
457void index::printSymbolRoles(SymbolRoleSet Roles, raw_ostream &OS) {
458 bool VisitedOnce = false;
460 if (VisitedOnce)
461 OS << ',';
462 else
463 VisitedOnce = true;
464 switch (Role) {
465 case SymbolRole::Declaration: OS << "Decl"; break;
466 case SymbolRole::Definition: OS << "Def"; break;
467 case SymbolRole::Reference: OS << "Ref"; break;
468 case SymbolRole::Read: OS << "Read"; break;
469 case SymbolRole::Write: OS << "Writ"; break;
470 case SymbolRole::Call: OS << "Call"; break;
471 case SymbolRole::Dynamic: OS << "Dyn"; break;
472 case SymbolRole::AddressOf: OS << "Addr"; break;
473 case SymbolRole::Implicit: OS << "Impl"; break;
474 case SymbolRole::Undefinition: OS << "Undef"; break;
475 case SymbolRole::RelationChildOf: OS << "RelChild"; break;
476 case SymbolRole::RelationBaseOf: OS << "RelBase"; break;
477 case SymbolRole::RelationOverrideOf: OS << "RelOver"; break;
478 case SymbolRole::RelationReceivedBy: OS << "RelRec"; break;
479 case SymbolRole::RelationCalledBy: OS << "RelCall"; break;
480 case SymbolRole::RelationExtendedBy: OS << "RelExt"; break;
481 case SymbolRole::RelationAccessorOf: OS << "RelAcc"; break;
482 case SymbolRole::RelationContainedBy: OS << "RelCont"; break;
483 case SymbolRole::RelationIBTypeOf: OS << "RelIBType"; break;
484 case SymbolRole::RelationSpecializationOf: OS << "RelSpecialization"; break;
485 case SymbolRole::NameReference: OS << "NameReference"; break;
486 }
487 });
488}
489
490bool index::printSymbolName(const Decl *D, const LangOptions &LO,
491 raw_ostream &OS) {
492 if (auto *ND = dyn_cast<NamedDecl>(D)) {
493 PrintingPolicy Policy(LO);
494 // Forward references can have different template argument names. Suppress
495 // the template argument names in constructors to make their name more
496 // stable.
498 DeclarationName DeclName = ND->getDeclName();
499 if (DeclName.isEmpty())
500 return true;
501 DeclName.print(OS, Policy);
502 return false;
503 } else {
504 return true;
505 }
506}
507
509 switch (K) {
510 // FIXME: for backwards compatibility, the include directive kind is treated
511 // the same as Unknown
513 case SymbolKind::Unknown: return "<unknown>";
514 case SymbolKind::Module: return "module";
515 case SymbolKind::Namespace: return "namespace";
516 case SymbolKind::NamespaceAlias: return "namespace-alias";
517 case SymbolKind::Macro: return "macro";
518 case SymbolKind::Enum: return "enum";
519 case SymbolKind::Struct: return "struct";
520 case SymbolKind::Class: return "class";
521 case SymbolKind::Protocol: return "protocol";
522 case SymbolKind::Extension: return "extension";
523 case SymbolKind::Union: return "union";
524 case SymbolKind::TypeAlias: return "type-alias";
525 case SymbolKind::Function: return "function";
526 case SymbolKind::Variable: return "variable";
527 case SymbolKind::Field: return "field";
528 case SymbolKind::EnumConstant: return "enumerator";
529 case SymbolKind::InstanceMethod: return "instance-method";
530 case SymbolKind::ClassMethod: return "class-method";
531 case SymbolKind::StaticMethod: return "static-method";
532 case SymbolKind::InstanceProperty: return "instance-property";
533 case SymbolKind::ClassProperty: return "class-property";
534 case SymbolKind::StaticProperty: return "static-property";
535 case SymbolKind::Constructor: return "constructor";
536 case SymbolKind::Destructor: return "destructor";
537 case SymbolKind::ConversionFunction: return "conversion-func";
538 case SymbolKind::Parameter: return "param";
539 case SymbolKind::Using: return "using";
540 case SymbolKind::TemplateTypeParm: return "template-type-param";
541 case SymbolKind::TemplateTemplateParm: return "template-template-param";
542 case SymbolKind::NonTypeTemplateParm: return "non-type-template-param";
544 return "concept";
545 }
546 llvm_unreachable("invalid symbol kind");
547}
548
550 switch (K) {
551 case SymbolSubKind::None: return "<none>";
552 case SymbolSubKind::CXXCopyConstructor: return "cxx-copy-ctor";
553 case SymbolSubKind::CXXMoveConstructor: return "cxx-move-ctor";
554 case SymbolSubKind::AccessorGetter: return "acc-get";
555 case SymbolSubKind::AccessorSetter: return "acc-set";
556 case SymbolSubKind::UsingTypename: return "using-typename";
557 case SymbolSubKind::UsingValue: return "using-value";
558 case SymbolSubKind::UsingEnum: return "using-enum";
559 }
560 llvm_unreachable("invalid symbol subkind");
561}
562
564 switch (K) {
565 case SymbolLanguage::C: return "C";
566 case SymbolLanguage::ObjC: return "ObjC";
567 case SymbolLanguage::CXX: return "C++";
568 case SymbolLanguage::Swift: return "Swift";
569 }
570 llvm_unreachable("invalid symbol language kind");
571}
572
574 llvm::function_ref<void(SymbolProperty)> Fn) {
575#define APPLY_FOR_PROPERTY(K) \
576 if (Props & (SymbolPropertySet)SymbolProperty::K) \
577 Fn(SymbolProperty::K)
578
588
589#undef APPLY_FOR_PROPERTY
590}
591
593 bool VisitedOnce = false;
595 if (VisitedOnce)
596 OS << ',';
597 else
598 VisitedOnce = true;
599 switch (Prop) {
600 case SymbolProperty::Generic: OS << "Gen"; break;
601 case SymbolProperty::TemplatePartialSpecialization: OS << "TPS"; break;
602 case SymbolProperty::TemplateSpecialization: OS << "TS"; break;
603 case SymbolProperty::UnitTest: OS << "test"; break;
604 case SymbolProperty::IBAnnotated: OS << "IB"; break;
605 case SymbolProperty::IBOutletCollection: OS << "IBColl"; break;
606 case SymbolProperty::GKInspectable: OS << "GKI"; break;
607 case SymbolProperty::Local: OS << "local"; break;
608 case SymbolProperty::ProtocolInterface: OS << "protocol"; break;
609 }
610 });
611}
Defines the C++ Decl subclasses, other than those for templates (found in DeclTemplate....
Defines the C++ template declaration subclasses.
std::shared_ptr< TokenRole > Role
A token can have a special role that can carry extra information about the token's formatting.
#define APPLY_FOR_PROPERTY(K)
static bool isUnitTest(const ObjCMethodDecl *D)
#define APPLY_FOR_ROLE(Role)
static void checkForIBOutlets(const Decl *D, SymbolPropertySet &PropSet)
static bool isUnitTestCase(const ObjCInterfaceDecl *D)
Defines the clang::MacroInfo and clang::MacroDirective classes.
Represents a static or instance method of a struct/union/class.
Definition DeclCXX.h:2129
bool isStatic() const
Definition DeclCXX.cpp:2401
Represents a C++ struct/union/class.
Definition DeclCXX.h:258
Decl - This represents one declaration (or definition), e.g.
Definition DeclBase.h:86
const DeclContext * getParentFunctionOrMethod(bool LexicalParent=false) const
If this decl is defined inside a function/method/block it returns the corresponding DeclContext,...
Definition DeclBase.cpp:319
T * getAttr() const
Definition DeclBase.h:573
ExternalSourceSymbolAttr * getExternalSourceSymbolAttr() const
Looks on this and related declarations for an applicable external source symbol attribute.
Definition DeclBase.cpp:590
DeclContext * getDeclContext()
Definition DeclBase.h:448
bool hasAttr() const
Definition DeclBase.h:577
Kind getKind() const
Definition DeclBase.h:442
The name of a declaration.
void print(raw_ostream &OS, const PrintingPolicy &Policy) const
bool isEmpty() const
Evaluates true when this declaration name is empty.
Represents a function declaration or definition.
Definition Decl.h:1999
@ TK_FunctionTemplateSpecialization
Definition Decl.h:2015
Keeps track of the various options that can be enabled, which controls the dialect of C or C++ that i...
Encapsulates the data about a macro definition (e.g.
Definition MacroInfo.h:39
This represents a decl that may have a name.
Definition Decl.h:273
Represents an ObjC class declaration.
Definition DeclObjC.h:1154
ObjCInterfaceDecl * getSuperClass() const
Definition DeclObjC.cpp:349
ObjCMethodDecl - Represents an instance or class method declaration.
Definition DeclObjC.h:140
ArrayRef< ParmVarDecl * > parameters() const
Definition DeclObjC.h:373
unsigned param_size() const
Definition DeclObjC.h:347
bool isPropertyAccessor() const
Definition DeclObjC.h:436
Selector getSelector() const
Definition DeclObjC.h:327
bool isInstanceMethod() const
Definition DeclObjC.h:426
QualType getReturnType() const
Definition DeclObjC.h:329
ObjCInterfaceDecl * getClassInterface()
StringRef getNameForSlot(unsigned argIndex) const
Retrieve the name at a given position in the selector.
Represents the declaration of a struct/union/class/enum.
Definition Decl.h:3714
bool isVoidType() const
Definition TypeBase.h:8878
void applyForEachSymbolProperty(SymbolPropertySet Props, llvm::function_ref< void(SymbolProperty)> Fn)
SymbolRole
Set of roles that are attributed to symbol occurrences.
StringRef getSymbolSubKindString(SymbolSubKind K)
void printSymbolProperties(SymbolPropertySet Props, raw_ostream &OS)
SymbolInfo getSymbolInfo(const Decl *D)
StringRef getSymbolKindString(SymbolKind K)
bool isFunctionLocalSymbol(const Decl *D)
void applyForEachSymbolRole(SymbolRoleSet Roles, llvm::function_ref< void(SymbolRole)> Fn)
void printSymbolRoles(SymbolRoleSet Roles, raw_ostream &OS)
SymbolInfo getSymbolInfoForMacro(const MacroInfo &MI)
bool printSymbolName(const Decl *D, const LangOptions &LO, raw_ostream &OS)
unsigned SymbolRoleSet
bool applyForEachSymbolRoleInterruptible(SymbolRoleSet Roles, llvm::function_ref< bool(SymbolRole)> Fn)
SymbolProperty
Set of properties that provide additional info about a symbol.
Definition IndexSymbol.h:86
@ ProtocolInterface
Symbol is part of a protocol interface.
Definition IndexSymbol.h:96
uint16_t SymbolPropertySet
Definition IndexSymbol.h:84
StringRef getSymbolLanguageString(SymbolLanguage K)
SymbolSubKind
Language specific sub-kinds.
Definition IndexSymbol.h:73
The JSON file list parser is used to communicate input to InstallAPI.
bool isa(CodeGen::Address addr)
Definition Address.h:330
@ VisibleNone
No linkage according to the standard, but is visible from other translation units because of types de...
Definition Linkage.h:48
@ None
No linkage, which means that the entity is unique and can only be referred to from within its scope.
Definition Linkage.h:30
@ UniqueExternal
External linkage within a unique namespace.
Definition Linkage.h:44
@ Internal
Internal linkage, which indicates that the entity can be referred to from within the translation unit...
Definition Linkage.h:35
@ External
External linkage, which indicates that the entity can be referred to from other translation units.
Definition Linkage.h:58
@ Module
Module linkage, which indicates that the entity can be referred to from other translation units withi...
Definition Linkage.h:54
@ Interface
The "__interface" keyword.
Definition TypeBase.h:5893
@ Struct
The "struct" keyword.
Definition TypeBase.h:5890
@ Class
The "class" keyword.
Definition TypeBase.h:5899
@ Union
The "union" keyword.
Definition TypeBase.h:5896
@ Enum
The "enum" keyword.
Definition TypeBase.h:5902
U cast(CodeGen::Address addr)
Definition Address.h:327
Describes how types, statements, expressions, and declarations should be printed.
unsigned SuppressTemplateArgsInCXXConstructors
When true, suppresses printing template arguments in names of C++ constructors.
SymbolPropertySet Properties