martinmoene / optional-lite

optional lite - A C++17-like optional, a nullable object for C++98, C++11 and later in a single-file header-only library
Boost Software License 1.0
403 stars 45 forks source link

Fix errors detected by clang with -Werror #13

Closed eao197 closed 6 years ago

eao197 commented 6 years ago

I tried to use optional-lite in our project and receive some errors when compiling by clang with -Werror flag. I've tested it on FreeBSD with clang 3.4.1 and on Win10 with clang 6.0.0.

This PR contains fixes for errors detected by clang.

martinmoene commented 6 years ago

You may have noticed I'm in the process of updating expected-lite to the latest revision of the proposal.

I gather the head already contains like changes as this PR.

martinmoene commented 6 years ago

Ah, this is optional (Working from a phone).

Then, expected-lite is likely a good example of how I'd approach it.

eao197 commented 6 years ago

I think I will change the code with _MSVC_LANG macro like in expected-lite and update the PR.

But there are other warnings which are treated as errors with -Werror.

martinmoene commented 6 years ago

I've fixed the C++ language detection in the head.

I need more time to look into the other issues (#if defined(...) vs #if ...) and decide what to do about it.

martinmoene commented 6 years ago

Hi @eao197,

Can you:

Thanks in advance

eao197 commented 6 years ago

When compiling with clang-6 (vc++ 15.6.1) on Windows 10:

$ ruby so_5/prj.rb
Compiling so_5/msg_tracing.cpp ...
In file included from so_5/msg_tracing.cpp:5:
In file included from .\so_5/h/msg_tracing.hpp:19:
In file included from .\so_5/h/optional.hpp:15:
.\so_5/3rd_party/optional-lite/nonstd/optional.hpp:264:7: error: 'nonstd_lite_HAVE_IN_PLACE_TYPES' is not defined, evaluates to 0 [-Werror,-Wundef]
#if ! nonstd_lite_HAVE_IN_PLACE_TYPES
      ^
1 error generated.
<<<[MxxRu::BuildEx]     Build error: 'clang++ -c -o target/_objs/clang_6_0_0__x86_64_pc_windows_msvc/release/so_5_prj_rb/so_5/msg_tracing.o -Werror -Wpedantic -Weverything -Wno-c++98-compat -Wno-c++98-compat-pedantic -Wno-padded -Wno-missing-noreturn -Wno-documentation-unknown-command -Wno-documentation-deprecated-sync -Wno-documentation -Wno-weak-vtables -Wno-missing-prototypes -Wno-missing-variable-declarations -Wno-exit-time-destructors -Wno-global-constructors -O2 -DSO_5_PRJ -DSO_5__PLATFORM_REQUIRES_CDECL -DNDEBUG -I. -std=c++14 so_5/msg_tracing.cpp' returns 'pid 13792 exit 1'>>>

I copied the current optional.hpp into source tree of my project and compiled my project with our build-tool. All compiler flags shown above.

eao197 commented 6 years ago

And for clang-3.4.1 on FreeBSD 10.3:

Compiling so_5/msg_tracing.cpp ...
In file included from so_5/msg_tracing.cpp:5:
In file included from ./so_5/h/msg_tracing.hpp:19:
In file included from ./so_5/h/optional.hpp:15:
./so_5/3rd_party/optional-lite/nonstd/optional.hpp:205:5: error: 'optional_HAVE_CONSTEXPR_14' is not defined, evaluates
      to 0 [-Werror,-Wundef]
#if optional_HAVE_CONSTEXPR_14
    ^
./so_5/3rd_party/optional-lite/nonstd/optional.hpp:264:7: error: 'nonstd_lite_HAVE_IN_PLACE_TYPES' is not defined,
      evaluates to 0 [-Werror,-Wundef]
#if ! nonstd_lite_HAVE_IN_PLACE_TYPES
      ^
2 errors generated.
<<<[MxxRu::BuildEx]     Build error: 'clang++ -c -o target/_objs/clang_3_4_1__x86_64_unknown_freebsd10_3/release/so_5_prj_rb/so_5/msg_tracing.o -Werror -Wpedantic -Weverything -Wno-c++98-compat -Wno-c++98-compat-pedantic -Wno-padded -Wno-missing-noreturn -Wno-documentation-unknown-command -Wno-documentation-deprecated-sync -Wno-documentation -Wno-weak-vtables -Wno-missing-prototypes -Wno-missing-variable-declarations -Wno-exit-time-destructors -Wno-global-constructors -O2 -fPIC -DSO_5_PRJ -DNDEBUG -I. -std=c++11 so_5/msg_tracing.cpp' returns 'pid 739 exit 1'>>>
martinmoene commented 6 years ago

Note to self.

Support using in itself correct #if undefined_macro with -Werror -Wundef.

  1. Via #if optional_HAVE(feature) with: #define optional_HAVE(FEATURE) ( defined optional_HAVE_##FEATURE && optional_HAVE_##FEATURE ). This is using undefined behaviour: warning: macro expansion producing 'defined' has undefined behavior [-Wexpansion-to-defined].

  2. Via #if optional_HAVE(feature) with: #define optional_HAVE(FEATURE) ( !!optional_HAVE_##FEATURE ).

  3. Via suppression of warning via #pragma or _pragma.

  4. ?

~~For now I've opted for variant 2. #define optional_HAVE(FEATURE) ( !!optional_HAVE_##FEATURE ). Let's see how long it takes for clang to also flag this with -Wundef.~~ (That doesn't help)

Using:

martinmoene commented 6 years ago

@eao197 I assume these are solved since 55aea6960c8bb39570f47a262aa142053b361cb2.

eao197 commented 6 years ago

Thanks! Everything seems to be working fine with the latest version.

On Fri, Sep 14, 2018 at 11:23 AM Martin Moene notifications@github.com wrote:

@eao197 https://github.com/eao197 I assume these are solved since 55aea69 https://github.com/martinmoene/optional-lite/commit/55aea6960c8bb39570f47a262aa142053b361cb2 .

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/martinmoene/optional-lite/pull/13#issuecomment-421270925, or mute the thread https://github.com/notifications/unsubscribe-auth/ALSczbOWJ-K-FhK2aEKzVAD6vLhZDt8hks5ua2eYgaJpZM4S80XU .

-- Regards, Yauheni Akhotnikau