GeneralPurpose-StaticInitializationOrderFiascoΒΆ
Initialization of global objects should not depend on other translation units
Required inputs: IR
Bad code (dependent initialization):
// file1.cpp
extern int global_value; // from file2.cpp
int initialized = global_value * 10; // ERROR: depends on file2 initialization order
// file2.cpp
int get_global_value() { return 42; }
int global_value = get_global_value(); // ERROR: may be initialized after file1.cpp uses it
Good code (independent initialization):
// file1.cpp int initialized = 100; // OK: no external dependencies // file2.cpp int global_value = 42; // OK: independent
Good code (lazy initialization):
// header.h
int& get_global_value();
// file.cpp
int& get_global_value() {
static int value = 42; // OK: initialized on first call
return value;
}
// usage
int initialized = get_global_value() * 10; // OK: guaranteed proper order
Possible Messages
Key |
Text |
Severity |
Disabled |
|---|---|---|---|
extern_dependency |
Initialization of static object depends on other translation unit |
None |
False |
Options
This rule shares the following common options: exclude_in_macros, exclude_messages_in_system_headers, excludes, extend_exclude_to_macro_invocations, includes, justification_checker, languages, post_processing, provider, report_at, severity
The following places define options that affect this rule: Stylechecks, Analysis-GlobalOptions
This rule has no individual options.