This is the mail archive of the
mailing list for the GCC project.
Re: [C, C++] Implement -Wstatic-local
- From: Gabriel Dos Reis <gdr at integrable-solutions dot net>
- To: Florian Weimer <fweimer at redhat dot com>
- Cc: Andrew Pinski <pinskia at gmail dot com>, GCC Patches <gcc-patches at gcc dot gnu dot org>
- Date: Tue, 23 Jul 2013 20:24:07 -0500
- Subject: Re: [C, C++] Implement -Wstatic-local
- References: <51EEDDF2 dot 9010001 at redhat dot com> <CA+=Sn1==BB4Af_wL118-2dS_ZApi2VhziJReMr4aTg7WzotRog at mail dot gmail dot com> <51EEE14F dot 7010103 at redhat dot com>
On Tue, Jul 23, 2013 at 3:02 PM, Florian Weimer <email@example.com> wrote:
> On 07/23/2013 09:51 PM, Andrew Pinski wrote:
>> On Tue, Jul 23, 2013 at 12:48 PM, Florian Weimer <firstname.lastname@example.org>
>>> We sometimes deal with code bases which use static local variables to cut
>>> down frame size, for compatibility with legacy targets. Obviously, this
>>> bad for thread safety. This new warning can be used to track down such
>>> cases once you suspect they exist.
>> Hmm, since you mentioned bad for thread safety but then I see in your
>> patch you don't check to see if the variable is a thread local
>> variable. Maybe you should not mention thread safety at all here or
>> change the code not to include TLS variables.
> Good point. What about this instead?
> Warn for variables declared inside functions which are static, but not
> declared const. Such local variables can make functions not reentrant.
Do you envision this to be useful for C++ too?
There are many bits here. 'const' in C++ does not necessary prevent
race conditions with the variable's type has to run non-atomic constructors,
or if the object has data members declared mutable, etc.
It used to be idiomatic in C++ to use local statics in order to enforce
initialization of "morally global" objects -- the function returns a pointer
or reference to the object.
I think the C++ ABI mandates that the implementation adds implicit locks
to enforce orderly initialization