]> git.ipfire.org Git - thirdparty/gcc.git/blobdiff - libcpp/macro.c
tree-optimization/95495 - use SLP_TREE_REPRESENTATIVE in assertion
[thirdparty/gcc.git] / libcpp / macro.c
index f4638c49cd99f0e418ac8e3cad862ffaf5e98c69..2c7d7322e09c6ca425b58392f591dc5bc87de70f 100644 (file)
@@ -1,7 +1,5 @@
 /* Part of CPP library.  (Macro and #define handling.)
-   Copyright (C) 1986, 1987, 1989, 1992, 1993, 1994, 1995, 1996, 1998,
-   1999, 2000, 2001, 2002, 2003, 2004, 2005,
-   2006, 2007, 2008, 2009, 2010, 2011, 2012 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
@@ -41,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.  */
 };
@@ -53,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,48 +70,235 @@ struct macro_arg_token_iter
   /* A pointer to the current token pointed to by the iterator.  */
   const cpp_token **token_ptr;
   /* A pointer to the "full" location of the current token.  If
-     -ftrack-macro-expansion is used this location tracks loci accross
+     -ftrack-macro-expansion is used this location tracks loci across
      macro expansion.  */
-  const source_location *location_ptr;
-#ifdef ENABLE_CHECKING
+  const location_t *location_ptr;
+#if CHECKING_P
   /* The number of times the iterator went forward. This useful only
      when checking is enabled.  */
   size_t num_forwards;
 #endif
 };
 
+/* Saved data about an identifier being used as a macro argument
+   name.  */
+struct macro_arg_saved_data {
+  /* The canonical (UTF-8) spelling of this identifier.  */
+  cpp_hashnode *canonical_node;
+  /* 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.  */
+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, macro_arg *arg)
+    : m_pfile (pfile),
+    m_arg (arg),
+    m_variadic (is_variadic),
+    m_last_was_paste (false),
+    m_state (0),
+    m_paste_location (0),
+    m_location (0),
+    m_update (ERROR)
+  {
+  }
+
+  /* Given a token, update the state of this tracker and return a
+     boolean indicating whether the token should be be included in the
+     expansion.  */
+  update_type update (const cpp_token *token)
+  {
+    /* If the macro isn't variadic, just don't bother.  */
+    if (!m_variadic)
+      return INCLUDE;
+
+    if (token->type == CPP_NAME
+       && token->val.node.node == m_pfile->spec_nodes.n__VA_OPT__)
+      {
+       if (m_state > 0)
+         {
+           cpp_error_at (m_pfile, CPP_DL_ERROR, token->src_loc,
+                         "__VA_OPT__ may not appear in a __VA_OPT__");
+           return ERROR;
+         }
+       ++m_state;
+       m_location = token->src_loc;
+       return BEGIN;
+      }
+    else if (m_state == 1)
+      {
+       if (token->type != CPP_OPEN_PAREN)
+         {
+           cpp_error_at (m_pfile, CPP_DL_ERROR, m_location,
+                         "__VA_OPT__ must be followed by an "
+                         "open parenthesis");
+           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)
+      {
+       if (m_state == 2 && token->type == CPP_PASTE)
+         {
+           cpp_error_at (m_pfile, CPP_DL_ERROR, token->src_loc,
+                         vaopt_paste_error);
+           return ERROR;
+         }
+       /* Advance states before further considering this token, in
+          case we see a close paren immediately after the open
+          paren.  */
+       if (m_state == 2)
+         ++m_state;
+
+       bool was_paste = m_last_was_paste;
+       m_last_was_paste = false;
+       if (token->type == CPP_PASTE)
+         {
+           m_last_was_paste = true;
+           m_paste_location = token->src_loc;
+         }
+       else if (token->type == CPP_OPEN_PAREN)
+         ++m_state;
+       else if (token->type == CPP_CLOSE_PAREN)
+         {
+           --m_state;
+           if (m_state == 2)
+             {
+               /* Saw the final paren.  */
+               m_state = 0;
+
+               if (was_paste)
+                 {
+                   cpp_error_at (m_pfile, CPP_DL_ERROR, token->src_loc,
+                                 vaopt_paste_error);
+                   return ERROR;
+                 }
+
+               return END;
+             }
+         }
+       return m_update;
+      }
+
+    /* Nothing to do with __VA_OPT__.  */
+    return INCLUDE;
+  }
+
+  /* Ensure that any __VA_OPT__ was completed.  If ok, return true.
+     Otherwise, issue an error and return false.  */
+  bool completed ()
+  {
+    if (m_variadic && m_state != 0)
+      cpp_error_at (m_pfile, CPP_DL_ERROR, m_location,
+                   "unterminated __VA_OPT__");
+    return m_state == 0;
+  }
+
+ private:
+
+  /* The cpp_reader.  */
+  cpp_reader *m_pfile;
+
+  /* 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
+     1 means __VA_OPT__ seen, looking for "("
+     2 means "(" seen (so the next token can't be "##")
+     >= 3 means looking for ")", the number encodes the paren depth.  */
+  int m_state;
+
+  /* The location of the paste token.  */
+  location_t m_paste_location;
+
+  /* Location of the __VA_OPT__ token.  */
+  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);
-static int builtin_macro (cpp_reader *, cpp_hashnode *);
+                               const cpp_token *, location_t);
+static int builtin_macro (cpp_reader *, cpp_hashnode *,
+                         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 *, const cpp_token **, const cpp_token *);
+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,
@@ -121,49 +306,52 @@ 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,
-                                                          const struct line_map *,
+                                                          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,
-                                               const struct line_map *,
+                                               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);
+
+static bool in_macro_expansion_p (cpp_reader *pfile);
 
 /* Statistical counter tracking the number of macros that got
    expanded.  */
