|
@@ -17,6 +17,7 @@
|
|
|
#include "common/error.h"
|
|
#include "common/error.h"
|
|
|
#include "explorer/common/trace_stream.h"
|
|
#include "explorer/common/trace_stream.h"
|
|
|
#include "explorer/parse_and_execute/parse_and_execute.h"
|
|
#include "explorer/parse_and_execute/parse_and_execute.h"
|
|
|
|
|
+#include "llvm/ADT/ScopeExit.h"
|
|
|
#include "llvm/ADT/SmallString.h"
|
|
#include "llvm/ADT/SmallString.h"
|
|
|
#include "llvm/Support/CommandLine.h"
|
|
#include "llvm/Support/CommandLine.h"
|
|
|
#include "llvm/Support/FileSystem.h"
|
|
#include "llvm/Support/FileSystem.h"
|
|
@@ -32,7 +33,6 @@ namespace path = llvm::sys::path;
|
|
|
auto ExplorerMain(int argc, char** argv, void* static_for_main_addr,
|
|
auto ExplorerMain(int argc, char** argv, void* static_for_main_addr,
|
|
|
llvm::StringRef relative_prelude_path) -> int {
|
|
llvm::StringRef relative_prelude_path) -> int {
|
|
|
llvm::setBugReportMsg(
|
|
llvm::setBugReportMsg(
|
|
|
-
|
|
|
|
|
"Please report issues to "
|
|
"Please report issues to "
|
|
|
"https://github.com/carbon-language/carbon-lang/issues and include the "
|
|
"https://github.com/carbon-language/carbon-lang/issues and include the "
|
|
|
"crash backtrace.\n");
|
|
"crash backtrace.\n");
|
|
@@ -42,6 +42,20 @@ auto ExplorerMain(int argc, char** argv, void* static_for_main_addr,
|
|
|
// is piped to stdout.
|
|
// is piped to stdout.
|
|
|
llvm::errs().tie(&llvm::outs());
|
|
llvm::errs().tie(&llvm::outs());
|
|
|
|
|
|
|
|
|
|
+ std::string exe =
|
|
|
|
|
+ llvm::sys::fs::getMainExecutable(argv[0], static_for_main_addr);
|
|
|
|
|
+ llvm::StringRef install_path = path::parent_path(exe);
|
|
|
|
|
+
|
|
|
|
|
+ return ExplorerMain(argc, const_cast<const char**>(argv), install_path,
|
|
|
|
|
+ relative_prelude_path, llvm::outs(), llvm::errs(),
|
|
|
|
|
+ llvm::outs(), *llvm::vfs::getRealFileSystem());
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+auto ExplorerMain(int argc, const char** argv, llvm::StringRef install_path,
|
|
|
|
|
+ llvm::StringRef relative_prelude_path,
|
|
|
|
|
+ llvm::raw_ostream& out_stream, llvm::raw_ostream& err_stream,
|
|
|
|
|
+ llvm::raw_ostream& out_stream_for_trace,
|
|
|
|
|
+ llvm::vfs::FileSystem& fs) -> int {
|
|
|
cl::opt<std::string> input_file_name(cl::Positional, cl::desc("<input file>"),
|
|
cl::opt<std::string> input_file_name(cl::Positional, cl::desc("<input file>"),
|
|
|
cl::Required);
|
|
cl::Required);
|
|
|
cl::opt<bool> parser_debug("parser_debug",
|
|
cl::opt<bool> parser_debug("parser_debug",
|
|
@@ -98,10 +112,9 @@ auto ExplorerMain(int argc, char** argv, void* static_for_main_addr,
|
|
|
"Include trace output for all files")),
|
|
"Include trace output for all files")),
|
|
|
cl::CommaSeparated);
|
|
cl::CommaSeparated);
|
|
|
|
|
|
|
|
|
|
+ CARBON_CHECK(argc > 0);
|
|
|
|
|
+
|
|
|
// Use the executable path as a base for the relative prelude path.
|
|
// Use the executable path as a base for the relative prelude path.
|
|
|
- std::string exe =
|
|
|
|
|
- llvm::sys::fs::getMainExecutable(argv[0], static_for_main_addr);
|
|
|
|
|
- llvm::StringRef install_path = path::parent_path(exe);
|
|
|
|
|
llvm::SmallString<256> default_prelude_file(install_path);
|
|
llvm::SmallString<256> default_prelude_file(install_path);
|
|
|
path::append(default_prelude_file,
|
|
path::append(default_prelude_file,
|
|
|
path::begin(relative_prelude_path, path::Style::posix),
|
|
path::begin(relative_prelude_path, path::Style::posix),
|
|
@@ -111,6 +124,8 @@ auto ExplorerMain(int argc, char** argv, void* static_for_main_addr,
|
|
|
cl::init(default_prelude_file_str));
|
|
cl::init(default_prelude_file_str));
|
|
|
|
|
|
|
|
cl::ParseCommandLineOptions(argc, argv);
|
|
cl::ParseCommandLineOptions(argc, argv);
|
|
|
|
|
+ auto reset_parser =
|
|
|
|
|
+ llvm::make_scope_exit([] { cl::ResetCommandLineParser(); });
|
|
|
|
|
|
|
|
// Set up a stream for trace output.
|
|
// Set up a stream for trace output.
|
|
|
std::unique_ptr<llvm::raw_ostream> scoped_trace_stream;
|
|
std::unique_ptr<llvm::raw_ostream> scoped_trace_stream;
|
|
@@ -147,13 +162,13 @@ auto ExplorerMain(int argc, char** argv, void* static_for_main_addr,
|
|
|
trace_stream.set_allowed_file_kinds(trace_file_kinds);
|
|
trace_stream.set_allowed_file_kinds(trace_file_kinds);
|
|
|
|
|
|
|
|
if (trace_file_name == "-") {
|
|
if (trace_file_name == "-") {
|
|
|
- trace_stream.set_stream(&llvm::outs());
|
|
|
|
|
|
|
+ trace_stream.set_stream(&out_stream_for_trace);
|
|
|
} else {
|
|
} else {
|
|
|
std::error_code err;
|
|
std::error_code err;
|
|
|
scoped_trace_stream =
|
|
scoped_trace_stream =
|
|
|
std::make_unique<llvm::raw_fd_ostream>(trace_file_name, err);
|
|
std::make_unique<llvm::raw_fd_ostream>(trace_file_name, err);
|
|
|
if (err) {
|
|
if (err) {
|
|
|
- llvm::errs() << err.message() << "\n";
|
|
|
|
|
|
|
+ err_stream << err.message() << "\n";
|
|
|
return EXIT_FAILURE;
|
|
return EXIT_FAILURE;
|
|
|
}
|
|
}
|
|
|
trace_stream.set_stream(scoped_trace_stream.get());
|
|
trace_stream.set_stream(scoped_trace_stream.get());
|
|
@@ -161,14 +176,14 @@ auto ExplorerMain(int argc, char** argv, void* static_for_main_addr,
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
ErrorOr<int> result =
|
|
ErrorOr<int> result =
|
|
|
- ParseAndExecuteFile(prelude_file_name, input_file_name, parser_debug,
|
|
|
|
|
- &trace_stream, &llvm::outs());
|
|
|
|
|
|
|
+ ParseAndExecute(fs, prelude_file_name, input_file_name, parser_debug,
|
|
|
|
|
+ &trace_stream, &out_stream);
|
|
|
if (result.ok()) {
|
|
if (result.ok()) {
|
|
|
// Print the return code to stdout.
|
|
// Print the return code to stdout.
|
|
|
- llvm::outs() << "result: " << *result << "\n";
|
|
|
|
|
|
|
+ out_stream << "result: " << *result << "\n";
|
|
|
return EXIT_SUCCESS;
|
|
return EXIT_SUCCESS;
|
|
|
} else {
|
|
} else {
|
|
|
- llvm::errs() << result.error() << "\n";
|
|
|
|
|
|
|
+ err_stream << result.error() << "\n";
|
|
|
return EXIT_FAILURE;
|
|
return EXIT_FAILURE;
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|