File tree Expand file tree Collapse file tree 4 files changed +34
-2
lines changed Expand file tree Collapse file tree 4 files changed +34
-2
lines changed Original file line number Diff line number Diff line change 9
9
#include " clang-include-cleaner/IncludeSpeller.h"
10
10
#include " clang-include-cleaner/Types.h"
11
11
#include " llvm/ADT/SmallVector.h"
12
+ #include " llvm/ADT/StringRef.h"
12
13
#include " llvm/Support/ErrorHandling.h"
13
14
#include " llvm/Support/Registry.h"
14
15
#include < memory>
@@ -30,8 +31,14 @@ class DefaultIncludeSpeller : public IncludeSpeller {
30
31
return Input.H .verbatim ().str ();
31
32
case Header::Physical:
32
33
bool IsAngled = false ;
34
+ std::string WorkingDir;
35
+ if (auto WD = Input.HS .getFileMgr ()
36
+ .getVirtualFileSystem ()
37
+ .getCurrentWorkingDirectory ())
38
+ WorkingDir = *WD;
33
39
std::string FinalSpelling = Input.HS .suggestPathToFileForDiagnostics (
34
- Input.H .physical (), Input.Main ->tryGetRealPathName (), &IsAngled);
40
+ Input.H .resolvedPath (), WorkingDir, Input.Main ->tryGetRealPathName (),
41
+ &IsAngled);
35
42
return IsAngled ? " <" + FinalSpelling + " >" : " \" " + FinalSpelling + " \" " ;
36
43
}
37
44
llvm_unreachable (" Unknown clang::include_cleaner::Header::Kind enum" );
@@ -60,4 +67,4 @@ std::string spellHeader(const IncludeSpeller::Input &Input) {
60
67
return Spelling;
61
68
}
62
69
63
- } // namespace clang::include_cleaner
70
+ } // namespace clang::include_cleaner
Original file line number Diff line number Diff line change @@ -89,6 +89,24 @@ TEST(IncludeSpeller, CanOverrideSystemHeaders) {
89
89
HS, MainFile}));
90
90
}
91
91
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
+
92
110
IncludeSpellingStrategy::Add<DummyIncludeSpeller>
93
111
Speller (" dummy" , " Dummy Include Speller" );
94
112
Original file line number Diff line number Diff line change @@ -49,6 +49,10 @@ struct TestInputs {
49
49
// / Keys are plain filenames ("foo.h"), values are file content.
50
50
llvm::StringMap<std::string> ExtraFiles = {};
51
51
52
+ // / Root of execution, all relative paths in Args/Files are resolved against
53
+ // / this.
54
+ std::string WorkingDir;
55
+
52
56
// / Filename to use for translation unit. A default will be used when empty.
53
57
std::string FileName;
54
58
Original file line number Diff line number Diff line change 13
13
#include " clang/Frontend/TextDiagnostic.h"
14
14
#include " clang/Testing/CommandLineArgs.h"
15
15
#include " llvm/ADT/ScopeExit.h"
16
+ #include " llvm/Support/Error.h"
16
17
#include " llvm/Support/VirtualFileSystem.h"
17
18
18
19
#include " gtest/gtest.h"
@@ -106,6 +107,8 @@ TestAST::TestAST(const TestInputs &In) {
106
107
107
108
// Set up a VFS with only the virtual file visible.
108
109
auto VFS = llvm::makeIntrusiveRefCnt<llvm::vfs::InMemoryFileSystem>();
110
+ if (auto Err = VFS->setCurrentWorkingDirectory (In.WorkingDir ))
111
+ ADD_FAILURE () << " Failed to setWD: " << Err.message ();
109
112
VFS->addFile (Filename, /* ModificationTime=*/ 0 ,
110
113
llvm::MemoryBuffer::getMemBufferCopy (In.Code , Filename));
111
114
for (const auto &Extra : In.ExtraFiles )
You can’t perform that action at this time.
0 commit comments