[PATCH] D53481: [clangd] Support passing a relative path to -compile-commands-dir

Daan De Meyer via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Sun Oct 21 10:26:33 PDT 2018


DaanDeMeyer created this revision.
DaanDeMeyer added reviewers: clang-tools-extra, sammccall.
DaanDeMeyer added a project: clang-tools-extra.
Herald added subscribers: cfe-commits, kadircet, arphaman, jkorous, MaskRay, ioeric, ilya-biryukov.

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).


Repository:
  rCTE Clang Tools Extra

https://reviews.llvm.org/D53481

Files:
  clangd/tool/ClangdMain.cpp


Index: clangd/tool/ClangdMain.cpp
===================================================================
--- clangd/tool/ClangdMain.cpp
+++ clangd/tool/ClangdMain.cpp
@@ -253,14 +253,27 @@
   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";
+  } else if (!sys::fs::exists(CompileCommandsDir)) {
+    errs() << "Path specified by --compile-commands-dir does not exist. The "
+              "argument will be ignored.\n";
     CompileCommandsDirPath = None;
   } else {
-    CompileCommandsDirPath = CompileCommandsDir;
+    // If the compile-commands-dir arg path is absolute, use it directly. If
+    // the path is relative, try to convert it to an absolute path first.
+    if (sys::path::is_absolute(CompileCommandsDir)) {
+      CompileCommandsDirPath = CompileCommandsDir;
+    } else {
+      SmallString<128> Path(CompileCommandsDir);
+      std::error_code EC = sys::fs::make_absolute(Path);
+      if (EC) {
+        errs() << "Error while converting the relative path specified by "
+                  "--compile-commands-dir to an absolute path: "
+               << EC.message() << ". The argument will be ignored.\n";
+        CompileCommandsDirPath = None;
+      } else {
+        CompileCommandsDirPath = Path.str();
+      }
+    }
   }
 
   ClangdServer::Options Opts;


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D53481.170338.patch
Type: text/x-patch
Size: 1639 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20181021/031e7cee/attachment.bin>


More information about the cfe-commits mailing list