diff options
author | Luboš Luňák <l.lunak@suse.cz> | 2012-03-13 16:09:25 +0100 |
---|---|---|
committer | Luboš Luňák <l.lunak@suse.cz> | 2012-03-13 16:21:04 +0100 |
commit | 3d9f5c856a4a77503695e68ce013ebf859fcb212 (patch) | |
tree | aaa75452b54542a52f746da9782ae6c3d2663453 /sal | |
parent | Resolves: fdo#46728 reference count the overlay managers (diff) | |
download | core-3d9f5c856a4a77503695e68ce013ebf859fcb212.tar.gz core-3d9f5c856a4a77503695e68ce013ebf859fcb212.zip |
make OString work even with old gcc that has SFINAE broken
Since OString already has a ctor accepting const char*, I cannot
find out a way to distinguish string literals other than using
a template, otherwise const char* somehow takes precedence
(all of gcc, clang, msvc). But the template requires
the Substitution Is Not A Failure Idiom to actually create
only wanted instances. And the compiler can try evaluate
the OString ctor as a possibility when comparing an int to
an anonymous enum, and anonymous enum as a type without linkage
cannot be a template argument before C++11. SFINAE should still
work, but not with gcc older than 4.0.2 (which we right now
use only on macs). So for that case disable the string literal
ctors, which means macs will have one extra strlen call,
and also that embedded \0's in string literals will be
inconsistent. The tiny performance problem shouldn't matter that
much and will eventually go away, the \0 problem should not
matter, since before string literal ctors were introduced
\0's had not been included anyway unless RTL_CONSTASCII_STRINGPARAM
was used. So we should be safe and when removing the CONSTASCII
macros \0 cases should be handled by explicitly mentioning
the length.
Diffstat (limited to 'sal')
-rw-r--r-- | sal/inc/rtl/string.hxx | 18 | ||||
-rw-r--r-- | sal/qa/rtl/strings/test_ostring_stringliterals.cxx | 12 | ||||
-rw-r--r-- | sal/rtl/source/strtmpl.cxx | 1 |
3 files changed, 26 insertions, 5 deletions
diff --git a/sal/inc/rtl/string.hxx b/sal/inc/rtl/string.hxx index 6590677efea0..79b9a15ae94a 100644 --- a/sal/inc/rtl/string.hxx +++ b/sal/inc/rtl/string.hxx @@ -85,6 +85,7 @@ namespace rtl use this class. */ +#ifndef HAVE_SFINAE_ANONYMOUS_BROKEN namespace internal { // This template is used for SFINAE (Substitution failure is not an error), to detect that @@ -107,6 +108,7 @@ struct CharPtrDetector< char* > typedef Dummy Type; }; } +#endif class OString { @@ -184,6 +186,17 @@ public: @param value a NULL-terminated character array. */ +#ifdef HAVE_SFINAE_ANONYMOUS_BROKEN + // Old gcc can try to convert anonymous enums to OString and give compile error. + // So there's no special-cased handling of string literals. + // These are inline functions and technically both variants should work + // the same in practice, so there should be no compatibility problem. + OString( const sal_Char * value ) SAL_THROW(()) + { + pData = 0; + rtl_string_newFromStr( &pData, value ); + } +#else template< typename T > OString( const T& value, typename internal::CharPtrDetector< T >::Type = internal::Dummy() ) SAL_THROW(()) { @@ -194,8 +207,8 @@ public: /** New string from a string literal. - Note that embedded \0's are included in the string if explicitly present - in the string literal. + If there are any embedded \0's in the string literal, the result is undefined. + Use the overload that explicitly accepts length. @param literal a string literal */ @@ -221,6 +234,7 @@ public: pData = 0; rtl_string_newFromStr( &pData, value ); } +#endif // HAVE_SFINAE_ANONYMOUS_BROKEN /** New string from a character buffer array. diff --git a/sal/qa/rtl/strings/test_ostring_stringliterals.cxx b/sal/qa/rtl/strings/test_ostring_stringliterals.cxx index 22f79ab630a6..8398ae9e63e9 100644 --- a/sal/qa/rtl/strings/test_ostring_stringliterals.cxx +++ b/sal/qa/rtl/strings/test_ostring_stringliterals.cxx @@ -62,6 +62,8 @@ CPPUNIT_TEST_SUITE_END(); void test::ostring::StringLiterals::checkCtors() { +// string literal ctors do not work with SFINAE broken and are disabled +#ifndef HAVE_SFINAE_ANONYMOUS_BROKEN CPPUNIT_ASSERT( CONST_CTOR_USED( "test" )); const char good1[] = "test"; CPPUNIT_ASSERT( CONST_CTOR_USED( good1 )); @@ -89,13 +91,19 @@ void test::ostring::StringLiterals::checkCtors() CPPUNIT_ASSERT( rtl::OString( (const char*)"ab" ) == rtl::OString( "ab" )); // Check that contents are correct and equal to the case when RTL_CONSTASCII_STRINGPARAM is used. + CPPUNIT_ASSERT( rtl::OString( RTL_CONSTASCII_STRINGPARAM( "" )) == rtl::OString( "" )); + CPPUNIT_ASSERT( rtl::OString( RTL_CONSTASCII_STRINGPARAM( "ab" )) == rtl::OString( "ab" )); +#if 0 +// This is currently disabled because it can't be consistent with HAVE_SFINAE_ANONYMOUS_BROKEN. +// Since the situation wasn't quite consistent even before, there should be no big harm. + // Check also that embedded \0 is included (RTL_CONSTASCII_STRINGPARAM does the same, // const char* ctor does not, but it seems to make more sense to include it when // it's explicitly mentioned in the string literal). - CPPUNIT_ASSERT( rtl::OString( RTL_CONSTASCII_STRINGPARAM( "" )) == rtl::OString( "" )); CPPUNIT_ASSERT( rtl::OString( RTL_CONSTASCII_STRINGPARAM( "\0" )) == rtl::OString( "\0" )); - CPPUNIT_ASSERT( rtl::OString( RTL_CONSTASCII_STRINGPARAM( "ab" )) == rtl::OString( "ab" )); CPPUNIT_ASSERT( rtl::OString( RTL_CONSTASCII_STRINGPARAM( "a\0b" )) == rtl::OString( "a\0b" )); +#endif +#endif } void test::ostring::StringLiterals::testcall( const char str[] ) diff --git a/sal/rtl/source/strtmpl.cxx b/sal/rtl/source/strtmpl.cxx index a3ef1facdadf..2d8c44bbe4fa 100644 --- a/sal/rtl/source/strtmpl.cxx +++ b/sal/rtl/source/strtmpl.cxx @@ -1189,7 +1189,6 @@ void SAL_CALL IMPL_RTL_STRINGNAME( newFromStr_WithLength )( IMPL_RTL_STRINGDATA* /* ----------------------------------------------------------------------- */ // Used when creating from string literals. -// Intentionally copies also embedded \0's if present. void SAL_CALL IMPL_RTL_STRINGNAME( newFromLiteral)( IMPL_RTL_STRINGDATA** ppThis, const sal_Char* pCharStr, sal_Int32 nLen ) |