file_test.cpp 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  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. #ifndef CARBON_TOOLCHAIN_DRIVER_DRIVER_FILE_TEST_BASE_H_
  5. #define CARBON_TOOLCHAIN_DRIVER_DRIVER_FILE_TEST_BASE_H_
  6. #include <string>
  7. #include "common/error.h"
  8. #include "llvm/ADT/STLExtras.h"
  9. #include "llvm/ADT/SmallVector.h"
  10. #include "llvm/ADT/StringRef.h"
  11. #include "llvm/Support/FormatVariadic.h"
  12. #include "llvm/Support/VirtualFileSystem.h"
  13. #include "testing/file_test/file_test_base.h"
  14. #include "toolchain/driver/driver.h"
  15. namespace Carbon::Testing {
  16. namespace {
  17. // Provides common test support for the driver. This is used by file tests in
  18. // phase subdirectories.
  19. class ToolchainFileTest : public FileTestBase {
  20. public:
  21. explicit ToolchainFileTest(llvm::StringRef test_name)
  22. : FileTestBase(test_name), component_(GetComponent(test_name)) {}
  23. auto Run(const llvm::SmallVector<llvm::StringRef>& test_args,
  24. llvm::vfs::InMemoryFileSystem& fs, llvm::raw_pwrite_stream& stdout,
  25. llvm::raw_pwrite_stream& stderr) -> ErrorOr<RunResult> override {
  26. const llvm::StringRef data_dir = "";
  27. auto prelude = Driver::FindPreludeFiles(data_dir, stderr);
  28. if (prelude.empty()) {
  29. return Error("Could not find prelude");
  30. }
  31. for (const auto& file : prelude) {
  32. CARBON_RETURN_IF_ERROR(AddFile(fs, file));
  33. }
  34. Driver driver(fs, data_dir, stdout, stderr);
  35. auto driver_result = driver.RunCommand(test_args);
  36. RunResult result{
  37. .success = driver_result.success,
  38. .per_file_success = std::move(driver_result.per_file_success)};
  39. // Drop entries that don't look like a file, and entries corresponding to
  40. // the prelude. Note this can empty out the list.
  41. llvm::erase_if(result.per_file_success,
  42. [&](std::pair<llvm::StringRef, bool> entry) {
  43. return entry.first == "." || entry.first == "-" ||
  44. entry.first.starts_with("not_file") ||
  45. llvm::is_contained(prelude, entry.first);
  46. });
  47. return result;
  48. }
  49. auto GetDefaultArgs() -> llvm::SmallVector<std::string> override {
  50. llvm::SmallVector<std::string> args = {"compile",
  51. "--phase=" + component_.str()};
  52. if (component_ == "lex") {
  53. args.push_back("--dump-tokens");
  54. } else if (component_ == "parse") {
  55. args.push_back("--dump-parse-tree");
  56. } else if (component_ == "check") {
  57. args.push_back("--dump-sem-ir");
  58. } else if (component_ == "lower") {
  59. args.push_back("--dump-llvm-ir");
  60. } else {
  61. CARBON_FATAL() << "Unexpected test component " << component_ << ": "
  62. << test_name();
  63. }
  64. args.insert(args.end(), {"--exclude-dump-file-prefix=core/", "%s"});
  65. return args;
  66. }
  67. auto GetDefaultFileRE(llvm::ArrayRef<llvm::StringRef> filenames)
  68. -> std::optional<RE2> override {
  69. if (component_ == "lex") {
  70. return std::make_optional<RE2>(
  71. llvm::formatv(R"(^- filename: ({0})$)", llvm::join(filenames, "|")));
  72. }
  73. return FileTestBase::GetDefaultFileRE(filenames);
  74. }
  75. auto GetLineNumberReplacements(llvm::ArrayRef<llvm::StringRef> filenames)
  76. -> llvm::SmallVector<LineNumberReplacement> override {
  77. auto replacements = FileTestBase::GetLineNumberReplacements(filenames);
  78. if (component_ == "lex") {
  79. replacements.push_back({.has_file = false,
  80. .re = std::make_shared<RE2>(R"(line: (\s*\d+))"),
  81. // The `{{{{` becomes `{{`.
  82. .line_formatv = "{{{{ *}}{0}"});
  83. }
  84. return replacements;
  85. }
  86. auto DoExtraCheckReplacements(std::string& check_line) -> void override {
  87. if (component_ == "driver") {
  88. // TODO: Disable token output, it's not interesting for these tests.
  89. if (llvm::StringRef(check_line).starts_with("// CHECK:STDOUT: {")) {
  90. check_line = "// CHECK:STDOUT: {{.*}}";
  91. }
  92. } else if (component_ == "lex") {
  93. // Both FileStart and FileEnd regularly have locations on CHECK
  94. // comment lines that don't work correctly. The line happens to be correct
  95. // for the FileEnd, but we need to avoid checking the column.
  96. // The column happens to be right for FileStart, but the line is wrong.
  97. static RE2 file_token_re(
  98. R"((FileEnd.*column: |FileStart.*line: )( *\d+))");
  99. RE2::Replace(&check_line, file_token_re, R"(\1{{ *\\d+}})");
  100. } else {
  101. return FileTestBase::DoExtraCheckReplacements(check_line);
  102. }
  103. }
  104. private:
  105. // Adds a file to the fs.
  106. auto AddFile(llvm::vfs::InMemoryFileSystem& fs, llvm::StringRef path)
  107. -> ErrorOr<Success> {
  108. llvm::ErrorOr<std::unique_ptr<llvm::MemoryBuffer>> file =
  109. llvm::MemoryBuffer::getFile(path);
  110. if (file.getError()) {
  111. return ErrorBuilder() << file.getError().message();
  112. }
  113. if (!fs.addFile(path, /*ModificationTime=*/0, std::move(*file))) {
  114. return ErrorBuilder() << "Duplicate file: " << path;
  115. }
  116. return Success();
  117. }
  118. // Returns the toolchain subdirectory being tested.
  119. static auto GetComponent(llvm::StringRef test_name) -> llvm::StringRef {
  120. // This handles cases where the toolchain directory may be copied into a
  121. // repository that doesn't put it at the root.
  122. auto pos = test_name.find("toolchain/");
  123. CARBON_CHECK(pos != llvm::StringRef::npos) << test_name;
  124. test_name = test_name.drop_front(pos + strlen("toolchain/"));
  125. test_name = test_name.take_front(test_name.find("/"));
  126. return test_name;
  127. }
  128. const llvm::StringRef component_;
  129. };
  130. } // namespace
  131. CARBON_FILE_TEST_FACTORY(ToolchainFileTest);
  132. } // namespace Carbon::Testing
  133. #endif // CARBON_TOOLCHAIN_DRIVER_DRIVER_FILE_TEST_BASE_H_