[clang-tools-extra] r345022 - [clangd] Support passing a relative path to -compile-commands-dir

Sam McCall via cfe-commits cfe-commits at lists.llvm.org
Tue Oct 23 04:54:36 PDT 2018


Author: sammccall
Date: Tue Oct 23 04:54:36 2018
New Revision: 345022

URL: http://llvm.org/viewvc/llvm-project?rev=345022&view=rev
Log:
[clangd] Support passing a relative path to -compile-commands-dir

Summary: This is useful when using clangd with CMake based projects in Visual Studio Code since when using CMake the `compile_commands.json` file is usually located in a `build` subdirectory which isn't a parent directory of the source files. Allowing passing relative paths to -compile-commands-dir allows specifying `clangd.arguments = ["-compile-commands-dir=build"]` in VSCode's settings file and having it work for each CMake based project that uses the `build` subdirectory as the build directory (instead of having to specify the absolute path to the compile commands directory for each separate project in VSCode's settings).

Patch by Daan De Meyer!

Reviewers: sammccall

Reviewed By: sammccall

Subscribers: ilya-biryukov, ioeric, MaskRay, jkorous, arphaman, kadircet, cfe-commits

Tags: #clang-tools-extra

Differential Revision: https://reviews.llvm.org/D53481

Modified:
    clang-tools-extra/trunk/clangd/tool/ClangdMain.cpp

Modified: clang-tools-extra/trunk/clangd/tool/ClangdMain.cpp
URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/clangd/tool/ClangdMain.cpp?rev=345022&r1=345021&r2=345022&view=diff
==============================================================================
--- clang-tools-extra/trunk/clangd/tool/ClangdMain.cpp (original)
+++ clang-tools-extra/trunk/clangd/tool/ClangdMain.cpp Tue Oct 23 04:54:36 2018
@@ -251,16 +251,24 @@ int main(int argc, char *argv[]) {
   // If --compile-commands-dir arg was invoked, check value and override default
   // path.
   Optional<Path> CompileCommandsDirPath;
-  if (CompileCommandsDir.empty()) {
-    CompileCommandsDirPath = None;
-  } else if (!sys::path::is_absolute(CompileCommandsDir) ||
-             !sys::fs::exists(CompileCommandsDir)) {
-    errs() << "Path specified by --compile-commands-dir either does not "
-              "exist or is not an absolute "
-              "path. The argument will be ignored.\n";
-    CompileCommandsDirPath = None;
-  } else {
-    CompileCommandsDirPath = CompileCommandsDir;
+  if (!CompileCommandsDir.empty()) {
+    if (sys::fs::exists(CompileCommandsDir)) {
+      // We support passing both relative and absolute paths to the
+      // --compile-commands-dir argument, but we assume the path is absolute in
+      // the rest of clangd so we make sure the path is absolute before
+      // continuing.
+      SmallString<128> Path(CompileCommandsDir);
+      if (std::error_code EC = sys::fs::make_absolute(Path)) {
+        errs() << "Error while converting the relative path specified by "
+                  "--compile-commands-dir to an absolute path: "
+               << EC.message() << ". The argument will be ignored.\n";
+      } else {
+        CompileCommandsDirPath = Path.str();
+      }
+    } else {
+      errs() << "Path specified by --compile-commands-dir does not exist. The "
+                "argument will be ignored.\n";
+    }
   }
 
   ClangdServer::Options Opts;




More information about the cfe-commits mailing list