Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to silence GCC pedantic (-Wpedantic) warning regarding __FUNCTION__

Tags:

c

gcc-warning

I am printing out (printf) the name of the function as I enter it by using the "__FUNCTION__" predefined macro (in gcc and clang). However, if I use -Wpedantic, I get this warning:

warning: ISO C does not support ‘__FUNCTION__’ predefined identifier [-Wpedantic]

How do I silence that warning?

like image 842
Scooter Avatar asked Oct 24 '18 07:10

Scooter


People also ask

What is the use of -pedantic warning in C?

Warnings from -pedantic are given where they are required by the base standard. (It would not make sense for such warnings to be given only for features not in the specified GNU C dialect, since by definition the GNU dialects of C include all features the compiler supports with the given option, and there would be nothing to warn about.)

Why are these warnings optional in GCC?

These warnings are made optional because GCC is not smart enough to see all the reasons why the code might be correct despite appearing to have an error. Here is one example of how this can happen: { int x; switch (y) { case 1: x = 1; break; case 2: x = 4; break; case 3: x = 5; } foo (x); }

How do I get GCC to warn when a pragma directive is encountered?

See Function Attributes . This warning is enabled by -Wall or -Wextra . Warn when a #pragma directive is encountered which is not understood by GCC. If this command line option is used, warnings will even be issued for unknown pragmas in system header files. This is not the case if the warnings were only enabled by the -Wall command line option.

What are the language-independent options in GCC?

The following language-independent options do not enable specific warnings but control the kinds of diagnostics produced by GCC. Check the code for syntax errors, but don't do anything beyond that. Inhibit all warning messages. Make all warnings into errors. Make the specified warning into an error.


2 Answers

There is no reason to use __FUNCTION__.

__func__ is the standard one (C99, C11, C17). C11 6.4.2.2p1:

  1. The identifier __func__ shall be implicitly declared by the translator as if, immediately following the opening brace of each function definition, the declaration

    static const char __func__[] = "function-name";
    

From GCC documentation:

__FUNCTION__ is another name for __func__, provided for backward compatibility with old versions of GCC.

And if you want to know how old, __func__ appeared in GCC 2.95, released July 31, 1999. Mind you, you do not need __FUNCTION__ for anything else but to support GCC 2.94 or earlier. If you do, then that warning is probably least of your worries.


However, __func__ isn't available in C89/90 mode either, so you'd get a warning there. If you care about ISO diagnostics, then you need to use a more recent revision. Modern GCCs default to GNU C11 or C17 already.


See also: What's the difference between __PRETTY_FUNCTION__, __FUNCTION__, __func__

like image 166

The standard compliant function identifier is __func__

From §6.4.2.2 of the C11 specification

The identifier __func__ shall be implicitly declared by the translator as if, immediately following the opening brace of each function definition, the declaration
static const char __func__[] = "function-name";
appeared, where function-name is the name of the lexically-enclosing function.

I believe that __func__ was added in C99.

like image 25
user3386109 Avatar answered Oct 17 '22 19:10

user3386109