preprocessor: Fix ICE with EOF in macro args [pr95182]
authorNathan Sidwell <nathan@acm.org>
Tue, 19 May 2020 13:11:22 +0000 (06:11 -0700)
committerNathan Sidwell <nathan@acm.org>
Tue, 19 May 2020 13:19:31 +0000 (06:19 -0700)
This was another latent case of us losing an EOF token, but succeeding
anyway.  Since my patch to make us pay more attention to EOFs it came
to light.  We also need to keep the EOF if we fall off the end of the
main file.  Forced includes look like regular nested includes at this
point.

PR preprocessor/95182
libcpp/
* macro.c (collect_args): Preserve EOFif we fell out of the main
file.
(cpp_get_token_1): Reformat a couple of short lines.

gcc/testsuite/c-c++-common/cpp/eof-1.c [new file with mode: 0644]
gcc/testsuite/c-c++-common/cpp/eof-2.c [new file with mode: 0644]
gcc/testsuite/c-c++-common/cpp/eof-2.h [new file with mode: 0644]
gcc/testsuite/c-c++-common/cpp/eof-3.c [new file with mode: 0644]
libcpp/ChangeLog
libcpp/macro.c

diff --git a/gcc/testsuite/c-c++-common/cpp/eof-1.c b/gcc/testsuite/c-c++-common/cpp/eof-1.c
new file mode 100644 (file)
index 0000000..0a06f09
--- /dev/null
@@ -0,0 +1,7 @@
+/* PR preprocess/95183  */
+
+/* { dg-do preprocess } */
+
+#define f(x) x
+
+f( /* { dg-error "-:unterminated" "unterminated macro" } */
diff --git a/gcc/testsuite/c-c++-common/cpp/eof-2.c b/gcc/testsuite/c-c++-common/cpp/eof-2.c
new file mode 100644 (file)
index 0000000..3a4af7f
--- /dev/null
@@ -0,0 +1,8 @@
+/* PR preprocess/95183  */
+
+/* { dg-do preprocess } */
+
+#define f(x) x
+
+#include "eof-2.h"
+ /* { dg-regexp {[^\n]*eof-2.h:4: error: unterminated argument list invoking macro "f"\n} } */
diff --git a/gcc/testsuite/c-c++-common/cpp/eof-2.h b/gcc/testsuite/c-c++-common/cpp/eof-2.h
new file mode 100644 (file)
index 0000000..48ad857
--- /dev/null
@@ -0,0 +1,4 @@
+
+#define f(x) x
+
+f( /* Error here  */
diff --git a/gcc/testsuite/c-c++-common/cpp/eof-3.c b/gcc/testsuite/c-c++-common/cpp/eof-3.c
new file mode 100644 (file)
index 0000000..316918e
--- /dev/null
@@ -0,0 +1,8 @@
+/* PR preprocess/95183  */
+
+/* { dg-do preprocess } */
+/* { dg-additional-options "-include $srcdir/c-c++-common/cpp/eof-2.h" } */
+
+ /* { dg-regexp {[^\n]*eof-2.h:4: error: unterminated argument list invoking macro "f"\n} } */
+
+token )
index a802408eb5704c5e74db4183e0fc31e2a998fb87..6c3cf092913a5c51f780d0ab2c4fc15627933152 100644 (file)
@@ -1,3 +1,10 @@
+2020-05-18  Nathan Sidwell  <nathan@acm.org>
+
+       PR preprocessor/95182
+       * macro.c (collect_args): Preserve EOFif we fell out of the main
+       file.
+       (cpp_get_token_1): Reformat a couple of short lines.
+
 2020-05-14  H.J. Lu  <hongjiu.lu@intel.com>
 
        * configure: Regenerated.
index dc4366ffefd36fd663262340249ba7aba2e77654..2c7d7322e09c6ca425b58392f591dc5bc87de70f 100644 (file)
@@ -1258,11 +1258,13 @@ collect_args (cpp_reader *pfile, const cpp_hashnode *node,
 
   if (token->type == CPP_EOF)
     {
-      /* We still need the CPP_EOF to end directives, and to end
-        pre-expansion of a macro argument.  Step back is not
-        unconditional, since we don't want to return a CPP_EOF to our
-        callers at the end of an -include-d file.  */
-      if (pfile->context->prev || pfile->state.in_directive)
+      /* We still need the CPP_EOF to end directives, to end
+        pre-expansion of a macro argument, and at the end of the main
+        file.  We do not want it at the end of a -include'd (forced)
+        header file.  */
+      if (pfile->state.in_directive
+         || !pfile->line_table->depth
+         || pfile->context->prev)
        _cpp_backup_tokens (pfile, 1);
       cpp_error (pfile, CPP_DL_ERROR,
                 "unterminated argument list invoking macro \"%s\"",
@@ -2870,8 +2872,7 @@ cpp_get_token_1 (cpp_reader *pfile, location_t *location)
                                      || (peek_tok->flags & PREV_WHITE));
                  node = pfile->cb.macro_to_expand (pfile, result);
                  if (node)
-                   ret = enter_macro_context (pfile, node, result,
-                                              virt_loc);
+                   ret = enter_macro_context (pfile, node, result, virt_loc);
                  else if (whitespace_after)
                    {
                      /* If macro_to_expand hook returned NULL and it
@@ -2888,8 +2889,7 @@ cpp_get_token_1 (cpp_reader *pfile, location_t *location)
                }
            }
          else
-           ret = enter_macro_context (pfile, node, result, 
-                                      virt_loc);
+           ret = enter_macro_context (pfile, node, result, virt_loc);
          if (ret)
            {
              if (pfile->state.in_directive || ret == 2)