[clang-tools-extra] r321358 - [clangd] Add a tool to build YAML-format global symbols.

Haojian Wu via cfe-commits cfe-commits at lists.llvm.org
Fri Dec 22 06:38:06 PST 2017


Author: hokein
Date: Fri Dec 22 06:38:05 2017
New Revision: 321358

URL: http://llvm.org/viewvc/llvm-project?rev=321358&view=rev
Log:
[clangd] Add a tool to build YAML-format global symbols.

Summary:
The tools is used to generate global symbols for clangd (global code completion),
The format is YAML, which is only for **experiment**.

Usage:
./bin/global-symbol-builder </path/to/llvm-dir> > global-symbols.yaml

TEST:
used the tool to generate global symbols for LLVM (~72MB).

Reviewers: sammccall

Reviewed By: sammccall

Subscribers: klimek, mgorny, ilya-biryukov, cfe-commits

Differential Revision: https://reviews.llvm.org/D41491

Added:
    clang-tools-extra/trunk/clangd/global-symbol-builder/
    clang-tools-extra/trunk/clangd/global-symbol-builder/CMakeLists.txt
    clang-tools-extra/trunk/clangd/global-symbol-builder/GlobalSymbolBuilderMain.cpp
Modified:
    clang-tools-extra/trunk/clangd/CMakeLists.txt
    clang-tools-extra/trunk/test/CMakeLists.txt

