driver.cpp 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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 "toolchain/driver/driver.h"
  5. #include <algorithm>
  6. #include <memory>
  7. #include <optional>
  8. #include "common/command_line.h"
  9. #include "common/version.h"
  10. #include "toolchain/driver/clang_subcommand.h"
  11. #include "toolchain/driver/compile_subcommand.h"
  12. #include "toolchain/driver/format_subcommand.h"
  13. #include "toolchain/driver/language_server_subcommand.h"
  14. #include "toolchain/driver/link_subcommand.h"
  15. namespace Carbon {
  16. namespace {
  17. struct Options {
  18. static const CommandLine::CommandInfo Info;
  19. auto Build(CommandLine::CommandBuilder& b) -> void;
  20. bool verbose = false;
  21. bool fuzzing = false;
  22. bool include_diagnostic_kind = false;
  23. ClangSubcommand clang;
  24. CompileSubcommand compile;
  25. FormatSubcommand format;
  26. LanguageServerSubcommand language_server;
  27. LinkSubcommand link;
  28. // On success, this is set to the subcommand to run.
  29. DriverSubcommand* selected_subcommand = nullptr;
  30. };
  31. } // namespace
  32. // Note that this is not constexpr so that it can include information generated
  33. // in separate translation units and potentially overridden at link time in the
  34. // version string.
  35. const CommandLine::CommandInfo Options::Info = {
  36. .name = "carbon",
  37. .version = Version::ToolchainInfo,
  38. .help = R"""(
  39. This is the unified Carbon Language toolchain driver. Its subcommands provide
  40. all of the core behavior of the toolchain, including compilation, linking, and
  41. developer tools. Each of these has its own subcommand, and you can pass a
  42. specific subcommand to the `help` subcommand to get details about its usage.
  43. )""",
  44. .help_epilogue = R"""(
  45. For questions, issues, or bug reports, please use our GitHub project:
  46. https://github.com/carbon-language/carbon-lang
  47. )""",
  48. };
  49. auto Options::Build(CommandLine::CommandBuilder& b) -> void {
  50. b.AddFlag(
  51. {
  52. .name = "verbose",
  53. .short_name = "v",
  54. .help = "Enable verbose logging to the stderr stream.",
  55. },
  56. [&](CommandLine::FlagBuilder& arg_b) { arg_b.Set(&verbose); });
  57. b.AddFlag(
  58. {
  59. .name = "fuzzing",
  60. .help = "Configure the command line for fuzzing.",
  61. },
  62. [&](CommandLine::FlagBuilder& arg_b) { arg_b.Set(&fuzzing); });
  63. b.AddFlag(
  64. {
  65. .name = "include-diagnostic-kind",
  66. .help = R"""(
  67. When printing diagnostics, include the diagnostic kind as part of output. This
  68. applies to each message that forms a diagnostic, not just the primary message.
  69. )""",
  70. },
  71. [&](auto& arg_b) { arg_b.Set(&include_diagnostic_kind); });
  72. clang.AddTo(b, &selected_subcommand);
  73. compile.AddTo(b, &selected_subcommand);
  74. format.AddTo(b, &selected_subcommand);
  75. language_server.AddTo(b, &selected_subcommand);
  76. link.AddTo(b, &selected_subcommand);
  77. b.RequiresSubcommand();
  78. }
  79. auto Driver::RunCommand(llvm::ArrayRef<llvm::StringRef> args) -> DriverResult {
  80. if (driver_env_.installation->error()) {
  81. CARBON_DIAGNOSTIC(DriverInstallInvalid, Error, "{0}", std::string);
  82. driver_env_.emitter.Emit(DriverInstallInvalid,
  83. driver_env_.installation->error()->str());
  84. return {.success = false};
  85. }
  86. Options options;
  87. ErrorOr<CommandLine::ParseResult> result = CommandLine::Parse(
  88. args, *driver_env_.output_stream, Options::Info,
  89. [&](CommandLine::CommandBuilder& b) { options.Build(b); });
  90. // Regardless of whether the parse succeeded, try to use the diagnostic kind
  91. // flag.
  92. driver_env_.consumer.set_include_diagnostic_kind(
  93. options.include_diagnostic_kind);
  94. if (!result.ok()) {
  95. CARBON_DIAGNOSTIC(DriverCommandLineParseFailed, Error, "{0}", std::string);
  96. driver_env_.emitter.Emit(DriverCommandLineParseFailed,
  97. PrintToString(result.error()));
  98. return {.success = false};
  99. } else if (*result == CommandLine::ParseResult::MetaSuccess) {
  100. return {.success = true};
  101. }
  102. if (options.verbose) {
  103. // Note this implies streamed output in order to interleave.
  104. driver_env_.vlog_stream = driver_env_.error_stream;
  105. }
  106. if (options.fuzzing) {
  107. driver_env_.fuzzing = true;
  108. }
  109. CARBON_CHECK(options.selected_subcommand != nullptr);
  110. return options.selected_subcommand->Run(driver_env_);
  111. }
  112. } // namespace Carbon