[llvm-bugs] [Bug 42614] New: [clang or clang-tidy] -Wformat awareness for <cinttypes>

via llvm-bugs llvm-bugs at lists.llvm.org
Sat Jul 13 11:56:43 PDT 2019


https://bugs.llvm.org/show_bug.cgi?id=42614

            Bug ID: 42614
           Summary: [clang or clang-tidy] -Wformat awareness for
                    <cinttypes>
           Product: clang
           Version: trunk
          Hardware: PC
                OS: Linux
            Status: NEW
          Severity: enhancement
          Priority: P
         Component: Frontend
          Assignee: unassignedclangbugs at nondot.org
          Reporter: lebedev.ri at gmail.com
                CC: llvm-bugs at lists.llvm.org, neeilans at live.com,
                    richard-llvm at metafoo.co.uk

printf()-style functions have a well-defined format string:
https://en.cppreference.com/w/cpp/io/c/fprintf
clang knows how to verify it (-Wformat),

However there is a huge pitfall hiding in plain sight.
If one defines the variable e.g. as uint64_t: normally it is a 'unsigned long',
so one will just use %lu - that is what clang recommends.
But on different platform  uint64_t can be 'unsigned long long',
and -Wformat will complain that '%llu' should be used.

Neither of these is the "correct" solution - PRIu64 should be used instead.
https://en.cppreference.com/w/cpp/header/cinttypes

Now, obviously the current -Wformat behavior isn't wrong - it does produce
the correct results on the current platform - but they aren't *great*, since
they don't catch (and actively advertise) platform-dependent format string.

While i expect it may be reasonably trivial to distinguish whether the 
printf() parameter is int or int32_t (e.g.), i'm honestly not sure how
to deal with format string parsing - the current approach won't work,
as it would need to be done before macro substitution.

-- 
You are receiving this mail because:
You are on the CC list for the bug.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/llvm-bugs/attachments/20190713/04ef6bda/attachment.html>


More information about the llvm-bugs mailing list