]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
Macro laziness now a property of cpp_macro.
authorNathan Sidwell <nathan@acm.org>
Mon, 6 Aug 2018 20:37:47 +0000 (20:37 +0000)
committerNathan Sidwell <nathan@gcc.gnu.org>
Mon, 6 Aug 2018 20:37:47 +0000 (20:37 +0000)
libcpp/
* include/cpp-id-data.h (cpp_macro): Add lazy field.
* include/libcpp.h (struct cpp_callbacks): Rename and retype
user_builtin_macro to user_lazy_macro.
(cpp_define_lazily): Adjust,
(cpp_define_lazy): Delete.
(enum cpp_builtin_type): Remove BT_FIRST_USER, BT_LAST_USER.
* internal.h (_cpp_do_lazy_macro): Declare.
(_cpp_maybe_lazy_macro): Inline fn.
* macro.c (enter_macro_context, warn_of_redefinition): Use
_cpp_maybe_lazy_macro.
(_cpp_new_macro): Initialize lazy field.
(cpp_define_lazily): Adjust.
(_cpp_do_lazy_macro): Define.
(cpp_macro_definition): Simplify.
* pch.c (write_macrodef, save_macros): Likewise.
* directives.c (do_ifdef, do_ifndef): Use _cpp_maybe_lazy_macro.
* expr.c (parse_defined): Likewise.
gcc/c-family/
* c-cppbuiltin.c (lazy_hex_fp_value): Adjust for API changes.
(builtin_define_with_hex_fp_valye): Likewise.

From-SVN: r263343

ChangeLog.name-lookup
gcc/c-family/c-cppbuiltin.c
libcpp/directives.c
libcpp/expr.c
libcpp/include/cpp-id-data.h
libcpp/include/cpplib.h
libcpp/internal.h
libcpp/macro.c
libcpp/pch.c

index 2afb6ebf95db46dabf1edde8591085f8894e9fb2..7a9977135519db019d1ca624be71c1df8c295fcd 100644 (file)
@@ -1,5 +1,28 @@
 2018-08-06  Nathan Sidwell  <nathan@acm.org>
 
+       Macro laziness now a property of cpp_macro.
+       libcpp/
+       * include/cpp-id-data.h (cpp_macro): Add lazy field.
+       * include/libcpp.h (struct cpp_callbacks): Rename and retype
+       user_builtin_macro to user_lazy_macro.
+       (cpp_define_lazily): Adjust,
+       (cpp_define_lazy): Delete.
+       (enum cpp_builtin_type): Remove BT_FIRST_USER, BT_LAST_USER.
+       * internal.h (_cpp_do_lazy_macro): Declare.
+       (_cpp_maybe_lazy_macro): Inline fn.
+       * macro.c (enter_macro_context, warn_of_redefinition): Use
+       _cpp_maybe_lazy_macro.
+       (_cpp_new_macro): Initialize lazy field.
+       (cpp_define_lazily): Adjust.
+       (_cpp_do_lazy_macro): Define.
+       (cpp_macro_definition): Simplify.
+       * pch.c (write_macrodef, save_macros): Likewise.
+       * directives.c (do_ifdef, do_ifndef): Use _cpp_maybe_lazy_macro.
+       * expr.c (parse_defined): Likewise.
+       gcc/c-family/
+       * c-cppbuiltin.c (lazy_hex_fp_value): Adjust for API changes.
+       (builtin_define_with_hex_fp_valye): Likewise.
+
        Merge trunk r263332.
 
 2018-08-03  Nathan Sidwell  <nathan@acm.org>
