clang  7.0.0
IndexSymbol.cpp
Go to the documentation of this file.
1 //===--- IndexSymbol.cpp - Types and functions for indexing symbols -------===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 
11 #include "clang/AST/DeclCXX.h"
12 #include "clang/AST/DeclObjC.h"
13 #include "clang/AST/DeclTemplate.h"
15 #include "clang/Lex/MacroInfo.h"
16 
17 using namespace clang;
18 using namespace clang::index;
19 
20 /// \returns true if \c D is a subclass of 'XCTestCase'.
21 static 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'.
34 static 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).startswith("test"))
40  return false;
41  return isUnitTestCase(D->getClassInterface());
42 }
43 
44 static 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 
59  if (isa<TemplateTemplateParmDecl>(D))
60  return true;
61 
62  if (isa<ObjCTypeParamDecl>(D))
63  return true;
64 
65  if (isa<UsingDirectiveDecl>(D))
66  return false;
67  if (!D->getParentFunctionOrMethod())
68  return false;
69 
70  if (const NamedDecl *ND = dyn_cast<NamedDecl>(D)) {
71  switch (ND->getFormalLinkage()) {
72  case NoLinkage:
73  case InternalLinkage:
74  return true;
75  case VisibleNoLinkage:
78  llvm_unreachable("Not a sema linkage");
79  case ModuleLinkage:
80  case ExternalLinkage:
81  return false;
82  }
83  }
84 
85  return true;
86 }
87 
89  assert(D);
90  SymbolInfo Info;
94  Info.Lang = SymbolLanguage::C;
95 
96  if (isFunctionLocalSymbol(D)) {
98  }
99 
100  if (const TagDecl *TD = dyn_cast<TagDecl>(D)) {
101  switch (TD->getTagKind()) {
102  case TTK_Struct:
103  Info.Kind = SymbolKind::Struct; break;
104  case TTK_Union:
105  Info.Kind = SymbolKind::Union; break;
106  case TTK_Class:
107  Info.Kind = SymbolKind::Class;
108  Info.Lang = SymbolLanguage::CXX;
109  break;
110  case TTK_Interface:
111  Info.Kind = SymbolKind::Protocol;
112  Info.Lang = SymbolLanguage::CXX;
113  break;
114  case TTK_Enum:
115  Info.Kind = SymbolKind::Enum; break;
116  }
117 
118  if (const CXXRecordDecl *CXXRec = dyn_cast<CXXRecordDecl>(D)) {
119  if (!CXXRec->isCLike()) {
120  Info.Lang = SymbolLanguage::CXX;
121  if (CXXRec->getDescribedClassTemplate()) {
123  }
124  }
125  }
126 
127  if (isa<ClassTemplatePartialSpecializationDecl>(D)) {
129  Info.Properties |=
131  } else if (isa<ClassTemplateSpecializationDecl>(D)) {
133  Info.Properties |=
135  }
136 
137  } else if (auto *VD = dyn_cast<VarDecl>(D)) {
138  Info.Kind = SymbolKind::Variable;
139  if (isa<ParmVarDecl>(D)) {
141  } else if (isa<CXXRecordDecl>(D->getDeclContext())) {
143  Info.Lang = SymbolLanguage::CXX;
144  }
145 
146  if (isa<VarTemplatePartialSpecializationDecl>(D)) {
147  Info.Lang = SymbolLanguage::CXX;
149  Info.Properties |=
151  } else if (isa<VarTemplateSpecializationDecl>(D)) {
152  Info.Lang = SymbolLanguage::CXX;
154  Info.Properties |=
156  } else if (VD->getDescribedVarTemplate()) {
157  Info.Lang = SymbolLanguage::CXX;
159  }
160 
161  } else {
162  switch (D->getKind()) {
163  case Decl::Import:
164  Info.Kind = SymbolKind::Module;
165  break;
166  case Decl::Typedef:
167  Info.Kind = SymbolKind::TypeAlias; break; // Lang = C
168  case Decl::Function:
169  Info.Kind = SymbolKind::Function;
170  break;
171  case Decl::Field:
172  Info.Kind = SymbolKind::Field;
173  if (const CXXRecordDecl *
174  CXXRec = dyn_cast<CXXRecordDecl>(D->getDeclContext())) {
175  if (!CXXRec->isCLike())
176  Info.Lang = SymbolLanguage::CXX;
177  }
178  break;
179  case Decl::EnumConstant:
180  Info.Kind = SymbolKind::EnumConstant; break;
181  case Decl::ObjCInterface:
182  case Decl::ObjCImplementation: {
183  Info.Kind = SymbolKind::Class;
184  Info.Lang = SymbolLanguage::ObjC;
185  const ObjCInterfaceDecl *ClsD = dyn_cast<ObjCInterfaceDecl>(D);
186  if (!ClsD)
187  ClsD = cast<ObjCImplementationDecl>(D)->getClassInterface();
188  if (isUnitTestCase(ClsD))
190  break;
191  }
192  case Decl::ObjCProtocol:
193  Info.Kind = SymbolKind::Protocol;
194  Info.Lang = SymbolLanguage::ObjC;
195  break;
196  case Decl::ObjCCategory:
197  case Decl::ObjCCategoryImpl: {
199  Info.Lang = SymbolLanguage::ObjC;
200  const ObjCInterfaceDecl *ClsD = nullptr;
201  if (auto *CatD = dyn_cast<ObjCCategoryDecl>(D))
202  ClsD = CatD->getClassInterface();
203  else
204  ClsD = cast<ObjCCategoryImplDecl>(D)->getClassInterface();
205  if (isUnitTestCase(ClsD))
207  break;
208  }
209  case Decl::ObjCMethod: {
210  const ObjCMethodDecl *MD = cast<ObjCMethodDecl>(D);
212  if (MD->isPropertyAccessor()) {
213  if (MD->param_size())
215  else
217  }
218  Info.Lang = SymbolLanguage::ObjC;
219  if (isUnitTest(MD))
221  if (D->hasAttr<IBActionAttr>())
223  break;
224  }
225  case Decl::ObjCProperty:
227  Info.Lang = SymbolLanguage::ObjC;
228  checkForIBOutlets(D, Info.Properties);
229  if (auto *Annot = D->getAttr<AnnotateAttr>()) {
230  if (Annot->getAnnotation() == "gk_inspectable")
232  }
233  break;
234  case Decl::ObjCIvar:
235  Info.Kind = SymbolKind::Field;
236  Info.Lang = SymbolLanguage::ObjC;
237  checkForIBOutlets(D, Info.Properties);
238  break;
239  case Decl::Namespace:
241  Info.Lang = SymbolLanguage::CXX;
242  break;
245  Info.Lang = SymbolLanguage::CXX;
246  break;
247  case Decl::CXXConstructor: {
249  Info.Lang = SymbolLanguage::CXX;
250  auto *CD = cast<CXXConstructorDecl>(D);
251  if (CD->isCopyConstructor())
253  else if (CD->isMoveConstructor())
255  break;
256  }
257  case Decl::CXXDestructor:
259  Info.Lang = SymbolLanguage::CXX;
260  break;
261  case Decl::CXXConversion:
263  Info.Lang = SymbolLanguage::CXX;
264  break;
265  case Decl::CXXMethod: {
266  const CXXMethodDecl *MD = cast<CXXMethodDecl>(D);
267  if (MD->isStatic())
269  else
271  Info.Lang = SymbolLanguage::CXX;
272  break;
273  }
274  case Decl::ClassTemplate:
275  Info.Kind = SymbolKind::Class;
277  Info.Lang = SymbolLanguage::CXX;
278  break;
279  case Decl::FunctionTemplate:
280  Info.Kind = SymbolKind::Function;
282  Info.Lang = SymbolLanguage::CXX;
283  if (const CXXMethodDecl *MD = dyn_cast_or_null<CXXMethodDecl>(
284  cast<FunctionTemplateDecl>(D)->getTemplatedDecl())) {
285  if (isa<CXXConstructorDecl>(MD))
287  else if (isa<CXXDestructorDecl>(MD))
289  else if (isa<CXXConversionDecl>(MD))
291  else {
292  if (MD->isStatic())
294  else
296  }
297  }
298  break;
299  case Decl::TypeAliasTemplate:
301  Info.Lang = SymbolLanguage::CXX;
303  break;
304  case Decl::TypeAlias:
306  Info.Lang = SymbolLanguage::CXX;
307  break;
308  case Decl::UnresolvedUsingTypename:
309  Info.Kind = SymbolKind::Using;
311  Info.Lang = SymbolLanguage::CXX;
313  break;
314  case Decl::UnresolvedUsingValue:
315  Info.Kind = SymbolKind::Using;
317  Info.Lang = SymbolLanguage::CXX;
319  break;
320  case Decl::Binding:
321  Info.Kind = SymbolKind::Variable;
322  Info.Lang = SymbolLanguage::CXX;
323  break;
324  default:
325  break;
326  }
327  }
328 
329  if (Info.Kind == SymbolKind::Unknown)
330  return Info;
331 
332  if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
333  if (FD->getTemplatedKind() ==
335  Info.Properties |= (SymbolPropertySet)SymbolProperty::Generic;
336  Info.Properties |=
338  }
339  }
340 
341  if (Info.Properties & (SymbolPropertySet)SymbolProperty::Generic)
342  Info.Lang = SymbolLanguage::CXX;
343 
344  if (auto *attr = D->getExternalSourceSymbolAttr()) {
345  if (attr->getLanguage() == "Swift")
346  Info.Lang = SymbolLanguage::Swift;
347  }
348 
349  return Info;
350 }
351 
353  SymbolInfo Info;
354  Info.Kind = SymbolKind::Macro;
356  Info.Properties = SymbolPropertySet();
357  Info.Lang = SymbolLanguage::C;
358  return Info;
359 }
360 
362  llvm::function_ref<bool(SymbolRole)> Fn) {
363 #define APPLY_FOR_ROLE(Role) \
364  if (Roles & (unsigned)SymbolRole::Role) \
365  if (!Fn(SymbolRole::Role)) \
366  return false;
367 
388 
389 #undef APPLY_FOR_ROLE
390 
391  return true;
392 }
393 
395  llvm::function_ref<void(SymbolRole)> Fn) {
396  applyForEachSymbolRoleInterruptible(Roles, [&](SymbolRole r) -> bool {
397  Fn(r);
398  return true;
399  });
400 }
401 
402 void index::printSymbolRoles(SymbolRoleSet Roles, raw_ostream &OS) {
403  bool VisitedOnce = false;
404  applyForEachSymbolRole(Roles, [&](SymbolRole Role) {
405  if (VisitedOnce)
406  OS << ',';
407  else
408  VisitedOnce = true;
409  switch (Role) {
410  case SymbolRole::Declaration: OS << "Decl"; break;
411  case SymbolRole::Definition: OS << "Def"; break;
412  case SymbolRole::Reference: OS << "Ref"; break;
413  case SymbolRole::Read: OS << "Read"; break;
414  case SymbolRole::Write: OS << "Writ"; break;
415  case SymbolRole::Call: OS << "Call"; break;
416  case SymbolRole::Dynamic: OS << "Dyn"; break;
417  case SymbolRole::AddressOf: OS << "Addr"; break;
418  case SymbolRole::Implicit: OS << "Impl"; break;
419  case SymbolRole::Undefinition: OS << "Undef"; break;
420  case SymbolRole::RelationChildOf: OS << "RelChild"; break;
421  case SymbolRole::RelationBaseOf: OS << "RelBase"; break;
422  case SymbolRole::RelationOverrideOf: OS << "RelOver"; break;
423  case SymbolRole::RelationReceivedBy: OS << "RelRec"; break;
424  case SymbolRole::RelationCalledBy: OS << "RelCall"; break;
425  case SymbolRole::RelationExtendedBy: OS << "RelExt"; break;
426  case SymbolRole::RelationAccessorOf: OS << "RelAcc"; break;
427  case SymbolRole::RelationContainedBy: OS << "RelCont"; break;
428  case SymbolRole::RelationIBTypeOf: OS << "RelIBType"; break;
429  case SymbolRole::RelationSpecializationOf: OS << "RelSpecialization"; break;
430  }
431  });
432 }
433 
434 bool index::printSymbolName(const Decl *D, const LangOptions &LO,
435  raw_ostream &OS) {
436  if (auto *ND = dyn_cast<NamedDecl>(D)) {
437  PrintingPolicy Policy(LO);
438  // Forward references can have different template argument names. Suppress
439  // the template argument names in constructors to make their name more
440  // stable.
442  DeclarationName DeclName = ND->getDeclName();
443  if (DeclName.isEmpty())
444  return true;
445  DeclName.print(OS, Policy);
446  return false;
447  } else {
448  return true;
449  }
450 }
451 
453  switch (K) {
454  case SymbolKind::Unknown: return "<unknown>";
455  case SymbolKind::Module: return "module";
456  case SymbolKind::Namespace: return "namespace";
457  case SymbolKind::NamespaceAlias: return "namespace-alias";
458  case SymbolKind::Macro: return "macro";
459  case SymbolKind::Enum: return "enum";
460  case SymbolKind::Struct: return "struct";
461  case SymbolKind::Class: return "class";
462  case SymbolKind::Protocol: return "protocol";
463  case SymbolKind::Extension: return "extension";
464  case SymbolKind::Union: return "union";
465  case SymbolKind::TypeAlias: return "type-alias";
466  case SymbolKind::Function: return "function";
467  case SymbolKind::Variable: return "variable";
468  case SymbolKind::Field: return "field";
469  case SymbolKind::EnumConstant: return "enumerator";
470  case SymbolKind::InstanceMethod: return "instance-method";
471  case SymbolKind::ClassMethod: return "class-method";
472  case SymbolKind::StaticMethod: return "static-method";
473  case SymbolKind::InstanceProperty: return "instance-property";
474  case SymbolKind::ClassProperty: return "class-property";
475  case SymbolKind::StaticProperty: return "static-property";
476  case SymbolKind::Constructor: return "constructor";
477  case SymbolKind::Destructor: return "destructor";
478  case SymbolKind::ConversionFunction: return "coversion-func";
479  case SymbolKind::Parameter: return "param";
480  case SymbolKind::Using: return "using";
481  }
482  llvm_unreachable("invalid symbol kind");
483 }
484 
486  switch (K) {
487  case SymbolSubKind::None: return "<none>";
488  case SymbolSubKind::CXXCopyConstructor: return "cxx-copy-ctor";
489  case SymbolSubKind::CXXMoveConstructor: return "cxx-move-ctor";
490  case SymbolSubKind::AccessorGetter: return "acc-get";
491  case SymbolSubKind::AccessorSetter: return "acc-set";
492  case SymbolSubKind::UsingTypename: return "using-typename";
493  case SymbolSubKind::UsingValue: return "using-value";
494  }
495  llvm_unreachable("invalid symbol subkind");
496 }
497 
499  switch (K) {
500  case SymbolLanguage::C: return "C";
501  case SymbolLanguage::ObjC: return "ObjC";
502  case SymbolLanguage::CXX: return "C++";
503  case SymbolLanguage::Swift: return "Swift";
504  }
505  llvm_unreachable("invalid symbol language kind");
506 }
507 
509  llvm::function_ref<void(SymbolProperty)> Fn) {
510 #define APPLY_FOR_PROPERTY(K) \
511  if (Props & (SymbolPropertySet)SymbolProperty::K) \
512  Fn(SymbolProperty::K)
513 
522 
523 #undef APPLY_FOR_PROPERTY
524 }
525 
526 void index::printSymbolProperties(SymbolPropertySet Props, raw_ostream &OS) {
527  bool VisitedOnce = false;
528  applyForEachSymbolProperty(Props, [&](SymbolProperty Prop) {
529  if (VisitedOnce)
530  OS << ',';
531  else
532  VisitedOnce = true;
533  switch (Prop) {
534  case SymbolProperty::Generic: OS << "Gen"; break;
535  case SymbolProperty::TemplatePartialSpecialization: OS << "TPS"; break;
536  case SymbolProperty::TemplateSpecialization: OS << "TS"; break;
537  case SymbolProperty::UnitTest: OS << "test"; break;
538  case SymbolProperty::IBAnnotated: OS << "IB"; break;
539  case SymbolProperty::IBOutletCollection: OS << "IBColl"; break;
540  case SymbolProperty::GKInspectable: OS << "GKI"; break;
541  case SymbolProperty::Local: OS << "local"; break;
542  }
543  });
544 }
Represents a function declaration or definition.
Definition: Decl.h:1716
External linkage, which indicates that the entity can be referred to from other translation units...
Definition: Linkage.h:60
if(T->getSizeExpr()) TRY_TO(TraverseStmt(T -> getSizeExpr()))
StringRef getSymbolLanguageString(SymbolLanguage K)
unsigned param_size() const
Definition: DeclObjC.h:381
ObjCInterfaceDecl * getClassInterface()
Definition: DeclObjC.cpp:1098
Internal linkage according to the Modules TS, but can be referred to from other translation units ind...
Definition: Linkage.h:50
bool printSymbolName(const Decl *D, const LangOptions &LO, raw_ostream &OS)
No linkage, which means that the entity is unique and can only be referred to from within its scope...
Definition: Linkage.h:27
Decl - This represents one declaration (or definition), e.g.
Definition: DeclBase.h:86
Defines the C++ template declaration subclasses.
Defines the clang::MacroInfo and clang::MacroDirective classes.
bool isEmpty() const
Evaluates true when this declaration name is empty.
const DeclContext * getParentFunctionOrMethod() const
If this decl is defined inside a function/method/block it returns the corresponding DeclContext...
Definition: DeclBase.cpp:254
The "union" keyword.
Definition: Type.h:4855
The "__interface" keyword.
Definition: Type.h:4852
ObjCMethodDecl - Represents an instance or class method declaration.
Definition: DeclObjC.h:139
Describes how types, statements, expressions, and declarations should be printed. ...
Definition: PrettyPrinter.h:38
bool isStatic() const
Definition: DeclCXX.cpp:1838
static bool isUnitTestCase(const ObjCInterfaceDecl *D)
Definition: IndexSymbol.cpp:21
bool applyForEachSymbolRoleInterruptible(SymbolRoleSet Roles, llvm::function_ref< bool(SymbolRole)> Fn)
SymbolSubKind
Language specific sub-kinds.
Definition: IndexSymbol.h:68
#define APPLY_FOR_PROPERTY(K)
void applyForEachSymbolProperty(SymbolPropertySet Props, llvm::function_ref< void(SymbolProperty)> Fn)
SymbolRole
Set of roles that are attributed to symbol occurrences.
Definition: IndexSymbol.h:95
bool isFunctionLocalSymbol(const Decl *D)
Definition: IndexSymbol.cpp:53
StringRef getSymbolKindString(SymbolKind K)
Keeps track of the various options that can be enabled, which controls the dialect of C or C++ that i...
Definition: LangOptions.h:50
StringRef getSymbolSubKindString(SymbolSubKind K)
Represents an ObjC class declaration.
Definition: DeclObjC.h:1193
static bool isUnitTest(const ObjCMethodDecl *D)
Definition: IndexSymbol.cpp:34
QualType getReturnType() const
Definition: DeclObjC.h:363
unsigned SuppressTemplateArgsInCXXConstructors
When true, suppresses printing template arguments in names of C++ constructors.
unsigned SymbolRoleSet
Definition: IndexSymbol.h:122
bool hasAttr() const
Definition: DeclBase.h:538
Module linkage, which indicates that the entity can be referred to from other translation units withi...
Definition: Linkage.h:56
void printSymbolProperties(SymbolPropertySet Props, raw_ostream &OS)
uint8_t SymbolPropertySet
Definition: IndexSymbol.h:78
#define APPLY_FOR_ROLE(Role)
StringRef getNameForSlot(unsigned argIndex) const
Retrieve the name at a given position in the selector.
DeclContext * getDeclContext()
Definition: DeclBase.h:428
ObjCInterfaceDecl * getSuperClass() const
Definition: DeclObjC.cpp:331
bool isInstanceMethod() const
Definition: DeclObjC.h:454
Selector getSelector() const
Definition: DeclObjC.h:361
static void checkForIBOutlets(const Decl *D, SymbolPropertySet &PropSet)
Definition: IndexSymbol.cpp:44
The "struct" keyword.
Definition: Type.h:4849
Represents the declaration of a struct/union/class/enum.
Definition: Decl.h:3020
Represents a static or instance method of a struct/union/class.
Definition: DeclCXX.h:2045
void applyForEachSymbolRole(SymbolRoleSet Roles, llvm::function_ref< void(SymbolRole)> Fn)
Dataflow Directional Tag Classes.
External linkage within a unique namespace.
Definition: Linkage.h:41
DeclarationName - The name of a declaration.
Kind getKind() const
Definition: DeclBase.h:422
Encapsulates the data about a macro definition (e.g.
Definition: MacroInfo.h:40
SymbolProperty
Set of properties that provide additional info about a symbol.
Definition: IndexSymbol.h:80
void printSymbolRoles(SymbolRoleSet Roles, raw_ostream &OS)
T * getAttr() const
Definition: DeclBase.h:534
Internal linkage, which indicates that the entity can be referred to from within the translation unit...
Definition: Linkage.h:32
The "class" keyword.
Definition: Type.h:4858
Defines the C++ Decl subclasses, other than those for templates (found in DeclTemplate.h) and friends (in DeclFriend.h).
SymbolInfo getSymbolInfo(const Decl *D)
Definition: IndexSymbol.cpp:88
Represents a C++ struct/union/class.
Definition: DeclCXX.h:302
bool isVoidType() const
Definition: Type.h:6340
No linkage according to the standard, but is visible from other translation units because of types de...
Definition: Linkage.h:45
The "enum" keyword.
Definition: Type.h:4861
void print(raw_ostream &OS, const PrintingPolicy &Policy)
SymbolInfo getSymbolInfoForMacro(const MacroInfo &MI)
This represents a decl that may have a name.
Definition: Decl.h:248
bool isPropertyAccessor() const
Definition: DeclObjC.h:461
SymbolPropertySet Properties
Definition: IndexSymbol.h:137
ArrayRef< ParmVarDecl * > parameters() const
Definition: DeclObjC.h:407