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