2017-04-13 01:13:08 +08:00
|
|
|
//===--- ClangdMain.cpp - clangd server loop ------------------------------===//
|
2017-02-07 18:28:20 +08:00
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2017-05-16 17:38:59 +08:00
|
|
|
#include "ClangdLSPServer.h"
|
2017-05-16 22:40:30 +08:00
|
|
|
#include "JSONRPCDispatcher.h"
|
2017-10-10 17:08:47 +08:00
|
|
|
#include "Path.h"
|
2017-03-02 00:16:29 +08:00
|
|
|
#include "llvm/Support/CommandLine.h"
|
2017-02-07 18:28:20 +08:00
|
|
|
#include "llvm/Support/FileSystem.h"
|
2017-10-02 23:13:20 +08:00
|
|
|
#include "llvm/Support/Path.h"
|
2017-02-07 20:40:59 +08:00
|
|
|
#include "llvm/Support/Program.h"
|
2017-10-10 17:08:47 +08:00
|
|
|
#include "llvm/Support/raw_ostream.h"
|
2017-02-07 18:28:20 +08:00
|
|
|
#include <iostream>
|
2017-05-16 17:38:59 +08:00
|
|
|
#include <memory>
|
2017-02-07 18:28:20 +08:00
|
|
|
#include <string>
|
2017-08-14 16:45:47 +08:00
|
|
|
#include <thread>
|
2017-05-16 17:38:59 +08:00
|
|
|
|
|
|
|
using namespace clang;
|
2017-02-07 18:28:20 +08:00
|
|
|
using namespace clang::clangd;
|
|
|
|
|
2017-10-02 23:13:20 +08:00
|
|
|
static llvm::cl::opt<Path> CompileCommandsDir(
|
|
|
|
"compile-commands-dir",
|
|
|
|
llvm::cl::desc("Specify a path to look for compile_commands.json. If path "
|
|
|
|
"is invalid, clangd will look in the current directory and "
|
|
|
|
"parent paths of each source file."));
|
|
|
|
|
2017-08-14 16:45:47 +08:00
|
|
|
static llvm::cl::opt<unsigned>
|
|
|
|
WorkerThreadsCount("j",
|
|
|
|
llvm::cl::desc("Number of async workers used by clangd"),
|
|
|
|
llvm::cl::init(getDefaultAsyncThreadsCount()));
|
|
|
|
|
2017-09-12 21:57:14 +08:00
|
|
|
static llvm::cl::opt<bool> EnableSnippets(
|
|
|
|
"enable-snippets",
|
|
|
|
llvm::cl::desc(
|
|
|
|
"Present snippet completions instead of plaintext completions"),
|
|
|
|
llvm::cl::init(false));
|
|
|
|
|
2017-08-14 16:45:47 +08:00
|
|
|
static llvm::cl::opt<bool> RunSynchronously(
|
|
|
|
"run-synchronously",
|
|
|
|
llvm::cl::desc("Parse on main thread. If set, -j is ignored"),
|
|
|
|
llvm::cl::init(false), llvm::cl::Hidden);
|
2017-03-02 00:16:29 +08:00
|
|
|
|
2017-10-10 17:08:47 +08:00
|
|
|
static llvm::cl::opt<Path>
|
2017-07-19 23:43:35 +08:00
|
|
|
ResourceDir("resource-dir",
|
2017-08-02 16:53:48 +08:00
|
|
|
llvm::cl::desc("Directory for system clang headers"),
|
2017-07-19 23:43:35 +08:00
|
|
|
llvm::cl::init(""), llvm::cl::Hidden);
|
|
|
|
|
2017-10-10 17:08:47 +08:00
|
|
|
static llvm::cl::opt<Path> InputMirrorFile(
|
|
|
|
"input-mirror-file",
|
|
|
|
llvm::cl::desc(
|
|
|
|
"Mirror all LSP input to the specified file. Useful for debugging."),
|
|
|
|
llvm::cl::init(""), llvm::cl::Hidden);
|
|
|
|
|
2017-02-07 18:28:20 +08:00
|
|
|
int main(int argc, char *argv[]) {
|
2017-03-02 00:16:29 +08:00
|
|
|
llvm::cl::ParseCommandLineOptions(argc, argv, "clangd");
|
2017-05-16 22:40:30 +08:00
|
|
|
|
2017-08-14 16:45:47 +08:00
|
|
|
if (!RunSynchronously && WorkerThreadsCount == 0) {
|
|
|
|
llvm::errs() << "A number of worker threads cannot be 0. Did you mean to "
|
|
|
|
"specify -run-synchronously?";
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Ignore -j option if -run-synchonously is used.
|
|
|
|
// FIXME: a warning should be shown here.
|
|
|
|
if (RunSynchronously)
|
|
|
|
WorkerThreadsCount = 0;
|
|
|
|
|
2017-10-26 18:07:04 +08:00
|
|
|
// Validate command line arguments.
|
2017-10-10 17:08:47 +08:00
|
|
|
llvm::Optional<llvm::raw_fd_ostream> InputMirrorStream;
|
|
|
|
if (!InputMirrorFile.empty()) {
|
|
|
|
std::error_code EC;
|
|
|
|
InputMirrorStream.emplace(InputMirrorFile, /*ref*/ EC, llvm::sys::fs::F_RW);
|
|
|
|
if (EC) {
|
|
|
|
InputMirrorStream.reset();
|
|
|
|
llvm::errs() << "Error while opening an input mirror file: "
|
|
|
|
<< EC.message();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-02-07 18:28:20 +08:00
|
|
|
llvm::raw_ostream &Outs = llvm::outs();
|
|
|
|
llvm::raw_ostream &Logs = llvm::errs();
|
2017-10-10 17:08:47 +08:00
|
|
|
JSONOutput Out(Outs, Logs,
|
|
|
|
InputMirrorStream ? InputMirrorStream.getPointer() : nullptr);
|
2017-02-07 18:28:20 +08:00
|
|
|
|
2017-10-02 23:13:20 +08:00
|
|
|
// If --compile-commands-dir arg was invoked, check value and override default
|
|
|
|
// path.
|
|
|
|
llvm::Optional<Path> CompileCommandsDirPath;
|
|
|
|
|
|
|
|
if (CompileCommandsDir.empty()) {
|
|
|
|
CompileCommandsDirPath = llvm::None;
|
|
|
|
} else if (!llvm::sys::path::is_absolute(CompileCommandsDir) ||
|
|
|
|
!llvm::sys::fs::exists(CompileCommandsDir)) {
|
|
|
|
llvm::errs() << "Path specified by --compile-commands-dir either does not "
|
|
|
|
"exist or is not an absolute "
|
|
|
|
"path. The argument will be ignored.\n";
|
|
|
|
CompileCommandsDirPath = llvm::None;
|
|
|
|
} else {
|
|
|
|
CompileCommandsDirPath = CompileCommandsDir;
|
|
|
|
}
|
2017-02-07 20:40:59 +08:00
|
|
|
|
2017-07-19 23:43:35 +08:00
|
|
|
llvm::Optional<StringRef> ResourceDirRef = None;
|
|
|
|
if (!ResourceDir.empty())
|
|
|
|
ResourceDirRef = ResourceDir;
|
2017-08-14 16:45:47 +08:00
|
|
|
|
2017-10-26 18:07:04 +08:00
|
|
|
// Change stdin to binary to not lose \r\n on windows.
|
2017-10-02 23:13:20 +08:00
|
|
|
llvm::sys::ChangeStdinToBinary();
|
|
|
|
|
2017-10-26 18:07:04 +08:00
|
|
|
// Initialize and run ClangdLSPServer.
|
2017-09-12 21:57:14 +08:00
|
|
|
ClangdLSPServer LSPServer(Out, WorkerThreadsCount, EnableSnippets,
|
2017-10-02 23:13:20 +08:00
|
|
|
ResourceDirRef, CompileCommandsDirPath);
|
2017-10-25 16:45:41 +08:00
|
|
|
|
|
|
|
constexpr int NoShutdownRequestErrorCode = 1;
|
|
|
|
return LSPServer.run(std::cin) ? 0 : NoShutdownRequestErrorCode;
|
2017-02-07 18:28:20 +08:00
|
|
|
}
|