From e33ca05b5df8e0e7493ddc832dd83dbd8207379e Mon Sep 17 00:00:00 2001 From: Jeff Walden Date: Sat, 10 Nov 2018 21:56:01 -0800 Subject: [PATCH] Bug 1504947 - Change fake-column computations to work from (LineToken, uint32_t offset) inputs. r=tcampbell --HG-- extra : rebase_source : b2518ff4566613388ad61c7c31ad1cf4036ce326 --- js/src/frontend/TokenStream.cpp | 19 +---------- js/src/frontend/TokenStream.h | 58 +++++++++++++++++++++++++-------- 2 files changed, 46 insertions(+), 31 deletions(-) diff --git a/js/src/frontend/TokenStream.cpp b/js/src/frontend/TokenStream.cpp index 86c382a19e27..6488488b7170 100644 --- a/js/src/frontend/TokenStream.cpp +++ b/js/src/frontend/TokenStream.cpp @@ -469,18 +469,6 @@ TokenStreamAnyChars::SourceCoords::lineToken(uint32_t offset) const { return LineToken(indexFromOffset(offset), offset); } -uint32_t TokenStreamAnyChars::SourceCoords::columnIndex(uint32_t offset) const { - return columnFromIndexAndOffset(indexFromOffset(offset), offset); -} - -void TokenStreamAnyChars::SourceCoords::lineNumAndColumnIndex( - uint32_t offset, uint32_t* lineNum, uint32_t* column) const { - LineToken token = lineToken(offset); - uint32_t index = token.index; - *lineNum = lineNumberFromIndex(index); - *column = columnFromIndexAndOffset(index, offset); -} - TokenStreamAnyChars::TokenStreamAnyChars(JSContext* cx, const ReadOnlyCompileOptions& options, StrictModeGetter* smg) @@ -1340,7 +1328,7 @@ bool TokenStreamAnyChars::fillExcludingContext(ErrorMetadata* err, // Otherwise use this TokenStreamAnyChars's location information. err->filename = filename_; - srcCoords.lineNumAndColumnIndex(offset, &err->lineNumber, &err->columnNumber); + lineAndColumnAt(offset, &err->lineNumber, &err->columnNumber); return true; } @@ -1350,11 +1338,6 @@ bool TokenStreamSpecific::hasTokenizationStarted() const { return anyChars.isCurrentTokenType(TokenKind::Eof) && !anyChars.isEOF(); } -void TokenStreamAnyChars::lineAndColumnAt(size_t offset, uint32_t* line, - uint32_t* column) const { - srcCoords.lineNumAndColumnIndex(offset, line, column); -} - template <> inline void SourceUnits::computeWindowOffsetAndLength( const char16_t* encodedWindow, size_t encodedTokenOffset, diff --git a/js/src/frontend/TokenStream.h b/js/src/frontend/TokenStream.h index fc64263e8448..cddd33a5a50f 100644 --- a/js/src/frontend/TokenStream.h +++ b/js/src/frontend/TokenStream.h @@ -843,11 +843,21 @@ class TokenStreamAnyChars : public TokenStreamShared { */ class LineToken { uint32_t index; +#ifdef DEBUG + uint32_t offset_; // stored for consistency-of-use assertions +#endif friend class SourceCoords; public: - explicit LineToken(uint32_t index, uint32_t offset) : index(index) {} + explicit LineToken(uint32_t index, uint32_t offset) + : index(index) +#ifdef DEBUG + , + offset_(offset) +#endif + { + } bool isFirstLine() const { return index == 0; } @@ -871,9 +881,24 @@ class TokenStreamAnyChars : public TokenStreamShared { return lineNumberFromIndex(lineToken.index); } - uint32_t columnIndex(uint32_t offset) const; - void lineNumAndColumnIndex(uint32_t offset, uint32_t* lineNum, - uint32_t* column) const; + /** + * Compute the offset *in code units* of |offset| from the start of the + * line containing it, plus any contribution from |initialColumnNumber| + * passed to the |SourceCoords| constructor. + * + * This is only really a "column". A subsequent patch in this stack + * removes it, computing a multi-unit-aware column number elsewhere in + * Unit-sensitive manner. + */ + uint32_t columnIndex(LineToken lineToken, uint32_t offset) const { + MOZ_ASSERT(lineToken.offset_ == offset, "use a consistent token"); + + uint32_t lineStartOffset = lineStartOffsets_[lineToken.index]; + MOZ_RELEASE_ASSERT(offset >= lineStartOffset); + + uint32_t relative = offset - lineStartOffset; + return (lineToken.isFirstLine() ? initialColumn_ : 0) + relative; + } }; SourceCoords srcCoords; @@ -890,6 +915,18 @@ class TokenStreamAnyChars : public TokenStreamShared { return srcCoords.lineNumber(lineToken); } + uint32_t columnIndex(LineToken lineToken, uint32_t offset) const { + return srcCoords.columnIndex(lineToken, offset); + } + + // A helper function if you want an offset's line *and* "column" info. + void lineAndColumnAt(uint32_t offset, uint32_t* lineNum, + uint32_t* column) const { + LineToken token = srcCoords.lineToken(offset); + *lineNum = srcCoords.lineNumber(token); + *column = srcCoords.columnIndex(token, offset); + } + /** * Fill in |err|, excepting line-of-context-related fields. If the token * stream has location information, use that and return true. If it does @@ -944,8 +981,6 @@ class TokenStreamAnyChars : public TokenStreamShared { // ErrorReporter API Helpers - void lineAndColumnAt(size_t offset, uint32_t* line, uint32_t* column) const; - // This is just straight up duplicated from TokenStreamSpecific's inheritance // of ErrorReporter's reportErrorNoOffset. varargs delenda est. void reportErrorNoOffset(unsigned errorNumber, ...); @@ -1929,12 +1964,7 @@ class GeneralTokenStreamChars : public SpecializedTokenStreamCharsBase { void computeLineAndColumn(uint32_t offset, uint32_t* line, uint32_t* column) const { - const TokenStreamAnyChars& anyChars = anyCharsAccess(); - - auto lineToken = anyChars.lineToken(offset); - *line = anyChars.lineNumber(lineToken); - - anyCharsAccess().srcCoords.lineNumAndColumnIndex(offset, line, column); + anyCharsAccess().lineAndColumnAt(offset, line, column); } void newSimpleToken(TokenKind kind, TokenStart start, @@ -2469,7 +2499,9 @@ class MOZ_STACK_CLASS TokenStreamSpecific } uint32_t columnAt(size_t offset) const final { - return anyCharsAccess().srcCoords.columnIndex(offset); + const TokenStreamAnyChars& anyChars = anyCharsAccess(); + auto lineToken = anyChars.lineToken(offset); + return anyChars.columnIndex(lineToken, offset); } bool hasTokenizationStarted() const final;