@@ -172,18 +360,92 @@ 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;
 
       if (!macro->used
-         && MAIN_FILE_P (linemap_lookup (pfile->line_table, macro->line)))
+         && MAIN_FILE_P (linemap_check_ordinary
+                           (linemap_lookup (pfile->line_table,
+                                            macro->line))))
        cpp_warning_with_line (pfile, CPP_W_UNUSED_MACROS, macro->line, 0,
                               "macro \"%s\" is not used", NODE_NAME (node));
     }
@@ -215,7 +477,8 @@ static const char * const monthnames[] =
 /* Helper function for builtin_macro.  Returns the text generated by
    a builtin macro. */
 const uchar *
-_cpp_builtin_macro_text (cpp_reader *pfile, cpp_hashnode *node)
+_cpp_builtin_macro_text (cpp_reader *pfile, cpp_hashnode *node,
+                        location_t loc)
 {
   const uchar *result = NULL;
   linenum_type number = 1;
@@ -229,6 +492,10 @@ _cpp_builtin_macro_text (cpp_reader *pfile, cpp_hashnode *node)
 
     case BT_TIMESTAMP:
       {
+       if (CPP_OPTION (pfile, warn_date_time))
+         cpp_warning (pfile, CPP_W_DATE_TIME, "macro \"%s\" might prevent "
+                      "reproducible builds", NODE_NAME (node));
+
        cpp_buffer *pbuffer = cpp_get_buffer (pfile);
        if (pbuffer->timestamp == NULL)
          {
@@ -281,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;
@@ -301,11 +570,14 @@ _cpp_builtin_macro_text (cpp_reader *pfile, cpp_hashnode *node)
     case BT_SPECLINE:
       /* If __LINE__ is embedded in a macro, it must expand to the
         line of the macro's invocation, not its definition.
-        Otherwise things like assert() will not work properly.  */
-      number = linemap_get_expansion_line (pfile->line_table,
-                                          CPP_OPTION (pfile, traditional)
-                                          ? pfile->line_table->highest_line
-                                          : pfile->cur_token[-1].src_loc);
+        Otherwise things like assert() will not work properly.
+        See WG14 N1911, WG21 N4220 sec 6.5, and PR 61861.  */
+      if (CPP_OPTION (pfile, traditional))
+       loc = pfile->line_table->highest_line;
+      else
+       loc = linemap_resolve_location (pfile->line_table, loc,
+                                       LRK_MACRO_EXPANSION_POINT, NULL);
+      number = linemap_get_expansion_line (pfile->line_table, loc);
       break;
 
       /* __STDC__ has the value 1 under normal circumstances.
@@ -322,6 +594,9 @@ _cpp_builtin_macro_text (cpp_reader *pfile, cpp_hashnode *node)
 
     case BT_DATE:
     case BT_TIME:
+      if (CPP_OPTION (pfile, warn_date_time))
+       cpp_warning (pfile, CPP_W_DATE_TIME, "macro \"%s\" might prevent "
+                    "reproducible builds", NODE_NAME (node));
       if (pfile->date == NULL)
        {
          /* Allocate __DATE__ and __TIME__ strings from permanent
@@ -331,13 +606,24 @@ _cpp_builtin_macro_text (cpp_reader *pfile, cpp_hashnode *node)
          time_t tt;
          struct tm *tb = NULL;
 
-         /* (time_t) -1 is a legitimate value for "number of seconds
-            since the Epoch", so we have to do a little dance to
-            distinguish that from a genuine error.  */
-         errno = 0;
-         tt = time(NULL);
-         if (tt != (time_t)-1 || errno == 0)
-           tb = localtime (&tt);
+         /* Set a reproducible timestamp for __DATE__ and __TIME__ macro
+            if SOURCE_DATE_EPOCH is defined.  */
+         if (pfile->source_date_epoch == (time_t) -2
+             && pfile->cb.get_source_date_epoch != NULL)
+           pfile->source_date_epoch = pfile->cb.get_source_date_epoch (pfile);
+
+         if (pfile->source_date_epoch >= (time_t) 0)
+           tb = gmtime (&pfile->source_date_epoch);
+         else
+           {
+             /* (time_t) -1 is a legitimate value for "number of seconds
+                since the Epoch", so we have to do a little dance to
+                distinguish that from a genuine error.  */
+             errno = 0;
+             tt = time (NULL);
+             if (tt != (time_t)-1 || errno == 0)
+               tb = localtime (&tt);
+           }
 
          if (tb)
            {
@@ -374,6 +660,20 @@ _cpp_builtin_macro_text (cpp_reader *pfile, cpp_hashnode *node)
            "__COUNTER__ expanded inside directive with -fdirectives-only");
       number = pfile->counter++;
       break;
+
+    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)
@@ -389,9 +689,11 @@ _cpp_builtin_macro_text (cpp_reader *pfile, cpp_hashnode *node)
 /* Convert builtin macros like __FILE__ to a token and push it on the
    context stack.  Also handles _Pragma, for which a new token may not
    be created.  Returns 1 if it generates a new token context, 0 to
-   return the token to the caller.  */
+   return the token to the caller.  LOC is the location of the expansion
+   point of the macro.  */
 static int
-builtin_macro (cpp_reader *pfile, cpp_hashnode *node)
+builtin_macro (cpp_reader *pfile, cpp_hashnode *node,
+              location_t loc, location_t expand_loc)
 {
   const uchar *buf;
   size_t len;
@@ -404,10 +706,10 @@ builtin_macro (cpp_reader *pfile, cpp_hashnode *node)
       if (pfile->state.in_directive)
        return 0;
 
-      return _cpp_do__Pragma (pfile);
+      return _cpp_do__Pragma (pfile, loc);
     }
 
-  buf = _cpp_builtin_macro_text (pfile, node);
+  buf = _cpp_builtin_macro_text (pfile, node, expand_loc);
   len = ustrlen (buf);
   nbuf = (char *) alloca (len + 1);
   memcpy (nbuf, buf, len);
@@ -418,7 +720,29 @@ builtin_macro (cpp_reader *pfile, cpp_hashnode *node)
 
   /* Set pfile->cur_token as required by _cpp_lex_direct.  */
   pfile->cur_token = _cpp_temp_token (pfile);
-  _cpp_push_token_context (pfile, NULL, _cpp_lex_direct (pfile), 1);
+  cpp_token *token = _cpp_lex_direct (pfile);
+  /* We should point to the expansion point of the builtin macro.  */
+  token->src_loc = loc;
+  if (pfile->context->tokens_kind == TOKENS_KIND_EXTENDED)
+    {
+      /* We are tracking tokens resulting from macro expansion.
+        Create a macro line map and generate a virtual location for
+        the token resulting from the expansion of the built-in
+        macro.  */
+      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);
+      tokens_buff_add_token (token_buf, virt_locs, token,
+                            pfile->line_table->builtin_location,
+                            pfile->line_table->builtin_location,
+                           map, /*macro_token_index=*/0);
+      push_extended_tokens_context (pfile, node, token_buf, virt_locs,
+                                   (const cpp_token **)token_buf->base,
+                                   1);
+    }
+  else
+    _cpp_push_token_context (pfile, NULL, token, 1);
   if (pfile->buffer->cur != pfile->buffer->rlimit)
     cpp_error (pfile, CPP_DL_ICE, "invalid built-in macro \"%s\"",
               NODE_NAME (node));
@@ -437,13 +761,21 @@ cpp_quote_string (uchar *dest, const uchar *src, unsigned int len)
     {
       uchar c = *src++;
 
-      if (c == '\\' || c == '"')
+      switch (c)
        {
+       case '\n':
+         /* Naked LF can appear in raw string literals  */
+         c = 'n';
+         /* FALLTHROUGH */
+
+       case '\\':
+       case '"':
          *dest++ = '\\';
+         /* FALLTHROUGH */
+
+       default:
          *dest++ = c;
        }
-      else
-         *dest++ = c;
     }
 
   return dest;
@@ -482,7 +814,9 @@ stringify_arg (cpp_reader *pfile, macro_arg *arg)
                   || token->type == CPP_WSTRING || token->type == CPP_WCHAR
                   || token->type == CPP_STRING32 || token->type == CPP_CHAR32
                   || token->type == CPP_STRING16 || token->type == CPP_CHAR16
-                  || token->type == CPP_UTF8STRING);
+                  || token->type == CPP_UTF8STRING || token->type == CPP_UTF8CHAR
+                  || cpp_userdef_string_p (token->type)
+                  || cpp_userdef_char_p (token->type));
 
       /* Room for each char being written in octal, initial space and
         final quote and NUL.  */
