2

I am trying to learn preprocessor tricks that I found not so easy (Can we have recursive macros?, Is there a way to use C++ preprocessor stringification on variadic macro arguments?, C++ preprocessor __VA_ARGS__ number of arguments, Variadic macro trick, ...).

I know the -E option to see the result of the preprocessor whole pass but I would like to know, if options or means exist to see the result step by step. Indeed, sometimes it is difficult to follow what happens when a macro calls a macro that calls a macro ... with the mechanism of disabling context, painting blue ... In brief, I wonder if a sort of preprocessor debugger with breakpoints and other tools exists.

(Do not answer that this use of preprocessor directives is dangerous, ugly, horrible, not good practices in C, produces unreadable code ... I am aware of that and it is not the question).

chrisemb
  • 79
  • 5
Stef1611
  • 1,978
  • 2
  • 11
  • 30
  • If you haven't already, check out https://github.com/pfultz2/Cloak if you're interested in high level macros. For more, look at P99 and BoostPP. – okovko Jan 12 '19 at 09:33
  • @okovko. Thanks for the information. This is what I try to understand. See the answer of Paul FulTZ II (https://stackoverflow.com/questions/12447557/can-we-have-recursive-macros – Stef1611 Jan 12 '19 at 09:39
  • You'll find your answer in the link I gave you. That's Paul Fultz II's macro tutorial on his github page :P Oops, here is the link I meant to give you. It is the explanation: https://github.com/pfultz2/Cloak/wiki/C-Preprocessor-tricks,-tips,-and-idioms – okovko Jan 12 '19 at 09:42

3 Answers3

2

Yes, this tool exists as a feature of Eclipse IDE. I think the default way to access the feature is to hover over a macro you want to see expanded (this will show the full expansion) and then press F2 on your keyboard (a popup appears that allows you to step through each expansion).

When I used this tool to learn more about macros it was very helpful. With just a little practice, you won't need it anymore.

In case anyone is confused about how to use this feature, I found a tutorial on the Eclipse documentation here.

okovko
  • 1,851
  • 14
  • 27
  • Does it exist such option with codeblocks ? Otherwise, I am going to install Eclipse. – Stef1611 Jan 12 '19 at 09:36
  • I don't know about any other IDEs, but when I was looking for this feature years ago, Eclipse was the only place I found it. – okovko Jan 12 '19 at 09:37
  • It only works if the macro is correct. Otherwise you will see only the error (not very friendly and explanatory). You cant "debug" the macros. – 0___________ Jan 12 '19 at 10:15
  • That is simply not true. It shows all possible expansions until an error is reached. Because of this, the tool is extremely friendly and explanatory. I would appreciate it if you removed your down vote, since your criticism is misinformed. – okovko Jan 12 '19 at 10:29
  • @okovko. Great. I installed Eclipse and it works on a simple example as you describe. (`#define ABC BCD #define BCD 1 main() {int a=ABC;}`). Thanks a lot, I validate your answer. – Stef1611 Jan 12 '19 at 10:34
  • Sorry, I just read your last comments on error in macro. I was focused on Eclipse installation. They are very interesting. First, I am very happy because I could understand what happens with complex expansion as I mentioned in my question. I will try latter with error, when I will understand these complex expansions. – Stef1611 Jan 12 '19 at 10:41
  • I don't know about Eclipse 2019, but when I used it years ago, I was able to expand macros until no further expansions were possible. So if you have a complex macro with many expansions, and one of them is incorrect, you will be able to expand until you reach your error, where the expansion will (probably, depending on your mistake) end. – okovko Jan 12 '19 at 10:45
  • I am going to try on Eclipse Oxygen. And after, I will install others versions and I will test it. Just, let me time. But please, do not be so critical to each other with down vote. I could tell you that, all of you help me a lot. I appreciate your answer/comments because none of you said do not do that, it is not good pratices ... as it happens often. – Stef1611 Jan 12 '19 at 10:55
  • I have done simple tests and I do not claim to be exhaustive. In some case, I could say that the eclipse tool could help to debug some errors even if it is not a cpp debugger. For example `#define ABC BCD #define BCD EFG #define EFF 1 main() {int a=ABC;}`, it informs you that symbol EFG could not be resolved and with F2 trick, you learn that the expansion has only two steps which are ABC -> BCD -> EFG. – Stef1611 Jan 12 '19 at 11:36
  • Yes, you will get as many expansions as possible. In complex macro expansions with conditionals, you may expand to the wrong condition for example, and you will be able to see that the error occurred when you expand to the incorrect macro. It will continue to expand past where the error occurred, but you can figure out where the error is by stepping through the expansions. – okovko Jan 12 '19 at 11:44
1

This answer to another question is relevant.

When you do weird preprocessor tricks (which are legitimate) it is useful to ask the compiler to generate the preprocessed form (e.g. with gcc -C -E if using GCC) and look into that preprocessed form.

In practice, for a source file foo.c it makes (sometimes) sense to get its preprocessed form foo.i with gcc -C -E foo.c > foo.i and look into that foo.i.

Sometimes, it even makes sense to get that foo.i without line information. The trick here (removing line information contained in lines starting with #) would be to do:

gcc -C -E foo.c | grep -v '^#' > foo.i

Then you could indent foo.i and compile it, e.g. with gcc -Wall -c foo.i; you'll get error locations in the preprocessed file and you could understand how you got that and go back to your preprocessor macros (or their invocations).

Remember that the C preprocessor is mostly a textual transformation working at the file level. It is not possible to macro-expand a few lines in isolation (because prior lines might have played with #if combined with #define -perhaps in prior #include-d files- or preprocessor options such as -DNDEBUG passed to gcc or g++). On Linux see also feature_test_macros(7)

A known example of expansion which works differently when compiled with or without -DNDEBUG passed to the compiler is assert. The meaning of assert(i++ > 0) (a very wrong thing to code) depends on it and illustrates that macro-expansion cannot be done locally (and you might imagine some prior header having #define NDEBUG 1 even if of course it is poor taste).

Another example (very common actually) where the macro expansion is context dependent is any macro using __LINE__ or __COUNTER__ ...

NB. You don't need Eclipse for all that, just a good enough source code editor (my preference is emacs but that is a matter of taste): for the preprocessing task you can use your compiler.

Basile Starynkevitch
  • 223,805
  • 18
  • 296
  • 547
  • 1
    It looks like the question is specifically asking for step by step macro expansion, and the question also mentions the -E option. Is there any command line tool to expand a macro step by step? It would be nice to find one. – okovko Jan 12 '19 at 11:29
  • 1
    Well it's definitely possible, I just don't think anyone has made it yet. For example, it is implemented in Eclipse IDE (see my answer). Eclipse allows you to see each expansion step by step, instead of just the final result, like you get with the -E flag. – okovko Jan 12 '19 at 11:32
  • Well, it does work in the general case. In fact in the comments to my answers, the person who asked the question confirmed that it works, even when there is an error. – okovko Jan 12 '19 at 11:36
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/186593/discussion-between-okovko-and-basile-starynkevitch). – okovko Jan 12 '19 at 11:37
  • Beginner question on Eclipse IDE : Does Eclipse use some options of cpp to catch these intermediate steps or is there a "rewriting" of cpp in Eclipse ? In the second case, how could we be sure that it will give the same output than cpp ? – Stef1611 Jan 12 '19 at 11:54
  • The C Preprocessor is defined by the C standard, so whatever Eclipse does follows the standard. Unfortunately, the C standard does a poor job defining the behavior of the preprocessor, so there are some implementation differences between different compilers. Fortunately, these differences are rare and typically the major compiler vendors normalize the differences over time (you will not see many differences between gcc and clang). – okovko Jan 12 '19 at 12:14
0

The only way to see what is wrong with your macro is to add the option which will keep the temporary files when compilation completes. For gcc it is -save-temps option. You can open the .i file and the the expanded macros.

IDE indexers (like Eclipse) will not help too much. They will not expand (as other answer states) the macros until the error occures. enter image description here

0___________
  • 60,014
  • 4
  • 34
  • 74
  • But I do not have all the steps as with F2 in eclipse IDE. It is what I see when I use -E option, isn't it ? – Stef1611 Jan 12 '19 at 10:28
  • It is But you do not have any steps if the macro is erratic. Eclipse viewer is only capable (as it uses the internal indexer) to see the **correct** macros. – 0___________ Jan 12 '19 at 10:31
  • You should try answering the question to improve your answer. – okovko Jan 12 '19 at 10:31
  • @okovko you should not post the false answers. Please show me any partially expanded erratic macro in the macro view (F2) in the eclipse. – 0___________ Jan 12 '19 at 10:44
  • And if there are errors, with the -save-temps, what happens ? Is it possible to see, in the output (.i file), the first expansion steps until error happens ? – Stef1611 Jan 12 '19 at 10:44
  • No you will the effect of the preprocessor work. There is no way to "debug" the preprocessor - you may write your own one. – 0___________ Jan 12 '19 at 10:46
  • @P__J__ well we're talking about your answer right now. The question is to "see the result step by step" so you're the one posting a "false answer," since you offer no solution. Anyways, I have found a tutorial on how to use this feature of eclipse. [Here](https://eclipsebook.in/c-cpp-development/reading-code/macro-expansions/). I think you are just confused. – okovko Jan 12 '19 at 10:49
  • @okovko try to change this macro to to give an error and then continue the discussion. The debug is needed when something **is not working** – 0___________ Jan 12 '19 at 11:00
  • @P__J__ hover your mouse over the yellow pop up and press F2. You should get a new pop up with arrows to go back and forth. If you do not see it, then perhaps you are correct that Eclipse will only allow stepping through macro expansions that are not ill formed. Just like a debugger will not allow you to step through code that is not compiling. If you instead made a logic error in your macros, rather than a compilation error, you could step through your macros. – okovko Jan 12 '19 at 12:16