James Kuyper
5/19/2011 10:17:00 AM
On 05/19/2011 05:03 AM, Sorc Chan wrote:
> On 18 май, 18:08, rouben@shady.(none) (Rouben Rostamian) wrote:
>> Consider:
>>
>> #include <stdio.h>
>> int main(void)
>> {
>> printf("entering function %s\n", __func__);
>> return 0;
>>
>> }
>>
>> Gcc compiles this program without a warning, even
>> in C89 mode, as in "gcc -Wall -pedantic -std=c89 progname.c".
>>
>> I expected to see a warning since the identifier __func__ was
>> introduced in C99. Is there a good reason why the compilation
>> does not fail in the C89 mode?
>>
>> --
>> Rouben Rostamian
>
> int main(void) {
> __func__;
> return 0;
> }
>
> $ gcc -S main1.c -Wall -pedantic -std=c89 -o main1.s && cat main1.s
> main1.c: In function â??mainâ??:
> main1.c:2: warning: statement with no effect
> .file "main1.c"
> .text
> .globl main
> .type main, @function
> main:
> pushl %ebp
> movl %esp, %ebp
> movl $0, %eax
> popl %ebp
> ret
> .size main, .-main
> .section .rodata
> .type __func__.869, @object
> .size __func__.869, 5
> __func__.869:
> .string "main"
> .ident "GCC: (GNU) 4.4.4 20100630 (Red Hat 4.4.4-10)"
> .section .note.GNU-stack,"",@progbits
> $
>
> Dat GCC epic fail IMHO.
It might be an "epic failure" to meet some arbitrary requirement that
you've chosen to impose; but it does not indicate a failure to meet the
requirements imposed by the C89 standard.
>>> What would _prevent_ GCC from providing this for C89 code?
>
> Because __func__ in C98 undeclared such as any __ohhelloguys__ thing.
C89 imposes no such requirement. In fact, the main reason why the C89
standard said that a program using __func__ has undefined behavior is so
an implementation can choose to do anything it wishes to do with
__func__. In particular, the fact that the behavior is undefined gives
the compiler permission to treat __func__ as behaving in precisely the
fashion it's required to behave, according to the C99 standard. It also
gives it permission to treat __func__ as an indication that it should
use your program to generate Funk music.
--
James Kuyper