gbadev.org forum archive

This is a read-only mirror of the content originally found on forum.gbadev.org (now offline), salvaged from Wayback machine copies. A new forum can be found here.

C/C++ > Open file, expand macros, save, lather, rinse, repeat...?

#163620 - Izhido - Mon Oct 06, 2008 6:39 pm

Guys:

Do you know of any applications that can do the above on a group of C/C++ files?

I'm having a hard time trying to navigate my way into a project that seems to be conceived before templates existed (or were known by the team who built it).

You can see entire classes bulk-defined with macros like:
Code:

FOR_EACH_CLASS(DECLARE_PROXY_CLASS)

FOR_EACH_CLASS(DECLARE_ALIAS_CLASS)

FOR_EACH_CLASS(DEFINE_PROXY_CLASS)

FOR_EACH_CLASS(DEFINE_ALIAS_CLASS)

where FOR_EACH_CLASS is
Code:

#define FOR_EACH_CLASS(x) FOR_EACH_PACK_1(x) FOR_EACH_PACK_2(x) FOR_EACH_PACK_3(x) FOR_EACH_PACK_4(x)

where FOR_EACH_PACK_*(x) is
Code:

#define   FOR_EACH_PACK_*(x) X(UInt8) X(UInt16) X(UInt32) X(Boolean) yada yada ...

and where DECLARE_PROXY_CLASS(x) is
Code:

#define DECLARE_PROXY_CLASS(x) DECLARE_PROXY_HEADER_FOR(x) DECLARE_PROXY_MEMBERS_FOR(x) DECLARE_PROXY_METHODS_FOR(x)

where DECLARE_PROXY_HEADER_FOR(x) is
Code:

#define DECLARE_PROXY_HEADER_FOR(x) \
class Proxy##x \
{ \
public:

and...



well, do you get the idea? No? Good, I didn't either.



However, since the project is already closed (abandonware?), *and* GPL, that means I can scourge through the sources as I please. And that means, first and foremost, that I really need to understand what the $%&$@ is going on under these $%&$@ macros.

So, a tool that can expand only certain carefully selected macros & then write back the files to disk would be really useful in this case.

To be honest, I have no idea how to google for such a thing, so I'm asking you, the crew who actually know about C/C++. Do you know if there's such a tool? And where to get it?

- Izhido

#163622 - Cearn - Mon Oct 06, 2008 7:18 pm

Have you tried looking at the preprocessor output? That would show you what the end-result of all those macros would be. Granted, it would also expand all the other macros, possibly resulting in a very large mess of prototypes and code, but it could be a useful start. Temporarily commenting out other includes may help to keep the rest of the code relatively intact.

The -save-temps flag should dump the CPP output in .i files.

#163644 - sgeos - Tue Oct 07, 2008 3:50 pm

You could write some regular expressions to expand those macros if you don't want to run anything else through the preprocessor. Regular expressions can have bugs, so you would need to be careful if you go that route.