OSDN Git Service

libcpp/
[pf3gnuchains/gcc-fork.git] / libcpp / macro.c
index d760383..32f99ca 100644 (file)
@@ -1,7 +1,7 @@
 /* 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 Free Software Foundation, Inc.
+   2006, 2007, 2008, 2009, 2010, 2011, 2012 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
@@ -128,13 +128,13 @@ static _cpp_buff *tokens_buff_new (cpp_reader *, size_t,
                                   source_location **);
 static size_t tokens_buff_count (_cpp_buff *);
 static const cpp_token **tokens_buff_last_token_ptr (_cpp_buff *);
-static const cpp_token **tokens_buff_put_token_to (const cpp_token **,
-                                                  source_location *, 
-                                                  const cpp_token *,
-                                                  source_location,
-                                                  source_location,
-                                                  const struct line_map *,
-                                                  unsigned int);
+static inline const cpp_token **tokens_buff_put_token_to (const cpp_token **,
+                                                          source_location *,
+                                                          const cpp_token *,
+                                                          source_location,
+                                                          source_location,
+                                                          const struct line_map *,
+                                                          unsigned int);
 
 static const cpp_token **tokens_buff_add_token (_cpp_buff *,
                                                source_location *,
@@ -143,7 +143,7 @@ static const cpp_token **tokens_buff_add_token (_cpp_buff *,
                                                source_location,
                                                const struct line_map *,
                                                unsigned int);
-static void tokens_buff_remove_last_token (_cpp_buff *);
+static inline void tokens_buff_remove_last_token (_cpp_buff *);
 static void replace_args (cpp_reader *, cpp_hashnode *, cpp_macro *,
                          macro_arg *, source_location);
 static _cpp_buff *funlike_invocation_p (cpp_reader *, cpp_hashnode *,
@@ -165,6 +165,13 @@ static void consume_next_token_from_context (cpp_reader *pfile,
                                             source_location *);
 static const cpp_token* cpp_get_token_1 (cpp_reader *, source_location *);
 
+/* Statistical counter tracking the number of macros that got
+   expanded.  */
+unsigned num_expanded_macros_counter = 0;
+/* Statistical counter tracking the total number tokens resulting
+   from macro expansion.  */
+unsigned num_macro_tokens_counter = 0;
+
 /* Emits a warning if NODE is a macro defined in the main file that
    has not been used.  */
 int
@@ -271,10 +278,9 @@ _cpp_builtin_macro_text (cpp_reader *pfile, cpp_hashnode *node)
                                                 pfile->line_table->highest_line);
        else
          {
-           map = linemap_lookup (pfile->line_table, pfile->line_table->highest_line);
-           while (! MAIN_FILE_P (map))
-             map = INCLUDED_FROM (pfile->line_table, map);
-           name = ORDINARY_MAP_FILE_NAME (map);
+           name = _cpp_get_file_name (pfile->main_file);
+           if (!name)
+             abort ();
          }
        len = strlen (name);
        buf = _cpp_unaligned_alloc (pfile, len * 2 + 3);
@@ -1082,10 +1088,15 @@ enter_macro_context (cpp_reader *pfile, cpp_hashnode *node,
                                            (const cpp_token **)
                                            macro_tokens->base,
                                            count);
+             num_macro_tokens_counter += count;
            }
          else
-           _cpp_push_token_context (pfile, node, macro->exp.tokens,
-                                    macro_real_token_count (macro));
+           {
+             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;
+           }
        }
 
       if (pragma_buff)
@@ -1095,13 +1106,18 @@ enter_macro_context (cpp_reader *pfile, cpp_hashnode *node,
                                     padding_token (pfile, result), 1);
          do
            {
+             unsigned tokens_count;
              _cpp_buff *tail = pragma_buff->next;
              pragma_buff->next = NULL;
+             tokens_count = ((const cpp_token **) BUFF_FRONT (pragma_buff)
+                             - (const cpp_token **) pragma_buff->base);
              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));
+                                  tokens_count);
              pragma_buff = tail;
+             if (!CPP_OPTION (pfile, track_macro_expansion))
+               num_macro_tokens_counter += tokens_count;
+
            }
          while (pragma_buff != NULL);
          return 2;
@@ -1261,6 +1277,10 @@ macro_arg_token_iter_init (macro_arg_token_iter *iter,
   iter->track_macro_exp_p = track_macro_exp_p;
   iter->kind = kind;
   iter->token_ptr = token_ptr;
+  /* Unconditionally initialize this so that the compiler doesn't warn
+     about iter->location_ptr being possibly uninitialized later after
+     this code has been inlined somewhere.  */
+  iter->location_ptr = NULL;
   if (track_macro_exp_p)
     iter->location_ptr = get_arg_token_location (arg, kind);
 #ifdef ENABLE_CHECKING
@@ -1711,6 +1731,8 @@ replace_args (cpp_reader *pfile, cpp_hashnode *node, cpp_macro *macro,
   else
     push_ptoken_context (pfile, node, buff, first,
                         tokens_buff_count (buff));
+
+  num_macro_tokens_counter += tokens_buff_count (buff);
 }
 
 /* Return a special padding token, with padding inherited from SOURCE.  */
@@ -2240,6 +2262,8 @@ cpp_get_token_1 (cpp_reader *pfile, source_location *location)
        }
       else
        {
+         if (pfile->context->c.macro)
+           ++num_expanded_macros_counter;
          _cpp_pop_context (pfile);
          if (pfile->state.in_directive)
            continue;