public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c++/98819] New: -Wall -Wformat-signedness  suggests %u for %u
@ 2021-01-25 10:22 jg at jguk dot org
  2021-01-25 10:37 ` [Bug c++/98819] Wall Wformat-signedness " pinskia at gcc dot gnu.org
                   ` (6 more replies)
  0 siblings, 7 replies; 8+ messages in thread
From: jg at jguk dot org @ 2021-01-25 10:22 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=98819

            Bug ID: 98819
           Summary: -Wall -Wformat-signedness  suggests %u for %u
           Product: gcc
           Version: 11.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: jg at jguk dot org
  Target Milestone: ---

Reproduced in latest Godbolt trunk

%u is suggested for %u


#1 with x86-64 gcc (trunk)
<source>: In function 'int main()':
<source>:6:19: warning: format '%u' expects argument of type 'unsigned int',
but argument 2 has type 'int' [-Wformat=]
    6 |     std::printf("%u", CURRENT_YEAR);
      |                  ~^
      |                   |
      |                   unsigned int
      |                  %u
Compiler returned: 0





#include <cstdio>
#define CURRENT_YEAR 2021

int main()
{
    std::printf("%u", CURRENT_YEAR);
}

^ permalink raw reply	[flat|nested] 8+ messages in thread

* [Bug c++/98819] Wall Wformat-signedness  suggests %u for %u
  2021-01-25 10:22 [Bug c++/98819] New: -Wall -Wformat-signedness suggests %u for %u jg at jguk dot org
@ 2021-01-25 10:37 ` pinskia at gcc dot gnu.org
  2021-01-25 10:42 ` jg at jguk dot org
                   ` (5 subsequent siblings)
  6 siblings, 0 replies; 8+ messages in thread
From: pinskia at gcc dot gnu.org @ 2021-01-25 10:37 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=98819

--- Comment #1 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
I think you misunderstood the diagnostic.  It is saying unsigned int is for %u.
 The type you have is int.

^ permalink raw reply	[flat|nested] 8+ messages in thread

* [Bug c++/98819] Wall Wformat-signedness  suggests %u for %u
  2021-01-25 10:22 [Bug c++/98819] New: -Wall -Wformat-signedness suggests %u for %u jg at jguk dot org
  2021-01-25 10:37 ` [Bug c++/98819] Wall Wformat-signedness " pinskia at gcc dot gnu.org
@ 2021-01-25 10:42 ` jg at jguk dot org
  2021-01-25 16:43 ` [Bug c/98819] " msebor at gcc dot gnu.org
                   ` (4 subsequent siblings)
  6 siblings, 0 replies; 8+ messages in thread
From: jg at jguk dot org @ 2021-01-25 10:42 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=98819

--- Comment #2 from Jonny Grant <jg at jguk dot org> ---
(In reply to Andrew Pinski from comment #1)
> I think you misunderstood the diagnostic.  It is saying unsigned int is for
> %u.  The type you have is int.

Ah, is that "%u" not the suggestion?


Change it to %f and it gives the clear "%d" suggestion I expected for my int



#1 with x86-64 gcc (trunk)
<source>: In function 'int main()':
<source>:6:19: warning: format '%f' expects argument of type 'double', but
argument 2 has type 'int' [-Wformat=]
    6 |     std::printf("%f", CURRENT_YEAR);
      |                  ~^
      |                   |
      |                   double
      |                  %d
Compiler returned: 0

^ permalink raw reply	[flat|nested] 8+ messages in thread

* [Bug c/98819] Wall Wformat-signedness  suggests %u for %u
  2021-01-25 10:22 [Bug c++/98819] New: -Wall -Wformat-signedness suggests %u for %u jg at jguk dot org
  2021-01-25 10:37 ` [Bug c++/98819] Wall Wformat-signedness " pinskia at gcc dot gnu.org
  2021-01-25 10:42 ` jg at jguk dot org
@ 2021-01-25 16:43 ` msebor at gcc dot gnu.org
  2021-01-25 17:51 ` dmalcolm at gcc dot gnu.org
                   ` (3 subsequent siblings)
  6 siblings, 0 replies; 8+ messages in thread
From: msebor at gcc dot gnu.org @ 2021-01-25 16:43 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=98819

Martin Sebor <msebor at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|UNCONFIRMED                 |NEW
   Last reconfirmed|                            |2021-01-25
     Ever confirmed|0                           |1
          Component|c++                         |c
                 CC|                            |msebor at gcc dot gnu.org

--- Comment #3 from Martin Sebor <msebor at gcc dot gnu.org> ---
The comments in the code that chooses to use the 'u' conversion specifier in
the hint suggest this might indeed be due to a bug:

              if (conversion_char)
                /* We found a match, using the given conversion char - the
                   length modifier was incorrect (or absent).
                   Provide a suggestion using the conversion char with the
                   correct length modifier for the type.  */
                return xasprintf ("%s%c", len_modifier, conversion_char);

The "match" refers to the type for the specifier that matches the type of the
argument (i.e., 'i' for 2021).  No length modifier was provided but that's not
incorrect.  What is "incorrect" (but not unsafe) is the provided conversion
specifier.

But I'm surprised the warning triggers to begin with in this case, when the
signed argument is representable in the unsigned type without change. 
Ordinarily GCC avoids issuing warnings for code that's demonstrably safe (e.g.,
-Wchar-subscripts when the char subscript is in the non-negative range).

It seems to me the warning should not be issued in this case; when it is
issued, the hint should be corrected to match the type of the provided
argument.

