Bug 81746 - a blackslash at the end of a file is not removed by the preprocessor
Summary: a blackslash at the end of a file is not removed by the preprocessor
Status: UNCONFIRMED
Alias: None
Product: gcc
Classification: Unclassified
Component: preprocessor (show other bugs)
Version: 8.0
: P3 normal
Target Milestone: ---
Assignee: Not yet assigned to anyone
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2017-08-07 01:05 UTC by Vincent Lefèvre
Modified: 2023-08-19 04:21 UTC (History)
0 users

See Also:
Host:
Target:
Build:
Known to work:
Known to fail:
Last reconfirmed:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Vincent Lefèvre 2017-08-07 01:05:24 UTC
According to https://gcc.gnu.org/ml/gcc-patches/2007-04/msg00504.html each input file is defined "to end with a newline, whether or not the actual physical Unix input file did so". This is why the following works:

$ printf '#define FOO' | gcc -E -
# 1 "<stdin>"
# 1 "<built-in>"
# 1 "<command-line>"
# 31 "<command-line>"
# 1 "/usr/include/stdc-predef.h" 1 3 4
# 32 "<command-line>" 2
# 1 "<stdin>"
$

and

$ printf 'int main(void) { return 0; } //' | gcc -E -
# 1 "<stdin>"
# 1 "<built-in>"
# 1 "<command-line>"
# 31 "<command-line>"
# 1 "/usr/include/stdc-predef.h" 1 3 4
# 32 "<command-line>" 2
# 1 "<stdin>"
int main(void) { return 0; }
$

But the following doesn't work:

$ printf 'int main(void) { return 0; } \\' | gcc -E -
# 1 "<stdin>"
# 1 "<built-in>"
# 1 "<command-line>"
# 31 "<command-line>"
# 1 "/usr/include/stdc-predef.h" 1 3 4
# 32 "<command-line>" 2
# 1 "<stdin>"
int main(void) { return 0; } \
$

The backslash should have been removed, just like "//" was removed above. This yields an error if there is no explicit newline character at the end of the physical Unix input file:

$ printf 'int main(void) { return 0; } \\' | gcc -xc -std=c99 - && echo OK
<stdin>:1:30: error: stray ‘\’ in program
$ printf 'int main(void) { return 0; } \\\n' | gcc -xc -std=c99 - && echo OK
<stdin>:1:30: warning: backslash-newline at end of file
OK
$
Comment 1 Drea Pinski 2021-12-19 10:15:36 UTC
I found only MSVC treats \ at the EOF as the same as there a new line afterwards and ignores the \. ICC, GCC, and clang all handle it the same way.