clang_subcommand.cpp 2.1 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. #include "toolchain/driver/clang_subcommand.h"
  5. #include "llvm/TargetParser/Host.h"
  6. #include "toolchain/driver/clang_runner.h"
  7. namespace Carbon {
  8. auto ClangOptions::Build(CommandLine::CommandBuilder& b) -> void {
  9. b.AddStringPositionalArg(
  10. {
  11. .name = "ARG",
  12. .help = R"""(
  13. Arguments passed to Clang.
  14. )""",
  15. },
  16. [&](auto& arg_b) { arg_b.Append(&args); });
  17. }
  18. static constexpr CommandLine::CommandInfo SubcommandInfo = {
  19. .name = "clang",
  20. .help = R"""(
  21. Runs Clang on arguments.
  22. This is equivalent to running the `clang` command line directly, and provides
  23. the full command line interface.
  24. Use `carbon clang -- ARGS` to pass flags to `clang`. Although there are
  25. currently no flags for `carbon clang`, the `--` reserves the ability to add
  26. flags in the future.
  27. This is provided to help guarantee consistent compilation of C++ files, both
  28. when Clang is invoked directly and when a Carbon file importing a C++ file
  29. results in an indirect Clang invocation.
  30. )""",
  31. };
  32. ClangSubcommand::ClangSubcommand() : DriverSubcommand(SubcommandInfo) {}
  33. // TODO: This lacks a lot of features from the main driver code. We may need to
  34. // add more.
  35. // https://github.com/llvm/llvm-project/blob/main/clang/tools/driver/driver.cpp
  36. auto ClangSubcommand::Run(DriverEnv& driver_env) -> DriverResult {
  37. std::string target = llvm::sys::getDefaultTargetTriple();
  38. ClangRunner runner(driver_env.installation, target, 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 (driver_env.fuzzing) {
  43. CARBON_DIAGNOSTIC(
  44. ClangFuzzingDisallowed, Error,
  45. "preventing fuzzing of `clang` subcommand due to library crashes");
  46. driver_env.emitter.Emit(ClangFuzzingDisallowed);
  47. return {.success = false};
  48. }
  49. return {.success = runner.Run(options_.args)};
  50. }
  51. } // namespace Carbon