From 31300c7d8b7ccfa17e6bc601f1f09c58d73dbc7e Mon Sep 17 00:00:00 2001 From: Eric Liu Date: Wed, 17 Jan 2018 18:27:41 +0000 Subject: [PATCH] [clangd] Deduplicate symbols collected in global-symbol-builder tool. Summary: After D42111, the executor framework no longer deduplicate tool results. Reviewers: bkramer, sammccall Subscribers: klimek, ilya-biryukov, cfe-commits Differential Revision: https://reviews.llvm.org/D42113 llvm-svn: 322722 --- .../global-symbol-builder/GlobalSymbolBuilderMain.cpp | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/clang-tools-extra/clangd/global-symbol-builder/GlobalSymbolBuilderMain.cpp b/clang-tools-extra/clangd/global-symbol-builder/GlobalSymbolBuilderMain.cpp index 42751cc7..9dd551d 100644 --- a/clang-tools-extra/clangd/global-symbol-builder/GlobalSymbolBuilderMain.cpp +++ b/clang-tools-extra/clangd/global-symbol-builder/GlobalSymbolBuilderMain.cpp @@ -105,7 +105,22 @@ int main(int argc, const char **argv) { llvm::errs() << llvm::toString(std::move(Err)) << "\n"; } + // Deduplicate the result by key and keep the longest value. + // FIXME(ioeric): Merge occurrences, rather than just dropping all but one. + // Definitions and forward declarations have the same key and may both have + // information. Usage count will need to be aggregated across occurrences, + // too. + llvm::StringMap UniqueSymbols; Executor->get()->getToolResults()->forEachResult( - [](llvm::StringRef, llvm::StringRef Value) { llvm::outs() << Value; }); + [&UniqueSymbols](llvm::StringRef Key, llvm::StringRef Value) { + auto Ret = UniqueSymbols.try_emplace(Key, Value); + if (!Ret.second) { + // If key already exists, keep the longest value. + llvm::StringRef &V = Ret.first->second; + V = V.size() < Value.size() ? Value : V; + } + }); + for (const auto &Sym : UniqueSymbols) + llvm::outs() << Sym.second; return 0; } -- 2.7.4