Prepend name to function names?

  • Follow


I was asked whether it is possible to prepend a name (using macro
substitution) to all function names in a file/program.
For example, the functions f1(), f2(), f3() should get aa_ prepended
to their names?

I.e., f1(), f2(), f3() should be changed to aa_f1(), aa_f2(), aa_f3()
using some macro substitution.

I came up with the following:

****************************************************
#include <stdio.h>

#define PREPEND aa_
#define MAKE_FUNC_NAME(prepend, func_name) prepend ## func_name

void MAKE_FUNC_NAME(PREPEND, f1)(void)
{
 printf("here..\n");
}
****************************************************

However, this does not define the function aa_f1, instead the function
PREPENDf1 is defined. Why? Is there some way to get around this to
achieve what is set out to be done?
0
Reply dspfun (158) 1/29/2008 8:00:47 PM

dspfun <dspfun@hotmail.com> writes:

> #define PREPEND aa_
> #define MAKE_FUNC_NAME(prepend, func_name) prepend ## func_name
>
> void MAKE_FUNC_NAME(PREPEND, f1)(void)
> {
>  printf("here..\n");
> }
> ****************************************************
>
> However, this does not define the function aa_f1, instead the function
> PREPENDf1 is defined. Why? Is there some way to get around this to
> achieve what is set out to be done?

This is in the C FAQ:

11.17:	I'm trying to use the ANSI "stringizing" preprocessing operator
	`#' to insert the value of a symbolic constant into a message,
	but it keeps stringizing the macro's name rather than its value.

A:	You can use something like the following two-step procedure to
	force a macro to be expanded as well as stringized:

		#define Str(x) #x
		#define Xstr(x) Str(x)
		#define OP plus
		char *opname = Xstr(OP);

	This code sets opname to "plus" rather than "OP".

	An equivalent circumlocution is necessary with the token-pasting
	operator ## when the values (rather than the names) of two
	macros are to be concatenated.

	References: ISO Sec. 6.8.3.2, Sec. 6.8.3.5.

-- 
char a[]="\n .CJacehknorstu";int putchar(int);int main(void){unsigned long b[]
={0x67dffdff,0x9aa9aa6a,0xa77ffda9,0x7da6aa6a,0xa67f6aaa,0xaa9aa9f6,0x11f6},*p
=b,i=24;for(;p+=!*p;*p/=4)switch(0[p]&3)case 0:{return 0;for(p--;i--;i--)case+
2:{i++;if(i)break;else default:continue;if(0)case 1:putchar(a[i&15]);break;}}}
0
Reply blp (3953) 1/29/2008 8:07:38 PM


On 29 Jan, 21:07, Ben Pfaff <b...@cs.stanford.edu> wrote:
> dspfun <dsp...@hotmail.com> writes:
> > #define PREPEND aa_
> > #define MAKE_FUNC_NAME(prepend, func_name) prepend ## func_name
>
> > void MAKE_FUNC_NAME(PREPEND, f1)(void)
> > {
> > =A0printf("here..\n");
> > }
> > ****************************************************
>
> > However, this does not define the function aa_f1, instead the function
> > PREPENDf1 is defined. Why? Is there some way to get around this to
> > achieve what is set out to be done?
>
> This is in the C FAQ:
>
> 11.17: =A0I'm trying to use the ANSI "stringizing" preprocessing operator
> =A0 =A0 =A0 =A0 `#' to insert the value of a symbolic constant into a mess=
age,
> =A0 =A0 =A0 =A0 but it keeps stringizing the macro's name rather than its =
value.
>

It's almost the same, but not quite. What I am trying to do is to
create the function name using macro substitution, however, the
problem is that the PREPEND is never substituted for its corresponding
#define inside the MAKE_FUNC_NAME macro.

I.e.
MAKE_FUNC_NAME(PREPEND, f1)(void);

is by the preprocessor substituted to:
PREPENDf1(void);

What I am trying to achieve is to get MAKE_FUNC_NAME(PREPEND, f1)
(void) substituted to:
aa_f1(void);

Then if for example one have 100 functions, f1()...f100, it is easy to
just change the #define to get all functions named with aa_ , bb_ etc.
prepended to them.

0
Reply dspfun (158) 1/29/2008 8:44:29 PM

