Author: Zeyi Xu
Date: 2026-07-07T10:45:54+08:00
New Revision: 5c715d56884364a3b05acb4d3e6b52a911f14667

URL: 
https://github.com/llvm/llvm-project/commit/5c715d56884364a3b05acb4d3e6b52a911f14667
DIFF: 
https://github.com/llvm/llvm-project/commit/5c715d56884364a3b05acb4d3e6b52a911f14667.diff

LOG: [Tooling] Preserve backslashes in POSIX source paths (#207499)

On POSIX, backslashes are valid filename characters. Avoid converting
them to slashes when computing absolute source paths in LibTooling,
since doing so changes paths such as `a\b.cc` into `a/b.cc`.

Closes #207396

Added: 
    

Modified: 
    clang-tools-extra/docs/ReleaseNotes.rst
    clang/lib/Tooling/Tooling.cpp
    clang/unittests/Tooling/ToolingTest.cpp

Removed: 
    


################################################################################
diff  --git a/clang-tools-extra/docs/ReleaseNotes.rst 
b/clang-tools-extra/docs/ReleaseNotes.rst
index 68f0fa568dbf9..b85ece288881e 100644
--- a/clang-tools-extra/docs/ReleaseNotes.rst
+++ b/clang-tools-extra/docs/ReleaseNotes.rst
@@ -202,6 +202,9 @@ Improvements to clang-tidy
 - Improved :program:`clang-tidy` ``-store-check-profile`` by generating valid
   JSON when the source file path contains characters that require JSON 
escaping.
 
+- Improved :program:`clang-tidy` by preserving literal backslash characters in
+  POSIX source paths.
+
 - Ensured that :program:`clang-tidy` and the clang compiler uses the same logic
   for the suppression of compiler diagnostics in system headers and expansions
   of macros defined in system headers. Previously the default setting of tidy

diff  --git a/clang/lib/Tooling/Tooling.cpp b/clang/lib/Tooling/Tooling.cpp
index 5da9c31ec47ec..71307d1fe7307 100644
--- a/clang/lib/Tooling/Tooling.cpp
+++ b/clang/lib/Tooling/Tooling.cpp
@@ -260,7 +260,7 @@ llvm::Expected<std::string> 
getAbsolutePath(llvm::vfs::FileSystem &FS,
   SmallString<1024> AbsolutePath = RelativePath;
   if (auto EC = FS.makeAbsolute(AbsolutePath))
     return llvm::errorCodeToError(EC);
-  llvm::sys::path::native(AbsolutePath);
+  llvm::sys::path::make_preferred(AbsolutePath);
   return std::string(AbsolutePath);
 }
 

diff  --git a/clang/unittests/Tooling/ToolingTest.cpp 
b/clang/unittests/Tooling/ToolingTest.cpp
index 0f6cfbd2266bb..9b638c2d6c4ee 100644
--- a/clang/unittests/Tooling/ToolingTest.cpp
+++ b/clang/unittests/Tooling/ToolingTest.cpp
@@ -688,6 +688,30 @@ TEST(runToolOnCodeWithArgs, DiagnosticsColor) {
       {"-fcolor-diagnostics"}));
 }
 
+TEST(getAbsolutePath, BackslashPath) {
+  auto FS = llvm::vfs::getRealFileSystem();
+  llvm::Expected<std::string> Path = getAbsolutePath(*FS, "a\\b.cc");
+  if (!Path)
+    FAIL() << llvm::toString(Path.takeError());
+
+  llvm::ErrorOr<std::string> CWD = FS->getCurrentWorkingDirectory();
+  ASSERT_TRUE(CWD) << CWD.getError().message();
+
+#if defined(_WIN32)
+  SmallString<128> Expected(*CWD);
+  llvm::sys::path::append(Expected, "a", "b.cc");
+  llvm::sys::path::make_preferred(Expected);
+  EXPECT_EQ(std::string(Expected), *Path);
+#else
+  SmallString<128> Expected(*CWD);
+  llvm::sys::path::append(Expected, "a\\b.cc");
+  SmallString<128> WithSlash(*CWD);
+  llvm::sys::path::append(WithSlash, "a/b.cc");
+  EXPECT_EQ(std::string(Expected), *Path);
+  EXPECT_NE(std::string(WithSlash), *Path);
+#endif
+}
+
 TEST(ClangToolTest, ArgumentAdjusters) {
   FixedCompilationDatabase Compilations("/", std::vector<std::string>());
 


        
_______________________________________________
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to