main.cpp 7.5 KB

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