@@ -542,9 +876,11 @@ stringify_arg (cpp_reader *pfile, macro_arg *arg)
 
 /* Try to paste two tokens.  On success, return nonzero.  In any
    case, PLHS is updated to point to the pasted token, which is
-   guaranteed to not have the PASTE_LEFT flag set.  */
+   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, const cpp_token **plhs, const cpp_token *rhs)
+paste_tokens (cpp_reader *pfile, location_t location,
+             const cpp_token **plhs, const cpp_token *rhs)
 {
   unsigned char *buf, *end, *lhsend;
   cpp_token *lhs;
@@ -552,7 +888,7 @@ paste_tokens (cpp_reader *pfile, const cpp_token **plhs, const cpp_token *rhs)
 
   len = cpp_token_len (*plhs) + cpp_token_len (rhs) + 1;
   buf = (unsigned char *) alloca (len);
-  end = lhsend = cpp_spell_token (pfile, *plhs, buf, false);
+  end = lhsend = cpp_spell_token (pfile, *plhs, buf, true);
 
   /* Avoid comment headers, since they are still processed in stage 3.
      It is simpler to insert a space here, rather than modifying the
@@ -562,7 +898,7 @@ paste_tokens (cpp_reader *pfile, const cpp_token **plhs, const cpp_token *rhs)
     *end++ = ' ';
   /* In one obscure case we might see padding here.  */
   if (rhs->type != CPP_PADDING)
-    end = cpp_spell_token (pfile, rhs, end, false);
+    end = cpp_spell_token (pfile, rhs, end, true);
   *end = '\n';
 
   cpp_push_buffer (pfile, buf, end - buf, /* from_stage3 */ true);
@@ -573,7 +909,7 @@ paste_tokens (cpp_reader *pfile, const cpp_token **plhs, const cpp_token *rhs)
   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);
@@ -588,7 +924,7 @@ paste_tokens (cpp_reader *pfile, const cpp_token **plhs, const cpp_token *rhs)
 
       /* Mandatory error for all apart from assembler.  */
       if (CPP_OPTION (pfile, lang) != CLK_ASM)
-       cpp_error (pfile, CPP_DL_ERROR,
+       cpp_error_with_line (pfile, CPP_DL_ERROR, location, 0,
         "pasting \"%s\" and \"%s\" does not give a valid preprocessing token",
                   buf, cpp_token_as_text (pfile, rhs));
       return false;
@@ -611,11 +947,12 @@ 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 must have been called on a token that appears at the left
-     hand side of a ## operator.  */
-  if (!(lhs->flags & PASTE_LEFT))
+  /* We are expanding a macro and we must have been called on a token
+     that appears at the left hand side of a ## operator.  */
+  if (macro_of_context (pfile->context) == NULL
+      || (!(lhs->flags & PASTE_LEFT)))
     abort ();
 
   if (context->tokens_kind == TOKENS_KIND_EXTENDED)
@@ -626,6 +963,11 @@ paste_all_tokens (cpp_reader *pfile, const cpp_token *lhs)
        resulting pasted token to have the location of the current
        *LHS, though.  */
     virt_loc = context->c.mc->cur_virt_loc[-1];
+  else
+    /* We are not tracking macro expansion.  So the best virtual
+       location we can get here is the expansion point of the macro we
+       are currently expanding.  */
+    virt_loc = pfile->invocation_location;
 
   do
     {
@@ -659,7 +1001,7 @@ paste_all_tokens (cpp_reader *pfile, const cpp_token *lhs)
          if (rhs->flags & PASTE_LEFT)
            abort ();
        }
-      if (!paste_tokens (pfile, &lhs, rhs))
+      if (!paste_tokens (pfile, virt_loc, &lhs, rhs))
        break;
     }
   while (rhs->flags & PASTE_LEFT);
