OSDN Git Service

* g++.dg/cdce3.C: Skip on alpha*-dec-osf5*.
[pf3gnuchains/gcc-fork.git] / libcpp / macro.c
index 12681c3..cbb0b0e 100644 (file)
@@ -1,14 +1,14 @@
 /* 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 Free Software Foundation, Inc.
+   2006, 2007, 2008, 2009 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
 
 This program is free software; you can redistribute it and/or modify it
 under the terms of the GNU General Public License as published by the
-Free Software Foundation; either version 2, or (at your option) any
+Free Software Foundation; either version 3, or (at your option) any
 later version.
 
 This program is distributed in the hope that it will be useful,
@@ -17,8 +17,8 @@ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 GNU General Public License for more details.
 
 You should have received a copy of the GNU General Public License
-along with this program; if not, write to the Free Software
-Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+along with this program; see the file COPYING3.  If not see
+<http://www.gnu.org/licenses/>.
 
  In other words, you are welcome to use, share and improve this program.
  You are forbidden to forbid anyone else to use, share and improve
@@ -41,11 +41,13 @@ struct macro_arg
 
 /* Macro expansion.  */
 
-static int enter_macro_context (cpp_reader *, cpp_hashnode *);
+static int enter_macro_context (cpp_reader *, cpp_hashnode *,
+                               const cpp_token *);
 static int builtin_macro (cpp_reader *, cpp_hashnode *);
 static void push_ptoken_context (cpp_reader *, cpp_hashnode *, _cpp_buff *,
                                 const cpp_token **, unsigned int);
-static _cpp_buff *collect_args (cpp_reader *, const cpp_hashnode *);
+static _cpp_buff *collect_args (cpp_reader *, const cpp_hashnode *,
+                               _cpp_buff **);
 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 *);
@@ -55,7 +57,8 @@ static void paste_all_tokens (cpp_reader *, const cpp_token *);
 static bool paste_tokens (cpp_reader *, const cpp_token **, const cpp_token *);
 static void replace_args (cpp_reader *, cpp_hashnode *, cpp_macro *,
                          macro_arg *);
-static _cpp_buff *funlike_invocation_p (cpp_reader *, cpp_hashnode *);
+static _cpp_buff *funlike_invocation_p (cpp_reader *, cpp_hashnode *,
+                                       _cpp_buff **);
 static bool create_iso_definition (cpp_reader *, cpp_macro *);
 
 /* #define directive parsing and handling.  */
@@ -80,8 +83,8 @@ _cpp_warn_if_unused_macro (cpp_reader *pfile, cpp_hashnode *node,
 
       if (!macro->used
          && MAIN_FILE_P (linemap_lookup (pfile->line_table, macro->line)))
-       cpp_error_with_line (pfile, CPP_DL_WARNING, macro->line, 0,
-                            "macro \"%s\" is not used", NODE_NAME (node));
+       cpp_warning_with_line (pfile, CPP_W_UNUSED_MACROS, macro->line, 0,
+                              "macro \"%s\" is not used", NODE_NAME (node));
     }
 
   return 1;
@@ -115,7 +118,7 @@ _cpp_builtin_macro_text (cpp_reader *pfile, cpp_hashnode *node)
 {
   const struct line_map *map;
   const uchar *result = NULL;
-  unsigned int number = 1;
+  linenum_type number = 1;
 
   switch (node->value.builtin)
     {
@@ -155,7 +158,7 @@ _cpp_builtin_macro_text (cpp_reader *pfile, cpp_hashnode *node)
                  {
                    cpp_errno (pfile, CPP_DL_WARNING,
                        "could not determine file timestamp");
-                   pbuffer->timestamp = U"\"??? ??? ?? ??:??:?? ????\"";
+                   pbuffer->timestamp = UC"\"??? ??? ?? ??:??:?? ????\"";
                  }
              }
          }
@@ -197,11 +200,10 @@ _cpp_builtin_macro_text (cpp_reader *pfile, cpp_hashnode *node)
       /* 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.  */
