Skip to content

Commit c1f7e9c

Browse files
committed
ICU-23254 Remove C++ static initialization
1 parent 9407fad commit c1f7e9c

26 files changed

+602
-617
lines changed

.github/workflows/icu4c.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -373,12 +373,12 @@ jobs:
373373
374374
# MacOS with clang
375375
macos-clang:
376-
runs-on: macos-14 # Updated in BRS
376+
runs-on: macos-15 # Updated in BRS
377377
steps:
378378
- uses: actions/checkout@ff7abcd0c3c05ccf6adc123a8cd1fd4fb30fb493 # v4.1.1
379379
- name: ICU4C with clang on MacOS
380380
env:
381-
CPPFLAGS: '-Wall -Wextra -Wextra-semi -Wundef -Wnon-virtual-dtor -Wctad-maybe-unsupported -Werror'
381+
CPPFLAGS: '-Wall -Wextra -Wextra-semi -Wundef -Wnon-virtual-dtor -Wctad-maybe-unsupported -Wglobal-constructors -Wexit-time-destructors -Werror'
382382
run: |
383383
cd icu4c/source;
384384
PYTHON=python3 ./runConfigureICU macOS;

.gitignore

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -201,6 +201,40 @@ icu4c/source/samples/ustring/ustring.exe
201201
icu4c/source/stubdata/stubdatabuilt.txt
202202
icu4c/source/test/cintltst/cintltst
203203
icu4c/source/test/cintltst/cintltst.exe
204+
icu4c/source/test/fuzzer/break_iterator_fuzzer
205+
icu4c/source/test/fuzzer/calendar_fuzzer
206+
icu4c/source/test/fuzzer/collator_compare_fuzzer
207+
icu4c/source/test/fuzzer/collator_rulebased_fuzzer
208+
icu4c/source/test/fuzzer/converter_fuzzer
209+
icu4c/source/test/fuzzer/date_format_fuzzer
210+
icu4c/source/test/fuzzer/date_time_pattern_generator_fuzzer
211+
icu4c/source/test/fuzzer/decimal_format_symbols_fuzzer
212+
icu4c/source/test/fuzzer/dtfmtsym_fuzzer
213+
icu4c/source/test/fuzzer/dummytestcase
214+
icu4c/source/test/fuzzer/list_format_fuzzer
215+
icu4c/source/test/fuzzer/locale_fuzzer
216+
icu4c/source/test/fuzzer/locale_morph_fuzzer
217+
icu4c/source/test/fuzzer/message_formatter_fuzzer
218+
icu4c/source/test/fuzzer/normalizer2_fuzzer
219+
icu4c/source/test/fuzzer/number_format_fuzzer
220+
icu4c/source/test/fuzzer/number_formatter_fuzzer
221+
icu4c/source/test/fuzzer/plurrule_fuzzer
222+
icu4c/source/test/fuzzer/relative_date_time_formatter_fuzzer
223+
icu4c/source/test/fuzzer/rule_based_break_iterator_fuzzer
224+
icu4c/source/test/fuzzer/time_zone_names_fuzzer
225+
icu4c/source/test/fuzzer/timezone_create_fuzzer
226+
icu4c/source/test/fuzzer/ubidi_fuzzer
227+
icu4c/source/test/fuzzer/ucasemap_fuzzer
228+
icu4c/source/test/fuzzer/uloc_canonicalize_fuzzer
229+
icu4c/source/test/fuzzer/uloc_for_language_tag_fuzzer
230+
icu4c/source/test/fuzzer/uloc_get_name_fuzzer
231+
icu4c/source/test/fuzzer/uloc_is_right_to_left_fuzzer
232+
icu4c/source/test/fuzzer/uloc_open_keywords_fuzzer
233+
icu4c/source/test/fuzzer/unicode_string_codepage_create_fuzzer
234+
icu4c/source/test/fuzzer/unicodeset_fuzzer
235+
icu4c/source/test/fuzzer/uprop_fuzzer
236+
icu4c/source/test/fuzzer/uregex_match_fuzzer
237+
icu4c/source/test/fuzzer/uregex_open_fuzzer
204238
icu4c/source/test/intltest/intltest
205239
icu4c/source/test/intltest/intltest.exe
206240
icu4c/source/test/iotest/iotest

