clang 24.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/Decl.h"
12#include "clang/AST/DeclCXX.h"
13#include "clang/AST/DeclObjC.h"
16#include "clang/AST/TypeBase.h"
17#include "clang/Lex/MacroInfo.h"
18
19using namespace clang;
20using namespace clang::index;
21
22/// \returns true if \c D is a subclass of 'XCTestCase'.
23static bool isUnitTestCase(const ObjCInterfaceDecl *D) {
24 if (!D)
25 return false;
26 while (const ObjCInterfaceDecl *SuperD = D->getSuperClass()) {
27 if (SuperD->getName() == "XCTestCase")
28 return true;
29 D = SuperD;
30 }
31 return false;
32}
33
34/// \returns true if \c D is in a subclass of 'XCTestCase', returns void, has
35/// no parameters, and its name starts with 'test'.
36static bool isUnitTest(const ObjCMethodDecl *D) {
37 if (!D->parameters().empty())
38 return false;
39 if (!D->getReturnType()->isVoidType())
40 return false;
41 if (!D->getSelector().getNameForSlot(0).starts_with("test"))
42 return false;
44}
45
46static void checkForIBOutlets(const Decl *D, SymbolPropertySet &PropSet) {
47 if (D->hasAttr<IBOutletAttr>()) {
49 } else if (D->hasAttr<IBOutletCollectionAttr>()) {
52 }
53}
54
56 assert(D);
57
58 if (isa<ParmVarDecl>(D))
59 return true;
60
62 return true;
63
65 return false;
67 return false;
68
69 if (const NamedDecl *ND = dyn_cast<NamedDecl>(D)) {
70 switch (ND->getFormalLinkage()) {
72 llvm_unreachable("Linkage hasn't been computed!");
73 case Linkage::None:
75 return true;
78 llvm_unreachable("Not a sema linkage");
79 case Linkage::Module:
81 return false;
82 }
83 }
84
85 return true;
86}
87
89 if (const TagDecl *TD = TND->getUnderlyingType()->getAsTagDecl()) {
90 switch (TD->getTagKind()) {
95 default:
96 // Leave SymbolSubKind blank.
97 // New subkinds like UsingUnion can be added if/when needed.
99 }
100 }
101 // Not a tag type, e.g. typedef for a builtin type.
102 return SymbolSubKind::None;
103}
104
106 assert(D);
107 SymbolInfo Info;
111 Info.Lang = SymbolLanguage::C;
112
113 if (isFunctionLocalSymbol(D)) {
115 }
118 }
119
120 if (auto *VT = dyn_cast<VarTemplateDecl>(D)) {
123 // All other fields are filled from the templated decl.
124 D = VT->getTemplatedDecl();
125 }
126
127 if (const TagDecl *TD = dyn_cast<TagDecl>(D)) {
128 switch (TD->getTagKind()) {
130 Info.Kind = SymbolKind::Struct; break;
132 Info.Kind = SymbolKind::Union; break;
134 Info.Kind = SymbolKind::Class;
136 break;
140 break;
142 Info.Kind = SymbolKind::Enum; break;
143 }
144
145 if (const CXXRecordDecl *CXXRec = dyn_cast<CXXRecordDecl>(D)) {
146 if (!CXXRec->isCLike()) {
148 if (CXXRec->getDescribedClassTemplate()) {
150 }
151 }
152 }
153
156 Info.Properties |=
160 Info.Properties |=
162 }
163
164 } else if (auto *VD = dyn_cast<VarDecl>(D)) {
166 if (isa<ParmVarDecl>(D)) {
168 } else if (isa<CXXRecordDecl>(D->getDeclContext())) {
171 }
172
176 Info.Properties |=
181 Info.Properties |=
183 } else if (VD->getDescribedVarTemplate()) {
186 }
187
188 } else {
189 switch (D->getKind()) {
190 case Decl::Import:
192 break;
193 case Decl::Typedef: {
194 Info.Kind = SymbolKind::TypeAlias; // Lang = C
196 break;
197 }
198 case Decl::Function:
200 break;
201 case Decl::Field:
202 case Decl::IndirectField:
203 Info.Kind = SymbolKind::Field;
204 if (const CXXRecordDecl *
205 CXXRec = dyn_cast<CXXRecordDecl>(D->getDeclContext())) {
206 if (!CXXRec->isCLike())
208 }
209 break;
210 case Decl::EnumConstant:
211 Info.Kind = SymbolKind::EnumConstant; break;
212 case Decl::ObjCInterface:
213 case Decl::ObjCImplementation: {
214 Info.Kind = SymbolKind::Class;
216 const ObjCInterfaceDecl *ClsD = dyn_cast<ObjCInterfaceDecl>(D);
217 if (!ClsD)
218 ClsD = cast<ObjCImplementationDecl>(D)->getClassInterface();
219 if (isUnitTestCase(ClsD))
221 break;
222 }
223 case Decl::ObjCProtocol:
226 break;
227 case Decl::ObjCCategory:
228 case Decl::ObjCCategoryImpl: {
231 const ObjCInterfaceDecl *ClsD = nullptr;
232 if (auto *CatD = dyn_cast<ObjCCategoryDecl>(D))
233 ClsD = CatD->getClassInterface();
234 else
235 ClsD = cast<ObjCCategoryImplDecl>(D)->getClassInterface();
236 if (isUnitTestCase(ClsD))
238 break;
239 }
240 case Decl::ObjCMethod: {
243 if (MD->isPropertyAccessor()) {
244 if (MD->param_size())
246 else
248 }
250 if (isUnitTest(MD))
252 if (D->hasAttr<IBActionAttr>())
254 break;
255 }
256 case Decl::ObjCProperty:
260 if (auto *Annot = D->getAttr<AnnotateAttr>()) {
261 if (Annot->getAnnotation() == "gk_inspectable")
263 }
264 break;
265 case Decl::ObjCIvar:
266 Info.Kind = SymbolKind::Field;
269 break;
270 case Decl::Namespace:
273 break;
274 case Decl::NamespaceAlias:
277 break;
278 case Decl::CXXConstructor: {
281 auto *CD = cast<CXXConstructorDecl>(D);
282 if (CD->isCopyConstructor())
284 else if (CD->isMoveConstructor())
286 break;
287 }
288 case Decl::CXXDestructor:
291 break;
292 case Decl::CXXConversion:
295 break;
296 case Decl::CXXMethod: {
297 const CXXMethodDecl *MD = cast<CXXMethodDecl>(D);
298 if (MD->isStatic())
300 else
303 break;
304 }
305 case Decl::ClassTemplate: {
312 break;
313 }
314 case Decl::FunctionTemplate:
318 if (const CXXMethodDecl *MD = dyn_cast_or_null<CXXMethodDecl>(
319 cast<FunctionTemplateDecl>(D)->getTemplatedDecl())) {
322 else if (isa<CXXDestructorDecl>(MD))
324 else if (isa<CXXConversionDecl>(MD))
326 else {
327 if (MD->isStatic())
329 else
331 }
332 }
333 break;
334 case Decl::TypeAliasTemplate:
338 break;
339 case Decl::TypeAlias: {
343 break;
344 }
345 case Decl::UnresolvedUsingTypename:
346 Info.Kind = SymbolKind::Using;
350 break;
351 case Decl::UnresolvedUsingValue:
352 Info.Kind = SymbolKind::Using;
356 break;
357 case Decl::Using:
358 Info.Kind = SymbolKind::Using;
360 break;
361 case Decl::UsingEnum:
362 Info.Kind = SymbolKind::Using;
365 break;
366 case Decl::Binding:
369 break;
370 case Decl::MSProperty:
372 if (const CXXRecordDecl *CXXRec =
373 dyn_cast<CXXRecordDecl>(D->getDeclContext())) {
374 if (!CXXRec->isCLike())
376 }
377 break;
378 case Decl::ClassTemplatePartialSpecialization:
379 case Decl::ClassTemplateSpecialization:
380 case Decl::CXXRecord:
381 case Decl::Enum:
382 case Decl::Record:
383 llvm_unreachable("records handled before");
384 break;
385 case Decl::VarTemplateSpecialization:
386 case Decl::VarTemplatePartialSpecialization:
387 case Decl::ImplicitParam:
388 case Decl::ParmVar:
389 case Decl::Var:
390 case Decl::VarTemplate:
391 llvm_unreachable("variables handled before");
392 break;
393 case Decl::TemplateTypeParm:
395 break;
396 case Decl::TemplateTemplateParm:
398 break;
399 case Decl::NonTypeTemplateParm:
401 break;
402 case Decl::Concept:
404 break;
405 // Other decls get the 'unknown' kind.
406 default:
407 break;
408 }
409 }
410
411 if (Info.Kind == SymbolKind::Unknown)
412 return Info;
413
414 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
415 if (FD->getTemplatedKind() ==
418 Info.Properties |=
420 }
421 }
422
425
426 if (auto *attr = D->getExternalSourceSymbolAttr()) {
427 if (attr->getLanguage() == "Swift")
429 }
430
431 return Info;
432}
433
435 SymbolInfo Info;
436 Info.Kind = SymbolKind::Macro;
439 Info.Lang = SymbolLanguage::C;
440 return Info;
441}
442
476
478 llvm::function_ref<void(SymbolRole)> Fn) {
479 applyForEachSymbolRoleInterruptible(Roles, [&](SymbolRole r) -> bool {
480 Fn(r);
481 return true;
482 });
483}
484
485void index::printSymbolRoles(SymbolRoleSet Roles, raw_ostream &OS) {
486 bool VisitedOnce = false;
488 if (VisitedOnce)
489 OS << ',';
490 else
491 VisitedOnce = true;
492 switch (Role) {
493 case SymbolRole::Declaration: OS << "Decl"; break;
494 case SymbolRole::Definition: OS << "Def"; break;
495 case SymbolRole::Reference: OS << "Ref"; break;
496 case SymbolRole::Read: OS << "Read"; break;
497 case SymbolRole::Write: OS << "Writ"; break;
498 case SymbolRole::Call: OS << "Call"; break;
499 case SymbolRole::Dynamic: OS << "Dyn"; break;
500 case SymbolRole::AddressOf: OS << "Addr"; break;
501 case SymbolRole::Implicit: OS << "Impl"; break;
502 case SymbolRole::Undefinition: OS << "Undef"; break;
503 case SymbolRole::RelationChildOf: OS << "RelChild"; break;
504 case SymbolRole::RelationBaseOf: OS << "RelBase"; break;
505 case SymbolRole::RelationOverrideOf: OS << "RelOver"; break;
506 case SymbolRole::RelationReceivedBy: OS << "RelRec"; break;
507 case SymbolRole::RelationCalledBy: OS << "RelCall"; break;
508 case SymbolRole::RelationExtendedBy: OS << "RelExt"; break;
509 case SymbolRole::RelationAccessorOf: OS << "RelAcc"; break;
510 case SymbolRole::RelationContainedBy: OS << "RelCont"; break;
511 case SymbolRole::RelationIBTypeOf: OS << "RelIBType"; break;
512 case SymbolRole::RelationSpecializationOf: OS << "RelSpecialization"; break;
513 case SymbolRole::NameReference: OS << "NameReference"; break;
514 }
515 });
516}
517
518bool index::printSymbolName(const Decl *D, const LangOptions &LO,
519 raw_ostream &OS) {
520 if (auto *ND = dyn_cast<NamedDecl>(D)) {
521 PrintingPolicy Policy(LO);
522 // Forward references can have different template argument names. Suppress
523 // the template argument names in constructors to make their name more
524 // stable.
526 DeclarationName DeclName = ND->getDeclName();
527 if (DeclName.isEmpty())
528 return true;
529 DeclName.print(OS, Policy);
530 return false;
531 } else {
532 return true;
533 }
534}
535
537 switch (K) {
538 // FIXME: for backwards compatibility, the include directive kind is treated
539 // the same as Unknown
541 case SymbolKind::Unknown: return "<unknown>";
542 case SymbolKind::Module: return "module";
543 case SymbolKind::Namespace: return "namespace";
544 case SymbolKind::NamespaceAlias: return "namespace-alias";
545 case SymbolKind::Macro: return "macro";
546 case SymbolKind::Enum: return "enum";
547 case SymbolKind::Struct: return "struct";
548 case SymbolKind::Class: return "class";
549 case SymbolKind::Protocol: return "protocol";
550 case SymbolKind::Extension: return "extension";
551 case SymbolKind::Union: return "union";
552 case SymbolKind::TypeAlias: return "type-alias";
553 case SymbolKind::Function: return "function";
554 case SymbolKind::Variable: return "variable";
555 case SymbolKind::Field: return "field";
556 case SymbolKind::EnumConstant: return "enumerator";
557 case SymbolKind::InstanceMethod: return "instance-method";
558 case SymbolKind::ClassMethod: return "class-method";
559 case SymbolKind::StaticMethod: return "static-method";
560 case SymbolKind::InstanceProperty: return "instance-property";
561 case SymbolKind::ClassProperty: return "class-property";
562 case SymbolKind::StaticProperty: return "static-property";
563 case SymbolKind::Constructor: return "constructor";
564 case SymbolKind::Destructor: return "destructor";
565 case SymbolKind::ConversionFunction: return "conversion-func";
566 case SymbolKind::Parameter: return "param";
567 case SymbolKind::Using: return "using";
568 case SymbolKind::TemplateTypeParm: return "template-type-param";
569 case SymbolKind::TemplateTemplateParm: return "template-template-param";
570 case SymbolKind::NonTypeTemplateParm: return "non-type-template-param";
572 return "concept";
573 }
574 llvm_unreachable("invalid symbol kind");
575}
576
578 switch (K) {
579 case SymbolSubKind::None: return "<none>";
580 case SymbolSubKind::CXXCopyConstructor: return "cxx-copy-ctor";
581 case SymbolSubKind::CXXMoveConstructor: return "cxx-move-ctor";
582 case SymbolSubKind::AccessorGetter: return "acc-get";
583 case SymbolSubKind::AccessorSetter: return "acc-set";
585 return "using-typename";
587 return "using-value";
589 return "using-enum";
591 return "using-class";
593 return "using-struct";
594 }
595 llvm_unreachable("invalid symbol subkind");
596}
597
599 switch (K) {
600 case SymbolLanguage::C: return "C";
601 case SymbolLanguage::ObjC: return "ObjC";
602 case SymbolLanguage::CXX: return "C++";
603 case SymbolLanguage::Swift: return "Swift";
604 }
605 llvm_unreachable("invalid symbol language kind");
606}
607
609 llvm::function_ref<void(SymbolProperty)> Fn) {
610#define APPLY_FOR_PROPERTY(K) \
611 if (Props & (SymbolPropertySet)SymbolProperty::K) \
612 Fn(SymbolProperty::K)
613
623
624#undef APPLY_FOR_PROPERTY
625}
626
628 bool VisitedOnce = false;
630 if (VisitedOnce)
631 OS << ',';
632 else
633 VisitedOnce = true;
634 switch (Prop) {
635 case SymbolProperty::Generic: OS << "Gen"; break;
636 case SymbolProperty::TemplatePartialSpecialization: OS << "TPS"; break;
637 case SymbolProperty::TemplateSpecialization: OS << "TS"; break;
638 case SymbolProperty::UnitTest: OS << "test"; break;
639 case SymbolProperty::IBAnnotated: OS << "IB"; break;
640 case SymbolProperty::IBOutletCollection: OS << "IBColl"; break;
641 case SymbolProperty::GKInspectable: OS << "GKI"; break;
642 case SymbolProperty::Local: OS << "local"; break;
643 case SymbolProperty::ProtocolInterface: OS << "protocol"; break;
644 }
645 });
646}
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)
static SymbolSubKind getSubKindForTypedef(const TypedefNameDecl *TND)
#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.
C Language Family Type Representation.
Represents a static or instance method of a struct/union/class.
Definition DeclCXX.h:2145
bool isStatic() const
Definition DeclCXX.cpp:2417
Represents a C++ struct/union/class.
Definition DeclCXX.h:258
Declaration of a class template.
CXXRecordDecl * getTemplatedDecl() const
Get the underlying class declarations of the template.
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:344
T * getAttr() const
Definition DeclBase.h:581
ExternalSourceSymbolAttr * getExternalSourceSymbolAttr() const
Looks on this and related declarations for an applicable external source symbol attribute.
Definition DeclBase.cpp:616
DeclContext * getDeclContext()
Definition DeclBase.h:456
bool hasAttr() const
Definition DeclBase.h:585
Kind getKind() const
Definition DeclBase.h:450
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:2029
@ TK_FunctionTemplateSpecialization
Definition Decl.h:2045
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:40
This represents a decl that may have a name.
Definition Decl.h:274
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:3761
TagKind getTagKind() const
Definition Decl.h:3961
bool isVoidType() const
Definition TypeBase.h:9092
TagDecl * getAsTagDecl() const
Retrieves the TagDecl that this type refers to, either because the type is a TagType or because it is...
Definition Type.h:63
Base class for declarations which introduce a typedef-name.
Definition Decl.h:3606
QualType getUnderlyingType() const
Definition Decl.h:3661
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:88
@ ProtocolInterface
Symbol is part of a protocol interface.
Definition IndexSymbol.h:98
uint16_t SymbolPropertySet
Definition IndexSymbol.h:86
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:6035
@ Struct
The "struct" keyword.
Definition TypeBase.h:6032
@ Class
The "class" keyword.
Definition TypeBase.h:6041
@ Union
The "union" keyword.
Definition TypeBase.h:6038
@ Enum
The "enum" keyword.
Definition TypeBase.h:6044
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