Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 1 commit into from
Jun 14, 2024

Conversation

kadircet
Copy link
Member

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

Addresses #81215.

…athToFileForDiagnostics

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

Addresses llvm#81215.
@kadircet kadircet requested a review from VitaNuo June 11, 2024 14:05
@llvmbot llvmbot added clang Clang issues not falling into any other category clang:frontend Language frontend issues, e.g. anything involving "Sema" labels Jun 11, 2024
@llvmbot
Copy link
Member

llvmbot commented Jun 11, 2024

@llvm/pr-subscribers-clang

Author: kadir çetinkaya (kadircet)

Changes

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

Addresses #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 --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\\");

@kadircet kadircet requested a review from hokein June 14, 2024 09:51
@kadircet kadircet merged commit 8e0ba08 into llvm:main Jun 14, 2024
10 checks passed
@kadircet kadircet deleted the hs_suggest_relative_header branch June 14, 2024 14:07
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
clang:frontend Language frontend issues, e.g. anything involving "Sema" clang Clang issues not falling into any other category
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants