Author: kadir çetinkaya Date: 2024-06-14T16:07:42+02:00 New Revision: 8e0ba08448d5935281e5afd007664d528dd672c4
URL: https://github.com/llvm/llvm-project/commit/8e0ba08448d5935281e5afd007664d528dd672c4 DIFF: https://github.com/llvm/llvm-project/commit/8e0ba08448d5935281e5afd007664d528dd672c4.diff LOG: [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. Added: Modified: clang/lib/Lex/HeaderSearch.cpp clang/unittests/Lex/HeaderSearchTest.cpp Removed: ################################################################################ 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\\"); _______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits