简体   繁体   中英

C macros with new lines in generated code

I need macro which expands into multiple lines of code. For example:

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

Should be converted into:

something
something_else
...

And not into:

something something_else ...

Also if you wonder why I need such thing. I need to generate such code, new line is part of inline assembly syntax.

_asm
{
    mov eax, 3
    div 5
}

I'm interested in any form of achieving this, so all suggestions are welcome.

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


I'm also more interested into variadic version but knowing simpler version may also help.

This may provide a little bit of help , to 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

Output like

     # 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;
     }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM