driver.h 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  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 TOOLCHAIN_DRIVER_DRIVER_H_
  5. #define 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 the token information for the provided source file.
  44. //
  45. // Requires exactly one positional parameter to designate the source file to
  46. // read. May be `-` to read from stdin.
  47. //
  48. // Returns true if the operation succeeds. If the operation fails, this
  49. // returns false and any information about the failure is printed to the
  50. // registered error stream (stderr by default).
  51. auto RunDumpTokensSubcommand(DiagnosticConsumer& consumer,
  52. llvm::ArrayRef<llvm::StringRef> args) -> bool;
  53. // Subcommand that dumps the parse tree for the provided source file.
  54. //
  55. // Requires exactly one positional parameter to designate the source file to
  56. // read. May be `-` to read from stdin.
  57. //
  58. // Returns true if the operation succeeds. If the operation fails, this
  59. // returns false and any information about the failure is printed to the
  60. // registered error stream (stderr by default).
  61. auto RunDumpParseTreeSubcommand(DiagnosticConsumer& consumer,
  62. llvm::ArrayRef<llvm::StringRef> args) -> bool;
  63. private:
  64. auto ReportExtraArgs(llvm::StringRef subcommand_text,
  65. llvm::ArrayRef<llvm::StringRef> args) -> void;
  66. llvm::raw_ostream& output_stream_;
  67. llvm::raw_ostream& error_stream_;
  68. };
  69. } // namespace Carbon
  70. #endif // TOOLCHAIN_DRIVER_DRIVER_H_