driver.h 2.8 KB

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