Index: clangd/ClangdLSPServer.h =================================================================== --- clangd/ClangdLSPServer.h +++ clangd/ClangdLSPServer.h @@ -25,7 +25,7 @@ /// dispatch and ClangdServer together. class ClangdLSPServer { public: - ClangdLSPServer(JSONOutput &Out, bool RunSynchronously); + ClangdLSPServer(JSONOutput &Out, bool RunSynchronously, 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 @@ -216,9 +216,9 @@ R"(,"result":[)" + Locations + R"(]})"); } -ClangdLSPServer::ClangdLSPServer(JSONOutput &Out, bool RunSynchronously) - : Out(Out), DiagConsumer(*this), - Server(CDB, DiagConsumer, FSProvider, RunSynchronously) {} +ClangdLSPServer::ClangdLSPServer(JSONOutput &Out, bool RunSynchronously, Path CompileCommandsDir) + : Out(Out), CDB(CompileCommandsDir), DiagConsumer(*this), + Server(CDB, DiagConsumer, FSProvider, RunSynchronously, CompileCommandsDir) {} void ClangdLSPServer::run(std::istream &In) { assert(!IsDone && "Run was called before"); Index: clangd/ClangdServer.h =================================================================== --- clangd/ClangdServer.h +++ clangd/ClangdServer.h @@ -156,7 +156,7 @@ /// location, obtained via CompilerInvocation::GetResourcePath. ClangdServer(GlobalCompilationDatabase &CDB, DiagnosticsConsumer &DiagConsumer, - FileSystemProvider &FSProvider, bool RunSynchronously, + FileSystemProvider &FSProvider, bool RunSynchronously, llvm::Optional CompileCommandsDir, llvm::Optional ResourceDir = llvm::None); /// Add a \p File to the list of tracked C++ files or update the contents if Index: clangd/ClangdServer.cpp =================================================================== --- clangd/ClangdServer.cpp +++ clangd/ClangdServer.cpp @@ -147,6 +147,7 @@ DiagnosticsConsumer &DiagConsumer, FileSystemProvider &FSProvider, bool RunSynchronously, + llvm::Optional CompileCommandsDir, llvm::Optional ResourceDir) : CDB(CDB), DiagConsumer(DiagConsumer), FSProvider(FSProvider), ResourceDir(ResourceDir ? ResourceDir->str() : getStandardResourceDir()), Index: clangd/GlobalCompilationDatabase.h =================================================================== --- clangd/GlobalCompilationDatabase.h +++ clangd/GlobalCompilationDatabase.h @@ -33,9 +33,9 @@ public: virtual ~GlobalCompilationDatabase() = default; - virtual std::vector + virtual std::vector getCompileCommands(PathRef File) = 0; - + /// FIXME(ibiryukov): add facilities to track changes to compilation flags of /// existing targets. }; @@ -45,13 +45,16 @@ class DirectoryBasedGlobalCompilationDatabase : public GlobalCompilationDatabase { public: + DirectoryBasedGlobalCompilationDatabase(Path NewCompileCommandsDir): CompileCommandsDir(NewCompileCommandsDir){} std::vector getCompileCommands(PathRef File) override; void setExtraFlagsForFile(PathRef File, std::vector ExtraFlags); + tooling::CompilationDatabase *tryLoadDatabaseFromPath(PathRef File, std::string &Error); private: tooling::CompilationDatabase *getCompilationDatabase(PathRef File); + Path CompileCommandsDir; std::mutex Mutex; /// Caches compilation databases loaded from directories(keys are Index: clangd/GlobalCompilationDatabase.cpp =================================================================== --- clangd/GlobalCompilationDatabase.cpp +++ clangd/GlobalCompilationDatabase.cpp @@ -61,37 +61,47 @@ ExtraFlagsForFile[File] = std::move(ExtraFlags); } +tooling::CompilationDatabase * DirectoryBasedGlobalCompilationDatabase::tryLoadDatabaseFromPath(PathRef File, std::string &Error) +{ + auto CachedIt = CompilationDatabases.find(File); + 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; + } + return nullptr; +} + tooling::CompilationDatabase * DirectoryBasedGlobalCompilationDatabase::getCompilationDatabase(PathRef File) { std::lock_guard Lock(Mutex); - + namespace path = llvm::sys::path; - - assert((path::is_absolute(File, path::Style::posix) || - path::is_absolute(File, path::Style::windows)) && - "path must be absolute"); + std::string Error; + if (!CompileCommandsDir.empty()) { + File = CompileCommandsDir; + File = path::parent_path(File); + auto CDB = tryLoadDatabaseFromPath(File, Error); + + if (CDB && Error.empty()) + return CDB; + else + return nullptr; + } for (auto Path = path::parent_path(File); !Path.empty(); - Path = path::parent_path(Path)) { - - auto CachedIt = CompilationDatabases.find(Path); - if (CachedIt != CompilationDatabases.end()) - return CachedIt->second.get(); - std::string Error; - auto CDB = tooling::CompilationDatabase::loadFromDirectory(Path, Error); - if (!CDB) { - if (!Error.empty()) { - // FIXME(ibiryukov): logging - // Output.log("Error when trying to load compilation database from " + - // Twine(Path) + ": " + Twine(Error) + "\n"); - } + Path = path::parent_path(Path)) { + Error = ""; + auto CDB = tryLoadDatabaseFromPath(Path, Error); + if (!CDB || !Error.empty()) 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; } // FIXME(ibiryukov): logging Index: clangd/tool/ClangdMain.cpp =================================================================== --- clangd/tool/ClangdMain.cpp +++ clangd/tool/ClangdMain.cpp @@ -12,7 +12,7 @@ #include "llvm/Support/CommandLine.h" #include "llvm/Support/FileSystem.h" #include "llvm/Support/Program.h" - +#include "llvm/Support/Path.h" #include #include #include @@ -25,6 +25,11 @@ llvm::cl::desc("parse on main thread"), llvm::cl::init(false), llvm::cl::Hidden); +static llvm::cl::opt + 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.")); + + int main(int argc, char *argv[]) { llvm::cl::ParseCommandLineOptions(argc, argv, "clangd"); @@ -32,9 +37,24 @@ llvm::raw_ostream &Logs = llvm::errs(); JSONOutput Out(Outs, Logs); + // If --CompileCommandsDir arg was invoked, check value and override default path. + namespace path = llvm::sys::path; + + if (!llvm::sys::path::is_absolute(CompileCommandsDir)) + { + Logs << "Path specified by --compile-commands-dir must be an absolute path. The argument will be ignored.\n"; + CompileCommandsDir = ""; + } + + if (!llvm::sys::fs::exists(CompileCommandsDir)) + { + Logs << "File does not exist. The argument will be ignored.\n"; + CompileCommandsDir = ""; + } + // Change stdin to binary to not lose \r\n on windows. llvm::sys::ChangeStdinToBinary(); - ClangdLSPServer LSPServer(Out, RunSynchronously); + ClangdLSPServer LSPServer(Out, RunSynchronously, CompileCommandsDir); LSPServer.run(std::cin); }