Need help reading macro-heavy code

J

J Krugman

I'm trying to read some source code that makes very heavy use of
macros and other pre-processor directives. To make matters worse,
macro definitions are stacked several layers deep (i.e. macros are
defined in terms of other macros, which are in turn defined in
terms of other macros, seemingly ad infinitum).

I tried reading the *.i files generated by the preprocessor (i.e.
by giving gcc the -E flag). To my surprise, the macros were not
fully expanded in the *.i files. For example, if

#define FOO(x) 2*(x)
#define BAR(x) FOO(x)

then occurrences of BAR(x) in the *.c files where expanded only to
FOO(x) in the corresponding *.i files, not to 2*(x).

What can I do to generate source code with all the macros fully
expanded? Are there tools that are useful when attempting to read
such code?

Thanks!

jill
 
B

Barry Schwarz

I'm trying to read some source code that makes very heavy use of
macros and other pre-processor directives. To make matters worse,
macro definitions are stacked several layers deep (i.e. macros are
defined in terms of other macros, which are in turn defined in
terms of other macros, seemingly ad infinitum).

I tried reading the *.i files generated by the preprocessor (i.e.
by giving gcc the -E flag). To my surprise, the macros were not
fully expanded in the *.i files. For example, if

#define FOO(x) 2*(x)
#define BAR(x) FOO(x)

then occurrences of BAR(x) in the *.c files where expanded only to
FOO(x) in the corresponding *.i files, not to 2*(x).

What can I do to generate source code with all the macros fully
expanded? Are there tools that are useful when attempting to read
such code?
Ask in a newsgroup specific to your compiler. Most have some method
of obtaining the output from the pre-processor which is what you are
looking for.


<<Remove the del for email>>
 
M

Martin Ambuhl

J said:
I'm trying to read some source code that makes very heavy use of
macros and other pre-processor directives. To make matters worse,
macro definitions are stacked several layers deep (i.e. macros are
defined in terms of other macros, which are in turn defined in
terms of other macros, seemingly ad infinitum).

I tried reading the *.i files generated by the preprocessor (i.e.
by giving gcc the -E flag). To my surprise, the macros were not
fully expanded in the *.i files. For example, if

#define FOO(x) 2*(x)
#define BAR(x) FOO(x)

then occurrences of BAR(x) in the *.c files where expanded only to
FOO(x) in the corresponding *.i files, not to 2*(x).

Questions about gcc should be addressed to a gnu newsgroup. Before you
post there, however, make sure that you have a clear question. My copy
of gcc using -E translates

#define FOO(x) 2*(x)
#define BAR(x) FOO(x)

int main(void)
{
int x = 3, y;
y = FOO(x);
return 0;
}

to

int main(void)
{
int x = 3, y;
y = 2 * (x);
return 0;
}

This suggests to me that you are doing something other than that that
you claim.
What can I do to generate source code with all the macros fully
expanded?

Use gcc -E. See above.
Are there tools that are useful when attempting to read
such code?

I have no idea what that's supposed to mean. Use your eyes and brain to
read it.
 
M

macluvitch

Hello folk

you can just use cpp file

to make easy reading an obfuscated source (as you mentioned
before :) ) try first to indent the source code
then make a tag table for emacs try this
etags thesourcecode.c
and fetch the function or macro (it can be supported in some version) by
alt-x tags-search ....

this make reading an understanding a foreign source easily
 

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,744
Messages
2,569,482
Members
44,900
Latest member
Nell636132

Latest Threads

Top