]> git.ipfire.org Git - thirdparty/gcc.git/blobdiff - libcpp/macro.c
Make TOPN counter dynamically allocated.
[thirdparty/gcc.git] / libcpp / macro.c
index 43f2baa67b37643ffd24df36ebb081e3fd841381..2c7d7322e09c6ca425b58392f591dc5bc87de70f 100644 (file)
@@ -1,5 +1,5 @@
 /* Part of CPP library.  (Macro and #define handling.)
-   Copyright (C) 1986-2017 Free Software Foundation, Inc.
+   Copyright (C) 1986-2020 Free Software Foundation, Inc.
    Written by Per Bothner, 1994.
    Based on CCCP program by Paul Rubin, June 1986
    Adapted to ANSI C, Richard Stallman, Jan 1987
@@ -39,9 +39,9 @@ struct macro_arg
   const cpp_token *stringified;        /* Stringified argument.  */
   unsigned int count;          /* # of tokens in argument.  */
   unsigned int expanded_count; /* # of tokens in expanded argument.  */
-  source_location *virt_locs;  /* Where virtual locations for
+  location_t *virt_locs;       /* Where virtual locations for
                                   unexpanded tokens are stored.  */
-  source_location *expanded_virt_locs; /* Where virtual locations for
+  location_t *expanded_virt_locs; /* Where virtual locations for
                                          expanded tokens are
                                          stored.  */
 };
@@ -51,7 +51,7 @@ struct macro_arg
 enum macro_arg_token_kind {
   MACRO_ARG_TOKEN_NORMAL,
   /* This is a macro argument token that got transformed into a string
-     litteral, e.g. #foo.  */
+     literal, e.g. #foo.  */
   MACRO_ARG_TOKEN_STRINGIFIED,
   /* This is a token resulting from the expansion of a macro
      argument that was itself a macro.  */
@@ -72,7 +72,7 @@ struct macro_arg_token_iter
   /* A pointer to the "full" location of the current token.  If
      -ftrack-macro-expansion is used this location tracks loci across
      macro expansion.  */
-  const source_location *location_ptr;
+  const location_t *location_ptr;
 #if CHECKING_P
   /* The number of times the iterator went forward. This useful only
      when checking is enabled.  */
@@ -85,13 +85,16 @@ struct macro_arg_token_iter
 struct macro_arg_saved_data {
   /* The canonical (UTF-8) spelling of this identifier.  */
   cpp_hashnode *canonical_node;
-  /* The previous value of this identifier.  */
+  /* The previous value & type of this identifier.  */
   union _cpp_hashnode_value value;
+  node_type type;
 };
 
 static const char *vaopt_paste_error =
   N_("'##' cannot appear at either end of __VA_OPT__");
 
+static void expand_arg (cpp_reader *, macro_arg *);
+
 /* A class for tracking __VA_OPT__ state while iterating over a
    sequence of tokens.  This is used during both macro definition and
    expansion.  */
@@ -99,26 +102,29 @@ class vaopt_state {
 
  public:
 
+  enum update_type
+  {
+    ERROR,
+    DROP,
+    INCLUDE,
+    BEGIN,
+    END
+  };
+
   /* Initialize the state tracker.  ANY_ARGS is true if variable
      arguments were provided to the macro invocation.  */
-  vaopt_state (cpp_reader *pfile, bool is_variadic, bool any_args)
+  vaopt_state (cpp_reader *pfile, bool is_variadic, macro_arg *arg)
     : m_pfile (pfile),
-    m_allowed (any_args),
+    m_arg (arg),
     m_variadic (is_variadic),
-    m_state (0),
     m_last_was_paste (false),
+    m_state (0),
     m_paste_location (0),
-    m_location (0)
+    m_location (0),
+    m_update (ERROR)
   {
   }
 
-  enum update_type
-  {
-    ERROR,
-    DROP,
-    INCLUDE
-  };
-
   /* Given a token, update the state of this tracker and return a
      boolean indicating whether the token should be be included in the
      expansion.  */
@@ -139,7 +145,7 @@ class vaopt_state {
          }
        ++m_state;
        m_location = token->src_loc;
-       return DROP;
+       return BEGIN;
       }
     else if (m_state == 1)
       {
@@ -151,6 +157,23 @@ class vaopt_state {
            return ERROR;
          }
        ++m_state;
+       if (m_update == ERROR)
+         {
+           if (m_arg == NULL)
+             m_update = INCLUDE;
+           else
+             {
+               m_update = DROP;
+               if (!m_arg->expanded)
+                 expand_arg (m_pfile, m_arg);
+               for (unsigned idx = 0; idx < m_arg->expanded_count; ++idx)
+                 if (m_arg->expanded[idx]->type != CPP_PADDING)
+                   {
+                     m_update = INCLUDE;
+                     break;
+                   }
+             }
+         }
        return DROP;
       }
     else if (m_state >= 2)
@@ -191,10 +214,10 @@ class vaopt_state {
                    return ERROR;
                  }
 
-               return DROP;
+               return END;
              }
          }
-       return m_allowed ? INCLUDE : DROP;
+       return m_update;
       }
 
     /* Nothing to do with __VA_OPT__.  */
@@ -216,10 +239,14 @@ class vaopt_state {
   /* The cpp_reader.  */
   cpp_reader *m_pfile;
 
-  /* True if there were varargs.  */
-  bool m_allowed;
+  /* The __VA_ARGS__ argument.  */
+  macro_arg *m_arg;
+
   /* True if the macro is variadic.  */
   bool m_variadic;
+  /* If true, the previous token was ##.  This is used to detect when
+     a paste occurs at the end of the sequence.  */
+  bool m_last_was_paste;
 
   /* The state variable:
      0 means not parsing
@@ -228,50 +255,50 @@ class vaopt_state {
      >= 3 means looking for ")", the number encodes the paren depth.  */
   int m_state;
 
-  /* If true, the previous token was ##.  This is used to detect when
-     a paste occurs at the end of the sequence.  */
-  bool m_last_was_paste;
   /* The location of the paste token.  */
-  source_location m_paste_location;
+  location_t m_paste_location;
 
   /* Location of the __VA_OPT__ token.  */
-  source_location m_location;
+  location_t m_location;
+
+  /* If __VA_ARGS__ substitutes to no preprocessing tokens,
+     INCLUDE, otherwise DROP.  ERROR when unknown yet.  */
+  update_type m_update;
 };
 
 /* Macro expansion.  */
 
 static int enter_macro_context (cpp_reader *, cpp_hashnode *,
-                               const cpp_token *, source_location);
+                               const cpp_token *, location_t);
 static int builtin_macro (cpp_reader *, cpp_hashnode *,
-                         source_location, source_location);
+                         location_t, location_t);
 static void push_ptoken_context (cpp_reader *, cpp_hashnode *, _cpp_buff *,
                                 const cpp_token **, unsigned int);
 static void push_extended_tokens_context (cpp_reader *, cpp_hashnode *,
-                                         _cpp_buff *, source_location *,
+                                         _cpp_buff *, location_t *,
                                          const cpp_token **, unsigned int);
 static _cpp_buff *collect_args (cpp_reader *, const cpp_hashnode *,
                                _cpp_buff **, unsigned *);
 static cpp_context *next_context (cpp_reader *);
 static const cpp_token *padding_token (cpp_reader *, const cpp_token *);
-static void expand_arg (cpp_reader *, macro_arg *);
 static const cpp_token *new_string_token (cpp_reader *, uchar *, unsigned int);
 static const cpp_token *stringify_arg (cpp_reader *, macro_arg *);
 static void paste_all_tokens (cpp_reader *, const cpp_token *);
-static bool paste_tokens (cpp_reader *, source_location,
+static bool paste_tokens (cpp_reader *, location_t,
                          const cpp_token **, const cpp_token *);
 static void alloc_expanded_arg_mem (cpp_reader *, macro_arg *, size_t);
 static void ensure_expanded_arg_room (cpp_reader *, macro_arg *, size_t, size_t *);
 static void delete_macro_args (_cpp_buff*, unsigned num_args);
 static void set_arg_token (macro_arg *, const cpp_token *,
-                          source_location, size_t,
+                          location_t, size_t,
                           enum macro_arg_token_kind,
                           bool);
-static const source_location *get_arg_token_location (const macro_arg *,
+static const location_t *get_arg_token_location (const macro_arg *,
                                                      enum macro_arg_token_kind);
 static const cpp_token **arg_token_ptr_at (const macro_arg *,
                                           size_t,
                                           enum macro_arg_token_kind,
-                                          source_location **virt_location);
+                                          location_t **virt_location);
 
 static void macro_arg_token_iter_init (macro_arg_token_iter *, bool,
                                       enum macro_arg_token_kind,
@@ -279,49 +306,48 @@ static void macro_arg_token_iter_init (macro_arg_token_iter *, bool,
                                       const cpp_token **);
 static const cpp_token *macro_arg_token_iter_get_token
 (const macro_arg_token_iter *it);
-static source_location macro_arg_token_iter_get_location
+static location_t macro_arg_token_iter_get_location
 (const macro_arg_token_iter *);
 static void macro_arg_token_iter_forward (macro_arg_token_iter *);
 static _cpp_buff *tokens_buff_new (cpp_reader *, size_t,
-                                  source_location **);
+                                  location_t **);
 static size_t tokens_buff_count (_cpp_buff *);
 static const cpp_token **tokens_buff_last_token_ptr (_cpp_buff *);
 static inline const cpp_token **tokens_buff_put_token_to (const cpp_token **,
-                                                          source_location *,
+                                                          location_t *,
                                                           const cpp_token *,
-                                                          source_location,
-                                                          source_location,
+                                                          location_t,
+                                                          location_t,
                                                           const line_map_macro *,
                                                           unsigned int);
 
 static const cpp_token **tokens_buff_add_token (_cpp_buff *,
-                                               source_location *,
+                                               location_t *,
                                                const cpp_token *,
-                                               source_location,
-                                               source_location,
+                                               location_t,
+                                               location_t,
                                                const line_map_macro *,
                                                unsigned int);
 static inline void tokens_buff_remove_last_token (_cpp_buff *);
 static void replace_args (cpp_reader *, cpp_hashnode *, cpp_macro *,
-                         macro_arg *, source_location);
+                         macro_arg *, location_t);
 static _cpp_buff *funlike_invocation_p (cpp_reader *, cpp_hashnode *,
                                        _cpp_buff **, unsigned *);