-      if (CPP_OPTION (pfile, traditional))
-       number = pfile->line_table->highest_line;
-      else
-       number = pfile->cur_token[-1].src_loc;
-      number = SOURCE_LINE (map, number);
+      number = SOURCE_LINE (map, 
+                           CPP_OPTION (pfile, traditional) 
+                           ? pfile->line_table->highest_line
+                           : pfile->cur_token[-1].src_loc);
       break;
 
       /* __STDC__ has the value 1 under normal circumstances.
@@ -253,8 +255,8 @@ _cpp_builtin_macro_text (cpp_reader *pfile, cpp_hashnode *node)
              cpp_errno (pfile, CPP_DL_WARNING,
                         "could not determine date and time");
                
-             pfile->date = U"\"??? ?? ????\"";
-             pfile->time = U"\"??:??:??\"";
+             pfile->date = UC"\"??? ?? ????\"";
+             pfile->time = UC"\"??:??:??\"";
            }
        }
 
@@ -263,6 +265,13 @@ _cpp_builtin_macro_text (cpp_reader *pfile, cpp_hashnode *node)
       else
        result = pfile->time;
       break;
+
+    case BT_COUNTER:
+      if (CPP_OPTION (pfile, directives_only) && pfile->state.in_directive)
+       cpp_error (pfile, CPP_DL_ERROR,
+           "__COUNTER__ expanded inside directive with -fdirectives-only");
+      number = pfile->counter++;
+      break;
     }
 
   if (result == NULL)
@@ -293,8 +302,7 @@ builtin_macro (cpp_reader *pfile, cpp_hashnode *node)
       if (pfile->state.in_directive)
        return 0;
 
-      _cpp_do__Pragma (pfile);
-      return 1;
+      return _cpp_do__Pragma (pfile);
     }
 
   buf = _cpp_builtin_macro_text (pfile, node);
@@ -361,13 +369,18 @@ stringify_arg (cpp_reader *pfile, macro_arg *arg)
 
       if (token->type == CPP_PADDING)
        {
-         if (source == NULL)
+         if (source == NULL
+             || (!(source->flags & PREV_WHITE)
+                 && token->val.source == NULL))
            source = token->val.source;
          continue;
        }
 
-      escape_it = (token->type == CPP_STRING || token->type == CPP_WSTRING
-                  || token->type == CPP_CHAR || token->type == CPP_WCHAR);
+      escape_it = (token->type == CPP_STRING || token->type == CPP_CHAR
+                  || 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);
 
       /* Room for each char being written in octal, initial space and
         final quote and NUL.  */
@@ -445,7 +458,9 @@ paste_tokens (cpp_reader *pfile, const cpp_token **plhs, const cpp_token *rhs)
      false doesn't work, since we want to clear the PASTE_LEFT flag.  */
   if ((*plhs)->type == CPP_DIV && rhs->type != CPP_EQ)
     *end++ = ' ';
-  end = cpp_spell_token (pfile, rhs, end, false);
+  /* In one obscure case we might see padding here.  */
+  if (rhs->type != CPP_PADDING)
+    end = cpp_spell_token (pfile, rhs, end, false);
   *end = '\n';
 
   cpp_push_buffer (pfile, buf, end - buf, /* from_stage3 */ true);
@@ -508,8 +523,10 @@ paste_all_tokens (cpp_reader *pfile, const cpp_token *lhs)
        rhs = *FIRST (context).ptoken++;
 
       if (rhs->type == CPP_PADDING)
-       abort ();
-
+       {
+         if (rhs->flags & PASTE_LEFT)
+           abort ();
+       }
       if (!paste_tokens (pfile, &lhs, rhs))
        break;
     }
