main.cpp 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  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. cl::list<ProgramPhase> trace_phases(
  43. "trace_phase",
  44. cl::desc("Select the program phases to include in the output. By "
  45. "default, only the execution trace will be added to the trace "
  46. "output. Use a combination of the following flags to include "
  47. "outputs for multiple phases:"),
  48. cl::values(
  49. clEnumValN(ProgramPhase::SourceProgram, "source_program",
  50. "Include trace output for the Source Program phase."),
  51. clEnumValN(ProgramPhase::NameResolution, "name_resolution",
  52. "Include trace output for the Name Resolution phase."),
  53. clEnumValN(
  54. ProgramPhase::ControlFlowResolution, "control_flow_resolution",
  55. "Include trace output for the Control Flow Resolution phase."),
  56. clEnumValN(ProgramPhase::TypeChecking, "type_checking",
  57. "Include trace output for the Type Checking phase."),
  58. clEnumValN(ProgramPhase::UnformedVariableResolution,
  59. "unformed_variables_resolution",
  60. "Include trace output for the Unformed Variables "
  61. "Resolution phase."),
  62. clEnumValN(ProgramPhase::Declarations, "declarations",
  63. "Include trace output for printing Declarations."),
  64. clEnumValN(ProgramPhase::Execution, "execution",
  65. "Include trace output for Program Execution."),
  66. clEnumValN(
  67. ProgramPhase::Timing, "timing",
  68. "Include timing logs for each phase, indicating the time taken."),
  69. clEnumValN(ProgramPhase::All, "all",
  70. "Include trace output for all phases.")),
  71. cl::CommaSeparated);
  72. enum class TraceFileContext { Main, Prelude, Import, All };
  73. cl::list<TraceFileContext> trace_file_contexts(
  74. "trace_file_context",
  75. cl::desc("Select file contexts for which you want to include the trace "
  76. "output"),
  77. cl::values(
  78. clEnumValN(
  79. TraceFileContext::Main, "main",
  80. "Include trace output for file containing the main function"),
  81. clEnumValN(TraceFileContext::Prelude, "prelude",
  82. "Include trace output for prelude"),
  83. clEnumValN(TraceFileContext::Import, "import",
  84. "Include trace output for imports"),
  85. clEnumValN(TraceFileContext::All, "all",
  86. "Include trace output for all files")),
  87. cl::CommaSeparated);
  88. // Use the executable path as a base for the relative prelude path.
  89. std::string exe =
  90. llvm::sys::fs::getMainExecutable(argv[0], static_for_main_addr);
  91. llvm::StringRef install_path = path::parent_path(exe);
  92. llvm::SmallString<256> default_prelude_file(install_path);
  93. path::append(default_prelude_file,
  94. path::begin(relative_prelude_path, path::Style::posix),
  95. path::end(relative_prelude_path));
  96. std::string default_prelude_file_str(default_prelude_file);
  97. cl::opt<std::string> prelude_file_name("prelude", cl::desc("<prelude file>"),
  98. cl::init(default_prelude_file_str));
  99. cl::ParseCommandLineOptions(argc, argv);
  100. // Set up a stream for trace output.
  101. std::unique_ptr<llvm::raw_ostream> scoped_trace_stream;
  102. TraceStream trace_stream;
  103. if (!trace_file_name.empty()) {
  104. // Adding allowed phases in the trace_stream.
  105. trace_stream.set_allowed_phases(trace_phases);
  106. // Translate --trace_file_context setting into a list of FileKinds.
  107. llvm::SmallVector<FileKind> trace_file_kinds = {FileKind::Unknown};
  108. if (!trace_file_contexts.getNumOccurrences()) {
  109. trace_file_kinds.push_back(FileKind::Main);
  110. } else {
  111. for (auto context : trace_file_contexts) {
  112. switch (context) {
  113. case TraceFileContext::Main:
  114. trace_file_kinds.push_back(FileKind::Main);
  115. break;
  116. case TraceFileContext::Prelude:
  117. trace_file_kinds.push_back(FileKind::Prelude);
  118. break;
  119. case TraceFileContext::Import:
  120. trace_file_kinds.push_back(FileKind::Import);
  121. break;
  122. case TraceFileContext::All:
  123. trace_file_kinds.push_back(FileKind::Main);
  124. trace_file_kinds.push_back(FileKind::Prelude);
  125. trace_file_kinds.push_back(FileKind::Import);
  126. break;
  127. }
  128. }
  129. }
  130. trace_stream.set_allowed_file_kinds(trace_file_kinds);
  131. if (trace_file_name == "-") {
  132. trace_stream.set_stream(&llvm::outs());
  133. } else {
  134. std::error_code err;
  135. scoped_trace_stream =
  136. std::make_unique<llvm::raw_fd_ostream>(trace_file_name, err);
  137. if (err) {
  138. llvm::errs() << err.message() << "\n";
  139. return EXIT_FAILURE;
  140. }
  141. trace_stream.set_stream(scoped_trace_stream.get());
  142. }
  143. }
  144. ErrorOr<int> result =
  145. ParseAndExecuteFile(prelude_file_name, input_file_name, parser_debug,
  146. &trace_stream, &llvm::outs());
  147. if (result.ok()) {
  148. // Print the return code to stdout.
  149. llvm::outs() << "result: " << *result << "\n";
  150. return EXIT_SUCCESS;
  151. } else {
  152. llvm::errs() << result.error() << "\n";
  153. return EXIT_FAILURE;
  154. }
  155. }
  156. } // namespace Carbon