index 9a89b7b3c4b4b4daa6b8960d531d9a8e205c75a0..54d9c8996bab24eca502c9233c4ea82038ad8d65 100644 (file)
@@ -1571,7 +1571,6 @@ builtin_define_with_int_value (const char *macro, HOST_WIDE_INT value)
 struct GTY(()) lazy_hex_fp_value_struct
 {
   const char *hex_str;
-  cpp_macro *macro;
   machine_mode mode;
   int digits;
   const char *fp_suffix;
@@ -1584,18 +1583,16 @@ struct GTY(()) lazy_hex_fp_value_struct
 #define LAZY_HEX_FP_VALUES_CNT (4 * (3 + NUM_FLOATN_NX_TYPES))
 static GTY(()) struct lazy_hex_fp_value_struct
   lazy_hex_fp_values[LAZY_HEX_FP_VALUES_CNT];
-static GTY(()) int lazy_hex_fp_value_count;
+static GTY(()) unsigned lazy_hex_fp_value_count;
 
-static bool
-lazy_hex_fp_value (cpp_reader *pfile, cpp_hashnode *node, unsigned num)
+static void
+lazy_hex_fp_value (cpp_reader *, cpp_macro *macro, unsigned num)
 {
   REAL_VALUE_TYPE real;
   char dec_str[64], buf1[256];
-  if (num < unsigned (BT_FIRST_USER)
-      || num >= unsigned (BT_FIRST_USER + lazy_hex_fp_value_count))
-    return false;
 
-  num -= BT_FIRST_USER;
+  gcc_checking_assert (num < lazy_hex_fp_value_count);
+
   real_from_string (&real, lazy_hex_fp_values[num].hex_str);
   real_to_decimal_for_mode (dec_str, &real, sizeof (dec_str),
                            lazy_hex_fp_values[num].digits, 0,
@@ -1604,19 +1601,17 @@ lazy_hex_fp_value (cpp_reader *pfile, cpp_hashnode *node, unsigned num)
   size_t len
     = sprintf (buf1, "%s%s", dec_str, lazy_hex_fp_values[num].fp_suffix);
   gcc_assert (len < sizeof (buf1));
-  cpp_macro *macro = lazy_hex_fp_values[num].macro;
   for (unsigned idx = 0; idx < macro->count; idx++)
     if (macro->exp.tokens[idx].type == CPP_NUMBER)
       {
        macro->exp.tokens[idx].val.str.len = len;
        macro->exp.tokens[idx].val.str.text
          = (const unsigned char *) ggc_strdup (buf1);
-       break;
+       return;
       }
 
-  cpp_define_lazy (pfile, node, macro);
-  
-  return true;
+  /* We must have replaced a token.  */
+  gcc_unreachable ();
 }
 
 /* Pass an object-like macro a hexadecimal floating-point value.  */
@@ -1635,22 +1630,18 @@ builtin_define_with_hex_fp_value (const char *macro,
       && flag_dump_macros == 0
       && !cpp_get_options (parse_in)->traditional)
     {
-      struct cpp_hashnode *node;
       if (lazy_hex_fp_value_count == 0)
-       cpp_get_callbacks (parse_in)->user_builtin_macro = lazy_hex_fp_value;
+       cpp_get_callbacks (parse_in)->user_lazy_macro = lazy_hex_fp_value;
       sprintf (buf2, fp_cast, "1.1");
       sprintf (buf1, "%s=%s", macro, buf2);
       cpp_define (parse_in, buf1);
-      node = C_CPP_HASHNODE (get_identifier (macro));
+      struct cpp_hashnode *node = C_CPP_HASHNODE (get_identifier (macro));
       lazy_hex_fp_values[lazy_hex_fp_value_count].hex_str
        = ggc_strdup (hex_str);
       lazy_hex_fp_values[lazy_hex_fp_value_count].mode = TYPE_MODE (type);
       lazy_hex_fp_values[lazy_hex_fp_value_count].digits = digits;
       lazy_hex_fp_values[lazy_hex_fp_value_count].fp_suffix = fp_suffix;
-      lazy_hex_fp_values[lazy_hex_fp_value_count].macro
-       = cpp_define_lazily (parse_in, node,
-                            BT_FIRST_USER + lazy_hex_fp_value_count);
-      lazy_hex_fp_value_count++;
+      cpp_define_lazily (parse_in, node, lazy_hex_fp_value_count++);
       return;
     }
 
index 6eb37a78ccf4e32413b7e9a9c80b7dd5dc91aab8..79497a828a7b9dc7b81cc722e11a14d7f1799080 100644 (file)
@@ -1967,10 +1967,7 @@ do_ifdef (cpp_reader *pfile)
              node->flags |= NODE_USED;
              if (node->type == NT_MACRO)
                {
-                 if ((node->flags & NODE_BUILTIN)
-                     && pfile->cb.user_builtin_macro)
-                   pfile->cb.user_builtin_macro (pfile, node,
-                                                 node->value.builtin);
+                 _cpp_maybe_lazy_macro (pfile, node);
                  if (pfile->cb.used_define)
                    pfile->cb.used_define (pfile, pfile->directive_line, node);
                }
@@ -2014,10 +2011,7 @@ do_ifndef (cpp_reader *pfile)
              node->flags |= NODE_USED;
              if (node->type == NT_MACRO)
                {
-                 if ((node->flags & NODE_BUILTIN)
-                     && pfile->cb.user_builtin_macro)
-                   pfile->cb.user_builtin_macro (pfile, node,
-                                                 node->value.builtin);
+                 _cpp_maybe_lazy_macro (pfile, node);
                  if (pfile->cb.used_define)
                    pfile->cb.used_define (pfile, pfile->directive_line, node);
                }
index 897ffe710aa1a7da2dc2537022cea3f79e5390df..b752d4f92ad88c71643eda5ab5a6b605d8bc511f 100644 (file)
@@ -1070,9 +1070,7 @@ parse_defined (cpp_reader *pfile)
          node->flags |= NODE_USED;
          if (node->type == NT_MACRO)
            {
-             if ((node->flags & NODE_BUILTIN)
-                 && pfile->cb.user_builtin_macro)
-               pfile->cb.user_builtin_macro (pfile, node, node->value.builtin);
+             _cpp_maybe_lazy_macro (pfile, node);
              if (pfile->cb.used_define)
                pfile->cb.used_define (pfile, pfile->directive_line, node);
            }
index ab5f9ef831ad42c295e0dfb7b858c0c225953821..24572d2cd67d950d5691ad29cd5f4a4440983f7e 100644 (file)
@@ -58,6 +58,9 @@ struct GTY(()) cpp_macro {
   /* Number of parameters.  */
   unsigned short paramc;
 
+  /* Non-zero if this is a user-lazy macro, value provided by user.  */
+  unsigned char lazy;
+
   /* The kind of this macro (ISO, trad or assert) */
   unsigned kind : 2;
 
@@ -78,7 +81,7 @@ struct GTY(()) cpp_macro {
      tokens.  */
   unsigned int extra_tokens : 1;
 
-  /* 9 bits spare (32-bit). 41 on 64-bit target.  */
+  /* 1 bits spare (32-bit). 33 on 64-bit target.  */
 
   union cpp_exp_u
   {
index 8b5b10595020425d3c1982e1bb030ad7d8122460..2f24afcc1e459d92843b1140e03d6744346ef16f 100644 (file)
@@ -604,8 +604,8 @@ struct cpp_callbacks
   /* Callback to identify whether an attribute exists.  */
   int (*has_attribute) (cpp_reader *);
 
-  /* Callback that can change a user builtin into normal macro.  */
-  bool (*user_builtin_macro) (cpp_reader *, cpp_hashnode *, unsigned);
+  /* Callback that can change a user lazy into normal macro.  */
+  void (*user_lazy_macro) (cpp_reader *, cpp_macro *, unsigned);
 
   /* Callback to parse SOURCE_DATE_EPOCH from environment.  */
   time_t (*get_source_date_epoch) (cpp_reader *);
@@ -716,9 +716,7 @@ enum cpp_builtin_type
   BT_PRAGMA,                   /* `_Pragma' operator */
   BT_TIMESTAMP,                        /* `__TIMESTAMP__' */
   BT_COUNTER,                  /* `__COUNTER__' */
-  BT_HAS_ATTRIBUTE,            /* `__has_attribute__(x)' */
-  BT_FIRST_USER,               /* User defined builtin macros.  */
-  BT_LAST_USER = BT_FIRST_USER + 63
+  BT_HAS_ATTRIBUTE             /* `__has_attribute__(x)' */
 };
 
 #define CPP_HASHNODE(HNODE)    ((cpp_hashnode *) (HNODE))
@@ -921,9 +919,7 @@ extern void cpp_undef (cpp_reader *, const char *);
 extern void cpp_unassert (cpp_reader *, const char *);
 
 /* Mark a node as a lazily defined macro.  */
-extern cpp_macro *cpp_define_lazily (cpp_reader *, cpp_hashnode *node, int);
-/* Supply the definition of a lazily-defined macro.  */
-extern void cpp_define_lazy (cpp_reader *, cpp_hashnode *node, cpp_macro *);
+extern void cpp_define_lazily (cpp_reader *, cpp_hashnode *node, unsigned N);
 
 /* Undefine all macros and assertions.  */
 extern void cpp_undef_all (cpp_reader *);
index 4b3c2802549fb4e8a95886718be1e526ced5dab3..9186aedbf77cc6b8b3a2187e22eae20d0f4e20b6 100644 (file)
@@ -622,6 +622,14 @@ cpp_in_primary_file (cpp_reader *pfile)
 }
 
 /* In macro.c */
+extern cpp_macro *_cpp_do_lazy_macro (cpp_reader *pfile, cpp_macro *macro);
+inline cpp_macro *_cpp_maybe_lazy_macro (cpp_reader *pfile, cpp_hashnode *node)
+{
+  cpp_macro *macro = (node->flags & NODE_BUILTIN) ? NULL : node->value.macro;
+  if (macro && macro->lazy)
+    macro = _cpp_do_lazy_macro (pfile, macro);
+  return macro;
+}
 extern cpp_macro *_cpp_new_macro (cpp_reader *, cpp_macro_kind, void *);
 extern void _cpp_free_definition (cpp_hashnode *);
 extern bool _cpp_create_definition (cpp_reader *, cpp_hashnode *);
index 7b7315beae7f8cf2e8aec336b88e61c58604cf95..72f223f7356efe5764c3487fa91cd57fc31bfa8f 100644 (file)
@@ -1273,19 +1273,9 @@ enter_macro_context (cpp_reader *pfile, cpp_hashnode *node,
      function where set this flag to FALSE.  */
   pfile->about_to_expand_macro_p = true;
 
-  if ((node->flags & NODE_BUILTIN) && !(node->flags & NODE_USED))
+  if (!(node->flags & NODE_BUILTIN))
     {
-      node->flags |= NODE_USED;
-      if ((!pfile->cb.user_builtin_macro
-          || !pfile->cb.user_builtin_macro (pfile, node, node->value.builtin))
-         && pfile->cb.used_define)
-       pfile->cb.used_define (pfile, pfile->directive_line, node);
-    }
-
-  /* Handle standard macros.  */
-  if (! (node->flags & NODE_BUILTIN))
-    {
-      cpp_macro *macro = node->value.macro;
+      cpp_macro *macro = _cpp_maybe_lazy_macro (pfile, node);
       _cpp_buff *pragma_buff = NULL;
 
       if (macro->fun_like)
@@ -2996,7 +2986,6 @@ static bool
 warn_of_redefinition (cpp_reader *pfile, cpp_hashnode *node,
                      const cpp_macro *macro2)
 {
-  const cpp_macro *macro1;
   unsigned int i;
 
   /* Some redefinitions need to be warned about regardless.  */
@@ -3005,9 +2994,7 @@ warn_of_redefinition (cpp_reader *pfile, cpp_hashnode *node,
 
   /* Suppress warnings for builtins that lack the NODE_WARN flag,
      unless Wbuiltin-macro-redefined.  */
-  if (node->flags & NODE_BUILTIN
-      && (!pfile->cb.user_builtin_macro
-         || !pfile->cb.user_builtin_macro (pfile, node, node->value.builtin)))
+  if (node->flags & NODE_BUILTIN)
     return CPP_OPTION (pfile, warn_builtin_macro_redefined);
 
   /* Redefinitions of conditional (context-sensitive) macros, on
@@ -3015,9 +3002,10 @@ warn_of_redefinition (cpp_reader *pfile, cpp_hashnode *node,
   if (node->flags & NODE_CONDITIONAL)
     return false;
 
+  const cpp_macro *macro1 = _cpp_maybe_lazy_macro (pfile, node);
+
   /* Redefinition of a macro is allowed if and only if the old and new
      definitions are the same.  (6.10.3 paragraph 2).  */
-  macro1 = node->value.macro;
 
   /* Don't check count here as it can be different in valid
      traditional redefinitions with just whitespace differences.  */
@@ -3488,6 +3476,7 @@ _cpp_new_macro (cpp_reader *pfile, cpp_macro_kind kind, void *placement)
 
   macro->line = pfile->directive_line;
   macro->parm.params = 0;
+  macro->lazy = 0;
   macro->paramc = 0;
   macro->variadic = 0;
   macro->used = !CPP_OPTION (pfile, warn_unused_macros);
@@ -3560,24 +3549,24 @@ _cpp_create_definition (cpp_reader *pfile, cpp_hashnode *node)
   return true;
 }
 
-extern cpp_macro *
-cpp_define_lazily (cpp_reader *, cpp_hashnode *node, int num)
+extern void
+cpp_define_lazily (cpp_reader *pfile, cpp_hashnode *node, unsigned num)
 {
   cpp_macro *macro = node->value.macro;
 
-  node->type = NT_MACRO;
-  node->flags |= NODE_BUILTIN;
+  gcc_checking_assert (pfile->cb.user_lazy_macro && macro && num < 255);
 
-  node->value.builtin = (enum cpp_builtin_type) (num);
-
-  return macro;
+  macro->lazy = num + 1;
 }
 
-extern void
-cpp_define_lazy (cpp_reader *, cpp_hashnode *node, cpp_macro *macro)
+extern cpp_macro *
+_cpp_do_lazy_macro (cpp_reader *pfile, cpp_macro *macro)
 {
-  node->flags &= ~(NODE_BUILTIN | NODE_USED);
-  node->value.macro = macro;
+  unsigned num = macro->lazy - 1;
+  macro->lazy = 0;
+  pfile->cb.user_lazy_macro (pfile, macro, num);
+
+  return macro;
 }
 
 /* Warn if a token in STRING matches one of a function-like MACRO's
@@ -3649,23 +3638,12 @@ const unsigned char *
 cpp_macro_definition (cpp_reader *pfile, cpp_hashnode *node)
 {
   unsigned int i, len;
-  const cpp_macro *macro;
   unsigned char *buffer;
 
-  if (node->type != NT_MACRO || (node->flags & NODE_BUILTIN))
-    {
-      if (node->type != NT_MACRO
-         || !pfile->cb.user_builtin_macro
-          || !pfile->cb.user_builtin_macro (pfile, node, node->value.builtin))
-       {
-         cpp_error (pfile, CPP_DL_ICE,
-                    "invalid hash type %d in cpp_macro_definition",
-                    node->type);
-         return 0;
-       }
-    }
+  gcc_checking_assert (node->type == NT_MACRO);
+
+  const cpp_macro *macro = node->value.macro;
 
-  macro = node->value.macro;
   /* Calculate length.  */
   len = NODE_LEN (node) * 10 + 2;              /* ' ' and NUL.  */
   if (macro->fun_like)
index 24acacdf54c149917889a1e0ef650f6daffd15ce..63022818e01c2a43c820a891c68dd4aa9b8222a2 100644 (file)
@@ -50,50 +50,45 @@ static int
 write_macdef (cpp_reader *pfile, cpp_hashnode *hn, void *file_p)
 {
   FILE *f = (FILE *) file_p;
+  bool is_void = false;
   switch (hn->type)
     {
     case NT_VOID:
       if (! (hn->flags & NODE_POISONED))
        return 1;
-      /* XXX Really fallthru?  */
-      /* FALLTHRU */
+      is_void = true;
+      goto poisoned;
 
     case NT_MACRO:
-      if (hn->flags & NODE_BUILTIN)
+      if (!(hn->flags & NODE_BUILTIN)
+         && hn->value.macro->kind != cmk_assert)
        {
-         if (!pfile->cb.user_builtin_macro
-             || !pfile->cb.user_builtin_macro (pfile, hn, hn->value.builtin))
-           return 1;
-       }
-      else if (hn->value.macro->kind == cmk_assert)
-       return 1;
+       poisoned:
+         struct macrodef_struct s;
+         const unsigned char *defn;
 
-      {
-       struct macrodef_struct s;
-       const unsigned char *defn;
+         s.name_length = NODE_LEN (hn);
+         s.flags = hn->flags & NODE_POISONED;
 
-       s.name_length = NODE_LEN (hn);
-       s.flags = hn->flags & NODE_POISONED;
-
-       if (hn->type == NT_MACRO)
-         {
-           defn = cpp_macro_definition (pfile, hn);
-           s.definition_length = ustrlen (defn);
-         }
-       else
-         {
-           defn = NODE_NAME (hn);
-           s.definition_length = s.name_length;
-         }
+         if (is_void)
+           {
+             defn = NODE_NAME (hn);
+             s.definition_length = s.name_length;
+           }
+         else
+           {
+             defn = cpp_macro_definition (pfile, hn);
+             s.definition_length = ustrlen (defn);
+           }
 
-       if (fwrite (&s, sizeof (s), 1, f) != 1
-           || fwrite (defn, 1, s.definition_length, f) != s.definition_length)
-         {
-           cpp_errno (pfile, CPP_DL_ERROR,
-                      "while writing precompiled header");
-           return 0;
-         }
-      }
+         if (fwrite (&s, sizeof (s), 1, f) != 1
+             || fwrite (defn, 1, s.definition_length, f) != s.definition_length)
+           {
+             cpp_errno (pfile, CPP_DL_ERROR,
+                        "while writing precompiled header");
+             return 0;
+           }
+       }
       return 1;
 
     default:
@@ -756,17 +751,9 @@ save_macros (cpp_reader *r, cpp_hashnode *h, void *data_p)
 {
   struct save_macro_data *data = (struct save_macro_data *)data_p;
 
-  if (h->type != NT_MACRO)
-    return 1;
-
-  if (h->flags & NODE_BUILTIN)
-    {
-      if (r->cb.user_builtin_macro)
-       r->cb.user_builtin_macro (r, h, h->value.builtin);
-    }
-  else if (h->value.macro->kind == cmk_assert)
-    return 1;
-  else
+  if (h->type == NT_MACRO
+      && !(h->flags & NODE_BUILTIN)
+      && h->value.macro->kind != cmk_assert)
     {
       if (data->count == data->array_size)
        {