Should GCC warn when __LINE__ is treated as %u %d %i?

Matthias Pfaller leo@marco.de
Tue Feb 19 07:01:00 GMT 2019


On 02/18/2019 11:03 PM, Martin Sebor wrote:
> On 2/17/19 7:12 AM, Jonny Grant wrote:
>> Hello
>>
>> **Please keep my email address in any replies.
>>
>>
>> 1) should this not give type conversion warnings?
>>
>> printf("lineoutput %d:%u:%i\n", __LINE__, __LINE__, __LINE__);
>>
>> 2) Should __LINE__ expand as an 'int', or 'unsigned int'?
>>
>> Had expected line 9 to expand as '9U', but -save-temps shows it ends
>> up as:
>>
>> printf("lineoutput %d:%u:%i\n", 9, 9, 9);
> 
> The bigger problem with __LINE__ is that it need not expand to int
> at all, such as in the following:
> 
> $ cat u.c && gcc -O2 -S -Wall -Wextra u.c
> #line 2147483647
> 
> void f (void)
> {
>   __builtin_printf ("%i", __LINE__);
> }
> u.c: In function ‘f’:
> u.c:-2147483646:21: warning: format ‘%i’ expects argument of type ‘int’,
> but argument 2 has type ‘long int’ [-Wformat=]
> 
> Notice how the line number in the message doesn't correspond to
> the sum of 2147483647 and 3 (the offset from the #line directive).
> 
> Martin
> 
>>
>>
>> C spec has §6.10.8
>> 6.10.8 Predefined macro names
>>
>> _ _LINE_ _ The presumed line number (within the current source file)
>> of the current
>> source line (an integer constant). 155)
>>
>>
>> Cheers, Jonny
>>
>>
>>
>> /* gcc -pedantic -ansi -Wall -Wconversion -Wsign-conversion -c -o
>> print_d_u print_d_u.c */
>> #include <stdio.h>
>>
>> int main()
>> {
>>      printf("lineoutput %d:%u:%i\n", __LINE__, __LINE__, __LINE__);
>>      return 0;
>> }
> 

bash$ f83
&2147483647 3 + u. 80000002 ok
80000002 dup decimal . u. -2147483646 2147483650 ok
bye
bash$

As you can see $80000002 (the sum of $7fffffff and 3) is -2147483646
when interpreted as a signed integer and 2147483650 when interpreted as
an unsigned integer.

regards, Matthias
-- 
Matthias Pfaller                          Software Entwicklung
marco Systemanalyse und Entwicklung GmbH  Tel   +49 8131 5161 41
Hans-Böckler-Str. 2, D 85221 Dachau       Fax   +49 8131 5161 66
http://www.marco.de/                      Email leo@marco.de
Geschäftsführer Martin Reuter             HRB 171775 Amtsgericht München

-------------- next part --------------
A non-text attachment was scrubbed...
Name: smime.p7s
Type: application/pkcs7-signature
Size: 5336 bytes
Desc: S/MIME Cryptographic Signature
URL: <https://gcc.gnu.org/pipermail/gcc-help/attachments/20190219/c4b86eac/attachment-0001.p7s>


More information about the Gcc-help mailing list