n1970=06-0040 C99 Compatibility : __func__ and predeclared identifiers
Alisdair Meredith [firstname.lastname@example.org]
C99 Compatibility : __func__ and predeclared identifiers
The 1999 revision of the C standard introduced the concept of 'a predeclared identifier'.
This feature is used capture function names as string literals, to assist diagnostic
libraries. This small change seems preferable to changing the preprocessor to provide
equivalent functionality, educating it to parse function definitions in along the way.
Although it would seem expensive to introduce a string literal for every function, it
should be relatively simple to optimise the literal away in the majority of cases, where
the function never refers to it.
Likewise, the proposed implementation below mandates that the literal is simply the
unqualified name of the function, so overload sets could all share the same representation
of the literal.
In writing up the proposal the format of the literal has been deliberately specified, as
an implementation defined string is less helpful in when trying to write a portable
diagnostic library facility. The unadorned function name seems the least controversial
representation, as the lowest common denominator, and has the benefit of being the
same format as would be emitted by a C99 compiler.
It would be quite possible for vendors to offer additional predefined identifiers using
similarly reserved names (eg using double underscores) This would allow vendors to
supply more information about the signature if they preferred, namespace or class
idenfiers etc. While such extensions would be conforming, they are not a requirement
for this proposal.
The only other point of note is that the library text for the C99 assert macro was updated
to require the value of __func__, as will as __FILE__ and __LINE__. As C++ includes the
C90 standard by reference, it may be necessary to update the library clause with words to
that effect. Alternatively, if the C99 library is included by reference, no further changes
are be necessary.
2. Proposed wording
2.10.1 Predefined identifiers
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 unqualified name of the lexically-enclosing function.
2 This name is encoded as if the implicit declaration had been written in the source
character set and then translated into the execution character set as indicated in translation
3 EXAMPLE Consider the code fragment:
/* ... */
Each time the example::myfunc() is called, it will print to the standard output stream: