re PR preprocessor/92296 (internal compiler error: Segmentation fault #pragma push_m...
authorJakub Jelinek <jakub@redhat.com>
Thu, 31 Oct 2019 17:38:44 +0000 (18:38 +0100)
committerJakub Jelinek <jakub@gcc.gnu.org>
Thu, 31 Oct 2019 17:38:44 +0000 (18:38 +0100)
PR preprocessor/92296
* internal.h (struct def_pragma_macro): Add is_builtin bitfield.
(_cpp_restore_special_builtin): Declare.
* init.c (_cpp_restore_special_builtin): New function.
* directives.c (do_pragma_push_macro): For NT_BUILTIN_MACRO
set is_builtin and don't try to grab definition.
(cpp_pop_definition): Use _cpp_restore_special_builtin to restore
builtin macros.

* c-c++-common/cpp/pr92296-1.c: New test.
* c-c++-common/cpp/pr92296-2.c: New test.

From-SVN: r277685

gcc/testsuite/ChangeLog
gcc/testsuite/c-c++-common/cpp/pr92296-1.c [new file with mode: 0644]
gcc/testsuite/c-c++-common/cpp/pr92296-2.c [new file with mode: 0644]
libcpp/ChangeLog
libcpp/directives.c
libcpp/init.c
libcpp/internal.h

index 79936089d20ab92816fd4f621717373a7f8af5f5..6f0cc3971b48d00721a179f282a8937cc76329ab 100644 (file)
@@ -1,3 +1,9 @@
+2019-10-31  Jakub Jelinek  <jakub@redhat.com>
+
+       PR preprocessor/92296
+       * c-c++-common/cpp/pr92296-1.c: New test.
+       * c-c++-common/cpp/pr92296-2.c: New test.
+
 2019-10-31  Jozef Lawrynowicz  <jozef.l@mittosystems.com>
 
        * gcc.target/msp430/devices-main.c: Move to devices subdirectory.
diff --git a/gcc/testsuite/c-c++-common/cpp/pr92296-1.c b/gcc/testsuite/c-c++-common/cpp/pr92296-1.c
new file mode 100644 (file)
index 0000000..a6a00b5
--- /dev/null
@@ -0,0 +1,35 @@
+/* PR preprocessor/92296 */
+/* { dg-do preprocess } */
+
+#pragma push_macro("__TIMESTAMP__")
+#pragma pop_macro("__TIMESTAMP__")
+
+#pragma push_macro("__TIME__")
+#pragma pop_macro("__TIME__")
+
+#pragma push_macro("__DATE__")
+#pragma pop_macro("__DATE__")
+
+#pragma push_macro("__FILE__")
+#pragma pop_macro("__FILE__")
+
+#pragma push_macro("__BASE_FILE__")
+#pragma pop_macro("__BASE_FILE__")
+
+#pragma push_macro("__LINE__")
+#pragma pop_macro("__LINE__")
+
+#pragma push_macro("__INCLUDE_LEVEL__")
+#pragma pop_macro("__INCLUDE_LEVEL__")
+
+#pragma push_macro("__COUNTER__")
+#pragma pop_macro("__COUNTER__")
+
+#pragma push_macro("__has_attribute")
+#pragma pop_macro("__has_attribute")
+
+#pragma push_macro("__has_cpp_attribute")
+#pragma pop_macro("__has_cpp_attribute")
+
+#pragma push_macro("__has_builtin")
+#pragma pop_macro("__has_builtin")
diff --git a/gcc/testsuite/c-c++-common/cpp/pr92296-2.c b/gcc/testsuite/c-c++-common/cpp/pr92296-2.c
new file mode 100644 (file)
index 0000000..e3f74cd
--- /dev/null
@@ -0,0 +1,80 @@
+/* PR preprocessor/92296 */
+/* { dg-do preprocess } */
+/* { dg-options "-Wno-builtin-macro-redefined" } */
+
+#pragma push_macro("__TIMESTAMP__")
+#undef __TIMESTAMP__
+#define __TIMESTAMP__ "Thu Oct 31 12:00:00 2019"
+timestamp1 = __TIMESTAMP__
+#pragma pop_macro("__TIMESTAMP__")
+timestamp2 = __TIMESTAMP__
+
+#pragma push_macro("__TIME__")
+#undef __TIME__
+#define __TIME__ "12:00:00"
+time1 = __TIME__
+#pragma pop_macro("__TIME__")
+time2 = __TIME__
+
+#pragma push_macro("__DATE__")
+#undef __DATE__
+#define __DATE__ "Oct 31 2019"
+date1 = __DATE__
+#pragma pop_macro("__DATE__")
+date2 = __DATE__
+
+#pragma push_macro("__FILE__")
+#undef __FILE__
+#define __FILE__ "pr92296-3.c"
+file1 = __FILE__       /* { dg-final { scan-file pr92296-2.i "file1 = \"pr92296-3.c\"" } } */
+#pragma pop_macro("__FILE__")
+file2 = __FILE__       /* { dg-final { scan-file-not pr92296-2.i "file2 = \"pr92296-3.c\"" } } */
+
+#pragma push_macro("__BASE_FILE__")
+#undef __BASE_FILE__
+#define __BASE_FILE__ "pr92296-4.c"
+filebase1 = __BASE_FILE__      /* { dg-final { scan-file pr92296-2.i "filebase1 = \"pr92296-4.c\"" } } */
+#pragma pop_macro("__BASE_FILE__")
+filebase2 = __BASE_FILE__      /* { dg-final { scan-file-not pr92296-2.i "filebase2 = \"pr92296-4.c\"" } } */
+
+#pragma push_macro("__LINE__")
+#undef __LINE__                /* { dg-warning "undefining" } */
+#define __LINE__ 142
+line1 = __LINE__       /* { dg-final { scan-file pr92296-2.i "line1 = 142" } } */
+#pragma pop_macro("__LINE__")
+line2 = __LINE__       /* { dg-final { scan-file pr92296-2.i "line2 = 45" } } */
+
+#pragma push_macro("__INCLUDE_LEVEL__")
+#undef __INCLUDE_LEVEL__       /* { dg-warning "undefining" } */
+#define __INCLUDE_LEVEL__ 42
+includelevel1 = __INCLUDE_LEVEL__      /* { dg-final { scan-file pr92296-2.i "includelevel1 = 42" } } */
+#pragma pop_macro("__INCLUDE_LEVEL__")
+includelevel2 = __INCLUDE_LEVEL__      /* { dg-final { scan-file pr92296-2.i "includelevel2 = 0" } } */
+
+#pragma push_macro("__COUNTER__")
+#undef __COUNTER__     /* { dg-warning "undefining" } */
+#define __COUNTER__ 172
+counter1 = __COUNTER__ /* { dg-final { scan-file pr92296-2.i "counter1 = 172" } } */
+#pragma pop_macro("__COUNTER__")
+counter2 = __COUNTER__ /* { dg-final { scan-file-not pr92296-2.i "counter2 = 172" } } */
+
+#pragma push_macro("__has_attribute")
+#undef __has_attribute /* { dg-warning "undefining" } */
+#define __has_attribute(x) 0
+hasattr1 = __has_attribute(noreturn)   /* { dg-final { scan-file pr92296-2.i "hasattr1 = 0" } } */
+#pragma pop_macro("__has_attribute")
+hasattr2 = __has_attribute(noreturn)   /* { dg-final { scan-file-not pr92296-2.i "hasattr2 = 0" } } */
+
+#pragma push_macro("__has_cpp_attribute")
+#undef __has_cpp_attribute     /* { dg-warning "undefining" } */
+#define __has_cpp_attribute(x) 0
+hasattrcpp1 = __has_cpp_attribute(noreturn)    /* { dg-final { scan-file pr92296-2.i "hasattrcpp1 = 0" } } */
+#pragma pop_macro("__has_cpp_attribute")
+hasattrcpp2 = __has_cpp_attribute(noreturn)    /* { dg-final { scan-file-not pr92296-2.i "hasattrcpp2 = 0" } } */
+
+#pragma push_macro("__has_builtin")
+#undef __has_builtin   /* { dg-warning "undefining" } */
+#define __has_builtin(x) 0
+hasbuiltin1 = __has_builtin(__builtin_expect)  /* { dg-final { scan-file pr92296-2.i "hasbuiltin1 = 0" } } */
+#pragma pop_macro("__has_builtin")
+hasbuiltin2 = __has_builtin(__builtin_expect)  /* { dg-final { scan-file pr92296-2.i "hasbuiltin2 = 1" } } */
index 670e97b039a748ac68df4168f32265de1a613a66..a13f75085990ea237e9af3a270db23dda5a6b60d 100644 (file)
@@ -1,3 +1,14 @@
+2019-10-31  Jakub Jelinek  <jakub@redhat.com>
+
+       PR preprocessor/92296
+       * internal.h (struct def_pragma_macro): Add is_builtin bitfield.
+       (_cpp_restore_special_builtin): Declare.
+       * init.c (_cpp_restore_special_builtin): New function.
+       * directives.c (do_pragma_push_macro): For NT_BUILTIN_MACRO
+       set is_builtin and don't try to grab definition.
+       (cpp_pop_definition): Use _cpp_restore_special_builtin to restore
+       builtin macros.
+
 2019-10-15  Nathan Sidwell  <nathan@acm.org>
 
        * include/line-map.h (struct maps_info_ordinary): Make cache
index 61f1fef94892511a6b25473e179b8a0420926a6a..6e011a1bae25062d8ef84f27cf8d30c6075201f7 100644 (file)
@@ -1582,6 +1582,8 @@ do_pragma_push_macro (cpp_reader *pfile)
   node = _cpp_lex_identifier (pfile, c->name);
   if (node->type == NT_VOID)
     c->is_undef = 1;
+  else if (node->type == NT_BUILTIN_MACRO)
+    c->is_builtin = 1;
   else
     {
       defn = cpp_macro_definition (pfile, node);
@@ -2470,6 +2472,11 @@ cpp_pop_definition (cpp_reader *pfile, struct def_pragma_macro *c)
 
   if (c->is_undef)
     return;
+  if (c->is_builtin)
+    {
+      _cpp_restore_special_builtin (pfile, c);
+      return;
+    }
 
   {
     size_t namelen;
index b0943174f02111604c97ac0a53dff4b2e9f47820..32b0e70a2107beab98485c4178a358c5e537c413 100644 (file)
@@ -497,6 +497,25 @@ cpp_init_special_builtins (cpp_reader *pfile)
     }
 }
 
+/* Restore macro C to builtin macro definition.  */
+
+void
+_cpp_restore_special_builtin (cpp_reader *pfile, struct def_pragma_macro *c)
+{
+  size_t len = strlen (c->name);
+
+  for (const struct builtin_macro *b = builtin_array;
+       b < builtin_array + ARRAY_SIZE (builtin_array); b++)
+    if (b->len == len && memcmp (c->name, b->name, len + 1) == 0)
+      {
+       cpp_hashnode *hp = cpp_lookup (pfile, b->name, b->len);
+       hp->type = NT_BUILTIN_MACRO;
+       if (b->always_warn_if_redefined)
+         hp->flags |= NODE_WARN;
+       hp->value.builtin = (enum cpp_builtin_type) b->value;
+      }
+}
+
 /* Read the builtins table above and enter them, and language-specific
    macros, into the hash table.  HOSTED is true if this is a hosted
    environment.  */
index cd1a523219eee25f876add61794f8cff1cfd510e..e759bec0b5f50c4b4fe7e8dc7d07d075aa8328a8 100644 (file)
@@ -391,6 +391,8 @@ struct def_pragma_macro {
 
   /* Mark if we save an undefined macro.  */
   unsigned int is_undef : 1;
+  /* Nonzero if it was a builtin macro.  */
+  unsigned int is_builtin : 1;
 };
 
 /* A cpp_reader encapsulates the "state" of a pre-processor run.
@@ -722,6 +724,8 @@ extern void *_cpp_commit_buff (cpp_reader *pfile, size_t size);
 /* In init.c.  */
 extern void _cpp_maybe_push_include_file (cpp_reader *);
 extern const char *cpp_named_operator2name (enum cpp_ttype type);
+extern void _cpp_restore_special_builtin (cpp_reader *pfile,
+                                         struct def_pragma_macro *);
 
 /* In directives.c */
 extern int _cpp_test_assertion (cpp_reader *, unsigned int *);