public inbox for gcc-patches@gcc.gnu.org
 help / color / mirror / Atom feed
* [PATCH 3/6] Emit macro expansion related diagnostics
  2011-10-17  9:58 [PATCH 0/6] Tracking locations of tokens resulting from macro expansion Dodji Seketeli
@ 2011-10-17  9:58 ` Dodji Seketeli
  2011-10-17 10:56   ` Richard Guenther
  2011-10-17 10:08 ` [PATCH 4/6] Support -fdebug-cpp option Dodji Seketeli
                   ` (4 subsequent siblings)
  5 siblings, 1 reply; 24+ messages in thread
From: Dodji Seketeli @ 2011-10-17  9:58 UTC (permalink / raw)
  To: gcc-patches; +Cc: jason, tromey, gdr, joseph, burnus, charlet

In this third instalment the diagnostic machinery -- when faced with
the virtual location of a token resulting from macro expansion -- uses
the new linemap APIs to unwind the stack of macro expansions that led
to that token and emits a [hopefully] more useful message than what we
have today.

diagnostic_report_current_module has been slightly changed to use the
location given by client code instead of the global input_location
variable.  This results in more precise diagnostic locations in
general but then the patch adjusts some C++ tests which output changed
as a result of this.

Three new regression tests have been added.

The mandatory screenshot goes like this:

[dodji@adjoa gcc]$ cat -n test.c
     1    #define OPERATE(OPRD1, OPRT, OPRD2) \
     2      OPRD1 OPRT OPRD2;
     3
     4    #define SHIFTL(A,B) \
     5      OPERATE (A,<<,B)
     6
     7    #define MULT(A) \
     8      SHIFTL (A,1)
     9
    10    void
    11    g ()
    12    {
    13      MULT (1.0);/* 1.0 << 1; <-- so this is an error.  */
    14    }

[dodji@adjoa gcc]$ ./cc1 -quiet -ftrack-macro-expansion test.c
test.c: In function 'g':
test.c:5:14: erreur: invalid operands to binary << (have 'double' and 'int')
test.c:2:9: note: in expansion of macro 'OPERATE'
test.c:5:3: note: expanded from here
test.c:5:14: note: in expansion of macro 'SHIFTL'
test.c:8:3: note: expanded from here
test.c:8:3: note: in expansion of macro 'MULT2'
test.c:13:3: note: expanded from here

gcc/ChangeLog
2011-10-15  Tom Tromey  <tromey@redhat.com>
 	    Dodji Seketeli  <dodji@redhat.com>
 
	* gcc/diagnostic.h (diagnostic_report_current_module): Add a
	location parameter.
	* diagnostic.c (diagnostic_report_current_module): Add a location
	parameter to the function definition.  Use it instead of
	input_location.  Resolve the virtual location rather than just
	looking up its map and risking to touch a resulting macro map.
	(default_diagnostic_starter): Pass the relevant diagnostic
	location to diagnostic_report_current_module.
	* tree-diagnostic.c (maybe_unwind_expanded_macro_loc): New.
	(virt_loc_aware_diagnostic_finalizer): Likewise.
	(diagnostic_report_current_function): Pass the
	relevant location to diagnostic_report_current_module.
	* tree-diagnostic.h (virt_loc_aware_diagnostic_finalizer): Declare
	new function.
	* toplev.c (general_init): By default, use the new
	virt_loc_aware_diagnostic_finalizer as diagnostic finalizer.
	* Makefile.in: Add vec.h dependency to tree-diagnostic.c.

gcc/cp/ChangeLog
2011-10-15  Tom Tromey  <tromey@redhat.com>
	    Dodji Seketeli  <dodji@redhat.com>

	* error.c (cp_diagnostic_starter): Pass the relevant location to
	diagnostic_report_current_module.
	(cp_diagnostic_finalizer): Call virt_loc_aware_diagnostic_finalizer.

gcc/testsuite/ChangeLog
2011-10-15  Tom Tromey  <tromey@redhat.com>
 	    Dodji Seketeli  <dodji@redhat.com>
 
	* lib/prune.exp (prune_gcc_output):  Prune output referring to
	included files.
	* gcc.dg/cpp/macro-exp-tracking-1.c: New test.
	* gcc.dg/cpp/macro-exp-tracking-2.c: Likewise.
	* gcc.dg/cpp/macro-exp-tracking-3.c: Likewise.
	* gcc.dg/cpp/pragma-diagnostic-2.c: Likewise.

---
 gcc/ChangeLog                                   |   21 +++
 gcc/Makefile.in                                 |    3 +-
 gcc/cp/ChangeLog                                |    7 +
 gcc/cp/error.c                                  |    5 +-
 gcc/diagnostic.c                                |   13 +-
 gcc/diagnostic.h                                |    2 +-
 gcc/testsuite/ChangeLog                         |   10 ++
 gcc/testsuite/gcc.dg/cpp/macro-exp-tracking-1.c |   21 +++
 gcc/testsuite/gcc.dg/cpp/macro-exp-tracking-2.c |   21 +++
 gcc/testsuite/gcc.dg/cpp/macro-exp-tracking-3.c |   14 ++
 gcc/testsuite/gcc.dg/cpp/macro-exp-tracking-4.c |   14 ++
 gcc/testsuite/gcc.dg/cpp/pragma-diagnostic-2.c  |   34 +++++
 gcc/testsuite/lib/prune.exp                     |    1 +
 gcc/toplev.c                                    |    3 +
 gcc/tree-diagnostic.c                           |  182 ++++++++++++++++++++++-
 gcc/tree-diagnostic.h                           |    3 +-
 16 files changed, 343 insertions(+), 11 deletions(-)
 create mode 100644 gcc/testsuite/gcc.dg/cpp/macro-exp-tracking-1.c
 create mode 100644 gcc/testsuite/gcc.dg/cpp/macro-exp-tracking-2.c
 create mode 100644 gcc/testsuite/gcc.dg/cpp/macro-exp-tracking-3.c
 create mode 100644 gcc/testsuite/gcc.dg/cpp/macro-exp-tracking-4.c
 create mode 100644 gcc/testsuite/gcc.dg/cpp/pragma-diagnostic-2.c

diff --git a/gcc/Makefile.in b/gcc/Makefile.in
index baec5fe..70dc0b8 100644
--- a/gcc/Makefile.in
+++ b/gcc/Makefile.in
@@ -2805,7 +2805,8 @@ tree-pretty-print.o : tree-pretty-print.c $(CONFIG_H) $(SYSTEM_H) \
    $(TM_H) coretypes.h tree-iterator.h $(SCEV_H) langhooks.h \
    $(TREE_PASS_H) value-prof.h output.h tree-pretty-print.h
 tree-diagnostic.o : tree-diagnostic.c $(CONFIG_H) $(SYSTEM_H) coretypes.h \
-   $(TREE_H) $(DIAGNOSTIC_H) tree-diagnostic.h langhooks.h $(LANGHOOKS_DEF_H)
+   $(TREE_H) $(DIAGNOSTIC_H) tree-diagnostic.h langhooks.h $(LANGHOOKS_DEF_H) \
+   $(VEC_H)
 fold-const.o : fold-const.c $(CONFIG_H) $(SYSTEM_H) coretypes.h $(TM_H) \
    $(TREE_H) $(FLAGS_H) $(DIAGNOSTIC_CORE_H) $(HASHTAB_H) $(EXPR_H) $(RTL_H) \
    $(GGC_H) $(TM_P_H) langhooks.h $(MD5_H) intl.h $(TARGET_H) \
diff --git a/gcc/cp/error.c b/gcc/cp/error.c
index 4d12a0d..c7c4525 100644
--- a/gcc/cp/error.c
+++ b/gcc/cp/error.c
@@ -2768,7 +2768,7 @@ static void
 cp_diagnostic_starter (diagnostic_context *context,
 		       diagnostic_info *diagnostic)
 {
-  diagnostic_report_current_module (context);
+  diagnostic_report_current_module (context, diagnostic->location);
   cp_print_error_function (context, diagnostic);
   maybe_print_instantiation_context (context);
   maybe_print_constexpr_context (context);
@@ -2778,8 +2778,9 @@ cp_diagnostic_starter (diagnostic_context *context,
 
 static void
 cp_diagnostic_finalizer (diagnostic_context *context,
-			 diagnostic_info *diagnostic ATTRIBUTE_UNUSED)
+			 diagnostic_info *diagnostic)
 {
+  virt_loc_aware_diagnostic_finalizer (context, diagnostic);
   pp_base_destroy_prefix (context->printer);
 }
 
diff --git a/gcc/diagnostic.c b/gcc/diagnostic.c
index b46eb35..a8c0e66 100644
--- a/gcc/diagnostic.c
+++ b/gcc/diagnostic.c
@@ -255,9 +255,9 @@ diagnostic_action_after_output (diagnostic_context *context,
 }
 
 void
-diagnostic_report_current_module (diagnostic_context *context)
+diagnostic_report_current_module (diagnostic_context *context, location_t where)
 {
-  const struct line_map *map;
+  const struct line_map *map = NULL;
 
   if (pp_needs_newline (context->printer))
     {
@@ -265,10 +265,13 @@ diagnostic_report_current_module (diagnostic_context *context)
       pp_needs_newline (context->printer) = false;
     }
 
-  if (input_location <= BUILTINS_LOCATION)
+  if (where <= BUILTINS_LOCATION)
     return;
 
-  map = linemap_lookup (line_table, input_location);
+  linemap_resolve_location (line_table, where,
+			    LRK_MACRO_DEFINITION_LOCATION,
+			    &map);
+
   if (map && diagnostic_last_module_changed (context, map))
     {
       diagnostic_set_last_module (context, map);
@@ -301,7 +304,7 @@ void
 default_diagnostic_starter (diagnostic_context *context,
 			    diagnostic_info *diagnostic)
 {
-  diagnostic_report_current_module (context);
+  diagnostic_report_current_module (context, diagnostic->location);
   pp_set_prefix (context->printer, diagnostic_build_prefix (context,
 							    diagnostic));
 }
diff --git a/gcc/diagnostic.h b/gcc/diagnostic.h
index 8074354..4b1265b 100644
--- a/gcc/diagnostic.h
+++ b/gcc/diagnostic.h
@@ -253,7 +253,7 @@ extern diagnostic_context *global_dc;
 /* Diagnostic related functions.  */
 extern void diagnostic_initialize (diagnostic_context *, int);
 extern void diagnostic_finish (diagnostic_context *);
-extern void diagnostic_report_current_module (diagnostic_context *);
+extern void diagnostic_report_current_module (diagnostic_context *, location_t);
 
 /* Force diagnostics controlled by OPTIDX to be kind KIND.  */
 extern diagnostic_t diagnostic_classify_diagnostic (diagnostic_context *,
diff --git a/gcc/testsuite/gcc.dg/cpp/macro-exp-tracking-1.c b/gcc/testsuite/gcc.dg/cpp/macro-exp-tracking-1.c
new file mode 100644
index 0000000..d975c8c
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/cpp/macro-exp-tracking-1.c
@@ -0,0 +1,21 @@
+/*
+   { dg-options "-ftrack-macro-expansion=1" }
+   { dg-do compile }
+*/
+
+#define OPERATE(OPRD1, OPRT, OPRD2) \
+do \
+{ \
+  OPRD1 OPRT OPRD2; /* { dg-message "expansion" }*/ 	   \
+} while (0)
+
+#define SHIFTL(A,B) \
+  OPERATE (A,<<,B) /* { dg-message "expanded|expansion" } */
+
+void
+foo ()
+{
+  SHIFTL (0.1,0.2); /* { dg-message "expanded" } */
+}
+
+/* { dg-error "invalid operands" "" { target *-*-* } 13 } */
diff --git a/gcc/testsuite/gcc.dg/cpp/macro-exp-tracking-2.c b/gcc/testsuite/gcc.dg/cpp/macro-exp-tracking-2.c
new file mode 100644
index 0000000..684af4c
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/cpp/macro-exp-tracking-2.c
@@ -0,0 +1,21 @@
+/* 
+   { dg-options "-ftrack-macro-expansion=1" }
+   { dg-do compile }
+*/
+
+#define OPERATE(OPRD1, OPRT, OPRD2) \
+ OPRD1 OPRT OPRD2;		/* { dg-message "expansion" } */
+
+#define SHIFTL(A,B) \
+  OPERATE (A,<<,B) /* { dg-message "expanded|expansion" } */
+
+#define MULT(A) \
+  SHIFTL (A,1)			/* { dg-message "expanded|expansion" } */
+
+void
+foo ()
+{
+  MULT (1.0);			/* { dg-message "expanded" } */
+}
+
+/* { dg-error "invalid operands to binary <<" "" { target *-*-* } { 10 } } */
diff --git a/gcc/testsuite/gcc.dg/cpp/macro-exp-tracking-3.c b/gcc/testsuite/gcc.dg/cpp/macro-exp-tracking-3.c
new file mode 100644
index 0000000..119053e
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/cpp/macro-exp-tracking-3.c
@@ -0,0 +1,14 @@
+/*
+  { dg-options "-fshow-column -ftrack-macro-expansion=1" }
+  { dg-do compile }
+ */
+
+#define SQUARE(A) A * A		/* { dg-message "expansion" } */
+
+void
+foo()
+{
+  SQUARE (1 << 0.1);		/* { dg-message "expanded" } */
+}
+
+/* { dg-error "16:invalid operands to binary <<" "" {target *-*-* } { 11 } } */
diff --git a/gcc/testsuite/gcc.dg/cpp/macro-exp-tracking-4.c b/gcc/testsuite/gcc.dg/cpp/macro-exp-tracking-4.c
new file mode 100644
index 0000000..1f9fe6a
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/cpp/macro-exp-tracking-4.c
@@ -0,0 +1,14 @@
+/*
+  { dg-options "-fshow-column -ftrack-macro-expansion=2" }
+  { dg-do compile }
+ */
+
+#define SQUARE(A) A * A		/* { dg-message "expansion" } */
+
+void
+foo()
+{
+  SQUARE (1 << 0.1);		/* { dg-message "expanded" } */
+}
+
+/* { dg-error "13:invalid operands to binary <<" "" { target *-*-* } { 11 } } */
diff --git a/gcc/testsuite/gcc.dg/cpp/pragma-diagnostic-2.c b/gcc/testsuite/gcc.dg/cpp/pragma-diagnostic-2.c
new file mode 100644
index 0000000..7ab95b0
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/cpp/pragma-diagnostic-2.c
@@ -0,0 +1,34 @@
+/*
+  { dg-options "-Wuninitialized -ftrack-macro-expansion=2" }
+  { dg-do compile }
+*/
+
+void f (unsigned);
+
+#define CODE_WITH_WARNING \
+  int a; /* { dg-message "expansion|declared here" } */  \
+  f (a)	 /* { dg-message "expansion" } */
+
+#pragma GCC diagnostic ignored "-Wuninitialized"
+
+void
+g (void)
+{
+  CODE_WITH_WARNING;
+}
+
+#pragma GCC diagnostic push
+
+#pragma GCC diagnostic error "-Wuninitialized"
+
+void
+h (void)
+{
+  CODE_WITH_WARNING;		/* { dg-message "expanded" } */
+}
+
+/*
+  { dg-message "some warnings being treated as errors" "" {target *-*-*} 0 }
+*/
+
+/* { dg-error "uninitialized" "" { target *-*-* } { 10 } } */
diff --git a/gcc/testsuite/lib/prune.exp b/gcc/testsuite/lib/prune.exp
index 4683f93..09d2581 100644
--- a/gcc/testsuite/lib/prune.exp
+++ b/gcc/testsuite/lib/prune.exp
@@ -29,6 +29,7 @@ proc prune_gcc_output { text } {
     regsub -all "(^|\n)collect: re(compiling|linking)\[^\n\]*" $text "" text
     regsub -all "(^|\n)Please submit.*instructions\[^\n\]*" $text "" text
     regsub -all "(^|\n)\[0-9\]\[0-9\]* errors\." $text "" text
+    regsub -all "(^|\n)(In file included|\[ \]+from)\[^\n\]*" $text "" text
 
     # Ignore informational notes.
     regsub -all "(^|\n)\[^\n\]*: note: \[^\n\]*" $text "" text
diff --git a/gcc/toplev.c b/gcc/toplev.c
index ab6b5a4..0188755 100644
--- a/gcc/toplev.c
+++ b/gcc/toplev.c
@@ -1171,6 +1171,9 @@ general_init (const char *argv0)
      can give warnings and errors.  */
   diagnostic_initialize (global_dc, N_OPTS);
   diagnostic_starter (global_dc) = default_tree_diagnostic_starter;
+  /* By default print macro expansion contexts in the diagnostic
+     finalizer -- for tokens resulting from macro macro expansion.  */
+  diagnostic_finalizer (global_dc) = virt_loc_aware_diagnostic_finalizer;
   /* Set a default printer.  Language specific initializations will
      override it later.  */
   pp_format_decoder (global_dc->printer) = &default_tree_printer;
diff --git a/gcc/tree-diagnostic.c b/gcc/tree-diagnostic.c
index b456a2a..53b350b 100644
--- a/gcc/tree-diagnostic.c
+++ b/gcc/tree-diagnostic.c
@@ -28,6 +28,7 @@ along with GCC; see the file COPYING3.  If not see
 #include "tree-diagnostic.h"
 #include "langhooks.h"
 #include "langhooks-def.h"
+#include "vec.h"
 
 /* Prints out, if necessary, the name of the current function
    that caused an error.  Called from all error and warning functions.  */
@@ -35,7 +36,7 @@ void
 diagnostic_report_current_function (diagnostic_context *context,
 				    diagnostic_info *diagnostic)
 {
-  diagnostic_report_current_module (context);
+  diagnostic_report_current_module (context, diagnostic->location);
   lang_hooks.print_error_function (context, input_filename, diagnostic);
 }
 
@@ -47,3 +48,182 @@ default_tree_diagnostic_starter (diagnostic_context *context,
   pp_set_prefix (context->printer, diagnostic_build_prefix (context,
 							    diagnostic));
 }
+
+/* This is a pair made of a location and the line map it originated
+   from.  It's used in the maybe_unwind_expanded_macro_loc function
+   below.  */
+typedef struct
+{
+  const struct line_map *map;
+  source_location where;
+} loc_t;
+
+DEF_VEC_O (loc_t);
+DEF_VEC_ALLOC_O (loc_t, heap);
+
+/* Unwind the different macro expansions that lead to the token which
+   location is WHERE and emit diagnostics showing the resulting
+   unwound macro expansion trace.  Let's look at an example to see how
+   the trace looks like.  Suppose we have this piece of code,
+   artificially annotated with the line numbers to increase
+   legibility:
+
+    $ cat -n test.c
+      1    #define OPERATE(OPRD1, OPRT, OPRD2) \
+      2      OPRD1 OPRT OPRD2;
+      3
+      4    #define SHIFTL(A,B) \
+      5      OPERATE (A,<<,B)
+      6
+      7    #define MULT(A) \
+      8      SHIFTL (A,1)
+      9
+     10    void
+     11    g ()
+     12    {
+     13      MULT (1.0);// 1.0 << 1; <-- so this is an error.
+     14    }
+
+   Here is the diagnostic that we want the compiler to generate:
+
+    test.c: In function 'g':
+    test.c:5:14: error: invalid operands to binary << (have 'double' and 'int')
+    test.c:2:9: note: in expansion of macro 'OPERATE'
+    test.c:5:3: note: expanded from here
+    test.c:5:14: note: in expansion of macro 'SHIFTL'
+    test.c:8:3: note: expanded from here
+    test.c:8:3: note: in expansion of macro 'MULT2'
+    test.c:13:3: note: expanded from here
+
+   The part that goes from the third to the eighth line of this
+   diagnostic (the lines containing the 'note:' string) is called the
+   unwound macro expansion trace.  That's the part generated by this
+   function.
+
+   If FIRST_EXP_POINT_MAP is non-null, *FIRST_EXP_POINT_MAP is set to
+   the map of the location in the source that first triggered the
+   macro expansion.  This must be an ordinary map.  */
+
+static void
+maybe_unwind_expanded_macro_loc (diagnostic_context *context,
+                                 diagnostic_info *diagnostic,
+                                 source_location where,
+                                 const struct line_map **first_exp_point_map)
+{
+  const struct line_map *map;
+  VEC(loc_t,heap) *loc_vec = NULL;
+  unsigned ix;
+  loc_t loc, *iter;
+
+  map = linemap_lookup (line_table, where);
+  if (!linemap_macro_expansion_map_p (map))
+    return;
+
+  /* Let's unwind the macros that got expanded and led to the token
+     which location is WHERE.  We are going to store these macros into
+     LOC_VEC, so that we can later walk it at our convenience to
+     display a somewhat meaningful trace of the macro expansion
+     history to the user.  Note that the first macro of the trace
+     (which is OPERATE in the example above) is going to be stored at
+     the beginning of LOC_VEC.  */
+
+  do
+    {
+      loc.where = where;
+      loc.map = map;
+
+      VEC_safe_push (loc_t, heap, loc_vec, &loc);
+
+      /* WHERE is the location of a token inside the expansion of a
+         macro.  MAP is the map holding the locations of that macro
+         expansion.  Let's get the location of the token inside the
+         context that triggered the expansion of this macro.
+         This is basically how we go "down" in the trace of macro
+         expansions that led to WHERE.  */
+      where = linemap_unwind_toward_expansion (line_table, where, &map);
+    } while (linemap_macro_expansion_map_p (map));
+
+  if (first_exp_point_map)
+    *first_exp_point_map = map;
+
+  /* Walk LOC_VEC and print the macro expansion trace, unless the
+     first macro which expansion triggered this trace was expanded
+     inside a system header.  */
+  if (!LINEMAP_SYSP (map))
+    FOR_EACH_VEC_ELT (loc_t, loc_vec, ix, iter)
+      {
+        source_location resolved_def_loc = 0, resolved_exp_loc = 0;
+        diagnostic_t saved_kind;
+        const char *saved_prefix;
+        source_location saved_location;
+
+        /* Okay, now here is what we want.  For each token resulting
+           from macro expansion we want to show: 1/ where in the
+           definition of the macro the token comes from; 2/ where the
+           macro got expanded.  */
+
+        /* Resolve the location iter->where into the locus 1/ of the
+           comment above.  */
+        resolved_def_loc =
+          linemap_resolve_location (line_table, iter->where,
+                                    LRK_MACRO_DEFINITION_LOCATION, NULL);
+
+        /* Resolve the location of the expansion point of the macro
+           which expansion gave the token represented by def_loc.
+           This is the locus 2/ of the earlier comment.  */
+        resolved_exp_loc =
+          linemap_resolve_location (line_table,
+                                    MACRO_MAP_EXPANSION_POINT_LOCATION (iter->map),
+                                    LRK_MACRO_DEFINITION_LOCATION, NULL);
+
+        saved_kind = diagnostic->kind;
+        saved_prefix = context->printer->prefix;
+        saved_location = diagnostic->location;
+
+        diagnostic->kind = DK_NOTE;
+        diagnostic->location = resolved_def_loc;
+        pp_base_set_prefix (context->printer,
+                            diagnostic_build_prefix (context,
+                                                     diagnostic));
+        pp_newline (context->printer);
+        pp_printf (context->printer, "in expansion of macro '%s'",
+                   linemap_map_get_macro_name (iter->map));
+        pp_destroy_prefix (context->printer);
+
+        diagnostic->location = resolved_exp_loc;
+        pp_base_set_prefix (context->printer,
+                            diagnostic_build_prefix (context,
+                                                     diagnostic));
+        pp_newline (context->printer);
+        pp_printf (context->printer, "expanded from here");
+        pp_destroy_prefix (context->printer);
+
+        diagnostic->kind = saved_kind;
+        diagnostic->location = saved_location;
+        context->printer->prefix = saved_prefix;
+      }
+
+  VEC_free (loc_t, heap, loc_vec);
+}
+
+/*  This is a diagnostic finalizer implementation that is aware of
+    virtual locations produced by libcpp.
+
+    It has to be called by the diagnostic finalizer of front ends that
+    uses libcpp and wish to get diagnostics involving tokens resulting
+    from macro expansion.
+
+    For a given location, if said location belongs to a token
+    resulting from a macro expansion, this starter prints the context
+    of the token.  E.g, for multiply nested macro expansion, it
+    unwinds the nested macro expansions and prints them in a manner
+    that is similar to what is done for function call stacks, or
+    template instantiation contexts.  */
+void
+virt_loc_aware_diagnostic_finalizer (diagnostic_context *context,
+				     diagnostic_info *diagnostic)
+{
+  maybe_unwind_expanded_macro_loc (context, diagnostic,
+				   diagnostic->location,
+				   NULL);
+}
diff --git a/gcc/tree-diagnostic.h b/gcc/tree-diagnostic.h
index 7d88089..6b8e8e6 100644
--- a/gcc/tree-diagnostic.h
+++ b/gcc/tree-diagnostic.h
@@ -52,5 +52,6 @@ along with GCC; see the file COPYING3.  If not see
 void default_tree_diagnostic_starter (diagnostic_context *, diagnostic_info *);
 extern void diagnostic_report_current_function (diagnostic_context *,
 						diagnostic_info *);
-
+void virt_loc_aware_diagnostic_finalizer (diagnostic_context *,
+					  diagnostic_info *);
 #endif /* ! GCC_TREE_DIAGNOSTIC_H */
-- 
1.7.6.4

^ permalink raw reply	[flat|nested] 24+ messages in thread

* [PATCH 0/6] Tracking locations of tokens resulting from macro expansion
@ 2011-10-17  9:58 Dodji Seketeli
  2011-10-17  9:58 ` [PATCH 3/6] Emit macro expansion related diagnostics Dodji Seketeli
                   ` (5 more replies)
  0 siblings, 6 replies; 24+ messages in thread
From: Dodji Seketeli @ 2011-10-17  9:58 UTC (permalink / raw)
  To: gcc-patches; +Cc: jason, tromey, gdr, joseph, burnus, charlet

This set of patches to track locations of tokens access macro
expansion was reviewed and accepted at
http://gcc.gnu.org/ml/gcc-patches/2011-10/msg01346.html.

I have bootstrapped and tested it on x86_64-unknown-linux-gnu against
trunk and I am checking it in now.

Thanks.

^ permalink raw reply	[flat|nested] 24+ messages in thread

* [PATCH 4/6] Support -fdebug-cpp option
  2011-10-17  9:58 [PATCH 0/6] Tracking locations of tokens resulting from macro expansion Dodji Seketeli
  2011-10-17  9:58 ` [PATCH 3/6] Emit macro expansion related diagnostics Dodji Seketeli
@ 2011-10-17 10:08 ` Dodji Seketeli
  2011-10-17 10:19 ` [PATCH 6/6] Reduce memory waste due to non-power-of-2 allocs Dodji Seketeli
                   ` (3 subsequent siblings)
  5 siblings, 0 replies; 24+ messages in thread
From: Dodji Seketeli @ 2011-10-17 10:08 UTC (permalink / raw)
  To: gcc-patches; +Cc: jason, tromey, gdr, joseph, burnus, charlet

This patch adds -fdebug-cpp option. When used with -E this dumps the
relevant macro map before every single token. This clutters the output
a lot but has proved to be invaluable in tracking some bugs during the
development of the virtual location support.

gcc/ChangeLog
2011-10-15  Tom Tromey  <tromey@redhat.com>
 	    Dodji Seketeli  <dodji@redhat.com>

	* doc/cppopts.texi: Document -fdebug-cpp.
	* doc/invoke.texi: Add -fdebug-cpp to the list of preprocessor
	options.

gcc/c-family/ChangeLog
2011-10-15  Tom Tromey  <tromey@redhat.com>
 	    Dodji Seketeli  <dodji@redhat.com>

	* c.opt (fdebug-cpp): New option.
	* c-opts.c (c_common_handle_option): Handle the option.
	* c-ppoutput.c (maybe_print_line_1): New static function. Takes an
	output stream in parameter. Factorized from ...
	(maybe_print_line): ... this. Dump location debug information when
	-fdebug-cpp is in effect.
	(print_line_1): New static function. Takes an output stream in
	parameter. Factorized from ...
	(print_line): ... here. Dump location information when -fdebug-cpp
	is in effect.
	(scan_translation_unit): Dump location information when
	-fdebug-cpp is in effect.

libcpp/ChangeLog
2011-10-15  Tom Tromey  <tromey@redhat.com>
 	    Dodji Seketeli  <dodji@redhat.com>

	* include/cpplib.h (struct cpp_options)<debug>: New struct member.
	* include/line-map.h (linemap_dump_location): Declare ...
	* line-map.c (linemap_dump_location): ... new function.

---
 gcc/ChangeLog             |    7 +++++
 gcc/c-family/ChangeLog    |   16 ++++++++++++
 gcc/c-family/c-opts.c     |    4 +++
 gcc/c-family/c-ppoutput.c |   57 ++++++++++++++++++++++++++++++++++++--------
 gcc/c-family/c.opt        |    4 +++
 gcc/doc/cppopts.texi      |   13 ++++++++++
 gcc/doc/invoke.texi       |    2 +-
 libcpp/ChangeLog          |    7 +++++
 libcpp/include/cpplib.h   |    4 +++
 libcpp/include/line-map.h |    4 +++
 libcpp/line-map.c         |   38 ++++++++++++++++++++++++++++++
 11 files changed, 144 insertions(+), 12 deletions(-)

diff --git a/gcc/c-family/c-opts.c b/gcc/c-family/c-opts.c
index 3184539..6869d5c 100644
--- a/gcc/c-family/c-opts.c
+++ b/gcc/c-family/c-opts.c
@@ -628,6 +628,10 @@ c_common_handle_option (size_t scode, const char *arg, int value,
       cpp_opts->preprocessed = value;
       break;
 
+    case OPT_fdebug_cpp:
+      cpp_opts->debug = 1;
+      break;
+
     case OPT_ftrack_macro_expansion:
       if (value)
 	value = 2;
diff --git a/gcc/c-family/c-ppoutput.c b/gcc/c-family/c-ppoutput.c
index 892f1ea..df46ce4 100644
--- a/gcc/c-family/c-ppoutput.c
+++ b/gcc/c-family/c-ppoutput.c
@@ -59,7 +59,9 @@ static void account_for_newlines (const unsigned char *, size_t);
 static int dump_macro (cpp_reader *, cpp_hashnode *, void *);
 static void dump_queued_macros (cpp_reader *);
 
+static void print_line_1 (source_location, const char*, FILE *);
 static void print_line (source_location, const char *);
+static void maybe_print_line_1 (source_location, FILE *);
 static void maybe_print_line (source_location);
 static void do_line_change (cpp_reader *, const cpp_token *,
 			    source_location, int);
@@ -243,7 +245,12 @@ scan_translation_unit (cpp_reader *pfile)
 	  in_pragma = false;
 	}
       else
-	cpp_output_token (token, print.outf);
+	{
+	  if (cpp_get_options (parse_in)->debug)
+	      linemap_dump_location (line_table, token->src_loc,
+				     print.outf);
+	  cpp_output_token (token, print.outf);
+	}
 
       if (token->type == CPP_COMMENT)
 	account_for_newlines (token->val.str.text, token->val.str.len);
@@ -297,8 +304,9 @@ scan_translation_unit_trad (cpp_reader *pfile)
 /* If the token read on logical line LINE needs to be output on a
    different line to the current one, output the required newlines or
    a line marker, and return 1.  Otherwise return 0.  */
+
 static void
-maybe_print_line (source_location src_loc)
+maybe_print_line_1 (source_location src_loc, FILE *stream)
 {
   int src_line = LOCATION_LINE (src_loc);
   const char *src_file = LOCATION_FILE (src_loc);
@@ -306,7 +314,7 @@ maybe_print_line (source_location src_loc)
   /* End the previous line of text.  */
   if (print.printed)
     {
-      putc ('\n', print.outf);
+      putc ('\n', stream);
       print.src_line++;
       print.printed = 0;
     }
@@ -318,22 +326,37 @@ maybe_print_line (source_location src_loc)
     {
       while (src_line > print.src_line)
 	{
-	  putc ('\n', print.outf);
+	  putc ('\n', stream);
 	  print.src_line++;
 	}
     }
   else
-    print_line (src_loc, "");
+    print_line_1 (src_loc, "", stream);
+
+}
+
+/* If the token read on logical line LINE needs to be output on a
+   different line to the current one, output the required newlines or
+   a line marker, and return 1.  Otherwise return 0.  */
+
+static void
+maybe_print_line (source_location src_loc)
+{
+  if (cpp_get_options (parse_in)->debug)
+    linemap_dump_location (line_table, src_loc,
+			   print.outf);
+  maybe_print_line_1 (src_loc, print.outf);
 }
 
 /* Output a line marker for logical line LINE.  Special flags are "1"
    or "2" indicating entering or leaving a file.  */
+
 static void
-print_line (source_location src_loc, const char *special_flags)
+print_line_1 (source_location src_loc, const char *special_flags, FILE *stream)
 {
   /* End any previous line of text.  */
   if (print.printed)
-    putc ('\n', print.outf);
+    putc ('\n', stream);
   print.printed = 0;
 
   if (!flag_no_line_commands)
@@ -354,20 +377,32 @@ print_line (source_location src_loc, const char *special_flags)
 			    (const unsigned char *) file_path,
 			    to_file_len);
       *p = '\0';
-      fprintf (print.outf, "# %u \"%s\"%s",
+      fprintf (stream, "# %u \"%s\"%s",
 	       print.src_line == 0 ? 1 : print.src_line,
 	       to_file_quoted, special_flags);
 
       sysp = in_system_header_at (src_loc);
       if (sysp == 2)
-	fputs (" 3 4", print.outf);
+	fputs (" 3 4", stream);
       else if (sysp == 1)
-	fputs (" 3", print.outf);
+	fputs (" 3", stream);
 
-      putc ('\n', print.outf);
+      putc ('\n', stream);
     }
 }
 
+/* Output a line marker for logical line LINE.  Special flags are "1"
+   or "2" indicating entering or leaving a file.  */
+
+static void
+print_line (source_location src_loc, const char *special_flags)
+{
+    if (cpp_get_options (parse_in)->debug)
+      linemap_dump_location (line_table, src_loc,
+			     print.outf);
+    print_line_1 (src_loc, special_flags, print.outf);
+}
+
 /* Helper function for cb_line_change and scan_translation_unit.  */
 static void
 do_line_change (cpp_reader *pfile, const cpp_token *token,
diff --git a/gcc/c-family/c.opt b/gcc/c-family/c.opt
index 07a6b87..f9db8f1 100644
--- a/gcc/c-family/c.opt
+++ b/gcc/c-family/c.opt
@@ -739,6 +739,10 @@ fconstexpr-depth=
 C++ ObjC++ Joined RejectNegative UInteger Var(max_constexpr_depth) Init(512)
 -fconstexpr-depth=<number>	Specify maximum constexpr recursion depth
 
+fdebug-cpp
+C ObjC C++ ObjC++
+Emit debug annotations during preprocessing
+
 fdeduce-init-list
 C++ ObjC++ Var(flag_deduce_init_list) Init(1)
 -fno-deduce-init-list	disable deduction of std::initializer_list for a template type parameter from a brace-enclosed initializer-list
diff --git a/gcc/doc/cppopts.texi b/gcc/doc/cppopts.texi
index b225236..ef3a0b2 100644
--- a/gcc/doc/cppopts.texi
+++ b/gcc/doc/cppopts.texi
@@ -583,6 +583,19 @@ correct column numbers in warnings or errors, even if tabs appear on the
 line.  If the value is less than 1 or greater than 100, the option is
 ignored.  The default is 8.
 
+@item -fdebug-cpp
+@opindex fdebug-cpp
+This option is only useful for debugging GCC.  When used with
+@option{-E}, dumps debugging information about location maps.  Every
+token in the output is preceded by the dump of the map its location
+belongs to.  The dump of the map holding the location of a token would
+be:
+@quotation
+@{@samp{P}:@file{/file/path};@samp{F}:@file{/includer/path};@samp{L}:@var{line_num};@samp{C}:@var{col_num};@samp{S}:@var{system_header_p};@samp{M}:@var{map_address};@samp{E}:@var{macro_expansion_p},@samp{loc}:@var{location}@}
+@end quotation
+
+When used without @option{-E}, this option has no effect.
+
 @item -ftrack-macro-expansion@r{[}=@var{level}@r{]}
 @opindex ftrack-macro-expansion
 Track locations of tokens across macro expansions. This allows the
diff --git a/gcc/doc/invoke.texi b/gcc/doc/invoke.texi
index 3c841ef..7c81d90 100644
--- a/gcc/doc/invoke.texi
+++ b/gcc/doc/invoke.texi
@@ -428,7 +428,7 @@ Objective-C and Objective-C++ Dialects}.
 -iwithprefixbefore @var{dir}  -isystem @var{dir} @gol
 -imultilib @var{dir} -isysroot @var{dir} @gol
 -M  -MM  -MF  -MG  -MP  -MQ  -MT  -nostdinc  @gol
--P -ftrack-macro-expansion -fworking-directory @gol
+-P  -fdebug-cpp -ftrack-macro-expansion -fworking-directory @gol
 -remap -trigraphs  -undef  -U@var{macro}  @gol
 -Wp,@var{option} -Xpreprocessor @var{option}}
 
diff --git a/libcpp/include/cpplib.h b/libcpp/include/cpplib.h
index 3e01c11..825bf2f 100644
--- a/libcpp/include/cpplib.h
+++ b/libcpp/include/cpplib.h
@@ -392,6 +392,10 @@ struct cpp_options
   /* Nonzero means we're looking at already preprocessed code, so don't
      bother trying to do macro expansion and whatnot.  */
   unsigned char preprocessed;
+  
+  /* Nonzero means we are going to emit debugging logs during
+     preprocessing.  */
+  unsigned char debug;
 
   /* Nonzero means we are tracking locations of tokens involved in
      macro expansion. 1 Means we track the location in degraded mode
diff --git a/libcpp/include/line-map.h b/libcpp/include/line-map.h
index 724f3f0..04a523c 100644
--- a/libcpp/include/line-map.h
+++ b/libcpp/include/line-map.h
@@ -675,4 +675,8 @@ expanded_location linemap_expand_location_full (struct line_maps *,
 						source_location loc,
 						enum location_resolution_kind lrk);
 
+/* Dump debugging information about source location LOC into the file
+   stream STREAM. SET is the line map set LOC comes from.  */
+void linemap_dump_location (struct line_maps *, source_location, FILE *);
+
 #endif /* !LIBCPP_LINE_MAP_H  */
diff --git a/libcpp/line-map.c b/libcpp/line-map.c
index fe07c16..3dbaeab 100644
--- a/libcpp/line-map.c
+++ b/libcpp/line-map.c
@@ -1105,3 +1105,41 @@ linemap_expand_location_full (struct line_maps *set,
   xloc = linemap_expand_location (map, loc);
   return xloc;
 }
+
+/* Dump debugging information about source location LOC into the file
+   stream STREAM. SET is the line map set LOC comes from.  */
+
+void
+linemap_dump_location (struct line_maps *set,
+		       source_location loc,
+		       FILE *stream)
+{
+  const struct line_map *map;
+  source_location location;
+  const char *path, *from;
+  int l,c,s,e;
+
+  if (loc == 0)
+    return;
+
+  location =
+    linemap_resolve_location (set, loc, LRK_MACRO_DEFINITION_LOCATION, &map);
+  path = LINEMAP_FILE (map);
+
+  l = SOURCE_LINE (map, location);
+  c = SOURCE_COLUMN (map, location);
+  s = LINEMAP_SYSP (map) != 0;
+  e = location != loc;
+
+  if (e)
+    from = "N/A";
+  else
+    from = (INCLUDED_FROM (set, map))
+      ? LINEMAP_FILE (INCLUDED_FROM (set, map))
+      : "<NULL>";
+
+  /* P: path, L: line, C: column, S: in-system-header, M: map address,
+     E: macro expansion?.   */
+  fprintf (stream, "{P:%s;F:%s;L:%d;C:%d;S:%d;M:%p;E:%d,LOC:%d}",
+	   path, from, l, c, s, (void*)map, e, loc);
+}
-- 
1.7.6.4

^ permalink raw reply	[flat|nested] 24+ messages in thread

* [PATCH 6/6] Reduce memory waste due to non-power-of-2 allocs
  2011-10-17  9:58 [PATCH 0/6] Tracking locations of tokens resulting from macro expansion Dodji Seketeli
  2011-10-17  9:58 ` [PATCH 3/6] Emit macro expansion related diagnostics Dodji Seketeli
  2011-10-17 10:08 ` [PATCH 4/6] Support -fdebug-cpp option Dodji Seketeli
@ 2011-10-17 10:19 ` Dodji Seketeli
  2011-10-17 10:22 ` [PATCH 5/6] Add line map statistics to -fmem-report output Dodji Seketeli
                   ` (2 subsequent siblings)
  5 siblings, 0 replies; 24+ messages in thread
From: Dodji Seketeli @ 2011-10-17 10:19 UTC (permalink / raw)
  To: gcc-patches; +Cc: jason, tromey, gdr, joseph, burnus, charlet

This patch basically arranges for the allocation size of line_map
buffers to be as close as possible to a power of two.  This
*significantly* decreases peak memory consumption as (macro) maps are
numerous and stay live during all the compilation.

The patch adds a new ggc_round_alloc_size interface to the ggc
allocator.  In each of the two main allocator implementations ('page'
and 'zone') the function has been extracted from the main allocation
function code and returns the actual size of the allocated memory
region, thus giving a chance to the caller to maximize the amount of
memory it actually uses from the allocated memory region.  In the
'none' allocator implementation (that uses xmalloc) the
ggc_round_alloc_size just returns the requested allocation size.

gcc/ChangeLog
2011-10-15  Tom Tromey  <tromey@redhat.com>
 	    Dodji Seketeli  <dodji@redhat.com>

	* ggc.h (ggc_round_alloc_size): Declare new public entry point.
	* ggc-none.c (ggc_round_alloc_size): New public stub function.
	* ggc-page.c (ggc_alloced_size_order_for_request): New static
	function.  Factorized from ggc_internal_alloc_stat.
	(ggc_round_alloc_size): New public function.  Uses
	ggc_alloced_size_order_for_request.
	(ggc_internal_alloc_stat): Use ggc_alloced_size_order_for_request.
	* ggc-zone.c (ggc_round_alloc_size): New public function extracted
	from ggc_internal_alloc_zone_stat.
	(ggc_internal_alloc_zone_stat): Use ggc_round_alloc_size.
	* toplev.c (general_init): Initialize
	line_table->alloced_size_for_request.

libcpp/ChangeLog
2011-10-15  Tom Tromey  <tromey@redhat.com>
 	    Dodji Seketeli  <dodji@redhat.com>

	* include/line-map.h (struct line_maps::alloced_size_for_request):
	New member.
	* line-map.c (new_linemap): Use set->alloced_size_for_request to
	get the actual allocated size of line maps.

---
 gcc/ChangeLog             |   16 +++++++++++++
 gcc/ggc-none.c            |    9 +++++++
 gcc/ggc-page.c            |   53 +++++++++++++++++++++++++++++++++++---------
 gcc/ggc-zone.c            |   27 ++++++++++++++++------
 gcc/ggc.h                 |    2 +
 gcc/toplev.c              |    1 +
 libcpp/ChangeLog          |    8 ++++++
 libcpp/include/line-map.h |    8 ++++++
 libcpp/line-map.c         |   39 ++++++++++++++++++++++++++++-----
 9 files changed, 138 insertions(+), 25 deletions(-)

diff --git a/gcc/ggc-none.c b/gcc/ggc-none.c
index 97d25b9..e57d617 100644
--- a/gcc/ggc-none.c
+++ b/gcc/ggc-none.c
@@ -39,6 +39,15 @@ ggc_alloc_typed_stat (enum gt_types_enum ARG_UNUSED (gte), size_t size
   return xmalloc (size);
 }
 
+/* For a given size of memory requested for allocation, return the
+   actual size that is going to be allocated.  */
+
+size_t
+ggc_round_alloc_size (size_t requested_size)
+{
+  return requested_size;
+}
+
 void *
 ggc_internal_alloc_stat (size_t size MEM_STAT_DECL)
 {
diff --git a/gcc/ggc-page.c b/gcc/ggc-page.c
index 624f029..f919a6b 100644
--- a/gcc/ggc-page.c
+++ b/gcc/ggc-page.c
@@ -1054,6 +1054,47 @@ static unsigned char size_lookup[NUM_SIZE_LOOKUP] =
   9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9
 };
 
+/* For a given size of memory requested for allocation, return the
+   actual size that is going to be allocated, as well as the size
+   order.  */
+
+static void
+ggc_round_alloc_size_1 (size_t requested_size,
+			size_t *size_order,
+			size_t *alloced_size)
+{
+  size_t order, object_size;
+
+  if (requested_size < NUM_SIZE_LOOKUP)
+    {
+      order = size_lookup[requested_size];
+      object_size = OBJECT_SIZE (order);
+    }
+  else
+    {
+      order = 10;
+      while (requested_size > (object_size = OBJECT_SIZE (order)))
+        order++;
+    }
+
+  if (size_order)
+    *size_order = order;
+  if (alloced_size)
+    *alloced_size = object_size;
+}
+
+/* For a given size of memory requested for allocation, return the
+   actual size that is going to be allocated.  */
+
+size_t
+ggc_round_alloc_size (size_t requested_size)
+{
+  size_t size = 0;
+  
+  ggc_round_alloc_size_1 (requested_size, NULL, &size);
+  return size;
+}
+
 /* Typed allocation function.  Does nothing special in this collector.  */
 
 void *
@@ -1072,17 +1113,7 @@ ggc_internal_alloc_stat (size_t size MEM_STAT_DECL)
   struct page_entry *entry;
   void *result;
 
-  if (size < NUM_SIZE_LOOKUP)
-    {
-      order = size_lookup[size];
-      object_size = OBJECT_SIZE (order);
-    }
-  else
-    {
-      order = 10;
-      while (size > (object_size = OBJECT_SIZE (order)))
-	order++;
-    }
+  ggc_round_alloc_size_1 (size, &order, &object_size);
 
   /* If there are non-full pages for this size allocation, they are at
      the head of the list.  */
diff --git a/gcc/ggc-zone.c b/gcc/ggc-zone.c
index d0c1d79..79c8c03 100644
--- a/gcc/ggc-zone.c
+++ b/gcc/ggc-zone.c
@@ -1073,6 +1073,24 @@ free_chunk (char *ptr, size_t size, struct alloc_zone *zone)
     fprintf (G.debug_file, "Deallocating object, chunk=%p\n", (void *)chunk);
 }
 
+/* For a given size of memory requested for allocation, return the
+   actual size that is going to be allocated.  */
+
+size_t
+ggc_round_alloc_size (size_t requested_size)
+{
+  size_t size;
+
+  /* Make sure that zero-sized allocations get a unique and freeable
+     pointer.  */
+  if (requested_size == 0)
+    size = MAX_ALIGNMENT;
+  else
+    size = (requested_size + MAX_ALIGNMENT - 1) & -MAX_ALIGNMENT;
+
+  return size;
+}
+
 /* Allocate a chunk of memory of at least ORIG_SIZE bytes, in ZONE.  */
 
 void *
@@ -1084,14 +1102,7 @@ ggc_internal_alloc_zone_stat (size_t orig_size, struct alloc_zone *zone
   struct small_page_entry *entry;
   struct alloc_chunk *chunk, **pp;
   void *result;
-  size_t size = orig_size;
-
-  /* Make sure that zero-sized allocations get a unique and freeable
-     pointer.  */
-  if (size == 0)
-    size = MAX_ALIGNMENT;
-  else
-    size = (size + MAX_ALIGNMENT - 1) & -MAX_ALIGNMENT;
+  size_t size = ggc_alloced_size_for_request (orig_size);
 
   /* Try to allocate the object from several different sources.  Each
      of these cases is responsible for setting RESULT and SIZE to
diff --git a/gcc/ggc.h b/gcc/ggc.h
index 30eca66..704237c 100644
--- a/gcc/ggc.h
+++ b/gcc/ggc.h
@@ -145,6 +145,8 @@ extern void gt_pch_save (FILE *f);
 /* The internal primitive.  */
 extern void *ggc_internal_alloc_stat (size_t MEM_STAT_DECL);
 
+extern size_t ggc_round_alloc_size (size_t requested_size);
+
 #define ggc_internal_alloc(s) ggc_internal_alloc_stat (s MEM_STAT_INFO)
 
 /* Allocate an object of the specified type and size.  */
diff --git a/gcc/toplev.c b/gcc/toplev.c
index 547ce7a..ec08597 100644
--- a/gcc/toplev.c
+++ b/gcc/toplev.c
@@ -1216,6 +1216,7 @@ general_init (const char *argv0)
   line_table = ggc_alloc_line_maps ();
   linemap_init (line_table);
   line_table->reallocator = realloc_for_line_map;
+  line_table->round_alloc_size = ggc_round_alloc_size;
   init_ttree ();
 
   /* Initialize register usage now so switches may override.  */
diff --git a/libcpp/include/line-map.h b/libcpp/include/line-map.h
index 572e330..1e2a148 100644
--- a/libcpp/include/line-map.h
+++ b/libcpp/include/line-map.h
@@ -53,6 +53,10 @@ typedef unsigned int source_location;
 /* Memory allocation function typedef.  Works like xrealloc.  */
 typedef void *(*line_map_realloc) (void *, size_t);
 
+/* Memory allocator function that returns the actual allocated size,
+   for a given requested allocation.  */
+typedef size_t (*line_map_round_alloc_size_func) (size_t);
+
 /* An ordinary line map encodes physical source locations. Those
    physical source locations are called "spelling locations".
    
@@ -281,6 +285,10 @@ struct GTY(()) line_maps {
   /* If non-null, the allocator to use when resizing 'maps'.  If null,
      xrealloc is used.  */
   line_map_realloc reallocator;
+
+  /* The allocators' function used to know the actual size it
+     allocated, for a certain allocation size requested.  */
+  line_map_round_alloc_size_func round_alloc_size;
 };
 
 /* Returns the pointer to the memory region where information about
diff --git a/libcpp/line-map.c b/libcpp/line-map.c
index 9086b3e..87b8bfe 100644
--- a/libcpp/line-map.c
+++ b/libcpp/line-map.c
@@ -92,16 +92,43 @@ new_linemap (struct line_maps *set,
   if (LINEMAPS_USED (set, macro_map_p) == LINEMAPS_ALLOCATED (set, macro_map_p))
     {
       /* We ran out of allocated line maps. Let's allocate more.  */
+      unsigned alloc_size;
 
       line_map_realloc reallocator
 	= set->reallocator ? set->reallocator : xrealloc;
+      line_map_round_alloc_size_func round_alloc_size =
+	set->round_alloc_size;
+
+      /* We are going to execute some dance to try to reduce the
+	 overhead of the memory allocator, in case we are using the
+	 ggc-page.c one.
+	 
+	 The actual size of memory we are going to get back from the
+	 allocator is the smallest power of 2 that is greater than the
+	 size we requested.  So let's consider that size then.  */
+
+      alloc_size =
+	(2 * LINEMAPS_ALLOCATED (set, macro_map_p) +  256)
+	* sizeof (struct line_map);
+
+      /* Get the actual size of memory that is going to be allocated
+	 by the allocator.  */
+      alloc_size = round_alloc_size (alloc_size);
+
+      /* Now alloc_size contains the exact memory size we would get if
+	 we have asked for the initial alloc_size amount of memory.
+	 Let's get back to the number of macro map that amounts
+	 to.  */
       LINEMAPS_ALLOCATED (set, macro_map_p) =
-	2 * LINEMAPS_ALLOCATED (set, macro_map_p) + 256;
-      LINEMAPS_MAPS (set, macro_map_p)
-	= (struct line_map *) (*reallocator) (LINEMAPS_MAPS (set, macro_map_p),
-					      LINEMAPS_ALLOCATED (set,
-								  macro_map_p)
-					      * sizeof (struct line_map));
+	alloc_size / (sizeof (struct line_map));
+
+      /* And now let's really do the re-allocation.  */
+      LINEMAPS_MAPS (set, macro_map_p) =
+	(struct line_map *) (*reallocator)
+	(LINEMAPS_MAPS (set, macro_map_p),
+	 (LINEMAPS_ALLOCATED (set, macro_map_p)
+	  * sizeof (struct line_map)));
+
       result =
 	&LINEMAPS_MAPS (set, macro_map_p)[LINEMAPS_USED (set, macro_map_p)];
       memset (result, 0,
-- 
1.7.6.4

^ permalink raw reply	[flat|nested] 24+ messages in thread

* [PATCH 5/6] Add line map statistics to -fmem-report output
  2011-10-17  9:58 [PATCH 0/6] Tracking locations of tokens resulting from macro expansion Dodji Seketeli
                   ` (2 preceding siblings ...)
  2011-10-17 10:19 ` [PATCH 6/6] Reduce memory waste due to non-power-of-2 allocs Dodji Seketeli
@ 2011-10-17 10:22 ` Dodji Seketeli
  2011-10-17 10:25 ` [PATCH 1/6] Linemap infrastructure for virtual locations Dodji Seketeli
  2011-10-17 10:26 ` [PATCH 2/6] Generate virtual locations for tokens Dodji Seketeli
  5 siblings, 0 replies; 24+ messages in thread
From: Dodji Seketeli @ 2011-10-17 10:22 UTC (permalink / raw)
  To: gcc-patches; +Cc: jason, tromey, gdr, joseph, burnus, charlet

This patch adds statistics about line maps' memory consumption and
macro expansion to the output of -fmem-report.  It has been useful in
trying to reduce the memory consumption of the macro maps support.

gcc/ChangeLog
2011-10-15  Tom Tromey  <tromey@redhat.com>
 	    Dodji Seketeli  <dodji@redhat.com>
 
	* input.c (ONE_K, ONE_M, SCALE, STAT_LABEL, FORMAT_AMOUNT): New
	macros.
	(num_expanded_macros_counter, num_macro_tokens_counter): Declare
	new counters.
	(dump_line_table_statistics): Define new function.
	* input.h (dump_line_table_statistics): Declare new function.
	* toplev.c (dump_memory_report): Call dump_line_table_statistics.

libcpp/ChangeLog
2011-10-15  Tom Tromey  <tromey@redhat.com>
 	    Dodji Seketeli  <dodji@redhat.com>
 
	* line-map.h (struct linemap_stats): Declare new struct.
	(linemap_get_statistics): Declare ...
	* line-map.c (linemap_get_statistics):  ... new function.
	* macro.c (num_expanded_macros_counter, num_macro_tokens_counter):
	Declare new counters.
	(enter_macro_context, replace_args): Update
	num_macro_tokens_counter.
	(cpp_get_token_1): Update num_expanded_macros_counter.

---
 gcc/ChangeLog             |   11 +++++
 gcc/input.c               |   96 +++++++++++++++++++++++++++++++++++++++++++++
 gcc/input.h               |    2 +
 gcc/toplev.c              |    1 +
 libcpp/ChangeLog          |   12 ++++++
 libcpp/include/line-map.h |   21 ++++++++++
 libcpp/line-map.c         |   63 +++++++++++++++++++++++++++++
 libcpp/macro.c            |   29 ++++++++++++--
 8 files changed, 231 insertions(+), 4 deletions(-)

diff --git a/gcc/input.c b/gcc/input.c
index 89af274..41842b7 100644
--- a/gcc/input.c
+++ b/gcc/input.c
@@ -46,3 +46,99 @@ expand_location (source_location loc)
 					 LRK_SPELLING_LOCATION);
   return xloc;
 }
+
+#define ONE_K 1024
+#define ONE_M (ONE_K * ONE_K)
+
+/* Display a number as an integer multiple of either:
+   - 1024, if said integer is >= to 10 K (in base 2)
+   - 1024 * 1024, if said integer is >= 10 M in (base 2)
+ */
+#define SCALE(x) ((unsigned long) ((x) < 10 * ONE_K \
+		  ? (x) \
+		  : ((x) < 10 * ONE_M \
+		     ? (x) / ONE_K \
+		     : (x) / ONE_M)))
+
+/* For a given integer, display either:
+   - the character 'k', if the number is higher than 10 K (in base 2)
+     but strictly lower than 10 M (in base 2)
+   - the character 'M' if the number is higher than 10 M (in base2)
+   - the charcter ' ' if the number is strictly lower  than 10 K  */
+#define STAT_LABEL(x) ((x) < 10 * ONE_K ? ' ' : ((x) < 10 * ONE_M ? 'k' : 'M'))
+
+/* Display an integer amount as multiple of 1K or 1M (in base 2).
+   Display the correct unit (either k, M, or ' ') after the amout, as
+   well.  */
+#define FORMAT_AMOUNT(size) SCALE (size), STAT_LABEL (size)
+
+/* Dump statistics to stderr about the memory usage of the line_table
+   set of line maps.  This also displays some statistics about macro
+   expansion.  */
+
+void
+dump_line_table_statistics (void)
+{
+  struct linemap_stats s;
+  size_t total_used_map_size,
+    macro_maps_size,
+    total_allocated_map_size;
+
+  memset (&s, 0, sizeof (s));
+
+  linemap_get_statistics (line_table, &s);
+
+  macro_maps_size = s.macro_maps_used_size
+    + s.macro_maps_locations_size;
+
+  total_allocated_map_size = s.ordinary_maps_allocated_size
+    + s.macro_maps_allocated_size
+    + s.macro_maps_locations_size;
+
+  total_used_map_size = s.ordinary_maps_used_size
+    + s.macro_maps_used_size
+    + s.macro_maps_locations_size;
+
+  fprintf (stderr, "Number of expanded macros:                     %5lu\n",
+           s.num_expanded_macros);
+  if (s.num_expanded_macros != 0)
+    fprintf (stderr, "Average number of tokens per macro expansion:  %5lu\n",
+             s.num_macro_tokens / s.num_expanded_macros);
+  fprintf (stderr,
+           "\nLine Table allocations during the "
+           "compilation process\n");
+  fprintf (stderr, "Number of ordinary maps used:        %5lu%c\n",
+           SCALE (s.num_ordinary_maps_used),
+           STAT_LABEL (s.num_ordinary_maps_used));
+  fprintf (stderr, "Ordinary map used size:              %5lu%c\n",
+           SCALE (s.ordinary_maps_used_size),
+           STAT_LABEL (s.ordinary_maps_used_size));
+  fprintf (stderr, "Number of ordinary maps allocated:   %5lu%c\n",
+           SCALE (s.num_ordinary_maps_allocated),
+           STAT_LABEL (s.num_ordinary_maps_allocated));
+  fprintf (stderr, "Ordinary maps allocated size:        %5lu%c\n",
+           SCALE (s.ordinary_maps_allocated_size),
+           STAT_LABEL (s.ordinary_maps_allocated_size));
+  fprintf (stderr, "Number of macro maps used:           %5lu%c\n",
+           SCALE (s.num_macro_maps_used),
+           STAT_LABEL (s.num_macro_maps_used));
+  fprintf (stderr, "Macro maps used size:                %5lu%c\n",
+           SCALE (s.macro_maps_used_size),
+           STAT_LABEL (s.macro_maps_used_size));
+  fprintf (stderr, "Macro maps locations size:           %5lu%c\n",
+           SCALE (s.macro_maps_locations_size),
+           STAT_LABEL (s.macro_maps_locations_size));
+  fprintf (stderr, "Macro maps size:                     %5lu%c\n",
+           SCALE (macro_maps_size),
+           STAT_LABEL (macro_maps_size));
+  fprintf (stderr, "Duplicated maps locations size:      %5lu%c\n",
+           SCALE (s.duplicated_macro_maps_locations_size),
+           STAT_LABEL (s.duplicated_macro_maps_locations_size));
+  fprintf (stderr, "Total allocated maps size:           %5lu%c\n",
+           SCALE (total_allocated_map_size),
+           STAT_LABEL (total_allocated_map_size));
+  fprintf (stderr, "Total used maps size:                %5lu%c\n",
+           SCALE (total_used_map_size),
+           STAT_LABEL (total_used_map_size));
+  fprintf (stderr, "\n");
+}
diff --git a/gcc/input.h b/gcc/input.h
index 9fc55f3..f2f3513 100644
--- a/gcc/input.h
+++ b/gcc/input.h
@@ -55,4 +55,6 @@ extern location_t input_location;
   ((linemap_location_in_system_header_p (line_table, LOC)))
 #define in_system_header (in_system_header_at (input_location))
 
+void dump_line_table_statistics (void);
+
 #endif
diff --git a/gcc/toplev.c b/gcc/toplev.c
index 0188755..547ce7a 100644
--- a/gcc/toplev.c
+++ b/gcc/toplev.c
@@ -1827,6 +1827,7 @@ target_reinit (void)
 void
 dump_memory_report (bool final)
 {
+  dump_line_table_statistics ();
   ggc_print_statistics ();
   stringpool_statistics ();
   dump_tree_statistics ();
diff --git a/libcpp/include/line-map.h b/libcpp/include/line-map.h
index 04a523c..572e330 100644
--- a/libcpp/include/line-map.h
+++ b/libcpp/include/line-map.h
@@ -675,6 +675,27 @@ expanded_location linemap_expand_location_full (struct line_maps *,
 						source_location loc,
 						enum location_resolution_kind lrk);
 
+/* Statistics about maps allocation and usage as returned by
+   linemap_get_statistics.  */
+struct linemap_stats
+{
+  size_t num_ordinary_maps_allocated;
+  size_t num_ordinary_maps_used;
+  size_t ordinary_maps_allocated_size;
+  size_t ordinary_maps_used_size;
+  size_t num_expanded_macros;
+  size_t num_macro_tokens;
+  size_t num_macro_maps_used;
+  size_t macro_maps_allocated_size;
+  size_t macro_maps_used_size;
+  size_t macro_maps_locations_size;
+  size_t duplicated_macro_maps_locations_size;
+};
+
+/* Compute and return statistics about the memory consumption of some
+   parts of the line table SET.  */
+void linemap_get_statistics (struct line_maps *, struct linemap_stats *);
+
 /* Dump debugging information about source location LOC into the file
    stream STREAM. SET is the line map set LOC comes from.  */
 void linemap_dump_location (struct line_maps *, source_location, FILE *);
diff --git a/libcpp/line-map.c b/libcpp/line-map.c
index 3dbaeab..9086b3e 100644
--- a/libcpp/line-map.c
+++ b/libcpp/line-map.c
@@ -46,6 +46,10 @@ static source_location linemap_macro_loc_to_exp_point (struct line_maps *,
 						       source_location,
 						       const struct line_map **);
 
+/* Counters defined in macro.c.  */
+extern unsigned num_expanded_macros_counter;
+extern unsigned num_macro_tokens_counter;
+
 /* Initialize a line map set.  */
 
 void
@@ -1143,3 +1147,62 @@ linemap_dump_location (struct line_maps *set,
   fprintf (stream, "{P:%s;F:%s;L:%d;C:%d;S:%d;M:%p;E:%d,LOC:%d}",
 	   path, from, l, c, s, (void*)map, e, loc);
 }
+
+/* Compute and return statistics about the memory consumption of some
+   parts of the line table SET.  */
+
+void
+linemap_get_statistics (struct line_maps *set,
+			struct linemap_stats *s)
+{
+  size_t ordinary_maps_allocated_size, ordinary_maps_used_size,
+    macro_maps_allocated_size, macro_maps_used_size,
+    macro_maps_locations_size = 0, duplicated_macro_maps_locations_size = 0;
+
+  struct line_map *cur_map;
+
+  ordinary_maps_allocated_size =
+    LINEMAPS_ORDINARY_ALLOCATED (set) * sizeof (struct line_map);
+
+  ordinary_maps_used_size =
+    LINEMAPS_ORDINARY_USED (set) * sizeof (struct line_map);
+
+  macro_maps_allocated_size =
+    LINEMAPS_MACRO_ALLOCATED (set) * sizeof (struct line_map);
+
+  for (cur_map = LINEMAPS_MACRO_MAPS (set);
+       cur_map && cur_map <= LINEMAPS_LAST_MACRO_MAP (set);
+       ++cur_map)
+    {
+      unsigned i;
+
+      linemap_assert (linemap_macro_expansion_map_p (cur_map));
+
+      macro_maps_locations_size +=
+	2 * MACRO_MAP_NUM_MACRO_TOKENS (cur_map) * sizeof (source_location);
+
+      for (i = 0; i < 2 * MACRO_MAP_NUM_MACRO_TOKENS (cur_map); i += 2)
+	{
+	  if (MACRO_MAP_LOCATIONS (cur_map)[i] ==
+	      MACRO_MAP_LOCATIONS (cur_map)[i + 1])
+	    duplicated_macro_maps_locations_size +=
+	      sizeof (source_location);
+	}
+    }
+
+  macro_maps_used_size =
+    LINEMAPS_MACRO_USED (set) * sizeof (struct line_map);
+
+  s->num_ordinary_maps_allocated = LINEMAPS_ORDINARY_ALLOCATED (set);
+  s->num_ordinary_maps_used = LINEMAPS_ORDINARY_USED (set);
+  s->ordinary_maps_allocated_size = ordinary_maps_allocated_size;
+  s->ordinary_maps_used_size = ordinary_maps_used_size;
+  s->num_expanded_macros = num_expanded_macros_counter;
+  s->num_macro_tokens = num_macro_tokens_counter;
+  s->num_macro_maps_used = LINEMAPS_MACRO_USED (set);
+  s->macro_maps_allocated_size = macro_maps_allocated_size;
+  s->macro_maps_locations_size = macro_maps_locations_size;
+  s->macro_maps_used_size = macro_maps_used_size;
+  s->duplicated_macro_maps_locations_size =
+    duplicated_macro_maps_locations_size;
+}
diff --git a/libcpp/macro.c b/libcpp/macro.c
index d760383..2d0eeaa 100644
--- a/libcpp/macro.c
+++ b/libcpp/macro.c
@@ -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
@@ -1082,10 +1089,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 +1107,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;
@@ -1711,6 +1728,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 +2259,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;
-- 
1.7.6.4

^ permalink raw reply	[flat|nested] 24+ messages in thread

* [PATCH 1/6] Linemap infrastructure for virtual locations
  2011-10-17  9:58 [PATCH 0/6] Tracking locations of tokens resulting from macro expansion Dodji Seketeli
                   ` (3 preceding siblings ...)
  2011-10-17 10:22 ` [PATCH 5/6] Add line map statistics to -fmem-report output Dodji Seketeli
@ 2011-10-17 10:25 ` Dodji Seketeli
  2011-10-17 19:53   ` Gerald Pfeifer
  2011-10-17 10:26 ` [PATCH 2/6] Generate virtual locations for tokens Dodji Seketeli
  5 siblings, 1 reply; 24+ messages in thread
From: Dodji Seketeli @ 2011-10-17 10:25 UTC (permalink / raw)
  To: gcc-patches; +Cc: jason, tromey, gdr, joseph, burnus, charlet

This is the first instalment of a set which goal is to track locations
of tokens across macro expansions.  Tom Tromey did the original work
and attached the patch to PR preprocessor/7263.  This opus is a
derivative of that original work.

This patch modifies the linemap module of libcpp to add virtual
locations support.

A virtual location is a mapped location that can resolve to several
different physical locations.  It can always resolve to the spelling
location of a token.  For tokens resulting from macro expansion it can
resolve to:
  - either the location of the expansion point of the macro.
  - or the location of the token in the definition of the
  macro
  - or, if the token is an argument of a function-like macro,
  the location of the use of the matching macro parameter in
  the definition of the macro

The patch creates a new type of line map called a macro map.  For every
single macro expansion, there is a macro map that generates a virtual
location for every single resulting token of the expansion.

The good old type of line map we all know is now called an ordinary
map.  That one still encodes spelling locations as it has always had.

As a result linemap_lookup as been extended to return a macro map when
given a virtual location resulting from a macro expansion.  The layout
of structs line_map has changed to support this new type of map.  So
did the layout of struct line_maps.  Accessor macros have been
introduced to avoid messing with the implementation details of these
datastructures directly.  This helped already as we have been testing
different ways of arranging these datastructure.  Having to constantly
adjust client code that is too tied with the internals of line_map and
line_maps would have been even more painful.

Of course, many new public functions have been added to the linemap
module to handle the resolution of virtual locations.

This patch introduces the infrastructure but no part of the compiler
uses virtual locations yet.

However the client code of the linemap data structures has been
adjusted as per the changes.  E.g, it's not anymore reliable for a
client code to manipulate struct line_map directly if it just wants to
deal with spelling locations, because struct line_map can now
represent a macro map as well.  In that case, it's better to use the
convenient API to resolve the initial (possibly virtual) location to a
spelling location (or to an ordinary map) and use that.

This is the reason why the patch adjusts the Java, Ada and Fortran
front ends.

Also, note that virtual locations are not supposed to be ordered for
relations '<' and '>' anymore.  To test if a virtual location appears
"before" another one, one has to use a new operator exposed by the
line map interface.  The patch updates the only spot (in the
diagnostics module) I have found that was making the assumption that
locations were ordered for these relations.  This is the only change
that introduces a use of the new line map API in this patch, so I am
adding a regression test for it only.

libcpp/ChangeLog
2011-10-15  Tom Tromey  <tromey@redhat>
	    Dodji Seketeli  <dodji@redhat.com>

	* include/line-map.h (enum lc_reason)<LC_ENTER_MACRO>: New enum
	member.
	(MAX_SOURCE_LOCATION): New constant.
	(struct line_map_ordinary, struct line_map_macro): New structs.
    	(struct line_map): Turn this into a union of the two above.  Add
	comments.
	(struct maps_info): New struct.
	(struct line_maps)<info_ordinary, info_macro>: Two new fields.
	These now carry the map information that was previously scattered
	in struct line_maps.
	(struct map_info::allocated): Fix comment.
	(MAP_START_LOCATION, ORDINARY_MAP_FILE_NAME)
	(ORDINARY_MAP_STARTING_LINE_NUMBER)
	(ORDINARY_MAP_INCLUDER_FILE_INDEX)
	(ORDINARY_MAP_IN_SYSTEM_HEADER_P)
	(ORDINARY_MAP_NUMBER_OF_COLUMN_BITS, MACRO_MAP_MACRO)
	(MACRO_MAP_NUM_MACRO_TOKENS MACRO_MAP_LOCATIONS)
	(MACRO_MAP_EXPANSION_POINT_LOCATION)
	(LOCATION_POSSIBLY_IN_MACRO_MAP_P, LINEMAPS_MAP_INFO)
	(LINEMAPS_MAPS, LINEMAPS_ALLOCATE, LINEMAPS_USED, LINEMAPS_CACHE)
	(LINEMAPS_LAST_MAP, LINEMAPS_LAST_ALLOCATED_MAP)
	(LINEMAPS_ORDINARY_MAPS, LINEMAPS_ORDINARY_ALLOCATED)
	(LINEMAPS_ORDINARY_USED, LINEMAPS_ORDINARY_CACHE)
	(LINEMAPS_LAST_ORDINARY_MAP, LINEMAPS_LAST_ALLOCATED_ORDINARY_MAP)
	(LINEMAPS_MACRO_MAPS, LINEMAPS_MACRO_ALLOCATED)
	(LINEMAPS_MACRO_USED, LINEMAPS_MACRO_CACHE)
	(LINEMAPS_LAST_MACRO_MAP, LINEMAPS_LAST_ALLOCATED_MACRO_MAP)
	(LINEMAPS_MAP_AT, LINEMAPS_ORDINARY_MAP_AT)
	(LINEMAPS_MACRO_MAP_AT): New accessors for ordinary and macro map
	information.
	(linemap_check_ordinary, linemap_assert)
	(linemap_location_before_p): New macros.
	(linemap_position_for_line_and_column)
	(linemap_tracks_macro_expansion_locs_p, linemap_add_macro_token)
	(linemap_macro_expansion_map_p)
	(linemap_macro_map_loc_to_def_point)
	(linemap_macro_map_loc_unwind_once)
	(linemap_macro_map_loc_to_exp_point, linemap_step_out_once)
	(linemap_get_source_line linemap_get_source_column)
	(linemap_map_get_macro_name, linemap_get_file_path)
	(linemap_location_in_system_header_p)
	(linemap_location_from_macro_expansion_p): Declare new functions.
	(SOURCE_LINE, SOURCE_COLUMN, LAST_SOURCE_LINE_LOCATION)
	(LINEMAP_FILE, LINEMAP_LINE, LINEMAP_SYSP): Assert that this
	accessors act on ordinary maps only.
	(INCLUDED_FROM): Return NULL for main files; use the new
	accessors.
	(LINEMAP_POSITION_FOR_COLUMN): Use the new accessors.
	(struct expanded_location): Move here from gcc/input.h
	(linemap_resolve_location, linemap_expand_location)
	(linemap_expand_location_full): Declare new functions.
	* line-map.c: Include cpplib.h, internal.h
	(linemap_enter_macro, linemap_add_macro_token)
	(linemap_get_expansion_line, linemap_get_expansion_filename): New
	functions that are private to libcpp.
	(linemap_assert): New macro.
	(linemap_macro_loc_to_exp_point, linemap_macro_loc_to_exp_point)
	(linemap_macro_loc_unwind, linemap_macro_map_loc_to_def_point)
	(linemap_macro_map_loc_unwind_toward_spelling)
	(linemap_macro_map_loc_to_exp_point)
	(first_map_in_common_1, first_map_in_common): New static
	functions.
	(new_linemap): Define new static functions.  Extracted and
	enhanced from ...
	(linemap_add): ... here.  Use linemap_assert in lieu of abort
	previously.
	(linemap_tracks_macro_expansion_locs_p)
	(linemap_add_macro_token, linemap_macro_expansion_map_p)
	(linemap_check_ordinary, linemap_macro_map_loc_to_exp_point)
	(linemap_macro_map_loc_to_def_point)
	(linemap_macro_map_loc_unwind_once)
	(linemap_step_out_once, linemap_map_get_index)
	(linemap_get_source_line,linemap_get_source_column)
	(linemap_get_file_path, linemap_map_get_macro_name)
	(linemap_location_in_system_header_p)
	(linemap_location_originated_from_system_header_p)
	(linemap_location_from_macro_expansion_p)
	(linemap_tracks_macro_expansion_locs_p)
	(linemap_resolve_location, linemap_expand_location)
	(linemap_expand_location_full)
	(linemap_tracks_macro_expansion_locs_p)
	(linemap_position_for_line_and_column, linemap_compare_locations):
	Define new public functions.
	(linemap_init): Initialize ordinary and macro maps information in
	the map set.
	(linemap_check_files_exited): Use the new accessors.
	(linemap_free): Remove this dead code.
	(linemap_line_start): Assert this uses an ordinary map.  Adjust to
	use the new ordinary map accessors and data structures.  Don't
	overflow past the lowest possible macro token's location.
	(linemap_position_for_column): Assert the ordinary maps of the map
	set are really ordinary.  Use ordinary map accessors.
	(linemap_lookup): Keep the same logic but generalize to allow
	lookup of both ordinary and macro maps.  Do not crash when called
	with an empty line table.
	* directives-only.c (_cpp_preprocess_dir_only): Adjust to use the
	new API of line-map.h.
	* directives.c (start_directive, do_line, do_linemarker)
	(do_linemarker): Likewise.
	* files.c (_cpp_find_file, _cpp_stack_include, open_file_failed)
	(make_cpp_dir, cpp_make_system_header): Likewise.
	* init.c (cpp_read_main_file): Likewise.
	* internal.h (CPP_INCREMENT_LINE): Likewise.
	(linemap_enter_macro, linemap_add_macro_token)
	(linemap_get_expansion_line, linemap_get_expansion_filename): New
	functions private to libcpp.
	* lex.c (_cpp_process_line_notes, _cpp_skip_block_comment)
	(skip_line_comment, skip_whitespace, lex_raw_string)
	(_cpp_lex_direct): Likewise.
	* macro.c (_cpp_builtin_macro_text): Likewise.
	(_cpp_aligned_alloc): Initialize the new name member of the macro.
	* traditional.c (copy_comment, _cpp_scan_out_logical_line):
	Likewise.
	* errors.c (cpp_diagnostic): Adjust to new linemap API.

gcc/ChangeLog
2011-10-15  Tom Tromey  <tromey@redhat>
	    Dodji Seketeli  <dodji@redhat.com>

	* input.h (struct expanded_location): Move to libcpp/line-map.h.
	(LOCATION_COLUMN): New accessor
	(in_system_header_at): Use linemap_location_in_system_header_p.
	* diagnostic.c (diagnostic_report_current_module): Adjust to avoid
	touching the internals of struct line_map.  Use the public API.
	instead.
	(diagnostic_report_diagnostic): Don't use relational operator '<'
	on virtual locations.  Use linemap_location_before_p instead.
	* input.c (expand_location): Adjust to expand to the tokens'
	spelling location when macro location tracking is on.


gcc/ada/ChangeLog
2011-10-15  Tom Tromey  <tromey@redhat.com> 
	    Dodji Seketeli  <dodji@redhat.com>

	* gcc-interface/trans.c (gigi, Sloc_to_locus): Adjust to use the
	new public ordinary map interface.

gcc/c-family/ChangeLog
2011-10-15  Tom Tromey  <tromey@redhat.com>
	    Dodji Seketeli  <dodji@redhat.com>

	* c-ppoutput.c (scan_translation_unit, maybe_print_line)
	(print_line, cb_define, do_line_change): Adjust to avoid touching
	the internals of struct line_map.  Use the public API instead.
	* c-pch.c (c_common_read_pch): Likewise.
	* c-lex.c (fe_file_change): Likewise.

gcc/fortran/ChangeLog
2011-10-15  Tom Tromey  <tromey@redhat.com>
	    Dodji Seketeli  <dodji@redhat.com>

	* cpp.c (print_line, cb_define): Adjust to avoid using internals
	of struct line_map.  Use the public API instead.

gcc/java/ChangeLog
2011-10-15  Tom Tromey  <tromey@redhat.com>
	    Dodji Seketeli  <dodji@redhat.com>

	* jcf-parse.c (set_source_filename): Adjust to the new map API.

 2011-08-24  Joseph Myers  <joseph@codesourcery.com>
 
 	* Make-lang.in (CFLAGS-java/jcf-io.o, CFLAGS-java/jcf-path.o):

gcc/testsuite/ChangeLog

2011-10-15  Tom Tromey  <tromey@redhat.com>
	    Dodji Seketeli  <dodji@redhat.com>

	* gcc.dg/cpp/pragma-diagnostic-1.c: New test.

---
 gcc/ChangeLog                                  |   15 +
 gcc/ada/ChangeLog                              |    6 +
 gcc/ada/gcc-interface/trans.c                  |   10 +-
 gcc/c-family/ChangeLog                         |    9 +
 gcc/c-family/c-lex.c                           |    6 +-
 gcc/c-family/c-ppoutput.c                      |   43 +-
 gcc/diagnostic.c                               |   11 +-
 gcc/fortran/ChangeLog                          |    6 +
 gcc/fortran/cpp.c                              |   22 +-
 gcc/input.c                                    |    9 +-
 gcc/input.h                                    |   18 +-
 gcc/java/ChangeLog                             |    5 +
 gcc/java/jcf-parse.c                           |    2 +-
 gcc/testsuite/ChangeLog                        |    5 +
 gcc/testsuite/gcc.dg/cpp/pragma-diagnostic-1.c |   32 +
 libcpp/ChangeLog                               |  118 +++
 libcpp/directives.c                            |   16 +-
 libcpp/files.c                                 |    5 +-
 libcpp/include/line-map.h                      |  585 ++++++++++++++-
 libcpp/init.c                                  |    4 +-
 libcpp/internal.h                              |   73 ++-
 libcpp/line-map.c                              |  946 ++++++++++++++++++++++--
 libcpp/macro.c                                 |   28 +-
 23 files changed, 1767 insertions(+), 207 deletions(-)
 create mode 100644 gcc/testsuite/gcc.dg/cpp/pragma-diagnostic-1.c

diff --git a/gcc/ada/gcc-interface/trans.c b/gcc/ada/gcc-interface/trans.c
index af7e9af..fefc9c7 100644
--- a/gcc/ada/gcc-interface/trans.c
+++ b/gcc/ada/gcc-interface/trans.c
@@ -289,7 +289,7 @@ gigi (Node_Id gnat_root, int max_gnat_node, int number_name ATTRIBUTE_UNUSED,
 	     (Get_Name_String (file_info_ptr[i].File_Name))));
 
       /* We rely on the order isomorphism between files and line maps.  */
-      gcc_assert ((int) line_table->used == i);
+      gcc_assert ((int) LINEMAPS_ORDINARY_USED (line_table) == i);
 
       /* We create the line map for a source file at once, with a fixed number
 	 of columns chosen to avoid jumping over the next power of 2.  */
@@ -8247,12 +8247,10 @@ Sloc_to_locus (Source_Ptr Sloc, location_t *locus)
       Source_File_Index file = Get_Source_File_Index (Sloc);
       Logical_Line_Number line = Get_Logical_Line_Number (Sloc);
       Column_Number column = Get_Column_Number (Sloc);
-      struct line_map *map = &line_table->maps[file - 1];
+      struct line_map *map = LINEMAPS_ORDINARY_MAP_AT (line_table, file - 1);
 
-      /* Translate the location according to the line-map.h formula.  */
-      *locus = map->start_location
-		+ ((line - map->to_line) << map->column_bits)
-		+ (column & ((1 << map->column_bits) - 1));
+      /* Translate the location.  */
+      *locus = linemap_position_for_line_and_column (map, line, column);
     }
 
   ref_filename
diff --git a/gcc/c-family/c-lex.c b/gcc/c-family/c-lex.c
index e60dcc5..be83b61 100644
--- a/gcc/c-family/c-lex.c
+++ b/gcc/c-family/c-lex.c
@@ -207,7 +207,7 @@ fe_file_change (const struct line_map *new_map)
 	    line = SOURCE_LINE (new_map - 1, included_at);
 
 	  input_location = new_map->start_location;
-	  (*debug_hooks->start_source_file) (line, new_map->to_file);
+	  (*debug_hooks->start_source_file) (line, LINEMAP_FILE (new_map));
 #ifndef NO_IMPLICIT_EXTERN_C
 	  if (c_header_level)
 	    ++c_header_level;
@@ -231,10 +231,10 @@ fe_file_change (const struct line_map *new_map)
 #endif
       input_location = new_map->start_location;
 
-      (*debug_hooks->end_source_file) (new_map->to_line);
+      (*debug_hooks->end_source_file) (LINEMAP_LINE (new_map));
     }
 
-  update_header_times (new_map->to_file);
+  update_header_times (LINEMAP_FILE (new_map));
   input_location = new_map->start_location;
 }
 
diff --git a/gcc/c-family/c-ppoutput.c b/gcc/c-family/c-ppoutput.c
index 16d4f7d..892f1ea 100644
--- a/gcc/c-family/c-ppoutput.c
+++ b/gcc/c-family/c-ppoutput.c
@@ -190,9 +190,7 @@ scan_translation_unit (cpp_reader *pfile)
       /* Subtle logic to output a space if and only if necessary.  */
       if (avoid_paste)
 	{
-	  const struct line_map *map
-	    = linemap_lookup (line_table, loc);
-	  int src_line = SOURCE_LINE (map, loc);
+	  int src_line = LOCATION_LINE (loc);
 
 	  if (print.source == NULL)
 	    print.source = token;
@@ -212,9 +210,7 @@ scan_translation_unit (cpp_reader *pfile)
 	}
       else if (token->flags & PREV_WHITE)
 	{
-	  const struct line_map *map
-	    = linemap_lookup (line_table, loc);
-	  int src_line = SOURCE_LINE (map, loc);
+	  int src_line = LOCATION_LINE (loc);
 
 	  if (src_line != print.src_line
 	      && do_line_adjustments
@@ -304,8 +300,9 @@ scan_translation_unit_trad (cpp_reader *pfile)
 static void
 maybe_print_line (source_location src_loc)
 {
-  const struct line_map *map = linemap_lookup (line_table, src_loc);
-  int src_line = SOURCE_LINE (map, src_loc);
+  int src_line = LOCATION_LINE (src_loc);
+  const char *src_file = LOCATION_FILE (src_loc);
+
   /* End the previous line of text.  */
   if (print.printed)
     {
@@ -317,7 +314,7 @@ maybe_print_line (source_location src_loc)
   if (!flag_no_line_commands
       && src_line >= print.src_line
       && src_line < print.src_line + 8
-      && strcmp (map->to_file, print.src_file) == 0)
+      && strcmp (src_file, print.src_file) == 0)
     {
       while (src_line > print.src_line)
 	{
@@ -341,28 +338,30 @@ print_line (source_location src_loc, const char *special_flags)
 
   if (!flag_no_line_commands)
     {
-      const struct line_map *map = linemap_lookup (line_table, src_loc);
-
-      size_t to_file_len = strlen (map->to_file);
+      const char *file_path = LOCATION_FILE (src_loc);
+      int sysp;
+      size_t to_file_len = strlen (file_path);
       unsigned char *to_file_quoted =
          (unsigned char *) alloca (to_file_len * 4 + 1);
       unsigned char *p;
 
-      print.src_line = SOURCE_LINE (map, src_loc);
-      print.src_file = map->to_file;
+      print.src_line = LOCATION_LINE (src_loc);
+      print.src_file = file_path;
 
       /* cpp_quote_string does not nul-terminate, so we have to do it
 	 ourselves.  */
       p = cpp_quote_string (to_file_quoted,
-			    (const unsigned char *) map->to_file, to_file_len);
+			    (const unsigned char *) file_path,
+			    to_file_len);
       *p = '\0';
       fprintf (print.outf, "# %u \"%s\"%s",
 	       print.src_line == 0 ? 1 : print.src_line,
 	       to_file_quoted, special_flags);
 
-      if (map->sysp == 2)
+      sysp = in_system_header_at (src_loc);
+      if (sysp == 2)
 	fputs (" 3 4", print.outf);
-      else if (map->sysp == 1)
+      else if (sysp == 1)
 	fputs (" 3", print.outf);
 
       putc ('\n', print.outf);
@@ -391,8 +390,7 @@ do_line_change (cpp_reader *pfile, const cpp_token *token,
      ought to care.  Some things do care; the fault lies with them.  */
   if (!CPP_OPTION (pfile, traditional))
     {
-      const struct line_map *map = linemap_lookup (line_table, src_loc);
-      int spaces = SOURCE_COLUMN (map, src_loc) - 2;
+      int spaces = LOCATION_COLUMN (src_loc) - 2;
       print.printed = 1;
 
       while (-- spaces >= 0)
@@ -421,6 +419,8 @@ cb_ident (cpp_reader *pfile ATTRIBUTE_UNUSED, source_location line,
 static void
 cb_define (cpp_reader *pfile, source_location line, cpp_hashnode *node)
 {
+  const struct line_map *map;
+
   maybe_print_line (line);
   fputs ("#define ", print.outf);
 
@@ -432,7 +432,10 @@ cb_define (cpp_reader *pfile, source_location line, cpp_hashnode *node)
     fputs ((const char *) NODE_NAME (node), print.outf);
 
   putc ('\n', print.outf);
-  if (linemap_lookup (line_table, line)->to_line != 0)
+  linemap_resolve_location (line_table, line,
+			    LRK_MACRO_DEFINITION_LOCATION,
+			    &map);
+  if (LINEMAP_LINE (map) != 0)
     print.src_line++;
 }
 
diff --git a/gcc/diagnostic.c b/gcc/diagnostic.c
index d297cdd..b46eb35 100644
--- a/gcc/diagnostic.c
+++ b/gcc/diagnostic.c
@@ -278,18 +278,18 @@ diagnostic_report_current_module (diagnostic_context *context)
 	  if (context->show_column)
 	    pp_verbatim (context->printer,
 			 "In file included from %s:%d:%d",
-			 map->to_file,
+			 LINEMAP_FILE (map),
 			 LAST_SOURCE_LINE (map), LAST_SOURCE_COLUMN (map));
 	  else
 	    pp_verbatim (context->printer,
 			 "In file included from %s:%d",
-			 map->to_file, LAST_SOURCE_LINE (map));
+			 LINEMAP_FILE (map), LAST_SOURCE_LINE (map));
 	  while (! MAIN_FILE_P (map))
 	    {
 	      map = INCLUDED_FROM (line_table, map);
 	      pp_verbatim (context->printer,
 			   ",\n                 from %s:%d",
-			   map->to_file, LAST_SOURCE_LINE (map));
+			   LINEMAP_FILE (map), LAST_SOURCE_LINE (map));
 	    }
 	  pp_verbatim (context->printer, ":");
 	  pp_newline (context->printer);
@@ -459,7 +459,10 @@ diagnostic_report_diagnostic (diagnostic_context *context,
 	  /* FIXME: Stupid search.  Optimize later. */
 	  for (i = context->n_classification_history - 1; i >= 0; i --)
 	    {
-	      if (context->classification_history[i].location <= location)
+	      if (linemap_location_before_p
+		  (line_table,
+		   context->classification_history[i].location,
+		   location))
 		{
 		  if (context->classification_history[i].kind == (int) DK_POP)
 		    {
diff --git a/gcc/fortran/cpp.c b/gcc/fortran/cpp.c
index 9368d89..2f18893 100644
--- a/gcc/fortran/cpp.c
+++ b/gcc/fortran/cpp.c
@@ -818,27 +818,29 @@ print_line (source_location src_loc, const char *special_flags)
 
   if (!gfc_cpp_option.no_line_commands)
     {
-      const struct line_map *map = linemap_lookup (line_table, src_loc);
-
-      size_t to_file_len = strlen (map->to_file);
-      unsigned char *to_file_quoted =
-         (unsigned char *) alloca (to_file_len * 4 + 1);
+      expanded_location loc;
+      size_t to_file_len;
+      unsigned char *to_file_quoted;
       unsigned char *p;
 
-      print.src_line = SOURCE_LINE (map, src_loc);
+      loc = expand_location (src_loc);
+      to_file_len = strlen (loc.file);
+      to_file_quoted = (unsigned char *) alloca (to_file_len * 4 + 1);
+
+      print.src_line = loc.line;
 
       /* cpp_quote_string does not nul-terminate, so we have to do it
 	 ourselves.  */
       p = cpp_quote_string (to_file_quoted,
-			    (const unsigned char *) map->to_file, to_file_len);
+			    (const unsigned char *) loc.file, to_file_len);
       *p = '\0';
       fprintf (print.outf, "# %u \"%s\"%s",
 	       print.src_line == 0 ? 1 : print.src_line,
 	       to_file_quoted, special_flags);
 
-      if (map->sysp == 2)
+      if (loc.sysp == 2)
 	fputs (" 3 4", print.outf);
-      else if (map->sysp == 1)
+      else if (loc.sysp == 1)
 	fputs (" 3", print.outf);
 
       putc ('\n', print.outf);
@@ -935,7 +937,7 @@ cb_define (cpp_reader *pfile ATTRIBUTE_UNUSED, source_location line,
     fputs ((const char *) NODE_NAME (node), print.outf);
 
   putc ('\n', print.outf);
-  if (linemap_lookup (line_table, line)->to_line != 0)
+  if (LOCATION_LINE (line) != 0)
     print.src_line++;
 }
 
diff --git a/gcc/input.c b/gcc/input.c
index e5e051f..83344d7 100644
--- a/gcc/input.c
+++ b/gcc/input.c
@@ -42,12 +42,7 @@ expand_location (source_location loc)
       xloc.sysp = 0;
     }
   else
-    {
-      const struct line_map *map = linemap_lookup (line_table, loc);
-      xloc.file = map->to_file;
-      xloc.line = SOURCE_LINE (map, loc);
-      xloc.column = SOURCE_COLUMN (map, loc);
-      xloc.sysp = map->sysp != 0;
-    };
+    xloc = linemap_expand_location_full (line_table, loc,
+					 LRK_SPELLING_LOCATION);
   return xloc;
 }
diff --git a/gcc/input.h b/gcc/input.h
index 5929064..9fc55f3 100644
--- a/gcc/input.h
+++ b/gcc/input.h
@@ -37,20 +37,6 @@ extern GTY(()) struct line_maps *line_table;
 extern char builtins_location_check[(BUILTINS_LOCATION
 				     < RESERVED_LOCATION_COUNT) ? 1 : -1];
 
-typedef struct
-{
-  /* The name of the source file involved.  */
-  const char *file;
-
-  /* The line-location in the source file.  */
-  int line;
-
-  int column;
-
-  /* In a system header?. */
-  bool sysp;
-} expanded_location;
-
 extern expanded_location expand_location (source_location);
 
 /* Historically GCC used location_t, while cpp used source_location.
@@ -61,10 +47,12 @@ extern location_t input_location;
 
 #define LOCATION_FILE(LOC) ((expand_location (LOC)).file)
 #define LOCATION_LINE(LOC) ((expand_location (LOC)).line)
+#define LOCATION_COLUMN(LOC)((expand_location (LOC)).column)
 
 #define input_line LOCATION_LINE (input_location)
 #define input_filename LOCATION_FILE (input_location)
-#define in_system_header_at(LOC) ((expand_location (LOC)).sysp != 0)
+#define in_system_header_at(LOC) \
+  ((linemap_location_in_system_header_p (line_table, LOC)))
 #define in_system_header (in_system_header_at (input_location))
 
 #endif
diff --git a/gcc/java/jcf-parse.c b/gcc/java/jcf-parse.c
index 37cea28..04c04f5 100644
--- a/gcc/java/jcf-parse.c
+++ b/gcc/java/jcf-parse.c
@@ -355,7 +355,7 @@ set_source_filename (JCF *jcf, int index)
     }
       
   sfname = find_sourcefile (sfname);
-  line_table->maps[line_table->used-1].to_file = sfname;
+  ORDINARY_MAP_FILE_NAME (LINEMAPS_LAST_ORDINARY_MAP (line_table)) = sfname;
   if (current_class == main_class) main_input_filename = sfname;
 }
 
diff --git a/gcc/testsuite/gcc.dg/cpp/pragma-diagnostic-1.c b/gcc/testsuite/gcc.dg/cpp/pragma-diagnostic-1.c
new file mode 100644
index 0000000..3a2f9da
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/cpp/pragma-diagnostic-1.c
@@ -0,0 +1,32 @@
+/*
+  { dg-options "-Wuninitialized" }
+  { dg-do compile }
+*/
+
+void f (unsigned);
+
+#define CODE_WITH_WARNING \
+  int a;		  \
+  f (a)
+
+#pragma GCC diagnostic ignored "-Wuninitialized"
+
+void
+g (void)
+{
+  CODE_WITH_WARNING;
+}
+
+#pragma GCC diagnostic push
+
+#pragma GCC diagnostic error "-Wuninitialized"
+
+void
+h (void)
+{
+  CODE_WITH_WARNING;		/* { dg-error "uninitialized" } */
+}
+
+/*
+  { dg-message "some warnings being treated as errors" "" {target *-*-*} 0 }
+*/
diff --git a/libcpp/directives.c b/libcpp/directives.c
index 83d4a0e..a62ddeb 100644
--- a/libcpp/directives.c
+++ b/libcpp/directives.c
@@ -884,14 +884,14 @@ static void
 do_line (cpp_reader *pfile)
 {
   const struct line_maps *line_table = pfile->line_table;
-  const struct line_map *map = &line_table->maps[line_table->used - 1];
+  const struct line_map *map = LINEMAPS_LAST_ORDINARY_MAP (line_table);
 
   /* skip_rest_of_line() may cause line table to be realloc()ed so note down
      sysp right now.  */
 
-  unsigned char map_sysp = map->sysp;
+  unsigned char map_sysp = ORDINARY_MAP_IN_SYSTEM_HEADER_P (map);
   const cpp_token *token;
-  const char *new_file = map->to_file;
+  const char *new_file = ORDINARY_MAP_FILE_NAME (map);
   linenum_type new_lineno;
 
   /* C99 raised the minimum limit on #line numbers.  */
@@ -946,11 +946,11 @@ static void
 do_linemarker (cpp_reader *pfile)
 {
   const struct line_maps *line_table = pfile->line_table;
-  const struct line_map *map = &line_table->maps[line_table->used - 1];
+  const struct line_map *map = LINEMAPS_LAST_ORDINARY_MAP (line_table);
   const cpp_token *token;
-  const char *new_file = map->to_file;
+  const char *new_file = ORDINARY_MAP_FILE_NAME (map);
   linenum_type new_lineno;
-  unsigned int new_sysp = map->sysp;
+  unsigned int new_sysp = ORDINARY_MAP_IN_SYSTEM_HEADER_P (map);
   enum lc_reason reason = LC_RENAME_VERBATIM;
   int flag;
   bool wrapped;
@@ -1038,7 +1038,9 @@ _cpp_do_file_change (cpp_reader *pfile, enum lc_reason reason,
   const struct line_map *map = linemap_add (pfile->line_table, reason, sysp,
 					    to_file, file_line);
   if (map != NULL)
-    linemap_line_start (pfile->line_table, map->to_line, 127);
+    linemap_line_start (pfile->line_table,
+			ORDINARY_MAP_STARTING_LINE_NUMBER (map),
+			127);
 
   if (pfile->cb.file_change)
     pfile->cb.file_change (pfile, map);
diff --git a/libcpp/files.c b/libcpp/files.c
index d2c6b8b..fad8b75 100644
--- a/libcpp/files.c
+++ b/libcpp/files.c
@@ -1220,13 +1220,12 @@ cpp_make_system_header (cpp_reader *pfile, int syshdr, int externc)
 {
   int flags = 0;
   const struct line_maps *line_table = pfile->line_table;
-  const struct line_map *map = &line_table->maps[line_table->used-1];
-
+  const struct line_map *map = LINEMAPS_LAST_ORDINARY_MAP (line_table);
   /* 1 = system header, 2 = system header to be treated as C.  */
   if (syshdr)
     flags = 1 + (externc != 0);
   pfile->buffer->sysp = flags;
-  _cpp_do_file_change (pfile, LC_RENAME, map->to_file,
+  _cpp_do_file_change (pfile, LC_RENAME, ORDINARY_MAP_FILE_NAME (map),
 		       SOURCE_LINE (map, pfile->line_table->highest_line), flags);
 }
 
diff --git a/libcpp/include/line-map.h b/libcpp/include/line-map.h
index 3c84035..d3c52b2 100644
--- a/libcpp/include/line-map.h
+++ b/libcpp/include/line-map.h
@@ -27,13 +27,22 @@ along with this program; see the file COPYING3.  If not see
 #define GTY(x) /* nothing */
 #endif
 
-/* Reason for adding a line change with add_line_map ().  LC_ENTER is
+/* Reason for creating a new line map with linemap_add.  LC_ENTER is
    when including a new file, e.g. a #include directive in C.
    LC_LEAVE is when reaching a file's end.  LC_RENAME is when a file
    name or line number changes for neither of the above reasons
    (e.g. a #line directive in C); LC_RENAME_VERBATIM is like LC_RENAME
-   but a filename of "" is not specially interpreted as standard input.  */
-enum lc_reason {LC_ENTER = 0, LC_LEAVE, LC_RENAME, LC_RENAME_VERBATIM};
+   but a filename of "" is not specially interpreted as standard
+   input. LC_ENTER_MACRO is when a macro expansion is about to start.  */
+enum lc_reason
+{
+  LC_ENTER = 0,
+  LC_LEAVE,
+  LC_RENAME,
+  LC_RENAME_VERBATIM,
+  LC_ENTER_MACRO
+  /* FIXME: add support for stringize and paste.  */
+};
 
 /* The type of line numbers.  */
 typedef unsigned int linenum_type;
@@ -44,37 +53,214 @@ typedef unsigned int source_location;
 /* Memory allocation function typedef.  Works like xrealloc.  */
 typedef void *(*line_map_realloc) (void *, size_t);
 
-/* Physical source file TO_FILE at line TO_LINE at column 0 is represented
+/* An ordinary line map encodes physical source locations. Those
+   physical source locations are called "spelling locations".
+   
+   Physical source file TO_FILE at line TO_LINE at column 0 is represented
    by the logical START_LOCATION.  TO_LINE+L at column C is represented by
    START_LOCATION+(L*(1<<column_bits))+C, as long as C<(1<<column_bits),
    and the result_location is less than the next line_map's start_location.
    (The top line is line 1 and the leftmost column is column 1; line/column 0
    means "entire file/line" or "unknown line/column" or "not applicable".)
-   INCLUDED_FROM is an index into the set that gives the line mapping
-   at whose end the current one was included.  File(s) at the bottom
-   of the include stack have this set to -1.  REASON is the reason for
-   creation of this line map, SYSP is one for a system header, two for
-   a C system header file that therefore needs to be extern "C"
-   protected in C++, and zero otherwise.  */
-struct GTY(()) line_map {
+
+   The highest possible source location is MAX_SOURCE_LOCATION.  */
+struct GTY(()) line_map_ordinary {
   const char *to_file;
   linenum_type to_line;
-  source_location start_location;
+
+  /* An index into the set that gives the line mapping at whose end
+     the current one was included.  File(s) at the bottom of the
+     include stack have this set to -1.  */
   int included_from;
-  ENUM_BITFIELD (lc_reason) reason : CHAR_BIT;
-  /* The sysp field isn't really needed now that it's in cpp_buffer.  */
+
+  /* SYSP is one for a system header, two for a C system header file
+     that therefore needs to be extern "C" protected in C++, and zero
+     otherwise.  This field isn't really needed now that it's in
+     cpp_buffer.  */
   unsigned char sysp;
+
   /* Number of the low-order source_location bits used for a column number.  */
   unsigned int column_bits : 8;
 };
 
-/* A set of chronological line_map structures.  */
-struct GTY(()) line_maps {
+/* This is the highest possible source location encoded within an
+   ordinary or macro map.  */
+#define MAX_SOURCE_LOCATION 0xFFFFFFFF
+
+struct cpp_hashnode;
+
+/* A macro line map encodes location of tokens coming from a macro
+   expansion.
+   
+   Please note that this struct line_map_macro is a field of struct
+   line_map below, go read the comments of struct line_map below and
+   then come back here.
+   
+   The offset from START_LOCATION is used to index into
+   MACRO_LOCATIONS; this holds the original location of the token.  */
+struct GTY(()) line_map_macro {
+  /* The cpp macro which expansion gave birth to this macro map.  */
+  struct cpp_hashnode * GTY ((nested_ptr (union tree_node,
+				   "%h ? CPP_HASHNODE (GCC_IDENT_TO_HT_IDENT (%h)) : NULL",
+				   "%h ? HT_IDENT_TO_GCC_IDENT (HT_NODE (%h)) : NULL")))
+    macro;
+
+  /* The number of tokens inside the replacement-list of MACRO.  */
+  unsigned int n_tokens;
+
+  /* This array of location is actually an array of pairs of
+     locations. The elements inside it thus look like:
+
+           x0,y0, x1,y1, x2,y2, ...., xn,yn.
+
+     where n == n_tokens;
+
+     Remember that these xI,yI are collected when libcpp is about to
+     expand a given macro.
+
+     yI is the location in the macro definition, either of the token
+     itself or of a macro parameter that it replaces.
+
+     Imagine this:
+
+	#define PLUS(A, B) A + B  <--- #1
+
+	int a = PLUS (1,2); <--- #2
+
+     There is a macro map for the expansion of PLUS in #2.  PLUS is
+     expanded into its expansion-list.  The expansion-list is the
+     replacement-list of PLUS where the macro parameters are replaced
+     with their arguments.  So the replacement-list of PLUS is made of
+     the tokens:
+
+        A, +, B
+
+     and the expansion-list is made of the tokens:
+
+        1, +, 2
+
+     Let's consider the case of token "+".  Its y1 [yI for I == 1] is
+     its spelling location in #1.
+
+     y0 (thus for token "1") is the spelling location of A in #1.
+
+     And y2 (of token "2") is the spelling location of B in #1.
+
+     When the token is /not/ an argument for a macro, xI is the same
+     location as yI.  Otherwise, xI is the location of the token
+     outside this macro expansion.  If this macro was expanded from
+     another macro expansion, xI is a virtual location representing
+     the token in that macro expansion; otherwise, it is the spelling
+     location of the token.
+
+     Note that a virtual location is a location returned by
+     linemap_add_macro_token.  It encodes the relevant locations (x,y
+     pairs) of that token accross the macro expansions from which it
+     (the token) might come from.
+
+     In the example above x1 (for token "+") is going to be the same
+     as y1.  x0 is the spelling location for the argument token "1",
+     and x2 is the spelling location for the argument token "2".  */
+  source_location * GTY((length ("2 * %h.n_tokens"))) macro_locations;
+
+  /* This is the location of the expansion point of the current macro
+     map.  It's the location of the macro name.  That location is held
+     by the map that was current right before the current one. It
+     could have been either a macro or an ordinary map, depending on
+     if we are in a nested expansion context not.  */
+  source_location expansion;
+};
+
+/* A line_map encodes a sequence of locations.
+   There are two kinds of maps. Ordinary maps and macro expansion
+   maps, a.k.a macro maps.
+
+   A macro map encodes source locations of tokens that are part of a
+   macro replacement-list, at a macro expansion point. E.g, in:
+
+            #define PLUS(A,B) A + B
+
+   No macro map is going to be created there, because we are not at a
+   macro expansion point. We are at a macro /definition/ point. So the
+   locations of the tokens of the macro replacement-list (i.e, A + B)
+   will be locations in an ordinary map, not a macro map.
+
+   On the other hand, if we later do:
+
+        int a = PLUS (1,2);
+
+   The invocation of PLUS here is a macro expansion. So we are at a
+   macro expansion point. The preprocessor expands PLUS (1,2) and
+   replaces it with the tokens of its replacement-list: 1 + 2. A macro
+   map is going to be created to hold (or rather to map, haha ...) the
+   locations of the tokens 1, + and 2. The macro map also records the
+   location of the expansion point of PLUS. That location is mapped in
+   the map that is active right before the location of the invocation
+   of PLUS.  */
+struct GTY(()) line_map {
+  source_location start_location;
+
+  /* The reason for creation of this line map.  */
+  ENUM_BITFIELD (lc_reason) reason : CHAR_BIT;
+
+  union map_u {
+    struct line_map_ordinary GTY((tag ("0"))) ordinary;
+    struct line_map_macro GTY((tag ("1"))) macro;
+  } GTY((desc ("%1.reason == LC_ENTER_MACRO"))) d;
+};
+
+#define MAP_START_LOCATION(MAP) (MAP)->start_location
+
+#define ORDINARY_MAP_FILE_NAME(MAP) \
+  linemap_check_ordinary (MAP)->d.ordinary.to_file
+
+#define ORDINARY_MAP_STARTING_LINE_NUMBER(MAP) \
+  linemap_check_ordinary (MAP)->d.ordinary.to_line
+
+#define ORDINARY_MAP_INCLUDER_FILE_INDEX(MAP) \
+  linemap_check_ordinary (MAP)->d.ordinary.included_from
+
+#define ORDINARY_MAP_IN_SYSTEM_HEADER_P(MAP) \
+  linemap_check_ordinary (MAP)->d.ordinary.sysp
+
+#define ORDINARY_MAP_NUMBER_OF_COLUMN_BITS(MAP) \
+  linemap_check_ordinary (MAP)->d.ordinary.column_bits
+
+#define MACRO_MAP_MACRO(MAP) (MAP)->d.macro.macro
+
+#define MACRO_MAP_NUM_MACRO_TOKENS(MAP) (MAP)->d.macro.n_tokens
+
+#define MACRO_MAP_LOCATIONS(MAP) (MAP)->d.macro.macro_locations
+
+#define MACRO_MAP_EXPANSION_POINT_LOCATION(MAP) (MAP)->d.macro.expansion
+
+/* The abstraction of a set of location maps. There can be several
+   types of location maps. This abstraction contains the attributes
+   that are independent from the type of the map.  */
+struct GTY(()) maps_info {
+  /* This array contains the different line maps.
+     A line map is created for the following events:
+       - when a new preprocessing unit start. 
+       - when a preprocessing unit ends.
+       - when a macro expansion occurs.  */
   struct line_map * GTY ((length ("%h.used"))) maps;
+
+  /* The total number of allocated maps.  */
   unsigned int allocated;
+
+  /* The number of elements used in maps. This number is smaller
+     or equal to ALLOCATED.  */
   unsigned int used;
 
   unsigned int cache;
+};
+
+/* A set of chronological line_map structures.  */
+struct GTY(()) line_maps {
+  
+  struct maps_info info_ordinary;
+
+  struct maps_info info_macro;
 
   /* Depth of the include stack, including the current file.  */
   unsigned int depth;
@@ -97,12 +283,126 @@ struct GTY(()) line_maps {
   line_map_realloc reallocator;
 };
 
+/* Returns the pointer to the memory region where information about
+   maps are stored in the line table SET. MACRO_MAP_P is a flag
+   telling if we want macro or ordinary maps.  */
+#define LINEMAPS_MAP_INFO(SET, MACRO_MAP_P)				\
+  ((MACRO_MAP_P)							\
+   ? &((SET)->info_macro)						\
+   : &((SET)->info_ordinary))
+
+/* Returns the pointer to the memory region where maps are stored in
+   the line table SET. MAP_KIND shall be TRUE if we are interested in
+   macro maps false otherwise.  */
+#define LINEMAPS_MAPS(SET, MAP_KIND) \
+  (LINEMAPS_MAP_INFO (SET, MAP_KIND))->maps
+
+/* Returns the number of allocated maps so far. MAP_KIND shall be TRUE
+   if we are interested in macro maps, FALSE otherwise.  */
+#define LINEMAPS_ALLOCATED(SET, MAP_KIND) \
+  (LINEMAPS_MAP_INFO (SET, MAP_KIND))->allocated
+
+/* Returns the number of used maps so far. MAP_KIND shall be TRUE if
+   we are interested in macro maps, FALSE otherwise.*/
+#define LINEMAPS_USED(SET, MAP_KIND) \
+  (LINEMAPS_MAP_INFO (SET, MAP_KIND))->used
+
+/* Returns the index of the last map that was looked up with
+   linemap_lookup. MAP_KIND shall be TRUE if we are interested in
+   macro maps, FALSE otherwise.  */
+#define LINEMAPS_CACHE(SET, MAP_KIND) \
+  (LINEMAPS_MAP_INFO (SET, MAP_KIND))->cache
+
+/* Return the map at a given index.  */
+#define LINEMAPS_MAP_AT(SET, MAP_KIND, INDEX)	\
+  (&((LINEMAPS_MAPS (SET, MAP_KIND))[(INDEX)]))
+
+/* Returns the last map used in the line table SET. MAP_KIND
+   shall be TRUE if we are interested in macro maps, FALSE
+   otherwise.*/
+#define LINEMAPS_LAST_MAP(SET, MAP_KIND) \
+  LINEMAPS_MAP_AT (SET, MAP_KIND, (LINEMAPS_USED (SET, MAP_KIND) - 1))
+
+/* Returns the last map that was allocated in the line table SET.
+   MAP_KIND shall be TRUE if we are interested in macro maps, FALSE
+   otherwise.*/
+#define LINEMAPS_LAST_ALLOCATED_MAP(SET, MAP_KIND) \
+  LINEMAPS_MAP_AT (SET, MAP_KIND, LINEMAPS_ALLOCATED (SET, MAP_KIND) - 1)
+
+/* Returns a pointer to the memory region where ordinary maps are
+   allocated in the line table SET.  */
+#define LINEMAPS_ORDINARY_MAPS(SET) \
+  LINEMAPS_MAPS (SET, false)
+
+/* Returns the INDEXth ordinary map.  */
+#define LINEMAPS_ORDINARY_MAP_AT(SET, INDEX)	\
+  LINEMAPS_MAP_AT (SET, false, INDEX)
+
+/* Return the number of ordinary maps allocated in the line table
+   SET.  */
+#define LINEMAPS_ORDINARY_ALLOCATED(SET) \
+  LINEMAPS_ALLOCATED(SET, false)
+
+/* Return the number of ordinary maps used in the line table SET.  */
+#define LINEMAPS_ORDINARY_USED(SET) \
+  LINEMAPS_USED(SET, false)
+
+/* Return the index of the last ordinary map that was looked up with
+   linemap_lookup.  */
+#define LINEMAPS_ORDINARY_CACHE(SET) \
+  LINEMAPS_CACHE(SET, false)
+
+/* Returns a pointer to the last ordinary map used in the line table
+   SET.  */
+#define LINEMAPS_LAST_ORDINARY_MAP(SET) \
+  LINEMAPS_LAST_MAP(SET, false)
+
+/* Returns a pointer to the last ordinary map allocated the line table
+   SET.  */
+#define LINEMAPS_LAST_ALLOCATED_ORDINARY_MAP(SET) \
+  LINEMAPS_LAST_ALLOCATED_MAP(SET, false)
+
+/* Returns a pointer to the begining of the region where macro maps
+   are allcoated.  */
+#define LINEMAPS_MACRO_MAPS(SET) \
+  LINEMAPS_MAPS(SET, true)
+
+/* Returns the INDEXth macro map.  */
+#define LINEMAPS_MACRO_MAP_AT(SET, INDEX)	\
+  LINEMAPS_MAP_AT (SET, true, INDEX)
+
+/* Returns the number of macro maps that were allocated in the line
+   table SET.  */
+#define LINEMAPS_MACRO_ALLOCATED(SET) \
+  LINEMAPS_ALLOCATED(SET, true)
+
+/* Returns the number of macro maps used in the line table SET.  */
+#define LINEMAPS_MACRO_USED(SET) \
+  LINEMAPS_USED(SET, true)
+
+/* Returns the index of the last macro map looked up with
+   linemap_lookup.  */
+#define LINEMAPS_MACRO_CACHE(SET) \
+  LINEMAPS_CACHE(SET, true)
+
+/* Returns the lowest location [of a token resulting from macro
+   expansion] encoded in this line table.  */
+#define LINEMAPS_MACRO_LOWEST_LOCATION(SET)			\
+  (LINEMAPS_MACRO_USED (set)					\
+   ? MAP_START_LOCATION (LINEMAPS_LAST_MACRO_MAP (set))		\
+   : MAX_SOURCE_LOCATION)
+
+/* Returns the last macro map used in the line table SET.  */
+#define LINEMAPS_LAST_MACRO_MAP(SET) \
+  LINEMAPS_LAST_MAP (SET, true)
+
+/* Returns the last macro map allocated in the line table SET.  */
+#define LINEMAPS_LAST_ALLOCATED_MACRO_MAP(SET) \
+  LINEMAPS_LAST_ALLOCATED_MAP (SET, true)
+
 /* Initialize a line map set.  */
 extern void linemap_init (struct line_maps *);
 
-/* Free a line map set.  */
-extern void linemap_free (struct line_maps *);
-
 /* Check for and warn about line_maps entered but not exited.  */
 
 extern void linemap_check_files_exited (struct line_maps *);
@@ -117,10 +417,12 @@ extern source_location linemap_line_start
 (struct line_maps *set, linenum_type to_line,  unsigned int max_column_hint);
 
 /* Add a mapping of logical source line to physical source file and
-   line number.
+   line number. This function creates an "ordinary map", which is a
+   map that records locations of tokens that are not part of macro
+   replacement-lists present at a macro expansion point.
 
    The text pointed to by TO_FILE must have a lifetime
-   at least as long as the final call to lookup_line ().  An empty
+   at least as long as the lifetime of SET.  An empty
    TO_FILE means standard input.  If reason is LC_LEAVE, and
    TO_FILE is NULL, then TO_FILE, TO_LINE and SYSP are given their
    natural values considering the file we are returning to.
@@ -131,41 +433,246 @@ extern const struct line_map *linemap_add
   (struct line_maps *, enum lc_reason, unsigned int sysp,
    const char *to_file, linenum_type to_line);
 
-/* Given a logical line, returns the map from which the corresponding
-   (source file, line) pair can be deduced.  */
+/* Given a logical source location, returns the map which the
+   corresponding (source file, line, column) triplet can be deduced
+   from. Since the set is built chronologically, the logical lines are
+   monotonic increasing, and so the list is sorted and we can use a
+   binary search. If no line map have been allocated yet, this
+   function returns NULL.  */
 extern const struct line_map *linemap_lookup
   (struct line_maps *, source_location);
 
+/* Returns TRUE if the line table set tracks token locations accross
+   macro expansion, FALSE otherwise.  */
+bool linemap_tracks_macro_expansion_locs_p (struct line_maps *);
+
+/* Return TRUE if MAP encodes locations coming from a macro
+   replacement-list at macro expansion point.  */
+bool linemap_macro_expansion_map_p (const struct line_map *);
+
+/* Return the name of the macro associated to MACRO_MAP.  */
+const char* linemap_map_get_macro_name (const struct line_map*);
+
+/* Return a positive value if LOCATION is the locus of a token that is
+   located in a system header, O otherwise. It returns 1 if LOCATION
+   is the locus of a token that is located in a system header, and 2
+   if LOCATION is the locus of a token located in a C system header
+   that therefore needs to be extern "C" protected in C++.
+
+   Note that this function returns 1 if LOCATION belongs to a token
+   that is part of a macro replacement-list defined in a system
+   header, but expanded in a non-system file.  */
+int linemap_location_in_system_header_p (struct line_maps *,
+					 source_location);
+
+/* Return TRUE if LOCATION is a source code location of a token coming
+   from a macro replacement-list at a macro expansion point, FALSE
+   otherwise.  */
+bool linemap_location_from_macro_expansion_p (struct line_maps *,
+					      source_location);
+
 /* source_location values from 0 to RESERVED_LOCATION_COUNT-1 will
    be reserved for libcpp user as special values, no token from libcpp
    will contain any of those locations.  */
 #define RESERVED_LOCATION_COUNT	2
 
 /* Converts a map and a source_location to source line.  */
-#define SOURCE_LINE(MAP, LOC) \
-  ((((LOC) - (MAP)->start_location) >> (MAP)->column_bits) + (MAP)->to_line)
-
-#define SOURCE_COLUMN(MAP, LOC) \
-  (((LOC) - (MAP)->start_location) & ((1 << (MAP)->column_bits) - 1))
-
-/* Returns the last source line within a map.  This is the (last) line
-   of the #include, or other directive, that caused a map change.  */
+#define SOURCE_LINE(MAP, LOC)						\
+  (((((LOC) - linemap_check_ordinary (MAP)->start_location)		\
+     >> (MAP)->d.ordinary.column_bits) + (MAP)->d.ordinary.to_line))
+
+/* Convert a map and source_location to source column number.  */
+#define SOURCE_COLUMN(MAP, LOC)						\
+  ((((LOC) - linemap_check_ordinary (MAP)->start_location)		\
+    & ((1 << (MAP)->d.ordinary.column_bits) - 1)))
+
+/* Returns the last source line number within an ordinary map.  This
+   is the (last) line of the #include, or other directive, that caused
+   a map change.  */
 #define LAST_SOURCE_LINE(MAP) \
   SOURCE_LINE (MAP, LAST_SOURCE_LINE_LOCATION (MAP))
+
+/* Return the last column number within an ordinary map.  */
 #define LAST_SOURCE_COLUMN(MAP) \
   SOURCE_COLUMN (MAP, LAST_SOURCE_LINE_LOCATION (MAP))
-#define LAST_SOURCE_LINE_LOCATION(MAP) \
-  ((((MAP)[1].start_location - 1 - (MAP)->start_location) \
-    & ~((1 << (MAP)->column_bits) - 1))			  \
-   + (MAP)->start_location)
 
-/* Returns the map a given map was included from.  */
-#define INCLUDED_FROM(SET, MAP) (&(SET)->maps[(MAP)->included_from])
+/* Return the location of the last source line within an ordinary
+   map.  */
+#define LAST_SOURCE_LINE_LOCATION(MAP)					\
+  ((((linemap_check_ordinary (MAP)[1].start_location - 1		\
+      - (MAP)->start_location)						\
+     & ~((1 << (MAP)->d.ordinary.column_bits) - 1))			\
+    + (MAP)->start_location))
+
+/* Returns the map a given map was included from, or NULL if the map
+   belongs to the main file, i.e, a file that wasn't included by
+   another one.  */
+#define INCLUDED_FROM(SET, MAP)						\
+  ((linemap_check_ordinary (MAP)->d.ordinary.included_from == -1)	\
+   ? NULL								\
+   : (&LINEMAPS_ORDINARY_MAPS (SET)[(MAP)->d.ordinary.included_from]))
 
 /* Nonzero if the map is at the bottom of the include stack.  */
-#define MAIN_FILE_P(MAP) ((MAP)->included_from < 0)
+#define MAIN_FILE_P(MAP)						\
+  ((linemap_check_ordinary (MAP)->d.ordinary.included_from < 0))
+
+#if defined ENABLE_CHECKING && (GCC_VERSION >= 2007)
+
+/* Assertion macro to be used in line-map code.  */
+#define linemap_assert(EXPR)			\
+  do {						\
+    if (! (EXPR))				\
+      abort ();					\
+  } while (0)
+
+/* Assert that MAP encodes locations of tokens that are not part of
+   the replacement-list of a macro expansion.  */
+#define linemap_check_ordinary(LINE_MAP) __extension__		\
+  ({linemap_assert (!linemap_macro_expansion_map_p (LINE_MAP)); \
+    (LINE_MAP);})
+#else
+#define linemap_assert(EXPR)
+#define linemap_check_ordinary(LINE_MAP) (LINE_MAP)
+#endif
 
+/* Encode and return a source_location from a column number. The
+   source line considered is the last source line used to call
+   linemap_line_start, i.e, the last source line which a location was
+   encoded from.  */
 extern source_location
-linemap_position_for_column (struct line_maps *set, unsigned int to_column);
+linemap_position_for_column (struct line_maps *, unsigned int);
+
+/* Encode and return a source location from a given line and
+   column.  */
+source_location linemap_position_for_line_and_column (struct line_map *,
+						      linenum_type,
+						      unsigned int);
+/* Return the file this map is for.  */
+#define LINEMAP_FILE(MAP)					\
+  (linemap_check_ordinary (MAP)->d.ordinary.to_file)
+
+/* Return the line number this map started encoding location from.  */
+#define LINEMAP_LINE(MAP)					\
+  (linemap_check_ordinary (MAP)->d.ordinary.to_line)
+
+/* Return a positive value if map encodes locations from a system
+   header, 0 otherwise. Returns 1 if MAP encodes locations in a
+   system header and 2 if it encodes locations in a C system header
+   that therefore needs to be extern "C" protected in C++.  */
+#define LINEMAP_SYSP(MAP)					\
+  (linemap_check_ordinary (MAP)->d.ordinary.sysp)
+
+/* Return a positive value if PRE denotes the location of a token that
+   comes before the token of POST, 0 if PRE denotes the location of
+   the same token as the token for POST, and a negative value
+   otherwise.  */
+int linemap_compare_locations (struct line_maps *set,
+			       source_location   pre,
+			       source_location   post);
+
+/* Return TRUE if LOC_A denotes the location a token that comes
+   topogically before the token denoted by location LOC_B, or if they
+   are equal.  */
+#define linemap_location_before_p(SET, LOC_A, LOC_B)	\
+  (linemap_compare_locations ((SET), (LOC_A), (LOC_B)) >= 0)
+
+typedef struct
+{
+  /* The name of the source file involved.  */
+  const char *file;
+
+  /* The line-location in the source file.  */
+  int line;
+
+  int column;
+
+  /* In a system header?. */
+  bool sysp;
+} expanded_location;
+
+/* This is enum is used by the function linemap_resolve_location
+   below.  The meaning of the values is explained in the comment of
+   that function.  */
+enum location_resolution_kind
+{
+  LRK_MACRO_EXPANSION_POINT,
+  LRK_SPELLING_LOCATION,
+  LRK_MACRO_DEFINITION_LOCATION
+};
+
+/* Resolve a virtual location into either a spelling location, an
+   expansion point location or a token argument replacement point
+   location.  Return the map that encodes the virtual location as well
+   as the resolved location.
+
+   If LOC is *NOT* the location of a token resulting from the
+   expansion of a macro, then the parameter LRK (which stands for
+   Location Resolution Kind) is ignored and the resulting location
+   just equals the one given in argument.
+
+   Now if LOC *IS* the location of a token resulting from the
+   expansion of a macro, this is what happens.
+
+   * If LRK is set to LRK_MACRO_EXPANSION_POINT
+   -------------------------------
+
+   The virtual location is resolved to the first macro expansion point
+   that led to this macro expansion.
+
+   * If LRK is set to LRK_SPELLING_LOCATION
+   -------------------------------------
+
+   The virtual location is resolved to the locus where the token has
+   been spelled in the source.   This can follow through all the macro
+   expansions that led to the token.
+
+   * If LRK is set to LRK_MACRO_DEFINITION_LOCATION
+   --------------------------------------
+
+   The virtual location is resolved to the locus of the token in the
+   context of the macro definition.
+
+   If LOC is the locus of a token that is an argument of a
+   function-like macro [replacing a parameter in the replacement list
+   of the macro] the virtual location is resolved to the locus of the
+   parameter that is replaced, in the context of the definition of the
+   macro.
+
+   If LOC is the locus of a token that is not an argument of a
+   function-like macro, then the function behaves as if LRK was set to
+   LRK_SPELLING_LOCATION.
+
+   If LOC_MAP is not NULL, *LOC_MAP is set to the map encoding the
+   returned location.  */
+
+source_location linemap_resolve_location (struct line_maps *,
+					  source_location loc,
+					  enum location_resolution_kind lrk,
+					  const struct line_map **loc_map);
+
+/* Suppose that LOC is the virtual location of a token coming from the
+   expansion of a macro M.  This function then steps up to get the
+   location L of the point where M got expanded.  If L is a spelling
+   location inside a macro expansion M', then this function returns
+   the point where M' was expanded.  LOC_MAP is an output parameter.
+   When non-NULL, *LOC_MAP is set the the map of the returned
+   location.  */
+source_location linemap_unwind_toward_expansion (struct line_maps *,
+						 source_location loc,
+						 const struct line_map **loc_map);
+
+/* Expand source code location LOC and return a user readable source
+   code location.  LOC must be a spelling (non-virtual) location.  */
+
+expanded_location linemap_expand_location (const struct line_map *,
+					   source_location loc);
+
+/* Expand source code location LOC and return a user readable source
+   code location.  LOC can be a virtual location.  The LRK parameter
+   is the same as for linemap_resolve_location.  */
+
+expanded_location linemap_expand_location_full (struct line_maps *,
+						source_location loc,
+						enum location_resolution_kind lrk);
 
 #endif /* !LIBCPP_LINE_MAP_H  */
diff --git a/libcpp/init.c b/libcpp/init.c
index c5c5325..6303868 100644
--- a/libcpp/init.c
+++ b/libcpp/init.c
@@ -586,7 +586,9 @@ cpp_read_main_file (cpp_reader *pfile, const char *fname)
   if (CPP_OPTION (pfile, preprocessed))
     {
       read_original_filename (pfile);
-      fname = pfile->line_table->maps[pfile->line_table->used-1].to_file;
+      fname =
+	ORDINARY_MAP_FILE_NAME
+	((LINEMAPS_LAST_ORDINARY_MAP (pfile->line_table)));
     }
   return fname;
 }
diff --git a/libcpp/internal.h b/libcpp/internal.h
index 6c423f0..65bfa1d 100644
--- a/libcpp/internal.h
+++ b/libcpp/internal.h
@@ -67,7 +67,8 @@ struct cset_converter
 
 #define CPP_INCREMENT_LINE(PFILE, COLS_HINT) do { \
     const struct line_maps *line_table = PFILE->line_table; \
-    const struct line_map *map = &line_table->maps[line_table->used-1]; \
+    const struct line_map *map = \
+      LINEMAPS_LAST_ORDINARY_MAP (line_table); \
     linenum_type line = SOURCE_LINE (map, line_table->highest_line); \
     linemap_line_start (PFILE->line_table, line + 1, COLS_HINT); \
   } while (0)
@@ -739,6 +740,76 @@ ufputs (const unsigned char *s, FILE *f)
   return fputs ((const char *)s, f);
 }
 
+  /* In line-map.c.  */
+
+/* Create a macro map.  A macro map encodes source locations of tokens
+   that are part of a macro replacement-list, at a macro expansion
+   point. See the extensive comments of struct line_map and struct
+   line_map_macro, in line-map.h.
+
+   This map shall be created when the macro is expanded. The map
+   encodes the source location of the expansion point of the macro as
+   well as the "original" source location of each token that is part
+   of the macro replacement-list. If a macro is defined but never
+   expanded, it has no macro map.  SET is the set of maps the macro
+   map should be part of.  MACRO_NODE is the macro which the new macro
+   map should encode source locations for.  EXPANSION is the location
+   of the expansion point of MACRO. For function-like macros
+   invocations, it's best to make it point to the closing parenthesis
+   of the macro, rather than the the location of the first character
+   of the macro.  NUM_TOKENS is the number of tokens that are part of
+   the replacement-list of MACRO.  */
+const struct line_map *linemap_enter_macro (struct line_maps *,
+					    struct cpp_hashnode*,
+					    source_location,
+					    unsigned int);
+
+/* Create and return a virtual location for a token that is part of a
+   macro expansion-list at a macro expansion point.  See the comment
+   inside struct line_map_macro to see what an expansion-list exactly
+   is.
+
+   A call to this function must come after a call to
+   linemap_enter_macro.
+
+   MAP is the map into which the source location is created.  TOKEN_NO
+   is the index of the token in the macro replacement-list, starting
+   at number 0.
+
+   ORIG_LOC is the location of the token outside of this macro
+   expansion.  If the token comes originally from the macro
+   definition, it is the locus in the macro definition; otherwise it
+   is a location in the context of the caller of this macro expansion
+   (which is a virtual location or a source location if the caller is
+   itself a macro expansion or not).
+
+   MACRO_DEFINITION_LOC is the location in the macro definition,
+   either of the token itself or of a macro parameter that it
+   replaces.  */
+source_location linemap_add_macro_token (const struct line_map *,
+					 unsigned int,
+					 source_location,
+					 source_location);
+
+/* Return the source line number corresponding to source location
+   LOCATION.  SET is the line map set LOCATION comes from.  If
+   LOCATION is the location of token that is part of the
+   expansion-list of a macro expansion return the line number of the
+   macro expansion point.  */
+int linemap_get_expansion_line (struct line_maps *,
+				source_location);
+
+/* Return the path of the file corresponding to source code location
+   LOCATION.
+
+   If LOCATION is the location of a token that is part of the
+   replacement-list of a macro expansion return the file path of the
+   macro expansion point.
+
+   SET is the line map set LOCATION comes from.  */
+const char* linemap_get_expansion_filename (struct line_maps *,
+					    source_location);
+
 #ifdef __cplusplus
 }
 #endif
diff --git a/libcpp/line-map.c b/libcpp/line-map.c
index 2a0749a..49de244 100644
--- a/libcpp/line-map.c
+++ b/libcpp/line-map.c
@@ -23,24 +23,37 @@ along with this program; see the file COPYING3.  If not see
 #include "config.h"
 #include "system.h"
 #include "line-map.h"
+#include "cpplib.h"
+#include "internal.h"
 
 static void trace_include (const struct line_maps *, const struct line_map *);
+static const struct line_map * linemap_ordinary_map_lookup (struct line_maps *,
+							    source_location);
+static const struct line_map* linemap_macro_map_lookup (struct line_maps *,
+							source_location);
+static source_location linemap_macro_map_loc_to_def_point
+(const struct line_map*, source_location);
+static source_location linemap_macro_map_loc_unwind_toward_spelling
+(const struct line_map*, source_location);
+static source_location linemap_macro_map_loc_to_exp_point
+(const struct line_map*, source_location);
+static source_location linemap_macro_loc_to_spelling_point
+(struct line_maps *, source_location, const struct line_map **);
+static source_location linemap_macro_loc_to_def_point (struct line_maps *,
+						       source_location,
+						       const struct line_map **);
+static source_location linemap_macro_loc_to_exp_point (struct line_maps *,
+						       source_location,
+						       const struct line_map **);
 
 /* Initialize a line map set.  */
 
 void
 linemap_init (struct line_maps *set)
 {
-  set->maps = NULL;
-  set->allocated = 0;
-  set->used = 0;
-  set->trace_includes = false;
-  set->depth = 0;
-  set->cache = 0;
+  memset (set, 0, sizeof (struct line_maps));
   set->highest_location = RESERVED_LOCATION_COUNT - 1;
   set->highest_line = RESERVED_LOCATION_COUNT - 1;
-  set->max_column_hint = 0;
-  set->reallocator = 0;
 }
 
 /* Check for and warn about line_maps entered but not exited.  */
@@ -51,23 +64,55 @@ linemap_check_files_exited (struct line_maps *set)
   struct line_map *map;
   /* Depending upon whether we are handling preprocessed input or
      not, this can be a user error or an ICE.  */
-  for (map = &set->maps[set->used - 1]; ! MAIN_FILE_P (map);
+  for (map = LINEMAPS_LAST_ORDINARY_MAP (set);
+       ! MAIN_FILE_P (map);
        map = INCLUDED_FROM (set, map))
     fprintf (stderr, "line-map.c: file \"%s\" entered but not left\n",
-	     map->to_file);
+	     ORDINARY_MAP_FILE_NAME (map));
 }
- 
-/* Free a line map set.  */
 
-void
-linemap_free (struct line_maps *set)
+/* Create a new line map in the line map set SET, and return it.
+   REASON is the reason of creating the map. It determines the type
+   of map created (ordinary or macro map). Note that ordinary maps and
+   macro maps are allocated in different memory location.  */
+
+static struct line_map *
+new_linemap (struct line_maps *set,
+	     enum lc_reason reason)
 {
-  if (set->maps)
+  /* Depending on this variable, a macro map would be allocated in a
+     different memory location than an ordinary map.  */
+  bool macro_map_p = (reason == LC_ENTER_MACRO);
+  struct line_map *result;
+
+  if (LINEMAPS_USED (set, macro_map_p) == LINEMAPS_ALLOCATED (set, macro_map_p))
     {
-      linemap_check_files_exited (set);
+      /* We ran out of allocated line maps. Let's allocate more.  */
 
-      free (set->maps);
+      line_map_realloc reallocator
+	= set->reallocator ? set->reallocator : xrealloc;
+      LINEMAPS_ALLOCATED (set, macro_map_p) =
+	2 * LINEMAPS_ALLOCATED (set, macro_map_p) + 256;
+      LINEMAPS_MAPS (set, macro_map_p)
+	= (struct line_map *) (*reallocator) (LINEMAPS_MAPS (set, macro_map_p),
+					      LINEMAPS_ALLOCATED (set,
+								  macro_map_p)
+					      * sizeof (struct line_map));
+      result =
+	&LINEMAPS_MAPS (set, macro_map_p)[LINEMAPS_USED (set, macro_map_p)];
+      memset (result, 0,
+	      ((LINEMAPS_ALLOCATED (set, macro_map_p)
+		- LINEMAPS_USED (set, macro_map_p))
+	       * sizeof (struct line_map)));
     }
+  else
+    result =
+      &LINEMAPS_MAPS (set, macro_map_p)[LINEMAPS_USED (set, macro_map_p)];
+
+  LINEMAPS_USED (set, macro_map_p)++;
+
+  result->reason = reason;
+  return result;
 }
 
 /* Add a mapping of logical source line to physical source file and
@@ -90,23 +135,24 @@ linemap_add (struct line_maps *set, enum lc_reason reason,
   struct line_map *map;
   source_location start_location = set->highest_location + 1;
 
-  if (set->used && start_location < set->maps[set->used - 1].start_location)
-    abort ();
+  linemap_assert (!(LINEMAPS_ORDINARY_USED (set)
+		    && (start_location
+			< MAP_START_LOCATION (LINEMAPS_LAST_ORDINARY_MAP (set)))));
+
+  /* When we enter the file for the first time reason cannot be
+     LC_RENAME.  */
+  linemap_assert (!(set->depth == 0 && reason == LC_RENAME));
 
-  if (set->used == set->allocated)
+  /* If we are leaving the main file, return a NULL map.  */
+  if (reason == LC_LEAVE
+      && MAIN_FILE_P (LINEMAPS_LAST_ORDINARY_MAP (set))
+      && to_file == NULL)
     {
-      line_map_realloc reallocator
-	= set->reallocator ? set->reallocator : xrealloc;
-      set->allocated = 2 * set->allocated + 256;
-      set->maps
-	= (struct line_map *) (*reallocator) (set->maps,
-					      set->allocated
-					      * sizeof (struct line_map));
-      memset (&set->maps[set->used], 0, ((set->allocated - set->used)
-					 * sizeof (struct line_map)));
+      set->depth--;
+      return NULL;
     }
 
-  map = &set->maps[set->used];
+  map = new_linemap (set, reason);
 
   if (to_file && *to_file == '\0' && reason != LC_RENAME_VERBATIM)
     to_file = "<stdin>";
@@ -114,29 +160,35 @@ linemap_add (struct line_maps *set, enum lc_reason reason,
   if (reason == LC_RENAME_VERBATIM)
     reason = LC_RENAME;
 
-  if (set->depth == 0 && reason == LC_RENAME)
-    abort ();
-
   if (reason == LC_LEAVE)
     {
+      /* When we are just leaving an "included" file, and jump to the next
+	 location inside the "includer" right after the #include
+	 "included", this variable points the map in use right before the
+	 #include "included", inside the same "includer" file.  */
       struct line_map *from;
       bool error;
 
       if (MAIN_FILE_P (map - 1))
 	{
-	  if (to_file == NULL)
-	    {
-	      set->depth--;
-	      return NULL;
-	    }
+	  /* So this _should_ means we are leaving the main file --
+	     effectively ending the compilation unit. But to_file not
+	     being NULL means the caller thinks we are leaving to
+	     another file. This is an erroneous behaviour but we'll
+	     try to recover from it. Let's pretend we are not leaving
+	     the main file.  */
 	  error = true;
           reason = LC_RENAME;
           from = map - 1;
 	}
       else
 	{
+	  /* (MAP - 1) points to the map we are leaving. The
+	     map from which (MAP - 1) got included should be the map
+	     that comes right before MAP in the same file.  */
 	  from = INCLUDED_FROM (set, map - 1);
-	  error = to_file && filename_cmp (from->to_file, to_file);
+	  error = to_file && filename_cmp (ORDINARY_MAP_FILE_NAME (from),
+					   to_file);
 	}
 
       /* Depending upon whether we are handling preprocessed input or
@@ -148,55 +200,176 @@ linemap_add (struct line_maps *set, enum lc_reason reason,
       /* A TO_FILE of NULL is special - we use the natural values.  */
       if (error || to_file == NULL)
 	{
-	  to_file = from->to_file;
+	  to_file = ORDINARY_MAP_FILE_NAME (from);
 	  to_line = SOURCE_LINE (from, from[1].start_location);
-	  sysp = from->sysp;
+	  sysp = ORDINARY_MAP_IN_SYSTEM_HEADER_P (from);
 	}
     }
 
-  map->reason = reason;
-  map->sysp = sysp;
-  map->start_location = start_location;
-  map->to_file = to_file;
-  map->to_line = to_line;
-  set->cache = set->used++;
-  map->column_bits = 0;
+  linemap_assert (reason != LC_ENTER_MACRO);
+  ORDINARY_MAP_IN_SYSTEM_HEADER_P (map) = sysp;
+  MAP_START_LOCATION (map) = start_location;
+  ORDINARY_MAP_FILE_NAME (map) = to_file;
+  ORDINARY_MAP_STARTING_LINE_NUMBER (map) = to_line;
+  LINEMAPS_ORDINARY_CACHE (set) = LINEMAPS_ORDINARY_USED (set) - 1;
+  ORDINARY_MAP_NUMBER_OF_COLUMN_BITS (map) = 0;
   set->highest_location = start_location;
   set->highest_line = start_location;
   set->max_column_hint = 0;
 
   if (reason == LC_ENTER)
     {
-      map->included_from = set->depth == 0 ? -1 : (int) (set->used - 2);
+      ORDINARY_MAP_INCLUDER_FILE_INDEX (map) = 
+	set->depth == 0 ? -1 : (int) (LINEMAPS_ORDINARY_USED (set) - 2);
       set->depth++;
       if (set->trace_includes)
 	trace_include (set, map);
     }
   else if (reason == LC_RENAME)
-    map->included_from = map[-1].included_from;
+    ORDINARY_MAP_INCLUDER_FILE_INDEX (map) =
+      ORDINARY_MAP_INCLUDER_FILE_INDEX (&map[-1]);
   else if (reason == LC_LEAVE)
     {
       set->depth--;
-      map->included_from = INCLUDED_FROM (set, map - 1)->included_from;
+      ORDINARY_MAP_INCLUDER_FILE_INDEX (map) =
+	ORDINARY_MAP_INCLUDER_FILE_INDEX (INCLUDED_FROM (set, map - 1));
     }
 
   return map;
 }
 
+/* Returns TRUE if the line table set tracks token locations accross
+   macro expansion, FALSE otherwise.  */
+
+bool
+linemap_tracks_macro_expansion_locs_p (struct line_maps *set)
+{
+  return LINEMAPS_MACRO_MAPS (set) != NULL;
+}
+
+/* Create a macro map.  A macro map encodes source locations of tokens
+   that are part of a macro replacement-list, at a macro expansion
+   point.  See the extensive comments of struct line_map and struct
+   line_map_macro, in line-map.h.
+
+   This map shall be created when the macro is expanded.  The map
+   encodes the source location of the expansion point of the macro as
+   well as the "original" source location of each token that is part
+   of the macro replacement-list.  If a macro is defined but never
+   expanded, it has no macro map.  SET is the set of maps the macro
+   map should be part of.  MACRO_NODE is the macro which the new macro
+   map should encode source locations for.  EXPANSION is the location
+   of the expansion point of MACRO. For function-like macros
+   invocations, it's best to make it point to the closing parenthesis
+   of the macro, rather than the the location of the first character
+   of the macro.  NUM_TOKENS is the number of tokens that are part of
+   the replacement-list of MACRO.
+
+   Note that when we run out of the integer space available for source
+   locations, this function returns NULL.  In that case, callers of
+   this function cannot encode {line,column} pairs into locations of
+   macro tokens anymore.  */
+
+const struct line_map *
+linemap_enter_macro (struct line_maps *set, struct cpp_hashnode *macro_node,
+		     source_location expansion, unsigned int num_tokens)
+{
+  struct line_map *map;
+  source_location start_location;
+  line_map_realloc reallocator
+    = set->reallocator ? set->reallocator : xrealloc;
+
+  start_location = LINEMAPS_MACRO_LOWEST_LOCATION (set) - num_tokens;
+
+  if (start_location <= set->highest_line
+      || start_location > LINEMAPS_MACRO_LOWEST_LOCATION (set))
+    /* We ran out of macro map space.   */
+    return NULL;
+
+  map = new_linemap (set, LC_ENTER_MACRO);
+
+  MAP_START_LOCATION (map) = start_location;
+  MACRO_MAP_MACRO (map) = macro_node;
+  MACRO_MAP_NUM_MACRO_TOKENS (map) = num_tokens;
+  MACRO_MAP_LOCATIONS (map)
+    = (source_location*) reallocator (NULL,
+				      2 * num_tokens
+				      * sizeof (source_location));
+  MACRO_MAP_EXPANSION_POINT_LOCATION (map) = expansion;
+  memset (MACRO_MAP_LOCATIONS (map), 0,
+	  num_tokens * sizeof (source_location));
+
+  LINEMAPS_MACRO_CACHE (set) = LINEMAPS_MACRO_USED (set) - 1;
+  set->max_column_hint = 0;
+
+  return map;
+}
+
+/* Create and return a virtual location for a token that is part of a
+   macro expansion-list at a macro expansion point.  See the comment
+   inside struct line_map_macro to see what an expansion-list exactly
+   is.
+
+   A call to this function must come after a call to
+   linemap_enter_macro.
+
+   MAP is the map into which the source location is created.  TOKEN_NO
+   is the index of the token in the macro replacement-list, starting
+   at number 0.
+
+   ORIG_LOC is the location of the token outside of this macro
+   expansion.  If the token comes originally from the macro
+   definition, it is the locus in the macro definition; otherwise it
+   is a location in the context of the caller of this macro expansion
+   (which is a virtual location or a source location if the caller is
+   itself a macro expansion or not).
+
+   MACRO_DEFINITION_LOC is the location in the macro definition,
+   either of the token itself or of a macro parameter that it
+   replaces.  */
+
+source_location
+linemap_add_macro_token (const struct line_map *map,
+			 unsigned int token_no,
+			 source_location orig_loc,
+			 source_location orig_parm_replacement_loc)
+{
+  source_location result;
+
+  linemap_assert (linemap_macro_expansion_map_p (map));
+  linemap_assert (token_no < MACRO_MAP_NUM_MACRO_TOKENS (map));
+
+  MACRO_MAP_LOCATIONS (map)[2 * token_no] = orig_loc;
+  MACRO_MAP_LOCATIONS (map)[2 * token_no + 1] = orig_parm_replacement_loc;
+
+  result = MAP_START_LOCATION (map) + token_no;
+  return result;
+}
+
+/* Return a source_location for the start (i.e. column==0) of
+   (physical) line TO_LINE in the current source file (as in the
+   most recent linemap_add).   MAX_COLUMN_HINT is the highest column
+   number we expect to use in this line (but it does not change
+   the highest_location).  */
+
 source_location
 linemap_line_start (struct line_maps *set, linenum_type to_line,
 		    unsigned int max_column_hint)
 {
-  struct line_map *map = &set->maps[set->used - 1];
+  struct line_map *map = LINEMAPS_LAST_ORDINARY_MAP (set);
   source_location highest = set->highest_location;
   source_location r;
-  linenum_type last_line = SOURCE_LINE (map, set->highest_line);
+  linenum_type last_line =
+    SOURCE_LINE (map, set->highest_line);
   int line_delta = to_line - last_line;
   bool add_map = false;
+
   if (line_delta < 0
-      || (line_delta > 10 && line_delta * map->column_bits > 1000)
-      || (max_column_hint >= (1U << map->column_bits))
-      || (max_column_hint <= 80 && map->column_bits >= 10))
+      || (line_delta > 10
+	  && line_delta * ORDINARY_MAP_NUMBER_OF_COLUMN_BITS (map) > 1000)
+      || (max_column_hint >= (1U << ORDINARY_MAP_NUMBER_OF_COLUMN_BITS (map)))
+      || (max_column_hint <= 80
+	  && ORDINARY_MAP_NUMBER_OF_COLUMN_BITS (map) >= 10))
     {
       add_map = true;
     }
@@ -224,16 +397,27 @@ linemap_line_start (struct line_maps *set, linenum_type to_line,
       /* Allocate the new line_map.  However, if the current map only has a
 	 single line we can sometimes just increase its column_bits instead. */
       if (line_delta < 0
-	  || last_line != map->to_line
+	  || last_line != ORDINARY_MAP_STARTING_LINE_NUMBER (map)
 	  || SOURCE_COLUMN (map, highest) >= (1U << column_bits))
-	map = (struct line_map *) linemap_add (set, LC_RENAME, map->sysp,
-					       map->to_file, to_line);
-      map->column_bits = column_bits;
-      r = map->start_location + ((to_line - map->to_line) << column_bits);
+	map = (struct line_map *) linemap_add (set, LC_RENAME,
+					       ORDINARY_MAP_IN_SYSTEM_HEADER_P
+					       (map),
+					       ORDINARY_MAP_FILE_NAME (map),
+					       to_line);
+      ORDINARY_MAP_NUMBER_OF_COLUMN_BITS (map) = column_bits;
+      r = (MAP_START_LOCATION (map)
+	   + ((to_line - ORDINARY_MAP_STARTING_LINE_NUMBER (map))
+	      << column_bits));
     }
   else
     r = highest - SOURCE_COLUMN (map, highest)
-      + (line_delta << map->column_bits);
+      + (line_delta << ORDINARY_MAP_NUMBER_OF_COLUMN_BITS (map));
+
+  /* Locations of ordinary tokens are always lower than locations of
+     macro tokens.  */
+  if (r >= LINEMAPS_MACRO_LOWEST_LOCATION (set))
+    return 0;
+
   set->highest_line = r;
   if (r > set->highest_location)
     set->highest_location = r;
@@ -241,10 +425,19 @@ linemap_line_start (struct line_maps *set, linenum_type to_line,
   return r;
 }
 
+/* Encode and return a source_location from a column number. The
+   source line considered is the last source line used to call
+   linemap_line_start, i.e, the last source line which a location was
+   encoded from.  */
+
 source_location
 linemap_position_for_column (struct line_maps *set, unsigned int to_column)
 {
   source_location r = set->highest_line;
+
+  linemap_assert
+    (!linemap_macro_expansion_map_p (LINEMAPS_LAST_ORDINARY_MAP (set)));
+
   if (to_column >= set->max_column_hint)
     {
       if (r >= 0xC000000 || to_column > 100000)
@@ -254,7 +447,7 @@ linemap_position_for_column (struct line_maps *set, unsigned int to_column)
 	}
       else
 	{
-	  struct line_map *map = &set->maps[set->used - 1];
+	  struct line_map *map = LINEMAPS_LAST_ORDINARY_MAP (set);
 	  r = linemap_line_start (set, SOURCE_LINE (map, r), to_column + 50);
 	}
     }
@@ -264,25 +457,55 @@ linemap_position_for_column (struct line_maps *set, unsigned int to_column)
   return r;
 }
 
-/* Given a logical line, returns the map from which the corresponding
-   (source file, line) pair can be deduced.  Since the set is built
-   chronologically, the logical lines are monotonic increasing, and so
-   the list is sorted and we can use a binary search.  */
+/* Encode and return a source location from a given line and
+   column.  */
 
-const struct line_map *
+source_location
+linemap_position_for_line_and_column (struct line_map *map,
+				      linenum_type line,
+				      unsigned column)
+{
+  linemap_assert (ORDINARY_MAP_STARTING_LINE_NUMBER (map) <= line);
+
+  return (MAP_START_LOCATION (map)
+	  + ((line - ORDINARY_MAP_STARTING_LINE_NUMBER (map))
+	     << ORDINARY_MAP_NUMBER_OF_COLUMN_BITS (map))
+	  + (column & ((1 << ORDINARY_MAP_NUMBER_OF_COLUMN_BITS (map)) - 1)));
+}
+
+/* Given a virtual source location yielded by a map (either an
+   ordinary or a macro map), returns that map.  */
+
+const struct line_map*
 linemap_lookup (struct line_maps *set, source_location line)
 {
+  if (linemap_location_from_macro_expansion_p (set, line))
+    return linemap_macro_map_lookup (set, line);
+  return linemap_ordinary_map_lookup (set, line);
+}
+
+/* Given a source location yielded by an ordinary map, returns that
+   map.  Since the set is built chronologically, the logical lines are
+   monotonic increasing, and so the list is sorted and we can use a
+   binary search.  */
+
+static const struct line_map *
+linemap_ordinary_map_lookup (struct line_maps *set, source_location line)
+{
   unsigned int md, mn, mx;
-  const struct line_map *cached;
+  const struct line_map *cached, *result;
 
-  mn = set->cache;
-  mx = set->used;
+  if (set ==  NULL || line < RESERVED_LOCATION_COUNT)
+    return NULL;
+
+  mn = LINEMAPS_ORDINARY_CACHE (set);
+  mx = LINEMAPS_ORDINARY_USED (set);
   
-  cached = &set->maps[mn];
+  cached = LINEMAPS_ORDINARY_MAP_AT (set, mn);
   /* We should get a segfault if no line_maps have been added yet.  */
-  if (line >= cached->start_location)
+  if (line >= MAP_START_LOCATION (cached))
     {
-      if (mn + 1 == mx || line < cached[1].start_location)
+      if (mn + 1 == mx || line < MAP_START_LOCATION (&cached[1]))
 	return cached;
     }
   else
@@ -294,14 +517,345 @@ linemap_lookup (struct line_maps *set, source_location line)
   while (mx - mn > 1)
     {
       md = (mn + mx) / 2;
-      if (set->maps[md].start_location > line)
+      if (MAP_START_LOCATION (LINEMAPS_ORDINARY_MAP_AT (set, md)) > line)
 	mx = md;
       else
 	mn = md;
     }
 
-  set->cache = mn;
-  return &set->maps[mn];
+  LINEMAPS_ORDINARY_CACHE (set) = mn;
+  result = LINEMAPS_ORDINARY_MAP_AT (set, mn);
+  linemap_assert (line >= MAP_START_LOCATION (result));
+  return result;
+}
+
+/* Given a source location yielded by a macro map, returns that map.
+   Since the set is built chronologically, the logical lines are
+   monotonic decreasing, and so the list is sorted and we can use a
+   binary search.  */
+
+static const struct line_map*
+linemap_macro_map_lookup (struct line_maps *set, source_location line)
+{
+  unsigned int md, mn, mx;
+  const struct line_map *cached, *result;
+
+  linemap_assert (line >= LINEMAPS_MACRO_LOWEST_LOCATION (set));
+
+  if (set ==  NULL)
+    return NULL;
+
+  mn = LINEMAPS_MACRO_CACHE (set);
+  mx = LINEMAPS_MACRO_USED (set);
+  cached = LINEMAPS_MACRO_MAP_AT (set, mn);
+  
+  if (line >= MAP_START_LOCATION (cached))
+    {
+      if (mn == 0 || line < MAP_START_LOCATION (&cached[-1]))
+	return cached;
+      mx = mn - 1;
+      mn = 0;
+    }
+
+  do 
+    {
+      md = (mx + mn) / 2;
+      if (MAP_START_LOCATION (LINEMAPS_MACRO_MAP_AT (set, md)) > line)
+	mn = md;
+      else
+	mx = md;
+    } while (mx - mn > 1);
+
+  LINEMAPS_MACRO_CACHE (set) = mx;
+  result = LINEMAPS_MACRO_MAP_AT (set, LINEMAPS_MACRO_CACHE (set));
+  linemap_assert (MAP_START_LOCATION (result) <= line);
+
+  return result;
+}
+
+/* Return TRUE if MAP encodes locations coming from a macro
+   replacement-list at macro expansion point.  */
+
+bool
+linemap_macro_expansion_map_p (const struct line_map *map)
+{
+  if (!map)
+    return false;
+  return (map->reason == LC_ENTER_MACRO);
+}
+
+/* If LOCATION is the locus of a token in a replacement-list of a
+   macro expansion return the location of the macro expansion point.
+
+   Read the comments of struct line_map and struct line_map_macro in
+   line-map.h to understand what a macro expansion point is.  */
+
+source_location
+linemap_macro_map_loc_to_exp_point (const struct line_map *map,
+				    source_location location)
+{
+  unsigned token_no;
+
+  linemap_assert (linemap_macro_expansion_map_p (map)
+		  && location >= MAP_START_LOCATION (map));
+
+  /* Make sure LOCATION is correct.  */
+  token_no = location - MAP_START_LOCATION (map);
+  linemap_assert (token_no <  MACRO_MAP_NUM_MACRO_TOKENS (map));
+
+  return MACRO_MAP_EXPANSION_POINT_LOCATION (map);
+}
+
+/* If LOCATION is the source location of a token that belongs to a
+   macro replacement-list -- as part of a macro expansion -- then
+   return the location of the token at the definition point of the
+   macro.  Otherwise, return LOCATION.  SET is the set of maps
+   location come from.  ORIGINAL_MAP is an output parm. If non NULL,
+   the function sets *ORIGINAL_MAP to the ordinary (non-macro) map the
+   returned location comes from.  */
+
+source_location
+linemap_macro_map_loc_to_def_point (const struct line_map *map,
+				    source_location location)
+{
+  unsigned token_no;
+
+  linemap_assert (linemap_macro_expansion_map_p (map)
+		  && location >= MAP_START_LOCATION (map));
+  linemap_assert (location >= RESERVED_LOCATION_COUNT);
+
+  token_no = location - MAP_START_LOCATION (map);
+  linemap_assert (token_no < MACRO_MAP_NUM_MACRO_TOKENS (map));
+
+  location = MACRO_MAP_LOCATIONS (map)[2 * token_no + 1];
+
+  return location;
+}
+
+/* If LOCATION is the locus of a token that is an argument of a
+   function-like macro M and appears in the expansion of M, return the
+   locus of that argument in the context of the caller of M.
+
+   In other words, this returns the xI location presented in the
+   comments of line_map_macro above.  */
+source_location
+linemap_macro_map_loc_unwind_toward_spelling (const struct line_map* map,
+					      source_location location)
+{
+  unsigned token_no;
+
+  linemap_assert (linemap_macro_expansion_map_p (map)
+		  && location >= MAP_START_LOCATION (map));
+  linemap_assert (location >= RESERVED_LOCATION_COUNT);
+
+  token_no = location - MAP_START_LOCATION (map);
+  linemap_assert (token_no < MACRO_MAP_NUM_MACRO_TOKENS (map));
+
+  location = MACRO_MAP_LOCATIONS (map)[2 * token_no];
+  
+  return location;
+}
+
+/* Return the source line number corresponding to source location
+   LOCATION.  SET is the line map set LOCATION comes from.  If
+   LOCATION is the source location of token that is part of the
+   replacement-list of a macro expansion return the line number of the
+   macro expansion point.  */
+
+int
+linemap_get_expansion_line (struct line_maps *set,
+			    source_location location)
+{
+  const struct line_map *map = NULL;
+
+  if (location < RESERVED_LOCATION_COUNT)
+    return 0;
+
+  location =
+    linemap_macro_loc_to_exp_point (set, location, &map);
+
+  return SOURCE_LINE (map, location);
+}
+
+/* Return the path of the file corresponding to source code location
+   LOCATION.
+
+   If LOCATION is the source location of token that is part of the
+   replacement-list of a macro expansion return the file path of the
+   macro expansion point.
+
+   SET is the line map set LOCATION comes from.  */
+
+const char*
+linemap_get_expansion_filename (struct line_maps *set,
+				source_location location)
+{
+  const struct line_map *map = NULL;
+
+  if (location < RESERVED_LOCATION_COUNT)
+    return NULL;
+
+  location =
+    linemap_macro_loc_to_exp_point (set, location, &map);
+
+  return LINEMAP_FILE (map);
+}
+
+/* Return the name of the macro associated to MACRO_MAP.  */
+
+const char*
+linemap_map_get_macro_name (const struct line_map* macro_map)
+{
+  linemap_assert (macro_map && linemap_macro_expansion_map_p (macro_map));
+  return (const char*) NODE_NAME (MACRO_MAP_MACRO (macro_map));
+}
+
+/* Return a positive value if LOCATION is the locus of a token that is
+   located in a system header, O otherwise. It returns 1 if LOCATION
+   is the locus of a token that is located in a system header, and 2
+   if LOCATION is the locus of a token located in a C system header
+   that therefore needs to be extern "C" protected in C++.
+
+   Note that this function returns 1 if LOCATION belongs to a token
+   that is part of a macro replacement-list defined in a system
+   header, but expanded in a non-system file.  */
+
+int
+linemap_location_in_system_header_p (struct line_maps *set,
+				     source_location location)
+{
+  const struct line_map *map = NULL;
+
+  if (location < RESERVED_LOCATION_COUNT)
+    return false;
+
+  location =
+    linemap_resolve_location (set, location, LRK_SPELLING_LOCATION, &map);
+
+  return LINEMAP_SYSP (map);
+}
+
+/* Return TRUE if LOCATION is a source code location of a token coming
+   from a macro replacement-list at a macro expansion point, FALSE
+   otherwise.  */
+
+bool
+linemap_location_from_macro_expansion_p (struct line_maps *set,
+					 source_location location)
+{
+  linemap_assert (location <= MAX_SOURCE_LOCATION
+		  && (set->highest_location
+		      < LINEMAPS_MACRO_LOWEST_LOCATION (set)));
+  if (set == NULL)
+    return false;
+  return (location > set->highest_location);
+}
+
+/* Given two virtual locations *LOC0 and *LOC1, return the first
+   common macro map in their macro expansion histories.  Return NULL
+   if no common macro was found.  *LOC0 (resp. *LOC1) is set to the
+   virtual location of the token inside the resulting macro.  */
+
+static const struct line_map*
+first_map_in_common_1 (struct line_maps *set,
+		       source_location *loc0,
+		       source_location *loc1)
+{
+  source_location l0 = *loc0, l1 = *loc1;
+  const struct line_map *map0 = linemap_lookup (set, l0),
+    *map1 = linemap_lookup (set, l1);
+
+  while (linemap_macro_expansion_map_p (map0)
+	 && linemap_macro_expansion_map_p (map1)
+	 && (map0 != map1))
+    {
+      if (MAP_START_LOCATION (map0) < MAP_START_LOCATION (map1))
+	{
+	  l0 = linemap_macro_map_loc_to_exp_point (map0, l0);
+	  map0 = linemap_lookup (set, l0);
+	}
+      else
+	{
+	  l1 = linemap_macro_map_loc_to_exp_point (map1, l1);
+	  map1 = linemap_lookup (set, l1);
+	}
+    }
+
+  if (map0 == map1)
+    {
+      *loc0 = l0;
+      *loc1 = l1;
+      return map0;
+    }
+  return NULL;
+}
+
+/* Given two virtual locations LOC0 and LOC1, return the first common
+   macro map in their macro expansion histories.  Return NULL if no
+   common macro was found.  *RES_LOC0 (resp. *RES_LOC1) is set to the
+   virtual location of the token inside the resulting macro, upon
+   return of a non-NULL result.  */
+
+static const struct line_map*
+first_map_in_common (struct line_maps *set,
+		     source_location loc0,
+		     source_location loc1,
+		     source_location  *res_loc0,
+		     source_location  *res_loc1)
+{
+  *res_loc0 = loc0;
+  *res_loc1 = loc1;
+
+  return first_map_in_common_1 (set, res_loc0, res_loc1);
+}
+
+/* Return a positive value if PRE denotes the location of a token that
+   comes before the token of POST, 0 if PRE denotes the location of
+   the same token as the token for POST, and a negative value
+   otherwise.  */
+
+int
+linemap_compare_locations (struct line_maps *set,
+			   source_location  pre,
+			   source_location post)
+{
+  bool pre_virtual_p, post_virtual_p;
+  source_location l0 = pre, l1 = post;
+
+  if (l0 == l1)
+    return 0;
+
+  if ((pre_virtual_p = linemap_location_from_macro_expansion_p (set, l0)))
+    l0 = linemap_resolve_location (set, l0,
+				   LRK_MACRO_EXPANSION_POINT,
+				   NULL);
+
+  if ((post_virtual_p = linemap_location_from_macro_expansion_p (set, l1)))
+    l1 = linemap_resolve_location (set, l1,
+				   LRK_MACRO_EXPANSION_POINT,
+				   NULL);
+
+  if (l0 == l1
+      && pre_virtual_p
+      && post_virtual_p)
+    {
+      /* So pre and post represent two tokens that are present in a
+	 same macro expansion.  Let's see if the token for pre was
+	 before the token for post in that expansion.  */
+      unsigned i0, i1;
+      const struct line_map *map =
+	first_map_in_common (set, pre, post, &l0, &l1);
+
+      if (map == NULL)
+	/* This should not be possible.  */
+	abort ();
+
+      i0 = l0 - MAP_START_LOCATION (map);
+      i1 = l1 - MAP_START_LOCATION (map);
+      return i1 - i0;
+    }
+
+  return l1 - l0;
 }
 
 /* Print an include trace, for e.g. the -H option of the preprocessor.  */
@@ -313,5 +867,241 @@ trace_include (const struct line_maps *set, const struct line_map *map)
 
   while (--i)
     putc ('.', stderr);
-  fprintf (stderr, " %s\n", map->to_file);
+
+  fprintf (stderr, " %s\n", ORDINARY_MAP_FILE_NAME (map));
+}
+
+/* Return the spelling location of the token wherever it comes from,
+   whether part of a macro definition or not.
+
+   This is a subroutine for linemap_resolve_location.  */
+
+static source_location
+linemap_macro_loc_to_spelling_point (struct line_maps *set,
+				     source_location location,
+				     const struct line_map **original_map)
+{
+  struct line_map *map;
+
+  linemap_assert (set && location >= RESERVED_LOCATION_COUNT);
+
+  while (true)
+    {
+      map = (struct line_map*) linemap_lookup (set, location);
+      if (!linemap_macro_expansion_map_p (map))
+	break;
+
+      location =
+	linemap_macro_map_loc_unwind_toward_spelling (map, location);
+    }
+
+  if (original_map)
+    *original_map = map;
+  return location;
+}
+
+/* If LOCATION is the source location of a token that belongs to a
+   macro replacement-list -- as part of a macro expansion -- then
+   return the location of the token at the definition point of the
+   macro.  Otherwise, return LOCATION.  SET is the set of maps
+   location come from.  ORIGINAL_MAP is an output parm. If non NULL,
+   the function sets *ORIGINAL_MAP to the ordinary (non-macro) map the
+   returned location comes from. 
+
+   This is a subroutine of linemap_resolve_location.  */
+
+static source_location
+linemap_macro_loc_to_def_point (struct line_maps *set,
+				source_location location,
+				const struct line_map **original_map)
+{
+  struct line_map *map;
+
+  linemap_assert (set && location >= RESERVED_LOCATION_COUNT);
+
+  while (true)
+    {
+      map = (struct line_map*) linemap_lookup (set, location);
+      if (!linemap_macro_expansion_map_p (map))
+	break;
+
+      location =
+	linemap_macro_map_loc_to_def_point (map, location);
+    }
+
+  if (original_map)
+    *original_map = map;
+  return location;
+}
+
+/* If LOCATION is the source location of a token that belongs to a
+   macro replacement-list -- at a macro expansion point -- then return
+   the location of the topmost expansion point of the macro.  We say
+   topmost because if we are in the context of a nested macro
+   expansion, the function returns the source location of the first
+   macro expansion that triggered the nested expansions.
+
+   Otherwise, return LOCATION.  SET is the set of maps location come
+   from.  ORIGINAL_MAP is an output parm. If non NULL, the function
+   sets *ORIGINAL_MAP to the ordinary (non-macro) map the returned
+   location comes from.
+
+   This is a subroutine of linemap_resolve_location.  */
+
+static source_location
+linemap_macro_loc_to_exp_point (struct line_maps *set,
+				source_location location,
+				const struct line_map **original_map)
+{
+  struct line_map *map;
+
+  linemap_assert (set && location >= RESERVED_LOCATION_COUNT);
+
+  while (true)
+    {
+      map = (struct line_map*) linemap_lookup (set, location);
+      if (!linemap_macro_expansion_map_p (map))
+	break;
+      location = linemap_macro_map_loc_to_exp_point (map, location);
+    }
+
+  if (original_map)
+    *original_map = map;
+  return location;
+}
+
+/* Resolve a virtual location into either a spelling location, an
+   expansion point location or a token argument replacement point
+   location.  Return the map that encodes the virtual location as well
+   as the resolved location.
+
+   If LOC is *NOT* the location of a token resulting from the
+   expansion of a macro, then the parameter LRK (which stands for
+   Location Resolution Kind) is ignored and the resulting location
+   just equals the one given in argument.
+
+   Now if LOC *IS* the location of a token resulting from the
+   expansion of a macro, this is what happens.
+
+   * If LRK is set to LRK_MACRO_EXPANSION_POINT
+   -------------------------------
+
+   The virtual location is resolved to the location to the locus of
+   the expansion point of the macro.
+
+   * If LRK is set to LRK_SPELLING_LOCATION
+   -------------------------------------
+
+   The virtual location is resolved to the location to the locus where
+   the token has been spelled in the source. This can follow through
+   all the macro expansions that led to the token.
+
+   * If LRK is set to LRK_MACRO_PARM_REPLACEMENT_POINT
+   --------------------------------------
+
+   If LOC is the locus of a token that is an argument of a
+   function-like macro [replacing a parameter in the replacement list
+   of the macro] the virtual location is resolved to the locus of the
+   parameter that is replaced, in the context of the definition of the
+   macro.
+
+   If LOC is the locus of a token that is not an argument of a
+   function-like macro, then the function behaves as if LRK was set to
+   LRK_SPELLING_LOCATION.
+
+   If MAP is non-NULL, *MAP is set to the map of the resolved
+   location.  */
+
+source_location
+linemap_resolve_location (struct line_maps *set,
+			  source_location loc,
+			  enum location_resolution_kind lrk,
+			  const struct line_map **map)
+{
+  linemap_assert (set && loc >= RESERVED_LOCATION_COUNT);
+
+  switch (lrk)
+    {
+    case LRK_MACRO_EXPANSION_POINT:
+      loc = linemap_macro_loc_to_exp_point (set, loc, map);
+      break;
+    case LRK_SPELLING_LOCATION:
+      loc = linemap_macro_loc_to_spelling_point (set, loc, map);
+      break;
+    case LRK_MACRO_DEFINITION_LOCATION:
+      loc = linemap_macro_loc_to_def_point (set, loc, map);
+      break;
+    default:
+      abort ();
+    }
+  return loc;
+}
+
+/* 
+   Suppose that LOC is the virtual location of a token T coming from
+   the expansion of a macro M.  This function then steps up to get the
+   location L of the point where M got expanded.  If L is a spelling
+   location inside a macro expansion M', then this function returns
+   the locus of the point where M' was expanded.  Said otherwise, this
+   function returns the location of T in the context that triggered
+   the expansion of M. 
+
+   *LOC_MAP must be set to the map of LOC.  This function then sets it
+   to the map of the returned location.  */
+
+source_location
+linemap_unwind_toward_expansion (struct line_maps *set,
+				 source_location loc,
+				 const struct line_map **map)
+{
+  source_location resolved_location;
+  const struct line_map *resolved_map;
+
+  resolved_location =
+    linemap_macro_map_loc_unwind_toward_spelling (*map, loc);
+  resolved_map = linemap_lookup (set, resolved_location);
+
+  if (!linemap_macro_expansion_map_p (resolved_map))
+    {
+      resolved_location = linemap_macro_map_loc_to_exp_point (*map, loc);
+      resolved_map = linemap_lookup (set, resolved_location);
+    }
+
+  *map = resolved_map;
+  return resolved_location;
+}
+
+/* Expand source code location LOC and return a user readable source
+   code location.  LOC must be a spelling (non-virtual) location.  */
+
+expanded_location
+linemap_expand_location (const struct line_map *map,
+			 source_location loc)
+
+{
+  expanded_location xloc;
+
+  xloc.file = LINEMAP_FILE (map);
+  xloc.line = SOURCE_LINE (map, loc);
+  xloc.column = SOURCE_COLUMN (map, loc);
+  xloc.sysp = LINEMAP_SYSP (map) != 0;
+
+  return xloc;
+}
+
+/* Expand source code location LOC and return a user readable source
+   code location.  LOC can be a virtual location.  The LRK parameter
+   is the same as for linemap_resolve_location.  */
+
+expanded_location
+linemap_expand_location_full (struct line_maps *set,
+			      source_location loc,
+			      enum location_resolution_kind lrk)
+{
+  const struct line_map *map;
+  expanded_location xloc;
+
+  loc = linemap_resolve_location (set, loc, lrk, &map);
+  xloc = linemap_expand_location (map, loc);
+  return xloc;
 }
diff --git a/libcpp/macro.c b/libcpp/macro.c
index eba2349..defc486 100644
--- a/libcpp/macro.c
+++ b/libcpp/macro.c
@@ -171,13 +171,17 @@ _cpp_builtin_macro_text (cpp_reader *pfile, cpp_hashnode *node)
 	unsigned int len;
 	const char *name;
 	uchar *buf;
-	map = linemap_lookup (pfile->line_table, pfile->line_table->highest_line);
-
-	if (node->value.builtin == BT_BASE_FILE)
-	  while (! MAIN_FILE_P (map))
-	    map = INCLUDED_FROM (pfile->line_table, map);
-
-	name = map->to_file;
+	
+	if (node->value.builtin == BT_FILE)
+	  name = linemap_get_expansion_filename (pfile->line_table,
+						 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);
+	  }
 	len = strlen (name);
 	buf = _cpp_unaligned_alloc (pfile, len * 2 + 3);
 	result = buf;
@@ -196,14 +200,14 @@ _cpp_builtin_macro_text (cpp_reader *pfile, cpp_hashnode *node)
       break;
 
     case BT_SPECLINE:
-      map = &pfile->line_table->maps[pfile->line_table->used-1];
+      map = LINEMAPS_LAST_ORDINARY_MAP (pfile->line_table);
       /* 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 = SOURCE_LINE (map, 
-			    CPP_OPTION (pfile, traditional) 
-			    ? pfile->line_table->highest_line
-			    : pfile->cur_token[-1].src_loc);
+      number = linemap_get_expansion_line (pfile->line_table,
+					   CPP_OPTION (pfile, traditional)
+					   ? pfile->line_table->highest_line
+					   : pfile->cur_token[-1].src_loc);
       break;
 
       /* __STDC__ has the value 1 under normal circumstances.
-- 
1.7.6.4

^ permalink raw reply	[flat|nested] 24+ messages in thread

* [PATCH 2/6] Generate virtual locations for tokens
  2011-10-17  9:58 [PATCH 0/6] Tracking locations of tokens resulting from macro expansion Dodji Seketeli
                   ` (4 preceding siblings ...)
  2011-10-17 10:25 ` [PATCH 1/6] Linemap infrastructure for virtual locations Dodji Seketeli
@ 2011-10-17 10:26 ` Dodji Seketeli
  2011-10-19 14:48   ` Ulrich Weigand
  5 siblings, 1 reply; 24+ messages in thread
From: Dodji Seketeli @ 2011-10-17 10:26 UTC (permalink / raw)
  To: gcc-patches; +Cc: jason, tromey, gdr, joseph, burnus, charlet

This second instalment uses the infrastructure of the previous patch
to allocate a macro map for each macro expansion and assign a virtual
location to each token resulting from the expansion.

To date when cpp_get_token comes across a token that happens to be a
macro, the macro expander kicks in, expands the macro, pushes the
resulting tokens onto a "token context" and returns a dummy padding
token. The next call to cpp_get_token goes look into the token context
for the next token [which is going to result from the previous macro
expansion] and returns it.  If the token is a macro, the macro expander
kicks in and you know the story.

This patch piggy-backs on that macro expansion process, so to speak.
First it modifies the macro expander to make it create a macro map for
each macro expansion. It then allocates a virtual location for each
resulting token.  Virtual locations of tokens resulting from macro
expansions are then stored on a special kind of context called an
"expanded tokens context".  In other words, in an expanded tokens
context, there are tokens resulting from macro expansion and their
associated virtual locations.  cpp_get_token_with_location is modified
to return the virtual location of tokens resulting from macro
expansion.  Note that once all tokens from an expanded token context have
been consumed and the context and is freed, the memory used to store the
virtual locations of the tokens held in that context is freed as well.
This helps reducing the overall peak memory consumption.

The client code that was getting macro expansion point location from
cpp_get_token_with_location now gets virtual location from it. Those
virtual locations can in turn be resolved into the different
interesting physical locations thanks to the linemap API exposed by
the previous patch.

Expensive progress. Possibly. So this whole virtual location
allocation business is switched off by default. So by default no
extended token is created. No extended token context is created
either. One has to use -ftrack-macro-expansion to switch this on. This
complicates the code but I believe it can be useful as some of our
friends found out at http://llvm.org/bugs/show_bug.cgi?id=5610

The patch tries to reduce the memory consumption by freeing some token
context memory that was being reused before. I didn't notice any
compilation slow down due to this immediate freeing on my GNU/Linux
system.

As no client code tries to resolve virtual locations to anything but
what was being done before, no new test case has been added.

gcc/ChangeLog
2011-10-15  Tom Tromey  <tromey@redhat.com>
	    Dodji Seketeli  <dodji@redhat.com>

	* doc/cppopts.texi (-ftrack-macro-expansion): Document new option.
	* doc/invoke.texi (-ftrack-macro-expansion): Add this to the list of
	preprocessor related options.

gcc/c-family/ChangeLog
2011-10-15  Tom Tromey  <tromey@redhat.com>
 	    Dodji Seketeli  <dodji@redhat.com>
 
	* c.opt (ftrack-macro-expansion): New option. Handle it with and
	without argument.
	* c-opts.c (c_common_handle_option)<case
	OPT_ftrack_macro_expansion_, case OPT_ftrack_macro_expansion>: New
	cases. Handle -ftrack-macro-expansion with and without argument.

libcpp/ChangeLog
2011-10-15  Tom Tromey  <tromey@redhat.com>
	    Dodji Seketeli  <dodji@redhat.com>

	* include/cpplib.h (struct cpp_options)<track_macro_expansion>:
	New option.
	* internal.h (struct macro_context): New struct.
	(enum context_tokens_kind): New enum.
	(struct cpp_context)<tokens_kind>: New member of type enum
	context_tokens_kind.
	(struct cpp_context)<macro>: Remove this.  Replace it with an enum
	of macro and  macro_context.
	(struct cpp_context)<direct_p>: Remove.
	(_cpp_remaining_tokens_num_in_context): Declare new function.
	* directives.c (destringize_and_run): Adjust.
	* lex.c (_cpp_remaining_tokens_num_in_context)
	(_cpp_token_from_context_at): Define new functions
	(cpp_peek_token): Use them.
	* init.c (cpp_create_reader): Initialize the base context to zero.
	(_cpp_token_from_context_at): Define new static function.
	(cpp_peek_token): Use new _cpp_remaining_tokens_num_in_context and
	_cpp_token_from_context_at.
	* macro.c (struct macro_arg)<virt_locs, expanded_virt_locs>: New
	members.
	(enum macro_arg_token_kind): New enum.
	(struct macro_arg_token_iter): New struct.
	(maybe_adjust_loc_for_trad_cpp, push_extended_tokens_context)
	(alloc_expanded_arg_mem, ensure_expanded_arg_room)
	(delete_macro_args, set_arg_token, get_arg_token_location)
	(arg_token_ptr_at, macro_arg_token_iter_init)
	(macro_arg_token_iter_get_token)
	(macro_arg_token_iter_get_location, macro_arg_token_iter_forward)
	(expanded_token_index, tokens_buff_new, tokens_buff_count)
	(tokens_buff_last_token_ptr, tokens_buff_put_token_to)
	(tokens_buff_add_token, tokens_buff_remove_last_token)
	(reached_end_of_context, consume_next_token_from_context): New
	static functions.
	(cpp_get_token_1): New static function. Split and extended from
	cpp_get_token.  Use reached_end_of_context and
	consume_next_token_from_context.  Unify its return point.  Move
	the location tweaking from cpp_get_token_with_location in here.
	(cpp_get_token): Use cpp_get_token_1
	(stringify_arg): Use the new arg_token_at.
	(paste_all_tokens): Support tokens coming from extended tokens
	contexts.
	(collect_args): Return the number of collected arguments, by
	parameter.  Store virtual locations of tokens that constitute the
	collected args.
	(funlike_invocation_p): Return the number of collected arguments,
	by parameter.
	(enter_macro_context): Add a parameter for macro expansion point.
	Pass it to replace_args and to the "used" cpp callback.  Get the
	number of function-like macro arguments from funlike_invocation_p,
	pass it to the new delete_macro_args to free the memory used by
	macro args.  When -ftrack-macro-expansion is in effect, for macros
	that have no arguments, create a macro map for the macro expansion
	and use it to allocate proper virtual locations for tokens
	resulting from the expansion.  Push an extended tokens context
	containing the tokens resulting from macro expansion and their
	virtual locations.
	(replace_args): Rename the different variables named 'count' into
	variables with more meaningful names.  Create a macro map;
	allocate virtual locations of tokens resulting from this
	expansion.  Use macro_arg_token_iter to iterate over tokens of a
	given macro.  Handle the case of the argument of
	-ftrack-macro-expansion being < 2.  Don't free macro arguments
	memory resulting from expand_arg here, as these are freed by the
	caller of replace_arg using delete_macro_args now.  Push extended
	token context.
	(next_context, push_ptoken_context, _cpp_push_token_context)
	(_cpp_push_text_context): Properly initialize the context.
	(expand_arg): Use the new alloc_expanded_arg_mem,
	push_extended_tokens_context, cpp_get_token_1, and set_arg_token.
	(_cpp_pop_context): Really free the memory held by the context.
	Handle freeing memory used by extended tokens contexts.
	(cpp_get_token_with_location): Use cpp_get_token_1.
	(cpp_sys_macro_p): Adjust.
	(_cpp_backup_tokens): Support the new kinds of token contexts.
	* traditional.c (recursive_macro): Adjust.

---
 gcc/ChangeLog             |    7 +
 gcc/c-family/ChangeLog    |    9 +
 gcc/c-family/c-opts.c     |   12 +
 gcc/c-family/c.opt        |    8 +
 gcc/doc/cppopts.texi      |   18 +
 gcc/doc/invoke.texi       |    6 +-
 gcc/input.c               |    2 +-
 libcpp/ChangeLog          |   79 +++
 libcpp/directives.c       |    4 +-
 libcpp/include/cpplib.h   |    8 +
 libcpp/include/line-map.h |    2 +-
 libcpp/init.c             |    3 +-
 libcpp/internal.h         |   58 ++-
 libcpp/lex.c              |   41 ++-
 libcpp/line-map.c         |    2 +-
 libcpp/macro.c            | 1326 ++++++++++++++++++++++++++++++++++++++++-----
 libcpp/traditional.c      |    2 +-
 17 files changed, 1434 insertions(+), 153 deletions(-)

diff --git a/gcc/c-family/c-opts.c b/gcc/c-family/c-opts.c
index 49ff80d..3184539 100644
--- a/gcc/c-family/c-opts.c
+++ b/gcc/c-family/c-opts.c
@@ -628,6 +628,18 @@ c_common_handle_option (size_t scode, const char *arg, int value,
       cpp_opts->preprocessed = value;
       break;
 
+    case OPT_ftrack_macro_expansion:
+      if (value)
+	value = 2;
+      /* Fall Through.  */
+
+    case OPT_ftrack_macro_expansion_:
+      if (arg && *arg != '\0')
+	cpp_opts->track_macro_expansion = value;
+      else
+	cpp_opts->track_macro_expansion = 2;
+      break;
+
     case OPT_frepo:
       flag_use_repository = value;
       if (value)
diff --git a/gcc/c-family/c.opt b/gcc/c-family/c.opt
index e6ac5dc..07a6b87 100644
--- a/gcc/c-family/c.opt
+++ b/gcc/c-family/c.opt
@@ -941,6 +941,14 @@ fpreprocessed
 C ObjC C++ ObjC++
 Treat the input file as already preprocessed
 
+ftrack-macro-expansion
+C ObjC C++ ObjC++ JoinedOrMissing RejectNegative UInteger
+; converted into ftrack-macro-expansion=
+
+ftrack-macro-expansion=
+C ObjC C++ ObjC++ JoinedOrMissing RejectNegative UInteger
+-ftrack-macro-expansion=<0|1|2>  Track locations of tokens coming from macro expansion and display them in error messages
+
 fpretty-templates
 C++ ObjC++ Var(flag_pretty_templates) Init(1)
 -fno-pretty-templates Do not pretty-print template specializations as the template signature followed by the arguments
diff --git a/gcc/doc/cppopts.texi b/gcc/doc/cppopts.texi
index 5212478..b225236 100644
--- a/gcc/doc/cppopts.texi
+++ b/gcc/doc/cppopts.texi
@@ -583,6 +583,24 @@ correct column numbers in warnings or errors, even if tabs appear on the
 line.  If the value is less than 1 or greater than 100, the option is
 ignored.  The default is 8.
 
+@item -ftrack-macro-expansion@r{[}=@var{level}@r{]}
+@opindex ftrack-macro-expansion
+Track locations of tokens across macro expansions. This allows the
+compiler to emit diagnostic about the current macro expansion stack
+when a compilation error occurs in a macro expansion. Using this
+option makes the preprocessor and the compiler consume more
+memory. The @var{level} parameter can be used to choose the level of
+precision of token location tracking thus decreasing the memory
+consumption if necessary. Value @samp{0} of @var{level} de-activates
+this option just as if no @option{-ftrack-macro-expansion} was present
+on the command line. Value @samp{1} tracks tokens locations in a
+degraded mode for the sake of minimal memory overhead. In this mode
+all tokens resulting from the expansion of an argument of a
+function-like macro have the same location. Value @samp{2} tracks
+tokens locations completely. This value is the most memory hungry.
+When this option is given no argument, the default parameter value is
+@samp{2}.
+
 @item -fexec-charset=@var{charset}
 @opindex fexec-charset
 @cindex character set, execution
diff --git a/gcc/doc/invoke.texi b/gcc/doc/invoke.texi
index ef7ac68..3c841ef 100644
--- a/gcc/doc/invoke.texi
+++ b/gcc/doc/invoke.texi
@@ -428,9 +428,9 @@ Objective-C and Objective-C++ Dialects}.
 -iwithprefixbefore @var{dir}  -isystem @var{dir} @gol
 -imultilib @var{dir} -isysroot @var{dir} @gol
 -M  -MM  -MF  -MG  -MP  -MQ  -MT  -nostdinc  @gol
--P  -fworking-directory  -remap @gol
--trigraphs  -undef  -U@var{macro}  -Wp,@var{option} @gol
--Xpreprocessor @var{option}}
+-P -ftrack-macro-expansion -fworking-directory @gol
+-remap -trigraphs  -undef  -U@var{macro}  @gol
+-Wp,@var{option} -Xpreprocessor @var{option}}
 
 @item Assembler Option
 @xref{Assembler Options,,Passing Options to the Assembler}.
diff --git a/gcc/input.c b/gcc/input.c
index 83344d7..89af274 100644
--- a/gcc/input.c
+++ b/gcc/input.c
@@ -1,5 +1,5 @@
 /* Data and functions related to line maps and input files.
-   Copyright (C) 2004, 2007, 2008, 2009, 2010
+   Copyright (C) 2004, 2007, 2008, 2009, 2010, 2011
    Free Software Foundation, Inc.
 
 This file is part of GCC.
diff --git a/libcpp/directives.c b/libcpp/directives.c
index a62ddeb..0510c6e 100644
--- a/libcpp/directives.c
+++ b/libcpp/directives.c
@@ -1,7 +1,7 @@
 /* CPP Library. (Directive handling.)
    Copyright (C) 1986, 1987, 1989, 1992, 1993, 1994, 1995, 1996, 1997, 1998,
    1999, 2000, 2001, 2002, 2003, 2004, 2005,
-   2007, 2008, 2009, 2010 Free Software Foundation, Inc.
+   2007, 2008, 2009, 2010, 2011 Free Software Foundation, Inc.
    Contributed by Per Bothner, 1994-95.
    Based on CCCP program by Paul Rubin, June 1986
    Adapted to ANSI C, Richard Stallman, Jan 1987
@@ -1742,7 +1742,7 @@ destringize_and_run (cpp_reader *pfile, const cpp_string *in)
   saved_cur_run = pfile->cur_run;
 
   pfile->context = XNEW (cpp_context);
-  pfile->context->macro = 0;
+  pfile->context->c.macro = 0;
   pfile->context->prev = 0;
   pfile->context->next = 0;
 
diff --git a/libcpp/include/cpplib.h b/libcpp/include/cpplib.h
index 0e90821..3e01c11 100644
--- a/libcpp/include/cpplib.h
+++ b/libcpp/include/cpplib.h
@@ -393,6 +393,14 @@ struct cpp_options
      bother trying to do macro expansion and whatnot.  */
   unsigned char preprocessed;
 
+  /* Nonzero means we are tracking locations of tokens involved in
+     macro expansion. 1 Means we track the location in degraded mode
+     where we do not track locations of tokens resulting from the
+     expansion of arguments of function-like macro.  2 Means we do
+     track all macro expansions. This last option is the one that
+     consumes the highest amount of memory.  */
+  unsigned char track_macro_expansion;
+
   /* Nonzero means handle C++ alternate operator names.  */
   unsigned char operator_names;
 
diff --git a/libcpp/include/line-map.h b/libcpp/include/line-map.h
index d3c52b2..724f3f0 100644
--- a/libcpp/include/line-map.h
+++ b/libcpp/include/line-map.h
@@ -1,5 +1,5 @@
 /* Map logical line numbers to (source file, line number) pairs.
-   Copyright (C) 2001, 2003, 2004, 2007, 2008, 2009, 2010
+   Copyright (C) 2001, 2003, 2004, 2007, 2008, 2009, 2010, 2011
    Free Software Foundation, Inc.
 
 This program is free software; you can redistribute it and/or modify it
diff --git a/libcpp/init.c b/libcpp/init.c
index 6303868..6771e63 100644
--- a/libcpp/init.c
+++ b/libcpp/init.c
@@ -154,6 +154,7 @@ cpp_create_reader (enum c_lang lang, hash_table *table,
   init_library ();
 
   pfile = XCNEW (cpp_reader);
+  memset (&pfile->base_context, 0, sizeof (pfile->base_context));
 
   cpp_set_lang (pfile, lang);
   CPP_OPTION (pfile, warn_multichar) = 1;
@@ -213,7 +214,7 @@ cpp_create_reader (enum c_lang lang, hash_table *table,
 
   /* Initialize the base context.  */
   pfile->context = &pfile->base_context;
-  pfile->base_context.macro = 0;
+  pfile->base_context.c.macro = 0;
   pfile->base_context.prev = pfile->base_context.next = 0;
 
   /* Aligned and unaligned storage.  */
diff --git a/libcpp/internal.h b/libcpp/internal.h
index 65bfa1d..6fb2606 100644
--- a/libcpp/internal.h
+++ b/libcpp/internal.h
@@ -1,6 +1,6 @@
 /* Part of CPP library.
    Copyright (C) 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2007,
-   2008, 2009, 2010 Free Software Foundation, Inc.
+   2008, 2009, 2010, 2011 Free Software Foundation, Inc.
 
 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
@@ -139,6 +139,40 @@ struct tokenrun
 #define CUR(c) ((c)->u.trad.cur)
 #define RLIMIT(c) ((c)->u.trad.rlimit)
 
+/* This describes some additional data that is added to the macro
+   token context of type cpp_context, when -ftrack-macro-expansion is
+   on.  */
+typedef struct
+{
+  /* The node of the macro we are referring to.  */
+  cpp_hashnode *macro_node;
+  /* This buffer contains an array of virtual locations.  The virtual
+     location at index 0 is the virtual location of the token at index
+     0 in the current instance of cpp_context; similarly for all the
+     other virtual locations.  */
+  source_location *virt_locs;
+  /* This is a pointer to the current virtual location.  This is used
+     to iterate over the virtual locations while we iterate over the
+     tokens they belong to.  */
+  source_location *cur_virt_loc;
+} macro_context;
+
+/* The kind of tokens carried by a cpp_context.  */
+enum context_tokens_kind {
+  /* This is the value of cpp_context::tokens_kind if u.iso.first
+     contains an instance of cpp_token **.  */
+  TOKENS_KIND_INDIRECT,
+  /* This is the value of cpp_context::tokens_kind if u.iso.first
+     contains an instance of cpp_token *.  */
+  TOKENS_KIND_DIRECT,
+  /* This is the value of cpp_context::tokens_kind when the token
+     context contains tokens resulting from macro expansion.  In that
+     case struct cpp_context::macro points to an instance of struct
+     macro_context.  This is used only when the
+     -ftrack-macro-expansion flag is on.  */
+  TOKENS_KIND_EXTENDED
+};
+
 typedef struct cpp_context cpp_context;
 struct cpp_context
 {
@@ -168,11 +202,24 @@ struct cpp_context
      When the context is popped, the buffer is released.  */
   _cpp_buff *buff;
 
-  /* For a macro context, the macro node, otherwise NULL.  */
-  cpp_hashnode *macro;
+  /* If tokens_kind is TOKEN_KIND_EXTENDED, then (as we thus are in a
+     macro context) this is a pointer to an instance of macro_context.
+     Otherwise if tokens_kind is *not* TOKEN_KIND_EXTENDED, then, if
+     we are in a macro context, this is a pointer to an instance of
+     cpp_hashnode, representing the name of the macro this context is
+     for.  If we are not in a macro context, then this is just NULL.
+     Note that when tokens_kind is TOKEN_KIND_EXTENDED, the memory
+     used by the instance of macro_context pointed to by this member
+     is de-allocated upon de-allocation of the instance of struct
+     cpp_context.  */
+  union
+  {
+    macro_context *mc;
+    cpp_hashnode *macro;
+  } c;
 
-  /* True if utoken element is token, else ptoken.  */
-  bool direct_p;
+  /* This determines the type of tokens held by this context.  */
+  enum context_tokens_kind tokens_kind;
 };
 
 struct lexer_state
@@ -605,6 +652,7 @@ extern cpp_token *_cpp_lex_direct (cpp_reader *);
 extern int _cpp_equiv_tokens (const cpp_token *, const cpp_token *);
 extern void _cpp_init_tokenrun (tokenrun *, unsigned int);
 extern cpp_hashnode *_cpp_lex_identifier (cpp_reader *, const char *);
+extern int _cpp_remaining_tokens_num_in_context (cpp_reader *);
 
 /* In init.c.  */
 extern void _cpp_maybe_push_include_file (cpp_reader *);
diff --git a/libcpp/lex.c b/libcpp/lex.c
index 75b2b1d..cd6ae9f 100644
--- a/libcpp/lex.c
+++ b/libcpp/lex.c
@@ -1703,6 +1703,38 @@ next_tokenrun (tokenrun *run)
   return run->next;
 }
 
+/* Return the number of not yet processed token in the the current
+   context.  */
+int
+_cpp_remaining_tokens_num_in_context (cpp_reader *pfile)
+{
+  cpp_context *context = pfile->context;
+  if (context->tokens_kind == TOKENS_KIND_DIRECT)
+    return ((LAST (context).token - FIRST (context).token)
+	    / sizeof (cpp_token));
+  else if (context->tokens_kind == TOKENS_KIND_INDIRECT
+	   || context->tokens_kind == TOKENS_KIND_EXTENDED)
+    return ((LAST (context).ptoken - FIRST (context).ptoken)
+	    / sizeof (cpp_token *));
+  else
+      abort ();
+}
+
+/* Returns the token present at index INDEX in the current context.
+   If INDEX is zero, the next token to be processed is returned.  */
+static const cpp_token*
+_cpp_token_from_context_at (cpp_reader *pfile, int index)
+{
+  cpp_context *context = pfile->context;
+  if (context->tokens_kind == TOKENS_KIND_DIRECT)
+    return &(FIRST (context).token[index]);
+  else if (context->tokens_kind == TOKENS_KIND_INDIRECT
+	   || context->tokens_kind == TOKENS_KIND_EXTENDED)
+    return FIRST (context).ptoken[index];
+ else
+   abort ();
+}
+
 /* Look ahead in the input stream.  */
 const cpp_token *
 cpp_peek_token (cpp_reader *pfile, int index)
@@ -1714,15 +1746,10 @@ cpp_peek_token (cpp_reader *pfile, int index)
   /* First, scan through any pending cpp_context objects.  */
   while (context->prev)
     {
-      ptrdiff_t sz = (context->direct_p
-                      ? LAST (context).token - FIRST (context).token
-                      : LAST (context).ptoken - FIRST (context).ptoken);
+      ptrdiff_t sz = _cpp_remaining_tokens_num_in_context (pfile);
 
       if (index < (int) sz)
-        return (context->direct_p
-                ? FIRST (context).token + index
-                : *(FIRST (context).ptoken + index));
-
+        return _cpp_token_from_context_at (pfile, index);
       index -= (int) sz;
       context = context->prev;
     }
diff --git a/libcpp/line-map.c b/libcpp/line-map.c
index 49de244..fe07c16 100644
--- a/libcpp/line-map.c
+++ b/libcpp/line-map.c
@@ -1,5 +1,5 @@
 /* Map logical line numbers to (source file, line number) pairs.
-   Copyright (C) 2001, 2003, 2004, 2007, 2008, 2009
+   Copyright (C) 2001, 2003, 2004, 2007, 2008, 2009, 2010, 2011
    Free Software Foundation, Inc.
 
 This program is free software; you can redistribute it and/or modify it
diff --git a/libcpp/macro.c b/libcpp/macro.c
index defc486..d760383 100644
--- a/libcpp/macro.c
+++ b/libcpp/macro.c
@@ -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 Free Software Foundation, Inc.
+   2006, 2007, 2008, 2009, 2010, 2011 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
@@ -30,6 +30,10 @@ along with this program; see the file COPYING3.  If not see
 #include "internal.h"
 
 typedef struct macro_arg macro_arg;
+/* This structure represents the tokens of a macro argument.  These
+   tokens can be macro themselves, in which case they can be either
+   expanded or unexpanded.  When they are expanded, this data
+   structure keeps both the expanded and unexpanded forms.  */
 struct macro_arg
 {
   const cpp_token **first;	/* First token in unexpanded argument.  */
@@ -37,17 +41,59 @@ 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
+				   unexpanded tokens are stored.  */
+  source_location *expanded_virt_locs; /* Where virtual locations for
+					  expanded tokens are
+					  stored.  */
+};
+
+/* The kind of macro tokens which the instance of
+   macro_arg_token_iter is supposed to iterate over.  */
+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.  */
+  MACRO_ARG_TOKEN_STRINGIFIED,
+  /* This is a token resulting from the expansion of a macro
+     argument that was itself a macro.  */
+  MACRO_ARG_TOKEN_EXPANDED
+};
+
+/* An iterator over tokens coming from a function-like macro
+   argument.  */
+typedef struct macro_arg_token_iter macro_arg_token_iter;
+struct macro_arg_token_iter
+{
+  /* Whether or not -ftrack-macro-expansion is used.  */
+  bool track_macro_exp_p;
+  /* The kind of token over which we are supposed to iterate.  */
+  enum macro_arg_token_kind kind;
+  /* 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
+     macro expansion.  */
+  const source_location *location_ptr;
+#ifdef ENABLE_CHECKING
+  /* The number of times the iterator went forward. This useful only
+     when checking is enabled.  */
+  size_t num_forwards;
+#endif
 };
 
 /* Macro expansion.  */
 
 static int enter_macro_context (cpp_reader *, cpp_hashnode *,
-				const cpp_token *);
+				const cpp_token *, source_location);
 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 void push_extended_tokens_context (cpp_reader *, cpp_hashnode *,
+					  _cpp_buff *, source_location *,
+					  const cpp_token **, unsigned int);
 static _cpp_buff *collect_args (cpp_reader *, const cpp_hashnode *,
-				_cpp_buff **);
+				_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 *);
@@ -55,10 +101,53 @@ 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 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,
+			   enum macro_arg_token_kind,
+			   bool);
+static const source_location *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);
+
+static void macro_arg_token_iter_init (macro_arg_token_iter *, bool,
+				       enum macro_arg_token_kind,
+				       const macro_arg *,
+				       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
+(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 **);
+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 const cpp_token **tokens_buff_add_token (_cpp_buff *,
+						source_location *,
+						const cpp_token *,
+						source_location,
+						source_location,
+						const struct line_map *,
+						unsigned int);
+static void tokens_buff_remove_last_token (_cpp_buff *);
 static void replace_args (cpp_reader *, cpp_hashnode *, cpp_macro *,
-			  macro_arg *);
+			  macro_arg *, source_location);
 static _cpp_buff *funlike_invocation_p (cpp_reader *, cpp_hashnode *,
-					_cpp_buff **);
+					_cpp_buff **, unsigned *);
 static bool create_iso_definition (cpp_reader *, cpp_macro *);
 
 /* #define directive parsing and handling.  */
@@ -70,6 +159,11 @@ static bool warn_of_redefinition (cpp_reader *, cpp_hashnode *,
 static bool parse_params (cpp_reader *, cpp_macro *);
 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 *);
 
 /* Emits a warning if NODE is a macro defined in the main file that
    has not been used.  */
@@ -511,7 +605,7 @@ paste_tokens (cpp_reader *pfile, const cpp_token **plhs, const cpp_token *rhs)
 static void
 paste_all_tokens (cpp_reader *pfile, const cpp_token *lhs)
 {
-  const cpp_token *rhs;
+  const cpp_token *rhs = NULL;
   cpp_context *context = pfile->context;
 
   do
@@ -521,10 +615,25 @@ paste_all_tokens (cpp_reader *pfile, const cpp_token *lhs)
 	 object-like macro, or a function-like macro with arguments
 	 inserted.  In either case, the constraints to #define
 	 guarantee we have at least one more token.  */
-      if (context->direct_p)
+      if (context->tokens_kind == TOKENS_KIND_DIRECT)
 	rhs = FIRST (context).token++;
-      else
+      else if (context->tokens_kind == TOKENS_KIND_INDIRECT)
 	rhs = *FIRST (context).ptoken++;
+      else if (context->tokens_kind == TOKENS_KIND_EXTENDED)
+	{
+	  /* So we are in presence of an extended token context, which
+	     means that each token in this context has a virtual
+	     location attached to it.  So let's not forget to update
+	     the pointer to the current virtual location of the
+	     current token when we update the pointer to the current
+	     token */
+
+	  rhs = *FIRST (context).ptoken++;
+	  /* context->c.mc must be non-null, as if we were not in a
+	     macro context, context->tokens_kind could not be equal to
+	     TOKENS_KIND_EXTENDED.  */
+	  context->c.mc->cur_virt_loc++;
+	}
 
       if (rhs->type == CPP_PADDING)
 	{
@@ -588,23 +697,37 @@ _cpp_arguments_ok (cpp_reader *pfile, cpp_macro *macro, const cpp_hashnode *node
    NULL.  Each argument is terminated by a CPP_EOF token, for the
    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.  */
+   CPP_PRAGMA ... CPP_PRAGMA_EOL tokens into *PRAGMA_BUFF buffer.
+
+   What is returned is the buffer that contains the memory allocated
+   to hold the macro arguments.  NODE is the name of the macro this
+   function is dealing with.  If NUM_ARGS is non-NULL, *NUM_ARGS is
+   set to the actual number of macro arguments allocated in the
+   returned buffer.  */
 static _cpp_buff *
 collect_args (cpp_reader *pfile, const cpp_hashnode *node,
-	      _cpp_buff **pragma_buff)
+	      _cpp_buff **pragma_buff, unsigned *num_args)
 {
   _cpp_buff *buff, *base_buff;
   cpp_macro *macro;
   macro_arg *args, *arg;
   const cpp_token *token;
   unsigned int argc;
+  source_location virt_loc;
+  bool track_macro_expansion_p = CPP_OPTION (pfile, track_macro_expansion);
+  unsigned num_args_alloced = 0;
 
   macro = node->value.macro;
   if (macro->paramc)
     argc = macro->paramc;
   else
     argc = 1;
-  buff = _cpp_get_buff (pfile, argc * (50 * sizeof (cpp_token *)
+
+#define DEFAULT_NUM_TOKENS_PER_MACRO_ARG 50
+#define ARG_TOKENS_EXTENT 1000
+
+  buff = _cpp_get_buff (pfile, argc * (DEFAULT_NUM_TOKENS_PER_MACRO_ARG
+				       * sizeof (cpp_token *)
 				       + sizeof (macro_arg)));
   base_buff = buff;
   args = (macro_arg *) buff->base;
@@ -619,9 +742,17 @@ collect_args (cpp_reader *pfile, const cpp_hashnode *node,
     {
       unsigned int paren_depth = 0;
       unsigned int ntokens = 0;
+      unsigned virt_locs_capacity = DEFAULT_NUM_TOKENS_PER_MACRO_ARG;
+      num_args_alloced++;
 
       argc++;
       arg->first = (const cpp_token **) buff->cur;
+      if (track_macro_expansion_p)
+	{
+	  virt_locs_capacity = DEFAULT_NUM_TOKENS_PER_MACRO_ARG;
+	  arg->virt_locs = XNEWVEC (source_location,
+				    virt_locs_capacity);
+	}
 
       for (;;)
 	{
@@ -629,11 +760,20 @@ collect_args (cpp_reader *pfile, const cpp_hashnode *node,
 	  if ((unsigned char *) &arg->first[ntokens + 2] > buff->limit)
 	    {
 	      buff = _cpp_append_extend_buff (pfile, buff,
-					      1000 * sizeof (cpp_token *));
+					      ARG_TOKENS_EXTENT
+					      * sizeof (cpp_token *));
 	      arg->first = (const cpp_token **) buff->cur;
 	    }
+	  if (track_macro_expansion_p
+	      && (ntokens + 2 > virt_locs_capacity))
+	    {
+	      virt_locs_capacity += ARG_TOKENS_EXTENT;
+	      arg->virt_locs = XRESIZEVEC (source_location,
+					   arg->virt_locs,
+					   virt_locs_capacity);
+	    }
 
-	  token = cpp_get_token (pfile);
+	  token = cpp_get_token_1 (pfile, &virt_loc);
 
 	  if (token->type == CPP_PADDING)
 	    {
@@ -690,7 +830,7 @@ collect_args (cpp_reader *pfile, const cpp_hashnode *node,
 		  BUFF_FRONT (*pragma_buff) += sizeof (cpp_token *);
 		  if (token->type == CPP_PRAGMA_EOL)
 		    break;
-		  token = cpp_get_token (pfile);
+		  token = cpp_get_token_1 (pfile, &virt_loc);
 		}
 	      while (token->type != CPP_EOF);
 
@@ -704,8 +844,10 @@ collect_args (cpp_reader *pfile, const cpp_hashnode *node,
 	      else
 		continue;
 	    }
-
-	  arg->first[ntokens++] = token;
+	  set_arg_token (arg, token, virt_loc,
+			 ntokens, MACRO_ARG_TOKEN_NORMAL,
+			 CPP_OPTION (pfile, track_macro_expansion));
+	  ntokens++;
 	}
 
       /* Drop trailing padding.  */
@@ -713,7 +855,9 @@ collect_args (cpp_reader *pfile, const cpp_hashnode *node,
 	ntokens--;
 
       arg->count = ntokens;
-      arg->first[ntokens] = &pfile->eof;
+      set_arg_token (arg, &pfile->eof, pfile->eof.src_loc,
+		     ntokens, MACRO_ARG_TOKEN_NORMAL,
+		     CPP_OPTION (pfile, track_macro_expansion));
 
       /* Terminate the argument.  Excess arguments loop back and
 	 overwrite the final legitimate argument, before failing.  */
@@ -756,6 +900,8 @@ collect_args (cpp_reader *pfile, const cpp_hashnode *node,
 				  || (argc == 1 && args[0].count == 0
 				      && !CPP_OPTION (pfile, std))))
 	    args[macro->paramc - 1].first = NULL;
+	  if (num_args)
+	    *num_args = num_args_alloced;
 	  return base_buff;
 	}
     }
@@ -769,10 +915,12 @@ collect_args (cpp_reader *pfile, const cpp_hashnode *node,
    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.  PRAGMA_BUFF
-   argument is the same as in collect_args.  */
+   argument is the same as in collect_args.  If NUM_ARGS is non-NULL,
+   *NUM_ARGS is set to the number of arguments contained in the
+   returned buffer.  */
 static _cpp_buff *
 funlike_invocation_p (cpp_reader *pfile, cpp_hashnode *node,
-		      _cpp_buff **pragma_buff)
+		      _cpp_buff **pragma_buff, unsigned *num_args)
 {
   const cpp_token *token, *padding = NULL;
 
@@ -789,7 +937,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, pragma_buff);
+      return collect_args (pfile, node, pragma_buff, num_args);
     }
 
   /* CPP_EOF can be the end of macro arguments, or the end of the
@@ -823,13 +971,15 @@ macro_real_token_count (const cpp_macro *macro)
 /* 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.
-   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.  */
+   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. LOCATION is the location of the expansion point of the
+   macro.  */
 static int
 enter_macro_context (cpp_reader *pfile, cpp_hashnode *node,
-		     const cpp_token *result)
+		     const cpp_token *result, source_location location)
 {
   /* The presence of a macro invalidates a file's controlling macro.  */
   pfile->mi_valid = false;
@@ -854,11 +1004,13 @@ enter_macro_context (cpp_reader *pfile, cpp_hashnode *node,
       if (macro->fun_like)
 	{
 	  _cpp_buff *buff;
+	  unsigned num_args = 0;
 
 	  pfile->state.prevent_expansion++;
 	  pfile->keep_tokens++;
 	  pfile->state.parsing_args = 1;
-	  buff = funlike_invocation_p (pfile, node, &pragma_buff);
+	  buff = funlike_invocation_p (pfile, node, &pragma_buff,
+				       &num_args);
 	  pfile->state.parsing_args = 0;
 	  pfile->keep_tokens--;
 	  pfile->state.prevent_expansion--;
@@ -877,8 +1029,13 @@ enter_macro_context (cpp_reader *pfile, cpp_hashnode *node,
 	    }
 
 	  if (macro->paramc > 0)
-	    replace_args (pfile, node, macro, (macro_arg *) buff->base);
-	  _cpp_release_buff (pfile, buff);
+	    replace_args (pfile, node, macro,
+			  (macro_arg *) buff->base,
+			  location);
+	  /* Free the memory used by the arguments of this
+	     function-like macro.  This memory has been allocated by
+	     funlike_invocation_p and by replace_args.  */
+	  delete_macro_args (buff, num_args);
 	}
 
       /* Disable the macro within its expansion.  */
@@ -892,13 +1049,44 @@ enter_macro_context (cpp_reader *pfile, cpp_hashnode *node,
 	}
 
       if (pfile->cb.used)
-	pfile->cb.used (pfile, result->src_loc, node);
+	pfile->cb.used (pfile, location, node);
 
       macro->used = 1;
 
       if (macro->paramc == 0)
-	_cpp_push_token_context (pfile, node, macro->exp.tokens,
-				 macro_real_token_count (macro));
+	{
+	  if (CPP_OPTION (pfile, track_macro_expansion))
+	    {
+	      unsigned int i, count = macro->count;
+	      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);
+
+	      /* 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)
+		{
+		  tokens_buff_add_token (macro_tokens, virt_locs,
+					 src, src->src_loc,
+					 src->src_loc, map, i);
+		  ++src;
+		}
+	      push_extended_tokens_context (pfile, node,
+					    macro_tokens,
+					    virt_locs,
+					    (const cpp_token **)
+					    macro_tokens->base,
+					    count);
+	    }
+	  else
+	    _cpp_push_token_context (pfile, node, macro->exp.tokens,
+				     macro_real_token_count (macro));
+	}
 
       if (pragma_buff)
 	{
@@ -926,33 +1114,314 @@ enter_macro_context (cpp_reader *pfile, cpp_hashnode *node,
   return builtin_macro (pfile, node);
 }
 
+/* De-allocate the memory used by BUFF which is an array of instances
+   of macro_arg.  NUM_ARGS is the number of instances of macro_arg
+   present in BUFF.  */
+static void
+delete_macro_args (_cpp_buff *buff, unsigned num_args)
+{
+  macro_arg *macro_args;
+  unsigned i;
+
+  if (buff == NULL)
+    return;
+
+  macro_args = (macro_arg *) buff->base;
+
+  /* Walk instances of macro_arg to free their expanded tokens as well
+     as their macro_arg::virt_locs members.  */
+  for (i = 0; i < num_args; ++i)
+    {
+      if (macro_args[i].expanded)
+	{
+	  free (macro_args[i].expanded);
+	  macro_args[i].expanded = NULL;
+	}
+      if (macro_args[i].virt_locs)
+	{
+	  free (macro_args[i].virt_locs);
+	  macro_args[i].virt_locs = NULL;
+	}
+      if (macro_args[i].expanded_virt_locs)
+	{
+	  free (macro_args[i].expanded_virt_locs);
+	  macro_args[i].expanded_virt_locs = NULL;
+	}
+    }
+  _cpp_free_buff (buff);
+}
+
+/* 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
+   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,
+	       enum macro_arg_token_kind kind,
+	       bool track_macro_exp_p)
+{
+  const cpp_token **token_ptr;
+  source_location *loc = NULL;
+
+  token_ptr =
+    arg_token_ptr_at (arg, index, kind,
+		      track_macro_exp_p ? &loc : NULL);
+  *token_ptr = 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
+      *loc = location;
+    }
+}
+
+/* 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 *
+get_arg_token_location (const macro_arg *arg,
+			enum macro_arg_token_kind kind)
+{
+  const source_location *loc = NULL;
+  const cpp_token **token_ptr =
+    arg_token_ptr_at (arg, 0, kind, (source_location **) &loc);
+
+  if (token_ptr == NULL)
+    return NULL;
+
+  return loc;
+}
+
+/* Return the pointer to the INDEXth token of the macro argument ARG.
+   KIND specifies the kind of token the macro argument ARG contains.
+   If VIRT_LOCATION is non NULL, *VIRT_LOCATION is set to the address
+   of the virtual location of the returned token if the
+   -ftrack-macro-expansion flag is on; otherwise, it's set to the
+   spelling location of the returned token.  */
+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)
+{
+  const cpp_token **tokens_ptr = NULL;
+
+  switch (kind)
+    {
+    case MACRO_ARG_TOKEN_NORMAL:
+      tokens_ptr = arg->first;
+      break;
+    case MACRO_ARG_TOKEN_STRINGIFIED:      
+      tokens_ptr = (const cpp_token **) &arg->stringified;
+      break;
+    case MACRO_ARG_TOKEN_EXPANDED:
+	tokens_ptr = arg->expanded;
+      break;
+    }
+
+  if (tokens_ptr == NULL)
+    /* This can happen for e.g, an empty token argument to a
+       funtion-like macro.  */
+    return tokens_ptr;
+
+  if (virt_location)
+    {
+      if (kind == MACRO_ARG_TOKEN_NORMAL)
+	*virt_location = &arg->virt_locs[index];
+      else if (kind == MACRO_ARG_TOKEN_EXPANDED)
+	*virt_location = &arg->expanded_virt_locs[index];
+      else if (kind == MACRO_ARG_TOKEN_STRINGIFIED)
+	*virt_location =
+	  (source_location *) &tokens_ptr[index]->src_loc;
+    }
+  return &tokens_ptr[index];
+}
+
+/* Initialize an iterator so that it iterates over the tokens of a
+   function-like macro argument.  KIND is the kind of tokens we want
+   ITER to iterate over. TOKEN_PTR points the first token ITER will
+   iterate over.  */
+static void
+macro_arg_token_iter_init (macro_arg_token_iter *iter,
+			   bool track_macro_exp_p,
+			   enum macro_arg_token_kind kind,
+			   const macro_arg *arg,
+			   const cpp_token **token_ptr)
+{
+  iter->track_macro_exp_p = track_macro_exp_p;
+  iter->kind = kind;
+  iter->token_ptr = token_ptr;
+  if (track_macro_exp_p)
+    iter->location_ptr = get_arg_token_location (arg, kind);
+#ifdef ENABLE_CHECKING
+  iter->num_forwards = 0;
+  if (track_macro_exp_p
+      && token_ptr != NULL
+      && iter->location_ptr == NULL)
+    abort ();
+#endif
+}
+
+/* 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
+   string.  */
+static void
+macro_arg_token_iter_forward (macro_arg_token_iter *it)
+{
+  switch (it->kind)
+    {
+    case MACRO_ARG_TOKEN_NORMAL:
+    case MACRO_ARG_TOKEN_EXPANDED:
+      it->token_ptr++;
+      if (it->track_macro_exp_p)
+	it->location_ptr++;
+      break;
+    case MACRO_ARG_TOKEN_STRINGIFIED:
+#ifdef ENABLE_CHECKING
+      if (it->num_forwards > 0)
+	abort ();
+#endif
+      break;
+    }
+
+#ifdef ENABLE_CHECKING
+  it->num_forwards++;
+#endif
+}
+
+/* Return the token pointed to by the iterator.  */
+static const cpp_token *
+macro_arg_token_iter_get_token (const macro_arg_token_iter *it)
+{
+#ifdef ENABLE_CHECKING
+  if (it->kind == MACRO_ARG_TOKEN_STRINGIFIED
+      && it->num_forwards > 0)
+    abort ();
+#endif
+  if (it->token_ptr == NULL)
+    return NULL;
+  return *it->token_ptr;
+}
+
+/* Return the location of the token pointed to by the iterator.*/
+static source_location
+macro_arg_token_iter_get_location (const macro_arg_token_iter *it)
+{
+#ifdef ENABLE_CHECKING
+  if (it->kind == MACRO_ARG_TOKEN_STRINGIFIED
+      && it->num_forwards > 0)
+    abort ();
+#endif
+  if (it->track_macro_exp_p)
+    return *it->location_ptr;
+  else
+    return (*it->token_ptr)->src_loc;
+}
+
+/* Return the index of a token [resulting from macro expansion] inside
+   the total list of tokens resulting from a given macro
+   expansion. The index can be different depending on whether if we
+   want each tokens resulting from function-like macro arguments
+   expansion to have a different location or not.
+
+   E.g, consider this function-like macro: 
+
+        #define M(x) x - 3
+
+   Then consider us "calling" it (and thus expanding it) like:
+   
+       M(1+4)
+
+   It will be expanded into:
+
+       1+4-3
+
+   Let's consider the case of the token '4'.
+
+   Its index can be 2 (it's the third token of the set of tokens
+   resulting from the expansion) or it can be 0 if we consider that
+   all tokens resulting from the expansion of the argument "1+2" have
+   the same index, which is 0. In this later case, the index of token
+   '-' would then be 1 and the index of token '3' would be 2.
+
+   The later case is useful to use less memory e.g, for the case of
+   the user using the option -ftrack-macro-expansion=1.
+
+   ABSOLUTE_TOKEN_INDEX is the index of the macro argument token we
+   are interested in.  CUR_REPLACEMENT_TOKEN is the token of the macro
+   parameter (inside the macro replacement list) that corresponds to
+   the macro argument for which ABSOLUTE_TOKEN_INDEX is a token index
+   of.
+
+   If we refer to the example above, for the '4' argument token,
+   ABSOLUTE_TOKEN_INDEX would be set to 2, and CUR_REPLACEMENT_TOKEN
+   would be set to the token 'x', in the replacement list "x - 3" of
+   macro M.
+
+   This is a subroutine of replace_args.  */
+inline static unsigned
+expanded_token_index (cpp_reader *pfile, cpp_macro *macro,
+		      const cpp_token *cur_replacement_token,
+		      unsigned absolute_token_index)
+{
+  if (CPP_OPTION (pfile, track_macro_expansion) > 1)
+    return absolute_token_index;
+  return cur_replacement_token - macro->exp.tokens;
+}
+
 /* 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
-   by the # or ## operators.  */
+   by the # or ## operators. EXPANSION_POINT_LOC is the location of
+   the expansion point of the macro. E.g, the location of the
+   function-like macro invocation.  */
 static void
-replace_args (cpp_reader *pfile, cpp_hashnode *node, cpp_macro *macro, macro_arg *args)
+replace_args (cpp_reader *pfile, cpp_hashnode *node, cpp_macro *macro,
+	      macro_arg *args, source_location expansion_point_loc)
 {
   unsigned int i, total;
   const cpp_token *src, *limit;
-  const cpp_token **dest, **first;
+  const cpp_token **first = NULL;
   macro_arg *arg;
-  _cpp_buff *buff;
-  unsigned int count;
+  _cpp_buff *buff = NULL;
+  source_location *virt_locs = NULL;
+  unsigned int exp_count;
+  const struct line_map *map = NULL;
+  int track_macro_exp;
 
   /* 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.  */
-  count = macro_real_token_count (macro);
-  total = count;
-  limit = macro->exp.tokens + count;
+
+  /* EXP_COUNT is the number of tokens in the macro replacement
+     list.  TOTAL is the number of tokens /after/ macro parameters
+     have been replaced by their arguments.   */
+  exp_count = macro_real_token_count (macro);
+  total = exp_count;
+  limit = macro->exp.tokens + exp_count;
 
   for (src = macro->exp.tokens; src < limit; src++)
     if (src->type == CPP_MACRO_ARG)
       {
 	/* Leading and trailing padding tokens.  */
 	total += 2;
+	/* Account for leading and padding tokens in exp_count too.
+	   This is going to be important later down this function,
+	   when we want to handle the case of (track_macro_exp <
+	   2).  */
+	exp_count += 2;
 
 	/* We have an argument.  If it is not being stringified or
 	   pasted it is macro-replaced before insertion.  */
@@ -974,67 +1443,230 @@ replace_args (cpp_reader *pfile, cpp_hashnode *node, cpp_macro *macro, macro_arg
 	  }
       }
 
-  /* Now allocate space for the expansion, copy the tokens and replace
-     the arguments.  */
-  buff = _cpp_get_buff (pfile, total * sizeof (cpp_token *));
+  /* When the compiler is called with the -ftrack-macro-expansion
+     flag, we need to keep track of the location of each token that
+     results from macro expansion.
+
+     A token resulting from macro expansion is not a new token. It is
+     simply the same token as the token coming from the macro
+     definition.  The new things that are allocated are the buffer
+     that holds the tokens resulting from macro expansion and a new
+     location that records many things like the locus of the expansion
+     point as well as the original locus inside the definition of the
+     macro.  This location is called a virtual location.
+     
+     So the buffer BUFF holds a set of cpp_token*, and the buffer
+     VIRT_LOCS holds the virtual locations of the tokens held by BUFF.
+
+     Both of these two buffers are going to be hung off of the macro
+     context, when the latter is pushed.  The memory allocated to
+     store the tokens and their locations is going to be freed once
+     the context of macro expansion is popped.
+     
+     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).
+     The good news is that extra memory gets freed when the macro
+     context is freed, i.e shortly after the macro got expanded.  */
+
+  /* Is the -ftrack-macro-expansion flag in effect?  */
+  track_macro_exp = CPP_OPTION (pfile, track_macro_expansion);
+
+  /* Now allocate memory space for tokens and locations resulting from
+     the macro expansion, copy the tokens and replace the arguments.
+     This memory must be freed when the context of the macro MACRO is
+     popped.  */
+  buff = tokens_buff_new (pfile, total, track_macro_exp ? &virt_locs : NULL);
+
   first = (const cpp_token **) buff->base;
-  dest = first;
 
+  /* Create a macro map to record the locations of the tokens that are
+     involved in the expansion.  Note that the expansion point is set
+     to the location of the closing parenthesis.  Otherwise, the
+     subsequent map created for the first token that comes after the
+     macro map might have a wrong line number.  That would lead to
+     tokens with wrong line numbers after the macro expansion.  This
+     adds up to the memory overhead of the -ftrack-macro-expansion
+     flag; for every macro that is expanded, a "macro map" is
+     created.  */
+  if (track_macro_exp)
+    {
+      int num_macro_tokens = total;
+      if (track_macro_exp < 2)
+	/* Then the number of macro tokens won't take in account the
+	   fact that function-like macro arguments can expand to
+	   multiple tokens. This is to save memory at the expense of
+	   accuracy.
+
+	   Suppose we have #define SQARE(A) A * A
+
+	   And then we do SQARE(2+3)
+
+	   Then the tokens 2, +, 3, will have the same location,
+	   saying they come from the expansion of the argument A.  */
+	num_macro_tokens = exp_count;
+      map = linemap_enter_macro (pfile->line_table, node,
+				 expansion_point_loc,
+				 num_macro_tokens);
+    }
+  i = 0;
   for (src = macro->exp.tokens; src < limit; src++)
     {
-      unsigned int count;
-      const cpp_token **from, **paste_flag;
+      unsigned int arg_tokens_count;
+      macro_arg_token_iter from;
+      const cpp_token **paste_flag = NULL;
+      const cpp_token **tmp_token_ptr;
 
       if (src->type != CPP_MACRO_ARG)
 	{
-	  *dest++ = src;
+	  /* Allocate a virtual location for token SRC, and add that
+	     token and its virtual location into the buffers BUFF and
+	     VIRT_LOCS.  */
+	  unsigned index = expanded_token_index (pfile, macro, src, i);
+	  tokens_buff_add_token (buff, virt_locs, src,
+				 src->src_loc, src->src_loc,
+				 map, index);
+	  i += 1;
 	  continue;
 	}
 
       paste_flag = 0;
       arg = &args[src->val.macro_arg.arg_no - 1];
+      /* SRC is a macro parameter that we need to replace with its
+	 corresponding argument.  So at some point we'll need to
+	 iterate over the tokens of the macro argument and copy them
+	 into the "place" now holding the correspondig macro
+	 parameter.  We are going to use the iterator type
+	 macro_argo_token_iter to handle that iterating.  The 'if'
+	 below is to initialize the iterator depending on the type of
+	 tokens the macro argument has.  It also does some adjustment
+	 related to padding tokens and some pasting corner cases.  */
       if (src->flags & STRINGIFY_ARG)
-	count = 1, from = &arg->stringified;
+	{
+	  arg_tokens_count = 1;
+	  macro_arg_token_iter_init (&from,
+				     CPP_OPTION (pfile,
+						 track_macro_expansion),
+				     MACRO_ARG_TOKEN_STRINGIFIED,
+				     arg, &arg->stringified);
+	}
       else if (src->flags & PASTE_LEFT)
-	count = arg->count, from = arg->first;
+	{
+	  arg_tokens_count = arg->count;
+	  macro_arg_token_iter_init (&from,
+				     CPP_OPTION (pfile,
+						 track_macro_expansion),
+				     MACRO_ARG_TOKEN_NORMAL,
+				     arg, arg->first);
+	}
       else if (src != macro->exp.tokens && (src[-1].flags & PASTE_LEFT))
 	{
-	  count = arg->count, from = arg->first;
-	  if (dest != first)
+	  int num_toks;
+	  arg_tokens_count = arg->count;
+	  macro_arg_token_iter_init (&from,
+				     CPP_OPTION (pfile,
+						 track_macro_expansion),
+				     MACRO_ARG_TOKEN_NORMAL,
+				     arg, arg->first);
+
+	  num_toks = tokens_buff_count (buff);
+
+	  if (num_toks != 0)
 	    {
-	      if (dest[-1]->type == CPP_COMMA
+	      /* So the current parameter token is pasted to the previous
+		 token in the replacement list.  Let's look at what
+		 we have as previous and current arguments.  */
+
+	      /* This is the previous argument's token ...  */
+	      tmp_token_ptr = tokens_buff_last_token_ptr (buff);
+
+	      if ((*tmp_token_ptr)->type == CPP_COMMA
 		  && macro->variadic
 		  && src->val.macro_arg.arg_no == macro->paramc)
 		{
-		  /* Swallow a pasted comma if from == NULL, otherwise
-		     drop the paste flag.  */
-		  if (from == NULL)
-		    dest--;
+		  /* ... which is a comma; and the current parameter
+		     is the last parameter of a variadic function-like
+		     macro.  If the argument to the current last
+		     parameter is NULL, then swallow the comma,
+		     otherwise drop the paste flag.  */
+		  if (macro_arg_token_iter_get_token (&from) == NULL)
+		    tokens_buff_remove_last_token (buff);
 		  else
-		    paste_flag = dest - 1;
+		    paste_flag = tmp_token_ptr;
 		}
 	      /* Remove the paste flag if the RHS is a placemarker.  */
-	      else if (count == 0)
-		paste_flag = dest - 1;
+	      else if (arg_tokens_count == 0)
+		paste_flag = tmp_token_ptr;
 	    }
 	}
       else
-	count = arg->expanded_count, from = arg->expanded;
+	{
+	  arg_tokens_count = arg->expanded_count;
+	  macro_arg_token_iter_init (&from,
+				     CPP_OPTION (pfile,
+						 track_macro_expansion),
+				     MACRO_ARG_TOKEN_EXPANDED,
+				     arg, arg->expanded);
+	}
 
       /* 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))
-	*dest++ = padding_token (pfile, src);
+	{
+	  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);
+	}
 
-      if (count)
+      if (arg_tokens_count)
 	{
-	  memcpy (dest, from, count * sizeof (cpp_token *));
-	  dest += count;
+	  /* So now we've got the number of tokens that make up the
+	     argument that is going to replace the current parameter
+	     in the macro's replacement list.  */
+	  unsigned int j;
+	  for (j = 0; j < arg_tokens_count; ++j)
+	    {
+	      /* So if track_macro_exp is < 2, the user wants to
+		 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
+
+		 And then we do SQARE(2+3)
+
+		 Then the tokens 2, +, 3, will have the same location,
+		 saying they come from the expansion of the argument
+		 A.
+
+	      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
+	      resulting from the expansion of the macro argument will
+	      have the index I. Normally, each of those token should
+	      have index I+J.  */
+	      unsigned token_index = i;
+	      unsigned index;
+	      if (track_macro_exp > 1)
+		token_index += j;
+
+	      index = expanded_token_index (pfile, macro, src, token_index);
+	      tokens_buff_add_token (buff, virt_locs,
+				     macro_arg_token_iter_get_token (&from),
+				     macro_arg_token_iter_get_location (&from),
+				     src->src_loc, map, index);
+	      macro_arg_token_iter_forward (&from);
+	    }
 
 	  /* With a non-empty argument on the LHS of ##, the last
 	     token should be flagged PASTE_LEFT.  */
 	  if (src->flags & PASTE_LEFT)
-	    paste_flag = dest - 1;
+	    paste_flag =
+	      (const cpp_token **) tokens_buff_last_token_ptr (buff);
 	}
       else if (CPP_PEDANTIC (pfile) && ! macro->syshdr
 	       && ! CPP_OPTION (pfile, c99)
@@ -1050,7 +1682,12 @@ replace_args (cpp_reader *pfile, cpp_hashnode *node, cpp_macro *macro, macro_arg
 
       /* Avoid paste on RHS (even case count == 0).  */
       if (!pfile->state.in_directive && !(src->flags & PASTE_LEFT))
-	*dest++ = &pfile->avoid_paste;
+	{
+	  const cpp_token *t = &pfile->avoid_paste;
+	  tokens_buff_add_token (buff, virt_locs,
+				 t, t->src_loc, t->src_loc,
+				 NULL, 0);
+	}
 
       /* Add a new paste flag, or remove an unwanted one.  */
       if (paste_flag)
@@ -1064,13 +1701,16 @@ replace_args (cpp_reader *pfile, cpp_hashnode *node, cpp_macro *macro, macro_arg
 	    token->flags = (*paste_flag)->flags & ~PASTE_LEFT;
 	  *paste_flag = token;
 	}
-    }
 
-  /* Free the expanded arguments.  */
-  for (i = 0; i < macro->paramc; i++)
-    free (args[i].expanded);
+      i += arg_tokens_count;
+    }
 
-  push_ptoken_context (pfile, node, buff, first, dest - first);
+  if (track_macro_exp)
+    push_extended_tokens_context (pfile, node, buff, virt_locs, first,
+				  tokens_buff_count (buff));
+  else
+    push_ptoken_context (pfile, node, buff, first,
+			 tokens_buff_count (buff));
 }
 
 /* Return a special padding token, with padding inherited from SOURCE.  */
@@ -1098,6 +1738,7 @@ next_context (cpp_reader *pfile)
   if (result == 0)
     {
       result = XNEW (cpp_context);
+      memset (result, 0, sizeof (cpp_context));
       result->prev = pfile->context;
       result->next = 0;
       pfile->context->next = result;
@@ -1114,8 +1755,8 @@ push_ptoken_context (cpp_reader *pfile, cpp_hashnode *macro, _cpp_buff *buff,
 {
   cpp_context *context = next_context (pfile);
 
-  context->direct_p = false;
-  context->macro = macro;
+  context->tokens_kind = TOKENS_KIND_INDIRECT;
+  context->c.macro = macro;
   context->buff = buff;
   FIRST (context).ptoken = first;
   LAST (context).ptoken = first + count;
@@ -1126,15 +1767,44 @@ void
 _cpp_push_token_context (cpp_reader *pfile, cpp_hashnode *macro,
 			 const cpp_token *first, unsigned int count)
 {
-  cpp_context *context = next_context (pfile);
-
-  context->direct_p = true;
-  context->macro = macro;
-  context->buff = NULL;
+   cpp_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;
 }
 
+/* Build a context containing a list of tokens as well as their
+   virtual locations and push it.  TOKENS_BUFF is the buffer that
+   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.  */
+static void
+push_extended_tokens_context (cpp_reader *pfile,
+			      cpp_hashnode *macro,
+			      _cpp_buff *token_buff,
+			      source_location *virt_locs,
+			      const cpp_token **first,
+			      unsigned int count)
+{
+  cpp_context *context = next_context (pfile);
+  macro_context *m;
+
+  context->tokens_kind = TOKENS_KIND_EXTENDED;
+  context->buff = token_buff;
+
+  m = XNEW (macro_context);
+  m->macro_node = macro;
+  m->virt_locs = virt_locs;
+  m->cur_virt_loc = virt_locs;
+  context->c.mc = m;
+  FIRST (context).ptoken = first;
+  LAST (context).ptoken = first + count;
+}
+
 /* Push a traditional macro's replacement text.  */
 void
 _cpp_push_text_context (cpp_reader *pfile, cpp_hashnode *macro,
@@ -1142,14 +1812,200 @@ _cpp_push_text_context (cpp_reader *pfile, cpp_hashnode *macro,
 {
   cpp_context *context = next_context (pfile);
 
-  context->direct_p = true;
-  context->macro = macro;
+  context->tokens_kind = TOKENS_KIND_DIRECT;
+  context->c.macro = macro;
   context->buff = NULL;
   CUR (context) = start;
   RLIMIT (context) = start + len;
   macro->flags |= NODE_DISABLED;
 }
 
+/* Creates a buffer that holds tokens a.k.a "token buffer", usually
+   for the purpose of storing them on a cpp_context. If VIRT_LOCS is
+   non-null (which means that -ftrack-macro-expansion is on),
+   *VIRT_LOCS is set to a newly allocated buffer that is supposed to
+   hold the virtual locations of the tokens resulting from macro
+   expansion.  */
+static _cpp_buff*
+tokens_buff_new (cpp_reader *pfile, size_t len,
+		 source_location **virt_locs)
+{
+  size_t tokens_size = len * sizeof (cpp_token *);
+  size_t locs_size = len * sizeof (source_location);
+
+  if (virt_locs != NULL)
+    *virt_locs = XNEWVEC (source_location, locs_size);
+  return _cpp_get_buff (pfile, tokens_size);
+}
+
+/* Returns the number of tokens contained in a token buffer.  The
+   buffer holds a set of cpp_token*.  */
+static size_t
+tokens_buff_count (_cpp_buff *buff)
+{
+  return (BUFF_FRONT (buff) - buff->base) / sizeof (cpp_token *);
+}
+
+/* Return a pointer to the last token contained in the token buffer
+   BUFF.  */
+static const cpp_token **
+tokens_buff_last_token_ptr (_cpp_buff *buff)
+{
+  return &((const cpp_token **) BUFF_FRONT (buff))[-1];
+}
+
+/* Remove the last token contained in the token buffer TOKENS_BUFF.
+   If VIRT_LOCS_BUFF is non-NULL,  it should point at the buffer
+   containing the virtual locations of the tokens in TOKENS_BUFF; in
+   which case the function updates that buffer as well.   */
+static inline void
+tokens_buff_remove_last_token (_cpp_buff *tokens_buff)
+
+{
+  if (BUFF_FRONT (tokens_buff) > tokens_buff->base)
+    BUFF_FRONT (tokens_buff) =
+      (unsigned char *) &((cpp_token **) BUFF_FRONT (tokens_buff))[-1];
+}
+
+/* Insert a token into the token buffer at the position pointed to by
+   DEST.  Note that the buffer is not enlarged so the previous token
+   that was at *DEST is overwritten.  VIRT_LOC_DEST, if non-null,
+   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
+   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
+   the macro.  If TOKEN is not an argument of a function-like macro or
+   if it doesn't come from a macro expansion, then VIRT_LOC can just
+   be set to the same value as PARM_DEF_LOC.  If MAP is non null, it
+   means TOKEN comes from a macro expansion and MAP is the macro map
+   associated to the macro.  MACRO_TOKEN_INDEX points to the index of
+   the token in the macro map; it is not considered if MAP is NULL.
+
+   Upon successful completion this function returns the a pointer to
+   the position of the token coming right after the insertion
+   point.  */
+static inline const cpp_token **
+tokens_buff_put_token_to (const cpp_token **dest,
+			  source_location *virt_loc_dest,
+			  const cpp_token *token,
+			  source_location virt_loc,
+			  source_location parm_def_loc,			  
+			  const struct line_map *map,
+			  unsigned int macro_token_index)
+{
+  source_location macro_loc = virt_loc;
+  const cpp_token **result;
+
+  if (virt_loc_dest)
+    {
+      /* -ftrack-macro-expansion is on.  */
+      if (map)
+	macro_loc = linemap_add_macro_token (map, macro_token_index,
+					     virt_loc, parm_def_loc);
+      *virt_loc_dest = macro_loc;
+    }
+  *dest = token;
+  result = &dest[1];
+
+  return result;
+}
+
+/* Adds a token at the end of the tokens contained in BUFFER.  Note
+   that this function doesn't enlarge BUFFER when the number of tokens
+   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
+   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
+   from a macro expansion, then VIRT_LOC can just be set to the same
+   value as PARM_DEF_LOC.  If MAP is non null, it means TOKEN comes
+   from a macro expansion and MAP is the macro map associated to the
+   macro.  MACRO_TOKEN_INDEX points to the index of the token in the
+   macro map; It is not considered if MAP is NULL.  If VIRT_LOCS is
+   non-null, it means -ftrack-macro-expansion is on; in which case
+   this function adds the virtual location DEF_LOC to the VIRT_LOCS
+   array, at the same index as the one of TOKEN in BUFFER.  Upon
+   successful completion this function returns the a pointer to the
+   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,
+		       const cpp_token *token,
+		       source_location virt_loc,
+		       source_location parm_def_loc,
+		       const struct line_map *map,
+		       unsigned int macro_token_index)
+{
+  const cpp_token **result;
+  source_location *virt_loc_dest = NULL;
+  unsigned token_index = 
+    (BUFF_FRONT (buffer) - buffer->base) / sizeof (cpp_token *);
+
+  /* Abort if we pass the end the buffer.  */
+  if (BUFF_FRONT (buffer) > BUFF_LIMIT (buffer))
+    abort ();
+
+  if (virt_locs != NULL)
+    virt_loc_dest = &virt_locs[token_index];
+
+  result =
+    tokens_buff_put_token_to ((const cpp_token **) BUFF_FRONT (buffer),
+			      virt_loc_dest, token, virt_loc, parm_def_loc,
+			      map, macro_token_index);
+
+  BUFF_FRONT (buffer) = (unsigned char *) result;
+  return result;
+}
+
+/* Allocate space for the function-like macro argument ARG to store
+   the tokens resulting from the macro-expansion of the tokens that
+   make up ARG itself. That space is allocated in ARG->expanded and
+   needs to be freed using free.  */
+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
+  arg->expanded = XNEWVEC (const cpp_token *, capacity);
+  if (CPP_OPTION (pfile, track_macro_expansion))
+    arg->expanded_virt_locs = XNEWVEC (source_location, capacity);
+
+}
+
+/* If necessary, enlarge ARG->expanded to so that it can contain SIZE
+   tokens.  */
+static void
+ensure_expanded_arg_room (cpp_reader *pfile, macro_arg *arg,
+			  size_t size, size_t *expanded_capacity)
+{
+  if (size <= *expanded_capacity)
+    return;
+
+  size *= 2;
+
+  arg->expanded =
+    XRESIZEVEC (const cpp_token *, arg->expanded, size);
+  *expanded_capacity = size;
+
+  if (CPP_OPTION (pfile, track_macro_expansion))
+    {
+      if (arg->expanded_virt_locs == NULL)
+	arg->expanded_virt_locs = XNEWVEC (source_location, size);
+      else
+	arg->expanded_virt_locs = XRESIZEVEC (source_location,
+					      arg->expanded_virt_locs,
+					      size);
+    }
+}
+
 /* Expand an argument ARG before replacing parameters in a
    function-like macro.  This works by pushing a context with the
    argument's tokens, and then expanding that into a temporary buffer
@@ -1159,38 +2015,48 @@ _cpp_push_text_context (cpp_reader *pfile, cpp_hashnode *macro,
 static void
 expand_arg (cpp_reader *pfile, macro_arg *arg)
 {
-  unsigned int capacity;
+  size_t capacity;
   bool saved_warn_trad;
+  bool track_macro_exp_p = CPP_OPTION (pfile, track_macro_expansion);
 
-  if (arg->count == 0)
+  if (arg->count == 0
+      || arg->expanded != NULL)
     return;
 
   /* Don't warn about funlike macros when pre-expanding.  */
   saved_warn_trad = CPP_WTRADITIONAL (pfile);
   CPP_WTRADITIONAL (pfile) = 0;
 
-  /* Loop, reading in the arguments.  */
+  /* Loop, reading in the tokens of the argument.  */
   capacity = 256;
-  arg->expanded = XNEWVEC (const cpp_token *, capacity);
+  alloc_expanded_arg_mem (pfile, arg, capacity);
+
+  if (track_macro_exp_p)
+    push_extended_tokens_context (pfile, NULL, NULL,
+				  arg->virt_locs,
+				  arg->first,
+				  arg->count + 1);
+  else
+    push_ptoken_context (pfile, NULL, NULL,
+			 arg->first, arg->count + 1);
 
-  push_ptoken_context (pfile, NULL, NULL, arg->first, arg->count + 1);
   for (;;)
     {
       const cpp_token *token;
+      source_location location;
 
-      if (arg->expanded_count + 1 >= capacity)
-	{
-	  capacity *= 2;
-	  arg->expanded = XRESIZEVEC (const cpp_token *, arg->expanded,
-                                      capacity);
-	}
+      ensure_expanded_arg_room (pfile, arg, arg->expanded_count + 1,
+				&capacity);
 
-      token = cpp_get_token (pfile);
+      token = cpp_get_token_1 (pfile, &location);
 
       if (token->type == CPP_EOF)
 	break;
 
-      arg->expanded[arg->expanded_count++] = token;
+      set_arg_token (arg, token, location,
+		     arg->expanded_count, MACRO_ARG_TOKEN_EXPANDED,
+		     CPP_OPTION (pfile, track_macro_expansion));
+      arg->expanded_count++;
     }
 
   _cpp_pop_context (pfile);
@@ -1199,25 +2065,132 @@ expand_arg (cpp_reader *pfile, macro_arg *arg)
 }
 
 /* Pop the current context off the stack, re-enabling the macro if the
-   context represented a macro's replacement list.  The context
-   structure is not freed so that we can re-use it later.  */
+   context represented a macro's replacement list.  Initially the
+   context structure was not freed so that we can re-use it later, but
+   now we do free it to reduce peak memory consumption.  */
 void
 _cpp_pop_context (cpp_reader *pfile)
 {
   cpp_context *context = pfile->context;
 
-  if (context->macro)
-    context->macro->flags &= ~NODE_DISABLED;
+  if (context->c.macro)
+    {
+      cpp_hashnode *macro;
+      if (context->tokens_kind == TOKENS_KIND_EXTENDED)
+	{
+	  macro_context *mc = context->c.mc;
+	  macro = mc->macro_node;
+	  /* If context->buff is set, it means the life time of tokens
+	     is bound to the life time of this context; so we must
+	     free the tokens; that means we must free the virtual
+	     locations of these tokens too.  */
+	  if (context->buff && mc->virt_locs)
+	    {
+	      free (mc->virt_locs);
+	      mc->virt_locs = NULL;
+	    }
+	  free (mc);
+	  context->c.mc = NULL;
+	}
+      else
+	macro = context->c.macro;
+
+      /* Beware that MACRO can be NULL in cases like when we are
+	 called from expand_arg.  In those cases, a dummy context with
+	 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)
+	macro->flags &= ~NODE_DISABLED;
+    }
 
   if (context->buff)
-    _cpp_release_buff (pfile, context->buff);
+    {
+      /* Decrease memory peak consumption by freeing the memory used
+	 by the context.  */
+      _cpp_free_buff (context->buff);
+    }
 
   pfile->context = context->prev;
+  /* decrease peak memory consumption by feeing the context.  */
+  pfile->context->next = NULL;
+  free (context);
 }
 
-/* External routine to get a token.  Also used nearly everywhere
-   internally, except for places where we know we can safely call
-   _cpp_lex_token directly, such as lexing a directive name.
+/* Return TRUE if we reached the end of the set of tokens stored in
+   CONTEXT, FALSE otherwise.  */
+static inline bool
+reached_end_of_context (cpp_context *context)
+{
+  if (context->tokens_kind == TOKENS_KIND_DIRECT)
+      return FIRST (context).token == LAST (context).token;
+  else if (context->tokens_kind == TOKENS_KIND_INDIRECT
+	   || context->tokens_kind == TOKENS_KIND_EXTENDED)
+    return FIRST (context).ptoken == LAST (context).ptoken;
+  else
+    abort ();
+}
+
+/* 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
+   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)
+{
+  cpp_context *c = pfile->context;
+
+  if ((c)->tokens_kind == TOKENS_KIND_DIRECT)
+    {
+      *token = FIRST (c).token;
+      *location = (*token)->src_loc;
+      FIRST (c).token++;
+    }
+  else if ((c)->tokens_kind == TOKENS_KIND_INDIRECT)		
+    {
+      *token = *FIRST (c).ptoken;
+      *location = (*token)->src_loc;
+      FIRST (c).ptoken++;
+    }
+  else if ((c)->tokens_kind == TOKENS_KIND_EXTENDED)
+    {
+      macro_context *m = c->c.mc;
+      *token = *FIRST (c).ptoken;
+      if (m->virt_locs)
+	{
+	  *location = *m->cur_virt_loc;
+	  m->cur_virt_loc++;
+	}
+      else
+	*location = (*token)->src_loc;
+      FIRST (c).ptoken++;
+    }
+  else
+    abort ();
+}
+
+/* In the traditional mode of the preprocessor, if we are currently in
+   a directive, the location of a token must be the location of the
+   start of the directive line.  This function returns the proper
+   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)
+{
+  if (CPP_OPTION (pfile, traditional))
+    {
+      if (pfile->state.in_directive)
+	return pfile->directive_line;
+    }
+  return location;
+}
+
+/* Routine to get a token as well as its location.
 
    Macro expansions and directives are transparently handled,
    including entering included files.  Thus tokens are post-macro
@@ -1225,12 +2198,20 @@ _cpp_pop_context (cpp_reader *pfile)
    see CPP_EOF only at EOF.  Internal callers also see it when meeting
    a directive inside a macro call, when at the end of a directive and
    state.in_directive is still 1, and at the end of argument
-   pre-expansion.  */
-const cpp_token *
-cpp_get_token (cpp_reader *pfile)
+   pre-expansion.
+
+   LOC is an out parameter; *LOC is set to the location "as expected
+   by the user".  Please read the comment of
+   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)
 {
   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;
 
   for (;;)
@@ -1240,20 +2221,21 @@ cpp_get_token (cpp_reader *pfile)
 
       /* Context->prev == 0 <=> base context.  */
       if (!context->prev)
-	result = _cpp_lex_token (pfile);
-      else if (FIRST (context).token != LAST (context).token)
 	{
-	  if (context->direct_p)
-	    result = FIRST (context).token++;
-	  else
-	    result = *FIRST (context).ptoken++;
-
+	  result = _cpp_lex_token (pfile);
+	  virt_loc = result->src_loc;
+	}
+      else if (!reached_end_of_context (context))
+	{
+	  consume_next_token_from_context (pfile, &result,
+					   &virt_loc);
 	  if (result->flags & PASTE_LEFT)
 	    {
 	      paste_all_tokens (pfile, result);
 	      if (pfile->state.in_directive)
 		continue;
-	      return padding_token (pfile, result);
+	      result = padding_token (pfile, result);
+	      goto out;
 	    }
 	}
       else
@@ -1261,7 +2243,8 @@ cpp_get_token (cpp_reader *pfile)
 	  _cpp_pop_context (pfile);
 	  if (pfile->state.in_directive)
 	    continue;
-	  return &pfile->avoid_paste;
+	  result = &pfile->avoid_paste;
+	  goto out;
 	}
 
       if (pfile->state.in_directive && result->type == CPP_COMMENT)
@@ -1280,7 +2263,7 @@ cpp_get_token (cpp_reader *pfile)
 	  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)
+	  if (can_set && !context->c.macro)
 	    pfile->invocation_location = result->src_loc;
 	  if (pfile->state.prevent_expansion)
 	    break;
@@ -1298,7 +2281,8 @@ cpp_get_token (cpp_reader *pfile)
 				      || (peek_tok->flags & PREV_WHITE));
 		  node = pfile->cb.macro_to_expand (pfile, result);
 		  if (node)
-		    ret = enter_macro_context (pfile, node, result);
+		    ret = enter_macro_context (pfile, node, result,
+					       virt_loc);
 		  else if (whitespace_after)
 		    {
 		      /* If macro_to_expand hook returned NULL and it
@@ -1315,12 +2299,14 @@ cpp_get_token (cpp_reader *pfile)
 		}
 	    }
 	  else
-	    ret = enter_macro_context (pfile, node, result);
+	    ret = enter_macro_context (pfile, node, result, 
+				       virt_loc);
 	  if (ret)
  	    {
 	      if (pfile->state.in_directive || ret == 2)
 		continue;
-	      return padding_token (pfile, result);
+	      result = padding_token (pfile, result);
+	      goto out;
 	    }
 	}
       else
@@ -1337,27 +2323,88 @@ cpp_get_token (cpp_reader *pfile)
       break;
     }
 
+ out:
+  if (location != NULL)
+    {
+      if (virt_loc == 0)
+	virt_loc = result->src_loc;
+      *location = virt_loc;
+
+      if (!CPP_OPTION (pfile, track_macro_expansion)
+	  && can_set
+	  && pfile->context->c.macro != 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.  */
+	*location = pfile->invocation_location;
+
+      *location = maybe_adjust_loc_for_trad_cpp (pfile, *location);
+    }
   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.  */
+/* External routine to get a token.  Also used nearly everywhere
+   internally, except for places where we know we can safely call
+   _cpp_lex_token directly, such as lexing a directive name.
+
+   Macro expansions and directives are transparently handled,
+   including entering included files.  Thus tokens are post-macro
+   expansion, and after any intervening directives.  External callers
+   see CPP_EOF only at EOF.  Internal callers also see it when meeting
+   a directive inside a macro call, when at the end of a directive and
+   state.in_directive is still 1, and at the end of argument
+   pre-expansion.  */
+const cpp_token *
+cpp_get_token (cpp_reader *pfile)
+{
+  return cpp_get_token_1 (pfile, NULL);
+}
+
+/* Like cpp_get_token, but also returns a virtual token location
+   separate from the spelling location carried 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; in that case the token's spelling location indicates the
+   locus of the token in the definition of the macro but *LOC
+   virtually encodes all the other meaningful locuses associated to
+   the token.
+
+   What? virtual location? Yes, virtual location.
+
+   If the token results from macro expansion and if macro expansion
+   location tracking is enabled its virtual location encodes (at the
+   same time):
+
+   - the spelling location of the token
+
+   - the locus of the macro expansion point
+
+   - the locus of the point where the token got instantiated as part
+     of the macro expansion process.
+
+   You have to use the linemap API to get the locus you are interested
+   in from a given virtual location.
+
+   Note however that virtual locations are not necessarily ordered for
+   relations '<' and '>'.  One must use the function
+   linemap_location_before_p instead of using the relational operator
+   '<'.
+
+   If macro expansion tracking is off and if the token results from
+   macro expansion the virtual location is the expansion point of the
+   macro that got expanded.
+
+   When the token doesn't result from macro expansion, the virtual
+   location is just the same thing as its spelling location.  */
+
 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;
-
+  result = cpp_get_token_1 (pfile, loc);
   return result;
 }
 
@@ -1367,7 +2414,7 @@ cpp_get_token_with_location (cpp_reader *pfile, source_location *loc)
 int
 cpp_sys_macro_p (cpp_reader *pfile)
 {
-  cpp_hashnode *node = pfile->context->macro;
+  cpp_hashnode *node = pfile->context->c.macro;
 
   return node && node->value.macro && node->value.macro->syshdr;
 }
@@ -1424,10 +2471,27 @@ _cpp_backup_tokens (cpp_reader *pfile, unsigned int count)
     {
       if (count != 1)
 	abort ();
-      if (pfile->context->direct_p)
+      if (pfile->context->tokens_kind == TOKENS_KIND_DIRECT)
 	FIRST (pfile->context).token--;
-      else
+      else if (pfile->context->tokens_kind == TOKENS_KIND_INDIRECT)
 	FIRST (pfile->context).ptoken--;
+      else if (pfile->context->tokens_kind == TOKENS_KIND_EXTENDED)
+	{
+	  FIRST (pfile->context).ptoken--;
+	  if (pfile->context->c.macro)
+	    {
+	      macro_context *m = pfile->context->c.mc;
+	      m->cur_virt_loc--;
+#ifdef ENABLE_CHECKING
+	      if (m->cur_virt_loc < m->virt_locs)
+		abort ();
+#endif
+	    }
+	  else
+	    abort ();
+	}
+      else
+	abort ();
     }
 }
 
diff --git a/libcpp/traditional.c b/libcpp/traditional.c
index 7ff11bb..4206b6f 100644
--- a/libcpp/traditional.c
+++ b/libcpp/traditional.c
@@ -738,7 +738,7 @@ recursive_macro (cpp_reader *pfile, cpp_hashnode *node)
       do
 	{
 	  depth++;
-	  if (context->macro == node && depth > 20)
+	  if (context->c.macro == node && depth > 20)
 	    break;
 	  context = context->prev;
 	}
-- 
1.7.6.4

^ permalink raw reply	[flat|nested] 24+ messages in thread

* Re: [PATCH 3/6] Emit macro expansion related diagnostics
  2011-10-17  9:58 ` [PATCH 3/6] Emit macro expansion related diagnostics Dodji Seketeli
@ 2011-10-17 10:56   ` Richard Guenther
  2011-10-17 12:22     ` Dodji Seketeli
  0 siblings, 1 reply; 24+ messages in thread
From: Richard Guenther @ 2011-10-17 10:56 UTC (permalink / raw)
  To: Dodji Seketeli; +Cc: gcc-patches, jason, tromey, gdr, joseph, burnus, charlet

On Mon, Oct 17, 2011 at 11:57 AM, Dodji Seketeli <dodji@redhat.com> wrote:
> In this third instalment the diagnostic machinery -- when faced with
> the virtual location of a token resulting from macro expansion -- uses
> the new linemap APIs to unwind the stack of macro expansions that led
> to that token and emits a [hopefully] more useful message than what we
> have today.
>
> diagnostic_report_current_module has been slightly changed to use the
> location given by client code instead of the global input_location
> variable.  This results in more precise diagnostic locations in
> general but then the patch adjusts some C++ tests which output changed
> as a result of this.
>
> Three new regression tests have been added.
>
> The mandatory screenshot goes like this:
>
> [dodji@adjoa gcc]$ cat -n test.c
>     1    #define OPERATE(OPRD1, OPRT, OPRD2) \
>     2      OPRD1 OPRT OPRD2;
>     3
>     4    #define SHIFTL(A,B) \
>     5      OPERATE (A,<<,B)
>     6
>     7    #define MULT(A) \
>     8      SHIFTL (A,1)
>     9
>    10    void
>    11    g ()
>    12    {
>    13      MULT (1.0);/* 1.0 << 1; <-- so this is an error.  */
>    14    }
>
> [dodji@adjoa gcc]$ ./cc1 -quiet -ftrack-macro-expansion test.c
> test.c: In function 'g':
> test.c:5:14: erreur: invalid operands to binary << (have 'double' and 'int')
> test.c:2:9: note: in expansion of macro 'OPERATE'
> test.c:5:3: note: expanded from here
> test.c:5:14: note: in expansion of macro 'SHIFTL'
> test.c:8:3: note: expanded from here
> test.c:8:3: note: in expansion of macro 'MULT2'
> test.c:13:3: note: expanded from here

This broke bootstrap on x86_64-linux.

/space/rguenther/src/svn/trunk/libcpp/line-map.c: In function
'source_location linemap_macro_map_loc_to_exp_point(const line_map*,
source_location)':
/space/rguenther/src/svn/trunk/libcpp/line-map.c:628:12: error:
variable 'token_no' set but not used [-Werror=unused-but-set-variable]
cc1plus: all warnings being treated as errors

make[3]: *** [line-map.o] Error 1


> gcc/ChangeLog
> 2011-10-15  Tom Tromey  <tromey@redhat.com>
>            Dodji Seketeli  <dodji@redhat.com>
>
>        * gcc/diagnostic.h (diagnostic_report_current_module): Add a
>        location parameter.
>        * diagnostic.c (diagnostic_report_current_module): Add a location
>        parameter to the function definition.  Use it instead of
>        input_location.  Resolve the virtual location rather than just
>        looking up its map and risking to touch a resulting macro map.
>        (default_diagnostic_starter): Pass the relevant diagnostic
>        location to diagnostic_report_current_module.
>        * tree-diagnostic.c (maybe_unwind_expanded_macro_loc): New.
>        (virt_loc_aware_diagnostic_finalizer): Likewise.
>        (diagnostic_report_current_function): Pass the
>        relevant location to diagnostic_report_current_module.
>        * tree-diagnostic.h (virt_loc_aware_diagnostic_finalizer): Declare
>        new function.
>        * toplev.c (general_init): By default, use the new
>        virt_loc_aware_diagnostic_finalizer as diagnostic finalizer.
>        * Makefile.in: Add vec.h dependency to tree-diagnostic.c.
>
> gcc/cp/ChangeLog
> 2011-10-15  Tom Tromey  <tromey@redhat.com>
>            Dodji Seketeli  <dodji@redhat.com>
>
>        * error.c (cp_diagnostic_starter): Pass the relevant location to
>        diagnostic_report_current_module.
>        (cp_diagnostic_finalizer): Call virt_loc_aware_diagnostic_finalizer.
>
> gcc/testsuite/ChangeLog
> 2011-10-15  Tom Tromey  <tromey@redhat.com>
>            Dodji Seketeli  <dodji@redhat.com>
>
>        * lib/prune.exp (prune_gcc_output):  Prune output referring to
>        included files.
>        * gcc.dg/cpp/macro-exp-tracking-1.c: New test.
>        * gcc.dg/cpp/macro-exp-tracking-2.c: Likewise.
>        * gcc.dg/cpp/macro-exp-tracking-3.c: Likewise.
>        * gcc.dg/cpp/pragma-diagnostic-2.c: Likewise.
>
> ---
>  gcc/ChangeLog                                   |   21 +++
>  gcc/Makefile.in                                 |    3 +-
>  gcc/cp/ChangeLog                                |    7 +
>  gcc/cp/error.c                                  |    5 +-
>  gcc/diagnostic.c                                |   13 +-
>  gcc/diagnostic.h                                |    2 +-
>  gcc/testsuite/ChangeLog                         |   10 ++
>  gcc/testsuite/gcc.dg/cpp/macro-exp-tracking-1.c |   21 +++
>  gcc/testsuite/gcc.dg/cpp/macro-exp-tracking-2.c |   21 +++
>  gcc/testsuite/gcc.dg/cpp/macro-exp-tracking-3.c |   14 ++
>  gcc/testsuite/gcc.dg/cpp/macro-exp-tracking-4.c |   14 ++
>  gcc/testsuite/gcc.dg/cpp/pragma-diagnostic-2.c  |   34 +++++
>  gcc/testsuite/lib/prune.exp                     |    1 +
>  gcc/toplev.c                                    |    3 +
>  gcc/tree-diagnostic.c                           |  182 ++++++++++++++++++++++-
>  gcc/tree-diagnostic.h                           |    3 +-
>  16 files changed, 343 insertions(+), 11 deletions(-)
>  create mode 100644 gcc/testsuite/gcc.dg/cpp/macro-exp-tracking-1.c
>  create mode 100644 gcc/testsuite/gcc.dg/cpp/macro-exp-tracking-2.c
>  create mode 100644 gcc/testsuite/gcc.dg/cpp/macro-exp-tracking-3.c
>  create mode 100644 gcc/testsuite/gcc.dg/cpp/macro-exp-tracking-4.c
>  create mode 100644 gcc/testsuite/gcc.dg/cpp/pragma-diagnostic-2.c
>
> diff --git a/gcc/Makefile.in b/gcc/Makefile.in
> index baec5fe..70dc0b8 100644
> --- a/gcc/Makefile.in
> +++ b/gcc/Makefile.in
> @@ -2805,7 +2805,8 @@ tree-pretty-print.o : tree-pretty-print.c $(CONFIG_H) $(SYSTEM_H) \
>    $(TM_H) coretypes.h tree-iterator.h $(SCEV_H) langhooks.h \
>    $(TREE_PASS_H) value-prof.h output.h tree-pretty-print.h
>  tree-diagnostic.o : tree-diagnostic.c $(CONFIG_H) $(SYSTEM_H) coretypes.h \
> -   $(TREE_H) $(DIAGNOSTIC_H) tree-diagnostic.h langhooks.h $(LANGHOOKS_DEF_H)
> +   $(TREE_H) $(DIAGNOSTIC_H) tree-diagnostic.h langhooks.h $(LANGHOOKS_DEF_H) \
> +   $(VEC_H)
>  fold-const.o : fold-const.c $(CONFIG_H) $(SYSTEM_H) coretypes.h $(TM_H) \
>    $(TREE_H) $(FLAGS_H) $(DIAGNOSTIC_CORE_H) $(HASHTAB_H) $(EXPR_H) $(RTL_H) \
>    $(GGC_H) $(TM_P_H) langhooks.h $(MD5_H) intl.h $(TARGET_H) \
> diff --git a/gcc/cp/error.c b/gcc/cp/error.c
> index 4d12a0d..c7c4525 100644
> --- a/gcc/cp/error.c
> +++ b/gcc/cp/error.c
> @@ -2768,7 +2768,7 @@ static void
>  cp_diagnostic_starter (diagnostic_context *context,
>                       diagnostic_info *diagnostic)
>  {
> -  diagnostic_report_current_module (context);
> +  diagnostic_report_current_module (context, diagnostic->location);
>   cp_print_error_function (context, diagnostic);
>   maybe_print_instantiation_context (context);
>   maybe_print_constexpr_context (context);
> @@ -2778,8 +2778,9 @@ cp_diagnostic_starter (diagnostic_context *context,
>
>  static void
>  cp_diagnostic_finalizer (diagnostic_context *context,
> -                        diagnostic_info *diagnostic ATTRIBUTE_UNUSED)
> +                        diagnostic_info *diagnostic)
>  {
> +  virt_loc_aware_diagnostic_finalizer (context, diagnostic);
>   pp_base_destroy_prefix (context->printer);
>  }
>
> diff --git a/gcc/diagnostic.c b/gcc/diagnostic.c
> index b46eb35..a8c0e66 100644
> --- a/gcc/diagnostic.c
> +++ b/gcc/diagnostic.c
> @@ -255,9 +255,9 @@ diagnostic_action_after_output (diagnostic_context *context,
>  }
>
>  void
> -diagnostic_report_current_module (diagnostic_context *context)
> +diagnostic_report_current_module (diagnostic_context *context, location_t where)
>  {
> -  const struct line_map *map;
> +  const struct line_map *map = NULL;
>
>   if (pp_needs_newline (context->printer))
>     {
> @@ -265,10 +265,13 @@ diagnostic_report_current_module (diagnostic_context *context)
>       pp_needs_newline (context->printer) = false;
>     }
>
> -  if (input_location <= BUILTINS_LOCATION)
> +  if (where <= BUILTINS_LOCATION)
>     return;
>
> -  map = linemap_lookup (line_table, input_location);
> +  linemap_resolve_location (line_table, where,
> +                           LRK_MACRO_DEFINITION_LOCATION,
> +                           &map);
> +
>   if (map && diagnostic_last_module_changed (context, map))
>     {
>       diagnostic_set_last_module (context, map);
> @@ -301,7 +304,7 @@ void
>  default_diagnostic_starter (diagnostic_context *context,
>                            diagnostic_info *diagnostic)
>  {
> -  diagnostic_report_current_module (context);
> +  diagnostic_report_current_module (context, diagnostic->location);
>   pp_set_prefix (context->printer, diagnostic_build_prefix (context,
>                                                            diagnostic));
>  }
> diff --git a/gcc/diagnostic.h b/gcc/diagnostic.h
> index 8074354..4b1265b 100644
> --- a/gcc/diagnostic.h
> +++ b/gcc/diagnostic.h
> @@ -253,7 +253,7 @@ extern diagnostic_context *global_dc;
>  /* Diagnostic related functions.  */
>  extern void diagnostic_initialize (diagnostic_context *, int);
>  extern void diagnostic_finish (diagnostic_context *);
> -extern void diagnostic_report_current_module (diagnostic_context *);
> +extern void diagnostic_report_current_module (diagnostic_context *, location_t);
>
>  /* Force diagnostics controlled by OPTIDX to be kind KIND.  */
>  extern diagnostic_t diagnostic_classify_diagnostic (diagnostic_context *,
> diff --git a/gcc/testsuite/gcc.dg/cpp/macro-exp-tracking-1.c b/gcc/testsuite/gcc.dg/cpp/macro-exp-tracking-1.c
> new file mode 100644
> index 0000000..d975c8c
> --- /dev/null
> +++ b/gcc/testsuite/gcc.dg/cpp/macro-exp-tracking-1.c
> @@ -0,0 +1,21 @@
> +/*
> +   { dg-options "-ftrack-macro-expansion=1" }
> +   { dg-do compile }
> +*/
> +
> +#define OPERATE(OPRD1, OPRT, OPRD2) \
> +do \
> +{ \
> +  OPRD1 OPRT OPRD2; /* { dg-message "expansion" }*/       \
> +} while (0)
> +
> +#define SHIFTL(A,B) \
> +  OPERATE (A,<<,B) /* { dg-message "expanded|expansion" } */
> +
> +void
> +foo ()
> +{
> +  SHIFTL (0.1,0.2); /* { dg-message "expanded" } */
> +}
> +
> +/* { dg-error "invalid operands" "" { target *-*-* } 13 } */
> diff --git a/gcc/testsuite/gcc.dg/cpp/macro-exp-tracking-2.c b/gcc/testsuite/gcc.dg/cpp/macro-exp-tracking-2.c
> new file mode 100644
> index 0000000..684af4c
> --- /dev/null
> +++ b/gcc/testsuite/gcc.dg/cpp/macro-exp-tracking-2.c
> @@ -0,0 +1,21 @@
> +/*
> +   { dg-options "-ftrack-macro-expansion=1" }
> +   { dg-do compile }
> +*/
> +
> +#define OPERATE(OPRD1, OPRT, OPRD2) \
> + OPRD1 OPRT OPRD2;             /* { dg-message "expansion" } */
> +
> +#define SHIFTL(A,B) \
> +  OPERATE (A,<<,B) /* { dg-message "expanded|expansion" } */
> +
> +#define MULT(A) \
> +  SHIFTL (A,1)                 /* { dg-message "expanded|expansion" } */
> +
> +void
> +foo ()
> +{
> +  MULT (1.0);                  /* { dg-message "expanded" } */
> +}
> +
> +/* { dg-error "invalid operands to binary <<" "" { target *-*-* } { 10 } } */
> diff --git a/gcc/testsuite/gcc.dg/cpp/macro-exp-tracking-3.c b/gcc/testsuite/gcc.dg/cpp/macro-exp-tracking-3.c
> new file mode 100644
> index 0000000..119053e
> --- /dev/null
> +++ b/gcc/testsuite/gcc.dg/cpp/macro-exp-tracking-3.c
> @@ -0,0 +1,14 @@
> +/*
> +  { dg-options "-fshow-column -ftrack-macro-expansion=1" }
> +  { dg-do compile }
> + */
> +
> +#define SQUARE(A) A * A                /* { dg-message "expansion" } */
> +
> +void
> +foo()
> +{
> +  SQUARE (1 << 0.1);           /* { dg-message "expanded" } */
> +}
> +
> +/* { dg-error "16:invalid operands to binary <<" "" {target *-*-* } { 11 } } */
> diff --git a/gcc/testsuite/gcc.dg/cpp/macro-exp-tracking-4.c b/gcc/testsuite/gcc.dg/cpp/macro-exp-tracking-4.c
> new file mode 100644
> index 0000000..1f9fe6a
> --- /dev/null
> +++ b/gcc/testsuite/gcc.dg/cpp/macro-exp-tracking-4.c
> @@ -0,0 +1,14 @@
> +/*
> +  { dg-options "-fshow-column -ftrack-macro-expansion=2" }
> +  { dg-do compile }
> + */
> +
> +#define SQUARE(A) A * A                /* { dg-message "expansion" } */
> +
> +void
> +foo()
> +{
> +  SQUARE (1 << 0.1);           /* { dg-message "expanded" } */
> +}
> +
> +/* { dg-error "13:invalid operands to binary <<" "" { target *-*-* } { 11 } } */
> diff --git a/gcc/testsuite/gcc.dg/cpp/pragma-diagnostic-2.c b/gcc/testsuite/gcc.dg/cpp/pragma-diagnostic-2.c
> new file mode 100644
> index 0000000..7ab95b0
> --- /dev/null
> +++ b/gcc/testsuite/gcc.dg/cpp/pragma-diagnostic-2.c
> @@ -0,0 +1,34 @@
> +/*
> +  { dg-options "-Wuninitialized -ftrack-macro-expansion=2" }
> +  { dg-do compile }
> +*/
> +
> +void f (unsigned);
> +
> +#define CODE_WITH_WARNING \
> +  int a; /* { dg-message "expansion|declared here" } */  \
> +  f (a)         /* { dg-message "expansion" } */
> +
> +#pragma GCC diagnostic ignored "-Wuninitialized"
> +
> +void
> +g (void)
> +{
> +  CODE_WITH_WARNING;
> +}
> +
> +#pragma GCC diagnostic push
> +
> +#pragma GCC diagnostic error "-Wuninitialized"
> +
> +void
> +h (void)
> +{
> +  CODE_WITH_WARNING;           /* { dg-message "expanded" } */
> +}
> +
> +/*
> +  { dg-message "some warnings being treated as errors" "" {target *-*-*} 0 }
> +*/
> +
> +/* { dg-error "uninitialized" "" { target *-*-* } { 10 } } */
> diff --git a/gcc/testsuite/lib/prune.exp b/gcc/testsuite/lib/prune.exp
> index 4683f93..09d2581 100644
> --- a/gcc/testsuite/lib/prune.exp
> +++ b/gcc/testsuite/lib/prune.exp
> @@ -29,6 +29,7 @@ proc prune_gcc_output { text } {
>     regsub -all "(^|\n)collect: re(compiling|linking)\[^\n\]*" $text "" text
>     regsub -all "(^|\n)Please submit.*instructions\[^\n\]*" $text "" text
>     regsub -all "(^|\n)\[0-9\]\[0-9\]* errors\." $text "" text
> +    regsub -all "(^|\n)(In file included|\[ \]+from)\[^\n\]*" $text "" text
>
>     # Ignore informational notes.
>     regsub -all "(^|\n)\[^\n\]*: note: \[^\n\]*" $text "" text
> diff --git a/gcc/toplev.c b/gcc/toplev.c
> index ab6b5a4..0188755 100644
> --- a/gcc/toplev.c
> +++ b/gcc/toplev.c
> @@ -1171,6 +1171,9 @@ general_init (const char *argv0)
>      can give warnings and errors.  */
>   diagnostic_initialize (global_dc, N_OPTS);
>   diagnostic_starter (global_dc) = default_tree_diagnostic_starter;
> +  /* By default print macro expansion contexts in the diagnostic
> +     finalizer -- for tokens resulting from macro macro expansion.  */
> +  diagnostic_finalizer (global_dc) = virt_loc_aware_diagnostic_finalizer;
>   /* Set a default printer.  Language specific initializations will
>      override it later.  */
>   pp_format_decoder (global_dc->printer) = &default_tree_printer;
> diff --git a/gcc/tree-diagnostic.c b/gcc/tree-diagnostic.c
> index b456a2a..53b350b 100644
> --- a/gcc/tree-diagnostic.c
> +++ b/gcc/tree-diagnostic.c
> @@ -28,6 +28,7 @@ along with GCC; see the file COPYING3.  If not see
>  #include "tree-diagnostic.h"
>  #include "langhooks.h"
>  #include "langhooks-def.h"
> +#include "vec.h"
>
>  /* Prints out, if necessary, the name of the current function
>    that caused an error.  Called from all error and warning functions.  */
> @@ -35,7 +36,7 @@ void
>  diagnostic_report_current_function (diagnostic_context *context,
>                                    diagnostic_info *diagnostic)
>  {
> -  diagnostic_report_current_module (context);
> +  diagnostic_report_current_module (context, diagnostic->location);
>   lang_hooks.print_error_function (context, input_filename, diagnostic);
>  }
>
> @@ -47,3 +48,182 @@ default_tree_diagnostic_starter (diagnostic_context *context,
>   pp_set_prefix (context->printer, diagnostic_build_prefix (context,
>                                                            diagnostic));
>  }
> +
> +/* This is a pair made of a location and the line map it originated
> +   from.  It's used in the maybe_unwind_expanded_macro_loc function
> +   below.  */
> +typedef struct
> +{
> +  const struct line_map *map;
> +  source_location where;
> +} loc_t;
> +
> +DEF_VEC_O (loc_t);
> +DEF_VEC_ALLOC_O (loc_t, heap);
> +
> +/* Unwind the different macro expansions that lead to the token which
> +   location is WHERE and emit diagnostics showing the resulting
> +   unwound macro expansion trace.  Let's look at an example to see how
> +   the trace looks like.  Suppose we have this piece of code,
> +   artificially annotated with the line numbers to increase
> +   legibility:
> +
> +    $ cat -n test.c
> +      1    #define OPERATE(OPRD1, OPRT, OPRD2) \
> +      2      OPRD1 OPRT OPRD2;
> +      3
> +      4    #define SHIFTL(A,B) \
> +      5      OPERATE (A,<<,B)
> +      6
> +      7    #define MULT(A) \
> +      8      SHIFTL (A,1)
> +      9
> +     10    void
> +     11    g ()
> +     12    {
> +     13      MULT (1.0);// 1.0 << 1; <-- so this is an error.
> +     14    }
> +
> +   Here is the diagnostic that we want the compiler to generate:
> +
> +    test.c: In function 'g':
> +    test.c:5:14: error: invalid operands to binary << (have 'double' and 'int')
> +    test.c:2:9: note: in expansion of macro 'OPERATE'
> +    test.c:5:3: note: expanded from here
> +    test.c:5:14: note: in expansion of macro 'SHIFTL'
> +    test.c:8:3: note: expanded from here
> +    test.c:8:3: note: in expansion of macro 'MULT2'
> +    test.c:13:3: note: expanded from here
> +
> +   The part that goes from the third to the eighth line of this
> +   diagnostic (the lines containing the 'note:' string) is called the
> +   unwound macro expansion trace.  That's the part generated by this
> +   function.
> +
> +   If FIRST_EXP_POINT_MAP is non-null, *FIRST_EXP_POINT_MAP is set to
> +   the map of the location in the source that first triggered the
> +   macro expansion.  This must be an ordinary map.  */
> +
> +static void
> +maybe_unwind_expanded_macro_loc (diagnostic_context *context,
> +                                 diagnostic_info *diagnostic,
> +                                 source_location where,
> +                                 const struct line_map **first_exp_point_map)
> +{
> +  const struct line_map *map;
> +  VEC(loc_t,heap) *loc_vec = NULL;
> +  unsigned ix;
> +  loc_t loc, *iter;
> +
> +  map = linemap_lookup (line_table, where);
> +  if (!linemap_macro_expansion_map_p (map))
> +    return;
> +
> +  /* Let's unwind the macros that got expanded and led to the token
> +     which location is WHERE.  We are going to store these macros into
> +     LOC_VEC, so that we can later walk it at our convenience to
> +     display a somewhat meaningful trace of the macro expansion
> +     history to the user.  Note that the first macro of the trace
> +     (which is OPERATE in the example above) is going to be stored at
> +     the beginning of LOC_VEC.  */
> +
> +  do
> +    {
> +      loc.where = where;
> +      loc.map = map;
> +
> +      VEC_safe_push (loc_t, heap, loc_vec, &loc);
> +
> +      /* WHERE is the location of a token inside the expansion of a
> +         macro.  MAP is the map holding the locations of that macro
> +         expansion.  Let's get the location of the token inside the
> +         context that triggered the expansion of this macro.
> +         This is basically how we go "down" in the trace of macro
> +         expansions that led to WHERE.  */
> +      where = linemap_unwind_toward_expansion (line_table, where, &map);
> +    } while (linemap_macro_expansion_map_p (map));
> +
> +  if (first_exp_point_map)
> +    *first_exp_point_map = map;
> +
> +  /* Walk LOC_VEC and print the macro expansion trace, unless the
> +     first macro which expansion triggered this trace was expanded
> +     inside a system header.  */
> +  if (!LINEMAP_SYSP (map))
> +    FOR_EACH_VEC_ELT (loc_t, loc_vec, ix, iter)
> +      {
> +        source_location resolved_def_loc = 0, resolved_exp_loc = 0;
> +        diagnostic_t saved_kind;
> +        const char *saved_prefix;
> +        source_location saved_location;
> +
> +        /* Okay, now here is what we want.  For each token resulting
> +           from macro expansion we want to show: 1/ where in the
> +           definition of the macro the token comes from; 2/ where the
> +           macro got expanded.  */
> +
> +        /* Resolve the location iter->where into the locus 1/ of the
> +           comment above.  */
> +        resolved_def_loc =
> +          linemap_resolve_location (line_table, iter->where,
> +                                    LRK_MACRO_DEFINITION_LOCATION, NULL);
> +
> +        /* Resolve the location of the expansion point of the macro
> +           which expansion gave the token represented by def_loc.
> +           This is the locus 2/ of the earlier comment.  */
> +        resolved_exp_loc =
> +          linemap_resolve_location (line_table,
> +                                    MACRO_MAP_EXPANSION_POINT_LOCATION (iter->map),
> +                                    LRK_MACRO_DEFINITION_LOCATION, NULL);
> +
> +        saved_kind = diagnostic->kind;
> +        saved_prefix = context->printer->prefix;
> +        saved_location = diagnostic->location;
> +
> +        diagnostic->kind = DK_NOTE;
> +        diagnostic->location = resolved_def_loc;
> +        pp_base_set_prefix (context->printer,
> +                            diagnostic_build_prefix (context,
> +                                                     diagnostic));
> +        pp_newline (context->printer);
> +        pp_printf (context->printer, "in expansion of macro '%s'",
> +                   linemap_map_get_macro_name (iter->map));
> +        pp_destroy_prefix (context->printer);
> +
> +        diagnostic->location = resolved_exp_loc;
> +        pp_base_set_prefix (context->printer,
> +                            diagnostic_build_prefix (context,
> +                                                     diagnostic));
> +        pp_newline (context->printer);
> +        pp_printf (context->printer, "expanded from here");
> +        pp_destroy_prefix (context->printer);
> +
> +        diagnostic->kind = saved_kind;
> +        diagnostic->location = saved_location;
> +        context->printer->prefix = saved_prefix;
> +      }
> +
> +  VEC_free (loc_t, heap, loc_vec);
> +}
> +
> +/*  This is a diagnostic finalizer implementation that is aware of
> +    virtual locations produced by libcpp.
> +
> +    It has to be called by the diagnostic finalizer of front ends that
> +    uses libcpp and wish to get diagnostics involving tokens resulting
> +    from macro expansion.
> +
> +    For a given location, if said location belongs to a token
> +    resulting from a macro expansion, this starter prints the context
> +    of the token.  E.g, for multiply nested macro expansion, it
> +    unwinds the nested macro expansions and prints them in a manner
> +    that is similar to what is done for function call stacks, or
> +    template instantiation contexts.  */
> +void
> +virt_loc_aware_diagnostic_finalizer (diagnostic_context *context,
> +                                    diagnostic_info *diagnostic)
> +{
> +  maybe_unwind_expanded_macro_loc (context, diagnostic,
> +                                  diagnostic->location,
> +                                  NULL);
> +}
> diff --git a/gcc/tree-diagnostic.h b/gcc/tree-diagnostic.h
> index 7d88089..6b8e8e6 100644
> --- a/gcc/tree-diagnostic.h
> +++ b/gcc/tree-diagnostic.h
> @@ -52,5 +52,6 @@ along with GCC; see the file COPYING3.  If not see
>  void default_tree_diagnostic_starter (diagnostic_context *, diagnostic_info *);
>  extern void diagnostic_report_current_function (diagnostic_context *,
>                                                diagnostic_info *);
> -
> +void virt_loc_aware_diagnostic_finalizer (diagnostic_context *,
> +                                         diagnostic_info *);
>  #endif /* ! GCC_TREE_DIAGNOSTIC_H */
> --
> 1.7.6.4
>
>

^ permalink raw reply	[flat|nested] 24+ messages in thread

* Re: [PATCH 3/6] Emit macro expansion related diagnostics
  2011-10-17 10:56   ` Richard Guenther
@ 2011-10-17 12:22     ` Dodji Seketeli
  2011-10-17 14:11       ` Dodji Seketeli
  0 siblings, 1 reply; 24+ messages in thread
From: Dodji Seketeli @ 2011-10-17 12:22 UTC (permalink / raw)
  To: Richard Guenther; +Cc: gcc-patches, jason, tromey, gdr, joseph, burnus, charlet

Richard Guenther <richard.guenther@gmail.com> writes:

> This broke bootstrap on x86_64-linux.
>
> /space/rguenther/src/svn/trunk/libcpp/line-map.c: In function
> 'source_location linemap_macro_map_loc_to_exp_point(const line_map*,
> source_location)':
> /space/rguenther/src/svn/trunk/libcpp/line-map.c:628:12: error:
> variable 'token_no' set but not used [-Werror=unused-but-set-variable]
> cc1plus: all warnings being treated as errors

Sigh.

I guess the reason why my testing hasn't caught this is that I am
bootstrapping with --enable-checking and so on my system ENABLE_CHECKING
is defined and my GCC_VERSION >= 2007.

I am bootstrapping and testing the obvious patch below with
--disable-bootstrap and I am planning to check it in if it passes, under
the obvious rule.

Sorry for the inconvenience.

commit e957242a9a8ec8f297e05ca0dae1d63bf543fad8
Author: Dodji Seketeli <dodji@redhat.com>
Date:   Mon Oct 17 13:33:41 2011 +0200

    Fix bootstrapping with --disable-checking
    
    libcpp/ChangeLog
    
    	* line-map.c (linemap_macro_map_loc_to_exp_point): Avoid setting a
    	variable without using it if ENABLE_CHECKING is not defined.

diff --git a/libcpp/line-map.c b/libcpp/line-map.c
index 87b8bfe..a1d0fbb 100644
--- a/libcpp/line-map.c
+++ b/libcpp/line-map.c
@@ -625,14 +625,12 @@ source_location
 linemap_macro_map_loc_to_exp_point (const struct line_map *map,
 				    source_location location)
 {
-  unsigned token_no;
-
   linemap_assert (linemap_macro_expansion_map_p (map)
 		  && location >= MAP_START_LOCATION (map));
 
   /* Make sure LOCATION is correct.  */
-  token_no = location - MAP_START_LOCATION (map);
-  linemap_assert (token_no <  MACRO_MAP_NUM_MACRO_TOKENS (map));
+  linemap_assert ((location - MAP_START_LOCATION (map))
+		  <  MACRO_MAP_NUM_MACRO_TOKENS (map));
 
   return MACRO_MAP_EXPANSION_POINT_LOCATION (map);
 }
-- 
		Dodji

^ permalink raw reply	[flat|nested] 24+ messages in thread

* Re: [PATCH 3/6] Emit macro expansion related diagnostics
  2011-10-17 12:22     ` Dodji Seketeli
@ 2011-10-17 14:11       ` Dodji Seketeli
  2011-10-17 17:41         ` H.J. Lu
  0 siblings, 1 reply; 24+ messages in thread
From: Dodji Seketeli @ 2011-10-17 14:11 UTC (permalink / raw)
  To: Richard Guenther; +Cc: gcc-patches, jason, tromey, gdr, joseph, burnus, charlet

Finally here is what I am checking in, which passes bootstrap with
--disable-checking --enable-languages=all,ada -- modulo this other bug
that breaks bootstrap as well
http://gcc.gnu.org/bugzilla/show_bug.cgi?id=50709.

It's been OKed off-line by Tom Tromey.

commit c1cd2be336ceec75cf40ac5f32cc4f72b8fc5da3
Author: Dodji Seketeli <dodji@redhat.com>
Date:   Mon Oct 17 13:33:41 2011 +0200

    Fix bootstrapping with --disable-checking
    
    libcpp/ChangeLog
    
    	* line-map.c (linemap_macro_map_loc_to_exp_point): Avoid setting a
    	variable without using it if ENABLE_CHECKING is not defined.  Mark
    	the LOCATION parameter as being unused.

diff --git a/libcpp/line-map.c b/libcpp/line-map.c
index 87b8bfe..43e2856 100644
--- a/libcpp/line-map.c
+++ b/libcpp/line-map.c
@@ -621,18 +621,16 @@ linemap_macro_expansion_map_p (const struct line_map *map)
    Read the comments of struct line_map and struct line_map_macro in
    line-map.h to understand what a macro expansion point is.  */
 
-source_location
+static source_location
 linemap_macro_map_loc_to_exp_point (const struct line_map *map,
-				    source_location location)
+				    source_location location ATTRIBUTE_UNUSED)
 {
-  unsigned token_no;
-
   linemap_assert (linemap_macro_expansion_map_p (map)
 		  && location >= MAP_START_LOCATION (map));
 
   /* Make sure LOCATION is correct.  */
-  token_no = location - MAP_START_LOCATION (map);
-  linemap_assert (token_no <  MACRO_MAP_NUM_MACRO_TOKENS (map));
+  linemap_assert ((location - MAP_START_LOCATION (map))
+		  <  MACRO_MAP_NUM_MACRO_TOKENS (map));
 
   return MACRO_MAP_EXPANSION_POINT_LOCATION (map);
 }
-- 
		Dodji

^ permalink raw reply	[flat|nested] 24+ messages in thread

* Re: [PATCH 3/6] Emit macro expansion related diagnostics
  2011-10-17 14:11       ` Dodji Seketeli
@ 2011-10-17 17:41         ` H.J. Lu
  2011-10-18  0:29           ` Dodji Seketeli
  0 siblings, 1 reply; 24+ messages in thread
From: H.J. Lu @ 2011-10-17 17:41 UTC (permalink / raw)
  To: Dodji Seketeli
  Cc: Richard Guenther, gcc-patches, jason, tromey, gdr, joseph,
	burnus, charlet

On Mon, Oct 17, 2011 at 6:41 AM, Dodji Seketeli <dodji@redhat.com> wrote:
> Finally here is what I am checking in, which passes bootstrap with
> --disable-checking --enable-languages=all,ada -- modulo this other bug
> that breaks bootstrap as well
> http://gcc.gnu.org/bugzilla/show_bug.cgi?id=50709.
>
> It's been OKed off-line by Tom Tromey.
>
> commit c1cd2be336ceec75cf40ac5f32cc4f72b8fc5da3
> Author: Dodji Seketeli <dodji@redhat.com>
> Date:   Mon Oct 17 13:33:41 2011 +0200
>
>    Fix bootstrapping with --disable-checking
>
>    libcpp/ChangeLog
>
>        * line-map.c (linemap_macro_map_loc_to_exp_point): Avoid setting a
>        variable without using it if ENABLE_CHECKING is not defined.  Mark
>        the LOCATION parameter as being unused.
>

There are at least 2 bootstrap problems:

http://gcc.gnu.org/bugzilla/show_bug.cgi?id=50760


-- 
H.J.

^ permalink raw reply	[flat|nested] 24+ messages in thread

* Re: [PATCH 1/6] Linemap infrastructure for virtual locations
  2011-10-17 10:25 ` [PATCH 1/6] Linemap infrastructure for virtual locations Dodji Seketeli
@ 2011-10-17 19:53   ` Gerald Pfeifer
  2011-10-17 20:44     ` Dodji Seketeli
  0 siblings, 1 reply; 24+ messages in thread
From: Gerald Pfeifer @ 2011-10-17 19:53 UTC (permalink / raw)
  To: Dodji Seketeli, Tom Tromey
  Cc: gcc-patches, Jason Merrill, Gabriel Dos Reis, Joseph S. Myers,
	Tobias Burnus, Arnaud Charlet

Hi Dodji,

On Mon, 17 Oct 2011, Dodji Seketeli wrote:
> This is the first instalment of a set which goal is to track locations
> of tokens across macro expansions.  Tom Tromey did the original work
> and attached the patch to PR preprocessor/7263.  This opus is a
> derivative of that original work.

this looks like it's causing the following bootstrap failure for me
on i386-unknown-freebsd9.0?

/scratch/tmp/gerald/gcc-HEAD/gcc/input.c: In function 'void dump_line_table_stat
istics()':
/scratch/tmp/gerald/gcc-HEAD/gcc/input.c:103:33: error: format '%lu' expects arg
ument of type 'long unsigned int', but argument 3 has type 'size_t {aka unsigned
 int}' [-Werror=format]

Gerald

^ permalink raw reply	[flat|nested] 24+ messages in thread

* Re: [PATCH 1/6] Linemap infrastructure for virtual locations
  2011-10-17 19:53   ` Gerald Pfeifer
@ 2011-10-17 20:44     ` Dodji Seketeli
  2011-10-18  7:24       ` Gerald Pfeifer
  0 siblings, 1 reply; 24+ messages in thread
From: Dodji Seketeli @ 2011-10-17 20:44 UTC (permalink / raw)
  To: Gerald Pfeifer
  Cc: Tom Tromey, gcc-patches, Jason Merrill, Gabriel Dos Reis,
	Joseph S. Myers, Tobias Burnus, Arnaud Charlet

Gerald Pfeifer <gerald@pfeifer.com> writes:

> Hi Dodji,
>
> On Mon, 17 Oct 2011, Dodji Seketeli wrote:
>> This is the first instalment of a set which goal is to track locations
>> of tokens across macro expansions.  Tom Tromey did the original work
>> and attached the patch to PR preprocessor/7263.  This opus is a
>> derivative of that original work.
>
> this looks like it's causing the following bootstrap failure for me
> on i386-unknown-freebsd9.0?

Yes this is http://gcc.gnu.org/bugzilla/show_bug.cgi?id=50760, and I am
testing the patch attached to the bug at the moment.  Just curious, does
that patch help for your target?

-- 
		Dodji

^ permalink raw reply	[flat|nested] 24+ messages in thread

* Re: [PATCH 3/6] Emit macro expansion related diagnostics
  2011-10-17 17:41         ` H.J. Lu
@ 2011-10-18  0:29           ` Dodji Seketeli
  2011-10-18  6:07             ` Jason Merrill
  0 siblings, 1 reply; 24+ messages in thread
From: Dodji Seketeli @ 2011-10-18  0:29 UTC (permalink / raw)
  To: H.J. Lu
  Cc: Richard Guenther, gcc-patches, jason, tromey, gdr, joseph,
	burnus, charlet

"H.J. Lu" <hjl.tools@gmail.com> writes:

> There are at least 2 bootstrap problems:
>
> http://gcc.gnu.org/bugzilla/show_bug.cgi?id=50760

The patch below should address those issues.

The first two hunks are to fix bootstrap for targets that have
!NO_IMPLICIT_EXTERN_C.  To test it, I built the compiler with
--target=avr.

The next hunk is to fix one part of the issue reported in the PR.  It's
because size_t on ia32 is unsigned int, while being unsigned long int on
x86_64.  To fix ia32 in a portable way I am casting size_t to long as
it's done in other parts of the compiler when fprinting.

The last hunk is because it appears to me that after
macro_arg_token_iter_init gets inlined into replace_args,
from->token_ptr appears possibly uninitialized.  So I am unconditionally
initializing it.

For the ia32 fixes, as I don't have any such target at hand, I ran
configure i686-pc-linux-gnu on my x86_64 host and the bootstrap is
underway.  It's taking time, sorry.

I ran a full bootstrap of the changes which went up to the comparison
failure we are currently having on trunk.

OK if this appears to fix the raised issues and passes bootstraps on the
i686 target?

Author: Dodji Seketeli <dodji@redhat.com>
Date:   Mon Oct 17 22:33:46 2011 +0200

    Fix bootstrap on !NO_IMPLICIT_EXTERN_C and ia32 targets
    
    libcpp/
    
    	* macro.c (macro_arg_token_iter_init): Unconditionally initialize
    	iter->location_ptr.
    
    gcc/c-family/
    
    	* c-lex.c (fe_file_change): Use LINEMAP_SYSP when
    	!NO_IMPLICIT_EXTERN_C.
    
    gcc/
    	* input.c (dump_line_table_statistics): Cast size_t to long for
    	printing.

diff --git a/gcc/c-family/c-lex.c b/gcc/c-family/c-lex.c
index be83b61..b151564 100644
--- a/gcc/c-family/c-lex.c
+++ b/gcc/c-family/c-lex.c
@@ -211,7 +211,7 @@ fe_file_change (const struct line_map *new_map)
 #ifndef NO_IMPLICIT_EXTERN_C
 	  if (c_header_level)
 	    ++c_header_level;
-	  else if (new_map->sysp == 2)
+	  else if (LINEMAP_SYSP (new_map) == 2)
 	    {
 	      c_header_level = 1;
 	      ++pending_lang_change;
@@ -224,7 +224,7 @@ fe_file_change (const struct line_map *new_map)
 #ifndef NO_IMPLICIT_EXTERN_C
       if (c_header_level && --c_header_level == 0)
 	{
-	  if (new_map->sysp == 2)
+	  if (LINEMAP_SYSP (new_map) == 2)
 	    warning (0, "badly nested C headers from preprocessor");
 	  --pending_lang_change;
 	}
diff --git a/gcc/input.c b/gcc/input.c
index 41842b7..8138a65 100644
--- a/gcc/input.c
+++ b/gcc/input.c
@@ -99,46 +99,46 @@ dump_line_table_statistics (void)
     + s.macro_maps_used_size
     + s.macro_maps_locations_size;
 
-  fprintf (stderr, "Number of expanded macros:                     %5lu\n",
-           s.num_expanded_macros);
+  fprintf (stderr, "Number of expanded macros:                     %5ld\n",
+           (long)s.num_expanded_macros);
   if (s.num_expanded_macros != 0)
-    fprintf (stderr, "Average number of tokens per macro expansion:  %5lu\n",
-             s.num_macro_tokens / s.num_expanded_macros);
+    fprintf (stderr, "Average number of tokens per macro expansion:  %5ld\n",
+             (long)s.num_macro_tokens / s.num_expanded_macros);
   fprintf (stderr,
            "\nLine Table allocations during the "
            "compilation process\n");
-  fprintf (stderr, "Number of ordinary maps used:        %5lu%c\n",
-           SCALE (s.num_ordinary_maps_used),
+  fprintf (stderr, "Number of ordinary maps used:        %5ld%c\n",
+           (long)SCALE (s.num_ordinary_maps_used),
            STAT_LABEL (s.num_ordinary_maps_used));
-  fprintf (stderr, "Ordinary map used size:              %5lu%c\n",
-           SCALE (s.ordinary_maps_used_size),
+  fprintf (stderr, "Ordinary map used size:              %5ld%c\n",
+           (long)SCALE (s.ordinary_maps_used_size),
            STAT_LABEL (s.ordinary_maps_used_size));
-  fprintf (stderr, "Number of ordinary maps allocated:   %5lu%c\n",
-           SCALE (s.num_ordinary_maps_allocated),
+  fprintf (stderr, "Number of ordinary maps allocated:   %5ld%c\n",
+           (long)SCALE (s.num_ordinary_maps_allocated),
            STAT_LABEL (s.num_ordinary_maps_allocated));
-  fprintf (stderr, "Ordinary maps allocated size:        %5lu%c\n",
-           SCALE (s.ordinary_maps_allocated_size),
+  fprintf (stderr, "Ordinary maps allocated size:        %5ld%c\n",
+           (long)SCALE (s.ordinary_maps_allocated_size),
            STAT_LABEL (s.ordinary_maps_allocated_size));
-  fprintf (stderr, "Number of macro maps used:           %5lu%c\n",
-           SCALE (s.num_macro_maps_used),
+  fprintf (stderr, "Number of macro maps used:           %5ld%c\n",
+           (long)SCALE (s.num_macro_maps_used),
            STAT_LABEL (s.num_macro_maps_used));
-  fprintf (stderr, "Macro maps used size:                %5lu%c\n",
-           SCALE (s.macro_maps_used_size),
+  fprintf (stderr, "Macro maps used size:                %5ld%c\n",
+           (long)SCALE (s.macro_maps_used_size),
            STAT_LABEL (s.macro_maps_used_size));
-  fprintf (stderr, "Macro maps locations size:           %5lu%c\n",
-           SCALE (s.macro_maps_locations_size),
+  fprintf (stderr, "Macro maps locations size:           %5ld%c\n",
+           (long)SCALE (s.macro_maps_locations_size),
            STAT_LABEL (s.macro_maps_locations_size));
-  fprintf (stderr, "Macro maps size:                     %5lu%c\n",
-           SCALE (macro_maps_size),
+  fprintf (stderr, "Macro maps size:                     %5ld%c\n",
+           (long)SCALE (macro_maps_size),
            STAT_LABEL (macro_maps_size));
-  fprintf (stderr, "Duplicated maps locations size:      %5lu%c\n",
-           SCALE (s.duplicated_macro_maps_locations_size),
+  fprintf (stderr, "Duplicated maps locations size:      %5ld%c\n",
+           (long)SCALE (s.duplicated_macro_maps_locations_size),
            STAT_LABEL (s.duplicated_macro_maps_locations_size));
-  fprintf (stderr, "Total allocated maps size:           %5lu%c\n",
-           SCALE (total_allocated_map_size),
+  fprintf (stderr, "Total allocated maps size:           %5ld%c\n",
+           (long)SCALE (total_allocated_map_size),
            STAT_LABEL (total_allocated_map_size));
-  fprintf (stderr, "Total used maps size:                %5lu%c\n",
-           SCALE (total_used_map_size),
+  fprintf (stderr, "Total used maps size:                %5ld%c\n",
+           (long)SCALE (total_used_map_size),
            STAT_LABEL (total_used_map_size));
   fprintf (stderr, "\n");
 }
diff --git a/libcpp/macro.c b/libcpp/macro.c
index 2d0eeaa..f313959 100644
--- a/libcpp/macro.c
+++ b/libcpp/macro.c
@@ -1278,6 +1278,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
-- 
		Dodji

^ permalink raw reply	[flat|nested] 24+ messages in thread

* Re: [PATCH 3/6] Emit macro expansion related diagnostics
  2011-10-18  0:29           ` Dodji Seketeli
@ 2011-10-18  6:07             ` Jason Merrill
  2011-10-18  9:22               ` Dodji Seketeli
  0 siblings, 1 reply; 24+ messages in thread
From: Jason Merrill @ 2011-10-18  6:07 UTC (permalink / raw)
  To: Dodji Seketeli
  Cc: H.J. Lu, Richard Guenther, gcc-patches, tromey, gdr, joseph,
	burnus, charlet

On 10/17/2011 06:33 PM, Dodji Seketeli wrote:
> OK if this appears to fix the raised issues and passes bootstraps on the
> i686 target?

If you have a patch like this that fixes a major regression, go ahead 
and check it in without waiting for approval; we can adjust it as 
necessary after build is working again.

>   size_t num_expanded_macros;

> -  fprintf (stderr, "Number of expanded macros:                     %5lu\n",
> -           s.num_expanded_macros);
> +  fprintf (stderr, "Number of expanded macros:                     %5ld\n",
> +           (long)s.num_expanded_macros);

If we're going to use %l in printf, we should use long rather than 
size_t in linemap_stats; that way we don't need the cast.

Jason

^ permalink raw reply	[flat|nested] 24+ messages in thread

* Re: [PATCH 1/6] Linemap infrastructure for virtual locations
  2011-10-17 20:44     ` Dodji Seketeli
@ 2011-10-18  7:24       ` Gerald Pfeifer
  2011-10-18  9:44         ` Dodji Seketeli
  0 siblings, 1 reply; 24+ messages in thread
From: Gerald Pfeifer @ 2011-10-18  7:24 UTC (permalink / raw)
  To: Dodji Seketeli
  Cc: Tom Tromey, gcc-patches, Jason Merrill, Gabriel Dos Reis,
	Joseph S. Myers, Tobias Burnus, Arnaud Charlet

On Mon, 17 Oct 2011, Dodji Seketeli wrote:
>> this looks like it's causing the following bootstrap failure for me
>> on i386-unknown-freebsd9.0?
> Yes this is http://gcc.gnu.org/bugzilla/show_bug.cgi?id=50760, and I am
> testing the patch attached to the bug at the moment.  Just curious, does
> that patch help for your target?

Yes, applying the input.c part of the patch I am onw beyond the point
where bootstrap failed last night and into stage 3.

Thanks,
Gerald

^ permalink raw reply	[flat|nested] 24+ messages in thread

* Re: [PATCH 3/6] Emit macro expansion related diagnostics
  2011-10-18  6:07             ` Jason Merrill
@ 2011-10-18  9:22               ` Dodji Seketeli
  0 siblings, 0 replies; 24+ messages in thread
From: Dodji Seketeli @ 2011-10-18  9:22 UTC (permalink / raw)
  To: Jason Merrill
  Cc: H.J. Lu, Richard Guenther, gcc-patches, tromey, gdr, joseph,
	burnus, charlet

Jason Merrill <jason@redhat.com> writes:

> If you have a patch like this that fixes a major regression, go ahead
> and check it in without waiting for approval; we can adjust it as
> necessary after build is working again.

OK.

>>   size_t num_expanded_macros;
>
>> -  fprintf (stderr, "Number of expanded macros:                     %5lu\n",
>> -           s.num_expanded_macros);
>> +  fprintf (stderr, "Number of expanded macros:                     %5ld\n",
>> +           (long)s.num_expanded_macros);
>
> If we're going to use %l in printf, we should use long rather than
> size_t in linemap_stats; that way we don't need the cast.

OK, I have checked the below in.
    
    libcpp/
    
    	* include/line-map.h (struct linemap_stats): Change the type of
    	the members from size_t to long.
    	* macro.c (macro_arg_token_iter_init): Unconditionally initialize
    	iter->location_ptr.
    
    gcc/c-family/
    
    	* c-lex.c (fe_file_change): Use LINEMAP_SYSP when
    	!NO_IMPLICIT_EXTERN_C.
    
    gcc/
    	* input.c (dump_line_table_statistics): Use long, not size_t.
    
    git-svn-id: svn+ssh://gcc.gnu.org/svn/gcc/trunk@180124 138bc75d-0d04-0410-961f-82ee72b054a4

diff --git a/gcc/c-family/c-lex.c b/gcc/c-family/c-lex.c
index be83b61..b151564 100644
--- a/gcc/c-family/c-lex.c
+++ b/gcc/c-family/c-lex.c
@@ -211,7 +211,7 @@ fe_file_change (const struct line_map *new_map)
 #ifndef NO_IMPLICIT_EXTERN_C
 	  if (c_header_level)
 	    ++c_header_level;
-	  else if (new_map->sysp == 2)
+	  else if (LINEMAP_SYSP (new_map) == 2)
 	    {
 	      c_header_level = 1;
 	      ++pending_lang_change;
@@ -224,7 +224,7 @@ fe_file_change (const struct line_map *new_map)
 #ifndef NO_IMPLICIT_EXTERN_C
       if (c_header_level && --c_header_level == 0)
 	{
-	  if (new_map->sysp == 2)
+	  if (LINEMAP_SYSP (new_map) == 2)
 	    warning (0, "badly nested C headers from preprocessor");
 	  --pending_lang_change;
 	}
diff --git a/gcc/input.c b/gcc/input.c
index 41842b7..a780f5c 100644
--- a/gcc/input.c
+++ b/gcc/input.c
@@ -80,7 +80,7 @@ void
 dump_line_table_statistics (void)
 {
   struct linemap_stats s;
-  size_t total_used_map_size,
+  long total_used_map_size,
     macro_maps_size,
     total_allocated_map_size;
 
@@ -99,45 +99,45 @@ dump_line_table_statistics (void)
     + s.macro_maps_used_size
     + s.macro_maps_locations_size;
 
-  fprintf (stderr, "Number of expanded macros:                     %5lu\n",
+  fprintf (stderr, "Number of expanded macros:                     %5ld\n",
            s.num_expanded_macros);
   if (s.num_expanded_macros != 0)
-    fprintf (stderr, "Average number of tokens per macro expansion:  %5lu\n",
+    fprintf (stderr, "Average number of tokens per macro expansion:  %5ld\n",
              s.num_macro_tokens / s.num_expanded_macros);
   fprintf (stderr,
            "\nLine Table allocations during the "
            "compilation process\n");
-  fprintf (stderr, "Number of ordinary maps used:        %5lu%c\n",
+  fprintf (stderr, "Number of ordinary maps used:        %5ld%c\n",
            SCALE (s.num_ordinary_maps_used),
            STAT_LABEL (s.num_ordinary_maps_used));
-  fprintf (stderr, "Ordinary map used size:              %5lu%c\n",
+  fprintf (stderr, "Ordinary map used size:              %5ld%c\n",
            SCALE (s.ordinary_maps_used_size),
            STAT_LABEL (s.ordinary_maps_used_size));
-  fprintf (stderr, "Number of ordinary maps allocated:   %5lu%c\n",
+  fprintf (stderr, "Number of ordinary maps allocated:   %5ld%c\n",
            SCALE (s.num_ordinary_maps_allocated),
            STAT_LABEL (s.num_ordinary_maps_allocated));
-  fprintf (stderr, "Ordinary maps allocated size:        %5lu%c\n",
+  fprintf (stderr, "Ordinary maps allocated size:        %5ld%c\n",
            SCALE (s.ordinary_maps_allocated_size),
            STAT_LABEL (s.ordinary_maps_allocated_size));
-  fprintf (stderr, "Number of macro maps used:           %5lu%c\n",
+  fprintf (stderr, "Number of macro maps used:           %5ld%c\n",
            SCALE (s.num_macro_maps_used),
            STAT_LABEL (s.num_macro_maps_used));
-  fprintf (stderr, "Macro maps used size:                %5lu%c\n",
+  fprintf (stderr, "Macro maps used size:                %5ld%c\n",
            SCALE (s.macro_maps_used_size),
            STAT_LABEL (s.macro_maps_used_size));
-  fprintf (stderr, "Macro maps locations size:           %5lu%c\n",
+  fprintf (stderr, "Macro maps locations size:           %5ld%c\n",
            SCALE (s.macro_maps_locations_size),
            STAT_LABEL (s.macro_maps_locations_size));
-  fprintf (stderr, "Macro maps size:                     %5lu%c\n",
+  fprintf (stderr, "Macro maps size:                     %5ld%c\n",
            SCALE (macro_maps_size),
            STAT_LABEL (macro_maps_size));
-  fprintf (stderr, "Duplicated maps locations size:      %5lu%c\n",
+  fprintf (stderr, "Duplicated maps locations size:      %5ld%c\n",
            SCALE (s.duplicated_macro_maps_locations_size),
            STAT_LABEL (s.duplicated_macro_maps_locations_size));
-  fprintf (stderr, "Total allocated maps size:           %5lu%c\n",
+  fprintf (stderr, "Total allocated maps size:           %5ld%c\n",
            SCALE (total_allocated_map_size),
            STAT_LABEL (total_allocated_map_size));
-  fprintf (stderr, "Total used maps size:                %5lu%c\n",
+  fprintf (stderr, "Total used maps size:                %5ld%c\n",
            SCALE (total_used_map_size),
            STAT_LABEL (total_used_map_size));
   fprintf (stderr, "\n");
diff --git a/libcpp/include/line-map.h b/libcpp/include/line-map.h
index 1e2a148..ef98f59 100644
--- a/libcpp/include/line-map.h
+++ b/libcpp/include/line-map.h
@@ -687,17 +687,17 @@ expanded_location linemap_expand_location_full (struct line_maps *,
    linemap_get_statistics.  */
 struct linemap_stats
 {
-  size_t num_ordinary_maps_allocated;
-  size_t num_ordinary_maps_used;
-  size_t ordinary_maps_allocated_size;
-  size_t ordinary_maps_used_size;
-  size_t num_expanded_macros;
-  size_t num_macro_tokens;
-  size_t num_macro_maps_used;
-  size_t macro_maps_allocated_size;
-  size_t macro_maps_used_size;
-  size_t macro_maps_locations_size;
-  size_t duplicated_macro_maps_locations_size;
+  long num_ordinary_maps_allocated;
+  long num_ordinary_maps_used;
+  long ordinary_maps_allocated_size;
+  long ordinary_maps_used_size;
+  long num_expanded_macros;
+  long num_macro_tokens;
+  long num_macro_maps_used;
+  long macro_maps_allocated_size;
+  long macro_maps_used_size;
+  long macro_maps_locations_size;
+  long duplicated_macro_maps_locations_size;
 };
 
 /* Compute and return statistics about the memory consumption of some
diff --git a/libcpp/line-map.c b/libcpp/line-map.c
index 43e2856..fb3be3a 100644
--- a/libcpp/line-map.c
+++ b/libcpp/line-map.c
@@ -1180,7 +1180,7 @@ void
 linemap_get_statistics (struct line_maps *set,
 			struct linemap_stats *s)
 {
-  size_t ordinary_maps_allocated_size, ordinary_maps_used_size,
+  long ordinary_maps_allocated_size, ordinary_maps_used_size,
     macro_maps_allocated_size, macro_maps_used_size,
     macro_maps_locations_size = 0, duplicated_macro_maps_locations_size = 0;
 
diff --git a/libcpp/macro.c b/libcpp/macro.c
index 2d0eeaa..f313959 100644
--- a/libcpp/macro.c
+++ b/libcpp/macro.c
@@ -1278,6 +1278,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

-- 
		Dodji

^ permalink raw reply	[flat|nested] 24+ messages in thread

* Re: [PATCH 1/6] Linemap infrastructure for virtual locations
  2011-10-18  7:24       ` Gerald Pfeifer
@ 2011-10-18  9:44         ` Dodji Seketeli
  0 siblings, 0 replies; 24+ messages in thread
From: Dodji Seketeli @ 2011-10-18  9:44 UTC (permalink / raw)
  To: Gerald Pfeifer
  Cc: Tom Tromey, gcc-patches, Jason Merrill, Gabriel Dos Reis,
	Joseph S. Myers, Tobias Burnus, Arnaud Charlet

Gerald Pfeifer <gerald@pfeifer.com> writes:

> On Mon, 17 Oct 2011, Dodji Seketeli wrote:
>>> this looks like it's causing the following bootstrap failure for me
>>> on i386-unknown-freebsd9.0?
>> Yes this is http://gcc.gnu.org/bugzilla/show_bug.cgi?id=50760, and I am
>> testing the patch attached to the bug at the moment.  Just curious, does
>> that patch help for your target?
>
> Yes, applying the input.c part of the patch I am onw beyond the point
> where bootstrap failed last night and into stage 3.

Thank you for testing.  I have checked the fix in.  It should hopefully
be fixed now.

-- 
		Dodji

^ permalink raw reply	[flat|nested] 24+ messages in thread

* Re: [PATCH 2/6] Generate virtual locations for tokens
  2011-10-17 10:26 ` [PATCH 2/6] Generate virtual locations for tokens Dodji Seketeli
@ 2011-10-19 14:48   ` Ulrich Weigand
  2011-10-19 17:11     ` Dodji Seketeli
  2011-10-19 20:42     ` Dodji Seketeli
  0 siblings, 2 replies; 24+ messages in thread
From: Ulrich Weigand @ 2011-10-19 14:48 UTC (permalink / raw)
  To: Dodji Seketeli; +Cc: gcc-patches, jason, tromey, gdr, joseph, burnus, charlet

Dodji Seketeli wrote:

> libcpp/ChangeLog
> 2011-10-15  Tom Tromey  <tromey@redhat.com>
> 	    Dodji Seketeli  <dodji@redhat.com>
> 
> 	(cpp_get_token_1): New static function. Split and extended from
> 	cpp_get_token.  Use reached_end_of_context and
> 	consume_next_token_from_context.  Unify its return point.  Move
> 	the location tweaking from cpp_get_token_with_location in here.
> 	(cpp_get_token): Use cpp_get_token_1

Since this set of patches went in, I'm seeing weird errors when building
newlib for SPU:

/home/kwerner/dailybuild/spu-tc-2011-10-18/newlib-head/src/newlib/libm/machine/spu/headers/recipd2.h: In function '_recipd2':
/home/kwerner/dailybuild/spu-tc-2011-10-18/newlib-head/src/newlib/libm/machine/spu/headers/recipd2.h:129:3: note: use -flax-vector-conversions to permit conversions between vectors with differing element types or numbers of subparts
/home/kwerner/dailybuild/spu-tc-2011-10-18/newlib-head/src/newlib/libm/machine/spu/headers/recipd2.h:129:13: error: incompatible types when assigning to type '__vector(4) int' from type '__vector(4) unsigned int'
/home/kwerner/dailybuild/spu-tc-2011-10-18/newlib-head/src/newlib/libm/machine/spu/headers/recipd2.h:131:10: error: incompatible types when assigning to type '__vector(4) int' from type '__vector(4) unsigned int'
/home/kwerner/dailybuild/spu-tc-2011-10-18/newlib-head/src/newlib/libm/machine/spu/headers/recipd2.h:132:10: error: incompatible types when assigning to type '__vector(4) int' from type '__vector(4) unsigned int'

The code in question looks like:

  vec_uint4 isinf, iszero, isdenorm0;
[snip]
  isdenorm0 = spu_cmpeq(spu_shuffle((vec_uint4)exp, (vec_uint4)exp, splat_hi), 0);

  isinf  = spu_cmpeq((vec_uint4)value_abs, (vec_uint4)expmask);
  iszero = spu_cmpeq((vec_uint4)value_abs, 0);

Note that isinf etc. *are* defined as *unsigned* vector types.

The problem seems to be that the preprocessor somehow stripped
off the "unsigned" keyword.  A reduced test case is:

#define isinf(__x)

#define vec_uint4 __vector unsigned int

vec_uint4 isinf;

(Using the name of a function-like macro as identifer is maybe a bit odd,
but should be perfectly legal C as far as I know ...)

Running this through "cc1 -E" on a SPU target before the patch set yields:

__attribute__((__spu_vector__)) unsigned int isinf;

as expected, but after the patch set we get:

__attribute__((__spu_vector__)) int isinf;

instead.

The problem is clearly related to the platform-specific "macro_to_expand"
routine that is used on SPU to implement the context-sensitive __vector
keyword.

With your changes to cpp_get_token (which is the sole caller of the
macro_to_expand callback), are there any changes in the interface
to the callback?  Any suggestions what could be going on here?

Note that the implementation of the callback is in
  config/spu/spu-c.c:spu_macro_to_expand


Thanks,
Ulrich

-- 
  Dr. Ulrich Weigand
  GNU Toolchain for Linux on System z and Cell BE
  Ulrich.Weigand@de.ibm.com

^ permalink raw reply	[flat|nested] 24+ messages in thread

* Re: [PATCH 2/6] Generate virtual locations for tokens
  2011-10-19 14:48   ` Ulrich Weigand
@ 2011-10-19 17:11     ` Dodji Seketeli
  2011-10-19 20:42     ` Dodji Seketeli
  1 sibling, 0 replies; 24+ messages in thread
From: Dodji Seketeli @ 2011-10-19 17:11 UTC (permalink / raw)
  To: Ulrich Weigand; +Cc: gcc-patches, jason, tromey, gdr, joseph, burnus, charlet

"Ulrich Weigand" <uweigand@de.ibm.com> writes:

> The problem seems to be that the preprocessor somehow stripped
> off the "unsigned" keyword.  A reduced test case is:
>
> #define isinf(__x)
>
> #define vec_uint4 __vector unsigned int
>
> vec_uint4 isinf;
>
> (Using the name of a function-like macro as identifer is maybe a bit odd,
> but should be perfectly legal C as far as I know ...)
>
> Running this through "cc1 -E" on a SPU target before the patch set yields:
>
> __attribute__((__spu_vector__)) unsigned int isinf;
>
> as expected, but after the patch set we get:
>
> __attribute__((__spu_vector__)) int isinf;
>
> instead.
>
> The problem is clearly related to the platform-specific "macro_to_expand"
> routine that is used on SPU to implement the context-sensitive __vector
> keyword.
>
> With your changes to cpp_get_token (which is the sole caller of the
> macro_to_expand callback), are there any changes in the interface
> to the callback?

Not that I would expect.

>  Any suggestions what could be going on here?

Nothing obvious is coming to my mind right now, I am still looking
at the issue.

-- 
		Dodji

^ permalink raw reply	[flat|nested] 24+ messages in thread

* Re: [PATCH 2/6] Generate virtual locations for tokens
  2011-10-19 14:48   ` Ulrich Weigand
  2011-10-19 17:11     ` Dodji Seketeli
@ 2011-10-19 20:42     ` Dodji Seketeli
  1 sibling, 0 replies; 24+ messages in thread
From: Dodji Seketeli @ 2011-10-19 20:42 UTC (permalink / raw)
  To: Ulrich Weigand; +Cc: gcc-patches, jason, tromey, gdr, joseph, burnus, charlet

This is probably going to take some back and forth as I don't have any
SPU target at hand to debug, so I have filed
http://gcc.gnu.org/bugzilla/show_bug.cgi?id=50801 to track this issue,
along with a candidate fix.  Could you please test it and reply to the
bug with your results?

Thank you for your time and I apologize for the inconvenience.

-- 
		Dodji

^ permalink raw reply	[flat|nested] 24+ messages in thread

* Re: [PATCH 5/6] Add line map statistics to -fmem-report output
  2010-12-21  7:30   ` Gabriel Dos Reis
@ 2011-04-13 20:08     ` Dodji Seketeli
  0 siblings, 0 replies; 24+ messages in thread
From: Dodji Seketeli @ 2011-04-13 20:08 UTC (permalink / raw)
  To: Gabriel Dos Reis; +Cc: gcc-patches, tromey, joseph, lopezibanez

Hello,

Gabriel Dos Reis <gdr@integrable-solutions.net> writes:

> On Fri, Dec 10, 2010 at 5:11 AM, Dodji Seketeli <dodji@redhat.com> wrote:
> > This patch adds statistics about the memory consumption of line maps
> > to the output of -fmem-report. It has been useful in trying to reduce
> > the memory consumption of the macro maps support.
> >
> > Tested on x86_64-unknown-linux-gnu against trunk.
> >
> > gcc/
> >        * input.c (SCALE, STAT_LABEL, FORMAT_AMOUNT): New macros.
> >        (dump_line_table_statistics): Define new function.
> >        * input.h (dump_line_table_statistics): Declare new function.
> >        * toplev.c (dump_memory_report): Call dump_line_table_statistics.
>
> Can we give these `1024' some meaningfull symbolic names?
> SCALE is a bit a vague -- one has to look into its body to
> understand what it is doing, which defeats the purpose of abstraction.
> Also, those macro should be documented.

Agreed.  Done.

I have one additional question, though.  I actually copied these macros
from tree-flow.h; there are copied all over the place, by the way.  I
guess it would be good to have them defined at only on place.  But
where?  Ideally it would be header suitable to included both by input.c
and pretty much everything else.  Would input.h be good enough?
everywhere.  I guess this would be a separate patch independent from
this macro location business.

>
> Finally, this:
>
> > +  linemap_get_statistics (line_table,
> > +                         &num_ordinary_maps_allocated,
> > +                         &num_ordinary_maps_used,
> > +                         &ordinary_maps_allocated_size,
> > +                         &ordinary_maps_used_size,
> > +                         &num_macro_maps_used,
> > +                         &macro_maps_used_size,
> > +                         &macro_maps_locations_size,
> > +                         &duplicated_maps_locations_size,
> > +                         &total_allocated_map_size,
> > +                         &total_used_map_size);
>
> is a too impressive paramater list :-)
> Could you use a structure to package all monster?

Sure.  Done in the patch below.

>
> > +void linemap_get_statistics (struct line_maps *set,
> > +                            size_t *, size_t *,
> > +                            size_t *, size_t *,
> > +                            size_t *, size_t *,
> > +                            size_t *, size_t *,
> > +                            size_t *, size_t *);
>
> same here.

Fixed in the patch below as well.

Thank you for the review, and sorry for my late reply.

-- 
		Dodji

    gcc/
    	* input.c (SCALE, STAT_LABEL, FORMAT_AMOUNT): New macros.
    	(dump_line_table_statistics): Define new function.
    	* input.h (dump_line_table_statistics): Declare new function.
    	* toplev.c (dump_memory_report): Call dump_line_table_statistics.
    
    libcpp/
    	* line-map.h (linemap_get_statistics): Declare ...
    	* line-map.c (linemap_get_statistics):  ... new function.

diff --git a/gcc/input.c b/gcc/input.c
index 29e4de1..8701e4d 100644
--- a/gcc/input.c
+++ b/gcc/input.c
@@ -61,3 +61,74 @@ expand_location (source_location loc)
     }
   return xloc;
 }
+
+#define ONE_K 1024
+#define ONE_M ONE_K * ONE_K
+
+/* Display a number as an integer multiple of either:
+   - 1024, if said integer is >= to 10 K (in base 2)
+   - 1024 * 1024, if said integer is >= 10 M in (base 2)
+ */
+#define SCALE(x) ((unsigned long) ((x) < 10 * ONE_K \
+		  ? (x) \
+		  : ((x) < 10 * ONE_M \
+		     ? (x) / ONE_K \
+		     : (x) / (ONE_M))))
+
+/* For a given integer, display either:
+   - the character 'k', if the number is higher than 10 K (in base 2)
+     but strictly lower than 10 M (in base 2)
+   - the character 'M' if the number is higher than 10 M (in base2)
+   - the charcter ' ' if the number is strictly lower  than 10 K  */
+#define STAT_LABEL(x) ((x) < 10 * ONE_K ? ' ' : ((x) < 10 * ONE_M ? 'k' : 'M'))
+
+/* Display an integer amount as multiple of 1K or 1M (in base 2à).
+   Display the correct unit (either k, M, or ' ') after the amout, as
+   well.  */
+#define FORMAT_AMOUNT(size) SCALE (size), STAT_LABEL (size)
+
+/* Dump statistics to stderr about the memory usage of the line_table
+   set of line maps.  */
+
+void
+dump_line_table_statistics (void)
+{
+  struct linemap_stats s;
+
+  memset (&s, 0, sizeof (s));
+
+  linemap_get_statistics (line_table, &s);
+
+  fprintf (stderr, "\nLine Table allocations during the compilation process\n");
+  fprintf (stderr, "Total allocated maps size:           %5lu%c\n",
+	   SCALE (s.total_allocated_map_size),
+	   STAT_LABEL (s.total_allocated_map_size));
+  fprintf (stderr, "Total used maps size:                %5lu%c\n",
+	   SCALE (s.total_used_map_size),
+	   STAT_LABEL (s.total_used_map_size));
+  fprintf (stderr, "Ordinary map used size:              %5lu%c\n",
+	   SCALE (s.ordinary_maps_used_size),
+	   STAT_LABEL (s.ordinary_maps_used_size));
+  fprintf (stderr, "Macro maps used size:                %5lu%c\n",
+	   SCALE (s.macro_maps_used_size),
+	   STAT_LABEL (s.macro_maps_used_size));
+  fprintf (stderr, "Number of ordinary maps allocated:   %5lu%c\n",
+	   SCALE (s.num_ordinary_maps_allocated),
+	   STAT_LABEL (s.num_ordinary_maps_allocated));
+  fprintf (stderr, "Number of ordinary maps used:        %5lu%c\n",
+	   SCALE (s.num_ordinary_maps_used),
+	   STAT_LABEL (s.num_ordinary_maps_used));
+  fprintf (stderr, "Number of macro maps used:           %5lu%c\n",
+	   SCALE (s.num_macro_maps_used),
+	   STAT_LABEL (s.num_macro_maps_used));
+  fprintf (stderr, "Ordinary maps allocated size:        %5lu%c\n",
+	   SCALE (s.ordinary_maps_allocated_size),
+	   STAT_LABEL (s.ordinary_maps_allocated_size));
+  fprintf (stderr, "Macro maps locations size:           %5lu%c\n",
+	   SCALE (s.macro_maps_locations_size),
+	   STAT_LABEL (s.macro_maps_locations_size));
+  fprintf (stderr, "Duplicated maps locations size:      %5lu%c\n",
+	   SCALE (s.duplicated_macro_maps_locations_size),
+	   STAT_LABEL (s.duplicated_macro_maps_locations_size));
+  fprintf (stderr, "\n");
+}
diff --git a/gcc/input.h b/gcc/input.h
index 835c95a..ca122b5 100644
--- a/gcc/input.h
+++ b/gcc/input.h
@@ -55,4 +55,6 @@ extern location_t input_location;
   ((linemap_location_in_system_header_p (line_table, LOC)))
 #define in_system_header  (in_system_header_at (input_location))
 
+void dump_line_table_statistics (void);
+
 #endif
diff --git a/gcc/toplev.c b/gcc/toplev.c
index c0f6ee3..6f5792b 100644
--- a/gcc/toplev.c
+++ b/gcc/toplev.c
@@ -1818,6 +1818,7 @@ target_reinit (void)
 void
 dump_memory_report (bool final)
 {
+  dump_line_table_statistics ();
   ggc_print_statistics ();
   stringpool_statistics ();
   dump_tree_statistics ();
diff --git a/libcpp/include/line-map.h b/libcpp/include/line-map.h
index fdd0a71..3216049 100644
--- a/libcpp/include/line-map.h
+++ b/libcpp/include/line-map.h
@@ -641,4 +641,21 @@ expanded_location linemap_expand_location_full (struct line_maps *,
 						enum location_resolution_kind,
 						const struct line_map**);
 void linemap_dump_location (struct line_maps *, source_location, FILE *);
+
+struct linemap_stats
+{
+  size_t num_ordinary_maps_allocated;
+  size_t num_ordinary_maps_used;
+  size_t ordinary_maps_allocated_size;
+  size_t ordinary_maps_used_size;
+  size_t num_macro_maps_used;
+  size_t macro_maps_used_size;
+  size_t macro_maps_locations_size;
+  size_t duplicated_macro_maps_locations_size;
+  size_t total_allocated_map_size;
+  size_t total_used_map_size;
+};
+
+void linemap_get_statistics (struct line_maps *, struct linemap_stats *);
+
 #endif /* !LIBCPP_LINE_MAP_H  */
diff --git a/libcpp/line-map.c b/libcpp/line-map.c
index 4012f99..442dddc 100644
--- a/libcpp/line-map.c
+++ b/libcpp/line-map.c
@@ -1021,3 +1021,68 @@ linemap_dump_location (struct line_maps *set,
   fprintf (stream, "{P:%s;F:%s;L:%d;C:%d;S:%d;M:%p;E:%d,LOC:%d}",
 	   path, from, l, c, s, (void*)map, e, loc);
 }
+
+/* Compute and return statistics about the memory consumption of some
+   parts of the line table SET.  */
+
+void
+linemap_get_statistics (struct line_maps *set,
+			struct linemap_stats *s)
+{
+  size_t ordinary_maps_allocated_size, ordinary_maps_used_size,
+    macro_maps_allocated_size, macro_maps_used_size,
+    macro_maps_locations_size = 0, duplicated_macro_maps_locations_size = 0,
+    total_allocated_map_size, total_used_map_size;
+  struct line_map *cur_map;
+
+  ordinary_maps_allocated_size =
+    LINEMAPS_ORDINARY_ALLOCATED (set) * sizeof (struct line_map);
+
+  ordinary_maps_used_size =
+    LINEMAPS_ORDINARY_USED (set) * sizeof (struct line_map);
+
+  macro_maps_allocated_size =
+    LINEMAPS_MACRO_ALLOCATED (set) * sizeof (struct line_map);
+
+  for (cur_map = LINEMAPS_MACRO_MAPS (set);
+       cur_map && cur_map <= LINEMAPS_LAST_MACRO_MAP (set);
+       ++cur_map)
+    {
+      unsigned i;
+
+      linemap_assert (linemap_macro_expansion_map_p (cur_map));
+
+      macro_maps_locations_size +=
+	2 * MACRO_MAP_NUM_MACRO_TOKENS (cur_map) * sizeof (source_location);
+
+      for (i = 0; i < 2 * MACRO_MAP_NUM_MACRO_TOKENS (cur_map); i+=2)
+	{
+	  if (MACRO_MAP_LOCATIONS (cur_map)[i] ==
+	      MACRO_MAP_LOCATIONS (cur_map)[i + 1])
+	    duplicated_macro_maps_locations_size +=
+	      sizeof (source_location);
+	}
+    }
+
+  macro_maps_used_size =
+    LINEMAPS_MACRO_USED (set) * sizeof (struct line_map)
+    + macro_maps_locations_size;
+
+  total_used_map_size = ordinary_maps_used_size + macro_maps_used_size;
+
+  total_allocated_map_size =
+    ordinary_maps_allocated_size + macro_maps_allocated_size +
+    macro_maps_locations_size;
+
+  s->num_ordinary_maps_allocated = LINEMAPS_ORDINARY_ALLOCATED (set);
+  s->num_ordinary_maps_used = LINEMAPS_ORDINARY_USED (set);
+  s->ordinary_maps_allocated_size = ordinary_maps_allocated_size;
+  s->ordinary_maps_used_size = ordinary_maps_used_size;
+  s->num_macro_maps_used = LINEMAPS_MACRO_USED (set);
+  s->macro_maps_used_size = macro_maps_used_size;
+  s->macro_maps_locations_size = macro_maps_locations_size;
+  s->duplicated_macro_maps_locations_size =
+    duplicated_macro_maps_locations_size;
+  s->total_allocated_map_size = total_allocated_map_size;
+  s->total_used_map_size = total_used_map_size;
+}

^ permalink raw reply	[flat|nested] 24+ messages in thread

* Re: [PATCH 5/6] Add line map statistics to -fmem-report output
  2010-12-10 11:27 ` [PATCH 5/6] Add line map statistics to -fmem-report output Dodji Seketeli
@ 2010-12-21  7:30   ` Gabriel Dos Reis
  2011-04-13 20:08     ` Dodji Seketeli
  0 siblings, 1 reply; 24+ messages in thread
From: Gabriel Dos Reis @ 2010-12-21  7:30 UTC (permalink / raw)
  To: Dodji Seketeli; +Cc: gcc-patches, tromey, joseph, lopezibanez

On Fri, Dec 10, 2010 at 5:11 AM, Dodji Seketeli <dodji@redhat.com> wrote:
> This patch adds statistics about the memory consumption of line maps
> to the output of -fmem-report. It has been useful in trying to reduce
> the memory consumption of the macro maps support.
>
> Tested on x86_64-unknown-linux-gnu against trunk.
>
> gcc/
>        * input.c (SCALE, STAT_LABEL, FORMAT_AMOUNT): New macros.
>        (dump_line_table_statistics): Define new function.
>        * input.h (dump_line_table_statistics): Declare new function.
>        * toplev.c (dump_memory_report): Call dump_line_table_statistics.

Can we give these `1024' some meaningfull symbolic names?
SCALE is a bit a vague -- one has to look into its body to
understand what it is doing, which defeats the purpose of abstraction.
Also, those macro should be documented.

Finally, this:

> +  linemap_get_statistics (line_table,
> +                         &num_ordinary_maps_allocated,
> +                         &num_ordinary_maps_used,
> +                         &ordinary_maps_allocated_size,
> +                         &ordinary_maps_used_size,
> +                         &num_macro_maps_used,
> +                         &macro_maps_used_size,
> +                         &macro_maps_locations_size,
> +                         &duplicated_maps_locations_size,
> +                         &total_allocated_map_size,
> +                         &total_used_map_size);

is a too impressive paramater list :-)
Could you use a structure to package all monster?

> +void linemap_get_statistics (struct line_maps *set,
> +                            size_t *, size_t *,
> +                            size_t *, size_t *,
> +                            size_t *, size_t *,
> +                            size_t *, size_t *,
> +                            size_t *, size_t *);

same here.

^ permalink raw reply	[flat|nested] 24+ messages in thread

* [PATCH 5/6] Add line map statistics to -fmem-report output
  2010-12-10 11:27 [PATCH 0/6] Tracking locations of tokens resulting from macro expansion Dodji Seketeli
@ 2010-12-10 11:27 ` Dodji Seketeli
  2010-12-21  7:30   ` Gabriel Dos Reis
  0 siblings, 1 reply; 24+ messages in thread
From: Dodji Seketeli @ 2010-12-10 11:27 UTC (permalink / raw)
  To: gcc-patches; +Cc: tromey, joseph, gdr, lopezibanez

This patch adds statistics about the memory consumption of line maps
to the output of -fmem-report. It has been useful in trying to reduce
the memory consumption of the macro maps support.

Tested on x86_64-unknown-linux-gnu against trunk.

gcc/
	* input.c (SCALE, STAT_LABEL, FORMAT_AMOUNT): New macros.
	(dump_line_table_statistics): Define new function.
	* input.h (dump_line_table_statistics): Declare new function.
	* toplev.c (dump_memory_report): Call dump_line_table_statistics.

libcpp/
	* line-map.h (linemap_get_statistics): Declare ...
	* line-map.c (linemap_get_statistics):  ... new function.
---
 gcc/input.c               |   67 ++++++++++++++++++++++++++++++++
 gcc/input.h               |    2 +
 gcc/toplev.c              |    1 +
 libcpp/include/line-map.h |    7 +++
 libcpp/line-map.c         |   93 +++++++++++++++++++++++++++++++++++++++++++++
 5 files changed, 170 insertions(+), 0 deletions(-)

diff --git a/gcc/input.c b/gcc/input.c
index 6dbc253..6d265f4 100644
--- a/gcc/input.c
+++ b/gcc/input.c
@@ -75,3 +75,70 @@ expand_location (source_location loc)
     }
   return xloc;
 }
+
+#define SCALE(x) ((unsigned long) ((x) < 1024*10 \
+		  ? (x) \
+		  : ((x) < 1024*1024*10 \
+		     ? (x) / 1024 \
+		     : (x) / (1024*1024))))
+#define STAT_LABEL(x) ((x) < 1024*10 ? ' ' : ((x) < 1024*1024*10 ? 'k' : 'M'))
+
+#define FORMAT_AMOUNT(size) SCALE (size), STAT_LABEL (size)
+
+/* Dump statistics to stderr about the memory usage of the line_table
+   set of line maps.  */
+
+void
+dump_line_table_statistics (void)
+{
+  size_t num_ordinary_maps_allocated = 0, num_ordinary_maps_used = 0,
+    num_macro_maps_used = 0, macro_maps_used_size = 0,
+    ordinary_maps_allocated_size = 0, ordinary_maps_used_size = 0,
+    macro_maps_locations_size = 0, duplicated_maps_locations_size = 0,
+    total_allocated_map_size = 0, total_used_map_size = 0;
+
+  linemap_get_statistics (line_table,
+			  &num_ordinary_maps_allocated,
+			  &num_ordinary_maps_used,
+			  &ordinary_maps_allocated_size,
+			  &ordinary_maps_used_size,
+			  &num_macro_maps_used,
+			  &macro_maps_used_size,
+			  &macro_maps_locations_size,
+			  &duplicated_maps_locations_size,
+			  &total_allocated_map_size,
+			  &total_used_map_size);
+
+  fprintf (stderr, "\nLine Table allocations during the compilation process\n");
+  fprintf (stderr, "Total allocated maps size:           %5lu%c\n",
+	   SCALE (total_allocated_map_size),
+	   STAT_LABEL (total_allocated_map_size));
+  fprintf (stderr, "Total used maps size:                %5lu%c\n",
+	   SCALE (total_used_map_size),
+	   STAT_LABEL (total_used_map_size));
+  fprintf (stderr, "Ordinary map used size:              %5lu%c\n",
+	   SCALE (ordinary_maps_used_size),
+	   STAT_LABEL (ordinary_maps_used_size));
+  fprintf (stderr, "Macro maps used size:                %5lu%c\n",
+	   SCALE (macro_maps_used_size),
+	   STAT_LABEL (macro_maps_used_size));
+  fprintf (stderr, "Number of ordinary maps allocated:   %5lu%c\n",
+	   SCALE (num_ordinary_maps_allocated),
+	   STAT_LABEL (num_ordinary_maps_allocated));
+  fprintf (stderr, "Number of ordinary maps used:        %5lu%c\n",
+	   SCALE (num_ordinary_maps_used),
+	   STAT_LABEL (num_ordinary_maps_used));
+  fprintf (stderr, "Number of macro maps used:           %5lu%c\n",
+	   SCALE (num_macro_maps_used),
+	   STAT_LABEL (num_macro_maps_used));
+  fprintf (stderr, "Ordinary maps allocated size:        %5lu%c\n",
+	   SCALE (ordinary_maps_allocated_size),
+	   STAT_LABEL (ordinary_maps_allocated_size));
+  fprintf (stderr, "Macro maps locations size:           %5lu%c\n",
+	   SCALE (macro_maps_locations_size),
+	   STAT_LABEL (macro_maps_locations_size));
+  fprintf (stderr, "Duplicated maps locations size:      %5lu%c\n",
+	   SCALE (duplicated_maps_locations_size),
+	   STAT_LABEL (duplicated_maps_locations_size));
+  fprintf (stderr, "\n");
+}
diff --git a/gcc/input.h b/gcc/input.h
index 835c95a..ca122b5 100644
--- a/gcc/input.h
+++ b/gcc/input.h
@@ -55,4 +55,6 @@ extern location_t input_location;
   ((linemap_location_in_system_header_p (line_table, LOC)))
 #define in_system_header  (in_system_header_at (input_location))
 
+void dump_line_table_statistics (void);
+
 #endif
diff --git a/gcc/toplev.c b/gcc/toplev.c
index 78985cb..c86bc90 100644
--- a/gcc/toplev.c
+++ b/gcc/toplev.c
@@ -1786,6 +1786,7 @@ target_reinit (void)
 void
 dump_memory_report (bool final)
 {
+  dump_line_table_statistics ();
   ggc_print_statistics ();
   stringpool_statistics ();
   dump_tree_statistics ();
diff --git a/libcpp/include/line-map.h b/libcpp/include/line-map.h
index 3f58e03..36e7ad2 100644
--- a/libcpp/include/line-map.h
+++ b/libcpp/include/line-map.h
@@ -641,4 +641,11 @@ expanded_location linemap_expand_location_full (struct line_maps *,
 						enum location_resolution_kind,
 						const struct line_map**);
 void linemap_dump_location (struct line_maps *, source_location, FILE *);
+
+void linemap_get_statistics (struct line_maps *set,
+			     size_t *, size_t *,
+			     size_t *, size_t *,
+			     size_t *, size_t *,
+			     size_t *, size_t *,
+			     size_t *, size_t *);
 #endif /* !LIBCPP_LINE_MAP_H  */
diff --git a/libcpp/line-map.c b/libcpp/line-map.c
index a924912..c05c9d5 100644
--- a/libcpp/line-map.c
+++ b/libcpp/line-map.c
@@ -1019,3 +1019,96 @@ linemap_dump_location (struct line_maps *set,
   fprintf (stream, "{P:%s;F:%s;L:%d;C:%d;S:%d;M:%p;E:%d,LOC:%d}",
 	   path, from, l, c, s, (void*)map, e, loc);
 }
+
+/* Compute and return statistics about the memory consumption of some
+   parts of the line table SET.  */
+
+void
+linemap_get_statistics (struct line_maps *set,
+			size_t *num_ordinary_maps_allocated_ptr,
+			size_t *num_ordinary_maps_used_ptr,
+			size_t *ordinary_maps_allocated_size_ptr,
+			size_t *ordinary_maps_used_size_ptr,
+			size_t *num_macro_maps_used_ptr,
+			size_t *macro_maps_used_size_ptr,
+			size_t *macro_maps_locations_size_ptr,
+			size_t *duplicated_macro_maps_locations_size_ptr,
+			size_t *total_allocated_map_size_ptr,
+			size_t *total_used_map_size_ptr)
+{
+  size_t ordinary_maps_allocated_size, ordinary_maps_used_size,
+    macro_maps_allocated_size, macro_maps_used_size,
+    macro_maps_locations_size = 0, duplicated_macro_maps_locations_size = 0,
+    total_allocated_map_size, total_used_map_size;
+  struct line_map *cur_map;
+
+  ordinary_maps_allocated_size =
+    LINEMAPS_ORDINARY_ALLOCATED (set) * sizeof (struct line_map);
+
+  ordinary_maps_used_size =
+    LINEMAPS_ORDINARY_USED (set) * sizeof (struct line_map);
+
+  macro_maps_allocated_size =
+    LINEMAPS_MACRO_ALLOCATED (set) * sizeof (struct line_map);
+
+  for (cur_map = LINEMAPS_MACRO_MAPS (set);
+       cur_map && cur_map <= LINEMAPS_LAST_MACRO_MAP (set);
+       ++cur_map)
+    {
+      unsigned i;
+
+      linemap_assert (linemap_macro_expansion_map_p (cur_map));
+
+      macro_maps_locations_size +=
+	2 * MACRO_MAP_NUM_MACRO_TOKENS (cur_map) * sizeof (source_location);
+
+      for (i = 0; i < 2 * MACRO_MAP_NUM_MACRO_TOKENS (cur_map); i+=2)
+	{
+	  if (MACRO_MAP_LOCATIONS (cur_map)[i] ==
+	      MACRO_MAP_LOCATIONS (cur_map)[i + 1])
+	    duplicated_macro_maps_locations_size +=
+	      sizeof (source_location);
+	}
+    }
+
+  macro_maps_used_size =
+    LINEMAPS_MACRO_USED (set) * sizeof (struct line_map)
+    + macro_maps_locations_size;
+
+  total_used_map_size = ordinary_maps_used_size + macro_maps_used_size;
+
+  total_allocated_map_size =
+    ordinary_maps_allocated_size + macro_maps_allocated_size +
+    macro_maps_locations_size;
+
+  if (num_ordinary_maps_allocated_ptr)
+    *num_ordinary_maps_allocated_ptr = LINEMAPS_ORDINARY_ALLOCATED (set);
+
+  if (num_ordinary_maps_used_ptr)
+    *num_ordinary_maps_used_ptr = LINEMAPS_ORDINARY_USED (set);
+
+  if (ordinary_maps_allocated_size_ptr)
+    *ordinary_maps_allocated_size_ptr = ordinary_maps_allocated_size;
+
+  if (ordinary_maps_used_size_ptr)
+    *ordinary_maps_used_size_ptr = ordinary_maps_used_size;
+
+  if (num_macro_maps_used_ptr)
+    *num_macro_maps_used_ptr = LINEMAPS_MACRO_USED (set);
+
+  if (macro_maps_used_size_ptr)
+    *macro_maps_used_size_ptr = macro_maps_used_size;
+
+  if (macro_maps_locations_size_ptr)
+    *macro_maps_locations_size_ptr = macro_maps_locations_size;
+
+  if (duplicated_macro_maps_locations_size_ptr)
+    *duplicated_macro_maps_locations_size_ptr =
+      duplicated_macro_maps_locations_size;
+
+  if (total_allocated_map_size_ptr)
+    *total_allocated_map_size_ptr = total_allocated_map_size;
+
+  if (total_used_map_size_ptr)
+    *total_used_map_size_ptr = total_used_map_size;
+}
-- 
        Dodji

^ permalink raw reply	[flat|nested] 24+ messages in thread

end of thread, other threads:[~2011-10-19 20:08 UTC | newest]

Thread overview: 24+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2011-10-17  9:58 [PATCH 0/6] Tracking locations of tokens resulting from macro expansion Dodji Seketeli
2011-10-17  9:58 ` [PATCH 3/6] Emit macro expansion related diagnostics Dodji Seketeli
2011-10-17 10:56   ` Richard Guenther
2011-10-17 12:22     ` Dodji Seketeli
2011-10-17 14:11       ` Dodji Seketeli
2011-10-17 17:41         ` H.J. Lu
2011-10-18  0:29           ` Dodji Seketeli
2011-10-18  6:07             ` Jason Merrill
2011-10-18  9:22               ` Dodji Seketeli
2011-10-17 10:08 ` [PATCH 4/6] Support -fdebug-cpp option Dodji Seketeli
2011-10-17 10:19 ` [PATCH 6/6] Reduce memory waste due to non-power-of-2 allocs Dodji Seketeli
2011-10-17 10:22 ` [PATCH 5/6] Add line map statistics to -fmem-report output Dodji Seketeli
2011-10-17 10:25 ` [PATCH 1/6] Linemap infrastructure for virtual locations Dodji Seketeli
2011-10-17 19:53   ` Gerald Pfeifer
2011-10-17 20:44     ` Dodji Seketeli
2011-10-18  7:24       ` Gerald Pfeifer
2011-10-18  9:44         ` Dodji Seketeli
2011-10-17 10:26 ` [PATCH 2/6] Generate virtual locations for tokens Dodji Seketeli
2011-10-19 14:48   ` Ulrich Weigand
2011-10-19 17:11     ` Dodji Seketeli
2011-10-19 20:42     ` Dodji Seketeli
  -- strict thread matches above, loose matches on Subject: below --
2010-12-10 11:27 [PATCH 0/6] Tracking locations of tokens resulting from macro expansion Dodji Seketeli
2010-12-10 11:27 ` [PATCH 5/6] Add line map statistics to -fmem-report output Dodji Seketeli
2010-12-21  7:30   ` Gabriel Dos Reis
2011-04-13 20:08     ` Dodji Seketeli

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).