-static bool create_iso_definition (cpp_reader *, cpp_macro *);
+static cpp_macro *create_iso_definition (cpp_reader *);
 
 /* #define directive parsing and handling.  */
 
-static cpp_token *alloc_expansion_token (cpp_reader *, cpp_macro *);
-static cpp_token *lex_expansion_token (cpp_reader *, cpp_macro *);
+static cpp_macro *lex_expansion_token (cpp_reader *, cpp_macro *);
 static bool warn_of_redefinition (cpp_reader *, cpp_hashnode *,
                                  const cpp_macro *);
-static bool parse_params (cpp_reader *, cpp_macro *);
+static bool parse_params (cpp_reader *, unsigned *, bool *);
 static void check_trad_stringification (cpp_reader *, const cpp_macro *,
                                        const cpp_string *);
 static bool reached_end_of_context (cpp_context *);
 static void consume_next_token_from_context (cpp_reader *pfile,
                                             const cpp_token **,
-                                            source_location *);
-static const cpp_token* cpp_get_token_1 (cpp_reader *, source_location *);
+                                            location_t *);
+static const cpp_token* cpp_get_token_1 (cpp_reader *, location_t *);
 
 static cpp_hashnode* macro_of_context (cpp_context *context);
 
@@ -334,13 +360,85 @@ unsigned num_expanded_macros_counter = 0;
    from macro expansion.  */
 unsigned num_macro_tokens_counter = 0;
 
