file_test.cpp 4.3 KB

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