clang_runner.cpp 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  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_runner.h"
  5. #include <algorithm>
  6. #include <memory>
  7. #include <numeric>
  8. #include <optional>
  9. #include "clang/Basic/Diagnostic.h"
  10. #include "clang/Basic/DiagnosticOptions.h"
  11. #include "clang/Driver/Compilation.h"
  12. #include "clang/Driver/Driver.h"
  13. #include "clang/Frontend/CompilerInvocation.h"
  14. #include "clang/Frontend/TextDiagnosticPrinter.h"
  15. #include "common/command_line.h"
  16. #include "common/vlog.h"
  17. #include "llvm/ADT/ArrayRef.h"
  18. #include "llvm/ADT/ScopeExit.h"
  19. #include "llvm/ADT/StringExtras.h"
  20. #include "llvm/ADT/StringRef.h"
  21. #include "llvm/IR/LLVMContext.h"
  22. #include "llvm/Support/FileSystem.h"
  23. #include "llvm/Support/Path.h"
  24. #include "llvm/Support/Program.h"
  25. #include "llvm/Support/VirtualFileSystem.h"
  26. #include "llvm/TargetParser/Host.h"
  27. namespace Carbon {
  28. static auto GetExecutablePath(llvm::StringRef exe_name) -> std::string {
  29. // If the `exe_name` isn't already a valid path, look it up.
  30. if (!llvm::sys::fs::exists(exe_name)) {
  31. if (llvm::ErrorOr<std::string> path_result =
  32. llvm::sys::findProgramByName(exe_name)) {
  33. return *path_result;
  34. }
  35. }
  36. return exe_name.str();
  37. }
  38. ClangRunner::ClangRunner(llvm::StringRef exe_name, llvm::StringRef target,
  39. llvm::raw_ostream* vlog_stream)
  40. : exe_name_(exe_name),
  41. exe_path_(GetExecutablePath(exe_name)),
  42. target_(target),
  43. vlog_stream_(vlog_stream),
  44. diagnostic_ids_(new clang::DiagnosticIDs()) {}
  45. auto ClangRunner::Run(llvm::ArrayRef<llvm::StringRef> args) -> bool {
  46. // TODO: Maybe handle response file expansion similar to the Clang CLI?
  47. // If we have a verbose logging stream, and that stream is the same as
  48. // `llvm::errs`, then add the `-v` flag so that the driver also prints verbose
  49. // information.
  50. bool inject_v_arg = vlog_stream_ == &llvm::errs();
  51. std::array<llvm::StringRef, 1> v_arg_storage;
  52. llvm::ArrayRef<llvm::StringRef> maybe_v_arg;
  53. if (inject_v_arg) {
  54. v_arg_storage[0] = "-v";
  55. maybe_v_arg = v_arg_storage;
  56. }
  57. CARBON_CHECK(!args.empty());
  58. CARBON_VLOG() << "Running Clang driver with arguments: \n";
  59. // Render the arguments into null-terminated C-strings for use by the Clang
  60. // driver. Command lines can get quite long in build systems so this tries to
  61. // minimize the memory allocation overhead.
  62. std::array<llvm::StringRef, 1> exe_arg = {exe_name_};
  63. auto args_range =
  64. llvm::concat<const llvm::StringRef>(exe_arg, maybe_v_arg, args);
  65. int total_size = 0;
  66. for (llvm::StringRef arg : args_range) {
  67. // Accumulate both the string size and a null terminator byte.
  68. total_size += arg.size() + 1;
  69. }
  70. // Allocate one chunk of storage for the actual C-strings and a vector of
  71. // pointers into the storage.
  72. llvm::OwningArrayRef<char> cstr_arg_storage(total_size);
  73. llvm::SmallVector<const char*, 64> cstr_args;
  74. cstr_args.reserve(args.size() + inject_v_arg + 1);
  75. for (ssize_t i = 0; llvm::StringRef arg : args_range) {
  76. cstr_args.push_back(&cstr_arg_storage[i]);
  77. memcpy(&cstr_arg_storage[i], arg.data(), arg.size());
  78. i += arg.size();
  79. cstr_arg_storage[i] = '\0';
  80. ++i;
  81. }
  82. for (const char* cstr_arg : llvm::ArrayRef(cstr_args).drop_front()) {
  83. CARBON_VLOG() << " '" << cstr_arg << "'\n";
  84. }
  85. CARBON_VLOG() << "Preparing Clang driver...\n";
  86. // Create the diagnostic options and parse arguments controlling them out of
  87. // our arguments.
  88. llvm::IntrusiveRefCntPtr<clang::DiagnosticOptions> diagnostic_options =
  89. clang::CreateAndPopulateDiagOpts(cstr_args);
  90. // TODO: We don't yet support serializing diagnostics the way the actual
  91. // `clang` command line does. Unclear if we need to or not, but it would need
  92. // a bit more logic here to set up chained consumers.
  93. clang::TextDiagnosticPrinter diagnostic_client(llvm::errs(),
  94. diagnostic_options.get());
  95. clang::DiagnosticsEngine diagnostics(
  96. diagnostic_ids_, diagnostic_options.get(), &diagnostic_client,
  97. /*ShouldOwnClient=*/false);
  98. clang::ProcessWarningOptions(diagnostics, *diagnostic_options);
  99. clang::driver::Driver driver(exe_path_, target_, diagnostics);
  100. // TODO: Directly run in-process rather than using a subprocess. This is both
  101. // more efficient and makes debugging (much) easier. Needs code like:
  102. // driver.CC1Main = [](llvm::SmallVectorImpl<const char*>& argv) {};
  103. std::unique_ptr<clang::driver::Compilation> compilation(
  104. driver.BuildCompilation(cstr_args));
  105. CARBON_CHECK(compilation) << "Should always successfully allocate!";
  106. if (compilation->containsError()) {
  107. // These should have been diagnosed by the driver.
  108. return false;
  109. }
  110. CARBON_VLOG() << "Running Clang driver...\n";
  111. llvm::SmallVector<std::pair<int, const clang::driver::Command*>>
  112. failing_commands;
  113. int result = driver.ExecuteCompilation(*compilation, failing_commands);
  114. // Finish diagnosing any failures before we verbosely log the source of those
  115. // failures.
  116. diagnostic_client.finish();
  117. CARBON_VLOG() << "Execution result code: " << result << "\n";
  118. for (const auto& [command_result, failing_command] : failing_commands) {
  119. CARBON_VLOG() << "Failing command '" << failing_command->getExecutable()
  120. << "' with code '" << command_result << "' was:\n";
  121. if (vlog_stream_) {
  122. failing_command->Print(*vlog_stream_, "\n\n", /*Quote=*/true);
  123. }
  124. }
  125. // Return whether the command was executed successfully.
  126. return result == 0 && failing_commands.empty();
  127. }
  128. } // namespace Carbon