https://gcc.gnu.org/bugzilla/show_bug.cgi?id=119753

            Bug ID: 119753
           Summary: gcc -E is not POSIX-compliant
           Product: gcc
           Version: 14.2.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: preprocessor
          Assignee: unassigned at gcc dot gnu.org
          Reporter: love4boobies at yahoo dot com
  Target Milestone: ---

Hi,

According to POSIX:

---
If the -E option is specified, the standard output shall be a text file that
represents the results of the preprocessing stage of the language; it may
contain extra information appropriate for subsequent compilation passes and
shall contain at least one line with the format:

"# %d \"%s\"\n", <line>, <pathname>

for each file processed as a result of a #include directive, unless no other
output generated from that file is present in the output, where line is a line
number and pathname is the pathname used to open the file.
---

However, GCC prints lines that contain more things after the pathname:

# 931 "/usr/include/stdio.h" 3 4

Given that POSIX does not mandate the -M family of options, portable makefiles
sometimes parse these lines to extract the necessary information for
automatically generating dependency files using utilities such as sed. While it
is entirely possible to do so with GCC's current output by doing something
like:

gcc -E test.c | LC_ALL=C sed -En '/^# [1-9][[:digit:]]* /s/.*"([^"]*)".*/\1/p'
| LC_ALL=C sort -u | tr '\n' ' ' >>test.d

a makefile that is unaware of GCC's behavior might do something like:

gcc -E test.c | LC_ALL=C sed -En '/^# [1-9][[:digit:]]* "([^"]*)"$/s//\1/p' |
LC_ALL=C sort -u | tr '\n' ' ' >>test.d

or other variations, resulting in not all of the dependencies being listed
inside that file. This can lead to silent issues.

Proposed solutions:

1. If those numbers at the end are unimportant, maybe just strip them from the
output. I'm not even sure what they represent.
2. If they are important, change -E to match POSIX and have GCC's current
output be available via either a new option or an extra one used in conjunction
with -E.
3. Continue using the current behavior but switch to POSIX behavior when the
POSIXLY_CORRECT environment variable is defined; this is in the spirt of other
GNU utilities.

Cheers,
Bogdan

Reply via email to