public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug preprocessor/100392] New: compiling result of "g++ -E -fdirectives-only" causes "error: stray ‘#’ in program" if no newline at EOF
@ 2021-05-03  9:59 mst.lo at arcor dot de
  2021-05-03 10:45 ` [Bug preprocessor/100392] [11/12 Regression] " jakub at gcc dot gnu.org
                   ` (4 more replies)
  0 siblings, 5 replies; 6+ messages in thread
From: mst.lo at arcor dot de @ 2021-05-03  9:59 UTC (permalink / raw)
  To: gcc-bugs

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

            Bug ID: 100392
           Summary: compiling result of "g++ -E -fdirectives-only" causes
                    "error: stray ‘#’ in program" if no newline at EOF
           Product: gcc
           Version: 11.1.1
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: preprocessor
          Assignee: unassigned at gcc dot gnu.org
          Reporter: mst.lo at arcor dot de
  Target Milestone: ---

> g++ --version
g++ (GCC) 11.1.1 20210428 (Red Hat 11.1.1-1)

works:

> echo '#include "test.h"' > test.cpp
> printf 'extern const int ksc5601_hangul_to_unicode[2350];\n' > test.h
> g++ -E -fdirectives-only test.cpp -o test.i
> g++ -c test.i

doesn't work:

> echo '#include "test.h"' > test.cpp
> printf 'extern const int ksc5601_hangul_to_unicode[2350];' > test.h
> g++ -E -fdirectives-only test.cpp -o test.i
> g++ -c test.i
In file included from test.cpp:1:
test.h:1:50: error: stray ‘#’ in program
    1 | extern const int ksc5601_hangul_to_unicode[2350];
      |                                                  ^
test.h:1:52: error: expected unqualified-id before numeric constant
    1 | extern const int ksc5601_hangul_to_unicode[2350];
      |      

in a draft of C++11, 2.2 Phases of translation [lex.phases] 1.2 says:

  A source file
  that is not empty and that does not end in a new-line character, or that ends
in a new-line character
  immediately preceded by a backslash character before any such splicing takes
place, shall be processed
  as if an additional new-line character were appended to the file.

similar language in
https://github.com/cplusplus/draft/blob/master/source/lex.tex#L97

wheres e.g. in a draft of C99, 5.1.1.2 Translation phases 2. says:

  A source file that is not empty shall end in a new-line character,
  which shall not be immediately preceded by a backslash character before any
such
  splicing takes place.

... so apparently different standards have different requirements here.

i believe this problem didn't occur when using GCC 10 with sccache or icecream,
which use "-E -fdirectives-only" to create a suitable input for distributed
compilation.

^ permalink raw reply	[flat|nested] 6+ messages in thread

* [Bug preprocessor/100392] [11/12 Regression] compiling result of "g++ -E -fdirectives-only" causes "error: stray ‘#’ in program" if no newline at EOF
  2021-05-03  9:59 [Bug preprocessor/100392] New: compiling result of "g++ -E -fdirectives-only" causes "error: stray ‘#’ in program" if no newline at EOF mst.lo at arcor dot de
@ 2021-05-03 10:45 ` jakub at gcc dot gnu.org
  2021-05-04 14:56 ` jakub at gcc dot gnu.org
                   ` (3 subsequent siblings)
  4 siblings, 0 replies; 6+ messages in thread
From: jakub at gcc dot gnu.org @ 2021-05-03 10:45 UTC (permalink / raw)
  To: gcc-bugs

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

Jakub Jelinek <jakub at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
     Ever confirmed|0                           |1
            Summary|compiling result of "g++ -E |[11/12 Regression]
                   |-fdirectives-only" causes   |compiling result of "g++ -E
                   |"error: stray ‘#’ in        |-fdirectives-only" causes
                   |program" if no newline at   |"error: stray ‘#’ in
                   |EOF                         |program" if no newline at
                   |                            |EOF
                 CC|                            |jakub at gcc dot gnu.org
   Target Milestone|---                         |11.2
   Last reconfirmed|                            |2021-05-03
             Status|UNCONFIRMED                 |NEW

--- Comment #1 from Jakub Jelinek <jakub at gcc dot gnu.org> ---
/opt/notnfs/gcc-bisect/obj/gcc/cc1plus.r11-205 -quiet -fdirectives-only -E
test.cpp -o test.ii; tail -n 2 test.ii 
extern const int ksc5601_hangul_to_unicode[2350];
# 2 "test.cpp" 2
Starting with r11-206-gb224c3763e018e8bdd0047b3eb283992fb655ce0
we ICEd on it, and starting with
r11-7093-gac16f4327fef5dfc288409371a61649253353ef7
we emit:
/opt/notnfs/gcc-bisect/obj/gcc/cc1plus.r11-7093 -quiet -fdirectives-only -E
test.cpp -o test.ii; tail -n 2 test.ii 
# 1 "test.h" 1
extern const int ksc5601_hangul_to_unicode[2350];# 2 "test.cpp" 2

^ permalink raw reply	[flat|nested] 6+ messages in thread

* [Bug preprocessor/100392] [11/12 Regression] compiling result of "g++ -E -fdirectives-only" causes "error: stray ‘#’ in program" if no newline at EOF
  2021-05-03  9:59 [Bug preprocessor/100392] New: compiling result of "g++ -E -fdirectives-only" causes "error: stray ‘#’ in program" if no newline at EOF mst.lo at arcor dot de
  2021-05-03 10:45 ` [Bug preprocessor/100392] [11/12 Regression] " jakub at gcc dot gnu.org