On Tue, 29 Jan 2008 12:44:29 -0800, dspfun wrote:
> On 29 Jan, 21:07, Ben Pfaff <b...@cs.stanford.edu> wrote:
>> dspfun <dsp...@hotmail.com> writes:
>> > #define PREPEND aa_
>> > #define MAKE_FUNC_NAME(prepend, func_name) prepend ## func_name
>>
>> > void MAKE_FUNC_NAME(PREPEND, f1)(void) {
>> >  printf("here..\n");
>> > }
>> > ****************************************************
>>
>> > However, this does not define the function aa_f1, instead the
>> > function PREPENDf1 is defined. Why? Is there some way to get around
>> > this to achieve what is set out to be done?
>>
>> This is in the C FAQ:
>>
>> 11.17:  I'm trying to use the ANSI "stringizing" preprocessing operator
>>         `#' to insert the value of a symbolic constant into a
>>         message, but it keeps stringizing the macro's name rather
>>         than its value.
>>
>>
> It's almost the same, but not quite. [...]

Correct. However, the fix for the ## operator is exactly the same as the 
fix for the # operator: add an extra macro. PREPEND gets expanded as long 
as you don't perform the concatenation in MAKE_FUNC_NAME itself, like so:

#define PREPEND aa_
#define MAKE_FUNC_NAME_HELPER(x, y) x ## y
#define MAKE_FUNC_NAME(prepend, func_name) MAKE_FUNC_NAME_HELPER(prepend, 
func_name)

void MAKE_FUNC_NAME(PREPEND, f1)(void) {
  printf("here..\n");
}
0
Reply truedfx (1926) 1/29/2008 8:55:11 PM

On 29 Jan, 21:55, Harald van D=C4=B3k <true...@gmail.com> wrote:
> On Tue, 29 Jan 2008 12:44:29 -0800, dspfun wrote:
> > On 29 Jan, 21:07, Ben Pfaff <b...@cs.stanford.edu> wrote:
> >> dspfun <dsp...@hotmail.com> writes:
> >> > #define PREPEND aa_
> >> > #define MAKE_FUNC_NAME(prepend, func_name) prepend ## func_name
>
> >> > void MAKE_FUNC_NAME(PREPEND, f1)(void) {
> >> > =C2=A0printf("here..\n");
> >> > }
> >> > ****************************************************
>
> >> > However, this does not define the function aa_f1, instead the
> >> > function PREPENDf1 is defined. Why? Is there some way to get around
> >> > this to achieve what is set out to be done?
>
> >> This is in the C FAQ:
>
> >> 11.17: =C2=A0I'm trying to use the ANSI "stringizing" preprocessing ope=
rator
> >> =C2=A0 =C2=A0 =C2=A0 =C2=A0 `#' to insert the value of a symbolic const=
ant into a
> >> =C2=A0 =C2=A0 =C2=A0 =C2=A0 message, but it keeps stringizing the macro=
's name rather
> >> =C2=A0 =C2=A0 =C2=A0 =C2=A0 than its value.
>
> > It's almost the same, but not quite. [...]
>
> Correct. However, the fix for the ## operator is exactly the same as the
> fix for the # operator: add an extra macro. PREPEND gets expanded as long
> as you don't perform the concatenation in MAKE_FUNC_NAME itself, like so:
>
> #define PREPEND aa_
> #define MAKE_FUNC_NAME_HELPER(x, y) x ## y
> #define MAKE_FUNC_NAME(prepend, func_name) MAKE_FUNC_NAME_HELPER(prepend,
> func_name)
>
> void MAKE_FUNC_NAME(PREPEND, f1)(void) {
> =C2=A0 printf("here..\n");
>
>
>
> }- D=C3=B6lj citerad text -
>
> - Visa citerad text -- D=C3=B6lj citerad text -
>
> - Visa citerad text -

Ok, thanks! What is the reason it's not possible to perform the
conatenation in MAKE_FUNC_NAME itself? Which sentence(s) in the
standard (n1256.pdf) specifies this?
0
Reply dspfun (158) 1/29/2008 9:12:49 PM

