Migrate explorer tests to file_test and remove lit support. (#3050)

This migrates explorer tests to file_test, using the new --autoupdate
functionality. Per discussion, the trace tests that were using "not"
output are mostly migrated to checking full output. The main exception
is tests that were including trace output from the prelude: the prelude
output is pretty long (multiple MB already) and I think it wasn't the
intent to include, only trace output from the small program.

This is the remaining use of lit support, so the supporting libraries
are also removed here.
This commit is contained in:
Jon Ross-Perkins
2023-08-02 21:44:16 +00:00
committed by GitHub
parent b5167b2d69
commit 96517a1ee3
48 changed files with 772 additions and 1251 deletions
+76 -27
View File
@@ -19,12 +19,23 @@ namespace Carbon::Testing {
// Put helper classes in an anonymous namespace.
namespace {
// Converts a matched line number to an int, trimming whitespace.
static auto ParseLineNumber(absl::string_view matched_line_number) -> int {
llvm::StringRef trimmed = matched_line_number;
trimmed = trimmed.trim();
// NOLINTNEXTLINE(google-runtime-int): API requirement.
long long val;
CARBON_CHECK(!llvm::getAsSignedInteger(trimmed, 10, val));
return val;
}
class CheckLine : public FileTestLineBase {
public:
// RE2 is passed by a pointer because it doesn't support std::optional.
explicit CheckLine(int line_number, const RE2* line_number_re,
std::string line)
explicit CheckLine(int line_number, bool line_number_re_has_file,
const RE2* line_number_re, std::string line)
: FileTestLineBase(line_number),
line_number_re_has_file_(line_number_re_has_file),
line_number_re_(line_number_re),
line_(std::move(line)) {}
@@ -41,20 +52,50 @@ class CheckLine : public FileTestLineBase {
// When the location of all lines in a file are known, we can set the line
// offset based on the target line.
auto SetRemappedLine(const std::string& sub_for_formatv,
int target_line_number) -> void {
// Should only be called when we have a regex.
CARBON_CHECK(line_number_re_);
auto RemapLineNumbers(const llvm::DenseMap<int, int>& output_line_remap,
const std::string& line_formatv) -> void {
// Only need to do remappings when there's a regex.
if (!line_number_re_) {
return;
}
int offset = target_line_number - output_line_number_;
const char* offset_prefix = offset < 0 ? "" : "+";
std::string replacement =
llvm::formatv(sub_for_formatv.data(),
llvm::formatv("[[@LINE{0}{1}]]", offset_prefix, offset));
RE2::Replace(&line_, *line_number_re_, replacement);
bool found_one = false;
while (true) {
// Look for a line number to replace. There may be multiple, so we
// repeatedly check.
absl::string_view matched_line_number;
if (line_number_re_has_file_) {
RE2::PartialMatch(line_, *line_number_re_, nullptr,
&matched_line_number);
} else {
RE2::PartialMatch(line_, *line_number_re_, &matched_line_number);
}
if (matched_line_number.empty()) {
CARBON_CHECK(found_one) << line_;
return;
}
found_one = true;
// Calculate the offset from the CHECK line to the new line number
// (possibly with new CHECK lines added, or some removed).
// NOLINTNEXTLINE(google-runtime-int): API requirement.
auto remapped =
output_line_remap.find(ParseLineNumber(matched_line_number));
CARBON_CHECK(remapped != output_line_remap.end());
int offset = remapped->second - output_line_number_;
// Update the line offset in the CHECK line.
const char* offset_prefix = offset < 0 ? "" : "+";
std::string replacement = llvm::formatv(
line_formatv.c_str(),
llvm::formatv("[[@LINE{0}{1}]]", offset_prefix, offset));
line_.replace(matched_line_number.data() - line_.data(),
matched_line_number.size(), replacement);
}
}
private:
bool line_number_re_has_file_;
const RE2* line_number_re_;
std::string line_;
llvm::StringRef indent_;
@@ -90,10 +131,19 @@ static auto AddCheckLines(
lines.pop_back();
}
// `{{` and `[[` are escaped as a regex matcher.
RE2 double_brace_re(R"(\{\{)");
RE2 double_square_bracket_re(R"(\[\[)");
// End-of-line whitespace is replaced with a regex matcher to make it visible.
RE2 end_of_line_whitespace_re(R"((\s+)$)");
int append_to = 0;
for (const auto& line : lines) {
std::string check_line = llvm::formatv("// CHECK:{0}:{1}{2}", label,
line.empty() ? "" : " ", 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}})");
// Ignore TEST_TMPDIR in output.
if (auto pos = check_line.find(tmpdir); pos != std::string::npos) {
@@ -102,10 +152,10 @@ static auto AddCheckLines(
do_extra_check_replacements(check_line);
// Look for line information in the output. line_number is only set if the
// match is correct.
int line_number = -1;
int match_line_number;
// Look for line information in the output. use_line_number is only set if
// the match is correct.
std::optional<llvm::StringRef> use_line_number;
absl::string_view match_line_number;
if (line_number_re_has_file) {
absl::string_view match_filename;
if (RE2::PartialMatch(check_line, line_number_re, &match_filename,
@@ -119,22 +169,25 @@ static auto AddCheckLines(
if (pos != filenames.end()) {
remaining_filenames = pos + 1;
append_to = pos - filenames.begin();
line_number = match_line_number;
use_line_number = match_line_number;
}
} else {
// The line applies to the current file.
line_number = match_line_number;
use_line_number = match_line_number;
}
}
} else {
// There's no file association, so we only look at the line.
if (RE2::PartialMatch(check_line, line_number_re, &match_line_number)) {
line_number = match_line_number;
use_line_number = match_line_number;
}
}
// NOLINTNEXTLINE(google-runtime-int): API requirement.
long long line_number =
use_line_number ? ParseLineNumber(*use_line_number) : -1;
check_lines[append_to].push_back(
CheckLine(line_number, line_number == -1 ? nullptr : &line_number_re,
check_line));
CheckLine(line_number, line_number_re_has_file,
use_line_number ? &line_number_re : nullptr, check_line));
}
}
@@ -212,12 +265,8 @@ auto AutoupdateFileTest(
// Update all remapped lines in CHECK output.
for (auto& offset_check_line : check_file) {
if (offset_check_line.line_number() >= 1) {
auto new_line = output_line_remap.find(offset_check_line.line_number());
CARBON_CHECK(new_line != output_line_remap.end());
offset_check_line.SetRemappedLine(
line_number_replacement.sub_for_formatv, new_line->second);
}
offset_check_line.RemapLineNumbers(output_line_remap,
line_number_replacement.line_formatv);
}
}