Change explorer's file_test to support argument passing to main. (#3032)

This shifts explorer's file_test to use ExplorerMain in order to be able
to pass arguments similar to how the command line would. It also means
that various output wrapping done by the command line is shared, no
longer copied by file_test.

In order to help make this work, I plugged vfs::FileSystem into
explorer, similar to how we're doing this on the toolchain side (might
try to share more code later). I was having trouble getting an overlay
working, I think due to how paths are specified -- but due to the issues
in fixing this, I'm just loading the prelude into the InMemoryFilesystem
for now.

Under this approach, I'm unifying more of the file versus string
handling. I think we should shift towards an approach where, like
toolchain code, anyone trying to use Explorer should use a vfs
implementation to supply code. This should reduce the number of distinct
code paths which we're maintaining/testing.

Short-term, this allows the trace testdata to be merged into file_test
with less special casing, using ARGS:. Long-term, it means that the
file_test knows the ARGS to pass to generate checks to match against,
which is the direction I'm planning for autoupdate.
This commit is contained in:
Jon Ross-Perkins
2023-07-28 15:29:51 +00:00
committed by GitHub
parent 3f24aa28b7
commit 9e03d5efe2
18 changed files with 167 additions and 139 deletions
@@ -6,7 +6,6 @@
#include <locale>
#include "common/check.h"
#include "common/error.h"
#include "explorer/common/trace_stream.h"
#include "explorer/interpreter/exec_program.h"
@@ -37,22 +36,22 @@ static auto PrintTimingOnExit(TraceStream* trace_stream, const char* label,
return exit_scope_function;
}
static auto ParseAndExecuteHelper(std::function<ErrorOr<AST>(Arena*)> parse,
std::string_view prelude_path,
Nonnull<TraceStream*> trace_stream,
Nonnull<llvm::raw_ostream*> print_stream)
-> ErrorOr<int> {
auto ParseAndExecute(llvm::vfs::FileSystem& fs, std::string_view prelude_path,
std::string_view input_file_name, bool parser_debug,
Nonnull<TraceStream*> trace_stream,
Nonnull<llvm::raw_ostream*> print_stream) -> ErrorOr<int> {
return RunWithExtraStack([&]() -> ErrorOr<int> {
Arena arena;
auto cursor = std::chrono::steady_clock::now();
ErrorOr<AST> parse_result = parse(&arena);
ErrorOr<AST> parse_result =
Parse(fs, &arena, input_file_name, FileKind::Main, parser_debug);
auto print_parse_time = PrintTimingOnExit(trace_stream, "Parse", &cursor);
if (!parse_result.ok()) {
return ErrorBuilder() << "SYNTAX ERROR: " << parse_result.error();
}
AddPrelude(prelude_path, &arena, &parse_result->declarations,
AddPrelude(fs, prelude_path, &arena, &parse_result->declarations,
&parse_result->num_prelude_declarations);
auto print_prelude_time =
PrintTimingOnExit(trace_stream, "AddPrelude", &cursor);
@@ -87,27 +86,4 @@ static auto ParseAndExecuteHelper(std::function<ErrorOr<AST>(Arena*)> parse,
});
}
auto ParseAndExecuteFile(std::string_view prelude_path,
std::string_view input_file_name, bool parser_debug,
Nonnull<TraceStream*> trace_stream,
Nonnull<llvm::raw_ostream*> print_stream)
-> ErrorOr<int> {
auto parse = [&](Arena* arena) {
return Parse(arena, input_file_name, FileKind::Main, parser_debug);
};
return ParseAndExecuteHelper(parse, prelude_path, trace_stream, print_stream);
}
auto ParseAndExecute(std::string_view prelude_path,
std::string_view input_file_name,
std::string_view file_contents, bool parser_debug,
Nonnull<TraceStream*> trace_stream,
Nonnull<llvm::raw_ostream*> print_stream) -> ErrorOr<int> {
auto parse = [&](Arena* arena) {
return ParseFromString(arena, input_file_name, FileKind::Main,
file_contents, parser_debug);
};
return ParseAndExecuteHelper(parse, prelude_path, trace_stream, print_stream);
}
} // namespace Carbon