From b5b52ad027462d11000e1cbf7a4a5a597d6833b2 Mon Sep 17 00:00:00 2001 From: Markus Scherer Date: Wed, 8 Mar 2017 01:07:20 +0000 Subject: [PATCH] ICU-12992 make tools & tests work with configured UChar=uint16_t X-SVN-Rev: 39742 --- icu4c/source/common/normalizer2impl.h | 2 +- icu4c/source/common/uinvchar.h | 6 +++++- icu4c/source/common/unicode/umachine.h | 8 ++++++++ icu4c/source/common/unicode/unistr.h | 17 ++++++++++++----- icu4c/source/common/unistr.cpp | 3 ++- icu4c/source/test/iotest/stream.cpp | 11 +++++++++-- icu4c/source/tools/ctestfw/datamap.cpp | 3 ++- icu4c/source/tools/gennorm2/n2builder.cpp | 10 +++++----- icu4c/source/tools/genrb/reslist.cpp | 2 +- icu4c/source/tools/genrb/reslist.h | 3 ++- icu4c/source/tools/genrb/wrtxml.cpp | 5 +++-- icu4c/source/tools/toolutil/dbgutil.cpp | 5 +++-- icu4c/source/tools/toolutil/ppucd.cpp | 5 +++-- icu4c/source/tools/toolutil/toolutil.h | 14 ++++++++++++++ icu4c/source/tools/toolutil/xmlparser.cpp | 5 +++-- 15 files changed, 73 insertions(+), 26 deletions(-) diff --git a/icu4c/source/common/normalizer2impl.h b/icu4c/source/common/normalizer2impl.h index 6dba0eab5c4..946abee98f3 100644 --- a/icu4c/source/common/normalizer2impl.h +++ b/icu4c/source/common/normalizer2impl.h @@ -176,7 +176,7 @@ public: lastCC=0; } void copyReorderableSuffixTo(UnicodeString &s) const { - s.setTo(reorderStart, (int32_t)(limit-reorderStart)); + s.setTo(ConstChar16Ptr(reorderStart), (int32_t)(limit-reorderStart)); } private: /* diff --git a/icu4c/source/common/uinvchar.h b/icu4c/source/common/uinvchar.h index 0bb5e73d04a..2a960bdfca4 100644 --- a/icu4c/source/common/uinvchar.h +++ b/icu4c/source/common/uinvchar.h @@ -64,7 +64,11 @@ uprv_isInvariantUString(const UChar *s, int32_t length); */ U_INTERNAL inline UBool U_EXPORT2 uprv_isInvariantUnicodeString(const icu::UnicodeString &s) { - return uprv_isInvariantUString(s.getBuffer(), s.length()); + const char16_t *p = s.getBuffer(); +#ifdef U_ALIASING_BARRIER + U_ALIASING_BARRIER(p); +#endif + return uprv_isInvariantUString(reinterpret_cast(p), s.length()); } #endif /* __cplusplus */ diff --git a/icu4c/source/common/unicode/umachine.h b/icu4c/source/common/unicode/umachine.h index e0fedfe9579..4c8b5c1f1e2 100644 --- a/icu4c/source/common/unicode/umachine.h +++ b/icu4c/source/common/unicode/umachine.h @@ -313,6 +313,14 @@ typedef int8_t UBool; * * @stable ICU 4.4 */ +#if 1 + // #if 1 is normal. UChar defaults to char16_t in C++. + // For configuration testing of UChar=uint16_t temporarily change this to #if 0. + // The intltest Makefile #defines UCHAR_TYPE=char16_t, + // so we only #define it to uint16_t if it is undefined so far. +#elif !defined(UCHAR_TYPE) +# define UCHAR_TYPE uint16_t +#endif #if defined(U_COMBINED_IMPLEMENTATION) || defined(U_COMMON_IMPLEMENTATION) || \ defined(U_I18N_IMPLEMENTATION) || defined(U_IO_IMPLEMENTATION) // Inside the ICU library code, never configurable. diff --git a/icu4c/source/common/unicode/unistr.h b/icu4c/source/common/unicode/unistr.h index 9eedd2f52df..835e64ff774 100644 --- a/icu4c/source/common/unicode/unistr.h +++ b/icu4c/source/common/unicode/unistr.h @@ -2059,7 +2059,7 @@ public: * @stable ICU 2.0 */ UnicodeString &setTo(UBool isTerminated, - const char16_t *text, + ConstChar16Ptr text, int32_t textLength); /** @@ -3495,6 +3495,13 @@ protected: virtual UChar32 getChar32At(int32_t offset) const; private: + static inline const UChar *constUCharPtr(const char16_t *p) { +#ifdef U_ALIASING_BARRIER + U_ALIASING_BARRIER(p); +#endif + return reinterpret_cast(p); + } + // For char* constructors. Could be made public. UnicodeString &setToUTF8(StringPiece utf8); // For extract(char*). @@ -4360,7 +4367,7 @@ UnicodeString::startsWith(const UnicodeString& srcText, inline UBool UnicodeString::startsWith(ConstChar16Ptr srcChars, int32_t srcLength) const { if(srcLength < 0) { - srcLength = u_strlen(srcChars); + srcLength = u_strlen(constUCharPtr(srcChars)); } return doCompare(0, srcLength, srcChars, 0, srcLength) == 0; } @@ -4368,7 +4375,7 @@ UnicodeString::startsWith(ConstChar16Ptr srcChars, int32_t srcLength) const { inline UBool UnicodeString::startsWith(const char16_t *srcChars, int32_t srcStart, int32_t srcLength) const { if(srcLength < 0) { - srcLength = u_strlen(srcChars); + srcLength = u_strlen(constUCharPtr(srcChars)); } return doCompare(0, srcLength, srcChars, srcStart, srcLength) == 0; } @@ -4391,7 +4398,7 @@ inline UBool UnicodeString::endsWith(ConstChar16Ptr srcChars, int32_t srcLength) const { if(srcLength < 0) { - srcLength = u_strlen(srcChars); + srcLength = u_strlen(constUCharPtr(srcChars)); } return doCompare(length() - srcLength, srcLength, srcChars, 0, srcLength) == 0; @@ -4402,7 +4409,7 @@ UnicodeString::endsWith(const char16_t *srcChars, int32_t srcStart, int32_t srcLength) const { if(srcLength < 0) { - srcLength = u_strlen(srcChars + srcStart); + srcLength = u_strlen(constUCharPtr(srcChars + srcStart)); } return doCompare(length() - srcLength, srcLength, srcChars, srcStart, srcLength) == 0; diff --git a/icu4c/source/common/unistr.cpp b/icu4c/source/common/unistr.cpp index 416b909917d..1bfb71aa107 100644 --- a/icu4c/source/common/unistr.cpp +++ b/icu4c/source/common/unistr.cpp @@ -1258,7 +1258,7 @@ UnicodeString::getTerminatedBuffer() { // setTo() analogous to the readonly-aliasing constructor with the same signature UnicodeString & UnicodeString::setTo(UBool isTerminated, - const UChar *text, + ConstChar16Ptr textPtr, int32_t textLength) { if(fUnion.fFields.fLengthAndFlags & kOpenGetBuffer) { @@ -1266,6 +1266,7 @@ UnicodeString::setTo(UBool isTerminated, return *this; } + const UChar *text = textPtr; if(text == NULL) { // treat as an empty string, do not alias releaseArray(); diff --git a/icu4c/source/test/iotest/stream.cpp b/icu4c/source/test/iotest/stream.cpp index 427276efb26..892e0d7d58a 100644 --- a/icu4c/source/test/iotest/stream.cpp +++ b/icu4c/source/test/iotest/stream.cpp @@ -49,6 +49,13 @@ const char C_NEW_LINE[] = {'\n',0}; #endif U_CDECL_END +inline const UChar *constUCharPtr(const char16_t *p) { +#ifdef U_ALIASING_BARRIER + U_ALIASING_BARRIER(p); +#endif + return reinterpret_cast(p); +} + U_CDECL_BEGIN static void U_CALLCONV TestStream(void) { @@ -106,12 +113,12 @@ static void U_CALLCONV TestStream(void) inTestStream >> inStr >> inStr2; if (inStr.compare(thisMu) != 0) { - u_austrncpy(inStrC, inStr.getBuffer(), inStr.length()); + u_austrncpy(inStrC, constUCharPtr(inStr.getBuffer()), inStr.length()); inStrC[inStr.length()] = 0; log_err("Got: \"%s\", Expected: \"tHis\\u03BC\"\n", inStrC); } if (inStr2.compare(mu) != 0) { - u_austrncpy(inStrC, inStr.getBuffer(), inStr.length()); + u_austrncpy(inStrC, constUCharPtr(inStr.getBuffer()), inStr.length()); inStrC[inStr.length()] = 0; log_err("Got: \"%s\", Expected: \"mu\"\n", inStrC); } diff --git a/icu4c/source/tools/ctestfw/datamap.cpp b/icu4c/source/tools/ctestfw/datamap.cpp index ded0c7b609f..d85341ac40c 100644 --- a/icu4c/source/tools/ctestfw/datamap.cpp +++ b/icu4c/source/tools/ctestfw/datamap.cpp @@ -11,6 +11,7 @@ #include "unicode/datamap.h" #include "unicode/resbund.h" #include "hash.h" +#include "toolutil.h" #include DataMap::~DataMap() {} @@ -20,7 +21,7 @@ int32_t DataMap::utoi(const UnicodeString &s) const { char ch[256]; - const UChar *u = s.getBuffer(); + const UChar *u = constUCharPtr(s.getBuffer()); int32_t len = s.length(); u_UCharsToChars(u, ch, len); ch[len] = 0; /* include terminating \0 */ diff --git a/icu4c/source/tools/gennorm2/n2builder.cpp b/icu4c/source/tools/gennorm2/n2builder.cpp index 940db3b13c4..5c7c9c0a016 100644 --- a/icu4c/source/tools/gennorm2/n2builder.cpp +++ b/icu4c/source/tools/gennorm2/n2builder.cpp @@ -282,7 +282,7 @@ uint8_t Normalizer2DataBuilder::getCC(UChar32 c) const { static UBool isWellFormed(const UnicodeString &s) { UErrorCode errorCode=U_ZERO_ERROR; - u_strToUTF8(NULL, 0, NULL, s.getBuffer(), s.length(), &errorCode); + u_strToUTF8(NULL, 0, NULL, constUCharPtr(s.getBuffer()), s.length(), &errorCode); return U_SUCCESS(errorCode) || errorCode==U_BUFFER_OVERFLOW_ERROR; } @@ -315,7 +315,7 @@ void Normalizer2DataBuilder::setRoundTripMapping(UChar32 c, const UnicodeString (int)phase, (long)c); exit(U_INVALID_FORMAT_ERROR); } - int32_t numCP=u_countChar32(m.getBuffer(), m.length()); + int32_t numCP=u_countChar32(constUCharPtr(m.getBuffer()), m.length()); if(numCP!=2) { fprintf(stderr, "error in gennorm2 phase %d: " @@ -452,7 +452,7 @@ Normalizer2DataBuilder::decompose(UChar32 start, UChar32 end, uint32_t value) { Norm &norm=norms[value]; const UnicodeString &m=*norm.mapping; UnicodeString *decomposed=NULL; - const UChar *s=m.getBuffer(); + const UChar *s=constUCharPtr(m.getBuffer()); int32_t length=m.length(); int32_t prev, i=0; UChar32 c; @@ -607,7 +607,7 @@ Normalizer2DataBuilder::reorder(Norm *p, BuilderReorderingBuffer &buffer) { if(length>Normalizer2Impl::MAPPING_LENGTH_MASK) { return; // writeMapping() will complain about it and print the code point. } - const UChar *s=m.getBuffer(); + const UChar *s=constUCharPtr(m.getBuffer()); int32_t i=0; UChar32 c; while(i(p); +} + +inline UChar *UCharPtr(char16_t *p) { +#ifdef U_ALIASING_BARRIER + U_ALIASING_BARRIER(p); +#endif + return reinterpret_cast(p); +} + /** * ErrorCode subclass for use in ICU command-line tools. * The destructor calls handleFailure() which calls exit(errorCode) when isFailure(). diff --git a/icu4c/source/tools/toolutil/xmlparser.cpp b/icu4c/source/tools/toolutil/xmlparser.cpp index 3fbf1a96f3c..baf9a73bc3e 100644 --- a/icu4c/source/tools/toolutil/xmlparser.cpp +++ b/icu4c/source/tools/toolutil/xmlparser.cpp @@ -21,6 +21,7 @@ #include "unicode/ucnv.h" #include "unicode/regex.h" #include "filestrm.h" +#include "toolutil.h" #include "xmlparser.h" #if !UCONFIG_NO_REGULAR_EXPRESSIONS && !UCONFIG_NO_CONVERSION @@ -209,7 +210,7 @@ UXMLParser::parseFile(const char *filename, UErrorCode &errorCode) { goto exit; } - buffer=src.getBuffer(bytesLength); + buffer=UCharPtr(src.getBuffer(bytesLength)); if(buffer==NULL) { // unexpected failure to reserve some string capacity errorCode=U_MEMORY_ALLOCATION_ERROR; @@ -278,7 +279,7 @@ UXMLParser::parseFile(const char *filename, UErrorCode &errorCode) { pb=bytes; for(;;) { length=src.length(); - buffer=src.getBuffer(capacity); + buffer=UCharPtr(src.getBuffer(capacity)); if(buffer==NULL) { // unexpected failure to reserve some string capacity errorCode=U_MEMORY_ALLOCATION_ERROR; -- 2.40.0