clang_runner_test.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  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 <gmock/gmock.h>
  6. #include <gtest/gtest.h>
  7. #include <filesystem>
  8. #include <fstream>
  9. #include <string>
  10. #include <utility>
  11. #include "common/check.h"
  12. #include "common/ostream.h"
  13. #include "common/raw_string_ostream.h"
  14. #include "llvm/ADT/ScopeExit.h"
  15. #include "llvm/Object/Binary.h"
  16. #include "llvm/Object/ObjectFile.h"
  17. #include "llvm/Support/FormatVariadic.h"
  18. #include "llvm/Support/Program.h"
  19. #include "llvm/TargetParser/Host.h"
  20. #include "testing/base/capture_std_streams.h"
  21. #include "testing/base/file_helpers.h"
  22. #include "testing/base/global_exe_path.h"
  23. #include "toolchain/driver/llvm_runner.h"
  24. namespace Carbon {
  25. namespace {
  26. using ::testing::Eq;
  27. using ::testing::HasSubstr;
  28. using ::testing::IsSupersetOf;
  29. using ::testing::StrEq;
  30. // NOLINTNEXTLINE(modernize-use-trailing-return-type): Macro based function.
  31. MATCHER_P(TextSymbolNamed, name_matcher, "") {
  32. llvm::Expected<llvm::StringRef> name = arg.getName();
  33. if (auto error = name.takeError()) {
  34. *result_listener << "with an error instead of a name: " << error;
  35. return false;
  36. }
  37. if (!testing::ExplainMatchResult(name_matcher, *name, result_listener)) {
  38. return false;
  39. }
  40. // We have to dig out the section to determine if this was a text symbol.
  41. auto expected_section_it = arg.getSection();
  42. if (auto error = expected_section_it.takeError()) {
  43. *result_listener << "without a section: " << error;
  44. return false;
  45. }
  46. llvm::object::SectionRef section = **expected_section_it;
  47. if (!section.isText()) {
  48. *result_listener << "in the non-text section: " << *section.getName();
  49. return false;
  50. }
  51. return true;
  52. }
  53. class ClangRunnerTest : public ::testing::Test {
  54. public:
  55. InstallPaths install_paths_ =
  56. InstallPaths::MakeForBazelRunfiles(Testing::GetExePath());
  57. Runtimes::Cache runtimes_cache_ =
  58. *Runtimes::Cache::MakeSystem(install_paths_);
  59. llvm::IntrusiveRefCntPtr<llvm::vfs::FileSystem> vfs_ =
  60. llvm::vfs::getRealFileSystem();
  61. };
  62. TEST_F(ClangRunnerTest, Version) {
  63. RawStringOstream test_os;
  64. ClangRunner runner(&install_paths_, &runtimes_cache_, vfs_, &test_os);
  65. std::string out;
  66. std::string err;
  67. EXPECT_TRUE(Testing::CallWithCapturedOutput(out, err, [&] {
  68. return runner.RunTargetIndependentCommand({"--version"});
  69. }));
  70. // The arguments to Clang should be part of the verbose log.
  71. EXPECT_THAT(test_os.TakeStr(), HasSubstr("--version"));
  72. // No need to flush stderr, just check its contents.
  73. EXPECT_THAT(err, StrEq(""));
  74. // Flush and get the captured stdout to test that this command worked.
  75. // We don't care about any particular version, just that it is printed.
  76. EXPECT_THAT(out, HasSubstr("clang version"));
  77. // The target should match the LLVM default.
  78. EXPECT_THAT(out, HasSubstr((llvm::Twine("Target: ") +
  79. llvm::sys::getDefaultTargetTriple())
  80. .str()));
  81. // Clang's install should be our private LLVM install bin directory.
  82. EXPECT_THAT(out, HasSubstr(std::string("InstalledDir: ") +
  83. install_paths_.llvm_install_bin().native()));
  84. }
  85. TEST_F(ClangRunnerTest, DashC) {
  86. std::filesystem::path test_file =
  87. *Testing::WriteTestFile("test.cpp", "int test() { return 0; }");
  88. std::filesystem::path test_output = *Testing::WriteTestFile("test.o", "");
  89. RawStringOstream verbose_out;
  90. ClangRunner runner(&install_paths_, &runtimes_cache_, vfs_, &verbose_out);
  91. std::string out;
  92. std::string err;
  93. EXPECT_TRUE(Testing::CallWithCapturedOutput(
  94. out, err,
  95. [&] {
  96. return runner.RunTargetIndependentCommand(
  97. {"-c", test_file.string(), "-o", test_output.string()});
  98. }))
  99. << "Verbose output from runner:\n"
  100. << verbose_out.TakeStr() << "\n";
  101. verbose_out.clear();
  102. // No output should be produced.
  103. EXPECT_THAT(out, StrEq(""));
  104. EXPECT_THAT(err, StrEq(""));
  105. }
  106. TEST_F(ClangRunnerTest, BuitinHeaders) {
  107. std::filesystem::path test_file = *Testing::WriteTestFile("test.c", R"cpp(
  108. #include <stdalign.h>
  109. #ifndef alignas
  110. #error included the wrong header
  111. #endif
  112. )cpp");
  113. std::filesystem::path test_output = *Testing::WriteTestFile("test.o", "");
  114. RawStringOstream verbose_out;
  115. ClangRunner runner(&install_paths_, &runtimes_cache_, vfs_, &verbose_out);
  116. std::string out;
  117. std::string err;
  118. EXPECT_TRUE(Testing::CallWithCapturedOutput(
  119. out, err,
  120. [&] {
  121. return runner.RunTargetIndependentCommand(
  122. {"-c", test_file.string(), "-o", test_output.string()});
  123. }))
  124. << "Verbose output from runner:\n"
  125. << verbose_out.TakeStr() << "\n";
  126. verbose_out.clear();
  127. // No output should be produced.
  128. EXPECT_THAT(out, StrEq(""));
  129. EXPECT_THAT(err, StrEq(""));
  130. }
  131. TEST_F(ClangRunnerTest, CompileMultipleFiles) {
  132. // Memory leaks and other errors from running Clang can at times only manifest
  133. // with repeated compilations. Use a lambda to just do a series of compiles.
  134. auto compile = [&](llvm::StringRef filename, llvm::StringRef source) {
  135. std::string output_file = std::string(filename.split('.').first) + ".o";
  136. std::filesystem::path file = *Testing::WriteTestFile(filename, source);
  137. std::filesystem::path output = *Testing::WriteTestFile(output_file, "");
  138. RawStringOstream verbose_out;
  139. ClangRunner runner(&install_paths_, &runtimes_cache_, vfs_, &verbose_out);
  140. std::string out;
  141. std::string err;
  142. EXPECT_TRUE(Testing::CallWithCapturedOutput(
  143. out, err,
  144. [&] {
  145. return runner.RunTargetIndependentCommand(
  146. {"-c", file.string(), "-o", output.string()});
  147. }))
  148. << "Verbose output from runner:\n"
  149. << verbose_out.TakeStr() << "\n";
  150. verbose_out.clear();
  151. EXPECT_THAT(out, StrEq(""));
  152. EXPECT_THAT(err, StrEq(""));
  153. };
  154. compile("test1.cpp", "int test1() { return 0; }");
  155. compile("test2.cpp", "int test2() { return 0; }");
  156. compile("test3.cpp", "int test3() { return 0; }");
  157. }
  158. TEST_F(ClangRunnerTest, BuildResourceDir) {
  159. ClangRunner runner(&install_paths_, &runtimes_cache_, vfs_, &llvm::errs(),
  160. /*build_runtimes_on_demand=*/true);
  161. // Note that we can't test arbitrary targets here as we need to be able to
  162. // compile the builtin functions for the target. We use the default target as
  163. // the most likely to pass.
  164. std::string target = llvm::sys::getDefaultTargetTriple();
  165. llvm::Triple target_triple(target);
  166. Runtimes::Cache::Features features = {.target = target};
  167. auto runtimes = *runtimes_cache_.Lookup(features);
  168. auto tmp_dir = *Filesystem::MakeTmpDir();
  169. auto build_result =
  170. runner.BuildTargetResourceDir(features, runtimes, tmp_dir.abs_path());
  171. ASSERT_TRUE(build_result.ok()) << build_result.error();
  172. std::filesystem::path resource_dir_path = std::move(*build_result);
  173. // For Linux we can directly check the CRT begin/end object files.
  174. if (target_triple.isOSLinux()) {
  175. std::filesystem::path crt_begin_path =
  176. resource_dir_path / "lib" / target / "clang_rt.crtbegin.o";
  177. ASSERT_TRUE(std::filesystem::is_regular_file(crt_begin_path));
  178. auto begin_result =
  179. llvm::object::ObjectFile::createObjectFile(crt_begin_path.native());
  180. llvm::object::ObjectFile& crtbegin = *begin_result->getBinary();
  181. EXPECT_TRUE(crtbegin.isELF());
  182. EXPECT_TRUE(crtbegin.isObject());
  183. EXPECT_THAT(crtbegin.getArch(), Eq(target_triple.getArch()));
  184. llvm::SmallVector<llvm::object::SymbolRef> symbols(crtbegin.symbols());
  185. // The first symbol should come from the source file.
  186. EXPECT_THAT(*symbols.front().getName(), Eq("crtbegin.c"));
  187. // Check for representative symbols of `crtbegin.o` -- we always use
  188. // `.init_array` in our runtimes build so we have predictable functions.
  189. EXPECT_THAT(symbols, IsSupersetOf({TextSymbolNamed("__do_init"),
  190. TextSymbolNamed("__do_fini")}));
  191. std::filesystem::path crt_end_path =
  192. resource_dir_path / "lib" / target / "clang_rt.crtend.o";
  193. ASSERT_TRUE(std::filesystem::is_regular_file(crt_end_path));
  194. auto end_result =
  195. llvm::object::ObjectFile::createObjectFile(crt_end_path.native());
  196. llvm::object::ObjectFile& crtend = *end_result->getBinary();
  197. EXPECT_TRUE(crtend.isELF());
  198. EXPECT_TRUE(crtend.isObject());
  199. EXPECT_THAT(crtend.getArch(), Eq(target_triple.getArch()));
  200. // Just check the source file symbol, not much of interest in the end.
  201. llvm::object::SymbolRef crtend_front_symbol = *crtend.symbol_begin();
  202. EXPECT_THAT(*crtend_front_symbol.getName(), Eq("crtend.c"));
  203. }
  204. // Across all targets, check that the builtins archive exists, and contains a
  205. // relevant symbol by running the `llvm-nm` tool over it. Using `nm` rather
  206. // than directly inspecting the objects is a bit awkward, but lets us easily
  207. // ignore the wrapping in an archive file.
  208. std::filesystem::path builtins_path =
  209. resource_dir_path / "lib" / target / "libclang_rt.builtins.a";
  210. LLVMRunner llvm_runner(&install_paths_, &llvm::errs());
  211. std::string out;
  212. std::string err;
  213. EXPECT_TRUE(Testing::CallWithCapturedOutput(out, err, [&] {
  214. return llvm_runner.Run(LLVMTool::Nm, {builtins_path.native()});
  215. }));
  216. // Check that we found a definition of `__mulodi4`, a builtin function
  217. // provided by Compiler-RT, but not `libgcc` historically. Note that on macOS
  218. // there is a leading `_` due to mangling.
  219. EXPECT_THAT(out, HasSubstr(target_triple.isMacOSX() ? "T ___mulodi4\n"
  220. : "T __mulodi4\n"));
  221. // Check that we don't include the `chkstk` builtins outside of Windows.
  222. if (!target_triple.isOSWindows()) {
  223. EXPECT_THAT(out, Not(HasSubstr("chkstk")));
  224. }
  225. }
  226. // It's hard to write a portable and reliable unittest for all the layers of the
  227. // Clang driver because they work hard to interact with the underlying
  228. // filesystem and operating system. For now, we just check that a link command
  229. // is echoed back with plausible contents.
  230. //
  231. // TODO: We should eventually strive to have a more complete setup that lets us
  232. // test more complete Clang functionality here.
  233. TEST_F(ClangRunnerTest, LinkCommandEcho) {
  234. // Just create some empty files to use in a synthetic link command below.
  235. std::filesystem::path foo_file = *Testing::WriteTestFile("foo.o", "");
  236. std::filesystem::path bar_file = *Testing::WriteTestFile("bar.o", "");
  237. RawStringOstream verbose_out;
  238. ClangRunner runner(&install_paths_, &runtimes_cache_, vfs_, &verbose_out);
  239. std::string out;
  240. std::string err;
  241. EXPECT_TRUE(Testing::CallWithCapturedOutput(
  242. out, err,
  243. [&] {
  244. // Note that we use the target independent run command here because
  245. // we're just getting the echo-ed output back. For this to actually
  246. // link, we'd need to have the target-dependent resources, but those are
  247. // expensive to build so we only want to test them once (above).
  248. return runner.RunTargetIndependentCommand(
  249. {"-###", "-o", "binary", foo_file.string(), bar_file.string()});
  250. }))
  251. << "Verbose output from runner:\n"
  252. << verbose_out.TakeStr() << "\n";
  253. verbose_out.clear();
  254. // Because we use `-###' above, we should just see the command that the Clang
  255. // driver would have run in a subprocess. This will be very architecture
  256. // dependent and have lots of variety, but we expect to see both file strings
  257. // in it the command at least.
  258. EXPECT_THAT(err, HasSubstr(foo_file.string())) << err;
  259. EXPECT_THAT(err, HasSubstr(bar_file.string())) << err;
  260. // And no non-stderr output should be produced.
  261. EXPECT_THAT(out, StrEq(""));
  262. }
  263. } // namespace
  264. } // namespace Carbon