diff options
author | Krasimir Georgiev <krasimir@google.com> | 2017-07-06 08:44:54 +0000 |
---|---|---|
committer | Krasimir Georgiev <krasimir@google.com> | 2017-07-06 08:44:54 +0000 |
commit | 5ec1f7ca32eb85077a22ce81d41aa02a017d4852 (patch) | |
tree | 90f55c6e2bb1cc6f30aa4067e6391fb32f8f8a7e /clangd/GlobalCompilationDatabase.cpp | |
parent | 9978ec5d4094a4e9cf90bb898ca0706c9a0944bc (diff) |
[clangd] Add support for per-file extra flags
Summary:
This patch adds the ability to specify user-defined extra flags per opened file
through the LSP layer. This is a non-standard extension to the protocol.
I've already created a feature request about it for upstream lsp:
https://github.com/Microsoft/language-server-protocol/issues/255
The particular use-case is ycmd, which has a python script for figuring out
extra flags per file:
https://github.com/Valloric/ycmd#flagsforfile-filename-kwargs-
Reviewers: ilya-biryukov, klimek, bkramer
Reviewed By: ilya-biryukov
Subscribers: cfe-commits
Tags: #clang-tools-extra
Differential Revision: https://reviews.llvm.org/D34947
git-svn-id: https://llvm.org/svn/llvm-project/clang-tools-extra/trunk@307241 91177308-0d34-0410-b5e6-96231b3b80d8
Diffstat (limited to 'clangd/GlobalCompilationDatabase.cpp')
-rw-r--r-- | clangd/GlobalCompilationDatabase.cpp | 49 |
1 files changed, 44 insertions, 5 deletions
diff --git a/clangd/GlobalCompilationDatabase.cpp b/clangd/GlobalCompilationDatabase.cpp index 91d77026..9cf8572c 100644 --- a/clangd/GlobalCompilationDatabase.cpp +++ b/clangd/GlobalCompilationDatabase.cpp @@ -12,17 +12,53 @@ #include "llvm/Support/FileSystem.h" #include "llvm/Support/Path.h" -using namespace clang::clangd; -using namespace clang; +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) { + std::vector<std::string> CommandLine{"clang", "-fsyntax-only", File.str()}; + return tooling::CompileCommand(llvm::sys::path::parent_path(File), + llvm::sys::path::filename(File), CommandLine, + /*Output=*/""); +} std::vector<tooling::CompileCommand> DirectoryBasedGlobalCompilationDatabase::getCompileCommands(PathRef File) { std::vector<tooling::CompileCommand> Commands; auto CDB = getCompilationDatabase(File); - if (!CDB) - return {}; - return CDB->getCompileCommands(File); + 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); } tooling::CompilationDatabase * @@ -63,3 +99,6 @@ DirectoryBasedGlobalCompilationDatabase::getCompilationDatabase(PathRef File) { // "\n"); return nullptr; } + +} // namespace clangd +} // namespace clang |