On Tue, 29 Jan 2008 13:12:49 -0800, dspfun wrote:
> On 29 Jan, 21:55, Harald van Dijk <true...@gmail.com> wrote:
>> >> dspfun <dsp...@hotmail.com> writes:
>> >> > #define PREPEND aa_
>> >> > #define MAKE_FUNC_NAME(prepend, func_name) prepend ## func_name
[quoted text below edited for readability]
>> #define PREPEND aa_
>> #define MAKE_FUNC_NAME_HELPER(x, y) x ## y
>> #define MAKE_FUNC_NAME(prepend, func_name) \
>>   MAKE_FUNC_NAME_HELPER(prepend, func_name)
>
> Ok, thanks! What is the reason it's not possible to perform the
> conatenation in MAKE_FUNC_NAME itself?

The rationale states:

"The specification of this pasting operator is based on these principles:
[...]
* A formal parameter as an operand for ## is not expanded before pasting.
  The actual parameter is substituted for the formal parameter; but the
  actual parameter is not expanded. Given, for example
    #define a(n) aaa ## n
    #define b   2
  the expansion of a(b) is aaab, not aaa2 or aaan.
[...]
These principles codify the essential features of prior art and are
consistent with the specification of the stringizing operator."

In other words, there was no specific reason for or against expansion of 
macro arguments, and existing implementations didn't expand macro 
arguments, so that's what got into the standard.

> Which sentence(s) in the standard
> (n1256.pdf)

I expect you'll get comments focusing solely on this.

> specifies this?

The standard specifies this in 6.10.3.1p1:
"After the arguments for the invocation of a function-like macro have
 been identified, argument substitution takes place. A parameter in the
 replacement list, unless preceded by a # or ## preprocessing token or
 followed by a ## preprocessing token (see below), is replaced by the
 corresponding argument after all macros contained therein have been
 expanded."
0
Reply truedfx (1926) 1/29/2008 9:39:28 PM

On 29 Jan, 22:39, Harald van D=A9=A6k <true...@gmail.com> wrote:
> On Tue, 29 Jan 2008 13:12:49 -0800, dspfun wrote:
> > On 29 Jan, 21:55, Harald van D=A9=A6k <true...@gmail.com> wrote:
> >> >> dspfun <dsp...@hotmail.com> writes:
> >> >> > #define PREPEND aa_
> >> >> > #define MAKE_FUNC_NAME(prepend, func_name) prepend ## func_name
>
> [quoted text below edited for readability]
>
> >> #define PREPEND aa_
> >> #define MAKE_FUNC_NAME_HELPER(x, y) x ## y
> >> #define MAKE_FUNC_NAME(prepend, func_name) \
> >>   MAKE_FUNC_NAME_HELPER(prepend, func_name)
>
> > Ok, thanks! What is the reason it's not possible to perform the
> > conatenation in MAKE_FUNC_NAME itself?
>
> The rationale states:
>
> "The specification of this pasting operator is based on these principles:
> [...]
> * A formal parameter as an operand for ## is not expanded before pasting.
>   The actual parameter is substituted for the formal parameter; but the
>   actual parameter is not expanded. Given, for example
>     #define a(n) aaa ## n
>     #define b   2
>   the expansion of a(b) is aaab, not aaa2 or aaan.
> [...]
> These principles codify the essential features of prior art and are
> consistent with the specification of the stringizing operator."
>
> In other words, there was no specific reason for or against expansion of
> macro arguments, and existing implementations didn't expand macro
> arguments, so that's what got into the standard.
>
> > Which sentence(s) in the standard
> > (n1256.pdf)
>
> I expect you'll get comments focusing solely on this.
>
> > specifies this?
>
> The standard specifies this in 6.10.3.1p1:
> "After the arguments for the invocation of a function-like macro have
>  been identified, argument substitution takes place. A parameter in the
>  replacement list, unless preceded by a # or ## preprocessing token or
>  followed by a ## preprocessing token (see below), is replaced by the
>  corresponding argument after all macros contained therein have been
>  expanded."

Thank you very much!
0
Reply dspfun (158) 1/29/2008 9:50:49 PM

dspfun <dspfun@hotmail.com> writes:

