From a0cb91f335b5f60bc7b0e7e193c34e4d664368f0 Mon Sep 17 00:00:00 2001 From: Argyrios Kyrtzidis Date: Fri, 22 Apr 2016 07:21:04 +0000 Subject: [PATCH] [index] Change SymbolCXXTemplateKind to a 'SymbolSubKinds' bitset. This provides a more general and flexible way to annotate special symbols. git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@267116 91177308-0d34-0410-b5e6-96231b3b80d8 --- include/clang/Index/IndexSymbol.h | 18 +++++--- lib/Index/IndexSymbol.cpp | 60 ++++++++++++++++++-------- tools/c-index-test/core_main.cpp | 5 ++- tools/libclang/CXIndexDataConsumer.cpp | 19 ++++---- 4 files changed, 63 insertions(+), 39 deletions(-) diff --git a/include/clang/Index/IndexSymbol.h b/include/clang/Index/IndexSymbol.h index 56b12fa6be..68d97d75ac 100644 --- a/include/clang/Index/IndexSymbol.h +++ b/include/clang/Index/IndexSymbol.h @@ -59,12 +59,13 @@ enum class SymbolLanguage { CXX, }; -enum class SymbolCXXTemplateKind { - NonTemplate, - Template, - TemplatePartialSpecialization, - TemplateSpecialization, +enum class SymbolSubKind : uint8_t { + Generic = 1 << 0, + TemplatePartialSpecialization = 1 << 1, + TemplateSpecialization = 1 << 2, }; +static const unsigned SymbolSubKindBitNum = 3; +typedef unsigned SymbolSubKindSet; /// Set of roles that are attributed to symbol occurrences. enum class SymbolRole : uint16_t { @@ -99,7 +100,7 @@ struct SymbolRelation { struct SymbolInfo { SymbolKind Kind; - SymbolCXXTemplateKind TemplateKind; + SymbolSubKindSet SubKinds; SymbolLanguage Lang; }; @@ -113,9 +114,12 @@ void printSymbolRoles(SymbolRoleSet Roles, raw_ostream &OS); bool printSymbolName(const Decl *D, const LangOptions &LO, raw_ostream &OS); StringRef getSymbolKindString(SymbolKind K); -StringRef getTemplateKindStr(SymbolCXXTemplateKind TK); StringRef getSymbolLanguageString(SymbolLanguage K); +void applyForEachSymbolSubKind(SymbolSubKindSet SubKinds, + llvm::function_ref Fn); +void printSymbolSubKinds(SymbolSubKindSet SubKinds, raw_ostream &OS); + } // namespace index } // namespace clang diff --git a/lib/Index/IndexSymbol.cpp b/lib/Index/IndexSymbol.cpp index 9f91e6274d..097aaf0721 100644 --- a/lib/Index/IndexSymbol.cpp +++ b/lib/Index/IndexSymbol.cpp @@ -20,7 +20,7 @@ SymbolInfo index::getSymbolInfo(const Decl *D) { assert(D); SymbolInfo Info; Info.Kind = SymbolKind::Unknown; - Info.TemplateKind = SymbolCXXTemplateKind::NonTemplate; + Info.SubKinds = SymbolSubKindSet(); Info.Lang = SymbolLanguage::C; if (const TagDecl *TD = dyn_cast(D)) { @@ -46,9 +46,11 @@ SymbolInfo index::getSymbolInfo(const Decl *D) { Info.Lang = SymbolLanguage::CXX; if (isa(D)) { - Info.TemplateKind = SymbolCXXTemplateKind::TemplatePartialSpecialization; + Info.SubKinds |= (unsigned)SymbolSubKind::Generic; + Info.SubKinds |= (unsigned)SymbolSubKind::TemplatePartialSpecialization; } else if (isa(D)) { - Info.TemplateKind = SymbolCXXTemplateKind::TemplateSpecialization; + Info.SubKinds |= (unsigned)SymbolSubKind::Generic; + Info.SubKinds |= (unsigned)SymbolSubKind::TemplateSpecialization; } } else { @@ -141,12 +143,12 @@ SymbolInfo index::getSymbolInfo(const Decl *D) { } case Decl::ClassTemplate: Info.Kind = SymbolKind::Class; - Info.TemplateKind = SymbolCXXTemplateKind::Template; + Info.SubKinds |= (unsigned)SymbolSubKind::Generic; Info.Lang = SymbolLanguage::CXX; break; case Decl::FunctionTemplate: Info.Kind = SymbolKind::Function; - Info.TemplateKind = SymbolCXXTemplateKind::Template; + Info.SubKinds |= (unsigned)SymbolSubKind::Generic; Info.Lang = SymbolLanguage::CXX; if (const CXXMethodDecl *MD = dyn_cast_or_null( cast(D)->getTemplatedDecl())) { @@ -167,7 +169,7 @@ SymbolInfo index::getSymbolInfo(const Decl *D) { case Decl::TypeAliasTemplate: Info.Kind = SymbolKind::TypeAlias; Info.Lang = SymbolLanguage::CXX; - Info.TemplateKind = SymbolCXXTemplateKind::Template; + Info.SubKinds |= (unsigned)SymbolSubKind::Generic; break; case Decl::TypeAlias: Info.Kind = SymbolKind::TypeAlias; @@ -183,11 +185,13 @@ SymbolInfo index::getSymbolInfo(const Decl *D) { if (const FunctionDecl *FD = dyn_cast(D)) { if (FD->getTemplatedKind() == - FunctionDecl::TK_FunctionTemplateSpecialization) - Info.TemplateKind = SymbolCXXTemplateKind::TemplateSpecialization; + FunctionDecl::TK_FunctionTemplateSpecialization) { + Info.SubKinds |= (unsigned)SymbolSubKind::Generic; + Info.SubKinds |= (unsigned)SymbolSubKind::TemplateSpecialization; + } } - if (Info.TemplateKind != SymbolCXXTemplateKind::NonTemplate) + if (Info.SubKinds & (unsigned)SymbolSubKind::Generic) Info.Lang = SymbolLanguage::CXX; return Info; @@ -292,16 +296,6 @@ StringRef index::getSymbolKindString(SymbolKind K) { llvm_unreachable("invalid symbol kind"); } -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"; - } - llvm_unreachable("invalid template kind"); -} - StringRef index::getSymbolLanguageString(SymbolLanguage K) { switch (K) { case SymbolLanguage::C: return "C"; @@ -310,3 +304,31 @@ StringRef index::getSymbolLanguageString(SymbolLanguage K) { } llvm_unreachable("invalid symbol language kind"); } + +void index::applyForEachSymbolSubKind(SymbolSubKindSet SubKinds, + llvm::function_ref Fn) { +#define APPLY_FOR_SUBKIND(K) \ + if (SubKinds & (unsigned)SymbolSubKind::K) \ + Fn(SymbolSubKind::K) + + APPLY_FOR_SUBKIND(Generic); + APPLY_FOR_SUBKIND(TemplatePartialSpecialization); + APPLY_FOR_SUBKIND(TemplateSpecialization); + +#undef APPLY_FOR_SUBKIND +} + +void index::printSymbolSubKinds(SymbolSubKindSet SubKinds, raw_ostream &OS) { + bool VisitedOnce = false; + applyForEachSymbolSubKind(SubKinds, [&](SymbolSubKind SubKind) { + if (VisitedOnce) + OS << ','; + else + VisitedOnce = true; + switch (SubKind) { + case SymbolSubKind::Generic: OS << "Gen"; break; + case SymbolSubKind::TemplatePartialSpecialization: OS << "TPS"; break; + case SymbolSubKind::TemplateSpecialization: OS << "TS"; break; + } + }); +} diff --git a/tools/c-index-test/core_main.cpp b/tools/c-index-test/core_main.cpp index 1881e31e20..b19c8cbba7 100644 --- a/tools/c-index-test/core_main.cpp +++ b/tools/c-index-test/core_main.cpp @@ -168,8 +168,9 @@ static bool printSourceSymbols(ArrayRef Args) { static void printSymbolInfo(SymbolInfo SymInfo, raw_ostream &OS) { OS << getSymbolKindString(SymInfo.Kind); - if (SymInfo.TemplateKind != SymbolCXXTemplateKind::NonTemplate) { - OS << '-' << getTemplateKindStr(SymInfo.TemplateKind); + if (SymInfo.SubKinds) { + OS << '-'; + printSymbolSubKinds(SymInfo.SubKinds, OS); } OS << '/' << getSymbolLanguageString(SymInfo.Lang); } diff --git a/tools/libclang/CXIndexDataConsumer.cpp b/tools/libclang/CXIndexDataConsumer.cpp index 3b556d441c..59fa92bb21 100644 --- a/tools/libclang/CXIndexDataConsumer.cpp +++ b/tools/libclang/CXIndexDataConsumer.cpp @@ -1134,7 +1134,7 @@ void CXIndexDataConsumer::translateLoc(SourceLocation Loc, static CXIdxEntityKind getEntityKindFromSymbolKind(SymbolKind K, SymbolLanguage L); static CXIdxEntityCXXTemplateKind -getEntityKindFromSymbolCXXTemplateKind(SymbolCXXTemplateKind K); +getEntityKindFromSymbolSubKinds(SymbolSubKindSet K); static CXIdxEntityLanguage getEntityLangFromSymbolLang(SymbolLanguage L); void CXIndexDataConsumer::getEntityInfo(const NamedDecl *D, @@ -1150,8 +1150,7 @@ void CXIndexDataConsumer::getEntityInfo(const NamedDecl *D, SymbolInfo SymInfo = getSymbolInfo(D); EntityInfo.kind = getEntityKindFromSymbolKind(SymInfo.Kind, SymInfo.Lang); - EntityInfo.templateKind = - getEntityKindFromSymbolCXXTemplateKind(SymInfo.TemplateKind); + EntityInfo.templateKind = getEntityKindFromSymbolSubKinds(SymInfo.SubKinds); EntityInfo.lang = getEntityLangFromSymbolLang(SymInfo.Lang); if (D->hasAttrs()) { @@ -1291,16 +1290,14 @@ static CXIdxEntityKind getEntityKindFromSymbolKind(SymbolKind K, SymbolLanguage } static CXIdxEntityCXXTemplateKind -getEntityKindFromSymbolCXXTemplateKind(SymbolCXXTemplateKind K) { - switch (K) { - case SymbolCXXTemplateKind::NonTemplate: return CXIdxEntity_NonTemplate; - case SymbolCXXTemplateKind::Template: return CXIdxEntity_Template; - case SymbolCXXTemplateKind::TemplatePartialSpecialization: +getEntityKindFromSymbolSubKinds(SymbolSubKindSet K) { + if (K & (unsigned)SymbolSubKind::TemplatePartialSpecialization) return CXIdxEntity_TemplatePartialSpecialization; - case SymbolCXXTemplateKind::TemplateSpecialization: + if (K & (unsigned)SymbolSubKind::TemplateSpecialization) return CXIdxEntity_TemplateSpecialization; - } - llvm_unreachable("invalid template kind"); + if (K & (unsigned)SymbolSubKind::Generic) + return CXIdxEntity_Template; + return CXIdxEntity_NonTemplate; } static CXIdxEntityLanguage getEntityLangFromSymbolLang(SymbolLanguage L) { -- 2.40.0