clang_runner_test.cpp 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  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 <string>
  9. #include <utility>
  10. #include "common/error_test_helpers.h"
  11. #include "common/ostream.h"
  12. #include "common/raw_string_ostream.h"
  13. #include "llvm/ADT/IntrusiveRefCntPtr.h"
  14. #include "llvm/ADT/Twine.h"
  15. #include "llvm/Object/Binary.h"
  16. #include "llvm/Object/ObjectFile.h"
  17. #include "llvm/Support/VirtualFileSystem.h"
  18. #include "llvm/TargetParser/Host.h"
  19. #include "testing/base/capture_std_streams.h"
  20. #include "testing/base/file_helpers.h"
  21. #include "testing/base/global_exe_path.h"
  22. #include "toolchain/base/install_paths.h"
  23. #include "toolchain/driver/runtimes_cache.h"
  24. namespace Carbon {
  25. namespace {
  26. using ::testing::HasSubstr;
  27. using Testing::IsSuccess;
  28. using ::testing::StrEq;
  29. // NOLINTNEXTLINE(modernize-use-trailing-return-type): Macro based function.
  30. MATCHER_P(TextSymbolNamed, name_matcher, "") {
  31. llvm::Expected<llvm::StringRef> name = arg.getName();
  32. if (auto error = name.takeError()) {
  33. *result_listener << "with an error instead of a name: " << error;
  34. return false;
  35. }
  36. if (!testing::ExplainMatchResult(name_matcher, *name, result_listener)) {
  37. return false;
  38. }
  39. // We have to dig out the section to determine if this was a text symbol.
  40. auto expected_section_it = arg.getSection();
  41. if (auto error = expected_section_it.takeError()) {
  42. *result_listener << "without a section: " << error;
  43. return false;
  44. }
  45. llvm::object::SectionRef section = **expected_section_it;
  46. if (!section.isText()) {
  47. *result_listener << "in the non-text section: " << *section.getName();
  48. return false;
  49. }
  50. return true;
  51. }
  52. class ClangRunnerTest : public ::testing::Test {
  53. public:
  54. InstallPaths install_paths_ =
  55. InstallPaths::MakeForBazelRunfiles(Testing::GetExePath());
  56. Runtimes::Cache runtimes_cache_ =
  57. *Runtimes::Cache::MakeSystem(install_paths_);
  58. llvm::IntrusiveRefCntPtr<llvm::vfs::FileSystem> vfs_ =
  59. llvm::vfs::getRealFileSystem();
  60. };
  61. TEST_F(ClangRunnerTest, Version) {
  62. RawStringOstream test_os;
  63. ClangRunner runner(&install_paths_, vfs_, &test_os);
  64. std::string out;
  65. std::string err;
  66. EXPECT_THAT(
  67. Testing::CallWithCapturedOutput(
  68. out, err, [&] { return runner.RunWithNoRuntimes({"--version"}); }),
  69. IsSuccess(true));
  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_, vfs_, &verbose_out);
  91. std::string out;
  92. std::string err;
  93. EXPECT_THAT(Testing::CallWithCapturedOutput(
  94. out, err,
  95. [&] {
  96. return runner.RunWithNoRuntimes(
  97. {"-c", test_file.string(), "-o", test_output.string()});
  98. }),
  99. IsSuccess(true))
  100. << "Verbose output from runner:\n"
  101. << verbose_out.TakeStr() << "\n";
  102. verbose_out.clear();
  103. // No output should be produced.
  104. EXPECT_THAT(out, StrEq(""));
  105. EXPECT_THAT(err, StrEq(""));
  106. }
  107. TEST_F(ClangRunnerTest, BuitinHeaders) {
  108. std::filesystem::path test_file = *Testing::WriteTestFile("test.c", R"cpp(
  109. #include <stdalign.h>
  110. #ifndef alignas
  111. #error included the wrong header
  112. #endif
  113. )cpp");
  114. std::filesystem::path test_output = *Testing::WriteTestFile("test.o", "");
  115. RawStringOstream verbose_out;
  116. ClangRunner runner(&install_paths_, vfs_, &verbose_out);
  117. std::string out;
  118. std::string err;
  119. EXPECT_THAT(Testing::CallWithCapturedOutput(
  120. out, err,
  121. [&] {
  122. return runner.RunWithNoRuntimes(
  123. {"-c", test_file.string(), "-o", test_output.string()});
  124. }),
  125. IsSuccess(true))
  126. << "Verbose output from runner:\n"
  127. << verbose_out.TakeStr() << "\n";
  128. verbose_out.clear();
  129. // No output should be produced.
  130. EXPECT_THAT(out, StrEq(""));
  131. EXPECT_THAT(err, StrEq(""));
  132. }
  133. TEST_F(ClangRunnerTest, CompileMultipleFiles) {
  134. // Memory leaks and other errors from running Clang can at times only manifest
  135. // with repeated compilations. Use a lambda to just do a series of compiles.
  136. auto compile = [&](llvm::StringRef filename, llvm::StringRef source) {
  137. std::string output_file = std::string(filename.split('.').first) + ".o";
  138. std::filesystem::path file = *Testing::WriteTestFile(filename, source);
  139. std::filesystem::path output = *Testing::WriteTestFile(output_file, "");
  140. RawStringOstream verbose_out;
  141. ClangRunner runner(&install_paths_, vfs_, &verbose_out);
  142. std::string out;
  143. std::string err;
  144. EXPECT_THAT(Testing::CallWithCapturedOutput(
  145. out, err,
  146. [&] {
  147. return runner.RunWithNoRuntimes(
  148. {"-c", file.string(), "-o", output.string()});
  149. }),
  150. IsSuccess(true))
  151. << "Verbose output from runner:\n"
  152. << verbose_out.TakeStr() << "\n";
  153. verbose_out.clear();
  154. EXPECT_THAT(out, StrEq(""));
  155. EXPECT_THAT(err, StrEq(""));
  156. };
  157. compile("test1.cpp", "int test1() { return 0; }");
  158. compile("test2.cpp", "int test2() { return 0; }");
  159. compile("test3.cpp", "int test3() { return 0; }");
  160. }
  161. // It's hard to write a portable and reliable unittest for all the layers of the
  162. // Clang driver because they work hard to interact with the underlying
  163. // filesystem and operating system. For now, we just check that a link command
  164. // is echoed back with plausible contents.
  165. //
  166. // TODO: We should eventually strive to have a more complete setup that lets us
  167. // test more complete Clang functionality here.
  168. TEST_F(ClangRunnerTest, LinkCommandEcho) {
  169. // Just create some empty files to use in a synthetic link command below.
  170. std::filesystem::path foo_file = *Testing::WriteTestFile("foo.o", "");
  171. std::filesystem::path bar_file = *Testing::WriteTestFile("bar.o", "");
  172. RawStringOstream verbose_out;
  173. ClangRunner runner(&install_paths_, vfs_, &verbose_out);
  174. std::string out;
  175. std::string err;
  176. EXPECT_THAT(
  177. Testing::CallWithCapturedOutput(
  178. out, err,
  179. [&] {
  180. // Note that we use the target independent run command here because
  181. // we're just getting the echo-ed output back. For this to actually
  182. // link, we'd need to have the target-dependent resources, but those
  183. // are expensive to build so we only want to test them once (above).
  184. return runner.RunWithNoRuntimes(
  185. {"-###", "-o", "binary", foo_file.string(), bar_file.string()});
  186. }),
  187. IsSuccess(true))
  188. << "Verbose output from runner:\n"
  189. << verbose_out.TakeStr() << "\n";
  190. verbose_out.clear();
  191. // Because we use `-###' above, we should just see the command that the Clang
  192. // driver would have run in a subprocess. This will be very architecture
  193. // dependent and have lots of variety, but we expect to see both file strings
  194. // in it the command at least.
  195. EXPECT_THAT(err, HasSubstr(foo_file.string())) << err;
  196. EXPECT_THAT(err, HasSubstr(bar_file.string())) << err;
  197. // And no non-stderr output should be produced.
  198. EXPECT_THAT(out, StrEq(""));
  199. }
  200. TEST_F(ClangRunnerTest, ParamsFile) {
  201. // Use an overlay file system to ensure the params file expansion goes through
  202. // the VFS.
  203. llvm::IntrusiveRefCntPtr<llvm::vfs::OverlayFileSystem> overlay_fs(
  204. new llvm::vfs::OverlayFileSystem(vfs_));
  205. llvm::IntrusiveRefCntPtr<llvm::vfs::InMemoryFileSystem> in_memory_fs(
  206. new llvm::vfs::InMemoryFileSystem);
  207. overlay_fs->pushOverlay(in_memory_fs);
  208. std::filesystem::path params_path = "/params";
  209. in_memory_fs->addFile(params_path.native(), 0,
  210. llvm::MemoryBuffer::getMemBuffer(R"(
  211. --version
  212. )"));
  213. RawStringOstream verbose_out;
  214. ClangRunner runner(&install_paths_, overlay_fs, &verbose_out);
  215. std::string out;
  216. std::string err;
  217. EXPECT_THAT(
  218. Testing::CallWithCapturedOutput(out, err,
  219. [&] {
  220. return runner.RunWithNoRuntimes(
  221. {"@" + params_path.native()});
  222. }),
  223. IsSuccess(true))
  224. << "Verbose output:\n"
  225. << verbose_out.TakeStr();
  226. verbose_out.clear();
  227. // Check that the version is printed, as if we directly passed `--version`.
  228. EXPECT_THAT(err, StrEq(""));
  229. EXPECT_THAT(out, HasSubstr("clang version"));
  230. }
  231. } // namespace
  232. } // namespace Carbon