Nebiroth updated this revision to Diff 116559.
Nebiroth added a comment.
Fixed inverted compile_commands check logic that made tests fail.
More readable command arg checks.
https://reviews.llvm.org/D37150
Files:
clangd/ClangdLSPServer.cpp
clangd/ClangdLSPServer.h
clangd/GlobalCompilationDatabase.cpp
clangd/GlobalCompilationDatabase.h
clangd/tool/ClangdMain.cpp
Index: clangd/tool/ClangdMain.cpp
===================================================================
--- clangd/tool/ClangdMain.cpp
+++ clangd/tool/ClangdMain.cpp
@@ -11,16 +11,23 @@
#include "JSONRPCDispatcher.h"
#include "llvm/Support/CommandLine.h"
#include "llvm/Support/FileSystem.h"
+#include "llvm/Support/Path.h"
#include "llvm/Support/Program.h"
-
#include <iostream>
#include <memory>
#include <string>
#include <thread>
+#include <unistd.h>
using namespace clang;
using namespace clang::clangd;
+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."));
+
static llvm::cl::opt<unsigned>
WorkerThreadsCount("j",
llvm::cl::desc("Number of async workers used by clangd"),
@@ -56,18 +63,40 @@
if (RunSynchronously)
WorkerThreadsCount = 0;
+ /// Validate command line arguments.
llvm::raw_ostream &Outs = llvm::outs();
llvm::raw_ostream &Logs = llvm::errs();
JSONOutput Out(Outs, Logs);
- // Change stdin to binary to not lose \r\n on windows.
- llvm::sys::ChangeStdinToBinary();
+ // 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::errs()
+ << "Path specified by --compile-commands-dir must be an absolute "
+ "path. The argument will be ignored.\n";
+ CompileCommandsDirPath = llvm::None;
+ } else if (!llvm::sys::fs::exists(CompileCommandsDir)) {
+ llvm::errs() << "Path specified by --compile-commands-dir does not exist. "
+ "The argument will be "
+ "ignored.\n";
+ CompileCommandsDirPath = llvm::None;
+ } else
+ CompileCommandsDirPath = CompileCommandsDir;
llvm::Optional<StringRef> ResourceDirRef = None;
if (!ResourceDir.empty())
ResourceDirRef = ResourceDir;
+ /// Change stdin to binary to not lose \r\n on windows.
+ llvm::sys::ChangeStdinToBinary();
+
+ /// Initialize and run ClangdLSPServer.
ClangdLSPServer LSPServer(Out, WorkerThreadsCount, EnableSnippets,
- ResourceDirRef);
+ ResourceDirRef, CompileCommandsDirPath);
LSPServer.run(std::cin);
}
Index: clangd/GlobalCompilationDatabase.h
===================================================================
--- clangd/GlobalCompilationDatabase.h
+++ clangd/GlobalCompilationDatabase.h
@@ -47,15 +47,17 @@
class DirectoryBasedGlobalCompilationDatabase
: public GlobalCompilationDatabase {
public:
- DirectoryBasedGlobalCompilationDatabase(clangd::Logger &Logger);
+ DirectoryBasedGlobalCompilationDatabase(
+ clangd::Logger &Logger, llvm::Optional<Path> NewCompileCommandsDir);
std::vector<tooling::CompileCommand>
getCompileCommands(PathRef File) override;
void setExtraFlagsForFile(PathRef File, std::vector<std::string> ExtraFlags);
private:
tooling::CompilationDatabase *getCompilationDatabase(PathRef File);
+ tooling::CompilationDatabase *tryLoadDatabaseFromPath(PathRef File);
std::mutex Mutex;
/// Caches compilation databases loaded from directories(keys are
@@ -67,6 +69,9 @@
llvm::StringMap<std::vector<std::string>> ExtraFlagsForFile;
/// Used for logging.
clangd::Logger &Logger;
+ /// Used for command argument pointing to folder where compile_commands.json
+ /// is located.
+ llvm::Optional<Path> CompileCommandsDir;
};
} // namespace clangd
} // namespace clang
Index: clangd/GlobalCompilationDatabase.cpp
===================================================================
--- clangd/GlobalCompilationDatabase.cpp
+++ clangd/GlobalCompilationDatabase.cpp
@@ -38,8 +38,9 @@
}
DirectoryBasedGlobalCompilationDatabase::
- DirectoryBasedGlobalCompilationDatabase(clangd::Logger &Logger)
- : Logger(Logger) {}
+ DirectoryBasedGlobalCompilationDatabase(
+ clangd::Logger &Logger, llvm::Optional<Path> NewCompileCommandsDir)
+ : Logger(Logger), CompileCommandsDir(NewCompileCommandsDir) {}
std::vector<tooling::CompileCommand>
DirectoryBasedGlobalCompilationDatabase::getCompileCommands(PathRef File) {
@@ -67,34 +68,48 @@
}
tooling::CompilationDatabase *
-DirectoryBasedGlobalCompilationDatabase::getCompilationDatabase(PathRef File) {
- std::lock_guard<std::mutex> Lock(Mutex);
+DirectoryBasedGlobalCompilationDatabase::tryLoadDatabaseFromPath(PathRef File) {
namespace path = llvm::sys::path;
+ auto CachedIt = CompilationDatabases.find(File);
+ std::string Error = "";
assert((path::is_absolute(File, path::Style::posix) ||
path::is_absolute(File, path::Style::windows)) &&
"path must be absolute");
- for (auto Path = path::parent_path(File); !Path.empty();
- Path = path::parent_path(Path)) {
+ if (CachedIt != CompilationDatabases.end())
+ return CachedIt->second.get();
+ auto CDB = tooling::CompilationDatabase::loadFromDirectory(File, Error);
+ if (CDB && Error.empty()) {
+ auto Result = CDB.get();
+ CompilationDatabases.insert(std::make_pair(File, std::move(CDB)));
+ return Result;
+ }
- auto CachedIt = CompilationDatabases.find(Path);
- if (CachedIt != CompilationDatabases.end())
- return CachedIt->second.get();
+ // FIXME(ibiryukov): logging
+ // Output.log("Failed to find compilation database for " + Twine(File) +
+ // "\n");
+ return nullptr;
+}
- std::string Error;
- auto CDB = tooling::CompilationDatabase::loadFromDirectory(Path, Error);
+tooling::CompilationDatabase *
+DirectoryBasedGlobalCompilationDatabase::getCompilationDatabase(PathRef File) {
+ std::lock_guard<std::mutex> Lock(Mutex);
+
+ namespace path = llvm::sys::path;
+ if (CompileCommandsDir.hasValue())
+ return tryLoadDatabaseFromPath(CompileCommandsDir.getValue());
+
+ for (auto Path = path::parent_path(File); !Path.empty();
+ Path = path::parent_path(Path)) {
+ auto CDB = tryLoadDatabaseFromPath(Path);
if (!CDB)
continue;
- // FIXME(ibiryukov): Invalidate cached compilation databases on changes
- auto Result = CDB.get();
- CompilationDatabases.insert(std::make_pair(Path, std::move(CDB)));
- return Result;
+ return CDB;
}
- Logger.log("Failed to find compilation database for " + Twine(File) + "\n");
return nullptr;
}
Index: clangd/ClangdLSPServer.h
===================================================================
--- clangd/ClangdLSPServer.h
+++ clangd/ClangdLSPServer.h
@@ -28,7 +28,8 @@
public:
ClangdLSPServer(JSONOutput &Out, unsigned AsyncThreadsCount,
bool SnippetCompletions,
- llvm::Optional<StringRef> ResourceDir);
+ llvm::Optional<StringRef> ResourceDir,
+ llvm::Optional<Path> CompileCommandsDir);
/// Run LSP server loop, receiving input for it from \p In. \p In must be
/// opened in binary mode. Output will be written using Out variable passed to
Index: clangd/ClangdLSPServer.cpp
===================================================================
--- clangd/ClangdLSPServer.cpp
+++ clangd/ClangdLSPServer.cpp
@@ -222,8 +222,9 @@
ClangdLSPServer::ClangdLSPServer(JSONOutput &Out, unsigned AsyncThreadsCount,
bool SnippetCompletions,
- llvm::Optional<StringRef> ResourceDir)
- : Out(Out), CDB(/*Logger=*/Out), DiagConsumer(*this),
+ llvm::Optional<StringRef> ResourceDir,
+ llvm::Optional<Path> CompileCommandsDir)
+ : Out(Out), CDB(Out, CompileCommandsDir), DiagConsumer(*this),
Server(CDB, DiagConsumer, FSProvider, AsyncThreadsCount,
SnippetCompletions, /*Logger=*/Out, ResourceDir) {}
_______________________________________________
cfe-commits mailing list
[email protected]
http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits