Add vfs support to toolchain. (#2888)

This adds vfs support to the toolchain, allowing Driver to take in-memory inputs in tests. As a consequence, I'm simplifying SourceBuffer: rather than allowing tests to pass in their own memory buffer, I'm using InMemoryFileSystem to push for greater consistency with production code. This does hit a quirk where I need to be careful about null terminator handling because fuzzer imports don't always have one, but that's probably more robust anyways.

Co-authored-by: Richard Smith <richard@metafoo.co.uk>
This commit is contained in:
Jon Ross-Perkins
2023-06-12 13:19:57 -07:00
committed by GitHub
co-authored by Richard Smith
parent 2c45cb3be8
commit a93e621488
17 changed files with 217 additions and 367 deletions
+11 -7
View File
@@ -12,7 +12,6 @@
#include "llvm/ADT/ArrayRef.h"
#include "llvm/ADT/Sequence.h"
#include "llvm/ADT/SmallString.h"
#include "llvm/ADT/Twine.h"
#include "llvm/Support/SourceMgr.h"
#include "llvm/Support/raw_ostream.h"
#include "toolchain/common/yaml_test_helpers.h"
@@ -31,19 +30,24 @@ using ::testing::StrEq;
class LexerTest : public ::testing::Test {
protected:
auto GetSourceBuffer(llvm::Twine text) -> SourceBuffer& {
source_storage.push_back(
std::move(*SourceBuffer::CreateFromText(text.str())));
return source_storage.back();
auto GetSourceBuffer(llvm::StringRef text) -> SourceBuffer& {
std::string filename = llvm::formatv("test{0}.carbon", ++file_index);
CARBON_CHECK(fs.addFile(filename, /*ModificationTime=*/0,
llvm::MemoryBuffer::getMemBuffer(text)));
source_storage.push_front(
std::move(*SourceBuffer::CreateFromFile(fs, filename)));
return source_storage.front();
}
auto Lex(llvm::Twine text,
auto Lex(llvm::StringRef text,
DiagnosticConsumer& consumer = ConsoleDiagnosticConsumer())
-> TokenizedBuffer {
return TokenizedBuffer::Lex(GetSourceBuffer(text), consumer);
}
llvm::SmallVector<SourceBuffer, 16> source_storage;
llvm::vfs::InMemoryFileSystem fs;
int file_index = 0;
std::forward_list<SourceBuffer> source_storage;
};
TEST_F(LexerTest, HandlesEmptyBuffer) {