Use -std option in test for C99 macros in <cmath>
[gcc.git] / libcpp / macro.c
index ff6685c2665b2a6af0cc722b8edcd69dea0232cd..ca1d1d6fddecd4d32d70495374dcb3dc3658e61c 100644 (file)
@@ -1,5 +1,5 @@
 /* Part of CPP library.  (Macro and #define handling.)
-   Copyright (C) 1986-2014 Free Software Foundation, Inc.
+   Copyright (C) 1986-2016 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
@@ -73,13 +73,22 @@ struct macro_arg_token_iter
      -ftrack-macro-expansion is used this location tracks loci across
      macro expansion.  */
   const source_location *location_ptr;
-#ifdef ENABLE_CHECKING
+#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 of this identifier.  */
+  union _cpp_hashnode_value value;
+};
+
 /* Macro expansion.  */
 
 static int enter_macro_context (cpp_reader *, cpp_hashnode *,
@@ -132,7 +141,7 @@ static inline const cpp_token **tokens_buff_put_token_to (const cpp_token **,
                                                           const cpp_token *,
                                                           source_location,
                                                           source_location,
-                                                          const struct line_map *,
+                                                          const line_map_macro *,
                                                           unsigned int);
 
 static const cpp_token **tokens_buff_add_token (_cpp_buff *,
@@ -140,7 +149,7 @@ static const cpp_token **tokens_buff_add_token (_cpp_buff *,
                                                const cpp_token *,
                                                source_location,
                                                source_location,
-                                               const struct line_map *,
+                                               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 *,
@@ -186,7 +195,9 @@ _cpp_warn_if_unused_macro (cpp_reader *pfile, cpp_hashnode *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));
     }
@@ -384,6 +395,10 @@ _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;
     }
 
   if (result == NULL)
@@ -440,7 +455,7 @@ builtin_macro (cpp_reader *pfile, cpp_hashnode *node, source_location loc)
         macro.  */
       source_location *virt_locs = NULL;
       _cpp_buff *token_buf = tokens_buff_new (pfile, 1, &virt_locs);
-      const line_map * map =
+      const line_map_macro * map =
        linemap_enter_macro (pfile->line_table, node,
                                            token->src_loc, 1);
       tokens_buff_add_token (token_buf, virt_locs, token,
@@ -516,7 +531,7 @@ 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));
 