@@ -667,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);
@@ -693,19 +1035,29 @@ _cpp_arguments_ok (cpp_reader *pfile, cpp_macro *macro, const cpp_hashnode *node
 
   if (argc < macro->paramc)
     {
-      /* As an extension, a rest argument is allowed to not appear in
+      /* 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
         debug("string");
 
-        This is exactly the same as if there had been an empty rest
-        argument - debug("string", ).  */
+        This is exactly the same as if an empty variadic list had been
+        supplied - debug("string", ).  */
 
       if (argc + 1 == macro->paramc && macro->variadic)
        {
-         if (CPP_PEDANTIC (pfile) && ! macro->syshdr)
-           cpp_error (pfile, CPP_DL_PEDWARN,
-                      "ISO C99 requires rest arguments to be used");
+         if (CPP_PEDANTIC (pfile) && ! macro->syshdr
+             && ! CPP_OPTION (pfile, va_opt))
+           {
+             if (CPP_OPTION (pfile, cplusplus))
+               cpp_error (pfile, CPP_DL_PEDWARN,
+                          "ISO C++11 requires at least one argument "
+                          "for the \"...\" in a variadic macro");
+             else
+               cpp_error (pfile, CPP_DL_PEDWARN,
+                          "ISO C99 requires at least one argument "
+                          "for the \"...\" in a variadic macro");
+           }
          return true;
        }
 
@@ -718,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;
 }
 
@@ -743,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;
 
@@ -780,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);
        }
 
@@ -798,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);
            }
@@ -902,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\"",
@@ -989,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
@@ -1009,24 +1368,25 @@ 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;
 
   pfile->state.angled_headers = false;
 
-  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))
+  /* From here to when we push the context for the macro later down
+     this function, we need to flag the fact that we are about to
+     expand a macro.  This is useful when -ftrack-macro-expansion is
+     turned off.  In that case, we need to record the location of the
+     expansion point of the top-most macro we are about to to expand,
+     into pfile->invocation_location.  But we must not record any such
+     location once the process of expanding the macro starts; that is,
+     we must not do that recording between now and later down this
+     function where set this flag to FALSE.  */
+  pfile->about_to_expand_macro_p = true;
+
+  if (cpp_user_macro_p (node))
     {
       cpp_macro *macro = node->value.macro;
       _cpp_buff *pragma_buff = NULL;
@@ -1055,6 +1415,7 @@ enter_macro_context (cpp_reader *pfile, cpp_hashnode *node,
              if (pragma_buff)
                _cpp_release_buff (pfile, pragma_buff);
 
+             pfile->about_to_expand_macro_p = false;
              return 0;
            }
 
@@ -1071,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);
 
@@ -1085,21 +1442,22 @@ enter_macro_context (cpp_reader *pfile, cpp_hashnode *node,
 
       if (macro->paramc == 0)
        {
+         unsigned tokens_count = macro_real_token_count (macro);
          if (CPP_OPTION (pfile, track_macro_expansion))
            {
-             unsigned int i, count = macro->count;
+             unsigned int i;
              const cpp_token *src = macro->exp.tokens;
-             const struct line_map *map;
-             source_location *virt_locs = NULL;
-             _cpp_buff *macro_tokens =
-               tokens_buff_new (pfile, count, &virt_locs);
+             const line_map_macro *map;
+             location_t *virt_locs = NULL;
+             _cpp_buff *macro_tokens
+               = tokens_buff_new (pfile, tokens_count, &virt_locs);
 
              /* Create a macro map to record the locations of the
                 tokens that are involved in the expansion. LOCATION
                 is the location of the macro expansion point.  */
-             map  = linemap_enter_macro (pfile->line_table,
-                                         node, location, count);
-             for (i = 0; i < count; ++i)
+             map = linemap_enter_macro (pfile->line_table,
+                                        node, location, tokens_count);
+             for (i = 0; i < tokens_count; ++i)
                {
                  tokens_buff_add_token (macro_tokens, virt_locs,
                                         src, src->src_loc,
@@ -1111,16 +1469,12 @@ enter_macro_context (cpp_reader *pfile, cpp_hashnode *node,
                                            virt_locs,
                                            (const cpp_token **)
                                            macro_tokens->base,
-                                           count);
-             num_macro_tokens_counter += count;
+                                           tokens_count);
            }
          else
-           {
-             unsigned tokens_count = macro_real_token_count (macro);
-             _cpp_push_token_context (pfile, node, macro->exp.tokens,
-                                      tokens_count);
-             num_macro_tokens_counter += tokens_count;
-           }
+           _cpp_push_token_context (pfile, node, macro->exp.tokens,
+                                    tokens_count);
+         num_macro_tokens_counter += tokens_count;
        }
 
       if (pragma_buff)
@@ -1144,14 +1498,35 @@ enter_macro_context (cpp_reader *pfile, cpp_hashnode *node,
 
            }
          while (pragma_buff != NULL);
+         pfile->about_to_expand_macro_p = false;
          return 2;
        }
 
+      pfile->about_to_expand_macro_p = false;
       return 1;
     }
 
