clang_subcommand.cpp 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  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. std::string target = llvm::sys::getDefaultTargetTriple();
  39. ClangRunner runner(driver_env.installation, target, driver_env.fs,
  40. driver_env.vlog_stream);
  41. // Don't run Clang when fuzzing, it is known to not be reliable under fuzzing
  42. // due to many unfixed issues.
  43. if (!DisableFuzzingExternalLibraries(driver_env, "clang")) {
  44. return {.success = false};
  45. }
  46. // Only enable Clang's leaking of memory if the driver can support that.
  47. if (driver_env.enable_leaking) {
  48. runner.EnableLeakingMemory();
  49. }
  50. return {.success = runner.Run(options_.args)};
  51. }
  52. } // namespace Carbon