[clang] Relative Path Handling for Cross-Platform Compatibility (PR #133526)

Ayush Pareek via cfe-commits cfe-commits at lists.llvm.org
Fri Mar 28 14:56:05 PDT 2025


https://github.com/ayushpareek2003 created https://github.com/llvm/llvm-project/pull/133526

Both Unix (./, ../) and Windows (.\, ..\) path formats are handled properly

>From 7c70f0c2bf45b34c1134820a21ec169777bce7bf Mon Sep 17 00:00:00 2001
From: Ayush Pareek <AYUSHPAREEK1980 at GMAIL.COM>
Date: Sat, 29 Mar 2025 03:25:39 +0530
Subject: [PATCH] Relative Path Handling for Cross-Platform Compatibility

Both Unix (./, ../) and Windows (.\, ..\) path formats are handled properly
---
 clang/lib/Tooling/Tooling.cpp | 15 +++++++++++++--
 1 file changed, 13 insertions(+), 2 deletions(-)

diff --git a/clang/lib/Tooling/Tooling.cpp b/clang/lib/Tooling/Tooling.cpp
index 03523c3f17eda..51508d123eb32 100644
--- a/clang/lib/Tooling/Tooling.cpp
+++ b/clang/lib/Tooling/Tooling.cpp
@@ -255,8 +255,19 @@ llvm::Expected<std::string> getAbsolutePath(llvm::vfs::FileSystem &FS,
                                             StringRef File) {
   StringRef RelativePath(File);
   // FIXME: Should '.\\' be accepted on Win32?
-  RelativePath.consume_front("./");
-
+  if (RelativePath.startswith("./")) {  
+    // Unix-like relative path
+    RelativePath = RelativePath.drop_front(2);  
+  } else if (RelativePath.startswith(".\\")) {  
+    // Windows-style relative path
+    RelativePath = RelativePath.drop_front(2);  
+  } else if (RelativePath.startswith("../")) {  
+    // Unix-like parent directory reference
+    RelativePath = RelativePath.drop_front(3);  
+  } else if (RelativePath.startswith("..\\")) {  
+    // Windows-style parent directory reference
+    RelativePath = RelativePath.drop_front(3);  
+  }
   SmallString<1024> AbsolutePath = RelativePath;
   if (auto EC = FS.makeAbsolute(AbsolutePath))
     return llvm::errorCodeToError(EC);



More information about the cfe-commits mailing list