altendky
altendky

Reputation: 4354

How can I get the function name as text not string in a macro?

I am trying to use a function-like macro to generate an object-like macro name (generically, a symbol). The following will not work because __func__ (C99 6.4.2.2-1) puts quotes around the function name.

#define MAKE_AN_IDENTIFIER(x) __func__##__##x

The desired result of calling MAKE_AN_IDENTIFIER(NULL_POINTER_PASSED) would be MyFunctionName__NULL_POINTER_PASSED. There may be other reasons this would not work (such as __func__ being taken literally and not interpreted, but I could fix that) but my question is what will provide a predefined macro like __func__ except without the quotes? I believe this is not possible within the C99 standard so valid answers could be references to other preprocessors.

Presently I have simply created my own object-like macro and redefined it manually before each function to be the function name. Obviously this is a poor and probably unacceptable practice. I am aware that I could take an existing cpp program or library and modify it to provide this functionality. I am hoping there is either a commonly used cpp replacement which provides this or a preprocessor library (prefer Python) which is designed for extensibility so as to allow me to 'configure' it to create the macro I need.


I wrote the above to try to provide a concise and well defined question but it is certainly the Y referred to by @Ruud. The X is...

I am trying to manage unique values for reporting errors in an embedded system. The values will be passed as a parameter to a(some) particular function(s). I have already written a Python program using pycparser to parse my code and identify all symbols being passed to the function(s) of interest. It generates a .h file of #defines maintaining the values of previously existing entries, commenting out removed entries (to avoid reusing the value and also allow for reintroduction with the same value), assigning new unique numbers for new identifiers, reporting malformed identifiers, and also reporting multiple use of any given identifier. This means that I can simply write:

void MyFunc(int * p)
{
    if (p == NULL)
    {
        myErrorFunc(MYFUNC_NULL_POINTER_PASSED);
        return;
    }

    // do something actually interesting here
}

and the Python program will create the #define MYFUNC_NULL_POINTER_PASSED 7 (or whatever next available number) for me with all the listed considerations. I have also written a set of macros that further simplify the above to:

#define FUNC MYFUNC
void MyFunc(int * p)
{
    RETURN_ASSERT_NOT_NULL(p);

    // do something actually interesting here
}

assuming I provide the #define FUNC. I want to use the function name since that will be constant throughout many changes (as opposed to LINE) and will be much easier for someone to transfer the value from the old generated #define to the new generated #define when the function itself is renamed. Honestly, I think the only reason I am trying to 'solve' this 'issue' is because I have to work in C rather than C++. At work we are writing fairly object oriented C and so there is a lot of NULL pointer checking and IsInitialized checking. I have two line functions that turn into 30 because of all these basic checks (these macros reduce those lines by a factor of five). While I do enjoy the challenge of crazy macro development, I much prefer to avoid them. That said, I dislike repeating myself and hiding the functional code in a pile of error checking even more than I dislike crazy macros.

If you prefer to take a stab at this issue, have at.

Upvotes: 3

Views: 2613

Answers (3)

Ruud Helderman
Ruud Helderman

Reputation: 11018

As pointed out by others, there is no macro that returns the (unquoted) function name (mainly because the C preprocessor has insufficient syntactic knowledge to recognize functions). You would have to explicitly define such a macro yourself, as you already did yourself:

#define FUNC MYFUNC

To avoid having to do this manually, you could write your own preprocessor to add the macro definition automatically. A similar question is this: How to automatically insert pragmas in your program

If your source code has a consistent coding style (particularly indentation), then a simple line-based filter (sed, awk, perl) might do. In its most naive form: every function starts with a line that does not start with a hash or whitespace, and ends with a closing parenthesis or a comma. With awk:

{
    print $0;
}

/^[^# \t].*[,\)][ \t]*$/ {
    sub(/\(.*$/, "");
    sub(/^.*[ \t]/, "");
    print "#define FUNC " toupper($0);
}

For a more robust solution, you need a compiler framework like ROSE.

Upvotes: 1

Joseph Quinsey
Joseph Quinsey

Reputation: 9972

__FUNCTION__ used to compile to a string literal (I think in gcc 2.96), but it hasn't for many years. Now instead we have __func__, which compiles to a string array, and __FUNCTION__ is a deprecated alias for it. (The change was a bit painful.)

But in neither case was it possible to use this predefined macro to generate a valid C identifier (i.e. "remove the quotes").

But could you instead use the line number rather than function name as part of your identifier?

If so, the following would work. As an example, compiling the following 5-line source file:

 #define CONCAT_TOKENS4(a,b,c,d)      a##b##c##d
 #define EXPAND_THEN_CONCAT4(a,b,c,d) CONCAT_TOKENS4(a,b,c,d)
 #define MAKE_AN_IDENTIFIER(x)        EXPAND_THEN_CONCAT4(line_,__LINE__,__,x)

 static int MAKE_AN_IDENTIFIER(NULL_POINTER_PASSED);

will generate the warning:

foo.c:5: warning: 'line_5__NULL_POINTER_PASSED' defined but not used

Upvotes: 1

Anonymouse
Anonymouse

Reputation: 945

Gnu-C has a __FUNCTION__ macro, but sadly even that cannot be used in the way you are asking.

Upvotes: 0

Related Questions