clang_subcommand.cpp 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  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. #include "toolchain/driver/clang_subcommand.h"
  5. #include <string>
  6. #include "llvm/TargetParser/Host.h"
  7. #include "toolchain/driver/clang_runner.h"
  8. namespace Carbon {
  9. auto ClangOptions::Build(CommandLine::CommandBuilder& b) -> void {
  10. b.AddStringPositionalArg(
  11. {
  12. .name = "ARG",
  13. .help = R"""(
  14. Arguments passed to Clang.
  15. )""",
  16. },
  17. [&](auto& arg_b) { arg_b.Append(&args); });
  18. }
  19. static constexpr CommandLine::CommandInfo SubcommandInfo = {
  20. .name = "clang",
  21. .help = R"""(
  22. Runs Clang on arguments.
  23. This is equivalent to running the `clang` command line directly, and provides
  24. the full command line interface.
  25. Use `carbon clang -- ARGS` to pass flags to `clang`. Although there are
  26. currently no flags for `carbon clang`, the `--` reserves the ability to add
  27. flags in the future.
  28. This is provided to help guarantee consistent compilation of C++ files, both
  29. when Clang is invoked directly and when a Carbon file importing a C++ file
  30. results in an indirect Clang invocation.
  31. )""",
  32. };
  33. ClangSubcommand::ClangSubcommand() : DriverSubcommand(SubcommandInfo) {}
  34. // TODO: This lacks a lot of features from the main driver code. We may need to
  35. // add more.
  36. // https://github.com/llvm/llvm-project/blob/main/clang/tools/driver/driver.cpp
  37. auto ClangSubcommand::Run(DriverEnv& driver_env) -> DriverResult {
  38. ClangRunner runner(driver_env.installation, driver_env.fs,
  39. driver_env.vlog_stream);
  40. // Don't run Clang when fuzzing, it is known to not be reliable under fuzzing
  41. // due to many unfixed issues.
  42. if (TestAndDiagnoseIfFuzzingExternalLibraries(driver_env, "clang")) {
  43. return {.success = false};
  44. }
  45. ErrorOr<bool> run_result = false;
  46. if (driver_env.prebuilt_runtimes) {
  47. run_result = runner.RunWithPrebuiltRuntimes(options_.args,
  48. *driver_env.prebuilt_runtimes,
  49. driver_env.enable_leaking);
  50. } else if (driver_env.build_runtimes_on_demand) {
  51. run_result = runner.Run(options_.args, driver_env.runtimes_cache,
  52. *driver_env.thread_pool, driver_env.enable_leaking);
  53. } else {
  54. run_result =
  55. runner.RunWithNoRuntimes(options_.args, driver_env.enable_leaking);
  56. }
  57. if (!run_result.ok()) {
  58. // This is not a Clang failure, but a failure to even run Clang, so we need
  59. // to diagnose it here.
  60. CARBON_DIAGNOSTIC(FailureRunningClang, Error,
  61. "failure running `clang` subcommand: {0}", std::string);
  62. driver_env.emitter.Emit(FailureRunningClang, run_result.error().message());
  63. return {.success = false};
  64. }
  65. // Successfully ran Clang, but return whether Clang itself succeeded.
  66. return {.success = *run_result};
  67. }
  68. } // namespace Carbon