[PATCH] D39149: [libc++] Prevent tautological comparisons

Shoaib Meenai via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Mon Oct 30 13:39:59 PDT 2017


smeenai added a comment.

In https://reviews.llvm.org/D39149#911024, @rsmith wrote:

> In https://reviews.llvm.org/D39149#910936, @smeenai wrote:
>
> > I'm thinking you could account for all possible type sizes in the existing (enabled by default) warning, and have a different warning for possibly tautological comparisons. E.g. if a `long` is being compared against `INT_MAX`, you know that's only tautological on some platforms, so it should go under `-Wpossible-tautological-constant-compare` (which would only be enabled by `-Weverything` and not `-Wall` or `-Wextra`); `-Wtautological-constant-compare` should be reserved for definitely tautological cases.
>
>
> This sounds like a very promising direction. (That is: if the types of the values being compared are different, but are of the same size, then suppress the warning or move it to a different warning group that's not part of `-Wtautological-compare`.) That would also suppress warnings for cases like 'ch > CHAR_MAX', when `ch` is a `char`, but we could detect and re-enable the warning for such cases by, say, always warning if the constant side is an integer literal.


Yeah, the type comparison implementation was what I was thinking of originally, though I wasn't sure about the edge cases.

When you say "always warning if the constant side is an integer literal", do you mean if it's a straight-up integer literal, or if it's an expression which evaluates to an integer literal at compile time? For example, would there be a difference if you were comparing to `numeric_limits<int>::max()` vs. `INT_MAX`?


https://reviews.llvm.org/D39149





More information about the cfe-commits mailing list