clang_runner.h 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  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_CLANG_RUNNER_H_
  5. #define CARBON_TOOLCHAIN_DRIVER_CLANG_RUNNER_H_
  6. #include "clang/Basic/DiagnosticIDs.h"
  7. #include "common/ostream.h"
  8. #include "llvm/ADT/ArrayRef.h"
  9. #include "llvm/ADT/StringRef.h"
  10. #include "toolchain/install/install_paths.h"
  11. namespace Carbon {
  12. // Runs Clang in a similar fashion to invoking it with the provided arguments on
  13. // the command line. We use a textual command line interface to allow easily
  14. // incorporating custom command line flags from user invocations that we don't
  15. // parse, but will pass transparently along to Clang itself.
  16. //
  17. // This doesn't literally use a subprocess to invoke Clang; it instead tries to
  18. // directly use the Clang command line driver library. We also work to simplify
  19. // how that driver operates and invoke it in an opinionated way to get the best
  20. // behavior for our expected use cases in the Carbon driver:
  21. //
  22. // - Minimize canonicalization of file names to try to preserve the paths as
  23. // users type them.
  24. // - Minimize the use of subprocess invocations which are expensive on some
  25. // operating systems. To the extent possible, we try to directly invoke the
  26. // Clang logic within this process.
  27. // - Provide programmatic API to control defaults of Clang. For example, causing
  28. // verbose output.
  29. //
  30. // Note that this makes the current process behave like running Clang -- it uses
  31. // standard output and standard error, and otherwise can only read and write
  32. // files based on their names described in the arguments. It doesn't provide any
  33. // higher-level abstraction such as streams for inputs or outputs.
  34. class ClangRunner {
  35. public:
  36. // Build a Clang runner that uses the provided `exe_name` and `err_stream`.
  37. //
  38. // If `verbose` is passed as true, will enable verbose logging to the
  39. // `err_stream` both from the runner and Clang itself.
  40. ClangRunner(const InstallPaths* install_paths, llvm::StringRef target,
  41. llvm::raw_ostream* vlog_stream = nullptr);
  42. // Run Clang with the provided arguments.
  43. auto Run(llvm::ArrayRef<llvm::StringRef> args) -> bool;
  44. private:
  45. const InstallPaths* installation_;
  46. llvm::StringRef target_;
  47. llvm::raw_ostream* vlog_stream_;
  48. llvm::IntrusiveRefCntPtr<clang::DiagnosticIDs> diagnostic_ids_;
  49. };
  50. } // namespace Carbon
  51. #endif // CARBON_TOOLCHAIN_DRIVER_CLANG_RUNNER_H_