2018-08-15 00:03:32 +08:00
|
|
|
//===--- GlobalCompilationDatabase.cpp ---------------------------*- C++-*-===//
|
2017-05-16 17:38:59 +08:00
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
2018-08-15 00:03:32 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
2017-05-16 17:38:59 +08:00
|
|
|
|
|
|
|
#include "GlobalCompilationDatabase.h"
|
2017-10-02 23:13:20 +08:00
|
|
|
#include "Logger.h"
|
2017-05-16 17:38:59 +08:00
|
|
|
#include "clang/Tooling/CompilationDatabase.h"
|
|
|
|
#include "llvm/Support/FileSystem.h"
|
|
|
|
#include "llvm/Support/Path.h"
|
|
|
|
|
2017-07-06 16:44:54 +08:00
|
|
|
namespace clang {
|
|
|
|
namespace clangd {
|
|
|
|
|
2017-12-04 18:08:45 +08:00
|
|
|
tooling::CompileCommand
|
|
|
|
GlobalCompilationDatabase::getFallbackCommand(PathRef File) const {
|
2018-04-20 19:35:17 +08:00
|
|
|
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 (llvm::sys::path::extension(File) == ".h")
|
|
|
|
Argv.push_back("-xobjective-c++-header");
|
|
|
|
Argv.push_back(File);
|
2017-07-06 16:44:54 +08:00
|
|
|
return tooling::CompileCommand(llvm::sys::path::parent_path(File),
|
2017-12-04 18:08:45 +08:00
|
|
|
llvm::sys::path::filename(File),
|
2018-04-20 19:35:17 +08:00
|
|
|
std::move(Argv),
|
2017-07-06 16:44:54 +08:00
|
|
|
/*Output=*/"");
|
|
|
|
}
|
2017-05-16 17:38:59 +08:00
|
|
|
|
2017-09-20 15:24:15 +08:00
|
|
|
DirectoryBasedGlobalCompilationDatabase::
|
2017-10-02 23:13:20 +08:00
|
|
|
DirectoryBasedGlobalCompilationDatabase(
|
2017-12-13 20:51:22 +08:00
|
|
|
llvm::Optional<Path> CompileCommandsDir)
|
|
|
|
: CompileCommandsDir(std::move(CompileCommandsDir)) {}
|
2017-09-20 15:24:15 +08:00
|
|
|
|
2018-04-20 19:35:17 +08:00
|
|
|
DirectoryBasedGlobalCompilationDatabase::
|
|
|
|
~DirectoryBasedGlobalCompilationDatabase() = default;
|
|
|
|
|
2017-12-04 18:08:45 +08:00
|
|
|
llvm::Optional<tooling::CompileCommand>
|
|
|
|
DirectoryBasedGlobalCompilationDatabase::getCompileCommand(PathRef File) const {
|
2017-12-22 17:47:34 +08:00
|
|
|
if (auto CDB = getCDBForFile(File)) {
|
2017-12-04 18:08:45 +08:00
|
|
|
auto Candidates = CDB->getCompileCommands(File);
|
|
|
|
if (!Candidates.empty()) {
|
|
|
|
addExtraFlags(File, Candidates.front());
|
|
|
|
return std::move(Candidates.front());
|
|
|
|
}
|
2017-12-22 17:47:34 +08:00
|
|
|
} else {
|
[clangd] Upgrade logging facilities with levels and formatv.
Summary:
log() is split into four functions:
- elog()/log()/vlog() have different severity levels, allowing filtering
- dlog() is a lazy macro which uses LLVM_DEBUG - it logs to the logger, but
conditionally based on -debug-only flag and is omitted in release builds
All logging functions use formatv-style format strings now, e.g:
log("Could not resolve URI {0}: {1}", URI, Result.takeError());
Existing log sites have been split between elog/log/vlog by best guess.
This includes a workaround for passing Error to formatv that can be
simplified when D49170 or similar lands.
Subscribers: ilya-biryukov, javed.absar, ioeric, MaskRay, jkorous, cfe-commits
Differential Revision: https://reviews.llvm.org/D49008
llvm-svn: 336785
2018-07-11 18:35:11 +08:00
|
|
|
log("Failed to find compilation database for {0}", File);
|
2017-07-06 16:44:54 +08:00
|
|
|
}
|
2017-12-04 18:08:45 +08:00
|
|
|
return llvm::None;
|
|
|
|
}
|
2017-07-06 16:44:54 +08:00
|
|
|
|
2017-12-04 18:08:45 +08:00
|
|
|
tooling::CompileCommand
|
|
|
|
DirectoryBasedGlobalCompilationDatabase::getFallbackCommand(
|
|
|
|
PathRef File) const {
|
|
|
|
auto C = GlobalCompilationDatabase::getFallbackCommand(File);
|
|
|
|
addExtraFlags(File, C);
|
|
|
|
return C;
|
2017-07-06 16:44:54 +08:00
|
|
|
}
|
|
|
|
|
[clangd] DidChangeConfiguration Notification
Summary:
Implementation of DidChangeConfiguration notification handling in
clangd. This currently only supports changing one setting: the path of
the compilation database to be used for the current project. In other
words, it is no longer necessary to restart clangd with a different
command line argument in order to change the compilation database.
Reviewers: malaperle, krasimir, bkramer, ilya-biryukov
Subscribers: jkorous-apple, ioeric, simark, klimek, ilya-biryukov, arphaman, rwols, cfe-commits
Differential Revision: https://reviews.llvm.org/D39571
Signed-off-by: Simon Marchi <simon.marchi@ericsson.com>
Signed-off-by: William Enright <william.enright@polymtl.ca>
llvm-svn: 325784
2018-02-22 22:00:39 +08:00
|
|
|
void DirectoryBasedGlobalCompilationDatabase::setCompileCommandsDir(Path P) {
|
|
|
|
std::lock_guard<std::mutex> Lock(Mutex);
|
|
|
|
CompileCommandsDir = P;
|
|
|
|
CompilationDatabases.clear();
|
|
|
|
}
|
|
|
|
|
2017-07-06 16:44:54 +08:00
|
|
|
void DirectoryBasedGlobalCompilationDatabase::setExtraFlagsForFile(
|
|
|
|
PathRef File, std::vector<std::string> ExtraFlags) {
|
2017-12-04 18:08:45 +08:00
|
|
|
std::lock_guard<std::mutex> Lock(Mutex);
|
2017-07-06 16:44:54 +08:00
|
|
|
ExtraFlagsForFile[File] = std::move(ExtraFlags);
|
2017-05-16 17:38:59 +08:00
|
|
|
}
|
|
|
|
|
2017-12-04 18:08:45 +08:00
|
|
|
void DirectoryBasedGlobalCompilationDatabase::addExtraFlags(
|
|
|
|
PathRef File, tooling::CompileCommand &C) const {
|
|
|
|
std::lock_guard<std::mutex> Lock(Mutex);
|
|
|
|
|
|
|
|
auto It = ExtraFlagsForFile.find(File);
|
|
|
|
if (It == ExtraFlagsForFile.end())
|
|
|
|
return;
|
|
|
|
|
|
|
|
auto &Args = C.CommandLine;
|
|
|
|
assert(Args.size() >= 2 && "Expected at least [compiler, source file]");
|
|
|
|
// The last argument of CommandLine is the name of the input file.
|
|
|
|
// Add ExtraFlags before it.
|
|
|
|
Args.insert(Args.end() - 1, It->second.begin(), It->second.end());
|
|
|
|
}
|
|
|
|
|
2017-05-16 17:38:59 +08:00
|
|
|
tooling::CompilationDatabase *
|
2017-12-22 17:47:34 +08:00
|
|
|
DirectoryBasedGlobalCompilationDatabase::getCDBInDirLocked(PathRef Dir) const {
|
|
|
|
// FIXME(ibiryukov): Invalidate cached compilation databases on changes
|
|
|
|
auto CachedIt = CompilationDatabases.find(Dir);
|
2017-10-02 23:13:20 +08:00
|
|
|
if (CachedIt != CompilationDatabases.end())
|
|
|
|
return CachedIt->second.get();
|
|
|
|
std::string Error = "";
|
2017-12-22 17:47:34 +08:00
|
|
|
auto CDB = tooling::CompilationDatabase::loadFromDirectory(Dir, Error);
|
|
|
|
auto Result = CDB.get();
|
|
|
|
CompilationDatabases.insert(std::make_pair(Dir, std::move(CDB)));
|
|
|
|
return Result;
|
2017-10-02 23:13:20 +08:00
|
|
|
}
|
2017-09-20 15:24:15 +08:00
|
|
|
|
2017-10-02 23:13:20 +08:00
|
|
|
tooling::CompilationDatabase *
|
2017-12-22 17:47:34 +08:00
|
|
|
DirectoryBasedGlobalCompilationDatabase::getCDBForFile(PathRef File) const {
|
2017-10-02 23:13:20 +08:00
|
|
|
namespace path = llvm::sys::path;
|
2017-12-22 17:47:34 +08:00
|
|
|
assert((path::is_absolute(File, path::Style::posix) ||
|
|
|
|
path::is_absolute(File, path::Style::windows)) &&
|
|
|
|
"path must be absolute");
|
2017-10-02 23:13:20 +08:00
|
|
|
|
2017-12-22 17:47:34 +08:00
|
|
|
std::lock_guard<std::mutex> Lock(Mutex);
|
|
|
|
if (CompileCommandsDir)
|
|
|
|
return getCDBInDirLocked(*CompileCommandsDir);
|
2017-10-02 23:13:20 +08:00
|
|
|
for (auto Path = path::parent_path(File); !Path.empty();
|
2017-12-22 17:47:34 +08:00
|
|
|
Path = path::parent_path(Path))
|
|
|
|
if (auto CDB = getCDBInDirLocked(Path))
|
|
|
|
return CDB;
|
2017-05-16 17:38:59 +08:00
|
|
|
return nullptr;
|
|
|
|
}
|
2017-07-06 16:44:54 +08:00
|
|
|
|
2018-06-13 17:20:41 +08:00
|
|
|
CachingCompilationDb::CachingCompilationDb(
|
|
|
|
const GlobalCompilationDatabase &InnerCDB)
|
|
|
|
: InnerCDB(InnerCDB) {}
|
|
|
|
|
|
|
|
llvm::Optional<tooling::CompileCommand>
|
|
|
|
CachingCompilationDb::getCompileCommand(PathRef File) const {
|
|
|
|
std::unique_lock<std::mutex> Lock(Mut);
|
|
|
|
auto It = Cached.find(File);
|
|
|
|
if (It != Cached.end())
|
|
|
|
return It->second;
|
|
|
|
|
|
|
|
Lock.unlock();
|
|
|
|
llvm::Optional<tooling::CompileCommand> Command =
|
|
|
|
InnerCDB.getCompileCommand(File);
|
|
|
|
Lock.lock();
|
|
|
|
return Cached.try_emplace(File, std::move(Command)).first->getValue();
|
|
|
|
}
|
|
|
|
|
|
|
|
tooling::CompileCommand
|
|
|
|
CachingCompilationDb::getFallbackCommand(PathRef File) const {
|
|
|
|
return InnerCDB.getFallbackCommand(File);
|
|
|
|
}
|
|
|
|
|
|
|
|
void CachingCompilationDb::invalidate(PathRef File) {
|
|
|
|
std::unique_lock<std::mutex> Lock(Mut);
|
|
|
|
Cached.erase(File);
|
|
|
|
}
|
|
|
|
|
|
|
|
void CachingCompilationDb::clear() {
|
|
|
|
std::unique_lock<std::mutex> Lock(Mut);
|
|
|
|
Cached.clear();
|
|
|
|
}
|
|
|
|
|
2018-08-02 01:39:29 +08:00
|
|
|
llvm::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;
|
|
|
|
}
|
|
|
|
|
|
|
|
void InMemoryCompilationDb::invalidate(PathRef File) {
|
|
|
|
std::unique_lock<std::mutex> Lock(Mutex);
|
|
|
|
Commands.erase(File);
|
|
|
|
}
|
|
|
|
|
2017-07-06 16:44:54 +08:00
|
|
|
} // namespace clangd
|
|
|
|
} // namespace clang
|