+  pfile->about_to_expand_macro_p = false;
   /* Handle built-in macros and the _Pragma operator.  */
-  return builtin_macro (pfile, node);
+  {
+    location_t expand_loc;
+
+    if (/* The top-level macro invocation that triggered the expansion
+          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.  */
+      expand_loc = pfile->invocation_location;
+
+    return builtin_macro (pfile, node, location, expand_loc);
+  }
 }
 
 /* De-allocate the memory used by BUFF which is an array of instances
@@ -1193,19 +1568,19 @@ delete_macro_args (_cpp_buff *buff, unsigned num_args)
 
 /* Set the INDEXth token of the macro argument ARG. TOKEN is the token
    to set, LOCATION is its virtual location.  "Virtual" location means
-   the location that encodes loci accross macro expansion. Otherwise
+   the location that encodes loci across macro expansion. Otherwise
    it has to be TOKEN->SRC_LOC.  KIND is the kind of tokens the
    argument ARG is supposed to contain.  Note that ARG must be
    tailored so that it has enough room to contain INDEX + 1 numbers of
    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,
@@ -1214,14 +1589,11 @@ set_arg_token (macro_arg *arg, const cpp_token *token,
 
   if (loc != NULL)
     {
-#ifdef ENABLE_CHECKING
-      if (kind == MACRO_ARG_TOKEN_STRINGIFIED
-         || !track_macro_exp_p)
-       /* We can't set the location of a stringified argument
-          token and we can't set any location if we aren't tracking
-          macro expansion locations.   */
-       abort ();
-#endif
+      /* We can't set the location of a stringified argument
+        token and we can't set any location if we aren't tracking
+        macro expansion locations.   */
+      gcc_checking_assert (kind != MACRO_ARG_TOKEN_STRINGIFIED
+                          && track_macro_exp_p);
       *loc = location;
     }
 }
@@ -1229,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;
@@ -1252,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;
 
@@ -1282,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];
 }
@@ -1307,7 +1679,7 @@ macro_arg_token_iter_init (macro_arg_token_iter *iter,
   iter->location_ptr = NULL;
   if (track_macro_exp_p)
     iter->location_ptr = get_arg_token_location (arg, kind);
-#ifdef ENABLE_CHECKING
+#if CHECKING_P
   iter->num_forwards = 0;
   if (track_macro_exp_p
       && token_ptr != NULL
@@ -1318,7 +1690,7 @@ macro_arg_token_iter_init (macro_arg_token_iter *iter,
 
 /* Move the iterator one token forward. Note that if IT was
    initialized on an argument that has a stringified token, moving it
-   foward doesn't make sense as a stringified token is essentially one
+   forward doesn't make sense as a stringified token is essentially one
    string.  */
 static void
 macro_arg_token_iter_forward (macro_arg_token_iter *it)
@@ -1332,14 +1704,14 @@ macro_arg_token_iter_forward (macro_arg_token_iter *it)
        it->location_ptr++;
       break;
     case MACRO_ARG_TOKEN_STRINGIFIED:
-#ifdef ENABLE_CHECKING
+#if CHECKING_P
       if (it->num_forwards > 0)
        abort ();
 #endif
       break;
     }
 
-#ifdef ENABLE_CHECKING
+#if CHECKING_P
   it->num_forwards++;
 #endif
 }
@@ -1348,7 +1720,7 @@ macro_arg_token_iter_forward (macro_arg_token_iter *it)
 static const cpp_token *
 macro_arg_token_iter_get_token (const macro_arg_token_iter *it)
 {
-#ifdef ENABLE_CHECKING
+#if CHECKING_P
   if (it->kind == MACRO_ARG_TOKEN_STRINGIFIED
       && it->num_forwards > 0)
     abort ();
@@ -1359,10 +1731,10 @@ 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)
 {
-#ifdef ENABLE_CHECKING
+#if CHECKING_P
   if (it->kind == MACRO_ARG_TOKEN_STRINGIFIED
       && it->num_forwards > 0)
     abort ();
@@ -1424,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
@@ -1432,16 +1828,16 @@ 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 struct line_map *map = NULL;
+  const line_map_macro *map = NULL;
   int track_macro_exp;
 
   /* First, fully macro-expand arguments, calculating the number of
@@ -1477,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
          {
@@ -1509,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.  */
 
@@ -1542,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.  */
@@ -1554,6 +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]);
+  const cpp_token **vaopt_start = NULL;
   for (src = macro->exp.tokens; src < limit; src++)
     {
       unsigned int arg_tokens_count;
@@ -1561,6 +1959,60 @@ replace_args (cpp_reader *pfile, cpp_hashnode *node, cpp_macro *macro,
       const cpp_token **paste_flag = NULL;
       const cpp_token **tmp_token_ptr;
 
+      /* __VA_OPT__ handling.  */
+      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)
        {
          /* Allocate a virtual location for token SRC, and add that
@@ -1638,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;
            }
        }
@@ -1651,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);
@@ -1679,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
@@ -1689,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;
@@ -1709,23 +2179,39 @@ 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) && ! macro->syshdr
-              && ! CPP_OPTION (pfile, c99)
-              && ! cpp_in_system_header (pfile))
+      else if (CPP_PEDANTIC (pfile) && ! CPP_OPTION (pfile, c99)
+              && ! macro->syshdr && ! cpp_in_system_header (pfile))
        {
-         cpp_error (pfile, CPP_DL_PEDWARN,
+         if (CPP_OPTION (pfile, cplusplus))
+           cpp_pedwarning (pfile, CPP_W_PEDANTIC,
+                           "invoking macro %s argument %d: "
+                           "empty macro arguments are undefined"
+                           " in ISO C++98",
+                           NODE_NAME (node), src->val.macro_arg.arg_no);
+         else if (CPP_OPTION (pfile, cpp_warn_c90_c99_compat))
+           cpp_pedwarning (pfile, 
+                           CPP_OPTION (pfile, cpp_warn_c90_c99_compat) > 0
+                           ? CPP_W_C90_C99_COMPAT : CPP_W_PEDANTIC,
+                           "invoking macro %s argument %d: "
+                           "empty macro arguments are undefined"
+                           " in ISO C90",
+                           NODE_NAME (node), src->val.macro_arg.arg_no);
+       }
+      else if (CPP_OPTION (pfile, cpp_warn_c90_c99_compat) > 0
+              && ! CPP_OPTION (pfile, cplusplus)
+              && ! macro->syshdr && ! cpp_in_system_header (pfile))
+       cpp_warning (pfile, CPP_W_C90_C99_COMPAT,
                     "invoking macro %s argument %d: "
                     "empty macro arguments are undefined"
-                    " in ISO C90 and ISO C++98",
-                    NODE_NAME (node),
-                    src->val.macro_arg.arg_no);
-       }
+                    " in ISO C90",
+                    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,
@@ -1735,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;
     }
@@ -1808,18 +2285,27 @@ push_ptoken_context (cpp_reader *pfile, cpp_hashnode *macro, _cpp_buff *buff,
   LAST (context).ptoken = first + count;
 }
 
-/* Push a list of tokens.  */
+/* Push a list of tokens.
+
+   A NULL macro means that we should continue the current macro
+   expansion, in essence.  That means that if we are currently in a
+   macro expansion context, we'll make the new pfile->context refer to
+   the current macro.  */
 void
 _cpp_push_token_context (cpp_reader *pfile, cpp_hashnode *macro,
                         const cpp_token *first, unsigned int count)
 {
-   cpp_context *context = next_context (pfile);
+  cpp_context *context;
+
+   if (macro == NULL)
+     macro = macro_of_context (pfile->context);
+
+   context = next_context (pfile);
    context->tokens_kind = TOKENS_KIND_DIRECT;
    context->c.macro = macro;
    context->buff = NULL;
-  FIRST (context).token = first;
-  LAST (context).token = first + count;
+   FIRST (context).token = first;
+   LAST (context).token = first + count;
 }
 
 /* Build a context containing a list of tokens as well as their
@@ -1827,18 +2313,27 @@ _cpp_push_token_context (cpp_reader *pfile, cpp_hashnode *macro,
    contains the tokens pointed to by FIRST.  If TOKENS_BUFF is
    non-NULL, it means that the context owns it, meaning that
    _cpp_pop_context will free it as well as VIRT_LOCS_BUFF that
-   contains the virtual locations.  */
+   contains the virtual locations.
+
+   A NULL macro means that we should continue the current macro
+   expansion, in essence.  That means that if we are currently in a
+   macro expansion context, we'll make the new pfile->context refer to
+   the current macro.  */
 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)
 {
-  cpp_context *context = next_context (pfile);
+  cpp_context *context;
   macro_context *m;
 
+  if (macro == NULL)
+    macro = macro_of_context (pfile->context);
+
+  context = next_context (pfile);
   context->tokens_kind = TOKENS_KIND_EXTENDED;
   context->buff = token_buff;
 
@@ -1874,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);
 }
 
