main.cpp 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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> allowed_program_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. // Use the executable path as a base for the relative prelude path.
  73. std::string exe =
  74. llvm::sys::fs::getMainExecutable(argv[0], static_for_main_addr);
  75. llvm::StringRef install_path = path::parent_path(exe);
  76. llvm::SmallString<256> default_prelude_file(install_path);
  77. path::append(default_prelude_file,
  78. path::begin(relative_prelude_path, path::Style::posix),
  79. path::end(relative_prelude_path));
  80. std::string default_prelude_file_str(default_prelude_file);
  81. cl::opt<std::string> prelude_file_name("prelude", cl::desc("<prelude file>"),
  82. cl::init(default_prelude_file_str));
  83. cl::ParseCommandLineOptions(argc, argv);
  84. // Set up a stream for trace output.
  85. std::unique_ptr<llvm::raw_ostream> scoped_trace_stream;
  86. TraceStream trace_stream;
  87. if (!trace_file_name.empty()) {
  88. // Adding allowed phases in the trace_stream
  89. trace_stream.set_allowed_phases(allowed_program_phases);
  90. if (trace_file_name == "-") {
  91. trace_stream.set_stream(&llvm::outs());
  92. } else {
  93. std::error_code err;
  94. scoped_trace_stream =
  95. std::make_unique<llvm::raw_fd_ostream>(trace_file_name, err);
  96. if (err) {
  97. llvm::errs() << err.message() << "\n";
  98. return EXIT_FAILURE;
  99. }
  100. trace_stream.set_stream(scoped_trace_stream.get());
  101. }
  102. }
  103. ErrorOr<int> result =
  104. ParseAndExecuteFile(prelude_file_name, input_file_name, parser_debug,
  105. &trace_stream, &llvm::outs());
  106. if (result.ok()) {
  107. // Print the return code to stdout.
  108. llvm::outs() << "result: " << *result << "\n";
  109. return EXIT_SUCCESS;
  110. } else {
  111. llvm::errs() << result.error() << "\n";
  112. return EXIT_FAILURE;
  113. }
  114. }
  115. } // namespace Carbon