@ 2021-05-04 14:56 ` jakub at gcc dot gnu.org
  2021-05-12 13:15 ` cvs-commit at gcc dot gnu.org
                   ` (2 subsequent siblings)
  4 siblings, 0 replies; 6+ messages in thread
From: jakub at gcc dot gnu.org @ 2021-05-04 14:56 UTC (permalink / raw)
  To: gcc-bugs

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

Jakub Jelinek <jakub at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|NEW                         |ASSIGNED
           Assignee|unassigned at gcc dot gnu.org      |jakub at gcc dot gnu.org

--- Comment #2 from Jakub Jelinek <jakub at gcc dot gnu.org> ---
Created attachment 50749
  --> https://gcc.gnu.org/bugzilla/attachment.cgi?id=50749&action=edit
gcc12-pr100392.patch

Untested fix.

^ permalink raw reply	[flat|nested] 6+ messages in thread

* [Bug preprocessor/100392] [11/12 Regression] compiling result of "g++ -E -fdirectives-only" causes "error: stray ‘#’ in program" if no newline at EOF
  2021-05-03  9:59 [Bug preprocessor/100392] New: compiling result of "g++ -E -fdirectives-only" causes "error: stray ‘#’ in program" if no newline at EOF mst.lo at arcor dot de
  2021-05-03 10:45 ` [Bug preprocessor/100392] [11/12 Regression] " jakub at gcc dot gnu.org
  2021-05-04 14:56 ` jakub at gcc dot gnu.org
@ 2021-05-12 13:15 ` cvs-commit at gcc dot gnu.org
  2021-05-12 13:25 ` cvs-commit at gcc dot gnu.org
  2021-05-12 13:26 ` jakub at gcc dot gnu.org
  4 siblings, 0 replies; 6+ messages in thread
From: cvs-commit at gcc dot gnu.org @ 2021-05-12 13:15 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #3 from CVS Commits <cvs-commit at gcc dot gnu.org> ---
The master branch has been updated by Jakub Jelinek <jakub@gcc.gnu.org>:

https://gcc.gnu.org/g:c6b664e2c4c127025e076d8b584abe0976694629

commit r12-748-gc6b664e2c4c127025e076d8b584abe0976694629
Author: Jakub Jelinek <jakub@redhat.com>
Date:   Wed May 12 15:14:35 2021 +0200

    libcpp: Fix up -fdirectives-only preprocessing of includes not ending with
newline [PR100392]

    If a header doesn't end with a new-line, with -fdirectives-only we right
now
    preprocess it as
    int i = 1;# 2 "pr100392.c" 2
    i.e. the line directive isn't on the next line, which means we fail to
parse
    it when compiling.

    GCC 10 and earlier libcpp/directives-only.c had for this:
      if (!pfile->state.skipping && cur != base)
        {
          /* If the file was not newline terminated, add rlimit, which is
             guaranteed to point to a newline, to the end of our range.  */
          if (cur[-1] != '\n')
            {
              cur++;
              CPP_INCREMENT_LINE (pfile, 0);
              lines++;
            }

          cb->print_lines (lines, base, cur - base);
        }
    and we have the assertion
          /* Files always end in a newline or carriage return.  We rely on this
for
             character peeking safety.  */
          gcc_assert (buffer->rlimit[0] == '\n' || buffer->rlimit[0] == '\r');
    So, this patch just does readd the more less same thing, so that we emit
    a newline after the inline even when it wasn't there before.

    2021-05-12  Jakub Jelinek  <jakub@redhat.com>

            PR preprocessor/100392
            * lex.c (cpp_directive_only_process): If buffer doesn't end with
'\n',
            add buffer->rlimit[0] character to the printed range and
            CPP_INCREMENT_LINE and increment line_count.

            * gcc.dg/cpp/pr100392.c: New test.
            * gcc.dg/cpp/pr100392.h: New file.

^ permalink raw reply	[flat|nested] 6+ messages in thread