@@ -1897,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];
 }
 
@@ -1919,7 +2416,7 @@ tokens_buff_remove_last_token (_cpp_buff *tokens_buff)
    means -ftrack-macro-expansion is effect; it then points to where to
    insert the virtual location of TOKEN.  TOKEN is the token to
    insert.  VIRT_LOC is the virtual location of the token, i.e, the
-   location possibly encoding its locus accross macro expansion.  If
+   location possibly encoding its locus across macro expansion.  If
    TOKEN is an argument of a function-like macro (inside a macro
    replacement list), PARM_DEF_LOC is the spelling location of the
    macro parameter that TOKEN is replacing, in the replacement list of
@@ -1935,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,                   
-                         const struct line_map *map,
+                         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)
@@ -1964,7 +2461,7 @@ tokens_buff_put_token_to (const cpp_token **dest,
    reaches BUFFER's size; it aborts in that situation.
 
    TOKEN is the token to append. VIRT_LOC is the virtual location of
-   the token, i.e, the location possibly encoding its locus accross
+   the token, i.e, the location possibly encoding its locus across
    macro expansion. If TOKEN is an argument of a function-like macro
    (inside a macro replacement list), PARM_DEF_LOC is the location of
    the macro parameter that TOKEN is replacing.  If TOKEN doesn't come
@@ -1980,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,
-                      const struct line_map *map,
+                      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 *);
 
@@ -2015,14 +2512,12 @@ tokens_buff_add_token (_cpp_buff *buffer,
 static void
 alloc_expanded_arg_mem (cpp_reader *pfile, macro_arg *arg, size_t capacity)
 {
-#ifdef ENABLE_CHECKING
-  if (arg->expanded != NULL
-      || arg->expanded_virt_locs != NULL)
-    abort ();
-#endif
+  gcc_checking_assert (arg->expanded == NULL
+                      && arg->expanded_virt_locs == NULL);
+
   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);
 
 }
 
@@ -2044,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);
     }
@@ -2089,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);
@@ -2110,6 +2605,33 @@ expand_arg (cpp_reader *pfile, macro_arg *arg)
   CPP_WTRADITIONAL (pfile) = saved_warn_trad;
 }
 
