lest icon indicating copy to clipboard operation
lest copied to clipboard

clang: warning about operator<< precedence

Open genbattle opened this issue 10 years ago • 4 comments

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 ) )

genbattle avatar Jan 06 '16 08:01 genbattle

...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") \

martinmoene avatar Jan 06 '16 09:01 martinmoene

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.

genbattle avatar Jan 06 '16 09:01 genbattle

As an aside:

For some time lest does contain:

#ifdef __clang__
...
# pragma clang diagnostic ignored "-Woverloaded-shift-op-parentheses"
...

martinmoene avatar Jan 06 '16 10:01 martinmoene

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

martinmoene avatar Jan 07 '16 06:01 martinmoene