This is an archive of the discontinued LLVM Phabricator instance.

[libc++] fix macro redef warning when exception is disabled
ClosedPublic

Authored by weimingz on Apr 20 2016, 5:35 PM.

Diff Detail

Repository
rL LLVM

Event Timeline

weimingz updated this revision to Diff 54444.Apr 20 2016, 5:35 PM
weimingz retitled this revision from to [libc++] fix constexpr error when build with MUSL and macro redef warning when no exception.
weimingz updated this object.
  1. when setting LIBCXX_ENABLE_EXCEPTIONS=false, CMakeLists.txt passes both -fno-exception and and -D_LIBCPP_NO_EXCEPTIONS

In _config, it is defined again due to fno-exception

  1. MUSL defines
typedef struct { union { int __i[6]; volatile int __vi[6]; volatile void *volatile __p[6]; } __u; } pthread_mutex_t;

and

#define PTHREAD_MUTEX_INITIALIZER {{{0}}}

The PTHREAD_MUTEX_INITIALIZER is wrong because it's partially initialized. However, even I changed it to

{{{0, 0, 0, 0, 0, 0 }}}

clang still diagnose:
"error: constexpr constructor never produces a constant expression"

If I pass -std=c++14, it is fine.

weimingz added a subscriber: cfe-commits.
EricWF requested changes to this revision.Apr 20 2016, 5:54 PM
EricWF edited edge metadata.
EricWF added inline comments.
include/__mutex_base
43 ↗(On Diff #54444)

This is not OK. It's critical that mutex have a constexpr constructor that it runs during the "Constant initialization" phase of static initialization.
Heres an example of the difference this makes: https://godbolt.org/g/3cvlMJ

Also the constructor is specified as being constexpr in C++11. We can't turn that off.

If one particular pthread implementation is broken then we need a fix targeted to only that implementation. However this seems like a pthread bug and not a libc++ bug.

282 ↗(On Diff #54444)

Same as above.

This revision now requires changes to proceed.Apr 20 2016, 5:54 PM
weimingz added inline comments.Apr 20 2016, 9:09 PM
include/__config
300 ↗(On Diff #54444)

Is this change OK?

include/__mutex_base
43 ↗(On Diff #54444)

The macro has an "#else" part. I'm not familiar with this, but it seems the constexpr an "optional feature".

EricWF added inline comments.Apr 20 2016, 9:18 PM
include/__config
300 ↗(On Diff #54444)

Yes.

include/__mutex_base
43 ↗(On Diff #54444)

So _LIBCPP_HAS_NO_CONSTEXPR checks for the presence of C++11 constexpr semantics. In C++14 support for constexpr was greatly increased, allowing many more expressions to be considered "constant expressions". The original macro, and _LIBCPP_HAS_NO_CXX14_CONSTXPR check if a compiler has completely implemented the C++11 or C++14 constexpr requirements respectively. In your case PTHREAD_MUTEX_INITIALIZER is defined in such a way that it requires the C++14 definition of constexpr.

weimingz updated this revision to Diff 54457.Apr 20 2016, 10:07 PM
weimingz retitled this revision from [libc++] fix constexpr error when build with MUSL and macro redef warning when no exception to [libc++] fix macro redef warning when exception is disabled.
weimingz updated this object.
weimingz edited edge metadata.
EricWF accepted this revision.Apr 20 2016, 10:11 PM
EricWF edited edge metadata.

LGTM.

Normally system header would simply ignore this problem. However it's valuable to compile libc++ as a non-system header for both users and developers.

Than you for the patch.

This revision is now accepted and ready to land.Apr 20 2016, 10:11 PM

s/than you/thank you

This revision was automatically updated to reflect the committed changes.