Skip to content

Commit 0a45a4f

Browse files
committed
[support] Use VFS in SourceMgr for loading includes (llvm#162903)
Most `SourceMgr` clients don't make use of include files, but those that do might want to specify the file system to use. This patch enables that by making it possible to pass a `vfs::FileSystem` instance into `SourceMgr`.
1 parent 09a93b4 commit 0a45a4f

File tree

17 files changed

+70
-18
lines changed

17 files changed

+70
-18
lines changed

clang/tools/driver/cc1as_main.cpp

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -429,7 +429,8 @@ getOutputStream(StringRef Path, DiagnosticsEngine &Diags, bool Binary) {
429429
}
430430

431431
static bool ExecuteAssemblerImpl(AssemblerInvocation &Opts,
432-
DiagnosticsEngine &Diags) {
432+
DiagnosticsEngine &Diags,
433+
IntrusiveRefCntPtr<vfs::FileSystem> VFS) {
433434
// Get the target specific parser.
434435
std::string Error;
435436
const Target *TheTarget = TargetRegistry::lookupTarget(Opts.Triple, Error);
@@ -452,6 +453,7 @@ static bool ExecuteAssemblerImpl(AssemblerInvocation &Opts,
452453
// Record the location of the include directories so that the lexer can find
453454
// it later.
454455
SrcMgr.setIncludeDirs(Opts.IncludePaths);
456+
SrcMgr.setVirtualFileSystem(VFS);
455457

456458
std::unique_ptr<MCRegisterInfo> MRI(TheTarget->createMCRegInfo(Opts.Triple));
457459
assert(MRI && "Unable to create target register info!");
@@ -647,8 +649,9 @@ static bool ExecuteAssemblerImpl(AssemblerInvocation &Opts,
647649
}
648650

649651
static bool ExecuteAssembler(AssemblerInvocation &Opts,
650-
DiagnosticsEngine &Diags) {
651-
bool Failed = ExecuteAssemblerImpl(Opts, Diags);
652+
DiagnosticsEngine &Diags,
653+
IntrusiveRefCntPtr<vfs::FileSystem> VFS) {
654+
bool Failed = ExecuteAssemblerImpl(Opts, Diags, VFS);
652655

653656
// Delete output file if there were errors.
654657
if (Failed) {
@@ -730,7 +733,7 @@ int cc1as_main(ArrayRef<const char *> Argv, const char *Argv0, void *MainAddr) {
730733
}
731734

732735
// Execute the invocation, unless there were parsing errors.
733-
bool Failed = Diags.hasErrorOccurred() || ExecuteAssembler(Asm, Diags);
736+
bool Failed = Diags.hasErrorOccurred() || ExecuteAssembler(Asm, Diags, VFS);
734737

735738
// If any timers were active but haven't been destroyed yet, print their
736739
// results now.

llvm/include/llvm/CodeGen/AsmPrinter.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
#define LLVM_CODEGEN_ASMPRINTER_H
1717

1818
#include "llvm/ADT/DenseMap.h"
19+
#include "llvm/ADT/IntrusiveRefCntPtr.h"
1920
#include "llvm/ADT/MapVector.h"
2021
#include "llvm/ADT/SmallVector.h"
2122
#include "llvm/Analysis/ProfileSummaryInfo.h"
@@ -88,6 +89,10 @@ namespace remarks {
8889
class RemarkStreamer;
8990
}
9091

92+
namespace vfs {
93+
class FileSystem;
94+
}
95+
9196
/// This class is intended to be used as a driving class for all asm writers.
9297
class LLVM_ABI AsmPrinter : public MachineFunctionPass {
9398
public:
@@ -106,6 +111,9 @@ class LLVM_ABI AsmPrinter : public MachineFunctionPass {
106111
/// generating (such as the current section etc).
107112
std::unique_ptr<MCStreamer> OutStreamer;
108113

114+
/// The VFS to resolve asm include directives.
115+
IntrusiveRefCntPtr<vfs::FileSystem> VFS;
116+
109117
/// The current machine function.
110118
MachineFunction *MF = nullptr;
111119

llvm/include/llvm/Support/SourceMgr.h

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -96,22 +96,25 @@ class SourceMgr {
9696
DiagHandlerTy DiagHandler = nullptr;
9797
void *DiagContext = nullptr;
9898

99-
// Filesystem for finding includes, if not the real FS.
99+
// Optional file system for finding include files.
100100
IntrusiveRefCntPtr<vfs::FileSystem> FS;
101101

102102
bool isValidBufferID(unsigned i) const { return i && i <= Buffers.size(); }
103103

104104
public:
105+
/// Create new source manager without support for include files.
105106
SourceMgr();
107+
/// Create new source manager with the capability of finding include files
108+
/// via the provided file system.
109+
explicit SourceMgr(IntrusiveRefCntPtr<vfs::FileSystem> FS);
106110
SourceMgr(const SourceMgr &) = delete;
107111
SourceMgr &operator=(const SourceMgr &) = delete;
108112
SourceMgr(SourceMgr &&);
109113
SourceMgr &operator=(SourceMgr &&);
110114
~SourceMgr();
111115

112-
/// Specify a \a vfs::FileSystem for finding includes.
113-
explicit SourceMgr(IntrusiveRefCntPtr<vfs::FileSystem> FS);
114-
void setFileSystem(IntrusiveRefCntPtr<vfs::FileSystem> FS);
116+
IntrusiveRefCntPtr<vfs::FileSystem> getVirtualFileSystem() const;
117+
void setVirtualFileSystem(IntrusiveRefCntPtr<vfs::FileSystem> FS);
115118

116119
/// Return the include directories of this source manager.
117120
ArrayRef<std::string> getIncludeDirs() const { return IncludeDirectories; }

llvm/lib/CodeGen/AsmPrinter/AsmPrinter.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,7 @@
117117
#include "llvm/Support/MathExtras.h"
118118
#include "llvm/Support/Path.h"
119119
#include "llvm/Support/VCSRevision.h"
120+
#include "llvm/Support/VirtualFileSystem.h"
120121
#include "llvm/Support/raw_ostream.h"
121122
#include "llvm/Target/TargetLoweringObjectFile.h"
122123
#include "llvm/Target/TargetMachine.h"
@@ -463,6 +464,7 @@ void AsmPrinter::getAnalysisUsage(AnalysisUsage &AU) const {
463464
}
464465

465466
bool AsmPrinter::doInitialization(Module &M) {
467+
VFS = vfs::getRealFileSystem();
466468
auto *MMIWP = getAnalysisIfAvailable<MachineModuleInfoWrapperPass>();
467469
MMI = MMIWP ? &MMIWP->getMMI() : nullptr;
468470
HasSplitStack = false;

llvm/lib/CodeGen/AsmPrinter/AsmPrinterInlineAsm.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
#include "llvm/Support/ErrorHandling.h"
3737
#include "llvm/Support/MemoryBuffer.h"
3838
#include "llvm/Support/SourceMgr.h"
39+
#include "llvm/Support/VirtualFileSystem.h"
3940
#include "llvm/Support/raw_ostream.h"
4041
#include "llvm/Target/TargetMachine.h"
4142
using namespace llvm;
@@ -98,6 +99,7 @@ void AsmPrinter::emitInlineAsm(StringRef Str, const MCSubtargetInfo &STI,
9899
unsigned BufNum = addInlineAsmDiagBuffer(Str, LocMDNode);
99100
SourceMgr &SrcMgr = *MMI->getContext().getInlineSourceManager();
100101
SrcMgr.setIncludeDirs(MCOptions.IASSearchPaths);
102+
SrcMgr.setVirtualFileSystem(VFS);
101103

102104
std::unique_ptr<MCAsmParser> Parser(
103105
createMCAsmParser(SrcMgr, OutContext, *OutStreamer, *MAI, BufNum));

llvm/lib/Support/SourceMgr.cpp

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -41,14 +41,17 @@ static const size_t TabStop = 8;
4141

4242
// Out of line to avoid needing definition of vfs::FileSystem in header.
4343
SourceMgr::SourceMgr() = default;
44-
SourceMgr::~SourceMgr() = default;
44+
SourceMgr::SourceMgr(IntrusiveRefCntPtr<vfs::FileSystem> FS)
45+
: FS(std::move(FS)) {}
4546
SourceMgr::SourceMgr(SourceMgr &&) = default;
4647
SourceMgr &SourceMgr::operator=(SourceMgr &&) = default;
48+
SourceMgr::~SourceMgr() = default;
4749

48-
SourceMgr::SourceMgr(IntrusiveRefCntPtr<vfs::FileSystem> FS)
49-
: FS(std::move(FS)) {}
50+
IntrusiveRefCntPtr<vfs::FileSystem> SourceMgr::getVirtualFileSystem() const {
51+
return FS;
52+
}
5053

51-
void SourceMgr::setFileSystem(IntrusiveRefCntPtr<vfs::FileSystem> FS) {
54+
void SourceMgr::setVirtualFileSystem(IntrusiveRefCntPtr<vfs::FileSystem> FS) {
5255
this->FS = std::move(FS);
5356
}
5457

@@ -66,18 +69,19 @@ unsigned SourceMgr::AddIncludeFile(const std::string &Filename,
6669
ErrorOr<std::unique_ptr<MemoryBuffer>>
6770
SourceMgr::OpenIncludeFile(const std::string &Filename,
6871
std::string &IncludedFile) {
69-
auto getFile = [this](StringRef Path) {
72+
auto GetFile = [this](StringRef Path) {
7073
return FS ? FS->getBufferForFile(Path) : MemoryBuffer::getFile(Path);
7174
};
72-
ErrorOr<std::unique_ptr<MemoryBuffer>> NewBufOrErr = getFile(Filename);
75+
76+
ErrorOr<std::unique_ptr<MemoryBuffer>> NewBufOrErr = GetFile(Filename);
7377

7478
SmallString<64> Buffer(Filename);
7579
// If the file didn't exist directly, see if it's in an include path.
7680
for (unsigned i = 0, e = IncludeDirectories.size(); i != e && !NewBufOrErr;
7781
++i) {
7882
Buffer = IncludeDirectories[i];
7983
sys::path::append(Buffer, Filename);
80-
NewBufOrErr = getFile(Buffer);
84+
NewBufOrErr = GetFile(Buffer);
8185
}
8286

8387
if (NewBufOrErr)

llvm/lib/TableGen/Main.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
#include "llvm/Support/SMLoc.h"
2727
#include "llvm/Support/SourceMgr.h"
2828
#include "llvm/Support/ToolOutputFile.h"
29+
#include "llvm/Support/VirtualFileSystem.h"
2930
#include "llvm/Support/raw_ostream.h"
3031
#include "llvm/TableGen/Error.h"
3132
#include "llvm/TableGen/Record.h"
@@ -130,6 +131,7 @@ int llvm::TableGenMain(const char *argv0,
130131
// Record the location of the include directory so that the lexer can find
131132
// it later.
132133
SrcMgr.setIncludeDirs(IncludeDirs);
134+
SrcMgr.setVirtualFileSystem(vfs::getRealFileSystem());
133135

134136
TGParser Parser(SrcMgr, MacroNames, Records, NoWarnOnUnusedTemplateArgs);
135137

llvm/lib/TableGen/Parser.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
#include "llvm/TableGen/Parser.h"
1010
#include "TGParser.h"
1111
#include "llvm/Support/MemoryBuffer.h"
12+
#include "llvm/Support/VirtualFileSystem.h"
1213
#include "llvm/TableGen/Record.h"
1314

1415
using namespace llvm;
@@ -21,6 +22,7 @@ bool llvm::TableGenParseFile(SourceMgr &InputSrcMgr, RecordKeeper &Records) {
2122
SrcMgr = SourceMgr();
2223
SrcMgr.takeSourceBuffersFrom(InputSrcMgr);
2324
SrcMgr.setIncludeDirs(InputSrcMgr.getIncludeDirs());
25+
SrcMgr.setVirtualFileSystem(InputSrcMgr.getVirtualFileSystem());
2426
SrcMgr.setDiagHandler(InputSrcMgr.getDiagHandler(),
2527
InputSrcMgr.getDiagContext());
2628

llvm/tools/llvm-mc-assemble-fuzzer/llvm-mc-assemble-fuzzer.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
#include "llvm/Support/SourceMgr.h"
3232
#include "llvm/Support/TargetSelect.h"
3333
#include "llvm/Support/ToolOutputFile.h"
34+
#include "llvm/Support/VirtualFileSystem.h"
3435
#include "llvm/Support/raw_ostream.h"
3536
#include "llvm/TargetParser/Host.h"
3637
#include "llvm/TargetParser/SubtargetFeature.h"
@@ -142,6 +143,7 @@ int AssembleOneInput(const uint8_t *Data, size_t Size) {
142143

143144
static const std::vector<std::string> NoIncludeDirs;
144145
SrcMgr.setIncludeDirs(NoIncludeDirs);
146+
SrcMgr.setVirtualFileSystem(vfs::getRealFileSystem());
145147

146148
static std::string ArchName;
147149
std::string Error;

llvm/tools/llvm-mc/llvm-mc.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141
#include "llvm/Support/SourceMgr.h"
4242
#include "llvm/Support/TargetSelect.h"
4343
#include "llvm/Support/ToolOutputFile.h"
44+
#include "llvm/Support/VirtualFileSystem.h"
4445
#include "llvm/Support/WithColor.h"
4546
#include "llvm/TargetParser/Host.h"
4647
#include <memory>
@@ -434,6 +435,7 @@ int main(int argc, char **argv) {
434435
// Record the location of the include directories so that the lexer can find
435436
// it later.
436437
SrcMgr.setIncludeDirs(IncludeDirs);
438+
SrcMgr.setVirtualFileSystem(vfs::getRealFileSystem());
437439

438440
std::unique_ptr<MCRegisterInfo> MRI(TheTarget->createMCRegInfo(TripleName));
439441
assert(MRI && "Unable to create target register info!");

0 commit comments

Comments
 (0)