Skip to content

Commit

Permalink
ICU-22954 Workaround: Replace std::u16string member with UnicodeString&.
Browse files Browse the repository at this point in the history
  • Loading branch information
roubert committed Jan 7, 2025
1 parent b2a47f9 commit df0422e
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 8 deletions.
12 changes: 9 additions & 3 deletions icu4c/source/tools/ctestfw/tstdtmod.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,18 +11,25 @@
#include <stdarg.h>

#include "unicode/tstdtmod.h"
#include "unicode/unistr.h"
#include "cmemory.h"
#include <stdio.h>
#include "cstr.h"
#include "cstring.h"

TestLog::~TestLog() {}

IcuTestErrorCode::IcuTestErrorCode(TestLog &callingTestClass, const char *callingTestName)
: errorCode(U_ZERO_ERROR),
testClass(callingTestClass), testName(callingTestName), scopeMessage(*new UnicodeString) {
}

IcuTestErrorCode::~IcuTestErrorCode() {
// Safe because our errlog() does not throw exceptions.
if(isFailure()) {
errlog(false, u"destructor: expected success", nullptr);
}
delete &scopeMessage;
}

UErrorCode IcuTestErrorCode::reset() {
Expand Down Expand Up @@ -119,8 +126,7 @@ UBool IcuTestErrorCode::expectErrorAndReset(UErrorCode expectedError, const char
}

void IcuTestErrorCode::setScope(const char* message) {
UnicodeString us(message, -1, US_INV);
scopeMessage = us;
scopeMessage.remove().append({ message, -1, US_INV });
}

void IcuTestErrorCode::setScope(std::u16string_view message) {
Expand All @@ -136,7 +142,7 @@ void IcuTestErrorCode::errlog(UBool dataErr, std::u16string_view mainMessage, co
msg.append(u' ').append(mainMessage);
msg.append(u" but got error: ").append(UnicodeString(errorName(), -1, US_INV));

if (!scopeMessage.empty()) {
if (!scopeMessage.isEmpty()) {
msg.append(u" scope: ").append(scopeMessage);
}

Expand Down
18 changes: 13 additions & 5 deletions icu4c/source/tools/ctestfw/unicode/testlog.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
#ifndef U_TESTFW_TESTLOG
#define U_TESTFW_TESTLOG

#include <string>
#include <string_view>
#include "unicode/utypes.h"
#include "unicode/testtype.h"
Expand All @@ -35,14 +34,16 @@ class T_CTEST_EXPORT_API TestLog {
// unit tests that work without U_SHOW_CPLUSPLUS_API.
// So instead we *copy* the ErrorCode API.

U_NAMESPACE_BEGIN
class UnicodeString;
U_NAMESPACE_END

class T_CTEST_EXPORT_API IcuTestErrorCode {
public:
IcuTestErrorCode(const IcuTestErrorCode&) = delete;
IcuTestErrorCode& operator=(const IcuTestErrorCode&) = delete;

IcuTestErrorCode(TestLog &callingTestClass, const char *callingTestName)
: errorCode(U_ZERO_ERROR),
testClass(callingTestClass), testName(callingTestName), scopeMessage() {}
IcuTestErrorCode(TestLog &callingTestClass, const char *callingTestName);
virtual ~IcuTestErrorCode();

// ErrorCode API
Expand Down Expand Up @@ -75,7 +76,14 @@ class T_CTEST_EXPORT_API IcuTestErrorCode {
UErrorCode errorCode;
TestLog &testClass;
const char *const testName;
std::u16string scopeMessage;

// It's not possible to use a UnicodeString member directly here because
// that won't work without U_SHOW_CPLUSPLUS_API, but it's also not possible
// to use a std::u16string member because for unknown reasons that leads to
// a crash in the icu4c-windows-cygwin-gcc CI job. As a workaround, the
// UnicodeString class is forward declared to make it possible to use a
// reference here and then heap allocate the object in the constructor.
UnicodeString& scopeMessage;

void errlog(UBool dataErr, std::u16string_view mainMessage, const char* extraMessage) const;
};
Expand Down

0 comments on commit df0422e

Please sign in to comment.