Search code examples
cc-preprocessorsolaris

Is backslash-newline valid on #if and #elif?


This is related to Multi line preprocessor macros. I'm interested in the #if or #elif, and not the macro being defined. For example, is the following valid:

#if defined(X) || defined(Y) || \
    defined(Z)
...
#endif

The reason I'm asking is Clang, GCC and MSVC accept it, but some Sun tools on Solaris are complaining about it. GCC documents the behavior at 1.2 Initial processing ("backslash-newline" and "continued lines"), but Sun tools like DBX encounter an internal error.


Solution

  • They're valid, because the backslashes before newlines are removed in phase 2 before the preprocessing is done in phase 4.

    Phase 2

    1. Whenever backslash appears at the end of a line (immediately followed by the newline character), both backslash and newline are deleted, combining two physical source lines into one logical source line. This is a single-pass operation: a line ending in two backslashes followed by an empty line does not combine three lines into one.

    ...

    Phase 4

    1. Preprocessor is executed.

    http://en.cppreference.com/w/c/language/translation_phases