teak-llvm/clang-tools-extra/clangd/GlobalCompilationDatabase.cpp
Sam McCall 2eb6b4038a [clangd] Remove didOpen extraFlags extension.
Summary:
This was added in D34947 to support YCM, but YCM actually provides *all* args,
and this was never actually used.
Meanwhile, we grew another extension that allows specifying all args.

I did find one user of this extension: https://github.com/thomasjo/atom-ide-cpp.
I'll reach out, there are multiple good alternatives:
 - compile_commands.txt can serve the same purpose as .clang_complete there
 - we can add an extension to support setting the fallback command

Reviewers: ilya-biryukov

Subscribers: ioeric, MaskRay, jkorous, arphaman, kadircet, cfe-commits

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

llvm-svn: 345969
2018-11-02 13:06:55 +00:00

103 lines
3.5 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) const {
if (auto CDB = getCDBForFile(File)) {
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;
}
tooling::CompilationDatabase *
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();
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;
}
tooling::CompilationDatabase *
DirectoryBasedGlobalCompilationDatabase::getCDBForFile(PathRef File) const {
namespace path = sys::path;
assert((path::is_absolute(File, path::Style::posix) ||
path::is_absolute(File, path::Style::windows)) &&
"path must be absolute");
std::lock_guard<std::mutex> Lock(Mutex);
if (CompileCommandsDir)
return getCDBInDirLocked(*CompileCommandsDir);
for (auto Path = path::parent_path(File); !Path.empty();
Path = path::parent_path(Path))
if (auto CDB = getCDBInDirLocked(Path))
return CDB;
return nullptr;
}
Optional<tooling::CompileCommand>
InMemoryCompilationDb::getCompileCommand(PathRef File) const {
std::lock_guard<std::mutex> Lock(Mutex);
auto It = Commands.find(File);
if (It == Commands.end())
return None;
return It->second;
}
bool InMemoryCompilationDb::setCompilationCommandForFile(
PathRef File, tooling::CompileCommand CompilationCommand) {
std::unique_lock<std::mutex> Lock(Mutex);
auto ItInserted = Commands.insert(std::make_pair(File, CompilationCommand));
if (ItInserted.second)
return true;
ItInserted.first->setValue(std::move(CompilationCommand));
return false;
}
} // namespace clangd
} // namespace clang