clang_runner.h 2.4 KB

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