@@ -590,7 +605,7 @@ paste_tokens (cpp_reader *pfile, source_location location,
 
   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
@@ -600,7 +615,7 @@ paste_tokens (cpp_reader *pfile, source_location location,
     *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);
@@ -1154,7 +1169,7 @@ enter_macro_context (cpp_reader *pfile, cpp_hashnode *node,
            {
              unsigned int i;
              const cpp_token *src = macro->exp.tokens;
-             const struct line_map *map;
+             const line_map_macro *map;
              source_location *virt_locs = NULL;
              _cpp_buff *macro_tokens
                = tokens_buff_new (pfile, tokens_count, &virt_locs);
@@ -1215,7 +1230,24 @@ enter_macro_context (cpp_reader *pfile, cpp_hashnode *node,
 
   pfile->about_to_expand_macro_p = false;
   /* Handle built-in macros and the _Pragma operator.  */
-  return builtin_macro (pfile, node, location);
+  {
+    source_location loc;
+    if (/* The top-level macro invocation that triggered the expansion
+          we are looking at is with a standard macro ...*/
+       !(pfile->top_most_macro_node->flags & NODE_BUILTIN)
+       /* ... and it's a function-like macro invocation.  */
+       && pfile->top_most_macro_node->value.macro->fun_like)
+      /* Then the location of the end of the macro invocation is the
+        location of the closing parenthesis.  */
+      loc = pfile->cur_token[-1].src_loc;
+    else
+      /* Otherwise, the location of the end of the macro invocation is
+        the location of the expansion point of that top-level macro
+        invocation.  */
+      loc = location;
+
+    return builtin_macro (pfile, node, loc);
+  }
 }
 
 /* De-allocate the memory used by BUFF which is an array of instances
@@ -1278,14 +1310,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;
     }
 }
@@ -1371,7 +1400,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
@@ -1396,14 +1425,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
 }
@@ -1412,7 +1441,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 ();
@@ -1426,7 +1455,7 @@ macro_arg_token_iter_get_token (const macro_arg_token_iter *it)
 static source_location
 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 ();
@@ -1505,7 +1534,7 @@ replace_args (cpp_reader *pfile, cpp_hashnode *node, cpp_macro *macro,
   _cpp_buff *buff = NULL;
   source_location *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
@@ -1776,35 +1805,32 @@ replace_args (cpp_reader *pfile, cpp_hashnode *node, cpp_macro *macro,
            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))
        {
          if (CPP_OPTION (pfile, cplusplus))
-           cpp_error (pfile, CPP_DL_PEDWARN,
-                      "invoking macro %s argument %d: "
-                      "empty macro arguments are undefined"
-                      " in ISO C++98",
-                      NODE_NAME (node),
-                      src->val.macro_arg.arg_no);
-         else
-           cpp_error (pfile, CPP_DL_PEDWARN,
-                      "invoking macro %s argument %d: "
-                      "empty macro arguments are undefined"
-                      " in ISO C90",
-                      NODE_NAME (node),
-                      src->val.macro_arg.arg_no);
+           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)
-              && ! macro->syshdr
-              && ! cpp_in_system_header (pfile)
-              && ! CPP_OPTION (pfile, cplusplus))
-       cpp_error (pfile, CPP_DL_WARNING,
-                  "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",
+                    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))
@@ -2039,7 +2065,7 @@ tokens_buff_put_token_to (const cpp_token **dest,
                          const cpp_token *token,
                          source_location virt_loc,
                          source_location parm_def_loc,                   
-                         const struct line_map *map,
+                         const line_map_macro *map,
                          unsigned int macro_token_index)
 {
   source_location macro_loc = virt_loc;
@@ -2084,7 +2110,7 @@ tokens_buff_add_token (_cpp_buff *buffer,
                       const cpp_token *token,
                       source_location virt_loc,
                       source_location parm_def_loc,
-                      const struct line_map *map,
+                      const line_map_macro *map,
                       unsigned int macro_token_index)
 {
   const cpp_token **result;
@@ -2115,11 +2141,9 @@ 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);
@@ -2286,6 +2310,10 @@ _cpp_pop_context (cpp_reader *pfile)
             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)
@@ -2450,9 +2478,13 @@ cpp_get_token_1 (cpp_reader *pfile, source_location *location)
        {
          int ret = 0;
          /* If not in a macro context, and we're going to start an
-            expansion, record the location.  */
+            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->invocation_location = result->src_loc;
+             pfile->top_most_macro_node = node;
+           }
          if (pfile->state.prevent_expansion)
            break;
 
@@ -2672,10 +2704,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 ();
@@ -2699,13 +2728,12 @@ warn_of_redefinition (cpp_reader *pfile, 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)
-    {
-      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 (node->flags & NODE_BUILTIN
+      && (!pfile->cb.user_builtin_macro
+         || !pfile->cb.user_builtin_macro (pfile, node)))
+    return CPP_OPTION (pfile, warn_builtin_macro_redefined);
 
   /* Redefinitions of conditional (context-sensitive) macros, on
      the other hand, must be allowed silently.  */
@@ -2752,10 +2780,12 @@ _cpp_free_definition (cpp_hashnode *h)
   h->flags &= ~(NODE_BUILTIN | 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 zero on success, nonzero if the parameter is
+   a duplicate.  */
 bool
-_cpp_save_parameter (cpp_reader *pfile, cpp_macro *macro, cpp_hashnode *node)
+_cpp_save_parameter (cpp_reader *pfile, cpp_macro *macro, cpp_hashnode *node,
+                    cpp_hashnode *spelling)
 {
   unsigned int len;
   /* Constraint 6.10.3.6 - duplicate parameter names.  */
@@ -2770,17 +2800,20 @@ _cpp_save_parameter (cpp_reader *pfile, cpp_macro *macro, cpp_hashnode *node)
       < (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;
+  ((cpp_hashnode **) BUFF_FRONT (pfile->a_buff))[macro->paramc++] = spelling;
   node->flags |= NODE_MACRO_ARG;
-  len = macro->paramc * sizeof (union _cpp_hashnode_value);
+  len = macro->paramc * 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_len = len;
     }
-  ((union _cpp_hashnode_value *) pfile->macro_buffer)[macro->paramc - 1]
-    = node->value;
+  struct macro_arg_saved_data save;
+  save.value = node->value;
+  save.canonical_node = node;
+  ((struct macro_arg_saved_data *) pfile->macro_buffer)[macro->paramc - 1]
+    = save;
   
   node->value.arg_index  = macro->paramc;
   return false;
@@ -2820,7 +2853,8 @@ parse_params (cpp_reader *pfile, cpp_macro *macro)
            }
          prev_ident = 1;
 
-         if (_cpp_save_parameter (pfile, macro, token->val.node.node))
+         if (_cpp_save_parameter (pfile, macro, token->val.node.node,
+                                  token->val.node.spelling))
            return false;
          continue;
 
@@ -2843,6 +2877,7 @@ parse_params (cpp_reader *pfile, cpp_macro *macro)
          if (!prev_ident)
            {
              _cpp_save_parameter (pfile, macro,
+                                  pfile->spec_nodes.n__VA_ARGS__,
                                   pfile->spec_nodes.n__VA_ARGS__);
              pfile->state.va_args_ok = 1;
              if (! CPP_OPTION (pfile, c99)
@@ -2858,7 +2893,7 @@ parse_params (cpp_reader *pfile, cpp_macro *macro)
                        (pfile, CPP_W_VARIADIC_MACROS,
                        "anonymous variadic macros were introduced in C99");
                }
-             else if (CPP_OPTION (pfile, cpp_warn_c90_c99_compat)
+             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");
@@ -2913,8 +2948,10 @@ lex_expansion_token (cpp_reader *pfile, cpp_macro *macro)
   if (token->type == CPP_NAME
       && (token->val.node.node->flags & NODE_MACRO_ARG) != 0)
     {
+      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))
@@ -3166,9 +3203,11 @@ _cpp_create_definition (cpp_reader *pfile, cpp_hashnode *node)
   /* Clear the fast argument lookup indices.  */
   for (i = macro->paramc; i-- > 0; )
     {
-      struct cpp_hashnode *node = macro->params[i];
+      struct macro_arg_saved_data *save =
+       &((struct macro_arg_saved_data *) pfile->macro_buffer)[i];
+      struct cpp_hashnode *node = save->canonical_node;
       node->flags &= ~ NODE_MACRO_ARG;
-      node->value = ((union _cpp_hashnode_value *) pfile->macro_buffer)[i];
+      node->value = save->value;
     }
 
   if (!ok)
@@ -3181,14 +3220,14 @@ _cpp_create_definition (cpp_reader *pfile, cpp_hashnode *node)
 
       if (warn_of_redefinition (pfile, node, macro))
        {
-          const int reason = (node->flags & NODE_BUILTIN)
+          const int reason = ((node->flags & NODE_BUILTIN)
+                             && !(node->flags & NODE_WARN))
                              ? CPP_W_BUILTIN_MACRO_REDEFINED : CPP_W_NONE;
-         bool warned;
 
-         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))
            cpp_error_with_line (pfile, CPP_DL_NOTE,
@@ -3289,7 +3328,7 @@ cpp_macro_definition (cpp_reader *pfile, cpp_hashnode *node)
 
   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
@@ -3309,7 +3348,7 @@ cpp_macro_definition (cpp_reader *pfile, cpp_hashnode *node)
          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);
 
@@ -3331,8 +3370,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)
@@ -3381,12 +3419,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)
            {