mirror of
https://github.com/Gericom/teak-llvm.git
synced 2025-06-19 19:45:40 -04:00

Summary: Instead of receiving compilation commands, auto-index is triggered by just filenames to reindex, and gets commands from the global comp DB internally. This has advantages: - more of the work can be done asynchronously (fetching compilation commands upfront can be slow for large CDBs) - we get access to the CDB which can be used to retrieve interpolated commands for headers (useful in some cases where the original TU goes away) - fits nicely with the filename-only change observation from r347297 The interface to GlobalCompilationDatabase gets extended: when retrieving a compile command, the GCDB can optionally report the project the file belongs to. This naturally fits together with getCompileCommand: it's hard to implement one without the other. But because most callers don't care, I've ended up with an awkward optional-out-param-in-virtual method pattern - maybe there's a better one. This is the main missing integration point between ClangdServer and BackgroundIndex, after this we should be able to add an auto-index flag. Reviewers: ioeric, kadircet Subscribers: MaskRay, jkorous, arphaman, cfe-commits, ilya-biryukov Differential Revision: https://reviews.llvm.org/D54865 llvm-svn: 347538
141 lines
4.7 KiB
C++
141 lines
4.7 KiB
C++
//===--- GlobalCompilationDatabase.cpp ---------------------------*- C++-*-===//
|
|
//
|
|
// The LLVM Compiler Infrastructure
|
|
//
|
|
// This file is distributed under the University of Illinois Open Source
|
|
// License. See LICENSE.TXT for details.
|
|
//
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
#include "GlobalCompilationDatabase.h"
|
|
#include "Logger.h"
|
|
#include "clang/Tooling/CompilationDatabase.h"
|
|
#include "llvm/Support/FileSystem.h"
|
|
#include "llvm/Support/Path.h"
|
|
|
|
using namespace llvm;
|
|
namespace clang {
|
|
namespace clangd {
|
|
|
|
tooling::CompileCommand
|
|
GlobalCompilationDatabase::getFallbackCommand(PathRef File) const {
|
|
std::vector<std::string> Argv = {"clang"};
|
|
// Clang treats .h files as C by default, resulting in unhelpful diagnostics.
|
|
// Parsing as Objective C++ is friendly to more cases.
|
|
if (sys::path::extension(File) == ".h")
|
|
Argv.push_back("-xobjective-c++-header");
|
|
Argv.push_back(File);
|
|
return tooling::CompileCommand(sys::path::parent_path(File),
|
|
sys::path::filename(File), std::move(Argv),
|
|
/*Output=*/"");
|
|
}
|
|
|
|
DirectoryBasedGlobalCompilationDatabase::
|
|
DirectoryBasedGlobalCompilationDatabase(Optional<Path> CompileCommandsDir)
|
|
: CompileCommandsDir(std::move(CompileCommandsDir)) {}
|
|
|
|
DirectoryBasedGlobalCompilationDatabase::
|
|
~DirectoryBasedGlobalCompilationDatabase() = default;
|
|
|
|
Optional<tooling::CompileCommand>
|
|
DirectoryBasedGlobalCompilationDatabase::getCompileCommand(
|
|
PathRef File, ProjectInfo *Project) const {
|
|
if (auto CDB = getCDBForFile(File, Project)) {
|
|
auto Candidates = CDB->getCompileCommands(File);
|
|
if (!Candidates.empty()) {
|
|
return std::move(Candidates.front());
|
|
}
|
|
} else {
|
|
log("Failed to find compilation database for {0}", File);
|
|
}
|
|
return None;
|
|
}
|
|
|
|
std::pair<tooling::CompilationDatabase *, /*Cached*/ bool>
|
|
DirectoryBasedGlobalCompilationDatabase::getCDBInDirLocked(PathRef Dir) const {
|
|
// FIXME(ibiryukov): Invalidate cached compilation databases on changes
|
|
auto CachedIt = CompilationDatabases.find(Dir);
|
|
if (CachedIt != CompilationDatabases.end())
|
|
return {CachedIt->second.get(), true};
|
|
std::string Error = "";
|
|
auto CDB = tooling::CompilationDatabase::loadFromDirectory(Dir, Error);
|
|
auto Result = CDB.get();
|
|
CompilationDatabases.insert(std::make_pair(Dir, std::move(CDB)));
|
|
return {Result, false};
|
|
}
|
|
|
|
tooling::CompilationDatabase *
|
|
DirectoryBasedGlobalCompilationDatabase::getCDBForFile(
|
|
PathRef File, ProjectInfo *Project) const {
|
|
namespace path = sys::path;
|
|
assert((path::is_absolute(File, path::Style::posix) ||
|
|
path::is_absolute(File, path::Style::windows)) &&
|
|
"path must be absolute");
|
|
|
|
tooling::CompilationDatabase *CDB = nullptr;
|
|
bool Cached = false;
|
|
std::lock_guard<std::mutex> Lock(Mutex);
|
|
if (CompileCommandsDir) {
|
|
std::tie(CDB, Cached) = getCDBInDirLocked(*CompileCommandsDir);
|
|
if (Project && CDB)
|
|
Project->SourceRoot = *CompileCommandsDir;
|
|
} else {
|
|
for (auto Path = path::parent_path(File); !CDB && !Path.empty();
|
|
Path = path::parent_path(Path)) {
|
|
std::tie(CDB, Cached) = getCDBInDirLocked(Path);
|
|
if (Project && CDB)
|
|
Project->SourceRoot = Path;
|
|
}
|
|
}
|
|
if (CDB && !Cached)
|
|
OnCommandChanged.broadcast(CDB->getAllFiles());
|
|
return CDB;
|
|
}
|
|
|
|
OverlayCDB::OverlayCDB(const GlobalCompilationDatabase *Base,
|
|
std::vector<std::string> FallbackFlags)
|
|
: Base(Base), FallbackFlags(std::move(FallbackFlags)) {
|
|
if (Base)
|
|
BaseChanged = Base->watch([this](const std::vector<std::string> Changes) {
|
|
OnCommandChanged.broadcast(Changes);
|
|
});
|
|
}
|
|
|
|
Optional<tooling::CompileCommand>
|
|
OverlayCDB::getCompileCommand(PathRef File, ProjectInfo *Project) const {
|
|
{
|
|
std::lock_guard<std::mutex> Lock(Mutex);
|
|
auto It = Commands.find(File);
|
|
if (It != Commands.end()) {
|
|
if (Project)
|
|
Project->SourceRoot = "";
|
|
return It->second;
|
|
}
|
|
}
|
|
return Base ? Base->getCompileCommand(File) : None;
|
|
}
|
|
|
|
tooling::CompileCommand OverlayCDB::getFallbackCommand(PathRef File) const {
|
|
auto Cmd = Base ? Base->getFallbackCommand(File)
|
|
: GlobalCompilationDatabase::getFallbackCommand(File);
|
|
std::lock_guard<std::mutex> Lock(Mutex);
|
|
Cmd.CommandLine.insert(Cmd.CommandLine.end(), FallbackFlags.begin(),
|
|
FallbackFlags.end());
|
|
return Cmd;
|
|
}
|
|
|
|
void OverlayCDB::setCompileCommand(
|
|
PathRef File, llvm::Optional<tooling::CompileCommand> Cmd) {
|
|
{
|
|
std::unique_lock<std::mutex> Lock(Mutex);
|
|
if (Cmd)
|
|
Commands[File] = std::move(*Cmd);
|
|
else
|
|
Commands.erase(File);
|
|
}
|
|
OnCommandChanged.broadcast({File});
|
|
}
|
|
|
|
} // namespace clangd
|
|
} // namespace clang
|