driver_subcommand.h 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  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. #ifndef CARBON_TOOLCHAIN_DRIVER_DRIVER_SUBCOMMAND_H_
  5. #define CARBON_TOOLCHAIN_DRIVER_DRIVER_SUBCOMMAND_H_
  6. #include "common/command_line.h"
  7. #include "common/ostream.h"
  8. #include "llvm/Support/VirtualFileSystem.h"
  9. #include "toolchain/driver/driver_env.h"
  10. #include "toolchain/install/install_paths.h"
  11. namespace Carbon {
  12. // The result of a driver run.
  13. struct DriverResult {
  14. // Overall success result.
  15. bool success;
  16. // Per-file success results. May be empty if files aren't individually
  17. // processed.
  18. llvm::SmallVector<std::pair<std::string, bool>> per_file_success = {};
  19. };
  20. // A subcommand for the driver.
  21. class DriverSubcommand {
  22. public:
  23. explicit DriverSubcommand(CommandLine::CommandInfo info) : info_(info) {}
  24. // Adds the subcommand to the main command, assigning `selected_command` when
  25. // the subcommand is in use.
  26. auto AddTo(CommandLine::CommandBuilder& b,
  27. DriverSubcommand** selected_subcommand) -> void {
  28. b.AddSubcommand(info_, [this, selected_subcommand](
  29. CommandLine::CommandBuilder& sub_b) {
  30. BuildOptions(sub_b);
  31. sub_b.Do([this, selected_subcommand] { *selected_subcommand = this; });
  32. });
  33. }
  34. // Adds command line options.
  35. virtual auto BuildOptions(CommandLine::CommandBuilder& b) -> void = 0;
  36. // Runs the command.
  37. virtual auto Run(DriverEnv& driver_env) -> DriverResult = 0;
  38. private:
  39. // Subcommand information.
  40. CommandLine::CommandInfo info_;
  41. };
  42. } // namespace Carbon
  43. #endif // CARBON_TOOLCHAIN_DRIVER_DRIVER_SUBCOMMAND_H_