From aec951d7c3e645889a5ca4ae5edf2de9f45da39e Mon Sep 17 00:00:00 2001 From: Jon Ross-Perkins Date: Fri, 24 Jan 2025 08:41:57 -0800 Subject: [PATCH] Fix autoupdate handling of carriage return (#4840) Switching from RE2 to StrReplaceAll because it seems a fair fit for what actually needs to be done here. Also pick up \t for visibility reasons. This came up because clangd's LSP-related APIs print carriage returns. --- testing/file_test/BUILD | 1 + testing/file_test/autoupdate.cpp | 20 ++++++++++-------- testing/file_test/file_test_base_test.cpp | 14 ++++++++++++ testing/file_test/testdata/escaping.carbon | 19 +++++++++++++++++ toolchain/lex/testdata/string_literals.carbon | Bin 4800 -> 4815 bytes 5 files changed, 45 insertions(+), 9 deletions(-) create mode 100644 testing/file_test/testdata/escaping.carbon diff --git a/testing/file_test/BUILD b/testing/file_test/BUILD index 8020e0ca1df7..a82112942304 100644 --- a/testing/file_test/BUILD +++ b/testing/file_test/BUILD @@ -19,6 +19,7 @@ cc_library( "//common:check", "//common:ostream", "//common:raw_string_ostream", + "@abseil-cpp//absl/strings", "@abseil-cpp//absl/strings:string_view", "@llvm-project//llvm:Support", "@re2", diff --git a/testing/file_test/autoupdate.cpp b/testing/file_test/autoupdate.cpp index b8588198aa69..142775c52870 100644 --- a/testing/file_test/autoupdate.cpp +++ b/testing/file_test/autoupdate.cpp @@ -6,6 +6,7 @@ #include +#include "absl/strings/str_replace.h" #include "absl/strings/string_view.h" #include "common/check.h" #include "common/ostream.h" @@ -158,12 +159,6 @@ auto FileTestAutoupdater::BuildCheckLines(llvm::StringRef output, lines.pop_back(); } - // `{{` and `[[` are escaped as a regex matcher. - static RE2 double_brace_re(R"(\{\{)"); - static RE2 double_square_bracket_re(R"(\[\[)"); - // End-of-line whitespace is replaced with a regex matcher to make it visible. - static RE2 end_of_line_whitespace_re(R"((\s+)$)"); - // The default file number for when no specific file is found. int default_file_number = 0; @@ -182,9 +177,16 @@ auto FileTestAutoupdater::BuildCheckLines(llvm::StringRef output, check_line.append(line); } - RE2::Replace(&check_line, double_brace_re, R"({{\\{\\{}})"); - RE2::Replace(&check_line, double_square_bracket_re, R"({{\\[\\[}})"); - RE2::Replace(&check_line, end_of_line_whitespace_re, R"({{\1}})"); + // \r and \t are invisible characters worth marking. + // {{ and [[ are autoupdate syntax which we need to escape. + check_line = absl::StrReplaceAll(check_line, {{"\r", R"({{\r}})"}, + {"\t", R"({{\t}})"}, + {"{{", R"({{\{\{}})"}, + {"[[", R"({{\[\[}})"}}); + // Add an empty regex to call out end-of-line whitespace. + if (check_line.ends_with(' ')) { + check_line.append("{{}}"); + } // Ignore TEST_TMPDIR in output. if (auto pos = check_line.find(tmpdir); pos != std::string::npos) { diff --git a/testing/file_test/file_test_base_test.cpp b/testing/file_test/file_test_base_test.cpp index aeedb6880f65..4a2c49fe2111 100644 --- a/testing/file_test/file_test_base_test.cpp +++ b/testing/file_test/file_test_base_test.cpp @@ -185,6 +185,19 @@ static auto TestNoLineNumber(TestParams& params) return {{.success = true}}; } +// Prints and returns expected results for escaping.carbon. +static auto TestEscaping(TestParams& params) + -> ErrorOr { + params.error_stream << "carriage return\r\n" + "{one brace}\n" + "{{two braces}}\n" + "[one bracket]\n" + "[[two brackets]]\n" + "end of line whitespace \n" + "\ttabs\t\n"; + return {{.success = true}}; +} + // Prints and returns expected results for stdin.carbon. static auto TestStdin(TestParams& params) -> ErrorOr { @@ -268,6 +281,7 @@ auto FileTestBaseTest::Run( filename.string()) .Case("alternating_files.carbon", &TestAlternatingFiles) .Case("capture_console_output.carbon", &TestCaptureConsoleOutput) + .Case("escaping.carbon", &TestEscaping) .Case("example.carbon", &TestExample) .Case("fail_example.carbon", &TestFailExample) .Case("file_only_re_one_file.carbon", &TestFileOnlyREOneFile) diff --git a/testing/file_test/testdata/escaping.carbon b/testing/file_test/testdata/escaping.carbon new file mode 100644 index 000000000000..d0939730c666 --- /dev/null +++ b/testing/file_test/testdata/escaping.carbon @@ -0,0 +1,19 @@ +// Part of the Carbon Language project, under the Apache License v2.0 with LLVM +// Exceptions. See /LICENSE for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception + +// SET-CAPTURE-CONSOLE-OUTPUT +// AUTOUPDATE +// TIP: To test this file alone, run: +// TIP: bazel test //testing/file_test:file_test_base_test --test_arg=--file_tests=testing/file_test/testdata/escaping.carbon +// TIP: To dump output, run: +// TIP: bazel run //testing/file_test:file_test_base_test -- --dump_output --file_tests=testing/file_test/testdata/escaping.carbon +// CHECK:STDERR: carriage return{{\r}} +// CHECK:STDERR: {one brace} +// CHECK:STDERR: {{\{\{}}two braces}} +// CHECK:STDERR: [one bracket] +// CHECK:STDERR: {{\[\[}}two brackets]] +// CHECK:STDERR: end of line whitespace {{}} +// CHECK:STDERR: {{\t}}tabs{{\t}} + +// CHECK:STDOUT: 2 args: `default_args`, `escaping.carbon` diff --git a/toolchain/lex/testdata/string_literals.carbon b/toolchain/lex/testdata/string_literals.carbon index f5956f4f1af147b71d6e45cb406cb0facadee50d..b20c4efc59a4569b32a606e2755c2dc7ebf62fd2 100644 GIT binary patch delta 59 zcmX@0dR}$IK3>-9>X?$+$rlAhCtv36k^u8+mDF_<$`W%*Q>_#dAR?0)xwIy)72w!B Imrt1m0KzO4%>V!Z delta 45 zcmX@FdO&rnM~Z=9H#dDI{=CX5`YEyhngz^BO*7764Ma B4Y2?K