Skip to content

Commit b0f6ad0

Browse files
eliau2005v8-scoped@luci-project-accounts.iam.gserviceaccount.com
authored andcommitted
[parser] Factor magic comment value scanning into a helper
Follow-up to https://crrev.com/c/8472768, addressing the review comment that the is_compile_hints handling in TryToParseMagicComment was "a bit nasty". Move the value scanning into ScanMagicCommentValue, which takes a per-character callback and returns the value's Location, or an invalid Location if anything but whitespace follows the value on the line. The debug magic comments pass a no-op callback and store the Location; the compile hints path buffers the characters into a local LiteralBuffer. This removes the scattered is_compile_hints checks from the scanning loops. No behaviour change. [email protected], [email protected] Bug: 566467883 Change-Id: I276b3f6646abef42ffd9d75b0261a519887b026c Reviewed-on: https://chromium-review.googlesource.com/c/v8/v8/+/8569278 Reviewed-by: Toon Verwaest <[email protected]> Commit-Queue: Toon Verwaest <[email protected]> Auto-Submit: eliau elkouby (‫אליהו אלקובי‬‎) <[email protected]> Reviewed-by: Leszek Swirski <[email protected]> Cr-Commit-Position: refs/heads/main@{#110652}
1 parent df14fa3 commit b0f6ad0

2 files changed

Lines changed: 43 additions & 32 deletions

File tree

‎src/parsing/scanner.cc‎

Lines changed: 37 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -277,6 +277,27 @@ void ProcessPerFunctionCompileHints(const base::Vector<const uint8_t>& data,
277277

278278
} // namespace
279279

280+
template <typename CharCallback>
281+
Scanner::Location Scanner::ScanMagicCommentValue(CharCallback on_char) {
282+
DCHECK_EQ(c0_, '=');
283+
Advance();
284+
while (IsWhiteSpace(c0_)) {
285+
Advance();
286+
}
287+
int value_beg_pos = source_pos();
288+
while (c0_ != kEndOfInput && !IsWhiteSpaceOrLineTerminator(c0_)) {
289+
on_char(c0_);
290+
Advance();
291+
}
292+
Location value_location(value_beg_pos, source_pos());
293+
// Allow whitespace at the end.
294+
while (c0_ != kEndOfInput && !unibrow::IsLineTerminator(c0_)) {
295+
if (!IsWhiteSpace(c0_)) return Location::invalid();
296+
Advance();
297+
}
298+
return value_location;
299+
}
300+
280301
void Scanner::TryToParseMagicComment(base::uc32 hash_or_at_sign) {
281302
// Magic comments are of the form: //[#@]\s<name>=\s*<value>\s*.* and this
282303
// function will just return if it cannot parse a magic comment.
@@ -297,7 +318,6 @@ void Scanner::TryToParseMagicComment(base::uc32 hash_or_at_sign) {
297318
// they can be created as substrings of the source instead of being copied.
298319
Location* value_location = nullptr;
299320
bool is_compile_hints = false;
300-
LiteralBuffer per_function_compile_hints_value;
301321
if (name_literal == base::StaticOneByteVector("sourceURL")) {
302322
value_location = &debug_magic_comment_locations_.source_url;
303323
} else if (name_literal == base::StaticOneByteVector("sourceMappingURL")) {
@@ -322,38 +342,23 @@ void Scanner::TryToParseMagicComment(base::uc32 hash_or_at_sign) {
322342
if (c0_ != '=') {
323343
return;
324344
}
325-
if (is_compile_hints) per_function_compile_hints_value.Start();
326-
Advance();
327-
while (IsWhiteSpace(c0_)) {
328-
Advance();
329-
}
330-
int value_beg_pos = source_pos();
331-
while (c0_ != kEndOfInput && !unibrow::IsLineTerminator(c0_)) {
332-
if (IsWhiteSpace(c0_)) {
333-
break;
334-
}
335-
if (is_compile_hints) per_function_compile_hints_value.AddChar(c0_);
336-
Advance();
337-
}
338-
Location value_location_in_source(value_beg_pos, source_pos());
339-
// Allow whitespace at the end.
340-
while (c0_ != kEndOfInput && !unibrow::IsLineTerminator(c0_)) {
341-
if (!IsWhiteSpace(c0_)) {
342-
if (is_compile_hints) per_function_compile_hints_value.Start();
343-
value_location_in_source = Location::invalid();
344-
break;
345-
}
346-
Advance();
347-
}
348-
if (value_location != nullptr) *value_location = value_location_in_source;
349-
if (is_compile_hints && per_function_compile_hints_value.is_one_byte()) {
350-
base::Vector<const uint8_t> value_literal =
351-
per_function_compile_hints_value.one_byte_literal();
352-
per_function_compile_hint_positions_.clear();
353-
per_function_compile_hint_positions_idx_ = 0;
354-
ProcessPerFunctionCompileHints(value_literal, source_pos(),
355-
per_function_compile_hint_positions_);
345+
if (!is_compile_hints) {
346+
DCHECK_NOT_NULL(value_location);
347+
*value_location = ScanMagicCommentValue([](base::uc32) {});
348+
return;
356349
}
350+
LiteralBuffer value;
351+
value.Start();
352+
Location value_location_in_source =
353+
ScanMagicCommentValue([&value](base::uc32 c) { value.AddChar(c); });
354+
// A value followed by trailing garbage is treated as empty, which clears
355+
// the previously recorded compile hints.
356+
if (!value_location_in_source.IsValid()) value.Start();
357+
if (!value.is_one_byte()) return;
358+
per_function_compile_hint_positions_.clear();
359+
per_function_compile_hint_positions_idx_ = 0;
360+
ProcessPerFunctionCompileHints(value.one_byte_literal(), source_pos(),
361+
per_function_compile_hint_positions_);
357362
}
358363

359364
bool Scanner::HasPerFunctionCompileHint(int position) {

‎src/parsing/scanner.h‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -728,6 +728,12 @@ class V8_EXPORT_PRIVATE Scanner {
728728
Token::Value SkipSingleLineComment();
729729
Token::Value SkipMagicComment(base::uc32 hash_or_at_sign);
730730
void TryToParseMagicComment(base::uc32 hash_or_at_sign);
731+
// Scans the value of a magic comment, starting at the '=', calling on_char
732+
// for each value character. Returns the value's location, or
733+
// Location::invalid() if anything but whitespace follows the value on the
734+
// line.
735+
template <typename CharCallback>
736+
Location ScanMagicCommentValue(CharCallback on_char);
731737
Token::Value SkipMultiLineComment();
732738
// Scans a possible HTML comment -- begins with '<!'.
733739
Token::Value ScanHtmlComment();

0 commit comments

Comments
 (0)