views:

146

answers:

1

Possible Duplicate:
How can I make the preprocessor insert linebreaks into the macro expansion result?

#define IDENTIFIER { /*new line here*/\
            my_multiline(); /*new line here*/\
            macro(); /*new line here*/\
        } /*new line here*/

Can I force the C pre processor to generate a new line in it's expansion? I don't believe there's a standard way to do this but I wouldn't mind platform specific hacks for Visual C++ (2008) and gcc.

I'm not interested in doing this through m4 or a separate pre processor.

Why I'm asking this:

It's more or less just curiosity. Since it's a hack I'm not going to try this in production but I'd like to know if I'm able to do this anyway. A few days ago I stumbled into this question:

http://stackoverflow.com/questions/3272599/can-you-turn-off-specific-compiler-warnings-for-any-header-included-from-a-spec

This is a question that I'd like a solution myself. I tought about creating a macro for including a header, but with correct pragmas to disable a warning before the including, include the header I'd like to turn warnings off, enabling the warning after the include.

Of course, I could create a script to generate mock includes with the pragma for the warning and the include.

My first problem was that since #include is a pre processor directive a macro would be somewhat useless to generate it. But then I found this answer:

http://stackoverflow.com/questions/3314032/is-there-a-way-to-do-a-define-inside-of-another-define/3314055#3314055

With this I believe I can generate the special include macro if I'm able to force the compiler to generate newlines in it's expansion.

I'm at home right now but I'll post my code tomorrow when I'm at work.

+5  A: 

If you're simply aiming to make the code more readable in a debugger, you could use inline functions instead of #defined macros. Unfortunately, the inline keyword is a C99 addition to C and only supported by proprietary extensions under ANSI C (like MSVC).

#if __STDC_VERSION__ < 199901L
# if defined(_MSC_VER)
#  define inline __inline
# elseif defined(__GNUC__)
#  define inline __inline__
# endif
#endif

static inline void IDENTIFIER() {
    my_multiline();
    macro();
}

This has the additional benefit of being type-checked (and generally less prone to error).

Matt B.
Don't go macro happy, you will regret it. Wait a minute. You won't regret it, but the maintenance programmers after you will curse the day you ever laid fingers on the code. :)
C Johnson
@C Johnson: Unless the "maintenance programmer" in this case is yourself.
Billy ONeal
+1 for inline .... I didn't like last "d" in #defined above in your answer ;-)
Kedar