[clang-tools-extra] [Clangd] Sanitize path before recording into IncludeStructure during buildPreamble (PR #70798)

via cfe-commits cfe-commits at lists.llvm.org
Thu Nov 9 13:27:36 PST 2023


https://github.com/Maddobun updated https://github.com/llvm/llvm-project/pull/70798

>From 0572afa42e4e6ca1d1de0e9df045828552cb4480 Mon Sep 17 00:00:00 2001
From: Leo Zhu <yifu.zhu at microchip.com>
Date: Wed, 8 Nov 2023 11:10:13 -0500
Subject: [PATCH 1/2] Convert URI to uppercase drive letter during parsing

---
 clang-tools-extra/clangd/URI.cpp | 7 +++++--
 1 file changed, 5 insertions(+), 2 deletions(-)

diff --git a/clang-tools-extra/clangd/URI.cpp b/clang-tools-extra/clangd/URI.cpp
index ca65df329aeebf3..4bd4b6db9f51db4 100644
--- a/clang-tools-extra/clangd/URI.cpp
+++ b/clang-tools-extra/clangd/URI.cpp
@@ -165,8 +165,7 @@ std::string URI::toString() const {
     return Result;
   // If authority if empty, we only print body if it starts with "/"; otherwise,
   // the URI is invalid.
-  if (!Authority.empty() || llvm::StringRef(Body).startswith("/"))
-  {
+  if (!Authority.empty() || llvm::StringRef(Body).startswith("/")) {
     Result.append("//");
     percentEncode(Authority, Result);
   }
@@ -192,6 +191,10 @@ llvm::Expected<URI> URI::parse(llvm::StringRef OrigUri) {
     Uri = Uri.substr(Pos);
   }
   U.Body = percentDecode(Uri);
+  if (clang::clangd::isWindowsPath(U.Body)) {
+    Pos = U.Body.find(":");
+    U.Body.at(Pos - 1) = std::toupper(U.Body.at(Pos - 1));
+  }
   return U;
 }
 

>From 5a4b2ed67032a75cec7fde6a3e8c589c13d0fef5 Mon Sep 17 00:00:00 2001
From: Leo Zhu <yifu.zhu at microchip.com>
Date: Thu, 9 Nov 2023 16:27:24 -0500
Subject: [PATCH 2/2] Add check for scheme

---
 clang-tools-extra/clangd/URI.cpp | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/clang-tools-extra/clangd/URI.cpp b/clang-tools-extra/clangd/URI.cpp
index 4bd4b6db9f51db4..e448d1b216d909f 100644
--- a/clang-tools-extra/clangd/URI.cpp
+++ b/clang-tools-extra/clangd/URI.cpp
@@ -191,7 +191,7 @@ llvm::Expected<URI> URI::parse(llvm::StringRef OrigUri) {
     Uri = Uri.substr(Pos);
   }
   U.Body = percentDecode(Uri);
-  if (clang::clangd::isWindowsPath(U.Body)) {
+  if (U.scheme() == "file" && clang::clangd::isWindowsPath(U.Body)) {
     Pos = U.Body.find(":");
     U.Body.at(Pos - 1) = std::toupper(U.Body.at(Pos - 1));
   }



More information about the cfe-commits mailing list