Yes, your expected result is the one required by the C and C++ standards. The backslash simply escapes the newline, i.e. the backslash-newline sequence is deleted.
GCC 4.2.1 from my OS X installation gives the expected result, as does Clang. Furthermore, adding a #define
to the beginning and testing with
#define main(){\
return 0;
}
main()
yields the correct result
}
{return 0;
Perhaps gcc -E
does some extra processing after preprocessing and before outputting it. In any case, the line break seen by the rest of the preprocessor seems to be in the right place. So it's a cosmetic bug.
UPDATE: According to the GCC FAQ, -E
(or the default setting of the cpp
command) attempts to put output tokens in roughly the same visual location as input tokens. To get "raw" output, specify -P
as well. This fixes the observed issues.
Probably what happened:
- In preserving visual appearance, tokens not separated by spaces are kept together.
- Line splicing happens before spaces are identified for the above.
- The
{
and return
tokens are grouped into the same visual block.
0
follows a space and its location on the next line is duly noted.
PLUG: If this is really important to you, I have implemented my own preprocessor with correct implementation of both raw-preprocessed and whitespace-preserving "pretty" modes. Following this discussion I added line splices to the preserved whitespace. It's not really intended as a standalone tool, though. It's a testbed for a compiler framework which happens to be a fully compliant C++11 preprocessor library, which happens to have a miniature command-line driver. (The error messages are on par with GCC, or Clang, sans color, though.)
gcc -E
(or equivalent)."foo" /* bar */ "baz"
is equivalent to"foobaz"
). The only thing I think the backslash-after-new-line is really useful for is macro definitions (where it lets you put the definition across multiple lines).