+/* Returns the macro associated to the current context if we are in
+   the context a macro expansion, NULL otherwise.  */
+static cpp_hashnode*
+macro_of_context (cpp_context *context)
+{
+  if (context == NULL)
+    return NULL;
+
+  return (context->tokens_kind == TOKENS_KIND_EXTENDED)
+    ? context->c.mc->macro_node
+    : context->c.macro;
+}
+
+/* Return TRUE iff we are expanding a macro or are about to start
+   expanding one.  If we are effectively expanding a macro, the
+   function macro_of_context returns a pointer to the macro being
+   expanded.  */
+static bool
+in_macro_expansion_p (cpp_reader *pfile)
+{
+  if (pfile == NULL)
+    return false;
+
+  return (pfile->about_to_expand_macro_p 
+         || macro_of_context (pfile->context));
+}
+
 /* Pop the current context off the stack, re-enabling the macro if the
    context represented a macro's replacement list.  Initially the
    context structure was not freed so that we can re-use it later, but
@@ -2119,6 +2641,10 @@ _cpp_pop_context (cpp_reader *pfile)
 {
   cpp_context *context = pfile->context;
 
+  /* We should not be popping the base context.  */
+  if (context == &pfile->base_context)
+    abort ();
+
   if (context->c.macro)
     {
       cpp_hashnode *macro;
@@ -2146,8 +2672,19 @@ _cpp_pop_context (cpp_reader *pfile)
         tokens is pushed just for the purpose of walking them using
         cpp_get_token_1.  In that case, no 'macro' field is set into
         the dummy context.  */
-      if (macro != NULL)
+      if (macro != NULL
+         /* Several contiguous macro expansion contexts can be
+            associated to the same macro; that means it's the same
+            macro expansion that spans across all these (sub)
+            contexts.  So we should re-enable an expansion-disabled
+            macro only when we are sure we are really out of that
+            macro expansion.  */
+         && macro_of_context (context->prev) != macro)
        macro->flags &= ~NODE_DISABLED;
+
+      if (macro == pfile->top_most_macro_node && context->prev == NULL)
+       /* We are popping the context of the top-most macro node.  */
+       pfile->top_most_macro_node = NULL;
     }
 
   if (context->buff)
@@ -2180,13 +2717,13 @@ reached_end_of_context (cpp_context *context)
 /* Consume the next token contained in the current context of PFILE,
    and return it in *TOKEN. It's "full location" is returned in
    *LOCATION. If -ftrack-macro-location is in effeect, fFull location"
-   means the location encoding the locus of the token accross macro
+   means the location encoding the locus of the token across macro
    expansion; otherwise it's just is the "normal" location of the
    token which (*TOKEN)->src_loc.  */
 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;
 
@@ -2225,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))
     {
@@ -2251,14 +2788,16 @@ 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;
-  bool can_set = pfile->set_invocation_location;
   /* This token is a virtual token that either encodes a location
      related to macro expansion or a spelling location.  */
-  source_location virt_loc = 0;
-  pfile->set_invocation_location = false;
+  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.  */
+  bool saved_about_to_expand_macro = pfile->about_to_expand_macro_p;
 
   for (;;)
     {
@@ -2303,16 +2842,20 @@ 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))
        {
          int ret = 0;
          /* If not in a macro context, and we're going to start an
-            expansion, record the location.  */
-         if (can_set && !context->c.macro)
-           pfile->invocation_location = result->src_loc;
+            expansion, record the location and the top level macro
+            about to be expanded.  */
+         if (!in_macro_expansion_p (pfile))
+           {
+             pfile->invocation_location = result->src_loc;
+             pfile->top_most_macro_node = node;
+           }
          if (pfile->state.prevent_expansion)
            break;
 
@@ -2329,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
@@ -2347,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)
@@ -2379,8 +2920,7 @@ cpp_get_token_1 (cpp_reader *pfile, source_location *location)
       *location = virt_loc;
 
       if (!CPP_OPTION (pfile, track_macro_expansion)
-         && can_set
-         && pfile->context->c.macro != NULL)
+         && macro_of_context (pfile->context) != NULL)
        /* We are in a macro expansion context, are not tracking
           virtual location, but were asked to report the location
           of the expansion point of the macro being expanded.  */
@@ -2388,6 +2928,8 @@ cpp_get_token_1 (cpp_reader *pfile, source_location *location)
 
       *location = maybe_adjust_loc_for_trad_cpp (pfile, *location);
     }
+
+  pfile->about_to_expand_macro_p = saved_about_to_expand_macro;
   return result;
 }
 
@@ -2447,13 +2989,9 @@ 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)
 {
-  const cpp_token *result;
-
-  pfile->set_invocation_location = true;
-  result = cpp_get_token_1 (pfile, loc);
-  return result;
+  return cpp_get_token_1 (pfile, loc);
 }
 
 /* Returns true if we're expanding an object-like macro that was
@@ -2535,10 +3073,7 @@ _cpp_backup_tokens (cpp_reader *pfile, unsigned int count)
            {
              macro_context *m = pfile->context->c.mc;
              m->cur_virt_loc--;
-#ifdef ENABLE_CHECKING
-             if (m->cur_virt_loc < m->virt_locs)
-               abort ();
-#endif
+             gcc_checking_assert (m->cur_virt_loc >= m->virt_locs);
            }
          else
            abort ();
@@ -2550,34 +3085,36 @@ _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.  */
-  if (node->flags & NODE_BUILTIN)
-    {
-      if (!pfile->cb.user_builtin_macro
-         || !pfile->cb.user_builtin_macro (pfile, node))
-       return false;
-    }
+  /* Suppress warnings for builtins that lack the NODE_WARN flag,
+     unless Wbuiltin-macro-redefined.  */
+  if (cpp_builtin_macro_p (node))
+    return CPP_OPTION (pfile, warn_builtin_macro_redefined);
 
   /* Redefinitions of conditional (context-sensitive) macros, on
      the other hand, must be allowed silently.  */
   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.  */
@@ -2587,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;
 