Modified: clang-tools-extra/trunk/clangd/CMakeLists.txt
URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/clangd/CMakeLists.txt?rev=321358&r1=321357&r2=321358&view=diff
==============================================================================
--- clang-tools-extra/trunk/clangd/CMakeLists.txt (original)
+++ clang-tools-extra/trunk/clangd/CMakeLists.txt Fri Dec 22 06:38:05 2017
@@ -47,3 +47,4 @@ if( LLVM_LIB_FUZZING_ENGINE OR LLVM_USE_
   add_subdirectory(fuzzer)
 endif()
 add_subdirectory(tool)
+add_subdirectory(global-symbol-builder)

Added: clang-tools-extra/trunk/clangd/global-symbol-builder/CMakeLists.txt
URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/clangd/global-symbol-builder/CMakeLists.txt?rev=321358&view=auto
==============================================================================
--- clang-tools-extra/trunk/clangd/global-symbol-builder/CMakeLists.txt (added)
+++ clang-tools-extra/trunk/clangd/global-symbol-builder/CMakeLists.txt Fri Dec 22 06:38:05 2017
@@ -0,0 +1,19 @@
+include_directories(${CMAKE_CURRENT_SOURCE_DIR}/../)
+
+set(LLVM_LINK_COMPONENTS
+    Support
+    )
+
+add_clang_executable(global-symbol-builder
+  GlobalSymbolBuilderMain.cpp
+  )
+
+target_link_libraries(global-symbol-builder
+  PRIVATE
+  clangAST
+  clangIndex
+  clangDaemon
+  clangBasic
+  clangFrontend
+  clangTooling
+)

Added: clang-tools-extra/trunk/clangd/global-symbol-builder/GlobalSymbolBuilderMain.cpp
URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/clangd/global-symbol-builder/GlobalSymbolBuilderMain.cpp?rev=321358&view=auto
==============================================================================
--- clang-tools-extra/trunk/clangd/global-symbol-builder/GlobalSymbolBuilderMain.cpp (added)
+++ clang-tools-extra/trunk/clangd/global-symbol-builder/GlobalSymbolBuilderMain.cpp Fri Dec 22 06:38:05 2017
@@ -0,0 +1,110 @@
+//===--- GlobalSymbolBuilderMain.cpp -----------------------------*- C++-*-===//
+//
+//                     The LLVM Compiler Infrastructure
+//
+// This file is distributed under the University of Illinois Open Source
+// License. See LICENSE.TXT for details.
+//
+//===----------------------------------------------------------------------===//
+//
+// GlobalSymbolBuilder is a tool to generate YAML-format symbols across the
+// whole project. This tools is for **experimental** only. Don't use it in
+// production code.
+//
+//===---------------------------------------------------------------------===//
+
+#include "index/Index.h"
+#include "index/SymbolCollector.h"
+#include "index/SymbolYAML.h"
+#include "clang/Frontend/FrontendActions.h"
+#include "clang/Index/IndexingAction.h"
+#include "clang/Index/IndexDataConsumer.h"
+#include "clang/Tooling/CommonOptionsParser.h"
+#include "clang/Tooling/Tooling.h"
+#include "clang/Tooling/CommonOptionsParser.h"
+#include "llvm/Support/MemoryBuffer.h"
+#include "llvm/Support/Signals.h"
+#include "llvm/Support/Path.h"
+#include "llvm/Support/CommandLine.h"
+#include "llvm/Support/ThreadPool.h"
+
+using namespace llvm;
+using namespace clang::tooling;
+using clang::clangd::SymbolSlab;
+
+namespace clang {
+namespace clangd {
+
+class SymbolIndexActionFactory : public tooling::FrontendActionFactory {
+public:
+  SymbolIndexActionFactory() = default;
+
+  clang::FrontendAction *create() override {
+    index::IndexingOptions IndexOpts;
+    IndexOpts.SystemSymbolFilter =
+        index::IndexingOptions::SystemSymbolFilterKind::All;
+    IndexOpts.IndexFunctionLocals = false;
+    Collector = std::make_shared<SymbolCollector>();
+    return index::createIndexingAction(Collector, IndexOpts, nullptr).release();
+  }
+
+  std::shared_ptr<SymbolCollector> Collector;
+};
+
+} // namespace clangd
+} // namespace clang
+
+int main(int argc, const char **argv) {
+  llvm::sys::PrintStackTraceOnErrorSignal(argv[0]);
+
+  const char* Overview =
+      "This is an **experimental** tool to generate YAML-format "
+      "project-wide symbols for clangd (global code completion). It would be "
+      "changed and deprecated eventually. Don't use it in production code!";
+  CommonOptionsParser OptionsParser(argc, argv, cl::GeneralCategory,
+                                    /*Overview=*/Overview);
+
+  // No compilation database found, fallback to single TU analysis, this is
+  // mainly for debugging purpose:
+  //   global-symbol-buidler /tmp/t.cc -- -std=c++11.
+  if (OptionsParser.getCompilations().getAllFiles().empty()) {
+    llvm::errs() << "No compilation database found, processing individual "
+                    "files with flags from command-line\n.";
+    ClangTool Tool(OptionsParser.getCompilations(),
+                   OptionsParser.getSourcePathList());
+    clang::clangd::SymbolIndexActionFactory IndexAction;
+    Tool.run(&IndexAction);
+    llvm::outs() << SymbolToYAML(IndexAction.Collector->takeSymbols());
+    return 0;
+  }
+
+  // Found compilation database, we iterate all TUs from database to get all
+  // symbols, and then merge them into a single SymbolSlab.
+  SymbolSlab GlobalSymbols;
+  std::mutex SymbolMutex;
+  auto AddSymbols = [&](const SymbolSlab& NewSymbols) {
+    // Synchronize set accesses.
+    std::unique_lock<std::mutex> LockGuard(SymbolMutex);
+    for (auto It : NewSymbols) {
+      // FIXME: Better handling the overlap symbols, currently we overwrite it
+      // with the latest one, but we always want to good declarations (class
+      // definitions, instead of forward declarations).
+      GlobalSymbols.insert(It.second);
+    }
+  };
+
+  {
+    llvm::ThreadPool Pool;
+    for (auto& file : OptionsParser.getCompilations().getAllFiles()) {
+      Pool.async([&OptionsParser, &AddSymbols](llvm::StringRef Path) {
+        ClangTool Tool(OptionsParser.getCompilations(), {Path});
+        clang::clangd::SymbolIndexActionFactory IndexAction;
+        Tool.run(&IndexAction);
+        AddSymbols(IndexAction.Collector->takeSymbols());
+      }, file);
+    }
+  }
+
+  llvm::outs() << SymbolToYAML(GlobalSymbols);
+  return 0;
+}

Modified: clang-tools-extra/trunk/test/CMakeLists.txt
URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/test/CMakeLists.txt?rev=321358&r1=321357&r2=321358&view=diff
==============================================================================
--- clang-tools-extra/trunk/test/CMakeLists.txt (original)
+++ clang-tools-extra/trunk/test/CMakeLists.txt Fri Dec 22 06:38:05 2017
@@ -49,6 +49,10 @@ set(CLANG_TOOLS_TEST_DEPS
   modularize
   pp-trace
 
+  # These individual tools have no tests, add them here to make them compile
+  # together with check-clang-tools, so that we won't break them in the future.
+  global-symbol-builder
+
   # Unit tests
   ExtraToolsUnitTests
   )




More information about the cfe-commits mailing list