Treating __func__ as a string literal instead of a

2020-02-10 14:01发布

I am using gcc to compile C99 code. I want to write a macro which will return a string containing the function name and line number.

This is what I have:

#define INFO_MSG  __FILE__ ":"__func__"()"

However, when I compile code which attempts to use this string, for example:

char buff[256] = {'\0'}
sprintf(buff, "Something bad happened here: %s, at line: %d", INFO_MSG, __LINE__);
printf("INFO: %s\n", buff);

I get the following error message:

error: expected ‘)’ before ‘__func__’

I have tracked the problem down to the macro. as when I remove __func__ from the macro, the code compiles correctly.

How do I fix the macro, so that I can include the predefined __func__ macro in my string?

标签: c macros
4条回答
疯言疯语
2楼-- · 2020-02-10 14:34

Remark that, "__func__ is not a function so it cannot be called; in fact, it is a predefined identifier that points to a string that is the name of the function, and is only valid inside the scope of a function." - Jonathan.

The following is what you are looking for:

#define TO_STR_A( A ) #A
#define TO_STR( A ) TO_STR_A( A )
#define INFO_MSG TO_STR( __LINE__ ) ":" __FILE__

char buff[ 256 ] = { 0 };

sprintf( buff, "Something bad happened here, %s in function %s().", INFO_MSG, __func__ );
printf( "INFO: %s\n", buff );

... note that a call to __func__ can be made inside the function itself. See this.

查看更多
祖国的老花朵
3楼-- · 2020-02-10 14:35

Judging from your comments, the objective is to have a macro which combines the file name and function name (and maybe line number) into a single string that can be passed as an argument to functions such as printf() or strcpy() or syslog().

Unfortunately, I don't think that's possible.

The C11 standard says:

ISO/IEC 9899:2011 §6.4.2.2 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 name of the lexically-enclosing function.

Therefore, __func__ is not a macro, unlike __FILE__ or __LINE__.

The related question What's the difference between __PRETTY_FUNCTION__, __FUNCTION__, __func__? covers some alternative names. These are GCC-specific extensions, not standard names. Moreover, the GCC 4.8.1 documentation says:

These identifiers are not preprocessor macros. In GCC 3.3 and earlier, in C only, __FUNCTION__ and __PRETTY_FUNCTION__ were treated as string literals; they could be used to initialize char arrays, and they could be concatenated with other string literals. GCC 3.4 and later treat them as variables, like __func__. In C++, __FUNCTION__ and __PRETTY_FUNCTION__ have always been variables.

There are sound reasons why these cannot be preprocessor constructs. The preprocessor does not know what a function is and whether the text it is processing is in the scope of a function, or what the name of the enclosing function is. It is a simple text processor, not a compiler. Clearly, it would be possible to build that much understanding into the preprocessor (solely for the support of this one feature), but it is not required by the standard, and neither should it be required by the standard.

Unfortunately, though, I think it means that attempts to combine __func__ (by any spelling) with __FILE__ and __LINE__ in a single macro to generate a single string literal are doomed.

Clearly, you can generate the file name and line number as a string using the standard two-step macro mechanism:

#define STR(x) #x
#define STRINGIFY(x) STR(x)

#define FILE_LINE __FILE__ ":" STRINGIFY(__LINE__)

You can't get the function name into that as part of a string literal, though.

There are arguments that the file name and line number are sufficient to identify where the problem is; the function name is barely necessary. It is more cosmetic than functional, and slightly helps programmers but not other users.

查看更多
对你真心纯属浪费
4楼-- · 2020-02-10 14:35

After a quick experiment I found that you cannot use __func__ with stringification. It would not make much sense if you could as this would mean that the value would be wherever the macro is defined instead of where it is applied.

The nature of __func__, as noted in the comments on the question, is described in this answer.

Stringification is performed at pre-processor time and because of that __func__ is unavailable as it is essentially a function local string that is defined later on the compilation process.

However you can use __func__ in a macro as long as you don't use stringification on it. I think the following performs what you're after:

#include <stdio.h>

#define INFO_MSG "Something bad happened here: %s : %s(), at line: %d", \
                 __FILE__, __func__, __LINE__

int main()
{
    char buff[256] = {'\0'};
    sprintf(buff, INFO_MSG);
    printf("INFO: %s\n", buff);
    return 0;
}

Note that there's no particular reason, in the question as presented, to use a string buffer. The following main function would achieve the same effect without the possibility of buffer overrun:

int main()
{
    printf("INFO: ");
    printf(INFO_MSG);
    printf("\n");
    return 0;
}

Personally, I'd wrap up the whole process in the macro like this:

#include <stdio.h>

#define INFO_MSG(msg) printf("%s: %s : %s(), at line: %d\n", \
                        msg, __FILE__, __func__, __LINE__)

int main()
{
    INFO_MSG("Something bad happened");
    return 0;
}
查看更多
欢心
5楼-- · 2020-02-10 14:36

it is a syntax error. I try to come over with your macro specification but I didnt find a efficient way, so maybe you can try this:

#define INFO_MSG  __FILE__ , __FUNCTION__   

int main()
{
    char buff[256] = {'\0'};
    sprintf(buff, "Something bad happened here: %s : %s(), at line: %d", INFO_MSG, __LINE__);
    printf("INFO: %s\n", buff);
}
查看更多
登录 后发表回答