main.cpp 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. // Part of the Carbon Language project, under the Apache License v2.0 with LLVM
  2. // Exceptions. See /LICENSE for license information.
  3. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  4. #include "explorer/main.h"
  5. #include <unistd.h>
  6. #include <chrono>
  7. #include <cstdio>
  8. #include <cstring>
  9. #include <iostream>
  10. #include <optional>
  11. #include <string>
  12. #include <vector>
  13. #include "common/error.h"
  14. #include "explorer/common/trace_stream.h"
  15. #include "explorer/parse_and_execute/parse_and_execute.h"
  16. #include "llvm/ADT/SmallString.h"
  17. #include "llvm/Support/CommandLine.h"
  18. #include "llvm/Support/FileSystem.h"
  19. #include "llvm/Support/InitLLVM.h"
  20. #include "llvm/Support/Path.h"
  21. #include "llvm/Support/raw_ostream.h"
  22. namespace Carbon {
  23. namespace cl = llvm::cl;
  24. namespace path = llvm::sys::path;
  25. auto ExplorerMain(int argc, char** argv, void* static_for_main_addr,
  26. llvm::StringRef relative_prelude_path) -> int {
  27. llvm::setBugReportMsg(
  28. "Please report issues to "
  29. "https://github.com/carbon-language/carbon-lang/issues and include the "
  30. "crash backtrace.\n");
  31. llvm::InitLLVM init_llvm(argc, argv);
  32. // Printing to stderr should flush stdout. This is most noticeable when stderr
  33. // is piped to stdout.
  34. llvm::errs().tie(&llvm::outs());
  35. cl::opt<std::string> input_file_name(cl::Positional, cl::desc("<input file>"),
  36. cl::Required);
  37. cl::opt<bool> parser_debug("parser_debug",
  38. cl::desc("Enable debug output from the parser"));
  39. cl::opt<std::string> trace_file_name(
  40. "trace_file",
  41. cl::desc("Output file for tracing; set to `-` to output to stdout."));
  42. // Use the executable path as a base for the relative prelude path.
  43. std::string exe =
  44. llvm::sys::fs::getMainExecutable(argv[0], static_for_main_addr);
  45. llvm::StringRef install_path = path::parent_path(exe);
  46. llvm::SmallString<256> default_prelude_file(install_path);
  47. path::append(default_prelude_file,
  48. path::begin(relative_prelude_path, path::Style::posix),
  49. path::end(relative_prelude_path));
  50. std::string default_prelude_file_str(default_prelude_file);
  51. cl::opt<std::string> prelude_file_name("prelude", cl::desc("<prelude file>"),
  52. cl::init(default_prelude_file_str));
  53. cl::ParseCommandLineOptions(argc, argv);
  54. // Set up a stream for trace output.
  55. std::unique_ptr<llvm::raw_ostream> scoped_trace_stream;
  56. TraceStream trace_stream;
  57. if (!trace_file_name.empty()) {
  58. if (trace_file_name == "-") {
  59. trace_stream.set_stream(&llvm::outs());
  60. } else {
  61. std::error_code err;
  62. scoped_trace_stream =
  63. std::make_unique<llvm::raw_fd_ostream>(trace_file_name, err);
  64. if (err) {
  65. llvm::errs() << err.message() << "\n";
  66. return EXIT_FAILURE;
  67. }
  68. trace_stream.set_stream(scoped_trace_stream.get());
  69. }
  70. }
  71. ErrorOr<int> result =
  72. ParseAndExecuteFile(prelude_file_name, input_file_name, parser_debug,
  73. &trace_stream, &llvm::outs());
  74. if (result.ok()) {
  75. // Print the return code to stdout.
  76. llvm::outs() << "result: " << *result << "\n";
  77. // When there's a dedicated trace file, print the return code to it too.
  78. if (scoped_trace_stream) {
  79. trace_stream << "result: " << *result << "\n";
  80. }
  81. return EXIT_SUCCESS;
  82. } else {
  83. llvm::errs() << result.error() << "\n";
  84. return EXIT_FAILURE;
  85. }
  86. }
  87. } // namespace Carbon