Warnings during build with older version of clang

Uli Schlachter psychon at znc.in
Sat Apr 13 07:15:59 UTC 2024


Hi,

Am 12.04.24 um 16:03 schrieb H. Thiele:
[...]
> Cairo properly builds through, but I'm getting some suspicious warnings 
> that tickle my "this build may end up broken" senses ("comparison is
> always true/false" stuff).
[...]

The first such warning is:

> ../src/cairo-truetype-subset.c:128:9: warning: comparison of constant 100 with expression of type 'cairo_status_t' (aka 'enum _cairo_status') is always false [-Wtautological-constant-out-of-range-compare]
>         status == (int)CAIRO_INT_STATUS_UNSUPPORTED)

This appears in the following code:

> static cairo_status_t
> _cairo_truetype_font_set_error (cairo_truetype_font_t *font,
> 			        cairo_status_t status)
> {
>     if (status == CAIRO_STATUS_SUCCESS ||
> 	status == (int)CAIRO_INT_STATUS_UNSUPPORTED)
> 	return status;
> 
>     _cairo_status_set_error (&font->status, status);
> 
>     return _cairo_error (status);
> }

This function looks exactly the same in cairo 1.14.6 and in cairo 1.18.0:

https://gitlab.freedesktop.org/cairo/cairo/-/blob/9d3191da6fae7dfd914c3516d6ba369c9ba1a576/src/cairo-truetype-subset.c#L123
https://gitlab.freedesktop.org/cairo/cairo/-/blob/3909090108bb2db55330e3eb148aebe664735363/src/cairo-truetype-subset.c#L123

So... this might not be a helpful answer, but: Are you sure you didn't 
also get the warning before?

For what is actually going on: Cairo plays some tricks with enums. There 
is a cairo_status_t that is public API and there is _cairo_int_status 
that is used internally and has more values. As far as I know, this is 
legal C code that existed in cairo before I even knew what cairo was. 
So, I can't say why you get this warning.

All of those warnings seem to have similar cases, but sometimes involve 
other enums.

Here is one that you can easily write a test for:

> ../src/cairo-pdf-surface.c:801:17: warning: comparison of unsigned enum expression < 0 is always false [-Wtautological-compare]

> const char *
> cairo_pdf_version_to_string (cairo_pdf_version_t version)
> {
>     if (version < 0 || version >= CAIRO_PDF_VERSION_LAST)
> 	return NULL;
> 
>     return _cairo_pdf_version_strings[version];
> }

This function is public API. So, something like the following should 
tell you whether your compiler is only warning about these comparisons 
or really "doing the wrong thing" with them:

assert(NULL == cairo_pdf_version_to_string((cairo_pdf_version_t)-1));
assert(NULL == cairo_pdf_version_to_string((cairo_pdf_version_t)42));

Cheers,
Uli
-- 
“I’m Olaf and I like warm hugs.”



More information about the cairo mailing list