^ permalink raw reply	[flat|nested] 8+ messages in thread

* [Bug c/98819] Wall Wformat-signedness  suggests %u for %u
  2021-01-25 10:22 [Bug c++/98819] New: -Wall -Wformat-signedness suggests %u for %u jg at jguk dot org
                   ` (2 preceding siblings ...)
  2021-01-25 16:43 ` [Bug c/98819] " msebor at gcc dot gnu.org
@ 2021-01-25 17:51 ` dmalcolm at gcc dot gnu.org
  2021-01-26 12:46 ` jg at jguk dot org
                   ` (2 subsequent siblings)
  6 siblings, 0 replies; 8+ messages in thread
From: dmalcolm at gcc dot gnu.org @ 2021-01-25 17:51 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=98819

David Malcolm <dmalcolm at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |dmalcolm at gcc dot gnu.org

--- Comment #4 from David Malcolm <dmalcolm at gcc dot gnu.org> ---
In comment #0, the bottom-most "%u" is a fix-it hint, giving the nonsensical
suggestion to the user that they replace the "%u" with itself.  Clearly we
shouldn't issue such a fix-it hint.

I'm not able to reproduce the warning with the given reproducer.  What flags
are you using?  Do you have a URL for your godbolt example?

^ permalink raw reply	[flat|nested] 8+ messages in thread

* [Bug c/98819] Wall Wformat-signedness  suggests %u for %u
  2021-01-25 10:22 [Bug c++/98819] New: -Wall -Wformat-signedness suggests %u for %u jg at jguk dot org
                   ` (3 preceding siblings ...)
  2021-01-25 17:51 ` dmalcolm at gcc dot gnu.org
@ 2021-01-26 12:46 ` jg at jguk dot org
  2021-01-26 12:59 ` jg at jguk dot org
  2021-02-03 13:06 ` jg at jguk dot org
  6 siblings, 0 replies; 8+ messages in thread
From: jg at jguk dot org @ 2021-01-26 12:46 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=98819

--- Comment #5 from Jonny Grant <jg at jguk dot org> ---
(In reply to David Malcolm from comment #4)
> In comment #0, the bottom-most "%u" is a fix-it hint, giving the nonsensical
> suggestion to the user that they replace the "%u" with itself.  Clearly we
> shouldn't issue such a fix-it hint.
> 
> I'm not able to reproduce the warning with the given reproducer.  What flags
> are you using?  Do you have a URL for your godbolt example?

Hello David, Martin

Godbot was with:  -Wall -Wformat-signedness

https://godbolt.org/z/bn3oeh

^ permalink raw reply	[flat|nested] 8+ messages in thread

* [Bug c/98819] Wall Wformat-signedness  suggests %u for %u
  2021-01-25 10:22 [Bug c++/98819] New: -Wall -Wformat-signedness suggests %u for %u jg at jguk dot org
                   ` (4 preceding siblings ...)
  2021-01-26 12:46 ` jg at jguk dot org
@ 2021-01-26 12:59 ` jg at jguk dot org
  2021-02-03 13:06 ` jg at jguk dot org
  6 siblings, 0 replies; 8+ messages in thread
From: jg at jguk dot org @ 2021-01-26 12:59 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=98819

--- Comment #6 from Jonny Grant <jg at jguk dot org> ---
Godbolt %u example

https://godbolt.org/z/sc7K6T

^ permalink raw reply	[flat|nested] 8+ messages in thread

* [Bug c/98819] Wall Wformat-signedness  suggests %u for %u
  2021-01-25 10:22 [Bug c++/98819] New: -Wall -Wformat-signedness suggests %u for %u jg at jguk dot org
                   ` (5 preceding siblings ...)
  2021-01-26 12:59 ` jg at jguk dot org
@ 2021-02-03 13:06 ` jg at jguk dot org
  6 siblings, 0 replies; 8+ messages in thread
From: jg at jguk dot org @ 2021-02-03 13:06 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=98819

--- Comment #7 from Jonny Grant <jg at jguk dot org> ---
https://godbolt.org/z/bhnbsb

Another example where %ld suggests to replace with %ld


#include <cstdio>

int main()
{
    size_t i = 0;
    std::printf("%ld", i);
}

 -Wall -Wformat-signedness

x86-64 gcc (trunk) - 600ms (19538B)
#1 with x86-64 gcc (trunk)
<source>: In function 'int main()':
<source>:6:20: warning: format '%ld' expects argument of type 'long int', but
argument 2 has type 'size_t' {aka 'long unsigned int'} [-Wformat=]
    6 |     std::printf("%ld", i);
      |                  ~~^   ~
      |                    |   |
      |                    |   size_t {aka long unsigned int}
      |                    long int
      |                  %ld
Compiler returned: 0

^ permalink raw reply	[flat|nested] 8+ messages in thread

end of thread, other threads:[~2021-02-03 13:06 UTC | newest]

Thread overview: 8+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-01-25 10:22 [Bug c++/98819] New: -Wall -Wformat-signedness suggests %u for %u jg at jguk dot org
2021-01-25 10:37 ` [Bug c++/98819] Wall Wformat-signedness " pinskia at gcc dot gnu.org
2021-01-25 10:42 ` jg at jguk dot org
2021-01-25 16:43 ` [Bug c/98819] " msebor at gcc dot gnu.org
2021-01-25 17:51 ` dmalcolm at gcc dot gnu.org
2021-01-26 12:46 ` jg at jguk dot org
2021-01-26 12:59 ` jg at jguk dot org
2021-02-03 13:06 ` jg at jguk dot org

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).