driver_subcommand.h 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  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/base/install_paths.h"
  10. #include "toolchain/driver/driver_env.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(
  29. info_, [this, selected_subcommand](CommandLine::CommandBuilder& sub_b) {
  30. BuildOptionsAndSetAction(sub_b, selected_subcommand);
  31. });
  32. }
  33. // Adds command line options.
  34. virtual auto BuildOptions(CommandLine::CommandBuilder& b) -> void = 0;
  35. // Adds command line options and registers the `Run` method to be called.
  36. //
  37. // For more complex subcommands that need to control the action this method
  38. // can be overridden and bypass the simple API above.
  39. //
  40. // TODO: This isn't the most elegant way to manage this. There is probably a
  41. // better factoring / organization of the driver subcommand infrastructure
  42. // that bakes in the needed flexibility here, but that was deferred for a
  43. // future refactoring.
  44. virtual auto BuildOptionsAndSetAction(CommandLine::CommandBuilder& b,
  45. DriverSubcommand** selected_subcommand)
  46. -> void {
  47. BuildOptions(b);
  48. b.Do([this, selected_subcommand] { *selected_subcommand = this; });
  49. }
  50. // Runs the command.
  51. virtual auto Run(DriverEnv& driver_env) -> DriverResult = 0;
  52. protected:
  53. // Tests if fuzzing and if so diagnose and returns true.
  54. //
  55. // This should be used in subcommands to diagnose and exit early rather than
  56. // entering them during fuzzing when they use external libraries that we can't
  57. // keep fuzz-clean.
  58. auto TestAndDiagnoseIfFuzzingExternalLibraries(DriverEnv& driver_env,
  59. llvm::StringRef name) -> bool;
  60. private:
  61. // Subcommand information.
  62. CommandLine::CommandInfo info_;
  63. };
  64. } // namespace Carbon
  65. #endif // CARBON_TOOLCHAIN_DRIVER_DRIVER_SUBCOMMAND_H_