driver.h 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  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_H_
  5. #define CARBON_TOOLCHAIN_DRIVER_DRIVER_H_
  6. #include <cstdint>
  7. #include "llvm/ADT/ArrayRef.h"
  8. #include "llvm/ADT/StringRef.h"
  9. #include "llvm/Support/Debug.h"
  10. #include "llvm/Support/raw_ostream.h"
  11. #include "toolchain/diagnostics/diagnostic_emitter.h"
  12. namespace Carbon {
  13. // Command line interface driver.
  14. //
  15. // Provides simple API to parse and run command lines for Carbon. It is
  16. // generally expected to be used to implement command line tools for working
  17. // with the language.
  18. class Driver {
  19. public:
  20. // Default constructed driver uses stderr for all error and informational
  21. // output.
  22. Driver() : output_stream_(llvm::outs()), error_stream_(llvm::errs()) {}
  23. // Constructs a driver with any error or informational output directed to a
  24. // specified stream.
  25. Driver(llvm::raw_ostream& output_stream, llvm::raw_ostream& error_stream)
  26. : output_stream_(output_stream), error_stream_(error_stream) {}
  27. // Parses the given arguments into both a subcommand to select the operation
  28. // to perform and any arguments to that subcommand.
  29. //
  30. // Returns true if the operation succeeds. If the operation fails, returns
  31. // false and any information about the failure is printed to the registered
  32. // error stream (stderr by default).
  33. auto RunFullCommand(llvm::ArrayRef<llvm::StringRef> args) -> bool;
  34. // Subcommand that prints available help text to the error stream.
  35. //
  36. // Optionally one positional parameter may be provided to select a particular
  37. // subcommand or detailed section of help to print.
  38. //
  39. // Returns true if appropriate help text was found and printed. If an invalid
  40. // positional parameter (or flag) is provided, returns false.
  41. auto RunHelpSubcommand(DiagnosticConsumer& consumer,
  42. llvm::ArrayRef<llvm::StringRef> args) -> bool;
  43. // Subcommand that dumps internal compilation information for the provided
  44. // source file.
  45. //
  46. // Requires exactly one positional parameter to designate the source file to
  47. // read. May be `-` to read from stdin.
  48. //
  49. // Returns true if the operation succeeds. If the operation fails, this
  50. // returns false and any information about the failure is printed to the
  51. // registered error stream (stderr by default).
  52. auto RunDumpSubcommand(DiagnosticConsumer& consumer,
  53. llvm::ArrayRef<llvm::StringRef> args) -> bool;
  54. private:
  55. auto ReportExtraArgs(llvm::StringRef subcommand_text,
  56. llvm::ArrayRef<llvm::StringRef> args) -> void;
  57. llvm::raw_ostream& output_stream_;
  58. llvm::raw_ostream& error_stream_;
  59. llvm::raw_ostream* vlog_stream_ = nullptr;
  60. };
  61. } // namespace Carbon
  62. #endif // CARBON_TOOLCHAIN_DRIVER_DRIVER_H_