public inbox for gcc-patches@gcc.gnu.org
 help / color / mirror / Atom feed
From: Jakub Jelinek <jakub@redhat.com>
To: Nathan Sidwell <nathan@acm.org>, Jason Merrill <jason@redhat.com>,
	"Joseph S. Myers" <joseph@codesourcery.com>,
	Marek Polacek <polacek@redhat.com>,
	David Malcolm <dmalcolm@redhat.com>
Cc: gcc-patches@gcc.gnu.org
Subject: [PATCH] libcpp: Fix up -fdirectives-only preprocessing of includes not ending with newline [PR100392]
Date: Wed, 5 May 2021 10:41:50 +0200	[thread overview]
Message-ID: <20210505084150.GM1179226@tucnak> (raw)

Hi!

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.

Bootstrapped/regtested on x86_64-linux and i686-linux, ok for trunk/11.2?

2021-05-05  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.

--- libcpp/lex.c.jj	2021-02-03 23:17:05.121659691 +0100
+++ libcpp/lex.c	2021-05-04 16:46:54.560272231 +0200
@@ -4756,7 +4756,18 @@ cpp_directive_only_process (cpp_reader *
 	}
 
       if (buffer->rlimit > base && !pfile->state.skipping)
-	cb (pfile, CPP_DO_print, data, line_count, base, buffer->rlimit - base);
+	{
+	  const unsigned char *limit = buffer->rlimit;
+	  /* If the file was not newline terminated, add rlimit, which is
+	     guaranteed to point to a newline, to the end of our range.  */
+	  if (limit[-1] != '\n')
+	    {
+	      limit++;
+	      CPP_INCREMENT_LINE (pfile, 0);
+	      line_count++;
+	    }
+	  cb (pfile, CPP_DO_print, data, line_count, base, limit - base);
+	}
 
       _cpp_pop_buffer (pfile);
     }
--- gcc/testsuite/gcc.dg/cpp/pr100392.c.jj	2021-05-04 16:50:17.491025563 +0200
+++ gcc/testsuite/gcc.dg/cpp/pr100392.c	2021-05-04 16:48:37.683130548 +0200
@@ -0,0 +1,5 @@
+/* PR preprocessor/100392 */
+/* { dg-do compile } */
+/* { dg-options "-save-temps -fdirectives-only" } */
+
+#include "pr100392.h"
--- gcc/testsuite/gcc.dg/cpp/pr100392.h.jj	2021-05-04 16:50:14.026063924 +0200
+++ gcc/testsuite/gcc.dg/cpp/pr100392.h	2021-05-04 16:50:05.969153124 +0200
@@ -0,0 +1,4 @@
+/* PR preprocessor/100392 */
+
+/* No newline after ; below.  */
+int i = 1;
\ No newline at end of file

	Jakub


                 reply	other threads:[~2021-05-05  8:42 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20210505084150.GM1179226@tucnak \
    --to=jakub@redhat.com \
    --cc=dmalcolm@redhat.com \
    --cc=gcc-patches@gcc.gnu.org \
    --cc=jason@redhat.com \
    --cc=joseph@codesourcery.com \
    --cc=nathan@acm.org \
    --cc=polacek@redhat.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).