Skip to content

Commit aec951d

Browse files
authored
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.
1 parent bc952b1 commit aec951d

File tree

5 files changed

+45
-9
lines changed

5 files changed

+45
-9
lines changed

testing/file_test/BUILD

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ cc_library(
1919
"//common:check",
2020
"//common:ostream",
2121
"//common:raw_string_ostream",
22+
"@abseil-cpp//absl/strings",
2223
"@abseil-cpp//absl/strings:string_view",
2324
"@llvm-project//llvm:Support",
2425
"@re2",

testing/file_test/autoupdate.cpp

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
#include <fstream>
88

9+
#include "absl/strings/str_replace.h"
910
#include "absl/strings/string_view.h"
1011
#include "common/check.h"
1112
#include "common/ostream.h"
@@ -158,12 +159,6 @@ auto FileTestAutoupdater::BuildCheckLines(llvm::StringRef output,
158159
lines.pop_back();
159160
}
160161

161-
// `{{` and `[[` are escaped as a regex matcher.
162-
static RE2 double_brace_re(R"(\{\{)");
163-
static RE2 double_square_bracket_re(R"(\[\[)");
164-
// End-of-line whitespace is replaced with a regex matcher to make it visible.
165-
static RE2 end_of_line_whitespace_re(R"((\s+)$)");
166-
167162
// The default file number for when no specific file is found.
168163
int default_file_number = 0;
169164

@@ -182,9 +177,16 @@ auto FileTestAutoupdater::BuildCheckLines(llvm::StringRef output,
182177
check_line.append(line);
183178
}
184179

185-
RE2::Replace(&check_line, double_brace_re, R"({{\\{\\{}})");
186-
RE2::Replace(&check_line, double_square_bracket_re, R"({{\\[\\[}})");
187-
RE2::Replace(&check_line, end_of_line_whitespace_re, R"({{\1}})");
180+
// \r and \t are invisible characters worth marking.
181+
// {{ and [[ are autoupdate syntax which we need to escape.
182+
check_line = absl::StrReplaceAll(check_line, {{"\r", R"({{\r}})"},
183+
{"\t", R"({{\t}})"},
184+
{"{{", R"({{\{\{}})"},
185+
{"[[", R"({{\[\[}})"}});
186+
// Add an empty regex to call out end-of-line whitespace.
187+
if (check_line.ends_with(' ')) {
188+
check_line.append("{{}}");
189+
}
188190

189191
// Ignore TEST_TMPDIR in output.
190192
if (auto pos = check_line.find(tmpdir); pos != std::string::npos) {

testing/file_test/file_test_base_test.cpp

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,19 @@ static auto TestNoLineNumber(TestParams& params)
185185
return {{.success = true}};
186186
}
187187

188+
// Prints and returns expected results for escaping.carbon.
189+
static auto TestEscaping(TestParams& params)
190+
-> ErrorOr<FileTestBaseTest::RunResult> {
191+
params.error_stream << "carriage return\r\n"
192+
"{one brace}\n"
193+
"{{two braces}}\n"
194+
"[one bracket]\n"
195+
"[[two brackets]]\n"
196+
"end of line whitespace \n"
197+
"\ttabs\t\n";
198+
return {{.success = true}};
199+
}
200+
188201
// Prints and returns expected results for stdin.carbon.
189202
static auto TestStdin(TestParams& params)
190203
-> ErrorOr<FileTestBaseTest::RunResult> {
@@ -268,6 +281,7 @@ auto FileTestBaseTest::Run(
268281
filename.string())
269282
.Case("alternating_files.carbon", &TestAlternatingFiles)
270283
.Case("capture_console_output.carbon", &TestCaptureConsoleOutput)
284+
.Case("escaping.carbon", &TestEscaping)
271285
.Case("example.carbon", &TestExample)
272286
.Case("fail_example.carbon", &TestFailExample)
273287
.Case("file_only_re_one_file.carbon", &TestFileOnlyREOneFile)
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
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+
5+
// SET-CAPTURE-CONSOLE-OUTPUT
6+
// AUTOUPDATE
7+
// TIP: To test this file alone, run:
8+
// TIP: bazel test //testing/file_test:file_test_base_test --test_arg=--file_tests=testing/file_test/testdata/escaping.carbon
9+
// TIP: To dump output, run:
10+
// TIP: bazel run //testing/file_test:file_test_base_test -- --dump_output --file_tests=testing/file_test/testdata/escaping.carbon
11+
// CHECK:STDERR: carriage return{{\r}}
12+
// CHECK:STDERR: {one brace}
13+
// CHECK:STDERR: {{\{\{}}two braces}}
14+
// CHECK:STDERR: [one bracket]
15+
// CHECK:STDERR: {{\[\[}}two brackets]]
16+
// CHECK:STDERR: end of line whitespace {{}}
17+
// CHECK:STDERR: {{\t}}tabs{{\t}}
18+
19+
// CHECK:STDOUT: 2 args: `default_args`, `escaping.carbon`
15 Bytes
Binary file not shown.

0 commit comments

Comments
 (0)