clang  3.9.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 
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, SymbolSubKindSet &SubKindSet) {
44  if (D->hasAttr<IBOutletAttr>()) {
45  SubKindSet |= (unsigned)SymbolSubKind::IBAnnotated;
46  } else if (D->hasAttr<IBOutletCollectionAttr>()) {
47  SubKindSet |= (unsigned)SymbolSubKind::IBAnnotated;
49  }
50 }
51 
53  assert(D);
54  SymbolInfo Info;
56  Info.SubKinds = SymbolSubKindSet();
57  Info.Lang = SymbolLanguage::C;
58 
59  if (const TagDecl *TD = dyn_cast<TagDecl>(D)) {
60  switch (TD->getTagKind()) {
61  case TTK_Struct:
62  Info.Kind = SymbolKind::Struct; break;
63  case TTK_Union:
64  Info.Kind = SymbolKind::Union; break;
65  case TTK_Class:
66  Info.Kind = SymbolKind::Class;
68  break;
69  case TTK_Interface:
72  break;
73  case TTK_Enum:
74  Info.Kind = SymbolKind::Enum; break;
75  }
76 
77  if (const CXXRecordDecl *CXXRec = dyn_cast<CXXRecordDecl>(D))
78  if (!CXXRec->isCLike())
80 
81  if (isa<ClassTemplatePartialSpecializationDecl>(D)) {
84  } else if (isa<ClassTemplateSpecializationDecl>(D)) {
87  }
88 
89  } else {
90  switch (D->getKind()) {
91  case Decl::Import:
92  Info.Kind = SymbolKind::Module;
93  break;
94  case Decl::Typedef:
95  Info.Kind = SymbolKind::TypeAlias; break; // Lang = C
96  case Decl::Function:
98  break;
99  case Decl::ParmVar:
100  Info.Kind = SymbolKind::Variable;
101  break;
102  case Decl::Var:
103  Info.Kind = SymbolKind::Variable;
104  if (isa<CXXRecordDecl>(D->getDeclContext())) {
106  Info.Lang = SymbolLanguage::CXX;
107  }
108  break;
109  case Decl::Field:
110  Info.Kind = SymbolKind::Field;
111  if (const CXXRecordDecl *
112  CXXRec = dyn_cast<CXXRecordDecl>(D->getDeclContext())) {
113  if (!CXXRec->isCLike())
114  Info.Lang = SymbolLanguage::CXX;
115  }
116  break;
117  case Decl::EnumConstant:
118  Info.Kind = SymbolKind::EnumConstant; break;
119  case Decl::ObjCInterface:
120  case Decl::ObjCImplementation: {
121  Info.Kind = SymbolKind::Class;
122  Info.Lang = SymbolLanguage::ObjC;
123  const ObjCInterfaceDecl *ClsD = dyn_cast<ObjCInterfaceDecl>(D);
124  if (!ClsD)
125  ClsD = cast<ObjCImplementationDecl>(D)->getClassInterface();
126  if (isUnitTestCase(ClsD))
128  break;
129  }
130  case Decl::ObjCProtocol:
131  Info.Kind = SymbolKind::Protocol;
132  Info.Lang = SymbolLanguage::ObjC;
133  break;
134  case Decl::ObjCCategory:
135  case Decl::ObjCCategoryImpl:
137  Info.Lang = SymbolLanguage::ObjC;
138  break;
139  case Decl::ObjCMethod:
140  if (cast<ObjCMethodDecl>(D)->isInstanceMethod())
142  else
144  Info.Lang = SymbolLanguage::ObjC;
145  if (isUnitTest(cast<ObjCMethodDecl>(D)))
146  Info.SubKinds |= (unsigned)SymbolSubKind::UnitTest;
147  if (D->hasAttr<IBActionAttr>())
148  Info.SubKinds |= (unsigned)SymbolSubKind::IBAnnotated;
149  break;
150  case Decl::ObjCProperty:
152  Info.Lang = SymbolLanguage::ObjC;
153  checkForIBOutlets(D, Info.SubKinds);
154  break;
155  case Decl::ObjCIvar:
156  Info.Kind = SymbolKind::Field;
157  Info.Lang = SymbolLanguage::ObjC;
158  checkForIBOutlets(D, Info.SubKinds);
159  break;
160  case Decl::Namespace:
162  Info.Lang = SymbolLanguage::CXX;
163  break;
166  Info.Lang = SymbolLanguage::CXX;
167  break;
168  case Decl::CXXConstructor:
170  Info.Lang = SymbolLanguage::CXX;
171  break;
172  case Decl::CXXDestructor:
174  Info.Lang = SymbolLanguage::CXX;
175  break;
176  case Decl::CXXConversion:
178  Info.Lang = SymbolLanguage::CXX;
179  break;
180  case Decl::CXXMethod: {
181  const CXXMethodDecl *MD = cast<CXXMethodDecl>(D);
182  if (MD->isStatic())
184  else
186  Info.Lang = SymbolLanguage::CXX;
187  break;
188  }
189  case Decl::ClassTemplate:
190  Info.Kind = SymbolKind::Class;
192  Info.Lang = SymbolLanguage::CXX;
193  break;
194  case Decl::FunctionTemplate:
195  Info.Kind = SymbolKind::Function;
197  Info.Lang = SymbolLanguage::CXX;
198  if (const CXXMethodDecl *MD = dyn_cast_or_null<CXXMethodDecl>(
199  cast<FunctionTemplateDecl>(D)->getTemplatedDecl())) {
200  if (isa<CXXConstructorDecl>(MD))
202  else if (isa<CXXDestructorDecl>(MD))
204  else if (isa<CXXConversionDecl>(MD))
206  else {
207  if (MD->isStatic())
209  else
211  }
212  }
213  break;
214  case Decl::TypeAliasTemplate:
216  Info.Lang = SymbolLanguage::CXX;
218  break;
219  case Decl::TypeAlias:
221  Info.Lang = SymbolLanguage::CXX;
222  break;
223  default:
224  break;
225  }
226  }
227 
228  if (Info.Kind == SymbolKind::Unknown)
229  return Info;
230 
231  if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
232  if (FD->getTemplatedKind() ==
234  Info.SubKinds |= (unsigned)SymbolSubKind::Generic;
236  }
237  }
238 
239  if (Info.SubKinds & (unsigned)SymbolSubKind::Generic)
240  Info.Lang = SymbolLanguage::CXX;
241 
242  return Info;
243 }
244 
246  llvm::function_ref<void(SymbolRole)> Fn) {
247 #define APPLY_FOR_ROLE(Role) \
248  if (Roles & (unsigned)SymbolRole::Role) \
249  Fn(SymbolRole::Role)
250 
265 
266 #undef APPLY_FOR_ROLE
267 }
268 
269 void index::printSymbolRoles(SymbolRoleSet Roles, raw_ostream &OS) {
270  bool VisitedOnce = false;
271  applyForEachSymbolRole(Roles, [&](SymbolRole Role) {
272  if (VisitedOnce)
273  OS << ',';
274  else
275  VisitedOnce = true;
276  switch (Role) {
277  case SymbolRole::Declaration: OS << "Decl"; break;
278  case SymbolRole::Definition: OS << "Def"; break;
279  case SymbolRole::Reference: OS << "Ref"; break;
280  case SymbolRole::Read: OS << "Read"; break;
281  case SymbolRole::Write: OS << "Writ"; break;
282  case SymbolRole::Call: OS << "Call"; break;
283  case SymbolRole::Dynamic: OS << "Dyn"; break;
284  case SymbolRole::AddressOf: OS << "Addr"; break;
285  case SymbolRole::Implicit: OS << "Impl"; break;
286  case SymbolRole::RelationChildOf: OS << "RelChild"; break;
287  case SymbolRole::RelationBaseOf: OS << "RelBase"; break;
288  case SymbolRole::RelationOverrideOf: OS << "RelOver"; break;
289  case SymbolRole::RelationReceivedBy: OS << "RelRec"; break;
290  case SymbolRole::RelationCalledBy: OS << "RelCall"; break;
291  }
292  });
293 }
294 
295 bool index::printSymbolName(const Decl *D, const LangOptions &LO,
296  raw_ostream &OS) {
297  if (auto *ND = dyn_cast<NamedDecl>(D)) {
298  PrintingPolicy Policy(LO);
299  // Forward references can have different template argument names. Suppress
300  // the template argument names in constructors to make their name more
301  // stable.
303  DeclarationName DeclName = ND->getDeclName();
304  if (DeclName.isEmpty())
305  return true;
306  DeclName.print(OS, Policy);
307  return false;
308  } else {
309  return true;
310  }
311 }
312 
314  switch (K) {
315  case SymbolKind::Unknown: return "<unknown>";
316  case SymbolKind::Module: return "module";
317  case SymbolKind::Namespace: return "namespace";
318  case SymbolKind::NamespaceAlias: return "namespace-alias";
319  case SymbolKind::Macro: return "macro";
320  case SymbolKind::Enum: return "enum";
321  case SymbolKind::Struct: return "struct";
322  case SymbolKind::Class: return "class";
323  case SymbolKind::Protocol: return "protocol";
324  case SymbolKind::Extension: return "extension";
325  case SymbolKind::Union: return "union";
326  case SymbolKind::TypeAlias: return "type-alias";
327  case SymbolKind::Function: return "function";
328  case SymbolKind::Variable: return "variable";
329  case SymbolKind::Field: return "field";
330  case SymbolKind::EnumConstant: return "enumerator";
331  case SymbolKind::InstanceMethod: return "instance-method";
332  case SymbolKind::ClassMethod: return "class-method";
333  case SymbolKind::StaticMethod: return "static-method";
334  case SymbolKind::InstanceProperty: return "instance-property";
335  case SymbolKind::ClassProperty: return "class-property";
336  case SymbolKind::StaticProperty: return "static-property";
337  case SymbolKind::Constructor: return "constructor";
338  case SymbolKind::Destructor: return "destructor";
339  case SymbolKind::ConversionFunction: return "coversion-func";
340  }
341  llvm_unreachable("invalid symbol kind");
342 }
343 
345  switch (K) {
346  case SymbolLanguage::C: return "C";
347  case SymbolLanguage::ObjC: return "ObjC";
348  case SymbolLanguage::CXX: return "C++";
349  }
350  llvm_unreachable("invalid symbol language kind");
351 }
352 
354  llvm::function_ref<void(SymbolSubKind)> Fn) {
355 #define APPLY_FOR_SUBKIND(K) \
356  if (SubKinds & (unsigned)SymbolSubKind::K) \
357  Fn(SymbolSubKind::K)
358 
365 
366 #undef APPLY_FOR_SUBKIND
367 }
368 
369 void index::printSymbolSubKinds(SymbolSubKindSet SubKinds, raw_ostream &OS) {
370  bool VisitedOnce = false;
371  applyForEachSymbolSubKind(SubKinds, [&](SymbolSubKind SubKind) {
372  if (VisitedOnce)
373  OS << ',';
374  else
375  VisitedOnce = true;
376  switch (SubKind) {
377  case SymbolSubKind::Generic: OS << "Gen"; break;
378  case SymbolSubKind::TemplatePartialSpecialization: OS << "TPS"; break;
379  case SymbolSubKind::TemplateSpecialization: OS << "TS"; break;
380  case SymbolSubKind::UnitTest: OS << "test"; break;
381  case SymbolSubKind::IBAnnotated: OS << "IB"; break;
382  case SymbolSubKind::IBOutletCollection: OS << "IBColl"; break;
383  }
384  });
385 }
FunctionDecl - An instance of this class is created to represent a function declaration or definition...
Definition: Decl.h:1561
static void checkForIBOutlets(const Decl *D, SymbolSubKindSet &SubKindSet)
Definition: IndexSymbol.cpp:43
StringRef getSymbolLanguageString(SymbolLanguage K)
ObjCInterfaceDecl * getClassInterface()
Definition: DeclObjC.cpp:1071
bool printSymbolName(const Decl *D, const LangOptions &LO, raw_ostream &OS)
Defines the C++ template declaration subclasses.
The "union" keyword.
Definition: Type.h:4348
The "__interface" keyword.
Definition: Type.h:4346
ObjCMethodDecl - Represents an instance or class method declaration.
Definition: DeclObjC.h:113
Describes how types, statements, expressions, and declarations should be printed. ...
Definition: PrettyPrinter.h:38
SymbolRole
Set of roles that are attributed to symbol occurrences.
Definition: IndexSymbol.h:74
bool isVoidType() const
Definition: Type.h:5680
static bool isUnitTestCase(const ObjCInterfaceDecl *D)
Definition: IndexSymbol.cpp:20
void print(raw_ostream &OS, const PrintingPolicy &Policy)
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:48
Represents an ObjC class declaration.
Definition: DeclObjC.h:1091
static bool isUnitTest(const ObjCMethodDecl *D)
Definition: IndexSymbol.cpp:33
unsigned SuppressTemplateArgsInCXXConstructors
When true, suppresses printing template arguments in names of C++ constructors.
bool isStatic() const
Definition: DeclCXX.cpp:1475
void printSymbolSubKinds(SymbolSubKindSet SubKinds, raw_ostream &OS)
bool isEmpty() const
Evaluates true when this declaration name is empty.
#define APPLY_FOR_ROLE(Role)
ArrayRef< ParmVarDecl * > parameters() const
Definition: DeclObjC.h:371
The "struct" keyword.
Definition: Type.h:4344
#define APPLY_FOR_SUBKIND(K)
TagDecl - Represents the declaration of a struct/union/class/enum.
Definition: Decl.h:2727
StringRef getNameForSlot(unsigned argIndex) const
Retrieve the name at a given position in the selector.
Represents a static or instance method of a struct/union/class.
Definition: DeclCXX.h:1736
QualType getReturnType() const
Definition: DeclObjC.h:330
unsigned TypeAlias
Whether this template specialization type is a substituted type alias.
Definition: Type.h:4171
void applyForEachSymbolRole(SymbolRoleSet Roles, llvm::function_ref< void(SymbolRole)> Fn)
if(T->getSizeExpr()) TRY_TO(TraverseStmt(T-> getSizeExpr()))
DeclarationName - The name of a declaration.
unsigned SymbolSubKindSet
Definition: IndexSymbol.h:71
Selector getSelector() const
Definition: DeclObjC.h:328
SymbolSubKindSet SubKinds
Definition: IndexSymbol.h:106
void printSymbolRoles(SymbolRoleSet Roles, raw_ostream &OS)
The "class" keyword.
Definition: Type.h:4350
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:52
Represents a C++ struct/union/class.
Definition: DeclCXX.h:263
The "enum" keyword.
Definition: Type.h:4352
ObjCInterfaceDecl * getSuperClass() const
Definition: DeclObjC.cpp:314
void applyForEachSymbolSubKind(SymbolSubKindSet SubKinds, llvm::function_ref< void(SymbolSubKind)> Fn)