+/* Wrapper around cpp_get_token to skip CPP_PADDING tokens
+   and not consume CPP_EOF.  */
+static const cpp_token *
+cpp_get_token_no_padding (cpp_reader *pfile)
+{
+  for (;;)
+    {
+      const cpp_token *ret = cpp_peek_token (pfile, 0);
+      if (ret->type == CPP_EOF)
+       return ret;
+      ret = cpp_get_token (pfile);
+      if (ret->type != CPP_PADDING)
+       return ret;
+    }
+}
+
+/* Handle meeting "__has_include" builtin macro.  */
+
+static int
+builtin_has_include (cpp_reader *pfile, cpp_hashnode *op, bool has_next)
+{
+  int result = 0;
+
+  if (!pfile->state.in_directive)
+    cpp_error (pfile, CPP_DL_ERROR,
+              "\"%s\" used outside of preprocessing directive",
+              NODE_NAME (op));
+
+  pfile->state.angled_headers = true;
+  const cpp_token *token = cpp_get_token_no_padding (pfile);
+  bool paren = token->type == CPP_OPEN_PAREN;
+  if (paren)
+    token = cpp_get_token_no_padding (pfile);
+  else
+    cpp_error (pfile, CPP_DL_ERROR,
+              "missing '(' before \"%s\" operand", NODE_NAME (op));
+  pfile->state.angled_headers = false;
+
+  bool bracket = token->type != CPP_STRING;
+  char *fname = NULL;
+  if (token->type == CPP_STRING || token->type == CPP_HEADER_NAME)
+    {
+      fname = XNEWVEC (char, token->val.str.len - 1);
+      memcpy (fname, token->val.str.text + 1, token->val.str.len - 2);
+      fname[token->val.str.len - 2] = '\0';
+    }
+  else if (token->type == CPP_LESS)
+    fname = _cpp_bracket_include (pfile);
+  else
+    cpp_error (pfile, CPP_DL_ERROR,
+              "operator \"%s\" requires a header-name", NODE_NAME (op));
+
+  if (fname)
+    {
+      /* Do not do the lookup if we're skipping, that's unnecessary
+        IO.  */
+      if (!pfile->state.skip_eval
+         && _cpp_has_header (pfile, fname, bracket,
+                             has_next ? IT_INCLUDE_NEXT : IT_INCLUDE))
+       result = 1;
+
+      XDELETEVEC (fname);
+    }
+
+  if (paren
+      && cpp_get_token_no_padding (pfile)->type != CPP_CLOSE_PAREN)
+    cpp_error (pfile, CPP_DL_ERROR,
+              "missing ')' after \"%s\" operand", NODE_NAME (op));
+
+  return result;
+}
+
 /* Emits a warning if NODE is a macro defined in the main file that
    has not been used.  */
 int
 _cpp_warn_if_unused_macro (cpp_reader *pfile, cpp_hashnode *node,
                           void *v ATTRIBUTE_UNUSED)
 {
-  if (node->type == NT_MACRO && !(node->flags & NODE_BUILTIN))
+  if (cpp_user_macro_p (node))
     {
       cpp_macro *macro = node->value.macro;
 
@@ -380,7 +478,7 @@ static const char * const monthnames[] =
    a builtin macro. */
 const uchar *
 _cpp_builtin_macro_text (cpp_reader *pfile, cpp_hashnode *node,
-                        source_location loc)
+                        location_t loc)
 {
   const uchar *result = NULL;
   linenum_type number = 1;
@@ -450,6 +548,8 @@ _cpp_builtin_macro_text (cpp_reader *pfile, cpp_hashnode *node,
            if (!name)
              abort ();
          }
+       if (pfile->cb.remap_filename)
+         name = pfile->cb.remap_filename (name);
        len = strlen (name);
        buf = _cpp_unaligned_alloc (pfile, len * 2 + 3);
        result = buf;
@@ -564,6 +664,16 @@ _cpp_builtin_macro_text (cpp_reader *pfile, cpp_hashnode *node,
     case BT_HAS_ATTRIBUTE:
       number = pfile->cb.has_attribute (pfile);
       break;
+
+    case BT_HAS_BUILTIN:
+      number = pfile->cb.has_builtin (pfile);
+      break;
+
+    case BT_HAS_INCLUDE:
+    case BT_HAS_INCLUDE_NEXT:
+      number = builtin_has_include (pfile, node,
+                                   node->value.builtin == BT_HAS_INCLUDE_NEXT);
+      break;
     }
 
   if (result == NULL)
@@ -583,7 +693,7 @@ _cpp_builtin_macro_text (cpp_reader *pfile, cpp_hashnode *node,
    point of the macro.  */
 static int
 builtin_macro (cpp_reader *pfile, cpp_hashnode *node,
-              source_location loc, source_location expand_loc)
+              location_t loc, location_t expand_loc)
 {
   const uchar *buf;
   size_t len;
@@ -619,7 +729,7 @@ builtin_macro (cpp_reader *pfile, cpp_hashnode *node,
         Create a macro line map and generate a virtual location for
         the token resulting from the expansion of the built-in
         macro.  */
-      source_location *virt_locs = NULL;
+      location_t *virt_locs = NULL;
       _cpp_buff *token_buf = tokens_buff_new (pfile, 1, &virt_locs);
       const line_map_macro * map =
        linemap_enter_macro (pfile->line_table, node, loc, 1);
@@ -769,7 +879,7 @@ stringify_arg (cpp_reader *pfile, macro_arg *arg)
    guaranteed to not have the PASTE_LEFT flag set.  LOCATION is
    the virtual location used for error reporting.  */
 static bool
-paste_tokens (cpp_reader *pfile, source_location location,
+paste_tokens (cpp_reader *pfile, location_t location,
              const cpp_token **plhs, const cpp_token *rhs)
 {
   unsigned char *buf, *end, *lhsend;
@@ -799,7 +909,7 @@ paste_tokens (cpp_reader *pfile, source_location location,
   lhs = _cpp_lex_direct (pfile);
   if (pfile->buffer->cur != pfile->buffer->rlimit)
     {
-      source_location saved_loc = lhs->src_loc;
+      location_t saved_loc = lhs->src_loc;
 
       _cpp_pop_buffer (pfile);
       _cpp_backup_tokens (pfile, 1);
@@ -837,7 +947,7 @@ paste_all_tokens (cpp_reader *pfile, const cpp_token *lhs)
 {
   const cpp_token *rhs = NULL;
   cpp_context *context = pfile->context;
-  source_location virt_loc = 0;
+  location_t virt_loc = 0;
 
   /* We are expanding a macro and we must have been called on a token
      that appears at the left hand side of a ## operator.  */
@@ -899,7 +1009,7 @@ paste_all_tokens (cpp_reader *pfile, const cpp_token *lhs)
   /* Put the resulting token in its own context.  */
   if (context->tokens_kind == TOKENS_KIND_EXTENDED)
     {
-      source_location *virt_locs = NULL;
+      location_t *virt_locs = NULL;
       _cpp_buff *token_buf = tokens_buff_new (pfile, 1, &virt_locs);
       tokens_buff_add_token (token_buf, virt_locs, lhs,
                             virt_loc, 0, NULL, 0);
@@ -925,7 +1035,7 @@ _cpp_arguments_ok (cpp_reader *pfile, cpp_macro *macro, const cpp_hashnode *node
 
   if (argc < macro->paramc)
     {
-      /* In C++2a (here the va_opt flag is used), and also as a GNU
+      /* In C++20 (here the va_opt flag is used), and also as a GNU
         extension, variadic arguments are allowed to not appear in
         the invocation at all.
         e.g. #define debug(format, args...) something
@@ -960,6 +1070,10 @@ _cpp_arguments_ok (cpp_reader *pfile, cpp_macro *macro, const cpp_hashnode *node
               "macro \"%s\" passed %u arguments, but takes just %u",
               NODE_NAME (node), argc, macro->paramc);
 
+  if (macro->line > RESERVED_LOCATION_COUNT)
+    cpp_error_at (pfile, CPP_DL_NOTE, macro->line, "macro \"%s\" defined here",
+                 NODE_NAME (node));
+
   return false;
 }
 
@@ -985,7 +1099,7 @@ collect_args (cpp_reader *pfile, const cpp_hashnode *node,
   macro_arg *args, *arg;
   const cpp_token *token;
   unsigned int argc;
-  source_location virt_loc;
+  location_t virt_loc;
   bool track_macro_expansion_p = CPP_OPTION (pfile, track_macro_expansion);
   unsigned num_args_alloced = 0;
 
@@ -1022,7 +1136,7 @@ collect_args (cpp_reader *pfile, const cpp_hashnode *node,
       if (track_macro_expansion_p)
        {
          virt_locs_capacity = DEFAULT_NUM_TOKENS_PER_MACRO_ARG;
-         arg->virt_locs = XNEWVEC (source_location,
+         arg->virt_locs = XNEWVEC (location_t,
                                    virt_locs_capacity);
        }
 
@@ -1040,7 +1154,7 @@ collect_args (cpp_reader *pfile, const cpp_hashnode *node,
              && (ntokens + 2 > virt_locs_capacity))
            {
              virt_locs_capacity += ARG_TOKENS_EXTENT;
-             arg->virt_locs = XRESIZEVEC (source_location,
+             arg->virt_locs = XRESIZEVEC (location_t,
                                           arg->virt_locs,
                                           virt_locs_capacity);
            }
@@ -1144,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\"",
@@ -1231,13 +1347,14 @@ funlike_invocation_p (cpp_reader *pfile, cpp_hashnode *node,
 static inline unsigned int
 macro_real_token_count (const cpp_macro *macro)
 {
-  unsigned int i;
   if (__builtin_expect (!macro->extra_tokens, true))
     return macro->count;
-  for (i = 0; i < macro->count; i++)
-    if (macro->exp.tokens[i].type == CPP_PASTE)
-      return i;
-  abort ();
+
+  for (unsigned i = macro->count; i--;)
+    if (macro->exp.tokens[i].type != CPP_PASTE)
+      return i + 1;
+
+  return 0;
 }
 
 /* Push the context of a macro with hash entry NODE onto the context
@@ -1251,7 +1368,7 @@ macro_real_token_count (const cpp_macro *macro)
    macro.  */
 static int
 enter_macro_context (cpp_reader *pfile, cpp_hashnode *node,
-                    const cpp_token *result, source_location location)
+                    const cpp_token *result, location_t location)
 {
   /* The presence of a macro invalidates a file's controlling macro.  */
   pfile->mi_valid = false;
@@ -1269,17 +1386,7 @@ 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))
-    {
-      node->flags |= NODE_USED;
-      if ((!pfile->cb.user_builtin_macro
-          || !pfile->cb.user_builtin_macro (pfile, node))
-         && pfile->cb.used_define)
-       pfile->cb.used_define (pfile, pfile->directive_line, node);
-    }
-
-  /* Handle standard macros.  */
-  if (! (node->flags & NODE_BUILTIN))
+  if (cpp_user_macro_p (node))
     {
       cpp_macro *macro = node->value.macro;
       _cpp_buff *pragma_buff = NULL;
@@ -1325,13 +1432,9 @@ enter_macro_context (cpp_reader *pfile, cpp_hashnode *node,
       /* Disable the macro within its expansion.  */
       node->flags |= NODE_DISABLED;
 
-      if (!(node->flags & NODE_USED))
-       {
-         node->flags |= NODE_USED;
-         if (pfile->cb.used_define)
-           pfile->cb.used_define (pfile, pfile->directive_line, node);
-       }
-
+      /* Laziness can only affect the expansion tokens of the macro,
+        not its fun-likeness or parameters.  */
+      _cpp_maybe_notify_macro_use (pfile, node);
       if (pfile->cb.used)
        pfile->cb.used (pfile, location, node);
 
@@ -1345,7 +1448,7 @@ enter_macro_context (cpp_reader *pfile, cpp_hashnode *node,
              unsigned int i;
              const cpp_token *src = macro->exp.tokens;
              const line_map_macro *map;
-             source_location *virt_locs = NULL;
+             location_t *virt_locs = NULL;
              _cpp_buff *macro_tokens
                = tokens_buff_new (pfile, tokens_count, &virt_locs);
 
@@ -1406,29 +1509,23 @@ enter_macro_context (cpp_reader *pfile, cpp_hashnode *node,
   pfile->about_to_expand_macro_p = false;
   /* Handle built-in macros and the _Pragma operator.  */
   {
-    source_location loc, expand_loc;
+    location_t expand_loc;
 
     if (/* The top-level macro invocation that triggered the expansion
-          we are looking at is with a standard macro ...*/
-       !(pfile->top_most_macro_node->flags & NODE_BUILTIN)
-       /* ... and it's a function-like macro invocation.  */
-       && pfile->top_most_macro_node->value.macro->fun_like)
-      {
-       /* Then the location of the end of the macro invocation is the
-          location of the closing parenthesis.  */
-       loc = pfile->cur_token[-1].src_loc;
-       expand_loc = loc;
-      }
+          we are looking at is with a function-like user macro ...  */
+       cpp_fun_like_macro_p (pfile->top_most_macro_node)
+       /* ... and we are tracking the macro expansion.  */
+       && CPP_OPTION (pfile, track_macro_expansion))
+      /* Then the location of the end of the macro invocation is the
+        location of the expansion point of this macro.  */
+      expand_loc = location;
     else
-      {
-       /* Otherwise, the location of the end of the macro invocation is
-          the location of the expansion point of that top-level macro
-          invocation.  */
-       loc = location;
-       expand_loc = pfile->invocation_location;
-      }
+      /* Otherwise, the location of the end of the macro invocation is
+        the location of the expansion point of that top-level macro
+        invocation.  */
+      expand_loc = pfile->invocation_location;
 
-    return builtin_macro (pfile, node, loc, expand_loc);
+    return builtin_macro (pfile, node, location, expand_loc);
   }
 }
 
@@ -1478,12 +1575,12 @@ delete_macro_args (_cpp_buff *buff, unsigned num_args)
    tokens, at least.  */
 static void
 set_arg_token (macro_arg *arg, const cpp_token *token,
-              source_location location, size_t index,
+              location_t location, size_t index,
               enum macro_arg_token_kind kind,
               bool track_macro_exp_p)
 {
   const cpp_token **token_ptr;
-  source_location *loc = NULL;
+  location_t *loc = NULL;
 
   token_ptr =
     arg_token_ptr_at (arg, index, kind,
@@ -1504,13 +1601,13 @@ set_arg_token (macro_arg *arg, const cpp_token *token,
 /* Get the pointer to the location of the argument token of the
    function-like macro argument ARG.  This function must be called
    only when we -ftrack-macro-expansion is on.  */
-static const source_location *
+static const location_t *
 get_arg_token_location (const macro_arg *arg,
                        enum macro_arg_token_kind kind)
 {
-  const source_location *loc = NULL;
+  const location_t *loc = NULL;
   const cpp_token **token_ptr =
-    arg_token_ptr_at (arg, 0, kind, (source_location **) &loc);
+    arg_token_ptr_at (arg, 0, kind, (location_t **) &loc);
 
   if (token_ptr == NULL)
     return NULL;
@@ -1527,7 +1624,7 @@ get_arg_token_location (const macro_arg *arg,
 static const cpp_token **
 arg_token_ptr_at (const macro_arg *arg, size_t index,
                  enum macro_arg_token_kind kind,
-                 source_location **virt_location)
+                 location_t **virt_location)
 {
   const cpp_token **tokens_ptr = NULL;
 
@@ -1557,7 +1654,7 @@ arg_token_ptr_at (const macro_arg *arg, size_t index,
        *virt_location = &arg->expanded_virt_locs[index];
       else if (kind == MACRO_ARG_TOKEN_STRINGIFIED)
        *virt_location =
-         (source_location *) &tokens_ptr[index]->src_loc;
+         (location_t *) &tokens_ptr[index]->src_loc;
     }
   return &tokens_ptr[index];
 }
@@ -1634,7 +1731,7 @@ macro_arg_token_iter_get_token (const macro_arg_token_iter *it)
 }
 
 /* Return the location of the token pointed to by the iterator.*/
-static source_location
+static location_t
 macro_arg_token_iter_get_location (const macro_arg_token_iter *it)
 {
 #if CHECKING_P
@@ -1699,6 +1796,30 @@ expanded_token_index (cpp_reader *pfile, cpp_macro *macro,
   return cur_replacement_token - macro->exp.tokens;
 }
 
+/* Copy whether PASTE_LEFT is set from SRC to *PASTE_FLAG.  */
+
+static void
+copy_paste_flag (cpp_reader *pfile, const cpp_token **paste_flag,
+                const cpp_token *src)
+{
+  cpp_token *token = _cpp_temp_token (pfile);
+  token->type = (*paste_flag)->type;
+  token->val = (*paste_flag)->val;
+  if (src->flags & PASTE_LEFT)
+    token->flags = (*paste_flag)->flags | PASTE_LEFT;
+  else
+    token->flags = (*paste_flag)->flags & ~PASTE_LEFT;
+  *paste_flag = token;
+}
+
+/* True IFF the last token emitted into BUFF (if any) is PTR.  */
+
+static bool
+last_token_is (_cpp_buff *buff, const cpp_token **ptr)
+{
+  return (ptr && tokens_buff_last_token_ptr (buff) == ptr);
+}
+
 /* Replace the parameters in a function-like macro of NODE with the
    actual ARGS, and place the result in a newly pushed token context.
    Expand each argument before replacing, unless it is operated upon
@@ -1707,14 +1828,14 @@ expanded_token_index (cpp_reader *pfile, cpp_macro *macro,
    function-like macro invocation.  */
 static void
 replace_args (cpp_reader *pfile, cpp_hashnode *node, cpp_macro *macro,
-             macro_arg *args, source_location expansion_point_loc)
+             macro_arg *args, location_t expansion_point_loc)
 {
   unsigned int i, total;
   const cpp_token *src, *limit;
   const cpp_token **first = NULL;
   macro_arg *arg;
   _cpp_buff *buff = NULL;
-  source_location *virt_locs = NULL;
+  location_t *virt_locs = NULL;
   unsigned int exp_count;
   const line_map_macro *map = NULL;
   int track_macro_exp;
@@ -1752,7 +1873,7 @@ replace_args (cpp_reader *pfile, cpp_hashnode *node, cpp_macro *macro,
              arg->stringified = stringify_arg (pfile, arg);
          }
        else if ((src->flags & PASTE_LEFT)
-                || (src > macro->exp.tokens && (src[-1].flags & PASTE_LEFT)))
+                || (src != macro->exp.tokens && (src[-1].flags & PASTE_LEFT)))
          total += arg->count - 1;
        else
          {
@@ -1784,7 +1905,7 @@ replace_args (cpp_reader *pfile, cpp_hashnode *node, cpp_macro *macro,
      
      As far as tokens are concerned, the memory overhead of
      -ftrack-macro-expansion is proportional to the number of
-     macros that get expanded multiplied by sizeof (source_location).
+     macros that get expanded multiplied by sizeof (location_t).
      The good news is that extra memory gets freed when the macro
      context is freed, i.e shortly after the macro got expanded.  */
 
@@ -1817,9 +1938,9 @@ replace_args (cpp_reader *pfile, cpp_hashnode *node, cpp_macro *macro,
           multiple tokens. This is to save memory at the expense of
           accuracy.
 
-          Suppose we have #define SQARE(A) A * A
+          Suppose we have #define SQUARE(A) A * A
 
-          And then we do SQARE(2+3)
+          And then we do SQUARE(2+3)
 
           Then the tokens 2, +, 3, will have the same location,
           saying they come from the expansion of the argument A.  */
@@ -1829,8 +1950,8 @@ replace_args (cpp_reader *pfile, cpp_hashnode *node, cpp_macro *macro,
                                 num_macro_tokens);
     }
   i = 0;
-  vaopt_state vaopt_tracker (pfile, macro->variadic,
-                            args[macro->paramc - 1].count > 0);
+  vaopt_state vaopt_tracker (pfile, macro->variadic, &args[macro->paramc - 1]);
+  const cpp_token **vaopt_start = NULL;
   for (src = macro->exp.tokens; src < limit; src++)
     {
       unsigned int arg_tokens_count;
@@ -1839,8 +1960,58 @@ replace_args (cpp_reader *pfile, cpp_hashnode *node, cpp_macro *macro,
       const cpp_token **tmp_token_ptr;
 
       /* __VA_OPT__ handling.  */
-      if (vaopt_tracker.update (src) != vaopt_state::INCLUDE)
-       continue;
+      vaopt_state::update_type vostate = vaopt_tracker.update (src);
+      if (vostate != vaopt_state::INCLUDE)
+       {
+         if (vostate == vaopt_state::BEGIN)
+           {
+             /* Padding on the left of __VA_OPT__ (unless RHS of ##).  */
+             if (src != macro->exp.tokens && !(src[-1].flags & PASTE_LEFT))
+               {
+                 const cpp_token *t = padding_token (pfile, src);
+                 unsigned index = expanded_token_index (pfile, macro, src, i);
+                 /* Allocate a virtual location for the padding token and
+                    append the token and its location to BUFF and
+                    VIRT_LOCS.   */
+                 tokens_buff_add_token (buff, virt_locs, t,
+                                        t->src_loc, t->src_loc,
+                                        map, index);
+               }
+             vaopt_start = tokens_buff_last_token_ptr (buff);
+           }
+         else if (vostate == vaopt_state::END)
+           {
+             const cpp_token **start = vaopt_start;
+             vaopt_start = NULL;
+
+             /* Remove any tail padding from inside the __VA_OPT__.  */
+             paste_flag = tokens_buff_last_token_ptr (buff);
+             while (paste_flag && paste_flag != start
+                    && (*paste_flag)->type == CPP_PADDING)
+               {
+                 tokens_buff_remove_last_token (buff);
+                 paste_flag = tokens_buff_last_token_ptr (buff);
+               }
+
+             if (src->flags & PASTE_LEFT)
+               {
+                 /* With a non-empty __VA_OPT__ on the LHS of ##, the last
+                    token should be flagged PASTE_LEFT.  */
+                 if (paste_flag && (*paste_flag)->type != CPP_PADDING)
+                   copy_paste_flag (pfile, paste_flag, src);
+               }
+             else
+               {
+                 /* Otherwise, avoid paste on RHS, __VA_OPT__(c)d or
+                    __VA_OPT__(c)__VA_OPT__(d).  */
+                 const cpp_token *t = &pfile->avoid_paste;
+                 tokens_buff_add_token (buff, virt_locs,
+                                        t, t->src_loc, t->src_loc,
+                                        NULL, 0);
+               }
+           }
+         continue;
+       }
 
       if (src->type != CPP_MACRO_ARG)
        {
@@ -1919,8 +2090,11 @@ replace_args (cpp_reader *pfile, cpp_hashnode *node, cpp_macro *macro,
                  else
                    paste_flag = tmp_token_ptr;
                }
-             /* Remove the paste flag if the RHS is a placemarker.  */
-             else if (arg_tokens_count == 0)
+             /* Remove the paste flag if the RHS is a placemarker, unless the
+                previous emitted token is at the beginning of __VA_OPT__;
+                placemarkers within __VA_OPT__ are ignored in that case.  */
+             else if (arg_tokens_count == 0
+                      && tmp_token_ptr != vaopt_start)
                paste_flag = tmp_token_ptr;
            }
        }
@@ -1932,11 +2106,26 @@ replace_args (cpp_reader *pfile, cpp_hashnode *node, cpp_macro *macro,
                                                 track_macro_expansion),
                                     MACRO_ARG_TOKEN_EXPANDED,
                                     arg, arg->expanded);
+
+         if (last_token_is (buff, vaopt_start))
+           {
+             /* We're expanding an arg at the beginning of __VA_OPT__.
+                Skip padding. */
+             while (arg_tokens_count)
+               {
+                 const cpp_token *t = macro_arg_token_iter_get_token (&from);
+                 if (t->type != CPP_PADDING)
+                   break;
+                 macro_arg_token_iter_forward (&from);
+                 --arg_tokens_count;
+               }
+           }
        }
 
       /* Padding on the left of an argument (unless RHS of ##).  */
       if ((!pfile->state.in_directive || pfile->state.directive_wants_padding)
-         && src != macro->exp.tokens && !(src[-1].flags & PASTE_LEFT))
+         && src != macro->exp.tokens && !(src[-1].flags & PASTE_LEFT)
+         && !last_token_is (buff, vaopt_start))
        {
          const cpp_token *t = padding_token (pfile, src);
          unsigned index = expanded_token_index (pfile, macro, src, i);
@@ -1960,9 +2149,9 @@ replace_args (cpp_reader *pfile, cpp_hashnode *node, cpp_macro *macro,
                 save extra memory while tracking macro expansion
                 locations.  So in that case here is what we do:
 
-                Suppose we have #define SQARE(A) A * A
+                Suppose we have #define SQUARE(A) A * A
 
-                And then we do SQARE(2+3)
+                And then we do SQUARE(2+3)
 
                 Then the tokens 2, +, 3, will have the same location,
                 saying they come from the expansion of the argument
@@ -1970,9 +2159,9 @@ replace_args (cpp_reader *pfile, cpp_hashnode *node, cpp_macro *macro,
 
              So that means we are going to ignore the COUNT tokens
              resulting from the expansion of the current macro
-             arugment. In other words all the ARG_TOKENS_COUNT tokens
+             argument. In other words all the ARG_TOKENS_COUNT tokens
              resulting from the expansion of the macro argument will
-             have the index I. Normally, each of those token should
+             have the index I.  Normally, each of those tokens should
              have index I+J.  */
              unsigned token_index = i;
              unsigned index;
@@ -1990,8 +2179,8 @@ replace_args (cpp_reader *pfile, cpp_hashnode *node, cpp_macro *macro,
          /* With a non-empty argument on the LHS of ##, the last
             token should be flagged PASTE_LEFT.  */
          if (src->flags & PASTE_LEFT)
-           paste_flag =
-             (const cpp_token **) tokens_buff_last_token_ptr (buff);
+           paste_flag
+             (const cpp_token **) tokens_buff_last_token_ptr (buff);
        }
       else if (CPP_PEDANTIC (pfile) && ! CPP_OPTION (pfile, c99)
               && ! macro->syshdr && ! cpp_in_system_header (pfile))
@@ -2021,7 +2210,8 @@ replace_args (cpp_reader *pfile, cpp_hashnode *node, cpp_macro *macro,
                     NODE_NAME (node), src->val.macro_arg.arg_no);
 
       /* Avoid paste on RHS (even case count == 0).  */
-      if (!pfile->state.in_directive && !(src->flags & PASTE_LEFT))
+      if (!pfile->state.in_directive && !(src->flags & PASTE_LEFT)
+         && !last_token_is (buff, vaopt_start))
        {
          const cpp_token *t = &pfile->avoid_paste;
          tokens_buff_add_token (buff, virt_locs,
@@ -2031,16 +2221,7 @@ replace_args (cpp_reader *pfile, cpp_hashnode *node, cpp_macro *macro,
 
       /* Add a new paste flag, or remove an unwanted one.  */
       if (paste_flag)
-       {
-         cpp_token *token = _cpp_temp_token (pfile);
-         token->type = (*paste_flag)->type;
-         token->val = (*paste_flag)->val;
-         if (src->flags & PASTE_LEFT)
-           token->flags = (*paste_flag)->flags | PASTE_LEFT;
-         else
-           token->flags = (*paste_flag)->flags & ~PASTE_LEFT;
-         *paste_flag = token;
-       }
+       copy_paste_flag (pfile, paste_flag, src);
 
       i += arg_tokens_count;
     }
@@ -2142,7 +2323,7 @@ static void
 push_extended_tokens_context (cpp_reader *pfile,
                              cpp_hashnode *macro,
                              _cpp_buff *token_buff,
-                             source_location *virt_locs,
+                             location_t *virt_locs,
                              const cpp_token **first,
                              unsigned int count)
 {
@@ -2188,13 +2369,13 @@ _cpp_push_text_context (cpp_reader *pfile, cpp_hashnode *macro,
    expansion.  */
 static _cpp_buff*
 tokens_buff_new (cpp_reader *pfile, size_t len,
-                source_location **virt_locs)
+                location_t **virt_locs)
 {
   size_t tokens_size = len * sizeof (cpp_token *);
-  size_t locs_size = len * sizeof (source_location);
+  size_t locs_size = len * sizeof (location_t);
 
   if (virt_locs != NULL)
-    *virt_locs = XNEWVEC (source_location, locs_size);
+    *virt_locs = XNEWVEC (location_t, locs_size);
   return _cpp_get_buff (pfile, tokens_size);
 }
 
@@ -2211,6 +2392,8 @@ tokens_buff_count (_cpp_buff *buff)
 static const cpp_token **
 tokens_buff_last_token_ptr (_cpp_buff *buff)
 {
+  if (BUFF_FRONT (buff) == buff->base)
+    return NULL;
   return &((const cpp_token **) BUFF_FRONT (buff))[-1];
 }
 
@@ -2249,14 +2432,14 @@ tokens_buff_remove_last_token (_cpp_buff *tokens_buff)
    point.  */
 static inline const cpp_token **
 tokens_buff_put_token_to (const cpp_token **dest,
-                         source_location *virt_loc_dest,
+                         location_t *virt_loc_dest,
                          const cpp_token *token,
-                         source_location virt_loc,
-                         source_location parm_def_loc,                   
+                         location_t virt_loc,
+                         location_t parm_def_loc,
                          const line_map_macro *map,
                          unsigned int macro_token_index)
 {
-  source_location macro_loc = virt_loc;
+  location_t macro_loc = virt_loc;
   const cpp_token **result;
 
   if (virt_loc_dest)
@@ -2294,15 +2477,15 @@ tokens_buff_put_token_to (const cpp_token **dest,
    position of the token coming right after the insertion point.  */
 static const cpp_token **
 tokens_buff_add_token (_cpp_buff *buffer,
-                      source_location *virt_locs,
+                      location_t *virt_locs,
                       const cpp_token *token,
-                      source_location virt_loc,
-                      source_location parm_def_loc,
+                      location_t virt_loc,
+                      location_t parm_def_loc,
                       const line_map_macro *map,
                       unsigned int macro_token_index)
 {
   const cpp_token **result;
-  source_location *virt_loc_dest = NULL;
+  location_t *virt_loc_dest = NULL;
   unsigned token_index = 
     (BUFF_FRONT (buffer) - buffer->base) / sizeof (cpp_token *);
 
@@ -2334,7 +2517,7 @@ alloc_expanded_arg_mem (cpp_reader *pfile, macro_arg *arg, size_t capacity)
 
   arg->expanded = XNEWVEC (const cpp_token *, capacity);
   if (CPP_OPTION (pfile, track_macro_expansion))
-    arg->expanded_virt_locs = XNEWVEC (source_location, capacity);
+    arg->expanded_virt_locs = XNEWVEC (location_t, capacity);
 
 }
 
@@ -2356,9 +2539,9 @@ ensure_expanded_arg_room (cpp_reader *pfile, macro_arg *arg,
   if (CPP_OPTION (pfile, track_macro_expansion))
     {
       if (arg->expanded_virt_locs == NULL)
-       arg->expanded_virt_locs = XNEWVEC (source_location, size);
+       arg->expanded_virt_locs = XNEWVEC (location_t, size);
       else
-       arg->expanded_virt_locs = XRESIZEVEC (source_location,
+       arg->expanded_virt_locs = XRESIZEVEC (location_t,
                                              arg->expanded_virt_locs,
                                              size);
     }
@@ -2401,7 +2584,7 @@ expand_arg (cpp_reader *pfile, macro_arg *arg)
   for (;;)
     {
       const cpp_token *token;
-      source_location location;
+      location_t location;
 
       ensure_expanded_arg_room (pfile, arg, arg->expanded_count + 1,
                                &capacity);
@@ -2540,7 +2723,7 @@ reached_end_of_context (cpp_context *context)
 static inline void
 consume_next_token_from_context (cpp_reader *pfile,
                                 const cpp_token ** token,
-                                source_location *location)
+                                location_t *location)
 {
   cpp_context *c = pfile->context;
 
@@ -2579,8 +2762,8 @@ consume_next_token_from_context (cpp_reader *pfile,
    location if we are in the traditional mode, and just returns
    LOCATION otherwise.  */
 
-static inline source_location
-maybe_adjust_loc_for_trad_cpp (cpp_reader *pfile, source_location location)
+static inline location_t
+maybe_adjust_loc_for_trad_cpp (cpp_reader *pfile, location_t location)
 {
   if (CPP_OPTION (pfile, traditional))
     {
@@ -2605,12 +2788,12 @@ maybe_adjust_loc_for_trad_cpp (cpp_reader *pfile, source_location location)
    cpp_get_token_with_location to learn more about the meaning of this
    location.  */
 static const cpp_token*
-cpp_get_token_1 (cpp_reader *pfile, source_location *location)
+cpp_get_token_1 (cpp_reader *pfile, location_t *location)
 {
   const cpp_token *result;
   /* This token is a virtual token that either encodes a location
      related to macro expansion or a spelling location.  */
-  source_location virt_loc = 0;
+  location_t virt_loc = 0;
   /* pfile->about_to_expand_macro_p can be overriden by indirect calls
      to functions that push macro contexts.  So let's save it so that
      we can restore it when we are about to leave this routine.  */
@@ -2659,7 +2842,7 @@ cpp_get_token_1 (cpp_reader *pfile, source_location *location)
 
       node = result->val.node.node;
 
-      if (node->type != NT_MACRO || (result->flags & NO_EXPAND))
+      if (node->type == NT_VOID || (result->flags & NO_EXPAND))
        break;
 
       if (!(node->flags & NODE_DISABLED))
@@ -2689,8 +2872,7 @@ cpp_get_token_1 (cpp_reader *pfile, source_location *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
@@ -2707,8 +2889,7 @@ cpp_get_token_1 (cpp_reader *pfile, source_location *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)
@@ -2808,7 +2989,7 @@ cpp_get_token (cpp_reader *pfile)
    location is just the same thing as its spelling location.  */
 
 const cpp_token *
-cpp_get_token_with_location (cpp_reader *pfile, source_location *loc)
+cpp_get_token_with_location (cpp_reader *pfile, location_t *loc)
 {
   return cpp_get_token_1 (pfile, loc);
 }
@@ -2904,23 +3085,18 @@ _cpp_backup_tokens (cpp_reader *pfile, unsigned int count)
 
 /* #define directive parsing and handling.  */
 
-/* Returns nonzero if a macro redefinition warning is required.  */
+/* Returns true if a macro redefinition warning is required.  */
 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.  */
   if (node->flags & NODE_WARN)
     return true;
 
   /* 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)))
+  if (cpp_builtin_macro_p (node))
     return CPP_OPTION (pfile, warn_builtin_macro_redefined);
 
   /* Redefinitions of conditional (context-sensitive) macros, on
@@ -2928,9 +3104,17 @@ warn_of_redefinition (cpp_reader *pfile, cpp_hashnode *node,
   if (node->flags & NODE_CONDITIONAL)
     return false;
 
+  cpp_macro *macro1 = node->value.macro;
+  if (macro1->lazy)
+    {
+      /* We don't want to mark MACRO as used, but do need to finalize
+        its laziness.  */
+      pfile->cb.user_lazy_macro (pfile, macro1, macro1->lazy - 1);
+      macro1->lazy = 0;
+    }
+
   /* 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.  */
@@ -2940,18 +3124,18 @@ warn_of_redefinition (cpp_reader *pfile, cpp_hashnode *node,
     return true;
 
   /* Check parameter spellings.  */
-  for (i = 0; i < macro1->paramc; i++)
-    if (macro1->params[i] != macro2->params[i])
+  for (unsigned i = macro1->paramc; i--; )
+    if (macro1->parm.params[i] != macro2->parm.params[i])
       return true;
 
   /* Check the replacement text or tokens.  */
-  if (CPP_OPTION (pfile, traditional))
+  if (macro1->kind == cmk_traditional)
     return _cpp_expansions_different_trad (macro1, macro2);
 
   if (macro1->count != macro2->count)
     return true;
 
-  for (i = 0; i < macro1->count; i++)
+  for (unsigned i= macro1->count; i--; )
     if (!_cpp_equiv_tokens (&macro1->exp.tokens[i], &macro2->exp.tokens[i]))
       return true;
 
@@ -2964,124 +3148,164 @@ _cpp_free_definition (cpp_hashnode *h)
 {
   /* Macros and assertions no longer have anything to free.  */
   h->type = NT_VOID;
-  /* Clear builtin flag in case of redefinition.  */
-  h->flags &= ~(NODE_BUILTIN | NODE_DISABLED | NODE_USED);
+  h->value.answers = NULL;
+  h->flags &= ~(NODE_DISABLED | NODE_USED);
 }
 
 /* Save parameter NODE (spelling SPELLING) to the parameter list of
-   macro MACRO.  Returns zero on success, nonzero if the parameter is
-   a duplicate.  */
+   macro MACRO.  Returns true on success, false on failure.   */
 bool
-_cpp_save_parameter (cpp_reader *pfile, cpp_macro *macro, cpp_hashnode *node,
+_cpp_save_parameter (cpp_reader *pfile, unsigned n, cpp_hashnode *node,
                     cpp_hashnode *spelling)
 {
-  unsigned int len;
   /* Constraint 6.10.3.6 - duplicate parameter names.  */
-  if (node->flags & NODE_MACRO_ARG)
+  if (node->type == NT_MACRO_ARG)
     {
       cpp_error (pfile, CPP_DL_ERROR, "duplicate macro parameter \"%s\"",
                 NODE_NAME (node));
-      return true;
+      return false;
     }
 
-  if (BUFF_ROOM (pfile->a_buff)
-      < (macro->paramc + 1) * sizeof (cpp_hashnode *))
-    _cpp_extend_buff (pfile, &pfile->a_buff, sizeof (cpp_hashnode *));
-
-  ((cpp_hashnode **) BUFF_FRONT (pfile->a_buff))[macro->paramc++] = spelling;
-  node->flags |= NODE_MACRO_ARG;
-  len = macro->paramc * sizeof (struct macro_arg_saved_data);
+  unsigned len = (n + 1) * sizeof (struct macro_arg_saved_data);
   if (len > pfile->macro_buffer_len)
     {
-      pfile->macro_buffer = XRESIZEVEC (unsigned char, pfile->macro_buffer,
-                                        len);
+      pfile->macro_buffer
+       = XRESIZEVEC (unsigned char, pfile->macro_buffer, len);
       pfile->macro_buffer_len = len;
     }
-  struct macro_arg_saved_data save;
-  save.value = node->value;
-  save.canonical_node = node;
-  ((struct macro_arg_saved_data *) pfile->macro_buffer)[macro->paramc - 1]
-    = save;
   
-  node->value.arg_index  = macro->paramc;
-  return false;
+  macro_arg_saved_data *saved = (macro_arg_saved_data *)pfile->macro_buffer;
+  saved[n].canonical_node = node;
+  saved[n].value = node->value;
+  saved[n].type = node->type;
+
+  void *base = _cpp_reserve_room (pfile, n * sizeof (cpp_hashnode *),
+                                 sizeof (cpp_hashnode *));
+  ((cpp_hashnode **)base)[n] = spelling;
+
+  /* Morph into a macro arg.  */
+  node->type = NT_MACRO_ARG;
+  /* Index is 1 based.  */
+  node->value.arg_index = n + 1;
+
+  return true;
+}
+
+/* Restore the parameters to their previous state.  */
+void
+_cpp_unsave_parameters (cpp_reader *pfile, unsigned n)
+{
+  /* Clear the fast argument lookup indices.  */
+  while (n--)
+    {
+      struct macro_arg_saved_data *save =
+       &((struct macro_arg_saved_data *) pfile->macro_buffer)[n];
+
+      struct cpp_hashnode *node = save->canonical_node;
+      node->type = save->type;
+      node->value = save->value;
+    }
 }
 
-/* Check the syntax of the parameters in a MACRO definition.  Returns
-   false if an error occurs.  */
+/* Check the syntax of the parameters in a MACRO definition.  Return
+   false on failure.  Set *N_PTR and *VARADIC_PTR as appropriate.
+   '(' ')'
+   '(' parm-list ',' last-parm ')'
+   '(' last-parm ')'
+   parm-list: name
+            | parm-list, name
+   last-parm: name
+           | name '...'
+            | '...'
+*/
+
 static bool
-parse_params (cpp_reader *pfile, cpp_macro *macro)
+parse_params (cpp_reader *pfile, unsigned *n_ptr, bool *varadic_ptr)
 {
-  unsigned int prev_ident = 0;
+  unsigned nparms = 0;
+  bool ok = false;
 
-  for (;;)
+  for (bool prev_ident = false;;)
     {
       const cpp_token *token = _cpp_lex_token (pfile);
 
       switch (token->type)
        {
-       default:
+       case CPP_COMMENT:
          /* Allow/ignore comments in parameter lists if we are
             preserving comments in macro expansions.  */
-         if (token->type == CPP_COMMENT
-             && ! CPP_OPTION (pfile, discard_comments_in_macro_exp))
-           continue;
+         if (!CPP_OPTION (pfile, discard_comments_in_macro_exp))
+           break;
 
-         cpp_error (pfile, CPP_DL_ERROR,
-                    "\"%s\" may not appear in macro parameter list",
-                    cpp_token_as_text (pfile, token));
-         return false;
+         /* FALLTHRU  */
+       default:
+       bad:
+         {
+           const char *const msgs[5] =
+             {
+              N_("expected parameter name, found \"%s\""),
+              N_("expected ',' or ')', found \"%s\""),
+              N_("expected parameter name before end of line"),
+              N_("expected ')' before end of line"),
+              N_("expected ')' after \"...\"")
+             };
+           unsigned ix = prev_ident;
+           const unsigned char *as_text = NULL;
+           if (*varadic_ptr)
+             ix = 4;
+           else if (token->type == CPP_EOF)
+             ix += 2;
+           else
+             as_text = cpp_token_as_text (pfile, token);
+           cpp_error (pfile, CPP_DL_ERROR, msgs[ix], as_text);
+         }
+         goto out;
 
        case CPP_NAME:
-         if (prev_ident)
-           {
-             cpp_error (pfile, CPP_DL_ERROR,
-                        "macro parameters must be comma-separated");
-             return false;
-           }
-         prev_ident = 1;
-
-         if (_cpp_save_parameter (pfile, macro, token->val.node.node,
-                                  token->val.node.spelling))
-           return false;
-         continue;
+         if (prev_ident || *varadic_ptr)
+           goto bad;
+         prev_ident = true;
+
+         if (!_cpp_save_parameter (pfile, nparms, token->val.node.node,
+                                   token->val.node.spelling))
+           goto out;
+         nparms++;
+         break;
 
        case CPP_CLOSE_PAREN:
-         if (prev_ident || macro->paramc == 0)
-           return true;
+         if (prev_ident || !nparms || *varadic_ptr)
+           {
+             ok = true;
+             goto out;
+           }
 
-         /* Fall through to pick up the error.  */
          /* FALLTHRU */
        case CPP_COMMA:
-         if (!prev_ident)
-           {
-             cpp_error (pfile, CPP_DL_ERROR, "parameter name missing");
-             return false;
-           }
-         prev_ident = 0;
-         continue;
+         if (!prev_ident || *varadic_ptr)
+           goto bad;
+         prev_ident = false;
+         break;
 
        case CPP_ELLIPSIS:
-         macro->variadic = 1;
+         if (*varadic_ptr)
+           goto bad;
+         *varadic_ptr = true;
          if (!prev_ident)
            {
-             _cpp_save_parameter (pfile, macro,
+             /* An ISO bare ellipsis.  */
+             _cpp_save_parameter (pfile, nparms,
                                   pfile->spec_nodes.n__VA_ARGS__,
                                   pfile->spec_nodes.n__VA_ARGS__);
+             nparms++;
              pfile->state.va_args_ok = 1;
              if (! CPP_OPTION (pfile, c99)
                  && CPP_OPTION (pfile, cpp_pedantic)
                  && CPP_OPTION (pfile, warn_variadic_macros))
-               {
-                 if (CPP_OPTION (pfile, cplusplus))
-                   cpp_pedwarning
-                       (pfile, CPP_W_VARIADIC_MACROS,
-                       "anonymous variadic macros were introduced in C++11");
-                 else
-                   cpp_pedwarning
-                       (pfile, CPP_W_VARIADIC_MACROS,
-                       "anonymous variadic macros were introduced in C99");
-               }
+               cpp_pedwarning
+                 (pfile, CPP_W_VARIADIC_MACROS,
+                  CPP_OPTION (pfile, cplusplus)
+                  ? N_("anonymous variadic macros were introduced in C++11")
+                  : N_("anonymous variadic macros were introduced in C99"));
              else if (CPP_OPTION (pfile, cpp_warn_c90_c99_compat) > 0
                       && ! CPP_OPTION (pfile, cplusplus))
                cpp_error (pfile, CPP_DL_WARNING,
@@ -3089,54 +3313,38 @@ parse_params (cpp_reader *pfile, cpp_macro *macro)
            }
          else if (CPP_OPTION (pfile, cpp_pedantic)
                   && CPP_OPTION (pfile, warn_variadic_macros))
-           {
-             if (CPP_OPTION (pfile, cplusplus))
-               cpp_pedwarning (pfile, CPP_W_VARIADIC_MACROS,
-                           "ISO C++ does not permit named variadic macros");
-             else
-               cpp_pedwarning (pfile, CPP_W_VARIADIC_MACROS,
-                           "ISO C does not permit named variadic macros");
-           }
-
-         /* We're at the end, and just expect a closing parenthesis.  */
-         token = _cpp_lex_token (pfile);
-         if (token->type == CPP_CLOSE_PAREN)
-           return true;
-         /* Fall through.  */
-
-       case CPP_EOF:
-         cpp_error (pfile, CPP_DL_ERROR, "missing ')' in macro parameter list");
-         return false;
+           cpp_pedwarning (pfile, CPP_W_VARIADIC_MACROS,
+                           CPP_OPTION (pfile, cplusplus)
+                           ? N_("ISO C++ does not permit named variadic macros")
+                           : N_("ISO C does not permit named variadic macros"));
+         break;
        }
     }
-}
 
-/* Allocate room for a token from a macro's replacement list.  */
-static cpp_token *
-alloc_expansion_token (cpp_reader *pfile, cpp_macro *macro)
-{
-  if (BUFF_ROOM (pfile->a_buff) < (macro->count + 1) * sizeof (cpp_token))
-    _cpp_extend_buff (pfile, &pfile->a_buff, sizeof (cpp_token));
+ out:
+  *n_ptr = nparms;
 
-  return &((cpp_token *) BUFF_FRONT (pfile->a_buff))[macro->count++];
+  return ok;
 }
 
 /* Lex a token from the expansion of MACRO, but mark parameters as we
    find them and warn of traditional stringification.  */
-static cpp_token *
+static cpp_macro *
 lex_expansion_token (cpp_reader *pfile, cpp_macro *macro)
 {
-  cpp_token *token, *saved_cur_token;
-
-  saved_cur_token = pfile->cur_token;
-  pfile->cur_token = alloc_expansion_token (pfile, macro);
-  token = _cpp_lex_direct (pfile);
+  macro = (cpp_macro *)_cpp_reserve_room (pfile,
+                                         sizeof (cpp_macro) - sizeof (cpp_token)
+                                         + macro->count * sizeof (cpp_token),
+                                         sizeof (cpp_token));
+  cpp_token *saved_cur_token = pfile->cur_token;
+  pfile->cur_token = &macro->exp.tokens[macro->count];
+  cpp_token *token = _cpp_lex_direct (pfile);
   pfile->cur_token = saved_cur_token;
 
   /* Is this a parameter?  */
-  if (token->type == CPP_NAME
-      && (token->val.node.node->flags & NODE_MACRO_ARG) != 0)
+  if (token->type == CPP_NAME && token->val.node.node->type == NT_MACRO_ARG)
     {
+      /* Morph into a parameter reference.  */
       cpp_hashnode *spelling = token->val.node.spelling;
       token->type = CPP_MACRO_ARG;
       token->val.macro_arg.arg_no = token->val.node.node->value.arg_index;
@@ -3146,62 +3354,58 @@ lex_expansion_token (cpp_reader *pfile, cpp_macro *macro)
           && (token->type == CPP_STRING || token->type == CPP_CHAR))
     check_trad_stringification (pfile, macro, &token->val.str);
 
-  return token;
+  return macro;
 }
 
-static bool
-create_iso_definition (cpp_reader *pfile, cpp_macro *macro)
+static cpp_macro *
+create_iso_definition (cpp_reader *pfile)
 {
-  cpp_token *token;
-  const cpp_token *ctoken;
   bool following_paste_op = false;
   const char *paste_op_error_msg =
     N_("'##' cannot appear at either end of a macro expansion");
   unsigned int num_extra_tokens = 0;
+  unsigned nparms = 0;
+  cpp_hashnode **params = NULL;
+  bool varadic = false;
+  bool ok = false;
+  cpp_macro *macro = NULL;
+
+  /* Look at the first token, to see if this is a function-like
+     macro.   */
+  cpp_token first;
+  cpp_token *saved_cur_token = pfile->cur_token;
+  pfile->cur_token = &first;
+  cpp_token *token = _cpp_lex_direct (pfile);
+  pfile->cur_token = saved_cur_token;
 
-  /* Get the first token of the expansion (or the '(' of a
-     function-like macro).  */
-  ctoken = _cpp_lex_token (pfile);
-
-  if (ctoken->type == CPP_OPEN_PAREN && !(ctoken->flags & PREV_WHITE))
+  if (token->flags & PREV_WHITE)
+    /* Preceeded by space, must be part of expansion.  */;
+  else if (token->type == CPP_OPEN_PAREN)
     {
-      bool ok = parse_params (pfile, macro);
-      macro->params = (cpp_hashnode **) BUFF_FRONT (pfile->a_buff);
-      if (!ok)
-       return false;
+      /* An open-paren, get a parameter list.  */
+      if (!parse_params (pfile, &nparms, &varadic))
+       goto out;
 
-      /* Success.  Commit or allocate the parameter array.  */
-      if (pfile->hash_table->alloc_subobject)
-       {
-         cpp_hashnode **params =
-            (cpp_hashnode **) pfile->hash_table->alloc_subobject
-            (sizeof (cpp_hashnode *) * macro->paramc);
-         memcpy (params, macro->params,
-                 sizeof (cpp_hashnode *) * macro->paramc);
-         macro->params = params;
-       }
-      else
-       BUFF_FRONT (pfile->a_buff) = (uchar *) &macro->params[macro->paramc];
-      macro->fun_like = 1;
+      params = (cpp_hashnode **)_cpp_commit_buff
+       (pfile, sizeof (cpp_hashnode *) * nparms);
+      token = NULL;
     }
-  else if (ctoken->type != CPP_EOF && !(ctoken->flags & PREV_WHITE))
+  else if (token->type != CPP_EOF
+          && !(token->type == CPP_COMMENT
+               && ! CPP_OPTION (pfile, discard_comments_in_macro_exp)))
     {
       /* While ISO C99 requires whitespace before replacement text
         in a macro definition, ISO C90 with TC1 allows characters
         from the basic source character set there.  */
       if (CPP_OPTION (pfile, c99))
-       {
-         if (CPP_OPTION (pfile, cplusplus))
-           cpp_error (pfile, CPP_DL_PEDWARN,
-                      "ISO C++11 requires whitespace after the macro name");
-         else
-           cpp_error (pfile, CPP_DL_PEDWARN,
-                      "ISO C99 requires whitespace after the macro name");
-       }
+       cpp_error (pfile, CPP_DL_PEDWARN,
+                  CPP_OPTION (pfile, cplusplus)
+                  ? N_("ISO C++11 requires whitespace after the macro name")
+                  : N_("ISO C99 requires whitespace after the macro name"));
       else
        {
-         int warntype = CPP_DL_WARNING;
-         switch (ctoken->type)
+         enum cpp_diagnostic_level warntype = CPP_DL_WARNING;
+         switch (token->type)
            {
            case CPP_ATSIGN:
            case CPP_AT_NAME:
@@ -3212,7 +3416,7 @@ create_iso_definition (cpp_reader *pfile, cpp_macro *macro)
            case CPP_OTHER:
              /* Basic character set sans letters, digits and _.  */
              if (strchr ("!\"#%&'()*+,-./:;<=>?[\\]^{|}~",
-                         ctoken->val.str.text[0]) == NULL)
+                         token->val.str.text[0]) == NULL)
                warntype = CPP_DL_PEDWARN;
              break;
            default:
@@ -3225,19 +3429,32 @@ create_iso_definition (cpp_reader *pfile, cpp_macro *macro)
        }
     }
 
-  if (macro->fun_like)
-    token = lex_expansion_token (pfile, macro);
+  macro = _cpp_new_macro (pfile, cmk_macro,
+                         _cpp_reserve_room (pfile, 0, sizeof (cpp_macro)));
+
+  if (!token)
+    {
+      macro->variadic = varadic;
+      macro->paramc = nparms;
+      macro->parm.params = params;
+      macro->fun_like = true;
+    }
   else
     {
-      token = alloc_expansion_token (pfile, macro);
-      *token = *ctoken;
+      /* Preserve the token we peeked, there is already a single slot for it.  */
+      macro->exp.tokens[0] = *token;
+      token = &macro->exp.tokens[0];
+      macro->count = 1;
     }
 
-  /* The argument doesn't matter here.  */
-  vaopt_state vaopt_tracker (pfile, macro->variadic, true);
-
-  for (;;)
+  for (vaopt_state vaopt_tracker (pfile, macro->variadic, NULL);; token = NULL)
     {
+      if (!token)
+       {
+         macro = lex_expansion_token (pfile, macro);
+         token = &macro->exp.tokens[macro->count++];
+       }
+
       /* Check the stringifying # constraint 6.10.3.2.1 of
         function-like macros when lexing the subsequent token.  */
       if (macro->count > 1 && token[-1].type == CPP_HASH && macro->fun_like)
@@ -3259,7 +3476,7 @@ create_iso_definition (cpp_reader *pfile, cpp_macro *macro)
            {
              cpp_error (pfile, CPP_DL_ERROR,
                         "'#' is not followed by a macro parameter");
-             return false;
+             goto out;
            }
        }
 
@@ -3271,8 +3488,10 @@ create_iso_definition (cpp_reader *pfile, cpp_macro *macro)
          if (following_paste_op)
            {
              cpp_error (pfile, CPP_DL_ERROR, paste_op_error_msg);
-             return false;
+             goto out;
            }
+         if (!vaopt_tracker.completed ())
+           goto out;
          break;
        }
 
@@ -3284,17 +3503,19 @@ create_iso_definition (cpp_reader *pfile, cpp_macro *macro)
          if (macro->count == 1)
            {
              cpp_error (pfile, CPP_DL_ERROR, paste_op_error_msg);
-             return false;
+             goto out;
            }
 
-         if (token[-1].flags & PASTE_LEFT)
+         if (following_paste_op)
            {
-             macro->extra_tokens = 1;
+             /* Consecutive paste operators.  This one will be moved
+                to the end.  */
              num_extra_tokens++;
              token->val.token_no = macro->count - 1;
            }
          else
            {
+             /* Drop the paste operator.  */
              --macro->count;
              token[-1].flags |= PASTE_LEFT;
              if (token->flags & DIGRAPH)
@@ -3302,78 +3523,67 @@ create_iso_definition (cpp_reader *pfile, cpp_macro *macro)
              if (token->flags & PREV_WHITE)
                token[-1].flags |= SP_PREV_WHITE;
            }
+         following_paste_op = true;
        }
+      else
+       following_paste_op = false;
 
       if (vaopt_tracker.update (token) == vaopt_state::ERROR)
-       return false;
-
-      following_paste_op = (token->type == CPP_PASTE);
-      token = lex_expansion_token (pfile, macro);
+       goto out;
     }
 
-  if (!vaopt_tracker.completed ())
-    return false;
-
-  macro->exp.tokens = (cpp_token *) BUFF_FRONT (pfile->a_buff);
-  macro->traditional = 0;
+  /* We're committed to winning now.  */
+  ok = true;
 
   /* Don't count the CPP_EOF.  */
   macro->count--;
 
-  /* Clear whitespace on first token for warn_of_redefinition().  */
+  macro = (cpp_macro *)_cpp_commit_buff
+    (pfile, sizeof (cpp_macro) - sizeof (cpp_token)
+     + sizeof (cpp_token) * macro->count);
+
+  /* Clear whitespace on first token.  */
   if (macro->count)
     macro->exp.tokens[0].flags &= ~PREV_WHITE;
 
-  /* Commit or allocate the memory.  */
-  if (pfile->hash_table->alloc_subobject)
+  if (num_extra_tokens)
     {
-      cpp_token *tokns =
-        (cpp_token *) pfile->hash_table->alloc_subobject (sizeof (cpp_token)
-                                                          * macro->count);
-      if (num_extra_tokens)
-       {
-         /* Place second and subsequent ## or %:%: tokens in
-            sequences of consecutive such tokens at the end of the
-            list to preserve information about where they appear, how
-            they are spelt and whether they are preceded by
-            whitespace without otherwise interfering with macro
-            expansion.  */
-         cpp_token *normal_dest = tokns;
-         cpp_token *extra_dest = tokns + macro->count - num_extra_tokens;
-         unsigned int i;
-         for (i = 0; i < macro->count; i++)
-           {
-             if (macro->exp.tokens[i].type == CPP_PASTE)
-               *extra_dest++ = macro->exp.tokens[i];
-             else
-               *normal_dest++ = macro->exp.tokens[i];
-           }
-       }
-      else
-       memcpy (tokns, macro->exp.tokens, sizeof (cpp_token) * macro->count);
-      macro->exp.tokens = tokns;
+      /* Place second and subsequent ## or %:%: tokens in sequences of
+        consecutive such tokens at the end of the list to preserve
+        information about where they appear, how they are spelt and
+        whether they are preceded by whitespace without otherwise
+        interfering with macro expansion.   Remember, this is
+        extremely rare, so efficiency is not a priority.  */
+      cpp_token *temp = (cpp_token *)_cpp_reserve_room
+       (pfile, 0, num_extra_tokens * sizeof (cpp_token));
+      unsigned extra_ix = 0, norm_ix = 0;
+      cpp_token *exp = macro->exp.tokens;
+      for (unsigned ix = 0; ix != macro->count; ix++)
+       if (exp[ix].type == CPP_PASTE)
+         temp[extra_ix++] = exp[ix];
+       else
+         exp[norm_ix++] = exp[ix];
+      memcpy (&exp[norm_ix], temp, num_extra_tokens * sizeof (cpp_token));
+
+      /* Record there are extra tokens.  */
+      macro->extra_tokens = 1;
     }
-  else
-    BUFF_FRONT (pfile->a_buff) = (uchar *) &macro->exp.tokens[macro->count];
 
-  return true;
+ out:
+  pfile->state.va_args_ok = 0;
+  _cpp_unsave_parameters (pfile, nparms);
+
+  return ok ? macro : NULL;
 }
 
-/* Parse a macro and save its expansion.  Returns nonzero on success.  */
-bool
-_cpp_create_definition (cpp_reader *pfile, cpp_hashnode *node)
+cpp_macro *
+_cpp_new_macro (cpp_reader *pfile, cpp_macro_kind kind, void *placement)
 {
-  cpp_macro *macro;
-  unsigned int i;
-  bool ok;
+  cpp_macro *macro = (cpp_macro *) placement;
 
-  if (pfile->hash_table->alloc_subobject)
-    macro = (cpp_macro *) pfile->hash_table->alloc_subobject
-      (sizeof (cpp_macro));
-  else
-    macro = (cpp_macro *) _cpp_aligned_alloc (pfile, sizeof (cpp_macro));
   macro->line = pfile->directive_line;
-  macro->params = 0;
+  macro->parm.params = 0;
+  macro->lazy = 0;
   macro->paramc = 0;
   macro->variadic = 0;
   macro->used = !CPP_OPTION (pfile, warn_unused_macros);
@@ -3383,62 +3593,51 @@ _cpp_create_definition (cpp_reader *pfile, cpp_hashnode *node)
   /* To suppress some diagnostics.  */
   macro->syshdr = pfile->buffer && pfile->buffer->sysp != 0;
 
-  if (CPP_OPTION (pfile, traditional))
-    ok = _cpp_create_trad_definition (pfile, macro);
-  else
-    {
-      ok = create_iso_definition (pfile, macro);
+  macro->kind = kind;
 
-      /* We set the type for SEEN_EOL() in directives.c.
-
-        Longer term we should lex the whole line before coming here,
-        and just copy the expansion.  */
+  return macro;
+}
 
-      /* Stop the lexer accepting __VA_ARGS__.  */
-      pfile->state.va_args_ok = 0;
-    }
+/* Parse a macro and save its expansion.  Returns nonzero on success.  */
+bool
+_cpp_create_definition (cpp_reader *pfile, cpp_hashnode *node)
+{
+  cpp_macro *macro;
 
-  /* Clear the fast argument lookup indices.  */
-  for (i = macro->paramc; i-- > 0; )
-    {
-      struct macro_arg_saved_data *save =
-       &((struct macro_arg_saved_data *) pfile->macro_buffer)[i];
-      struct cpp_hashnode *node = save->canonical_node;
-      node->flags &= ~ NODE_MACRO_ARG;
-      node->value = save->value;
-    }
+  if (CPP_OPTION (pfile, traditional))
+    macro = _cpp_create_trad_definition (pfile);
+  else
+    macro = create_iso_definition (pfile);
 
-  if (!ok)
-    return ok;
+  if (!macro)
+    return false;
 
-  if (node->type == NT_MACRO)
+  if (cpp_macro_p (node))
     {
       if (CPP_OPTION (pfile, warn_unused_macros))
        _cpp_warn_if_unused_macro (pfile, node, NULL);
 
       if (warn_of_redefinition (pfile, node, macro))
        {
-          const int reason = ((node->flags & NODE_BUILTIN)
-                             && !(node->flags & NODE_WARN))
-                             ? CPP_W_BUILTIN_MACRO_REDEFINED : CPP_W_NONE;
+          const enum cpp_warning_reason reason
+           = (cpp_builtin_macro_p (node) && !(node->flags & NODE_WARN))
+           ? CPP_W_BUILTIN_MACRO_REDEFINED : CPP_W_NONE;
 
          bool warned = 
            cpp_pedwarning_with_line (pfile, reason,
                                      pfile->directive_line, 0,
                                      "\"%s\" redefined", NODE_NAME (node));
 
-         if (warned && node->type == NT_MACRO && !(node->flags & NODE_BUILTIN))
+         if (warned && cpp_user_macro_p (node))
            cpp_error_with_line (pfile, CPP_DL_NOTE,
                                 node->value.macro->line, 0,
                         "this is the location of the previous definition");
        }
+      _cpp_free_definition (node);
     }
 
-  if (node->type != NT_VOID)
-    _cpp_free_definition (node);
-
   /* Enter definition in hash table.  */
-  node->type = NT_MACRO;
+  node->type = NT_USER_MACRO;
   node->value.macro = macro;
   if (! ustrncmp (NODE_NAME (node), DSC ("__STDC_"))
       && ustrcmp (NODE_NAME (node), (const uchar *) "__STDC_FORMAT_MACROS")
@@ -3454,7 +3653,52 @@ _cpp_create_definition (cpp_reader *pfile, cpp_hashnode *node)
      conditional flag */
   node->flags &= ~NODE_CONDITIONAL;
 
-  return ok;
+  return true;
+}
+
+extern void
+cpp_define_lazily (cpp_reader *pfile, cpp_hashnode *node, unsigned num)
+{
+  cpp_macro *macro = node->value.macro;
+
+  gcc_checking_assert (pfile->cb.user_lazy_macro && macro && num < UCHAR_MAX);
+
+  macro->lazy = num + 1;
+}
+
+/* Notify the use of NODE in a macro-aware context (i.e. expanding it,
+   or testing its existance).  Also applies any lazy definition.  */
+
+extern void
+_cpp_notify_macro_use (cpp_reader *pfile, cpp_hashnode *node)
+{
+  node->flags |= NODE_USED;
+  switch (node->type)
+    {
+    case NT_USER_MACRO:
+      {
+       cpp_macro *macro = node->value.macro;
+       if (macro->lazy)
+         {
+           pfile->cb.user_lazy_macro (pfile, macro, macro->lazy - 1);
+           macro->lazy = 0;
+         }
+      }
+      /* FALLTHROUGH.  */
+
+    case NT_BUILTIN_MACRO:
+      if (pfile->cb.used_define)
+       pfile->cb.used_define (pfile, pfile->directive_line, node);
+      break;
+
+    case NT_VOID:
+      if (pfile->cb.used_undef)
+       pfile->cb.used_undef (pfile, pfile->directive_line, node);
+      break;
+
+    default:
+      abort ();
+    }
 }
 
 /* Warn if a token in STRING matches one of a function-like MACRO's
@@ -3485,12 +3729,12 @@ check_trad_stringification (cpp_reader *pfile, const cpp_macro *macro,
         identifier inside the string matches one of them.  */
       for (i = 0; i < macro->paramc; i++)
        {
-         const cpp_hashnode *node = macro->params[i];
+         const cpp_hashnode *node = macro->parm.params[i];
 
          if (NODE_LEN (node) == len
              && !memcmp (p, NODE_NAME (node), len))
            {
-             cpp_error (pfile, CPP_DL_WARNING,
+             cpp_warning (pfile, CPP_W_TRADITIONAL,
           "macro argument \"%s\" would be stringified in traditional C",
                         NODE_NAME (node));
              break;
@@ -3499,15 +3743,6 @@ check_trad_stringification (cpp_reader *pfile, const cpp_macro *macro,
     }
 }
 
-/* Returns true of NODE is a function-like macro.  */
-bool
-cpp_fun_like_macro_p (cpp_hashnode *node)
-{
-  return (node->type == NT_MACRO
-         && (node->flags & (NODE_BUILTIN | NODE_MACRO_ARG)) == 0
-         && node->value.macro->fun_like);
-}
-
 /* Returns the name, arguments and expansion of a macro, in a format
    suitable to be read back in again, and therefore also for DWARF 2
    debugging info.  e.g. "PASTE(X, Y) X ## Y", or "MACNAME EXPANSION".
@@ -3517,23 +3752,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))
-       {
-         cpp_error (pfile, CPP_DL_ICE,
-                    "invalid hash type %d in cpp_macro_definition",
-                    node->type);
-         return 0;
-       }
-    }
+  gcc_checking_assert (cpp_user_macro_p (node));
+
+  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)
@@ -3541,7 +3765,7 @@ cpp_macro_definition (cpp_reader *pfile, cpp_hashnode *node)
       len += 4;                /* "()" plus possible final ".." of named
                           varargs (we have + 1 below).  */
       for (i = 0; i < macro->paramc; i++)
-       len += NODE_LEN (macro->params[i]) + 1; /* "," */
+       len += NODE_LEN (macro->parm.params[i]) + 1; /* "," */
     }
 
   /* This should match below where we fill in the buffer.  */
@@ -3552,7 +3776,7 @@ cpp_macro_definition (cpp_reader *pfile, cpp_hashnode *node)
       unsigned int count = macro_real_token_count (macro);
       for (i = 0; i < count; i++)
        {
-         cpp_token *token = &macro->exp.tokens[i];
+         const cpp_token *token = &macro->exp.tokens[i];
 
          if (token->type == CPP_MACRO_ARG)
            len += NODE_LEN (token->val.macro_arg.spelling);
@@ -3585,7 +3809,7 @@ cpp_macro_definition (cpp_reader *pfile, cpp_hashnode *node)
       *buffer++ = '(';
       for (i = 0; i < macro->paramc; i++)
        {
-         cpp_hashnode *param = macro->params[i];
+         cpp_hashnode *param = macro->parm.params[i];
 
          if (param != pfile->spec_nodes.n__VA_ARGS__)
            {
@@ -3616,7 +3840,7 @@ cpp_macro_definition (cpp_reader *pfile, cpp_hashnode *node)
       unsigned int count = macro_real_token_count (macro);
       for (i = 0; i < count; i++)
        {
-         cpp_token *token = &macro->exp.tokens[i];
+         const cpp_token *token = &macro->exp.tokens[i];
 
          if (token->flags & PREV_WHITE)
            *buffer++ = ' ';
@@ -3646,11 +3870,3 @@ cpp_macro_definition (cpp_reader *pfile, cpp_hashnode *node)
   *buffer = '\0';
   return pfile->macro_buffer;
 }
-
-/* Get the line at which the macro was defined.  */
-
-source_location
-cpp_macro_definition_location (cpp_hashnode *node)
-{
-  return node->value.macro->line;
-}