0

I want to replicate some code snippets using the C preprocessor. I know how to handle multi-line macros, but I am facing two difficulties:

  1. I didn't find a way to embed comments in the macro,

  2. The generated output doesn't have newlines.

E.g. here is what I would like to be able to do

#define Snippet \
// This is my snippet \
a= b + c;

(sort of).

Desired generated output:

// This is my snippet
a= b + c;

Do you have solutions for 1. and 2. ? Thanks in advance.

2
  • The "generated output" you show, is that the actual or expected output? Please try to create a minimal reproducible example to show us, and both the actual and expected "generated output". Commented Feb 6, 2019 at 8:30
  • @Someprogrammerdude: Expected. The macro definition, as written, cannot work. Commented Feb 6, 2019 at 8:38

2 Answers 2

1

The problem with the macro as you show it is because of how the compilation process works.

If you look at e.g. this C translation phase reference you will see that line-continuation happens in phase 2, then comments are replaced by space in phase 3, and finally preprocessing happens in phase 4.

That is, after phase 2 what you have is

#define Snippet // This is my snippet a= b+c;

Then after replacing comments in phase 3 the macro definition becomes empty.

The solution for comments is to use block comments using /* and */.

There is no solution for the line-continuation problem, as that's how it must work.

Sign up to request clarification or add additional context in comments.

3 Comments

Block comments do not work, they are swallowed by the preprocessor (thought I activate the Keep Comments option).
@YvesDaoust Why do you want to keep comments in the expanded macro? Comments won't be in the compiled executable anyway, and as per the specification must be removed in phase 3. What is the real problem you try to solve?
I want readable source code generated by assembling snippets, compilation comes later.
0

For those interested, I have solved as follows:

  1. The following macro allows to embed C++-style comments:

    #define Comment(Text) #/#/ Text
    
  2. I am adding a reserved character before the end of every line. After macro expansion, I turn it to a newline with a find/replace macro.

    #define Snippet \
    Comment(This is my snippet)@\
    a= b + c;
    

expands as

// This is my snippet@a= b + c;

and after substitution

// This is my snippet
a= b + c;

I am still looking for a way to have a symbol that would expand as a newline, though the current solution is already manageable.

3 Comments

#/#/ is not legal C because the stringify operator only works with macro arguments. If you meant to stringify, you could have written it as "/" "/" but that doesn't seem consistent with your output. Perhaps you intended to try to concatenate the two slashes using /##/? But that's not legal C either because // is not a token.
@rici: what matters to me is that it works with my compiler, and it does, I don't know why.
in that case, consider adding the tag for your compiler to the question so that we don't think you're looking for a general solution.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.