Changeset View
Standalone View
clang-tools-extra/clangd/background-indexer/BackgroundIndexer.cpp
- This file was added.
//===--- BackgroundIndexer.cpp -----------------------------------*- C++-*-===// | |||||
// | |||||
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. | |||||
// See https://llvm.org/LICENSE.txt for license information. | |||||
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception | |||||
// | |||||
//===----------------------------------------------------------------------===// | |||||
// | |||||
// A standalone binary to run clangd's background indexer. | |||||
// | |||||
//===----------------------------------------------------------------------===// | |||||
#include "Context.h" | |||||
#include "FSProvider.h" | |||||
Eugene.Zelenko: Please use relative path. Same below. | |||||
Heh, where do these come from? ilya-biryukov: Heh, where do these come from?
Does our include insertion prefer to add global paths in some… | |||||
ah interesting, my guess is static index without any "-I" flags to direct path shortening. kadircet: ah interesting, my guess is static index without any "-I" flags to direct path shortening. | |||||
#include "GlobalCompilationDatabase.h" | |||||
#include "Logger.h" | |||||
#include "index/Background.h" | |||||
#include "clang/Tooling/CompilationDatabase.h" | |||||
#include "llvm/ADT/None.h" | |||||
#include "llvm/ADT/SmallString.h" | |||||
#include "llvm/ADT/StringRef.h" | |||||
#include "llvm/Support/CommandLine.h" | |||||
#include "llvm/Support/FileSystem.h" | |||||
#include "llvm/Support/Path.h" | |||||
#include "llvm/Support/Signals.h" | |||||
#include "llvm/Support/raw_ostream.h" | |||||
#include <memory> | |||||
static llvm::cl::opt<std::string> CompileCommandsDir( | |||||
ilya-biryukovUnsubmitted Not Done ReplyInline ActionsThat's not the accurate name or description, right? Maybe rename and document accordingly? ilya-biryukov: That's not the accurate name or description, right?
We would actually walk up the directory… | |||||
llvm::cl::Positional, | |||||
llvm::cl::desc("Directory containing compilation database"), | |||||
llvm::cl::init(".")); | |||||
int main(int Argc, const char **Argv) { | |||||
llvm::sys::PrintStackTraceOnErrorSignal(Argv[0]); | |||||
llvm::cl::ParseCommandLineOptions( | |||||
Argc, Argv, "A standalone binary to run clangd's background indexer."); | |||||
clang::clangd::RealFileSystemProvider FSProvider; | |||||
llvm::errs().SetBuffered(); | |||||
ilya-biryukovUnsubmitted Not Done ReplyInline ActionsWhy? Maybe add a comment? ilya-biryukov: Why? Maybe add a comment? | |||||
clang::clangd::StreamLogger Logger(llvm::errs(), clang::clangd::Logger::Info); | |||||
clang::clangd::LoggingSession LoggingSession(Logger); | |||||
clang::clangd::DirectoryBasedGlobalCompilationDatabase DirectoryBasedCDB( | |||||
/*CompileCommandsDir=*/llvm::None); | |||||
clang::clangd::OverlayCDB CDB(&DirectoryBasedCDB); | |||||
// FIXME: Maybe add a way to not lower thread priority? | |||||
ilya-biryukovUnsubmitted Not Done ReplyInline ActionsI think that's actually a hard requirement. ilya-biryukov: I think that's actually a hard requirement.
We definitely want to run with normal priority when… | |||||
clang::clangd::BackgroundIndex BackgroundIdx( | |||||
clang::clangd::Context::empty(), FSProvider, CDB, | |||||
clang::clangd::BackgroundIndexStorage::createDiskBackedStorageFactory(), | |||||
// We don't want to build dex/mem index until every file has been | |||||
// indexed/loaded. | |||||
// FIXME: Change BackgroundIndex to only built after all files have been | |||||
// read if build period is zero, which should only be used by | |||||
// non-interactive tools like this one. | |||||
24 * 60 * 60 * 1000); | |||||
ilya-biryukovUnsubmitted Not Done ReplyInline ActionsMaybe use std::numeric_limits<size_t>::max() here? ilya-biryukov: Maybe use `std::numeric_limits<size_t>::max()` here?
Would mean we'll never build the dex index… | |||||
Not Done ReplyInline ActionsNit: maybe we should give this constant a name? Or maybe create a command line option for this? jkorous: Nit: maybe we should give this constant a name? Or maybe create a command line option for this? | |||||
It is the period for building index data structures which helps making queries fast, as I mentioned in the comment we definitely don't need this to happen until we've indexed every file, and BackgroundIndex should in my opinion work in this mode if period was set to zero. So I don't see any point in giving it a name. Btw, the reason we want to build index data structures in the end is to just see how long it takes. kadircet: It is the period for building index data structures which helps making queries fast, as I… | |||||
// To trigger background indexing we need to discover a compilation database, | |||||
// which can be done by simply querying compile commands for a cpp file under | |||||
// the directory containing compilation_database.jon. | |||||
llvm::SmallString<128> DummyFile(CompileCommandsDir); | |||||
Maybe we should add a comment about why we do this? jkorous: Maybe we should add a comment about why we do this? | |||||
llvm::sys::fs::make_absolute(DummyFile); | |||||
llvm::sys::path::remove_dots(DummyFile, true); | |||||
llvm::sys::path::append(DummyFile, "dummy.cpp"); | |||||
CDB.getCompileCommand(DummyFile); | |||||
ilya-biryukovUnsubmitted Not Done ReplyInline ActionsWe seem to be fighting with the interface we defined here. ilya-biryukov: We seem to be fighting with the interface we defined here.
If we need to support an operation… | |||||
kadircetAuthorUnsubmitted I should've rather performed this through enqueue, will change to use that. kadircet: I should've rather performed this through `enqueue`, will change to use that. | |||||
// Wait untill indexing finishes. | |||||
BackgroundIdx.blockUntilIdleForTest(llvm::None); | |||||
ilya-biryukovUnsubmitted Not Done ReplyInline ActionsWe can't leave the function called like this, since we now seem to have a legitimate usage outside the test code. Maybe rename it accordingly? ilya-biryukov: We can't leave the function called like this, since we now seem to have a legitimate usage… | |||||
return 0; | |||||
} |
Please use relative path. Same below.