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;


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;
_______________________________________________
cfe-commits mailing list
cfe-commits@lists.llvm.org
http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to