file_test.cpp 4.9 KB

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