Commit f6874c73 authored by Jakob Gruber's avatar Jakob Gruber Committed by Commit Bot

[globals] Change uc32 to be unsigned

Prior to this change, uc16 was typedef'd to (unsigned) uint16_t while
uc32 was typedef'd to (signed) int32_t.

For consistency, and to avoid unexpected behavior around
signed/unsigned comparisons, this changes uc32 to the unsigned
uint32_t type.

As part of this change, old-style error passing (return -1, check for
negative return values) was updated to use named error values.

Bug: v8:10568
Change-Id: I8524e66ee20e8738749cd34c4fe82c14e885dcb3
Reviewed-on: https://chromium-review.googlesource.com/c/v8/v8/+/2235533Reviewed-by: 's avatarLeszek Swirski <leszeks@chromium.org>
Reviewed-by: 's avatarClemens Backes <clemensb@chromium.org>
Reviewed-by: 's avatarJakob Kummerow <jkummerow@chromium.org>
Commit-Queue: Jakob Gruber <jgruber@chromium.org>
Cr-Commit-Position: refs/heads/master@{#68282}
parent d8c9ae52
......@@ -99,7 +99,7 @@ void AsmJsScanner::Next() {
preceded_by_newline_ = true;
break;
case kEndOfInput:
case kEndOfInputU:
token_ = kEndOfInput;
return;
......@@ -354,7 +354,7 @@ bool AsmJsScanner::ConsumeCComment() {
if (ch == '\n') {
preceded_by_newline_ = true;
}
if (ch == kEndOfInput) {
if (ch == kEndOfInputU) {
return false;
}
}
......@@ -367,7 +367,7 @@ void AsmJsScanner::ConsumeCPPComment() {
preceded_by_newline_ = true;
return;
}
if (ch == kEndOfInput) {
if (ch == kEndOfInputU) {
return;
}
}
......@@ -377,7 +377,7 @@ void AsmJsScanner::ConsumeString(uc32 quote) {
// Only string allowed is 'use asm' / "use asm".
const char* expected = "use asm";
for (; *expected != '\0'; ++expected) {
if (stream_->Advance() != *expected) {
if (stream_->Advance() != static_cast<uc32>(*expected)) {
token_ = kParseError;
return;
}
......
......@@ -135,6 +135,8 @@ class V8_EXPORT_PRIVATE AsmJsScanner {
};
// clang-format on
static constexpr uc32 kEndOfInputU = static_cast<uc32>(kEndOfInput);
private:
Utf16CharacterStream* stream_;
token_t token_;
......
......@@ -40,14 +40,16 @@ bool IsValidCodePoint(Isolate* isolate, Handle<Object> value) {
return true;
}
static constexpr uc32 kInvalidCodePoint = static_cast<uc32>(-1);
uc32 NextCodePoint(Isolate* isolate, BuiltinArguments args, int index) {
Handle<Object> value = args.at(1 + index);
ASSIGN_RETURN_ON_EXCEPTION_VALUE(isolate, value,
Object::ToNumber(isolate, value), -1);
ASSIGN_RETURN_ON_EXCEPTION_VALUE(
isolate, value, Object::ToNumber(isolate, value), kInvalidCodePoint);
if (!IsValidCodePoint(isolate, value)) {
isolate->Throw(*isolate->factory()->NewRangeError(
MessageTemplate::kInvalidCodePoint, value));
return -1;
return kInvalidCodePoint;
}
return DoubleToUint32(value->Number());
}
......@@ -69,7 +71,7 @@ BUILTIN(StringFromCodePoint) {
int index;
for (index = 0; index < length; index++) {
code = NextCodePoint(isolate, args, index);
if (code < 0) {
if (code == kInvalidCodePoint) {
return ReadOnlyRoots(isolate).exception();
}
if (code > String::kMaxOneByteCharCode) {
......@@ -99,7 +101,7 @@ BUILTIN(StringFromCodePoint) {
break;
}
code = NextCodePoint(isolate, args, index);
if (code < 0) {
if (code == kInvalidCodePoint) {
return ReadOnlyRoots(isolate).exception();
}
}
......
......@@ -329,7 +329,7 @@ constexpr uint64_t kQuietNaNMask = static_cast<uint64_t>(0xfff) << 51;
// Code-point values in Unicode 4.0 are 21 bits wide.
// Code units in UTF-16 are 16 bits wide.
using uc16 = uint16_t;
using uc32 = int32_t;
using uc32 = uint32_t;
constexpr int kOneByteSize = kCharSize;
constexpr int kUC16Size = sizeof(uc16); // NOLINT
......
......@@ -335,7 +335,7 @@ uc32 JsonParser<Char>::ScanUnicodeCharacter() {
uc32 value = 0;
for (int i = 0; i < 4; i++) {
int digit = HexValue(NextCharacter());
if (V8_UNLIKELY(digit < 0)) return -1;
if (V8_UNLIKELY(digit < 0)) return kInvalidUnicodeCharacter;
value = value * 16 + digit;
}
return value;
......@@ -1173,7 +1173,7 @@ JsonString JsonParser<Char>::ScanJsonString(bool needs_internalization) {
case EscapeKind::kUnicode: {
uc32 value = ScanUnicodeCharacter();
if (value == -1) {
if (value == kInvalidUnicodeCharacter) {
AllowHeapAllocation allow_before_exception;
ReportUnexpectedCharacter(CurrentCharacter());
return JsonString();
......
......@@ -151,7 +151,8 @@ class JsonParser final {
return result;
}
static const int kEndOfString = -1;
static constexpr uc32 kEndOfString = static_cast<uc32>(-1);
static constexpr uc32 kInvalidUnicodeCharacter = static_cast<uc32>(-1);
private:
struct JsonContinuation {
......
......@@ -305,7 +305,7 @@ V8_INLINE Token::Value Scanner::ScanIdentifierOrKeywordInner() {
// Special case for escapes at the start of an identifier.
escaped = true;
uc32 c = ScanIdentifierUnicodeEscape();
DCHECK(!IsIdentifierStart(-1));
DCHECK(!IsIdentifierStart(Invalid()));
if (c == '\\' || !IsIdentifierStart(c)) {
return Token::ILLEGAL;
}
......
......@@ -107,6 +107,12 @@ void Scanner::Initialize() {
Scan();
}
// static
bool Scanner::IsInvalid(uc32 c) {
DCHECK(c == Invalid() || base::IsInRange(c, 0u, String::kMaxCodePoint));
return c == Scanner::Invalid();
}
template <bool capture_raw, bool unicode>
uc32 Scanner::ScanHexNumber(int expected_length) {
DCHECK_LE(expected_length, 4); // prevent overflow
......@@ -120,7 +126,7 @@ uc32 Scanner::ScanHexNumber(int expected_length) {
unicode
? MessageTemplate::kInvalidUnicodeEscapeSequence
: MessageTemplate::kInvalidHexEscapeSequence);
return -1;
return Invalid();
}
x = x * 16 + d;
Advance<capture_raw>();
......@@ -130,17 +136,17 @@ uc32 Scanner::ScanHexNumber(int expected_length) {
}
template <bool capture_raw>
uc32 Scanner::ScanUnlimitedLengthHexNumber(int max_value, int beg_pos) {
uc32 Scanner::ScanUnlimitedLengthHexNumber(uc32 max_value, int beg_pos) {
uc32 x = 0;
int d = HexValue(c0_);
if (d < 0) return -1;
if (d < 0) return Invalid();
while (d >= 0) {
x = x * 16 + d;
if (x > max_value) {
ReportScannerError(Location(beg_pos, source_pos() + 1),
MessageTemplate::kUndefinedUnicodeCodePoint);
return -1;
return Invalid();
}
Advance<capture_raw>();
d = HexValue(c0_);
......@@ -386,7 +392,7 @@ bool Scanner::ScanEscape() {
case 't' : c = '\t'; break;
case 'u' : {
c = ScanUnicodeEscape<capture_raw>();
if (c < 0) return false;
if (IsInvalid(c)) return false;
break;
}
case 'v':
......@@ -394,7 +400,7 @@ bool Scanner::ScanEscape() {
break;
case 'x': {
c = ScanHexNumber<capture_raw>(2);
if (c < 0) return false;
if (IsInvalid(c)) return false;
break;
}
case '0': // Fall through.
......@@ -416,6 +422,7 @@ bool Scanner::ScanEscape() {
template <bool capture_raw>
uc32 Scanner::ScanOctalEscape(uc32 c, int length) {
DCHECK('0' <= c && c <= '7');
uc32 x = c - '0';
int i = 0;
for (; i < length; i++) {
......@@ -553,7 +560,7 @@ Token::Value Scanner::ScanTemplateSpan() {
scanner_error_state.MoveErrorTo(next_);
octal_error_state.MoveErrorTo(next_);
}
} else if (c < 0) {
} else if (c == kEndOfInput) {
// Unterminated template literal
break;
} else {
......@@ -861,7 +868,7 @@ Token::Value Scanner::ScanNumber(bool seen_period) {
uc32 Scanner::ScanIdentifierUnicodeEscape() {
Advance();
if (c0_ != 'u') return -1;
if (c0_ != 'u') return Invalid();
Advance();
return ScanUnicodeEscape<false>();
}
......@@ -873,11 +880,12 @@ uc32 Scanner::ScanUnicodeEscape() {
if (c0_ == '{') {
int begin = source_pos() - 2;
Advance<capture_raw>();
uc32 cp = ScanUnlimitedLengthHexNumber<capture_raw>(0x10FFFF, begin);
if (cp < 0 || c0_ != '}') {
uc32 cp =
ScanUnlimitedLengthHexNumber<capture_raw>(String::kMaxCodePoint, begin);
if (cp == kInvalidSequence || c0_ != '}') {
ReportScannerError(source_pos(),
MessageTemplate::kInvalidUnicodeEscapeSequence);
return -1;
return Invalid();
}
Advance<capture_raw>();
return cp;
......@@ -895,7 +903,7 @@ Token::Value Scanner::ScanIdentifierOrKeywordInnerSlow(bool escaped,
// Only allow legal identifier part characters.
// TODO(verwaest): Make this true.
// DCHECK(!IsIdentifierPart('\'));
DCHECK(!IsIdentifierPart(-1));
DCHECK(!IsIdentifierPart(Invalid()));
if (c == '\\' || !IsIdentifierPart(c)) {
return Token::ILLEGAL;
}
......
......@@ -39,7 +39,7 @@ class Zone;
// or one part of a surrogate pair that make a single 21 bit code point.
class Utf16CharacterStream {
public:
static const uc32 kEndOfInput = -1;
static constexpr uc32 kEndOfInput = static_cast<uc32>(-1);
virtual ~Utf16CharacterStream() = default;
......@@ -267,8 +267,11 @@ class V8_EXPORT_PRIVATE Scanner {
};
// -1 is outside of the range of any real source code.
static const int kNoOctalLocation = -1;
static const uc32 kEndOfInput = Utf16CharacterStream::kEndOfInput;
static constexpr uc32 kEndOfInput = Utf16CharacterStream::kEndOfInput;
static constexpr uc32 kInvalidSequence = static_cast<uc32>(-1);
static constexpr uc32 Invalid() { return Scanner::kInvalidSequence; }
static bool IsInvalid(uc32 c);
explicit Scanner(Utf16CharacterStream* source, UnoptimizedCompileFlags flags);
......@@ -623,7 +626,7 @@ class V8_EXPORT_PRIVATE Scanner {
// number can be 000000001, so it's very long in characters but its value is
// small.
template <bool capture_raw>
uc32 ScanUnlimitedLengthHexNumber(int max_value, int beg_pos);
uc32 ScanUnlimitedLengthHexNumber(uc32 max_value, int beg_pos);
// Scans a single JavaScript token.
V8_INLINE Token::Value ScanSingleToken();
......
......@@ -55,8 +55,9 @@ void PrintSpecial(std::ofstream& out) {
CHECK(U_SUCCESS(status));
// Iterate through all chars in BMP except surrogates.
for (UChar32 i = 0; i < kNonBmpStart; i++) {
if (i >= kSurrogateStart && i <= kSurrogateEnd) {
for (UChar32 i = 0; i < static_cast<UChar32>(kNonBmpStart); i++) {
if (i >= static_cast<UChar32>(kSurrogateStart) &&
i <= static_cast<UChar32>(kSurrogateEnd)) {
continue; // Ignore surrogate range
}
current.set(i, i);
......
......@@ -56,11 +56,11 @@ static bool CompareInverseRanges(ZoneList<CharacterRange>* ranges,
return false;
}
for (int i = 0; i < length; i += 2) {
if (special_class[i] != (range.to() + 1)) {
if (static_cast<uc32>(special_class[i]) != (range.to() + 1)) {
return false;
}
range = ranges->at((i >> 1) + 1);
if (special_class[i + 1] != range.from()) {
if (static_cast<uc32>(special_class[i + 1]) != range.from()) {
return false;
}
}
......@@ -79,8 +79,8 @@ static bool CompareRanges(ZoneList<CharacterRange>* ranges,
}
for (int i = 0; i < length; i += 2) {
CharacterRange range = ranges->at(i >> 1);
if (range.from() != special_class[i] ||
range.to() != special_class[i + 1] - 1) {
if (range.from() != static_cast<uc32>(special_class[i]) ||
range.to() != static_cast<uc32>(special_class[i + 1] - 1)) {
return false;
}
}
......@@ -1154,7 +1154,7 @@ void CharacterRange::AddCaseEquivalents(Isolate* isolate, Zone* zone,
CharacterRange range = ranges->at(i);
uc32 from = range.from();
if (from > String::kMaxUtf16CodeUnit) continue;
uc32 to = Min(range.to(), String::kMaxUtf16CodeUnit);
uc32 to = Min(range.to(), String::kMaxUtf16CodeUnitU);
// Nothing to be done for surrogates.
if (from >= kLeadSurrogateStart && to <= kTrailSurrogateEnd) continue;
if (is_one_byte && !RangeContainsLatin1Equivalents(range)) {
......@@ -1197,7 +1197,7 @@ void CharacterRange::AddCaseEquivalents(Isolate* isolate, Zone* zone,
CharacterRange range = ranges->at(i);
uc32 bottom = range.from();
if (bottom > String::kMaxUtf16CodeUnit) continue;
uc32 top = Min(range.to(), String::kMaxUtf16CodeUnit);
uc32 top = Min(range.to(), String::kMaxUtf16CodeUnitU);
// Nothing to be done for surrogates.
if (bottom >= kLeadSurrogateStart && top <= kTrailSurrogateEnd) continue;
if (is_one_byte && !RangeContainsLatin1Equivalents(range)) {
......@@ -1232,7 +1232,7 @@ void CharacterRange::AddCaseEquivalents(Isolate* isolate, Zone* zone,
// block we do this for all the blocks covered by the range (handling
// characters that is not in a block as a "singleton block").
unibrow::uchar equivalents[unibrow::Ecma262UnCanonicalize::kMaxWidth];
int pos = bottom;
uc32 pos = bottom;
while (pos <= top) {
int length =
isolate->jsregexp_canonrange()->get(pos, '\0', equivalents);
......@@ -1265,7 +1265,7 @@ bool CharacterRange::IsCanonical(ZoneList<CharacterRange>* ranges) {
DCHECK_NOT_NULL(ranges);
int n = ranges->length();
if (n <= 1) return true;
int max = ranges->at(0).to();
uc32 max = ranges->at(0).to();
for (int i = 1; i < n; i++) {
CharacterRange next_range = ranges->at(i);
if (next_range.from() <= max + 1) return false;
......@@ -1366,7 +1366,7 @@ void CharacterRange::Canonicalize(ZoneList<CharacterRange>* character_ranges) {
// Check whether ranges are already canonical (increasing, non-overlapping,
// non-adjacent).
int n = character_ranges->length();
int max = character_ranges->at(0).to();
uc32 max = character_ranges->at(0).to();
int i = 1;
while (i < n) {
CharacterRange current = character_ranges->at(i);
......
......@@ -1126,7 +1126,7 @@ static void GenerateBranches(RegExpMacroAssembler* masm, ZoneList<int>* ranges,
return;
}
if ((min_char >> kBits) != (first >> kBits)) {
if ((min_char >> kBits) != static_cast<uc32>(first >> kBits)) {
masm->CheckCharacterLT(first, odd_label);
GenerateBranches(masm, ranges, start_index + 1, end_index, first, max_char,
fall_through, odd_label, even_label);
......@@ -1197,7 +1197,7 @@ static void EmitCharClass(RegExpMacroAssembler* macro_assembler,
int last_valid_range = range_count - 1;
while (last_valid_range >= 0) {
CharacterRange& range = ranges->at(last_valid_range);
if (range.from() <= max_char) {
if (static_cast<int>(range.from()) <= max_char) {
break;
}
last_valid_range--;
......@@ -1240,6 +1240,7 @@ static void EmitCharClass(RegExpMacroAssembler* macro_assembler,
// entry at zero which goes to the failure label, but if there
// was already one there we fall through for success on that entry.
// Subsequent entries have alternating meaning (success/failure).
// TODO(jgruber,v8:10568): Change `range_boundaries` to a ZoneList<uc32>.
ZoneList<int>* range_boundaries =
new (zone) ZoneList<int>(last_valid_range, zone);
......@@ -1636,7 +1637,7 @@ void TextNode::GetQuickCheckDetails(QuickCheckDetails* details,
pos->value = 0;
} else {
int first_range = 0;
while (ranges->at(first_range).from() > char_mask) {
while (static_cast<int>(ranges->at(first_range).from()) > char_mask) {
first_range++;
if (first_range == ranges->length()) {
details->set_cannot_match();
......@@ -3788,7 +3789,7 @@ void TextNode::FillInBMInfo(Isolate* isolate, int initial_offset, int budget,
} else {
for (int k = 0; k < ranges->length(); k++) {
CharacterRange& range = ranges->at(k);
if (range.from() > max_char) continue;
if (static_cast<int>(range.from()) > max_char) continue;
int to = Min(max_char, static_cast<int>(range.to()));
bm->SetInterval(offset, Interval(range.from(), to));
}
......
......@@ -1550,7 +1550,7 @@ bool RegExpParser::ParseUnlimitedLengthHexNumber(int max_value, uc32* value) {
}
while (d >= 0) {
x = x * 16 + d;
if (x > max_value) {
if (x > static_cast<uc32>(max_value)) {
return false;
}
Advance();
......
......@@ -448,17 +448,17 @@ void TestCharacterStream(const char* reference, i::Utf16CharacterStream* stream,
CHECK_EQU(reference[i], stream->Advance());
}
CHECK_EQU(i, stream->pos());
CHECK_LT(stream->Advance(), 0);
CHECK(i::Scanner::IsInvalid(stream->Advance()));
// Seek back, then seek beyond end of stream.
stream->Seek(start);
if (start < length) {
CHECK_EQU(stream->Advance(), reference[start]);
} else {
CHECK_LT(stream->Advance(), 0);
CHECK(i::Scanner::IsInvalid(stream->Advance()));
}
stream->Seek(length + 5);
CHECK_LT(stream->Advance(), 0);
CHECK(i::Scanner::IsInvalid(stream->Advance()));
}
void TestCloneCharacterStream(const char* reference,
......
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