[preprocessor/91639] #includes at EOF
authorNathan Sidwell <nathan@acm.org>
Thu, 5 Sep 2019 11:23:48 +0000 (11:23 +0000)
committerNathan Sidwell <nathan@gcc.gnu.org>
Thu, 5 Sep 2019 11:23:48 +0000 (11:23 +0000)
https://gcc.gnu.org/ml/gcc-patches/2019-09/msg00280.html
libcpp/
PR preprocessor/91639
* directives.c (do_include_common): Tell lexer we're a #include.
* files.c (_cpp_stack_file): Lexer will have always incremented.
* internal.h (struct cpp_context): Extend in_directive's
semantics.
* lex.c (_cpp_lex_direct): Increment line for final \n when lexing
for an ISO #include.
* line-map.c (linemap_line_start): Remember if we overflowed.

gcc/testsuite/
PR preprocessor/91639
* c-c++-common/cpp/pr91639.c: New.
* c-c++-common/cpp/pr91639-one.h: New.
* c-c++-common/cpp/pr91639-two.h: New.

From-SVN: r275402

gcc/testsuite/ChangeLog
gcc/testsuite/c-c++-common/cpp/pr91639-one.h [new file with mode: 0644]
gcc/testsuite/c-c++-common/cpp/pr91639-two.h [new file with mode: 0644]
gcc/testsuite/c-c++-common/cpp/pr91639.c [new file with mode: 0644]
libcpp/ChangeLog
libcpp/directives.c
libcpp/files.c
libcpp/internal.h
libcpp/lex.c
libcpp/line-map.c

index ce3a385d3edcb08f6b9135bae7c4a6b3f67ad474..cdaa2705287984beee4f36ea305f580798b50134 100644 (file)
@@ -1,3 +1,10 @@
+2019-09-05  Nathan Sidwell  <nathan@acm.org>
+
+       PR preprocessor/91639
+       * c-c++-common/cpp/pr91639.c: New.
+       * c-c++-common/cpp/pr91639-one.h: New.
+       * c-c++-common/cpp/pr91639-two.h: New.
+
 2019-09-05  Richard Sandiford  <richard.sandiford@arm.com>
 
        PR middle-end/91577
diff --git a/gcc/testsuite/c-c++-common/cpp/pr91639-one.h b/gcc/testsuite/c-c++-common/cpp/pr91639-one.h
new file mode 100644 (file)
index 0000000..141f6a7
--- /dev/null
@@ -0,0 +1,2 @@
+one
+#include "pr91639-two.h"
diff --git a/gcc/testsuite/c-c++-common/cpp/pr91639-two.h b/gcc/testsuite/c-c++-common/cpp/pr91639-two.h
new file mode 100644 (file)
index 0000000..f719efd
--- /dev/null
@@ -0,0 +1 @@
+two
diff --git a/gcc/testsuite/c-c++-common/cpp/pr91639.c b/gcc/testsuite/c-c++-common/cpp/pr91639.c
new file mode 100644 (file)
index 0000000..6da0b66
--- /dev/null
@@ -0,0 +1,9 @@
+/* PR91639 Line markers for an end-of-file #include   */
+/* { dg-do preprocess } */
+/* { dg-additional-options -Wno-pedantic } */
+/* { dg-additional-files {pr91639-one.h pr91639-two.h} } */
+
+#include "pr91639-one.h"
+main
+
+/* { dg-final { scan-file pr91639.i "# 1 \"\[^\n\"\]*pr91639-one.h\" 1\none\n# 1 \"\[^\n\"\]*pr91639-two.h\" 1\ntwo\n# 3 \"\[^\n\"\]*pr91639-one.h\" 2\n# 7 \"\[^\n\"\]*pr91639.c\" 2\nmain\n" } } */
index 6791b3644e525fabbc145981ef1e823d0ca5c05a..c418d75c950abf6bd1fcf7f736a41ae6396f9fc8 100644 (file)
@@ -1,3 +1,14 @@
+2019-09-05  Nathan Sidwell  <nathan@acm.org>
+
+       PR preprocessor/91639
+       * directives.c (do_include_common): Tell lexer we're a #include.
+       * files.c (_cpp_stack_file): Lexer will have always incremented.
+       * internal.h (struct cpp_context): Extend in_directive's
+       semantics.
+       * lex.c (_cpp_lex_direct): Increment line for final \n when lexing
+       for an ISO #include.
+       * line-map.c (linemap_line_start): Remember if we overflowed.
+
 2019-09-03  Ulrich Weigand  <uweigand@de.ibm.com>
 
        * directives.c: Remove references to spu from comments.