> On 29 Jan, 21:07, Ben Pfaff <b...@cs.stanford.edu> wrote:
>> dspfun <dsp...@hotmail.com> writes:
>> > #define PREPEND aa_
>> > #define MAKE_FUNC_NAME(prepend, func_name) prepend ## func_name
>>
>> > void MAKE_FUNC_NAME(PREPEND, f1)(void)
>> > {
>> > �printf("here..\n");
>> > }
>> > ****************************************************
>>
>> > However, this does not define the function aa_f1, instead the function
>> > PREPENDf1 is defined. Why? Is there some way to get around this to
>> > achieve what is set out to be done?
>>
>> This is in the C FAQ:
>>
>> 11.17: �I'm trying to use the ANSI "stringizing" preprocessing operator
>> � � � � `#' to insert the value of a symbolic constant into a message,
>> � � � � but it keeps stringizing the macro's name rather than its value.
>>
>
> It's almost the same, but not quite. What I am trying to do is to
> create the function name using macro substitution, however, the
> problem is that the PREPEND is never substituted for its corresponding
> #define inside the MAKE_FUNC_NAME macro.

It's pretty clear that you didn't read the whole answer in the
FAQ:

	An equivalent circumlocution is necessary with the token-pasting
	operator ## when the values (rather than the names) of two
	macros are to be concatenated.

-- 
char a[]="\n .CJacehknorstu";int putchar(int);int main(void){unsigned long b[]
={0x67dffdff,0x9aa9aa6a,0xa77ffda9,0x7da6aa6a,0xa67f6aaa,0xaa9aa9f6,0x11f6},*p
=b,i=24;for(;p+=!*p;*p/=4)switch(0[p]&3)case 0:{return 0;for(p--;i--;i--)case+
2:{i++;if(i)break;else default:continue;if(0)case 1:putchar(a[i&15]);break;}}}
0
Reply blp (3953) 1/29/2008 10:15:06 PM

On 29 Jan, 23:15, Ben Pfaff <b...@cs.stanford.edu> wrote:
> dspfun <dsp...@hotmail.com> writes:
> > On 29 Jan, 21:07, Ben Pfaff <b...@cs.stanford.edu> wrote:
> >> dspfun <dsp...@hotmail.com> writes:
> >> > #define PREPEND aa_
> >> > #define MAKE_FUNC_NAME(prepend, func_name) prepend ## func_name
>
> >> > void MAKE_FUNC_NAME(PREPEND, f1)(void)
> >> > {
> >> > =A0printf("here..\n");
> >> > }
> >> > ****************************************************
>
> >> > However, this does not define the function aa_f1, instead the functio=
n
> >> > PREPENDf1 is defined. Why? Is there some way to get around this to
> >> > achieve what is set out to be done?
>
> >> This is in the C FAQ:
>
> >> 11.17: =A0I'm trying to use the ANSI "stringizing" preprocessing operat=
or
> >> =A0 =A0 =A0 =A0 `#' to insert the value of a symbolic constant into a m=
essage,
> >> =A0 =A0 =A0 =A0 but it keeps stringizing the macro's name rather than i=
ts value.
>
> > It's almost the same, but not quite. What I am trying to do is to
> > create the function name using macro substitution, however, the
> > problem is that the PREPEND is never substituted for its corresponding
> > #define inside the MAKE_FUNC_NAME macro.
>
> It's pretty clear that you didn't read the whole answer in the
> FAQ:
>
> =A0 =A0 =A0 =A0 An equivalent circumlocution is necessary with the token-p=
asting
> =A0 =A0 =A0 =A0 operator ## when the values (rather than the names) of two=

> =A0 =A0 =A0 =A0 macros are to be concatenated.
>
> --
> char a[]=3D"\n .CJacehknorstu";int putchar(int);int main(void){unsigned lo=
ng b[]
> =3D{0x67dffdff,0x9aa9aa6a,0xa77ffda9,0x7da6aa6a,0xa67f6aaa,0xaa9aa9f6,0x11=
f6}=AD,*p
> =3Db,i=3D24;for(;p+=3D!*p;*p/=3D4)switch(0[p]&3)case 0:{return 0;for(p--;i=
--;i--)case+
> 2:{i++;if(i)break;else default:continue;if(0)case 1:putchar(a[i&15]);break=
;}}}- D=F6lj citerad text -
>
> - Visa citerad text -

I saw that, my bad.. Thanks for your help!
0
Reply dspfun (158) 1/29/2008 10:25:02 PM

8 Replies
54 Views

(page loaded in 0.076 seconds)

Similiar Articles:













7/29/2012 7:05:53 AM


Reply: