Skip to content

Commit 049e8c9

Browse files
committed
Add a unit test, try to fix tests failing in CI
1 parent dccd14c commit 049e8c9

File tree

10 files changed

+38
-6
lines changed

10 files changed

+38
-6
lines changed

clang/tools/driver/cc1as_main.cpp

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

419419
static bool ExecuteAssemblerImpl(AssemblerInvocation &Opts,
420-
DiagnosticsEngine &Diags) {
420+
DiagnosticsEngine &Diags,
421+
IntrusiveRefCntPtr<vfs::FileSystem> VFS) {
421422
// Get the target specific parser.
422423
std::string Error;
423424
const Target *TheTarget = TargetRegistry::lookupTarget(Opts.Triple, Error);
@@ -440,6 +441,7 @@ static bool ExecuteAssemblerImpl(AssemblerInvocation &Opts,
440441
// Record the location of the include directories so that the lexer can find
441442
// it later.
442443
SrcMgr.setIncludeDirs(Opts.IncludePaths);
444+
SrcMgr.setVirtualFileSystem(VFS);
443445

444446
std::unique_ptr<MCRegisterInfo> MRI(TheTarget->createMCRegInfo(Opts.Triple));
445447
assert(MRI && "Unable to create target register info!");
@@ -632,8 +634,9 @@ static bool ExecuteAssemblerImpl(AssemblerInvocation &Opts,
632634
}
633635

634636
static bool ExecuteAssembler(AssemblerInvocation &Opts,
635-
DiagnosticsEngine &Diags) {
636-
bool Failed = ExecuteAssemblerImpl(Opts, Diags);
637+
DiagnosticsEngine &Diags,
638+
IntrusiveRefCntPtr<vfs::FileSystem> VFS) {
639+
bool Failed = ExecuteAssemblerImpl(Opts, Diags, VFS);
637640

638641
// Delete output file if there were errors.
639642
if (Failed) {
@@ -714,7 +717,7 @@ int cc1as_main(ArrayRef<const char *> Argv, const char *Argv0, void *MainAddr) {
714717
}
715718

716719
// Execute the invocation, unless there were parsing errors.
717-
bool Failed = Diags.hasErrorOccurred() || ExecuteAssembler(Asm, Diags);
720+
bool Failed = Diags.hasErrorOccurred() || ExecuteAssembler(Asm, Diags, VFS);
718721

719722
// If any timers were active but haven't been destroyed yet, print their
720723
// 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/SmallSet.h"
2122
#include "llvm/ADT/SmallVector.h"
@@ -87,6 +88,10 @@ namespace remarks {
8788
class RemarkStreamer;
8889
}
8990

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

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

llvm/lib/CodeGen/AsmPrinter/AsmPrinter.cpp

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

466467
bool AsmPrinter::doInitialization(Module &M) {
468+
VFS = vfs::getRealFileSystem();
467469
auto *MMIWP = getAnalysisIfAvailable<MachineModuleInfoWrapperPass>();
468470
MMI = MMIWP ? &MMIWP->getMMI() : nullptr;
469471
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/TableGen/Main.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,6 @@ int llvm::TableGenMain(const char *argv0,
129129
// Record the location of the include directory so that the lexer can find
130130
// it later.
131131
SrcMgr.setIncludeDirs(IncludeDirs);
132-
133132
SrcMgr.setVirtualFileSystem(vfs::getRealFileSystem());
134133

135134
TGParser Parser(SrcMgr, MacroNames, Records, NoWarnOnUnusedTemplateArgs);

llvm/lib/TableGen/Parser.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,8 @@ bool llvm::TableGenParseFile(SourceMgr &InputSrcMgr, RecordKeeper &Records) {
2121
// this reliance, we could drop all of this.
2222
SrcMgr = SourceMgr();
2323
SrcMgr.takeSourceBuffersFrom(InputSrcMgr);
24-
SrcMgr.setVirtualFileSystem(InputSrcMgr.getVirtualFileSystem());
2524
SrcMgr.setIncludeDirs(InputSrcMgr.getIncludeDirs());
25+
SrcMgr.setVirtualFileSystem(InputSrcMgr.getVirtualFileSystem());
2626
SrcMgr.setDiagHandler(InputSrcMgr.getDiagHandler(),
2727
InputSrcMgr.getDiagContext());
2828

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
@@ -40,6 +40,7 @@
4040
#include "llvm/Support/TargetSelect.h"
4141
#include "llvm/Support/TimeProfiler.h"
4242
#include "llvm/Support/ToolOutputFile.h"
43+
#include "llvm/Support/VirtualFileSystem.h"
4344
#include "llvm/Support/WithColor.h"
4445
#include "llvm/TargetParser/Host.h"
4546
#include <memory>
@@ -439,6 +440,7 @@ int main(int argc, char **argv) {
439440
// Record the location of the include directories so that the lexer can find
440441
// it later.
441442
SrcMgr.setIncludeDirs(IncludeDirs);
443+
SrcMgr.setVirtualFileSystem(vfs::getRealFileSystem());
442444

443445
std::unique_ptr<MCRegisterInfo> MRI(TheTarget->createMCRegInfo(TheTriple));
444446
assert(MRI && "Unable to create target register info!");

llvm/tools/llvm-ml/llvm-ml.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 <ctime>
@@ -313,6 +314,7 @@ int llvm_ml_main(int Argc, char **Argv, const llvm::ToolContext &) {
313314
}
314315
}
315316
SrcMgr.setIncludeDirs(IncludeDirs);
317+
SrcMgr.setVirtualFileSystem(vfs::getRealFileSystem());
316318

317319
std::unique_ptr<MCRegisterInfo> MRI(TheTarget->createMCRegInfo(TheTriple));
318320
assert(MRI && "Unable to create target register info!");

llvm/unittests/Support/SourceMgrTest.cpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
#include "llvm/Support/SourceMgr.h"
1010
#include "llvm/Support/MemoryBuffer.h"
11+
#include "llvm/Support/VirtualFileSystem.h"
1112
#include "llvm/Support/raw_ostream.h"
1213
#include "gtest/gtest.h"
1314

@@ -506,3 +507,14 @@ TEST_F(SourceMgrTest, PrintWithoutLoc) {
506507
Diag.print(nullptr, OS, false, false, false);
507508
EXPECT_EQ("message\n", Output);
508509
}
510+
511+
TEST_F(SourceMgrTest, IncludeDirs) {
512+
auto VFS = makeIntrusiveRefCnt<vfs::InMemoryFileSystem>();
513+
VFS->addFile("include/file", 0, MemoryBuffer::getMemBuffer("contents"));
514+
SM.setVirtualFileSystem(std::move(VFS));
515+
SM.setIncludeDirs({"include"});
516+
std::string ResolvedPath;
517+
unsigned NumBuffers = SM.AddIncludeFile("file", SMLoc(), ResolvedPath);
518+
EXPECT_EQ(NumBuffers, 1u);
519+
EXPECT_EQ(ResolvedPath, "include/file");
520+
}

0 commit comments

Comments
 (0)