abelx / googletest

Automatically exported from code.google.com/p/googletest
BSD 3-Clause "New" or "Revised" License
0 stars 0 forks source link

Attempt to fix GTEST_DEFINE_STATIC_MUTEX_ missing-field-initializers gcc warning #433

Open GoogleCodeExporter opened 9 years ago

GoogleCodeExporter commented 9 years ago
Currently, GTEST_DEFINE_STATIC_MUTEX_ is implemented as follows:

// Defines and statically (i.e. at link time) initializes a static mutex.
// The initialization list here does not explicitly initialize each field,
// instead relying on default initialization for the unspecified fields. In
// particular, the owner_ field (a pthread_t) is not explicitly initialized.
// This allows initialization to work whether pthread_t is a scalar or struct.
// The flag -Wmissing-field-initializers must not be specified for this to work.
# define GTEST_DEFINE_STATIC_MUTEX_(mutex) \
    ::testing::internal::MutexBase mutex = { PTHREAD_MUTEX_INITIALIZER, false }

1) I suggest to change the comment "The flag -Wmissing-field-initializers must 
not be specified for this to work." to "Use -Wno-missing-field-initializers to 
make GCC silent." since "-Wmissing-field-initializers" does not prevent gtest 
from successful build in any way (as far as -Werror is not supplied).
2) I think we should change the code the following way:
# define GTEST_DEFINE_STATIC_MUTEX_(mutex) \
    ::testing::internal::MutexBase mutex = { PTHREAD_MUTEX_INITIALIZER, false, pthread_t() }

Thus we are using the default constructor. It works either with scalar or 
struct types and fixes the GCC warning.

Original issue reported on code.google.com by gmark...@gmail.com on 13 May 2013 at 12:43

GoogleCodeExporter commented 9 years ago
Is there a reason why suggestion 2) could not be implemented?

Disabling warnings is a bad idea in my opinion.

Original comment by daCh...@gmail.com on 10 Apr 2014 at 8:27

GoogleCodeExporter commented 9 years ago
If pthread_t() does not work in all cases then I see no harm of using 
pthread_self() instead. That should provide valid value for pthread_t. Initial 
value doesn't really have a meaning in here as there is boolean has_owner_ that 
defines whether this value is valid or not.

Original comment by daCh...@gmail.com on 10 Apr 2014 at 8:41

GoogleCodeExporter commented 9 years ago
2) seems to work for me on linux and windows, thanks.

Adding -Wmissing-field-initializers is hard to do surgically, I'd rather not 
turn off any warnings.

Original comment by daniel.r...@gmail.com on 8 Oct 2014 at 10:33

GoogleCodeExporter commented 9 years ago
2) worked for me on OSX 10.9, Xcode 6.1.1

Original comment by ketilwri...@gmail.com on 11 Dec 2014 at 10:05