aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorkadir çetinkaya <kadircet@google.com>2024-06-14 16:07:42 +0200
committerGitHub <noreply@github.com>2024-06-14 16:07:42 +0200
commit8e0ba08448d5935281e5afd007664d528dd672c4 (patch)
treef6ada8e53ecb7f4d567e0a4a3973d7c9b53d75eb
parent094572701dce4aaf36f4521d6cf750420d39f206 (diff)
downloadllvm-8e0ba08448d5935281e5afd007664d528dd672c4.zip
llvm-8e0ba08448d5935281e5afd007664d528dd672c4.tar.gz
llvm-8e0ba08448d5935281e5afd007664d528dd672c4.tar.bz2
[clang][HeaderSearch] Fix handling of relative file-paths in suggestPathToFileForDiagnostics (#95121)
Normalize header-to-be-spelled using WorkingDir, similar to search paths themselves. Addresses https://github.com/llvm/llvm-project/issues/81215.
-rw-r--r--clang/lib/Lex/HeaderSearch.cpp2
-rw-r--r--clang/unittests/Lex/HeaderSearchTest.cpp15
2 files changed, 17 insertions, 0 deletions
diff --git a/clang/lib/Lex/HeaderSearch.cpp b/clang/lib/Lex/HeaderSearch.cpp
index 574723b..d6da6c2 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 c578fa7..a5f193e 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\\");