C macros with newlines in the generated code

I need a macro that extends over several lines of code. For instance:

#define foo(...)
foo(something, something_else, ...)
...

Must be converted to:

something
something_else
...

And not in:

something something_else ...

Also, if you are wondering why I need such a thing. I need to generate such code, a new line is part of the syntax of the built-in assembly.

_asm
{
    mov eax, 3
    div 5
}

I am interested in any form of achieving this goal, so all suggestions are welcome.

Just an idea after reading this answer . Is it possible to have a macro for a new line and call foo(something, NL, something_else, NL, ...)?


I am also interested in the variational version, but a simpler version may also help.

+4
source share
1 answer

It might help a little, just give you an idea.

    #include "stdio.h"
    #define EXPAND_MULTIPLE_LINES(X, Y, Z) /*
    */ X /*
    */ Y /*
    */ Z

    int main()
    {
        int X, Y, Z;
        EXPAND_MULTIPLE_LINES(X, Y, Z);
        return 0;
    }

use gcc -E -CC testMacro.c

,

     # 844 "/usr/include/stdio.h" 3 4

     # 2 "testMacro.c" 2
     # 12 "testMacro.c"
     int main()

     {

          int X, Y, Z;
          /*

          */ X /*
          */ Y /*
          */ Z;
          # 18 "testMacro.c"
          return 0;
     }
0

Source: https://habr.com/ru/post/1599836/


All Articles