[clang] [clang][HeaderSearch] Fix handling of relative file-paths in suggestPathToFileForDiagnostics (PR #95121)

via cfe-commits cfe-commits at lists.llvm.org
Tue Jun 11 07:06:15 PDT 2024


llvmbot wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-clang

Author: kadir çetinkaya (kadircet)

<details>
<summary>Changes</summary>

Normalize header-to-be-spelled using WorkingDir, similar to search paths
themselves.

Addresses https://github.com/llvm/llvm-project/issues/81215.


---
Full diff: https://github.com/llvm/llvm-project/pull/95121.diff


2 Files Affected:

- (modified) clang/lib/Lex/HeaderSearch.cpp (+2) 
- (modified) clang/unittests/Lex/HeaderSearchTest.cpp (+15) 


``````````diff
diff --git a/clang/lib/Lex/HeaderSearch.cpp b/clang/lib/Lex/HeaderSearch.cpp
index 574723b33866a..d6da6c2fe6c0e 100644
--- a/clang/lib/Lex/HeaderSearch.cpp
+++ b/clang/lib/Lex/HeaderSearch.cpp
@@ -2039,6 +2039,8 @@ std::string HeaderSearch::suggestPathToFileForDiagnostics(
   using namespace llvm::sys;
 
   llvm::SmallString<32> FilePath = File;
+  if (!WorkingDir.empty() && !path::is_absolute(FilePath))
+    fs::make_absolute(WorkingDir, FilePath);
   // remove_dots switches to backslashes on windows as a side-effect!
   // We always want to suggest forward slashes for includes.
   // (not remove_dots(..., posix) as that misparses windows paths).
diff --git a/clang/unittests/Lex/HeaderSearchTest.cpp b/clang/unittests/Lex/HeaderSearchTest.cpp
index c578fa72c859e..a5f193ef37ce8 100644
--- a/clang/unittests/Lex/HeaderSearchTest.cpp
+++ b/clang/unittests/Lex/HeaderSearchTest.cpp
@@ -131,6 +131,21 @@ TEST_F(HeaderSearchTest, Dots) {
             "z");
 }
 
+TEST_F(HeaderSearchTest, RelativeDirs) {
+  ASSERT_FALSE(VFS->setCurrentWorkingDirectory("/root/some/dir"));
+  addSearchDir("..");
+  EXPECT_EQ(
+      Search.suggestPathToFileForDiagnostics("/root/some/foo.h",
+                                             /*WorkingDir=*/"/root/some/dir",
+                                             /*MainFile=*/""),
+      "foo.h");
+  EXPECT_EQ(
+      Search.suggestPathToFileForDiagnostics("../foo.h",
+                                             /*WorkingDir=*/"/root/some/dir",
+                                             /*MainFile=*/""),
+      "foo.h");
+}
+
 #ifdef _WIN32
 TEST_F(HeaderSearchTest, BackSlash) {
   addSearchDir("C:\\x\\y\\");

``````````

</details>


https://github.com/llvm/llvm-project/pull/95121


More information about the cfe-commits mailing list