lex_file_test.cpp 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  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 <string>
  5. #include "llvm/ADT/SmallVector.h"
  6. #include "re2/re2.h"
  7. #include "toolchain/driver/driver_file_test_base.h"
  8. namespace Carbon::Testing {
  9. namespace {
  10. class LexerFileTest : public DriverFileTestBase {
  11. public:
  12. explicit LexerFileTest(std::filesystem::path path)
  13. : DriverFileTestBase(std::move(path)),
  14. end_of_file_re_((R"((EndOfFile.*column: )( *\d+))")) {}
  15. auto GetDefaultArgs() -> llvm::SmallVector<std::string> override {
  16. return {"compile", "--phase=lex", "--dump-tokens", "%s"};
  17. }
  18. auto GetLineNumberReplacement(llvm::ArrayRef<llvm::StringRef> /*filenames*/)
  19. -> LineNumberReplacement override {
  20. return {.has_file = false,
  21. .pattern = R"(line: (\s*\d+))",
  22. // The `{{{{` becomes `{{`.
  23. .line_formatv = "{{{{ *}}{0}"};
  24. }
  25. auto DoExtraCheckReplacements(std::string& check_line) -> void override {
  26. // Ignore the resulting column of EndOfFile because it's often the end of
  27. // the CHECK comment.
  28. RE2::Replace(&check_line, end_of_file_re_, R"(\1{{ *\\d+}})");
  29. }
  30. private:
  31. RE2 end_of_file_re_;
  32. };
  33. } // namespace
  34. CARBON_FILE_TEST_FACTORY(LexerFileTest);
  35. } // namespace Carbon::Testing