clang  9.0.0svn
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/DeclCXX.h"
11 #include "clang/AST/DeclObjC.h"
12 #include "clang/AST/DeclTemplate.h"
14 #include "clang/Lex/MacroInfo.h"
15 
16 using namespace clang;
17 using namespace clang::index;
18 
19 /// \returns true if \c D is a subclass of 'XCTestCase'.
20 static bool isUnitTestCase(const ObjCInterfaceDecl *D) {
21  if (!D)
22  return false;
23  while (const ObjCInterfaceDecl *SuperD = D->getSuperClass()) {
24  if (SuperD->getName() == "XCTestCase")
25  return true;
26  D = SuperD;
27  }
28  return false;
29 }
30 
31 /// \returns true if \c D is in a subclass of 'XCTestCase', returns void, has
32 /// no parameters, and its name starts with 'test'.
33 static bool isUnitTest(const ObjCMethodDecl *D) {
34  if (!D->parameters().empty())
35  return false;
36  if (!D->getReturnType()->isVoidType())
37  return false;
38  if (!D->getSelector().getNameForSlot(0).startswith("test"))
39  return false;
40  return isUnitTestCase(D->getClassInterface());
41 }
42 
43 static void checkForIBOutlets(const Decl *D, SymbolPropertySet &PropSet) {
44  if (D->hasAttr<IBOutletAttr>()) {
46  } else if (D->hasAttr<IBOutletCollectionAttr>()) {
49  }
50 }
51 
53  assert(D);
54 
55  if (isa<ParmVarDecl>(D))
56  return true;
57 
58  if (isa<ObjCTypeParamDecl>(D))
59  return true;
60 
61  if (isa<UsingDirectiveDecl>(D))
62  return false;
63  if (!D->getParentFunctionOrMethod())
64  return false;
65 
66  if (const NamedDecl *ND = dyn_cast<NamedDecl>(D)) {
67  switch (ND->getFormalLinkage()) {
68  case NoLinkage:
69  case InternalLinkage:
70  return true;
71  case VisibleNoLinkage:
74  llvm_unreachable("Not a sema linkage");
75  case ModuleLinkage:
76  case ExternalLinkage:
77  return false;
78  }
79  }
80 
81  return true;
82 }
83 
85  assert(D);
86  SymbolInfo Info;
90  Info.Lang = SymbolLanguage::C;
91 
92  if (isFunctionLocalSymbol(D)) {
94  }
95  if (isa<ObjCProtocolDecl>(D->getDeclContext())) {
97  }
98 
99  if (const TagDecl *TD = dyn_cast<TagDecl>(D)) {
100  switch (TD->getTagKind()) {
101  case TTK_Struct:
102  Info.Kind = SymbolKind::Struct; break;
103  case TTK_Union:
104  Info.Kind = SymbolKind::Union; break;
105  case TTK_Class:
106  Info.Kind = SymbolKind::Class;
107  Info.Lang = SymbolLanguage::CXX;
108  break;
109  case TTK_Interface:
110  Info.Kind = SymbolKind::Protocol;
111  Info.Lang = SymbolLanguage::CXX;
112  break;
113  case TTK_Enum:
114  Info.Kind = SymbolKind::Enum; break;
115  }
116 
117  if (const CXXRecordDecl *CXXRec = dyn_cast<CXXRecordDecl>(D)) {
118  if (!CXXRec->isCLike()) {
119  Info.Lang = SymbolLanguage::CXX;
120  if (CXXRec->getDescribedClassTemplate()) {
122  }
123  }
124  }
125 
126  if (isa<ClassTemplatePartialSpecializationDecl>(D)) {
128  Info.Properties |=
130  } else if (isa<ClassTemplateSpecializationDecl>(D)) {
132  Info.Properties |=
134  }
135 
136  } else if (auto *VD = dyn_cast<VarDecl>(D)) {
137  Info.Kind = SymbolKind::Variable;
138  if (isa<ParmVarDecl>(D)) {
140  } else if (isa<CXXRecordDecl>(D->getDeclContext())) {
142  Info.Lang = SymbolLanguage::CXX;
143  }
144 
145  if (isa<VarTemplatePartialSpecializationDecl>(D)) {
146  Info.Lang = SymbolLanguage::CXX;
148  Info.Properties |=
150  } else if (isa<VarTemplateSpecializationDecl>(D)) {
151  Info.Lang = SymbolLanguage::CXX;
153  Info.Properties |=
155  } else if (VD->getDescribedVarTemplate()) {
156  Info.Lang = SymbolLanguage::CXX;
158  }
159 
160  } else {
161  switch (D->getKind()) {
162  case Decl::Import:
163  Info.Kind = SymbolKind::Module;
164  break;
165  case Decl::Typedef:
166  Info.Kind = SymbolKind::TypeAlias; break; // Lang = C
167  case Decl::Function:
168  Info.Kind = SymbolKind::Function;
169  break;
170  case Decl::Field:
171  Info.Kind = SymbolKind::Field;
172  if (const CXXRecordDecl *
173  CXXRec = dyn_cast<CXXRecordDecl>(D->getDeclContext())) {
174  if (!CXXRec->isCLike())
175  Info.Lang = SymbolLanguage::CXX;
176  }
177  break;
178  case Decl::EnumConstant:
179  Info.Kind = SymbolKind::EnumConstant; break;
180  case Decl::ObjCInterface:
181  case Decl::ObjCImplementation: {
182  Info.Kind = SymbolKind::Class;
183  Info.Lang = SymbolLanguage::ObjC;
184  const ObjCInterfaceDecl *ClsD = dyn_cast<ObjCInterfaceDecl>(D);
185  if (!ClsD)
186  ClsD = cast<ObjCImplementationDecl>(D)->getClassInterface();
187  if (isUnitTestCase(ClsD))
189  break;
190  }
191  case Decl::ObjCProtocol:
192  Info.Kind = SymbolKind::Protocol;
193  Info.Lang = SymbolLanguage::ObjC;
194  break;
195  case Decl::ObjCCategory:
196  case Decl::ObjCCategoryImpl: {
198  Info.Lang = SymbolLanguage::ObjC;
199  const ObjCInterfaceDecl *ClsD = nullptr;
200  if (auto *CatD = dyn_cast<ObjCCategoryDecl>(D))
201  ClsD = CatD->getClassInterface();
202  else
203  ClsD = cast<ObjCCategoryImplDecl>(D)->getClassInterface();
204  if (isUnitTestCase(ClsD))
206  break;
207  }
208  case Decl::ObjCMethod: {
209  const ObjCMethodDecl *MD = cast<ObjCMethodDecl>(D);
211  if (MD->isPropertyAccessor()) {
212  if (MD->param_size())
214  else
216  }
217  Info.Lang = SymbolLanguage::ObjC;
218  if (isUnitTest(MD))
220  if (D->hasAttr<IBActionAttr>())
222  break;
223  }
224  case Decl::ObjCProperty:
226  Info.Lang = SymbolLanguage::ObjC;
227  checkForIBOutlets(D, Info.Properties);
228  if (auto *Annot = D->getAttr<AnnotateAttr>()) {
229  if (Annot->getAnnotation() == "gk_inspectable")
231  }
232  break;
233  case Decl::ObjCIvar:
234  Info.Kind = SymbolKind::Field;
235  Info.Lang = SymbolLanguage::ObjC;
236  checkForIBOutlets(D, Info.Properties);
237  break;
238  case Decl::Namespace:
240  Info.Lang = SymbolLanguage::CXX;
241  break;
244  Info.Lang = SymbolLanguage::CXX;
245  break;
246  case Decl::CXXConstructor: {
248  Info.Lang = SymbolLanguage::CXX;
249  auto *CD = cast<CXXConstructorDecl>(D);
250  if (CD->isCopyConstructor())
252  else if (CD->isMoveConstructor())
254  break;
255  }
256  case Decl::CXXDestructor:
258  Info.Lang = SymbolLanguage::CXX;
259  break;
260  case Decl::CXXConversion:
262  Info.Lang = SymbolLanguage::CXX;
263  break;
264  case Decl::CXXMethod: {
265  const CXXMethodDecl *MD = cast<CXXMethodDecl>(D);
266  if (MD->isStatic())
268  else
270  Info.Lang = SymbolLanguage::CXX;
271  break;
272  }
273  case Decl::ClassTemplate:
274  Info.Kind = SymbolKind::Class;
276  Info.Lang = SymbolLanguage::CXX;
277  break;
278  case Decl::FunctionTemplate:
279  Info.Kind = SymbolKind::Function;
281  Info.Lang = SymbolLanguage::CXX;
282  if (const CXXMethodDecl *MD = dyn_cast_or_null<CXXMethodDecl>(
283  cast<FunctionTemplateDecl>(D)->getTemplatedDecl())) {
284  if (isa<CXXConstructorDecl>(MD))
286  else if (isa<CXXDestructorDecl>(MD))
288  else if (isa<CXXConversionDecl>(MD))
290  else {
291  if (MD->isStatic())
293  else
295  }
296  }
297  break;
298  case Decl::TypeAliasTemplate:
300  Info.Lang = SymbolLanguage::CXX;
302  break;
303  case Decl::TypeAlias:
305  Info.Lang = SymbolLanguage::CXX;
306  break;
307  case Decl::UnresolvedUsingTypename:
308  Info.Kind = SymbolKind::Using;
310  Info.Lang = SymbolLanguage::CXX;
312  break;
313  case Decl::UnresolvedUsingValue:
314  Info.Kind = SymbolKind::Using;
316  Info.Lang = SymbolLanguage::CXX;
318  break;
319  case Decl::Using:
320  Info.Kind = SymbolKind::Using;
321  Info.Lang = SymbolLanguage::CXX;
322  break;
323  case Decl::Binding:
324  Info.Kind = SymbolKind::Variable;
325  Info.Lang = SymbolLanguage::CXX;
326  break;
327  case Decl::MSProperty:
329  if (const CXXRecordDecl *CXXRec =
330  dyn_cast<CXXRecordDecl>(D->getDeclContext())) {
331  if (!CXXRec->isCLike())
332  Info.Lang = SymbolLanguage::CXX;
333  }
334  break;
335  default:
336  break;
337  }
338  }
339 
340  if (Info.Kind == SymbolKind::Unknown)
341  return Info;
342 
343  if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
344  if (FD->getTemplatedKind() ==
346  Info.Properties |= (SymbolPropertySet)SymbolProperty::Generic;
347  Info.Properties |=
349  }
350  }
351 
352  if (Info.Properties & (SymbolPropertySet)SymbolProperty::Generic)
353  Info.Lang = SymbolLanguage::CXX;
354 
355  if (auto *attr = D->getExternalSourceSymbolAttr()) {
356  if (attr->getLanguage() == "Swift")
357  Info.Lang = SymbolLanguage::Swift;
358  }
359 
360  return Info;
361 }
362 
364  SymbolInfo Info;
365  Info.Kind = SymbolKind::Macro;
367  Info.Properties = SymbolPropertySet();
368  Info.Lang = SymbolLanguage::C;
369  return Info;
370 }
371 
373  llvm::function_ref<bool(SymbolRole)> Fn) {
374 #define APPLY_FOR_ROLE(Role) \
375  if (Roles & (unsigned)SymbolRole::Role) \
376  if (!Fn(SymbolRole::Role)) \
377  return false;
378 
400 
401 #undef APPLY_FOR_ROLE
402 
403  return true;
404 }
405 
407  llvm::function_ref<void(SymbolRole)> Fn) {
408  applyForEachSymbolRoleInterruptible(Roles, [&](SymbolRole r) -> bool {
409  Fn(r);
410  return true;
411  });
412 }
413 
414 void index::printSymbolRoles(SymbolRoleSet Roles, raw_ostream &OS) {
415  bool VisitedOnce = false;
416  applyForEachSymbolRole(Roles, [&](SymbolRole Role) {
417  if (VisitedOnce)
418  OS << ',';
419  else
420  VisitedOnce = true;
421  switch (Role) {
422  case SymbolRole::Declaration: OS << "Decl"; break;
423  case SymbolRole::Definition: OS << "Def"; break;
424  case SymbolRole::Reference: OS << "Ref"; break;
425  case SymbolRole::Read: OS << "Read"; break;
426  case SymbolRole::Write: OS << "Writ"; break;
427  case SymbolRole::Call: OS << "Call"; break;
428  case SymbolRole::Dynamic: OS << "Dyn"; break;
429  case SymbolRole::AddressOf: OS << "Addr"; break;
430  case SymbolRole::Implicit: OS << "Impl"; break;
431  case SymbolRole::Undefinition: OS << "Undef"; break;
432  case SymbolRole::RelationChildOf: OS << "RelChild"; break;
433  case SymbolRole::RelationBaseOf: OS << "RelBase"; break;
434  case SymbolRole::RelationOverrideOf: OS << "RelOver"; break;
435  case SymbolRole::RelationReceivedBy: OS << "RelRec"; break;
436  case SymbolRole::RelationCalledBy: OS << "RelCall"; break;
437  case SymbolRole::RelationExtendedBy: OS << "RelExt"; break;
438  case SymbolRole::RelationAccessorOf: OS << "RelAcc"; break;
439  case SymbolRole::RelationContainedBy: OS << "RelCont"; break;
440  case SymbolRole::RelationIBTypeOf: OS << "RelIBType"; break;
441  case SymbolRole::RelationSpecializationOf: OS << "RelSpecialization"; break;
442  case SymbolRole::NameReference: OS << "NameReference"; break;
443  }
444  });
445 }
446 
447 bool index::printSymbolName(const Decl *D, const LangOptions &LO,
448  raw_ostream &OS) {
449  if (auto *ND = dyn_cast<NamedDecl>(D)) {
450  PrintingPolicy Policy(LO);
451  // Forward references can have different template argument names. Suppress
452  // the template argument names in constructors to make their name more
453  // stable.
455  DeclarationName DeclName = ND->getDeclName();
456  if (DeclName.isEmpty())
457  return true;
458  DeclName.print(OS, Policy);
459  return false;
460  } else {
461  return true;
462  }
463 }
464 
466  switch (K) {
467  case SymbolKind::Unknown: return "<unknown>";
468  case SymbolKind::Module: return "module";
469  case SymbolKind::Namespace: return "namespace";
470  case SymbolKind::NamespaceAlias: return "namespace-alias";
471  case SymbolKind::Macro: return "macro";
472  case SymbolKind::Enum: return "enum";
473  case SymbolKind::Struct: return "struct";
474  case SymbolKind::Class: return "class";
475  case SymbolKind::Protocol: return "protocol";
476  case SymbolKind::Extension: return "extension";
477  case SymbolKind::Union: return "union";
478  case SymbolKind::TypeAlias: return "type-alias";
479  case SymbolKind::Function: return "function";
480  case SymbolKind::Variable: return "variable";
481  case SymbolKind::Field: return "field";
482  case SymbolKind::EnumConstant: return "enumerator";
483  case SymbolKind::InstanceMethod: return "instance-method";
484  case SymbolKind::ClassMethod: return "class-method";
485  case SymbolKind::StaticMethod: return "static-method";
486  case SymbolKind::InstanceProperty: return "instance-property";
487  case SymbolKind::ClassProperty: return "class-property";
488  case SymbolKind::StaticProperty: return "static-property";
489  case SymbolKind::Constructor: return "constructor";
490  case SymbolKind::Destructor: return "destructor";
491  case SymbolKind::ConversionFunction: return "coversion-func";
492  case SymbolKind::Parameter: return "param";
493  case SymbolKind::Using: return "using";
494  }
495  llvm_unreachable("invalid symbol kind");
496 }
497 
499  switch (K) {
500  case SymbolSubKind::None: return "<none>";
501  case SymbolSubKind::CXXCopyConstructor: return "cxx-copy-ctor";
502  case SymbolSubKind::CXXMoveConstructor: return "cxx-move-ctor";
503  case SymbolSubKind::AccessorGetter: return "acc-get";
504  case SymbolSubKind::AccessorSetter: return "acc-set";
505  case SymbolSubKind::UsingTypename: return "using-typename";
506  case SymbolSubKind::UsingValue: return "using-value";
507  }
508  llvm_unreachable("invalid symbol subkind");
509 }
510 
512  switch (K) {
513  case SymbolLanguage::C: return "C";
514  case SymbolLanguage::ObjC: return "ObjC";
515  case SymbolLanguage::CXX: return "C++";
516  case SymbolLanguage::Swift: return "Swift";
517  }
518  llvm_unreachable("invalid symbol language kind");
519 }
520 
522  llvm::function_ref<void(SymbolProperty)> Fn) {
523 #define APPLY_FOR_PROPERTY(K) \
524  if (Props & (SymbolPropertySet)SymbolProperty::K) \
525  Fn(SymbolProperty::K)
526 
536 
537 #undef APPLY_FOR_PROPERTY
538 }
539 
540 void index::printSymbolProperties(SymbolPropertySet Props, raw_ostream &OS) {
541  bool VisitedOnce = false;
542  applyForEachSymbolProperty(Props, [&](SymbolProperty Prop) {
543  if (VisitedOnce)
544  OS << ',';
545  else
546  VisitedOnce = true;
547  switch (Prop) {
548  case SymbolProperty::Generic: OS << "Gen"; break;
549  case SymbolProperty::TemplatePartialSpecialization: OS << "TPS"; break;
550  case SymbolProperty::TemplateSpecialization: OS << "TS"; break;
551  case SymbolProperty::UnitTest: OS << "test"; break;
552  case SymbolProperty::IBAnnotated: OS << "IB"; break;
553  case SymbolProperty::IBOutletCollection: OS << "IBColl"; break;
554  case SymbolProperty::GKInspectable: OS << "GKI"; break;
555  case SymbolProperty::Local: OS << "local"; break;
556  case SymbolProperty::ProtocolInterface: OS << "protocol"; break;
557  }
558  });
559 }
Represents a function declaration or definition.
Definition: Decl.h:1737
External linkage, which indicates that the entity can be referred to from other translation units...
Definition: Linkage.h:59
if(T->getSizeExpr()) TRY_TO(TraverseStmt(T -> getSizeExpr()))
StringRef getSymbolLanguageString(SymbolLanguage K)
unsigned param_size() const
Definition: DeclObjC.h:340
ObjCInterfaceDecl * getClassInterface()
Definition: DeclObjC.cpp:1143
Internal linkage according to the Modules TS, but can be referred to from other translation units ind...
Definition: Linkage.h:49
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:26
Decl - This represents one declaration (or definition), e.g.
Definition: DeclBase.h:86
Defines the C++ template declaration subclasses.
uint16_t SymbolPropertySet
Definition: IndexSymbol.h:77
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:253
The "union" keyword.
Definition: Type.h:5049
The "__interface" keyword.
Definition: Type.h:5046
ObjCMethodDecl - Represents an instance or class method declaration.
Definition: DeclObjC.h:138
Describes how types, statements, expressions, and declarations should be printed. ...
Definition: PrettyPrinter.h:37
bool isStatic() const
Definition: DeclCXX.cpp:1882
static bool isUnitTestCase(const ObjCInterfaceDecl *D)
Definition: IndexSymbol.cpp:20
bool applyForEachSymbolRoleInterruptible(SymbolRoleSet Roles, llvm::function_ref< bool(SymbolRole)> Fn)
SymbolSubKind
Language specific sub-kinds.
Definition: IndexSymbol.h:67
#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:96
bool isFunctionLocalSymbol(const Decl *D)
Definition: IndexSymbol.cpp:52
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:49
StringRef getSymbolSubKindString(SymbolSubKind K)
Represents an ObjC class declaration.
Definition: DeclObjC.h:1171
static bool isUnitTest(const ObjCMethodDecl *D)
Definition: IndexSymbol.cpp:33
QualType getReturnType() const
Definition: DeclObjC.h:322
unsigned SuppressTemplateArgsInCXXConstructors
When true, suppresses printing template arguments in names of C++ constructors.
unsigned SymbolRoleSet
Definition: IndexSymbol.h:127
bool hasAttr() const
Definition: DeclBase.h:533
Module linkage, which indicates that the entity can be referred to from other translation units withi...
Definition: Linkage.h:55
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:429
ObjCInterfaceDecl * getSuperClass() const
Definition: DeclObjC.cpp:337
bool isInstanceMethod() const
Definition: DeclObjC.h:421
Selector getSelector() const
Definition: DeclObjC.h:320
static void checkForIBOutlets(const Decl *D, SymbolPropertySet &PropSet)
Definition: IndexSymbol.cpp:43
The "struct" keyword.
Definition: Type.h:5043
Represents the declaration of a struct/union/class/enum.
Definition: Decl.h:3063
Represents a static or instance method of a struct/union/class.
Definition: DeclCXX.h:2048
void applyForEachSymbolRole(SymbolRoleSet Roles, llvm::function_ref< void(SymbolRole)> Fn)
Dataflow Directional Tag Classes.
External linkage within a unique namespace.
Definition: Linkage.h:40
The name of a declaration.
Kind getKind() const
Definition: DeclBase.h:423
Encapsulates the data about a macro definition (e.g.
Definition: MacroInfo.h:39
SymbolProperty
Set of properties that provide additional info about a symbol.
Definition: IndexSymbol.h:79
void printSymbolRoles(SymbolRoleSet Roles, raw_ostream &OS)
T * getAttr() const
Definition: DeclBase.h:529
Internal linkage, which indicates that the entity can be referred to from within the translation unit...
Definition: Linkage.h:31
The "class" keyword.
Definition: Type.h:5052
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:84
Represents a C++ struct/union/class.
Definition: DeclCXX.h:299
bool isVoidType() const
Definition: Type.h:6558
No linkage according to the standard, but is visible from other translation units because of types de...
Definition: Linkage.h:44
The "enum" keyword.
Definition: Type.h:5055
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:431
SymbolPropertySet Properties
Definition: IndexSymbol.h:142
ArrayRef< ParmVarDecl * > parameters() const
Definition: DeclObjC.h:366