2017-05-16 17:38:59 +08:00
|
|
|
//===--- 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"
|
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 {
|
|
|
|
|
|
|
|
static void addExtraFlags(tooling::CompileCommand &Command,
|
|
|
|
const std::vector<std::string> &ExtraFlags) {
|
|
|
|
if (ExtraFlags.empty())
|
|
|
|
return;
|
|
|
|
assert(Command.CommandLine.size() >= 2 &&
|
|
|
|
"Expected a command line containing at least 2 arguments, the "
|
|
|
|
"compiler binary and the output file");
|
|
|
|
// The last argument of CommandLine is the name of the input file.
|
|
|
|
// Add ExtraFlags before it.
|
|
|
|
auto It = Command.CommandLine.end();
|
|
|
|
--It;
|
|
|
|
Command.CommandLine.insert(It, ExtraFlags.begin(), ExtraFlags.end());
|
|
|
|
}
|
|
|
|
|
|
|
|
tooling::CompileCommand getDefaultCompileCommand(PathRef File) {
|
2017-11-16 05:33:56 +08:00
|
|
|
std::vector<std::string> CommandLine{"clang", "-fsyntax-only", File.str()};
|
2017-07-06 16:44:54 +08:00
|
|
|
return tooling::CompileCommand(llvm::sys::path::parent_path(File),
|
|
|
|
llvm::sys::path::filename(File), CommandLine,
|
|
|
|
/*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(
|
|
|
|
clangd::Logger &Logger, llvm::Optional<Path> CompileCommandsDir)
|
2017-10-10 22:21:04 +08:00
|
|
|
: Logger(Logger), CompileCommandsDir(std::move(CompileCommandsDir)) {}
|
2017-09-20 15:24:15 +08:00
|
|
|
|
2017-05-16 17:38:59 +08:00
|
|
|
std::vector<tooling::CompileCommand>
|
|
|
|
DirectoryBasedGlobalCompilationDatabase::getCompileCommands(PathRef File) {
|
|
|
|
std::vector<tooling::CompileCommand> Commands;
|
|
|
|
|
|
|
|
auto CDB = getCompilationDatabase(File);
|
2017-07-06 16:44:54 +08:00
|
|
|
if (CDB)
|
|
|
|
Commands = CDB->getCompileCommands(File);
|
|
|
|
if (Commands.empty())
|
|
|
|
Commands.push_back(getDefaultCompileCommand(File));
|
|
|
|
|
|
|
|
auto It = ExtraFlagsForFile.find(File);
|
|
|
|
if (It != ExtraFlagsForFile.end()) {
|
|
|
|
// Append the user-specified flags to the compile commands.
|
|
|
|
for (tooling::CompileCommand &Command : Commands)
|
|
|
|
addExtraFlags(Command, It->second);
|
|
|
|
}
|
|
|
|
|
|
|
|
return Commands;
|
|
|
|
}
|
|
|
|
|
|
|
|
void DirectoryBasedGlobalCompilationDatabase::setExtraFlagsForFile(
|
|
|
|
PathRef File, std::vector<std::string> ExtraFlags) {
|
|
|
|
ExtraFlagsForFile[File] = std::move(ExtraFlags);
|
2017-05-16 17:38:59 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
tooling::CompilationDatabase *
|
2017-10-02 23:13:20 +08:00
|
|
|
DirectoryBasedGlobalCompilationDatabase::tryLoadDatabaseFromPath(PathRef File) {
|
2017-05-16 17:38:59 +08:00
|
|
|
|
|
|
|
namespace path = llvm::sys::path;
|
2017-10-02 23:13:20 +08:00
|
|
|
auto CachedIt = CompilationDatabases.find(File);
|
2017-05-16 17:38:59 +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
|
|
|
if (CachedIt != CompilationDatabases.end())
|
|
|
|
return CachedIt->second.get();
|
|
|
|
std::string Error = "";
|
|
|
|
auto CDB = tooling::CompilationDatabase::loadFromDirectory(File, Error);
|
2017-11-10 05:45:27 +08:00
|
|
|
if (CDB) {
|
2017-10-02 23:13:20 +08:00
|
|
|
auto Result = CDB.get();
|
|
|
|
CompilationDatabases.insert(std::make_pair(File, std::move(CDB)));
|
|
|
|
return Result;
|
|
|
|
}
|
2017-05-16 17:38:59 +08:00
|
|
|
|
2017-10-02 23:13:20 +08:00
|
|
|
return nullptr;
|
|
|
|
}
|
2017-09-20 15:24:15 +08:00
|
|
|
|
2017-10-02 23:13:20 +08:00
|
|
|
tooling::CompilationDatabase *
|
|
|
|
DirectoryBasedGlobalCompilationDatabase::getCompilationDatabase(PathRef File) {
|
|
|
|
std::lock_guard<std::mutex> Lock(Mutex);
|
|
|
|
|
|
|
|
namespace path = llvm::sys::path;
|
|
|
|
if (CompileCommandsDir.hasValue()) {
|
|
|
|
tooling::CompilationDatabase *ReturnValue =
|
|
|
|
tryLoadDatabaseFromPath(CompileCommandsDir.getValue());
|
|
|
|
if (ReturnValue == nullptr)
|
|
|
|
Logger.log("Failed to find compilation database for " + Twine(File) +
|
|
|
|
"in overriden directory " + CompileCommandsDir.getValue() +
|
|
|
|
"\n");
|
|
|
|
return ReturnValue;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (auto Path = path::parent_path(File); !Path.empty();
|
|
|
|
Path = path::parent_path(Path)) {
|
|
|
|
auto CDB = tryLoadDatabaseFromPath(Path);
|
2017-09-20 15:24:15 +08:00
|
|
|
if (!CDB)
|
2017-05-16 17:38:59 +08:00
|
|
|
continue;
|
|
|
|
// FIXME(ibiryukov): Invalidate cached compilation databases on changes
|
2017-10-02 23:13:20 +08:00
|
|
|
return CDB;
|
2017-05-16 17:38:59 +08:00
|
|
|
}
|
|
|
|
|
2017-09-20 15:24:15 +08:00
|
|
|
Logger.log("Failed to find compilation database for " + Twine(File) + "\n");
|
2017-05-16 17:38:59 +08:00
|
|
|
return nullptr;
|
|
|
|
}
|
2017-07-06 16:44:54 +08:00
|
|
|
|
|
|
|
} // namespace clangd
|
|
|
|
} // namespace clang
|