clang_runner_test.cpp 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  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/ostream.h"
  11. #include "common/raw_string_ostream.h"
  12. #include "llvm/ADT/IntrusiveRefCntPtr.h"
  13. #include "llvm/ADT/Twine.h"
  14. #include "llvm/Object/Binary.h"
  15. #include "llvm/Object/ObjectFile.h"
  16. #include "llvm/Support/VirtualFileSystem.h"
  17. #include "llvm/TargetParser/Host.h"
  18. #include "testing/base/capture_std_streams.h"
  19. #include "testing/base/file_helpers.h"
  20. #include "testing/base/global_exe_path.h"
  21. #include "toolchain/base/install_paths.h"
  22. #include "toolchain/driver/runtimes_cache.h"
  23. namespace Carbon {
  24. namespace {
  25. using ::testing::HasSubstr;
  26. using ::testing::StrEq;
  27. // NOLINTNEXTLINE(modernize-use-trailing-return-type): Macro based function.
  28. MATCHER_P(TextSymbolNamed, name_matcher, "") {
  29. llvm::Expected<llvm::StringRef> name = arg.getName();
  30. if (auto error = name.takeError()) {
  31. *result_listener << "with an error instead of a name: " << error;
  32. return false;
  33. }
  34. if (!testing::ExplainMatchResult(name_matcher, *name, result_listener)) {
  35. return false;
  36. }
  37. // We have to dig out the section to determine if this was a text symbol.
  38. auto expected_section_it = arg.getSection();
  39. if (auto error = expected_section_it.takeError()) {
  40. *result_listener << "without a section: " << error;
  41. return false;
  42. }
  43. llvm::object::SectionRef section = **expected_section_it;
  44. if (!section.isText()) {
  45. *result_listener << "in the non-text section: " << *section.getName();
  46. return false;
  47. }
  48. return true;
  49. }
  50. class ClangRunnerTest : public ::testing::Test {
  51. public:
  52. InstallPaths install_paths_ =
  53. InstallPaths::MakeForBazelRunfiles(Testing::GetExePath());
  54. Runtimes::Cache runtimes_cache_ =
  55. *Runtimes::Cache::MakeSystem(install_paths_);
  56. llvm::IntrusiveRefCntPtr<llvm::vfs::FileSystem> vfs_ =
  57. llvm::vfs::getRealFileSystem();
  58. };
  59. TEST_F(ClangRunnerTest, Version) {
  60. RawStringOstream test_os;
  61. ClangRunner runner(&install_paths_, vfs_, &test_os);
  62. std::string out;
  63. std::string err;
  64. EXPECT_TRUE(Testing::CallWithCapturedOutput(
  65. out, err, [&] { return runner.RunWithNoRuntimes({"--version"}); }));
  66. // The arguments to Clang should be part of the verbose log.
  67. EXPECT_THAT(test_os.TakeStr(), HasSubstr("--version"));
  68. // No need to flush stderr, just check its contents.
  69. EXPECT_THAT(err, StrEq(""));
  70. // Flush and get the captured stdout to test that this command worked.
  71. // We don't care about any particular version, just that it is printed.
  72. EXPECT_THAT(out, HasSubstr("clang version"));
  73. // The target should match the LLVM default.
  74. EXPECT_THAT(out, HasSubstr((llvm::Twine("Target: ") +
  75. llvm::sys::getDefaultTargetTriple())
  76. .str()));
  77. // Clang's install should be our private LLVM install bin directory.
  78. EXPECT_THAT(out, HasSubstr(std::string("InstalledDir: ") +
  79. install_paths_.llvm_install_bin().native()));
  80. }
  81. TEST_F(ClangRunnerTest, DashC) {
  82. std::filesystem::path test_file =
  83. *Testing::WriteTestFile("test.cpp", "int test() { return 0; }");
  84. std::filesystem::path test_output = *Testing::WriteTestFile("test.o", "");
  85. RawStringOstream verbose_out;
  86. ClangRunner runner(&install_paths_, vfs_, &verbose_out);
  87. std::string out;
  88. std::string err;
  89. EXPECT_TRUE(Testing::CallWithCapturedOutput(
  90. out, err,
  91. [&] {
  92. return runner.RunWithNoRuntimes(
  93. {"-c", test_file.string(), "-o", test_output.string()});
  94. }))
  95. << "Verbose output from runner:\n"
  96. << verbose_out.TakeStr() << "\n";
  97. verbose_out.clear();
  98. // No output should be produced.
  99. EXPECT_THAT(out, StrEq(""));
  100. EXPECT_THAT(err, StrEq(""));
  101. }
  102. TEST_F(ClangRunnerTest, BuitinHeaders) {
  103. std::filesystem::path test_file = *Testing::WriteTestFile("test.c", R"cpp(
  104. #include <stdalign.h>
  105. #ifndef alignas
  106. #error included the wrong header
  107. #endif
  108. )cpp");
  109. std::filesystem::path test_output = *Testing::WriteTestFile("test.o", "");
  110. RawStringOstream verbose_out;
  111. ClangRunner runner(&install_paths_, vfs_, &verbose_out);
  112. std::string out;
  113. std::string err;
  114. EXPECT_TRUE(Testing::CallWithCapturedOutput(
  115. out, err,
  116. [&] {
  117. return runner.RunWithNoRuntimes(
  118. {"-c", test_file.string(), "-o", test_output.string()});
  119. }))
  120. << "Verbose output from runner:\n"
  121. << verbose_out.TakeStr() << "\n";
  122. verbose_out.clear();
  123. // No output should be produced.
  124. EXPECT_THAT(out, StrEq(""));
  125. EXPECT_THAT(err, StrEq(""));
  126. }
  127. TEST_F(ClangRunnerTest, CompileMultipleFiles) {
  128. // Memory leaks and other errors from running Clang can at times only manifest
  129. // with repeated compilations. Use a lambda to just do a series of compiles.
  130. auto compile = [&](llvm::StringRef filename, llvm::StringRef source) {
  131. std::string output_file = std::string(filename.split('.').first) + ".o";
  132. std::filesystem::path file = *Testing::WriteTestFile(filename, source);
  133. std::filesystem::path output = *Testing::WriteTestFile(output_file, "");
  134. RawStringOstream verbose_out;
  135. ClangRunner runner(&install_paths_, vfs_, &verbose_out);
  136. std::string out;
  137. std::string err;
  138. EXPECT_TRUE(Testing::CallWithCapturedOutput(
  139. out, err,
  140. [&] {
  141. return runner.RunWithNoRuntimes(
  142. {"-c", file.string(), "-o", output.string()});
  143. }))
  144. << "Verbose output from runner:\n"
  145. << verbose_out.TakeStr() << "\n";
  146. verbose_out.clear();
  147. EXPECT_THAT(out, StrEq(""));
  148. EXPECT_THAT(err, StrEq(""));
  149. };
  150. compile("test1.cpp", "int test1() { return 0; }");
  151. compile("test2.cpp", "int test2() { return 0; }");
  152. compile("test3.cpp", "int test3() { return 0; }");
  153. }
  154. // It's hard to write a portable and reliable unittest for all the layers of the
  155. // Clang driver because they work hard to interact with the underlying
  156. // filesystem and operating system. For now, we just check that a link command
  157. // is echoed back with plausible contents.
  158. //
  159. // TODO: We should eventually strive to have a more complete setup that lets us
  160. // test more complete Clang functionality here.
  161. TEST_F(ClangRunnerTest, LinkCommandEcho) {
  162. // Just create some empty files to use in a synthetic link command below.
  163. std::filesystem::path foo_file = *Testing::WriteTestFile("foo.o", "");
  164. std::filesystem::path bar_file = *Testing::WriteTestFile("bar.o", "");
  165. RawStringOstream verbose_out;
  166. ClangRunner runner(&install_paths_, vfs_, &verbose_out);
  167. std::string out;
  168. std::string err;
  169. EXPECT_TRUE(Testing::CallWithCapturedOutput(
  170. out, err,
  171. [&] {
  172. // Note that we use the target independent run command here because
  173. // we're just getting the echo-ed output back. For this to actually
  174. // link, we'd need to have the target-dependent resources, but those are
  175. // expensive to build so we only want to test them once (above).
  176. return runner.RunWithNoRuntimes(
  177. {"-###", "-o", "binary", foo_file.string(), bar_file.string()});
  178. }))
  179. << "Verbose output from runner:\n"
  180. << verbose_out.TakeStr() << "\n";
  181. verbose_out.clear();
  182. // Because we use `-###' above, we should just see the command that the Clang
  183. // driver would have run in a subprocess. This will be very architecture
  184. // dependent and have lots of variety, but we expect to see both file strings
  185. // in it the command at least.
  186. EXPECT_THAT(err, HasSubstr(foo_file.string())) << err;
  187. EXPECT_THAT(err, HasSubstr(bar_file.string())) << err;
  188. // And no non-stderr output should be produced.
  189. EXPECT_THAT(out, StrEq(""));
  190. }
  191. } // namespace
  192. } // namespace Carbon