From 048f1dc7a5e21b4a959ae54ace7901524a6afd0c Mon Sep 17 00:00:00 2001 From: Fangrui Song Date: Fri, 22 Feb 2019 10:59:05 +0800 Subject: [PATCH] indexer: index TemplateTypeParmDecl and ParmVarDecl in declarations for clang >= 9 Index ParmVarDecl in declarations if index.parametersInDeclarations is true And support some unhandled Decl::Kind --- src/config.hh | 7 ++++-- src/indexer.cc | 31 ++++++++++++++++++++----- src/messages/textDocument_completion.cc | 3 +++ 3 files changed, 33 insertions(+), 8 deletions(-) diff --git a/src/config.hh b/src/config.hh index 33f97fd1..9ce0cc1b 100644 --- a/src/config.hh +++ b/src/config.hh @@ -278,6 +278,9 @@ struct Config { // May be too slow for big projects, so it is off by default. bool onChange = false; + // If true, index parameters in declarations. + bool parametersInDeclarations = true; + // Number of indexer threads. If 0, 80% of cores are used. int threads = 0; @@ -338,8 +341,8 @@ REFLECT_STRUCT(Config::Diagnostics, blacklist, onChange, onOpen, onSave, REFLECT_STRUCT(Config::Highlight, largeFileSize, lsRanges, blacklist, whitelist) REFLECT_STRUCT(Config::Index, blacklist, comments, initialBlacklist, initialWhitelist, maxInitializerLines, multiVersion, - multiVersionBlacklist, multiVersionWhitelist, onChange, threads, - trackDependency, whitelist); + multiVersionBlacklist, multiVersionWhitelist, onChange, + parametersInDeclarations, threads, trackDependency, whitelist); REFLECT_STRUCT(Config::Request, timeout); REFLECT_STRUCT(Config::Session, maxNum); REFLECT_STRUCT(Config::WorkspaceSymbol, caseSensitivity, maxNum, sort); diff --git a/src/indexer.cc b/src/indexer.cc index 03bcc9b1..59ad91dc 100644 --- a/src/indexer.cc +++ b/src/indexer.cc @@ -171,6 +171,9 @@ Kind GetKind(const Decl *D, SymbolKind &kind) { case Decl::UnresolvedUsingTypename: kind = SymbolKind::TypeAlias; return Kind::Type; + case Decl::Using: + kind = SymbolKind::Null; // ignored + return Kind::Invalid; case Decl::Binding: kind = SymbolKind::Variable; return Kind::Var; @@ -193,6 +196,10 @@ Kind GetKind(const Decl *D, SymbolKind &kind) { case Decl::CXXDestructor: kind = SymbolKind::Method; return Kind::Func; + case Decl::NonTypeTemplateParm: + // ccls extension + kind = SymbolKind::Parameter; + return Kind::Var; case Decl::Var: case Decl::Decomposition: kind = SymbolKind::Variable; @@ -216,7 +223,6 @@ Kind GetKind(const Decl *D, SymbolKind &kind) { return Kind::Invalid; default: - LOG_S(INFO) << "unhandled " << int(D->getKind()); return Kind::Invalid; } } @@ -736,7 +742,7 @@ public: IndexFunc *func = nullptr; IndexType *type = nullptr; IndexVar *var = nullptr; - SymbolKind ls_kind; + SymbolKind ls_kind = SymbolKind::Unknown; Kind kind = GetKind(D, ls_kind); if (is_def) @@ -785,8 +791,10 @@ public: }; switch (kind) { case Kind::Invalid: - LOG_S(INFO) << "Unhandled " << int(D->getKind()) << " " << info->qualified - << " in " << db->path << ":" << loc.start.line + 1; + if (ls_kind == SymbolKind::Unknown) + LOG_S(INFO) << "Unhandled " << int(D->getKind()) << " " + << info->qualified << " in " << db->path << ":" + << (loc.start.line + 1) << ":" << (loc.start.column + 1); return true; case Kind::File: return true; @@ -820,8 +828,12 @@ public: do_def_decl(type); if (Spell != Loc) AddMacroUse(db, SM, usr, Kind::Type, Spell); - if (type->def.detailed_name[0] == '\0' && info->short_name.size()) - SetName(D, info->short_name, info->qualified, type->def); + if (type->def.detailed_name[0] == '\0' && info->short_name.size()) { + if (D->getKind() == Decl::TemplateTypeParm) + type->def.detailed_name = Intern(info->short_name); + else + SetName(OrigD, info->short_name, info->qualified, type->def); + } if (is_def || is_decl) { const Decl *DC = cast(SemDC); if (GetKind(DC, ls_kind) == Kind::Type) @@ -853,6 +865,7 @@ public: if (!isa(D)) db->ToType(usr1).instances.push_back(usr); } else if (const Decl *D1 = GetAdjustedDecl(GetTypeDecl(T))) { +#if LLVM_VERSION_MAJOR < 9 if (isa(D1)) { // e.g. TemplateTypeParmDecl is not handled by // handleDeclOccurence. @@ -875,6 +888,7 @@ public: break; } } +#endif IndexParam::DeclInfo *info1; Usr usr1 = GetUsr(D1, &info1); @@ -1250,6 +1264,11 @@ Index(SemaManager *manager, WorkingFiles *wfiles, VFS *vfs, index::IndexingOptions::SystemSymbolFilterKind::All; IndexOpts.IndexFunctionLocals = true; IndexOpts.IndexImplicitInstantiation = true; +#if LLVM_VERSION_MAJOR >= 9 + IndexOpts.IndexParametersInDeclarations = + g_config->index.parametersInDeclarations; + IndexOpts.IndexTemplateParameters = true; +#endif std::unique_ptr Action = createIndexingAction( DataConsumer, IndexOpts, std::make_unique(param)); diff --git a/src/messages/textDocument_completion.cc b/src/messages/textDocument_completion.cc index 714a63a6..8e0a4772 100644 --- a/src/messages/textDocument_completion.cc +++ b/src/messages/textDocument_completion.cc @@ -272,6 +272,9 @@ CompletionItemKind GetCompletionKind(CodeCompletionContext::Kind K, case Decl::TypeAlias: case Decl::Typedef: return CompletionItemKind::TypeParameter; + case Decl::Using: + case Decl::ConstructorUsingShadow: + return CompletionItemKind::Keyword; case Decl::Binding: return CompletionItemKind::Variable; case Decl::Field: