martinmoene / lest

A modern, C++11-native, single-file header-only, tiny framework for unit-tests, TDD and BDD (includes C++98 variant)
Boost Software License 1.0
390 stars 45 forks source link

clang: warning about operator<< precedence #27

Open genbattle opened 8 years ago

genbattle commented 8 years ago

Compiling tests that use lest under clang++ (3.7) yields warnings for each EXPECT such as the following:

/home/nick/Projects/km/src/test/test.cpp:67:62: error: overloaded operator <<
      has higher precedence than comparison operator
      [-Werror,-Woverloaded-shift-op-parentheses]
  ...EXPECT(std::count(clusters.cbegin(), clusters.cend(), 3) == 0);
                                                              ^  ~
/home/nick/Projects/km/src/test/lest.hpp:132:55: note: expanded from macro
      'lest_EXPECT'
            if ( lest::result score = lest_DECOMPOSE( expr ) ) \
                                                      ^
/home/nick/Projects/km/src/test/lest.hpp:215:67: note: expanded from macro
      'lest_DECOMPOSE'
#define lest_DECOMPOSE( expr ) ( lest::expression_decomposer() << expr )

The warning is talking about this line in lest.hpp:

#define lest_DECOMPOSE( expr ) ( lest::expression_decomposer() << expr )

Adding extra parenthesis prevents this warning, and a potentially catastrophic evaluation order mess:

#define lest_DECOMPOSE( expr ) ( lest::expression_decomposer() << ( expr ) )
martinmoene commented 8 years ago

...and defeats the expression decomposition...

See also:

I'm following this discussion and see what comes out of it.

I might try to change lest_DECOMPOSE() to use:

_Pragma("clang diagnostic push") \
_Pragma("clang diagnostic ignored \"-Woverloaded-shift-op-parentheses\"") \
( lest::expression_decomposer() << expr ) \
_Pragma("clang diagnostic pop") \
genbattle commented 8 years ago

Ah, it appears I didn't fully understand the intention of the code. Ignoring the warning may be a better solution in this case if the behaviour is intended.

martinmoene commented 8 years ago

As an aside:

For some time lest does contain:

#ifdef __clang__
...
# pragma clang diagnostic ignored "-Woverloaded-shift-op-parentheses"
...
martinmoene commented 8 years ago

Keeping this open to remind myself about this ongoing discussion with Catch.