driver.h 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  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 "common/command_line.h"
  7. #include "llvm/ADT/ArrayRef.h"
  8. #include "llvm/ADT/StringRef.h"
  9. #include "toolchain/driver/driver_env.h"
  10. #include "toolchain/driver/driver_subcommand.h"
  11. namespace Carbon {
  12. // Command line interface driver.
  13. //
  14. // Provides simple API to parse and run command lines for Carbon. It is
  15. // generally expected to be used to implement command line tools for working
  16. // with the language.
  17. class Driver {
  18. public:
  19. // Constructs a driver with the provided environment. `input_stream` is
  20. // optional; other parameters are required.
  21. explicit Driver(llvm::IntrusiveRefCntPtr<llvm::vfs::FileSystem> fs,
  22. const InstallPaths* installation, FILE* input_stream,
  23. llvm::raw_pwrite_stream* output_stream,
  24. llvm::raw_pwrite_stream* error_stream, bool fuzzing = false,
  25. bool enable_leaking = false)
  26. : fs_(std::move(fs)),
  27. installation_(installation),
  28. input_stream_(input_stream),
  29. output_stream_(output_stream),
  30. error_stream_(error_stream),
  31. fuzzing_(fuzzing),
  32. enable_leaking_(enable_leaking) {}
  33. // Parses the given arguments into both a subcommand to select the operation
  34. // to perform and any arguments to that subcommand.
  35. //
  36. // Returns true if the operation succeeds. If the operation fails, returns
  37. // false and any information about the failure is printed to the registered
  38. // error stream (stderr by default).
  39. auto RunCommand(llvm::ArrayRef<llvm::StringRef> args) -> DriverResult;
  40. private:
  41. // We store the initial values in the `DriverEnv` that will be used for each
  42. // subcommand invocation here. These are used as the _starting_ values of the
  43. // environment, but individual `RunCommand` invocations may customize the
  44. // `DriverEnv` instance changing these values.
  45. //
  46. // For details on each of these fields, see the documentation in `DriverEnv`.
  47. llvm::IntrusiveRefCntPtr<llvm::vfs::FileSystem> fs_;
  48. const InstallPaths* installation_;
  49. FILE* input_stream_;
  50. llvm::raw_pwrite_stream* output_stream_;
  51. llvm::raw_pwrite_stream* error_stream_;
  52. bool fuzzing_;
  53. bool enable_leaking_;
  54. };
  55. } // namespace Carbon
  56. #endif // CARBON_TOOLCHAIN_DRIVER_DRIVER_H_