2016-02-13 07:10:59 +08:00
|
|
|
//===--- IndexSymbol.cpp - Types and functions for indexing symbols -------===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "clang/Index/IndexSymbol.h"
|
|
|
|
#include "clang/AST/DeclCXX.h"
|
|
|
|
#include "clang/AST/DeclObjC.h"
|
|
|
|
#include "clang/AST/DeclTemplate.h"
|
2016-02-15 09:32:36 +08:00
|
|
|
#include "clang/AST/PrettyPrinter.h"
|
2016-02-13 07:10:59 +08:00
|
|
|
|
|
|
|
using namespace clang;
|
|
|
|
using namespace clang::index;
|
|
|
|
|
|
|
|
SymbolInfo index::getSymbolInfo(const Decl *D) {
|
|
|
|
assert(D);
|
|
|
|
SymbolInfo Info;
|
|
|
|
Info.Kind = SymbolKind::Unknown;
|
|
|
|
Info.TemplateKind = SymbolCXXTemplateKind::NonTemplate;
|
|
|
|
Info.Lang = SymbolLanguage::C;
|
|
|
|
|
|
|
|
if (const TagDecl *TD = dyn_cast<TagDecl>(D)) {
|
|
|
|
switch (TD->getTagKind()) {
|
|
|
|
case TTK_Struct:
|
|
|
|
Info.Kind = SymbolKind::Struct; break;
|
|
|
|
case TTK_Union:
|
|
|
|
Info.Kind = SymbolKind::Union; break;
|
|
|
|
case TTK_Class:
|
2016-03-26 01:01:59 +08:00
|
|
|
Info.Kind = SymbolKind::Class;
|
2016-02-13 07:10:59 +08:00
|
|
|
Info.Lang = SymbolLanguage::CXX;
|
|
|
|
break;
|
|
|
|
case TTK_Interface:
|
2016-03-26 01:01:59 +08:00
|
|
|
Info.Kind = SymbolKind::Protocol;
|
2016-02-13 07:10:59 +08:00
|
|
|
Info.Lang = SymbolLanguage::CXX;
|
|
|
|
break;
|
|
|
|
case TTK_Enum:
|
|
|
|
Info.Kind = SymbolKind::Enum; break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (const CXXRecordDecl *CXXRec = dyn_cast<CXXRecordDecl>(D))
|
|
|
|
if (!CXXRec->isCLike())
|
|
|
|
Info.Lang = SymbolLanguage::CXX;
|
|
|
|
|
|
|
|
if (isa<ClassTemplatePartialSpecializationDecl>(D)) {
|
|
|
|
Info.TemplateKind = SymbolCXXTemplateKind::TemplatePartialSpecialization;
|
|
|
|
} else if (isa<ClassTemplateSpecializationDecl>(D)) {
|
|
|
|
Info.TemplateKind = SymbolCXXTemplateKind::TemplateSpecialization;
|
|
|
|
}
|
|
|
|
|
|
|
|
} else {
|
|
|
|
switch (D->getKind()) {
|
2016-02-29 15:56:07 +08:00
|
|
|
case Decl::Import:
|
|
|
|
Info.Kind = SymbolKind::Module;
|
|
|
|
break;
|
2016-02-13 07:10:59 +08:00
|
|
|
case Decl::Typedef:
|
2016-03-26 01:01:59 +08:00
|
|
|
Info.Kind = SymbolKind::TypeAlias; break; // Lang = C
|
2016-02-13 07:10:59 +08:00
|
|
|
case Decl::Function:
|
|
|
|
Info.Kind = SymbolKind::Function;
|
|
|
|
break;
|
|
|
|
case Decl::ParmVar:
|
|
|
|
Info.Kind = SymbolKind::Variable;
|
|
|
|
break;
|
|
|
|
case Decl::Var:
|
|
|
|
Info.Kind = SymbolKind::Variable;
|
|
|
|
if (isa<CXXRecordDecl>(D->getDeclContext())) {
|
2016-03-26 01:01:59 +08:00
|
|
|
Info.Kind = SymbolKind::StaticProperty;
|
2016-02-13 07:10:59 +08:00
|
|
|
Info.Lang = SymbolLanguage::CXX;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case Decl::Field:
|
|
|
|
Info.Kind = SymbolKind::Field;
|
|
|
|
if (const CXXRecordDecl *
|
|
|
|
CXXRec = dyn_cast<CXXRecordDecl>(D->getDeclContext())) {
|
|
|
|
if (!CXXRec->isCLike())
|
|
|
|
Info.Lang = SymbolLanguage::CXX;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case Decl::EnumConstant:
|
|
|
|
Info.Kind = SymbolKind::EnumConstant; break;
|
|
|
|
case Decl::ObjCInterface:
|
|
|
|
case Decl::ObjCImplementation:
|
2016-03-26 01:01:59 +08:00
|
|
|
Info.Kind = SymbolKind::Class;
|
2016-02-13 07:10:59 +08:00
|
|
|
Info.Lang = SymbolLanguage::ObjC;
|
|
|
|
break;
|
|
|
|
case Decl::ObjCProtocol:
|
2016-03-26 01:01:59 +08:00
|
|
|
Info.Kind = SymbolKind::Protocol;
|
2016-02-13 07:10:59 +08:00
|
|
|
Info.Lang = SymbolLanguage::ObjC;
|
|
|
|
break;
|
|
|
|
case Decl::ObjCCategory:
|
|
|
|
case Decl::ObjCCategoryImpl:
|
2016-03-26 01:01:59 +08:00
|
|
|
Info.Kind = SymbolKind::Extension;
|
2016-02-13 07:10:59 +08:00
|
|
|
Info.Lang = SymbolLanguage::ObjC;
|
|
|
|
break;
|
|
|
|
case Decl::ObjCMethod:
|
|
|
|
if (cast<ObjCMethodDecl>(D)->isInstanceMethod())
|
2016-03-26 01:01:59 +08:00
|
|
|
Info.Kind = SymbolKind::InstanceMethod;
|
2016-02-13 07:10:59 +08:00
|
|
|
else
|
2016-03-26 01:01:59 +08:00
|
|
|
Info.Kind = SymbolKind::ClassMethod;
|
2016-02-13 07:10:59 +08:00
|
|
|
Info.Lang = SymbolLanguage::ObjC;
|
|
|
|
break;
|
|
|
|
case Decl::ObjCProperty:
|
2016-03-26 01:01:59 +08:00
|
|
|
Info.Kind = SymbolKind::InstanceProperty;
|
2016-02-13 07:10:59 +08:00
|
|
|
Info.Lang = SymbolLanguage::ObjC;
|
|
|
|
break;
|
|
|
|
case Decl::ObjCIvar:
|
2016-03-26 01:01:59 +08:00
|
|
|
Info.Kind = SymbolKind::Field;
|
2016-02-13 07:10:59 +08:00
|
|
|
Info.Lang = SymbolLanguage::ObjC;
|
|
|
|
break;
|
|
|
|
case Decl::Namespace:
|
2016-03-26 01:01:59 +08:00
|
|
|
Info.Kind = SymbolKind::Namespace;
|
2016-02-13 07:10:59 +08:00
|
|
|
Info.Lang = SymbolLanguage::CXX;
|
|
|
|
break;
|
|
|
|
case Decl::NamespaceAlias:
|
2016-03-26 01:01:59 +08:00
|
|
|
Info.Kind = SymbolKind::NamespaceAlias;
|
2016-02-13 07:10:59 +08:00
|
|
|
Info.Lang = SymbolLanguage::CXX;
|
|
|
|
break;
|
|
|
|
case Decl::CXXConstructor:
|
2016-03-26 01:01:59 +08:00
|
|
|
Info.Kind = SymbolKind::Constructor;
|
2016-02-13 07:10:59 +08:00
|
|
|
Info.Lang = SymbolLanguage::CXX;
|
|
|
|
break;
|
|
|
|
case Decl::CXXDestructor:
|
2016-03-26 01:01:59 +08:00
|
|
|
Info.Kind = SymbolKind::Destructor;
|
2016-02-13 07:10:59 +08:00
|
|
|
Info.Lang = SymbolLanguage::CXX;
|
|
|
|
break;
|
|
|
|
case Decl::CXXConversion:
|
2016-03-26 01:01:59 +08:00
|
|
|
Info.Kind = SymbolKind::ConversionFunction;
|
2016-02-13 07:10:59 +08:00
|
|
|
Info.Lang = SymbolLanguage::CXX;
|
|
|
|
break;
|
|
|
|
case Decl::CXXMethod: {
|
|
|
|
const CXXMethodDecl *MD = cast<CXXMethodDecl>(D);
|
|
|
|
if (MD->isStatic())
|
2016-03-26 01:01:59 +08:00
|
|
|
Info.Kind = SymbolKind::StaticMethod;
|
2016-02-13 07:10:59 +08:00
|
|
|
else
|
2016-03-26 01:01:59 +08:00
|
|
|
Info.Kind = SymbolKind::InstanceMethod;
|
2016-02-13 07:10:59 +08:00
|
|
|
Info.Lang = SymbolLanguage::CXX;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case Decl::ClassTemplate:
|
2016-03-26 01:01:59 +08:00
|
|
|
Info.Kind = SymbolKind::Class;
|
2016-02-13 07:10:59 +08:00
|
|
|
Info.TemplateKind = SymbolCXXTemplateKind::Template;
|
2016-03-26 01:01:59 +08:00
|
|
|
Info.Lang = SymbolLanguage::CXX;
|
2016-02-13 07:10:59 +08:00
|
|
|
break;
|
|
|
|
case Decl::FunctionTemplate:
|
|
|
|
Info.Kind = SymbolKind::Function;
|
|
|
|
Info.TemplateKind = SymbolCXXTemplateKind::Template;
|
2016-03-26 01:01:59 +08:00
|
|
|
Info.Lang = SymbolLanguage::CXX;
|
2016-02-13 07:10:59 +08:00
|
|
|
if (const CXXMethodDecl *MD = dyn_cast_or_null<CXXMethodDecl>(
|
|
|
|
cast<FunctionTemplateDecl>(D)->getTemplatedDecl())) {
|
|
|
|
if (isa<CXXConstructorDecl>(MD))
|
2016-03-26 01:01:59 +08:00
|
|
|
Info.Kind = SymbolKind::Constructor;
|
2016-02-13 07:10:59 +08:00
|
|
|
else if (isa<CXXDestructorDecl>(MD))
|
2016-03-26 01:01:59 +08:00
|
|
|
Info.Kind = SymbolKind::Destructor;
|
2016-02-13 07:10:59 +08:00
|
|
|
else if (isa<CXXConversionDecl>(MD))
|
2016-03-26 01:01:59 +08:00
|
|
|
Info.Kind = SymbolKind::ConversionFunction;
|
2016-02-13 07:10:59 +08:00
|
|
|
else {
|
|
|
|
if (MD->isStatic())
|
2016-03-26 01:01:59 +08:00
|
|
|
Info.Kind = SymbolKind::StaticMethod;
|
2016-02-13 07:10:59 +08:00
|
|
|
else
|
2016-03-26 01:01:59 +08:00
|
|
|
Info.Kind = SymbolKind::InstanceMethod;
|
2016-02-13 07:10:59 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case Decl::TypeAliasTemplate:
|
2016-03-26 01:01:59 +08:00
|
|
|
Info.Kind = SymbolKind::TypeAlias;
|
|
|
|
Info.Lang = SymbolLanguage::CXX;
|
2016-02-13 07:10:59 +08:00
|
|
|
Info.TemplateKind = SymbolCXXTemplateKind::Template;
|
|
|
|
break;
|
|
|
|
case Decl::TypeAlias:
|
2016-03-26 01:01:59 +08:00
|
|
|
Info.Kind = SymbolKind::TypeAlias;
|
2016-02-13 07:10:59 +08:00
|
|
|
Info.Lang = SymbolLanguage::CXX;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (Info.Kind == SymbolKind::Unknown)
|
|
|
|
return Info;
|
|
|
|
|
|
|
|
if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
|
|
|
|
if (FD->getTemplatedKind() ==
|
|
|
|
FunctionDecl::TK_FunctionTemplateSpecialization)
|
|
|
|
Info.TemplateKind = SymbolCXXTemplateKind::TemplateSpecialization;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (Info.TemplateKind != SymbolCXXTemplateKind::NonTemplate)
|
|
|
|
Info.Lang = SymbolLanguage::CXX;
|
|
|
|
|
|
|
|
return Info;
|
|
|
|
}
|
2016-02-14 14:39:11 +08:00
|
|
|
|
|
|
|
void index::applyForEachSymbolRole(SymbolRoleSet Roles,
|
|
|
|
llvm::function_ref<void(SymbolRole)> Fn) {
|
|
|
|
#define APPLY_FOR_ROLE(Role) \
|
|
|
|
if (Roles & (unsigned)SymbolRole::Role) \
|
|
|
|
Fn(SymbolRole::Role)
|
|
|
|
|
|
|
|
APPLY_FOR_ROLE(Declaration);
|
|
|
|
APPLY_FOR_ROLE(Definition);
|
|
|
|
APPLY_FOR_ROLE(Reference);
|
|
|
|
APPLY_FOR_ROLE(Read);
|
|
|
|
APPLY_FOR_ROLE(Write);
|
|
|
|
APPLY_FOR_ROLE(Call);
|
|
|
|
APPLY_FOR_ROLE(Dynamic);
|
|
|
|
APPLY_FOR_ROLE(AddressOf);
|
|
|
|
APPLY_FOR_ROLE(Implicit);
|
|
|
|
APPLY_FOR_ROLE(RelationChildOf);
|
|
|
|
APPLY_FOR_ROLE(RelationBaseOf);
|
|
|
|
APPLY_FOR_ROLE(RelationOverrideOf);
|
|
|
|
APPLY_FOR_ROLE(RelationReceivedBy);
|
2016-02-29 15:56:00 +08:00
|
|
|
APPLY_FOR_ROLE(RelationCalledBy);
|
2016-02-14 14:39:11 +08:00
|
|
|
|
|
|
|
#undef APPLY_FOR_ROLE
|
|
|
|
}
|
|
|
|
|
|
|
|
void index::printSymbolRoles(SymbolRoleSet Roles, raw_ostream &OS) {
|
|
|
|
bool VisitedOnce = false;
|
|
|
|
applyForEachSymbolRole(Roles, [&](SymbolRole Role) {
|
|
|
|
if (VisitedOnce)
|
2016-02-29 15:55:51 +08:00
|
|
|
OS << ',';
|
2016-02-14 14:39:11 +08:00
|
|
|
else
|
|
|
|
VisitedOnce = true;
|
|
|
|
switch (Role) {
|
|
|
|
case SymbolRole::Declaration: OS << "Decl"; break;
|
|
|
|
case SymbolRole::Definition: OS << "Def"; break;
|
|
|
|
case SymbolRole::Reference: OS << "Ref"; break;
|
|
|
|
case SymbolRole::Read: OS << "Read"; break;
|
|
|
|
case SymbolRole::Write: OS << "Writ"; break;
|
|
|
|
case SymbolRole::Call: OS << "Call"; break;
|
|
|
|
case SymbolRole::Dynamic: OS << "Dyn"; break;
|
|
|
|
case SymbolRole::AddressOf: OS << "Addr"; break;
|
|
|
|
case SymbolRole::Implicit: OS << "Impl"; break;
|
|
|
|
case SymbolRole::RelationChildOf: OS << "RelChild"; break;
|
|
|
|
case SymbolRole::RelationBaseOf: OS << "RelBase"; break;
|
|
|
|
case SymbolRole::RelationOverrideOf: OS << "RelOver"; break;
|
|
|
|
case SymbolRole::RelationReceivedBy: OS << "RelRec"; break;
|
2016-02-29 15:56:00 +08:00
|
|
|
case SymbolRole::RelationCalledBy: OS << "RelCall"; break;
|
2016-02-14 14:39:11 +08:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2016-02-15 09:32:36 +08:00
|
|
|
bool index::printSymbolName(const Decl *D, const LangOptions &LO,
|
|
|
|
raw_ostream &OS) {
|
|
|
|
if (auto *ND = dyn_cast<NamedDecl>(D)) {
|
|
|
|
PrintingPolicy Policy(LO);
|
|
|
|
// Forward references can have different template argument names. Suppress
|
|
|
|
// the template argument names in constructors to make their name more
|
|
|
|
// stable.
|
|
|
|
Policy.SuppressTemplateArgsInCXXConstructors = true;
|
|
|
|
DeclarationName DeclName = ND->getDeclName();
|
|
|
|
if (DeclName.isEmpty())
|
|
|
|
return true;
|
|
|
|
DeclName.print(OS, Policy);
|
|
|
|
return false;
|
|
|
|
} else {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-02-14 14:39:11 +08:00
|
|
|
StringRef index::getSymbolKindString(SymbolKind K) {
|
|
|
|
switch (K) {
|
|
|
|
case SymbolKind::Unknown: return "<unknown>";
|
|
|
|
case SymbolKind::Module: return "module";
|
2016-03-26 01:01:59 +08:00
|
|
|
case SymbolKind::Namespace: return "namespace";
|
|
|
|
case SymbolKind::NamespaceAlias: return "namespace-alias";
|
2016-02-14 14:39:11 +08:00
|
|
|
case SymbolKind::Macro: return "macro";
|
|
|
|
case SymbolKind::Enum: return "enum";
|
|
|
|
case SymbolKind::Struct: return "struct";
|
2016-03-26 01:01:59 +08:00
|
|
|
case SymbolKind::Class: return "class";
|
|
|
|
case SymbolKind::Protocol: return "protocol";
|
|
|
|
case SymbolKind::Extension: return "extension";
|
2016-02-14 14:39:11 +08:00
|
|
|
case SymbolKind::Union: return "union";
|
2016-03-26 01:01:59 +08:00
|
|
|
case SymbolKind::TypeAlias: return "type-alias";
|
2016-02-14 14:39:11 +08:00
|
|
|
case SymbolKind::Function: return "function";
|
|
|
|
case SymbolKind::Variable: return "variable";
|
|
|
|
case SymbolKind::Field: return "field";
|
|
|
|
case SymbolKind::EnumConstant: return "enumerator";
|
2016-03-26 01:01:59 +08:00
|
|
|
case SymbolKind::InstanceMethod: return "instance-method";
|
|
|
|
case SymbolKind::ClassMethod: return "class-method";
|
|
|
|
case SymbolKind::StaticMethod: return "static-method";
|
|
|
|
case SymbolKind::InstanceProperty: return "instance-property";
|
|
|
|
case SymbolKind::ClassProperty: return "class-property";
|
|
|
|
case SymbolKind::StaticProperty: return "static-property";
|
|
|
|
case SymbolKind::Constructor: return "constructor";
|
|
|
|
case SymbolKind::Destructor: return "destructor";
|
|
|
|
case SymbolKind::ConversionFunction: return "coversion-func";
|
2016-02-14 14:39:11 +08:00
|
|
|
}
|
2016-02-15 08:36:52 +08:00
|
|
|
llvm_unreachable("invalid symbol kind");
|
2016-02-14 14:39:11 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
StringRef index::getTemplateKindStr(SymbolCXXTemplateKind TK) {
|
|
|
|
switch (TK) {
|
|
|
|
case SymbolCXXTemplateKind::NonTemplate: return "NT";
|
|
|
|
case SymbolCXXTemplateKind::Template : return "T";
|
|
|
|
case SymbolCXXTemplateKind::TemplatePartialSpecialization : return "TPS";
|
|
|
|
case SymbolCXXTemplateKind::TemplateSpecialization: return "TS";
|
|
|
|
}
|
2016-02-15 08:36:52 +08:00
|
|
|
llvm_unreachable("invalid template kind");
|
2016-02-14 14:39:11 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
StringRef index::getSymbolLanguageString(SymbolLanguage K) {
|
|
|
|
switch (K) {
|
|
|
|
case SymbolLanguage::C: return "C";
|
|
|
|
case SymbolLanguage::ObjC: return "ObjC";
|
|
|
|
case SymbolLanguage::CXX: return "C++";
|
|
|
|
}
|
2016-02-15 08:36:52 +08:00
|
|
|
llvm_unreachable("invalid symbol language kind");
|
2016-02-14 14:39:11 +08:00
|
|
|
}
|