Commit 56daccb8 authored by vogelheim's avatar vogelheim Committed by Commit bot

Fix memory leak in cctest/parsing/test-scanner.

BUG=chromium:662388

Review-Url: https://codereview.chromium.org/2495533003
Cr-Commit-Position: refs/heads/master@{#41266}
parent 061c2ab2
......@@ -17,10 +17,29 @@ namespace {
const char src_simple[] = "function foo() { var x = 2 * a() + b; }";
std::unique_ptr<Scanner> make_scanner(const char* src, UnicodeCache* cache) {
std::unique_ptr<Scanner> scanner(new Scanner(cache));
scanner->Initialize(ScannerStream::ForTesting(src).release());
return scanner;
struct ScannerTestHelper {
ScannerTestHelper() = default;
ScannerTestHelper(ScannerTestHelper&& other)
: unicode_cache(std::move(other.unicode_cache)),
stream(std::move(other.stream)),
scanner(std::move(other.scanner)) {}
std::unique_ptr<UnicodeCache> unicode_cache;
std::unique_ptr<Utf16CharacterStream> stream;
std::unique_ptr<Scanner> scanner;
Scanner* operator->() const { return scanner.get(); }
Scanner* get() const { return scanner.get(); }
};
ScannerTestHelper make_scanner(const char* src) {
ScannerTestHelper helper;
helper.unicode_cache = std::unique_ptr<UnicodeCache>(new UnicodeCache);
helper.stream = ScannerStream::ForTesting(src);
helper.scanner =
std::unique_ptr<Scanner>(new Scanner(helper.unicode_cache.get()));
helper.scanner->Initialize(helper.stream.get());
return helper;
}
} // anonymous namespace
......@@ -30,13 +49,11 @@ std::unique_ptr<Scanner> make_scanner(const char* src, UnicodeCache* cache) {
#define DCHECK_TOK(a, b) DCHECK_EQ(Token::Name(a), Token::Name(b))
TEST(Bookmarks) {
UnicodeCache unicode_cache;
// Scan through the given source and record the tokens for use as reference
// below.
std::vector<Token::Value> tokens;
{
auto scanner = make_scanner(src_simple, &unicode_cache);
auto scanner = make_scanner(src_simple);
do {
tokens.push_back(scanner->Next());
} while (scanner->current_token() != Token::EOS);
......@@ -50,7 +67,7 @@ TEST(Bookmarks) {
// - scan until the end.
// At each step, compare to the reference token sequence generated above.
for (size_t bookmark_pos = 0; bookmark_pos < tokens.size(); bookmark_pos++) {
auto scanner = make_scanner(src_simple, &unicode_cache);
auto scanner = make_scanner(src_simple);
Scanner::BookmarkScope bookmark(scanner.get());
for (size_t i = 0; i < std::min(bookmark_pos + 10, tokens.size()); i++) {
......@@ -79,9 +96,8 @@ TEST(AllThePushbacks) {
{"<!-- xx -->\nx", {Token::IDENTIFIER, Token::EOS}},
};
UnicodeCache unicode_cache;
for (const auto& test_case : test_cases) {
auto scanner = make_scanner(test_case.src, &unicode_cache);
auto scanner = make_scanner(test_case.src);
for (size_t i = 0; test_case.tokens[i] != Token::EOS; i++) {
DCHECK_TOK(test_case.tokens[i], scanner->Next());
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment