[webkit-dev] Reminder regarding formatting uint64_t

Adrian Perez de Castro aperez at igalia.com
Wed Feb 27 14:50:42 PST 2019


Hello,

On a related note...

On Wed, 27 Feb 2019 16:35:39 -0600, Michael Catanzaro <mcatanzaro at igalia.com> wrote:
 
> For the past several years, I've been regularly fixing -Wformat 
> warnings that look like this:
> 
> ../../Source/WebKit/WebProcess/WebPage/WebPage.cpp:3148:46: warning: 
> format ‘%llu’ expects argument of type ‘long long unsigned 
> int’, but argument 6 has type ‘uint64_t’ {aka ‘long unsigned 
> int’} [-Wformat=]
>          RELEASE_LOG_ERROR(ProcessSuspension, "%p - WebPage 
> (PageID=%llu) - LayerTreeFreezeReason::ProcessSuspended was set when 
> removing LayerTreeFreezeReason::PageTransition; current reasons are %d",
>                                               
> ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
>              this, m_pageID, m_LayerTreeFreezeReasons.toRaw());
>                    ~~~~~~~~
> 
> Problem is that uint64_t is long long unsigned int on Mac, but only 
> long unsigned int on Linux. It can't be printed with %llu, so please 
> use PRIu64 instead. E.g.:
> 
> LOG("%llu", pageID); // wrong
> LOG("%" PRIu64, pageID); // correct
>
> This is good to keep in mind for any sized integer types, but uint64_t 
> in particular since this is what causes problems for us in practice.

Related tip: if you want to print a “size_t” or “ssize_t”, use the
corresponding format specifiers “%zu” and “%zs” — do not cast the value
to some “big enough” integral type. Examples:

  size_t byteSize = /* ... */;
  LOG("size: %llu bytes", static_cast<unsigned long long>(byteSize)); // meh
  LOG("size: %zu bytes", byteSize); // better!

This is not as much of an issue as the issue with “uint64_t” mentioned by
Michael, but still nice to keep in mind as well.

Cheers,


-Adrián
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 195 bytes
Desc: not available
URL: <http://lists.webkit.org/pipermail/webkit-dev/attachments/20190228/375bf639/attachment.bin>


More information about the webkit-dev mailing list