main.cpp 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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. cl::desc("Select the program phases to include in the output. By "
  44. "default, only the execution trace will be added to the trace "
  45. "output. Use a combination of the following flags to include "
  46. "outputs for multiple phases:"),
  47. cl::values(
  48. clEnumValN(ProgramPhase::SourceProgram, "trace_source_program",
  49. "Include trace output for the Source Program phase."),
  50. clEnumValN(ProgramPhase::NameResolution, "trace_name_resolution",
  51. "Include trace output for the Name Resolution phase."),
  52. clEnumValN(
  53. ProgramPhase::ControlFlowResolution,
  54. "trace_control_flow_resolution",
  55. "Include trace output for the Control Flow Resolution phase."),
  56. clEnumValN(ProgramPhase::TypeChecking, "trace_type_checking",
  57. "Include trace output for the Type Checking phase."),
  58. clEnumValN(ProgramPhase::UnformedVariableResolution,
  59. "trace_unformed_variables_resolution",
  60. "Include trace output for the Unformed Variables "
  61. "Resolution phase."),
  62. clEnumValN(ProgramPhase::Declarations, "trace_declarations",
  63. "Include trace output for printing Declarations."),
  64. clEnumValN(ProgramPhase::Execution, "trace_execution",
  65. "Include trace output for Program Execution."),
  66. clEnumValN(
  67. ProgramPhase::Timing, "trace_timing",
  68. "Include timing logs for each phase, indicating the time taken."),
  69. clEnumValN(ProgramPhase::All, "trace_all",
  70. "Include trace output for all phases.")));
  71. // Use the executable path as a base for the relative prelude path.
  72. std::string exe =
  73. llvm::sys::fs::getMainExecutable(argv[0], static_for_main_addr);
  74. llvm::StringRef install_path = path::parent_path(exe);
  75. llvm::SmallString<256> default_prelude_file(install_path);
  76. path::append(default_prelude_file,
  77. path::begin(relative_prelude_path, path::Style::posix),
  78. path::end(relative_prelude_path));
  79. std::string default_prelude_file_str(default_prelude_file);
  80. cl::opt<std::string> prelude_file_name("prelude", cl::desc("<prelude file>"),
  81. cl::init(default_prelude_file_str));
  82. cl::ParseCommandLineOptions(argc, argv);
  83. // Set up a stream for trace output.
  84. std::unique_ptr<llvm::raw_ostream> scoped_trace_stream;
  85. TraceStream trace_stream;
  86. if (!trace_file_name.empty()) {
  87. // Adding allowed phases in the trace_stream
  88. trace_stream.set_allowed_phases(allowed_program_phases);
  89. if (trace_file_name == "-") {
  90. trace_stream.set_stream(&llvm::outs());
  91. } else {
  92. std::error_code err;
  93. scoped_trace_stream =
  94. std::make_unique<llvm::raw_fd_ostream>(trace_file_name, err);
  95. if (err) {
  96. llvm::errs() << err.message() << "\n";
  97. return EXIT_FAILURE;
  98. }
  99. trace_stream.set_stream(scoped_trace_stream.get());
  100. }
  101. }
  102. ErrorOr<int> result =
  103. ParseAndExecuteFile(prelude_file_name, input_file_name, parser_debug,
  104. &trace_stream, &llvm::outs());
  105. if (result.ok()) {
  106. // Print the return code to stdout.
  107. llvm::outs() << "result: " << *result << "\n";
  108. return EXIT_SUCCESS;
  109. } else {
  110. llvm::errs() << result.error() << "\n";
  111. return EXIT_FAILURE;
  112. }
  113. }
  114. } // namespace Carbon