clang_runner_test.cpp 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  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 <utility>
  10. #include "common/check.h"
  11. #include "common/ostream.h"
  12. #include "common/raw_string_ostream.h"
  13. #include "llvm/ADT/ScopeExit.h"
  14. #include "llvm/Object/Binary.h"
  15. #include "llvm/Support/FormatVariadic.h"
  16. #include "llvm/Support/Program.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. namespace Carbon {
  22. namespace {
  23. using ::testing::HasSubstr;
  24. using ::testing::StrEq;
  25. TEST(ClangRunnerTest, Version) {
  26. RawStringOstream test_os;
  27. const auto install_paths =
  28. InstallPaths::MakeForBazelRunfiles(Testing::GetExePath());
  29. std::string target = llvm::sys::getDefaultTargetTriple();
  30. auto vfs = llvm::vfs::getRealFileSystem();
  31. ClangRunner runner(&install_paths, target, vfs, &test_os);
  32. std::string out;
  33. std::string err;
  34. EXPECT_TRUE(Testing::CallWithCapturedOutput(
  35. out, err, [&] { return runner.Run({"--version"}); }));
  36. // The arguments to Clang should be part of the verbose log.
  37. EXPECT_THAT(test_os.TakeStr(), HasSubstr("--version"));
  38. // No need to flush stderr, just check its contents.
  39. EXPECT_THAT(err, StrEq(""));
  40. // Flush and get the captured stdout to test that this command worked.
  41. // We don't care about any particular version, just that it is printed.
  42. EXPECT_THAT(out, HasSubstr("clang version"));
  43. // The target should match what we provided.
  44. EXPECT_THAT(out, HasSubstr((llvm::Twine("Target: ") + target).str()));
  45. // Clang's install should be our private LLVM install bin directory.
  46. EXPECT_THAT(out, HasSubstr(std::string("InstalledDir: ") +
  47. install_paths.llvm_install_bin()));
  48. }
  49. // It's hard to write a portable and reliable unittest for all the layers of the
  50. // Clang driver because they work hard to interact with the underlying
  51. // filesystem and operating system. For now, we just check that a link command
  52. // is echoed back with plausible contents.
  53. //
  54. // TODO: We should eventually strive to have a more complete setup that lets us
  55. // test more complete Clang functionality here.
  56. TEST(ClangRunnerTest, LinkCommandEcho) {
  57. // Just create some empty files to use in a synthetic link command below.
  58. std::filesystem::path foo_file = *Testing::WriteTestFile("foo.o", "");
  59. std::filesystem::path bar_file = *Testing::WriteTestFile("bar.o", "");
  60. const auto install_paths =
  61. InstallPaths::MakeForBazelRunfiles(Testing::GetExePath());
  62. RawStringOstream verbose_out;
  63. std::string target = llvm::sys::getDefaultTargetTriple();
  64. auto vfs = llvm::vfs::getRealFileSystem();
  65. ClangRunner runner(&install_paths, target, vfs, &verbose_out);
  66. std::string out;
  67. std::string err;
  68. EXPECT_TRUE(Testing::CallWithCapturedOutput(
  69. out, err,
  70. [&] {
  71. return runner.Run(
  72. {"-###", "-o", "binary", foo_file.string(), bar_file.string()});
  73. }))
  74. << "Verbose output from runner:\n"
  75. << verbose_out.TakeStr() << "\n";
  76. verbose_out.clear();
  77. // Because we use `-###' above, we should just see the command that the Clang
  78. // driver would have run in a subprocess. This will be very architecture
  79. // dependent and have lots of variety, but we expect to see both file strings
  80. // in it the command at least.
  81. EXPECT_THAT(err, HasSubstr(foo_file.string())) << err;
  82. EXPECT_THAT(err, HasSubstr(bar_file.string())) << err;
  83. // And no non-stderr output should be produced.
  84. EXPECT_THAT(out, StrEq(""));
  85. }
  86. TEST(ClangRunnerTest, DashC) {
  87. std::filesystem::path test_file =
  88. *Testing::WriteTestFile("test.cpp", "int test() { return 0; }");
  89. std::filesystem::path test_output = *Testing::WriteTestFile("test.o", "");
  90. const auto install_paths =
  91. InstallPaths::MakeForBazelRunfiles(Testing::GetExePath());
  92. RawStringOstream verbose_out;
  93. std::string target = llvm::sys::getDefaultTargetTriple();
  94. auto vfs = llvm::vfs::getRealFileSystem();
  95. ClangRunner runner(&install_paths, target, vfs, &verbose_out);
  96. std::string out;
  97. std::string err;
  98. EXPECT_TRUE(Testing::CallWithCapturedOutput(
  99. out, err,
  100. [&] {
  101. return runner.Run(
  102. {"-c", test_file.string(), "-o", test_output.string()});
  103. }))
  104. << "Verbose output from runner:\n"
  105. << verbose_out.TakeStr() << "\n";
  106. verbose_out.clear();
  107. // No output should be produced.
  108. EXPECT_THAT(out, StrEq(""));
  109. EXPECT_THAT(err, StrEq(""));
  110. }
  111. TEST(ClangRunnerTest, BuitinHeaders) {
  112. std::filesystem::path test_file = *Testing::WriteTestFile("test.c", R"cpp(
  113. #include <stdalign.h>
  114. #ifndef alignas
  115. #error included the wrong header
  116. #endif
  117. )cpp");
  118. std::filesystem::path test_output = *Testing::WriteTestFile("test.o", "");
  119. const auto install_paths =
  120. InstallPaths::MakeForBazelRunfiles(Testing::GetExePath());
  121. RawStringOstream verbose_out;
  122. std::string target = llvm::sys::getDefaultTargetTriple();
  123. auto vfs = llvm::vfs::getRealFileSystem();
  124. ClangRunner runner(&install_paths, target, vfs, &verbose_out);
  125. std::string out;
  126. std::string err;
  127. EXPECT_TRUE(Testing::CallWithCapturedOutput(
  128. out, err,
  129. [&] {
  130. return runner.Run(
  131. {"-c", test_file.string(), "-o", test_output.string()});
  132. }))
  133. << "Verbose output from runner:\n"
  134. << verbose_out.TakeStr() << "\n";
  135. verbose_out.clear();
  136. // No output should be produced.
  137. EXPECT_THAT(out, StrEq(""));
  138. EXPECT_THAT(err, StrEq(""));
  139. }
  140. } // namespace
  141. } // namespace Carbon