Bug 1604188: Replace unreachable string functions with if-constexpr code. r=jandem,jwalden

Instead of using MOZ_CRASH/MOZ_ASSERT_UNREACHABLE for unreachable functions, we can
now use if-constexpr to avoid generating the unreachable calls in the first place.

Differential Revision: https://phabricator.services.mozilla.com/D57324

--HG--
extra : moz-landing-system : lando
This commit is contained in:
André Bargull 2020-01-06 12:04:29 +00:00
Родитель b92dbf5479
Коммит 27f9867d61
2 изменённых файлов: 48 добавлений и 67 удалений

Просмотреть файл

@ -692,12 +692,6 @@ static char16_t Final_Sigma(const char16_t* chars, size_t length,
return unicode::GREEK_SMALL_LETTER_SIGMA;
}
static Latin1Char Final_Sigma(const Latin1Char* chars, size_t length,
size_t index) {
MOZ_ASSERT_UNREACHABLE("U+03A3 is not a Latin-1 character");
return 0;
}
// If |srcLength == destLength| is true, the destination buffer was allocated
// with the same size as the source buffer. When we append characters which
// have special casing mappings, we test |srcLength == destLength| to decide
@ -716,7 +710,7 @@ static size_t ToLowerCaseImpl(CharT* destChars, const CharT* srcChars,
size_t j = startIndex;
for (size_t i = startIndex; i < srcLength; i++) {
char16_t c = srcChars[i];
if (!IsSame<CharT, Latin1Char>::value) {
if constexpr (!IsSame<CharT, Latin1Char>::value) {
if (unicode::IsLeadSurrogate(c) && i + 1 < srcLength) {
char16_t trail = srcChars[i + 1];
if (unicode::IsTrailSurrogate(trail)) {
@ -773,12 +767,6 @@ static size_t ToLowerCaseLength(const char16_t* chars, size_t startIndex,
return lowerLength;
}
static size_t ToLowerCaseLength(const Latin1Char* chars, size_t startIndex,
size_t length) {
MOZ_ASSERT_UNREACHABLE("never called for Latin-1 strings");
return 0;
}
template <typename CharT>
static JSString* ToLowerCase(JSContext* cx, JSLinearString* str) {
// Unlike toUpperCase, toLowerCase has the nice invariant that if the
@ -804,7 +792,7 @@ static JSString* ToLowerCase(JSContext* cx, JSLinearString* str) {
// One element Latin-1 strings can be directly retrieved from the
// static strings cache.
if (IsSame<CharT, Latin1Char>::value) {
if constexpr (IsSame<CharT, Latin1Char>::value) {
if (length == 1) {
char16_t lower = unicode::ToLowerCase(chars[0]);
MOZ_ASSERT(lower <= JSString::MAX_LATIN1_CHAR);
@ -818,7 +806,7 @@ static JSString* ToLowerCase(JSContext* cx, JSLinearString* str) {
size_t i = 0;
for (; i < length; i++) {
CharT c = chars[i];
if (!IsSame<CharT, Latin1Char>::value) {
if constexpr (!IsSame<CharT, Latin1Char>::value) {
if (unicode::IsLeadSurrogate(c) && i + 1 < length) {
CharT trail = chars[i + 1];
if (unicode::IsTrailSurrogate(trail)) {
@ -850,18 +838,21 @@ static JSString* ToLowerCase(JSContext* cx, JSLinearString* str) {
size_t readChars =
ToLowerCaseImpl(newChars.get(), chars, i, length, resultLength);
if (readChars < length) {
MOZ_ASSERT((!IsSame<CharT, Latin1Char>::value),
"Latin-1 strings don't have special lower case mappings");
resultLength = ToLowerCaseLength(chars, readChars, length);
if constexpr (!IsSame<CharT, Latin1Char>::value) {
if (readChars < length) {
resultLength = ToLowerCaseLength(chars, readChars, length);
if (!newChars.maybeRealloc(cx, length, resultLength)) {
return nullptr;
if (!newChars.maybeRealloc(cx, length, resultLength)) {
return nullptr;
}
MOZ_ALWAYS_TRUE(length == ToLowerCaseImpl(newChars.get(), chars,
readChars, length,
resultLength));
}
MOZ_ALWAYS_TRUE(length == ToLowerCaseImpl(newChars.get(), chars,
readChars, length,
resultLength));
} else {
MOZ_ASSERT(readChars == length,
"Latin-1 strings don't have special lower case mappings");
}
}
@ -1091,7 +1082,7 @@ static size_t ToUpperCaseImpl(DestChar* destChars, const SrcChar* srcChars,
size_t j = startIndex;
for (size_t i = startIndex; i < srcLength; i++) {
char16_t c = srcChars[i];
if (!IsSame<DestChar, Latin1Char>::value) {
if constexpr (!IsSame<DestChar, Latin1Char>::value) {
if (unicode::IsLeadSurrogate(c) && i + 1 < srcLength) {
char16_t trail = srcChars[i + 1];
if (unicode::IsTrailSurrogate(trail)) {
@ -1125,15 +1116,6 @@ static size_t ToUpperCaseImpl(DestChar* destChars, const SrcChar* srcChars,
return srcLength;
}
// Explicit instantiation so we don't hit the static_assert from above.
static bool ToUpperCaseImpl(Latin1Char* destChars, const char16_t* srcChars,
size_t startIndex, size_t srcLength,
size_t destLength) {
MOZ_ASSERT_UNREACHABLE(
"cannot write non-Latin-1 characters into Latin-1 string");
return false;
}
template <typename CharT>
static size_t ToUpperCaseLength(const CharT* chars, size_t startIndex,
size_t length) {
@ -1209,7 +1191,7 @@ static JSString* ToUpperCase(JSContext* cx, JSLinearString* str) {
// Most one element Latin-1 strings can be directly retrieved from the
// static strings cache.
if (IsSame<CharT, Latin1Char>::value) {
if constexpr (IsSame<CharT, Latin1Char>::value) {
if (length == 1) {
Latin1Char c = chars[0];
if (c != unicode::MICRO_SIGN &&
@ -1231,7 +1213,7 @@ static JSString* ToUpperCase(JSContext* cx, JSLinearString* str) {
size_t i = 0;
for (; i < length; i++) {
CharT c = chars[i];
if (!IsSame<CharT, Latin1Char>::value) {
if constexpr (!IsSame<CharT, Latin1Char>::value) {
if (unicode::IsLeadSurrogate(c) && i + 1 < length) {
CharT trail = chars[i + 1];
if (unicode::IsTrailSurrogate(trail)) {
@ -1268,9 +1250,8 @@ static JSString* ToUpperCase(JSContext* cx, JSLinearString* str) {
// If the original string is a two-byte string, its uppercase form is
// so rarely Latin-1 that we don't even consider creating a new
// Latin-1 string.
bool resultIsLatin1;
if (IsSame<CharT, Latin1Char>::value) {
resultIsLatin1 = true;
if constexpr (IsSame<CharT, Latin1Char>::value) {
bool resultIsLatin1 = true;
for (size_t j = i; j < length; j++) {
Latin1Char c = chars[j];
if (c == unicode::MICRO_SIGN ||
@ -1282,16 +1263,21 @@ static JSString* ToUpperCase(JSContext* cx, JSLinearString* str) {
MOZ_ASSERT(unicode::ToUpperCase(c) <= JSString::MAX_LATIN1_CHAR);
}
}
} else {
resultIsLatin1 = false;
}
if (resultIsLatin1) {
newChars.construct<Latin1Buffer>();
if (resultIsLatin1) {
newChars.construct<Latin1Buffer>();
if (!ToUpperCase(cx, newChars.ref<Latin1Buffer>(), chars, i, length,
&resultLength)) {
return nullptr;
if (!ToUpperCase(cx, newChars.ref<Latin1Buffer>(), chars, i, length,
&resultLength)) {
return nullptr;
}
} else {
newChars.construct<TwoByteBuffer>();
if (!ToUpperCase(cx, newChars.ref<TwoByteBuffer>(), chars, i, length,
&resultLength)) {
return nullptr;
}
}
} else {
newChars.construct<TwoByteBuffer>();

Просмотреть файл

@ -1501,10 +1501,6 @@ static inline bool CanStoreCharsAsLatin1(const char16_t* s, size_t length) {
return IsUtf16Latin1(MakeSpan(s, length));
}
static bool CanStoreCharsAsLatin1(const Latin1Char* s, size_t length) {
MOZ_CRASH("Shouldn't be called for Latin1 chars");
}
static bool CanStoreCharsAsLatin1(LittleEndianChars chars, size_t length) {
for (size_t i = 0; i < length; i++) {
if (chars[i] > JSString::MAX_LATIN1_CHAR) {
@ -1556,12 +1552,6 @@ static JSLinearString* NewStringDeflated(JSContext* cx, const char16_t* s,
return JSLinearString::new_<allowGC>(cx, std::move(news), n);
}
template <AllowGC allowGC>
static JSLinearString* NewStringDeflated(JSContext* cx, const Latin1Char* s,
size_t n) {
MOZ_CRASH("Shouldn't be called for Latin1 chars");
}
static JSLinearString* NewStringDeflatedFromLittleEndianNoGC(
JSContext* cx, LittleEndianChars chars, size_t length) {
MOZ_ASSERT(CanStoreCharsAsLatin1(chars, length));
@ -1618,10 +1608,12 @@ template <typename CharT>
JSLinearString* js::NewString(JSContext* cx,
UniquePtr<CharT[], JS::FreePolicy> chars,
size_t length) {
if (IsSame<CharT, char16_t>::value &&
CanStoreCharsAsLatin1(chars.get(), length)) {
// Deflating copies from |chars.get()| and lets |chars| be freed on return.
return NewStringDeflated<CanGC>(cx, chars.get(), length);
if constexpr (IsSame<CharT, char16_t>::value) {
if (CanStoreCharsAsLatin1(chars.get(), length)) {
// Deflating copies from |chars.get()| and lets |chars| be freed on
// return.
return NewStringDeflated<CanGC>(cx, chars.get(), length);
}
}
return NewStringDontDeflate(cx, std::move(chars), length);
@ -1665,9 +1657,10 @@ template <AllowGC allowGC, typename CharT>
JSLinearString* js::NewString(JSContext* cx,
UniquePtr<CharT[], JS::FreePolicy> chars,
size_t length) {
if (IsSame<CharT, char16_t>::value &&
CanStoreCharsAsLatin1(chars.get(), length)) {
return NewStringDeflated<allowGC>(cx, chars.get(), length);
if constexpr (IsSame<CharT, char16_t>::value) {
if (CanStoreCharsAsLatin1(chars.get(), length)) {
return NewStringDeflated<allowGC>(cx, chars.get(), length);
}
}
return NewStringDontDeflate<allowGC>(cx, std::move(chars), length);
@ -1763,8 +1756,10 @@ JSLinearString* NewLatin1StringZ(JSContext* cx, UniqueChars chars) {
template <AllowGC allowGC, typename CharT>
JSLinearString* NewStringCopyN(JSContext* cx, const CharT* s, size_t n) {
if (IsSame<CharT, char16_t>::value && CanStoreCharsAsLatin1(s, n)) {
return NewStringDeflated<allowGC>(cx, s, n);
if constexpr (IsSame<CharT, char16_t>::value) {
if (CanStoreCharsAsLatin1(s, n)) {
return NewStringDeflated<allowGC>(cx, s, n);
}
}
return NewStringCopyNDontDeflate<allowGC>(cx, s, n);