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-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-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 04:22:05 +08:00
|
|
|
static llvm::cl::opt<std::string>
|
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-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-02 23:13:20 +08:00
|
|
|
/// Validate command line arguments.
|
2017-02-07 18:28:20 +08:00
|
|
|
llvm::raw_ostream &Outs = llvm::outs();
|
|
|
|
llvm::raw_ostream &Logs = llvm::errs();
|
2017-10-10 04:22:05 +08:00
|
|
|
JSONOutput Out(Outs, Logs);
|
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.
|
|
|
|
namespace path = llvm::sys::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-02 23:13:20 +08:00
|
|
|
/// Change stdin to binary to not lose \r\n on windows.
|
|
|
|
llvm::sys::ChangeStdinToBinary();
|
|
|
|
|
|
|
|
/// 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-05-16 22:40:30 +08:00
|
|
|
LSPServer.run(std::cin);
|
2017-02-07 18:28:20 +08:00
|
|
|
}
|