Scooter
Scooter

Reputation: 7061

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

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?

Upvotes: 10

Views: 5750

Answers (4)

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__

Upvotes: 14

user3386109
user3386109

Reputation: 34829

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.

Upvotes: 8

P.W
P.W

Reputation: 26800

The -Wpedantic option is used to:

Issue all the warnings demanded by strict ISO C and ISO C++; reject all programs that use forbidden extensions, and some other programs that do not follow ISO C and ISO C++. For ISO C, follows the version of the ISO C standard specified by any -std option used.

__FUNCTION__ is a GCC extension. However __func__ is a predefined identifier in C11. I understand this is part of C99 as well.

Committee draft for C11 (N1570) states that:

6.4.2.2 Predefined identifiers
Semantics
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";

appeared, where function-name is the name of the lexically-enclosing function.

Upvotes: 2

marcolz
marcolz

Reputation: 2970

Do no use -Wpedantic unless you try to adhere to the ANSI C standard, which obviously does not support the __FUNCTION__ keyword.

Use -Wall -Wextra instead.

Upvotes: 0

Related Questions