Support multi-file lex printing and testing. (#3214)

Lex now prints its yaml as:
```
- filename: name
  tokens: [ ... ]
```

New support in file_test allows the `filename` marker at the top to
define the default file number for later lines, meaning multi-file
output from lexing is now associated with the appropriate file. Similar
support will probably also apply to lowering, semir, and other places
that print a filename once for the full dump.

This hammers a bit at how line number replacements work in file_test,
allowing stacking them so that lex errors and stdout can both be
line-associated properly. I've tried to make the autoupdate more
frequently work in one pass, now also taking into account the file index
when doing line replacements.

There are still some issues with EndOfFile that it may be good to
discuss: because CHECK lines are appended to the end of the file now,
and the EndOfFile token points at the last line including comments, new
lex tests now take two runs to autoupdate (because without CHECK lines,
the EndOfFile points at a content line, which content is then inserted
after). Note that removing CHECK lines from the test is not a solution:
autoupdate also started inserting blank lines, which breaks this for a
similar reason. One solution here might be to not have EndOfFile
associate with a line or column, which has been a bit of an issue
regardless.

Also fixes a small issue with toolchain's autoupdate script.
This commit is contained in:
Jon Ross-Perkins
2023-09-13 16:14:09 +00:00
committed by GitHub
parent 2800fc86c9
commit 2ecab78297
25 changed files with 579 additions and 373 deletions
+40 -2
View File
@@ -9,12 +9,11 @@
#include "common/ostream.h"
#include "llvm/ADT/StringExtras.h"
#include "llvm/Support/FormatVariadic.h"
namespace Carbon::Testing {
namespace {
using ::testing::Eq;
class FileTestBaseTest : public FileTestBase {
public:
using FileTestBase::FileTestBase;
@@ -96,6 +95,27 @@ class FileTestBaseTest : public FileTestBase {
stderr << "unattached message 3\n"
<< "unattached message 4\n";
return true;
} else if (filename == "file_only_re_one_file.carbon") {
stdout << "unattached message 1\n"
<< "file: file_only_re_one_file.carbon\n"
<< "line: 1\n"
<< "unattached message 2\n";
return true;
} else if (filename == "file_only_re_multi_file.carbon") {
int msg_count = 0;
stdout << "unattached message " << ++msg_count << "\n"
<< "file: a.carbon\n"
<< "unattached message " << ++msg_count << "\n"
<< "line: 1: attached message " << ++msg_count << "\n"
<< "unattached message " << ++msg_count << "\n"
<< "line: 8: late message " << ++msg_count << "\n"
<< "unattached message " << ++msg_count << "\n"
<< "file: b.carbon\n"
<< "line: 1: attached message " << ++msg_count << "\n"
<< "unattached message " << ++msg_count << "\n"
<< "line: 7: late message " << ++msg_count << "\n"
<< "unattached message " << ++msg_count << "\n";
return true;
} else {
return ErrorBuilder() << "Unexpected file: " << filename;
}
@@ -104,6 +124,24 @@ class FileTestBaseTest : public FileTestBase {
auto GetDefaultArgs() -> llvm::SmallVector<std::string> override {
return {"default_args", "%s"};
}
auto GetDefaultFileRE(llvm::ArrayRef<llvm::StringRef> filenames)
-> std::optional<RE2> override {
return std::make_optional<RE2>(
llvm::formatv(R"(file: ({0}))", llvm::join(filenames, "|")));
}
auto GetLineNumberReplacements(llvm::ArrayRef<llvm::StringRef> filenames)
-> llvm::SmallVector<LineNumberReplacement> override {
auto replacements = FileTestBase::GetLineNumberReplacements(filenames);
auto filename = std::filesystem::path(test_name().str()).filename();
if (llvm::StringRef(filename).startswith("file_only_re_")) {
replacements.push_back({.has_file = false,
.re = std::make_shared<RE2>(R"(line: (\d+))"),
.line_formatv = "{0}"});
}
return replacements;
}
};
} // namespace