preprocessing issues

G

gaetanoortisi

Hello,
I would like to know if there is some preprocessing trick available on
gcc that could expand something like:

debug(a)
debug(x,y, z)

as:

debug_a();

debug_x();
debug_y();
debug_z();

to help me debug many vars of different type.
Thanks,
Gaetano
 
J

Jean-MArc Lienher

Hello,
I would like to know if there is some preprocessing trick available on
gcc that could expand something like:

debug(a)
debug(x,y, z)

as:

debug_a();

debug_x();
debug_y();
debug_z();

to help me debug many vars of different type.
Thanks,
Gaetano

You can't have a variable number of parameters (except when using "...")
in a macro.

So :
#define debug1(p1) debug_##p1##();
#define debug2(p1, p2) debug_##p1##(); debug_##p2##();
#define debug3(p1, p2, p3) debug_##p1##(); debug_##p2##(); debug_##p3##();

This is the only solution in C99, but maybe there is some GNU extensions
that I don't know.
 
L

Laurent Deniau

Hello,
I would like to know if there is some preprocessing trick available on
gcc that could expand something like:

debug(a)
debug(x,y, z)

as:

debug_a();

debug_x();
debug_y();
debug_z();

to help me debug many vars of different type.
Thanks,

C99 allows to write such macro, no needs for gcc tricks.

Using the COS macros from:

http://cos.cvs.sourceforge.net/cos/COS/src/cos/fpp/

you can define the macro debug as (tested):

#define debug(...) FPP_SEP(FPP_MAP((__VA_ARGS__),debug_))
#define debug_(a) FPP_CAT(debug_,a)();

where FPP_SEP, FPP_MAP and FPP_CAT are macros available from fpp/
list.h

debug(a) -> debug_a();
debug(x,y,z) -> debug_x(); debug_y(); debug_z();

Other preprocessor libraries like Chaos can also be used instead of
COS fpp.

a+, ld.
 
J

Jean-Marc Lienher

Laurent said:
C99 allows to write such macro, no needs for gcc tricks.

Using the COS macros from:

http://cos.cvs.sourceforge.net/cos/COS/src/cos/fpp/

Thank you for the information!!!

For my own education, I've tried to understand your macros.

Here is what I've understood :

#define count_args(...) get_nb_args(__VA_ARGS__, 3, 2, 1)
#define get_nb_args(pad1, pad2, pad3, n, ...) n

#define debug(...) debug_(count_args(__VA_ARGS__), __VA_ARGS__)

#define debug_(a, ...) debug##a(__VA_ARGS__)

#define debug1(a) debug_##a()
#define debug2(a, b) debug1(a); debug_##b()
#define debug3(a, b, c) debug2(a, b); debug_##c()

I've removed all the generic code to make it shorter and to work only in
this specific case.
It seems to work, but I've not carefully tested it in all situations.
 

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

Forum statistics

Threads
473,774
Messages
2,569,598
Members
45,145
Latest member
web3PRAgeency
Top