clang_runner.h 3.2 KB

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