index 29d21ed9fdf0d6352343ba5c15b3e21f9ae89926..1c6b31e143bb6768454c9df2dc8dbe96a19757c1 100644 (file)
@@ -818,6 +818,10 @@ do_include_common (cpp_reader *pfile, enum include_type type)
      callback can dump comments which follow #include.  */
   pfile->state.save_comments = ! CPP_OPTION (pfile, discard_comments);
 
+  /* Tell the lexer this is an include directive -- we want it to
+     increment the line number even if this is the last line of a file.  */
+  pfile->state.in_directive = 2;
+
   fname = parse_include (pfile, &angle_brackets, &buf, &location);
   if (!fname)
     goto done;
index ee31aff606d8a063850b20a2461d7c7d8c3e8ad8..aa77dc74e9427e02b5fddeb3e181f86fc2223c1f 100644 (file)
@@ -938,25 +938,16 @@ _cpp_stack_file (cpp_reader *pfile, _cpp_file *file, include_type type,
   pfile->mi_valid = true;
   pfile->mi_cmacro = 0;
 
-  /* Compensate for the increment in linemap_add that occurs when in
-     do_file_change.   In the case of a normal #include, we're
-     currently at the start of the line *following* the #include.  A
-     separate location_t for this location makes no sense (until we do
-     the LC_LEAVE), and complicates LAST_SOURCE_LINE_LOCATION.  This
-     does not apply if we found a PCH file (in which case linemap_add
-     is not called) or we were included from the command-line.  In the
-     case that the #include is the last line in the file,
-     highest_location still points to the current line, not the start
-     of the next line, so we do not decrement in this case.  See
-     plugin/location-overflow-test-pr83173.h for an example.  */
-  bool decremented = false;
-  if (file->pchname == NULL && file->err_no == 0 && type < IT_DIRECTIVE_HWM)
-    {
-      decremented = (pfile->line_table->highest_line
-                    == pfile->line_table->highest_location);
-      if (decremented)
-       pfile->line_table->highest_location--;
-    }
+  /* In the case of a normal #include, we're now at the start of the
+     line *following* the #include.  A separate location_t for this
+     location makes no sense, until we do the LC_LEAVE.
+
+     This does not apply if we found a PCH file, we're not a regular
+     include, or we ran out of locations.  */
+  if (file->pchname == NULL
+      && type < IT_DIRECTIVE_HWM
+      && pfile->line_table->highest_location != LINE_MAP_MAX_LOCATION - 1)
+    pfile->line_table->highest_location--;
 
   /* Add line map and do callbacks.  */
   _cpp_do_file_change (pfile, LC_ENTER, file->path, 1, sysp);
index d4768aee7133cd2b357d95e8555925bd367baa2f..f9bcd37c571781910384b0d8da9a4e7f23489dfa 100644 (file)
@@ -234,7 +234,8 @@ struct cpp_context
 
 struct lexer_state
 {
-  /* Nonzero if first token on line is CPP_HASH.  */
+  /* 1 if we're handling a directive.  2 if it's an include-like
+     directive.  */
   unsigned char in_directive;
 
   /* Nonzero if in a directive that will handle padding tokens itself.
index 16ded6e9b0589c365febba41cf04a090a11d9cca..52e5bceb3ffce58afc6be83db025e7b5cf75fc7b 100644 (file)
@@ -2771,7 +2771,13 @@ _cpp_lex_direct (cpp_reader *pfile)
       goto skipped_white;
 
     case '\n':
-      if (buffer->cur < buffer->rlimit)
+      /* Increment the line, unless this is the last line ...  */
+      if (buffer->cur < buffer->rlimit
+         /* ... or this is a #include, (where _cpp_stack_file needs to
+            unwind by one line) ...  */
+         || (pfile->state.in_directive > 1
+             /* ... except traditional-cpp increments this elsewhere.  */
+             && !CPP_OPTION (pfile, traditional)))
        CPP_INCREMENT_LINE (pfile, 0);
       buffer->need_line = true;
       goto fresh_line;
index 572d7f416f5a9aa98eb09bd8a4715c5d136025fa..d6924eb3e78689e73799c3c36e3a9e778869143f 100644 (file)
@@ -764,7 +764,11 @@ linemap_line_start (line_maps *set, linenum_type to_line,
   /* Locations of ordinary tokens are always lower than locations of
      macro tokens.  */
   if (r >= LINE_MAP_MAX_LOCATION)
-    return 0;
+    {
+      /* Remember we overflowed.  */
+      set->highest_line = set->highest_location = LINE_MAP_MAX_LOCATION - 1;
+      return 0;
+    }
 
   set->highest_line = r;
   if (r > set->highest_location)