[cfe-dev] Getting better diagnostics from clang in portable code

Richard Smith richard at metafoo.co.uk
Tue Oct 25 10:58:51 PDT 2011

On Tue, October 25, 2011 17:39, Marshall Clow wrote:
> This is not a proposal, but rather a starting point for a discussion.
> I've been running a fair amount of ostensibly portable code through clang,
> and I'm getting a bunch of warnings that, while correct, are (I believe)
> unhelpful.
> Here are two examples:
> 1) libjpeg tests for integer size mismatch/overflows with:
>> if ((long) jd_samplesperrow != samplesperrow)
> and clang helpfully responds with (when compiling on x86_64):
> jdmaster.c:285:31: warning: Both operands to '!=' always have the same value

This message comes from the static analyzer. In the general case, the static
analyzer may have proven interesting things about these values in making this
conclusion, so I would imagine this is often a useful warning. What is it
about this specific case which makes it unhelpful?

> 2) crypto++ has a template that looks like this:
>> template <class T> std::string IntToString(T a, unsigned int base = 10)
>> {
>> if (a == 0) return "0"; bool negate = false; if (a < 0) {
>> negate = true;
 and so on.
> and when instantiated like this IntToString<unsigned long>
> clang pipes up with: ./misc.h:414:8: warning: comparison of unsigned
> expression < 0 is always false [-Wtautological-compare] if (a < 0)

This is known, and already filed as PR8682. My view is that we should only
issue this warning (and possibly others like it, on a case-by-case basis) if
it is true for all possible instantiations. One conservative approach to this
is to only warn if we detect the problem while parsing the template (this
would incidentally also fix the issue that, for the non-dependent problem
cases, we currently see this warning once for the template and once in each

> I can find other examples if people want to see them.

The more specific you can be about problems, the better.


More information about the cfe-dev mailing list