3

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:

Can you turn off (specific) compiler warnings for any header included from a specific location?

This is a question that I'd like a solution myself. I thought 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:

Is there a way to do a #define inside of another #define?

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.

Community
  • 1
  • 1
Vitor Py
  • 5,145
  • 4
  • 39
  • 62
  • 1
    Your `#define` needs an identifier. And what purpose would the whitespace serve anyway? You never see the expanded code. – GManNickG Jul 22 '10 at 23:32
  • @GMan: He probably just wants the macro itself to be more readable. – CookieOfFortune Jul 22 '10 at 23:32
  • 3
    Can you give some background on why you want it to do that? Is there a particular macro you're trying to write? Maybe there's a different solution to your problem. – Matt Curtis Jul 22 '10 at 23:33
  • @CookieOfFortune: But then he can pretty much do what he has now, C++ doesn't care about whitespace. Just needs the backslash at the end of the line to indicate the macro continues on the next line, which he already has. – GManNickG Jul 22 '10 at 23:33
  • 2
    There is no portable or standard way to do this. Newlines are not significant after preprocessing directives are evaluated, so there is no reason for them to be retained. – James McNellis Jul 22 '10 at 23:35
  • @Matt Curtis I'll improve the question. – Vitor Py Jul 22 '10 at 23:35
  • @James McNellis I believed there's no standard way to do this. As I stated, I'd be happy with a platform specific hack. – Vitor Py Jul 22 '10 at 23:45
  • @Vitor: I saw; hence, I posted that as a comment, not an answer. :-) – James McNellis Jul 22 '10 at 23:47

1 Answers1

6

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).

mbauman
  • 30,958
  • 4
  • 88
  • 123
  • 1
    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.J. Jul 23 '10 at 00:03
  • @C Johnson: Unless the "maintenance programmer" in this case is yourself. – Billy ONeal Jul 23 '10 at 02:12
  • +1 for inline .... I didn't like last "d" in #defined above in your answer ;-) – KedarX Jul 23 '10 at 13:16