tool_runner_base.h 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  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_TOOL_RUNNER_BASE_H_
  5. #define CARBON_TOOLCHAIN_DRIVER_TOOL_RUNNER_BASE_H_
  6. #include <optional>
  7. #include "common/ostream.h"
  8. #include "llvm/ADT/ArrayRef.h"
  9. #include "llvm/ADT/StringRef.h"
  10. #include "toolchain/base/install_paths.h"
  11. namespace Carbon {
  12. // Base that factors out common utilities needed when implementing a runner of
  13. // an external tool, especially tools part of the LLVM and Clang C++ toolchain
  14. // that Carbon will end up wrapping.
  15. //
  16. // Note that this struct just collects common data and helper methods, and does
  17. // not itself impose any invariants or form a meaningful API. It should be used
  18. // as an implementation detail only.
  19. class ToolRunnerBase {
  20. public:
  21. // Construct the tool runner bas.
  22. //
  23. // If `vlog_stream` is provided, it will be used for `CARBON_VLOG`s. If it is
  24. // also equal to `&llvm::errs()`, and so tied to stderr, that will be used by
  25. // verbose flag injection helpers in this class.
  26. explicit ToolRunnerBase(const InstallPaths* install_paths,
  27. llvm::raw_ostream* vlog_stream = nullptr);
  28. protected:
  29. // Translates `args` into C-string arguments for tool APIs based on `main`.
  30. //
  31. // Accepts a `tool_name` for logging, and a `tool_path` that will be used as
  32. // the first C-string argument to simulate and `argv[0]` entry.
  33. //
  34. // Accepts a `cstr_arg_storage` that will provide the underlying storage for
  35. // the C-strings, and returns a small vector of the C-string pointers. The
  36. // returned small vector uses a large small size to allow most common command
  37. // lines to avoid extra allocations and growth passes.
  38. //
  39. // Lastly accepts an optional `verbose_flag`. If provided, and if
  40. // `vlog_stream_` is bound to stderr for this instance, the verbose flag will
  41. // be injected at the start of the argument list.
  42. auto BuildCStrArgs(llvm::StringRef tool_name, llvm::StringRef tool_path,
  43. std::optional<llvm::StringRef> verbose_flag,
  44. llvm::ArrayRef<llvm::StringRef> args,
  45. llvm::OwningArrayRef<char>& cstr_arg_storage)
  46. -> llvm::SmallVector<const char*, 64>;
  47. // We use protected members as this base is just factoring out common
  48. // implementation details of other runners.
  49. //
  50. // NOLINTBEGIN(misc-non-private-member-variables-in-classes)
  51. const InstallPaths* installation_;
  52. llvm::raw_ostream* vlog_stream_;
  53. // NOLINTEND(misc-non-private-member-variables-in-classes)
  54. };
  55. } // namespace Carbon
  56. #endif // CARBON_TOOLCHAIN_DRIVER_TOOL_RUNNER_BASE_H_