clang  8.0.0svn
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  if (isa<ObjCProtocolDecl>(D->getDeclContext())) {
101  }
102 
103  if (const TagDecl *TD = dyn_cast<TagDecl>(D)) {
104  switch (TD->getTagKind()) {
105  case TTK_Struct:
106  Info.Kind = SymbolKind::Struct; break;
107  case TTK_Union:
108  Info.Kind = SymbolKind::Union; break;
109  case TTK_Class:
110  Info.Kind = SymbolKind::Class;
111  Info.Lang = SymbolLanguage::CXX;
112  break;
113  case TTK_Interface:
114  Info.Kind = SymbolKind::Protocol;
115  Info.Lang = SymbolLanguage::CXX;
116  break;
117  case TTK_Enum:
118  Info.Kind = SymbolKind::Enum; break;
119  }
120 
121  if (const CXXRecordDecl *CXXRec = dyn_cast<CXXRecordDecl>(D)) {
122  if (!CXXRec->isCLike()) {
123  Info.Lang = SymbolLanguage::CXX;
124  if (CXXRec->getDescribedClassTemplate()) {
126  }
127  }
128  }
129 
130  if (isa<ClassTemplatePartialSpecializationDecl>(D)) {
132  Info.Properties |=
134  } else if (isa<ClassTemplateSpecializationDecl>(D)) {
136  Info.Properties |=
138  }
139 
140  } else if (auto *VD = dyn_cast<VarDecl>(D)) {
141  Info.Kind = SymbolKind::Variable;
142  if (isa<ParmVarDecl>(D)) {
144  } else if (isa<CXXRecordDecl>(D->getDeclContext())) {
146  Info.Lang = SymbolLanguage::CXX;
147  }
148 
149  if (isa<VarTemplatePartialSpecializationDecl>(D)) {
150  Info.Lang = SymbolLanguage::CXX;
152  Info.Properties |=
154  } else if (isa<VarTemplateSpecializationDecl>(D)) {
155  Info.Lang = SymbolLanguage::CXX;
157  Info.Properties |=
159  } else if (VD->getDescribedVarTemplate()) {
160  Info.Lang = SymbolLanguage::CXX;
162  }
163 
164  } else {
165  switch (D->getKind()) {
166  case Decl::Import:
167  Info.Kind = SymbolKind::Module;
168  break;
169  case Decl::Typedef:
170  Info.Kind = SymbolKind::TypeAlias; break; // Lang = C
171  case Decl::Function:
172  Info.Kind = SymbolKind::Function;
173  break;
174  case Decl::Field:
175  Info.Kind = SymbolKind::Field;
176  if (const CXXRecordDecl *
177  CXXRec = dyn_cast<CXXRecordDecl>(D->getDeclContext())) {
178  if (!CXXRec->isCLike())
179  Info.Lang = SymbolLanguage::CXX;
180  }
181  break;
182  case Decl::EnumConstant:
183  Info.Kind = SymbolKind::EnumConstant; break;
184  case Decl::ObjCInterface:
185  case Decl::ObjCImplementation: {
186  Info.Kind = SymbolKind::Class;
187  Info.Lang = SymbolLanguage::ObjC;
188  const ObjCInterfaceDecl *ClsD = dyn_cast<ObjCInterfaceDecl>(D);
189  if (!ClsD)
190  ClsD = cast<ObjCImplementationDecl>(D)->getClassInterface();
191  if (isUnitTestCase(ClsD))
193  break;
194  }
195  case Decl::ObjCProtocol:
196  Info.Kind = SymbolKind::Protocol;
197  Info.Lang = SymbolLanguage::ObjC;
198  break;
199  case Decl::ObjCCategory:
200  case Decl::ObjCCategoryImpl: {
202  Info.Lang = SymbolLanguage::ObjC;
203  const ObjCInterfaceDecl *ClsD = nullptr;
204  if (auto *CatD = dyn_cast<ObjCCategoryDecl>(D))
205  ClsD = CatD->getClassInterface();
206  else
207  ClsD = cast<ObjCCategoryImplDecl>(D)->getClassInterface();
208  if (isUnitTestCase(ClsD))
210  break;
211  }
212  case Decl::ObjCMethod: {
213  const ObjCMethodDecl *MD = cast<ObjCMethodDecl>(D);
215  if (MD->isPropertyAccessor()) {
216  if (MD->param_size())
218  else
220  }
221  Info.Lang = SymbolLanguage::ObjC;
222  if (isUnitTest(MD))
224  if (D->hasAttr<IBActionAttr>())
226  break;
227  }
228  case Decl::ObjCProperty:
230  Info.Lang = SymbolLanguage::ObjC;
231  checkForIBOutlets(D, Info.Properties);
232  if (auto *Annot = D->getAttr<AnnotateAttr>()) {
233  if (Annot->getAnnotation() == "gk_inspectable")
235  }
236  break;
237  case Decl::ObjCIvar:
238  Info.Kind = SymbolKind::Field;
239  Info.Lang = SymbolLanguage::ObjC;
240  checkForIBOutlets(D, Info.Properties);
241  break;
242  case Decl::Namespace:
244  Info.Lang = SymbolLanguage::CXX;
245  break;
248  Info.Lang = SymbolLanguage::CXX;
249  break;
250  case Decl::CXXConstructor: {
252  Info.Lang = SymbolLanguage::CXX;
253  auto *CD = cast<CXXConstructorDecl>(D);
254  if (CD->isCopyConstructor())
256  else if (CD->isMoveConstructor())
258  break;
259  }
260  case Decl::CXXDestructor:
262  Info.Lang = SymbolLanguage::CXX;
263  break;
264  case Decl::CXXConversion:
266  Info.Lang = SymbolLanguage::CXX;
267  break;
268  case Decl::CXXMethod: {
269  const CXXMethodDecl *MD = cast<CXXMethodDecl>(D);
270  if (MD->isStatic())
272  else
274  Info.Lang = SymbolLanguage::CXX;
275  break;
276  }
277  case Decl::ClassTemplate:
278  Info.Kind = SymbolKind::Class;
280  Info.Lang = SymbolLanguage::CXX;
281  break;
282  case Decl::FunctionTemplate:
283  Info.Kind = SymbolKind::Function;
285  Info.Lang = SymbolLanguage::CXX;
286  if (const CXXMethodDecl *MD = dyn_cast_or_null<CXXMethodDecl>(
287  cast<FunctionTemplateDecl>(D)->getTemplatedDecl())) {
288  if (isa<CXXConstructorDecl>(MD))
290  else if (isa<CXXDestructorDecl>(MD))
292  else if (isa<CXXConversionDecl>(MD))
294  else {
295  if (MD->isStatic())
297  else
299  }
300  }
301  break;
302  case Decl::TypeAliasTemplate:
304  Info.Lang = SymbolLanguage::CXX;
306  break;
307  case Decl::TypeAlias:
309  Info.Lang = SymbolLanguage::CXX;
310  break;
311  case Decl::UnresolvedUsingTypename:
312  Info.Kind = SymbolKind::Using;
314  Info.Lang = SymbolLanguage::CXX;
316  break;
317  case Decl::UnresolvedUsingValue:
318  Info.Kind = SymbolKind::Using;
320  Info.Lang = SymbolLanguage::CXX;
322  break;
323  case Decl::Binding:
324  Info.Kind = SymbolKind::Variable;
325  Info.Lang = SymbolLanguage::CXX;
326  break;
327  default:
328  break;
329  }
330  }
331 
332  if (Info.Kind == SymbolKind::Unknown)
333  return Info;
334 
335  if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
336  if (FD->getTemplatedKind() ==
338  Info.Properties |= (SymbolPropertySet)SymbolProperty::Generic;
339  Info.Properties |=
341  }
342  }
343 
344  if (Info.Properties & (SymbolPropertySet)SymbolProperty::Generic)
345  Info.Lang = SymbolLanguage::CXX;
346 
347  if (auto *attr = D->getExternalSourceSymbolAttr()) {
348  if (attr->getLanguage() == "Swift")
349  Info.Lang = SymbolLanguage::Swift;
350  }
351 
352  return Info;
353 }
354 
356  SymbolInfo Info;
357  Info.Kind = SymbolKind::Macro;
359  Info.Properties = SymbolPropertySet();
360  Info.Lang = SymbolLanguage::C;
361  return Info;
362 }
363 
365  llvm::function_ref<bool(SymbolRole)> Fn) {
366 #define APPLY_FOR_ROLE(Role) \
367  if (Roles & (unsigned)SymbolRole::Role) \
368  if (!Fn(SymbolRole::Role)) \
369  return false;
370 
391 
392 #undef APPLY_FOR_ROLE
393 
394  return true;
395 }
396 
398  llvm::function_ref<void(SymbolRole)> Fn) {
399  applyForEachSymbolRoleInterruptible(Roles, [&](SymbolRole r) -> bool {
400  Fn(r);
401  return true;
402  });
403 }
404 
405 void index::printSymbolRoles(SymbolRoleSet Roles, raw_ostream &OS) {
406  bool VisitedOnce = false;
407  applyForEachSymbolRole(Roles, [&](SymbolRole Role) {
408  if (VisitedOnce)
409  OS << ',';
410  else
411  VisitedOnce = true;
412  switch (Role) {
413  case SymbolRole::Declaration: OS << "Decl"; break;
414  case SymbolRole::Definition: OS << "Def"; break;
415  case SymbolRole::Reference: OS << "Ref"; break;
416  case SymbolRole::Read: OS << "Read"; break;
417  case SymbolRole::Write: OS << "Writ"; break;
418  case SymbolRole::Call: OS << "Call"; break;
419  case SymbolRole::Dynamic: OS << "Dyn"; break;
420  case SymbolRole::AddressOf: OS << "Addr"; break;
421  case SymbolRole::Implicit: OS << "Impl"; break;
422  case SymbolRole::Undefinition: OS << "Undef"; break;
423  case SymbolRole::RelationChildOf: OS << "RelChild"; break;
424  case SymbolRole::RelationBaseOf: OS << "RelBase"; break;
425  case SymbolRole::RelationOverrideOf: OS << "RelOver"; break;
426  case SymbolRole::RelationReceivedBy: OS << "RelRec"; break;
427  case SymbolRole::RelationCalledBy: OS << "RelCall"; break;
428  case SymbolRole::RelationExtendedBy: OS << "RelExt"; break;
429  case SymbolRole::RelationAccessorOf: OS << "RelAcc"; break;
430  case SymbolRole::RelationContainedBy: OS << "RelCont"; break;
431  case SymbolRole::RelationIBTypeOf: OS << "RelIBType"; break;
432  case SymbolRole::RelationSpecializationOf: OS << "RelSpecialization"; break;
433  }
434  });
435 }
436 
437 bool index::printSymbolName(const Decl *D, const LangOptions &LO,
438  raw_ostream &OS) {
439  if (auto *ND = dyn_cast<NamedDecl>(D)) {
440  PrintingPolicy Policy(LO);
441  // Forward references can have different template argument names. Suppress
442  // the template argument names in constructors to make their name more
443  // stable.
445  DeclarationName DeclName = ND->getDeclName();
446  if (DeclName.isEmpty())
447  return true;
448  DeclName.print(OS, Policy);
449  return false;
450  } else {
451  return true;
452  }
453 }
454 
456  switch (K) {
457  case SymbolKind::Unknown: return "<unknown>";
458  case SymbolKind::Module: return "module";
459  case SymbolKind::Namespace: return "namespace";
460  case SymbolKind::NamespaceAlias: return "namespace-alias";
461  case SymbolKind::Macro: return "macro";
462  case SymbolKind::Enum: return "enum";
463  case SymbolKind::Struct: return "struct";
464  case SymbolKind::Class: return "class";
465  case SymbolKind::Protocol: return "protocol";
466  case SymbolKind::Extension: return "extension";
467  case SymbolKind::Union: return "union";
468  case SymbolKind::TypeAlias: return "type-alias";
469  case SymbolKind::Function: return "function";
470  case SymbolKind::Variable: return "variable";
471  case SymbolKind::Field: return "field";
472  case SymbolKind::EnumConstant: return "enumerator";
473  case SymbolKind::InstanceMethod: return "instance-method";
474  case SymbolKind::ClassMethod: return "class-method";
475  case SymbolKind::StaticMethod: return "static-method";
476  case SymbolKind::InstanceProperty: return "instance-property";
477  case SymbolKind::ClassProperty: return "class-property";
478  case SymbolKind::StaticProperty: return "static-property";
479  case SymbolKind::Constructor: return "constructor";
480  case SymbolKind::Destructor: return "destructor";
481  case SymbolKind::ConversionFunction: return "coversion-func";
482  case SymbolKind::Parameter: return "param";
483  case SymbolKind::Using: return "using";
484  }
485  llvm_unreachable("invalid symbol kind");
486 }
487 
489  switch (K) {
490  case SymbolSubKind::None: return "<none>";
491  case SymbolSubKind::CXXCopyConstructor: return "cxx-copy-ctor";
492  case SymbolSubKind::CXXMoveConstructor: return "cxx-move-ctor";
493  case SymbolSubKind::AccessorGetter: return "acc-get";
494  case SymbolSubKind::AccessorSetter: return "acc-set";
495  case SymbolSubKind::UsingTypename: return "using-typename";
496  case SymbolSubKind::UsingValue: return "using-value";
497  }
498  llvm_unreachable("invalid symbol subkind");
499 }
500 
502  switch (K) {
503  case SymbolLanguage::C: return "C";
504  case SymbolLanguage::ObjC: return "ObjC";
505  case SymbolLanguage::CXX: return "C++";
506  case SymbolLanguage::Swift: return "Swift";
507  }
508  llvm_unreachable("invalid symbol language kind");
509 }
510 
512  llvm::function_ref<void(SymbolProperty)> Fn) {
513 #define APPLY_FOR_PROPERTY(K) \
514  if (Props & (SymbolPropertySet)SymbolProperty::K) \
515  Fn(SymbolProperty::K)
516 
526 
527 #undef APPLY_FOR_PROPERTY
528 }
529 
530 void index::printSymbolProperties(SymbolPropertySet Props, raw_ostream &OS) {
531  bool VisitedOnce = false;
532  applyForEachSymbolProperty(Props, [&](SymbolProperty Prop) {
533  if (VisitedOnce)
534  OS << ',';
535  else
536  VisitedOnce = true;
537  switch (Prop) {
538  case SymbolProperty::Generic: OS << "Gen"; break;
539  case SymbolProperty::TemplatePartialSpecialization: OS << "TPS"; break;
540  case SymbolProperty::TemplateSpecialization: OS << "TS"; break;
541  case SymbolProperty::UnitTest: OS << "test"; break;
542  case SymbolProperty::IBAnnotated: OS << "IB"; break;
543  case SymbolProperty::IBOutletCollection: OS << "IBColl"; break;
544  case SymbolProperty::GKInspectable: OS << "GKI"; break;
545  case SymbolProperty::Local: OS << "local"; break;
546  case SymbolProperty::ProtocolInterface: OS << "protocol"; break;
547  }
548  });
549 }
Represents a function declaration or definition.
Definition: Decl.h:1739
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:341
ObjCInterfaceDecl * getClassInterface()
Definition: DeclObjC.cpp:1144
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:87
Defines the C++ template declaration subclasses.
uint16_t SymbolPropertySet
Definition: IndexSymbol.h:78
Defines the clang::MacroInfo and clang::MacroDirective classes.
Symbol is part of a protocol interface.
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:5025
The "__interface" keyword.
Definition: Type.h:5022
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:1872
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:97
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:1164
static bool isUnitTest(const ObjCMethodDecl *D)
Definition: IndexSymbol.cpp:34
QualType getReturnType() const
Definition: DeclObjC.h:323
unsigned SuppressTemplateArgsInCXXConstructors
When true, suppresses printing template arguments in names of C++ constructors.
unsigned SymbolRoleSet
Definition: IndexSymbol.h:124
bool hasAttr() const
Definition: DeclBase.h:531
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)
#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:427
ObjCInterfaceDecl * getSuperClass() const
Definition: DeclObjC.cpp:338
bool isInstanceMethod() const
Definition: DeclObjC.h:414
Selector getSelector() const
Definition: DeclObjC.h:321
static void checkForIBOutlets(const Decl *D, SymbolPropertySet &PropSet)
Definition: IndexSymbol.cpp:44
The "struct" keyword.
Definition: Type.h:5019
Represents the declaration of a struct/union/class/enum.
Definition: Decl.h:3073
Represents a static or instance method of a struct/union/class.
Definition: DeclCXX.h:2041
void applyForEachSymbolRole(SymbolRoleSet Roles, llvm::function_ref< void(SymbolRole)> Fn)
Dataflow Directional Tag Classes.
External linkage within a unique namespace.
Definition: Linkage.h:41
The name of a declaration.
Kind getKind() const
Definition: DeclBase.h:421
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:527
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:5028
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:300
bool isVoidType() const
Definition: Type.h:6530
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:5031
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:424
SymbolPropertySet Properties
Definition: IndexSymbol.h:139
ArrayRef< ParmVarDecl * > parameters() const
Definition: DeclObjC.h:367