clang 23.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:
306 Info.Kind = SymbolKind::Class;
309 break;
310 case Decl::FunctionTemplate:
314 if (const CXXMethodDecl *MD = dyn_cast_or_null<CXXMethodDecl>(
315 cast<FunctionTemplateDecl>(D)->getTemplatedDecl())) {
318 else if (isa<CXXDestructorDecl>(MD))
320 else if (isa<CXXConversionDecl>(MD))
322 else {
323 if (MD->isStatic())
325 else
327 }
328 }
329 break;
330 case Decl::TypeAliasTemplate:
334 break;
335 case Decl::TypeAlias: {
339 break;
340 }
341 case Decl::UnresolvedUsingTypename:
342 Info.Kind = SymbolKind::Using;
346 break;
347 case Decl::UnresolvedUsingValue:
348 Info.Kind = SymbolKind::Using;
352 break;
353 case Decl::Using:
354 Info.Kind = SymbolKind::Using;
356 break;
357 case Decl::UsingEnum:
358 Info.Kind = SymbolKind::Using;
361 break;
362 case Decl::Binding:
365 break;
366 case Decl::MSProperty:
368 if (const CXXRecordDecl *CXXRec =
369 dyn_cast<CXXRecordDecl>(D->getDeclContext())) {
370 if (!CXXRec->isCLike())
372 }
373 break;
374 case Decl::ClassTemplatePartialSpecialization:
375 case Decl::ClassTemplateSpecialization:
376 case Decl::CXXRecord:
377 case Decl::Enum:
378 case Decl::Record:
379 llvm_unreachable("records handled before");
380 break;
381 case Decl::VarTemplateSpecialization:
382 case Decl::VarTemplatePartialSpecialization:
383 case Decl::ImplicitParam:
384 case Decl::ParmVar:
385 case Decl::Var:
386 case Decl::VarTemplate:
387 llvm_unreachable("variables handled before");
388 break;
389 case Decl::TemplateTypeParm:
391 break;
392 case Decl::TemplateTemplateParm:
394 break;
395 case Decl::NonTypeTemplateParm:
397 break;
398 case Decl::Concept:
400 break;
401 // Other decls get the 'unknown' kind.
402 default:
403 break;
404 }
405 }
406
407 if (Info.Kind == SymbolKind::Unknown)
408 return Info;
409
410 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
411 if (FD->getTemplatedKind() ==
414 Info.Properties |=
416 }
417 }
418
421
422 if (auto *attr = D->getExternalSourceSymbolAttr()) {
423 if (attr->getLanguage() == "Swift")
425 }
426
427 return Info;
428}
429
431 SymbolInfo Info;
432 Info.Kind = SymbolKind::Macro;
435 Info.Lang = SymbolLanguage::C;
436 return Info;
437}
438
472
474 llvm::function_ref<void(SymbolRole)> Fn) {
475 applyForEachSymbolRoleInterruptible(Roles, [&](SymbolRole r) -> bool {
476 Fn(r);
477 return true;
478 });
479}
480
481void index::printSymbolRoles(SymbolRoleSet Roles, raw_ostream &OS) {
482 bool VisitedOnce = false;
484 if (VisitedOnce)
485 OS << ',';
486 else
487 VisitedOnce = true;
488 switch (Role) {
489 case SymbolRole::Declaration: OS << "Decl"; break;
490 case SymbolRole::Definition: OS << "Def"; break;
491 case SymbolRole::Reference: OS << "Ref"; break;
492 case SymbolRole::Read: OS << "Read"; break;
493 case SymbolRole::Write: OS << "Writ"; break;
494 case SymbolRole::Call: OS << "Call"; break;
495 case SymbolRole::Dynamic: OS << "Dyn"; break;
496 case SymbolRole::AddressOf: OS << "Addr"; break;
497 case SymbolRole::Implicit: OS << "Impl"; break;
498 case SymbolRole::Undefinition: OS << "Undef"; break;
499 case SymbolRole::RelationChildOf: OS << "RelChild"; break;
500 case SymbolRole::RelationBaseOf: OS << "RelBase"; break;
501 case SymbolRole::RelationOverrideOf: OS << "RelOver"; break;
502 case SymbolRole::RelationReceivedBy: OS << "RelRec"; break;
503 case SymbolRole::RelationCalledBy: OS << "RelCall"; break;
504 case SymbolRole::RelationExtendedBy: OS << "RelExt"; break;
505 case SymbolRole::RelationAccessorOf: OS << "RelAcc"; break;
506 case SymbolRole::RelationContainedBy: OS << "RelCont"; break;
507 case SymbolRole::RelationIBTypeOf: OS << "RelIBType"; break;
508 case SymbolRole::RelationSpecializationOf: OS << "RelSpecialization"; break;
509 case SymbolRole::NameReference: OS << "NameReference"; break;
510 }
511 });
512}
513
514bool index::printSymbolName(const Decl *D, const LangOptions &LO,
515 raw_ostream &OS) {
516 if (auto *ND = dyn_cast<NamedDecl>(D)) {
517 PrintingPolicy Policy(LO);
518 // Forward references can have different template argument names. Suppress
519 // the template argument names in constructors to make their name more
520 // stable.
522 DeclarationName DeclName = ND->getDeclName();
523 if (DeclName.isEmpty())
524 return true;
525 DeclName.print(OS, Policy);
526 return false;
527 } else {
528 return true;
529 }
530}
531
533 switch (K) {
534 // FIXME: for backwards compatibility, the include directive kind is treated
535 // the same as Unknown
537 case SymbolKind::Unknown: return "<unknown>";
538 case SymbolKind::Module: return "module";
539 case SymbolKind::Namespace: return "namespace";
540 case SymbolKind::NamespaceAlias: return "namespace-alias";
541 case SymbolKind::Macro: return "macro";
542 case SymbolKind::Enum: return "enum";
543 case SymbolKind::Struct: return "struct";
544 case SymbolKind::Class: return "class";
545 case SymbolKind::Protocol: return "protocol";
546 case SymbolKind::Extension: return "extension";
547 case SymbolKind::Union: return "union";
548 case SymbolKind::TypeAlias: return "type-alias";
549 case SymbolKind::Function: return "function";
550 case SymbolKind::Variable: return "variable";
551 case SymbolKind::Field: return "field";
552 case SymbolKind::EnumConstant: return "enumerator";
553 case SymbolKind::InstanceMethod: return "instance-method";
554 case SymbolKind::ClassMethod: return "class-method";
555 case SymbolKind::StaticMethod: return "static-method";
556 case SymbolKind::InstanceProperty: return "instance-property";
557 case SymbolKind::ClassProperty: return "class-property";
558 case SymbolKind::StaticProperty: return "static-property";
559 case SymbolKind::Constructor: return "constructor";
560 case SymbolKind::Destructor: return "destructor";
561 case SymbolKind::ConversionFunction: return "conversion-func";
562 case SymbolKind::Parameter: return "param";
563 case SymbolKind::Using: return "using";
564 case SymbolKind::TemplateTypeParm: return "template-type-param";
565 case SymbolKind::TemplateTemplateParm: return "template-template-param";
566 case SymbolKind::NonTypeTemplateParm: return "non-type-template-param";
568 return "concept";
569 }
570 llvm_unreachable("invalid symbol kind");
571}
572
574 switch (K) {
575 case SymbolSubKind::None: return "<none>";
576 case SymbolSubKind::CXXCopyConstructor: return "cxx-copy-ctor";
577 case SymbolSubKind::CXXMoveConstructor: return "cxx-move-ctor";
578 case SymbolSubKind::AccessorGetter: return "acc-get";
579 case SymbolSubKind::AccessorSetter: return "acc-set";
581 return "using-typename";
583 return "using-value";
585 return "using-enum";
587 return "using-class";
589 return "using-struct";
590 }
591 llvm_unreachable("invalid symbol subkind");
592}
593
595 switch (K) {
596 case SymbolLanguage::C: return "C";
597 case SymbolLanguage::ObjC: return "ObjC";
598 case SymbolLanguage::CXX: return "C++";
599 case SymbolLanguage::Swift: return "Swift";
600 }
601 llvm_unreachable("invalid symbol language kind");
602}
603
605 llvm::function_ref<void(SymbolProperty)> Fn) {
606#define APPLY_FOR_PROPERTY(K) \
607 if (Props & (SymbolPropertySet)SymbolProperty::K) \
608 Fn(SymbolProperty::K)
609
619
620#undef APPLY_FOR_PROPERTY
621}
622
624 bool VisitedOnce = false;
626 if (VisitedOnce)
627 OS << ',';
628 else
629 VisitedOnce = true;
630 switch (Prop) {
631 case SymbolProperty::Generic: OS << "Gen"; break;
632 case SymbolProperty::TemplatePartialSpecialization: OS << "TPS"; break;
633 case SymbolProperty::TemplateSpecialization: OS << "TS"; break;
634 case SymbolProperty::UnitTest: OS << "test"; break;
635 case SymbolProperty::IBAnnotated: OS << "IB"; break;
636 case SymbolProperty::IBOutletCollection: OS << "IBColl"; break;
637 case SymbolProperty::GKInspectable: OS << "GKI"; break;
638 case SymbolProperty::Local: OS << "local"; break;
639 case SymbolProperty::ProtocolInterface: OS << "protocol"; break;
640 }
641 });
642}
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:2136
bool isStatic() const
Definition DeclCXX.cpp:2419
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:341
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:612
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:2000
@ TK_FunctionTemplateSpecialization
Definition Decl.h:2016
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: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:3717
bool isVoidType() const
Definition TypeBase.h:8991
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:3562
QualType getUnderlyingType() const
Definition Decl.h:3617
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:5944
@ Struct
The "struct" keyword.
Definition TypeBase.h:5941
@ Class
The "class" keyword.
Definition TypeBase.h:5950
@ Union
The "union" keyword.
Definition TypeBase.h:5947
@ Enum
The "enum" keyword.
Definition TypeBase.h:5953
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