Skip to content

Commit

Permalink
Bug 1393230 - Part 3: Convert the xpcom string classes to be template…
Browse files Browse the repository at this point in the history
…d on char type. r=njn, r=fitzgen, r=sfink

This removes the double-include macro hackery that we use to define two
separate string types (nsAString and nsACString) in favor of a templated
solution.

Annotations for Valgrind and the JS hazard analysis are updated as well as
the rust binding generations for string code.

--HG--
extra : rebase_source : 63ab2c4620cfcd4b764d42d654c82f30f984d016
extra : source : 9115364cd4aa078c49bba7911069f8178e55166f
  • Loading branch information
EricRahm committed Aug 14, 2017
1 parent 0617c21 commit 030b39d
Show file tree
Hide file tree
Showing 42 changed files with 1,910 additions and 1,469 deletions.
2 changes: 1 addition & 1 deletion build/valgrind/cross-architecture.sup
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
PR_SetEnv requires its argument to be leaked, but does not appear on stacks. (See bug 793549.)
Memcheck:Leak
...
fun:_ZL13SaveWordToEnvPKcRK10nsACString
fun:_ZL13SaveWordToEnvPKcRK12nsTSubstringIcE
...
}
{
Expand Down
22 changes: 11 additions & 11 deletions js/src/devtools/rootAnalysis/analyzeHeapWrites.js
Original file line number Diff line number Diff line change
Expand Up @@ -445,17 +445,17 @@ function ignoreContents(entry)
/nsTArray_base.*?::EnsureCapacity/,
/nsTArray_base.*?::ShiftData/,
/AutoTArray.*?::Init/,
/nsAC?String::SetCapacity/,
/nsAC?String::SetLength/,
/nsAC?String::Assign/,
/nsAC?String::Append/,
/nsAC?String::Replace/,
/nsAC?String::Trim/,
/nsAC?String::Truncate/,
/nsAString::StripTaggedASCII/,
/nsAC?String::operator=/,
/nsAutoString::nsAutoString/,
/nsFixedCString::nsFixedCString/,
/nsTSubstring<T>::SetCapacity/,
/nsTSubstring<T>::SetLength/,
/nsTSubstring<T>::Assign/,
/nsTSubstring<T>::Append/,
/nsTSubstring<T>::Replace/,
/nsTSubstring<T>::Trim/,
/nsTSubstring<T>::Truncate/,
/nsTSubstring<T>::StripTaggedASCII/,
/nsTSubstring<T>::operator=/,
/nsTAutoStringN<T, N>::nsTAutoStringN/,
/nsTFixedString<T>::nsTFixedString/,

// Similar for some other data structures
/nsCOMArray_base::SetCapacity/,
Expand Down
7 changes: 5 additions & 2 deletions layout/style/ServoBindings.toml
Original file line number Diff line number Diff line change
Expand Up @@ -343,7 +343,8 @@ mapped-generic-types = [
{ generic = false, gecko = "ServoStyleContextStrong", servo = "::gecko_bindings::sugar::ownership::Strong<::properties::ComputedValues>" },
]
fixups = [
{ pat = "root::nsString", rep = "::nsstring::nsStringRepr" },
{ pat = "\\broot::nsString\\b", rep = "::nsstring::nsStringRepr" },
{ pat = "\\broot::nsTString<u16>", rep = "::nsstring::nsStringRepr" },
]

[bindings]
Expand Down Expand Up @@ -541,6 +542,8 @@ servo-borrow-types = [
"RawGeckoStyleChildrenIterator",
]
fixups = [
# Remap the templated string type to the helper type
{ pat = "\\bnsTString<u16>", rep = "nsString" },
# hack for gecko-owned string
{ pat = "<nsString", rep = "<nsStringRepr" },
{ pat = "\\b<nsString\\b", rep = "<nsStringRepr" },
]
6 changes: 3 additions & 3 deletions xpcom/string/moz.build
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ EXPORTS += [
'nsReadableUtils.h',
'nsString.h',
'nsStringBuffer.h',
'nsStringFlags.h',
'nsStringFwd.h',
'nsStringIterator.h',
'nsSubstring.h',
Expand All @@ -29,12 +30,10 @@ EXPORTS += [
'nsTLiteralString.h',
'nsTPromiseFlatString.h',
'nsTString.h',
'nsTStringRepr.h',
'nsTSubstring.h',
'nsTSubstringTuple.h',
'nsUTF8Utils.h',
'string-template-def-char.h',
'string-template-def-unichar.h',
'string-template-undef.h',
]

UNIFIED_SOURCES += [
Expand All @@ -49,6 +48,7 @@ UNIFIED_SOURCES += [
'nsSubstring.cpp',
'nsSubstringTuple.cpp',
'nsTextFormatter.cpp',
'precompiled_templates.cpp',
]

# Are we targeting x86 or x86-64? If so, compile the SSE2 functions for
Expand Down
74 changes: 2 additions & 72 deletions xpcom/string/nsAString.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,79 +17,9 @@

#define kNotFound -1

namespace mozilla {
namespace detail {
// NOTE: these flags are declared public _only_ for convenience inside
// the string implementation. And they are outside of the string
// class so that the type is the same for both narrow and wide
// strings.

// bits for mDataFlags
enum class StringDataFlags : uint16_t
{
// Some terminology:
//
// "dependent buffer" A dependent buffer is one that the string class
// does not own. The string class relies on some
// external code to ensure the lifetime of the
// dependent buffer.
//
// "shared buffer" A shared buffer is one that the string class
// allocates. When it allocates a shared string
// buffer, it allocates some additional space at
// the beginning of the buffer for additional
// fields, including a reference count and a
// buffer length. See nsStringHeader.
//
// "adopted buffer" An adopted buffer is a raw string buffer
// allocated on the heap (using moz_xmalloc)
// of which the string class subsumes ownership.
//
// Some comments about the string data flags:
//
// SHARED, OWNED, and FIXED are all mutually exlusive. They
// indicate the allocation type of mData. If none of these flags
// are set, then the string buffer is dependent.
//
// SHARED, OWNED, or FIXED imply TERMINATED. This is because
// the string classes always allocate null-terminated buffers, and
// non-terminated substrings are always dependent.
//
// VOIDED implies TERMINATED, and moreover it implies that mData
// points to char_traits::sEmptyBuffer. Therefore, VOIDED is
// mutually exclusive with SHARED, OWNED, and FIXED.

TERMINATED = 1 << 0, // IsTerminated returns true
VOIDED = 1 << 1, // IsVoid returns true
SHARED = 1 << 2, // mData points to a heap-allocated, shared buffer
OWNED = 1 << 3, // mData points to a heap-allocated, raw buffer
FIXED = 1 << 4, // mData points to a fixed-size writable, dependent buffer
LITERAL = 1 << 5 // mData points to a string literal; DataFlags::TERMINATED will also be set
};

// bits for mClassFlags
enum class StringClassFlags : uint16_t
{
FIXED = 1 << 0, // |this| is of type nsTFixedString
NULL_TERMINATED = 1 << 1 // |this| requires its buffer is null-terminated
};

MOZ_MAKE_ENUM_CLASS_BITWISE_OPERATORS(StringDataFlags)
MOZ_MAKE_ENUM_CLASS_BITWISE_OPERATORS(StringClassFlags)

} // namespace detail
} // namespace mozilla

// declare nsAString
#include "string-template-def-unichar.h"
#include "nsStringFlags.h"
#include "nsTStringRepr.h"
#include "nsTSubstring.h"
#include "string-template-undef.h"

// declare nsACString
#include "string-template-def-char.h"
#include "nsTSubstring.h"
#include "string-template-undef.h"


/**
* ASCII case-insensitive comparator. (for Unicode case-insensitive
Expand Down
8 changes: 0 additions & 8 deletions xpcom/string/nsDependentString.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,4 @@
#include "nsDependentString.h"
#include "nsAlgorithm.h"

// define nsDependentString
#include "string-template-def-unichar.h"
#include "nsTDependentString.cpp"
#include "string-template-undef.h"

// define nsDependentCString
#include "string-template-def-char.h"
#include "nsTDependentString.cpp"
#include "string-template-undef.h"
7 changes: 0 additions & 7 deletions xpcom/string/nsDependentString.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,7 @@
#include "nsString.h"
#include "nsDebug.h"

// declare nsDependentString
#include "string-template-def-unichar.h"
#include "nsTDependentString.h"
#include "string-template-undef.h"

// declare nsDependentCString
#include "string-template-def-char.h"
#include "nsTDependentString.h"
#include "string-template-undef.h"

#endif /* !defined(nsDependentString_h___) */
8 changes: 0 additions & 8 deletions xpcom/string/nsDependentSubstring.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,4 @@
#include "nsDependentSubstring.h"
#include "nsAlgorithm.h"

// define nsDependentSubstring
#include "string-template-def-unichar.h"
#include "nsTDependentSubstring.cpp"
#include "string-template-undef.h"

// define nsDependentCSubstring
#include "string-template-def-char.h"
#include "nsTDependentSubstring.cpp"
#include "string-template-undef.h"
9 changes: 0 additions & 9 deletions xpcom/string/nsDependentSubstring.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,6 @@
#define nsDependentSubstring_h___

#include "nsSubstring.h"

// declare nsDependentSubstring
#include "string-template-def-unichar.h"
#include "nsTDependentSubstring.h"
#include "string-template-undef.h"

// declare nsDependentCSubstring
#include "string-template-def-char.h"
#include "nsTDependentSubstring.h"
#include "string-template-undef.h"

#endif /* !defined(nsDependentSubstring_h___) */
8 changes: 0 additions & 8 deletions xpcom/string/nsLiteralString.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,7 @@
#include "nscore.h"
#include "nsString.h"

// declare nsLiteralString
#include "string-template-def-unichar.h"
#include "nsTLiteralString.h"
#include "string-template-undef.h"

// declare nsLiteralCString
#include "string-template-def-char.h"
#include "nsTLiteralString.h"
#include "string-template-undef.h"

#include "mozilla/Char16.h"

Expand Down
8 changes: 0 additions & 8 deletions xpcom/string/nsPromiseFlatString.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,4 @@

#include "nsPromiseFlatString.h"

// define nsPromiseFlatString
#include "string-template-def-unichar.h"
#include "nsTPromiseFlatString.cpp"
#include "string-template-undef.h"

// define nsPromiseFlatCString
#include "string-template-def-char.h"
#include "nsTPromiseFlatString.cpp"
#include "string-template-undef.h"
8 changes: 0 additions & 8 deletions xpcom/string/nsPromiseFlatString.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,6 @@

#include "nsString.h"

// declare nsPromiseFlatString
#include "string-template-def-unichar.h"
#include "nsTPromiseFlatString.h"
#include "string-template-undef.h"

// declare nsPromiseFlatCString
#include "string-template-def-char.h"
#include "nsTPromiseFlatString.h"
#include "string-template-undef.h"

#endif /* !defined(nsPromiseFlatString_h___) */
8 changes: 0 additions & 8 deletions xpcom/string/nsString.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,4 @@

#include "nsString.h"

// define nsString
#include "string-template-def-unichar.h"
#include "nsTString.cpp"
#include "string-template-undef.h"

// define nsCString
#include "string-template-def-char.h"
#include "nsTString.cpp"
#include "string-template-undef.h"
23 changes: 12 additions & 11 deletions xpcom/string/nsString.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@

#include "mozilla/Attributes.h"

#include "nsStringFwd.h"

#include "nsSubstring.h"
#include "nsDependentSubstring.h"
#include "nsReadableUtils.h"
Expand All @@ -27,16 +29,7 @@
#define kAutoDetect (100)
#endif


// declare nsString, et. al.
#include "string-template-def-unichar.h"
#include "nsTString.h"
#include "string-template-undef.h"

// declare nsCString, et. al.
#include "string-template-def-char.h"
#include "nsTString.h"
#include "string-template-undef.h"

static_assert(sizeof(char16_t) == 2, "size of char16_t must be 2");
static_assert(sizeof(nsString::char_type) == 2,
Expand All @@ -46,6 +39,14 @@ static_assert(nsString::char_type(-1) > nsString::char_type(0),
static_assert(sizeof(nsCString::char_type) == 1,
"size of nsCString::char_type must be 1");

static_assert(sizeof(nsTLiteralString<char>) == sizeof(nsTString<char>),
"nsLiteralCString can masquerade as nsCString, "
"so they must have identical layout");

static_assert(sizeof(nsTLiteralString<char16_t>) == sizeof(nsTString<char16_t>),
"nsTLiteralString can masquerade as nsString, "
"so they must have identical layout");


/**
* A helper class that converts a UTF-16 string to ASCII in a lossy manner
Expand All @@ -60,7 +61,7 @@ class NS_LossyConvertUTF16toASCII : public nsAutoCString

NS_LossyConvertUTF16toASCII(const char16ptr_t aString, uint32_t aLength)
{
LossyAppendUTF16toASCII(Substring(aString, aLength), *this);
LossyAppendUTF16toASCII(Substring(static_cast<const char16_t*>(aString), aLength), *this);
}

explicit NS_LossyConvertUTF16toASCII(const nsAString& aString)
Expand Down Expand Up @@ -111,7 +112,7 @@ class NS_ConvertUTF16toUTF8 : public nsAutoCString

NS_ConvertUTF16toUTF8(const char16ptr_t aString, uint32_t aLength)
{
AppendUTF16toUTF8(Substring(aString, aLength), *this);
AppendUTF16toUTF8(Substring(static_cast<const char16_t*>(aString), aLength), *this);
}

explicit NS_ConvertUTF16toUTF8(const nsAString& aString)
Expand Down
9 changes: 0 additions & 9 deletions xpcom/string/nsStringComparator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,7 @@
#include "nsAString.h"
#include "plstr.h"


// define nsStringComparator
#include "string-template-def-unichar.h"
#include "nsTStringComparator.cpp"
#include "string-template-undef.h"

// define nsCStringComparator
#include "string-template-def-char.h"
#include "nsTStringComparator.cpp"
#include "string-template-undef.h"


int
Expand Down
Loading

0 comments on commit 030b39d

Please sign in to comment.