Change StringLiteral to less frequently allocate a new string. (#3314)

Building on #3311, which started moving the result string into a
`unique_ptr`, instead have `StringLiteral` use a `BumpPtrAllocator` to
manage memory. But also, detect when a string is really trivial during
`Lex` and, if so, return `contents_` directly.
This commit is contained in:
Jon Ross-Perkins
2023-10-24 19:15:49 +00:00
committed by GitHub
parent 1d6298290f
commit b2cfd5a8a8
7 changed files with 149 additions and 76 deletions
+4 -3
View File
@@ -25,13 +25,14 @@ class StringLiteralTest : public ::testing::Test {
return *result;
}
auto Parse(llvm::StringRef text) -> std::string {
auto Parse(llvm::StringRef text) -> llvm::StringRef {
StringLiteral token = Lex(text);
Testing::SingleTokenDiagnosticTranslator translator(text);
DiagnosticEmitter<const char*> emitter(translator, error_tracker);
return token.ComputeValue(emitter);
return token.ComputeValue(allocator, emitter);
}
llvm::BumpPtrAllocator allocator;
ErrorTrackingDiagnosticConsumer error_tracker;
};
@@ -311,7 +312,7 @@ TEST_F(StringLiteralTest, StringLiteralBadEscapeSequence) {
for (llvm::StringLiteral test : testcases) {
error_tracker.Reset();
auto value = Parse(test);
Parse(test);
EXPECT_TRUE(error_tracker.seen_error()) << "`" << test << "`";
// TODO: Test value produced by error recovery.
}