@@ -565,9 +582,12 @@ _cpp_arguments_ok (cpp_reader *pfile, cpp_macro *macro, const cpp_hashnode *node
    invocation.  Assumes the opening parenthesis has been processed.
    If there is an error, emits an appropriate diagnostic and returns
    NULL.  Each argument is terminated by a CPP_EOF token, for the
-   future benefit of expand_arg().  */
+   future benefit of expand_arg().  If there are any deferred
+   #pragma directives among macro arguments, store pointers to the
+   CPP_PRAGMA ... CPP_PRAGMA_EOL tokens into *PRAGMA_BUFF buffer.  */
 static _cpp_buff *
-collect_args (cpp_reader *pfile, const cpp_hashnode *node)
+collect_args (cpp_reader *pfile, const cpp_hashnode *node,
+             _cpp_buff **pragma_buff)
 {
   _cpp_buff *buff, *base_buff;
   cpp_macro *macro;
@@ -635,6 +655,51 @@ collect_args (cpp_reader *pfile, const cpp_hashnode *node)
          else if (token->type == CPP_EOF
                   || (token->type == CPP_HASH && token->flags & BOL))
            break;
+         else if (token->type == CPP_PRAGMA)
+           {
+             cpp_token *newtok = _cpp_temp_token (pfile);
+
+             /* CPP_PRAGMA token lives in directive_result, which will
+                be overwritten on the next directive.  */
+             *newtok = *token;
+             token = newtok;
+             do
+               {
+                 if (*pragma_buff == NULL
+                     || BUFF_ROOM (*pragma_buff) < sizeof (cpp_token *))
+                   {
+                     _cpp_buff *next;
+                     if (*pragma_buff == NULL)
+                       *pragma_buff
+                         = _cpp_get_buff (pfile, 32 * sizeof (cpp_token *));
+                     else
+                       {
+                         next = *pragma_buff;
+                         *pragma_buff
+                           = _cpp_get_buff (pfile,
+                                            (BUFF_FRONT (*pragma_buff)
+                                             - (*pragma_buff)->base) * 2);
+                         (*pragma_buff)->next = next;
+                       }
+                   }
+                 *(const cpp_token **) BUFF_FRONT (*pragma_buff) = token;
+                 BUFF_FRONT (*pragma_buff) += sizeof (cpp_token *);
+                 if (token->type == CPP_PRAGMA_EOL)
+                   break;
+                 token = cpp_get_token (pfile);
+               }
+             while (token->type != CPP_EOF);
+
+             /* In deferred pragmas parsing_args and prevent_expansion
+                had been changed, reset it.  */
+             pfile->state.parsing_args = 2;
+             pfile->state.prevent_expansion = 1;
+
+             if (token->type == CPP_EOF)
+               break;
+             else
+               continue;
+           }
 
          arg->first[ntokens++] = token;
        }
@@ -699,9 +764,11 @@ collect_args (cpp_reader *pfile, const cpp_hashnode *node)
 /* Search for an opening parenthesis to the macro of NODE, in such a
    way that, if none is found, we don't lose the information in any
    intervening padding tokens.  If we find the parenthesis, collect
-   the arguments and return the buffer containing them.  */
+   the arguments and return the buffer containing them.  PRAGMA_BUFF
+   argument is the same as in collect_args.  */
 static _cpp_buff *
-funlike_invocation_p (cpp_reader *pfile, cpp_hashnode *node)
+funlike_invocation_p (cpp_reader *pfile, cpp_hashnode *node,
+                     _cpp_buff **pragma_buff)
 {
   const cpp_token *token, *padding = NULL;
 
@@ -718,7 +785,7 @@ funlike_invocation_p (cpp_reader *pfile, cpp_hashnode *node)
   if (token->type == CPP_OPEN_PAREN)
     {
       pfile->state.parsing_args = 2;
-      return collect_args (pfile, node);
+      return collect_args (pfile, node, pragma_buff);
     }
 
   /* CPP_EOF can be the end of macro arguments, or the end of the
@@ -736,22 +803,47 @@ funlike_invocation_p (cpp_reader *pfile, cpp_hashnode *node)
   return NULL;
 }
 
+/* Return the real number of tokens in the expansion of MACRO.  */
+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 ();
+}
+
 /* Push the context of a macro with hash entry NODE onto the context
    stack.  If we can successfully expand the macro, we push a context
    containing its yet-to-be-rescanned replacement list and return one.
-   Otherwise, we don't push a context and return zero.  */
+   If there were additionally any unexpanded deferred #pragma directives
+   among macro arguments, push another context containing the
+   pragma tokens before the yet-to-be-rescanned replacement list
+   and return two.  Otherwise, we don't push a context and return zero.  */
 static int
-enter_macro_context (cpp_reader *pfile, cpp_hashnode *node)
+enter_macro_context (cpp_reader *pfile, cpp_hashnode *node,
+                    const cpp_token *result)
 {
   /* 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.used_define)
+       pfile->cb.used_define (pfile, pfile->directive_line, node);
+    }
+
   /* Handle standard macros.  */
   if (! (node->flags & NODE_BUILTIN))
     {
       cpp_macro *macro = node->value.macro;
+      _cpp_buff *pragma_buff = NULL;
 
       if (macro->fun_like)
        {
@@ -760,7 +852,7 @@ enter_macro_context (cpp_reader *pfile, cpp_hashnode *node)
          pfile->state.prevent_expansion++;
          pfile->keep_tokens++;
          pfile->state.parsing_args = 1;
-         buff = funlike_invocation_p (pfile, node);
+         buff = funlike_invocation_p (pfile, node, &pragma_buff);
          pfile->state.parsing_args = 0;
          pfile->keep_tokens--;
          pfile->state.prevent_expansion--;
@@ -768,9 +860,12 @@ enter_macro_context (cpp_reader *pfile, cpp_hashnode *node)
          if (buff == NULL)
            {
              if (CPP_WTRADITIONAL (pfile) && ! node->value.macro->syshdr)
-               cpp_error (pfile, CPP_DL_WARNING,
+               cpp_warning (pfile, CPP_W_TRADITIONAL,
  "function-like macro \"%s\" must be used with arguments in traditional C",
-                          NODE_NAME (node));
+                            NODE_NAME (node));
+
+             if (pragma_buff)
+               _cpp_release_buff (pfile, pragma_buff);
 
              return 0;
            }
@@ -783,10 +878,40 @@ 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);
+       }
+
+      if (pfile->cb.used)
+       pfile->cb.used (pfile, result->src_loc, node);
+
       macro->used = 1;
 
       if (macro->paramc == 0)
-       _cpp_push_token_context (pfile, node, macro->exp.tokens, macro->count);
+       _cpp_push_token_context (pfile, node, macro->exp.tokens,
+                                macro_real_token_count (macro));
+
+      if (pragma_buff)
+       {
+         if (!pfile->state.in_directive)
+           _cpp_push_token_context (pfile, NULL,
+                                    padding_token (pfile, result), 1);
+         do
+           {
+             _cpp_buff *tail = pragma_buff->next;
+             pragma_buff->next = NULL;
+             push_ptoken_context (pfile, NULL, pragma_buff,
+                                  (const cpp_token **) pragma_buff->base,
+                                  ((const cpp_token **) BUFF_FRONT (pragma_buff)
+                                   - (const cpp_token **) pragma_buff->base));
+             pragma_buff = tail;
+           }
+         while (pragma_buff != NULL);
+         return 2;
+       }
 
       return 1;
     }
@@ -807,13 +932,15 @@ replace_args (cpp_reader *pfile, cpp_hashnode *node, cpp_macro *macro, macro_arg
   const cpp_token **dest, **first;
   macro_arg *arg;
   _cpp_buff *buff;
+  unsigned int count;
 
   /* First, fully macro-expand arguments, calculating the number of
      tokens in the final expansion as we go.  The ordering of the if
      statements below is subtle; we must handle stringification before
      pasting.  */
-  total = macro->count;
-  limit = macro->exp.tokens + macro->count;
+  count = macro_real_token_count (macro);
+  total = count;
+  limit = macro->exp.tokens + count;
 
   for (src = macro->exp.tokens; src < limit; src++)
     if (src->type == CPP_MACRO_ARG)
@@ -823,7 +950,7 @@ replace_args (cpp_reader *pfile, cpp_hashnode *node, cpp_macro *macro, macro_arg
 
        /* We have an argument.  If it is not being stringified or
           pasted it is macro-replaced before insertion.  */
-       arg = &args[src->val.arg_no - 1];
+       arg = &args[src->val.macro_arg.arg_no - 1];
 
        if (src->flags & STRINGIFY_ARG)
          {
@@ -859,7 +986,7 @@ replace_args (cpp_reader *pfile, cpp_hashnode *node, cpp_macro *macro, macro_arg
        }
 
       paste_flag = 0;
-      arg = &args[src->val.arg_no - 1];
+      arg = &args[src->val.macro_arg.arg_no - 1];
       if (src->flags & STRINGIFY_ARG)
        count = 1, from = &arg->stringified;
       else if (src->flags & PASTE_LEFT)
@@ -871,7 +998,7 @@ replace_args (cpp_reader *pfile, cpp_hashnode *node, cpp_macro *macro, macro_arg
            {
              if (dest[-1]->type == CPP_COMMA
                  && macro->variadic
-                 && src->val.arg_no == macro->paramc)
+                 && src->val.macro_arg.arg_no == macro->paramc)
                {
                  /* Swallow a pasted comma if from == NULL, otherwise
                     drop the paste flag.  */
@@ -903,6 +1030,17 @@ replace_args (cpp_reader *pfile, cpp_hashnode *node, cpp_macro *macro, macro_arg
          if (src->flags & PASTE_LEFT)
            paste_flag = dest - 1;
        }
+      else if (CPP_PEDANTIC (pfile) && ! macro->syshdr
+              && ! CPP_OPTION (pfile, c99)
+              && ! cpp_in_system_header (pfile))
+       {
+         cpp_error (pfile, CPP_DL_PEDWARN,
+                    "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);
+       }
 
       /* Avoid paste on RHS (even case count == 0).  */
       if (!pfile->state.in_directive && !(src->flags & PASTE_LEFT))
@@ -1087,6 +1225,8 @@ const cpp_token *
 cpp_get_token (cpp_reader *pfile)
 {
   const cpp_token *result;
+  bool can_set = pfile->set_invocation_location;
+  pfile->set_invocation_location = false;
 
   for (;;)
     {
@@ -1125,17 +1265,55 @@ cpp_get_token (cpp_reader *pfile)
       if (result->type != CPP_NAME)
        break;
 
-      node = result->val.node;
+      node = result->val.node.node;
 
       if (node->type != NT_MACRO || (result->flags & NO_EXPAND))
        break;
 
       if (!(node->flags & NODE_DISABLED))
        {
-         if (!pfile->state.prevent_expansion
-             && enter_macro_context (pfile, node))
+         int ret = 0;
+         /* If not in a macro context, and we're going to start an
+            expansion, record the location.  */
+         if (can_set && !context->macro)
+           pfile->invocation_location = result->src_loc;
+         if (pfile->state.prevent_expansion)
+           break;
+
+         /* Conditional macros require that a predicate be evaluated
+            first.  */
+         if ((node->flags & NODE_CONDITIONAL) != 0)
            {
-             if (pfile->state.in_directive)
+             if (pfile->cb.macro_to_expand)
+               {
+                 bool whitespace_after;
+                 const cpp_token *peek_tok = cpp_peek_token (pfile, 0);
+
+                 whitespace_after = (peek_tok->type == CPP_PADDING
+                                     || (peek_tok->flags & PREV_WHITE));
+                 node = pfile->cb.macro_to_expand (pfile, result);
+                 if (node)
+                   ret = enter_macro_context (pfile, node, result);
+                 else if (whitespace_after)
+                   {
+                     /* If macro_to_expand hook returned NULL and it
+                        ate some tokens, see if we don't need to add
+                        a padding token in between this and the
+                        next token.  */
+                     peek_tok = cpp_peek_token (pfile, 0);
+                     if (peek_tok->type != CPP_PADDING
+                         && (peek_tok->flags & PREV_WHITE) == 0)
+                       _cpp_push_token_context (pfile, NULL,
+                                                padding_token (pfile,
+                                                               peek_tok), 1);
+                   }
+               }
+           }
+         else
+           ret = enter_macro_context (pfile, node, result);
+         if (ret)
+           {
+             if (pfile->state.in_directive || ret == 2)
                continue;
              return padding_token (pfile, result);
            }
@@ -1157,6 +1335,27 @@ cpp_get_token (cpp_reader *pfile)
   return result;
 }
 
+/* Like cpp_get_token, but also returns a location separate from the
+   one provided by the returned token.  LOC is an out parameter; *LOC
+   is set to the location "as expected by the user".  This matters
+   when a token results from macro expansion -- the token's location
+   will indicate where the macro is defined, but *LOC will be the
+   location of the start of the expansion.  */
+const cpp_token *
+cpp_get_token_with_location (cpp_reader *pfile, source_location *loc)
+{
+  const cpp_token *result;
+
+  pfile->set_invocation_location = true;
+  result = cpp_get_token (pfile);
+  if (pfile->context->macro)
+    *loc = pfile->invocation_location;
+  else
+    *loc = result->src_loc;
+
+  return result;
+}
+
 /* Returns true if we're expanding an object-like macro that was
    defined in a system header.  Just checks the macro at the top of
    the stack.  Used for diagnostic suppression.  */
@@ -1191,26 +1390,31 @@ cpp_scan_nooutput (cpp_reader *pfile)
   pfile->state.prevent_expansion--;
 }
 
+/* Step back one or more tokens obtained from the lexer.  */
+void
+_cpp_backup_tokens_direct (cpp_reader *pfile, unsigned int count)
+{
+  pfile->lookaheads += count;
+  while (count--)
+    {
+      pfile->cur_token--;
+      if (pfile->cur_token == pfile->cur_run->base
+          /* Possible with -fpreprocessed and no leading #line.  */
+          && pfile->cur_run->prev != NULL)
+        {
+          pfile->cur_run = pfile->cur_run->prev;
+          pfile->cur_token = pfile->cur_run->limit;
+        }
+    }
+}
+
 /* Step back one (or more) tokens.  Can only step back more than 1 if
    they are from the lexer, and not from macro expansion.  */
 void
 _cpp_backup_tokens (cpp_reader *pfile, unsigned int count)
 {
   if (pfile->context->prev == NULL)
-    {
-      pfile->lookaheads += count;
-      while (count--)
-       {
-         pfile->cur_token--;
-         if (pfile->cur_token == pfile->cur_run->base
-             /* Possible with -fpreprocessed and no leading #line.  */
-             && pfile->cur_run->prev != NULL)
-           {
-             pfile->cur_run = pfile->cur_run->prev;
-             pfile->cur_token = pfile->cur_run->limit;
-           }
-       }
-    }
+    _cpp_backup_tokens_direct (pfile, count);
   else
     {
       if (count != 1)
@@ -1236,6 +1440,15 @@ warn_of_redefinition (cpp_reader *pfile, const cpp_hashnode *node,
   if (node->flags & NODE_WARN)
     return true;
 
+  /* Suppress warnings for builtins that lack the NODE_WARN flag.  */
+  if (node->flags & NODE_BUILTIN)
+    return false;
+
+  /* Redefinitions of conditional (context-sensitive) macros, on
+     the other hand, must be allowed silently.  */
+  if (node->flags & NODE_CONDITIONAL)
+    return false;
+
   /* 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;
@@ -1273,7 +1486,7 @@ _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);
+  h->flags &= ~(NODE_BUILTIN | NODE_DISABLED | NODE_USED);
 }
 
 /* Save parameter NODE to the parameter list of macro MACRO.  Returns
@@ -1344,7 +1557,7 @@ parse_params (cpp_reader *pfile, cpp_macro *macro)
            }
          prev_ident = 1;
 
-         if (_cpp_save_parameter (pfile, macro, token->val.node))
+         if (_cpp_save_parameter (pfile, macro, token->val.node.node))
            return false;
          continue;
 
@@ -1372,13 +1585,14 @@ parse_params (cpp_reader *pfile, cpp_macro *macro)
              if (! CPP_OPTION (pfile, c99)
                  && CPP_OPTION (pfile, pedantic)
                  && CPP_OPTION (pfile, warn_variadic_macros))
-               cpp_error (pfile, CPP_DL_PEDWARN,
-                          "anonymous variadic macros were introduced in C99");
+               cpp_pedwarning
+                  (pfile, CPP_W_VARIADIC_MACROS,
+                  "anonymous variadic macros were introduced in C99");
            }
          else if (CPP_OPTION (pfile, pedantic)
                   && CPP_OPTION (pfile, warn_variadic_macros))
-           cpp_error (pfile, CPP_DL_PEDWARN,
-                      "ISO C does not permit named 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);
@@ -1417,10 +1631,10 @@ lex_expansion_token (cpp_reader *pfile, cpp_macro *macro)
 
   /* Is this a parameter?  */
   if (token->type == CPP_NAME
-      && (token->val.node->flags & NODE_MACRO_ARG) != 0)
+      && (token->val.node.node->flags & NODE_MACRO_ARG) != 0)
     {
       token->type = CPP_MACRO_ARG;
-      token->val.arg_no = token->val.node->value.arg_index;
+      token->val.macro_arg.arg_no = token->val.node.node->value.arg_index;
     }
   else if (CPP_WTRADITIONAL (pfile) && macro->paramc > 0
           && (token->type == CPP_STRING || token->type == CPP_CHAR))
@@ -1437,6 +1651,7 @@ create_iso_definition (cpp_reader *pfile, cpp_macro *macro)
   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;
 
   /* Get the first token of the expansion (or the '(' of a
      function-like macro).  */
@@ -1514,6 +1729,10 @@ create_iso_definition (cpp_reader *pfile, cpp_macro *macro)
        {
          if (token->type == CPP_MACRO_ARG)
            {
+             if (token->flags & PREV_WHITE)
+               token->flags |= SP_PREV_WHITE;
+             if (token[-1].flags & DIGRAPH)
+               token->flags |= SP_DIGRAPH;
              token->flags &= ~PREV_WHITE;
              token->flags |= STRINGIFY_ARG;
              token->flags |= token[-1].flags & PREV_WHITE;
@@ -1553,8 +1772,21 @@ create_iso_definition (cpp_reader *pfile, cpp_macro *macro)
              return false;
            }
 
-         --macro->count;
-         token[-1].flags |= PASTE_LEFT;
+         if (token[-1].flags & PASTE_LEFT)
+           {
+             macro->extra_tokens = 1;
+             num_extra_tokens++;
+             token->val.token_no = macro->count - 1;
+           }
+         else
+           {
+             --macro->count;
+             token[-1].flags |= PASTE_LEFT;
+             if (token->flags & DIGRAPH)
+               token[-1].flags |= SP_DIGRAPH;
+             if (token->flags & PREV_WHITE)
+               token[-1].flags |= SP_PREV_WHITE;
+           }
        }
 
       following_paste_op = (token->type == CPP_PASTE);
@@ -1577,7 +1809,27 @@ create_iso_definition (cpp_reader *pfile, cpp_macro *macro)
       cpp_token *tokns =
         (cpp_token *) pfile->hash_table->alloc_subobject (sizeof (cpp_token)
                                                           * macro->count);
-      memcpy (tokns, macro->exp.tokens, 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;
     }
   else
@@ -1606,6 +1858,7 @@ _cpp_create_definition (cpp_reader *pfile, cpp_hashnode *node)
   macro->used = !CPP_OPTION (pfile, warn_unused_macros);
   macro->count = 0;
   macro->fun_like = 0;
+  macro->extra_tokens = 0;
   /* To suppress some diagnostics.  */
   macro->syshdr = pfile->buffer && pfile->buffer->sysp != 0;
 
@@ -1642,11 +1895,17 @@ _cpp_create_definition (cpp_reader *pfile, cpp_hashnode *node)
 
       if (warn_of_redefinition (pfile, node, macro))
        {
-         cpp_error_with_line (pfile, CPP_DL_PEDWARN, pfile->directive_line, 0,
-                              "\"%s\" redefined", NODE_NAME (node));
+          const int reason = (node->flags & NODE_BUILTIN)
+                             ? CPP_W_BUILTIN_MACRO_REDEFINED : CPP_W_NONE;
+         bool warned;
 
-         if (node->type == NT_MACRO && !(node->flags & NODE_BUILTIN))
-           cpp_error_with_line (pfile, CPP_DL_PEDWARN,
+         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))
+           cpp_error_with_line (pfile, CPP_DL_NOTE,
                                 node->value.macro->line, 0,
                         "this is the location of the previous definition");
        }
@@ -1658,9 +1917,20 @@ _cpp_create_definition (cpp_reader *pfile, cpp_hashnode *node)
   /* Enter definition in hash table.  */
   node->type = NT_MACRO;
   node->value.macro = macro;
-  if (! ustrncmp (NODE_NAME (node), DSC ("__STDC_")))
+  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.  */
+      && ustrcmp (NODE_NAME (node), (const uchar *) "__STDC_LIMIT_MACROS")
+      && ustrcmp (NODE_NAME (node), (const uchar *) "__STDC_CONSTANT_MACROS"))
     node->flags |= NODE_WARN;
 
+  /* If user defines one of the conditional macros, remove the
+     conditional flag */
+  node->flags &= ~NODE_CONDITIONAL;
+
   return ok;
 }
 
@@ -1740,12 +2010,13 @@ cpp_macro_definition (cpp_reader *pfile, const cpp_hashnode *node)
     len += _cpp_replacement_text_len (macro);
   else
     {
-      for (i = 0; i < macro->count; i++)
+      unsigned int count = macro_real_token_count (macro);
+      for (i = 0; i < count; i++)
        {
          cpp_token *token = &macro->exp.tokens[i];
 
          if (token->type == CPP_MACRO_ARG)
-           len += NODE_LEN (macro->params[token->val.arg_no - 1]);
+           len += NODE_LEN (macro->params[token->val.macro_arg.arg_no - 1]);
          else
            len += cpp_token_len (token);
 
@@ -1804,7 +2075,8 @@ cpp_macro_definition (cpp_reader *pfile, const cpp_hashnode *node)
   else if (macro->count)
   /* Expansion tokens.  */
     {
-      for (i = 0; i < macro->count; i++)
+      unsigned int count = macro_real_token_count (macro);
+      for (i = 0; i < count; i++)
        {
          cpp_token *token = &macro->exp.tokens[i];
 
@@ -1816,9 +2088,9 @@ cpp_macro_definition (cpp_reader *pfile, const cpp_hashnode *node)
          if (token->type == CPP_MACRO_ARG)
            {
              memcpy (buffer,
-                     NODE_NAME (macro->params[token->val.arg_no - 1]),
-                     NODE_LEN (macro->params[token->val.arg_no - 1]));
-             buffer += NODE_LEN (macro->params[token->val.arg_no - 1]);
+                     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]);
            }
          else
            buffer = cpp_spell_token (pfile, token, buffer, false);