main.cpp 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  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 <string>
  7. #include "common/error.h"
  8. #include "explorer/base/trace_stream.h"
  9. #include "explorer/parse_and_execute/parse_and_execute.h"
  10. #include "llvm/ADT/ScopeExit.h"
  11. #include "llvm/ADT/SmallString.h"
  12. #include "llvm/Support/CommandLine.h"
  13. #include "llvm/Support/FileSystem.h"
  14. #include "llvm/Support/InitLLVM.h"
  15. #include "llvm/Support/Path.h"
  16. #include "llvm/Support/raw_ostream.h"
  17. namespace Carbon {
  18. namespace cl = llvm::cl;
  19. namespace path = llvm::sys::path;
  20. auto ExplorerMain(int argc, char** argv, void* static_for_main_addr,
  21. llvm::StringRef relative_prelude_path) -> int {
  22. llvm::setBugReportMsg(
  23. "Please report issues to "
  24. "https://github.com/carbon-language/carbon-lang/issues and include the "
  25. "crash backtrace.\n");
  26. llvm::InitLLVM init_llvm(argc, argv);
  27. // Printing to stderr should flush stdout. This is most noticeable when stderr
  28. // is piped to stdout.
  29. llvm::errs().tie(&llvm::outs());
  30. std::string exe =
  31. llvm::sys::fs::getMainExecutable(argv[0], static_for_main_addr);
  32. llvm::StringRef install_path = path::parent_path(exe);
  33. return ExplorerMain(argc, const_cast<const char**>(argv), install_path,
  34. relative_prelude_path, llvm::outs(), llvm::errs(),
  35. llvm::outs(), *llvm::vfs::getRealFileSystem());
  36. }
  37. auto ExplorerMain(int argc, const char** argv, llvm::StringRef install_path,
  38. llvm::StringRef relative_prelude_path,
  39. llvm::raw_ostream& out_stream, llvm::raw_ostream& err_stream,
  40. llvm::raw_ostream& out_stream_for_trace,
  41. llvm::vfs::FileSystem& fs) -> int {
  42. cl::opt<std::string> input_file_name(cl::Positional, cl::desc("<input file>"),
  43. cl::Required);
  44. cl::opt<bool> parser_debug("parser_debug",
  45. cl::desc("Enable debug output from the parser"));
  46. cl::opt<std::string> trace_file_name(
  47. "trace_file",
  48. cl::desc("Output file for tracing; set to `-` to output to stdout."));
  49. cl::list<ProgramPhase> trace_phases(
  50. "trace_phase",
  51. cl::desc("Select the program phases to include in the output. By "
  52. "default, only the execution trace will be added to the trace "
  53. "output. Use a combination of the following flags to include "
  54. "outputs for multiple phases:"),
  55. cl::values(
  56. clEnumValN(ProgramPhase::SourceProgram, "source_program",
  57. "Include trace output for the Source Program phase."),
  58. clEnumValN(ProgramPhase::NameResolution, "name_resolution",
  59. "Include trace output for the Name Resolution phase."),
  60. clEnumValN(
  61. ProgramPhase::ControlFlowResolution, "control_flow_resolution",
  62. "Include trace output for the Control Flow Resolution phase."),
  63. clEnumValN(ProgramPhase::TypeChecking, "type_checking",
  64. "Include trace output for the Type Checking phase."),
  65. clEnumValN(ProgramPhase::UnformedVariableResolution,
  66. "unformed_variables_resolution",
  67. "Include trace output for the Unformed Variables "
  68. "Resolution phase."),
  69. clEnumValN(ProgramPhase::Declarations, "declarations",
  70. "Include trace output for printing Declarations."),
  71. clEnumValN(ProgramPhase::Execution, "execution",
  72. "Include trace output for Program Execution."),
  73. clEnumValN(
  74. ProgramPhase::Timing, "timing",
  75. "Include timing logs for each phase, indicating the time taken."),
  76. clEnumValN(ProgramPhase::All, "all",
  77. "Include trace output for all phases.")),
  78. cl::CommaSeparated);
  79. enum class TraceFileContext { Main, Prelude, Import, All };
  80. cl::list<TraceFileContext> trace_file_contexts(
  81. "trace_file_context",
  82. cl::desc("Select file contexts for which you want to include the trace "
  83. "output"),
  84. cl::values(
  85. clEnumValN(
  86. TraceFileContext::Main, "main",
  87. "Include trace output for file containing the main function"),
  88. clEnumValN(TraceFileContext::Prelude, "prelude",
  89. "Include trace output for prelude"),
  90. clEnumValN(TraceFileContext::Import, "import",
  91. "Include trace output for imports"),
  92. clEnumValN(TraceFileContext::All, "all",
  93. "Include trace output for all files")),
  94. cl::CommaSeparated);
  95. CARBON_CHECK(argc > 0);
  96. // Use the executable path as a base for the relative prelude path.
  97. llvm::SmallString<256> default_prelude_file(install_path);
  98. path::append(default_prelude_file,
  99. path::begin(relative_prelude_path, path::Style::posix),
  100. path::end(relative_prelude_path));
  101. std::string default_prelude_file_str(default_prelude_file);
  102. cl::opt<std::string> prelude_file_name("prelude", cl::desc("<prelude file>"),
  103. cl::init(default_prelude_file_str));
  104. cl::ParseCommandLineOptions(argc, argv);
  105. auto reset_parser =
  106. llvm::make_scope_exit([] { cl::ResetCommandLineParser(); });
  107. // Set up a stream for trace output.
  108. std::unique_ptr<llvm::raw_ostream> scoped_trace_stream;
  109. TraceStream trace_stream;
  110. if (!trace_file_name.empty()) {
  111. // Adding allowed phases in the trace_stream.
  112. trace_stream.set_allowed_phases(trace_phases);
  113. // Translate --trace_file_context setting into a list of FileKinds.
  114. llvm::SmallVector<FileKind> trace_file_kinds = {FileKind::Unknown};
  115. if (!trace_file_contexts.getNumOccurrences()) {
  116. trace_file_kinds.push_back(FileKind::Main);
  117. } else {
  118. for (auto context : trace_file_contexts) {
  119. switch (context) {
  120. case TraceFileContext::Main:
  121. trace_file_kinds.push_back(FileKind::Main);
  122. break;
  123. case TraceFileContext::Prelude:
  124. trace_file_kinds.push_back(FileKind::Prelude);
  125. break;
  126. case TraceFileContext::Import:
  127. trace_file_kinds.push_back(FileKind::Import);
  128. break;
  129. case TraceFileContext::All:
  130. trace_file_kinds.push_back(FileKind::Main);
  131. trace_file_kinds.push_back(FileKind::Prelude);
  132. trace_file_kinds.push_back(FileKind::Import);
  133. break;
  134. }
  135. }
  136. }
  137. trace_stream.set_allowed_file_kinds(trace_file_kinds);
  138. if (trace_file_name == "-") {
  139. trace_stream.set_stream(&out_stream_for_trace);
  140. } else {
  141. std::error_code err;
  142. scoped_trace_stream =
  143. std::make_unique<llvm::raw_fd_ostream>(trace_file_name, err);
  144. if (err) {
  145. err_stream << err.message() << "\n";
  146. return EXIT_FAILURE;
  147. }
  148. trace_stream.set_stream(scoped_trace_stream.get());
  149. }
  150. }
  151. ErrorOr<int> result =
  152. ParseAndExecute(fs, prelude_file_name, input_file_name, parser_debug,
  153. &trace_stream, &out_stream);
  154. if (result.ok()) {
  155. // Print the return code to stdout.
  156. out_stream << "result: " << *result << "\n";
  157. return EXIT_SUCCESS;
  158. } else {
  159. err_stream << result.error() << "\n";
  160. return EXIT_FAILURE;
  161. }
  162. }
  163. } // namespace Carbon