]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/llvm/tools/clang/lib/Index/IndexSymbol.cpp
Merge llvm, clang, lld, lldb, compiler-rt and libc++ r306956, and update
[FreeBSD/FreeBSD.git] / contrib / llvm / tools / clang / lib / Index / IndexSymbol.cpp
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
10 #include "clang/Index/IndexSymbol.h"
11 #include "clang/AST/DeclCXX.h"
12 #include "clang/AST/DeclObjC.h"
13 #include "clang/AST/DeclTemplate.h"
14 #include "clang/AST/PrettyPrinter.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>()) {
45     PropSet |= (unsigned)SymbolProperty::IBAnnotated;
46   } else if (D->hasAttr<IBOutletCollectionAttr>()) {
47     PropSet |= (unsigned)SymbolProperty::IBAnnotated;
48     PropSet |= (unsigned)SymbolProperty::IBOutletCollection;
49   }
50 }
51
52 bool index::isFunctionLocalSymbol(const Decl *D) {
53   assert(D);
54
55   if (isa<ParmVarDecl>(D))
56     return true;
57
58   if (isa<TemplateTemplateParmDecl>(D))
59     return true;
60
61   if (isa<ObjCTypeParamDecl>(D))
62     return true;
63
64   if (isa<UsingDirectiveDecl>(D))
65     return false;
66   if (!D->getParentFunctionOrMethod())
67     return false;
68
69   if (const NamedDecl *ND = dyn_cast<NamedDecl>(D)) {
70     switch (ND->getFormalLinkage()) {
71       case NoLinkage:
72       case VisibleNoLinkage:
73       case InternalLinkage:
74         return true;
75       case UniqueExternalLinkage:
76         llvm_unreachable("Not a sema linkage");
77       case ExternalLinkage:
78         return false;
79     }
80   }
81
82   return true;
83 }
84
85 SymbolInfo index::getSymbolInfo(const Decl *D) {
86   assert(D);
87   SymbolInfo Info;
88   Info.Kind = SymbolKind::Unknown;
89   Info.SubKind = SymbolSubKind::None;
90   Info.Properties = SymbolPropertySet();
91   Info.Lang = SymbolLanguage::C;
92
93   if (isFunctionLocalSymbol(D)) {
94     Info.Properties |= (unsigned)SymbolProperty::Local;
95   }
96
97   if (const TagDecl *TD = dyn_cast<TagDecl>(D)) {
98     switch (TD->getTagKind()) {
99     case TTK_Struct:
100       Info.Kind = SymbolKind::Struct; break;
101     case TTK_Union:
102       Info.Kind = SymbolKind::Union; break;
103     case TTK_Class:
104       Info.Kind = SymbolKind::Class;
105       Info.Lang = SymbolLanguage::CXX;
106       break;
107     case TTK_Interface:
108       Info.Kind = SymbolKind::Protocol;
109       Info.Lang = SymbolLanguage::CXX;
110       break;
111     case TTK_Enum:
112       Info.Kind = SymbolKind::Enum; break;
113     }
114
115     if (const CXXRecordDecl *CXXRec = dyn_cast<CXXRecordDecl>(D)) {
116       if (!CXXRec->isCLike()) {
117         Info.Lang = SymbolLanguage::CXX;
118         if (CXXRec->getDescribedClassTemplate()) {
119           Info.Properties |= (unsigned)SymbolProperty::Generic;
120         }
121       }
122     }
123
124     if (isa<ClassTemplatePartialSpecializationDecl>(D)) {
125       Info.Properties |= (unsigned)SymbolProperty::Generic;
126       Info.Properties |= (unsigned)SymbolProperty::TemplatePartialSpecialization;
127     } else if (isa<ClassTemplateSpecializationDecl>(D)) {
128       Info.Properties |= (unsigned)SymbolProperty::Generic;
129       Info.Properties |= (unsigned)SymbolProperty::TemplateSpecialization;
130     }
131
132   } else if (auto *VD = dyn_cast<VarDecl>(D)) {
133     Info.Kind = SymbolKind::Variable;
134     if (isa<ParmVarDecl>(D)) {
135       Info.Kind = SymbolKind::Parameter;
136     } else if (isa<CXXRecordDecl>(D->getDeclContext())) {
137       Info.Kind = SymbolKind::StaticProperty;
138       Info.Lang = SymbolLanguage::CXX;
139     }
140
141     if (isa<VarTemplatePartialSpecializationDecl>(D)) {
142       Info.Lang = SymbolLanguage::CXX;
143       Info.Properties |= (unsigned)SymbolProperty::Generic;
144       Info.Properties |= (unsigned)SymbolProperty::TemplatePartialSpecialization;
145     } else if (isa<VarTemplateSpecializationDecl>(D)) {
146       Info.Lang = SymbolLanguage::CXX;
147       Info.Properties |= (unsigned)SymbolProperty::Generic;
148       Info.Properties |= (unsigned)SymbolProperty::TemplateSpecialization;
149     } else if (VD->getDescribedVarTemplate()) {
150       Info.Lang = SymbolLanguage::CXX;
151       Info.Properties |= (unsigned)SymbolProperty::Generic;
152     }
153
154   } else {
155     switch (D->getKind()) {
156     case Decl::Import:
157       Info.Kind = SymbolKind::Module;
158       break;
159     case Decl::Typedef:
160       Info.Kind = SymbolKind::TypeAlias; break; // Lang = C
161     case Decl::Function:
162       Info.Kind = SymbolKind::Function;
163       break;
164     case Decl::Field:
165       Info.Kind = SymbolKind::Field;
166       if (const CXXRecordDecl *
167             CXXRec = dyn_cast<CXXRecordDecl>(D->getDeclContext())) {
168         if (!CXXRec->isCLike())
169           Info.Lang = SymbolLanguage::CXX;
170       }
171       break;
172     case Decl::EnumConstant:
173       Info.Kind = SymbolKind::EnumConstant; break;
174     case Decl::ObjCInterface:
175     case Decl::ObjCImplementation: {
176       Info.Kind = SymbolKind::Class;
177       Info.Lang = SymbolLanguage::ObjC;
178       const ObjCInterfaceDecl *ClsD = dyn_cast<ObjCInterfaceDecl>(D);
179       if (!ClsD)
180         ClsD = cast<ObjCImplementationDecl>(D)->getClassInterface();
181       if (isUnitTestCase(ClsD))
182         Info.Properties |= (unsigned)SymbolProperty::UnitTest;
183       break;
184     }
185     case Decl::ObjCProtocol:
186       Info.Kind = SymbolKind::Protocol;
187       Info.Lang = SymbolLanguage::ObjC;
188       break;
189     case Decl::ObjCCategory:
190     case Decl::ObjCCategoryImpl: {
191       Info.Kind = SymbolKind::Extension;
192       Info.Lang = SymbolLanguage::ObjC;
193       const ObjCInterfaceDecl *ClsD = nullptr;
194       if (auto *CatD = dyn_cast<ObjCCategoryDecl>(D))
195         ClsD = CatD->getClassInterface();
196       else
197         ClsD = cast<ObjCCategoryImplDecl>(D)->getClassInterface();
198       if (isUnitTestCase(ClsD))
199         Info.Properties |= (unsigned)SymbolProperty::UnitTest;
200       break;
201     }
202     case Decl::ObjCMethod:
203       if (cast<ObjCMethodDecl>(D)->isInstanceMethod()) {
204         const ObjCMethodDecl *MD = cast<ObjCMethodDecl>(D);
205         Info.Kind = SymbolKind::InstanceMethod;
206         if (MD->isPropertyAccessor()) {
207           if (MD->param_size())
208             Info.SubKind = SymbolSubKind::AccessorSetter;
209           else
210             Info.SubKind = SymbolSubKind::AccessorGetter;
211         }
212       } else {
213         Info.Kind = SymbolKind::ClassMethod;
214       }
215       Info.Lang = SymbolLanguage::ObjC;
216       if (isUnitTest(cast<ObjCMethodDecl>(D)))
217         Info.Properties |= (unsigned)SymbolProperty::UnitTest;
218       if (D->hasAttr<IBActionAttr>())
219         Info.Properties |= (unsigned)SymbolProperty::IBAnnotated;
220       break;
221     case Decl::ObjCProperty:
222       Info.Kind = SymbolKind::InstanceProperty;
223       Info.Lang = SymbolLanguage::ObjC;
224       checkForIBOutlets(D, Info.Properties);
225       if (auto *Annot = D->getAttr<AnnotateAttr>()) {
226         if (Annot->getAnnotation() == "gk_inspectable")
227           Info.Properties |= (unsigned)SymbolProperty::GKInspectable;
228       }
229       break;
230     case Decl::ObjCIvar:
231       Info.Kind = SymbolKind::Field;
232       Info.Lang = SymbolLanguage::ObjC;
233       checkForIBOutlets(D, Info.Properties);
234       break;
235     case Decl::Namespace:
236       Info.Kind = SymbolKind::Namespace;
237       Info.Lang = SymbolLanguage::CXX;
238       break;
239     case Decl::NamespaceAlias:
240       Info.Kind = SymbolKind::NamespaceAlias;
241       Info.Lang = SymbolLanguage::CXX;
242       break;
243     case Decl::CXXConstructor: {
244       Info.Kind = SymbolKind::Constructor;
245       Info.Lang = SymbolLanguage::CXX;
246       auto *CD = cast<CXXConstructorDecl>(D);
247       if (CD->isCopyConstructor())
248         Info.SubKind = SymbolSubKind::CXXCopyConstructor;
249       else if (CD->isMoveConstructor())
250         Info.SubKind = SymbolSubKind::CXXMoveConstructor;
251       break;
252     }
253     case Decl::CXXDestructor:
254       Info.Kind = SymbolKind::Destructor;
255       Info.Lang = SymbolLanguage::CXX;
256       break;
257     case Decl::CXXConversion:
258       Info.Kind = SymbolKind::ConversionFunction;
259       Info.Lang = SymbolLanguage::CXX;
260       break;
261     case Decl::CXXMethod: {
262       const CXXMethodDecl *MD = cast<CXXMethodDecl>(D);
263       if (MD->isStatic())
264         Info.Kind = SymbolKind::StaticMethod;
265       else
266         Info.Kind = SymbolKind::InstanceMethod;
267       Info.Lang = SymbolLanguage::CXX;
268       break;
269     }
270     case Decl::ClassTemplate:
271       Info.Kind = SymbolKind::Class;
272       Info.Properties |= (unsigned)SymbolProperty::Generic;
273       Info.Lang = SymbolLanguage::CXX;
274       break;
275     case Decl::FunctionTemplate:
276       Info.Kind = SymbolKind::Function;
277       Info.Properties |= (unsigned)SymbolProperty::Generic;
278       Info.Lang = SymbolLanguage::CXX;
279       if (const CXXMethodDecl *MD = dyn_cast_or_null<CXXMethodDecl>(
280                            cast<FunctionTemplateDecl>(D)->getTemplatedDecl())) {
281         if (isa<CXXConstructorDecl>(MD))
282           Info.Kind = SymbolKind::Constructor;
283         else if (isa<CXXDestructorDecl>(MD))
284           Info.Kind = SymbolKind::Destructor;
285         else if (isa<CXXConversionDecl>(MD))
286           Info.Kind = SymbolKind::ConversionFunction;
287         else {
288           if (MD->isStatic())
289             Info.Kind = SymbolKind::StaticMethod;
290           else
291             Info.Kind = SymbolKind::InstanceMethod;
292         }
293       }
294       break;
295     case Decl::TypeAliasTemplate:
296       Info.Kind = SymbolKind::TypeAlias;
297       Info.Lang = SymbolLanguage::CXX;
298       Info.Properties |= (unsigned)SymbolProperty::Generic;
299       break;
300     case Decl::TypeAlias:
301       Info.Kind = SymbolKind::TypeAlias;
302       Info.Lang = SymbolLanguage::CXX;
303       break;
304     case Decl::Binding:
305       Info.Kind = SymbolKind::Variable;
306       Info.Lang = SymbolLanguage::CXX;
307       break;
308     default:
309       break;
310     }
311   }
312
313   if (Info.Kind == SymbolKind::Unknown)
314     return Info;
315
316   if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
317     if (FD->getTemplatedKind() ==
318           FunctionDecl::TK_FunctionTemplateSpecialization) {
319       Info.Properties |= (unsigned)SymbolProperty::Generic;
320       Info.Properties |= (unsigned)SymbolProperty::TemplateSpecialization;
321     }
322   }
323
324   if (Info.Properties & (unsigned)SymbolProperty::Generic)
325     Info.Lang = SymbolLanguage::CXX;
326
327   if (auto *attr = D->getExternalSourceSymbolAttr()) {
328     if (attr->getLanguage() == "Swift")
329       Info.Lang = SymbolLanguage::Swift;
330   }
331
332   return Info;
333 }
334
335 bool index::applyForEachSymbolRoleInterruptible(SymbolRoleSet Roles,
336                                    llvm::function_ref<bool(SymbolRole)> Fn) {
337 #define APPLY_FOR_ROLE(Role) \
338   if (Roles & (unsigned)SymbolRole::Role) \
339     if (!Fn(SymbolRole::Role)) \
340       return false;
341
342   APPLY_FOR_ROLE(Declaration);
343   APPLY_FOR_ROLE(Definition);
344   APPLY_FOR_ROLE(Reference);
345   APPLY_FOR_ROLE(Read);
346   APPLY_FOR_ROLE(Write);
347   APPLY_FOR_ROLE(Call);
348   APPLY_FOR_ROLE(Dynamic);
349   APPLY_FOR_ROLE(AddressOf);
350   APPLY_FOR_ROLE(Implicit);
351   APPLY_FOR_ROLE(RelationChildOf);
352   APPLY_FOR_ROLE(RelationBaseOf);
353   APPLY_FOR_ROLE(RelationOverrideOf);
354   APPLY_FOR_ROLE(RelationReceivedBy);
355   APPLY_FOR_ROLE(RelationCalledBy);
356   APPLY_FOR_ROLE(RelationExtendedBy);
357   APPLY_FOR_ROLE(RelationAccessorOf);
358   APPLY_FOR_ROLE(RelationContainedBy);
359   APPLY_FOR_ROLE(RelationIBTypeOf);
360   APPLY_FOR_ROLE(RelationSpecializationOf);
361
362 #undef APPLY_FOR_ROLE
363
364   return true;
365 }
366
367 void index::applyForEachSymbolRole(SymbolRoleSet Roles,
368                                    llvm::function_ref<void(SymbolRole)> Fn) {
369   applyForEachSymbolRoleInterruptible(Roles, [&](SymbolRole r) -> bool {
370     Fn(r);
371     return true;
372   });
373 }
374
375 void index::printSymbolRoles(SymbolRoleSet Roles, raw_ostream &OS) {
376   bool VisitedOnce = false;
377   applyForEachSymbolRole(Roles, [&](SymbolRole Role) {
378     if (VisitedOnce)
379       OS << ',';
380     else
381       VisitedOnce = true;
382     switch (Role) {
383     case SymbolRole::Declaration: OS << "Decl"; break;
384     case SymbolRole::Definition: OS << "Def"; break;
385     case SymbolRole::Reference: OS << "Ref"; break;
386     case SymbolRole::Read: OS << "Read"; break;
387     case SymbolRole::Write: OS << "Writ"; break;
388     case SymbolRole::Call: OS << "Call"; break;
389     case SymbolRole::Dynamic: OS << "Dyn"; break;
390     case SymbolRole::AddressOf: OS << "Addr"; break;
391     case SymbolRole::Implicit: OS << "Impl"; break;
392     case SymbolRole::RelationChildOf: OS << "RelChild"; break;
393     case SymbolRole::RelationBaseOf: OS << "RelBase"; break;
394     case SymbolRole::RelationOverrideOf: OS << "RelOver"; break;
395     case SymbolRole::RelationReceivedBy: OS << "RelRec"; break;
396     case SymbolRole::RelationCalledBy: OS << "RelCall"; break;
397     case SymbolRole::RelationExtendedBy: OS << "RelExt"; break;
398     case SymbolRole::RelationAccessorOf: OS << "RelAcc"; break;
399     case SymbolRole::RelationContainedBy: OS << "RelCont"; break;
400     case SymbolRole::RelationIBTypeOf: OS << "RelIBType"; break;
401     case SymbolRole::RelationSpecializationOf: OS << "RelSpecialization"; break;
402     }
403   });
404 }
405
406 bool index::printSymbolName(const Decl *D, const LangOptions &LO,
407                             raw_ostream &OS) {
408   if (auto *ND = dyn_cast<NamedDecl>(D)) {
409     PrintingPolicy Policy(LO);
410     // Forward references can have different template argument names. Suppress
411     // the template argument names in constructors to make their name more
412     // stable.
413     Policy.SuppressTemplateArgsInCXXConstructors = true;
414     DeclarationName DeclName = ND->getDeclName();
415     if (DeclName.isEmpty())
416       return true;
417     DeclName.print(OS, Policy);
418     return false;
419   } else {
420     return true;
421   }
422 }
423
424 StringRef index::getSymbolKindString(SymbolKind K) {
425   switch (K) {
426   case SymbolKind::Unknown: return "<unknown>";
427   case SymbolKind::Module: return "module";
428   case SymbolKind::Namespace: return "namespace";
429   case SymbolKind::NamespaceAlias: return "namespace-alias";
430   case SymbolKind::Macro: return "macro";
431   case SymbolKind::Enum: return "enum";
432   case SymbolKind::Struct: return "struct";
433   case SymbolKind::Class: return "class";
434   case SymbolKind::Protocol: return "protocol";
435   case SymbolKind::Extension: return "extension";
436   case SymbolKind::Union: return "union";
437   case SymbolKind::TypeAlias: return "type-alias";
438   case SymbolKind::Function: return "function";
439   case SymbolKind::Variable: return "variable";
440   case SymbolKind::Field: return "field";
441   case SymbolKind::EnumConstant: return "enumerator";
442   case SymbolKind::InstanceMethod: return "instance-method";
443   case SymbolKind::ClassMethod: return "class-method";
444   case SymbolKind::StaticMethod: return "static-method";
445   case SymbolKind::InstanceProperty: return "instance-property";
446   case SymbolKind::ClassProperty: return "class-property";
447   case SymbolKind::StaticProperty: return "static-property";
448   case SymbolKind::Constructor: return "constructor";
449   case SymbolKind::Destructor: return "destructor";
450   case SymbolKind::ConversionFunction: return "coversion-func";
451   case SymbolKind::Parameter: return "param";
452   }
453   llvm_unreachable("invalid symbol kind");
454 }
455
456 StringRef index::getSymbolSubKindString(SymbolSubKind K) {
457   switch (K) {
458   case SymbolSubKind::None: return "<none>";
459   case SymbolSubKind::CXXCopyConstructor: return "cxx-copy-ctor";
460   case SymbolSubKind::CXXMoveConstructor: return "cxx-move-ctor";
461   case SymbolSubKind::AccessorGetter: return "acc-get";
462   case SymbolSubKind::AccessorSetter: return "acc-set";
463   }
464   llvm_unreachable("invalid symbol subkind");
465 }
466
467 StringRef index::getSymbolLanguageString(SymbolLanguage K) {
468   switch (K) {
469   case SymbolLanguage::C: return "C";
470   case SymbolLanguage::ObjC: return "ObjC";
471   case SymbolLanguage::CXX: return "C++";
472   case SymbolLanguage::Swift: return "Swift";
473   }
474   llvm_unreachable("invalid symbol language kind");
475 }
476
477 void index::applyForEachSymbolProperty(SymbolPropertySet Props,
478                                   llvm::function_ref<void(SymbolProperty)> Fn) {
479 #define APPLY_FOR_PROPERTY(K) \
480   if (Props & (unsigned)SymbolProperty::K) \
481     Fn(SymbolProperty::K)
482
483   APPLY_FOR_PROPERTY(Generic);
484   APPLY_FOR_PROPERTY(TemplatePartialSpecialization);
485   APPLY_FOR_PROPERTY(TemplateSpecialization);
486   APPLY_FOR_PROPERTY(UnitTest);
487   APPLY_FOR_PROPERTY(IBAnnotated);
488   APPLY_FOR_PROPERTY(IBOutletCollection);
489   APPLY_FOR_PROPERTY(GKInspectable);
490   APPLY_FOR_PROPERTY(Local);
491
492 #undef APPLY_FOR_PROPERTY
493 }
494
495 void index::printSymbolProperties(SymbolPropertySet Props, raw_ostream &OS) {
496   bool VisitedOnce = false;
497   applyForEachSymbolProperty(Props, [&](SymbolProperty Prop) {
498     if (VisitedOnce)
499       OS << ',';
500     else
501       VisitedOnce = true;
502     switch (Prop) {
503     case SymbolProperty::Generic: OS << "Gen"; break;
504     case SymbolProperty::TemplatePartialSpecialization: OS << "TPS"; break;
505     case SymbolProperty::TemplateSpecialization: OS << "TS"; break;
506     case SymbolProperty::UnitTest: OS << "test"; break;
507     case SymbolProperty::IBAnnotated: OS << "IB"; break;
508     case SymbolProperty::IBOutletCollection: OS << "IBColl"; break;
509     case SymbolProperty::GKInspectable: OS << "GKI"; break;
510     case SymbolProperty::Local: OS << "local"; break;
511     }
512   });
513 }