driver.cpp 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  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 "driver/driver.h"
  5. #include "diagnostics/diagnostic_emitter.h"
  6. #include "lexer/tokenized_buffer.h"
  7. #include "llvm/ADT/ArrayRef.h"
  8. #include "llvm/ADT/StringExtras.h"
  9. #include "llvm/ADT/StringRef.h"
  10. #include "llvm/ADT/StringSwitch.h"
  11. #include "llvm/Support/Error.h"
  12. #include "llvm/Support/ErrorHandling.h"
  13. #include "llvm/Support/Format.h"
  14. #include "source/source_buffer.h"
  15. namespace Carbon {
  16. namespace {
  17. enum class Subcommand {
  18. #define CARBON_SUBCOMMAND(Name, ...) Name,
  19. #include "driver/flags.def"
  20. Unknown,
  21. };
  22. auto GetSubcommand(llvm::StringRef name) -> Subcommand {
  23. return llvm::StringSwitch<Subcommand>(name)
  24. #define CARBON_SUBCOMMAND(Name, Spelling, ...) .Case(Spelling, Subcommand::Name)
  25. #include "driver/flags.def"
  26. .Default(Subcommand::Unknown);
  27. }
  28. } // namespace
  29. auto Driver::RunFullCommand(llvm::ArrayRef<llvm::StringRef> args) -> bool {
  30. if (args.empty()) {
  31. error_stream << "ERROR: No subcommand specified.\n";
  32. return false;
  33. }
  34. llvm::StringRef subcommand_text = args[0];
  35. llvm::SmallVector<llvm::StringRef, 16> subcommand_args(
  36. std::next(args.begin()), args.end());
  37. switch (GetSubcommand(subcommand_text)) {
  38. case Subcommand::Unknown:
  39. error_stream << "ERROR: Unknown subcommand '" << subcommand_text
  40. << "'.\n";
  41. return false;
  42. #define CARBON_SUBCOMMAND(Name, ...) \
  43. case Subcommand::Name: \
  44. return Run##Name##Subcommand(subcommand_args);
  45. #include "driver/flags.def"
  46. }
  47. llvm_unreachable("All subcommands handled!");
  48. }
  49. auto Driver::RunHelpSubcommand(llvm::ArrayRef<llvm::StringRef> args) -> bool {
  50. // FIXME: We should support getting detailed help on a subcommand by looking
  51. // for it as a positional parameter here.
  52. if (!args.empty()) {
  53. ReportExtraArgs("help", args);
  54. return false;
  55. }
  56. output_stream << "List of subcommands:\n\n";
  57. constexpr llvm::StringLiteral SubcommandsAndHelp[][2] = {
  58. #define CARBON_SUBCOMMAND(Name, Spelling, HelpText) {Spelling, HelpText},
  59. #include "driver/flags.def"
  60. };
  61. int max_subcommand_width = 0;
  62. for (auto subcommand_and_help : SubcommandsAndHelp) {
  63. max_subcommand_width = std::max(
  64. max_subcommand_width, static_cast<int>(subcommand_and_help[0].size()));
  65. }
  66. for (auto subcommand_and_help : SubcommandsAndHelp) {
  67. llvm::StringRef subcommand_text = subcommand_and_help[0];
  68. // FIXME: We should wrap this to the number of columns left after the
  69. // subcommand on the terminal, and using a hanging indent.
  70. llvm::StringRef help_text = subcommand_and_help[1];
  71. output_stream << " "
  72. << llvm::left_justify(subcommand_text, max_subcommand_width)
  73. << " - " << help_text << "\n";
  74. }
  75. output_stream << "\n";
  76. return true;
  77. }
  78. auto Driver::RunDumpTokensSubcommand(llvm::ArrayRef<llvm::StringRef> args)
  79. -> bool {
  80. if (args.empty()) {
  81. error_stream << "ERROR: No input file specified.\n";
  82. return false;
  83. }
  84. llvm::StringRef input_file_name = args.front();
  85. args = args.drop_front();
  86. if (!args.empty()) {
  87. ReportExtraArgs("dump-tokens", args);
  88. return false;
  89. }
  90. auto source = SourceBuffer::CreateFromFile(input_file_name);
  91. if (!source) {
  92. error_stream << "ERROR: Unable to open input source file: ";
  93. llvm::handleAllErrors(source.takeError(),
  94. [&](const llvm::ErrorInfoBase& ei) {
  95. ei.log(error_stream);
  96. error_stream << "\n";
  97. });
  98. return false;
  99. }
  100. auto tokenized_source =
  101. TokenizedBuffer::Lex(*source, ConsoleDiagnosticConsumer());
  102. if (tokenized_source.HasErrors()) {
  103. error_stream << "ERROR: Unable to tokenize source file '" << input_file_name
  104. << "'!\n";
  105. return false;
  106. }
  107. tokenized_source.Print(output_stream);
  108. return true;
  109. }
  110. auto Driver::ReportExtraArgs(llvm::StringRef subcommand_text,
  111. llvm::ArrayRef<llvm::StringRef> args) -> void {
  112. error_stream << "ERROR: Unexpected additional arguments to the '"
  113. << subcommand_text << "' subcommand:";
  114. for (auto arg : args) {
  115. error_stream << " " << arg;
  116. }
  117. error_stream << "\n";
  118. }
  119. } // namespace Carbon