* [Bug preprocessor/100392] [11/12 Regression] compiling result of "g++ -E -fdirectives-only" causes "error: stray ‘#’ in program" if no newline at EOF
  2021-05-03  9:59 [Bug preprocessor/100392] New: compiling result of "g++ -E -fdirectives-only" causes "error: stray ‘#’ in program" if no newline at EOF mst.lo at arcor dot de
                   ` (2 preceding siblings ...)
  2021-05-12 13:15 ` cvs-commit at gcc dot gnu.org
@ 2021-05-12 13:25 ` cvs-commit at gcc dot gnu.org
  2021-05-12 13:26 ` jakub at gcc dot gnu.org
  4 siblings, 0 replies; 6+ messages in thread
From: cvs-commit at gcc dot gnu.org @ 2021-05-12 13:25 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #4 from CVS Commits <cvs-commit at gcc dot gnu.org> ---
The releases/gcc-11 branch has been updated by Jakub Jelinek
<jakub@gcc.gnu.org>:

https://gcc.gnu.org/g:b6ecd493886891f8150905a2fd3b7d54e88fc7be

commit r11-8404-gb6ecd493886891f8150905a2fd3b7d54e88fc7be
Author: Jakub Jelinek <jakub@redhat.com>
Date:   Wed May 12 15:14:35 2021 +0200

    libcpp: Fix up -fdirectives-only preprocessing of includes not ending with
newline [PR100392]

    If a header doesn't end with a new-line, with -fdirectives-only we right
now
    preprocess it as
    int i = 1;# 2 "pr100392.c" 2
    i.e. the line directive isn't on the next line, which means we fail to
parse
    it when compiling.

    GCC 10 and earlier libcpp/directives-only.c had for this:
      if (!pfile->state.skipping && cur != base)
        {
          /* If the file was not newline terminated, add rlimit, which is
             guaranteed to point to a newline, to the end of our range.  */
          if (cur[-1] != '\n')
            {
              cur++;
              CPP_INCREMENT_LINE (pfile, 0);
              lines++;
            }

          cb->print_lines (lines, base, cur - base);
        }
    and we have the assertion
          /* Files always end in a newline or carriage return.  We rely on this
for
             character peeking safety.  */
          gcc_assert (buffer->rlimit[0] == '\n' || buffer->rlimit[0] == '\r');
    So, this patch just does readd the more less same thing, so that we emit
    a newline after the inline even when it wasn't there before.

    2021-05-12  Jakub Jelinek  <jakub@redhat.com>

            PR preprocessor/100392
            * lex.c (cpp_directive_only_process): If buffer doesn't end with
'\n',
            add buffer->rlimit[0] character to the printed range and
            CPP_INCREMENT_LINE and increment line_count.

            * gcc.dg/cpp/pr100392.c: New test.
            * gcc.dg/cpp/pr100392.h: New file.

    (cherry picked from commit c6b664e2c4c127025e076d8b584abe0976694629)

^ permalink raw reply	[flat|nested] 6+ messages in thread

* [Bug preprocessor/100392] [11/12 Regression] compiling result of "g++ -E -fdirectives-only" causes "error: stray ‘#’ in program" if no newline at EOF
  2021-05-03  9:59 [Bug preprocessor/100392] New: compiling result of "g++ -E -fdirectives-only" causes "error: stray ‘#’ in program" if no newline at EOF mst.lo at arcor dot de
                   ` (3 preceding siblings ...)
  2021-05-12 13:25 ` cvs-commit at gcc dot gnu.org
@ 2021-05-12 13:26 ` jakub at gcc dot gnu.org
  4 siblings, 0 replies; 6+ messages in thread
From: jakub at gcc dot gnu.org @ 2021-05-12 13:26 UTC (permalink / raw)
  To: gcc-bugs

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

Jakub Jelinek <jakub at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|ASSIGNED                    |RESOLVED
         Resolution|---                         |FIXED

--- Comment #5 from Jakub Jelinek <jakub at gcc dot gnu.org> ---
Fixed for 11.2/12+.

^ permalink raw reply	[flat|nested] 6+ messages in thread

end of thread, other threads:[~2021-05-12 13:26 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-05-03  9:59 [Bug preprocessor/100392] New: compiling result of "g++ -E -fdirectives-only" causes "error: stray ‘#’ in program" if no newline at EOF mst.lo at arcor dot de
2021-05-03 10:45 ` [Bug preprocessor/100392] [11/12 Regression] " jakub at gcc dot gnu.org
2021-05-04 14:56 ` jakub at gcc dot gnu.org
2021-05-12 13:15 ` cvs-commit at gcc dot gnu.org
2021-05-12 13:25 ` cvs-commit at gcc dot gnu.org
2021-05-12 13:26 ` jakub at gcc dot gnu.org

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).