Prepend name to function names?

D

dspfun

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?
 
B

Ben Pfaff

dspfun said:
#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.
 
D

dspfun

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.

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.
 
H

Harald van Dijk

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");
}
 
D

dspfun

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");



}- Dölj citerad text -

- Visa citerad text -- Dölj 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?
 
H

Harald van Dijk

#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."
 
D

dspfun

[quoted text below edited for readability]
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!
 
B

Ben Pfaff

dspfun said:
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.
 
D

dspfun

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;}}}- Dölj citerad text -

- Visa citerad text -

I saw that, my bad.. Thanks for your help!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,534
Members
45,008
Latest member
Rahul737

Latest Threads

Top