diff --git a/clang/lib/Lex/HeaderSearch.cpp b/clang/lib/Lex/HeaderSearch.cpp index 574723b33866af..d6da6c2fe6c0e9 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 c578fa72c859e0..a5f193ef37ce8f 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\\");