C preprocessor: Is there any way to expand the name of a macro inside itself -


i have macro prints it's own name (among other things), can't find way expand macros' name in macro itself. basically, want equivalent of __function__ macro name.

for example:

#define f(a, b, c) { \    printf("%s: %s,%s,%s\n", __macro__, #a, #b, #c); \    c = a+b; b=a; \ } while(0)  f(x,y,z);  

i print "f: x,y,z" when called.


this indeed xy problem.

here i'm trying do: soft linking symbols library using dlopen , dlsyms.

in order typical way know of is: 1) include headers library. 2) declare variable of type of function trying import 3) initialize said variable call dlsym 4) call actual function through variable instead of calling symbol directly.

when have many such function soft link, becomes repetitive, macro can help.

for full fledge example of that, see here: https://github.com/webkit/webkit/blob/master/source/webcore/platform/mac/softlinking.h

the problem in example , others still need manually copy parameters list each functions want soft link. i'm trying improve on that, , experimenting macro tricks, 1 of have been helped being able answer initial question.

--

beside soft linking issues, there other situation useful. example debugging purpose, 1 might want print diagnostic information before or after every call functions. 1 overriding functions names macros:

#define funca(...) \ printf("before call %s:%d - %d\n", #funca, __line__, g_count); \ funca(__va_args__) \ printf("after call %s:%d - %d\n", #funca, __line__, g_count); 

this works enough if want bunch of function, have copy macro , change funca funcb 4 times each new macro. i'd rather copy , change name of macros, without having change what's inside.

i can optimize above defining internal macro p, similar basile suggested:

#define funca(...) wrap(funca, __va_args__) 

but that's still means need change funca new function name twice every time copy macro function.

read more c preprocessor. read documentation of gnu cpp. notice stringification , concatenation abilities.

perhaps going thru internal macro might you, might have public macro like

 #define foo(x,y,z) private_foo(foo,x,y,z) 

with private_foo macro doing stringification tricks on first argument, maybe

#define private_foo(mac,x,y,z) { \  printf("%s: %s,%s,%s\n", #mac, #x, #y, #z); \  z = x+y; y=x; \ } while(0) 

perhaps c preprocessor not adequate task. generate c code other tool (perhaps gpp preprocessor, or perhaps simple awk or python script, or perhaps real program; you'll need change build process, e.g. makefile, handle that). customize gcc compiler melt if need that. since don't know actual motivations, cannot more.

i cannot more, unless motivate , clarify question.

your soft linking issue needs use weak symbols. unrelated c preprocessing.


Comments

Popular posts from this blog

sublimetext3 - what keyboard shortcut is to comment/uncomment for this script tag in sublime -

java - No use of nillable="0" in SOAP Webservice -

ubuntu - Laravel 5.2 quickstart guide gives Not Found Error -