views:

461

answers:

6

Hello,

I've got a pretty complicated macro inside my (unmanaged) C++ code. Is there any way to expand macros in VS debugger? Or maybe there is another way to debug macros there?

F.e. I'd like to place a breakpoint inside it.

(Yes, I know macros are bad.)

A: 

Macros are not that bad if used wisely. You can debug macros with VS macros IDE. Can read about it here: debugging macros

Yaroslav Yakovlev
Isn't that about VS macros and not C++ macros? I am not talking about macros to automate actions in VS.
liori
My bad, sorry, didn`t read carefully.
Yaroslav Yakovlev
+2  A: 

The compiler expands macros nicely. Get a compilation listing with the macros expanded.

Insert the expanded macro into your code. Recompile and you should be able to step through it, to do a simple test.

Generally when I am building a complicated macro, I hard code it first, and test it. Then I turn it into a macro.

For 2008, this may be of help

EvilTeach
I really couldn't find how to expand the macros automatically. I know I can do that by hand, but I expect that IDE can do that... like GDB's expand command.
liori
you can run a C preprocessor on a source file to get all macros expanded.
n0rd
When running the preprocessor separately be aware that it may have a different idea of what macros/constants are predefined (specifically, platform/compiler-version-specific ones). I've been bitten by this!
j_random_hacker
@j, hence why you simply extract it from the compilation listing.That avoids your issue.
EvilTeach
+4  A: 

Go to either project or source file properties by right-clicking and going to "Properties". Under Configuration Properties->C/C++->Preprocessor, set "Generate Preprocessed File" to either with or without line numbers, whichever you prefer. This will show what your macro expands to in context. If you need to debug it on live compiled code, just cut and paste that, and put it in place of your macro while debugging.

Jim Buck
Have I said that the macros are very complicated? I'd like to expand one or two layers only, not all macros (otherwise it is plainly unreadable)... while debugging.
liori
Yeah, it sucks, but there is no good solution that I know of. I usually have to hack around a bit myself when doing this, either manually replacing the code myself just to get the layers I want or going for the whole thing via a fully preprocessed file.
Jim Buck
A: 

Counter an evil with an eviler method. Expand one instance of the macro by hand, until you finish debugging, or cpp utility can help you to expand all macros, but I'm not sure if there is Windows version of it.

Cem Kalyoncu
A: 

Have I said that the macros are very complicated? I'd like to expand one or two layers only, not all macros (otherwise it is plainly unreadable)... while debugging

If there are not way too many macros you don't want to expand, then you can simply #undef them before the snippet you want to debug. Then compile with the [Generate Preprocessed File] as mentioned above. The compilation will fail but you will get a usable snipped in the preprocessed output with the correct expansions. Copy-paste, remove the #undef-s and debug.

Sadly, your best friends for dealing with C-style macros are inline, template and a good optimizing compiler.

brittle
I am trying to grok existing code, not rewrite it. So inlines and templates will not help me here.
liori
A: 

Use inline functions (force inlining if you really mean it) and stick to it! This will be more efficient (get only inlined if it brings performance if you optimize for perf) and maintainable. You loose much by using macros: type checking & safety.

Macros do not exist for the compiler, they only do for the preprocessor. This one makes text replacements without looking for side effects. So macros are actually never called and you cannot debug into them. No matter how you code a macro, misuse is always possible. Like in this example:

#define SQUARE(a) ((a)*(a))

Note already the ( `)´ to protect against replacements like SQUARE(3+2). But they do not protect against:

int a = 1;
int b = SQUARE(++a);

One would expect it to be 4 but you end up with 6 which is not really square. As it expands to int b = (++a)*(++a); You wouldn't have this trouble with inlined functions.

Note that the implementation of an inlined function has to be visible for the compiler at the places where you use it. Also debugging will be weird to the novice user: as there are many instances of an inlined function, you'll get a huge list of methods if you want to move the execution point to another place for example.

jdehaan
I am well informed about the bad and good sides of macros. -1
liori