@@ -2611,211 +3148,264 @@ _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 to the parameter list of macro MACRO.  Returns
-   zero on success, nonzero if the parameter is a duplicate.  */
+/* Save parameter NODE (spelling SPELLING) to the parameter list of
+   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++] = node;
-  node->flags |= NODE_MACRO_ARG;
-  len = macro->paramc * sizeof (union _cpp_hashnode_value);
+  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;
     }
-  ((union _cpp_hashnode_value *) pfile->macro_buffer)[macro->paramc - 1]
-    = node->value;
   
-  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))
-           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;
-
-         /* Fall through to pick up the error.  */
-       case CPP_COMMA:
-         if (!prev_ident)
+         if (prev_ident || !nparms || *varadic_ptr)
            {
-             cpp_error (pfile, CPP_DL_ERROR, "parameter name missing");
-             return false;
+             ok = true;
+             goto out;
            }
-         prev_ident = 0;
-         continue;
+
+         /* FALLTHRU */
+       case CPP_COMMA:
+         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))
                cpp_pedwarning
-                  (pfile, CPP_W_VARIADIC_MACROS,
-                  "anonymous variadic macros were introduced in C99");
+                 (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,
+                          "anonymous variadic macros were introduced in C99");
            }
          else if (CPP_OPTION (pfile, cpp_pedantic)
                   && CPP_OPTION (pfile, warn_variadic_macros))
            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_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;
+      token->val.macro_arg.spelling = spelling;
     }
   else if (CPP_WTRADITIONAL (pfile) && macro->paramc > 0
           && (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 there characters
-        from the basic source character set.  */
+        in a macro definition, ISO C90 with TC1 allows characters
+        from the basic source character set there.  */
       if (CPP_OPTION (pfile, c99))
        cpp_error (pfile, CPP_DL_PEDWARN,
-                  "ISO C99 requires whitespace after the macro name");
+                  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:
@@ -2826,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:
@@ -2839,16 +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;
     }
 
-  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)
@@ -2870,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;
            }
        }
 
@@ -2882,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;
        }
 
@@ -2895,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)
@@ -2913,72 +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;
 
-      following_paste_op = (token->type == CPP_PASTE);
-      token = lex_expansion_token (pfile, macro);
+      if (vaopt_tracker.update (token) == vaopt_state::ERROR)
+       goto out;
     }
 
-  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);
@@ -2988,67 +3593,58 @@ _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);
-
-      /* We set the type for SEEN_EOL() in directives.c.
+  macro->kind = kind;
 
-        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 cpp_hashnode *node = macro->params[i];
-      node->flags &= ~ NODE_MACRO_ARG;
-      node->value = ((union _cpp_hashnode_value *) pfile->macro_buffer)[i];
-    }
+  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)
-                             ? CPP_W_BUILTIN_MACRO_REDEFINED : CPP_W_NONE;
-         bool warned;
+          const enum cpp_warning_reason reason
+           = (cpp_builtin_macro_p (node) && !(node->flags & NODE_WARN))
+           ? CPP_W_BUILTIN_MACRO_REDEFINED : CPP_W_NONE;
 
-         warned = cpp_pedwarning_with_line (pfile, reason,
-                                            pfile->directive_line, 0,
-                                            "\"%s\" redefined",
-                                             NODE_NAME (node));
+         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")
       /* __STDC_LIMIT_MACROS and __STDC_CONSTANT_MACROS are mentioned
         in the C standard, as something that one must use in C++.
-        However DR#593 indicates that these aren't actually mentioned
-        in the C++ standard.  We special-case them anyway.  */
+        However DR#593 and C++11 indicate that they play no role in C++.
+        We special-case them anyway.  */
       && ustrcmp (NODE_NAME (node), (const uchar *) "__STDC_LIMIT_MACROS")
       && ustrcmp (NODE_NAME (node), (const uchar *) "__STDC_CONSTANT_MACROS"))
     node->flags |= NODE_WARN;
@@ -3057,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
@@ -3088,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;
@@ -3111,31 +3752,20 @@ 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) + 2;                   /* ' ' and NUL.  */
+  len = NODE_LEN (node) * 10 + 2;              /* ' ' and NUL.  */
   if (macro->fun_like)
     {
       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.  */
@@ -3146,10 +3776,10 @@ 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 (macro->params[token->val.macro_arg.arg_no - 1]);
+           len += NODE_LEN (token->val.macro_arg.spelling);
          else
            len += cpp_token_len (token);
 
@@ -3171,8 +3801,7 @@ cpp_macro_definition (cpp_reader *pfile, cpp_hashnode *node)
 
   /* Fill in the buffer.  Start with the macro name.  */
   buffer = pfile->macro_buffer;
-  memcpy (buffer, NODE_NAME (node), NODE_LEN (node));
-  buffer += NODE_LEN (node);
+  buffer = _cpp_spell_ident_ucns (buffer, node);
 
   /* Parameter names.  */
   if (macro->fun_like)
@@ -3180,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__)
            {
@@ -3211,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++ = ' ';
@@ -3221,12 +3850,12 @@ cpp_macro_definition (cpp_reader *pfile, cpp_hashnode *node)
          if (token->type == CPP_MACRO_ARG)
            {
              memcpy (buffer,
-                     NODE_NAME (macro->params[token->val.macro_arg.arg_no - 1]),
-                     NODE_LEN (macro->params[token->val.macro_arg.arg_no - 1]));
-             buffer += NODE_LEN (macro->params[token->val.macro_arg.arg_no - 1]);
+                     NODE_NAME (token->val.macro_arg.spelling),
+                     NODE_LEN (token->val.macro_arg.spelling));
+             buffer += NODE_LEN (token->val.macro_arg.spelling);
            }
          else
-           buffer = cpp_spell_token (pfile, token, buffer, false);
+           buffer = cpp_spell_token (pfile, token, buffer, true);
 
          if (token->flags & PASTE_LEFT)
            {