Skip to content

Commit d5297b7

Browse files
authored
[include-cleaner] Pass WorkingDir to suggestPathToFileForDiagnostics (#95114)
Addresses #81215.
1 parent 9ad102f commit d5297b7

File tree

4 files changed

+34
-2
lines changed

4 files changed

+34
-2
lines changed

clang-tools-extra/include-cleaner/lib/IncludeSpeller.cpp

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
#include "clang-include-cleaner/IncludeSpeller.h"
1010
#include "clang-include-cleaner/Types.h"
1111
#include "llvm/ADT/SmallVector.h"
12+
#include "llvm/ADT/StringRef.h"
1213
#include "llvm/Support/ErrorHandling.h"
1314
#include "llvm/Support/Registry.h"
1415
#include <memory>
@@ -30,8 +31,14 @@ class DefaultIncludeSpeller : public IncludeSpeller {
3031
return Input.H.verbatim().str();
3132
case Header::Physical:
3233
bool IsAngled = false;
34+
std::string WorkingDir;
35+
if (auto WD = Input.HS.getFileMgr()
36+
.getVirtualFileSystem()
37+
.getCurrentWorkingDirectory())
38+
WorkingDir = *WD;
3339
std::string FinalSpelling = Input.HS.suggestPathToFileForDiagnostics(
34-
Input.H.physical(), Input.Main->tryGetRealPathName(), &IsAngled);
40+
Input.H.resolvedPath(), WorkingDir, Input.Main->tryGetRealPathName(),
41+
&IsAngled);
3542
return IsAngled ? "<" + FinalSpelling + ">" : "\"" + FinalSpelling + "\"";
3643
}
3744
llvm_unreachable("Unknown clang::include_cleaner::Header::Kind enum");
@@ -60,4 +67,4 @@ std::string spellHeader(const IncludeSpeller::Input &Input) {
6067
return Spelling;
6168
}
6269

63-
} // namespace clang::include_cleaner
70+
} // namespace clang::include_cleaner

clang-tools-extra/include-cleaner/unittests/IncludeSpellerTest.cpp

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,24 @@ TEST(IncludeSpeller, CanOverrideSystemHeaders) {
8989
HS, MainFile}));
9090
}
9191

92+
TEST(IncludeSpeller, RelativeIncludeSearchPath) {
93+
TestInputs Inputs;
94+
95+
Inputs.WorkingDir = "/root/inner";
96+
Inputs.ExtraArgs.push_back("-I..");
97+
Inputs.ExtraFiles["/root/foo.h"] = "";
98+
TestAST AST{Inputs};
99+
100+
auto &FM = AST.fileManager();
101+
auto &HS = AST.preprocessor().getHeaderSearchInfo();
102+
const auto *MainFile = AST.sourceManager().getFileEntryForID(
103+
AST.sourceManager().getMainFileID());
104+
105+
EXPECT_EQ("\"foo.h\"",
106+
spellHeader(
107+
{Header{*FM.getOptionalFileRef("/root/foo.h")}, HS, MainFile}));
108+
}
109+
92110
IncludeSpellingStrategy::Add<DummyIncludeSpeller>
93111
Speller("dummy", "Dummy Include Speller");
94112

clang/include/clang/Testing/TestAST.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,10 @@ struct TestInputs {
4949
/// Keys are plain filenames ("foo.h"), values are file content.
5050
llvm::StringMap<std::string> ExtraFiles = {};
5151

52+
/// Root of execution, all relative paths in Args/Files are resolved against
53+
/// this.
54+
std::string WorkingDir;
55+
5256
/// Filename to use for translation unit. A default will be used when empty.
5357
std::string FileName;
5458

clang/lib/Testing/TestAST.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
#include "clang/Frontend/TextDiagnostic.h"
1414
#include "clang/Testing/CommandLineArgs.h"
1515
#include "llvm/ADT/ScopeExit.h"
16+
#include "llvm/Support/Error.h"
1617
#include "llvm/Support/VirtualFileSystem.h"
1718

1819
#include "gtest/gtest.h"
@@ -106,6 +107,8 @@ TestAST::TestAST(const TestInputs &In) {
106107

107108
// Set up a VFS with only the virtual file visible.
108109
auto VFS = llvm::makeIntrusiveRefCnt<llvm::vfs::InMemoryFileSystem>();
110+
if (auto Err = VFS->setCurrentWorkingDirectory(In.WorkingDir))
111+
ADD_FAILURE() << "Failed to setWD: " << Err.message();
109112
VFS->addFile(Filename, /*ModificationTime=*/0,
110113
llvm::MemoryBuffer::getMemBufferCopy(In.Code, Filename));
111114
for (const auto &Extra : In.ExtraFiles)

0 commit comments

Comments
 (0)