Understanding Macro Heavy Code

Why read this?
It’s tough to make sense of code full of MACROs. Here is a solution to see your code with the MACROs replaced.

I have recently worked with many open-source projects, which made it evident that developers love using macros. How did I deal with it?

MACROS can make it difficult to read code. For example, here is an extract of LwIP:

We are going to create some “magic” to easily lets you see any code where MACROS’ definitions are replaced by their content, something like this:

How to?

1st, Most Important: Make sure you have the “Save Macro Expansion Set” checked in the project setup.
It’s under Project >> Configure Project >> C++ >> Options.

2nd, create a file named RemoveMacro.pl with the following PERL code:

use Understand;
die “Must be run from within Understand” if !Understand::Gui::active();
die “Must be run with a db open” if !Understand::Gui::db();
my $db = Understand::Gui::db();
my $filename = Understand::Gui::filename();
my $fileent = $db->lookup($filename,”File”);
if ($fileent) {
my $lexer = $fileent->lexer(1,8,1,1);
foreach my $lexeme ($lexer->lexemes()) {
my $lexeme_text = $lexeme->text();

3rd, Once you have created the script, Follow this 2-minute video’s instructions to set it up in your Understand Environment as a “User Tool”.

Note: (from Ken)

Another way to see macro definitions in Understand is to hover over a macro name anywhere you see it. Understand will show you its definition at that point in the code.

This is just one at a time, so it’s not as readable in total as the expand in source approach described in this article. I’m just adding it for completeness.