Warn on use of static objects in ICU4C library code (-Wglobal-constructors)

Description

Add the clang compiler option -Wglobal-constructors to the build of ICU4C library code. Generates warnings on the use of global or static objects with constructors.

Do not add the warning to tools or tests, where static objects are ok.

Activity

Show:
Andy Heninger
July 19, 2019, 11:56 PM
Edited

To test, add a static object declaration anywhere in the library code, e.g.,

$ git diff
diff --git a/icu4c/source/common/cstr.cpp b/icu4c/source/common/cstr.cpp
index 24654f8fc2..3f8dd60dce 100644
--- a/icu4c/source/common/cstr.cpp
+++ b/icu4c/source/common/cstr.cpp
@@ -18,6 +18,8 @@

U_NAMESPACE_BEGIN

+static UnicodeString gString;
+
CStr::CStr(const UnicodeString &in) {
UErrorCode status = U_ZERO_ERROR;
#if !UCONFIG_NO_CONVERSION || U_CHARSET_IS_UTF8

 

Building this gives

 

make[1]: Entering directory '/usr/local/google/home/aheninger/icu/icu4c/source/common'
(deps) cstr.cpp
clang++ ... cstr.cpp
cstr.cpp:21:23: warning: declaration requires a global destructor [-Wglobal-constructors]
static UnicodeString gString;
^
1 warning generated.

 

The tricky bit is that the warning only wants to be enabled for library code.
Static objects are allowed in the Tools and Tests, and appear frequently.

 

Fixed
Your pinned fields
Click on the next to a field label to start pinning.

Assignee

Steven R. Loomis

Reporter

Andy Heninger

Components

Reviewer

Steven R. Loomis

Priority

medium

Time Needed

Hours

Fix versions