[PATCH] D89529: [clangd][remote] Add Windows paths support

Aleksandr Platonov via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Fri Oct 16 02:48:54 PDT 2020


ArcsinX created this revision.
Herald added subscribers: cfe-commits, usaxena95, kadircet, arphaman.
Herald added a project: clang.
ArcsinX requested review of this revision.
Herald added subscribers: MaskRay, ilya-biryukov.

Without this patch 7 marshalling tests fails on Windows.
This patch contains the following changes:

- Allow paths with Windows slashes.
- Add support for URI with Windows path.
- Change the value of the second parameter of several `llvm::sys::path::convert_to_slash()` calls: We should use `windows` instead of `posix` to ensure UNIX slashes in the path.
- Remove part of `RemoteMarshallingTest::IncludeHeaderURI` test which could not be ported on Windows.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D89529

Files:
  clang-tools-extra/clangd/index/remote/marshalling/Marshalling.cpp
  clang-tools-extra/clangd/unittests/remote/MarshallingTests.cpp


Index: clang-tools-extra/clangd/unittests/remote/MarshallingTests.cpp
===================================================================
--- clang-tools-extra/clangd/unittests/remote/MarshallingTests.cpp
+++ clang-tools-extra/clangd/unittests/remote/MarshallingTests.cpp
@@ -239,10 +239,6 @@
 
   clangd::Symbol::IncludeHeaderWithReferences Header;
   // Add only valid headers.
-  Header.IncludeHeader = Strings.save(
-      URI::createFile("/usr/local/user/home/project/Header.h").toString());
-  Header.References = 21;
-  Sym.IncludeHeaders.push_back(Header);
   Header.IncludeHeader = Strings.save("<iostream>");
   Header.References = 100;
   Sym.IncludeHeaders.push_back(Header);
@@ -250,7 +246,7 @@
   Header.References = 200;
   Sym.IncludeHeaders.push_back(Header);
 
-  Marshaller ProtobufMarshaller(convert_to_slash("/"), convert_to_slash("/"));
+  Marshaller ProtobufMarshaller(testPath(""), testPath(""));
 
   auto Serialized = ProtobufMarshaller.toProtobuf(Sym);
   ASSERT_TRUE(bool(Serialized));
Index: clang-tools-extra/clangd/index/remote/marshalling/Marshalling.cpp
===================================================================
--- clang-tools-extra/clangd/index/remote/marshalling/Marshalling.cpp
+++ clang-tools-extra/clangd/index/remote/marshalling/Marshalling.cpp
@@ -52,17 +52,20 @@
     : Strings(Arena) {
   if (!RemoteIndexRoot.empty()) {
     assert(llvm::sys::path::is_absolute(RemoteIndexRoot));
-    assert(RemoteIndexRoot ==
-           llvm::sys::path::convert_to_slash(RemoteIndexRoot));
-    this->RemoteIndexRoot = RemoteIndexRoot.str();
-    if (!RemoteIndexRoot.endswith(llvm::sys::path::get_separator()))
+    llvm::SmallString<64> NativePath;
+    llvm::sys::path::native(RemoteIndexRoot, NativePath);
+    this->RemoteIndexRoot = NativePath.str().str();
+    llvm::StringRef Path(*this->RemoteIndexRoot);
+    if (!Path.endswith(llvm::sys::path::get_separator()))
       *this->RemoteIndexRoot += llvm::sys::path::get_separator();
   }
   if (!LocalIndexRoot.empty()) {
     assert(llvm::sys::path::is_absolute(LocalIndexRoot));
-    assert(LocalIndexRoot == llvm::sys::path::convert_to_slash(LocalIndexRoot));
-    this->LocalIndexRoot = LocalIndexRoot.str();
-    if (!LocalIndexRoot.endswith(llvm::sys::path::get_separator()))
+    llvm::SmallString<64> NativePath;
+    llvm::sys::path::native(LocalIndexRoot, NativePath);
+    this->LocalIndexRoot = NativePath.str().str();
+    llvm::StringRef Path(*this->LocalIndexRoot);
+    if (!Path.endswith(llvm::sys::path::get_separator()))
       *this->LocalIndexRoot += llvm::sys::path::get_separator();
   }
   assert(!RemoteIndexRoot.empty() || !LocalIndexRoot.empty());
@@ -92,6 +95,7 @@
   for (const auto &Path : Message->proximity_paths()) {
     llvm::SmallString<256> LocalPath = llvm::StringRef(*RemoteIndexRoot);
     llvm::sys::path::append(LocalPath, Path);
+    llvm::sys::path::native(LocalPath);
     Result.ProximityPaths.push_back(std::string(LocalPath));
   }
   for (const auto &Type : Message->preferred_types())
@@ -209,7 +213,7 @@
     llvm::SmallString<256> RelativePath = llvm::StringRef(Path);
     if (llvm::sys::path::replace_path_prefix(RelativePath, *LocalIndexRoot, ""))
       RPCRequest.add_proximity_paths(llvm::sys::path::convert_to_slash(
-          RelativePath, llvm::sys::path::Style::posix));
+          RelativePath, llvm::sys::path::Style::windows));
   }
   for (const auto &Type : From.PreferredTypes)
     RPCRequest.add_preferred_types(Type);
@@ -315,12 +319,19 @@
   if (ParsedURI->scheme() != "file")
     return error("Can not use URI schemes other than file, given: '{0}'.", URI);
   llvm::SmallString<256> Result = ParsedURI->body();
+  llvm::StringRef Path(Result);
+  // Check for Windows paths (URI=file:///X:/path => Body=/X:/path)
+  if (llvm::sys::path::is_absolute(Path.substr(1),
+                                   llvm::sys::path::Style::windows)) {
+    Result = Path.drop_front();
+    llvm::sys::path::native(Result);
+  }
   if (!llvm::sys::path::replace_path_prefix(Result, *RemoteIndexRoot, ""))
     return error("File path '{0}' doesn't start with '{1}'.", Result.str(),
                  *RemoteIndexRoot);
   // Make sure the result has UNIX slashes.
   return llvm::sys::path::convert_to_slash(Result,
-                                           llvm::sys::path::Style::posix);
+                                           llvm::sys::path::Style::windows);
 }
 
 clangd::SymbolLocation::Position


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D89529.298573.patch
Type: text/x-patch
Size: 4465 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20201016/8d82a772/attachment.bin>


More information about the cfe-commits mailing list