icu4c/source/common/loclikely.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -418,7 +418,7 @@ GetRegionFromKey(const char* localeID, std::string_view key, UErrorCode& status)
418418
if (U_SUCCESS(status) && len >= 3 && len <= 6 &&
419419
uprv_isASCIILetter(kw[0]) && uprv_isASCIILetter(kw[1])) {
420420
// Additional Check
421-
static icu::RegionValidateMap valid;
421+
icu::RegionValidateMap valid;
422422
const char region[] = {kw[0], kw[1], '\0'};
423423
if (valid.isSet(region)) {
424424
result.append(uprv_toupper(kw[0]), status);

icu4c/source/extra/scrptrun/scrptrun.cpp

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ U_NAMESPACE_BEGIN
2323

2424
const char ScriptRun::fgClassID=0;
2525

26-
UChar32 ScriptRun::pairedChars[] = {
26+
static const UChar32 pairedChars[] = {
2727
0x0028, 0x0029, // ascii paired punctuation
2828
0x003c, 0x003e,
2929
0x005b, 0x005d,
@@ -43,11 +43,9 @@ UChar32 ScriptRun::pairedChars[] = {
4343
0x301a, 0x301b
4444
};
4545

46-
const int32_t ScriptRun::pairedCharCount = UPRV_LENGTHOF(pairedChars);
47-
const int32_t ScriptRun::pairedCharPower = 1 << highBit(pairedCharCount);
48-
const int32_t ScriptRun::pairedCharExtra = pairedCharCount - pairedCharPower;
46+
static constexpr int32_t pairedCharCount = UPRV_LENGTHOF(pairedChars);
4947

50-
int8_t ScriptRun::highBit(int32_t value)
48+
static int8_t highBit(int32_t value)
5149
{
5250
if (value <= 0) {
5351
return -32;
@@ -83,10 +81,16 @@ int8_t ScriptRun::highBit(int32_t value)
8381
return bit;
8482
}
8583

86-
int32_t ScriptRun::getPairIndex(UChar32 ch)
84+
static int32_t getPairedCharPower() {
85+
static const int32_t pairedCharPower = 1 << highBit(pairedCharCount);
86+
return pairedCharPower;
87+
}
88+
89+
static int32_t getPairIndex(UChar32 ch)
8790
{
88-
int32_t probe = pairedCharPower;
91+
int32_t probe = getPairedCharPower();
8992
int32_t index = 0;
93+
int32_t pairedCharExtra = pairedCharCount - probe;
9094

9195
if (ch >= pairedChars[pairedCharExtra]) {
9296
index = pairedCharExtra;

icu4c/source/extra/scrptrun/scrptrun.h

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -86,14 +86,6 @@ class ScriptRun : public UObject {
8686
ParenStackEntry parenStack[128];
8787
int32_t parenSP;
8888

89-
static int8_t highBit(int32_t value);
90-
static int32_t getPairIndex(UChar32 ch);
91-
92-
static UChar32 pairedChars[];
93-
static const int32_t pairedCharCount;
94-
static const int32_t pairedCharPower;
95-
static const int32_t pairedCharExtra;
96-
9789
/**
9890
* The address of this static class variable serves as this class's ID
9991
* for ICU "poor man's RTTI".

icu4c/source/i18n/dtptngen.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -369,6 +369,7 @@ DateTimePatternGenerator::DateTimePatternGenerator(const DateTimePatternGenerato
369369
UObject(),
370370
skipMatcher(nullptr),
371371
fAvailableFormatKeyHash(nullptr),
372+
emptyString(u"", 0),
372373
fDefaultHourFormatChar(0),
373374
internalErrorCode(U_ZERO_ERROR)
374375
{
@@ -1406,7 +1407,6 @@ DateTimePatternGenerator::setDateTimeFormat(UDateFormatStyle style, const Unicod
14061407

14071408
const UnicodeString&
14081409
DateTimePatternGenerator::getDateTimeFormat(UDateFormatStyle style, UErrorCode& status) const {
1409-
static const UnicodeString emptyString = UNICODE_STRING_SIMPLE("");
14101410
if (U_FAILURE(status)) {
14111411
return emptyString;
14121412
}

icu4c/source/i18n/smpdtfmt.cpp

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1483,8 +1483,7 @@ SimpleDateFormat::subFormat(UnicodeString &appendTo,
14831483
Calendar& cal,
14841484
UErrorCode& status) const
14851485
{
1486-
static const int32_t maxIntCount = 10;
1487-
static const UnicodeString hebr(u"hebr");
1486+
static constexpr int32_t maxIntCount = 10;
14881487

14891488
if (U_FAILURE(status)) {
14901489
return;
@@ -1565,7 +1564,7 @@ SimpleDateFormat::subFormat(UnicodeString &appendTo,
15651564
//AD 12345 12345 45 12345 12345 12345
15661565
case UDAT_YEAR_FIELD:
15671566
case UDAT_YEAR_WOY_FIELD:
1568-
if (fDateOverride.compare(hebr)==0 && value>HEBREW_CAL_CUR_MILLENIUM_START_YEAR && value<HEBREW_CAL_CUR_MILLENIUM_END_YEAR) {
1567+
if (fDateOverride.compare(UnicodeString(u"hebr", 4))==0 && value>HEBREW_CAL_CUR_MILLENIUM_START_YEAR && value<HEBREW_CAL_CUR_MILLENIUM_END_YEAR) {
15691568
value-=HEBREW_CAL_CUR_MILLENIUM_START_YEAR;
15701569
}
15711570
if(count == 2)
@@ -3055,7 +3054,6 @@ int32_t SimpleDateFormat::subParse(const UnicodeString& text, int32_t& start, ch
30553054
return -start;
30563055
}
30573056
UCalendarDateFields field = fgPatternIndexToCalendarField[patternCharIndex]; // UCAL_FIELD_COUNT if irrelevant
3058-
UnicodeString hebr("hebr", 4, US_INV);
30593057

30603058
if (numericLeapMonthFormatter != nullptr) {
30613059
numericLeapMonthFormatter->setFormats(reinterpret_cast<const Format**>(&currentNumberFormat), 1);
@@ -3223,7 +3221,7 @@ int32_t SimpleDateFormat::subParse(const UnicodeString& text, int32_t& start, ch
32233221
// we made adjustments to place the 2-digit year in the proper
32243222
// century, for parsed strings from "00" to "99". Any other string
32253223
// is treated literally: "2250", "-1", "1", "002".
3226-
if (fDateOverride.compare(hebr)==0 && value < 1000) {
3224+
if (fDateOverride.compare(UnicodeString(u"hebr", 4))==0 && value < 1000) {
32273225
value += HEBREW_CAL_CUR_MILLENIUM_START_YEAR;
32283226
} else if (text.moveIndex32(start, 2) == pos.getIndex() && !isChineseCalendar
32293227
&& u_isdigit(text.char32At(start))
@@ -3263,7 +3261,7 @@ int32_t SimpleDateFormat::subParse(const UnicodeString& text, int32_t& start, ch
32633261

32643262
case UDAT_YEAR_WOY_FIELD:
32653263
// Comment is the same as for UDAT_Year_FIELDs - look above
3266-
if (fDateOverride.compare(hebr)==0 && value < 1000) {
3264+
if (fDateOverride.compare(UnicodeString(u"hebr", 4))==0 && value < 1000) {
32673265
value += HEBREW_CAL_CUR_MILLENIUM_START_YEAR;
32683266
} else if (text.moveIndex32(start, 2) == pos.getIndex()
32693267
&& u_isdigit(text.char32At(start))

icu4c/source/i18n/transreg.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -542,6 +542,7 @@ TransliteratorRegistry::TransliteratorRegistry(UErrorCode& status) :
542542
variantList.adoptElement(emptyString, status);
543543
}
544544
specDAG.setValueDeleter(uhash_deleteHashtable);
545+
fBogus.setToBogus();
545546
}
546547

547548
TransliteratorRegistry::~TransliteratorRegistry() {
@@ -756,8 +757,7 @@ const UnicodeString& TransliteratorRegistry::getAvailableID(int32_t index) const
756757

757758
// If the code reaches here, the hash table was likely modified during iteration.
758759
// Return an statically initialized empty string due to reference return type.
759-
static UnicodeString empty;
760-
return empty;
760+
return fBogus;
761761
}
762762

763763
StringEnumeration* TransliteratorRegistry::getAvailableIDs() const {

icu4c/source/i18n/transreg.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -455,6 +455,8 @@ class TransliteratorRegistry : public UMemory {
455455
*/
456456
Hashtable availableIDs;
457457

458+
UnicodeString fBogus;
459+
458460
TransliteratorRegistry(const TransliteratorRegistry &other); // forbid copying of this class
459461
TransliteratorRegistry &operator=(const TransliteratorRegistry &other); // forbid copying of this class
460462
};

0 commit comments

Comments
 (0)