file_test.cpp 3.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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/VirtualFileSystem.h"
  10. #include "testing/file_test/file_test_base.h"
  11. #include "toolchain/driver/driver.h"
  12. namespace Carbon::Testing {
  13. namespace {
  14. // Provides common test support for the driver. This is used by file tests in
  15. // phase subdirectories.
  16. class ToolchainFileTest : public FileTestBase {
  17. public:
  18. explicit ToolchainFileTest(llvm::StringRef test_name)
  19. : FileTestBase(test_name), component_(GetComponent(test_name)) {}
  20. auto Run(const llvm::SmallVector<llvm::StringRef>& test_args,
  21. llvm::vfs::InMemoryFileSystem& fs, llvm::raw_pwrite_stream& stdout,
  22. llvm::raw_pwrite_stream& stderr) -> ErrorOr<bool> override {
  23. Driver driver(fs, stdout, stderr);
  24. return driver.RunCommand(test_args);
  25. }
  26. auto GetDefaultArgs() -> llvm::SmallVector<std::string> override {
  27. if (component_ == "check") {
  28. return {"compile", "--phase=check", "--dump-semantics-ir", "%s"};
  29. } else if (component_ == "lex") {
  30. return {"compile", "--phase=lex", "--dump-tokens", "%s"};
  31. } else if (component_ == "lower") {
  32. return {"compile", "--phase=lower", "--dump-llvm-ir", "%s"};
  33. } else if (component_ == "parse") {
  34. return {"compile", "--phase=parse", "--dump-parse-tree", "%s"};
  35. } else if (component_ == "codegen" || component_ == "driver") {
  36. CARBON_FATAL() << "ARGS is always set in these tests";
  37. } else {
  38. CARBON_FATAL() << "Unexpected test component " << component_ << ": "
  39. << test_name();
  40. }
  41. }
  42. auto GetLineNumberReplacement(llvm::ArrayRef<llvm::StringRef> filenames)
  43. -> LineNumberReplacement override {
  44. if (component_ == "lex") {
  45. return {.has_file = false,
  46. .pattern = R"(line: (\s*\d+))",
  47. // The `{{{{` becomes `{{`.
  48. .line_formatv = "{{{{ *}}{0}"};
  49. } else {
  50. return FileTestBase::GetLineNumberReplacement(filenames);
  51. }
  52. }
  53. auto DoExtraCheckReplacements(std::string& check_line) -> void override {
  54. if (component_ == "driver") {
  55. // TODO: Disable token output, it's not interesting for these tests.
  56. if (llvm::StringRef(check_line).starts_with("// CHECK:STDOUT: {")) {
  57. check_line = "// CHECK:STDOUT: {{.*}}";
  58. }
  59. } else if (component_ == "lex") {
  60. // Ignore the resulting column of EndOfFile because it's often the end of
  61. // the CHECK comment.
  62. static RE2 end_of_file_re(R"((EndOfFile.*column: )( *\d+))");
  63. RE2::Replace(&check_line, end_of_file_re, R"(\1{{ *\\d+}})");
  64. } else {
  65. return FileTestBase::DoExtraCheckReplacements(check_line);
  66. }
  67. }
  68. private:
  69. // Returns the toolchain subdirectory being tested.
  70. static auto GetComponent(llvm::StringRef test_name) -> llvm::StringRef {
  71. CARBON_CHECK(test_name.consume_front("toolchain/"));
  72. test_name = test_name.take_front(test_name.find("/"));
  73. return test_name;
  74. }
  75. const llvm::StringRef component_;
  76. };
  77. } // namespace
  78. CARBON_FILE_TEST_FACTORY(ToolchainFileTest);
  79. } // namespace Carbon::Testing
  80. #endif // CARBON_TOOLCHAIN_DRIVER_DRIVER_FILE_TEST_BASE_H_