public inbox for gcc-patches@gcc.gnu.org
 help / color / mirror / Atom feed
* [PATCH 4/6] Support -fdebug-cpp option
  2010-12-10 11:27 [PATCH 0/6] Tracking locations of tokens resulting from macro expansion Dodji Seketeli
  2010-12-10 11:16 ` [PATCH 0/6] *** SUBJECT HERE *** Dodji Seketeli
@ 2010-12-10 11:16 ` Dodji Seketeli
  2010-12-10 11:27 ` [PATCH 3/6] Emit macro expansion related diagnostics Dodji Seketeli
                   ` (7 subsequent siblings)
  9 siblings, 0 replies; 135+ messages in thread
From: Dodji Seketeli @ 2010-12-10 11:16 UTC (permalink / raw)
  To: gcc-patches; +Cc: tromey, joseph, gdr, lopezibanez

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.

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

libcpp/

	* 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/

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

gcc/c-family/

	* 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.
---
 gcc/c-family/c-opts.c     |    4 +++
 gcc/c-family/c-ppoutput.c |   57 ++++++++++++++++++++++++++++++++++++--------
 gcc/c-family/c.opt        |    4 +++
 gcc/doc/cppopts.texi      |   12 +++++++++
 gcc/doc/invoke.texi       |    2 +-
 libcpp/include/cpplib.h   |    4 +++
 libcpp/include/line-map.h |    1 +
 libcpp/line-map.c         |   38 ++++++++++++++++++++++++++++++
 8 files changed, 110 insertions(+), 12 deletions(-)

diff --git a/gcc/c-family/c-opts.c b/gcc/c-family/c-opts.c
index 4c2e37e..2a00077 100644
--- a/gcc/c-family/c-opts.c
+++ b/gcc/c-family/c-opts.c
@@ -621,6 +621,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 0def8fa..446a261 100644
--- a/gcc/c-family/c-ppoutput.c
+++ b/gcc/c-family/c-ppoutput.c
@@ -58,7 +58,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);
@@ -241,7 +243,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);
@@ -295,14 +302,15 @@ 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);
   /* End the previous line of text.  */
   if (print.printed)
     {
-      putc ('\n', print.outf);
+      putc ('\n', stream);
       print.src_line++;
       print.printed = 0;
     }
@@ -311,22 +319,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)
@@ -346,20 +369,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 25d0cb6..c502c89 100644
--- a/gcc/c-family/c.opt
+++ b/gcc/c-family/c.opt
@@ -719,6 +719,10 @@ fconstant-string-class=
 ObjC ObjC++ Joined MissingArgError(no class name specified with %qs)
 -fconst-string-class=<name>	Use class <name> for constant strings
 
+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 a4f8b32..4dff6d5 100644
--- a/gcc/doc/cppopts.texi
+++ b/gcc/doc/cppopts.texi
@@ -583,6 +583,18 @@ 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
+When used with @option{-E}, dump 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 07f2b20..0fe924b 100644
--- a/gcc/doc/invoke.texi
+++ b/gcc/doc/invoke.texi
@@ -415,7 +415,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 783576b..909409e 100644
--- a/libcpp/include/cpplib.h
+++ b/libcpp/include/cpplib.h
@@ -388,6 +388,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 0011f38..3f58e03 100644
--- a/libcpp/include/line-map.h
+++ b/libcpp/include/line-map.h
@@ -640,4 +640,5 @@ expanded_location linemap_expand_location_full (struct line_maps *,
 						source_location,
 						enum location_resolution_kind,
 						const struct line_map**);
+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 aeccdee..a924912 100644
--- a/libcpp/line-map.c
+++ b/libcpp/line-map.c
@@ -981,3 +981,41 @@ linemap_expand_location_full (struct line_maps *set,
 
   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_macro_loc_to_def_point (set, loc, &map, true);
+  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);
+}
-- 
        Dodji

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

* [PATCH 0/6] *** SUBJECT HERE ***
  2010-12-10 11:27 [PATCH 0/6] Tracking locations of tokens resulting from macro expansion Dodji Seketeli
@ 2010-12-10 11:16 ` Dodji Seketeli
  2010-12-10 12:56   ` Dave Korn
  2010-12-10 11:16 ` [PATCH 4/6] Support -fdebug-cpp option Dodji Seketeli
                   ` (8 subsequent siblings)
  9 siblings, 1 reply; 135+ messages in thread
From: Dodji Seketeli @ 2010-12-10 11:16 UTC (permalink / raw)
  To: gcc-patches; +Cc: tromey, joseph, gdr, lopezibanez

*** BLURB HERE ***

Dodji Seketeli (6):
  Linemap infrastructure for virtual locations
  Generate virtual locations for tokens
  Emit macro expansion related diagnostics
  Support -fdebug-cpp option
  Add line map statistics to -fmem-report output
  Kill pedantic warnings on system headers macros

 gcc/Makefile.in                                 |    2 +-
 gcc/ada/gcc-interface/trans.c                   |   10 +-
 gcc/c-decl.c                                    |   17 +-
 gcc/c-family/c-lex.c                            |   10 +-
 gcc/c-family/c-opts.c                           |   17 +
 gcc/c-family/c-pch.c                            |    2 +-
 gcc/c-family/c-ppoutput.c                       |   92 ++-
 gcc/c-family/c.opt                              |   12 +
 gcc/c-parser.c                                  |   12 +-
 gcc/c-tree.h                                    |    2 +-
 gcc/cp/error.c                                  |    2 +-
 gcc/diagnostic.c                                |  160 +++-
 gcc/diagnostic.h                                |    2 +-
 gcc/doc/cppopts.texi                            |   29 +
 gcc/doc/invoke.texi                             |    6 +-
 gcc/fortran/cpp.c                               |   22 +-
 gcc/input.c                                     |  107 ++-
 gcc/input.h                                     |   22 +-
 gcc/java/jcf-parse.c                            |    2 +-
 gcc/testsuite/g++.dg/cpp0x/initlist15.C         |    1 +
 gcc/testsuite/g++.old-deja/g++.robertl/eb43.C   |    4 +
 gcc/testsuite/g++.old-deja/g++.robertl/eb79.C   |    4 +
 gcc/testsuite/gcc.dg/cpp/macro-exp-tracking-1.c |   30 +
 gcc/testsuite/gcc.dg/cpp/macro-exp-tracking-2.c |   31 +
 gcc/testsuite/gcc.dg/cpp/macro-exp-tracking-3.c |   18 +
 gcc/testsuite/gcc.dg/cpp/macro-exp-tracking-4.c |   19 +
 gcc/testsuite/gcc.dg/cpp/syshdr3.c              |   16 +
 gcc/testsuite/gcc.dg/cpp/syshdr3.h              |    7 +
 gcc/testsuite/gcc.dg/nofixed-point-2.c          |    6 +-
 gcc/testsuite/gcc.target/i386/sse-vect-types.c  |    6 +
 gcc/toplev.c                                    |    1 +
 gcc/tree-diagnostic.c                           |    2 +-
 libcpp/directives-only.c                        |    7 +-
 libcpp/directives.c                             |   19 +-
 libcpp/errors.c                                 |   21 +-
 libcpp/expr.c                                   |  176 ++--
 libcpp/files.c                                  |   24 +-
 libcpp/include/cpp-id-data.h                    |    6 +
 libcpp/include/cpplib.h                         |   15 +-
 libcpp/include/line-map.h                       |  634 +++++++++++--
 libcpp/init.c                                   |    3 +-
 libcpp/internal.h                               |   49 +-
 libcpp/lex.c                                    |  112 ++-
 libcpp/line-map.c                               |  997 +++++++++++++++++--
 libcpp/macro.c                                  | 1188 ++++++++++++++++++++---
 libcpp/traditional.c                            |    7 +-
 46 files changed, 3376 insertions(+), 555 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/syshdr3.c
 create mode 100644 gcc/testsuite/gcc.dg/cpp/syshdr3.h

-- 
        Dodji

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

* [PATCH 0/6] Tracking locations of tokens resulting from macro expansion
@ 2010-12-10 11:27 Dodji Seketeli
  2010-12-10 11:16 ` [PATCH 0/6] *** SUBJECT HERE *** Dodji Seketeli
                   ` (9 more replies)
  0 siblings, 10 replies; 135+ messages in thread
From: Dodji Seketeli @ 2010-12-10 11:27 UTC (permalink / raw)
  To: gcc-patches; +Cc: tromey, joseph, gdr, lopezibanez

* Problem statement

Let's consider the diagnostic GCC emits when an error occurs on an
expression that was defined in a macro that is later expanded:

[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 test.c
test.c: In function ‘g’:
test.c:13:3: error: invalid operands to binary << (have ‘double’ and ‘int’)

Just looking at line 13 as the diagnostic suggests is not enough to
understand why there is an error there.

On line 13, there is no "<<" token, but the compiler complains about
an opereator "<<" that is taking the wrong types of arguments on
/that/ line. Why?

Obviously what happened is that the MULT macro defined at line 7 and 8
got expanded. The macro SHIFTL used by MULT and defined at lines 1 and
2 got expanded as well. That macro expansion fest yielded the
expression:

  1.0 << 1;

which is an error as you can't shift a float.

The problem is tokens '1.0', '<<' and '1' all have their location set
to {line 11, column 3} ({11, 3} for short). That actually is the
location of the expansion point of the MULT macro. That's is an
interesting observation -- in the current incarnation of GCC the
location of each tokens resulting from a macro expansion is set to the
location of the expansion point of the macro.

* Path for improvement

For each token resulting from macro expansion it would be nice if
GCC could track two other kinds of locations for tokens resulting from
macro expansions:

    - Spelling location. The location of the point where the token
      appears in the definition of the macro. The spelling location of
      "<<" would be {5, 14}.

    - Macro argument replacement point location. This one only exists
      for a token that is and arguments of a function-like macro. It's
      the location of the point in the definition of the macro where
      the argument replaces the parameter. In other words it's the
      location of the point where the parameter of a given argument is
      used in the definition of a function-like macro. For the "<<"
      token, that would be the location {2,9} of the 'OPRT' token in
      the definition of the 'OPERATE' macro.
      
By using the three locations exposed above (spelling, argument
replacement point and expansion point locations), GCC could emit
diagnostics that are hopefully more useful. E.g:

[dodji@adjoa gcc]$ ./cc1 -quiet -ftrack-macro-expansion test.c
test.c: In function ‘g’:
test.c:5:14: error: invalid operands to binary << (have ‘double’ and ‘int’)
In macro 'OPERATE' at test.c:2:9
    Expanded at test.c:5:3
In macro 'SHIFTL' at test.c:5:14
    Expanded at test.c:8:3
In macro 'MULT' at test.c:8:3
    Expanded at test.c:13:3

Several obversations can be made in that hypothetical example.

- The location mentioned in the error message

 test.c:5:14: error: invalid operands to binary << (have ‘double’ and ‘int’)

is the spelling location of the token '<<'. I believe this makes more
sense than the original behaviour.

- The macro expansion stack following the error message unwinds from
the macro which expansion most directly yielded token "<<". Each
element of the stack mentions the argument replacement point and the
expansion point locations that were exposed earlier.

* What it would take

Luckily Tom Tromey attached a patch to this bug
http://gcc.gnu.org/bugzilla/show_bug.cgi?id=7263

That patch paved the way showing how to build on the locations maps
provided by libcpp to come up with what I would call virtual
locations.

A virtual location is a location that can resolve to many
possible physical locuses. For a token that does not result from macro
a expansion, a virtual location resolves only to the spelling location
of the token. For a token resulting from macro expansion, a virtual
location can resolve to either:
    [I would bet you heard about these different kinds of locations
     already but here we go]
    - the expansion point of the macro
    - the point where the token appears in the definition of the macro
    - the argument replacement point, in the context of a
      function-like macro.

So I took that patch and massaged it a little bit. The user facing
result is actually what I was showing in the [finally not so]
hypothetical improved GCC example earlier.

With the resulting patch-set the linemap module now provides and API
to generate and resolve virtual locations for tokens. When a
diagnostic function emit a message about a virtual location, it now
can detect [using the new linemap API] if that location belongs to a
token resulting from macro expansion. If yes, it can print the macro
expansion stack that led to that token.

So almost nothing changes for the diagnostics clients.

* Limits

I guess a pure marketing dude would elide this part :-)

The most annoying limit is obviously memory consumption. Tracking the
location of every token across every macro expansion does consume
memory. I tried compiling some a translation here that heavily uses
the C++ standard library and I could see some 13% increase of memory
consumption over the entire compilation; note that the compilation was
consuming around 250MB without the feature. This is why the feature is
triggered by the -ftrack-macro-expansion flag. Without that flag, the
memory consumption stays roughly flat. The noticeable downside of that
flag is that it makes the code more complex. I think there possibly is
room to decrease this, but fundamentaly the consumption is going to
increase with the number of macro expanded multiplied by the number of
tokens per expansion.

The patch-set I have today doesn't track the locations of tokens
resulting from pasting and stringification. For those, only the
spelling token is tracked for now. I think this can be improved. I
just haven't thought about it deeply yet.

* Perspectives

The patch-set is basically the point A.0) presented in the document
http://gcc.gnu.org/wiki/Better_Diagnostics.

This is material for GCC 4.7 at best but I felt it would be
interesting to post this now. So the patches will follow shortly.

For now please find below the summary of the changes.


  Linemap infrastructure for virtual locations
  Generate virtual locations for tokens
  Emit macro expansion related diagnostics
  Support -fdebug-cpp option
  Add line map statistics to -fmem-report output
  Kill pedantic warnings on system headers macros

 gcc/Makefile.in                                 |    2 +-
 gcc/ada/gcc-interface/trans.c                   |   10 +-
 gcc/c-decl.c                                    |   17 +-
 gcc/c-family/c-lex.c                            |   10 +-
 gcc/c-family/c-opts.c                           |   17 +
 gcc/c-family/c-pch.c                            |    2 +-
 gcc/c-family/c-ppoutput.c                       |   92 ++-
 gcc/c-family/c.opt                              |   12 +
 gcc/c-parser.c                                  |   12 +-
 gcc/c-tree.h                                    |    2 +-
 gcc/cp/error.c                                  |    2 +-
 gcc/diagnostic.c                                |  160 +++-
 gcc/diagnostic.h                                |    2 +-
 gcc/doc/cppopts.texi                            |   29 +
 gcc/doc/invoke.texi                             |    6 +-
 gcc/fortran/cpp.c                               |   22 +-
 gcc/input.c                                     |  107 ++-
 gcc/input.h                                     |   22 +-
 gcc/java/jcf-parse.c                            |    2 +-
 gcc/testsuite/g++.dg/cpp0x/initlist15.C         |    1 +
 gcc/testsuite/g++.old-deja/g++.robertl/eb43.C   |    4 +
 gcc/testsuite/g++.old-deja/g++.robertl/eb79.C   |    4 +
 gcc/testsuite/gcc.dg/cpp/macro-exp-tracking-1.c |   30 +
 gcc/testsuite/gcc.dg/cpp/macro-exp-tracking-2.c |   31 +
 gcc/testsuite/gcc.dg/cpp/macro-exp-tracking-3.c |   18 +
 gcc/testsuite/gcc.dg/cpp/macro-exp-tracking-4.c |   19 +
 gcc/testsuite/gcc.dg/cpp/syshdr3.c              |   16 +
 gcc/testsuite/gcc.dg/cpp/syshdr3.h              |    7 +
 gcc/testsuite/gcc.dg/nofixed-point-2.c          |    6 +-
 gcc/testsuite/gcc.target/i386/sse-vect-types.c  |    6 +
 gcc/toplev.c                                    |    1 +
 gcc/tree-diagnostic.c                           |    2 +-
 libcpp/directives-only.c                        |    7 +-
 libcpp/directives.c                             |   19 +-
 libcpp/errors.c                                 |   21 +-
 libcpp/expr.c                                   |  176 ++--
 libcpp/files.c                                  |   24 +-
 libcpp/include/cpp-id-data.h                    |    6 +
 libcpp/include/cpplib.h                         |   15 +-
 libcpp/include/line-map.h                       |  634 +++++++++++--
 libcpp/init.c                                   |    3 +-
 libcpp/internal.h                               |   49 +-
 libcpp/lex.c                                    |  112 ++-
 libcpp/line-map.c                               |  997 +++++++++++++++++--
 libcpp/macro.c                                  | 1188 ++++++++++++++++++++---
 libcpp/traditional.c                            |    7 +-
 46 files changed, 3376 insertions(+), 555 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/syshdr3.c
 create mode 100644 gcc/testsuite/gcc.dg/cpp/syshdr3.h

-- 
        Dodji

^ permalink raw reply	[flat|nested] 135+ 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
                   ` (2 preceding siblings ...)
  2010-12-10 11:27 ` [PATCH 3/6] Emit macro expansion related diagnostics Dodji Seketeli
@ 2010-12-10 11:27 ` Dodji Seketeli
  2010-12-21  7:30   ` Gabriel Dos Reis
  2010-12-10 11:53 ` [PATCH 1/6] Linemap infrastructure for virtual locations Dodji Seketeli
                   ` (5 subsequent siblings)
  9 siblings, 1 reply; 135+ 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] 135+ messages in thread

* [PATCH 3/6] Emit macro expansion related diagnostics
  2010-12-10 11:27 [PATCH 0/6] Tracking locations of tokens resulting from macro expansion Dodji Seketeli
  2010-12-10 11:16 ` [PATCH 0/6] *** SUBJECT HERE *** Dodji Seketeli
  2010-12-10 11:16 ` [PATCH 4/6] Support -fdebug-cpp option Dodji Seketeli
@ 2010-12-10 11:27 ` Dodji Seketeli
  2010-12-13 15:25   ` Paolo Bonzini
  2010-12-10 11:27 ` [PATCH 5/6] Add line map statistics to -fmem-report output Dodji Seketeli
                   ` (6 subsequent siblings)
  9 siblings, 1 reply; 135+ messages in thread
From: Dodji Seketeli @ 2010-12-10 11:27 UTC (permalink / raw)
  To: gcc-patches; +Cc: tromey, joseph, gdr, lopezibanez

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: error: invalid operands to binary << (have ‘double’ and ‘int’)
In macro 'OPERATE' at test.c:2:9
    Expanded at test.c:5:3
In macro 'SHIFTL' at test.c:5:14
    Expanded at test.c:8:3
In macro 'MULT' at test.c:8:3
    Expanded at test.c:13:3

The combination of this patch and the previous ones boostrapped with
--enable-languages=all,ada and passed regression tests on
x86_64-unknown-linux-gnu.

gcc/

	* gcc/diagnostic.h (diagnostic_report_current_module): Add a
	location parameter.
	* diagnostic.c (struct loc_t): new struct.
	(diagnostic_report_current_module): Add a location parameter to
	the function definition. Use it here instead of input_location.
	Fully expand the 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.
	(unwind_expanded_macro_location): New function.
	(default_diagnostic_finalizer): Use it.
	* tree-diagnostic.c (diagnostic_report_current_function): Pass the
	relevant location to diagnostic_report_current_module.

gcc/cp/

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

gcc/testsuite/

	* 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.
	* g++.dg/cpp0x/initlist15.C: Discard errors pointing at multiple
	levels of included files.
	* g++.old-deja/g++.robertl/eb43.C: Likewise.
	* g++.old-deja/g++.robertl/eb79.C: Likewise.
	* gcc.target/i386/sse-vect-types.c: Likewise.
---
 gcc/Makefile.in                                 |    2 +-
 gcc/cp/error.c                                  |    2 +-
 gcc/diagnostic.c                                |  154 ++++++++++++++++++++++-
 gcc/diagnostic.h                                |    2 +-
 gcc/testsuite/g++.dg/cpp0x/initlist15.C         |    1 +
 gcc/testsuite/g++.old-deja/g++.robertl/eb43.C   |    4 +
 gcc/testsuite/g++.old-deja/g++.robertl/eb79.C   |    4 +
 gcc/testsuite/gcc.dg/cpp/macro-exp-tracking-1.c |   30 +++++
 gcc/testsuite/gcc.dg/cpp/macro-exp-tracking-2.c |   31 +++++
 gcc/testsuite/gcc.dg/cpp/macro-exp-tracking-3.c |   18 +++
 gcc/testsuite/gcc.dg/cpp/macro-exp-tracking-4.c |   19 +++
 gcc/testsuite/gcc.target/i386/sse-vect-types.c  |    6 +
 gcc/tree-diagnostic.c                           |    2 +-
 13 files changed, 266 insertions(+), 9 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

diff --git a/gcc/Makefile.in b/gcc/Makefile.in
index 717326c..604f626 100644
--- a/gcc/Makefile.in
+++ b/gcc/Makefile.in
@@ -2825,7 +2825,7 @@ fold-const.o : fold-const.c $(CONFIG_H) $(SYSTEM_H) coretypes.h $(TM_H) \
    $(GGC_H) $(TM_P_H) langhooks.h $(MD5_H) intl.h $(TARGET_H) \
    $(GIMPLE_H) realmpfr.h $(TREE_FLOW_H)
 diagnostic.o : diagnostic.c $(CONFIG_H) $(SYSTEM_H) coretypes.h \
-   version.h $(INPUT_H) intl.h $(DIAGNOSTIC_H) diagnostic.def
+   version.h $(INPUT_H) intl.h $(DIAGNOSTIC_H) diagnostic.def $(VEC_H)
 opts.o : opts.c $(OPTS_H) $(OPTIONS_H) $(DIAGNOSTIC_CORE_H) $(CONFIG_H) $(SYSTEM_H) \
    coretypes.h $(TM_H) $(RTL_H) \
    $(DIAGNOSTIC_H) $(INSN_ATTR_H) intl.h $(TARGET_H) \
diff --git a/gcc/cp/error.c b/gcc/cp/error.c
index ed168c4..db35c61 100644
--- a/gcc/cp/error.c
+++ b/gcc/cp/error.c
@@ -2663,7 +2663,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);
diff --git a/gcc/diagnostic.c b/gcc/diagnostic.c
index 9df540b..671b6ea 100644
--- a/gcc/diagnostic.c
+++ b/gcc/diagnostic.c
@@ -30,6 +30,15 @@ along with GCC; see the file COPYING3.  If not see
 #include "input.h"
 #include "intl.h"
 #include "diagnostic.h"
+#include "vec.h"
+
+typedef struct 
+{
+  const struct line_map *map;
+  source_location where;
+} loc_t;
+DEF_VEC_O(loc_t);
+DEF_VEC_ALLOC_O (loc_t,heap);
 
 #define pedantic_warning_kind(DC)			\
   ((DC)->pedantic_errors ? DK_ERROR : DK_WARNING)
@@ -45,6 +54,9 @@ static void diagnostic_action_after_output (diagnostic_context *,
 					    diagnostic_info *);
 static void real_abort (void) ATTRIBUTE_NORETURN;
 
+static void unwind_expanded_macro_location (diagnostic_context *, source_location,
+					    const struct line_map **);
+
 /* Name of program invoked, sans directories.  */
 
 const char *progname;
@@ -254,10 +266,138 @@ diagnostic_action_after_output (diagnostic_context *context,
     }
 }
 
+/* Unwind the different macro expansions that lead to the token which
+   location is WHERE and emit diagnostics showing the resulting
+   unwound macro expansion stack. If TOPMOST_EXP_POINT_MAP is
+   non-null, *TOPMOST_EXP_POINT_MAP is set to the map of the
+   expansion point of the top most macro of the stack. This must be
+   an ordinary map.  */
+
+static void
+unwind_expanded_macro_location (diagnostic_context *context,
+				source_location where,
+				const struct line_map **topmost_exp_point_map)
+{
+  const struct line_map *map, *resolved_map;
+  bool unwind = true;
+  source_location resolved_location;
+  VEC(loc_t, heap) *loc_vec;
+  unsigned ix, len;
+  loc_t loc, *iter;
+
+  map = linemap_lookup (line_table, where);
+  if (!linemap_macro_expansion_map_p (map))
+    return;
+
+  loc_vec = VEC_alloc (loc_t, heap, 4);
+
+  /* Let's unwind the stack of macros that got expanded and that led
+     to the token which location is WHERE. We are going to store the
+     stack into MAP_VEC, so that we can later walk MAP_VEC backward to
+     display a somewhat meaningful trace of the macro expansion
+     history to the user.  Note that the deepest macro expansion is
+     going to be store at the beginning of MAP_VEC.  */
+  while (unwind)
+    {
+      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
+	 *definition* of the macro of MAP, that got expanded at
+	 WHERE. This is basically how we go "up" in the stack of
+	 macro expansions that led to WHERE.  */
+      resolved_location =
+	linemap_macro_map_loc_to_def_point (map, where, false);
+      resolved_map = linemap_lookup (line_table, resolved_location);
+
+      /* If the token at RESOLVED_LOCATION [at macro definition point]
+	 is itself inside an expanded macro then we keep unwinding the
+	 expansion stack by tracing the "parent macro" that got expanded
+	 inside the definition of the macro of MAP...  */
+      if (linemap_macro_expansion_map_p (resolved_map))
+	{
+	  where = resolved_location;
+	  map = resolved_map;
+	}
+      else
+	{
+	  /* Otherwise, let's consider the location of the expansion
+	     point of the macro of MAP. Keep in mind that MAP is a
+	     macro expansion map. To get a "normal map" (i.e a non
+	     macro expansion map) and be done with the unwinding, we
+	     must either consider the location of the location
+	     expansion point of the macro or the location of the token
+	     inside the macro definition that got expanded to
+	     WHERE.  */
+	  where =
+	    linemap_macro_map_loc_to_exp_point (map, where);
+	  map = linemap_lookup (line_table, where);
+	}
+      if (!linemap_macro_expansion_map_p (map))
+	unwind = false;
+    }
+
+  if (topmost_exp_point_map)
+    *topmost_exp_point_map = map;
+
+  /* Walk the map_vec and print the macro expansion stack.  */
+    len = VEC_length (loc_t, loc_vec);
+  for (ix = 0;
+       len && ix < len;
+       ++ix)
+    {
+      expanded_location def_loc, exp_loc;
+      const struct line_map *def_point_map = NULL,
+	*exp_point_map = NULL;
+
+      iter = VEC_index (loc_t, loc_vec, ix);
+
+      /* 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.  */
+
+      /* Expand the location iter->where into the locus 1/ of the
+	 comment above.  */
+      def_loc =
+	linemap_expand_location_full (line_table, iter->where,
+				      LRK_MACRO_PARM_REPLACEMENT_POINT,
+				      &def_point_map);
+
+      /* Expand the location of the expansion point of the macro
+	 which expansion gave the token at represented by
+	 def_loc. This is the locus 2/ of the earlier comment.  */
+      exp_loc =
+	linemap_expand_location_full (line_table,
+				      MACRO_MAP_EXPANSION_POINT_LOCATION
+				      (iter->map),
+				      LRK_MACRO_PARM_REPLACEMENT_POINT,
+				      &exp_point_map);
+
+      if (!LINEMAP_SYSP (resolved_map))
+	{
+	  pp_verbatim (context->printer,
+		       "\nIn macro '%s' at %s:%d:%d",
+		       linemap_map_get_macro_name (iter->map),
+		       LINEMAP_FILE (def_point_map),
+		       def_loc.line, def_loc.column);
+	  pp_verbatim (context->printer,
+		       "\n    Expanded at %s:%d:%d",
+		       LINEMAP_FILE (exp_point_map),
+		       exp_loc.line, exp_loc.column);
+	}
+    }
+  VEC_free (loc_t, heap, loc_vec);
+}
+
 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 +405,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_expand_location_full (line_table, where,
+				LRK_MACRO_PARM_REPLACEMENT_POINT,
+				&map);
+
   if (map && diagnostic_last_module_changed (context, map))
     {
       diagnostic_set_last_module (context, map);
@@ -301,7 +444,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));
 }
@@ -310,6 +453,7 @@ void
 default_diagnostic_finalizer (diagnostic_context *context,
 			      diagnostic_info *diagnostic ATTRIBUTE_UNUSED)
 {
+  unwind_expanded_macro_location (context, diagnostic->location, NULL);
   pp_destroy_prefix (context->printer);
 }
 
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/g++.dg/cpp0x/initlist15.C b/gcc/testsuite/g++.dg/cpp0x/initlist15.C
index b75cc81..cca56b1 100644
--- a/gcc/testsuite/g++.dg/cpp0x/initlist15.C
+++ b/gcc/testsuite/g++.dg/cpp0x/initlist15.C
@@ -2,6 +2,7 @@
 
 // Just discard errors pointing at header files
 // { dg-prune-output "include" }
+// { dg-prune-output "        from" }
 
 #include <vector>
 #include <typeinfo>
diff --git a/gcc/testsuite/g++.old-deja/g++.robertl/eb43.C b/gcc/testsuite/g++.old-deja/g++.robertl/eb43.C
index 1dc4328..bd784b1 100644
--- a/gcc/testsuite/g++.old-deja/g++.robertl/eb43.C
+++ b/gcc/testsuite/g++.old-deja/g++.robertl/eb43.C
@@ -6,6 +6,10 @@
 
 // { dg-prune-output "note" }
 
+// Discard errors pointing at header files
+// { dg-prune-output "In file included from" }
+// { dg-prune-output "        from" }
+
 #include <vector>
 #include <algorithm>
 #include <functional>
diff --git a/gcc/testsuite/g++.old-deja/g++.robertl/eb79.C b/gcc/testsuite/g++.old-deja/g++.robertl/eb79.C
index 1c1ad3e..60cc713 100644
--- a/gcc/testsuite/g++.old-deja/g++.robertl/eb79.C
+++ b/gcc/testsuite/g++.old-deja/g++.robertl/eb79.C
@@ -1,5 +1,9 @@
 // { dg-do assemble  }
 // { dg-prune-output "note" }
+
+// Discard errors pointing at header files
+// { dg-prune-output "In file included from" }
+// { dg-prune-output "        from" }
 // Makes bogus x86 assembly code.
 #include <iostream>
 
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..a087050
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/cpp/macro-exp-tracking-1.c
@@ -0,0 +1,30 @@
+/*
+   { dg-options "-ftrack-macro-expansion=1" }
+   { dg-do compile }
+*/
+
+#define OPERATE(OPRD1, OPRT, OPRD2) \
+do \
+{ \
+ OPRD1 OPRT OPRD2; \
+} while (0)
+
+#define SHIFTL(A,B) \
+  OPERATE (A,<<,B) /* { dg-error "invalid operands to binary <<" } */
+
+void
+foo ()
+{
+  SHIFTL (0.1,0.2);
+}
+
+/*
+ { dg-message "macro 'OPERATE'\[^\n\r\]*:9:8" "In macro OPERATE" { target *-*-* } 0 }
+{ dg-message "Expanded at\[^\n\r\]*:13:3" "OPERATE expansion point" { target *-*-* } 0 }
+
+ { dg-message "macro 'SHIFTL'\[^\n\r\]*:13:14" "In macro SHIFTL" { target *-*-* } 0 }
+
+{ dg-message "Expanded at\[^\n\r\]*:18:3" "SHIFTL expansion point" { target *-*-* } 0 }
+
+
+*/
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..0f15875
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/cpp/macro-exp-tracking-2.c
@@ -0,0 +1,31 @@
+/* 
+   { dg-options "-ftrack-macro-expansion=1" }
+   { dg-do compile }
+*/
+
+#define OPERATE(OPRD1, OPRT, OPRD2) \
+ OPRD1 OPRT OPRD2;
+
+#define SHIFTL(A,B) \
+  OPERATE (A,<<,B) /* { dg-error "invalid operands to binary <<" } */
+
+#define MULT(A) \
+  SHIFTL (A,1)
+
+void
+foo ()
+{
+  MULT (1.0);/* 1.0 << 1;*/
+}
+
+/*
+  { dg-message "macro 'OPERATE'\[^\n\r\]*:7:8*" "In macro OPERATE" { target *-*-* } 0 }
+  { dg-message "Expanded at\[^\n\r\]*:10:3" "OPERATE expansion point" { target *-*-* } 0 }
+
+  { dg-message "macro 'SHIFTL'\[^\n\r\]*:10:14" "In macro SHIFTL" { target *-*-* } 0 }
+{ dg-message "Expanded at\[^\n\r\]*:13:3" "SHIFTL expansion point" { target *-*-* } 0 }
+
+  { dg-message "macro 'MULT'\[^\n\r\]*:13:3" "In macro MULT" { target *-*-* } 0 }
+{ dg-message "Expanded at\[^\n\r\]*:18:3" "MULT expansion point" { target *-*-* } 0 }
+
+ */
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..0b5c662
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/cpp/macro-exp-tracking-3.c
@@ -0,0 +1,18 @@
+/*
+  { dg-options "-fshow-column -ftrack-macro-expansion=1" }
+  { dg-do compile }
+ */
+
+#define SQUARE(A) A * A
+
+void
+foo()
+{
+  SQUARE (1 << 0.1); /* { dg-error "16:invalid operands to binary <<" } */
+}
+
+/*
+{ dg-message "macro 'SQUARE'\[^\n\r\]*:6:19" "In macro SQUARE" { target *-*-* } 0 }
+{ dg-message "Expanded at\[^\n\r\]*:11:3" "SQUARE expansion point" { target *-*-* } 0 }
+
+*/
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..f9e4c2d
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/cpp/macro-exp-tracking-4.c
@@ -0,0 +1,19 @@
+/*
+  { dg-options "-fshow-column -ftrack-macro-expansion=2" }
+  { dg-do compile }
+ */
+
+#define SQUARE(A) A * A
+
+void
+foo()
+{
+  SQUARE (1 << 0.1); /* { dg-error "13:invalid operands to binary <<" } */
+}
+
+/*
+
+{ dg-message "macro 'SQUARE'\[^\n\r\]*:6:19" "In macro SQUARE" { target *-*-* } 0 }
+{ dg-message "Expanded at\[^\n\r\]*:11:3" "SQUARE expansion point" { target *-*-* } 0 }
+
+*/
diff --git a/gcc/testsuite/gcc.target/i386/sse-vect-types.c b/gcc/testsuite/gcc.target/i386/sse-vect-types.c
index 9cb6f3e..ce70125 100644
--- a/gcc/testsuite/gcc.target/i386/sse-vect-types.c
+++ b/gcc/testsuite/gcc.target/i386/sse-vect-types.c
@@ -1,6 +1,12 @@
 /* { dg-do compile } */
 /* { dg-options "-O0 -msse2" } */
 
+
+/*
+   Just discard diagnostic prolog about errors in include files
+   { dg-prune-output "In file included from" }
+*/
+
 #include <xmmintrin.h>
 
 __m128d foo1(__m128d z, __m128d  a, int N) { 
diff --git a/gcc/tree-diagnostic.c b/gcc/tree-diagnostic.c
index b456a2a..cbfd81c 100644
--- a/gcc/tree-diagnostic.c
+++ b/gcc/tree-diagnostic.c
@@ -35,7 +35,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);
 }
 
-- 
        Dodji

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

* [PATCH 1/6] Linemap infrastructure for virtual locations
  2010-12-10 11:27 [PATCH 0/6] Tracking locations of tokens resulting from macro expansion Dodji Seketeli
                   ` (3 preceding siblings ...)
  2010-12-10 11:27 ` [PATCH 5/6] Add line map statistics to -fmem-report output Dodji Seketeli
@ 2010-12-10 11:53 ` Dodji Seketeli
  2011-01-06 16:48   ` Tom Tromey
  2010-12-10 12:27 ` [PATCH 2/6] Generate virtual locations for tokens Dodji Seketeli
                   ` (4 subsequent siblings)
  9 siblings, 1 reply; 135+ messages in thread
From: Dodji Seketeli @ 2010-12-10 11:53 UTC (permalink / raw)
  To: gcc-patches; +Cc: tromey, joseph, gdr, lopezibanez

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.

The patch does not introduce any regression test because it doesn't
introduce any use of virtual locations at the compiler level yet.

Boostrapped with --enable-languages=all,ada and passed regression
tests on x86_unknown-linux-gnu against trunk.

libcpp/

	* include/cpp-id-data.h (struct cpp_macro)<name>: New field for
	diagnostics purposes.
	* 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.
	(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_MAP_P)
	(LOCATION_POSSIBLY_IN_MACRO_MAP_P, LINEMAPS_MAP_INFO)
	(LINEMAPS_MAPS, LINEMAPS_ALLOCATE, LINEMAPS_USED, LINEMAPS_CACHE)
	(LINEMAPS_HIGHEST_LOCATION, LINEMAPS_HIGHEST_LINE)
	(LINEMAPS_LAST_MAP, LINEMAPS_LAST_ALLOCATED_MAP)
	(LINEMAPS_ORDINARY_MAPS, LINEMAPS_ORDINARY_ALLOCATED)
	(LINEMAPS_ORDINARY_USED, LINEMAPS_ORDINARY_CACHE)
	(LINEMAPS_ORDINARY_HIGHEST_LOCATION)
	(LINEMAPS_ORDINARY_HIGHEST_LINE, LINEMAPS_LAST_ORDINARY_MAP)
	(LINEMAPS_LAST_ALLOCATED_ORDINARY_MAP, LINEMAPS_MACRO_MAPS)
	(LINEMAPS_MACRO_ALLOCATED, LINEMAPS_MACRO_USED)
	(LINEMAPS_MACRO_CACHE, LINEMAPS_MACRO_HIGHEST_LOCATION)
	(LINEMAPS_MACRO_HIGHEST_LINE, 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_init, linemap_free, linemap_add, linemap_lookup): Remove
	useless extern. Move comment to the function definition.
	(linemap_check_files_exited, linemap_line_start)
	(linemap_position_for_column): Remove useless extern.
	(linemap_position_for_line_and_column)
	(linemap_tracks_macro_expansion_locs_p, linemap_enter_macro)
	(linemap_add_macro_token, linemap_check_ordinary)
	(linemap_macro_expansion_map_p, linemap_macro_loc_to_exp_point)
	(linemap_macro_loc_to_def_point)
	(linemap_macro_map_loc_to_def_point)
	(linemap_macro_map_loc_to_exp_point, linemap_map_get_index)
	(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_expand_location_full, linemap_expand_location): Declare
	new functions.
	* line-map.c: Include cpp-id-data.h
	(linemap_assert): New macro.
	(new_linemap, create_and_add_line_map_internal): Define new static
	functions. Extracted from ...
	(linemap_add): ... here.
	(linemap_tracks_macro_expansion_locs_p, linemap_enter_macro)
	(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_loc_to_exp_point, linemap_map_get_index)
	(linemap_macro_loc_to_def_point, 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_expand_location_full)
	(linemap_tracks_macro_expansion_locs_p): 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): Adjust to free what has been allocated. Use the
	accessors.
	(linemap_line_start): Assert this uses an ordinary map. Adjust to
	use the new ordinary map accessors and data structures. Use the
	new MAX_SOURCE_LOCATION constant.
	(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.
	* 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/
	* 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.
	* input.c (expand_location): Fix the logic. Adjust to expand to
	the tokens spelling location when macro location tracking is on.

gcc/c-family

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

gcc/java/

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

gcc/ada/

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

gcc/fortran/

	* cpp.c (print_line, cb_define): Adjust to avoid using internals
	of struct line_map. Use the public API instead.
---
 gcc/ada/gcc-interface/trans.c |   10 +-
 gcc/c-family/c-lex.c          |    6 +-
 gcc/c-family/c-pch.c          |    2 +-
 gcc/c-family/c-ppoutput.c     |   35 +-
 gcc/diagnostic.c              |    6 +-
 gcc/fortran/cpp.c             |   22 +-
 gcc/input.c                   |   40 ++-
 gcc/input.h                   |   20 +-
 gcc/java/jcf-parse.c          |    2 +-
 libcpp/directives-only.c      |    7 +-
 libcpp/directives.c           |   19 +-
 libcpp/errors.c               |    2 +-
 libcpp/files.c                |   24 +-
 libcpp/include/cpp-id-data.h  |    6 +
 libcpp/include/line-map.h     |  626 +++++++++++++++++++++++++-----
 libcpp/init.c                 |    2 +-
 libcpp/internal.h             |    5 +-
 libcpp/lex.c                  |   37 ++-
 libcpp/line-map.c             |  866 ++++++++++++++++++++++++++++++++++++-----
 libcpp/macro.c                |   19 +-
 libcpp/traditional.c          |    7 +-
 21 files changed, 1460 insertions(+), 303 deletions(-)

diff --git a/gcc/ada/gcc-interface/trans.c b/gcc/ada/gcc-interface/trans.c
index b658620..c71c99a 100644
--- a/gcc/ada/gcc-interface/trans.c
+++ b/gcc/ada/gcc-interface/trans.c
@@ -268,7 +268,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.  */
@@ -7683,12 +7683,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 778e424..085ecd4 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-pch.c b/gcc/c-family/c-pch.c
index b429d9d..9d4f799 100644
--- a/gcc/c-family/c-pch.c
+++ b/gcc/c-family/c-pch.c
@@ -423,7 +423,7 @@ c_common_read_pch (cpp_reader *pfile, const char *name,
     }
 
   /* Save the location and then restore it after reading the PCH.  */
-  saved_loc = expand_location (line_table->highest_line);
+  saved_loc = expand_location (LINEMAPS_ORDINARY_HIGHEST_LINE (line_table));
   saved_trace_includes = line_table->trace_includes;
 
   timevar_push (TV_PCH_CPP_RESTORE);
diff --git a/gcc/c-family/c-ppoutput.c b/gcc/c-family/c-ppoutput.c
index 1700fae..0def8fa 100644
--- a/gcc/c-family/c-ppoutput.c
+++ b/gcc/c-family/c-ppoutput.c
@@ -188,9 +188,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;
@@ -210,9 +208,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
@@ -302,8 +298,7 @@ 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);
   /* End the previous line of text.  */
   if (print.printed)
     {
@@ -336,27 +331,29 @@ 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_line = LOCATION_LINE (src_loc);
 
       /* 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);
@@ -385,8 +382,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)
@@ -415,6 +411,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);
 
@@ -426,7 +424,8 @@ 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_macro_loc_to_exp_point (line_table, line, &map);
+  if (LINEMAP_LINE (map) != 0)
     print.src_line++;
 }
 
diff --git a/gcc/diagnostic.c b/gcc/diagnostic.c
index d297cdd..9df540b 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);
diff --git a/gcc/fortran/cpp.c b/gcc/fortran/cpp.c
index 4c1307c..c1b4d44 100644
--- a/gcc/fortran/cpp.c
+++ b/gcc/fortran/cpp.c
@@ -810,27 +810,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);
@@ -927,7 +929,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..6dbc253 100644
--- a/gcc/input.c
+++ b/gcc/input.c
@@ -35,19 +35,43 @@ expand_location (source_location loc)
 {
   expanded_location xloc;
   if (loc <= BUILTINS_LOCATION)
-    {
-      xloc.file = loc == UNKNOWN_LOCATION ? NULL : _("<built-in>");
+    {      
+      /* If LOC is UNKNOWN_LOCATION and there was a map allocated for
+	 it then XLOC.FILE should be the file path of the map,
+	 i.e. the file path of the main file being compiled.
+	 Otherwise [if we are being called before any line map has
+	 been allocated, e.g. during parsing of commande line
+	 options] if no line map has been allocated yet, then
+	 XLOC.FILE should just be NULL.  */
+      if (loc == UNKNOWN_LOCATION)
+	{
+	  const struct line_map *map =
+	    linemap_lookup (line_table, UNKNOWN_LOCATION);
+	  xloc.file = map != NULL ? LINEMAP_FILE (map) : NULL;
+	}
+      else
+	xloc.file = _("<built-in>");
       xloc.line = 0;
       xloc.column = 0;
       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;
-    };
+      bool expand_to_expansion_point_p =
+	!linemap_tracks_macro_expansion_locs_p (line_table);
+
+    /* If LOC is the location of a token resulting from macro
+       expansion, either expand to the location of the macro expansion
+       point if we don't support tracking token locations accross
+       macro expansion, or expand to the actual spelling location of
+       the token where the error is if we do support tracking
+       locations accross macro expansion.  */
+      xloc =
+	linemap_expand_location_full (line_table, loc,
+				      (expand_to_expansion_point_p)
+				      ? LRK_MACRO_EXPANSION_POINT
+				      : LRK_SPELLING_LOCATION,
+				      NULL);
+    }
   return xloc;
 }
diff --git a/gcc/input.h b/gcc/input.h
index 5929064..835c95a 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 (in_system_header_at (input_location))
+#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 9166f31..07a0bb3 100644
--- a/gcc/java/jcf-parse.c
+++ b/gcc/java/jcf-parse.c
@@ -356,7 +356,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/libcpp/directives-only.c b/libcpp/directives-only.c
index e19f806..e478fc3 100644
--- a/libcpp/directives-only.c
+++ b/libcpp/directives-only.c
@@ -54,7 +54,7 @@ _cpp_preprocess_dir_only (cpp_reader *pfile,
   buffer->need_line = false;
 
   /* This isn't really needed.  It prevents a compiler warning, though. */
-  loc = pfile->line_table->highest_line;
+  loc = LINEMAPS_ORDINARY_HIGHEST_LINE (pfile->line_table);
 
   /* Scan initialization. */
   next_line = cur = base = buffer->cur;
@@ -107,13 +107,14 @@ _cpp_preprocess_dir_only (cpp_reader *pfile,
 	    /* Sanitize the line settings.  Duplicate #include's can mess
 	       things up. */
 	    line_table = pfile->line_table;
-	    line_table->highest_location = line_table->highest_line;
+	    LINEMAPS_ORDINARY_HIGHEST_LOCATION (line_table) =
+	      LINEMAPS_ORDINARY_HIGHEST_LINE (line_table);
 
 	    /* The if block prevents us from outputing line information when
 	       the file ends with a directive and no newline.  Note that we
 	       must use pfile->buffer, not buffer. */
 	    if (pfile->buffer->next_line < pfile->buffer->rlimit)
-	      cb->maybe_print_line (pfile->line_table->highest_line);
+	      cb->maybe_print_line (LINEMAPS_ORDINARY_HIGHEST_LINE (pfile->line_table));
 
 	    goto restart;
 	  }
diff --git a/libcpp/directives.c b/libcpp/directives.c
index 6462605..4c273e0 100644
--- a/libcpp/directives.c
+++ b/libcpp/directives.c
@@ -274,7 +274,8 @@ start_directive (cpp_reader *pfile)
   pfile->directive_result.type = CPP_PADDING;
 
   /* Some handlers need the position of the # for diagnostics.  */
-  pfile->directive_line = pfile->line_table->highest_line;
+  pfile->directive_line =
+    LINEMAPS_ORDINARY_HIGHEST_LINE (pfile->line_table);
 }
 
 /* Called when leaving a directive, _Pragma or command-line directive.  */
@@ -883,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 = map->d.ordinary.sysp;
   const cpp_token *token;
-  const char *new_file = map->to_file;
+  const char *new_file = map->d.ordinary.to_file;
   linenum_type new_lineno;
 
   /* C99 raised the minimum limit on #line numbers.  */
@@ -945,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 = map->d.ordinary.to_file;
   linenum_type new_lineno;
-  unsigned int new_sysp = map->sysp;
+  unsigned int new_sysp = map->d.ordinary.sysp;
   enum lc_reason reason = LC_RENAME_VERBATIM;
   int flag;
   bool wrapped;
@@ -1020,7 +1021,7 @@ do_linemarker (cpp_reader *pfile)
      *following* the #line directive.  A separate source_location for this
      location makes no sense (until we do the LC_LEAVE), and
      complicates LAST_SOURCE_LINE_LOCATION.  */
-  pfile->line_table->highest_location--;
+  LINEMAPS_ORDINARY_HIGHEST_LOCATION (pfile->line_table)--;
 
   _cpp_do_file_change (pfile, reason, new_file, new_lineno, new_sysp);
 }
@@ -1037,7 +1038,7 @@ _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, map->d.ordinary.to_line, 127);
 
   if (pfile->cb.file_change)
     pfile->cb.file_change (pfile, map);
diff --git a/libcpp/errors.c b/libcpp/errors.c
index b7783f5..821e4ed 100644
--- a/libcpp/errors.c
+++ b/libcpp/errors.c
@@ -43,7 +43,7 @@ cpp_diagnostic (cpp_reader * pfile, int level, int reason,
       if (pfile->state.in_directive)
 	src_loc = pfile->directive_line;
       else
-	src_loc = pfile->line_table->highest_line;
+	src_loc = LINEMAPS_ORDINARY_HIGHEST_LINE (pfile->line_table);
     }
   /* We don't want to refer to a token before the beginning of the
      current run -- that is invalid.  */
diff --git a/libcpp/files.c b/libcpp/files.c
index be39db9..363b71f 100644
--- a/libcpp/files.c
+++ b/libcpp/files.c
@@ -542,7 +542,7 @@ _cpp_find_file (cpp_reader *pfile, const char *fname, cpp_dir *start_dir, bool f
   entry = new_file_hash_entry (pfile);
   entry->next = *hash_slot;
   entry->start_dir = start_dir;
-  entry->location = pfile->line_table->highest_location;
+  entry->location = LINEMAPS_ORDINARY_HIGHEST_LOCATION (pfile->line_table);
   entry->u.file = file;
   *hash_slot = entry;
 
@@ -555,7 +555,7 @@ _cpp_find_file (cpp_reader *pfile, const char *fname, cpp_dir *start_dir, bool f
       entry = new_file_hash_entry (pfile);
       entry->next = *hash_slot;
       entry->start_dir = pfile->bracket_include;
-      entry->location = pfile->line_table->highest_location;
+      entry->location = LINEMAPS_ORDINARY_HIGHEST_LOCATION (pfile->line_table);
       entry->u.file = file;
       *hash_slot = entry;
     }
@@ -566,7 +566,8 @@ _cpp_find_file (cpp_reader *pfile, const char *fname, cpp_dir *start_dir, bool f
       entry = new_file_hash_entry (pfile);
       entry->next = *hash_slot;
       entry->start_dir = pfile->quote_include;
-      entry->location = pfile->line_table->highest_location;
+      entry->location =
+	LINEMAPS_ORDINARY_HIGHEST_LOCATION (pfile->line_table);
       entry->u.file = file;
       *hash_slot = entry;
     }
@@ -927,7 +928,7 @@ _cpp_stack_include (cpp_reader *pfile, const char *fname, int angle_brackets,
      linemap_add is not called) or we were included from the
      command-line.  */
   if (file->pchname == NULL && file->err_no == 0 && type != IT_CMDLINE)
-    pfile->line_table->highest_location--;
+    LINEMAPS_ORDINARY_HIGHEST_LOCATION (pfile->line_table)--;
 
   return _cpp_stack_file (pfile, file, type == IT_IMPORT);
 }
@@ -936,7 +937,8 @@ _cpp_stack_include (cpp_reader *pfile, const char *fname, int angle_brackets,
 static void
 open_file_failed (cpp_reader *pfile, _cpp_file *file, int angle_brackets)
 {
-  int sysp = pfile->line_table->highest_line > 1 && pfile->buffer ? pfile->buffer->sysp : 0;
+  int sysp = (LINEMAPS_ORDINARY_HIGHEST_LINE (pfile->line_table) > 1)
+    && (pfile->buffer ? pfile->buffer->sysp : 0);
   bool print_dep = CPP_OPTION (pfile, deps.style) > (angle_brackets || !!sysp);
 
   errno = file->err_no;
@@ -1049,7 +1051,7 @@ make_cpp_dir (cpp_reader *pfile, const char *dir_name, int sysp)
   entry = new_file_hash_entry (pfile);
   entry->next = *hash_slot;
   entry->start_dir = NULL;
-  entry->location = pfile->line_table->highest_location;
+  entry->location = LINEMAPS_ORDINARY_HIGHEST_LOCATION (pfile->line_table);
   entry->u.dir = dir;
   *hash_slot = entry;
 
@@ -1221,14 +1223,16 @@ 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,
-		       SOURCE_LINE (map, pfile->line_table->highest_line), flags);
+  _cpp_do_file_change (pfile, LC_RENAME, map->d.ordinary.to_file,
+		       SOURCE_LINE (map,
+				    LINEMAPS_ORDINARY_HIGHEST_LINE
+				    (pfile->line_table)),
+		       flags);
 }
 
 /* Allow the client to change the current file.  Used by the front end
diff --git a/libcpp/include/cpp-id-data.h b/libcpp/include/cpp-id-data.h
index a57edad..8260fd0 100644
--- a/libcpp/include/cpp-id-data.h
+++ b/libcpp/include/cpp-id-data.h
@@ -34,6 +34,12 @@ struct GTY(()) answer {
 /* Each macro definition is recorded in a cpp_macro structure.
    Variadic macros cannot occur with traditional cpp.  */
 struct GTY(()) cpp_macro {
+  /* Name of this macro.  Used only for error reporting.  */
+  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")))
+    name;
+
   /* Parameters, if any.  */
   cpp_hashnode ** GTY ((nested_ptr (union tree_node,
 		"%h ? CPP_HASHNODE (GCC_IDENT_TO_HT_IDENT (%h)) : NULL",
diff --git a/libcpp/include/line-map.h b/libcpp/include/line-map.h
index 3f52d75..0011f38 100644
--- a/libcpp/include/line-map.h
+++ b/libcpp/include/line-map.h
@@ -27,13 +27,23 @@ 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
+  /* stringize */
+  /* paste */
+};
 
 /* The type of line numbers.  */
 typedef unsigned int linenum_type;
@@ -44,42 +54,235 @@ 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
+   physicall 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 {
+   means "entire file/line" or "unknown line/column" or "not
+   applicable".)
+
+   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.  */
+
+  /* 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 macro map.  */
+#define MAX_SOURCE_LOCATION 0xF0000000
+
+struct GTY(()) cpp_macro;
+
+/* A macro line map encodes locations 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_macro *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 we are at the expansion point of MACRO.  Each xI is the
+     location of the Ith token of the replacement-list. Now it gets
+     confusing. the xI is the location of the Ith token of the
+     replacement-list at the macro *definition* point. Not at the
+     macro replacement point. Okay, let's try to explain this below.
+
+     Imagine this:
+
+        #define OPERATION(OP0, OPERATOR, OP1) \
+                OP0 OPERATOR OP1 <-- #0
+
+	#define PLUS(A, B) OPERATION (A, +, B)  <--- #1
+
+	int a = PLUS (1,2); <--- #2
+     
+     In #2, there is a macro map for the expansion of PLUS. PLUS is
+     expanded into the replacement-list made of the tokens:
+     
+        OPERATION, (, A, +, B, )
+
+     and then further expanded into the tokens:
+
+        1, +, 2.
+
+     Let's consider the case of token "+" here. That will help us
+     understand what the xI we were talking about earlier means.
+
+     The token '+' has two locations, so to speak. One in the context
+     of the macro *expansion* of PLUS in #2 and one in the context of
+     the macro *definition* of PLUS in #1. These two locations are
+     encoded in the the latter context, somehow in the xI we are
+     talking about.
+
+     xI is roughly the index of the token inside the replacement-list
+     at the expansion point. So for '+', it's index would then be 1
+     [The index of token '1' would be 0 and the index of token 2 would
+     be 1]. So if '+' is our current xI, it is actualy an x1.
+
+     The value of x1 is the location of the token '+' inside the
+     replacement-list of PLUS at the definition point of PLUS. It is
+     its spelling location in #1.
+
+     So x0 would have described the token '1', x1 describes the token
+     '+' and x2 describes the token '2'.
+
+     Now what's the y1 then? Remember, we said macro_locations is an
+     array of pairs (xI,yI). We now know what the xI is, now let's
+     look at the yI.
+
+     Let's look at the token '+' again. We said it has two locations
+     somehow. Actually it has 3. Kind of. As '+' is an argument passed
+     to the macro OPERATION [at the definition point of the macro
+     PLUS], it would be nice to record the source location of the
+     *parameter* of OPERATION that is replaced by the argument '+'.
+     In other words, we want to record the location of the token
+     "OPERATOR" in the replacement-list of OPERATION, at the
+     /definition/ point of OPERATION in #0. And that is y1.
+
+     So when (xI,yI) describes a token that is passed as an argument
+     to a macro M, the yI is the location of the macro parameter that
+     the argument replaces, at the definition point of M. If (xI,yI)
+     does not describe a token that is passed as an argument to a
+     macro, xI == yI.
+   */
+  source_location * GTY((length ("2 * %h.n_tokens"))) macro_locations;
+
+  /* This is the location of the expansion point of the current macro
+     map.  That expansion point location is hold 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 {
+    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) (MAP)->d.ordinary.to_file
+
+#define ORDINARY_MAP_STARTING_LINE_NUMBER(MAP) (MAP)->d.ordinary.to_line
+
+#define ORDINARY_MAP_INCLUDER_FILE_INDEX(MAP) (MAP)->d.ordinary.included_from
+
+#define ORDINARY_MAP_IN_SYSTEM_HEADER_P(MAP) (MAP)->d.ordinary.sysp
+
+#define ORDINARY_MAP_NUMBER_OF_COLUMN_BITS(MAP) (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 allocated size of 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;
 
-  /* The most recently listed include stack, if any, starts with
-     LAST_LISTED as the topmost including file.  -1 indicates nothing
-     has been listed yet.  */
-  int last_listed;
+  /* Highest source_location "given out".  */
+  source_location highest_location;
+
+  /* Start of line of highest source_location "given out".  */
+  source_location highest_line;
+};
+
+/* 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;
@@ -87,12 +290,6 @@ struct GTY(()) line_maps {
   /* If true, prints an include trace a la -H.  */
   bool trace_includes;
 
-  /* Highest source_location "given out".  */
-  source_location highest_location;
-
-  /* Start of line of highest source_location "given out".  */
-  source_location highest_line;
-
   /* The maximum column number we can quickly allocate.  Higher numbers
      may require allocating a new line_map.  */
   unsigned int max_column_hint;
@@ -102,44 +299,232 @@ struct GTY(()) line_maps {
   line_map_realloc reallocator;
 };
 
-/* Initialize a line map set.  */
-extern void linemap_init (struct line_maps *);
+/* Return TRUE if LOC is possibly encoded in MAP, FALSE otherwise.
+   MACRO_MAP_P shall be TRUE if MAP is to be considered as a macro
+   map, false otherwise.  */
+#define LOCATION_POSSIBLY_IN_MAP_P(LOC, MAP, MACRO_MAP_P)		\
+  (((MACRO_MAP_P))							\
+   ? (LOC) <= (MAP)->start_location					\
+   : (LOC) >= (MAP)->start_location)
+
+/* Return TRUE if LOC is possibly encoded in the macro map MAP, FALSE
+   otherwise.  */
+#define LOCATION_POSSIBLY_IN_MACRO_MAP_P(LOC, MAP) \
+  LOCATION_POSSIBLY_IN_MAP_P ((LOC), (MAP), true)
+
+/* 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
+
+/* Returns the highest location that was encoded into the line table
+   SET. MAP_KIND shall be TRUE if we are interested in locations of
+   tokens resulting from macro expansion, FALSE otherwise.  */
+#define LINEMAPS_HIGHEST_LOCATION(SET, MAP_KIND) \
+  (LINEMAPS_MAP_INFO (SET, MAP_KIND))->highest_location
+
+/* Returns the location of the begining of the highest line that was
+   encoded in into the line table SET.  MAP_KIND shall be TRUE if we
+   are interested in locations of tokens resulting from macro
+   expansion, FALSE otherwise.  */
+#define LINEMAPS_HIGHEST_LINE(SET, MAP_KIND) \
+  (LINEMAPS_MAP_INFO (SET, MAP_KIND))->highest_line
+
+/* 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)
+
+/* Return the highest location of an ordinary token encoded in the
+   line table SET.  */
+#define LINEMAPS_ORDINARY_HIGHEST_LOCATION(SET) \
+  LINEMAPS_HIGHEST_LOCATION(SET, false)
+
+/* Return the location of the beginning of the highest (containing
+   ordinary tokens) line encoded in the in the set.  */
+#define LINEMAPS_ORDINARY_HIGHEST_LINE(SET) \
+  LINEMAPS_HIGHEST_LINE(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 highest location [of a token resulting from macro
+   expansion] encoded in this line table.  */
+#define LINEMAPS_MACRO_HIGHEST_LOCATION(SET) \
+  LINEMAPS_HIGHEST_LOCATION(SET, true)
+
+/* Returns the location of the begining of the highest line
+   -- containing a token resulting from macro expansion --  encoded
+   in the line table SET.  */
+#define LINEMAPS_MACRO_HIGHEST_LINE(SET) \
+  LINEMAPS_HIGHEST_LINE(SET, true)
+
+/* 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)
+
+void linemap_init (struct line_maps *);
+
+void linemap_free (struct line_maps *);
+
+void linemap_check_files_exited (struct line_maps *);
+
+bool linemap_tracks_macro_expansion_locs_p (struct line_maps *);
+
+source_location linemap_line_start
+(struct line_maps *set, linenum_type to_line,  unsigned int max_column_hint);
 
-/* Free a line map set.  */
-extern void linemap_free (struct line_maps *);
+const struct line_map *linemap_add (struct line_maps *,
+				    enum lc_reason,
+				    unsigned int sysp,
+				    const char *to_file,
+				    linenum_type to_line);
 
-/* Check for and warn about line_maps entered but not exited.  */
+const struct line_map *linemap_enter_macro (struct line_maps *,
+					    struct cpp_macro*,
+					    source_location,
+					    unsigned int);
 
-extern void linemap_check_files_exited (struct line_maps *);
 
-/* 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_add_macro_token (const struct line_map *,
+					 unsigned int,
+					 source_location,
+					 source_location);
 
-extern source_location linemap_line_start
-(struct line_maps *set, linenum_type to_line,  unsigned int max_column_hint);
+const struct line_map *linemap_lookup (struct line_maps *, source_location);
+
+int linemap_check_ordinary (const struct line_map *);
+
+bool linemap_macro_expansion_map_p (const struct line_map *);
+
+source_location linemap_macro_loc_to_exp_point (struct line_maps *,
+						source_location,
+						const struct line_map **);
+
+source_location linemap_macro_loc_to_def_point (struct line_maps *,
+						source_location,
+						const struct line_map **,
+						bool);
+
+source_location linemap_macro_map_loc_to_def_point (const struct line_map*,
+						    source_location,
+						    bool);
+
+source_location linemap_macro_map_loc_to_exp_point (const struct line_map*,
+						    source_location);
 
-/* Add a mapping of logical source line to physical source file and
-   line number.
+int linemap_map_get_index (const struct line_maps *,
+			   const struct line_map*);
 
-   The text pointed to by TO_FILE must have a lifetime
-   at least as long as the final call to lookup_line ().  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.
+int linemap_get_source_line (struct line_maps *,
+			     source_location);
 
-   A call to this function can relocate the previous set of
-   maps, so any stored line_map pointers should not be used.  */
-extern const struct line_map *linemap_add
-  (struct line_maps *, enum lc_reason, unsigned int sysp,
-   const char *to_file, linenum_type to_line);
+int linemap_get_source_column (struct line_maps *,
+			       source_location);
 
-/* Given a logical line, returns the map from which the corresponding
-   (source file, line) pair can be deduced.  */
-extern const struct line_map *linemap_lookup
-  (struct line_maps *, source_location);
+const char* linemap_map_get_macro_name (const struct line_map*);
+
+const char* linemap_get_file_path (struct line_maps *,
+				   source_location);
+
+int linemap_location_in_system_header_p (struct line_maps *,
+					 source_location);
+
+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
@@ -147,47 +532,112 @@ extern const struct line_map *linemap_lookup
 #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)						\
+  (linemap_check_ordinary (MAP),					\
+    ((((LOC) - (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)				\
+  (linemap_check_ordinary (MAP),			\
+   (((LOC) - (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),				\
+   ((((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),					\
+   ((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),		\
+   ((MAP)->d.ordinary.included_from < 0))
 
 /* Set LOC to a source position that is the same line as the most recent
    linemap_line_start, but with the specified TO_COLUMN column number.  */
 
-#define LINEMAP_POSITION_FOR_COLUMN(LOC, SET, TO_COLUMN) do { \
-  unsigned int to_column = (TO_COLUMN); \
-  struct line_maps *set = (SET); \
-  if (__builtin_expect (to_column >= set->max_column_hint, 0)) \
-    (LOC) = linemap_position_for_column (set, to_column); \
-  else { \
-    source_location r = set->highest_line; \
-    r = r + to_column; \
-    if (r >= set->highest_location) \
-      set->highest_location = r; \
-    (LOC) = r;			 \
-  }} while (0)
-    
-
-extern source_location
-linemap_position_for_column (struct line_maps *set, unsigned int to_column);
+#define LINEMAP_POSITION_FOR_COLUMN(LOC, SET, TO_COLUMN) do {		\
+    unsigned int to_column = (TO_COLUMN);				\
+    struct line_maps *set = (SET);					\
+    if (__builtin_expect (to_column >= set->max_column_hint, 0))	\
+      (LOC) = linemap_position_for_column (set, to_column);		\
+    else								\
+      {									\
+	source_location r = LINEMAPS_ORDINARY_HIGHEST_LINE (set);	\
+	r = r + to_column;						\
+	if (r >= LINEMAPS_ORDINARY_HIGHEST_LOCATION (set))		\
+	  LINEMAPS_ORDINARY_HIGHEST_LOCATION (set) = r;			\
+	(LOC) = r;							\
+      }									\
+  } while (0)
+
+source_location linemap_position_for_column (struct line_maps *, unsigned int);
+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), (MAP)->d.ordinary.to_file)
+
+/* Return the line number this map started encoding location from.  */
+#define LINEMAP_LINE(MAP)					\
+  (linemap_check_ordinary (MAP), (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), (MAP)->d.ordinary.sysp)
+
+typedef struct GTY (())
+{
+  /* 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;
+
+enum location_resolution_kind
+{
+  LRK_MACRO_EXPANSION_POINT,
+  LRK_SPELLING_LOCATION,
+  LRK_MACRO_PARM_REPLACEMENT_POINT
+};
+
+expanded_location linemap_expand_location (struct line_maps *,
+					   source_location);
 
+expanded_location linemap_expand_location_full (struct line_maps *,
+						source_location,
+						enum location_resolution_kind,
+						const struct line_map**);
 #endif /* !LIBCPP_LINE_MAP_H  */
diff --git a/libcpp/init.c b/libcpp/init.c
index cfc16e8..8836143 100644
--- a/libcpp/init.c
+++ b/libcpp/init.c
@@ -574,7 +574,7 @@ 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 = (LINEMAPS_LAST_ORDINARY_MAP (pfile->line_table))->d.ordinary.to_file;
     }
   return fname;
 }
diff --git a/libcpp/internal.h b/libcpp/internal.h
index d2872c4..f02c878 100644
--- a/libcpp/internal.h
+++ b/libcpp/internal.h
@@ -67,8 +67,9 @@ 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]; \
-    linenum_type line = SOURCE_LINE (map, line_table->highest_line); \
+    const struct line_map *map = \
+      LINEMAPS_LAST_ORDINARY_MAP (line_table); \
+    linenum_type line = SOURCE_LINE (map, LINEMAPS_ORDINARY_HIGHEST_LINE (line_table)); \
     linemap_line_start (PFILE->line_table, line + 1, COLS_HINT); \
   } while (0)
 
diff --git a/libcpp/lex.c b/libcpp/lex.c
index 5cd5686..bcf292c 100644
--- a/libcpp/lex.c
+++ b/libcpp/lex.c
@@ -830,12 +830,17 @@ _cpp_process_line_notes (cpp_reader *pfile, int in_comment)
       if (note->type == '\\' || note->type == ' ')
 	{
 	  if (note->type == ' ' && !in_comment)
-	    cpp_error_with_line (pfile, CPP_DL_WARNING, pfile->line_table->highest_line, col,
+	    cpp_error_with_line (pfile, CPP_DL_WARNING,
+				 LINEMAPS_ORDINARY_HIGHEST_LINE (pfile->line_table),
+				 col,
 				 "backslash and newline separated by space");
 
 	  if (buffer->next_line > buffer->rlimit)
 	    {
-	      cpp_error_with_line (pfile, CPP_DL_PEDWARN, pfile->line_table->highest_line, col,
+	      cpp_error_with_line (pfile, CPP_DL_PEDWARN,
+				   LINEMAPS_ORDINARY_HIGHEST_LINE
+				   (pfile->line_table),
+				   col,
 				   "backslash-newline at end of file");
 	      /* Prevent "no newline at end of file" warning.  */
 	      buffer->next_line = buffer->rlimit;
@@ -851,7 +856,9 @@ _cpp_process_line_notes (cpp_reader *pfile, int in_comment)
 	    {
 	      if (CPP_OPTION (pfile, trigraphs))
 		cpp_warning_with_line (pfile, CPP_W_TRIGRAPHS,
-                                       pfile->line_table->highest_line, col,
+                                       LINEMAPS_ORDINARY_HIGHEST_LINE
+				       (pfile->line_table),
+				       col,
 				       "trigraph ??%c converted to %c",
 				       note->type,
 				       (int) _cpp_trigraph_map[note->type]);
@@ -859,7 +866,7 @@ _cpp_process_line_notes (cpp_reader *pfile, int in_comment)
 		{
 		  cpp_warning_with_line 
 		    (pfile, CPP_W_TRIGRAPHS,
-                     pfile->line_table->highest_line, col,
+                     LINEMAPS_ORDINARY_HIGHEST_LINE (pfile->line_table), col,
 		     "trigraph ??%c ignored, use -trigraphs to enable",
 		     note->type);
 		}
@@ -907,7 +914,8 @@ _cpp_skip_block_comment (cpp_reader *pfile)
 	    {
 	      buffer->cur = cur;
 	      cpp_warning_with_line (pfile, CPP_W_COMMENTS,
-				     pfile->line_table->highest_line,
+				     LINEMAPS_ORDINARY_HIGHEST_LINE
+				     (pfile->line_table),
 				     CPP_BUF_COL (buffer),
 				     "\"/*\" within comment");
 	    }
@@ -940,13 +948,14 @@ static int
 skip_line_comment (cpp_reader *pfile)
 {
   cpp_buffer *buffer = pfile->buffer;
-  source_location orig_line = pfile->line_table->highest_line;
+  source_location orig_line =
+    LINEMAPS_ORDINARY_HIGHEST_LINE (pfile->line_table);
 
   while (*buffer->cur != '\n')
     buffer->cur++;
 
   _cpp_process_line_notes (pfile, true);
-  return orig_line != pfile->line_table->highest_line;
+  return orig_line != LINEMAPS_ORDINARY_HIGHEST_LINE (pfile->line_table);
 }
 
 /* Skips whitespace, saving the next non-whitespace character.  */
@@ -965,7 +974,8 @@ skip_whitespace (cpp_reader *pfile, cppchar_t c)
       else if (c == '\0')
 	saw_NUL = true;
       else if (pfile->state.in_directive && CPP_PEDANTIC (pfile))
-	cpp_error_with_line (pfile, CPP_DL_PEDWARN, pfile->line_table->highest_line,
+	cpp_error_with_line (pfile, CPP_DL_PEDWARN,
+			     LINEMAPS_ORDINARY_HIGHEST_LINE (pfile->line_table),
 			     CPP_BUF_COL (buffer),
 			     "%s in preprocessing directive",
 			     c == '\f' ? "form feed" : "vertical tab");
@@ -1461,7 +1471,8 @@ lex_raw_string (cpp_reader *pfile, cpp_token *token, const uchar *base,
 	      source_location src_loc = token->src_loc;
 	      token->type = CPP_EOF;
 	      /* Tell the compiler the line number of the EOF token.  */
-	      token->src_loc = pfile->line_table->highest_line;
+	      token->src_loc =
+		LINEMAPS_ORDINARY_HIGHEST_LINE (pfile->line_table);
 	      token->flags = BOL;
 	      if (first_buff != NULL)
 		_cpp_release_buff (pfile, first_buff);
@@ -1952,7 +1963,8 @@ _cpp_lex_direct (cpp_reader *pfile)
 	  if (!pfile->state.in_directive)
 	    {
 	      /* Tell the compiler the line number of the EOF token.  */
-	      result->src_loc = pfile->line_table->highest_line;
+	      result->src_loc =
+		LINEMAPS_ORDINARY_HIGHEST_LINE (pfile->line_table);
 	      result->flags = BOL;
 	    }
 	  return result;
@@ -1969,14 +1981,15 @@ _cpp_lex_direct (cpp_reader *pfile)
     }
   buffer = pfile->buffer;
  update_tokens_line:
-  result->src_loc = pfile->line_table->highest_line;
+  result->src_loc =
+    LINEMAPS_ORDINARY_HIGHEST_LINE (pfile->line_table);
 
  skipped_white:
   if (buffer->cur >= buffer->notes[buffer->cur_note].pos
       && !pfile->overlaid_buffer)
     {
       _cpp_process_line_notes (pfile, false);
-      result->src_loc = pfile->line_table->highest_line;
+      result->src_loc = LINEMAPS_ORDINARY_HIGHEST_LINE (pfile->line_table);
     }
   c = *buffer->cur++;
 
diff --git a/libcpp/line-map.c b/libcpp/line-map.c
index a82c428..aeccdee 100644
--- a/libcpp/line-map.c
+++ b/libcpp/line-map.c
@@ -23,6 +23,13 @@ along with this program; see the file COPYING3.  If not see
 #include "config.h"
 #include "system.h"
 #include "line-map.h"
+#include "cpp-id-data.h"
+
+#define linemap_assert(EXPR)			\
+  do {						\
+    if (! (EXPR))				\
+      abort ();					\
+  } while (0)
 
 static void trace_include (const struct line_maps *, const struct line_map *);
 
@@ -31,17 +38,11 @@ static void trace_include (const struct line_maps *, const struct line_map *);
 void
 linemap_init (struct line_maps *set)
 {
-  set->maps = NULL;
-  set->allocated = 0;
-  set->used = 0;
-  set->last_listed = -1;
-  set->trace_includes = false;
-  set->depth = 0;
-  set->cache = 0;
-  set->highest_location = RESERVED_LOCATION_COUNT - 1;
-  set->highest_line = RESERVED_LOCATION_COUNT - 1;
-  set->max_column_hint = 0;
-  set->reallocator = 0;
+  memset (set, 0, sizeof (struct line_maps));
+  LINEMAPS_ORDINARY_HIGHEST_LOCATION (set) = RESERVED_LOCATION_COUNT - 1;
+  LINEMAPS_ORDINARY_HIGHEST_LINE (set) = RESERVED_LOCATION_COUNT - 1;
+  LINEMAPS_MACRO_HIGHEST_LOCATION (set) = MAX_SOURCE_LOCATION + 1;
+  LINEMAPS_MACRO_HIGHEST_LINE (set) = MAX_SOURCE_LOCATION + 1;
 }
 
 /* Check for and warn about line_maps entered but not exited.  */
@@ -52,62 +53,168 @@ 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.  */
+/* Free a line map set.  This should be called only if maps have
+   *NOT* been allocated in garbage memory space.  */
 
 void
 linemap_free (struct line_maps *set)
 {
-  if (set->maps)
+  struct line_map *cur_map;
+  unsigned i;
+
+  /* Free ordinary maps related memory.  */
+  if (LINEMAPS_ORDINARY_MAPS (set))
     {
       linemap_check_files_exited (set);
 
-      free (set->maps);
+      free (LINEMAPS_ORDINARY_MAPS (set));
+      LINEMAPS_ORDINARY_MAPS (set) = NULL;
+    }
+
+  /* Free macro maps related memory.  */
+  if (LINEMAPS_MACRO_MAPS (set))
+    {
+      for (cur_map = LINEMAPS_MACRO_MAPS (set), i = 0;
+	   i < LINEMAPS_MACRO_ALLOCATED (set);
+	   ++i, ++cur_map)
+	{
+	  if (MACRO_MAP_LOCATIONS (LINEMAPS_MACRO_MAP_AT (set, i)))
+	    {
+	      free (MACRO_MAP_LOCATIONS (LINEMAPS_MACRO_MAP_AT (set, i)));
+	      MACRO_MAP_LOCATIONS (LINEMAPS_MACRO_MAP_AT (set, i)) = NULL;
+	    }
+	}
+      free (LINEMAPS_MACRO_MAPS (set));
+      LINEMAPS_MACRO_MAPS (set) = NULL;
     }
 }
 
-/* Add a mapping of logical source line to physical source file and
-   line number.
+/* Returns TRUE if the line table set tracks token locations accross
+   macro expansion, FALSE otherwise.  */
 
-   The text pointed to by TO_FILE must have a lifetime
-   at least as long as the final call to lookup_line ().  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.
+bool
+linemap_tracks_macro_expansion_locs_p (struct line_maps *set)
+{
+  return LINEMAPS_MACRO_MAPS (set) != NULL;
+}
 
-   FROM_LINE should be monotonic increasing across calls to this
-   function.  A call to this function can relocate the previous set of
-   maps, so any stored line_map pointers should not be used.  */
+/* 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.  */
 
-const struct line_map *
-linemap_add (struct line_maps *set, enum lc_reason reason,
-	     unsigned int sysp, const char *to_file, linenum_type to_line)
+static struct line_map *
+new_linemap (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 ();
+  /* 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 (set->used == set->allocated)
+  if (LINEMAPS_USED (set, macro_map_p) == LINEMAPS_ALLOCATED (set, macro_map_p))
     {
+      /* We ran out of allocated line maps. Let's allocate more.  */
+
       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
+      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));
-      memset (&set->maps[set->used], 0, ((set->allocated - set->used)
-					 * 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;
+}
+
+/* Create an ordinary line map -- a mapping between a logical source
+   location and physical source file and line number.
+
+   SET is the set of line maps the new map will belong to.  The text
+   pointed to by TO_FILE must have a lifetime 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.
+
+   If LC_REASON is LC_RENAME and PREVIOUS is non NULL, it means the
+   line map created by this function is considered to be for the same
+   file as the map PREVIOUS. Setting PREVIOUS to NULL makes the
+   argument to be ignored.
+
+   A call to this function can relocate the previous set of
+   maps, so any stored line_map pointers should not be used.
+
+   This function is a subroutine of linemap_add.  */
+
+static const struct line_map *
+create_and_add_line_map_internal (struct line_maps *set, enum lc_reason reason,
+				  unsigned int sysp, const char *to_file,
+				  linenum_type to_line,
+				  const struct line_map *previous)
+{
+  struct line_map *map = NULL;
+  /* 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 *prev_map_same_file = NULL;
+  source_location start_location = LINEMAPS_ORDINARY_HIGHEST_LOCATION (set) + 1;
+  int previous_index = -1;
+
+  linemap_assert (reason != LC_ENTER_MACRO);
+
+  linemap_assert (!(LINEMAPS_ORDINARY_USED (set)
+		    && (start_location
+			< MAP_START_LOCATION (LINEMAPS_LAST_ORDINARY_MAP (set)))));
+
+  /* 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)
+    {
+      set->depth--;
+      return NULL;
     }
 
-  map = &set->maps[set->used];
+  /*
+     *Kludge* ahead. new_linemap uses ggc_realloc to enlarge
+     line_maps::maps. ggc_realloc frees the previous storage of
+     line_maps::maps. So struct line_map* pointers that where pointing
+     into line_maps::maps prior to new_linemap can become dangling
+     after new_linemap. previous is such a pointer that can become
+     dangling after new_linemap. Let's make sure we make it point into
+     the newly allocated line_maps::maps.
+  */
+  if (previous)
+    previous_index = linemap_map_get_index (set, previous);
+
+  map = new_linemap (set, reason);
+  if (previous_index > -1)
+    previous = &LINEMAPS_ORDINARY_MAPS (set)[previous_index];
 
   if (to_file && *to_file == '\0' && reason != LC_RENAME_VERBATIM)
     to_file = "<stdin>";
@@ -121,27 +228,32 @@ linemap_add (struct line_maps *set, enum lc_reason reason,
     reason = LC_ENTER;
   else if (reason == LC_LEAVE)
     {
-      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;
+	  reason = LC_RENAME;
+	  prev_map_same_file = map - 1;
 	}
       else
 	{
-	  from = INCLUDED_FROM (set, map - 1);
-	  error = to_file && strcmp (from->to_file, to_file);
+	  /* (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.  */
+	  prev_map_same_file = INCLUDED_FROM (set, map - 1);
+	  linemap_check_ordinary (prev_map_same_file);
+	  error = to_file && strcmp (ORDINARY_MAP_FILE_NAME (prev_map_same_file),
+				     to_file);
 	}
 
-      /* Depending upon whether we are handling preprocessed input or
+      /* Depending on whether we are handling preprocessed input or
 	 not, this can be a user error or an ICE.  */
       if (error)
 	fprintf (stderr, "line-map.c: file \"%s\" left but not entered\n",
@@ -150,55 +262,190 @@ 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_line = SOURCE_LINE (from, from[1].start_location);
-	  sysp = from->sysp;
+	  linemap_check_ordinary (prev_map_same_file);
+	  to_file = ORDINARY_MAP_FILE_NAME (prev_map_same_file);
+	  to_line = SOURCE_LINE (prev_map_same_file,
+				 prev_map_same_file[1].start_location);
+	  sysp = ORDINARY_MAP_IN_SYSTEM_HEADER_P (prev_map_same_file);
 	}
     }
 
-  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;
-  set->highest_location = start_location;
-  set->highest_line = start_location;
+  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;
+  LINEMAPS_ORDINARY_HIGHEST_LOCATION (set) = start_location;
+  LINEMAPS_ORDINARY_HIGHEST_LINE (set) = 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;
+    {
+      if (previous == NULL)
+	previous = &map[-1];
+      linemap_check_ordinary (previous);
+      ORDINARY_MAP_INCLUDER_FILE_INDEX (map) =
+	ORDINARY_MAP_INCLUDER_FILE_INDEX (previous);
+    }
   else if (reason == LC_LEAVE)
     {
+      linemap_assert (prev_map_same_file != NULL);
       set->depth--;
-      map->included_from = INCLUDED_FROM (set, map - 1)->included_from;
+      ORDINARY_MAP_INCLUDER_FILE_INDEX (map) =
+		ORDINARY_MAP_INCLUDER_FILE_INDEX (prev_map_same_file);
     }
 
   return map;
 }
 
+/* Add a mapping of logical source line to physical source file and
+   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 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.
+
+   A call to this function can relocate the previous set of
+   maps, so any stored line_map pointers should not be used.  */
+
+const struct line_map *
+linemap_add (struct line_maps *set, enum lc_reason reason,
+	     unsigned int sysp, const char *to_file, linenum_type to_line)
+{
+  return create_and_add_line_map_internal (set, reason, sysp,
+					   to_file, to_line,
+					   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 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 *set, struct cpp_macro *macro,
+		     source_location expansion, unsigned int num_tokens)
+{
+  struct line_map *map;
+  source_location start_location = LINEMAPS_MACRO_HIGHEST_LOCATION (set) - 1;
+  line_map_realloc reallocator
+    = set->reallocator ? set->reallocator : xrealloc;
+
+  map = new_linemap (set, LC_ENTER_MACRO);
+
+  MAP_START_LOCATION (map) = start_location;
+  MACRO_MAP_MACRO (map) = macro;
+  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;
+  LINEMAPS_MACRO_HIGHEST_LOCATION (set) =
+    start_location - num_tokens;
+  LINEMAPS_MACRO_HIGHEST_LINE (set) = 
+    start_location - num_tokens;
+
+  return map;
+}
+
+/* Create and return a source location for a token that is part of a
+   macro replacement-list at a macro expansion point.
+
+   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 orginal location of the token at the definition
+   point of the macro. If you read the extensive comments of struct
+   line_map_macro in line-map.h, this is the xI.
+
+   If the token is part of a macro argument, ORIG_PARM_REPLACEMENT_LOC
+   is the location of the point at wich the token (the argument)
+   replaces the macro parameter in the context of the relevant macro
+   definition. If you read the comments of struct line_map_macro in
+   line-map.h, this is the yI.  */
+
+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];
-  source_location highest = set->highest_location;
+  struct line_map *map = LINEMAPS_LAST_ORDINARY_MAP (set);
+  source_location highest = LINEMAPS_ORDINARY_HIGHEST_LOCATION (set);
   source_location r;
-  linenum_type last_line = SOURCE_LINE (map, set->highest_line);
+  linenum_type last_line =
+    SOURCE_LINE (map, LINEMAPS_ORDINARY_HIGHEST_LINE (set));
   int line_delta = to_line - last_line;
   bool add_map = false;
+
+  linemap_check_ordinary (map);
+
   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;
     }
@@ -212,7 +459,7 @@ linemap_line_start (struct line_maps *set, linenum_type to_line,
 	  /* If the column number is ridiculous or we've allocated a huge
 	     number of source_locations, give up on column numbers. */
 	  max_column_hint = 0;
-	  if (highest >0xF0000000)
+	  if (highest > MAX_SOURCE_LOCATION)
 	    return 0;
 	  column_bits = 0;
 	}
@@ -226,27 +473,39 @@ 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);
-  set->highest_line = r;
-  if (r > set->highest_location)
-    set->highest_location = r;
+      + (line_delta << ORDINARY_MAP_NUMBER_OF_COLUMN_BITS (map));
+  LINEMAPS_ORDINARY_HIGHEST_LINE (set) = r;
+  if (r > LINEMAPS_ORDINARY_HIGHEST_LOCATION (set))
+      LINEMAPS_ORDINARY_HIGHEST_LOCATION (set) = r;
   set->max_column_hint = max_column_hint;
   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;
+  source_location r = LINEMAPS_ORDINARY_HIGHEST_LINE (set);
+
+  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)
@@ -256,35 +515,65 @@ 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);
 	}
     }
   r = r + to_column;
-  if (r >= set->highest_location)
-    set->highest_location = r;
+  if (r >= LINEMAPS_ORDINARY_HIGHEST_LOCATION (set))
+    LINEMAPS_ORDINARY_HIGHEST_LOCATION (set) = r;
   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.  */
+
+source_location
+linemap_position_for_line_and_column (struct line_map *map,
+				      linenum_type line,
+				      unsigned column)
+{
+  linemap_check_ordinary (map);
+  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 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.  */
 
 const struct line_map *
 linemap_lookup (struct line_maps *set, source_location line)
 {
   unsigned int md, mn, mx;
   const struct line_map *cached;
+  bool macro_map_p;
+
+  if (set ==  NULL)
+    return NULL;
 
-  mn = set->cache;
-  mx = set->used;
+  macro_map_p = linemap_location_from_macro_expansion_p (set, line);
+
+  if (LINEMAPS_MAPS (set, macro_map_p) == NULL)
+    return NULL;
+
+  mn = LINEMAPS_CACHE (set, macro_map_p);
+  mx = LINEMAPS_USED (set, macro_map_p);
   
-  cached = &set->maps[mn];
+  cached = &LINEMAPS_MAPS (set, macro_map_p)[mn];
   /* We should get a segfault if no line_maps have been added yet.  */
-  if (line >= cached->start_location)
+  if (LOCATION_POSSIBLY_IN_MAP_P (line, cached, macro_map_p))
     {
-      if (mn + 1 == mx || line < cached[1].start_location)
+      if (mn + 1 == mx || !LOCATION_POSSIBLY_IN_MAP_P (line,
+						       &cached[1],
+						       macro_map_p))
 	return cached;
     }
   else
@@ -296,14 +585,316 @@ 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 (!LOCATION_POSSIBLY_IN_MAP_P (line,
+				       &LINEMAPS_MAPS (set, macro_map_p)[md],
+				       macro_map_p))
 	mx = md;
       else
 	mn = md;
     }
 
-  set->cache = mn;
-  return &set->maps[mn];
+  LINEMAPS_CACHE (set, macro_map_p) = mn;
+  return &LINEMAPS_MAPS (set, macro_map_p)[mn];
+}
+
+/* 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);
+}
+
+/* Assert that MAP encodes locations of tokens that are not part of the
+   replacement-list of a macro expansion.  */
+
+int
+linemap_check_ordinary (const struct line_map *map)
+{
+  linemap_assert (!linemap_macro_expansion_map_p (map));
+  /* Return any old value.  */
+  return 0;
+}
+
+/* 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_POSSIBLY_IN_MACRO_MAP_P (location, map));
+
+  /* Make sure LOCATION is correct.  */
+  token_no = MAP_START_LOCATION (map) - location;
+  linemap_assert (token_no <  MACRO_MAP_NUM_MACRO_TOKENS (map));
+
+  return MACRO_MAP_EXPANSION_POINT_LOCATION (map);
+}
+
+/* If LOCATION is the locus of a token in a replacement-list of a
+   macro expansion return the location of said token in the
+   definition of the macro.
+
+   Read the comments of struct line_map and struct line_map_macro
+   in line-map.h to understand what a macro expansion point is.
+
+   If RETURN_MACRO_PARM_USAGE_POINT_P is TRUE and if LOCATION is the
+   locus of a token that is an argument of a macro M, this function
+   returns the locus of the parameter replaced by the argument, in the
+   definition of M. This is the yI in the comments of struct
+   line_map_macro in line-map.h.
+
+   Note that if the token is a builtin the function returns the
+   location of the expansion point of the macro.  */
+
+source_location
+linemap_macro_map_loc_to_def_point (const struct line_map *map,
+				    source_location location,
+				    bool return_macro_parm_usage_point_p)
+{
+  unsigned token_no;
+  linemap_assert (linemap_macro_expansion_map_p (map)
+		  && LOCATION_POSSIBLY_IN_MACRO_MAP_P (location, map));
+
+  token_no = MAP_START_LOCATION (map) - location;
+  linemap_assert (token_no < MACRO_MAP_NUM_MACRO_TOKENS (map));
+
+  if (return_macro_parm_usage_point_p)
+    location = MACRO_MAP_LOCATIONS (map)[2 * token_no + 1];
+  else
+    location = MACRO_MAP_LOCATIONS (map)[2 * token_no];
+
+  if (location >= RESERVED_LOCATION_COUNT)
+    return location;
+  else
+    /* If LOCATION is reserved for the user of libcpp, it means,
+     e.g. for gcc that it's the location of a built-in token. In that
+     case, let's say that the final location is the macro expansion
+     point because otherwise, the built-in location would not make
+     any sense and would violate the invariant that says that every
+     single location must be >= to the MAP_START_LOCATION (MAP) of its
+     map.  */
+    return MACRO_MAP_EXPANSION_POINT_LOCATION (map);
+}
+
+
+/* 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.  */
+
+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;
+}
+
+/* Return the index of MAP into set. MAP can be either an ordinary or
+   a macro map.  */
+
+int
+linemap_map_get_index (const struct line_maps *set,
+		       const struct line_map* map)
+{
+  int index;
+  bool macro_map_p;
+
+  linemap_assert (set && map);
+
+  macro_map_p = (map->reason == LC_ENTER_MACRO) ? true : false;
+
+  index = map - LINEMAPS_MAPS (set, macro_map_p);
+  linemap_assert (LINEMAPS_MAP_AT (set, macro_map_p, index)  == map);
+
+  return index;
+}
+
+/* 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_loc_to_def_point (struct line_maps *set,
+				source_location location,
+				const struct line_map **original_map,
+				bool return_macro_parm_usage_point_p)
+{
+  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,
+					    return_macro_parm_usage_point_p);
+    }
+
+  if (original_map)
+    *original_map = map;
+  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_source_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);
+  linemap_check_ordinary (map);
+
+  return ((location - MAP_START_LOCATION (map))
+	    >> ORDINARY_MAP_NUMBER_OF_COLUMN_BITS (map))
+	 + ORDINARY_MAP_STARTING_LINE_NUMBER (map);
+}
+
+/* Return the column number corresponding to location LOCATION.
+
+   If LOCATION is the source location of token that is part of the
+   replacement-list of a macro expansion return the column number of
+   the macro expansion point.
+
+   SET is the line map set LOCATION comes from.  */
+
+int
+linemap_get_source_column (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);
+  linemap_check_ordinary (map);
+
+  return (location - MAP_START_LOCATION (map))
+	  & ((1 << ORDINARY_MAP_NUMBER_OF_COLUMN_BITS (map)) - 1);
+}
+
+/* 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_file_path (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);
+  linemap_check_ordinary (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)->name);
+}
+
+/* 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 0 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_macro_loc_to_def_point (set, location, &map, false);
+  linemap_check_ordinary (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);
+  if (set == NULL)
+    return false;
+  return (location > LINEMAPS_ORDINARY_HIGHEST_LOCATION (set));
 }
 
 /* Print an include trace, for e.g. the -H option of the preprocessor.  */
@@ -315,5 +906,78 @@ trace_include (const struct line_maps *set, const struct line_map *map)
 
   while (--i)
     putc ('.', stderr);
-  fprintf (stderr, " %s\n", map->to_file);
+  linemap_check_ordinary (map);
+  fprintf (stderr, " %s\n", ORDINARY_MAP_FILE_NAME (map));
+}
+
+/* Expand source code location LOC and return a user readable source
+   code 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.
+
+   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 function expands the location to the locus of the
+   expansion point of the macro.
+
+   * If LRK is set to LRK_SPELLING_LOCATION
+   -------------------------------------
+
+   The function expands 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 function expands 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.  */
+
+expanded_location
+linemap_expand_location_full (struct line_maps *set,
+			      source_location loc,
+			      enum location_resolution_kind lrk,
+			      const struct line_map **loc_map)
+{
+  const struct line_map *map;
+  expanded_location xloc;
+
+  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_def_point (set, loc, &map, false);
+      break;
+    case LRK_MACRO_PARM_REPLACEMENT_POINT:
+      loc = linemap_macro_loc_to_def_point (set, loc, &map, true);
+      break;
+    default:
+      abort ();
+    }
+
+  xloc.file = LINEMAP_FILE (map);
+  xloc.line = SOURCE_LINE (map, loc);
+  xloc.column = SOURCE_COLUMN (map, loc);
+  xloc.sysp = LINEMAP_SYSP (map) != 0;
+  if (loc_map)
+    *loc_map = map;
+
+  return xloc;
 }
diff --git a/libcpp/macro.c b/libcpp/macro.c
index d9324a3..419114c 100644
--- a/libcpp/macro.c
+++ b/libcpp/macro.c
@@ -171,13 +171,16 @@ _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);
+	map =
+	  linemap_lookup (pfile->line_table,
+			  LINEMAPS_ORDINARY_HIGHEST_LINE (pfile->line_table));
 
 	if (node->value.builtin == BT_BASE_FILE)
 	  while (! MAIN_FILE_P (map))
 	    map = INCLUDED_FROM (pfile->line_table, map);
 
-	name = map->to_file;
+	linemap_check_ordinary (map);
+	name = map->d.ordinary.to_file;
 	len = strlen (name);
 	buf = _cpp_unaligned_alloc (pfile, len * 2 + 3);
 	result = buf;
@@ -196,14 +199,15 @@ _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_source_line (pfile->line_table,
+					CPP_OPTION (pfile, traditional)
+					? LINEMAPS_ORDINARY_HIGHEST_LINE
+					(pfile->line_table)
+					: pfile->cur_token[-1].src_loc);
       break;
 
       /* __STDC__ has the value 1 under normal circumstances.
@@ -1857,6 +1861,7 @@ _cpp_create_definition (cpp_reader *pfile, cpp_hashnode *node)
       (sizeof (cpp_macro));
   else
     macro = (cpp_macro *) _cpp_aligned_alloc (pfile, sizeof (cpp_macro));
+  macro->name = node;
   macro->line = pfile->directive_line;
   macro->params = 0;
   macro->paramc = 0;
diff --git a/libcpp/traditional.c b/libcpp/traditional.c
index 7ff11bb..5034b3f 100644
--- a/libcpp/traditional.c
+++ b/libcpp/traditional.c
@@ -149,7 +149,7 @@ static const uchar *
 copy_comment (cpp_reader *pfile, const uchar *cur, int in_define)
 {
   bool unterminated, copy = false;
-  source_location src_loc = pfile->line_table->highest_line;
+  source_location src_loc = LINEMAPS_ORDINARY_HIGHEST_LINE (pfile->line_table);
   cpp_buffer *buffer = pfile->buffer;
 
   buffer->cur = cur;
@@ -365,7 +365,7 @@ _cpp_scan_out_logical_line (cpp_reader *pfile, cpp_macro *macro)
   CUR (pfile->context) = pfile->buffer->cur;
   RLIMIT (pfile->context) = pfile->buffer->rlimit;
   pfile->out.cur = pfile->out.base;
-  pfile->out.first_line = pfile->line_table->highest_line;
+  pfile->out.first_line = LINEMAPS_ORDINARY_HIGHEST_LINE (pfile->line_table);
   /* start_of_input_line is needed to make sure that directives really,
      really start at the first character of the line.  */
   start_of_input_line = pfile->buffer->cur;
@@ -493,7 +493,8 @@ _cpp_scan_out_logical_line (cpp_reader *pfile, cpp_macro *macro)
 		    {
 		      maybe_start_funlike (pfile, node, out_start, &fmacro);
 		      lex_state = ls_fun_open;
-		      fmacro.line = pfile->line_table->highest_line;
+		      fmacro.line =
+			LINEMAPS_ORDINARY_HIGHEST_LINE (pfile->line_table);
 		      continue;
 		    }
 		  else if (!recursive_macro (pfile, node))
-- 
        Dodji

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

* [PATCH 2/6] Generate virtual locations for tokens
  2010-12-10 11:27 [PATCH 0/6] Tracking locations of tokens resulting from macro expansion Dodji Seketeli
                   ` (4 preceding siblings ...)
  2010-12-10 11:53 ` [PATCH 1/6] Linemap infrastructure for virtual locations Dodji Seketeli
@ 2010-12-10 12:27 ` Dodji Seketeli
  2010-12-10 12:33 ` [PATCH 6/6] Kill pedantic warnings on system headers macros Dodji Seketeli
                   ` (3 subsequent siblings)
  9 siblings, 0 replies; 135+ messages in thread
From: Dodji Seketeli @ 2010-12-10 12:27 UTC (permalink / raw)
  To: gcc-patches; +Cc: tromey, joseph, gdr, lopezibanez

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. As a result, a new kind of token (struct
cpp_expanded_token) is created. That token is just a normal token plus
a virtual location. That expanded token is then pushed onto a new kind
of context, suprisingly named an expanded token context. cpp_get_token
is modified to recognize expanded tokens from extended token contexts
and return the normal tokens from there. A similar modification was
done to cpp_get_token_with_location but this one now returns the
virtual location of the expanded token. Note that expanded tokens
don't leak out from libcpp. cpp_get_token and
cpp_get_token_with_location shield the rest of the world from that
implementation detail.

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.

Our beloved expand_location has been modified to resolve virtual
locations to spelling locations -- when macro expansion tracking is
turned on. Otherwise the behaviour is unchanged compared to before.

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

The combination of this patch and the previous one bootstraps with
--enable-languages=all,ada and passes regression tests on
x86_64-unknown-linux-gnu.

gcc/
	* 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/

	* 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/

	* include/cpplib.h (struct cpp_options)<track_macro_expansion>:
	New option.
	* internal.h (struct cpp_extended_token): New struct.
	(enum context_tokens_kind): New enum.
	(struct cpp_context)<tokens_kind>: New member of type enum
	context_tokens_kind.
	(_cpp_remaining_tokens_num_in_context): Declare new function.
	* init.c (cpp_create_reader): Initialize the base context to zero.
	(_cpp_remaining_tokens_num_in_context): Define new function.
	(_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 (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_args_mem, ensure_expanded_args_room)
	(alloc_args_buff, extend_arg_tokens_room, set_arg_token)
	(get_arg_token_location, arg_token_ptr_at, arg_token_at)
	(next_macro_token_ptr, prev_macro_token_ptr, macro_token_ptr_at)
	(next_arg_location_ptr, 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_append_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. Splitted and extended from ...
	(cpp_get_token): ... here.
	(stringify_arg): Use the new arg_token_at.
	(paste_tokens): Support the new cpp_extended_token struct kind of
	tokens in the macro token stream. Use the new next_macro_token_ptr
	function.
	(collect_args): Use the new alloc_args_buff to allocate the buffer
	holding the tokens of the arguments. Use arg_token_ptr_at and
	arg_token_at to access the tokens of an argument. Use
	extend_arg_tokens_room to grow the argument tokens buffer. Use
	cpp_get_token_1 to get virtual locations of the tokens of the
	arguments. Use set_arg_token to set a token into the tokens buffer
	of the argument.
	(expand_arg): Use the new alloc_expanded_args_mem,
	push_extended_tokens_context and set_arg_token.
	(_cpp_pop_context): Really free the memory hold by the context.
	(replace_args): Use the new tokens_buff_new, linemap_enter_macro,
	macro_arg_token_iter_init, macro_arg_token_iter_get_token,
	macro_arg_token_iter_forward, tokens_buff_remove_last_token,
	tokens_buff_count, tokens_buff_last_token_ptr,
	tokens_buff_append_token, expanded_token_index,
	push_extended_tokens_context.
	(next_context, push_ptoken_context, _cpp_push_token_context)
	(_cpp_push_text_context): Propertly initialize the context.
	(enter_macro_context): Add a parameter for macro expansion point.
	Pass it to replace_args and to the "used" cpp callback.  When
	-ftrack-macro-expansion is in effect, create a macro map for the
	macro expansion and use it to allocate proper virtual locations
	for tokens resulting from the expansion.
	(cpp_get_token_with_location): Use cpp_get_token_1 and
	maybe_adjust_loc_for_trad_cpp.
	(_cpp_backup_tokens): Support the new kinds of token contexts.
---
 gcc/c-family/c-opts.c   |   13 +
 gcc/c-family/c.opt      |    8 +
 gcc/doc/cppopts.texi    |   17 +
 gcc/doc/invoke.texi     |    6 +-
 libcpp/include/cpplib.h |    8 +
 libcpp/init.c           |    1 +
 libcpp/internal.h       |   43 ++-
 libcpp/lex.c            |   44 ++-
 libcpp/macro.c          | 1169 ++++++++++++++++++++++++++++++++++++++++++-----
 9 files changed, 1187 insertions(+), 122 deletions(-)

diff --git a/gcc/c-family/c-opts.c b/gcc/c-family/c-opts.c
index befd644..4c2e37e 100644
--- a/gcc/c-family/c-opts.c
+++ b/gcc/c-family/c-opts.c
@@ -621,6 +621,19 @@ 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;
+      goto ftrack_macro_expansion_with_arg;
+
+    case OPT_ftrack_macro_expansion_:
+    ftrack_macro_expansion_with_arg:
+      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 8682471..25d0cb6 100644
--- a/gcc/c-family/c.opt
+++ b/gcc/c-family/c.opt
@@ -909,6 +909,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 57624ed..a4f8b32 100644
--- a/gcc/doc/cppopts.texi
+++ b/gcc/doc/cppopts.texi
@@ -583,6 +583,23 @@ 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. It
+is the default value.
+
 @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 7c80415..07f2b20 100644
--- a/gcc/doc/invoke.texi
+++ b/gcc/doc/invoke.texi
@@ -415,9 +415,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/libcpp/include/cpplib.h b/libcpp/include/cpplib.h
index 8fa2881..783576b 100644
--- a/libcpp/include/cpplib.h
+++ b/libcpp/include/cpplib.h
@@ -389,6 +389,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. all macro
+     expansion. 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/init.c b/libcpp/init.c
index 8836143..02023e8 100644
--- a/libcpp/init.c
+++ b/libcpp/init.c
@@ -151,6 +151,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;
diff --git a/libcpp/internal.h b/libcpp/internal.h
index f02c878..9124276 100644
--- a/libcpp/internal.h
+++ b/libcpp/internal.h
@@ -139,6 +139,44 @@ struct tokenrun
 #define CUR(c) ((c)->u.trad.cur)
 #define RLIMIT(c) ((c)->u.trad.rlimit)
 
+
+/* This is a "special" token representation that reuses a "normal"
+   token and adds a location. The location is useful when the tokens
+   results from macro expansion. In that case LOCATION encodes the
+   locus of the token in the macro definition as well as the locus of
+   the token at the macro expansion point. This representation is
+   used when -ftrack-macro-expansion is on, and for tokens resulting
+   from macro expansion only.  */
+struct cpp_extended_token
+{
+  const cpp_token *token;
+  /* The location of the token in the context of the macro expansion.
+     This is different from the location of the token member above
+     which would be the location of the token in the context of the
+     definition of the macro.  */
+  source_location location;
+};
+typedef struct cpp_extended_token cpp_ext_token;
+
+/* 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 if u.iso.first
+     contains an instance of cpp_extended_token. This is used only
+     when the -ftrack-macro-expansion flag is on. Note that in this
+     case the static type of u.iso.first is still considered to be
+     cpp_token ** even though each element of the array pointed to by
+     u.iso.first are effectively instances of cpp_exted_token. This is
+     a hack done so that the size of u.iso.first remains "small" when
+     the flag -ftrack-macro-expansion is not used.  */
+  TOKENS_KIND_EXTENDED
+};
+
 typedef struct cpp_context cpp_context;
 struct cpp_context
 {
@@ -171,8 +209,8 @@ struct cpp_context
   /* For a macro context, the macro node, otherwise NULL.  */
   cpp_hashnode *macro;
 
-  /* True if utoken element is token, else ptoken.  */
-  bool direct_p;
+  /* This determines the type of tokens hold by this context.  */
+  enum context_tokens_kind tokens_kind;
 };
 
 struct lexer_state
@@ -601,6 +639,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 bcf292c..3071952 100644
--- a/libcpp/lex.c
+++ b/libcpp/lex.c
@@ -1719,6 +1719,41 @@ 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)
+    return (LAST (context).ptoken - FIRST (context).ptoken) / sizeof (cpp_token *);
+  else if (context->tokens_kind == TOKENS_KIND_EXTENDED)
+    return (LAST (context).ptoken - FIRST (context).ptoken) / sizeof (cpp_ext_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)
+    return FIRST (context).ptoken[index];
+  else if (context->tokens_kind == TOKENS_KIND_EXTENDED)
+    {
+      cpp_ext_token *ptr = (cpp_ext_token *) FIRST (context).ptoken;
+      return ptr[index].token;
+    }
+ else
+   abort ();
+}
+
 /* Look ahead in the input stream.  */
 const cpp_token *
 cpp_peek_token (cpp_reader *pfile, int index)
@@ -1730,15 +1765,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/macro.c b/libcpp/macro.c
index 419114c..edb59e9 100644
--- a/libcpp/macro.c
+++ b/libcpp/macro.c
@@ -37,15 +37,55 @@ 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.  */
+  size_t expanded_capacity;       /* total size of expanded array.  */
+};
+
+/* 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 token coming from a function line macro
+   argument.  */
+typedef struct macro_arg_token_iter macro_arg_token_iter;
+struct macro_arg_token_iter
+{
+  /* The cpp_reader the macro comes from.  */
+  cpp_reader *pfile;
+  /* The kind of token which we are supposed to iterator over.  */
+  enum macro_arg_token_kind kind;
+  /* The function-like macro the tokens come from.  */
+  const macro_arg *arg;
+  /* 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 locuses
+     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 *, const cpp_token **,
+					  unsigned int);
 static _cpp_buff *collect_args (cpp_reader *, const cpp_hashnode *,
 				_cpp_buff **);
 static cpp_context *next_context (cpp_reader *);
@@ -55,8 +95,50 @@ 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_args_mem (cpp_reader *, macro_arg *, size_t);
+static void ensure_expanded_args_room (cpp_reader *, macro_arg *, size_t);
+static _cpp_buff *alloc_args_buff (cpp_reader *, size_t);
+static _cpp_buff *extend_arg_tokens_room (cpp_reader *, _cpp_buff *, size_t);
+static void set_arg_token (cpp_reader *, macro_arg *, const cpp_token *,
+			   source_location, size_t, enum macro_arg_token_kind);
+static const source_location *get_arg_token_location (cpp_reader *,
+						      const macro_arg *,
+						      enum macro_arg_token_kind);
+static const cpp_token **arg_token_ptr_at (cpp_reader *, const macro_arg *,
+					   size_t, enum macro_arg_token_kind);
+static const cpp_token *arg_token_at (cpp_reader *, macro_arg *, size_t,
+				      enum macro_arg_token_kind);
+static const cpp_token **next_macro_token_ptr (cpp_reader *, const cpp_token **);
+static const cpp_token **prev_macro_token_ptr (cpp_reader *, const cpp_token **);
+static const cpp_token **macro_token_ptr_at (cpp_reader *, const cpp_token **, int);
+static source_location *next_arg_location_ptr (cpp_reader *,
+					       const source_location *);
+static void macro_arg_token_iter_init (macro_arg_token_iter *, cpp_reader*,
+				       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);
+static size_t tokens_buff_count (cpp_reader *, _cpp_buff *);
+static const cpp_token **tokens_buff_last_token_ptr (cpp_reader *,
+						      _cpp_buff *);
+static const cpp_token **tokens_buff_put_token_to
+(cpp_reader *, const cpp_token **, const cpp_token *, source_location,
+ source_location, const struct line_map *, unsigned int *);
+
+static const cpp_token **tokens_buff_append_token (cpp_reader *,
+						   _cpp_buff *,
+						   const cpp_token *,
+						   source_location,
+						   source_location,
+						   const struct line_map *,
+						   unsigned int *);
+static void tokens_buff_remove_last_token (cpp_reader *, _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 **);
 static bool create_iso_definition (cpp_reader *, cpp_macro *);
@@ -70,6 +152,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.  */
@@ -369,7 +456,8 @@ stringify_arg (cpp_reader *pfile, macro_arg *arg)
   /* Loop, reading in the argument's tokens.  */
   for (i = 0; i < arg->count; i++)
     {
-      const cpp_token *token = arg->first[i];
+      const cpp_token *token = arg_token_at (pfile, arg, i,
+					     MACRO_ARG_TOKEN_NORMAL);
 
       if (token->type == CPP_PADDING)
 	{
@@ -511,7 +599,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 +609,16 @@ 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)
+	{
+	  rhs = ((cpp_ext_token *) FIRST (context).ptoken)->token;
+	  FIRST (context).ptoken =
+	    next_macro_token_ptr (pfile, FIRST (context).ptoken);
+	}
 
       if (rhs->type == CPP_PADDING)
 	{
@@ -598,14 +692,16 @@ collect_args (cpp_reader *pfile, const cpp_hashnode *node,
   macro_arg *args, *arg;
   const cpp_token *token;
   unsigned int argc;
+  source_location virt_loc;
 
   macro = node->value.macro;
   if (macro->paramc)
     argc = macro->paramc;
   else
     argc = 1;
-  buff = _cpp_get_buff (pfile, argc * (50 * sizeof (cpp_token *)
-				       + sizeof (macro_arg)));
+
+  buff = alloc_args_buff (pfile, argc);
+
   base_buff = buff;
   args = (macro_arg *) buff->base;
   memset (args, 0, argc * sizeof (macro_arg));
@@ -626,15 +722,16 @@ collect_args (cpp_reader *pfile, const cpp_hashnode *node,
       for (;;)
 	{
 	  /* Require space for 2 new tokens (including a CPP_EOF).  */
-	  if ((unsigned char *) &arg->first[ntokens + 2] > buff->limit)
+	  if ((unsigned char *) arg_token_ptr_at (pfile, arg, ntokens + 2,
+						  MACRO_ARG_TOKEN_NORMAL)
+	      > buff->limit)
 	    {
-	      buff = _cpp_append_extend_buff (pfile, buff,
-					      1000 * sizeof (cpp_token *));
+	      buff = extend_arg_tokens_room (pfile, buff, 1000);
 	      arg->first = (const cpp_token **) buff->cur;
 	    }
 
-	  token = cpp_get_token (pfile);
-
+	  token = cpp_get_token_1 (pfile, &virt_loc);
+	  
 	  if (token->type == CPP_PADDING)
 	    {
 	      /* Drop leading padding.  */
@@ -690,7 +787,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,22 +801,28 @@ collect_args (cpp_reader *pfile, const cpp_hashnode *node,
 	      else
 		continue;
 	    }
-
-	  arg->first[ntokens++] = token;
+	  set_arg_token (pfile, arg, token, virt_loc,
+			 ntokens, MACRO_ARG_TOKEN_NORMAL);
+	  ntokens++;
 	}
 
       /* Drop trailing padding.  */
-      while (ntokens > 0 && arg->first[ntokens - 1]->type == CPP_PADDING)
+      while (ntokens > 0
+	     && arg_token_at (pfile, arg, ntokens - 1,
+			      MACRO_ARG_TOKEN_NORMAL)->type == CPP_PADDING)
 	ntokens--;
 
       arg->count = ntokens;
-      arg->first[ntokens] = &pfile->eof;
+      set_arg_token (pfile, arg, &pfile->eof, pfile->eof.src_loc,
+		     ntokens, MACRO_ARG_TOKEN_NORMAL);
 
       /* Terminate the argument.  Excess arguments loop back and
 	 overwrite the final legitimate argument, before failing.  */
       if (argc <= macro->paramc)
 	{
-	  buff->cur = (unsigned char *) &arg->first[ntokens + 1];
+	  buff->cur =
+	    (unsigned char *) arg_token_ptr_at (pfile, arg, ntokens + 1,
+						MACRO_ARG_TOKEN_NORMAL);
 	  if (argc != macro->paramc)
 	    arg++;
 	}
@@ -823,13 +926,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 *token, source_location location)
 {
   /* The presence of a macro invalidates a file's controlling macro.  */
   pfile->mi_valid = false;
@@ -877,8 +982,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);
+	  /* Unlike _cpp_release_buff, calling _cpp_free_buff
+	     actually frees the memory for real. This reduces peak
+	     memory usage on source code with a lot of macros.  */
+	  _cpp_free_buff (buff);
 	}
 
       /* Disable the macro within its expansion.  */
@@ -892,19 +1002,47 @@ 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;
+	      _cpp_buff *macro_tokens =
+		tokens_buff_new (pfile, count);
+		
+	      /* 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,
+					  macro, location, count);
+	      for (i = 0; i < count; ++i)
+		{
+		  tokens_buff_append_token (pfile, macro_tokens, src,
+					    src->src_loc, src->src_loc,
+					    map, &i);
+		  ++src;
+		}
+	      push_extended_tokens_context (pfile, node, macro_tokens,
+					    (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)
 	{
 	  if (!pfile->state.in_directive)
 	    _cpp_push_token_context (pfile, NULL,
-				     padding_token (pfile, result), 1);
+				     padding_token (pfile, token), 1);
 	  do
 	    {
 	      _cpp_buff *tail = pragma_buff->next;
@@ -926,33 +1064,336 @@ enter_macro_context (cpp_reader *pfile, cpp_hashnode *node,
   return builtin_macro (pfile, node);
 }
 
+/* Allocates a buffer to hold tokens coming from macro arguments. If
+   -ftrack-macro-expansion is used the buffer holds instances of
+   cpp_ext_token, otherwise it holds instance of cpp_token *.  The
+   buffer is big enough to contain NUM_ARGS instances of macro_arg
+   data, and for each instance of macro_arg can contain 50 tokens that
+   can be accessed from the macro_arg::first pointer.  */
+static _cpp_buff*
+alloc_args_buff (cpp_reader *pfile, size_t num_args)
+{
+  bool track_macro_exp_p = CPP_OPTION (pfile, track_macro_expansion);
+  size_t arg_size;
+
+  if (track_macro_exp_p)
+    arg_size = 50 * sizeof (cpp_ext_token) + sizeof (macro_arg);
+  else
+    arg_size = 50 * sizeof (cpp_token *) + sizeof (macro_arg);
+
+  return _cpp_get_buff (pfile, num_args * arg_size);
+}
+
+/* Enlarges the memory room pointed to by the macro_arg::first pointer
+   to make it capable of containing NUM_ELEMS more worth of tokens.
+
+   Creates a new buffer big enough to hold NUM_ELEMS tokens as well as
+   a the uncommitted tokens remaining in BUFF, chain that new buffer
+   to the end of BUFF, and return it. BUFF->CUR must be the memory room to
+   store tokens belonging to a function-like macro argument (the
+   macro_arg::first pointer).  */
+static _cpp_buff *
+extend_arg_tokens_room (cpp_reader *pfile, _cpp_buff *buff, size_t num_elems)
+{
+  bool track_macro_exp_p = CPP_OPTION (pfile, track_macro_expansion);
+  size_t token_size =
+    (track_macro_exp_p) ? sizeof (cpp_ext_token) : sizeof (cpp_token *);
+
+  return _cpp_append_extend_buff (pfile, buff, num_elems * token_size);
+}
+
+/* Set the INDEXth token of the macro argument ARG. TOKEN is the
+   token to set, LOCATION is its location. If TOKEN comes from a
+   macro expansion and if -ftrack-macro-location is in effect,
+   LOCATION must be the location that encodes locuses accross macro
+   expansion. Otherwise it has to be TOKEN->SRC_LOC. KIND is the kind
+   of tokens the argument ARG is supposed to contain.  */
+static void
+set_arg_token (cpp_reader *pfile, macro_arg *arg, const cpp_token *token,
+	       source_location location, size_t index,
+	       enum macro_arg_token_kind kind)
+{  
+  bool track_macro_exp_p = CPP_OPTION (pfile, track_macro_expansion);
+  const cpp_token **token_ptr;
+
+  token_ptr = arg_token_ptr_at (pfile, arg, index, kind);
+  if (track_macro_exp_p)
+    {
+      ((cpp_ext_token *) token_ptr)->token = (cpp_token *) token;
+      ((cpp_ext_token *) token_ptr)->location = location;
+    }
+  else
+    *token_ptr = token;
+}
+
+/* Get the pointer to the location of the argument token of the
+   function-like macro argument ARG.  */
+static const source_location *
+get_arg_token_location (cpp_reader *pfile,
+			const macro_arg *arg,
+			enum macro_arg_token_kind kind)
+{
+  bool track_macro_exp_p = CPP_OPTION (pfile, track_macro_expansion);
+  const cpp_token **token_ptr = arg_token_ptr_at (pfile, arg, 0, kind);
+
+  if (token_ptr == NULL)
+    return NULL;
+
+  if (track_macro_exp_p)
+      return &((cpp_ext_token *) token_ptr)->location;
+  return (source_location *) &((*token_ptr)->src_loc);
+
+}
+
+/* Return the pointer to the INDEXth token of the macro argument ARG.
+   KIND specifies the kind of token the macro argument ARG
+   contains.  */
+static const cpp_token **
+arg_token_ptr_at (cpp_reader *pfile, const macro_arg *arg,
+		  size_t index, enum macro_arg_token_kind kind)
+{
+  bool track_macro_exp_p = CPP_OPTION (pfile, track_macro_expansion);
+  const cpp_token **ptr = NULL;
+
+  switch (kind)
+    {
+    case MACRO_ARG_TOKEN_NORMAL:
+      ptr = arg->first;
+      break;
+    case MACRO_ARG_TOKEN_STRINGIFIED:
+      ptr = (const cpp_token **) &arg->stringified;
+      break;
+    case MACRO_ARG_TOKEN_EXPANDED:
+      ptr = arg->expanded;
+      break;
+    }
+
+  if (track_macro_exp_p)
+    return (const cpp_token **) &((cpp_ext_token *) ptr)[index].token;
+  else
+    return &ptr[index];
+}
+
+/* Return the INDEXth token of the macro argument ARG.  KIND specifies
+   the kind of token the macro argument ARG contains.  */
+static const cpp_token *
+arg_token_at (cpp_reader *pfile, macro_arg *arg, size_t index,
+	      enum macro_arg_token_kind kind)
+{
+  return *arg_token_ptr_at (pfile, arg, index, kind);
+}
+
+/* Return the address of the token coming right after *TOKEN_PTR. The
+   function handles the different types of tokens. If
+   -ftrack-macro-expansion is used, the token pointed to by TOKEN_PTR
+   is considered to be an instance of cpp_ext_token otherwise it's a
+   cpp_token*.  */
+static const cpp_token **
+next_macro_token_ptr (cpp_reader *pfile, const cpp_token **token_ptr)
+{
+  return macro_token_ptr_at (pfile, token_ptr, 1);
+}
+
+/* Return the address of the token coming right before *TOKEN_PTR. The
+   function handles the different types of tokens. If
+   -ftrack-macro-expansion is used, the token pointed to by TOKEN_PTR
+   is considered to be an instance of cpp_ext_token otherwise it's a
+   cpp_token*.  */
+static const cpp_token **
+prev_macro_token_ptr (cpp_reader *pfile, const cpp_token **token_ptr)
+{
+  return macro_token_ptr_at (pfile, token_ptr, -1);
+}
+
+/* Return the address of the token at index INDEX, starting from
+   TOKEN_PTR. The function handles the different types of tokens. If
+   -ftrack-macro-expansion is used, the token pointed to by TOKEN_PTR
+   is considered to be an instance of cpp_ext_token otherwise it's a
+   cpp_token *.  */
+static const cpp_token **
+macro_token_ptr_at (cpp_reader *pfile,
+		    const cpp_token **token_ptr,
+		    int index)
+{
+  bool track_macro_exp_p = CPP_OPTION (pfile, track_macro_expansion);
+
+  if (track_macro_exp_p)
+    return &((cpp_ext_token *) (token_ptr))[index].token;
+  return &token_ptr[index];
+}
+
+/* Return the address of the location of the token coming right after
+   the token which location is pointed to by LOCATION. If
+   -ftrack-macro-expansion is used, the token is considered to be an
+   instance of cpp_ext_token otherwise it's a cpp_token *.  */
+static source_location *
+next_arg_location_ptr (cpp_reader *pfile,
+		       const source_location *location)
+{
+  bool track_macro_exp_p = CPP_OPTION (pfile, track_macro_expansion);
+
+  if (track_macro_exp_p)
+    {
+      cpp_ext_token *base;
+      base = (cpp_ext_token *)
+	((unsigned char*) location - offsetof (cpp_ext_token, location));
+      return &base[1].location;
+    }
+  else
+    abort ();
+}
+
+/* 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,
+			   cpp_reader *pfile,
+			   enum macro_arg_token_kind kind,
+			   const macro_arg *arg,
+			   const cpp_token **token_ptr)
+{
+  iter->pfile = pfile;
+  iter->kind = kind;
+  iter->arg = arg;
+  iter->token_ptr = token_ptr;
+  iter->location_ptr = get_arg_token_location (pfile, arg, kind);
+#ifdef ENABLE_CHECKING
+  iter->num_forwards = 0;
+#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)
+{
+  bool track_macro_exp_p = CPP_OPTION (it->pfile,
+				       track_macro_expansion);
+
+  switch (it->kind)
+    {
+    case MACRO_ARG_TOKEN_NORMAL:
+    case MACRO_ARG_TOKEN_EXPANDED:
+      it->token_ptr = next_macro_token_ptr (it->pfile,
+					    it->token_ptr);
+      if (track_macro_exp_p)
+	it->location_ptr =
+	  next_arg_location_ptr (it->pfile,
+				 it->location_ptr);
+      break;
+    case MACRO_ARG_TOKEN_STRINGIFIED:
+#ifdef ENABLE_CHECKING
+      if (it->num_forwards > 0)
+	abort ();
+      it->num_forwards++;
+#endif
+      break;
+    }
+}
+
+/* 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
+  return *it->location_ptr;
+}
+
+/* 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+2)
+
+   It will be expanded into:
+
+       1+2-3
+
+   Let's consider the case of the token '2'.
+
+   Its index can be 1 (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.
+
+   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;
+  unsigned int exp_count;
+  const struct line_map *map = NULL;
+  bool track_macro_exp_p;
 
   /* 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 = 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;
+	exp_count += 2;
 
 	/* We have an argument.  If it is not being stringified or
 	   pasted it is macro-replaced before insertion.  */
@@ -974,67 +1415,189 @@ replace_args (cpp_reader *pfile, cpp_hashnode *node, cpp_macro *macro, macro_arg
 	  }
       }
 
+  /* 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 is basically what the buffer buff holds: a set of
+     cpp_token* and a location.
+
+     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_p = CPP_OPTION (pfile, track_macro_expansion);
+
   /* Now allocate space for the expansion, copy the tokens and replace
-     the arguments.  */
-  buff = _cpp_get_buff (pfile, total * sizeof (cpp_token *));
+     the arguments.  This memory must be freed when the context of
+     the macro MACRO is popped.  */
+  buff = tokens_buff_new (pfile, total);
+
   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. What 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_p)
+    {
+      int num_macro_tokens = total;
+      if (track_macro_exp_p < 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, macro,
+				 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;
+	  unsigned index = expanded_token_index (pfile, macro, src, i);
+	  tokens_buff_append_token (pfile, buff, src,
+				    src->src_loc, src->src_loc,
+				    map, &index);
+	  i += 1;
 	  continue;
 	}
 
       paste_flag = 0;
       arg = &args[src->val.macro_arg.arg_no - 1];
       if (src->flags & STRINGIFY_ARG)
-	count = 1, from = &arg->stringified;
+	{
+	  arg_tokens_count = 1;
+	  macro_arg_token_iter_init (&from, pfile,
+				     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, pfile,
+				     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, pfile,
+				     MACRO_ARG_TOKEN_NORMAL,
+				     arg, arg->first);
+
+	  num_toks = tokens_buff_count (pfile, buff);
+
+	  if (num_toks != 0)
 	    {
-	      if (dest[-1]->type == CPP_COMMA
+	      tmp_token_ptr = tokens_buff_last_token_ptr (pfile, 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--;
+		  if (macro_arg_token_iter_get_token (&from) == NULL)
+		    tokens_buff_remove_last_token (pfile, 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, pfile,
+				     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);
+	  tokens_buff_append_token (pfile, buff, t,
+				    t->src_loc, t->src_loc,
+				    map, &index);
+	}
 
-      if (count)
+      if (arg_tokens_count)
 	{
-	  memcpy (dest, from, count * sizeof (cpp_token *));
-	  dest += count;
+	  unsigned int j;
+	  for (j = 0; j < arg_tokens_count; ++j)
+	    {
+	      /* So if track_macro_exp_p 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 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_p > 1)
+		token_index += j;
+
+	      index = expanded_token_index (pfile, macro, src, token_index);
+	      tokens_buff_append_token (pfile, buff,
+					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 (pfile, buff);
 	}
       else if (CPP_PEDANTIC (pfile) && ! macro->syshdr
 	       && ! CPP_OPTION (pfile, c99)
@@ -1050,7 +1613,11 @@ 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_append_token (pfile, buff, t, t->src_loc,
+				    t->src_loc, NULL, NULL);
+	}
 
       /* Add a new paste flag, or remove an unwanted one.  */
       if (paste_flag)
@@ -1064,6 +1631,8 @@ replace_args (cpp_reader *pfile, cpp_hashnode *node, cpp_macro *macro, macro_arg
 	    token->flags = (*paste_flag)->flags & ~PASTE_LEFT;
 	  *paste_flag = token;
 	}
+
+      i += arg_tokens_count;
     }
 
   /* Free the expanded arguments.  */
@@ -1071,7 +1640,12 @@ replace_args (cpp_reader *pfile, cpp_hashnode *node, cpp_macro *macro, macro_arg
     if (args[i].expanded)
       free (args[i].expanded);
 
-  push_ptoken_context (pfile, node, buff, first, dest - first);
+  if (track_macro_exp_p)
+    push_extended_tokens_context (pfile, node, buff, first,
+				  tokens_buff_count (pfile, buff));
+  else
+    push_ptoken_context (pfile, node, buff, first,
+			 tokens_buff_count (pfile, buff));
 }
 
 /* Return a special padding token, with padding inherited from SOURCE.  */
@@ -1099,6 +1673,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;
@@ -1115,7 +1690,7 @@ push_ptoken_context (cpp_reader *pfile, cpp_hashnode *macro, _cpp_buff *buff,
 {
   cpp_context *context = next_context (pfile);
 
-  context->direct_p = false;
+  context->tokens_kind = TOKENS_KIND_INDIRECT;
   context->macro = macro;
   context->buff = buff;
   FIRST (context).ptoken = first;
@@ -1127,13 +1702,30 @@ 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->tokens_kind = TOKENS_KIND_DIRECT;
+   context->macro = macro;
+   context->buff = NULL;
+  FIRST (context).token = first;
+  LAST (context).token = first + count;
+}
+
+/* Push a list of tokens.  */
+static void
+push_extended_tokens_context (cpp_reader *pfile, cpp_hashnode *macro,
+			      _cpp_buff *token_buff,
+			      const cpp_token **first,
+			      unsigned int count)
+{
   cpp_context *context = next_context (pfile);
 
-  context->direct_p = true;
+  context->tokens_kind = TOKENS_KIND_EXTENDED;
+  context->buff = token_buff;
   context->macro = macro;
-  context->buff = NULL;
-  FIRST (context).token = first;
-  LAST (context).token = first + count;
+  FIRST (context).ptoken = first;
+  LAST (context).ptoken =
+    macro_token_ptr_at (pfile, first, count);
 }
 
 /* Push a traditional macro's replacement text.  */
@@ -1143,7 +1735,7 @@ _cpp_push_text_context (cpp_reader *pfile, cpp_hashnode *macro,
 {
   cpp_context *context = next_context (pfile);
 
-  context->direct_p = true;
+  context->tokens_kind = TOKENS_KIND_DIRECT;
   context->macro = macro;
   context->buff = NULL;
   CUR (context) = start;
@@ -1151,6 +1743,196 @@ _cpp_push_text_context (cpp_reader *pfile, cpp_hashnode *macro,
   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 the
+   -ftrack-macro-expansion flag is in effect the buffer holds a set of
+   cpp_ext_token. Otherwise it holds a set of cpp_token*.  */
+static _cpp_buff*
+tokens_buff_new (cpp_reader *pfile, size_t len)
+{
+  bool track_macro_exp_p = CPP_OPTION (pfile, track_macro_expansion);
+  size_t size =
+    track_macro_exp_p
+    ? len * sizeof (cpp_ext_token)
+    : len * sizeof (cpp_token *);
+
+  return _cpp_get_buff (pfile, size);
+}
+
+/* Returns the number of tokens contained in a token buffer. If the
+   -ftrack-macro-expansion flag is in effect the buffer holds a set of
+   cpp_ext_token. Otherwise it holds a set of cpp_token*.  */
+static size_t
+tokens_buff_count (cpp_reader *pfile, _cpp_buff *buff)
+{
+  bool track_macro_exp_p = CPP_OPTION (pfile, track_macro_expansion);
+  size_t size =
+    track_macro_exp_p
+    ? sizeof (cpp_ext_token)
+    : sizeof (cpp_token *);
+
+  return (BUFF_FRONT (buff) - buff->base) / size;
+}
+
+/* Return a pointer to the last token contained in the token buffer
+   BUFF. If the -ftrack-macro-expansion flag is in effect the buffer
+   holds a set of cpp_ext_token. Otherwise it holds a set of
+   cpp_token*.*/
+static const cpp_token **
+tokens_buff_last_token_ptr (cpp_reader *pfile, _cpp_buff *buff)
+{
+  bool track_macro_exp_p = CPP_OPTION (pfile, track_macro_expansion);
+
+  if (track_macro_exp_p)
+    return &((cpp_ext_token *) BUFF_FRONT (buff))[-1].token;
+  return &((const cpp_token **) BUFF_FRONT (buff))[-1];
+}
+
+/* Remove the last token contained in the token buffer BUFF. If the
+   -ftrack-macro-expansion flag is in effect the buffer holds a set of
+   cpp_ext_token. Otherwise it holds a set of cpp_token*.*/
+static void
+tokens_buff_remove_last_token (cpp_reader *pfile, _cpp_buff *buff)
+{
+  bool track_macro_exp_p = CPP_OPTION (pfile, track_macro_expansion);
+
+  if (BUFF_FRONT (buff) > buff->base)
+    {
+      if (track_macro_exp_p)
+	BUFF_FRONT (buff) =
+	  (unsigned char *) &((cpp_ext_token *) BUFF_FRONT (buff))[-1];
+      else
+	BUFF_FRONT (buff) =
+	  (unsigned char *) &((const cpp_token **) BUFF_FRONT (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. TOKEN is the token to
+   insert. DEF_LOC is the full 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 PARM_DEF_LOC can just be set to the same
+   value as 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.
+
+   If the -ftrack-macro-expansion flag is in effect the buffer holds a
+   set of cpp_ext_token. Otherwise it holds a set of cpp_token*.  */
+static inline const cpp_token **
+tokens_buff_put_token_to (cpp_reader *pfile,
+			  const cpp_token **dest,
+			  const cpp_token *token,
+			  source_location def_loc,
+			  source_location parm_def_loc,			  
+			  const struct line_map *map,
+			  unsigned int *macro_token_index)
+{
+  bool track_macro_exp_p = CPP_OPTION (pfile, track_macro_expansion);
+  source_location macro_loc = def_loc;
+  const cpp_token **result;
+
+  if (track_macro_exp_p)
+    {
+      cpp_ext_token *token_dest = (cpp_ext_token *) dest;
+      if (map)
+	macro_loc = linemap_add_macro_token (map, *macro_token_index,
+					     def_loc, parm_def_loc);
+      token_dest->token = token;
+      token_dest->location = macro_loc;
+      result =  &token_dest[1].token;
+    }
+  else
+    {
+      *dest = token;
+      result = &dest[1];
+    }
+  return result;
+}
+
+/* Appends a token to the end of the token buffer BUFFER.  TOKEN is
+   the token to append. DEF_LOC is the full 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 PARM_DEF_LOC can just be set to the
+   same value as 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.
+
+   If the -ftrack-macro-expansion flag is in effect the buffer holds a
+   set of cpp_ext_token. Otherwise it holds a set of cpp_token*.
+ */
+static const cpp_token **
+tokens_buff_append_token (cpp_reader *pfile,
+			  _cpp_buff *buffer,
+			  const cpp_token *token,
+			  source_location def_loc,
+			  source_location parm_def_loc,
+			  const struct line_map *map,
+			  unsigned int *macro_token_index)
+{
+  const cpp_token **result;
+
+  result =
+    tokens_buff_put_token_to (pfile,
+			      (const cpp_token **) BUFF_FRONT (buffer),
+			      token, def_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_args_mem (cpp_reader *pfile, macro_arg *arg, size_t capacity)
+{
+  bool track_macro_exp_p = CPP_OPTION (pfile, track_macro_expansion);
+
+  if (track_macro_exp_p)
+    arg->expanded = (const cpp_token **) XNEWVEC (cpp_ext_token, capacity);
+  else
+    arg->expanded = XNEWVEC (const cpp_token *, capacity);
+
+  arg->expanded_capacity = capacity;
+}
+
+/* If necessary, enlarge ARG->expanded to so that it can contain SIZE
+   more tokens.  */
+static void
+ensure_expanded_args_room (cpp_reader *pfile, macro_arg *arg, size_t size)
+{
+  bool track_macro_exp_p;
+
+  if (size <= arg->expanded_capacity)
+    return;
+
+  size *= 2;
+  track_macro_exp_p = CPP_OPTION (pfile, track_macro_expansion);
+
+  if (track_macro_exp_p)
+    arg->expanded = (const cpp_token **)
+	XRESIZEVEC (cpp_ext_token, arg->expanded, size);
+  else
+    arg->expanded =
+      XRESIZEVEC (const cpp_token *, arg->expanded, size);
+  arg->expanded_capacity = 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
@@ -1162,6 +1944,7 @@ expand_arg (cpp_reader *pfile, macro_arg *arg)
 {
   unsigned int capacity;
   bool saved_warn_trad;
+  bool track_macro_exp_p = CPP_OPTION (pfile, track_macro_expansion);
 
   if (arg->count == 0)
     return;
@@ -1172,26 +1955,30 @@ expand_arg (cpp_reader *pfile, macro_arg *arg)
 
   /* Loop, reading in the arguments.  */
   capacity = 256;
-  arg->expanded = XNEWVEC (const cpp_token *, capacity);
+  alloc_expanded_args_mem (pfile, arg, capacity);
+
+  if (track_macro_exp_p)
+    push_extended_tokens_context (pfile, NULL, NULL,
+				  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_args_room (pfile, arg, arg->expanded_count + 1);
 
-      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 (pfile, arg, token, location,
+		     arg->expanded_count, MACRO_ARG_TOKEN_EXPANDED);
+      arg->expanded_count++;
     }
 
   _cpp_pop_context (pfile);
@@ -1200,8 +1987,9 @@ 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)
 {
@@ -1211,14 +1999,85 @@ _cpp_pop_context (cpp_reader *pfile)
     context->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 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 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)
+    {
+      *token = ((cpp_ext_token *) FIRST (c).ptoken)->token;
+      *location = ((cpp_ext_token *) FIRST (c).ptoken)->location;
+      FIRST (c).ptoken = next_macro_token_ptr (pfile, FIRST (c).ptoken);
+    }
+  else
+    abort ();
+}
+
+/* In the traditionnal 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 traditionnal 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
@@ -1226,12 +2085,40 @@ _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".  This matters when a token results from macro
+   expansion -- the token's location will indicate where the macro is
+   defined (the spelling location of the token) but *LOC will be a
+   virtual location of the token. Virtual location means a location
+   that possibly encodes many types of locus at once. A virtual
+   location can encode the location of a token resulting from macro
+   expansion or not. If the token results from macro expansion its
+   virtual location encodes (at the same time):
+     - the spelling location of the token
+     - the locus of the macro expansion point
+     - the locus the point where the token got instantiated as part of
+       the macro expansion process.
+     (YES, IT ENCODES ALL THESE THREE AT THE SAME TIME! and maybe more.)
+
+   You can learn more about the different locuses encoded in a map by
+   reading the extensive comments of the line_map_macro and line_map
+   structs in line-map.h.  A virtual location, indeed.
+
+   The linemap API can then be used to retrieve the particular locus
+   we are interested in.
+
+   Otherwise *LOC is set to the same location as the location carried
+   by the returned token.  */
+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 (;;)
@@ -1241,20 +2128,23 @@ 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);
+	      if (location)
+		*location = result->src_loc;
+	      return result;
 	    }
 	}
       else
@@ -1262,6 +2152,8 @@ cpp_get_token (cpp_reader *pfile)
 	  _cpp_pop_context (pfile);
 	  if (pfile->state.in_directive)
 	    continue;
+	  if (location)
+	    *location = pfile->avoid_paste.src_loc;
 	  return &pfile->avoid_paste;
 	}
 
@@ -1299,7 +2191,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
@@ -1316,12 +2209,16 @@ 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);
+	      if (location)
+		*location = result->src_loc;
+	      return result;
 	    }
 	}
       else
@@ -1338,27 +2235,74 @@ cpp_get_token (cpp_reader *pfile)
       break;
     }
 
-  return result;
+  if (location)
+    *location = virt_loc;
+  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 enbled its virtual location encodes (at the
+   same time):
+
+   - the spelling location of the token the locus of the macro
+   - expansion point the locus 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.
+
+   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);
+  result = cpp_get_token_1 (pfile, loc);
   if (pfile->context->macro)
-    *loc = pfile->invocation_location;
+    {
+      if (!CPP_OPTION (pfile, track_macro_expansion))
+	*loc = pfile->invocation_location;
+    }
   else
     *loc = result->src_loc;
 
+  *loc = maybe_adjust_loc_for_trad_cpp (pfile, *loc);
   return result;
 }
 
@@ -1425,10 +2369,15 @@ _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 =
+	  prev_macro_token_ptr (pfile, FIRST (pfile->context).ptoken);
+      else
+	abort ();
     }
 }
 
-- 
        Dodji

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

* [PATCH 6/6] Kill pedantic warnings on system headers macros
  2010-12-10 11:27 [PATCH 0/6] Tracking locations of tokens resulting from macro expansion Dodji Seketeli
                   ` (5 preceding siblings ...)
  2010-12-10 12:27 ` [PATCH 2/6] Generate virtual locations for tokens Dodji Seketeli
@ 2010-12-10 12:33 ` Dodji Seketeli
  2010-12-10 12:52 ` [PATCH 0/6] Tracking locations of tokens resulting from macro expansion Gabriel Dos Reis
                   ` (2 subsequent siblings)
  9 siblings, 0 replies; 135+ messages in thread
From: Dodji Seketeli @ 2010-12-10 12:33 UTC (permalink / raw)
  To: gcc-patches; +Cc: tromey, joseph, gdr, lopezibanez

This patch leverages the virtual location infrastructure to avoid
emitting pedantic warnings related to macros defined in system headers
but expanded in normal TUs.

The point is to make diagnostic routines use virtual locations of
tokens instead of their spelling locations. The diagnostic routines in
turn indirectly use linemap_location_in_system_header_p to know if a
given virtual location originated from a system header.

The patch has two main parts.

The libcpp part makes diagnostic routines called from the preprocessor
expression parsing and number conversion code use virtual
locations.

The C FE part makes diagnostic routines called from the type
specifiers validation code use virtual locations.

This fixes the relevant examples presented in the comments of the bug
but I guess, as usual, libcpp and the FEs will need on-going care to
use more and more virtual locations of tokens instead of spelling
locations.

The combination of the patch and the previous ones boostrapped with
--enable-languages=all,ada and passed regression tests on
x86_64-unknown-linux-gnu.

libcpp/

	* include/cpplib.h (cpp_classify_number): Add a location parameter
	to the declaration.
	* internal.h (_cpp_get_prev_token_spelling_loc): Declare.
	* lex.c (_cpp_get_prev_token_spelling_loc): Factorize this from ...
	* errors.c (cpp_diagnostic): ... here.
	* expr.c (SYNTAX_ERROR_AT, SYNTAX_ERROR2_AT): New macros to emit
	syntax error using a virtual location.
	(cpp_classify_number): Add a virtual location parameter. Use
	SYNTAX_ERROR_AT instead of SYNTAX_ERROR, cpp_error_with_line
	instead of cpp_error and cpp_warning_with_line instead of
	cpp_warning. Pass the new virtual location parameter to those
	diagnostic routines.
	(eval_token): Add a virtual location parameter. Pass it down to
	cpp_classify_number. Use cpp_error_with_line instead of cpp_error,
	cpp_warning_with_line instead of cpp_warning, and pass the new
	virtual location parameter to these.
	(_cpp_parse_expr): Use cpp_get_token_with_location instead of
	cpp_get_token, to get the virtual location of the token. Use
	SYNTAX_ERROR2_AT instead of SYNTAX_ERROR2, cpp_error_with_line
	instead of cpp_error. Use the virtual location instead of the
	spelling location.
	* macro.c (maybe_adjust_loc_for_trad_cpp): Define new static
	function.
	(cpp_get_token_with_location): Use it.

gcc/c-family

	* c-lex.c (c_lex_with_flags): Adjust to pass the virtual location
	to cpp_classify_number.

gcc/

	* c-tree.h (finish_declspecs): Add a virtual location parameter.
	* c-decl.c (finish_declspecs): Add a virtual location
	parameter. Use error_at instead of error and pass down the virtual
	location to pewarn and error_at.
	(declspecs_add_type): Use in_system_header_at instead of
	in_system_header.
	* c-parser.c (c_parser_declaration_or_fndef): Pass virtual
	location of the relevant token to finish_declspecs.
	(c_parser_struct_declaration, c_parser_parameter_declaration):
	Likewise.
	(c_parser_type_name): Likewise.

gcc/testsuite/

	* gcc.dg/cpp/syshdr3.h: New test header.
	* gcc.dg/cpp/syshdr3.c: New test file.
	* gcc.dg/nofixed-point-2.c: Adjust to more precise location.
---
 gcc/c-decl.c                           |   17 ++--
 gcc/c-family/c-lex.c                   |    4 +-
 gcc/c-parser.c                         |   12 ++-
 gcc/c-tree.h                           |    2 +-
 gcc/testsuite/gcc.dg/cpp/syshdr3.c     |   16 +++
 gcc/testsuite/gcc.dg/cpp/syshdr3.h     |    7 ++
 gcc/testsuite/gcc.dg/nofixed-point-2.c |    6 +-
 libcpp/errors.c                        |   21 +----
 libcpp/expr.c                          |  176 +++++++++++++++++++-------------
 libcpp/include/cpplib.h                |    3 +-
 libcpp/internal.h                      |    1 +
 libcpp/lex.c                           |   31 ++++++
 12 files changed, 185 insertions(+), 111 deletions(-)
 create mode 100644 gcc/testsuite/gcc.dg/cpp/syshdr3.c
 create mode 100644 gcc/testsuite/gcc.dg/cpp/syshdr3.h

diff --git a/gcc/c-decl.c b/gcc/c-decl.c
index 06e01ad..04b9973 100644
--- a/gcc/c-decl.c
+++ b/gcc/c-decl.c
@@ -8915,7 +8915,7 @@ declspecs_add_type (location_t loc, struct c_declspecs *specs,
 	      break;
 	    case RID_COMPLEX:
 	      dupe = specs->complex_p;
-	      if (!flag_isoc99 && !in_system_header)
+	      if (!flag_isoc99 && !in_system_header_at (loc))
 		pedwarn (loc, OPT_pedantic,
 			 "ISO C90 does not support complex types");
 	      if (specs->typespec_word == cts_void)
@@ -9443,7 +9443,8 @@ declspecs_add_attrs (struct c_declspecs *specs, tree attrs)
    double".  */
 
 struct c_declspecs *
-finish_declspecs (struct c_declspecs *specs)
+finish_declspecs (struct c_declspecs *specs,
+		  location_t where)
 {
   /* If a type was specified as a whole, we have no modifiers and are
      done.  */
@@ -9464,9 +9465,9 @@ finish_declspecs (struct c_declspecs *specs)
     {
       if (specs->saturating_p)
 	{
-	  error ("%<_Sat%> is used without %<_Fract%> or %<_Accum%>");
+	  error_at (where, "%<_Sat%> is used without %<_Fract%> or %<_Accum%>");
 	  if (!targetm.fixed_point_supported_p ())
-	    error ("fixed-point types not supported for this target");
+	    error_at (where, "fixed-point types not supported for this target");
 	  specs->typespec_word = cts_fract;
 	}
       else if (specs->long_p || specs->short_p
@@ -9477,7 +9478,7 @@ finish_declspecs (struct c_declspecs *specs)
       else if (specs->complex_p)
 	{
 	  specs->typespec_word = cts_double;
-	  pedwarn (input_location, OPT_pedantic,
+	  pedwarn (where, OPT_pedantic,
 		   "ISO C does not support plain %<complex%> meaning "
 		   "%<double complex%>");
 	}
@@ -9522,7 +9523,7 @@ finish_declspecs (struct c_declspecs *specs)
 	specs->type = char_type_node;
       if (specs->complex_p)
 	{
-	  pedwarn (input_location, OPT_pedantic,
+	  pedwarn (where, OPT_pedantic,
 		   "ISO C does not support complex integer types");
 	  specs->type = build_complex_type (specs->type);
 	}
@@ -9535,7 +9536,7 @@ finish_declspecs (struct c_declspecs *specs)
 		     : int128_integer_type_node);
       if (specs->complex_p)
 	{
-	  pedwarn (input_location, OPT_pedantic,
+	  pedwarn (where, OPT_pedantic,
 		   "ISO C does not support complex integer types");
 	  specs->type = build_complex_type (specs->type);
 	}
@@ -9561,7 +9562,7 @@ finish_declspecs (struct c_declspecs *specs)
 		       : integer_type_node);
       if (specs->complex_p)
 	{
-	  pedwarn (input_location, OPT_pedantic,
+	  pedwarn (where, OPT_pedantic,
 		   "ISO C does not support complex integer types");
 	  specs->type = build_complex_type (specs->type);
 	}
diff --git a/gcc/c-family/c-lex.c b/gcc/c-family/c-lex.c
index 085ecd4..4d1988d 100644
--- a/gcc/c-family/c-lex.c
+++ b/gcc/c-family/c-lex.c
@@ -314,7 +314,7 @@ c_lex_with_flags (tree *value, location_t *loc, unsigned char *cpp_flags,
 
     case CPP_NUMBER:
       {
-	unsigned int flags = cpp_classify_number (parse_in, tok);
+	unsigned int flags = cpp_classify_number (parse_in, tok, *loc);
 
 	switch (flags & CPP_N_CATEGORY)
 	  {
@@ -397,7 +397,7 @@ c_lex_with_flags (tree *value, location_t *loc, unsigned char *cpp_flags,
 
 	*cpp_spell_token (parse_in, tok, name, true) = 0;
 
-	error ("stray %qs in program", name);
+	error_at (*loc, "stray %qs in program", name);
       }
 
       goto retry;
diff --git a/gcc/c-parser.c b/gcc/c-parser.c
index f2d5e5b..5ac740c 100644
--- a/gcc/c-parser.c
+++ b/gcc/c-parser.c
@@ -1437,7 +1437,7 @@ c_parser_declaration_or_fndef (c_parser *parser, bool fndef_ok,
       c_parser_skip_to_end_of_block_or_statement (parser);
       return;
     }
-  finish_declspecs (specs);
+  finish_declspecs (specs, here);
   if (c_parser_next_token_is (parser, CPP_SEMICOLON))
     {
       if (empty_ok)
@@ -2506,7 +2506,7 @@ c_parser_struct_declaration (c_parser *parser)
       c_parser_error (parser, "expected specifier-qualifier-list");
       return NULL_TREE;
     }
-  finish_declspecs (specs);
+  finish_declspecs (specs, decl_loc);
   if (c_parser_next_token_is (parser, CPP_SEMICOLON)
       || c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
     {
@@ -3172,6 +3172,8 @@ c_parser_parameter_declaration (c_parser *parser, tree attrs)
   tree prefix_attrs;
   tree postfix_attrs = NULL_TREE;
   bool dummy = false;
+  location_t here = c_parser_peek_token (parser)->location;
+
   if (!c_parser_next_token_starts_declspecs (parser))
     {
       c_token *token = c_parser_peek_token (parser);
@@ -3200,7 +3202,7 @@ c_parser_parameter_declaration (c_parser *parser, tree attrs)
       attrs = NULL_TREE;
     }
   c_parser_declspecs (parser, specs, true, true, true);
-  finish_declspecs (specs);
+  finish_declspecs (specs, here);
   pending_xref_error ();
   prefix_attrs = specs->attrs;
   specs->attrs = NULL_TREE;
@@ -3489,6 +3491,8 @@ c_parser_type_name (c_parser *parser)
   struct c_declarator *declarator;
   struct c_type_name *ret;
   bool dummy = false;
+  location_t here = c_parser_peek_token (parser)->location;
+
   c_parser_declspecs (parser, specs, false, true, true);
   if (!specs->declspecs_seen_p)
     {
@@ -3496,7 +3500,7 @@ c_parser_type_name (c_parser *parser)
       return NULL;
     }
   pending_xref_error ();
-  finish_declspecs (specs);
+  finish_declspecs (specs, here);
   declarator = c_parser_declarator (parser,
 				    specs->typespec_kind != ctsk_none,
 				    C_DTR_ABSTRACT, &dummy);
diff --git a/gcc/c-tree.h b/gcc/c-tree.h
index 7bf3bc0..a8d16c6 100644
--- a/gcc/c-tree.h
+++ b/gcc/c-tree.h
@@ -491,7 +491,7 @@ extern struct c_declspecs *declspecs_add_scspec (struct c_declspecs *, tree);
 extern struct c_declspecs *declspecs_add_attrs (struct c_declspecs *, tree);
 extern struct c_declspecs *declspecs_add_addrspace (struct c_declspecs *,
 						    addr_space_t);
-extern struct c_declspecs *finish_declspecs (struct c_declspecs *);
+extern struct c_declspecs *finish_declspecs (struct c_declspecs *, location_t);
 
 /* in c-objc-common.c */
 extern bool c_objc_common_init (void);
diff --git a/gcc/testsuite/gcc.dg/cpp/syshdr3.c b/gcc/testsuite/gcc.dg/cpp/syshdr3.c
new file mode 100644
index 0000000..8850410
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/cpp/syshdr3.c
@@ -0,0 +1,16 @@
+/* Contributed by Dodji Seketeli <dodji@redhat.com> */
+/* Origin: PR preprocessor/7263 */
+/* { dg-options "-pedantic -std=c89 -ftrack-macro-expansion=1" } */
+/* { dg-do compile } */
+
+/* This tests the proprer suppression of warning coming from macro
+   defined in system headers and expanded in a non-system header
+   location.  */
+#include "syshdr3.h"
+
+_Complex c = _Complex_I + _Complex_I; /* These macros are defined in
+					 system header so we should
+					 have no warning here.  */
+U_LL u = ONE_ULL; /* Likewise here.  */
+
+unsigned long long v = 1ULL; /* { dg-warning "long long" } */
diff --git a/gcc/testsuite/gcc.dg/cpp/syshdr3.h b/gcc/testsuite/gcc.dg/cpp/syshdr3.h
new file mode 100644
index 0000000..e5d502a
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/cpp/syshdr3.h
@@ -0,0 +1,7 @@
+#pragma GCC system_header
+
+#define _Complex __complex__
+#define _Complex_I 1.0iF
+
+#define U_LL unsigned long long
+#define ONE_ULL 1ULL
diff --git a/gcc/testsuite/gcc.dg/nofixed-point-2.c b/gcc/testsuite/gcc.dg/nofixed-point-2.c
index 5b2f209..8442a19 100644
--- a/gcc/testsuite/gcc.dg/nofixed-point-2.c
+++ b/gcc/testsuite/gcc.dg/nofixed-point-2.c
@@ -20,10 +20,10 @@ f3 (void)
   return 0k;			/* { dg-error "not supported" "reject fixed-point" } */
 }
 
-_Sat
-f4 (void)			/* { dg-error "not supported" "reject fixed-point" } */
+_Sat                            /* { dg-error "not supported" "reject fixed-point" } */
+f4 (void)
 {
   return 0k;			/* { dg-error "not supported" "reject fixed-point" } */
 }
 
-/* { dg-error "is used without" "" { target *-*-* } 24 } */
+/* { dg-error "is used without" "" { target *-*-* } 23 } */
diff --git a/libcpp/errors.c b/libcpp/errors.c
index 821e4ed..30afca2 100644
--- a/libcpp/errors.c
+++ b/libcpp/errors.c
@@ -38,26 +38,7 @@ cpp_diagnostic (cpp_reader * pfile, int level, int reason,
   source_location src_loc;
   bool ret;
 
-  if (CPP_OPTION (pfile, traditional))
-    {
-      if (pfile->state.in_directive)
-	src_loc = pfile->directive_line;
-      else
-	src_loc = LINEMAPS_ORDINARY_HIGHEST_LINE (pfile->line_table);
-    }
-  /* We don't want to refer to a token before the beginning of the
-     current run -- that is invalid.  */
-  else if (pfile->cur_token == pfile->cur_run->base)
-    {
-      if (pfile->cur_run->prev != NULL)
-	src_loc = pfile->cur_run->prev->limit->src_loc;
-      else
-	src_loc = 0;
-    }
-  else
-    {
-      src_loc = pfile->cur_token[-1].src_loc;
-    }
+  src_loc = _cpp_get_prev_token_spelling_loc (pfile);
 
   if (!pfile->cb.error)
     abort ();
diff --git a/libcpp/expr.c b/libcpp/expr.c
index d2fec2a..932b758 100644
--- a/libcpp/expr.c
+++ b/libcpp/expr.c
@@ -59,7 +59,7 @@ static cpp_num num_rshift (cpp_num, size_t, size_t);
 
 static cpp_num append_digit (cpp_num, int, int, size_t);
 static cpp_num parse_defined (cpp_reader *);
-static cpp_num eval_token (cpp_reader *, const cpp_token *);
+static cpp_num eval_token (cpp_reader *, const cpp_token *, source_location);
 static struct op *reduce (cpp_reader *, struct op *, enum cpp_ttype);
 static unsigned int interpret_float_suffix (const uchar *, size_t);
 static unsigned int interpret_int_suffix (const uchar *, size_t);
@@ -76,6 +76,12 @@ static void check_promotion (cpp_reader *, const struct op *);
 #define SYNTAX_ERROR2(msgid, arg) \
   do { cpp_error (pfile, CPP_DL_ERROR, msgid, arg); goto syntax_error; } \
   while(0)
+#define SYNTAX_ERROR_AT(loc, msgid) \
+  do { cpp_error_with_line (pfile, CPP_DL_ERROR, (loc), 0, msgid); goto syntax_error; } \
+  while(0)
+#define SYNTAX_ERROR2_AT(loc, msgid, arg)					\
+  do { cpp_error_with_line (pfile, CPP_DL_ERROR, (loc), 0, msgid, arg); goto syntax_error; } \
+  while(0)
 
 /* Subroutine of cpp_classify_number.  S points to a float suffix of
    length LEN, possibly zero.  Returns 0 for an invalid suffix, or a
@@ -223,7 +229,8 @@ interpret_int_suffix (const uchar *s, size_t len)
    floating point, or invalid), radix (decimal, octal, hexadecimal),
    and type suffixes.  */
 unsigned int
-cpp_classify_number (cpp_reader *pfile, const cpp_token *token)
+cpp_classify_number (cpp_reader *pfile, const cpp_token *token,
+		     source_location virtual_location)
 {
   const uchar *str = token->val.str.text;
   const uchar *limit;
@@ -279,7 +286,8 @@ cpp_classify_number (cpp_reader *pfile, const cpp_token *token)
 	  if (float_flag == NOT_FLOAT)
 	    float_flag = AFTER_POINT;
 	  else
-	    SYNTAX_ERROR ("too many decimal points in number");
+	    SYNTAX_ERROR_AT (virtual_location,
+			     "too many decimal points in number");
 	}
       else if ((radix <= 10 && (c == 'e' || c == 'E'))
 	       || (radix == 16 && (c == 'p' || c == 'P')))
@@ -307,8 +315,8 @@ cpp_classify_number (cpp_reader *pfile, const cpp_token *token)
 	    radix = 10;
 
 	  if (CPP_PEDANTIC (pfile))
-	    cpp_error (pfile, CPP_DL_PEDWARN,
-		       "fixed-point constants are a GCC extension");
+	    cpp_error_with_line (pfile, CPP_DL_PEDWARN, virtual_location, 0,
+				 "fixed-point constants are a GCC extension");
 	  goto syntax_ok;
 	}
       else
@@ -321,26 +329,29 @@ cpp_classify_number (cpp_reader *pfile, const cpp_token *token)
   if (max_digit >= radix)
     {
       if (radix == 2)
-	SYNTAX_ERROR2 ("invalid digit \"%c\" in binary constant", '0' + max_digit);
+	SYNTAX_ERROR2_AT (virtual_location,
+			  "invalid digit \"%c\" in binary constant", '0' + max_digit);
       else
-	SYNTAX_ERROR2 ("invalid digit \"%c\" in octal constant", '0' + max_digit);
+	SYNTAX_ERROR2_AT (virtual_location,
+			  "invalid digit \"%c\" in octal constant", '0' + max_digit);
     }
 
   if (float_flag != NOT_FLOAT)
     {
       if (radix == 2)
 	{
-	  cpp_error (pfile, CPP_DL_ERROR,
-		     "invalid prefix \"0b\" for floating constant");
+	  cpp_error_with_line (pfile, CPP_DL_ERROR, virtual_location, 0,
+			       "invalid prefix \"0b\" for floating constant");
 	  return CPP_N_INVALID;
 	}
 
       if (radix == 16 && !seen_digit)
-	SYNTAX_ERROR ("no digits in hexadecimal floating constant");
+	SYNTAX_ERROR_AT (virtual_location,
+			 "no digits in hexadecimal floating constant");
 
       if (radix == 16 && CPP_PEDANTIC (pfile) && !CPP_OPTION (pfile, c99))
-	cpp_error (pfile, CPP_DL_PEDWARN,
-		   "use of C99 hexadecimal floating constant");
+	cpp_error_with_line (pfile, CPP_DL_PEDWARN, virtual_location, 0,
+			     "use of C99 hexadecimal floating constant");
 
       if (float_flag == AFTER_EXPON)
 	{
@@ -349,21 +360,22 @@ cpp_classify_number (cpp_reader *pfile, const cpp_token *token)
 
 	  /* Exponent is decimal, even if string is a hex float.  */
 	  if (!ISDIGIT (*str))
-	    SYNTAX_ERROR ("exponent has no digits");
+	    SYNTAX_ERROR_AT (virtual_location, "exponent has no digits");
 
 	  do
 	    str++;
 	  while (ISDIGIT (*str));
 	}
       else if (radix == 16)
-	SYNTAX_ERROR ("hexadecimal floating constants require an exponent");
+	SYNTAX_ERROR_AT (virtual_location,
+			 "hexadecimal floating constants require an exponent");
 
       result = interpret_float_suffix (str, limit - str);
       if (result == 0)
 	{
-	  cpp_error (pfile, CPP_DL_ERROR,
-		     "invalid suffix \"%.*s\" on floating constant",
-		     (int) (limit - str), str);
+	  cpp_error_with_line (pfile, CPP_DL_ERROR, virtual_location, 0,
+			       "invalid suffix \"%.*s\" on floating constant",
+			       (int) (limit - str), str);
 	  return CPP_N_INVALID;
 	}
 
@@ -371,33 +383,33 @@ cpp_classify_number (cpp_reader *pfile, const cpp_token *token)
       if (limit != str
 	  && CPP_WTRADITIONAL (pfile)
 	  && ! cpp_sys_macro_p (pfile))
-	cpp_warning (pfile, CPP_W_TRADITIONAL,
-		     "traditional C rejects the \"%.*s\" suffix",
-		     (int) (limit - str), str);
+	cpp_warning_with_line (pfile, CPP_W_TRADITIONAL, virtual_location, 0,
+			       "traditional C rejects the \"%.*s\" suffix",
+			       (int) (limit - str), str);
 
       /* A suffix for double is a GCC extension via decimal float support.
 	 If the suffix also specifies an imaginary value we'll catch that
 	 later.  */
       if ((result == CPP_N_MEDIUM) && CPP_PEDANTIC (pfile))
-	cpp_error (pfile, CPP_DL_PEDWARN,
-		   "suffix for double constant is a GCC extension");
+	cpp_error_with_line (pfile, CPP_DL_PEDWARN, virtual_location, 0,
+			     "suffix for double constant is a GCC extension");
 
       /* Radix must be 10 for decimal floats.  */
       if ((result & CPP_N_DFLOAT) && radix != 10)
         {
-          cpp_error (pfile, CPP_DL_ERROR,
-                     "invalid suffix \"%.*s\" with hexadecimal floating constant",
-                     (int) (limit - str), str);
+          cpp_error_with_line (pfile, CPP_DL_ERROR, virtual_location, 0,
+			       "invalid suffix \"%.*s\" with hexadecimal floating constant",
+			       (int) (limit - str), str);
           return CPP_N_INVALID;
         }
 
       if ((result & (CPP_N_FRACT | CPP_N_ACCUM)) && CPP_PEDANTIC (pfile))
-	cpp_error (pfile, CPP_DL_PEDWARN,
-		   "fixed-point constants are a GCC extension");
+	cpp_error_with_line (pfile, CPP_DL_PEDWARN, virtual_location, 0,
+			     "fixed-point constants are a GCC extension");
 
       if ((result & CPP_N_DFLOAT) && CPP_PEDANTIC (pfile))
-	cpp_error (pfile, CPP_DL_PEDWARN,
-		   "decimal float constants are a GCC extension");
+	cpp_error_with_line (pfile, CPP_DL_PEDWARN, virtual_location, 0,
+			     "decimal float constants are a GCC extension");
 
       result |= CPP_N_FLOATING;
     }
@@ -406,9 +418,9 @@ cpp_classify_number (cpp_reader *pfile, const cpp_token *token)
       result = interpret_int_suffix (str, limit - str);
       if (result == 0)
 	{
-	  cpp_error (pfile, CPP_DL_ERROR,
-		     "invalid suffix \"%.*s\" on integer constant",
-		     (int) (limit - str), str);
+	  cpp_error_with_line (pfile, CPP_DL_ERROR, virtual_location, 0,
+			       "invalid suffix \"%.*s\" on integer constant",
+			       (int) (limit - str), str);
 	  return CPP_N_INVALID;
 	}
 
@@ -421,9 +433,10 @@ cpp_classify_number (cpp_reader *pfile, const cpp_token *token)
 		       && CPP_OPTION (pfile, cpp_warn_long_long);
 
 	  if (u_or_i || large)
-	    cpp_warning (pfile, large ? CPP_W_LONG_LONG : CPP_W_TRADITIONAL,
-		         "traditional C rejects the \"%.*s\" suffix",
-		         (int) (limit - str), str);
+	    cpp_warning_with_line (pfile, large ? CPP_W_LONG_LONG : CPP_W_TRADITIONAL,
+				   virtual_location, 0,
+				   "traditional C rejects the \"%.*s\" suffix",
+				   (int) (limit - str), str);
 	}
 
       if ((result & CPP_N_WIDTH) == CPP_N_LARGE
@@ -434,9 +447,11 @@ cpp_classify_number (cpp_reader *pfile, const cpp_token *token)
 		                : N_("use of C99 long long integer constant");
 
 	  if (CPP_OPTION (pfile, c99))
-            cpp_warning (pfile, CPP_W_LONG_LONG, message);
+            cpp_warning_with_line (pfile, CPP_W_LONG_LONG, virtual_location,
+				   0, message);
           else
-            cpp_pedwarning (pfile, CPP_W_LONG_LONG, message);
+            cpp_pedwarning_with_line (pfile, CPP_W_LONG_LONG,
+				      virtual_location, 0, message);
         }
 
       result |= CPP_N_INTEGER;
@@ -444,11 +459,11 @@ cpp_classify_number (cpp_reader *pfile, const cpp_token *token)
 
  syntax_ok:
   if ((result & CPP_N_IMAGINARY) && CPP_PEDANTIC (pfile))
-    cpp_error (pfile, CPP_DL_PEDWARN,
-	       "imaginary constants are a GCC extension");
+    cpp_error_with_line (pfile, CPP_DL_PEDWARN, virtual_location, 0,
+			 "imaginary constants are a GCC extension");
   if (radix == 2 && CPP_PEDANTIC (pfile))
-    cpp_error (pfile, CPP_DL_PEDWARN,
-	       "binary constants are a GCC extension");
+    cpp_error_with_line (pfile, CPP_DL_PEDWARN, virtual_location, 0,
+			 "binary constants are a GCC extension");
 
   if (radix == 10)
     result |= CPP_N_DECIMAL;
@@ -731,7 +746,8 @@ parse_defined (cpp_reader *pfile)
    number or character constant, or the result of the "defined" or "#"
    operators).  */
 static cpp_num
-eval_token (cpp_reader *pfile, const cpp_token *token)
+eval_token (cpp_reader *pfile, const cpp_token *token,
+	    source_location virtual_location)
 {
   cpp_num result;
   unsigned int temp;
@@ -743,18 +759,18 @@ eval_token (cpp_reader *pfile, const cpp_token *token)
   switch (token->type)
     {
     case CPP_NUMBER:
-      temp = cpp_classify_number (pfile, token);
+      temp = cpp_classify_number (pfile, token, virtual_location);
       switch (temp & CPP_N_CATEGORY)
 	{
 	case CPP_N_FLOATING:
-	  cpp_error (pfile, CPP_DL_ERROR,
-		     "floating constant in preprocessor expression");
+	  cpp_error_with_line (pfile, CPP_DL_ERROR, virtual_location, 0,
+			       "floating constant in preprocessor expression");
 	  break;
 	case CPP_N_INTEGER:
 	  if (!(temp & CPP_N_IMAGINARY))
 	    return cpp_interpret_integer (pfile, token, temp);
-	  cpp_error (pfile, CPP_DL_ERROR,
-		     "imaginary number in preprocessor expression");
+	  cpp_error_with_line (pfile, CPP_DL_ERROR, virtual_location, 0,
+			       "imaginary number in preprocessor expression");
 	  break;
 
 	case CPP_N_INVALID:
@@ -801,8 +817,9 @@ eval_token (cpp_reader *pfile, const cpp_token *token)
 	  result.high = 0;
 	  result.low = 0;
 	  if (CPP_OPTION (pfile, warn_undef) && !pfile->state.skip_eval)
-	    cpp_warning (pfile, CPP_W_UNDEF, "\"%s\" is not defined",
-		         NODE_NAME (token->val.node.node));
+	    cpp_warning_with_line (pfile, CPP_W_UNDEF, virtual_location, 0,
+				   "\"%s\" is not defined",
+				   NODE_NAME (token->val.node.node));
 	}
       break;
 
@@ -812,11 +829,12 @@ eval_token (cpp_reader *pfile, const cpp_token *token)
 	  /* A pedantic warning takes precedence over a deprecated
 	     warning here.  */
 	  if (CPP_PEDANTIC (pfile))
-	    cpp_error (pfile, CPP_DL_PEDWARN,
-		       "assertions are a GCC extension");
+	    cpp_error_with_line (pfile, CPP_DL_PEDWARN,
+				 virtual_location, 0,
+				 "assertions are a GCC extension");
 	  else if (CPP_OPTION (pfile, cpp_warn_deprecated))
-	    cpp_warning (pfile, CPP_W_DEPRECATED,
-		         "assertions are a deprecated extension");
+	    cpp_warning_with_line (pfile, CPP_W_DEPRECATED, virtual_location, 0,
+				   "assertions are a deprecated extension");
 	}
       _cpp_test_assertion (pfile, &temp);
       result.high = 0;
@@ -918,6 +936,8 @@ _cpp_parse_expr (cpp_reader *pfile, bool is_if)
   struct op *top = pfile->op_stack;
   unsigned int lex_count;
   bool saw_leading_not, want_value = true;
+  source_location virtual_location = 0,
+    prev_virtual_location = _cpp_get_prev_token_spelling_loc (pfile);
 
   pfile->state.skip_eval = 0;
 
@@ -934,9 +954,12 @@ _cpp_parse_expr (cpp_reader *pfile, bool is_if)
       struct op op;
 
       lex_count++;
-      op.token = cpp_get_token (pfile);
+
+      if (virtual_location)
+	prev_virtual_location = virtual_location;
+      op.token = cpp_get_token_with_location (pfile, &virtual_location);
       op.op = op.token->type;
-      op.loc = op.token->src_loc;
+      op.loc = virtual_location;
 
       switch (op.op)
 	{
@@ -949,10 +972,11 @@ _cpp_parse_expr (cpp_reader *pfile, bool is_if)
 	case CPP_NAME:
 	case CPP_HASH:
 	  if (!want_value)
-	    SYNTAX_ERROR2 ("missing binary operator before token \"%s\"",
-			   cpp_token_as_text (pfile, op.token));
+	    SYNTAX_ERROR2_AT (prev_virtual_location,
+			      "missing binary operator before token \"%s\"",
+			      cpp_token_as_text (pfile, op.token));
 	  want_value = false;
-	  top->value = eval_token (pfile, op.token);
+	  top->value = eval_token (pfile, op.token, virtual_location);
 	  continue;
 
 	case CPP_NOT:
@@ -969,8 +993,9 @@ _cpp_parse_expr (cpp_reader *pfile, bool is_if)
 
 	default:
 	  if ((int) op.op <= (int) CPP_EQ || (int) op.op >= (int) CPP_PLUS_EQ)
-	    SYNTAX_ERROR2 ("token \"%s\" is not valid in preprocessor expressions",
-			   cpp_token_as_text (pfile, op.token));
+	    SYNTAX_ERROR2_AT (op.loc,
+			      "token \"%s\" is not valid in preprocessor expressions",
+			      cpp_token_as_text (pfile, op.token));
 	  break;
 	}
 
@@ -978,27 +1003,32 @@ _cpp_parse_expr (cpp_reader *pfile, bool is_if)
       if (optab[op.op].flags & NO_L_OPERAND)
 	{
 	  if (!want_value)
-	    SYNTAX_ERROR2 ("missing binary operator before token \"%s\"",
-			   cpp_token_as_text (pfile, op.token));
+	    SYNTAX_ERROR2_AT (prev_virtual_location,
+			      "missing binary operator before token \"%s\"",
+			      cpp_token_as_text (pfile, op.token));
 	}
       else if (want_value)
 	{
 	  /* We want a number (or expression) and haven't got one.
 	     Try to emit a specific diagnostic.  */
 	  if (op.op == CPP_CLOSE_PAREN && top->op == CPP_OPEN_PAREN)
-	    SYNTAX_ERROR ("missing expression between '(' and ')'");
+	    SYNTAX_ERROR_AT (op.loc,
+			     "missing expression between '(' and ')'");
 
 	  if (op.op == CPP_EOF && top->op == CPP_EOF)
- 	    SYNTAX_ERROR2 ("%s with no expression", is_if ? "#if" : "#elif");
+ 	    SYNTAX_ERROR2_AT (op.loc,
+			      "%s with no expression", is_if ? "#if" : "#elif");
 
  	  if (top->op != CPP_EOF && top->op != CPP_OPEN_PAREN)
- 	    SYNTAX_ERROR2 ("operator '%s' has no right operand",
- 			   cpp_token_as_text (pfile, top->token));
+ 	    SYNTAX_ERROR2_AT (op.loc,
+			      "operator '%s' has no right operand",
+			      cpp_token_as_text (pfile, top->token));
 	  else if (op.op == CPP_CLOSE_PAREN || op.op == CPP_EOF)
 	    /* Complain about missing paren during reduction.  */;
 	  else
-	    SYNTAX_ERROR2 ("operator '%s' has no left operand",
-			   cpp_token_as_text (pfile, op.token));
+	    SYNTAX_ERROR2_AT (op.loc,
+			      "operator '%s' has no left operand",
+			      cpp_token_as_text (pfile, op.token));
 	}
 
       top = reduce (pfile, top, op.op);
@@ -1023,7 +1053,8 @@ _cpp_parse_expr (cpp_reader *pfile, bool is_if)
 	  break;
 	case CPP_COLON:
 	  if (top->op != CPP_QUERY)
-	    SYNTAX_ERROR (" ':' without preceding '?'");
+	    SYNTAX_ERROR_AT (op.loc,
+			     " ':' without preceding '?'");
 	  if (!num_zerop (top[-1].value)) /* Was '?' condition true?  */
 	    pfile->state.skip_eval++;
 	  else
@@ -1040,7 +1071,7 @@ _cpp_parse_expr (cpp_reader *pfile, bool is_if)
 
       top->op = op.op;
       top->token = op.token;
-      top->loc = op.token->src_loc;
+      top->loc = op.loc;
     }
 
   /* The controlling macro expression is only valid if we called lex 3
@@ -1051,8 +1082,9 @@ _cpp_parse_expr (cpp_reader *pfile, bool is_if)
 
   if (top != pfile->op_stack)
     {
-      cpp_error (pfile, CPP_DL_ICE, "unbalanced stack in %s",
-		 is_if ? "#if" : "#elif");
+      cpp_error_with_line (pfile, CPP_DL_ICE, top->loc, 0,
+			   "unbalanced stack in %s",
+			   is_if ? "#if" : "#elif");
     syntax_error:
       return false;  /* Return false on syntax error.  */
     }
diff --git a/libcpp/include/cpplib.h b/libcpp/include/cpplib.h
index 909409e..98fa4bb 100644
--- a/libcpp/include/cpplib.h
+++ b/libcpp/include/cpplib.h
@@ -827,7 +827,8 @@ struct cpp_num
 
 /* Classify a CPP_NUMBER token.  The return value is a combination of
    the flags from the above sets.  */
-extern unsigned cpp_classify_number (cpp_reader *, const cpp_token *);
+extern unsigned cpp_classify_number (cpp_reader *, const cpp_token *,
+				     source_location);
 
 /* Evaluate a token classified as category CPP_N_INTEGER.  */
 extern cpp_num cpp_interpret_integer (cpp_reader *, const cpp_token *,
diff --git a/libcpp/internal.h b/libcpp/internal.h
index 9124276..649dc0f 100644
--- a/libcpp/internal.h
+++ b/libcpp/internal.h
@@ -640,6 +640,7 @@ 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 *);
+extern source_location _cpp_get_prev_token_spelling_loc (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 3071952..5fbc587 100644
--- a/libcpp/lex.c
+++ b/libcpp/lex.c
@@ -2885,3 +2885,34 @@ cpp_token_val_index (cpp_token *tok)
       return CPP_TOKEN_FLD_NONE;
     }
 }
+
+/* Return the location of the previous token in the token stream.  */
+
+source_location
+_cpp_get_prev_token_spelling_loc (cpp_reader *pfile)
+{
+  source_location spelling_loc;
+
+  if (CPP_OPTION (pfile, traditional))
+    {
+      if (pfile->state.in_directive)
+	spelling_loc = pfile->directive_line;
+      else
+	spelling_loc = LINEMAPS_ORDINARY_HIGHEST_LINE (pfile->line_table);
+    }
+  /* We don't want to refer to a token before the beginning of the
+     current run -- that is invalid.  */
+  else if (pfile->cur_token == pfile->cur_run->base)
+    {
+      if (pfile->cur_run->prev != NULL)
+	spelling_loc = pfile->cur_run->prev->limit->src_loc;
+      else
+	spelling_loc = 0;
+    }
+  else
+    {
+      spelling_loc = pfile->cur_token[-1].src_loc;
+    }
+
+  return spelling_loc;
+}
-- 
        Dodji

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

* Re: [PATCH 0/6] Tracking locations of tokens resulting from macro expansion
  2010-12-10 11:27 [PATCH 0/6] Tracking locations of tokens resulting from macro expansion Dodji Seketeli
                   ` (6 preceding siblings ...)
  2010-12-10 12:33 ` [PATCH 6/6] Kill pedantic warnings on system headers macros Dodji Seketeli
@ 2010-12-10 12:52 ` Gabriel Dos Reis
  2010-12-10 18:22   ` Dodji Seketeli
  2010-12-10 16:59 ` Jeff Law
  2011-07-16 14:38 ` [PATCH 0/7] " Dodji Seketeli
  9 siblings, 1 reply; 135+ messages in thread
From: Gabriel Dos Reis @ 2010-12-10 12:52 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:
> * Problem statement
>
> Let's consider the diagnostic GCC emits when an error occurs on an
> expression that was defined in a macro that is later expanded:

I like the general idea of the patch.

Ideally, we should avoid introduction of yet another flag for this.
I understand the concern about memory consumption, and I share it.
But, it would be great we could limit that without introducing
new flags -- because it seems the flags would be there only to
expose implementation inefficiency, not fundamental user-level
functionality.

I suspect that for many reasonably well written C++ programs, all those
loci are the same.  Very few would have all three loci (and actually there
is at least a fourth) different.  I would suggest exploring functional
interface where there are different location implementations, corresponding
to the different sensible combinations of location kind.  This way, only
tokens that are actually macro expanded or created through macro
expansion or strnigification get additional necessary slots.

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

* Re: [PATCH 0/6] *** SUBJECT HERE ***
  2010-12-10 11:16 ` [PATCH 0/6] *** SUBJECT HERE *** Dodji Seketeli
@ 2010-12-10 12:56   ` Dave Korn
  0 siblings, 0 replies; 135+ messages in thread
From: Dave Korn @ 2010-12-10 12:56 UTC (permalink / raw)
  To: GCC Patches

On 10/12/2010 11:11, Dodji Seketeli wrote:
> *** BLURB HERE ***

  *** RESPONSE HERE ***

    cheers,
      DaveK
(*** SMILEY HERE ***)

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

* Re: [PATCH 0/6] Tracking locations of tokens resulting from macro expansion
  2010-12-10 11:27 [PATCH 0/6] Tracking locations of tokens resulting from macro expansion Dodji Seketeli
                   ` (7 preceding siblings ...)
  2010-12-10 12:52 ` [PATCH 0/6] Tracking locations of tokens resulting from macro expansion Gabriel Dos Reis
@ 2010-12-10 16:59 ` Jeff Law
  2010-12-10 19:00   ` Dodji Seketeli
  2011-07-16 14:38 ` [PATCH 0/7] " Dodji Seketeli
  9 siblings, 1 reply; 135+ messages in thread
From: Jeff Law @ 2010-12-10 16:59 UTC (permalink / raw)
  To: Dodji Seketeli; +Cc: gcc-patches, tromey, joseph, gdr, lopezibanez

On 12/10/10 04:11, Dodji Seketeli wrote:
> The problem is tokens '1.0', '<<' and '1' all have their location set
> to {line 11, column 3} ({11, 3} for short). That actually is the
> location of the expansion point of the MULT macro. That's is an
> interesting observation -- in the current incarnation of GCC the
> location of each tokens resulting from a macro expansion is set to the
> location of the expansion point of the macro.
Presumably you meant {13, 3}, right?

> Several obversations can be made in that hypothetical example.
>
> - The location mentioned in the error message
>
>   test.c:5:14: error: invalid operands to binary<<  (have ‘double’ and ‘int’)
>
> is the spelling location of the token '<<'. I believe this makes more
> sense than the original behaviour.
Agreed.  However, I suspect some will disagree and it may cause havoc in 
automated code that parses error messages.  Presumably there's a way to 
get the old behavior, even if you're tracking all three locations?


> - The macro expansion stack following the error message unwinds from
> the macro which expansion most directly yielded token "<<". Each
> element of the stack mentions the argument replacement point and the
> expansion point locations that were exposed earlier.
I like it.  Though you might want a parameter which defines how many 
levels in the stack you want to display.  I'm not sure on this one.


>
> * Limits
>
> I guess a pure marketing dude would elide this part :-)
>
> The most annoying limit is obviously memory consumption. Tracking the
> location of every token across every macro expansion does consume
> memory. I tried compiling some a translation here that heavily uses
> the C++ standard library and I could see some 13% increase of memory
> consumption over the entire compilation; note that the compilation was
> consuming around 250MB without the feature. This is why the feature is
> triggered by the -ftrack-macro-expansion flag. Without that flag, the
> memory consumption stays roughly flat. The noticeable downside of that
> flag is that it makes the code more complex. I think there possibly is
> room to decrease this, but fundamentaly the consumption is going to
> increase with the number of macro expanded multiplied by the number of
> tokens per expansion.
I'm surprised at the increase, not that it exists, but the magnitude.  
But if that's the cost, then we'll learn to deal with it.  Having the 
flag definitely helps -- though I can see a point in the future where if 
this stuff works really well, we'll just turn it on by default and be 
done with it.

Jeff

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

* Re: [PATCH 0/6] Tracking locations of tokens resulting from macro expansion
  2010-12-10 12:52 ` [PATCH 0/6] Tracking locations of tokens resulting from macro expansion Gabriel Dos Reis
@ 2010-12-10 18:22   ` Dodji Seketeli
  0 siblings, 0 replies; 135+ messages in thread
From: Dodji Seketeli @ 2010-12-10 18:22 UTC (permalink / raw)
  To: Gabriel Dos Reis; +Cc: gcc-patches, tromey, joseph, lopezibanez

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

> On Fri, Dec 10, 2010 at 5:11 AM, Dodji Seketeli <dodji@redhat.com> wrote:
>> * Problem statement
>>
>> Let's consider the diagnostic GCC emits when an error occurs on an
>> expression that was defined in a macro that is later expanded:
>
> I like the general idea of the patch.

Thanks.

> Ideally, we should avoid introduction of yet another flag for this.

I agree.

> I understand the concern about memory consumption, and I share it.
> But, it would be great we could limit that without introducing
> new flags -- because it seems the flags would be there only to
> expose implementation inefficiency, not fundamental user-level
> functionality.
>
> I suspect that for many reasonably well written C++ programs, all those
> loci are the same.  Very few would have all three loci (and actually there
> is at least a fourth) different.  I would suggest exploring functional
> interface where there are different location implementations, corresponding
> to the different sensible combinations of location kind.  This way, only
> tokens that are actually macro expanded or created through macro
> expansion or strnigification get additional necessary slots.

Actually that's what the second patch does. Only tokens resulting from
macro expansion have the new virtual location. Other tokens just have
the plain old spelling location and thus don't incur any cost.

-- 
	Dodji

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

* Re: [PATCH 0/6] Tracking locations of tokens resulting from macro expansion
  2010-12-10 16:59 ` Jeff Law
@ 2010-12-10 19:00   ` Dodji Seketeli
  2010-12-13 15:10     ` Jeff Law
  0 siblings, 1 reply; 135+ messages in thread
From: Dodji Seketeli @ 2010-12-10 19:00 UTC (permalink / raw)
  To: Jeff Law; +Cc: gcc-patches, tromey, joseph, gdr, lopezibanez

Jeff Law <law@redhat.com> writes:

> On 12/10/10 04:11, Dodji Seketeli wrote:
>> The problem is tokens '1.0', '<<' and '1' all have their location set
>> to {line 11, column 3} ({11, 3} for short). That actually is the
>> location of the expansion point of the MULT macro. That's is an
>> interesting observation -- in the current incarnation of GCC the
>> location of each tokens resulting from a macro expansion is set to the
>> location of the expansion point of the macro.
> Presumably you meant {13, 3}, right?

Yes. Sorry for the confusion.

>
>> Several obversations can be made in that hypothetical example.
>>
>> - The location mentioned in the error message
>>
>>   test.c:5:14: error: invalid operands to binary<<  (have ‘double’ and ‘int’)
>>
>> is the spelling location of the token '<<'. I believe this makes more
>> sense than the original behaviour.
> Agreed.  However, I suspect some will disagree and it may cause havoc
> in automated code that parses error messages.  Presumably there's a
> way to get the old behavior, even if you're tracking all three
> locations?

The linemap API allows us to get the old behaviour (i.e, resolve virtual
locations to the macro expansion point). But as most of the client code
does that through the expand_location function, I wasn't sure how to
introduce choice there in a convenient way. Maybe I should add another
flag to control the "user interface" so to speak?

>
>
>> - The macro expansion stack following the error message unwinds from
>> the macro which expansion most directly yielded token "<<". Each
>> element of the stack mentions the argument replacement point and the
>> expansion point locations that were exposed earlier.
> I like it.  Though you might want a parameter which defines how many
> levels in the stack you want to display.  I'm not sure on this one.

Indeed. I thought about this as well. But then what stopped me really is
wasn't sure about *what* to display. User interface again :-) I am open
to suggestions. Technically, limiting the displayed levels of the stack
is not hard to do.

>
>
>>
>> * Limits
>>
>> I guess a pure marketing dude would elide this part :-)
>>
>> The most annoying limit is obviously memory consumption. Tracking the
>> location of every token across every macro expansion does consume
>> memory. I tried compiling some a translation here that heavily uses
>> the C++ standard library and I could see some 13% increase of memory
>> consumption over the entire compilation; note that the compilation was
>> consuming around 250MB without the feature. This is why the feature is
>> triggered by the -ftrack-macro-expansion flag. Without that flag, the
>> memory consumption stays roughly flat. The noticeable downside of that
>> flag is that it makes the code more complex. I think there possibly is
>> room to decrease this, but fundamentaly the consumption is going to
>> increase with the number of macro expanded multiplied by the number of
>> tokens per expansion.
> I'm surprised at the increase, not that it exists, but the magnitude.
> But if that's the cost, then we'll learn to deal with it.  Having the
> flag definitely helps -- though I can see a point in the future where
> if this stuff works really well, we'll just turn it on by default and
> be done with it.

I got very annoyed by the magnitude. At least now the overhead scales
linearly with the number of macros and tokens per expansion. Tom and I
have been discussing about a way to encode the mapping virtual locations
--> {different kind of physical locations} in a more compact manner. I
really need and want to investigate that and see what I can
gain. Looking the dumps of the memory consumption of the macro maps did
suggest that there is some room of improvement there.

-- 
	Dodji

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

* Re: [PATCH 0/6] Tracking locations of tokens resulting from macro expansion
  2010-12-10 19:00   ` Dodji Seketeli
@ 2010-12-13 15:10     ` Jeff Law
  2010-12-13 16:35       ` Gabriel Dos Reis
  0 siblings, 1 reply; 135+ messages in thread
From: Jeff Law @ 2010-12-13 15:10 UTC (permalink / raw)
  To: Dodji Seketeli; +Cc: gcc-patches, tromey, joseph, gdr, lopezibanez

On 12/10/10 11:10, Dodji Seketeli wrote:
>>
>>> Several obversations can be made in that hypothetical example.
>>>
>>> - The location mentioned in the error message
>>>
>>>    test.c:5:14: error: invalid operands to binary<<   (have ‘double’ and ‘int’)
>>>
>>> is the spelling location of the token '<<'. I believe this makes more
>>> sense than the original behaviour.
>> Agreed.  However, I suspect some will disagree and it may cause havoc
>> in automated code that parses error messages.  Presumably there's a
>> way to get the old behavior, even if you're tracking all three
>> locations?
> The linemap API allows us to get the old behaviour (i.e, resolve virtual
> locations to the macro expansion point). But as most of the client code
> does that through the expand_location function, I wasn't sure how to
> introduce choice there in a convenient way. Maybe I should add another
> flag to control the "user interface" so to speak?
I'm not sure either.  I wouldn't be terribly surprised if we end up 
changing our minds (whatever they may be) after a period of time with 
access to more thorough diagnostic information.  ie, we may not know 
what the right interface should be until after we've used the info for a 
while.  So I guess we should keep our options open for the UI issues.

Jeff

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

* Re: [PATCH 3/6] Emit macro expansion related diagnostics
  2010-12-10 11:27 ` [PATCH 3/6] Emit macro expansion related diagnostics Dodji Seketeli
@ 2010-12-13 15:25   ` Paolo Bonzini
  2010-12-13 15:38     ` Paolo Bonzini
                       ` (3 more replies)
  0 siblings, 4 replies; 135+ messages in thread
From: Paolo Bonzini @ 2010-12-13 15:25 UTC (permalink / raw)
  To: Dodji Seketeli; +Cc: gcc-patches, tromey, joseph, gdr, lopezibanez

On 12/10/2010 12:11 PM, Dodji Seketeli wrote:
> [dodji@adjoa gcc]$ ./cc1 -quiet -ftrack-macro-expansion test.c
> test.c: In function ‘g’:
> test.c:5:14: error: invalid operands to binary<<  (have ‘double’ and ‘int’)
> In macro 'OPERATE' at test.c:2:9
>      Expanded at test.c:5:3
> In macro 'SHIFTL' at test.c:5:14
>      Expanded at test.c:8:3
> In macro 'MULT' at test.c:8:3
>      Expanded at test.c:13:3
> 

I'm not sure I share Jeff's doubts about the location to present.
Possibly _this_ could be controlled by a flag, though.

Also, I know this is just an RFC, but the error message should
probably look like either this example:

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 'MULT'
test.c:13:3: note: expanded from here

or this shorter example:

test.c:5:14: error: invalid operands to binary<<  (have ‘double’ and ‘int’)
test.c:2:9: note: while expanding macro 'OPERATE'
test.c:5:14: note: while expanding macro 'SHIFTL'
test.c:8:3: note: while expanding macro 'MULT'
test.c:13:3: note: expanded from here

or this that does not change the location compared to current GCC:

test.c:13:3: error: invalid operands to binary<<  (have ‘double’ and ‘int’)
test.c:2:9: note: while expanding macro 'OPERATE'
test.c:5:14: note: while expanding macro 'SHIFTL'
test.c:8:3: note: while expanding macro 'MULT'

Paolo

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

* Re: [PATCH 3/6] Emit macro expansion related diagnostics
  2010-12-13 15:25   ` Paolo Bonzini
@ 2010-12-13 15:38     ` Paolo Bonzini
  2010-12-13 16:30     ` Manuel López-Ibáñez
                       ` (2 subsequent siblings)
  3 siblings, 0 replies; 135+ messages in thread
From: Paolo Bonzini @ 2010-12-13 15:38 UTC (permalink / raw)
  To: gcc-patches; +Cc: gcc-patches, tromey, joseph, gdr, lopezibanez

On 12/10/2010 12:11 PM, Dodji Seketeli wrote:
> [dodji@adjoa gcc]$ ./cc1 -quiet -ftrack-macro-expansion test.c
> test.c: In function ‘g’:
> test.c:5:14: error: invalid operands to binary<<  (have ‘double’ and ‘int’)
> In macro 'OPERATE' at test.c:2:9
>      Expanded at test.c:5:3
> In macro 'SHIFTL' at test.c:5:14
>      Expanded at test.c:8:3
> In macro 'MULT' at test.c:8:3
>      Expanded at test.c:13:3
> 

I'm not sure I share Jeff's doubts about the location to present.
Possibly _this_ could be controlled by a flag, though.

Also, I know this is just an RFC, but the error message should
probably look like either this example:

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 'MULT'
test.c:13:3: note: expanded from here

or this shorter example:

test.c:5:14: error: invalid operands to binary<<  (have ‘double’ and ‘int’)
test.c:2:9: note: while expanding macro 'OPERATE'
test.c:5:14: note: while expanding macro 'SHIFTL'
test.c:8:3: note: while expanding macro 'MULT'
test.c:13:3: note: expanded from here

or this that does not change the location compared to current GCC:

test.c:13:3: error: invalid operands to binary<<  (have ‘double’ and ‘int’)
test.c:2:9: note: while expanding macro 'OPERATE'
test.c:5:14: note: while expanding macro 'SHIFTL'
test.c:8:3: note: while expanding macro 'MULT'

Paolo

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

* Re: [PATCH 3/6] Emit macro expansion related diagnostics
  2010-12-13 15:25   ` Paolo Bonzini
  2010-12-13 15:38     ` Paolo Bonzini
@ 2010-12-13 16:30     ` Manuel López-Ibáñez
  2010-12-14  7:24     ` Dodji Seketeli
  2010-12-14  7:28     ` Dodji Seketeli
  3 siblings, 0 replies; 135+ messages in thread
From: Manuel López-Ibáñez @ 2010-12-13 16:30 UTC (permalink / raw)
  To: Paolo Bonzini; +Cc: Dodji Seketeli, gcc-patches, tromey, joseph, gdr

On 13 December 2010 15:44, Paolo Bonzini <bonzini@gnu.org> wrote:
> On 12/10/2010 12:11 PM, Dodji Seketeli wrote:
>> [dodji@adjoa gcc]$ ./cc1 -quiet -ftrack-macro-expansion test.c
>> test.c: In function ‘g’:
>> test.c:5:14: error: invalid operands to binary<<  (have ‘double’ and ‘int’)
>> In macro 'OPERATE' at test.c:2:9
>>      Expanded at test.c:5:3
>> In macro 'SHIFTL' at test.c:5:14
>>      Expanded at test.c:8:3
>> In macro 'MULT' at test.c:8:3
>>      Expanded at test.c:13:3
>>
>
>
> or this shorter example:
>
> test.c:5:14: error: invalid operands to binary<<  (have ‘double’ and ‘int’)
> test.c:2:9: note: while expanding macro 'OPERATE'
> test.c:5:14: note: while expanding macro 'SHIFTL'
> test.c:8:3: note: while expanding macro 'MULT'
> test.c:13:3: note: expanded from here
>

This format is closer to what GCC currently prints for templates
instantiations, however, in template instantiations, the context goes
first and the error/warning goes last. The context is also not marked
with "note:". See
http://people.redhat.com/bkoz/diagnostics/diagnostics.html#template-instantiate-1
and http://people.redhat.com/bkoz/diagnostics/diagnostics.html#9335
and other examples therein.

See gcc/cp/error.c:print_instantiation_partial_context, which also
handles eliding excessive number of instantiation contexts (too deep
macro expansion). I am not saying that this has to be done in this
patch series, but it should eventually be implemented, so why not copy
what is already available?

> or this that does not change the location compared to current GCC:
>
> test.c:13:3: error: invalid operands to binary<<  (have ‘double’ and ‘int’)
> test.c:2:9: note: while expanding macro 'OPERATE'
> test.c:5:14: note: while expanding macro 'SHIFTL'
> test.c:8:3: note: while expanding macro 'MULT'

Interestingly, this is closer to the format adopted by Clang
(http://clang.llvm.org/diagnostics.html), which I guess just followed
GCC. However, I don't see how changing the locations to the ones
proposed by Dodji causes any kind of "havoc" for users. In fact,
looking at the caret information of clang, the diagnostic would be
clearer if the locations were exchanged, which is what Dodji proposes.

I don't think gcc diagnostics can be used to do any kind of automatic
rewriting. At most, it is parsed in order to present the location of
errors/messages in a nicer way, and the new locations proposed by
Dodji do not break that.

The question should be what locations are clearer/more informative to
the user. I think the new locations proposed by Dodji are more
informative and they follow what g++ already does for template errors.

In any case, I guess that if there is a flag to disable the tracking
of macro expansions for memory concerns, the same flag disables the
enhanced locations and reverts to the current status, no? No need for
yet another separate flag to control the output.

Cheers,

Manuel.

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

* Re: [PATCH 0/6] Tracking locations of tokens resulting from macro expansion
  2010-12-13 15:10     ` Jeff Law
@ 2010-12-13 16:35       ` Gabriel Dos Reis
  2010-12-14  9:24         ` Dodji Seketeli
  0 siblings, 1 reply; 135+ messages in thread
From: Gabriel Dos Reis @ 2010-12-13 16:35 UTC (permalink / raw)
  To: Jeff Law; +Cc: Dodji Seketeli, gcc-patches, tromey, joseph, lopezibanez

On Mon, Dec 13, 2010 at 8:27 AM, Jeff Law <law@redhat.com> wrote:
> On 12/10/10 11:10, Dodji Seketeli wrote:
>>>
>>>> Several obversations can be made in that hypothetical example.
>>>>
>>>> - The location mentioned in the error message
>>>>
>>>>   test.c:5:14: error: invalid operands to binary<<   (have ‘double’ and
>>>> ‘int’)
>>>>
>>>> is the spelling location of the token '<<'. I believe this makes more
>>>> sense than the original behaviour.
>>>
>>> Agreed.  However, I suspect some will disagree and it may cause havoc
>>> in automated code that parses error messages.  Presumably there's a
>>> way to get the old behavior, even if you're tracking all three
>>> locations?
>>
>> The linemap API allows us to get the old behaviour (i.e, resolve virtual
>> locations to the macro expansion point). But as most of the client code
>> does that through the expand_location function, I wasn't sure how to
>> introduce choice there in a convenient way. Maybe I should add another
>> flag to control the "user interface" so to speak?
>
> I'm not sure either.  I wouldn't be terribly surprised if we end up changing
> our minds (whatever they may be) after a period of time with access to more
> thorough diagnostic information.  ie, we may not know what the right
> interface should be until after we've used the info for a while.  So I guess
> we should keep our options open for the UI issues.

Agreed.  One thing we should also keep in mind though:  GCC has already
way too many flags.  Having dedicated flags to control every single
aspect does not necessarily make for good design.

-- gaby

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

* Re: [PATCH 3/6] Emit macro expansion related diagnostics
  2010-12-13 15:25   ` Paolo Bonzini
  2010-12-13 15:38     ` Paolo Bonzini
  2010-12-13 16:30     ` Manuel López-Ibáñez
@ 2010-12-14  7:24     ` Dodji Seketeli
  2010-12-14  7:28       ` Gabriel Dos Reis
  2010-12-14  7:28     ` Dodji Seketeli
  3 siblings, 1 reply; 135+ messages in thread
From: Dodji Seketeli @ 2010-12-14  7:24 UTC (permalink / raw)
  To: Paolo Bonzini; +Cc: gcc-patches, tromey, joseph, gdr, lopezibanez

Paolo Bonzini <bonzini@gnu.org> writes:

> On 12/10/2010 12:11 PM, Dodji Seketeli wrote:
>> [dodji@adjoa gcc]$ ./cc1 -quiet -ftrack-macro-expansion test.c
>> test.c: In function ‘g’:
>> test.c:5:14: error: invalid operands to binary<<  (have ‘double’ and ‘int’)
>> In macro 'OPERATE' at test.c:2:9
>>      Expanded at test.c:5:3
>> In macro 'SHIFTL' at test.c:5:14
>>      Expanded at test.c:8:3
>> In macro 'MULT' at test.c:8:3
>>      Expanded at test.c:13:3
>> 
>
> I'm not sure I share Jeff's doubts about the location to present.
> Possibly _this_ could be controlled by a flag, though.
>
> Also, I know this is just an RFC, but the error message should
> probably look like either this example:
>
> 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 'MULT'
> test.c:13:3: note: expanded from here

Sorry if I am dig into design consideration when we are talking about
UI, but I think it is needed to clarify things here.

The current custom in the compiler is that the "<locus> note:" prefix is
displayed only when client code explicitely calls the 'inform'
function. That sets the diagnostic kind to DK_NOTE.

This seems a little bit different from what is done for macro expansion
contexts.

The macro expansion context is unwound implicitly. That is, the client
code calls e.g:

    error_at (some_location, "An error occured")

and if some_location appears to be the location of a token resulting
from macro expansion the diagnostic machinery unwinds the expansion
context and displays it to the user, regardless of what the diagnostic
kind was.  So the context lines are not prefixed with "<locus> note:" as
the context is generated implicitely.  This is similar to what G++ does
when it displays template instantiation contexts.

On the other hand, I find the "<locus> note:" usage more regular and
thus easier to parse for tools that interact with the output of the
compiler. So there could be some value in us using that prefix.

However if we are to use that prefix for implicitely displayed contexts,
I think we should factorize diagnostic_build_prefix to allow using the
same prefix as the one used for e.g. DK_NOTE. Would that be acceptable?

-- 
	Dodji

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

* Re: [PATCH 3/6] Emit macro expansion related diagnostics
  2010-12-14  7:24     ` Dodji Seketeli
@ 2010-12-14  7:28       ` Gabriel Dos Reis
  2010-12-14  8:40         ` Dodji Seketeli
  0 siblings, 1 reply; 135+ messages in thread
From: Gabriel Dos Reis @ 2010-12-14  7:28 UTC (permalink / raw)
  To: Dodji Seketeli; +Cc: Paolo Bonzini, gcc-patches, tromey, joseph, lopezibanez

On Tue, Dec 14, 2010 at 12:54 AM, Dodji Seketeli <dodji@redhat.com> wrote:
> The macro expansion context is unwound implicitly. That is, the client
> code calls e.g:
>
>    error_at (some_location, "An error occured")
>
> and if some_location appears to be the location of a token resulting
> from macro expansion the diagnostic machinery unwinds the expansion
> context and displays it to the user, regardless of what the diagnostic
> kind was.  So the context lines are not prefixed with "<locus> note:" as
> the context is generated implicitely.  This is similar to what G++ does
> when it displays template instantiation contexts.

Let me add some background about this aspect of the diagnostic
machinery.  The functions inform(), error(), error_at(), etc. are
offered as some `high-level' building blocks for constructing more
advanced diagnostic functions.  It is just that we have not been
very disciplined at factorizing things correctly, and instead we
tend to go for the easier road of `inlining' calls to error() or
inform().   However, models to look for are print_candidates(),
cxx_print_error_function, the newly introduced qualified_name_lookup_errorr,
etc.

The point here is that I would expect CPP to define its own error print
diagnostic function that tracks macro expansion context
(at bit like what what we do with template instantiation contexts)
and combine calls to error_at() and inform().

Note also that we don't capitalize diagnostic messages (and they don't end
with periods either.)

-- Gaby

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

* Re: [PATCH 3/6] Emit macro expansion related diagnostics
  2010-12-13 15:25   ` Paolo Bonzini
                       ` (2 preceding siblings ...)
  2010-12-14  7:24     ` Dodji Seketeli
@ 2010-12-14  7:28     ` Dodji Seketeli
  2010-12-14  8:19       ` Gabriel Dos Reis
  3 siblings, 1 reply; 135+ messages in thread
From: Dodji Seketeli @ 2010-12-14  7:28 UTC (permalink / raw)
  To: Paolo Bonzini; +Cc: gcc-patches, tromey, joseph, gdr, lopezibanez

Paolo Bonzini <bonzini@gnu.org> writes:

>
> test.c:5:14: error: invalid operands to binary<<  (have ‘double’ and ‘int’)
> test.c:2:9: note: while expanding macro 'OPERATE'
> test.c:5:14: note: while expanding macro 'SHIFTL'
> test.c:8:3: note: while expanding macro 'MULT'
> test.c:13:3: note: expanded from here
>

In the absence of caret diagnostic I think it is useful to explicitely
make the difference between a location in the definition of the macro,
and the location of the macro expansion point, i.e:

In this error message:

test.c: In function ‘g’:
test.c:5:14: error: invalid operands to binary << (have ‘double’ and ‘int’)
In macro 'OPERATE' at test.c:2:9
    Expanded at test.c:5:3

[...]

Here is what we are trying to show:

     1    #define OPERATE(OPRD1, OPRT, OPRD2) \
     2      OPRD1 OPRT OPRD2;
                  ^
                  |
                  ---< location in the /definition/ of OPERATE
                       pointed to by: "In macro OPERATE at test.c:2:9"
     3
     4    #define SHIFTL(A,B) \
     5      OPERATE (A,<<,B)
            ^
            |
            ---< location of /expansion point/ of OPERATE
                 pointed to by "Expanded at test.c:5:3"

[...]


I think that's more explicit (and informative) than just saying:

> test.c:5:14: error: invalid operands to binary<<  (have ‘double’ and ‘int’)
> test.c:2:9: note: while expanding macro 'OPERATE'
> test.c:5:14: note: while expanding macro 'SHIFTL'

But when we have caret diagnostic where we actually show the user the
code we are talking about we can use the "while expanding" just fine.

-- 
	Dodji

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

* Re: [PATCH 3/6] Emit macro expansion related diagnostics
  2010-12-14  7:28     ` Dodji Seketeli
@ 2010-12-14  8:19       ` Gabriel Dos Reis
  2010-12-14  8:31         ` Paolo Bonzini
  0 siblings, 1 reply; 135+ messages in thread
From: Gabriel Dos Reis @ 2010-12-14  8:19 UTC (permalink / raw)
  To: Dodji Seketeli; +Cc: Paolo Bonzini, gcc-patches, tromey, joseph, lopezibanez

On Tue, Dec 14, 2010 at 1:19 AM, Dodji Seketeli <dodji@redhat.com> wrote:
> Paolo Bonzini <bonzini@gnu.org> writes:
>
>>
>> test.c:5:14: error: invalid operands to binary<<  (have ‘double’ and ‘int’)
>> test.c:2:9: note: while expanding macro 'OPERATE'
>> test.c:5:14: note: while expanding macro 'SHIFTL'
>> test.c:8:3: note: while expanding macro 'MULT'
>> test.c:13:3: note: expanded from here
>>
>
> In the absence of caret diagnostic I think it is useful to explicitely
> make the difference between a location in the definition of the macro,
> and the location of the macro expansion point, i.e:

Agreed.  But I also agree with Paolo's observation: the prefix "<locus>: note"
has to precede the diagnostic message.  I know there are other compilers
that do it differently, but GCC convention has been to precede diagnostics
with loci -- until we move to 2-dimensional diagnostic display (what
is sometimes
referred to as diagnostics with carets).

-- Gaby

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

* Re: [PATCH 3/6] Emit macro expansion related diagnostics
  2010-12-14  8:19       ` Gabriel Dos Reis
@ 2010-12-14  8:31         ` Paolo Bonzini
  2010-12-14  9:23           ` Dodji Seketeli
  0 siblings, 1 reply; 135+ messages in thread
From: Paolo Bonzini @ 2010-12-14  8:31 UTC (permalink / raw)
  To: Gabriel Dos Reis; +Cc: Dodji Seketeli, gcc-patches, tromey, joseph, lopezibanez

On Tue, Dec 14, 2010 at 08:28, Gabriel Dos Reis
<gdr@integrable-solutions.net> wrote:
> Agreed.  But I also agree with Paolo's observation: the prefix "<locus>: note"
> has to precede the diagnostic message.  I know there are other compilers
> that do it differently, but GCC convention has been to precede diagnostics
> with loci

That was my main point, yes.  I'm not sure about the verboseness of
having two lines per level of macro expansion, but I can live with
that and it was secondary in my message.

Paolo

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

* Re: [PATCH 3/6] Emit macro expansion related diagnostics
  2010-12-14  7:28       ` Gabriel Dos Reis
@ 2010-12-14  8:40         ` Dodji Seketeli
  2010-12-14  9:38           ` Gabriel Dos Reis
  0 siblings, 1 reply; 135+ messages in thread
From: Dodji Seketeli @ 2010-12-14  8:40 UTC (permalink / raw)
  To: Gabriel Dos Reis; +Cc: Paolo Bonzini, gcc-patches, tromey, joseph, lopezibanez

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

> Let me add some background about this aspect of the diagnostic
> machinery.  The functions inform(), error(), error_at(), etc. are
> offered as some `high-level' building blocks for constructing more
> advanced diagnostic functions.  It is just that we have not been
> very disciplined at factorizing things correctly, and instead we
> tend to go for the easier road of `inlining' calls to error() or
> inform().

Okay. Thank you for this background.

> However, models to look for are print_candidates(),
> cxx_print_error_function, the newly introduced
> qualified_name_lookup_errorr, etc.

I see. Though, some of these functions call below the level error and
inform by calling pp_base_set_prefix, pp_verbatim and the like. Are
those pp_* calls intended as well?

>
> The point here is that I would expect CPP to define its own error print
> diagnostic function that tracks macro expansion context
> (at bit like what what we do with template instantiation contexts)
> and combine calls to error_at() and inform().

Okay, so I guess I will move the macro expansion unwinder into CPP and
make it and use the CPP diagnostic routines that in turn use c_cpp_error
(via the cpp_reader callbacks) that is at the same level as inform()
error() etc.

I will still have to make default_diagnostic_finalizer call that
unwinder to make macro expansion context be printed implicitely, though.

> Note also that we don't capitalize diagnostic messages (and they don't end
> with periods either.)

Thanks. I'll fix that.

-- 
	Dodji

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

* Re: [PATCH 3/6] Emit macro expansion related diagnostics
  2010-12-14  8:31         ` Paolo Bonzini
@ 2010-12-14  9:23           ` Dodji Seketeli
  0 siblings, 0 replies; 135+ messages in thread
From: Dodji Seketeli @ 2010-12-14  9:23 UTC (permalink / raw)
  To: Paolo Bonzini; +Cc: Gabriel Dos Reis, gcc-patches, tromey, joseph, lopezibanez

Paolo Bonzini <bonzini@gnu.org> writes:

> On Tue, Dec 14, 2010 at 08:28, Gabriel Dos Reis
> <gdr@integrable-solutions.net> wrote:
>> Agreed.  But I also agree with Paolo's observation: the prefix "<locus>: note"
>> has to precede the diagnostic message.  I know there are other compilers
>> that do it differently, but GCC convention has been to precede diagnostics
>> with loci
>
> That was my main point, yes.  I'm not sure about the verboseness of
> having two lines per level of macro expansion, but I can live with
> that and it was secondary in my message.

OK, this is consistent with the discussion we've just had about re-using
functions like inform() in another sub-thread. I'll do that then.

Thanks.

-- 
	Dodji

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

* Re: [PATCH 0/6] Tracking locations of tokens resulting from macro expansion
  2010-12-13 16:35       ` Gabriel Dos Reis
@ 2010-12-14  9:24         ` Dodji Seketeli
  2010-12-14  9:40           ` Gabriel Dos Reis
  0 siblings, 1 reply; 135+ messages in thread
From: Dodji Seketeli @ 2010-12-14  9:24 UTC (permalink / raw)
  To: Gabriel Dos Reis; +Cc: Jeff Law, gcc-patches, tromey, joseph, lopezibanez

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

> On Mon, Dec 13, 2010 at 8:27 AM, Jeff Law <law@redhat.com> wrote:

[...]

>> I'm not sure either.  I wouldn't be terribly surprised if we end up changing
>> our minds (whatever they may be) after a period of time with access to more
>> thorough diagnostic information.  ie, we may not know what the right
>> interface should be until after we've used the info for a while.  So I guess
>> we should keep our options open for the UI issues.
>
> Agreed.  One thing we should also keep in mind though:  GCC has already
> way too many flags.  Having dedicated flags to control every single
> aspect does not necessarily make for good design.
>

Sure. I am not a flag-soup fan myself. My intent was just to provide a
way for people to opt in and out for experimentation purposes during,
say, stage 1. When we know what we want and come to an acceptable
implementation then of course the flag will go away. If we are sure
about what we want in advance as to avoid the flag in the first place,
even better.

-- 
	Dodji

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

* Re: [PATCH 3/6] Emit macro expansion related diagnostics
  2010-12-14  8:40         ` Dodji Seketeli
@ 2010-12-14  9:38           ` Gabriel Dos Reis
  2010-12-14  9:42             ` Dodji Seketeli
  0 siblings, 1 reply; 135+ messages in thread
From: Gabriel Dos Reis @ 2010-12-14  9:38 UTC (permalink / raw)
  To: Dodji Seketeli; +Cc: Paolo Bonzini, gcc-patches, tromey, joseph, lopezibanez

On Tue, Dec 14, 2010 at 2:22 AM, Dodji Seketeli <dodji@redhat.com> wrote:

>> However, models to look for are print_candidates(),
>> cxx_print_error_function, the newly introduced
>> qualified_name_lookup_errorr, etc.
>
> I see. Though, some of these functions call below the level error and
> inform by calling pp_base_set_prefix, pp_verbatim and the like. Are
> those pp_* calls intended as well?

Yes, that is another dimension to the diagnostic machinery.
The idea here is that every client (i.e. front-end) may have som
front-end specific actions to take (e.g. printing template instantiation
contexts) before the core diagnostic message is printed.  That
is the role of `diagnostic starter' functions (see the comments
in diagnostic.h).  Similarly we have `diagnostic finalizer' functions
which are supposed to do any sort of `cleanup' after a diagnostic
is printed.

Now, you can consider CPP as a sort of front-end that turns
raw input file into a stream of tokens which is then handed over
to the C or C++ parsers -- but I'm not sure that is the way it currently
works.

So, another option is to find a combination of these two dimensions.

>
>>
>> The point here is that I would expect CPP to define its own error print
>> diagnostic function that tracks macro expansion context
>> (at bit like what what we do with template instantiation contexts)
>> and combine calls to error_at() and inform().
>
> Okay, so I guess I will move the macro expansion unwinder into CPP and
> make it and use the CPP diagnostic routines that in turn use c_cpp_error
> (via the cpp_reader callbacks) that is at the same level as inform()
> error() etc.

It is my belief that the macro-expansion related diagnostics specific
improvements belong there.

>
> I will still have to make default_diagnostic_finalizer call that
> unwinder to make macro expansion context be printed implicitely, though.

something you can't do with CPP specific diagnostic finalizer?

-- Gaby

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

* Re: [PATCH 0/6] Tracking locations of tokens resulting from macro expansion
  2010-12-14  9:24         ` Dodji Seketeli
@ 2010-12-14  9:40           ` Gabriel Dos Reis
  2010-12-14  9:45             ` Dodji Seketeli
  0 siblings, 1 reply; 135+ messages in thread
From: Gabriel Dos Reis @ 2010-12-14  9:40 UTC (permalink / raw)
  To: Dodji Seketeli; +Cc: Jeff Law, gcc-patches, tromey, joseph, lopezibanez

On Tue, Dec 14, 2010 at 2:31 AM, Dodji Seketeli <dodji@redhat.com> wrote:
> Gabriel Dos Reis <gdr@integrable-solutions.net> writes:
>
>> On Mon, Dec 13, 2010 at 8:27 AM, Jeff Law <law@redhat.com> wrote:
>
> [...]
>
>>> I'm not sure either.  I wouldn't be terribly surprised if we end up changing
>>> our minds (whatever they may be) after a period of time with access to more
>>> thorough diagnostic information.  ie, we may not know what the right
>>> interface should be until after we've used the info for a while.  So I guess
>>> we should keep our options open for the UI issues.
>>
>> Agreed.  One thing we should also keep in mind though:  GCC has already
>> way too many flags.  Having dedicated flags to control every single
>> aspect does not necessarily make for good design.
>>
>
> Sure. I am not a flag-soup fan myself. My intent was just to provide a
> way for people to opt in and out for experimentation purposes during,
> say, stage 1. When we know what we want and come to an acceptable
> implementation then of course the flag will go away. If we are sure
> about what we want in advance as to avoid the flag in the first place,
> even better.

My experience with GCC is that things are easier to get in than to take
out.  And stage 1 is experimental and also the phase where you want
most feedback.  IMHO, it isn't a good stage to let people opt out :-)

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

* Re: [PATCH 3/6] Emit macro expansion related diagnostics
  2010-12-14  9:38           ` Gabriel Dos Reis
@ 2010-12-14  9:42             ` Dodji Seketeli
  2010-12-14  9:48               ` Gabriel Dos Reis
  0 siblings, 1 reply; 135+ messages in thread
From: Dodji Seketeli @ 2010-12-14  9:42 UTC (permalink / raw)
  To: Gabriel Dos Reis; +Cc: Paolo Bonzini, gcc-patches, tromey, joseph, lopezibanez

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

[...]

>> I see. Though, some of these functions call below the level error and
>> inform by calling pp_base_set_prefix, pp_verbatim and the like. Are
>> those pp_* calls intended as well?
>
> Yes, that is another dimension to the diagnostic machinery.
> The idea here is that every client (i.e. front-end) may have som
> front-end specific actions to take (e.g. printing template instantiation
> contexts) before the core diagnostic message is printed.  That
> is the role of `diagnostic starter' functions (see the comments
> in diagnostic.h).  Similarly we have `diagnostic finalizer' functions
> which are supposed to do any sort of `cleanup' after a diagnostic
> is printed.

OK. And front-ends are supposed to be able to provide their own
diagnostics starter and finalizer functions.

> Now, you can consider CPP as a sort of front-end that turns
> raw input file into a stream of tokens which is then handed over
> to the C or C++ parsers -- but I'm not sure that is the way it currently
> works.

A sort of front-end indeed.

[..]

>> Okay, so I guess I will move the macro expansion unwinder into CPP and
>> make it and use the CPP diagnostic routines that in turn use c_cpp_error
>> (via the cpp_reader callbacks) that is at the same level as inform()
>> error() etc.
>
> It is my belief that the macro-expansion related diagnostics specific
> improvements belong there.
>
>>
>> I will still have to make default_diagnostic_finalizer call that
>> unwinder to make macro expansion context be printed implicitely, though.
>
> something you can't do with CPP specific diagnostic finalizer?

Good catch. The thing is CPP is used like a library by the front-end (C,
C++, Fortran) that needs to consume the tokens it generates. As such,
CPP itself doesn't override the diagnostic finalizer. It's the front-end
that does it. And right now the C and Fortran FEs just use the default
diagnostic finalizer.

If I underrstand correctly, I should now provide a specific diagnostic
finalizer for C and Fortran FEs (like what G++ does) and have them call
the macro expansion unwinder (that would be) provided by CPP and leave
default_diagnostic_finalizer alone.

-- 
	Dodji

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

* Re: [PATCH 0/6] Tracking locations of tokens resulting from macro expansion
  2010-12-14  9:40           ` Gabriel Dos Reis
@ 2010-12-14  9:45             ` Dodji Seketeli
  0 siblings, 0 replies; 135+ messages in thread
From: Dodji Seketeli @ 2010-12-14  9:45 UTC (permalink / raw)
  To: Gabriel Dos Reis; +Cc: Jeff Law, gcc-patches, tromey, joseph, lopezibanez

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

> My experience with GCC is that things are easier to get in than to take
> out.  And stage 1 is experimental and also the phase where you want
> most feedback.  IMHO, it isn't a good stage to let people opt out :-)

Fair enough :-)

-- 
	Dodji

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

* Re: [PATCH 3/6] Emit macro expansion related diagnostics
  2010-12-14  9:42             ` Dodji Seketeli
@ 2010-12-14  9:48               ` Gabriel Dos Reis
  0 siblings, 0 replies; 135+ messages in thread
From: Gabriel Dos Reis @ 2010-12-14  9:48 UTC (permalink / raw)
  To: Dodji Seketeli; +Cc: Paolo Bonzini, gcc-patches, tromey, joseph, lopezibanez

On Tue, Dec 14, 2010 at 3:22 AM, Dodji Seketeli <dodji@redhat.com> wrote:
> Gabriel Dos Reis <gdr@integrable-solutions.net> writes:
>
> [...]
>
>>> I see. Though, some of these functions call below the level error and
>>> inform by calling pp_base_set_prefix, pp_verbatim and the like. Are
>>> those pp_* calls intended as well?
>>
>> Yes, that is another dimension to the diagnostic machinery.
>> The idea here is that every client (i.e. front-end) may have som
>> front-end specific actions to take (e.g. printing template instantiation
>> contexts) before the core diagnostic message is printed.  That
>> is the role of `diagnostic starter' functions (see the comments
>> in diagnostic.h).  Similarly we have `diagnostic finalizer' functions
>> which are supposed to do any sort of `cleanup' after a diagnostic
>> is printed.
>
> OK. And front-ends are supposed to be able to provide their own
> diagnostics starter and finalizer functions.

yes.

[..]

>>> I will still have to make default_diagnostic_finalizer call that
>>> unwinder to make macro expansion context be printed implicitely, though.
>>
>> something you can't do with CPP specific diagnostic finalizer?
>
> Good catch. The thing is CPP is used like a library by the front-end (C,
> C++, Fortran) that needs to consume the tokens it generates. As such,
> CPP itself doesn't override the diagnostic finalizer. It's the front-end
> that does it. And right now the C and Fortran FEs just use the default
> diagnostic finalizer.
>
> If I underrstand correctly, I should now provide a specific diagnostic
> finalizer for C and Fortran FEs (like what G++ does) and have them call
> the macro expansion unwinder (that would be) provided by CPP and leave
> default_diagnostic_finalizer alone.

yes, that sounds good.

^ permalink raw reply	[flat|nested] 135+ 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; 135+ 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] 135+ messages in thread

* Re: [PATCH 1/6] Linemap infrastructure for virtual locations
  2010-12-10 11:53 ` [PATCH 1/6] Linemap infrastructure for virtual locations Dodji Seketeli
@ 2011-01-06 16:48   ` Tom Tromey
  2011-04-12 14:39     ` Dodji Seketeli
  0 siblings, 1 reply; 135+ messages in thread
From: Tom Tromey @ 2011-01-06 16:48 UTC (permalink / raw)
  To: Dodji Seketeli; +Cc: gcc-patches, joseph, gdr, lopezibanez

>>>>> "Dodji" == Dodji Seketeli <dodji@redhat.com> writes:

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

Thank you very much for doing this.

I think the accessor macros and changes to use them could go in as a
separate patch.  It doesn't matter too much now, but I think this would
have made this patch more readable.

Dodji>    expanded_location xloc;
Dodji>    if (loc <= BUILTINS_LOCATION)
Dodji> -    {
Dodji> -      xloc.file = loc == UNKNOWN_LOCATION ? NULL : _("<built-in>");
Dodji> +    {      
Dodji> +      /* If LOC is UNKNOWN_LOCATION and there was a map allocated for
Dodji> +	 it then XLOC.FILE should be the file path of the map,
Dodji> +	 i.e. the file path of the main file being compiled.
Dodji> +	 Otherwise [if we are being called before any line map has
Dodji> +	 been allocated, e.g. during parsing of commande line
Dodji> +	 options] if no line map has been allocated yet, then
Dodji> +	 XLOC.FILE should just be NULL.  */
Dodji> +      if (loc == UNKNOWN_LOCATION)
Dodji> +	{
Dodji> +	  const struct line_map *map =
Dodji> +	    linemap_lookup (line_table, UNKNOWN_LOCATION);
Dodji> +	  xloc.file = map != NULL ? LINEMAP_FILE (map) : NULL;
Dodji> +	}

I don't understand this.  I thought that UNKNOWN_LOCATION should never
have an associated file.  This is handled in a funny way: each front end
is responsible for ensuring that this holds true (this could be cleaned
up...).

Am I mistaken about this?  Or is something weird happening after this
patch?

Dodji> @@ -883,14 +884,14 @@ static void
Dodji>  do_line (cpp_reader *pfile)
Dodji>  {
Dodji>    const struct line_maps *line_table = pfile->line_table;
Dodji> -  const struct line_map *map = &line_table->maps[line_table->used - 1];
Dodji> +  const struct line_map *map = LINEMAPS_LAST_ORDINARY_MAP (line_table);
 
Dodji>    /* skip_rest_of_line() may cause line table to be realloc()ed so note down
Dodji>       sysp right now.  */
 
Dodji> -  unsigned char map_sysp = map->sysp;
Dodji> +  unsigned char map_sysp = map->d.ordinary.sysp;
Dodji>    const cpp_token *token;
Dodji> -  const char *new_file = map->to_file;
Dodji> +  const char *new_file = map->d.ordinary.to_file;

Did you want to use the accessor macros here?
If not, why not?

Dodji>    /* C99 raised the minimum limit on #line numbers.  */
Dodji> @@ -945,11 +946,11 @@ static void
Dodji>  do_linemarker (cpp_reader *pfile)
Dodji>  {
Dodji>    const struct line_maps *line_table = pfile->line_table;
Dodji> -  const struct line_map *map = &line_table->maps[line_table->used - 1];
Dodji> +  const struct line_map *map = LINEMAPS_LAST_ORDINARY_MAP (line_table);
Dodji>    const cpp_token *token;
Dodji> -  const char *new_file = map->to_file;
Dodji> +  const char *new_file = map->d.ordinary.to_file;
Dodji>    linenum_type new_lineno;
Dodji> -  unsigned int new_sysp = map->sysp;
Dodji> +  unsigned int new_sysp = map->d.ordinary.sysp;

Likewise.

Dodji>    if (map != NULL)
Dodji> -    linemap_line_start (pfile->line_table, map->to_line, 127);
Dodji> +    linemap_line_start (pfile->line_table, map->d.ordinary.to_line, 127);

Likewise.

Dodji> +  int sysp = (LINEMAPS_ORDINARY_HIGHEST_LINE (pfile->line_table) > 1)
Dodji> +    && (pfile->buffer ? pfile->buffer->sysp : 0);

The indentation is off here.
Put another paren after the '=' and emacs will DTRT.

Dodji> +   physicall source locations are called "spelling locations".

Typo, should be "physical".

Dodji> +/* A set of chronological line_map structures.  */
Dodji> +struct GTY(()) line_maps {
Dodji> +  
Dodji> +  struct maps_info info_ordinary;
Dodji> +
Dodji> +  struct maps_info info_macro;

One thing to note is that there may be some code that assumes an
ordering of location values.  If you hand out ordinary and macro
locations separately (which I think is what is going on), then code
doing this may break.

My recollection is that maybe the diagnostic code did this?
Or maybe it was someplace more obscure :-(
Did you happen to run across this while working on this patch?

Dodji> -      fname = pfile->line_table->maps[pfile->line_table->used-1].to_file;
Dodji> +      fname = (LINEMAPS_LAST_ORDINARY_MAP (pfile->line_table))->d.ordinary.to_file;

Accessor?

Dodji> +/* Add a mapping of logical source line to physical source file and
Dodji> +   line number. This function creates an "ordinary map", which is a
Dodji> +   map that records locations of tokens that are not part of macro
Dodji> +   replacement-lists present at a macro expansion point.
Dodji> +
Dodji> +   The text pointed to by TO_FILE must have a lifetime
Dodji> +   at least as long as the lifetime of SET.  An empty
Dodji> +   TO_FILE means standard input.  If reason is LC_LEAVE, and
Dodji> +   TO_FILE is NULL, then TO_FILE, TO_LINE and SYSP are given their
Dodji> +   natural values considering the file we are returning to.
Dodji> +
Dodji> +   A call to this function can relocate the previous set of
Dodji> +   maps, so any stored line_map pointers should not be used.  */
Dodji> +
Dodji> +const struct line_map *
Dodji> +linemap_add (struct line_maps *set, enum lc_reason reason,
Dodji> +	     unsigned int sysp, const char *to_file, linenum_type to_line)

I would generally prefer for explanatory comments for public functions
to be in the header, not by the implementation.  This is more in keeping
with the current style of the line map code, but I also like it because
it provides a single text for users of the library to read.

Nice job on the comments, btw.

Tom

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

* Re: [PATCH 1/6] Linemap infrastructure for virtual locations
  2011-01-06 16:48   ` Tom Tromey
@ 2011-04-12 14:39     ` Dodji Seketeli
  2011-04-14 14:46       ` Tom Tromey
  0 siblings, 1 reply; 135+ messages in thread
From: Dodji Seketeli @ 2011-04-12 14:39 UTC (permalink / raw)
  To: Tom Tromey; +Cc: gcc-patches, joseph, gdr, lopezibanez

Hello,

Sorry for getting back to this just now, and thank you very much for the
review.  Please find below my reply to your comments.

Tom Tromey <tromey@redhat.com> writes:

> Dodji>    expanded_location xloc;
> Dodji>    if (loc <= BUILTINS_LOCATION)
> Dodji> -    {
> Dodji> -      xloc.file = loc == UNKNOWN_LOCATION ? NULL : _("<built-in>");
> Dodji> +    {      
> Dodji> +      /* If LOC is UNKNOWN_LOCATION and there was a map allocated for
> Dodji> +	 it then XLOC.FILE should be the file path of the map,
> Dodji> +	 i.e. the file path of the main file being compiled.
> Dodji> +	 Otherwise [if we are being called before any line map has
> Dodji> +	 been allocated, e.g. during parsing of commande line
> Dodji> +	 options] if no line map has been allocated yet, then
> Dodji> +	 XLOC.FILE should just be NULL.  */
> Dodji> +      if (loc == UNKNOWN_LOCATION)
> Dodji> +	{
> Dodji> +	  const struct line_map *map =
> Dodji> +	    linemap_lookup (line_table, UNKNOWN_LOCATION);
> Dodji> +	  xloc.file = map != NULL ? LINEMAP_FILE (map) : NULL;
> Dodji> +	}
> 
> I don't understand this.  I thought that UNKNOWN_LOCATION should never
> have an associated file.  This is handled in a funny way: each front end
> is responsible for ensuring that this holds true (this could be cleaned
> up...).
> 
> Am I mistaken about this?  Or is something weird happening after this
> patch?

I was confused.  I realize now this was some papering over what I later
realized was PR preprocessor/48532, that I filed separately along with a
patch.  I have thus removed this hunk from my local copy of the patch.

> 
> Dodji> @@ -883,14 +884,14 @@ static void
> Dodji>  do_line (cpp_reader *pfile)
> Dodji>  {
> Dodji>    const struct line_maps *line_table = pfile->line_table;
> Dodji> -  const struct line_map *map = &line_table->maps[line_table->used - 1];
> Dodji> +  const struct line_map *map = LINEMAPS_LAST_ORDINARY_MAP (line_table);
>  
> Dodji>    /* skip_rest_of_line() may cause line table to be realloc()ed so note down
> Dodji>       sysp right now.  */
>  
> Dodji> -  unsigned char map_sysp = map->sysp;
> Dodji> +  unsigned char map_sysp = map->d.ordinary.sysp;
> Dodji>    const cpp_token *token;
> Dodji> -  const char *new_file = map->to_file;
> Dodji> +  const char *new_file = map->d.ordinary.to_file;
> 
> Did you want to use the accessor macros here?
> If not, why not?

Oops.  Yes of course I wanted to use the accessor macros there.  I have
just done it on my local copy of the patch now.  Thanks.

> 
> Dodji>    /* C99 raised the minimum limit on #line numbers.  */
> Dodji> @@ -945,11 +946,11 @@ static void
> Dodji>  do_linemarker (cpp_reader *pfile)
> Dodji>  {
> Dodji>    const struct line_maps *line_table = pfile->line_table;
> Dodji> -  const struct line_map *map = &line_table->maps[line_table->used - 1];
> Dodji> +  const struct line_map *map = LINEMAPS_LAST_ORDINARY_MAP (line_table);
> Dodji>    const cpp_token *token;
> Dodji> -  const char *new_file = map->to_file;
> Dodji> +  const char *new_file = map->d.ordinary.to_file;
> Dodji>    linenum_type new_lineno;
> Dodji> -  unsigned int new_sysp = map->sysp;
> Dodji> +  unsigned int new_sysp = map->d.ordinary.sysp;
> 
> Likewise.
> 
> Dodji>    if (map != NULL)
> Dodji> -    linemap_line_start (pfile->line_table, map->to_line, 127);
> Dodji> +    linemap_line_start (pfile->line_table, map->d.ordinary.to_line, 127);
> 
> Likewise.
> 
> Dodji> +  int sysp = (LINEMAPS_ORDINARY_HIGHEST_LINE (pfile->line_table) > 1)
> Dodji> +    && (pfile->buffer ? pfile->buffer->sysp : 0);
> 
> The indentation is off here.
> Put another paren after the '=' and emacs will DTRT.

Done, thanks.

> 
> Dodji> +   physicall source locations are called "spelling locations".
> 
> Typo, should be "physical".
> 
> Dodji> +/* A set of chronological line_map structures.  */
> Dodji> +struct GTY(()) line_maps {
> Dodji> +  
> Dodji> +  struct maps_info info_ordinary;
> Dodji> +
> Dodji> +  struct maps_info info_macro;
> 
> One thing to note is that there may be some code that assumes an
> ordering of location values.  If you hand out ordinary and macro
> locations separately (which I think is what is going on), then code
> doing this may break.

Argh.  The reason why I am doing this is to avoid the (huge) increase of
memory usage due to the fact that the presence of a macro map was
triggering the creation of one more standard map.  Given the number of
macros in some TU, that was starting to add up.  We have discussed this
off-line and a possible way to avoid that was to separate the "integer
space" of locations mapped into macro maps from the integer space of
locations mapped into ordinary maps.  Of course, locations from a given
integer space (be it the space of macro tokens locations or ordinary
token location) is ordered.  This also eases the map lookup as a simple
comparisons is enough to know if a given location is of a token coming
from a macro expansion or not.

Now I think I am left with two options, I guess.  Either I find a way to
make the two kind of locations (those mapped into macro maps and those
mapped into ordinary maps) share the same integer space and be ordered,
or, I spot the places in the code that assume an ordering of all
location values and I change that.

Is the location ordering a strong property we want to keep?

> 
> My recollection is that maybe the diagnostic code did this?
> Or maybe it was someplace more obscure :-(
> Did you happen to run across this while working on this patch?

I haven't noticed that with my tests.  But after reading this, I have
found one spot of the in diagnostic_report_diagnostic that does this.  I
don't know yet if there are other places.

> 
> Dodji> -      fname = pfile->line_table->maps[pfile->line_table->used-1].to_file;
> Dodji> +      fname = (LINEMAPS_LAST_ORDINARY_MAP (pfile->line_table))->d.ordinary.to_file;
> 
> Accessor?

Oops, fixed in my local copy of the patch.

> 
> Dodji> +/* Add a mapping of logical source line to physical source file and
> Dodji> +   line number. This function creates an "ordinary map", which is a
> Dodji> +   map that records locations of tokens that are not part of macro
> Dodji> +   replacement-lists present at a macro expansion point.
> Dodji> +
> Dodji> +   The text pointed to by TO_FILE must have a lifetime
> Dodji> +   at least as long as the lifetime of SET.  An empty
> Dodji> +   TO_FILE means standard input.  If reason is LC_LEAVE, and
> Dodji> +   TO_FILE is NULL, then TO_FILE, TO_LINE and SYSP are given their
> Dodji> +   natural values considering the file we are returning to.
> Dodji> +
> Dodji> +   A call to this function can relocate the previous set of
> Dodji> +   maps, so any stored line_map pointers should not be used.  */
> Dodji> +
> Dodji> +const struct line_map *
> Dodji> +linemap_add (struct line_maps *set, enum lc_reason reason,
> Dodji> +	     unsigned int sysp, const char *to_file, linenum_type to_line)
> 
> I would generally prefer for explanatory comments for public functions
> to be in the header, not by the implementation.  This is more in keeping
> with the current style of the line map code, but I also like it because
> it provides a single text for users of the library to read.

Ah.  I did this because:

     - I thought it was in line of the rest of the code of libcpp (and
       GCC).  I didn't realize it was done on purpose in line-map.h
       only.
     - Having the documentation be at the place where etags points you
       to when you look for a function seemed very convenient to me.  -
       It seems to me that having the documentation near the definition
       helps in keeping the document and the definition in sync.

But if you want, I can just move the the comments to the header file,
no problem at all.

-- 
		Dodji

^ permalink raw reply	[flat|nested] 135+ 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; 135+ 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] 135+ messages in thread

* Re: [PATCH 1/6] Linemap infrastructure for virtual locations
  2011-04-12 14:39     ` Dodji Seketeli
@ 2011-04-14 14:46       ` Tom Tromey
  0 siblings, 0 replies; 135+ messages in thread
From: Tom Tromey @ 2011-04-14 14:46 UTC (permalink / raw)
  To: Dodji Seketeli; +Cc: gcc-patches, joseph, gdr, lopezibanez

>>>>> "Dodji" == Dodji Seketeli <dodji@redhat.com> writes:

Tom> One thing to note is that there may be some code that assumes an
Tom> ordering of location values.  If you hand out ordinary and macro
Tom> locations separately (which I think is what is going on), then code
Tom> doing this may break.

[...]
Dodji> Now I think I am left with two options, I guess.  Either I find a way to
Dodji> make the two kind of locations (those mapped into macro maps and those
Dodji> mapped into ordinary maps) share the same integer space and be ordered,
Dodji> or, I spot the places in the code that assume an ordering of all
Dodji> location values and I change that.

Dodji> Is the location ordering a strong property we want to keep?

It doesn't matter to me.  I think it would probably be sufficient if
there were a "cheap-enough" comparison function.

Dodji> I haven't noticed that with my tests.  But after reading this, I have
Dodji> found one spot of the in diagnostic_report_diagnostic that does this.  I
Dodji> don't know yet if there are other places.

Too bad we can't just write operator< :-)

Dodji> But if you want, I can just move the the comments to the header file,
Dodji> no problem at all.

Yeah, please do.

Tom

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

* [PATCH 3/7] Emit macro expansion related diagnostics
       [not found]   ` <cover.1310824120.git.dodji@redhat.com>
@ 2011-07-16 14:38     ` Dodji Seketeli
  2011-08-04 15:32       ` Dodji Seketeli
  2011-07-16 14:38     ` [PATCH 6/7] Kill pedantic warnings on system headers macros Dodji Seketeli
                       ` (5 subsequent siblings)
  6 siblings, 1 reply; 135+ messages in thread
From: Dodji Seketeli @ 2011-07-16 14:38 UTC (permalink / raw)
  To: gcc-patches; +Cc: tromey, gdr, joseph, burnus, charlet, paolo, jason

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

The combination of this patch and the previous ones boostrapped with
--enable-languages=all,ada and passed regression tests on
x86_64-unknown-linux-gnu.

libcpp/

	* include/line-map.h (linemap_expand_location_full): Add an output
	parameter returning the spelling location the input location got
	resolved to, at declaration point ...
	* line-map.c (linemap_expand_location_full): ... and at definition
	point.

gcc/

	* 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.  Fully expand the 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.
	* input.c (expand_location): Adjust call to
	linemap_expand_location_full.
	* toplev.c (general_init): By default, use the new
	virt_loc_aware_diagnostic_finalizer as diagnostic finalizer.

gcc/cp/

	* 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/

	* 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.
	* g++.dg/cpp0x/initlist15.C: Discard errors pointing at multiple
	levels of included files.
	* g++.old-deja/g++.robertl/eb43.C: Likewise.
	* g++.old-deja/g++.robertl/eb79.C: Likewise.
	* gcc.target/i386/sse-vect-types.c: Likewise.
---
 gcc/Makefile.in                                 |    2 +-
 gcc/cp/error.c                                  |    5 +-
 gcc/diagnostic.c                                |   14 +-
 gcc/diagnostic.h                                |    2 +-
 gcc/input.c                                     |    2 +-
 gcc/testsuite/g++.dg/cpp0x/initlist15.C         |    1 +
 gcc/testsuite/g++.old-deja/g++.robertl/eb43.C   |    4 +
 gcc/testsuite/g++.old-deja/g++.robertl/eb79.C   |    4 +
 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/gcc.target/i386/sse-vect-types.c  |    6 +
 gcc/toplev.c                                    |    3 +
 gcc/tree-diagnostic.c                           |  181 ++++++++++++++++++++++-
 gcc/tree-diagnostic.h                           |    3 +-
 libcpp/include/line-map.h                       |   14 ++-
 libcpp/line-map.c                               |    4 +-
 19 files changed, 332 insertions(+), 17 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 47e14fa..e800685 100644
--- a/gcc/Makefile.in
+++ b/gcc/Makefile.in
@@ -2792,7 +2792,7 @@ fold-const.o : fold-const.c $(CONFIG_H) $(SYSTEM_H) coretypes.h $(TM_H) \
    $(GGC_H) $(TM_P_H) langhooks.h $(MD5_H) intl.h $(TARGET_H) \
    $(GIMPLE_H) realmpfr.h $(TREE_FLOW_H)
 diagnostic.o : diagnostic.c $(CONFIG_H) $(SYSTEM_H) coretypes.h \
-   version.h $(INPUT_H) intl.h $(DIAGNOSTIC_H) diagnostic.def
+   version.h $(INPUT_H) intl.h $(DIAGNOSTIC_H) diagnostic.def $(VEC_H)
 opts.o : opts.c $(OPTS_H) $(OPTIONS_H) $(DIAGNOSTIC_CORE_H) $(CONFIG_H) $(SYSTEM_H) \
    coretypes.h $(TM_H) \
    $(DIAGNOSTIC_H) insn-attr-common.h intl.h $(COMMON_TARGET_H) \
diff --git a/gcc/cp/error.c b/gcc/cp/error.c
index b16fce6..964797d 100644
--- a/gcc/cp/error.c
+++ b/gcc/cp/error.c
@@ -2763,7 +2763,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);
@@ -2773,8 +2773,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..c41e88d 100644
--- a/gcc/diagnostic.c
+++ b/gcc/diagnostic.c
@@ -30,6 +30,7 @@ along with GCC; see the file COPYING3.  If not see
 #include "input.h"
 #include "intl.h"
 #include "diagnostic.h"
+#include "vec.h"
 
 #define pedantic_warning_kind(DC)			\
   ((DC)->pedantic_errors ? DK_ERROR : DK_WARNING)
@@ -255,9 +256,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 +266,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_expand_location_full (line_table, where,
+				LRK_MACRO_PARM_REPLACEMENT_POINT,
+				NULL, &map);
+
   if (map && diagnostic_last_module_changed (context, map))
     {
       diagnostic_set_last_module (context, map);
@@ -301,7 +305,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/input.c b/gcc/input.c
index 29e4de1..f3c7bfa 100644
--- a/gcc/input.c
+++ b/gcc/input.c
@@ -57,7 +57,7 @@ expand_location (source_location loc)
 				      (expand_to_expansion_point_p)
 				      ? LRK_MACRO_EXPANSION_POINT
 				      : LRK_SPELLING_LOCATION,
-				      NULL);
+				      NULL, NULL);
     }
   return xloc;
 }
diff --git a/gcc/testsuite/g++.dg/cpp0x/initlist15.C b/gcc/testsuite/g++.dg/cpp0x/initlist15.C
index b75cc81..cca56b1 100644
--- a/gcc/testsuite/g++.dg/cpp0x/initlist15.C
+++ b/gcc/testsuite/g++.dg/cpp0x/initlist15.C
@@ -2,6 +2,7 @@
 
 // Just discard errors pointing at header files
 // { dg-prune-output "include" }
+// { dg-prune-output "        from" }
 
 #include <vector>
 #include <typeinfo>
diff --git a/gcc/testsuite/g++.old-deja/g++.robertl/eb43.C b/gcc/testsuite/g++.old-deja/g++.robertl/eb43.C
index 1dc4328..bd784b1 100644
--- a/gcc/testsuite/g++.old-deja/g++.robertl/eb43.C
+++ b/gcc/testsuite/g++.old-deja/g++.robertl/eb43.C
@@ -6,6 +6,10 @@
 
 // { dg-prune-output "note" }
 
+// Discard errors pointing at header files
+// { dg-prune-output "In file included from" }
+// { dg-prune-output "        from" }
+
 #include <vector>
 #include <algorithm>
 #include <functional>
diff --git a/gcc/testsuite/g++.old-deja/g++.robertl/eb79.C b/gcc/testsuite/g++.old-deja/g++.robertl/eb79.C
index 1c1ad3e..60cc713 100644
--- a/gcc/testsuite/g++.old-deja/g++.robertl/eb79.C
+++ b/gcc/testsuite/g++.old-deja/g++.robertl/eb79.C
@@ -1,5 +1,9 @@
 // { dg-do assemble  }
 // { dg-prune-output "note" }
+
+// Discard errors pointing at header files
+// { dg-prune-output "In file included from" }
+// { dg-prune-output "        from" }
 // Makes bogus x86 assembly code.
 #include <iostream>
 
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/gcc.target/i386/sse-vect-types.c b/gcc/testsuite/gcc.target/i386/sse-vect-types.c
index 9cb6f3e..ce70125 100644
--- a/gcc/testsuite/gcc.target/i386/sse-vect-types.c
+++ b/gcc/testsuite/gcc.target/i386/sse-vect-types.c
@@ -1,6 +1,12 @@
 /* { dg-do compile } */
 /* { dg-options "-O0 -msse2" } */
 
+
+/*
+   Just discard diagnostic prolog about errors in include files
+   { dg-prune-output "In file included from" }
+*/
+
 #include <xmmintrin.h>
 
 __m128d foo1(__m128d z, __m128d  a, int N) { 
diff --git a/gcc/toplev.c b/gcc/toplev.c
index 109325c..9a98c5f 100644
--- a/gcc/toplev.c
+++ b/gcc/toplev.c
@@ -1132,6 +1132,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..6fae644 100644
--- a/gcc/tree-diagnostic.c
+++ b/gcc/tree-diagnostic.c
@@ -35,7 +35,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 +47,182 @@ default_tree_diagnostic_starter (diagnostic_context *context,
   pp_set_prefix (context->printer, diagnostic_build_prefix (context,
 							    diagnostic));
 }
+
+/* Unwind the different macro expansions that lead to the token which
+   location is WHERE and emit diagnostics showing the resulting
+   unwound macro expansion stack.  If TOPMOST_EXP_POINT_MAP is
+   non-null, *TOPMOST_EXP_POINT_MAP is set to the map of the expansion
+   point of the top most macro of the stack.  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 **topmost_exp_point_map)
+{
+  typedef struct 
+  {
+    const struct line_map *map;
+    source_location where;
+  } loc_t;
+
+  const struct line_map *map, *resolved_map;
+  bool unwind = true;
+  source_location resolved_location;
+  unsigned loc_vec_capacity = 0, num_locs = 0;
+  loc_t *loc_vec = NULL;
+  unsigned ix;
+  loc_t loc, *iter;
+
+#define APPEND_LOC_TO_VEC(LOC)						\
+  if (num_locs >= loc_vec_capacity)					\
+    {									\
+      loc_vec_capacity += 4;						\
+      loc_vec = XRESIZEVEC (loc_t, loc_vec, loc_vec_capacity);		\
+    }									\
+  loc_vec[num_locs++] = LOC;
+
+  map = linemap_lookup (line_table, where);
+  if (!linemap_macro_expansion_map_p (map))
+    return;
+
+  /* Let's unwind the stack of macros that got expanded and that led
+     to the token which location is WHERE.  We are going to store the
+     stack into MAP_VEC, so that we can later walk MAP_VEC backward to
+     display a somewhat meaningful trace of the macro expansion
+     history to the user.  Note that the deepest macro expansion is
+     going to be stored at the beginning of MAP_VEC.  */
+  while (unwind)
+    {
+      loc.where = where;
+      loc.map = map;
+
+      APPEND_LOC_TO_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
+	 *definition* of the macro of MAP, that got expanded at
+	 WHERE. This is basically how we go "up" in the stack of
+	 macro expansions that led to WHERE.  */
+      resolved_location =
+	linemap_macro_map_loc_to_def_point (map, where, false);
+      resolved_map = linemap_lookup (line_table, resolved_location);
+
+      /* If the token at RESOLVED_LOCATION [at macro definition point]
+	 is itself inside an expanded macro then we keep unwinding the
+	 expansion stack by tracing the "parent macro" that got expanded
+	 inside the definition of the macro of MAP...  */
+      if (linemap_macro_expansion_map_p (resolved_map))
+	{
+	  where = resolved_location;
+	  map = resolved_map;
+	}
+      else
+	{
+	  /* Otherwise, let's consider the location of the expansion
+	     point of the macro of MAP. Keep in mind that MAP is a
+	     macro expansion map. To get a "normal map" (i.e a non
+	     macro expansion map) and be done with the unwinding, we
+	     must either consider the location of the location
+	     expansion point of the macro or the location of the token
+	     inside the macro definition that got expanded to
+	     WHERE.  */
+	  where =
+	    linemap_macro_map_loc_to_exp_point (map, where);
+	  map = linemap_lookup (line_table, where);
+	}
+      if (!linemap_macro_expansion_map_p (map))
+	unwind = false;
+    }
+
+  if (topmost_exp_point_map)
+    *topmost_exp_point_map = map;
+
+  /* Walk the map_vec and print the macro expansion stack, unless the
+     topmost macro which expansion triggered this stack [assuming the
+     stack grows downwards] was expanded inside a system header.  */
+  if (!LINEMAP_SYSP (resolved_map))
+    for (ix = 0; num_locs && ix < num_locs; ++ix)
+      {
+	source_location resolved_def_loc = 0, resolved_exp_loc = 0;
+	diagnostic_t saved_kind;
+	const char *saved_prefix;
+	source_location saved_location;
+
+	iter = &loc_vec[ix];
+
+	/* 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.  */
+
+	/* Expand the location iter->where into the locus 1/ of the
+	   comment above.  */
+	linemap_expand_location_full (line_table, iter->where,
+				      LRK_MACRO_PARM_REPLACEMENT_POINT,
+				      &resolved_def_loc,
+				      NULL);
+
+	/* Expand the location of the expansion point of the macro
+	   which expansion gave the token at represented by def_loc.
+	   This is the locus 2/ of the earlier comment.  */
+	linemap_expand_location_full (line_table,
+				      MACRO_MAP_EXPANSION_POINT_LOCATION
+				      (iter->map),
+				      LRK_MACRO_PARM_REPLACEMENT_POINT,
+				      &resolved_exp_loc,
+				      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;
+      }
+
+  free (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 a multiply nested macro expansions, 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 */
diff --git a/libcpp/include/line-map.h b/libcpp/include/line-map.h
index 399194c..e04aa77 100644
--- a/libcpp/include/line-map.h
+++ b/libcpp/include/line-map.h
@@ -827,9 +827,15 @@ expanded_location linemap_expand_location (struct line_maps *,
 
    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.  */
+   LRK_SPELLING_LOCATION.
+
+   Finally, if SPELLING_LOC is not NULL, *RESULTING_LOC is set to the
+   location to which LOC was resolved, and similarly, *LOC_MAP is set
+   to its map.  */
 expanded_location linemap_expand_location_full (struct line_maps *,
-						source_location,
-						enum location_resolution_kind,
-						const struct line_map**);
+						source_location loc,
+						enum location_resolution_kind lrk,
+						source_location *resulting_loc,
+						const struct line_map **loc_map);
+
 #endif /* !LIBCPP_LINE_MAP_H  */
diff --git a/libcpp/line-map.c b/libcpp/line-map.c
index 693ed0b..5bd864d 100644
--- a/libcpp/line-map.c
+++ b/libcpp/line-map.c
@@ -768,6 +768,7 @@ expanded_location
 linemap_expand_location_full (struct line_maps *set,
 			      source_location loc,
 			      enum location_resolution_kind lrk,
+			      source_location *resulting_loc,
 			      const struct line_map **loc_map)
 {
   const struct line_map *map;
@@ -796,6 +797,7 @@ linemap_expand_location_full (struct line_maps *set,
   xloc.sysp = LINEMAP_SYSP (map) != 0;
   if (loc_map)
     *loc_map = map;
-
+  if (resulting_loc)
+    *resulting_loc = loc;
   return xloc;
 }
-- 
1.7.6

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

* [PATCH 0/7] Tracking locations of tokens resulting from macro expansion
  2010-12-10 11:27 [PATCH 0/6] Tracking locations of tokens resulting from macro expansion Dodji Seketeli
                   ` (8 preceding siblings ...)
  2010-12-10 16:59 ` Jeff Law
@ 2011-07-16 14:38 ` Dodji Seketeli
       [not found]   ` <cover.1310824120.git.dodji@redhat.com>
  2011-07-16 16:47   ` [PATCH 0/7] Tracking locations of tokens resulting from macro expansion Tobias Burnus
  9 siblings, 2 replies; 135+ messages in thread
From: Dodji Seketeli @ 2011-07-16 14:38 UTC (permalink / raw)
  To: gcc-patches; +Cc: tromey, gdr, joseph, burnus, charlet, paolo, jason

Hello,

This is an update of the patch set that I initially posted to
http://gcc.gnu.org/ml/gcc-patches/2010-12/msg00858.html.

The main goals achieved by this set are the following:

- Decrease the overall memory consumption.  On the tests I have done
  on a reasonably big C++ program compiled with the previous patch
  set, I have noticed a global memory consumption peak increase of
  13%, for the whole compilation.  With this new patch set, the global
  memory consumption peak is down to 0.6%.  Details of the figures are
  appended at the end of this message.  To do this, I have quite
  heavily modified the patch "Generate virtual locations for tokens"
  and I have added the patch "Reduce memory waste due to
  non-power-of-2 allocs".  The changes of these patches are explained
  in their respective preambles.

- Change the diagnostics output as suggested by Paolo and Gaby in the
  sub-thread http://gcc.gnu.org/ml/gcc-patches/2010-12/msg00987.html.

Besides, I have hopefully addressed most of the comments that were
made in the initial discussion thread.  Gaby suggested that I drop the
-ftrack-macro-expansion flag that I have added to enable this feature.
Now that the memory consumption has been reduce to something that is
more acceptable, I guess this goal is closer now.  However, I have
kept the flag for now as I believe there is value in keeping it to
ease testing and performance comparison measures on real life
examples.  When I am comfortable with those aspects, I'll happily
remove that flag, e.g, after a period of enabling it by default.

Please find below a summary of the patches of this set as well as the
details of the memory consumption gain I measured.

  Linemap infrastructure for virtual locations
  Generate virtual locations for tokens
  Emit macro expansion related diagnostics
  Support -fdebug-cpp option
  Add line map statistics to -fmem-report output
  Kill pedantic warnings on system headers macros
  Reduce memory waste due to non-power-of-2 allocs

 gcc/Makefile.in                                 |    2 +-
 gcc/ada/gcc-interface/trans.c                   |   10 +-
 gcc/c-decl.c                                    |   17 +-
 gcc/c-family/c-lex.c                            |   10 +-
 gcc/c-family/c-opts.c                           |   17 +
 gcc/c-family/c-pch.c                            |    2 +-
 gcc/c-family/c-ppoutput.c                       |   98 ++-
 gcc/c-family/c.opt                              |   12 +
 gcc/c-parser.c                                  |   12 +-
 gcc/c-tree.h                                    |    2 +-
 gcc/cp/error.c                                  |    5 +-
 gcc/diagnostic.c                                |   25 +-
 gcc/diagnostic.h                                |    2 +-
 gcc/doc/cppopts.texi                            |   29 +
 gcc/doc/invoke.texi                             |    6 +-
 gcc/fortran/cpp.c                               |   22 +-
 gcc/input.c                                     |  105 ++-
 gcc/input.h                                     |   22 +-
 gcc/java/jcf-parse.c                            |    2 +-
 gcc/testsuite/g++.dg/cpp0x/initlist15.C         |    1 +
 gcc/testsuite/g++.old-deja/g++.robertl/eb43.C   |    4 +
 gcc/testsuite/g++.old-deja/g++.robertl/eb79.C   |    4 +
 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-1.c  |   32 +
 gcc/testsuite/gcc.dg/cpp/pragma-diagnostic-2.c  |   34 +
 gcc/testsuite/gcc.dg/cpp/syshdr3.c              |   16 +
 gcc/testsuite/gcc.dg/cpp/syshdr3.h              |    7 +
 gcc/testsuite/gcc.dg/nofixed-point-2.c          |    6 +-
 gcc/testsuite/gcc.target/i386/sse-vect-types.c  |    6 +
 gcc/toplev.c                                    |    4 +
 gcc/tree-diagnostic.c                           |  181 +++-
 gcc/tree-diagnostic.h                           |    3 +-
 libcpp/directives-only.c                        |    7 +-
 libcpp/directives.c                             |   21 +-
 libcpp/errors.c                                 |   21 +-
 libcpp/expr.c                                   |  176 ++--
 libcpp/files.c                                  |   24 +-
 libcpp/include/cpp-id-data.h                    |    6 +
 libcpp/include/cpplib.h                         |   15 +-
 libcpp/include/line-map.h                       |  827 +++++++++++++--
 libcpp/init.c                                   |    5 +-
 libcpp/internal.h                               |   60 +-
 libcpp/lex.c                                    |  107 ++-
 libcpp/line-map.c                               |  831 ++++++++++++--
 libcpp/macro.c                                  | 1350 ++++++++++++++++++++---
 libcpp/traditional.c                            |    7 +-
 49 files changed, 3656 insertions(+), 569 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-1.c
 create mode 100644 gcc/testsuite/gcc.dg/cpp/pragma-diagnostic-2.c
 create mode 100644 gcc/testsuite/gcc.dg/cpp/syshdr3.c
 create mode 100644 gcc/testsuite/gcc.dg/cpp/syshdr3.h

Below are the details of the memory consumption I have measured with
both the previous and current versions of this patch set.

[ For abbreviations, there is a legend at the end of the document ]

Memory peak, and GC memory UAEOC increase incurred by macro tokens
location tracking, before the work:

			|pre-processing only	|full compilation|
------------------------|-----------------------|----------------|
% memory peak increase	|	19.9%'		|      8%	 |
% GC memory UAEOC	|	283%		|      9.82%	 |


Memory peak increase incurred by macro tokens location tracking,
after the work:

			|pre-processing only	|full compilation|
------------------------|-----------------------|----------------|
% memory peak increase	|	19.2%'		|      0.6%	 |
% GC memory UAEOC  	|	250.5%		|      4.9%	 |

Number of expanded macros:                     96524
Average number of tokens per macro expansion:  21


                        |C0-p-n |C0-p-t |C0-f-n |C0-f-t |C1-p-n |C1-p-t |C1-f-n |C1-f-t |
----------------------------------------------------------------------------------------
maps allocated		|310K	|21M	|310K	|21M	|255K	|20M	|255K	|20M	|
maps used		|213K	|20M	|213K	|20M	|213K	|20M	|213K	|20M	|
macro maps used		|0	|19M	|0	|19M	|0	|19M	|0	|19M	|
map allocation waste	|374K	|10.7M	|374	|10.7M	|64	|4.35M	|64	|4.35M	|
GC memory used at EOC	|9.9M	|38M	|285M	|313M	|9.7M	|34M	|285M	|299M	|
G.M.U.A.O.E overhead	|140K	|396K	|4M	|4.3M	|140K	|386K	|4M	|4M	|
total GC mem allocation	|11M	|47M	|617M	|653.7M	|10M	|36M	|616.5M	|642.6M	|
total GC mem overhead	|1M	|11M	|54M	|64.31M	|919K	|5.2M	|53.5M	|58M	|
peak memory usage	|166M	|199M	|658M	|711MB	|158,3M	|188.7M	|658M	|662,4M	|


Legend:
=======

C0-p-n: Code base 0 (before this work), for pre-processing only, without
        macro tokens location tracking activated.

C0-p-t: Code base 0 (before this work), for pre-processing only, with
        macro tokens location tracking activated.

C0-f-n: Code base 0 (before this work), for full compilation, without
        macro tokens location tracking activated.

C0-f-t: Code base 0 (before this work), for full compilation, with
        macro tokens location tracking activated.

C1-p-n: Code base 1 (after this work), for pre-processing only,
        without macro tokens location tracking activated.

C1-p-t: Code base 1 (after this work), for pre-processing only, with
        macro tokens location tracking activated.

C1-f-n: Code base 1 (after this work), for full compilation, without
        macro tokens location tracking activated.

C1-f-t: Code base 1 (after this work), for full compilation, with
        macro tokens location tracking activated.

EOC   	      : End of compilation.
G.M.U.A.O.E   : GC memory still used at end of compilation.
UAEOC	      : Used at end of compilation


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

* [PATCH 6/7] Kill pedantic warnings on system headers macros
       [not found]   ` <cover.1310824120.git.dodji@redhat.com>
  2011-07-16 14:38     ` [PATCH 3/7] Emit macro expansion related diagnostics Dodji Seketeli
@ 2011-07-16 14:38     ` Dodji Seketeli
  2011-09-12 22:09       ` Jason Merrill
  2011-07-16 14:39     ` [PATCH 5/7] Add line map statistics to -fmem-report output Dodji Seketeli
                       ` (4 subsequent siblings)
  6 siblings, 1 reply; 135+ messages in thread
From: Dodji Seketeli @ 2011-07-16 14:38 UTC (permalink / raw)
  To: gcc-patches; +Cc: tromey, gdr, joseph, burnus, charlet, paolo, jason

This patch leverages the virtual location infrastructure to avoid
emitting pedantic warnings related to macros defined in system headers
but expanded in normal TUs.

The point is to make diagnostic routines use virtual locations of
tokens instead of their spelling locations. The diagnostic routines in
turn indirectly use linemap_location_in_system_header_p to know if a
given virtual location originated from a system header.

The patch has two main parts.

The libcpp part makes diagnostic routines called from the preprocessor
expression parsing and number conversion code use virtual
locations.

The C FE part makes diagnostic routines called from the type
specifiers validation code use virtual locations.

This fixes the relevant examples presented in the comments of the bug
but I guess, as usual, libcpp and the FEs will need on-going care to
use more and more virtual locations of tokens instead of spelling
locations.

The combination of the patch and the previous ones boostrapped with
--enable-languages=all,ada and passed regression tests on
x86_64-unknown-linux-gnu.

libcpp/

	* include/cpplib.h (cpp_classify_number): Add a location parameter
	to the declaration.
	* internal.h (_cpp_get_prev_token_spelling_loc): Declare.
	* lex.c (_cpp_get_prev_token_spelling_loc): Factorize this from ...
	* errors.c (cpp_diagnostic): ... here.
	* expr.c (SYNTAX_ERROR_AT, SYNTAX_ERROR2_AT): New macros to emit
	syntax error using a virtual location.
	(cpp_classify_number): Add a virtual location parameter. Use
	SYNTAX_ERROR_AT instead of SYNTAX_ERROR, cpp_error_with_line
	instead of cpp_error and cpp_warning_with_line instead of
	cpp_warning. Pass the new virtual location parameter to those
	diagnostic routines.
	(eval_token): Add a virtual location parameter. Pass it down to
	cpp_classify_number. Use cpp_error_with_line instead of cpp_error,
	cpp_warning_with_line instead of cpp_warning, and pass the new
	virtual location parameter to these.
	(_cpp_parse_expr): Use cpp_get_token_with_location instead of
	cpp_get_token, to get the virtual location of the token. Use
	SYNTAX_ERROR2_AT instead of SYNTAX_ERROR2, cpp_error_with_line
	instead of cpp_error. Use the virtual location instead of the
	spelling location.
	* macro.c (maybe_adjust_loc_for_trad_cpp): Define new static
	function.
	(cpp_get_token_with_location): Use it.

gcc/c-family

	* c-lex.c (c_lex_with_flags): Adjust to pass the virtual location
	to cpp_classify_number.

gcc/

	* c-tree.h (finish_declspecs): Add a virtual location parameter.
	* c-decl.c (finish_declspecs): Add a virtual location
	parameter. Use error_at instead of error and pass down the virtual
	location to pewarn and error_at.
	(declspecs_add_type): Use in_system_header_at instead of
	in_system_header.
	* c-parser.c (c_parser_declaration_or_fndef): Pass virtual
	location of the relevant token to finish_declspecs.
	(c_parser_struct_declaration, c_parser_parameter_declaration):
	Likewise.
	(c_parser_type_name): Likewise.

gcc/testsuite/

	* gcc.dg/cpp/syshdr3.h: New test header.
	* gcc.dg/cpp/syshdr3.c: New test file.
	* gcc.dg/nofixed-point-2.c: Adjust to more precise location.
---
 gcc/c-decl.c                           |   17 ++--
 gcc/c-family/c-lex.c                   |    4 +-
 gcc/c-parser.c                         |   12 ++-
 gcc/c-tree.h                           |    2 +-
 gcc/testsuite/gcc.dg/cpp/syshdr3.c     |   16 +++
 gcc/testsuite/gcc.dg/cpp/syshdr3.h     |    7 ++
 gcc/testsuite/gcc.dg/nofixed-point-2.c |    6 +-
 libcpp/errors.c                        |   21 +----
 libcpp/expr.c                          |  176 +++++++++++++++++++-------------
 libcpp/include/cpplib.h                |    3 +-
 libcpp/internal.h                      |    1 +
 libcpp/lex.c                           |   29 +++++
 12 files changed, 183 insertions(+), 111 deletions(-)
 create mode 100644 gcc/testsuite/gcc.dg/cpp/syshdr3.c
 create mode 100644 gcc/testsuite/gcc.dg/cpp/syshdr3.h

diff --git a/gcc/c-decl.c b/gcc/c-decl.c
index 3ed3c46..d2f013b 100644
--- a/gcc/c-decl.c
+++ b/gcc/c-decl.c
@@ -8910,7 +8910,7 @@ declspecs_add_type (location_t loc, struct c_declspecs *specs,
 	      break;
 	    case RID_COMPLEX:
 	      dupe = specs->complex_p;
-	      if (!flag_isoc99 && !in_system_header)
+	      if (!flag_isoc99 && !in_system_header_at (loc))
 		pedwarn (loc, OPT_pedantic,
 			 "ISO C90 does not support complex types");
 	      if (specs->typespec_word == cts_void)
@@ -9433,7 +9433,8 @@ declspecs_add_attrs (struct c_declspecs *specs, tree attrs)
    double".  */
 
 struct c_declspecs *
-finish_declspecs (struct c_declspecs *specs)
+finish_declspecs (struct c_declspecs *specs,
+		  location_t where)
 {
   /* If a type was specified as a whole, we have no modifiers and are
      done.  */
@@ -9458,9 +9459,9 @@ finish_declspecs (struct c_declspecs *specs)
     {
       if (specs->saturating_p)
 	{
-	  error ("%<_Sat%> is used without %<_Fract%> or %<_Accum%>");
+	  error_at (where, "%<_Sat%> is used without %<_Fract%> or %<_Accum%>");
 	  if (!targetm.fixed_point_supported_p ())
-	    error ("fixed-point types not supported for this target");
+	    error_at (where, "fixed-point types not supported for this target");
 	  specs->typespec_word = cts_fract;
 	}
       else if (specs->long_p || specs->short_p
@@ -9471,7 +9472,7 @@ finish_declspecs (struct c_declspecs *specs)
       else if (specs->complex_p)
 	{
 	  specs->typespec_word = cts_double;
-	  pedwarn (input_location, OPT_pedantic,
+	  pedwarn (where, OPT_pedantic,
 		   "ISO C does not support plain %<complex%> meaning "
 		   "%<double complex%>");
 	}
@@ -9516,7 +9517,7 @@ finish_declspecs (struct c_declspecs *specs)
 	specs->type = char_type_node;
       if (specs->complex_p)
 	{
-	  pedwarn (input_location, OPT_pedantic,
+	  pedwarn (where, OPT_pedantic,
 		   "ISO C does not support complex integer types");
 	  specs->type = build_complex_type (specs->type);
 	}
@@ -9529,7 +9530,7 @@ finish_declspecs (struct c_declspecs *specs)
 		     : int128_integer_type_node);
       if (specs->complex_p)
 	{
-	  pedwarn (input_location, OPT_pedantic,
+	  pedwarn (where, OPT_pedantic,
 		   "ISO C does not support complex integer types");
 	  specs->type = build_complex_type (specs->type);
 	}
@@ -9555,7 +9556,7 @@ finish_declspecs (struct c_declspecs *specs)
 		       : integer_type_node);
       if (specs->complex_p)
 	{
-	  pedwarn (input_location, OPT_pedantic,
+	  pedwarn (where, OPT_pedantic,
 		   "ISO C does not support complex integer types");
 	  specs->type = build_complex_type (specs->type);
 	}
diff --git a/gcc/c-family/c-lex.c b/gcc/c-family/c-lex.c
index be83b61..ca088a8 100644
--- a/gcc/c-family/c-lex.c
+++ b/gcc/c-family/c-lex.c
@@ -314,7 +314,7 @@ c_lex_with_flags (tree *value, location_t *loc, unsigned char *cpp_flags,
 
     case CPP_NUMBER:
       {
-	unsigned int flags = cpp_classify_number (parse_in, tok);
+	unsigned int flags = cpp_classify_number (parse_in, tok, *loc);
 
 	switch (flags & CPP_N_CATEGORY)
 	  {
@@ -397,7 +397,7 @@ c_lex_with_flags (tree *value, location_t *loc, unsigned char *cpp_flags,
 
 	*cpp_spell_token (parse_in, tok, name, true) = 0;
 
-	error ("stray %qs in program", name);
+	error_at (*loc, "stray %qs in program", name);
       }
 
       goto retry;
diff --git a/gcc/c-parser.c b/gcc/c-parser.c
index 65966a9..7c5755d 100644
--- a/gcc/c-parser.c
+++ b/gcc/c-parser.c
@@ -1459,7 +1459,7 @@ c_parser_declaration_or_fndef (c_parser *parser, bool fndef_ok,
       c_parser_skip_to_end_of_block_or_statement (parser);
       return;
     }
-  finish_declspecs (specs);
+  finish_declspecs (specs, here);
   if (c_parser_next_token_is (parser, CPP_SEMICOLON))
     {
       if (empty_ok)
@@ -2555,7 +2555,7 @@ c_parser_struct_declaration (c_parser *parser)
       c_parser_error (parser, "expected specifier-qualifier-list");
       return NULL_TREE;
     }
-  finish_declspecs (specs);
+  finish_declspecs (specs, decl_loc);
   if (c_parser_next_token_is (parser, CPP_SEMICOLON)
       || c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
     {
@@ -3218,6 +3218,8 @@ c_parser_parameter_declaration (c_parser *parser, tree attrs)
   tree prefix_attrs;
   tree postfix_attrs = NULL_TREE;
   bool dummy = false;
+  location_t here = c_parser_peek_token (parser)->location;
+
   if (!c_parser_next_token_starts_declspecs (parser))
     {
       c_token *token = c_parser_peek_token (parser);
@@ -3244,7 +3246,7 @@ c_parser_parameter_declaration (c_parser *parser, tree attrs)
       attrs = NULL_TREE;
     }
   c_parser_declspecs (parser, specs, true, true, true, cla_nonabstract_decl);
-  finish_declspecs (specs);
+  finish_declspecs (specs, here);
   pending_xref_error ();
   prefix_attrs = specs->attrs;
   specs->attrs = NULL_TREE;
@@ -3536,6 +3538,8 @@ c_parser_type_name (c_parser *parser)
   struct c_declarator *declarator;
   struct c_type_name *ret;
   bool dummy = false;
+  location_t here = c_parser_peek_token (parser)->location;
+
   c_parser_declspecs (parser, specs, false, true, true, cla_prefer_type);
   if (!specs->declspecs_seen_p)
     {
@@ -3545,7 +3549,7 @@ c_parser_type_name (c_parser *parser)
   if (specs->type != error_mark_node)
     {
       pending_xref_error ();
-      finish_declspecs (specs);
+      finish_declspecs (specs, here);
     }
   declarator = c_parser_declarator (parser,
 				    specs->typespec_kind != ctsk_none,
diff --git a/gcc/c-tree.h b/gcc/c-tree.h
index 95a0ba2..a5deb05 100644
--- a/gcc/c-tree.h
+++ b/gcc/c-tree.h
@@ -492,7 +492,7 @@ extern struct c_declspecs *declspecs_add_scspec (struct c_declspecs *, tree);
 extern struct c_declspecs *declspecs_add_attrs (struct c_declspecs *, tree);
 extern struct c_declspecs *declspecs_add_addrspace (struct c_declspecs *,
 						    addr_space_t);
-extern struct c_declspecs *finish_declspecs (struct c_declspecs *);
+extern struct c_declspecs *finish_declspecs (struct c_declspecs *, location_t);
 
 /* in c-objc-common.c */
 extern bool c_objc_common_init (void);
diff --git a/gcc/testsuite/gcc.dg/cpp/syshdr3.c b/gcc/testsuite/gcc.dg/cpp/syshdr3.c
new file mode 100644
index 0000000..8850410
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/cpp/syshdr3.c
@@ -0,0 +1,16 @@
+/* Contributed by Dodji Seketeli <dodji@redhat.com> */
+/* Origin: PR preprocessor/7263 */
+/* { dg-options "-pedantic -std=c89 -ftrack-macro-expansion=1" } */
+/* { dg-do compile } */
+
+/* This tests the proprer suppression of warning coming from macro
+   defined in system headers and expanded in a non-system header
+   location.  */
+#include "syshdr3.h"
+
+_Complex c = _Complex_I + _Complex_I; /* These macros are defined in
+					 system header so we should
+					 have no warning here.  */
+U_LL u = ONE_ULL; /* Likewise here.  */
+
+unsigned long long v = 1ULL; /* { dg-warning "long long" } */
diff --git a/gcc/testsuite/gcc.dg/cpp/syshdr3.h b/gcc/testsuite/gcc.dg/cpp/syshdr3.h
new file mode 100644
index 0000000..e5d502a
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/cpp/syshdr3.h
@@ -0,0 +1,7 @@
+#pragma GCC system_header
+
+#define _Complex __complex__
+#define _Complex_I 1.0iF
+
+#define U_LL unsigned long long
+#define ONE_ULL 1ULL
diff --git a/gcc/testsuite/gcc.dg/nofixed-point-2.c b/gcc/testsuite/gcc.dg/nofixed-point-2.c
index 5b2f209..8442a19 100644
--- a/gcc/testsuite/gcc.dg/nofixed-point-2.c
+++ b/gcc/testsuite/gcc.dg/nofixed-point-2.c
@@ -20,10 +20,10 @@ f3 (void)
   return 0k;			/* { dg-error "not supported" "reject fixed-point" } */
 }
 
-_Sat
-f4 (void)			/* { dg-error "not supported" "reject fixed-point" } */
+_Sat                            /* { dg-error "not supported" "reject fixed-point" } */
+f4 (void)
 {
   return 0k;			/* { dg-error "not supported" "reject fixed-point" } */
 }
 
-/* { dg-error "is used without" "" { target *-*-* } 24 } */
+/* { dg-error "is used without" "" { target *-*-* } 23 } */
diff --git a/libcpp/errors.c b/libcpp/errors.c
index 13804f0..4139183 100644
--- a/libcpp/errors.c
+++ b/libcpp/errors.c
@@ -38,26 +38,7 @@ cpp_diagnostic (cpp_reader * pfile, int level, int reason,
   source_location src_loc;
   bool ret;
 
-  if (CPP_OPTION (pfile, traditional))
-    {
-      if (pfile->state.in_directive)
-	src_loc = pfile->directive_line;
-      else
-	src_loc = LINEMAPS_ORDINARY_HIGHEST_LINE (pfile->line_table);
-    }
-  /* We don't want to refer to a token before the beginning of the
-     current run -- that is invalid.  */
-  else if (pfile->cur_token == pfile->cur_run->base)
-    {
-      if (pfile->cur_run->prev != NULL)
-	src_loc = pfile->cur_run->prev->limit->src_loc;
-      else
-	src_loc = 0;
-    }
-  else
-    {
-      src_loc = pfile->cur_token[-1].src_loc;
-    }
+  src_loc = _cpp_get_prev_token_spelling_loc (pfile);
 
   if (!pfile->cb.error)
     abort ();
diff --git a/libcpp/expr.c b/libcpp/expr.c
index 3c36127..953dbc5 100644
--- a/libcpp/expr.c
+++ b/libcpp/expr.c
@@ -59,7 +59,7 @@ static cpp_num num_rshift (cpp_num, size_t, size_t);
 
 static cpp_num append_digit (cpp_num, int, int, size_t);
 static cpp_num parse_defined (cpp_reader *);
-static cpp_num eval_token (cpp_reader *, const cpp_token *);
+static cpp_num eval_token (cpp_reader *, const cpp_token *, source_location);
 static struct op *reduce (cpp_reader *, struct op *, enum cpp_ttype);
 static unsigned int interpret_float_suffix (const uchar *, size_t);
 static unsigned int interpret_int_suffix (const uchar *, size_t);
@@ -76,6 +76,12 @@ static void check_promotion (cpp_reader *, const struct op *);
 #define SYNTAX_ERROR2(msgid, arg) \
   do { cpp_error (pfile, CPP_DL_ERROR, msgid, arg); goto syntax_error; } \
   while(0)
+#define SYNTAX_ERROR_AT(loc, msgid) \
+  do { cpp_error_with_line (pfile, CPP_DL_ERROR, (loc), 0, msgid); goto syntax_error; } \
+  while(0)
+#define SYNTAX_ERROR2_AT(loc, msgid, arg)					\
+  do { cpp_error_with_line (pfile, CPP_DL_ERROR, (loc), 0, msgid, arg); goto syntax_error; } \
+  while(0)
 
 /* Subroutine of cpp_classify_number.  S points to a float suffix of
    length LEN, possibly zero.  Returns 0 for an invalid suffix, or a
@@ -223,7 +229,8 @@ interpret_int_suffix (const uchar *s, size_t len)
    floating point, or invalid), radix (decimal, octal, hexadecimal),
    and type suffixes.  */
 unsigned int
-cpp_classify_number (cpp_reader *pfile, const cpp_token *token)
+cpp_classify_number (cpp_reader *pfile, const cpp_token *token,
+		     source_location virtual_location)
 {
   const uchar *str = token->val.str.text;
   const uchar *limit;
@@ -279,7 +286,8 @@ cpp_classify_number (cpp_reader *pfile, const cpp_token *token)
 	  if (float_flag == NOT_FLOAT)
 	    float_flag = AFTER_POINT;
 	  else
-	    SYNTAX_ERROR ("too many decimal points in number");
+	    SYNTAX_ERROR_AT (virtual_location,
+			     "too many decimal points in number");
 	}
       else if ((radix <= 10 && (c == 'e' || c == 'E'))
 	       || (radix == 16 && (c == 'p' || c == 'P')))
@@ -307,8 +315,8 @@ cpp_classify_number (cpp_reader *pfile, const cpp_token *token)
 	    radix = 10;
 
 	  if (CPP_PEDANTIC (pfile))
-	    cpp_error (pfile, CPP_DL_PEDWARN,
-		       "fixed-point constants are a GCC extension");
+	    cpp_error_with_line (pfile, CPP_DL_PEDWARN, virtual_location, 0,
+				 "fixed-point constants are a GCC extension");
 	  goto syntax_ok;
 	}
       else
@@ -321,26 +329,29 @@ cpp_classify_number (cpp_reader *pfile, const cpp_token *token)
   if (max_digit >= radix)
     {
       if (radix == 2)
-	SYNTAX_ERROR2 ("invalid digit \"%c\" in binary constant", '0' + max_digit);
+	SYNTAX_ERROR2_AT (virtual_location,
+			  "invalid digit \"%c\" in binary constant", '0' + max_digit);
       else
-	SYNTAX_ERROR2 ("invalid digit \"%c\" in octal constant", '0' + max_digit);
+	SYNTAX_ERROR2_AT (virtual_location,
+			  "invalid digit \"%c\" in octal constant", '0' + max_digit);
     }
 
   if (float_flag != NOT_FLOAT)
     {
       if (radix == 2)
 	{
-	  cpp_error (pfile, CPP_DL_ERROR,
-		     "invalid prefix \"0b\" for floating constant");
+	  cpp_error_with_line (pfile, CPP_DL_ERROR, virtual_location, 0,
+			       "invalid prefix \"0b\" for floating constant");
 	  return CPP_N_INVALID;
 	}
 
       if (radix == 16 && !seen_digit)
-	SYNTAX_ERROR ("no digits in hexadecimal floating constant");
+	SYNTAX_ERROR_AT (virtual_location,
+			 "no digits in hexadecimal floating constant");
 
       if (radix == 16 && CPP_PEDANTIC (pfile) && !CPP_OPTION (pfile, c99))
-	cpp_error (pfile, CPP_DL_PEDWARN,
-		   "use of C99 hexadecimal floating constant");
+	cpp_error_with_line (pfile, CPP_DL_PEDWARN, virtual_location, 0,
+			     "use of C99 hexadecimal floating constant");
 
       if (float_flag == AFTER_EXPON)
 	{
@@ -349,21 +360,22 @@ cpp_classify_number (cpp_reader *pfile, const cpp_token *token)
 
 	  /* Exponent is decimal, even if string is a hex float.  */
 	  if (!ISDIGIT (*str))
-	    SYNTAX_ERROR ("exponent has no digits");
+	    SYNTAX_ERROR_AT (virtual_location, "exponent has no digits");
 
 	  do
 	    str++;
 	  while (ISDIGIT (*str));
 	}
       else if (radix == 16)
-	SYNTAX_ERROR ("hexadecimal floating constants require an exponent");
+	SYNTAX_ERROR_AT (virtual_location,
+			 "hexadecimal floating constants require an exponent");
 
       result = interpret_float_suffix (str, limit - str);
       if (result == 0)
 	{
-	  cpp_error (pfile, CPP_DL_ERROR,
-		     "invalid suffix \"%.*s\" on floating constant",
-		     (int) (limit - str), str);
+	  cpp_error_with_line (pfile, CPP_DL_ERROR, virtual_location, 0,
+			       "invalid suffix \"%.*s\" on floating constant",
+			       (int) (limit - str), str);
 	  return CPP_N_INVALID;
 	}
 
@@ -371,33 +383,33 @@ cpp_classify_number (cpp_reader *pfile, const cpp_token *token)
       if (limit != str
 	  && CPP_WTRADITIONAL (pfile)
 	  && ! cpp_sys_macro_p (pfile))
-	cpp_warning (pfile, CPP_W_TRADITIONAL,
-		     "traditional C rejects the \"%.*s\" suffix",
-		     (int) (limit - str), str);
+	cpp_warning_with_line (pfile, CPP_W_TRADITIONAL, virtual_location, 0,
+			       "traditional C rejects the \"%.*s\" suffix",
+			       (int) (limit - str), str);
 
       /* A suffix for double is a GCC extension via decimal float support.
 	 If the suffix also specifies an imaginary value we'll catch that
 	 later.  */
       if ((result == CPP_N_MEDIUM) && CPP_PEDANTIC (pfile))
-	cpp_error (pfile, CPP_DL_PEDWARN,
-		   "suffix for double constant is a GCC extension");
+	cpp_error_with_line (pfile, CPP_DL_PEDWARN, virtual_location, 0,
+			     "suffix for double constant is a GCC extension");
 
       /* Radix must be 10 for decimal floats.  */
       if ((result & CPP_N_DFLOAT) && radix != 10)
         {
-          cpp_error (pfile, CPP_DL_ERROR,
-                     "invalid suffix \"%.*s\" with hexadecimal floating constant",
-                     (int) (limit - str), str);
+          cpp_error_with_line (pfile, CPP_DL_ERROR, virtual_location, 0,
+			       "invalid suffix \"%.*s\" with hexadecimal floating constant",
+			       (int) (limit - str), str);
           return CPP_N_INVALID;
         }
 
       if ((result & (CPP_N_FRACT | CPP_N_ACCUM)) && CPP_PEDANTIC (pfile))
-	cpp_error (pfile, CPP_DL_PEDWARN,
-		   "fixed-point constants are a GCC extension");
+	cpp_error_with_line (pfile, CPP_DL_PEDWARN, virtual_location, 0,
+			     "fixed-point constants are a GCC extension");
 
       if ((result & CPP_N_DFLOAT) && CPP_PEDANTIC (pfile))
-	cpp_error (pfile, CPP_DL_PEDWARN,
-		   "decimal float constants are a GCC extension");
+	cpp_error_with_line (pfile, CPP_DL_PEDWARN, virtual_location, 0,
+			     "decimal float constants are a GCC extension");
 
       result |= CPP_N_FLOATING;
     }
@@ -406,9 +418,9 @@ cpp_classify_number (cpp_reader *pfile, const cpp_token *token)
       result = interpret_int_suffix (str, limit - str);
       if (result == 0)
 	{
-	  cpp_error (pfile, CPP_DL_ERROR,
-		     "invalid suffix \"%.*s\" on integer constant",
-		     (int) (limit - str), str);
+	  cpp_error_with_line (pfile, CPP_DL_ERROR, virtual_location, 0,
+			       "invalid suffix \"%.*s\" on integer constant",
+			       (int) (limit - str), str);
 	  return CPP_N_INVALID;
 	}
 
@@ -421,9 +433,10 @@ cpp_classify_number (cpp_reader *pfile, const cpp_token *token)
 		       && CPP_OPTION (pfile, cpp_warn_long_long);
 
 	  if (u_or_i || large)
-	    cpp_warning (pfile, large ? CPP_W_LONG_LONG : CPP_W_TRADITIONAL,
-		         "traditional C rejects the \"%.*s\" suffix",
-		         (int) (limit - str), str);
+	    cpp_warning_with_line (pfile, large ? CPP_W_LONG_LONG : CPP_W_TRADITIONAL,
+				   virtual_location, 0,
+				   "traditional C rejects the \"%.*s\" suffix",
+				   (int) (limit - str), str);
 	}
 
       if ((result & CPP_N_WIDTH) == CPP_N_LARGE
@@ -434,9 +447,11 @@ cpp_classify_number (cpp_reader *pfile, const cpp_token *token)
 		                : N_("use of C99 long long integer constant");
 
 	  if (CPP_OPTION (pfile, c99))
-            cpp_warning (pfile, CPP_W_LONG_LONG, message);
+            cpp_warning_with_line (pfile, CPP_W_LONG_LONG, virtual_location,
+				   0, message);
           else
-            cpp_pedwarning (pfile, CPP_W_LONG_LONG, message);
+            cpp_pedwarning_with_line (pfile, CPP_W_LONG_LONG,
+				      virtual_location, 0, message);
         }
 
       result |= CPP_N_INTEGER;
@@ -444,11 +459,11 @@ cpp_classify_number (cpp_reader *pfile, const cpp_token *token)
 
  syntax_ok:
   if ((result & CPP_N_IMAGINARY) && CPP_PEDANTIC (pfile))
-    cpp_error (pfile, CPP_DL_PEDWARN,
-	       "imaginary constants are a GCC extension");
+    cpp_error_with_line (pfile, CPP_DL_PEDWARN, virtual_location, 0,
+			 "imaginary constants are a GCC extension");
   if (radix == 2 && CPP_PEDANTIC (pfile))
-    cpp_error (pfile, CPP_DL_PEDWARN,
-	       "binary constants are a GCC extension");
+    cpp_error_with_line (pfile, CPP_DL_PEDWARN, virtual_location, 0,
+			 "binary constants are a GCC extension");
 
   if (radix == 10)
     result |= CPP_N_DECIMAL;
@@ -736,7 +751,8 @@ parse_defined (cpp_reader *pfile)
    number or character constant, or the result of the "defined" or "#"
    operators).  */
 static cpp_num
-eval_token (cpp_reader *pfile, const cpp_token *token)
+eval_token (cpp_reader *pfile, const cpp_token *token,
+	    source_location virtual_location)
 {
   cpp_num result;
   unsigned int temp;
@@ -748,18 +764,18 @@ eval_token (cpp_reader *pfile, const cpp_token *token)
   switch (token->type)
     {
     case CPP_NUMBER:
-      temp = cpp_classify_number (pfile, token);
+      temp = cpp_classify_number (pfile, token, virtual_location);
       switch (temp & CPP_N_CATEGORY)
 	{
 	case CPP_N_FLOATING:
-	  cpp_error (pfile, CPP_DL_ERROR,
-		     "floating constant in preprocessor expression");
+	  cpp_error_with_line (pfile, CPP_DL_ERROR, virtual_location, 0,
+			       "floating constant in preprocessor expression");
 	  break;
 	case CPP_N_INTEGER:
 	  if (!(temp & CPP_N_IMAGINARY))
 	    return cpp_interpret_integer (pfile, token, temp);
-	  cpp_error (pfile, CPP_DL_ERROR,
-		     "imaginary number in preprocessor expression");
+	  cpp_error_with_line (pfile, CPP_DL_ERROR, virtual_location, 0,
+			       "imaginary number in preprocessor expression");
 	  break;
 
 	case CPP_N_INVALID:
@@ -806,8 +822,9 @@ eval_token (cpp_reader *pfile, const cpp_token *token)
 	  result.high = 0;
 	  result.low = 0;
 	  if (CPP_OPTION (pfile, warn_undef) && !pfile->state.skip_eval)
-	    cpp_warning (pfile, CPP_W_UNDEF, "\"%s\" is not defined",
-		         NODE_NAME (token->val.node.node));
+	    cpp_warning_with_line (pfile, CPP_W_UNDEF, virtual_location, 0,
+				   "\"%s\" is not defined",
+				   NODE_NAME (token->val.node.node));
 	}
       break;
 
@@ -817,11 +834,12 @@ eval_token (cpp_reader *pfile, const cpp_token *token)
 	  /* A pedantic warning takes precedence over a deprecated
 	     warning here.  */
 	  if (CPP_PEDANTIC (pfile))
-	    cpp_error (pfile, CPP_DL_PEDWARN,
-		       "assertions are a GCC extension");
+	    cpp_error_with_line (pfile, CPP_DL_PEDWARN,
+				 virtual_location, 0,
+				 "assertions are a GCC extension");
 	  else if (CPP_OPTION (pfile, cpp_warn_deprecated))
-	    cpp_warning (pfile, CPP_W_DEPRECATED,
-		         "assertions are a deprecated extension");
+	    cpp_warning_with_line (pfile, CPP_W_DEPRECATED, virtual_location, 0,
+				   "assertions are a deprecated extension");
 	}
       _cpp_test_assertion (pfile, &temp);
       result.high = 0;
@@ -923,6 +941,8 @@ _cpp_parse_expr (cpp_reader *pfile, bool is_if)
   struct op *top = pfile->op_stack;
   unsigned int lex_count;
   bool saw_leading_not, want_value = true;
+  source_location virtual_location = 0,
+    prev_virtual_location = _cpp_get_prev_token_spelling_loc (pfile);
 
   pfile->state.skip_eval = 0;
 
@@ -939,9 +959,12 @@ _cpp_parse_expr (cpp_reader *pfile, bool is_if)
       struct op op;
 
       lex_count++;
-      op.token = cpp_get_token (pfile);
+
+      if (virtual_location)
+	prev_virtual_location = virtual_location;
+      op.token = cpp_get_token_with_location (pfile, &virtual_location);
       op.op = op.token->type;
-      op.loc = op.token->src_loc;
+      op.loc = virtual_location;
 
       switch (op.op)
 	{
@@ -954,10 +977,11 @@ _cpp_parse_expr (cpp_reader *pfile, bool is_if)
 	case CPP_NAME:
 	case CPP_HASH:
 	  if (!want_value)
-	    SYNTAX_ERROR2 ("missing binary operator before token \"%s\"",
-			   cpp_token_as_text (pfile, op.token));
+	    SYNTAX_ERROR2_AT (prev_virtual_location,
+			      "missing binary operator before token \"%s\"",
+			      cpp_token_as_text (pfile, op.token));
 	  want_value = false;
-	  top->value = eval_token (pfile, op.token);
+	  top->value = eval_token (pfile, op.token, virtual_location);
 	  continue;
 
 	case CPP_NOT:
@@ -974,8 +998,9 @@ _cpp_parse_expr (cpp_reader *pfile, bool is_if)
 
 	default:
 	  if ((int) op.op <= (int) CPP_EQ || (int) op.op >= (int) CPP_PLUS_EQ)
-	    SYNTAX_ERROR2 ("token \"%s\" is not valid in preprocessor expressions",
-			   cpp_token_as_text (pfile, op.token));
+	    SYNTAX_ERROR2_AT (op.loc,
+			      "token \"%s\" is not valid in preprocessor expressions",
+			      cpp_token_as_text (pfile, op.token));
 	  break;
 	}
 
@@ -983,27 +1008,32 @@ _cpp_parse_expr (cpp_reader *pfile, bool is_if)
       if (optab[op.op].flags & NO_L_OPERAND)
 	{
 	  if (!want_value)
-	    SYNTAX_ERROR2 ("missing binary operator before token \"%s\"",
-			   cpp_token_as_text (pfile, op.token));
+	    SYNTAX_ERROR2_AT (prev_virtual_location,
+			      "missing binary operator before token \"%s\"",
+			      cpp_token_as_text (pfile, op.token));
 	}
       else if (want_value)
 	{
 	  /* We want a number (or expression) and haven't got one.
 	     Try to emit a specific diagnostic.  */
 	  if (op.op == CPP_CLOSE_PAREN && top->op == CPP_OPEN_PAREN)
-	    SYNTAX_ERROR ("missing expression between '(' and ')'");
+	    SYNTAX_ERROR_AT (op.loc,
+			     "missing expression between '(' and ')'");
 
 	  if (op.op == CPP_EOF && top->op == CPP_EOF)
- 	    SYNTAX_ERROR2 ("%s with no expression", is_if ? "#if" : "#elif");
+ 	    SYNTAX_ERROR2_AT (op.loc,
+			      "%s with no expression", is_if ? "#if" : "#elif");
 
  	  if (top->op != CPP_EOF && top->op != CPP_OPEN_PAREN)
- 	    SYNTAX_ERROR2 ("operator '%s' has no right operand",
- 			   cpp_token_as_text (pfile, top->token));
+ 	    SYNTAX_ERROR2_AT (op.loc,
+			      "operator '%s' has no right operand",
+			      cpp_token_as_text (pfile, top->token));
 	  else if (op.op == CPP_CLOSE_PAREN || op.op == CPP_EOF)
 	    /* Complain about missing paren during reduction.  */;
 	  else
-	    SYNTAX_ERROR2 ("operator '%s' has no left operand",
-			   cpp_token_as_text (pfile, op.token));
+	    SYNTAX_ERROR2_AT (op.loc,
+			      "operator '%s' has no left operand",
+			      cpp_token_as_text (pfile, op.token));
 	}
 
       top = reduce (pfile, top, op.op);
@@ -1028,7 +1058,8 @@ _cpp_parse_expr (cpp_reader *pfile, bool is_if)
 	  break;
 	case CPP_COLON:
 	  if (top->op != CPP_QUERY)
-	    SYNTAX_ERROR (" ':' without preceding '?'");
+	    SYNTAX_ERROR_AT (op.loc,
+			     " ':' without preceding '?'");
 	  if (!num_zerop (top[-1].value)) /* Was '?' condition true?  */
 	    pfile->state.skip_eval++;
 	  else
@@ -1045,7 +1076,7 @@ _cpp_parse_expr (cpp_reader *pfile, bool is_if)
 
       top->op = op.op;
       top->token = op.token;
-      top->loc = op.token->src_loc;
+      top->loc = op.loc;
     }
 
   /* The controlling macro expression is only valid if we called lex 3
@@ -1056,8 +1087,9 @@ _cpp_parse_expr (cpp_reader *pfile, bool is_if)
 
   if (top != pfile->op_stack)
     {
-      cpp_error (pfile, CPP_DL_ICE, "unbalanced stack in %s",
-		 is_if ? "#if" : "#elif");
+      cpp_error_with_line (pfile, CPP_DL_ICE, top->loc, 0,
+			   "unbalanced stack in %s",
+			   is_if ? "#if" : "#elif");
     syntax_error:
       return false;  /* Return false on syntax error.  */
     }
diff --git a/libcpp/include/cpplib.h b/libcpp/include/cpplib.h
index 9e1bf67..a295267 100644
--- a/libcpp/include/cpplib.h
+++ b/libcpp/include/cpplib.h
@@ -827,7 +827,8 @@ struct cpp_num
 
 /* Classify a CPP_NUMBER token.  The return value is a combination of
    the flags from the above sets.  */
-extern unsigned cpp_classify_number (cpp_reader *, const cpp_token *);
+extern unsigned cpp_classify_number (cpp_reader *, const cpp_token *,
+				     source_location);
 
 /* Evaluate a token classified as category CPP_N_INTEGER.  */
 extern cpp_num cpp_interpret_integer (cpp_reader *, const cpp_token *,
diff --git a/libcpp/internal.h b/libcpp/internal.h
index 7470508..d7f4c78 100644
--- a/libcpp/internal.h
+++ b/libcpp/internal.h
@@ -645,6 +645,7 @@ 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 *);
+extern source_location _cpp_get_prev_token_spelling_loc (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 4bd8e71..8562805 100644
--- a/libcpp/lex.c
+++ b/libcpp/lex.c
@@ -2877,3 +2877,32 @@ cpp_token_val_index (cpp_token *tok)
       return CPP_TOKEN_FLD_NONE;
     }
 }
+
+/* Return the location of the previous token in the token stream.  */
+
+source_location
+_cpp_get_prev_token_spelling_loc (cpp_reader *pfile)
+{
+  source_location spelling_loc;
+
+  if (CPP_OPTION (pfile, traditional))
+    {
+      if (pfile->state.in_directive)
+	spelling_loc = pfile->directive_line;
+      else
+	spelling_loc = LINEMAPS_ORDINARY_HIGHEST_LINE (pfile->line_table);
+    }
+  /* We don't want to refer to a token before the beginning of the
+     current run -- that is invalid.  */
+  else if (pfile->cur_token == pfile->cur_run->base)
+    {
+      if (pfile->cur_run->prev != NULL)
+	spelling_loc = pfile->cur_run->prev->limit->src_loc;
+      else
+	spelling_loc = 0;
+    }
+  else
+    spelling_loc = pfile->cur_token[-1].src_loc;
+
+  return spelling_loc;
+}
-- 
1.7.6

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

* [PATCH 4/7] Support -fdebug-cpp option
       [not found]   ` <cover.1310824120.git.dodji@redhat.com>
                       ` (2 preceding siblings ...)
  2011-07-16 14:39     ` [PATCH 5/7] Add line map statistics to -fmem-report output Dodji Seketeli
@ 2011-07-16 14:39     ` Dodji Seketeli
  2011-08-21 11:02       ` Alexandre Oliva
  2011-09-12 22:07       ` Jason Merrill
  2011-07-16 15:25     ` [PATCH 2/7] Generate virtual locations for tokens Dodji Seketeli
                       ` (2 subsequent siblings)
  6 siblings, 2 replies; 135+ messages in thread
From: Dodji Seketeli @ 2011-07-16 14:39 UTC (permalink / raw)
  To: gcc-patches; +Cc: tromey, gdr, joseph, burnus, charlet, paolo, jason

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.

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

libcpp/

	* 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/

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

gcc/c-family/

	* 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.
---
 gcc/c-family/c-opts.c     |    4 +++
 gcc/c-family/c-ppoutput.c |   57 ++++++++++++++++++++++++++++++++++++--------
 gcc/c-family/c.opt        |    4 +++
 gcc/doc/cppopts.texi      |   12 +++++++++
 gcc/doc/invoke.texi       |    2 +-
 libcpp/include/cpplib.h   |    4 +++
 libcpp/include/line-map.h |    4 +++
 libcpp/line-map.c         |   35 +++++++++++++++++++++++++++
 8 files changed, 110 insertions(+), 12 deletions(-)

diff --git a/gcc/c-family/c-opts.c b/gcc/c-family/c-opts.c
index d36c396..61fa0ea 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 92e7fd3..4969b9a 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 c543cab..b7e0d69 100644
--- a/gcc/c-family/c.opt
+++ b/gcc/c-family/c.opt
@@ -731,6 +731,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 c75dd70..189c7e3 100644
--- a/gcc/doc/cppopts.texi
+++ b/gcc/doc/cppopts.texi
@@ -583,6 +583,18 @@ 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
+When used with @option{-E}, dump 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 e8aec6c..41b7e04 100644
--- a/gcc/doc/invoke.texi
+++ b/gcc/doc/invoke.texi
@@ -425,7 +425,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 395d2fe..9e1bf67 100644
--- a/libcpp/include/cpplib.h
+++ b/libcpp/include/cpplib.h
@@ -388,6 +388,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 e04aa77..ba937d6 100644
--- a/libcpp/include/line-map.h
+++ b/libcpp/include/line-map.h
@@ -838,4 +838,8 @@ expanded_location linemap_expand_location_full (struct line_maps *,
 						source_location *resulting_loc,
 						const struct line_map **loc_map);
 
+/* 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 5bd864d..2bf743c 100644
--- a/libcpp/line-map.c
+++ b/libcpp/line-map.c
@@ -801,3 +801,38 @@ linemap_expand_location_full (struct line_maps *set,
     *resulting_loc = loc;
   return xloc;
 }
+
+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_macro_loc_to_def_point (set, loc, &map, true);
+  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

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

* [PATCH 5/7] Add line map statistics to -fmem-report output
       [not found]   ` <cover.1310824120.git.dodji@redhat.com>
  2011-07-16 14:38     ` [PATCH 3/7] Emit macro expansion related diagnostics Dodji Seketeli
  2011-07-16 14:38     ` [PATCH 6/7] Kill pedantic warnings on system headers macros Dodji Seketeli
@ 2011-07-16 14:39     ` Dodji Seketeli
  2011-09-12 22:07       ` Jason Merrill
  2011-07-16 14:39     ` [PATCH 4/7] Support -fdebug-cpp option Dodji Seketeli
                       ` (3 subsequent siblings)
  6 siblings, 1 reply; 135+ messages in thread
From: Dodji Seketeli @ 2011-07-16 14:39 UTC (permalink / raw)
  To: gcc-patches; +Cc: tromey, gdr, joseph, burnus, charlet, paolo, jason

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.

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

gcc/
	* 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/
	* 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/input.c               |   83 +++++++++++++++++++++++++++++++++++++++++++++
 gcc/input.h               |    2 +
 gcc/toplev.c              |    1 +
 libcpp/include/line-map.h |   17 +++++++++
 libcpp/line-map.c         |   62 +++++++++++++++++++++++++++++++++
 libcpp/macro.c            |   29 +++++++++++++--
 6 files changed, 190 insertions(+), 4 deletions(-)

diff --git a/gcc/input.c b/gcc/input.c
index f3c7bfa..614db3e 100644
--- a/gcc/input.c
+++ b/gcc/input.c
@@ -61,3 +61,86 @@ 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)
+
+/* Counters defined in libcpp's macro.c.  */
+extern unsigned num_expanded_macros_counter;
+extern unsigned num_macro_tokens_counter;
+
+/* 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;
+
+  memset (&s, 0, sizeof (s));
+
+  fprintf (stderr, "Number of expanded macros:                     %u\n",
+	   num_expanded_macros_counter);  
+  if (num_expanded_macros_counter != 0)
+    fprintf (stderr, "Average number of tokens per macro expansion:  %u\n",
+	     num_macro_tokens_counter / num_expanded_macros_counter);
+  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 9a98c5f..6626e7a 100644
--- a/gcc/toplev.c
+++ b/gcc/toplev.c
@@ -1788,6 +1788,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 ba937d6..58ad98c 100644
--- a/libcpp/include/line-map.h
+++ b/libcpp/include/line-map.h
@@ -837,6 +837,23 @@ expanded_location linemap_expand_location_full (struct line_maps *,
 						enum location_resolution_kind lrk,
 						source_location *resulting_loc,
 						const struct line_map **loc_map);
+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;
+};
+
+/* 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.  */
diff --git a/libcpp/line-map.c b/libcpp/line-map.c
index 2bf743c..d69160c 100644
--- a/libcpp/line-map.c
+++ b/libcpp/line-map.c
@@ -836,3 +836,65 @@ 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);
 }
+
+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;
+}
diff --git a/libcpp/macro.c b/libcpp/macro.c
index 936e1b4..e4f941b 100644
--- a/libcpp/macro.c
+++ b/libcpp/macro.c
@@ -173,6 +173,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
@@ -1088,10 +1095,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)
@@ -1101,13 +1113,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;
@@ -1706,6 +1723,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.  */
@@ -2250,6 +2269,8 @@ cpp_get_token_1 (cpp_reader *pfile, source_location *location)
 	}
       else
 	{
+	  if (pfile->context->macro)
+	    ++num_expanded_macros_counter;
 	  _cpp_pop_context (pfile);
 	  if (pfile->state.in_directive)
 	    continue;
-- 
1.7.6

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

* [PATCH 2/7] Generate virtual locations for tokens
       [not found]   ` <cover.1310824120.git.dodji@redhat.com>
                       ` (3 preceding siblings ...)
  2011-07-16 14:39     ` [PATCH 4/7] Support -fdebug-cpp option Dodji Seketeli
@ 2011-07-16 15:25     ` Dodji Seketeli
  2011-08-09 15:30       ` Dodji Seketeli
  2011-07-16 15:28     ` [PATCH 1/7] Linemap infrastructure for virtual locations Dodji Seketeli
  2011-07-16 15:34     ` [PATCH 7/7] Reduce memory waste due to non-power-of-2 allocs Dodji Seketeli
  6 siblings, 1 reply; 135+ messages in thread
From: Dodji Seketeli @ 2011-07-16 15:25 UTC (permalink / raw)
  To: gcc-patches; +Cc: tromey, gdr, joseph, burnus, charlet, paolo, jason

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.

expand_location has been modified to resolve virtual locations to
spelling locations -- when macro expansion tracking is turned
on. Otherwise the behaviour is unchanged compared to before.

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

The combination of this patch and the previous one bootstraps with
	--enable-languages=all,ada and passes regression tests on
x86_64-unknown-linux-gnu.

gcc/
	* 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/

	* 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/

	* 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>: Change the type of this to void.
	(struct cpp_context)<direct_p>: Remove.
	(_cpp_remaining_tokens_num_in_context): Declare new function.
	* 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)<expanded_capacity, virt_locs>:
	(struct macro_arg)<virt_locs_capacity, 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_args_mem, ensure_expanded_args_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_append_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.
	(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_args_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 and
	maybe_adjust_loc_for_trad_cpp.
	(_cpp_backup_tokens): Support the new kinds of token contexts.
---
 gcc/c-family/c-opts.c   |   13 +
 gcc/c-family/c.opt      |    8 +
 gcc/doc/cppopts.texi    |   17 +
 gcc/doc/invoke.texi     |    6 +-
 libcpp/include/cpplib.h |    8 +
 libcpp/init.c           |    1 +
 libcpp/internal.h       |   54 ++-
 libcpp/lex.c            |   41 ++-
 libcpp/macro.c          | 1308 ++++++++++++++++++++++++++++++++++++++++++-----
 9 files changed, 1321 insertions(+), 135 deletions(-)

diff --git a/gcc/c-family/c-opts.c b/gcc/c-family/c-opts.c
index 5cf58ac..d36c396 100644
--- a/gcc/c-family/c-opts.c
+++ b/gcc/c-family/c-opts.c
@@ -628,6 +628,19 @@ 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;
+      goto ftrack_macro_expansion_with_arg;
+
+    case OPT_ftrack_macro_expansion_:
+    ftrack_macro_expansion_with_arg:
+      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 00bdd93..c543cab 100644
--- a/gcc/c-family/c.opt
+++ b/gcc/c-family/c.opt
@@ -933,6 +933,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..c75dd70 100644
--- a/gcc/doc/cppopts.texi
+++ b/gcc/doc/cppopts.texi
@@ -583,6 +583,23 @@ 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. It
+is the default value.
+
 @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 7541e3a..e8aec6c 100644
--- a/gcc/doc/invoke.texi
+++ b/gcc/doc/invoke.texi
@@ -425,9 +425,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/libcpp/include/cpplib.h b/libcpp/include/cpplib.h
index 55b0f1b..395d2fe 100644
--- a/libcpp/include/cpplib.h
+++ b/libcpp/include/cpplib.h
@@ -389,6 +389,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. all macro
+     expansion. 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/init.c b/libcpp/init.c
index 3a16d33..a4d4dbd 100644
--- a/libcpp/init.c
+++ b/libcpp/init.c
@@ -152,6 +152,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;
diff --git a/libcpp/internal.h b/libcpp/internal.h
index f02c878..7470508 100644
--- a/libcpp/internal.h
+++ b/libcpp/internal.h
@@ -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,20 @@ 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;
-
-  /* True if utoken element is token, else ptoken.  */
-  bool direct_p;
+  /* If tokens_kind is TOKEN_KIND_EXTENDED, then, if we 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
+     hash_node, 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 TKEN_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.  */
+  void *macro;
+
+  /* This determines the type of tokens held by this context.  */
+  enum context_tokens_kind tokens_kind;
 };
 
 struct lexer_state
@@ -601,6 +644,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 03d63bf..4bd8e71 100644
--- a/libcpp/lex.c
+++ b/libcpp/lex.c
@@ -1714,6 +1714,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)
@@ -1725,15 +1757,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/macro.c b/libcpp/macro.c
index 87ea7f8..936e1b4 100644
--- a/libcpp/macro.c
+++ b/libcpp/macro.c
@@ -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,64 @@ 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.  */
+  size_t expanded_capacity;     /* total size of expanded array.  */
+  source_location *virt_locs;	/* Where virtual locations for
+				   unexpanded tokens are stored.  */
+  unsigned virt_locs_capacity;	/* Total size of virtual locations
+				   array.  */
+  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 line macro
+   argument.  */
+typedef struct macro_arg_token_iter macro_arg_token_iter;
+struct macro_arg_token_iter
+{
+  /* The cpp_reader the macro comes from.  */
+  cpp_reader *pfile;
+  /* The kind of token over which we are supposed to iterate.  */
+  enum macro_arg_token_kind kind;
+  /* The function-like macro the tokens come from.  */
+  const macro_arg *arg;
+  /* 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 +106,56 @@ 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_args_mem (cpp_reader *, macro_arg *, size_t);
+static void ensure_expanded_args_room (cpp_reader *, macro_arg *, size_t);
+static void delete_macro_args (_cpp_buff*, unsigned num_args);
+static void set_arg_token (cpp_reader *, macro_arg *, const cpp_token *,
+			   source_location, size_t,
+			   enum macro_arg_token_kind);
+static const source_location *get_arg_token_location (cpp_reader *,
+						      const macro_arg *,
+						      enum macro_arg_token_kind);
+static const cpp_token **arg_token_ptr_at (cpp_reader *,
+					   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 *, cpp_reader*,
+				       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 (cpp_reader *,
+						   const cpp_token **,
+						   source_location *, 
+						   const cpp_token *,
+						   source_location,
+						   source_location,
+						   const struct line_map *,
+						   unsigned int *);
+
+static const cpp_token **tokens_buff_append_token (cpp_reader *,
+						   _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 +167,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 +613,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 +623,26 @@ 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++;
+	  if (context->macro)
+	    {
+	      macro_context *m = (macro_context *) context->macro;
+	      m->cur_virt_loc++;
+	    }
+	}
 
       if (rhs->type == CPP_PADDING)
 	{
@@ -588,23 +706,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 +751,16 @@ collect_args (cpp_reader *pfile, const cpp_hashnode *node,
     {
       unsigned int paren_depth = 0;
       unsigned int ntokens = 0;
+      num_args_alloced++;
 
       argc++;
       arg->first = (const cpp_token **) buff->cur;
+      if (track_macro_expansion_p)
+	{
+	  arg->virt_locs_capacity = DEFAULT_NUM_TOKENS_PER_MACRO_ARG;
+	  arg->virt_locs = XNEWVEC (source_location,
+				    arg->virt_locs_capacity);
+	}
 
       for (;;)
 	{
@@ -629,11 +768,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 > arg->virt_locs_capacity))
+	    {
+	      arg->virt_locs_capacity += ARG_TOKENS_EXTENT;
+	      arg->virt_locs = XRESIZEVEC (source_location,
+					   arg->virt_locs,
+					   arg->virt_locs_capacity);
+	    }
 
-	  token = cpp_get_token (pfile);
+	  token = cpp_get_token_1 (pfile, &virt_loc);
 
 	  if (token->type == CPP_PADDING)
 	    {
@@ -690,7 +838,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 +852,9 @@ collect_args (cpp_reader *pfile, const cpp_hashnode *node,
 	      else
 		continue;
 	    }
-
-	  arg->first[ntokens++] = token;
+	  set_arg_token (pfile, arg, token, virt_loc,
+			 ntokens, MACRO_ARG_TOKEN_NORMAL);
+	  ntokens++;
 	}
 
       /* Drop trailing padding.  */
@@ -713,7 +862,8 @@ collect_args (cpp_reader *pfile, const cpp_hashnode *node,
 	ntokens--;
 
       arg->count = ntokens;
-      arg->first[ntokens] = &pfile->eof;
+      set_arg_token (pfile, arg, &pfile->eof, pfile->eof.src_loc,
+		     ntokens, MACRO_ARG_TOKEN_NORMAL);
 
       /* Terminate the argument.  Excess arguments loop back and
 	 overwrite the final legitimate argument, before failing.  */
@@ -756,6 +906,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 +921,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 +943,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 +977,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 +1010,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 +1035,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 +1055,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,
+					  macro, location, count);
+	      for (i = 0; i < count; ++i)
+		{
+		  tokens_buff_append_token (pfile, 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 +1120,311 @@ 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 (cpp_reader *pfile, macro_arg *arg, const cpp_token *token,
+	       source_location location, size_t index,
+	       enum macro_arg_token_kind kind)
+{  
+  const cpp_token **token_ptr;
+  source_location *loc = NULL;
+  bool track_macro_exp_p = CPP_OPTION (pfile, track_macro_expansion);
+
+  token_ptr =
+    arg_token_ptr_at (pfile, 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.  */
+static const source_location *
+get_arg_token_location (cpp_reader *pfile,
+			const macro_arg *arg,
+			enum macro_arg_token_kind kind)
+{
+  source_location *loc = NULL;
+  const cpp_token **token_ptr = arg_token_ptr_at (pfile, arg, 0,
+						  kind, &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 (cpp_reader *pfile, const macro_arg *arg,
+		  size_t index, enum macro_arg_token_kind kind,
+		  source_location **virt_location)
+{
+  bool track_macro_exp_p = CPP_OPTION (pfile, track_macro_expansion);
+  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)
+    return NULL;
+
+  if (virt_location)
+    {
+      if (track_macro_exp_p)
+	{
+	  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;
+	}
+      else
+	*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,
+			   cpp_reader *pfile,
+			   enum macro_arg_token_kind kind,
+			   const macro_arg *arg,
+			   const cpp_token **token_ptr)
+{
+  iter->pfile = pfile;
+  iter->kind = kind;
+  iter->arg = arg;
+  iter->token_ptr = token_ptr;
+  iter->location_ptr = get_arg_token_location (pfile, arg, kind);
+#ifdef ENABLE_CHECKING
+  iter->num_forwards = 0;
+#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)
+{
+  bool track_macro_exp_p = CPP_OPTION (it->pfile,
+				       track_macro_expansion);
+
+  switch (it->kind)
+    {
+    case MACRO_ARG_TOKEN_NORMAL:
+    case MACRO_ARG_TOKEN_EXPANDED:
+      it->token_ptr++;
+      if (track_macro_exp_p)
+	it->location_ptr++;
+      break;
+    case MACRO_ARG_TOKEN_STRINGIFIED:
+#ifdef ENABLE_CHECKING
+      if (it->num_forwards > 0)
+	abort ();
+      it->num_forwards++;
+#endif
+      break;
+    }
+}
+
+/* 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
+  return *it->location_ptr;
+}
+
+/* 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;
+  bool track_macro_exp_p;
 
   /* 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_p <
+	   2).  */
+	exp_count += 2;
 
 	/* We have an argument.  If it is not being stringified or
 	   pasted it is macro-replaced before insertion.  */
@@ -974,67 +1446,222 @@ 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_p = 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, &virt_locs);
+
   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_p)
+    {
+      int num_macro_tokens = total;
+      if (track_macro_exp_p < 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, macro,
+				 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_append_token (pfile, 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, pfile,
+				     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, pfile,
+				     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, pfile,
+				     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, pfile,
+				     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_append_token (pfile, 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_p 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 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_p > 1)
+		token_index += j;
+
+	      index = expanded_token_index (pfile, macro, src, token_index);
+	      tokens_buff_append_token (pfile, 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 +1677,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_append_token (pfile, buff, virt_locs,
+				    t, t->src_loc, t->src_loc,
+				    NULL, NULL);
+	}
 
       /* Add a new paste flag, or remove an unwanted one.  */
       if (paste_flag)
@@ -1064,13 +1696,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_p)
+    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 +1733,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,7 +1750,7 @@ push_ptoken_context (cpp_reader *pfile, cpp_hashnode *macro, _cpp_buff *buff,
 {
   cpp_context *context = next_context (pfile);
 
-  context->direct_p = false;
+  context->tokens_kind = TOKENS_KIND_INDIRECT;
   context->macro = macro;
   context->buff = buff;
   FIRST (context).ptoken = first;
@@ -1126,15 +1762,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->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->macro = 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,7 +1807,7 @@ _cpp_push_text_context (cpp_reader *pfile, cpp_hashnode *macro,
 {
   cpp_context *context = next_context (pfile);
 
-  context->direct_p = true;
+  context->tokens_kind = TOKENS_KIND_DIRECT;
   context->macro = macro;
   context->buff = NULL;
   CUR (context) = start;
@@ -1150,6 +1815,187 @@ _cpp_push_text_context (cpp_reader *pfile, cpp_hashnode *macro,
   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 the
+   -ftrack-macro-expansion flag is in effect and if VIRT_LOCS is
+   non-null, *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)
+{
+  bool track_macro_exp_p = CPP_OPTION (pfile, track_macro_expansion);
+  size_t tokens_size = len * sizeof (cpp_token *);
+  size_t locs_size = len * sizeof (source_location);
+
+  if (track_macro_exp_p && 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 points to where to
+   insert the virtual location of TOKEN; that is, if the flag
+   -ftrack-macro-expansion is in effect.  TOKEN is the token to
+   insert.  DEF_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 PARM_DEF_LOC can
+   just be set to the same value as 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 (cpp_reader *pfile,
+			  const cpp_token **dest,
+			  source_location *virt_loc_dest,
+			  const cpp_token *token,
+			  source_location def_loc,
+			  source_location parm_def_loc,			  
+			  const struct line_map *map,
+			  unsigned int *macro_token_index)
+{
+  bool track_macro_exp_p = CPP_OPTION (pfile, track_macro_expansion);
+  source_location macro_loc = def_loc;
+  const cpp_token **result;
+
+  if (track_macro_exp_p)
+    {
+      if (map)
+	macro_loc = linemap_add_macro_token (map, *macro_token_index,
+					     def_loc, parm_def_loc);
+      *virt_loc_dest = macro_loc;
+    }
+  *dest = token;
+  result = &dest[1];
+
+  return result;
+}
+
+/* Appends a token to the end of the token buffer BUFFER.  Note that
+   this function doesn't enlarge BUFFER; it overwrite the last memory
+   location of BUFFER that holds a token.
+
+   TOKEN is the token to append. DEF_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 PARM_DEF_LOC can just be set to the
+   same value as 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.  This function adds
+   the virtual location DEF_LOC it 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_append_token (cpp_reader *pfile,
+			  _cpp_buff *buffer,
+			  source_location *virt_locs,
+			  const cpp_token *token,
+			  source_location def_loc,
+			  source_location parm_def_loc,
+			  const struct line_map *map,
+			  unsigned int *macro_token_index)
+{
+  const cpp_token **result;
+  unsigned token_index = 
+    (BUFF_FRONT (buffer) - buffer->base) / sizeof (cpp_token *);
+
+  result =
+    tokens_buff_put_token_to (pfile, (const cpp_token **) BUFF_FRONT (buffer),
+			      &virt_locs[token_index],
+			      token, def_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_args_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);
+  arg->expanded_capacity = 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_args_room (cpp_reader *pfile, macro_arg *arg, size_t size)
+{
+  if (size <= arg->expanded_capacity)
+    return;
+
+  size *= 2;
+
+  arg->expanded =
+    XRESIZEVEC (const cpp_token *, arg->expanded, size);
+  arg->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
@@ -1161,8 +2007,10 @@ expand_arg (cpp_reader *pfile, macro_arg *arg)
 {
   unsigned int 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.  */
@@ -1171,26 +2019,32 @@ expand_arg (cpp_reader *pfile, macro_arg *arg)
 
   /* Loop, reading in the arguments.  */
   capacity = 256;
-  arg->expanded = XNEWVEC (const cpp_token *, capacity);
+  alloc_expanded_args_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_args_room (pfile, arg, arg->expanded_count + 1);
 
-      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 (pfile, arg, token, location,
+		     arg->expanded_count, MACRO_ARG_TOKEN_EXPANDED);
+      arg->expanded_count++;
     }
 
   _cpp_pop_context (pfile);
@@ -1199,25 +2053,127 @@ 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;
+    {
+      cpp_hashnode *macro;
+      if (context->tokens_kind == TOKENS_KIND_EXTENDED)
+	{
+	  macro_context *mc = (macro_context *) context->macro;
+	  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->macro = NULL;
+	}
+      else
+	macro = (cpp_hashnode *) context->macro;
+
+      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 = (macro_context *) c->macro;
+      *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 traditionnal 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 traditionnal 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 +2181,45 @@ _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".  This matters when a token results from macro
+   expansion -- the token's location will indicate where the macro is
+   defined (the spelling location of the token) but *LOC will be a
+   virtual location of the token. Virtual location means a location
+   that possibly encodes many types of locus at once. A virtual
+   location can encode the location of a token resulting from macro
+   expansion or not. If the token results from macro expansion its
+   virtual location encodes (at the same time):
+     - the spelling location of the token
+     - the locus of the macro expansion point
+     - the locus the point where the token got instantiated as part of
+       the macro expansion process.
+     (YES, IT ENCODES ALL THESE THREE AT THE SAME TIME! and maybe more.)
+
+   You can learn more about the different locuses encoded in a map by
+   reading the extensive comments of the line_map_macro and line_map
+   structs in line-map.h.  A virtual location, indeed.
+
+   The linemap API can then be used to retrieve the particular locus
+   we are interested in.
+
+   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 operators
+   '<' and '>'.
+
+   Otherwise *LOC is set to the same location as the location carried
+   by the returned token.  */
+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 +2229,23 @@ 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);
+	      if (location)
+		*location = result->src_loc;
+	      return result;
 	    }
 	}
       else
@@ -1261,6 +2253,8 @@ cpp_get_token (cpp_reader *pfile)
 	  _cpp_pop_context (pfile);
 	  if (pfile->state.in_directive)
 	    continue;
+	  if (location)
+	    *location = pfile->avoid_paste.src_loc;
 	  return &pfile->avoid_paste;
 	}
 
@@ -1298,7 +2292,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 +2310,16 @@ 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);
+	      if (location)
+		*location = result->src_loc;
+	      return result;
 	    }
 	}
       else
@@ -1337,27 +2336,79 @@ cpp_get_token (cpp_reader *pfile)
       break;
     }
 
-  return result;
+  if (location)
+    *location = virt_loc;
+  return result;  
+}
+
+/* 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 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.  */
+/* 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 enbled its virtual location encodes (at the
+   same time):
+
+   - the spelling location of the token the locus of the macro
+   - expansion point the locus 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);
+  result = cpp_get_token_1 (pfile, loc);
   if (pfile->context->macro)
-    *loc = pfile->invocation_location;
+    {
+      if (!CPP_OPTION (pfile, track_macro_expansion))
+	*loc = pfile->invocation_location;
+    }
   else
     *loc = result->src_loc;
 
+  *loc = maybe_adjust_loc_for_trad_cpp (pfile, *loc);
   return result;
 }
 
@@ -1367,7 +2418,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 = (cpp_hashnode *) pfile->context->macro;
 
   return node && node->value.macro && node->value.macro->syshdr;
 }
@@ -1424,10 +2475,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->macro)
+	    {
+	      macro_context *m = (macro_context *) pfile->context->macro;
+	      m->cur_virt_loc--;
+#ifdef ENABLE_CHECKING
+	      if (m->cur_virt_loc < m->virt_locs)
+		abort ();
+#endif
+	    }
+	  else
+	    abort ();
+	}
+      else
+	abort ();
     }
 }
 
-- 
1.7.6

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

* [PATCH 1/7] Linemap infrastructure for virtual locations
       [not found]   ` <cover.1310824120.git.dodji@redhat.com>
                       ` (4 preceding siblings ...)
  2011-07-16 15:25     ` [PATCH 2/7] Generate virtual locations for tokens Dodji Seketeli
@ 2011-07-16 15:28     ` Dodji Seketeli
  2011-07-18 22:06       ` Jason Merrill
                         ` (4 more replies)
  2011-07-16 15:34     ` [PATCH 7/7] Reduce memory waste due to non-power-of-2 allocs Dodji Seketeli
  6 siblings, 5 replies; 135+ messages in thread
From: Dodji Seketeli @ 2011-07-16 15:28 UTC (permalink / raw)
  To: gcc-patches; +Cc: tromey, gdr, joseph, burnus, charlet, paolo, jason

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.

Boostrapped with --enable-languages=all,ada and passed regression
tests on x86_unknown-linux-gnu against trunk.

libcpp/

	* include/cpp-id-data.h (struct cpp_macro)<name>: New field for
	diagnostics purposes.
	* 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_MAP_P)
	(LOCATION_POSSIBLY_IN_MACRO_MAP_P, LINEMAPS_MAP_INFO)
	(LINEMAPS_MAPS, LINEMAPS_ALLOCATE, LINEMAPS_USED, LINEMAPS_CACHE)
	(LINEMAPS_HIGHEST_LOCATION, LINEMAPS_HIGHEST_LINE)
	(LINEMAPS_LAST_MAP, LINEMAPS_LAST_ALLOCATED_MAP)
	(LINEMAPS_ORDINARY_MAPS, LINEMAPS_ORDINARY_ALLOCATED)
	(LINEMAPS_ORDINARY_USED, LINEMAPS_ORDINARY_CACHE)
	(LINEMAPS_ORDINARY_HIGHEST_LOCATION)
	(LINEMAPS_ORDINARY_HIGHEST_LINE, LINEMAPS_LAST_ORDINARY_MAP)
	(LINEMAPS_LAST_ALLOCATED_ORDINARY_MAP, LINEMAPS_MACRO_MAPS)
	(LINEMAPS_MACRO_ALLOCATED, LINEMAPS_MACRO_USED)
	(LINEMAPS_MACRO_CACHE, LINEMAPS_MACRO_HIGHEST_LOCATION)
	(LINEMAPS_MACRO_HIGHEST_LINE, 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_init, linemap_free, linemap_add, linemap_lookup)
	(linemap_check_files_exited, linemap_line_start)
	(linemap_position_for_column): Remove useless extern.
	(linemap_position_for_line_and_column)
	(linemap_tracks_macro_expansion_locs_p, linemap_enter_macro)
	(linemap_add_macro_token, linemap_check_ordinary)
	(linemap_macro_expansion_map_p, linemap_macro_loc_to_exp_point)
	(linemap_macro_loc_to_def_point)
	(linemap_macro_map_loc_to_def_point)
	(linemap_macro_map_loc_to_exp_point, linemap_map_get_index)
	(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_expand_location_full, linemap_expand_location): Declare
	new functions.
	* line-map.c: Include cpp-id-data.h
	(linemap_assert): New macro.
	(new_linemap, create_and_add_line_map_internal): Define new static
	functions.  Extracted from ...
	(linemap_add): ... here.
	(linemap_tracks_macro_expansion_locs_p, linemap_enter_macro)
	(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_loc_to_exp_point, linemap_map_get_index)
	(linemap_macro_loc_to_def_point, 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_expand_location_full)
	(linemap_tracks_macro_expansion_locs_p)
	(linemap_position_for_line_and_column, linemap_location_before_p):
	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): Adjust to free what has been allocated.  Use the
	accessors.
	(linemap_line_start): Assert this uses an ordinary map.  Adjust to
	use the new ordinary map accessors and data structures.  Use the
	new MAX_SOURCE_LOCATION constant.
	(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.
	* 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/
	* 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/c-family

	* 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/java/

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

gcc/ada/

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

gcc/fortran/

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

gcc/testsuite/

	* gcc.dg/cpp/pragma-diagnostic-1.c: New test.
---
 gcc/ada/gcc-interface/trans.c                  |   10 +-
 gcc/c-family/c-lex.c                           |    6 +-
 gcc/c-family/c-pch.c                           |    2 +-
 gcc/c-family/c-ppoutput.c                      |   41 +-
 gcc/diagnostic.c                               |   11 +-
 gcc/fortran/cpp.c                              |   22 +-
 gcc/input.c                                    |   22 +-
 gcc/input.h                                    |   20 +-
 gcc/java/jcf-parse.c                           |    2 +-
 gcc/testsuite/gcc.dg/cpp/pragma-diagnostic-1.c |   32 +
 libcpp/directives-only.c                       |    7 +-
 libcpp/directives.c                            |   21 +-
 libcpp/errors.c                                |    2 +-
 libcpp/files.c                                 |   24 +-
 libcpp/include/cpp-id-data.h                   |    6 +
 libcpp/include/line-map.h                      |  800 +++++++++++++++++++++---
 libcpp/init.c                                  |    4 +-
 libcpp/internal.h                              |    5 +-
 libcpp/lex.c                                   |   37 +-
 libcpp/line-map.c                              |  696 +++++++++++++++++----
 libcpp/macro.c                                 |   19 +-
 libcpp/traditional.c                           |    7 +-
 22 files changed, 1495 insertions(+), 301 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 bf533bd..82a6ff1 100644
--- a/gcc/ada/gcc-interface/trans.c
+++ b/gcc/ada/gcc-interface/trans.c
@@ -275,7 +275,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.  */
@@ -7715,12 +7715,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-pch.c b/gcc/c-family/c-pch.c
index b429d9d..9d4f799 100644
--- a/gcc/c-family/c-pch.c
+++ b/gcc/c-family/c-pch.c
@@ -423,7 +423,7 @@ c_common_read_pch (cpp_reader *pfile, const char *name,
     }
 
   /* Save the location and then restore it after reading the PCH.  */
-  saved_loc = expand_location (line_table->highest_line);
+  saved_loc = expand_location (LINEMAPS_ORDINARY_HIGHEST_LINE (line_table));
   saved_trace_includes = line_table->trace_includes;
 
   timevar_push (TV_PCH_CPP_RESTORE);
diff --git a/gcc/c-family/c-ppoutput.c b/gcc/c-family/c-ppoutput.c
index 16d4f7d..92e7fd3 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 = LOCATION_FILE (src_loc);
 
       /* 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,8 @@ 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_macro_loc_to_exp_point (line_table, line, &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 0dece6c..2385e88 100644
--- a/gcc/fortran/cpp.c
+++ b/gcc/fortran/cpp.c
@@ -810,27 +810,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);
@@ -927,7 +929,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..29e4de1 100644
--- a/gcc/input.c
+++ b/gcc/input.c
@@ -43,11 +43,21 @@ expand_location (source_location loc)
     }
   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;
-    };
+      bool expand_to_expansion_point_p =
+	!linemap_tracks_macro_expansion_locs_p (line_table);
+
+    /* If LOC is the location of a token resulting from macro
+       expansion, either expand to the location of the macro expansion
+       point if we don't support tracking token locations accross
+       macro expansion, or expand to the actual spelling location of
+       the token where the error is if we do support tracking
+       locations accross macro expansion.  */
+      xloc =
+	linemap_expand_location_full (line_table, loc,
+				      (expand_to_expansion_point_p)
+				      ? LRK_MACRO_EXPANSION_POINT
+				      : LRK_SPELLING_LOCATION,
+				      NULL);
+    }
   return xloc;
 }
diff --git a/gcc/input.h b/gcc/input.h
index 5929064..835c95a 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 (in_system_header_at (input_location))
+#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-only.c b/libcpp/directives-only.c
index e19f806..e478fc3 100644
--- a/libcpp/directives-only.c
+++ b/libcpp/directives-only.c
@@ -54,7 +54,7 @@ _cpp_preprocess_dir_only (cpp_reader *pfile,
   buffer->need_line = false;
 
   /* This isn't really needed.  It prevents a compiler warning, though. */
-  loc = pfile->line_table->highest_line;
+  loc = LINEMAPS_ORDINARY_HIGHEST_LINE (pfile->line_table);
 
   /* Scan initialization. */
   next_line = cur = base = buffer->cur;
@@ -107,13 +107,14 @@ _cpp_preprocess_dir_only (cpp_reader *pfile,
 	    /* Sanitize the line settings.  Duplicate #include's can mess
 	       things up. */
 	    line_table = pfile->line_table;
-	    line_table->highest_location = line_table->highest_line;
+	    LINEMAPS_ORDINARY_HIGHEST_LOCATION (line_table) =
+	      LINEMAPS_ORDINARY_HIGHEST_LINE (line_table);
 
 	    /* The if block prevents us from outputing line information when
 	       the file ends with a directive and no newline.  Note that we
 	       must use pfile->buffer, not buffer. */
 	    if (pfile->buffer->next_line < pfile->buffer->rlimit)
-	      cb->maybe_print_line (pfile->line_table->highest_line);
+	      cb->maybe_print_line (LINEMAPS_ORDINARY_HIGHEST_LINE (pfile->line_table));
 
 	    goto restart;
 	  }
diff --git a/libcpp/directives.c b/libcpp/directives.c
index 83d4a0e..04db855 100644
--- a/libcpp/directives.c
+++ b/libcpp/directives.c
@@ -274,7 +274,8 @@ start_directive (cpp_reader *pfile)
   pfile->directive_result.type = CPP_PADDING;
 
   /* Some handlers need the position of the # for diagnostics.  */
-  pfile->directive_line = pfile->line_table->highest_line;
+  pfile->directive_line =
+    LINEMAPS_ORDINARY_HIGHEST_LINE (pfile->line_table);
 }
 
 /* Called when leaving a directive, _Pragma or command-line directive.  */
@@ -884,14 +885,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 +947,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;
@@ -1021,7 +1022,7 @@ do_linemarker (cpp_reader *pfile)
      *following* the #line directive.  A separate source_location for this
      location makes no sense (until we do the LC_LEAVE), and
      complicates LAST_SOURCE_LINE_LOCATION.  */
-  pfile->line_table->highest_location--;
+  LINEMAPS_ORDINARY_HIGHEST_LOCATION (pfile->line_table)--;
 
   _cpp_do_file_change (pfile, reason, new_file, new_lineno, new_sysp);
 }
@@ -1038,7 +1039,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/errors.c b/libcpp/errors.c
index c586749..13804f0 100644
--- a/libcpp/errors.c
+++ b/libcpp/errors.c
@@ -43,7 +43,7 @@ cpp_diagnostic (cpp_reader * pfile, int level, int reason,
       if (pfile->state.in_directive)
 	src_loc = pfile->directive_line;
       else
-	src_loc = pfile->line_table->highest_line;
+	src_loc = LINEMAPS_ORDINARY_HIGHEST_LINE (pfile->line_table);
     }
   /* We don't want to refer to a token before the beginning of the
      current run -- that is invalid.  */
diff --git a/libcpp/files.c b/libcpp/files.c
index d2c6b8b..435888e 100644
--- a/libcpp/files.c
+++ b/libcpp/files.c
@@ -542,7 +542,7 @@ _cpp_find_file (cpp_reader *pfile, const char *fname, cpp_dir *start_dir, bool f
   entry = new_file_hash_entry (pfile);
   entry->next = *hash_slot;
   entry->start_dir = start_dir;
-  entry->location = pfile->line_table->highest_location;
+  entry->location = LINEMAPS_ORDINARY_HIGHEST_LOCATION (pfile->line_table);
   entry->u.file = file;
   *hash_slot = entry;
 
@@ -555,7 +555,7 @@ _cpp_find_file (cpp_reader *pfile, const char *fname, cpp_dir *start_dir, bool f
       entry = new_file_hash_entry (pfile);
       entry->next = *hash_slot;
       entry->start_dir = pfile->bracket_include;
-      entry->location = pfile->line_table->highest_location;
+      entry->location = LINEMAPS_ORDINARY_HIGHEST_LOCATION (pfile->line_table);
       entry->u.file = file;
       *hash_slot = entry;
     }
@@ -566,7 +566,8 @@ _cpp_find_file (cpp_reader *pfile, const char *fname, cpp_dir *start_dir, bool f
       entry = new_file_hash_entry (pfile);
       entry->next = *hash_slot;
       entry->start_dir = pfile->quote_include;
-      entry->location = pfile->line_table->highest_location;
+      entry->location =
+	LINEMAPS_ORDINARY_HIGHEST_LOCATION (pfile->line_table);
       entry->u.file = file;
       *hash_slot = entry;
     }
@@ -927,7 +928,7 @@ _cpp_stack_include (cpp_reader *pfile, const char *fname, int angle_brackets,
      linemap_add is not called) or we were included from the
      command-line.  */
   if (file->pchname == NULL && file->err_no == 0 && type != IT_CMDLINE)
-    pfile->line_table->highest_location--;
+    LINEMAPS_ORDINARY_HIGHEST_LOCATION (pfile->line_table)--;
 
   return _cpp_stack_file (pfile, file, type == IT_IMPORT);
 }
@@ -936,7 +937,8 @@ _cpp_stack_include (cpp_reader *pfile, const char *fname, int angle_brackets,
 static void
 open_file_failed (cpp_reader *pfile, _cpp_file *file, int angle_brackets)
 {
-  int sysp = pfile->line_table->highest_line > 1 && pfile->buffer ? pfile->buffer->sysp : 0;
+  int sysp = ((LINEMAPS_ORDINARY_HIGHEST_LINE (pfile->line_table) > 1)
+	      && (pfile->buffer ? pfile->buffer->sysp : 0));
   bool print_dep = CPP_OPTION (pfile, deps.style) > (angle_brackets || !!sysp);
 
   errno = file->err_no;
@@ -1048,7 +1050,7 @@ make_cpp_dir (cpp_reader *pfile, const char *dir_name, int sysp)
   entry = new_file_hash_entry (pfile);
   entry->next = *hash_slot;
   entry->start_dir = NULL;
-  entry->location = pfile->line_table->highest_location;
+  entry->location = LINEMAPS_ORDINARY_HIGHEST_LOCATION (pfile->line_table);
   entry->u.dir = dir;
   *hash_slot = entry;
 
@@ -1220,14 +1222,16 @@ 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,
-		       SOURCE_LINE (map, pfile->line_table->highest_line), flags);
+  _cpp_do_file_change (pfile, LC_RENAME, ORDINARY_MAP_FILE_NAME (map),
+		       SOURCE_LINE (map,
+				    LINEMAPS_ORDINARY_HIGHEST_LINE
+				    (pfile->line_table)),
+		       flags);
 }
 
 /* Allow the client to change the current file.  Used by the front end
diff --git a/libcpp/include/cpp-id-data.h b/libcpp/include/cpp-id-data.h
index a57edad..8260fd0 100644
--- a/libcpp/include/cpp-id-data.h
+++ b/libcpp/include/cpp-id-data.h
@@ -34,6 +34,12 @@ struct GTY(()) answer {
 /* Each macro definition is recorded in a cpp_macro structure.
    Variadic macros cannot occur with traditional cpp.  */
 struct GTY(()) cpp_macro {
+  /* Name of this macro.  Used only for error reporting.  */
+  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")))
+    name;
+
   /* Parameters, if any.  */
   cpp_hashnode ** GTY ((nested_ptr (union tree_node,
 		"%h ? CPP_HASHNODE (GCC_IDENT_TO_HT_IDENT (%h)) : NULL",
diff --git a/libcpp/include/line-map.h b/libcpp/include/line-map.h
index 3234423..399194c 100644
--- a/libcpp/include/line-map.h
+++ b/libcpp/include/line-map.h
@@ -27,13 +27,23 @@ 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
+  /* stringize */
+  /* paste */
+};
 
 /* The type of line numbers.  */
 typedef unsigned int linenum_type;
@@ -44,42 +54,235 @@ 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 {
+   means "entire file/line" or "unknown line/column" or "not
+   applicable".)
+
+   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.  */
+
+  /* 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 macro map.  */
+#define MAX_SOURCE_LOCATION 0xF0000000
+
+struct GTY(()) cpp_macro;
+
+/* A macro line map encodes locations 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_macro *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 we are at the expansion point of MACRO.  Each xI is the
+     location of the Ith token of the replacement-list. Now it gets
+     confusing. the xI is the location of the Ith token of the
+     replacement-list at the macro *definition* point. Not at the
+     macro replacement point. Okay, let's try to explain this below.
+
+     Imagine this:
+
+        #define OPERATION(OP0, OPERATOR, OP1) \
+                OP0 OPERATOR OP1 <-- #0
+
+	#define PLUS(A, B) OPERATION (A, +, B)  <--- #1
+
+	int a = PLUS (1,2); <--- #2
+     
+     In #2, there is a macro map for the expansion of PLUS. PLUS is
+     expanded into the replacement-list made of the tokens:
+     
+        OPERATION, (, A, +, B, )
+
+     and then further expanded into the tokens:
+
+        1, +, 2.
+
+     Let's consider the case of token "+" here. That will help us
+     understand what the xI we were talking about earlier means.
+
+     The token '+' has two locations, so to speak. One in the context
+     of the macro *expansion* of PLUS in #2 and one in the context of
+     the macro *definition* of PLUS in #1. These two locations are
+     encoded in the the latter context, somehow in the xI we are
+     talking about.
+
+     xI is roughly the index of the token inside the replacement-list
+     at the expansion point. So for '+', it's index would then be 1
+     [The index of token '1' would be 0 and the index of token 2 would
+     be 1]. So if '+' is our current xI, it is actualy an x1.
+
+     The value of x1 is the location of the token '+' inside the
+     replacement-list of PLUS at the definition point of PLUS. It is
+     its spelling location in #1.
+
+     So x0 would have described the token '1', x1 describes the token
+     '+' and x2 describes the token '2'.
+
+     Now what's the y1 then? Remember, we said macro_locations is an
+     array of pairs (xI,yI). We now know what the xI is, now let's
+     look at the yI.
+
+     Let's look at the token '+' again. We said it has two locations
+     somehow. Actually it has 3. Kind of. As '+' is an argument passed
+     to the macro OPERATION [at the definition point of the macro
+     PLUS], it would be nice to record the source location of the
+     *parameter* of OPERATION that is replaced by the argument '+'.
+     In other words, we want to record the location of the token
+     "OPERATOR" in the replacement-list of OPERATION, at the
+     /definition/ point of OPERATION in #0. And that is y1.
+
+     So when (xI,yI) describes a token that is passed as an argument
+     to a macro M, the yI is the location of the macro parameter that
+     the argument replaces, at the definition point of M. If (xI,yI)
+     does not describe a token that is passed as an argument to a
+     macro, xI == yI.
+   */
+  source_location * GTY((length ("2 * %h.n_tokens"))) macro_locations;
+
+  /* This is the location of the expansion point of the current macro
+     map.  That expansion point 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 {
+    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) (MAP)->d.ordinary.to_file
+
+#define ORDINARY_MAP_STARTING_LINE_NUMBER(MAP) (MAP)->d.ordinary.to_line
+
+#define ORDINARY_MAP_INCLUDER_FILE_INDEX(MAP) (MAP)->d.ordinary.included_from
+
+#define ORDINARY_MAP_IN_SYSTEM_HEADER_P(MAP) (MAP)->d.ordinary.sysp
+
+#define ORDINARY_MAP_NUMBER_OF_COLUMN_BITS(MAP) (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;
 
-  /* The most recently listed include stack, if any, starts with
-     LAST_LISTED as the topmost including file.  -1 indicates nothing
-     has been listed yet.  */
-  int last_listed;
+  /* Highest source_location "given out".  */
+  source_location highest_location;
+
+  /* Start of line of highest source_location "given out".  */
+  source_location highest_line;
+};
+
+/* 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;
@@ -87,12 +290,6 @@ struct GTY(()) line_maps {
   /* If true, prints an include trace a la -H.  */
   bool trace_includes;
 
-  /* Highest source_location "given out".  */
-  source_location highest_location;
-
-  /* Start of line of highest source_location "given out".  */
-  source_location highest_line;
-
   /* The maximum column number we can quickly allocate.  Higher numbers
      may require allocating a new line_map.  */
   unsigned int max_column_hint;
@@ -102,44 +299,375 @@ struct GTY(()) line_maps {
   line_map_realloc reallocator;
 };
 
+/* Return TRUE if LOC is possibly encoded in MAP, FALSE otherwise.
+   MACRO_MAP_P shall be TRUE if MAP is to be considered as a macro
+   map, false otherwise.  */
+#define LOCATION_POSSIBLY_IN_MAP_P(LOC, MAP, MACRO_MAP_P)		\
+  (((MACRO_MAP_P))							\
+   ? (LOC) <= (MAP)->start_location					\
+   : (LOC) >= (MAP)->start_location)
+
+/* Return TRUE if LOC is possibly encoded in the macro map MAP, FALSE
+   otherwise.  */
+#define LOCATION_POSSIBLY_IN_MACRO_MAP_P(LOC, MAP) \
+  LOCATION_POSSIBLY_IN_MAP_P ((LOC), (MAP), true)
+
+/* 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
+
+/* Returns the highest location that was encoded into the line table
+   SET. MAP_KIND shall be TRUE if we are interested in locations of
+   tokens resulting from macro expansion, FALSE otherwise.  */
+#define LINEMAPS_HIGHEST_LOCATION(SET, MAP_KIND) \
+  (LINEMAPS_MAP_INFO (SET, MAP_KIND))->highest_location
+
+/* Returns the location of the begining of the highest line that was
+   encoded in into the line table SET.  MAP_KIND shall be TRUE if we
+   are interested in locations of tokens resulting from macro
+   expansion, FALSE otherwise.  */
+#define LINEMAPS_HIGHEST_LINE(SET, MAP_KIND) \
+  (LINEMAPS_MAP_INFO (SET, MAP_KIND))->highest_line
+
+/* 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)
+
+/* Return the highest location of an ordinary token encoded in the
+   line table SET.  */
+#define LINEMAPS_ORDINARY_HIGHEST_LOCATION(SET) \
+  LINEMAPS_HIGHEST_LOCATION(SET, false)
+
+/* Return the location of the beginning of the highest (containing
+   ordinary tokens) line encoded in the in the set.  */
+#define LINEMAPS_ORDINARY_HIGHEST_LINE(SET) \
+  LINEMAPS_HIGHEST_LINE(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 highest location [of a token resulting from macro
+   expansion] encoded in this line table.  */
+#define LINEMAPS_MACRO_HIGHEST_LOCATION(SET) \
+  LINEMAPS_HIGHEST_LOCATION(SET, true)
+
+/* Returns the location of the begining of the highest line
+   -- containing a token resulting from macro expansion --  encoded
+   in the line table SET.  */
+#define LINEMAPS_MACRO_HIGHEST_LINE(SET) \
+  LINEMAPS_HIGHEST_LINE(SET, true)
+
+/* 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 *);
+void linemap_init (struct line_maps *);
 
-/* Free a line map set.  */
-extern void linemap_free (struct line_maps *);
+/* Free a line map set.  This should be called only if maps have NOT
+   been allocated in garbage memory space.  */
+void linemap_free (struct line_maps *);
 
 /* Check for and warn about line_maps entered but not exited.  */
+void linemap_check_files_exited (struct line_maps *);
 
-extern void linemap_check_files_exited (struct line_maps *);
+/* 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 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).  */
-
-extern source_location linemap_line_start
+   (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);
 
 /* 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.
 
    A call to this function can relocate the previous set of
    maps, so any stored line_map pointers should not be used.  */
-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.  */
-extern const struct line_map *linemap_lookup
-  (struct line_maps *, source_location);
+const struct line_map *linemap_add (struct line_maps *,
+				    enum lc_reason,
+				    unsigned int sysp,
+				    const char *to_file,
+				    linenum_type to_line);
+
+/*
+   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 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_macro*,
+					    source_location,
+					    unsigned int);
+
+/* Create and return a source location for a token that is part of a
+   macro replacement-list at a macro expansion point.
+
+   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 orginal location of the token at the definition
+   point of the macro. If you read the extensive comments of struct
+   line_map_macro in line-map.h, this is the xI.
+
+   If the token is part of a macro argument, ORIG_PARM_REPLACEMENT_LOC
+   is the location of the point at wich the token (the argument)
+   replaces the macro parameter in the context of the relevant macro
+   definition. If you read the comments of struct line_map_macro in
+   line-map.h, this is the yI.  */
+source_location linemap_add_macro_token (const struct line_map *,
+					 unsigned int,
+					 source_location,
+					 source_location);
+
+/* 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.  */
+const struct line_map *linemap_lookup (struct line_maps *, source_location);
+
+/* Assert that MAP encodes locations of tokens that are not part of
+   the replacement-list of a macro expansion.  */
+int linemap_check_ordinary (const struct line_map *);
+
+/* 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 *);
+
+/* 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.  */
+source_location linemap_macro_loc_to_exp_point (struct line_maps *,
+						source_location,
+						const struct line_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_loc_to_def_point (struct line_maps *,
+						source_location,
+						const struct line_map **,
+						bool);
+
+/* If LOCATION is the locus of a token in a replacement-list of a
+   macro expansion return the location of said token in the definition
+   of the macro.
+
+   Read the comments of struct line_map and struct line_map_macro in
+   line-map.h to understand what a macro expansion point is.
+
+   If RETURN_MACRO_PARM_USAGE_POINT_P is TRUE and if LOCATION is the
+   locus of a token that is an argument of a macro M, this function
+   returns the locus of the parameter replaced by the argument, in the
+   definition of M. This is the yI in the comments of struct
+   line_map_macro in line-map.h.
+
+   Note that if the token is a builtin the function returns the
+   location of the expansion point of the macro.  */
+source_location linemap_macro_map_loc_to_def_point (const struct line_map*,
+						    source_location,
+						    bool);
+
+/* 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*,
+						    source_location);
+
+/* Return the index of MAP into set.  MAP can be either an ordinary or
+   a macro map.  */
+int linemap_map_get_index (const struct line_maps *,
+			   const struct line_map*);
+
+/* 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_source_line (struct line_maps *,
+			     source_location);
+
+/* Return the column number corresponding to location LOCATION.
+
+   If LOCATION is the source location of token that is part of the
+   replacement-list of a macro expansion return the column number of
+   the macro expansion point.
+
+   SET is the line map set LOCATION comes from.  */
+int linemap_get_source_column (struct line_maps *,
+			       source_location);
+
+/* Return the name of the macro associated to MACRO_MAP.  */
+const char* linemap_map_get_macro_name (const struct line_map*);
+
+/* 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_file_path (struct line_maps *,
+				   source_location);
+
+/* 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 0 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
@@ -147,47 +675,161 @@ extern const struct line_map *linemap_lookup
 #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)						\
+  (linemap_check_ordinary (MAP),					\
+    ((((LOC) - (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)				\
+  (linemap_check_ordinary (MAP),			\
+   (((LOC) - (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),				\
+   ((((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),					\
+   ((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),		\
+   ((MAP)->d.ordinary.included_from < 0))
 
 /* Set LOC to a source position that is the same line as the most recent
    linemap_line_start, but with the specified TO_COLUMN column number.  */
 
-#define LINEMAP_POSITION_FOR_COLUMN(LOC, SET, TO_COLUMN) do { \
-  unsigned int to_column = (TO_COLUMN); \
-  struct line_maps *set = (SET); \
-  if (__builtin_expect (to_column >= set->max_column_hint, 0)) \
-    (LOC) = linemap_position_for_column (set, to_column); \
-  else { \
-    source_location r = set->highest_line; \
-    r = r + to_column; \
-    if (r >= set->highest_location) \
-      set->highest_location = r; \
-    (LOC) = r;			 \
-  }} while (0)
-    
-
-extern source_location
-linemap_position_for_column (struct line_maps *set, unsigned int to_column);
+#define LINEMAP_POSITION_FOR_COLUMN(LOC, SET, TO_COLUMN) do {		\
+    unsigned int to_column = (TO_COLUMN);				\
+    struct line_maps *set = (SET);					\
+    if (__builtin_expect (to_column >= set->max_column_hint, 0))	\
+      (LOC) = linemap_position_for_column (set, to_column);		\
+    else								\
+      {									\
+	source_location r = LINEMAPS_ORDINARY_HIGHEST_LINE (set);	\
+	r = r + to_column;						\
+	if (r >= LINEMAPS_ORDINARY_HIGHEST_LOCATION (set))		\
+	  LINEMAPS_ORDINARY_HIGHEST_LOCATION (set) = r;			\
+	(LOC) = r;							\
+      }									\
+  } while (0)
+
+/* 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 *,
+					     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), (MAP)->d.ordinary.to_file)
+
+/* Return the line number this map started encoding location from.  */
+#define LINEMAP_LINE(MAP)					\
+  (linemap_check_ordinary (MAP), (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), (MAP)->d.ordinary.sysp)
+
+/* Return TRUE if PRE denotes a location that is before POST, FALSE
+   otherwise. LINE_MAPS is the set of line maps PRE and POST were
+   allocated from.  */
+bool linemap_location_before_p (struct line_maps *set,
+				source_location   pre,
+				source_location   post);
+
+typedef struct GTY (())
+{
+  /* 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;
+
+enum location_resolution_kind
+{
+  LRK_MACRO_EXPANSION_POINT,
+  LRK_SPELLING_LOCATION,
+  LRK_MACRO_PARM_REPLACEMENT_POINT
+};
+
+expanded_location linemap_expand_location (struct line_maps *,
+					   source_location);
+
+/* Expand source code location LOC and return a user readable source
+   code 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.
+
+   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 function expands the location to the locus of the
+   expansion point of the macro.
+
+   * If LRK is set to LRK_SPELLING_LOCATION
+   -------------------------------------
+
+   The function expands 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 function expands 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.  */
+expanded_location linemap_expand_location_full (struct line_maps *,
+						source_location,
+						enum location_resolution_kind,
+						const struct line_map**);
 #endif /* !LIBCPP_LINE_MAP_H  */
diff --git a/libcpp/init.c b/libcpp/init.c
index 5ba6666..3a16d33 100644
--- a/libcpp/init.c
+++ b/libcpp/init.c
@@ -574,7 +574,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 d2872c4..f02c878 100644
--- a/libcpp/internal.h
+++ b/libcpp/internal.h
@@ -67,8 +67,9 @@ 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]; \
-    linenum_type line = SOURCE_LINE (map, line_table->highest_line); \
+    const struct line_map *map = \
+      LINEMAPS_LAST_ORDINARY_MAP (line_table); \
+    linenum_type line = SOURCE_LINE (map, LINEMAPS_ORDINARY_HIGHEST_LINE (line_table)); \
     linemap_line_start (PFILE->line_table, line + 1, COLS_HINT); \
   } while (0)
 
diff --git a/libcpp/lex.c b/libcpp/lex.c
index d29f36d..03d63bf 100644
--- a/libcpp/lex.c
+++ b/libcpp/lex.c
@@ -831,12 +831,17 @@ _cpp_process_line_notes (cpp_reader *pfile, int in_comment)
       if (note->type == '\\' || note->type == ' ')
 	{
 	  if (note->type == ' ' && !in_comment)
-	    cpp_error_with_line (pfile, CPP_DL_WARNING, pfile->line_table->highest_line, col,
+	    cpp_error_with_line (pfile, CPP_DL_WARNING,
+				 LINEMAPS_ORDINARY_HIGHEST_LINE (pfile->line_table),
+				 col,
 				 "backslash and newline separated by space");
 
 	  if (buffer->next_line > buffer->rlimit)
 	    {
-	      cpp_error_with_line (pfile, CPP_DL_PEDWARN, pfile->line_table->highest_line, col,
+	      cpp_error_with_line (pfile, CPP_DL_PEDWARN,
+				   LINEMAPS_ORDINARY_HIGHEST_LINE
+				   (pfile->line_table),
+				   col,
 				   "backslash-newline at end of file");
 	      /* Prevent "no newline at end of file" warning.  */
 	      buffer->next_line = buffer->rlimit;
@@ -852,7 +857,9 @@ _cpp_process_line_notes (cpp_reader *pfile, int in_comment)
 	    {
 	      if (CPP_OPTION (pfile, trigraphs))
 		cpp_warning_with_line (pfile, CPP_W_TRIGRAPHS,
-                                       pfile->line_table->highest_line, col,
+                                       LINEMAPS_ORDINARY_HIGHEST_LINE
+				       (pfile->line_table),
+				       col,
 				       "trigraph ??%c converted to %c",
 				       note->type,
 				       (int) _cpp_trigraph_map[note->type]);
@@ -860,7 +867,7 @@ _cpp_process_line_notes (cpp_reader *pfile, int in_comment)
 		{
 		  cpp_warning_with_line 
 		    (pfile, CPP_W_TRIGRAPHS,
-                     pfile->line_table->highest_line, col,
+                     LINEMAPS_ORDINARY_HIGHEST_LINE (pfile->line_table), col,
 		     "trigraph ??%c ignored, use -trigraphs to enable",
 		     note->type);
 		}
@@ -908,7 +915,8 @@ _cpp_skip_block_comment (cpp_reader *pfile)
 	    {
 	      buffer->cur = cur;
 	      cpp_warning_with_line (pfile, CPP_W_COMMENTS,
-				     pfile->line_table->highest_line,
+				     LINEMAPS_ORDINARY_HIGHEST_LINE
+				     (pfile->line_table),
 				     CPP_BUF_COL (buffer),
 				     "\"/*\" within comment");
 	    }
@@ -941,13 +949,14 @@ static int
 skip_line_comment (cpp_reader *pfile)
 {
   cpp_buffer *buffer = pfile->buffer;
-  source_location orig_line = pfile->line_table->highest_line;
+  source_location orig_line =
+    LINEMAPS_ORDINARY_HIGHEST_LINE (pfile->line_table);
 
   while (*buffer->cur != '\n')
     buffer->cur++;
 
   _cpp_process_line_notes (pfile, true);
-  return orig_line != pfile->line_table->highest_line;
+  return orig_line != LINEMAPS_ORDINARY_HIGHEST_LINE (pfile->line_table);
 }
 
 /* Skips whitespace, saving the next non-whitespace character.  */
@@ -966,7 +975,8 @@ skip_whitespace (cpp_reader *pfile, cppchar_t c)
       else if (c == '\0')
 	saw_NUL = true;
       else if (pfile->state.in_directive && CPP_PEDANTIC (pfile))
-	cpp_error_with_line (pfile, CPP_DL_PEDWARN, pfile->line_table->highest_line,
+	cpp_error_with_line (pfile, CPP_DL_PEDWARN,
+			     LINEMAPS_ORDINARY_HIGHEST_LINE (pfile->line_table),
 			     CPP_BUF_COL (buffer),
 			     "%s in preprocessing directive",
 			     c == '\f' ? "form feed" : "vertical tab");
@@ -1463,7 +1473,8 @@ lex_raw_string (cpp_reader *pfile, cpp_token *token, const uchar *base,
 	      source_location src_loc = token->src_loc;
 	      token->type = CPP_EOF;
 	      /* Tell the compiler the line number of the EOF token.  */
-	      token->src_loc = pfile->line_table->highest_line;
+	      token->src_loc =
+		LINEMAPS_ORDINARY_HIGHEST_LINE (pfile->line_table);
 	      token->flags = BOL;
 	      if (first_buff != NULL)
 		_cpp_release_buff (pfile, first_buff);
@@ -1947,7 +1958,8 @@ _cpp_lex_direct (cpp_reader *pfile)
 	  if (!pfile->state.in_directive)
 	    {
 	      /* Tell the compiler the line number of the EOF token.  */
-	      result->src_loc = pfile->line_table->highest_line;
+	      result->src_loc =
+		LINEMAPS_ORDINARY_HIGHEST_LINE (pfile->line_table);
 	      result->flags = BOL;
 	    }
 	  return result;
@@ -1964,14 +1976,15 @@ _cpp_lex_direct (cpp_reader *pfile)
     }
   buffer = pfile->buffer;
  update_tokens_line:
-  result->src_loc = pfile->line_table->highest_line;
+  result->src_loc =
+    LINEMAPS_ORDINARY_HIGHEST_LINE (pfile->line_table);
 
  skipped_white:
   if (buffer->cur >= buffer->notes[buffer->cur_note].pos
       && !pfile->overlaid_buffer)
     {
       _cpp_process_line_notes (pfile, false);
-      result->src_loc = pfile->line_table->highest_line;
+      result->src_loc = LINEMAPS_ORDINARY_HIGHEST_LINE (pfile->line_table);
     }
   c = *buffer->cur++;
 
diff --git a/libcpp/line-map.c b/libcpp/line-map.c
index 86e2484..693ed0b 100644
--- a/libcpp/line-map.c
+++ b/libcpp/line-map.c
@@ -23,91 +23,189 @@ along with this program; see the file COPYING3.  If not see
 #include "config.h"
 #include "system.h"
 #include "line-map.h"
+#include "cpp-id-data.h"
 
-static void trace_include (const struct line_maps *, const struct line_map *);
+#define linemap_assert(EXPR)			\
+  do {						\
+    if (! (EXPR))				\
+      abort ();					\
+  } while (0)
 
-/* Initialize a line map set.  */
+static void trace_include (const struct line_maps *, const struct line_map *);
 
 void
 linemap_init (struct line_maps *set)
 {
-  set->maps = NULL;
-  set->allocated = 0;
-  set->used = 0;
-  set->last_listed = -1;
-  set->trace_includes = false;
-  set->depth = 0;
-  set->cache = 0;
-  set->highest_location = RESERVED_LOCATION_COUNT - 1;
-  set->highest_line = RESERVED_LOCATION_COUNT - 1;
-  set->max_column_hint = 0;
-  set->reallocator = 0;
+  memset (set, 0, sizeof (struct line_maps));
+  LINEMAPS_ORDINARY_HIGHEST_LOCATION (set) = RESERVED_LOCATION_COUNT - 1;
+  LINEMAPS_ORDINARY_HIGHEST_LINE (set) = RESERVED_LOCATION_COUNT - 1;
+  LINEMAPS_MACRO_HIGHEST_LOCATION (set) = MAX_SOURCE_LOCATION + 1;
+  LINEMAPS_MACRO_HIGHEST_LINE (set) = MAX_SOURCE_LOCATION + 1;
 }
 
-/* Check for and warn about line_maps entered but not exited.  */
-
 void
 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)
 {
-  if (set->maps)
+  struct line_map *cur_map;
+  unsigned i;
+
+  /* Free ordinary maps related memory.  */
+  if (LINEMAPS_ORDINARY_MAPS (set))
     {
       linemap_check_files_exited (set);
 
-      free (set->maps);
+      free (LINEMAPS_ORDINARY_MAPS (set));
+      LINEMAPS_ORDINARY_MAPS (set) = NULL;
     }
-}
 
-/* Add a mapping of logical source line to physical source file and
-   line number.
+  /* Free macro maps related memory.  */
+  if (LINEMAPS_MACRO_MAPS (set))
+    {
+      for (cur_map = LINEMAPS_MACRO_MAPS (set), i = 0;
+	   i < LINEMAPS_MACRO_ALLOCATED (set);
+	   ++i, ++cur_map)
+	{
+	  if (MACRO_MAP_LOCATIONS (LINEMAPS_MACRO_MAP_AT (set, i)))
+	    {
+	      free (MACRO_MAP_LOCATIONS (LINEMAPS_MACRO_MAP_AT (set, i)));
+	      MACRO_MAP_LOCATIONS (LINEMAPS_MACRO_MAP_AT (set, i)) = NULL;
+	    }
+	}
+      free (LINEMAPS_MACRO_MAPS (set));
+      LINEMAPS_MACRO_MAPS (set) = NULL;
+    }
+}
 
-   The text pointed to by TO_FILE must have a lifetime
-   at least as long as the final call to lookup_line ().  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.
+bool
+linemap_tracks_macro_expansion_locs_p (struct line_maps *set)
+{
+  return LINEMAPS_MACRO_MAPS (set) != NULL;
+}
 
-   FROM_LINE should be monotonic increasing across calls to this
-   function.  A call to this function can relocate the previous set of
-   maps, so any stored line_map pointers should not be used.  */
+/* 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.  */
 
-const struct line_map *
-linemap_add (struct line_maps *set, enum lc_reason reason,
-	     unsigned int sysp, const char *to_file, linenum_type to_line)
+static struct line_map *
+new_linemap (struct line_maps *set,
+	     enum lc_reason reason)
 {
-  struct line_map *map;
-  source_location start_location = set->highest_location + 1;
+  /* 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 (set->used && start_location < set->maps[set->used - 1].start_location)
-    abort ();
-
-  if (set->used == set->allocated)
+  if (LINEMAPS_USED (set, macro_map_p) == LINEMAPS_ALLOCATED (set, macro_map_p))
     {
+      /* We ran out of allocated line maps. Let's allocate more.  */
+
       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
+      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));
-      memset (&set->maps[set->used], 0, ((set->allocated - set->used)
-					 * 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;
+}
+
+/* Create an ordinary line map -- a mapping between a logical source
+   location and physical source file and line number.
+
+   SET is the set of line maps the new map will belong to.  The text
+   pointed to by TO_FILE must have a lifetime 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.
 
-  map = &set->maps[set->used];
+   If LC_REASON is LC_RENAME and PREVIOUS is non NULL, it means the
+   line map created by this function is considered to be for the same
+   file as the map PREVIOUS. Setting PREVIOUS to NULL makes the
+   argument to be ignored.
+
+   A call to this function can relocate the previous set of
+   maps, so any stored line_map pointers should not be used.
+
+   This function is a subroutine of linemap_add.  */
+
+static const struct line_map *
+create_and_add_line_map_internal (struct line_maps *set,
+				  enum lc_reason reason,
+				  unsigned int sysp, const char *to_file,
+				  linenum_type to_line,
+				  const struct line_map *previous)
+{
+  struct line_map *map = NULL;
+  /* 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 *prev_map_same_file = NULL;
+  source_location start_location = LINEMAPS_ORDINARY_HIGHEST_LOCATION (set) + 1;
+  int previous_index = -1;
+
+  linemap_assert (reason != LC_ENTER_MACRO);
+
+  linemap_assert (!(LINEMAPS_ORDINARY_USED (set)
+		    && (start_location
+			< MAP_START_LOCATION (LINEMAPS_LAST_ORDINARY_MAP (set)))));
+
+  /* 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)
+    {
+      set->depth--;
+      return NULL;
+    }
+
+  /*
+     *Kludge* ahead. new_linemap uses ggc_realloc to enlarge
+     line_maps::maps. ggc_realloc frees the previous storage of
+     line_maps::maps. So struct line_map* pointers that where pointing
+     into line_maps::maps prior to new_linemap can become dangling
+     after new_linemap. previous is such a pointer that can become
+     dangling after new_linemap. Let's make sure we make it point into
+     the newly allocated line_maps::maps.
+  */
+  if (previous)
+    previous_index = linemap_map_get_index (set, previous);
+
+  map = new_linemap (set, reason);
+  if (previous_index > -1)
+    previous = &LINEMAPS_ORDINARY_MAPS (set)[previous_index];
 
   if (to_file && *to_file == '\0' && reason != LC_RENAME_VERBATIM)
     to_file = "<stdin>";
@@ -121,27 +219,34 @@ linemap_add (struct line_maps *set, enum lc_reason reason,
     reason = LC_ENTER;
   else if (reason == LC_LEAVE)
     {
-      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;
+	  reason = LC_RENAME;
+	  prev_map_same_file = map - 1;
 	}
       else
 	{
-	  from = INCLUDED_FROM (set, map - 1);
-	  error = to_file && filename_cmp (from->to_file, to_file);
+	  /* (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.  */
+	  prev_map_same_file = INCLUDED_FROM (set, map - 1);
+	  linemap_check_ordinary (prev_map_same_file);
+	  error =
+	    to_file
+	    && filename_cmp (ORDINARY_MAP_FILE_NAME (prev_map_same_file),
+			     to_file);
 	}
 
-      /* Depending upon whether we are handling preprocessed input or
+      /* Depending on whether we are handling preprocessed input or
 	 not, this can be a user error or an ICE.  */
       if (error)
 	fprintf (stderr, "line-map.c: file \"%s\" left but not entered\n",
@@ -150,55 +255,131 @@ 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_line = SOURCE_LINE (from, from[1].start_location);
-	  sysp = from->sysp;
+	  linemap_check_ordinary (prev_map_same_file);
+	  to_file = ORDINARY_MAP_FILE_NAME (prev_map_same_file);
+	  to_line = SOURCE_LINE (prev_map_same_file,
+				 prev_map_same_file[1].start_location);
+	  sysp = ORDINARY_MAP_IN_SYSTEM_HEADER_P (prev_map_same_file);
 	}
     }
 
-  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;
-  set->highest_location = start_location;
-  set->highest_line = start_location;
+  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;
+  LINEMAPS_ORDINARY_HIGHEST_LOCATION (set) = start_location;
+  LINEMAPS_ORDINARY_HIGHEST_LINE (set) = 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;
+    {
+      if (previous == NULL)
+	previous = &map[-1];
+      linemap_check_ordinary (previous);
+      ORDINARY_MAP_INCLUDER_FILE_INDEX (map) =
+	ORDINARY_MAP_INCLUDER_FILE_INDEX (previous);
+    }
   else if (reason == LC_LEAVE)
     {
+      linemap_assert (prev_map_same_file != NULL);
       set->depth--;
-      map->included_from = INCLUDED_FROM (set, map - 1)->included_from;
+      ORDINARY_MAP_INCLUDER_FILE_INDEX (map) =
+		ORDINARY_MAP_INCLUDER_FILE_INDEX (prev_map_same_file);
     }
 
   return map;
 }
 
+const struct line_map *
+linemap_add (struct line_maps *set, enum lc_reason reason,
+	     unsigned int sysp, const char *to_file, linenum_type to_line)
+{
+  return create_and_add_line_map_internal (set, reason, sysp,
+					   to_file, to_line,
+					   NULL);
+}
+
+const struct line_map *
+linemap_enter_macro (struct line_maps *set, struct cpp_macro *macro,
+		     source_location expansion, unsigned int num_tokens)
+{
+  struct line_map *map;
+  source_location start_location = LINEMAPS_MACRO_HIGHEST_LOCATION (set) - 1;
+  line_map_realloc reallocator
+    = set->reallocator ? set->reallocator : xrealloc;
+
+  map = new_linemap (set, LC_ENTER_MACRO);
+
+  MAP_START_LOCATION (map) = start_location;
+  MACRO_MAP_MACRO (map) = macro;
+  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;
+  LINEMAPS_MACRO_HIGHEST_LOCATION (set) =
+    start_location - num_tokens;
+  LINEMAPS_MACRO_HIGHEST_LINE (set) = 
+    start_location - num_tokens;
+
+  return map;
+}
+
+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;
+}
+
 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];
-  source_location highest = set->highest_location;
+  struct line_map *map = LINEMAPS_LAST_ORDINARY_MAP (set);
+  source_location highest = LINEMAPS_ORDINARY_HIGHEST_LOCATION (set);
   source_location r;
-  linenum_type last_line = SOURCE_LINE (map, set->highest_line);
+  linenum_type last_line =
+    SOURCE_LINE (map, LINEMAPS_ORDINARY_HIGHEST_LINE (set));
   int line_delta = to_line - last_line;
   bool add_map = false;
+
+  linemap_check_ordinary (map);
+
   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;
     }
@@ -212,7 +393,7 @@ linemap_line_start (struct line_maps *set, linenum_type to_line,
 	  /* If the column number is ridiculous or we've allocated a huge
 	     number of source_locations, give up on column numbers. */
 	  max_column_hint = 0;
-	  if (highest >0xF0000000)
+	  if (highest > MAX_SOURCE_LOCATION)
 	    return 0;
 	  column_bits = 0;
 	}
@@ -226,19 +407,22 @@ 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);
-  set->highest_line = r;
-  if (r > set->highest_location)
-    set->highest_location = r;
+      + (line_delta << ORDINARY_MAP_NUMBER_OF_COLUMN_BITS (map));
+  LINEMAPS_ORDINARY_HIGHEST_LINE (set) = r;
+  if (r > LINEMAPS_ORDINARY_HIGHEST_LOCATION (set))
+      LINEMAPS_ORDINARY_HIGHEST_LOCATION (set) = r;
   set->max_column_hint = max_column_hint;
   return r;
 }
@@ -246,7 +430,11 @@ linemap_line_start (struct line_maps *set, linenum_type to_line,
 source_location
 linemap_position_for_column (struct line_maps *set, unsigned int to_column)
 {
-  source_location r = set->highest_line;
+  source_location r = LINEMAPS_ORDINARY_HIGHEST_LINE (set);
+
+  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)
@@ -256,35 +444,55 @@ 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);
 	}
     }
   r = r + to_column;
-  if (r >= set->highest_location)
-    set->highest_location = r;
+  if (r >= LINEMAPS_ORDINARY_HIGHEST_LOCATION (set))
+    LINEMAPS_ORDINARY_HIGHEST_LOCATION (set) = r;
   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.  */
+source_location
+linemap_position_for_line_and_column (struct line_map *map,
+				      linenum_type line,
+				      unsigned column)
+{
+  linemap_check_ordinary (map);
+  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)));
+}
 
 const struct line_map *
 linemap_lookup (struct line_maps *set, source_location line)
 {
   unsigned int md, mn, mx;
   const struct line_map *cached;
+  bool macro_map_p;
+
+  if (set ==  NULL)
+    return NULL;
 
-  mn = set->cache;
-  mx = set->used;
+  macro_map_p = linemap_location_from_macro_expansion_p (set, line);
+
+  if (LINEMAPS_MAPS (set, macro_map_p) == NULL)
+    return NULL;
+
+  mn = LINEMAPS_CACHE (set, macro_map_p);
+  mx = LINEMAPS_USED (set, macro_map_p);
   
-  cached = &set->maps[mn];
+  cached = &LINEMAPS_MAPS (set, macro_map_p)[mn];
   /* We should get a segfault if no line_maps have been added yet.  */
-  if (line >= cached->start_location)
+  if (LOCATION_POSSIBLY_IN_MAP_P (line, cached, macro_map_p))
     {
-      if (mn + 1 == mx || line < cached[1].start_location)
+      if (mn + 1 == mx || !LOCATION_POSSIBLY_IN_MAP_P (line,
+						       &cached[1],
+						       macro_map_p))
 	return cached;
     }
   else
@@ -296,14 +504,251 @@ 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 (!LOCATION_POSSIBLY_IN_MAP_P (line,
+				       &LINEMAPS_MAPS (set, macro_map_p)[md],
+				       macro_map_p))
 	mx = md;
       else
 	mn = md;
     }
 
-  set->cache = mn;
-  return &set->maps[mn];
+  LINEMAPS_CACHE (set, macro_map_p) = mn;
+  return &LINEMAPS_MAPS (set, macro_map_p)[mn];
+}
+
+bool
+linemap_macro_expansion_map_p (const struct line_map *map)
+{
+  if (!map)
+    return false;
+  return (map->reason == LC_ENTER_MACRO);
+}
+
+int
+linemap_check_ordinary (const struct line_map *map)
+{
+  linemap_assert (!linemap_macro_expansion_map_p (map));
+  /* Return any old value.  */
+  return 0;
+}
+
+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_POSSIBLY_IN_MACRO_MAP_P (location, map));
+
+  /* Make sure LOCATION is correct.  */
+  token_no = MAP_START_LOCATION (map) - location;
+  linemap_assert (token_no <  MACRO_MAP_NUM_MACRO_TOKENS (map));
+
+  return MACRO_MAP_EXPANSION_POINT_LOCATION (map);
+}
+
+source_location
+linemap_macro_map_loc_to_def_point (const struct line_map *map,
+				    source_location location,
+				    bool return_macro_parm_usage_point_p)
+{
+  unsigned token_no;
+  linemap_assert (linemap_macro_expansion_map_p (map)
+		  && LOCATION_POSSIBLY_IN_MACRO_MAP_P (location, map));
+
+  token_no = MAP_START_LOCATION (map) - location;
+  linemap_assert (token_no < MACRO_MAP_NUM_MACRO_TOKENS (map));
+
+  if (return_macro_parm_usage_point_p)
+    location = MACRO_MAP_LOCATIONS (map)[2 * token_no + 1];
+  else
+    location = MACRO_MAP_LOCATIONS (map)[2 * token_no];
+
+  if (location >= RESERVED_LOCATION_COUNT)
+    return location;
+  else
+    /* If LOCATION is reserved for the user of libcpp, it means,
+     e.g. for gcc that it's the location of a built-in token. In that
+     case, let's say that the final location is the macro expansion
+     point because otherwise, the built-in location would not make
+     any sense and would violate the invariant that says that every
+     single location must be >= to the MAP_START_LOCATION (MAP) of its
+     map.  */
+    return MACRO_MAP_EXPANSION_POINT_LOCATION (map);
+}
+
+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;
+}
+
+int
+linemap_map_get_index (const struct line_maps *set,
+		       const struct line_map* map)
+{
+  int index;
+  bool macro_map_p;
+
+  linemap_assert (set && map);
+
+  macro_map_p = (map->reason == LC_ENTER_MACRO) ? true : false;
+
+  index = map - LINEMAPS_MAPS (set, macro_map_p);
+  linemap_assert (LINEMAPS_MAP_AT (set, macro_map_p, index)  == map);
+
+  return index;
+}
+
+source_location
+linemap_macro_loc_to_def_point (struct line_maps *set,
+				source_location location,
+				const struct line_map **original_map,
+				bool return_macro_parm_usage_point_p)
+{
+  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,
+					    return_macro_parm_usage_point_p);
+    }
+
+  if (original_map)
+    *original_map = map;
+  return location;
+}
+
+int
+linemap_get_source_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);
+  linemap_check_ordinary (map);
+
+  return ((location - MAP_START_LOCATION (map))
+	    >> ORDINARY_MAP_NUMBER_OF_COLUMN_BITS (map))
+	 + ORDINARY_MAP_STARTING_LINE_NUMBER (map);
+}
+
+int
+linemap_get_source_column (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);
+  linemap_check_ordinary (map);
+
+  return (location - MAP_START_LOCATION (map))
+	  & ((1 << ORDINARY_MAP_NUMBER_OF_COLUMN_BITS (map)) - 1);
+}
+
+const char*
+linemap_get_file_path (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);
+  linemap_check_ordinary (map);
+  return LINEMAP_FILE (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)->name);
+}
+
+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_macro_loc_to_def_point (set, location, &map, false);
+  linemap_check_ordinary (map);
+  return LINEMAP_SYSP (map);
+}
+
+bool
+linemap_location_from_macro_expansion_p (struct line_maps *set,
+					 source_location location)
+{
+  linemap_assert (location <= MAX_SOURCE_LOCATION);
+  if (set == NULL)
+    return false;
+  return (location > LINEMAPS_ORDINARY_HIGHEST_LOCATION (set));
+}
+
+bool
+linemap_location_before_p (struct line_maps *set,
+			   source_location  pre,
+			   source_location post)
+{
+  bool pre_from_macro_p, post_from_macro_p;
+
+  if (pre == post)
+    return false;
+
+  pre_from_macro_p =
+    linemap_location_from_macro_expansion_p (set, pre);
+  post_from_macro_p =
+    linemap_location_from_macro_expansion_p (set, post);
+
+  if (pre_from_macro_p != post_from_macro_p)
+    {
+      if (pre_from_macro_p)
+	pre = linemap_macro_loc_to_exp_point (set, pre, NULL);
+      else
+	post = linemap_macro_loc_to_exp_point (set, post, NULL);
+    }
+
+  return pre < post;
 }
 
 /* Print an include trace, for e.g. the -H option of the preprocessor.  */
@@ -315,5 +760,42 @@ trace_include (const struct line_maps *set, const struct line_map *map)
 
   while (--i)
     putc ('.', stderr);
-  fprintf (stderr, " %s\n", map->to_file);
+  linemap_check_ordinary (map);
+  fprintf (stderr, " %s\n", ORDINARY_MAP_FILE_NAME (map));
+}
+
+expanded_location
+linemap_expand_location_full (struct line_maps *set,
+			      source_location loc,
+			      enum location_resolution_kind lrk,
+			      const struct line_map **loc_map)
+{
+  const struct line_map *map;
+  expanded_location xloc;
+
+  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_def_point (set, loc, &map, false);
+      break;
+    case LRK_MACRO_PARM_REPLACEMENT_POINT:
+      loc = linemap_macro_loc_to_def_point (set, loc, &map, true);
+      break;
+    default:
+      abort ();
+    }
+
+  xloc.file = LINEMAP_FILE (map);
+  xloc.line = SOURCE_LINE (map, loc);
+  xloc.column = SOURCE_COLUMN (map, loc);
+  xloc.sysp = LINEMAP_SYSP (map) != 0;
+  if (loc_map)
+    *loc_map = map;
+
+  return xloc;
 }
diff --git a/libcpp/macro.c b/libcpp/macro.c
index eba2349..87ea7f8 100644
--- a/libcpp/macro.c
+++ b/libcpp/macro.c
@@ -171,13 +171,16 @@ _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);
+	map =
+	  linemap_lookup (pfile->line_table,
+			  LINEMAPS_ORDINARY_HIGHEST_LINE (pfile->line_table));
 
 	if (node->value.builtin == BT_BASE_FILE)
 	  while (! MAIN_FILE_P (map))
 	    map = INCLUDED_FROM (pfile->line_table, map);
 
-	name = map->to_file;
+	linemap_check_ordinary (map);
+	name = map->d.ordinary.to_file;
 	len = strlen (name);
 	buf = _cpp_unaligned_alloc (pfile, len * 2 + 3);
 	result = buf;
@@ -196,14 +199,15 @@ _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_source_line (pfile->line_table,
+					CPP_OPTION (pfile, traditional)
+					? LINEMAPS_ORDINARY_HIGHEST_LINE
+					(pfile->line_table)
+					: pfile->cur_token[-1].src_loc);
       break;
 
       /* __STDC__ has the value 1 under normal circumstances.
@@ -1856,6 +1860,7 @@ _cpp_create_definition (cpp_reader *pfile, cpp_hashnode *node)
       (sizeof (cpp_macro));
   else
     macro = (cpp_macro *) _cpp_aligned_alloc (pfile, sizeof (cpp_macro));
+  macro->name = node;
   macro->line = pfile->directive_line;
   macro->params = 0;
   macro->paramc = 0;
diff --git a/libcpp/traditional.c b/libcpp/traditional.c
index 7ff11bb..5034b3f 100644
--- a/libcpp/traditional.c
+++ b/libcpp/traditional.c
@@ -149,7 +149,7 @@ static const uchar *
 copy_comment (cpp_reader *pfile, const uchar *cur, int in_define)
 {
   bool unterminated, copy = false;
-  source_location src_loc = pfile->line_table->highest_line;
+  source_location src_loc = LINEMAPS_ORDINARY_HIGHEST_LINE (pfile->line_table);
   cpp_buffer *buffer = pfile->buffer;
 
   buffer->cur = cur;
@@ -365,7 +365,7 @@ _cpp_scan_out_logical_line (cpp_reader *pfile, cpp_macro *macro)
   CUR (pfile->context) = pfile->buffer->cur;
   RLIMIT (pfile->context) = pfile->buffer->rlimit;
   pfile->out.cur = pfile->out.base;
-  pfile->out.first_line = pfile->line_table->highest_line;
+  pfile->out.first_line = LINEMAPS_ORDINARY_HIGHEST_LINE (pfile->line_table);
   /* start_of_input_line is needed to make sure that directives really,
      really start at the first character of the line.  */
   start_of_input_line = pfile->buffer->cur;
@@ -493,7 +493,8 @@ _cpp_scan_out_logical_line (cpp_reader *pfile, cpp_macro *macro)
 		    {
 		      maybe_start_funlike (pfile, node, out_start, &fmacro);
 		      lex_state = ls_fun_open;
-		      fmacro.line = pfile->line_table->highest_line;
+		      fmacro.line =
+			LINEMAPS_ORDINARY_HIGHEST_LINE (pfile->line_table);
 		      continue;
 		    }
 		  else if (!recursive_macro (pfile, node))
-- 
1.7.6

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

* [PATCH 7/7] Reduce memory waste due to non-power-of-2 allocs
       [not found]   ` <cover.1310824120.git.dodji@redhat.com>
                       ` (5 preceding siblings ...)
  2011-07-16 15:28     ` [PATCH 1/7] Linemap infrastructure for virtual locations Dodji Seketeli
@ 2011-07-16 15:34     ` Dodji Seketeli
  2011-09-12 22:25       ` Jason Merrill
  6 siblings, 1 reply; 135+ messages in thread
From: Dodji Seketeli @ 2011-07-16 15:34 UTC (permalink / raw)
  To: gcc-patches; +Cc: tromey, gdr, joseph, burnus, charlet, paolo, jason

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.

Ideally, I'd prefer some parts of this patch to be integrated into the
memory allocator.  That is, I'd like to see the memory allocator have
an interface that returns the actual size of memory it has allocated.
This would help client code like this one to avoid requesting memory
unnecessarily.  I am proposing this patch as is for now, at least to
see if the approach would be acceptable.

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

libcpp/

	* line-map.c (next_largest_power_of_two): New.
	(new_linemap): Use it.
---
 libcpp/line-map.c |   50 +++++++++++++++++++++++++++++++++++++++++++-------
 1 files changed, 43 insertions(+), 7 deletions(-)

diff --git a/libcpp/line-map.c b/libcpp/line-map.c
index d69160c..327ce85 100644
--- a/libcpp/line-map.c
+++ b/libcpp/line-map.c
@@ -95,6 +95,18 @@ linemap_tracks_macro_expansion_locs_p (struct line_maps *set)
   return LINEMAPS_MACRO_MAPS (set) != NULL;
 }
 
+/* Return the smallest power of two that is greater than N.   */
+static unsigned
+next_largest_power_of_two (unsigned n)
+{
+  n |= (n >> 1);
+  n |= (n >> 2);
+  n |= (n >> 4);
+  n |= (n >> 8);
+  n |= (n >> 16);
+  return n+1;
+}
+
 /* 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
@@ -109,19 +121,43 @@ new_linemap (struct line_maps *set,
   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))
+  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;
+
+      /* 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);
+
+      alloc_size = next_largest_power_of_two (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

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

* Re: [PATCH 0/7] Tracking locations of tokens resulting from macro expansion
  2011-07-16 14:38 ` [PATCH 0/7] " Dodji Seketeli
       [not found]   ` <cover.1310824120.git.dodji@redhat.com>
@ 2011-07-16 16:47   ` Tobias Burnus
  2011-07-16 17:57     ` Dodji Seketeli
  1 sibling, 1 reply; 135+ messages in thread
From: Tobias Burnus @ 2011-07-16 16:47 UTC (permalink / raw)
  To: Dodji Seketeli; +Cc: gcc-patches, tromey, gdr, joseph, charlet, paolo, jason

Dodji Seketeli wrote:
>    Support -fdebug-cpp option

Regarding Fortran: I think having a full support for the macro expansion 
would be quite a lot of work, but I think -fdebug-cpp comes for free as 
it is handled by libcpp.

Thus, how about adding support for that flag also to Fortran?

Tobias

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

* Re: [PATCH 0/7] Tracking locations of tokens resulting from macro expansion
  2011-07-16 16:47   ` [PATCH 0/7] Tracking locations of tokens resulting from macro expansion Tobias Burnus
@ 2011-07-16 17:57     ` Dodji Seketeli
  0 siblings, 0 replies; 135+ messages in thread
From: Dodji Seketeli @ 2011-07-16 17:57 UTC (permalink / raw)
  To: Tobias Burnus; +Cc: gcc-patches, tromey, gdr, joseph, charlet, bonzini, jason

Tobias Burnus <burnus@net-b.de> a écrit:

> Dodji Seketeli wrote:
>>    Support -fdebug-cpp option
>
> Regarding Fortran: I think having a full support for the macro
> expansion would be quite a lot of work,

I know nothing about Fortran, but I would hope that adding support for
this feature to it should not be hard.  Here is what happens basically,
when -ftrack-macro-expansion is on:

1/ locations that are instance of source_location are now virtual
locations.  They encode locations of tokens across macro expansion.

2/ the code that unwinds and prints the "stack" of macro expansion is
the function maybe_unwind_expanded_macro_loc, and is called by the
diagnostic finalizer, which passes it the (now virtual) location of the
diagnostic.

Each front ends provide its own diagnostic finalizer.  By default (in
general_init) I have set this diagnostic finalizer to
virt_loc_aware_diagnostic_finalizer which calls
maybe_unwind_expanded_macro_loc.  This is done in the patch "[PATCH 3/7]
Emit macro expansion related diagnostics".

If Fortran provides its own different diagnostic finalizer, it can call
maybe_unwind_expanded_macro_loc in there.  I believe this should be
enough.

> but I think -fdebug-cpp comes for free as it is handled by libcpp.
> Thus, how about adding support for that flag also to Fortran?

Except that I know zilsh about Fortran and it would take me some time,
at least for testing.  But yes, I think it would be good to have it for
Fortran.  I'll put that on my TODO list, unless someone beats me to it.

-- 
		Dodji

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

* Re: [PATCH 1/7] Linemap infrastructure for virtual locations
  2011-07-16 15:28     ` [PATCH 1/7] Linemap infrastructure for virtual locations Dodji Seketeli
@ 2011-07-18 22:06       ` Jason Merrill
  2011-07-19 10:47         ` Dodji Seketeli
  2011-07-19 23:37       ` Jason Merrill
                         ` (3 subsequent siblings)
  4 siblings, 1 reply; 135+ messages in thread
From: Jason Merrill @ 2011-07-18 22:06 UTC (permalink / raw)
  To: Dodji Seketeli; +Cc: gcc-patches, tromey, gdr, joseph, burnus, charlet, paolo

  On 07/16/2011 10:37 AM, Dodji Seketeli wrote:
> +  /* 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.

Pairs of locations still seems limited, given the flexibility of macro 
expansion; with macros that use other macros a particular token can be 
substituted an arbitrary number of times; the first time doesn't seem 
particularly special.

But then, it seems that nothing uses the replacement point currently. 
The diagnostics mentioned in patch 3 only seem to use the spelling 
location and the expansion location:

> [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

So how do you expect to use the replacement point information?

Jason

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

* Re: [PATCH 1/7] Linemap infrastructure for virtual locations
  2011-07-18 22:06       ` Jason Merrill
@ 2011-07-19 10:47         ` Dodji Seketeli
  2011-07-19 17:26           ` Jason Merrill
  0 siblings, 1 reply; 135+ messages in thread
From: Dodji Seketeli @ 2011-07-19 10:47 UTC (permalink / raw)
  To: Jason Merrill; +Cc: gcc-patches, tromey, gdr, joseph, burnus, charlet, bonzini

Jason Merrill <jason@redhat.com> writes:

>  On 07/16/2011 10:37 AM, Dodji Seketeli wrote:
>> +  /* 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.
>
> Pairs of locations still seems limited, given the flexibility of macro
> expansion; with macros that use other macros a particular token can be
> substituted an arbitrary number of times; the first time doesn't seem
> particularly special.

If you are talking about the case of a macro A that can have (among the
tokens of its replacement list) a token B that itself is a macro, then
this is supported by the current setup.

When A goes through its expansion process it's replaced by the tokens of
its replacement list. The B (like all the other tokens of the
replacement list of A) token is allocated a virtual location that
encodes the {xi,yi} pair, yes.  But that virtual location of B also
encodes the location of the expansion point of B, which is not stored in
the line_map_macro::macro_locations you are talking about here.  It is
stored in the line_map_macro::expansion.

Then when B goes through its expansion process, the same process of
virtual location allocation happens, recursively.

>
> But then, it seems that nothing uses the replacement point
> currently. The diagnostics mentioned in patch 3 only seem to use the
> spelling location and the expansion location:
>
>> [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
>
> So how do you expect to use the replacement point information?

In that example, this diagnostic

    test.c:2:9: note: in expansion of macro 'OPERATE'

actually uses the replacement point information.  It's just that it's
not obvious because the token on which the error happens is not an
argument of a function-like macro.

Consider this example instead:

$ cat -n test11.c 
     1	int var;
     2	#define S(x) 1 + x
     3	int foo (void)
     4	{
     5	  return S( 2 + var() + 3);
     6	}

$ ./cc1 -ftrack-macro-expansion -quiet ../../prtests/test11.c
test11.c: In function ‘foo’:
test11.c:5:20: erreur: called object ‘var’ is not a function
test11.c:2:18: note: in expansion of macro 'S'
test11.c:5:10: note: expanded from here
$ 

In this example, the error happens on the 'var' token which is passed as
an argument to the S function-like macro.  And we want to point, in the
definition of S, to the location where the 'var' argument token is used.
That's the 2:18 location referred to by the line:

    test11.c:2:18: note: in expansion of macro 'S'

-- 
		Dodji

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

* Re: [PATCH 1/7] Linemap infrastructure for virtual locations
  2011-07-19 10:47         ` Dodji Seketeli
@ 2011-07-19 17:26           ` Jason Merrill
  2011-07-19 18:03             ` Dodji Seketeli
  0 siblings, 1 reply; 135+ messages in thread
From: Jason Merrill @ 2011-07-19 17:26 UTC (permalink / raw)
  To: Dodji Seketeli; +Cc: gcc-patches, tromey, gdr, joseph, burnus, charlet, bonzini

On 07/19/2011 05:42 AM, Dodji Seketeli wrote:
> If you are talking about the case of a macro A that can have (among the
> tokens of its replacement list) a token B that itself is a macro, then
> this is supported by the current setup.

I was more thinking of the case of a macro A with a parameter X which is 
passed to macro B, and then macro C:

1: #define A(X) B(X)
2: #define B(X) C(X)
3: #define C(X) X+2
4:
5: A(blah)

what is the replacement point of "blah"?  Is it the use of X on line 3? 
  It seems that we can only have information about the X in one of the 
macro definitions.

Jason

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

* Re: [PATCH 1/7] Linemap infrastructure for virtual locations
  2011-07-19 17:26           ` Jason Merrill
@ 2011-07-19 18:03             ` Dodji Seketeli
  0 siblings, 0 replies; 135+ messages in thread
From: Dodji Seketeli @ 2011-07-19 18:03 UTC (permalink / raw)
  To: Jason Merrill; +Cc: gcc-patches, tromey, gdr, joseph, burnus, charlet, bonzini

Jason Merrill <jason@redhat.com> writes:

> On 07/19/2011 05:42 AM, Dodji Seketeli wrote:
>> If you are talking about the case of a macro A that can have (among the
>> tokens of its replacement list) a token B that itself is a macro, then
>> this is supported by the current setup.
>
> I was more thinking of the case of a macro A with a parameter X which
> is passed to macro B, and then macro C:
>
> 1: #define A(X) B(X)
> 2: #define B(X) C(X)
> 3: #define C(X) X+2
> 4:
> 5: A(blah)

So, let's consider a similar example then:

$ cat -n test12.c 
     1	int var;
     2	#define A(X) B(X)
     3	#define B(X) C(X)
     4	#define C(X) 2+X
     5	
     6	int
     7	foo (void)
     8	{
     9	  return A(blah());
    10	}
$ ./cc1 -ftrack-macro-expansion -quiet ../../prtests/test12.c
test12.c: In function ‘foo’:
test12.c:9:15: erreur: called object ‘var’ is not a function
test12.c:4:16: note: in expansion of macro 'C'
test12.c:3:14: note: expanded from here
test12.c:3:16: note: in expansion of macro 'B'
test12.c:2:14: note: expanded from here
test12.c:2:16: note: in expansion of macro 'A'
test12.c:9:10: note: expanded from here

>
> what is the replacement point of "blah"?  Is it the use of X on line
> 3?

There are going to be 3 macro expansions happening successively.
Eventually, in the context of the expansion of C (indirectly triggered
by the expansion of A), yes, the replacement point of "blah" is going to
be the use of X at line 3.

On my example, the corresponds to the diagnostic line:

    test12.c:4:16: note: in expansion of macro 'C'

> It seems that we can only have information about the X in one of
> the macro definitions.

That is true, in the context of a given macro expansion, e.g, in the
context of the expansion of the macro A, in A(blah).

But then, when libcpp is requested to get the next token (e.g, after
expanding A(blah) into B(blah)) it finds out that the next token B is a
macro itself.  It then expands that macro in a new expansion context.
So we have information about the X in the context of B.  We eventually
reach the expansion context of C that way.

-- 
		Dodji

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

* Re: [PATCH 1/7] Linemap infrastructure for virtual locations
  2011-07-16 15:28     ` [PATCH 1/7] Linemap infrastructure for virtual locations Dodji Seketeli
  2011-07-18 22:06       ` Jason Merrill
@ 2011-07-19 23:37       ` Jason Merrill
  2011-07-30  6:20       ` Jason Merrill
                         ` (2 subsequent siblings)
  4 siblings, 0 replies; 135+ messages in thread
From: Jason Merrill @ 2011-07-19 23:37 UTC (permalink / raw)
  To: Dodji Seketeli; +Cc: gcc-patches, tromey, gdr, joseph, burnus, charlet, paolo

On 07/16/2011 10:37 AM, Dodji Seketeli wrote:
> test12.c:9:15: erreur: called object ‘var’ is not a function
> test12.c:4:16: note: in expansion of macro 'C'
> test12.c:3:14: note: expanded from here
> test12.c:3:16: note: in expansion of macro 'B'
> test12.c:2:14: note: expanded from here
> test12.c:2:16: note: in expansion of macro 'A'
> test12.c:9:10: note: expanded from here

OK, that looks right.  I'm not sure how you get there, but I suppose 
I'll get that from the detailed review.

Jason

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

* Re: [PATCH 1/7] Linemap infrastructure for virtual locations
  2011-07-16 15:28     ` [PATCH 1/7] Linemap infrastructure for virtual locations Dodji Seketeli
  2011-07-18 22:06       ` Jason Merrill
  2011-07-19 23:37       ` Jason Merrill
@ 2011-07-30  6:20       ` Jason Merrill
  2011-08-01 18:54         ` Dodji Seketeli
  2011-08-01  4:42       ` Jason Merrill
  2011-08-02  4:48       ` Jason Merrill
  4 siblings, 1 reply; 135+ messages in thread
From: Jason Merrill @ 2011-07-30  6:20 UTC (permalink / raw)
  To: Dodji Seketeli; +Cc: gcc-patches, tromey, gdr, joseph, burnus, charlet, paolo

On 07/16/2011 07:37 AM, Dodji Seketeli wrote:
> +/* Returns the highest location [of a token resulting from macro
> +   expansion] encoded in this line table.  */
> +#define LINEMAPS_MACRO_HIGHEST_LOCATION(SET) \
> +  LINEMAPS_HIGHEST_LOCATION(SET, true)
> +
> +/* Returns the location of the begining of the highest line
> +   -- containing a token resulting from macro expansion --  encoded
> +   in the line table SET.  */
> +#define LINEMAPS_MACRO_HIGHEST_LINE(SET) \
> +  LINEMAPS_HIGHEST_LINE(SET, true)

What is the use of these?  The ordinary highest line/location are used 
for various things, but I can't think of a reason you would want the 
above, nor are they used in any of the patches.  Maybe these should be 
in line_maps instead of maps_info?

Jason

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

* Re: [PATCH 1/7] Linemap infrastructure for virtual locations
  2011-07-16 15:28     ` [PATCH 1/7] Linemap infrastructure for virtual locations Dodji Seketeli
                         ` (2 preceding siblings ...)
  2011-07-30  6:20       ` Jason Merrill
@ 2011-08-01  4:42       ` Jason Merrill
  2011-08-02  4:48       ` Jason Merrill
  4 siblings, 0 replies; 135+ messages in thread
From: Jason Merrill @ 2011-08-01  4:42 UTC (permalink / raw)
  To: Dodji Seketeli; +Cc: gcc-patches, tromey, gdr, joseph, burnus, charlet, paolo

On 07/16/2011 07:37 AM, Dodji Seketeli wrote:
> -extern void linemap_init (struct line_maps *);
> +void linemap_init (struct line_maps *);
>
> -/* Free a line map set.  */
> -extern void linemap_free (struct line_maps *);
> +/* Free a line map set.  This should be called only if maps have NOT
> +   been allocated in garbage memory space.  */
> +void linemap_free (struct line_maps *);
>
>  /* Check for and warn about line_maps entered but not exited.  */
> +void linemap_check_files_exited (struct line_maps *);
>
> -extern void linemap_check_files_exited (struct line_maps *);
> +/* 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 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).  */
> -
> -extern source_location linemap_line_start
> +   (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);

Apart from the added declaration, these changes seem like unnecessary 
churn; let's avoid reformatting unchanged comments and removing "extern" 
from declarations that are correct as they are.

Jason

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

* Re: [PATCH 1/7] Linemap infrastructure for virtual locations
  2011-07-30  6:20       ` Jason Merrill
@ 2011-08-01 18:54         ` Dodji Seketeli
  0 siblings, 0 replies; 135+ messages in thread
From: Dodji Seketeli @ 2011-08-01 18:54 UTC (permalink / raw)
  To: Jason Merrill; +Cc: gcc-patches, tromey, gdr, joseph, burnus, charlet, paolo

Jason Merrill <jason@redhat.com> writes:

> On 07/16/2011 07:37 AM, Dodji Seketeli wrote:
>> +/* Returns the highest location [of a token resulting from macro
>> +   expansion] encoded in this line table.  */
>> +#define LINEMAPS_MACRO_HIGHEST_LOCATION(SET) \
>> +  LINEMAPS_HIGHEST_LOCATION(SET, true)
>> +
>> +/* Returns the location of the begining of the highest line
>> +   -- containing a token resulting from macro expansion --  encoded
>> +   in the line table SET.  */
>> +#define LINEMAPS_MACRO_HIGHEST_LINE(SET) \
>> +  LINEMAPS_HIGHEST_LINE(SET, true)
>
> What is the use of these?  The ordinary highest line/location are used
> for various things, but I can't think of a reason you would want the
> above, nor are they used in any of the patches.

The former is necessary because locations yielded by original macro maps
grow from zero to upward while locations yielded by macro maps grow from
a MAX_SOURCE_LOCATION downward.  As locations of macro locations
decrease monotonically we need LINEMAPS_MACRO_HIGHEST_LOCATION (used in
linemap_enter_macro) for reasons that are similar to why we need
LINEMAPS_ORDINARY_HIGHEST_LOCATION.

I added LINEMAPS_MACRO_HIGHEST_LINE for symmetry, as we had
LINEMAPS_ORDINARY_HIGHEST_LINE.  It's also because at some point I
thought that I'd need a function like linemap_position_for_column, but
for macro locations.  This could be useful to e.g, create source
locations for macro tokens that we are re-constructing.  E.g, to support
macro locations with -fpreprocessed.  I stepped my ambitions down for
this first batch of patches and I forgot to remove this macro.  I'll
remove it from my local updated version of the patch.

>  Maybe these should be in line_maps instead of maps_info?

I think the former should stay in maps_info as each type of map (macro
and ordinary) need its own highest location.

Jason Merrill <jason@redhat.com> writes:

> On 07/16/2011 07:37 AM, Dodji Seketeli wrote:

> Apart from the added declaration, these changes seem like unnecessary
> churn; let's avoid reformatting unchanged comments and removing
> "extern" from declarations that are correct as they are.

I am cleaning up this in my local version of the patch.

Thanks.

-- 
		Dodji

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

* Re: [PATCH 1/7] Linemap infrastructure for virtual locations
  2011-07-16 15:28     ` [PATCH 1/7] Linemap infrastructure for virtual locations Dodji Seketeli
                         ` (3 preceding siblings ...)
  2011-08-01  4:42       ` Jason Merrill
@ 2011-08-02  4:48       ` Jason Merrill
  2011-08-04 15:28         ` Dodji Seketeli
  4 siblings, 1 reply; 135+ messages in thread
From: Jason Merrill @ 2011-08-02  4:48 UTC (permalink / raw)
  To: Dodji Seketeli; +Cc: gcc-patches, tromey, gdr, joseph, burnus, charlet, paolo

On 07/16/2011 10:37 AM, Dodji Seketeli wrote:
> As locations of macro locations
> decrease monotonically we need LINEMAPS_MACRO_HIGHEST_LOCATION (used in
> linemap_enter_macro) for reasons that are similar to why we need
> LINEMAPS_ORDINARY_HIGHEST_LOCATION.

Ah, I see, I was missing the use in linemap_enter_macro.  But then it's 
really LINEMAPS_MACRO_LOWEST_LOCATION, isn't it? :)

And I think you don't really need it: since we know exactly how many 
tokens a macro map has when we create it, you could just put the lower 
bound in start_location and index upward from there so we don't need 
LOCATION_POSSIBLY_IN_MAP_P (which is a strange name for an ordering 
predicate).

> +      bool expand_to_expansion_point_p =
> +       !linemap_tracks_macro_expansion_locs_p (line_table);
> +
> +    /* If LOC is the location of a token resulting from macro
> +       expansion, either expand to the location of the macro expansion
> +       point if we don't support tracking token locations accross
> +       macro expansion, or expand to the actual spelling location of
> +       the token where the error is if we do support tracking
> +       locations accross macro expansion.  */
> +      xloc =
> +       linemap_expand_location_full (line_table, loc,
> +                                     (expand_to_expansion_point_p)
> +                                     ? LRK_MACRO_EXPANSION_POINT
> +                                     : LRK_SPELLING_LOCATION,
> +                                     NULL);

Why is this logic here?  It seems like it would be better to just pass 
LRK_SPELLING_LOCATION and have linemap_expand_location_full do the best 
it can.

Also, in light of:

>         * include/line-map.h (linemap_expand_location_full): Add an output
>         parameter returning the spelling location the input location got
>         resolved to, at declaration point ...
>         * line-map.c (linemap_expand_location_full): ... and at definition
>         point.

it seems like what you really want is two functions, one that resolves 
one location to another, and a different function that just expands the 
result.

> +   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

Hmm.  The locations of the tokens in the macro definition are shared 
between all expansions of a particular macro, so I wonder if it would be 
possible to encode only the information about the actual macro 
invocation, i.e. the expansion point and the locations of the argument 
tokens.  That ought to be smaller than repeating all the macro token 
locations.

> +   ORIG_LOC is the orginal location of the token at the definition
> +   point of the macro. If you read the extensive comments of struct
> +   line_map_macro in line-map.h, this is the xI.

> +   If the token is part of a macro argument, ORIG_PARM_REPLACEMENT_LOC
> +   is the location of the point at wich the token (the argument)
> +   replaces the macro parameter in the context of the relevant macro
> +   definition. If you read the comments of struct line_map_macro in
> +   line-map.h, this is the yI.  */

I continue to find this description confusing.  In:

> +       int a = PLUS (1,2); <--- #2

Which of these is the location of "1"?  I'd think it would be an xI, but 
that isn't in the definition of a macro.

Jason

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

* Re: [PATCH 1/7] Linemap infrastructure for virtual locations
  2011-08-02  4:48       ` Jason Merrill
@ 2011-08-04 15:28         ` Dodji Seketeli
  2011-08-04 21:30           ` Jason Merrill
  0 siblings, 1 reply; 135+ messages in thread
From: Dodji Seketeli @ 2011-08-04 15:28 UTC (permalink / raw)
  To: Jason Merrill; +Cc: gcc-patches, tromey, gdr, joseph, burnus, charlet, bonzini

Jason Merrill <jason@redhat.com> writes:

> On 07/16/2011 10:37 AM, Dodji Seketeli wrote:
> > As locations of macro locations
> > decrease monotonically we need LINEMAPS_MACRO_HIGHEST_LOCATION (used in
> > linemap_enter_macro) for reasons that are similar to why we need
> > LINEMAPS_ORDINARY_HIGHEST_LOCATION.
> 
> Ah, I see, I was missing the use in linemap_enter_macro.  But then
> it's really LINEMAPS_MACRO_LOWEST_LOCATION, isn't it? :)

Indeed.

> 
> And I think you don't really need it: since we know exactly how many
> tokens a macro map has when we create it, you could just put the lower
> bound in start_location and index upward from there so we don't need
> LOCATION_POSSIBLY_IN_MAP_P (which is a strange name for an ordering
> predicate).

Correct.  I have updated the patch to arrange for the start_location
of each macro map to contain the smallest value of locations encoded
by the map, like for ordinary maps.  Macro maps are still sorted so
that the map at index 0 has higher locations than the map at index 1,
etc.  This is in reverse order, compared to ordinary maps.

I have changed linemap_lookup accordingly, but I felt that trying to
search both types of maps in a single place was hindering legibility
so I did split that function.

> 
> > +      bool expand_to_expansion_point_p =
> > +       !linemap_tracks_macro_expansion_locs_p (line_table);
> > +
> > +    /* If LOC is the location of a token resulting from macro
> > +       expansion, either expand to the location of the macro expansion
> > +       point if we don't support tracking token locations accross
> > +       macro expansion, or expand to the actual spelling location of
> > +       the token where the error is if we do support tracking
> > +       locations accross macro expansion.  */
> > +      xloc =
> > +       linemap_expand_location_full (line_table, loc,
> > +                                     (expand_to_expansion_point_p)
> > +                                     ? LRK_MACRO_EXPANSION_POINT
> > +                                     : LRK_SPELLING_LOCATION,
> > +                                     NULL);
> 
> Why is this logic here?  It seems like it would be better to just pass
> LRK_SPELLING_LOCATION and have linemap_expand_location_full do the
> best it can.

I guess it's a fallout from the times when the whole thing wasn't
ironed out.  Good catch.  I removed it.

> 
> Also, in light of:
> 
> >         * include/line-map.h (linemap_expand_location_full): Add an output
> >         parameter returning the spelling location the input location got
> >         resolved to, at declaration point ...
> >         * line-map.c (linemap_expand_location_full): ... and at definition
> >         point.
> 
> it seems like what you really want is two functions, one that resolves
> one location to another, and a different function that just expands
> the result.

Done.

> 
> > +   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
> 
> Hmm.  The locations of the tokens in the macro definition are shared
> between all expansions of a particular macro, so I wonder if it would
> be possible to encode only the information about the actual macro
> invocation, i.e. the expansion point and the locations of the argument
> tokens.  That ought to be smaller than repeating all the macro token
> locations.

Yes, Tom and I thought about that, and decided that, this could be an
optimization that could add later when the whole thing is known to
work.

Basically, that would require us to "replay" a part of the macro
expansion process, when we are about to decode a given location.  This
would be similar to what replace_args does to know where to stick
macro arguments in the context of a given expansion.

It not particularly trivial, as libcpp introduces invisible tokens in
the expansion of the macro, so we need to take these in account when
encoding the locations.

Even more debugging fun.  :-)

> > +   ORIG_LOC is the orginal location of the token at the definition
> > +   point of the macro. If you read the extensive comments of struct
> > +   line_map_macro in line-map.h, this is the xI.
> 
> > +   If the token is part of a macro argument, ORIG_PARM_REPLACEMENT_LOC
> > +   is the location of the point at wich the token (the argument)
> > +   replaces the macro parameter in the context of the relevant macro
> > +   definition. If you read the comments of struct line_map_macro in
> > +   line-map.h, this is the yI.  */
> 
> I continue to find this description confusing.  In:
> 
> > +       int a = PLUS (1,2); <--- #2
> 
> Which of these is the location of "1"?  I'd think it would be an xI,
> but that isn't in the definition of a macro.

xI is going to be set to the spelling location of that "1" token.  In
other words, it is going to be set to the location pointed to by the
caret below:

    int a = PLUS (1,2);
		  ^
yI is going to be set to the spelling location of A (in the definition
of PLUS).  That is the location pointed to by the caret below:

    #define PLUS(A,B) A + B
		      ^

Below is the amended version of this patch 1/7.  While looking at it, I
have also removed some hunks that didn't make sense anymore in this
iteration of the set.  I have also updated the patch "[PATCH 3/7] Emit
macro expansion related diagnostics".  I am sending it right away as a
reply to the message containing its initial version.

From: Dodji Seketeli <dodji@redhat.com>
Date: Fri, 3 Dec 2010 13:20:26 +0100
Subject: [PATCH 1/7] Linemap infrastructure for virtual locations

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.

Boostrapped with --enable-languages=all,ada and passed regression
tests on x86_unknown-linux-gnu against trunk.

libcpp/

	* include/cpp-id-data.h (struct cpp_macro)<name>: New field for
	diagnostics purposes.
	* 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_position_for_line_and_column)
	(linemap_tracks_macro_expansion_locs_p, linemap_enter_macro)
	(linemap_add_macro_token, linemap_check_ordinary)
	(linemap_macro_expansion_map_p, linemap_macro_loc_to_exp_point)
	(linemap_macro_loc_to_def_point)
	(linemap_macro_map_loc_to_def_point)
	(linemap_macro_map_loc_to_exp_point, linemap_map_get_index)
	(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 cpp-id-data.h
	(linemap_assert): New macro.
	(new_linemap): Define new static functions.  Extracted and
	enhanced from ...
	(linemap_add): ... here.
	(linemap_tracks_macro_expansion_locs_p, linemap_enter_macro)
	(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_loc_to_exp_point, linemap_map_get_index)
	(linemap_macro_loc_to_def_point, 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_location_before_p):
	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.  Use the
	new MAX_SOURCE_LOCATION constant.
	(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.
	* 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/
	* 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/c-family

	* 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/java/

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

gcc/ada/

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

gcc/fortran/

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

gcc/testsuite/

	* gcc.dg/cpp/pragma-diagnostic-1.c: New test.
---
 gcc/ada/gcc-interface/trans.c                  |   10 +-
 gcc/c-family/c-lex.c                           |    6 +-
 gcc/c-family/c-ppoutput.c                      |   41 +-
 gcc/diagnostic.c                               |   11 +-
 gcc/fortran/cpp.c                              |   22 +-
 gcc/input.c                                    |    9 +-
 gcc/input.h                                    |   20 +-
 gcc/java/jcf-parse.c                           |    2 +-
 gcc/testsuite/gcc.dg/cpp/pragma-diagnostic-1.c |   32 ++
 libcpp/directives.c                            |   16 +-
 libcpp/files.c                                 |    5 +-
 libcpp/include/cpp-id-data.h                   |    6 +
 libcpp/include/line-map.h                      |  702 ++++++++++++++++++++++--
 libcpp/init.c                                  |    4 +-
 libcpp/internal.h                              |    3 +-
 libcpp/line-map.c                              |  644 +++++++++++++++++++---
 libcpp/macro.c                                 |   14 +-
 17 files changed, 1338 insertions(+), 209 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 f9acdcd..bb842d5 100644
--- a/gcc/ada/gcc-interface/trans.c
+++ b/gcc/ada/gcc-interface/trans.c
@@ -275,7 +275,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.  */
@@ -7823,12 +7823,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..92e7fd3 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 = LOCATION_FILE (src_loc);
 
       /* 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,8 @@ 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_macro_loc_to_exp_point (line_table, line, &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 a40442e..5eed969 100644
--- a/gcc/fortran/cpp.c
+++ b/gcc/fortran/cpp.c
@@ -810,27 +810,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);
@@ -927,7 +929,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..835c95a 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 (in_system_header_at (input_location))
+#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/cpp-id-data.h b/libcpp/include/cpp-id-data.h
index a57edad..8260fd0 100644
--- a/libcpp/include/cpp-id-data.h
+++ b/libcpp/include/cpp-id-data.h
@@ -34,6 +34,12 @@ struct GTY(()) answer {
 /* Each macro definition is recorded in a cpp_macro structure.
    Variadic macros cannot occur with traditional cpp.  */
 struct GTY(()) cpp_macro {
+  /* Name of this macro.  Used only for error reporting.  */
+  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")))
+    name;
+
   /* Parameters, if any.  */
   cpp_hashnode ** GTY ((nested_ptr (union tree_node,
 		"%h ? CPP_HASHNODE (GCC_IDENT_TO_HT_IDENT (%h)) : NULL",
diff --git a/libcpp/include/line-map.h b/libcpp/include/line-map.h
index f1d5bee..f7ac3ed 100644
--- a/libcpp/include/line-map.h
+++ b/libcpp/include/line-map.h
@@ -27,13 +27,23 @@ 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
+  /* stringize */
+  /* paste */
+};
 
 /* The type of line numbers.  */
 typedef unsigned int linenum_type;
@@ -44,37 +54,226 @@ 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 {
+   means "entire file/line" or "unknown line/column" or "not
+   applicable".)
+
+   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 0xF0000000
+
+struct GTY(()) cpp_macro;
+
+/* A macro line map encodes locations 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_macro *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 we are at the expansion point of MACRO.  Each xI is the
+     location of the Ith token of the replacement-list. Now it gets
+     confusing. the xI is the location of the Ith token of the
+     replacement-list at the macro *definition* point. Not at the
+     macro replacement point. Okay, let's try to explain this below.
+
+     Imagine this:
+
+        #define OPERATION(OP0, OPERATOR, OP1) \
+                OP0 OPERATOR OP1 <-- #0
+
+	#define PLUS(A, B) OPERATION (A, +, B)  <--- #1
+
+	int a = PLUS (1,2); <--- #2
+     
+     In #2, there is a macro map for the expansion of PLUS. PLUS is
+     expanded into the replacement-list made of the tokens:
+     
+        OPERATION, (, A, +, B, )
+
+     and then further expanded into the tokens:
+
+        1, +, 2.
+
+     Let's consider the case of token "+" here. That will help us
+     understand what the xI we were talking about earlier means.
+
+     The token '+' has two locations, so to speak. One in the context
+     of the macro *expansion* of PLUS in #2 and one in the context of
+     the macro *definition* of PLUS in #1. These two locations are
+     encoded in the the latter context, somehow in the xI we are
+     talking about.
+
+     xI is roughly the index of the token inside the replacement-list
+     at the expansion point. So for '+', it's index would then be 1
+     [The index of token '1' would be 0 and the index of token 2 would
+     be 1]. So if '+' is our current xI, it is actualy an x1.
+
+     The value of x1 is the location of the token '+' inside the
+     replacement-list of PLUS at the definition point of PLUS. It is
+     its spelling location in #1.
+
+     So x0 would have described the token '1', x1 describes the token
+     '+' and x2 describes the token '2'.
+
+     Now what's the y1 then? Remember, we said macro_locations is an
+     array of pairs (xI,yI). We now know what the xI is, now let's
+     look at the yI.
+
+     Let's look at the token '+' again. We said it has two locations
+     somehow. Actually it has 3. Kind of. As '+' is an argument passed
+     to the macro OPERATION [at the definition point of the macro
+     PLUS], it would be nice to record the source location of the
+     *parameter* of OPERATION that is replaced by the argument '+'.
+     In other words, we want to record the location of the token
+     "OPERATOR" in the replacement-list of OPERATION, at the
+     /definition/ point of OPERATION in #0. And that is y1.
+
+     So when (xI,yI) describes a token that is passed as an argument
+     to a macro M, the yI is the location of the macro parameter that
+     the argument replaces, at the definition point of M. If (xI,yI)
+     does not describe a token that is passed as an argument to a
+     macro, xI == yI.
+   */
+  source_location * GTY((length ("2 * %h.n_tokens"))) macro_locations;
+
+  /* This is the location of the expansion point of the current macro
+     map.  That expansion point 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 {
+    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) (MAP)->d.ordinary.to_file
+
+#define ORDINARY_MAP_STARTING_LINE_NUMBER(MAP) (MAP)->d.ordinary.to_line
+
+#define ORDINARY_MAP_INCLUDER_FILE_INDEX(MAP) (MAP)->d.ordinary.included_from
+
+#define ORDINARY_MAP_IN_SYSTEM_HEADER_P(MAP) (MAP)->d.ordinary.sysp
+
+#define ORDINARY_MAP_NUMBER_OF_COLUMN_BITS(MAP) (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,19 +296,133 @@ 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 *);
 
 /* 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
+   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).  */
 
@@ -117,10 +430,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,39 +446,231 @@ 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 *);
+
+/*
+   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 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_macro*,
+					    source_location,
+					    unsigned int);
+
+/* Create and return a source location for a token that is part of a
+   macro replacement-list at a macro expansion point.
+
+   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 orginal location of the token at the definition
+   point of the macro. If you read the extensive comments of struct
+   line_map_macro in line-map.h, this is the xI.
+
+   If the token is part of a macro argument, ORIG_PARM_REPLACEMENT_LOC
+   is the location of the point at wich the token (the argument)
+   replaces the macro parameter in the context of the relevant macro
+   definition. If you read the comments of struct line_map_macro in
+   line-map.h, this is the yI.  */
+source_location linemap_add_macro_token (const struct line_map *,
+					 unsigned int,
+					 source_location,
+					 source_location);
+
+/* Assert that MAP encodes locations of tokens that are not part of
+   the replacement-list of a macro expansion.  */
+int linemap_check_ordinary (const struct line_map *);
+
+/* 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 *);
+
+/* 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.  */
+source_location linemap_macro_loc_to_exp_point (struct line_maps *,
+						source_location,
+						const struct line_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_loc_to_def_point (struct line_maps *,
+						source_location,
+						const struct line_map **,
+						bool);
+
+/* If LOCATION is the locus of a token in a replacement-list of a
+   macro expansion return the location of said token in the definition
+   of the macro.
+
+   Read the comments of struct line_map and struct line_map_macro in
+   line-map.h to understand what a macro expansion point is.
+
+   If RETURN_MACRO_PARM_USAGE_POINT_P is TRUE and if LOCATION is the
+   locus of a token that is an argument of a macro M, this function
+   returns the locus of the parameter replaced by the argument, in the
+   definition of M. This is the yI in the comments of struct
+   line_map_macro in line-map.h.
+
+   Note that if the token is a builtin the function returns the
+   location of the expansion point of the macro.  */
+source_location linemap_macro_map_loc_to_def_point (const struct line_map*,
+						    source_location,
+						    bool);
+
+/* 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*,
+						    source_location);
+
+/* Return the index of MAP into set.  MAP can be either an ordinary or
+   a macro map.  */
+int linemap_map_get_index (const struct line_maps *,
+			   const struct line_map*);
+
+/* 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_source_line (struct line_maps *,
+			     source_location);
+
+/* Return the column number corresponding to location LOCATION.
+
+   If LOCATION is the source location of token that is part of the
+   replacement-list of a macro expansion return the column number of
+   the macro expansion point.
+
+   SET is the line map set LOCATION comes from.  */
+int linemap_get_source_column (struct line_maps *,
+			       source_location);
+
+/* Return the name of the macro associated to MACRO_MAP.  */
+const char* linemap_map_get_macro_name (const struct line_map*);
+
+/* 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_file_path (struct line_maps *,
+				   source_location);
+
+/* 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 0 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)						\
+  (linemap_check_ordinary (MAP),					\
+    ((((LOC) - (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)				\
+  (linemap_check_ordinary (MAP),			\
+   (((LOC) - (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),				\
+   ((((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),					\
+   ((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),		\
+   ((MAP)->d.ordinary.included_from < 0))
 
 /* Set LOC to a source position that is the same line as the most recent
    linemap_line_start, but with the specified TO_COLUMN column number.  */
@@ -180,9 +687,120 @@ extern const struct line_map *linemap_lookup
       set->highest_location = r; \
     (LOC) = r;			 \
   }} while (0)
-    
 
+/* 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), (MAP)->d.ordinary.to_file)
+
+/* Return the line number this map started encoding location from.  */
+#define LINEMAP_LINE(MAP)					\
+  (linemap_check_ordinary (MAP), (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), (MAP)->d.ordinary.sysp)
+
+/* Return TRUE if PRE denotes a location that is before POST, FALSE
+   otherwise. LINE_MAPS is the set of line maps PRE and POST were
+   allocated from.  */
+bool linemap_location_before_p (struct line_maps *set,
+				source_location   pre,
+				source_location   post);
+
+typedef struct GTY (())
+{
+  /* 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;
+
+enum location_resolution_kind
+{
+  LRK_MACRO_EXPANSION_POINT,
+  LRK_SPELLING_LOCATION,
+  LRK_MACRO_PARM_REPLACEMENT_POINT
+};
+
+
+/* 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.
+
+   Finally, if SPELLING_LOC is not NULL, *RESULTING_LOC is set to the
+   location to which LOC was resolved, and similarly, *LOC_MAP is set
+   to its map.  */
+
+source_location linemap_resolve_location (struct line_maps *,
+					  source_location loc,
+					  enum location_resolution_kind lrk,
+					  const struct line_map **loc_map);
+
+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.  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 5ba6666..3a16d33 100644
--- a/libcpp/init.c
+++ b/libcpp/init.c
@@ -574,7 +574,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 d2872c4..c0fbab8 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)
diff --git a/libcpp/line-map.c b/libcpp/line-map.c
index dd3f11c..0b8f986 100644
--- a/libcpp/line-map.c
+++ b/libcpp/line-map.c
@@ -23,24 +23,27 @@ along with this program; see the file COPYING3.  If not see
 #include "config.h"
 #include "system.h"
 #include "line-map.h"
+#include "cpp-id-data.h"
 
-static void trace_include (const struct line_maps *, const struct line_map *);
+#define linemap_assert(EXPR)			\
+  do {						\
+    if (! (EXPR))				\
+      abort ();					\
+  } while (0)
 
+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);
 /* 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 +54,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 +125,25 @@ 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)))));
 
-  if (set->used == set->allocated)
+  /* If we don't keep our line maps consistent, we can easily
+     segfault.  Don't rely on the client to do it for us.  */
+  if (set->depth == 0)
+    reason = LC_ENTER;
+
+  /* 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,30 +151,35 @@ linemap_add (struct line_maps *set, enum lc_reason reason,
   if (reason == LC_RENAME_VERBATIM)
     reason = LC_RENAME;
 
-  /* If we don't keep our line maps consistent, we can easily
-     segfault.  Don't rely on the client to do it for us.  */
-  if (set->depth == 0)
-    reason = LC_ENTER;
-  else if (reason == LC_LEAVE)
+  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
@@ -149,55 +191,118 @@ 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;
 }
 
+bool
+linemap_tracks_macro_expansion_locs_p (struct line_maps *set)
+{
+  return LINEMAPS_MACRO_MAPS (set) != NULL;
+}
+
+const struct line_map *
+linemap_enter_macro (struct line_maps *set, struct cpp_macro *macro,
+		     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;
+
+  map = new_linemap (set, LC_ENTER_MACRO);
+
+  MAP_START_LOCATION (map) = start_location;
+  MACRO_MAP_MACRO (map) = macro;
+  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;
+}
+
+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;
+}
+
 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;
+
+  linemap_check_ordinary (map);
+
   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;
     }
@@ -211,7 +316,7 @@ linemap_line_start (struct line_maps *set, linenum_type to_line,
 	  /* If the column number is ridiculous or we've allocated a huge
 	     number of source_locations, give up on column numbers. */
 	  max_column_hint = 0;
-	  if (highest >0xF0000000)
+	  if (highest > MAX_SOURCE_LOCATION)
 	    return 0;
 	  column_bits = 0;
 	}
@@ -225,16 +330,21 @@ 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));
   set->highest_line = r;
   if (r > set->highest_location)
     set->highest_location = r;
@@ -246,6 +356,10 @@ 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)
@@ -255,7 +369,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);
 	}
     }
@@ -265,25 +379,53 @@ 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.  */
+source_location
+linemap_position_for_line_and_column (struct line_map *map,
+				      linenum_type line,
+				      unsigned column)
+{
+  linemap_check_ordinary (map);
+  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)));
+}
 
-const struct line_map *
+/* 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;
+
+  if (set ==  NULL || line < RESERVED_LOCATION_COUNT)
+    return NULL;
 
-  mn = set->cache;
-  mx = set->used;
+  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
@@ -295,14 +437,297 @@ 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;
+  
+#ifdef ENABLE_CHECKING
+  linemap_assert (line >= LINEMAPS_MACRO_LOWEST_LOCATION (set));
+#endif
+
+  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;
+    }
+
+  while (mx - mn > 1)
+    {
+      md = (mx + mn) / 2;
+      if (MAP_START_LOCATION (LINEMAPS_MACRO_MAP_AT (set, md)) > line)
+	mn = md;
+      else
+	mx = md;
+    }
+
+  LINEMAPS_MACRO_CACHE (set) = mx;
+  result = LINEMAPS_MACRO_MAP_AT (set, LINEMAPS_MACRO_CACHE (set));
+  linemap_assert (MAP_START_LOCATION (result) <= line);
+
+  return result;
+}
+
+bool
+linemap_macro_expansion_map_p (const struct line_map *map)
+{
+  if (!map)
+    return false;
+  return (map->reason == LC_ENTER_MACRO);
+}
+
+int
+linemap_check_ordinary (const struct line_map *map)
+{
+  linemap_assert (!linemap_macro_expansion_map_p (map));
+  /* Return any old value.  */
+  return 0;
+}
+
+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);
+}
+
+source_location
+linemap_macro_map_loc_to_def_point (const struct line_map *map,
+				    source_location location,
+				    bool return_macro_parm_usage_point_p)
+{
+  unsigned token_no;
+  linemap_assert (linemap_macro_expansion_map_p (map)
+		  && location >= MAP_START_LOCATION (map));
+
+  token_no = location - MAP_START_LOCATION (map);
+  linemap_assert (token_no < MACRO_MAP_NUM_MACRO_TOKENS (map));
+
+  if (return_macro_parm_usage_point_p)
+    location = MACRO_MAP_LOCATIONS (map)[2 * token_no + 1];
+  else
+    location = MACRO_MAP_LOCATIONS (map)[2 * token_no];
+
+  if (location >= RESERVED_LOCATION_COUNT)
+    return location;
+  else
+    /* If LOCATION is reserved for the user of libcpp, it means,
+     e.g. for gcc that it's the location of a built-in token. In that
+     case, let's say that the final location is the macro expansion
+     point because otherwise, the built-in location would not make
+     any sense and would violate the invariant that says that every
+     single location must be >= to the MAP_START_LOCATION (MAP) of its
+     map.  */
+    return MACRO_MAP_EXPANSION_POINT_LOCATION (map);
+}
+
+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;
+}
+
+int
+linemap_map_get_index (const struct line_maps *set,
+		       const struct line_map* map)
+{
+  int index;
+  bool macro_map_p;
+
+  linemap_assert (set && map);
+
+  macro_map_p = (map->reason == LC_ENTER_MACRO) ? true : false;
+
+  index = map - LINEMAPS_MAPS (set, macro_map_p);
+  linemap_assert (LINEMAPS_MAP_AT (set, macro_map_p, index)  == map);
+
+  return index;
+}
+
+source_location
+linemap_macro_loc_to_def_point (struct line_maps *set,
+				source_location location,
+				const struct line_map **original_map,
+				bool return_macro_parm_usage_point_p)
+{
+  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,
+					    return_macro_parm_usage_point_p);
+    }
+
+  if (original_map)
+    *original_map = map;
+  return location;
+}
+
+int
+linemap_get_source_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);
+  linemap_check_ordinary (map);
+
+  return ((location - MAP_START_LOCATION (map))
+	    >> ORDINARY_MAP_NUMBER_OF_COLUMN_BITS (map))
+	 + ORDINARY_MAP_STARTING_LINE_NUMBER (map);
+}
+
+int
+linemap_get_source_column (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);
+  linemap_check_ordinary (map);
+
+  return (location - MAP_START_LOCATION (map))
+	  & ((1 << ORDINARY_MAP_NUMBER_OF_COLUMN_BITS (map)) - 1);
+}
+
+const char*
+linemap_get_file_path (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);
+  linemap_check_ordinary (map);
+  return LINEMAP_FILE (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)->name);
+}
+
+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_macro_loc_to_def_point (set, location, &map, false);
+  linemap_check_ordinary (map);
+  return LINEMAP_SYSP (map);
+}
+
+bool
+linemap_location_from_macro_expansion_p (struct line_maps *set,
+					 source_location location)
+{
+  linemap_assert (location <= MAX_SOURCE_LOCATION);
+  if (set == NULL)
+    return false;
+  return (location > set->highest_location);
+}
+
+bool
+linemap_location_before_p (struct line_maps *set,
+			   source_location  pre,
+			   source_location post)
+{
+  bool pre_from_macro_p, post_from_macro_p;
+
+  if (pre == post)
+    return false;
+
+  pre_from_macro_p =
+    linemap_location_from_macro_expansion_p (set, pre);
+  post_from_macro_p =
+    linemap_location_from_macro_expansion_p (set, post);
+
+  if (pre_from_macro_p != post_from_macro_p)
+    {
+      if (pre_from_macro_p)
+	pre = linemap_macro_loc_to_exp_point (set, pre, NULL);
+      else
+	post = linemap_macro_loc_to_exp_point (set, post, NULL);
+    }
+
+  return pre < post;
 }
 
 /* Print an include trace, for e.g. the -H option of the preprocessor.  */
@@ -314,5 +739,60 @@ trace_include (const struct line_maps *set, const struct line_map *map)
 
   while (--i)
     putc ('.', stderr);
-  fprintf (stderr, " %s\n", map->to_file);
+  linemap_check_ordinary (map);
+  fprintf (stderr, " %s\n", ORDINARY_MAP_FILE_NAME (map));
+}
+
+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_def_point (set, loc, map, false);
+      break;
+    case LRK_MACRO_PARM_REPLACEMENT_POINT:
+      loc = linemap_macro_loc_to_def_point (set, loc, map, true);
+      break;
+    default:
+      abort ();
+    }
+  return loc;
 }
+
+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;
+}
+
+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..6f55a1e 100644
--- a/libcpp/macro.c
+++ b/libcpp/macro.c
@@ -177,7 +177,8 @@ _cpp_builtin_macro_text (cpp_reader *pfile, cpp_hashnode *node)
 	  while (! MAIN_FILE_P (map))
 	    map = INCLUDED_FROM (pfile->line_table, map);
 
-	name = map->to_file;
+	linemap_check_ordinary (map);
+	name = ORDINARY_MAP_FILE_NAME (map);
 	len = strlen (name);
 	buf = _cpp_unaligned_alloc (pfile, len * 2 + 3);
 	result = buf;
@@ -196,14 +197,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_source_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.
@@ -1856,6 +1857,7 @@ _cpp_create_definition (cpp_reader *pfile, cpp_hashnode *node)
       (sizeof (cpp_macro));
   else
     macro = (cpp_macro *) _cpp_aligned_alloc (pfile, sizeof (cpp_macro));
+  macro->name = node;
   macro->line = pfile->directive_line;
   macro->params = 0;
   macro->paramc = 0;
-- 
		Dodji

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

* Re: [PATCH 3/7] Emit macro expansion related diagnostics
  2011-07-16 14:38     ` [PATCH 3/7] Emit macro expansion related diagnostics Dodji Seketeli
@ 2011-08-04 15:32       ` Dodji Seketeli
  2011-09-12 21:54         ` Jason Merrill
  0 siblings, 1 reply; 135+ messages in thread
From: Dodji Seketeli @ 2011-08-04 15:32 UTC (permalink / raw)
  To: gcc-patches; +Cc: tromey, gdr, joseph, burnus, charlet, bonzini, jason

Hello,

Below is an amended version of this patch after Jason's comments at
http://gcc.gnu.org/ml/gcc-patches/2011-08/msg00099.html.

From: Dodji Seketeli <dodji@redhat.com>
Date: Sat, 4 Dec 2010 16:31:35 +0100
Subject: [PATCH 3/7] Emit macro expansion related diagnostics

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

The combination of this patch and the previous ones boostrapped with
--enable-languages=all,ada and passed regression tests on
x86_64-unknown-linux-gnu.

gcc/
	* 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.

gcc/cp/

	* 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/

	* 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.
	* g++.dg/cpp0x/initlist15.C: Discard errors pointing at multiple
	levels of included files.
	* g++.old-deja/g++.robertl/eb43.C: Likewise.
	* g++.old-deja/g++.robertl/eb79.C: Likewise.
	* gcc.target/i386/sse-vect-types.c: Likewise.
---
 gcc/Makefile.in                                 |    2 +-
 gcc/cp/error.c                                  |    5 +-
 gcc/diagnostic.c                                |   14 ++-
 gcc/diagnostic.h                                |    2 +-
 gcc/testsuite/g++.dg/cpp0x/initlist15.C         |    1 +
 gcc/testsuite/g++.old-deja/g++.robertl/eb43.C   |    4 +
 gcc/testsuite/g++.old-deja/g++.robertl/eb79.C   |    4 +
 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/gcc.target/i386/sse-vect-types.c  |    6 +
 gcc/toplev.c                                    |    3 +
 gcc/tree-diagnostic.c                           |  176 ++++++++++++++++++++++-
 gcc/tree-diagnostic.h                           |    3 +-
 16 files changed, 313 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 116a70b..2dae39d 100644
--- a/gcc/Makefile.in
+++ b/gcc/Makefile.in
@@ -2793,7 +2793,7 @@ fold-const.o : fold-const.c $(CONFIG_H) $(SYSTEM_H) coretypes.h $(TM_H) \
    $(GGC_H) $(TM_P_H) langhooks.h $(MD5_H) intl.h $(TARGET_H) \
    $(GIMPLE_H) realmpfr.h $(TREE_FLOW_H)
 diagnostic.o : diagnostic.c $(CONFIG_H) $(SYSTEM_H) coretypes.h \
-   version.h $(INPUT_H) intl.h $(DIAGNOSTIC_H) diagnostic.def
+   version.h $(INPUT_H) intl.h $(DIAGNOSTIC_H) diagnostic.def $(VEC_H)
 opts.o : opts.c $(OPTS_H) $(OPTIONS_H) $(DIAGNOSTIC_CORE_H) $(CONFIG_H) $(SYSTEM_H) \
    coretypes.h $(TM_H) \
    $(DIAGNOSTIC_H) insn-attr-common.h intl.h $(COMMON_TARGET_H) \
diff --git a/gcc/cp/error.c b/gcc/cp/error.c
index d435bbe..4915315 100644
--- a/gcc/cp/error.c
+++ b/gcc/cp/error.c
@@ -2763,7 +2763,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);
@@ -2773,8 +2773,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..06e73a5 100644
--- a/gcc/diagnostic.c
+++ b/gcc/diagnostic.c
@@ -30,6 +30,7 @@ along with GCC; see the file COPYING3.  If not see
 #include "input.h"
 #include "intl.h"
 #include "diagnostic.h"
+#include "vec.h"
 
 #define pedantic_warning_kind(DC)			\
   ((DC)->pedantic_errors ? DK_ERROR : DK_WARNING)
@@ -255,9 +256,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 +266,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_PARM_REPLACEMENT_POINT,
+			    &map);
+
   if (map && diagnostic_last_module_changed (context, map))
     {
       diagnostic_set_last_module (context, map);
@@ -301,7 +305,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/g++.dg/cpp0x/initlist15.C b/gcc/testsuite/g++.dg/cpp0x/initlist15.C
index b75cc81..cca56b1 100644
--- a/gcc/testsuite/g++.dg/cpp0x/initlist15.C
+++ b/gcc/testsuite/g++.dg/cpp0x/initlist15.C
@@ -2,6 +2,7 @@
 
 // Just discard errors pointing at header files
 // { dg-prune-output "include" }
+// { dg-prune-output "        from" }
 
 #include <vector>
 #include <typeinfo>
diff --git a/gcc/testsuite/g++.old-deja/g++.robertl/eb43.C b/gcc/testsuite/g++.old-deja/g++.robertl/eb43.C
index 1dc4328..bd784b1 100644
--- a/gcc/testsuite/g++.old-deja/g++.robertl/eb43.C
+++ b/gcc/testsuite/g++.old-deja/g++.robertl/eb43.C
@@ -6,6 +6,10 @@
 
 // { dg-prune-output "note" }
 
+// Discard errors pointing at header files
+// { dg-prune-output "In file included from" }
+// { dg-prune-output "        from" }
+
 #include <vector>
 #include <algorithm>
 #include <functional>
diff --git a/gcc/testsuite/g++.old-deja/g++.robertl/eb79.C b/gcc/testsuite/g++.old-deja/g++.robertl/eb79.C
index 1c1ad3e..60cc713 100644
--- a/gcc/testsuite/g++.old-deja/g++.robertl/eb79.C
+++ b/gcc/testsuite/g++.old-deja/g++.robertl/eb79.C
@@ -1,5 +1,9 @@
 // { dg-do assemble  }
 // { dg-prune-output "note" }
+
+// Discard errors pointing at header files
+// { dg-prune-output "In file included from" }
+// { dg-prune-output "        from" }
 // Makes bogus x86 assembly code.
 #include <iostream>
 
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/gcc.target/i386/sse-vect-types.c b/gcc/testsuite/gcc.target/i386/sse-vect-types.c
index 9cb6f3e..ce70125 100644
--- a/gcc/testsuite/gcc.target/i386/sse-vect-types.c
+++ b/gcc/testsuite/gcc.target/i386/sse-vect-types.c
@@ -1,6 +1,12 @@
 /* { dg-do compile } */
 /* { dg-options "-O0 -msse2" } */
 
+
+/*
+   Just discard diagnostic prolog about errors in include files
+   { dg-prune-output "In file included from" }
+*/
+
 #include <xmmintrin.h>
 
 __m128d foo1(__m128d z, __m128d  a, int N) { 
diff --git a/gcc/toplev.c b/gcc/toplev.c
index de0a58a..5f63b69 100644
--- a/gcc/toplev.c
+++ b/gcc/toplev.c
@@ -1132,6 +1132,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..be4a4c2 100644
--- a/gcc/tree-diagnostic.c
+++ b/gcc/tree-diagnostic.c
@@ -35,7 +35,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 +47,177 @@ default_tree_diagnostic_starter (diagnostic_context *context,
   pp_set_prefix (context->printer, diagnostic_build_prefix (context,
 							    diagnostic));
 }
+
+/* Unwind the different macro expansions that lead to the token which
+   location is WHERE and emit diagnostics showing the resulting
+   unwound macro expansion stack.  If TOPMOST_EXP_POINT_MAP is
+   non-null, *TOPMOST_EXP_POINT_MAP is set to the map of the expansion
+   point of the top most macro of the stack.  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 **topmost_exp_point_map)
+{
+  typedef struct
+  {
+    const struct line_map *map;
+    source_location where;
+  } loc_t;
+
+  const struct line_map *map, *resolved_map;
+  bool unwind = true;
+  source_location resolved_location;
+  unsigned loc_vec_capacity = 0, num_locs = 0;
+  loc_t *loc_vec = NULL;
+  unsigned ix;
+  loc_t loc, *iter;
+
+#define APPEND_LOC_TO_VEC(LOC)						\
+  if (num_locs >= loc_vec_capacity)					\
+    {									\
+      loc_vec_capacity += 4;						\
+      loc_vec = XRESIZEVEC (loc_t, loc_vec, loc_vec_capacity);		\
+    }									\
+  loc_vec[num_locs++] = LOC;
+
+  map = linemap_lookup (line_table, where);
+  if (!linemap_macro_expansion_map_p (map))
+    return;
+
+  /* Let's unwind the stack of macros that got expanded and that led
+     to the token which location is WHERE.  We are going to store the
+     stack into MAP_VEC, so that we can later walk MAP_VEC backward to
+     display a somewhat meaningful trace of the macro expansion
+     history to the user.  Note that the deepest macro expansion is
+     going to be stored at the beginning of MAP_VEC.  */
+  while (unwind)
+    {
+      loc.where = where;
+      loc.map = map;
+
+      APPEND_LOC_TO_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
+	 *definition* of the macro of MAP, that got expanded at
+	 WHERE. This is basically how we go "up" in the stack of
+	 macro expansions that led to WHERE.  */
+      resolved_location =
+	linemap_macro_map_loc_to_def_point (map, where, false);
+      resolved_map = linemap_lookup (line_table, resolved_location);
+
+      /* If the token at RESOLVED_LOCATION [at macro definition point]
+	 is itself inside an expanded macro then we keep unwinding the
+	 expansion stack by tracing the "parent macro" that got expanded
+	 inside the definition of the macro of MAP...  */
+      if (linemap_macro_expansion_map_p (resolved_map))
+	{
+	  where = resolved_location;
+	  map = resolved_map;
+	}
+      else
+	{
+	  /* Otherwise, let's consider the location of the expansion
+	     point of the macro of MAP. Keep in mind that MAP is a
+	     macro expansion map. To get a "normal map" (i.e a non
+	     macro expansion map) and be done with the unwinding, we
+	     must either consider the location of the location
+	     expansion point of the macro or the location of the token
+	     inside the macro definition that got expanded to
+	     WHERE.  */
+	  where =
+	    linemap_macro_map_loc_to_exp_point (map, where);
+	  map = linemap_lookup (line_table, where);
+	}
+      if (!linemap_macro_expansion_map_p (map))
+	unwind = false;
+    }
+
+  if (topmost_exp_point_map)
+    *topmost_exp_point_map = map;
+
+  /* Walk the map_vec and print the macro expansion stack, unless the
+     topmost macro which expansion triggered this stack [assuming the
+     stack grows downwards] was expanded inside a system header.  */
+  if (!LINEMAP_SYSP (resolved_map))
+    for (ix = 0; num_locs && ix < num_locs; ++ix)
+      {
+	source_location resolved_def_loc = 0, resolved_exp_loc = 0;
+	diagnostic_t saved_kind;
+	const char *saved_prefix;
+	source_location saved_location;
+
+	iter = &loc_vec[ix];
+
+	/* 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_PARM_REPLACEMENT_POINT, NULL);
+
+	/* Resolve the location of the expansion point of the macro
+	   which expansion gave the token at 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_PARM_REPLACEMENT_POINT, 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;
+      }
+
+  free (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 a multiply nested macro expansions, 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 */
-- 
		Dodji

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

* Re: [PATCH 1/7] Linemap infrastructure for virtual locations
  2011-08-04 15:28         ` Dodji Seketeli
@ 2011-08-04 21:30           ` Jason Merrill
  2011-08-05 17:12             ` Dodji Seketeli
  0 siblings, 1 reply; 135+ messages in thread
From: Jason Merrill @ 2011-08-04 21:30 UTC (permalink / raw)
  To: Dodji Seketeli; +Cc: gcc-patches, tromey, gdr, joseph, burnus, charlet, bonzini

On 08/04/2011 11:27 AM, Dodji Seketeli wrote:
> Yes, Tom and I thought about that, and decided that, this could be an
> optimization that could add later when the whole thing is known to
> work.

Makes sense.

> +/* This is the highest possible source location encoded within an
> +   ordinary or macro map.  */
> +#define MAX_SOURCE_LOCATION 0xF0000000

Why not 0xFFFFFFFF?  I'm not sure what the rationale for using that 
value here:

>           /* If the column number is ridiculous or we've allocated a huge
>              number of source_locations, give up on column numbers. */
>           max_column_hint = 0;
> -         if (highest >0xF0000000)
> +         if (highest > MAX_SOURCE_LOCATION)
>             return 0;

was, but I would think that we would be fine to use that upper range for 
macro maps.

> -      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 = LOCATION_FILE (src_loc);

It probably doesn't matter much, but you could just do

print.src_line = file_path;

to avoid an extra expand_location.  Or have an expanded_location 
variable here like you do in fortran/cpp.c.

> +  return ((location - MAP_START_LOCATION (map))
> +           >> ORDINARY_MAP_NUMBER_OF_COLUMN_BITS (map))
> +        + ORDINARY_MAP_STARTING_LINE_NUMBER (map);

This should use SOURCE_LINE.

> +  return (location - MAP_START_LOCATION (map))
> +         & ((1 << ORDINARY_MAP_NUMBER_OF_COLUMN_BITS (map)) - 1);

And SOURCE_COLUMN.

> -   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 {
> +   means "entire file/line" or "unknown line/column" or "not
> +   applicable".)
> +
> +   The highest possible source location is MAX_SOURCE_LOCATION
> +*/

Looks like you didn't need to rewrap the "entire file" line.  Also, the 
trailing */ goes on the last line of text, not on a line by itself.

> -   most recent linemap_add).   MAX_COLUMN_HINT is the highest column
> +   most recent linemap_add).  MAX_COLUMN_HINT is the highest column

Unnecessary reformatting.

>
> +bool
> +linemap_tracks_macro_expansion_locs_p (struct line_maps *set)
> +{
> +  return LINEMAPS_MACRO_MAPS (set) != NULL;
> +}
> +
> +const struct line_map *
> +linemap_enter_macro (struct line_maps *set, struct cpp_macro *macro,
> +                    source_location expansion, unsigned int num_tokens)

Two of the several functions without comments.  You can just copy the 
comments from the header in here.

> +#define linemap_assert(EXPR)                   \
> +  do {                                         \
> +    if (! (EXPR))                              \
> +      abort ();                                        \
> +  } while (0)

This should be controlled by ENABLE_CHECKING.

> +int
> +linemap_check_ordinary (const struct line_map *map)
> +{
> +  linemap_assert (!linemap_macro_expansion_map_p (map));
> +  /* Return any old value.  */
> +  return 0;
> +}

Why does this return int if we aren't interested in the value?  I would 
change it to a macro that returns 'map' so that it can expand to nothing 
if !ENABLE_CHECKING and can be used in-line like the gcc TREE_CHECK macros.

> +bool
> +linemap_location_from_macro_expansion_p (struct line_maps *set,
> +                                        source_location location)
> +{
> +  linemap_assert (location <= MAX_SOURCE_LOCATION);
> +  if (set == NULL)
> +    return false;
> +  return (location > set->highest_location);
> +}

We need to make sure that set->highest_location < 
LINEMAPS_MACRO_LOWEST_LOCATION, either here or anywhere that changes one 
of those two values.

Jason

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

* Re: [PATCH 1/7] Linemap infrastructure for virtual locations
  2011-08-04 21:30           ` Jason Merrill
@ 2011-08-05 17:12             ` Dodji Seketeli
  2011-08-05 17:31               ` Jason Merrill
  0 siblings, 1 reply; 135+ messages in thread
From: Dodji Seketeli @ 2011-08-05 17:12 UTC (permalink / raw)
  To: Jason Merrill; +Cc: gcc-patches, tromey, gdr, joseph, burnus, charlet, bonzini

Jason Merrill <jason@redhat.com> writes:

>
> Why not 0xFFFFFFFF?  I'm not sure what the rationale for using that
> value here:
>
>>           /* If the column number is ridiculous or we've allocated a huge
>>              number of source_locations, give up on column numbers. */
>>           max_column_hint = 0;
>> -         if (highest >0xF0000000)
>> +         if (highest > MAX_SOURCE_LOCATION)
>>             return 0;
>
> was, but I would think that we would be fine to use that upper range
> for macro maps.

I have no idea where the 0xF0000000 comes from.  I have now set it to
0xFFFFFFFF and it passed bootstrap + tests fine.

>
>> -      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 = LOCATION_FILE (src_loc);
>
> It probably doesn't matter much, but you could just do
>
> print.src_line = file_path;
>
> to avoid an extra expand_location.  Or have an expanded_location
> variable here like you do in fortran/cpp.c.

Done.

>
>> +  return ((location - MAP_START_LOCATION (map))
>> +           >> ORDINARY_MAP_NUMBER_OF_COLUMN_BITS (map))
>> +        + ORDINARY_MAP_STARTING_LINE_NUMBER (map);
>
> This should use SOURCE_LINE.
>

Done.

>> +  return (location - MAP_START_LOCATION (map))
>> +         & ((1 << ORDINARY_MAP_NUMBER_OF_COLUMN_BITS (map)) - 1);
>
> And SOURCE_COLUMN.
>

Done.

>> -   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 {
>> +   means "entire file/line" or "unknown line/column" or "not
>> +   applicable".)
>> +
>> +   The highest possible source location is MAX_SOURCE_LOCATION
>> +*/
>
> Looks like you didn't need to rewrap the "entire file" line.  Also,
> the trailing */ goes on the last line of text, not on a line by
> itself.

Fixed.

>
>> -   most recent linemap_add).   MAX_COLUMN_HINT is the highest column
>> +   most recent linemap_add).  MAX_COLUMN_HINT is the highest column
>
> Unnecessary reformatting.
>

Reverted.

>>
>> +bool
>> +linemap_tracks_macro_expansion_locs_p (struct line_maps *set)
>> +{
>> +  return LINEMAPS_MACRO_MAPS (set) != NULL;
>> +}
>> +
>> +const struct line_map *
>> +linemap_enter_macro (struct line_maps *set, struct cpp_macro *macro,
>> +                    source_location expansion, unsigned int num_tokens)
>
> Two of the several functions without comments.  You can just copy the
> comments from the header in here.
>

Done.

>> +#define linemap_assert(EXPR)                   \
>> +  do {                                         \
>> +    if (! (EXPR))                              \
>> +      abort ();                                        \
>> +  } while (0)
>
> This should be controlled by ENABLE_CHECKING.
>

Done.  It's now in line-map.h so that it can reused by ...

>> +int
>> +linemap_check_ordinary (const struct line_map *map)
>> +{
>> +  linemap_assert (!linemap_macro_expansion_map_p (map));
>> +  /* Return any old value.  */
>> +  return 0;
>> +}
>
> Why does this return int if we aren't interested in the value?  I
> would change it to a macro that returns 'map' so that it can expand to
> nothing if !ENABLE_CHECKING and can be used in-line like the gcc
> TREE_CHECK macros.
>

... this.  This is now implemented by a macro as you are suggesting.

>> +bool
>> +linemap_location_from_macro_expansion_p (struct line_maps *set,
>> +                                        source_location location)
>> +{
>> +  linemap_assert (location <= MAX_SOURCE_LOCATION);
>> +  if (set == NULL)
>> +    return false;
>> +  return (location > set->highest_location);
>> +}
>
> We need to make sure that set->highest_location <
> LINEMAPS_MACRO_LOWEST_LOCATION, either here or anywhere that changes
> one of those two values.

Done.

Below is the updated patch.  Along with the rest of the patch set, it
passes bootstraps and regression tests on x86_64-unknown-linux-gnu.

Thanks.

From: Dodji Seketeli <dodji@redhat.com>
Date: Fri, 3 Dec 2010 13:20:26 +0100
Subject: [PATCH 1/7] Linemap infrastructure for virtual locations

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.

Boostrapped with --enable-languages=all,ada and passed regression
tests on x86_unknown-linux-gnu against trunk.

libcpp/

	* include/cpp-id-data.h (struct cpp_macro)<name>: New field for
	diagnostics purposes.
	* 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): New macros.
	(linemap_position_for_line_and_column)
	(linemap_tracks_macro_expansion_locs_p, linemap_enter_macro)
	(linemap_add_macro_token, linemap_macro_expansion_map_p)
	(linemap_macro_loc_to_exp_point, linemap_macro_loc_to_def_point)
	(linemap_macro_map_loc_to_def_point)
	(linemap_macro_map_loc_to_exp_point, 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 cpp-id-data.h
	(linemap_assert): New macro.
	(new_linemap): Define new static functions.  Extracted and
	enhanced from ...
	(linemap_add): ... here.
	(linemap_tracks_macro_expansion_locs_p, linemap_enter_macro)
	(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_loc_to_exp_point, linemap_map_get_index)
	(linemap_macro_loc_to_def_point, 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_location_before_p):
	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.  Use the
	new MAX_SOURCE_LOCATION constant.
	(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.
	* 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/
	* 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/c-family

	* 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/java/

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

gcc/ada/

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

gcc/fortran/

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

gcc/testsuite/

	* gcc.dg/cpp/pragma-diagnostic-1.c: New test.
---
 gcc/ada/gcc-interface/trans.c                  |   10 +-
 gcc/c-family/c-lex.c                           |    6 +-
 gcc/c-family/c-ppoutput.c                      |   41 +-
 gcc/diagnostic.c                               |   11 +-
 gcc/fortran/cpp.c                              |   22 +-
 gcc/input.c                                    |    9 +-
 gcc/input.h                                    |   20 +-
 gcc/java/jcf-parse.c                           |    2 +-
 gcc/testsuite/gcc.dg/cpp/pragma-diagnostic-1.c |   32 +
 libcpp/directives.c                            |   16 +-
 libcpp/files.c                                 |    5 +-
 libcpp/include/cpp-id-data.h                   |    6 +
 libcpp/include/line-map.h                      |  708 ++++++++++++++++++++--
 libcpp/init.c                                  |    4 +-
 libcpp/internal.h                              |    3 +-
 libcpp/line-map.c                              |  804 +++++++++++++++++++++---
 libcpp/macro.c                                 |   14 +-
 17 files changed, 1506 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 f9acdcd..bb842d5 100644
--- a/gcc/ada/gcc-interface/trans.c
+++ b/gcc/ada/gcc-interface/trans.c
@@ -275,7 +275,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.  */
@@ -7823,12 +7823,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..b4bc9ce 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,8 @@ 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_macro_loc_to_exp_point (line_table, line, &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 a40442e..5eed969 100644
--- a/gcc/fortran/cpp.c
+++ b/gcc/fortran/cpp.c
@@ -810,27 +810,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);
@@ -927,7 +929,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..835c95a 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 (in_system_header_at (input_location))
+#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/cpp-id-data.h b/libcpp/include/cpp-id-data.h
index a57edad..8260fd0 100644
--- a/libcpp/include/cpp-id-data.h
+++ b/libcpp/include/cpp-id-data.h
@@ -34,6 +34,12 @@ struct GTY(()) answer {
 /* Each macro definition is recorded in a cpp_macro structure.
    Variadic macros cannot occur with traditional cpp.  */
 struct GTY(()) cpp_macro {
+  /* Name of this macro.  Used only for error reporting.  */
+  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")))
+    name;
+
   /* Parameters, if any.  */
   cpp_hashnode ** GTY ((nested_ptr (union tree_node,
 		"%h ? CPP_HASHNODE (GCC_IDENT_TO_HT_IDENT (%h)) : NULL",
diff --git a/libcpp/include/line-map.h b/libcpp/include/line-map.h
index f1d5bee..05deeba 100644
--- a/libcpp/include/line-map.h
+++ b/libcpp/include/line-map.h
@@ -27,13 +27,23 @@ 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
+  /* stringize */
+  /* paste */
+};
 
 /* The type of line numbers.  */
 typedef unsigned int linenum_type;
@@ -44,37 +54,224 @@ 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 GTY(()) cpp_macro;
+
+/* A macro line map encodes locations 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_macro *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 we are at the expansion point of MACRO.  Each xI is the
+     location of the Ith token of the replacement-list. Now it gets
+     confusing. the xI is the location of the Ith token of the
+     replacement-list at the macro *definition* point. Not at the
+     macro replacement point. Okay, let's try to explain this below.
+
+     Imagine this:
+
+        #define OPERATION(OP0, OPERATOR, OP1) \
+                OP0 OPERATOR OP1 <-- #0
+
+	#define PLUS(A, B) OPERATION (A, +, B)  <--- #1
+
+	int a = PLUS (1,2); <--- #2
+     
+     In #2, there is a macro map for the expansion of PLUS. PLUS is
+     expanded into the replacement-list made of the tokens:
+     
+        OPERATION, (, A, +, B, )
+
+     and then further expanded into the tokens:
+
+        1, +, 2.
+
+     Let's consider the case of token "+" here. That will help us
+     understand what the xI we were talking about earlier means.
+
+     The token '+' has two locations, so to speak. One in the context
+     of the macro *expansion* of PLUS in #2 and one in the context of
+     the macro *definition* of PLUS in #1. These two locations are
+     encoded in the the latter context, somehow in the xI we are
+     talking about.
+
+     xI is roughly the index of the token inside the replacement-list
+     at the expansion point. So for '+', it's index would then be 1
+     [The index of token '1' would be 0 and the index of token 2 would
+     be 1]. So if '+' is our current xI, it is actualy an x1.
+
+     The value of x1 is the location of the token '+' inside the
+     replacement-list of PLUS at the definition point of PLUS. It is
+     its spelling location in #1.
+
+     So x0 would have described the token '1', x1 describes the token
+     '+' and x2 describes the token '2'.
+
+     Now what's the y1 then? Remember, we said macro_locations is an
+     array of pairs (xI,yI). We now know what the xI is, now let's
+     look at the yI.
+
+     Let's look at the token '+' again. We said it has two locations
+     somehow. Actually it has 3. Kind of. As '+' is an argument passed
+     to the macro OPERATION [at the definition point of the macro
+     PLUS], it would be nice to record the source location of the
+     *parameter* of OPERATION that is replaced by the argument '+'.
+     In other words, we want to record the location of the token
+     "OPERATOR" in the replacement-list of OPERATION, at the
+     /definition/ point of OPERATION in #0. And that is y1.
+
+     So when (xI,yI) describes a token that is passed as an argument
+     to a macro M, the yI is the location of the macro parameter that
+     the argument replaces, at the definition point of M. If (xI,yI)
+     does not describe a token that is passed as an argument to a
+     macro, xI == yI.
+   */
+  source_location * GTY((length ("2 * %h.n_tokens"))) macro_locations;
+
+  /* This is the location of the expansion point of the current macro
+     map.  That expansion point 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 {
+    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) (MAP)->d.ordinary.to_file
+
+#define ORDINARY_MAP_STARTING_LINE_NUMBER(MAP) (MAP)->d.ordinary.to_line
+
+#define ORDINARY_MAP_INCLUDER_FILE_INDEX(MAP) (MAP)->d.ordinary.included_from
+
+#define ORDINARY_MAP_IN_SYSTEM_HEADER_P(MAP) (MAP)->d.ordinary.sysp
+
+#define ORDINARY_MAP_NUMBER_OF_COLUMN_BITS(MAP) (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 +294,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 +428,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,39 +444,222 @@ 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 *);
+
+/*
+   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 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_macro*,
+					    source_location,
+					    unsigned int);
+
+/* Create and return a source location for a token that is part of a
+   macro replacement-list at a macro expansion point.
+
+   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 orginal location of the token at the definition
+   point of the macro. If you read the extensive comments of struct
+   line_map_macro in line-map.h, this is the xI.
+
+   If the token is part of a macro argument, ORIG_PARM_REPLACEMENT_LOC
+   is the location of the point at wich the token (the argument)
+   replaces the macro parameter in the context of the relevant macro
+   definition. If you read the comments of struct line_map_macro in
+   line-map.h, this is the yI.  */
+source_location linemap_add_macro_token (const struct line_map *,
+					 unsigned int,
+					 source_location,
+					 source_location);
+
+/* 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 *);
+
+/* 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.  */
+source_location linemap_macro_loc_to_exp_point (struct line_maps *,
+						source_location,
+						const struct line_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_loc_to_def_point (struct line_maps *,
+						source_location,
+						const struct line_map **,
+						bool);
+
+/* If LOCATION is the locus of a token in a replacement-list of a
+   macro expansion return the location of said token in the definition
+   of the macro.
+
+   Read the comments of struct line_map and struct line_map_macro in
+   line-map.h to understand what a macro expansion point is.
+
+   If RETURN_MACRO_PARM_USAGE_POINT_P is TRUE and if LOCATION is the
+   locus of a token that is an argument of a macro M, this function
+   returns the locus of the parameter replaced by the argument, in the
+   definition of M. This is the yI in the comments of struct
+   line_map_macro in line-map.h.
+
+   Note that if the token is a builtin the function returns the
+   location of the expansion point of the macro.  */
+source_location linemap_macro_map_loc_to_def_point (const struct line_map*,
+						    source_location,
+						    bool);
+
+/* 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*,
+						    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 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_source_line (struct line_maps *,
+			     source_location);
+
+/* Return the column number corresponding to location LOCATION.
+
+   If LOCATION is the source location of token that is part of the
+   replacement-list of a macro expansion return the column number of
+   the macro expansion point.
+
+   SET is the line map set LOCATION comes from.  */
+int linemap_get_source_column (struct line_maps *,
+			       source_location);
+
+/* Return the name of the macro associated to MACRO_MAP.  */
+const char* linemap_map_get_macro_name (const struct line_map*);
+
+/* 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_file_path (struct line_maps *,
+				   source_location);
+
+/* 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 0 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)						\
+  (linemap_check_ordinary (MAP),					\
+    ((((LOC) - (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)				\
+  (linemap_check_ordinary (MAP),			\
+   (((LOC) - (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),				\
+   ((((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),					\
+   ((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),		\
+   ((MAP)->d.ordinary.included_from < 0))
 
 /* Set LOC to a source position that is the same line as the most recent
    linemap_line_start, but with the specified TO_COLUMN column number.  */
@@ -180,9 +676,141 @@ extern const struct line_map *linemap_lookup
       set->highest_location = r; \
     (LOC) = r;			 \
   }} while (0)
-    
 
+#ifdef ENABLE_CHECKING
+
+/* 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)
+#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), (MAP)->d.ordinary.to_file)
+
+/* Return the line number this map started encoding location from.  */
+#define LINEMAP_LINE(MAP)					\
+  (linemap_check_ordinary (MAP), (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), (MAP)->d.ordinary.sysp)
+
+/* Return TRUE if PRE denotes a location that is before POST, FALSE
+   otherwise. LINE_MAPS is the set of line maps PRE and POST were
+   allocated from.  */
+bool linemap_location_before_p (struct line_maps *set,
+				source_location   pre,
+				source_location   post);
+
+typedef struct GTY (())
+{
+  /* 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;
+
+enum location_resolution_kind
+{
+  LRK_MACRO_EXPANSION_POINT,
+  LRK_SPELLING_LOCATION,
+  LRK_MACRO_PARM_REPLACEMENT_POINT
+};
+
+
+/* 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.
+
+   Finally, if SPELLING_LOC is not NULL, *RESULTING_LOC is set to the
+   location to which LOC was resolved, and similarly, *LOC_MAP is set
+   to its map.  */
+
+source_location linemap_resolve_location (struct line_maps *,
+					  source_location loc,
+					  enum location_resolution_kind lrk,
+					  const struct line_map **loc_map);
+
+/* Expand source code location LOC and return a user readable source
+   code 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.  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 5ba6666..3a16d33 100644
--- a/libcpp/init.c
+++ b/libcpp/init.c
@@ -574,7 +574,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 d2872c4..c0fbab8 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)
diff --git a/libcpp/line-map.c b/libcpp/line-map.c
index dd3f11c..7738399 100644
--- a/libcpp/line-map.c
+++ b/libcpp/line-map.c
@@ -23,24 +23,23 @@ along with this program; see the file COPYING3.  If not see
 #include "config.h"
 #include "system.h"
 #include "line-map.h"
+#include "cpp-id-data.h"
+
 
-static void trace_include (const struct line_maps *, const struct line_map *);
 
+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);
 /* 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 +50,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 +121,25 @@ 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)))));
+
+  /* If we don't keep our line maps consistent, we can easily
+     segfault.  Don't rely on the client to do it for us.  */
+  if (set->depth == 0)
+    reason = LC_ENTER;
 
-  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,30 +147,35 @@ linemap_add (struct line_maps *set, enum lc_reason reason,
   if (reason == LC_RENAME_VERBATIM)
     reason = LC_RENAME;
 
-  /* If we don't keep our line maps consistent, we can easily
-     segfault.  Don't rely on the client to do it for us.  */
-  if (set->depth == 0)
-    reason = LC_ENTER;
-  else if (reason == LC_LEAVE)
+  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
@@ -149,55 +187,166 @@ 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 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 *set, struct cpp_macro *macro,
+		     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;
+
+  map = new_linemap (set, LC_ENTER_MACRO);
+
+  MAP_START_LOCATION (map) = start_location;
+  MACRO_MAP_MACRO (map) = macro;
+  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 source location for a token that is part of a
+   macro replacement-list at a macro expansion point.
+
+   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 orginal location of the token at the definition
+   point of the macro. If you read the extensive comments of struct
+   line_map_macro in line-map.h, this is the xI.
+
+   If the token is part of a macro argument, ORIG_PARM_REPLACEMENT_LOC
+   is the location of the point at wich the token (the argument)
+   replaces the macro parameter in the context of the relevant macro
+   definition. If you read the comments of struct line_map_macro in
+   line-map.h, this is the yI.  */
+
+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;
+
+  linemap_check_ordinary (map);
+
   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;
     }
@@ -211,7 +360,7 @@ linemap_line_start (struct line_maps *set, linenum_type to_line,
 	  /* If the column number is ridiculous or we've allocated a huge
 	     number of source_locations, give up on column numbers. */
 	  max_column_hint = 0;
-	  if (highest >0xF0000000)
+	  if (highest > MAX_SOURCE_LOCATION)
 	    return 0;
 	  column_bits = 0;
 	}
@@ -225,16 +374,21 @@ 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));
   set->highest_line = r;
   if (r > set->highest_location)
     set->highest_location = r;
@@ -242,10 +396,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)
@@ -255,7 +418,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);
 	}
     }
@@ -265,25 +428,56 @@ 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_check_ordinary (map);
+  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;
+
+  if (set ==  NULL || line < RESERVED_LOCATION_COUNT)
+    return NULL;
 
-  mn = set->cache;
-  mx = set->used;
+  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
@@ -295,14 +489,356 @@ 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;
+    }
+
+  while (mx - mn > 1)
+    {
+      md = (mx + mn) / 2;
+      if (MAP_START_LOCATION (LINEMAPS_MACRO_MAP_AT (set, md)) > line)
+	mn = md;
+      else
+	mx = md;
+    }
+
+  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 locus of a token in a replacement-list of a
+   macro expansion return the location of said token in the definition
+   of the macro.
+
+   Read the comments of struct line_map and struct line_map_macro in
+   line-map.h to understand what a macro expansion point is.
+
+   If RETURN_MACRO_PARM_USAGE_POINT_P is TRUE and if LOCATION is the
+   locus of a token that is an argument of a macro M, this function
+   returns the locus of the parameter replaced by the argument, in the
+   definition of M. This is the yI in the comments of struct
+   line_map_macro in line-map.h.
+
+   Note that if the token is a builtin the function returns the
+   location of the expansion point of the macro.  */
+
+source_location
+linemap_macro_map_loc_to_def_point (const struct line_map *map,
+				    source_location location,
+				    bool return_macro_parm_usage_point_p)
+{
+  unsigned token_no;
+  linemap_assert (linemap_macro_expansion_map_p (map)
+		  && location >= MAP_START_LOCATION (map));
+
+  token_no = location - MAP_START_LOCATION (map);
+  linemap_assert (token_no < MACRO_MAP_NUM_MACRO_TOKENS (map));
+
+  if (return_macro_parm_usage_point_p)
+    location = MACRO_MAP_LOCATIONS (map)[2 * token_no + 1];
+  else
+    location = MACRO_MAP_LOCATIONS (map)[2 * token_no];
+
+  if (location >= RESERVED_LOCATION_COUNT)
+    return location;
+  else
+    /* If LOCATION is reserved for the user of libcpp, it means,
+     e.g. for gcc that it's the location of a built-in token. In that
+     case, let's say that the final location is the macro expansion
+     point because otherwise, the built-in location would not make
+     any sense and would violate the invariant that says that every
+     single location must be >= to the MAP_START_LOCATION (MAP) of its
+     map.  */
+    return MACRO_MAP_EXPANSION_POINT_LOCATION (map);
+}
+
+/* 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.  */
+
+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;
+}
+
+/* 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_loc_to_def_point (struct line_maps *set,
+				source_location location,
+				const struct line_map **original_map,
+				bool return_macro_parm_usage_point_p)
+{
+  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,
+					    return_macro_parm_usage_point_p);
+    }
+
+  if (original_map)
+    *original_map = map;
+  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_source_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 column number corresponding to location LOCATION.
+
+   If LOCATION is the source location of token that is part of the
+   replacement-list of a macro expansion return the column number of
+   the macro expansion point.
+
+   SET is the line map set LOCATION comes from.  */
+
+int
+linemap_get_source_column (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);
+  linemap_check_ordinary (map);
+
+  return SOURCE_COLUMN (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_file_path (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);
+  linemap_check_ordinary (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)->name);
+}
+
+/* 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 0 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_macro_loc_to_def_point (set, location, &map, false);
+  linemap_check_ordinary (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);
+}
+
+/* Return TRUE if PRE denotes a location that is before POST, FALSE
+   otherwise. LINE_MAPS is the set of line maps PRE and POST were
+   allocated from.  */
+
+bool
+linemap_location_before_p (struct line_maps *set,
+			   source_location  pre,
+			   source_location post)
+{
+  bool pre_from_macro_p, post_from_macro_p;
+
+  if (pre == post)
+    return false;
+
+  pre_from_macro_p =
+    linemap_location_from_macro_expansion_p (set, pre);
+  post_from_macro_p =
+    linemap_location_from_macro_expansion_p (set, post);
+
+  if (pre_from_macro_p != post_from_macro_p)
+    {
+      if (pre_from_macro_p)
+	pre = linemap_macro_loc_to_exp_point (set, pre, NULL);
+      else
+	post = linemap_macro_loc_to_exp_point (set, post, NULL);
+    }
+
+  return pre < post;
 }
 
 /* Print an include trace, for e.g. the -H option of the preprocessor.  */
@@ -314,5 +850,109 @@ trace_include (const struct line_maps *set, const struct line_map *map)
 
   while (--i)
     putc ('.', stderr);
-  fprintf (stderr, " %s\n", map->to_file);
+  linemap_check_ordinary (map);
+  fprintf (stderr, " %s\n", ORDINARY_MAP_FILE_NAME (map));
+}
+
+/* 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.
+
+   Finally, if SPELLING_LOC is not NULL, *RESULTING_LOC is set to the
+   location to which LOC was resolved, and similarly, *LOC_MAP is set
+   to its map.  */
+
+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_def_point (set, loc, map, false);
+      break;
+    case LRK_MACRO_PARM_REPLACEMENT_POINT:
+      loc = linemap_macro_loc_to_def_point (set, loc, map, true);
+      break;
+    default:
+      abort ();
+    }
+  return loc;
+}
+
+/* Expand source code location LOC and return a user readable source
+   code 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.  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..6f55a1e 100644
--- a/libcpp/macro.c
+++ b/libcpp/macro.c
@@ -177,7 +177,8 @@ _cpp_builtin_macro_text (cpp_reader *pfile, cpp_hashnode *node)
 	  while (! MAIN_FILE_P (map))
 	    map = INCLUDED_FROM (pfile->line_table, map);
 
-	name = map->to_file;
+	linemap_check_ordinary (map);
+	name = ORDINARY_MAP_FILE_NAME (map);
 	len = strlen (name);
 	buf = _cpp_unaligned_alloc (pfile, len * 2 + 3);
 	result = buf;
@@ -196,14 +197,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_source_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.
@@ -1856,6 +1857,7 @@ _cpp_create_definition (cpp_reader *pfile, cpp_hashnode *node)
       (sizeof (cpp_macro));
   else
     macro = (cpp_macro *) _cpp_aligned_alloc (pfile, sizeof (cpp_macro));
+  macro->name = node;
   macro->line = pfile->directive_line;
   macro->params = 0;
   macro->paramc = 0;
-- 
		Dodji

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

* Re: [PATCH 1/7] Linemap infrastructure for virtual locations
  2011-08-05 17:12             ` Dodji Seketeli
@ 2011-08-05 17:31               ` Jason Merrill
  2011-08-09 14:56                 ` Dodji Seketeli
  0 siblings, 1 reply; 135+ messages in thread
From: Jason Merrill @ 2011-08-05 17:31 UTC (permalink / raw)
  To: Dodji Seketeli; +Cc: gcc-patches, tromey, gdr, joseph, burnus, charlet, bonzini

On 08/05/2011 01:04 PM, Dodji Seketeli wrote:
> Jason Merrill<jason@redhat.com>  writes:
>>
>> Why not 0xFFFFFFFF?  I'm not sure what the rationale for using that
>> value here:
>>
>>>            /* If the column number is ridiculous or we've allocated a huge
>>>               number of source_locations, give up on column numbers. */
>>>            max_column_hint = 0;
>>> -         if (highest>0xF0000000)
>>> +         if (highest>  MAX_SOURCE_LOCATION)
>>>              return 0;
>>
>> was, but I would think that we would be fine to use that upper range
>> for macro maps.
>
> I have no idea where the 0xF0000000 comes from.  I have now set it to
> 0xFFFFFFFF and it passed bootstrap + tests fine.

Sorry, I should have been clearer; I think we want to leave this test 
alone so that ordinary locations don't grow past 0xF0000000, leaving the 
top range for macro locations.

Jason

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

* Re: [PATCH 1/7] Linemap infrastructure for virtual locations
  2011-08-05 17:31               ` Jason Merrill
@ 2011-08-09 14:56                 ` Dodji Seketeli
  2011-08-19  8:46                   ` Jason Merrill
  0 siblings, 1 reply; 135+ messages in thread
From: Dodji Seketeli @ 2011-08-09 14:56 UTC (permalink / raw)
  To: Jason Merrill; +Cc: gcc-patches, tromey, gdr, joseph, burnus, charlet, bonzini

Jason Merrill <jason@redhat.com> writes:

> On 08/05/2011 01:04 PM, Dodji Seketeli wrote:
> > Jason Merrill<jason@redhat.com>  writes:
> >>
> >> Why not 0xFFFFFFFF?  I'm not sure what the rationale for using that
> >> value here:
> >>
> >>>            /* If the column number is ridiculous or we've allocated a huge
> >>>               number of source_locations, give up on column numbers. */
> >>>            max_column_hint = 0;
> >>> -         if (highest>0xF0000000)
> >>> +         if (highest>  MAX_SOURCE_LOCATION)
> >>>              return 0;
> >>
> >> was, but I would think that we would be fine to use that upper range
> >> for macro maps.
> >
> > I have no idea where the 0xF0000000 comes from.  I have now set it to
> > 0xFFFFFFFF and it passed bootstrap + tests fine.
> 
> Sorry, I should have been clearer; I think we want to leave this test
> alone so that ordinary locations don't grow past 0xF0000000, leaving
> the top range for macro locations.

Nah, I think I understood what you meant.  It's just that I shouldn't
send patches when I am tired.  :-)

So this is a second try.  I have removed that test altogether.  But
then I figured I should nonetheless handle the case where we run out
of integer space when allocating locations for both ordinary and macro
tokens.  So linemap_line_start hands out 0 in that case (for ordinary
tokens) and linemap_enter_macro hands out NULL in lieu of a macro map
in that case.  That NULL map is handled (in the second patch of the
series) so that the spelling location of the macro token is used in
that case.

The entire patch series has passed bootstrap and regression testing on
x86_64-unknown-linux-gnu against trunk.

Thanks.

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.

Boostrapped with --enable-languages=all,ada and passed regression
tests on x86_unknown-linux-gnu against trunk.

libcpp/

	* include/cpp-id-data.h (struct cpp_macro)<name>: New field for
	diagnostics purposes.
	* 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): New macros.
	(linemap_position_for_line_and_column)
	(linemap_tracks_macro_expansion_locs_p, linemap_enter_macro)
	(linemap_add_macro_token, linemap_macro_expansion_map_p)
	(linemap_macro_loc_to_exp_point, linemap_macro_loc_to_def_point)
	(linemap_macro_map_loc_to_def_point)
	(linemap_macro_map_loc_to_exp_point, 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 cpp-id-data.h
	(linemap_assert): New macro.
	(new_linemap): Define new static functions.  Extracted and
	enhanced from ...
	(linemap_add): ... here.
	(linemap_tracks_macro_expansion_locs_p, linemap_enter_macro)
	(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_loc_to_exp_point, linemap_map_get_index)
	(linemap_macro_loc_to_def_point, 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_location_before_p):
	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.
	* 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/
	* 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/c-family

	* 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/java/

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

gcc/ada/

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

gcc/fortran/

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

gcc/testsuite/

	* gcc.dg/cpp/pragma-diagnostic-1.c: New test.
---
 gcc/ada/gcc-interface/trans.c                  |   10 +-
 gcc/c-family/c-lex.c                           |    6 +-
 gcc/c-family/c-ppoutput.c                      |   41 +-
 gcc/diagnostic.c                               |   11 +-
 gcc/fortran/cpp.c                              |   22 +-
 gcc/input.c                                    |    9 +-
 gcc/input.h                                    |   20 +-
 gcc/java/jcf-parse.c                           |    2 +-
 gcc/testsuite/gcc.dg/cpp/pragma-diagnostic-1.c |   32 +
 libcpp/directives.c                            |   16 +-
 libcpp/files.c                                 |    5 +-
 libcpp/include/cpp-id-data.h                   |    6 +
 libcpp/include/line-map.h                      |  708 +++++++++++++++++++--
 libcpp/init.c                                  |    4 +-
 libcpp/internal.h                              |    3 +-
 libcpp/line-map.c                              |  820 +++++++++++++++++++++---
 libcpp/macro.c                                 |   14 +-
 17 files changed, 1521 insertions(+), 208 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 f9acdcd..bb842d5 100644
--- a/gcc/ada/gcc-interface/trans.c
+++ b/gcc/ada/gcc-interface/trans.c
@@ -275,7 +275,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.  */
@@ -7823,12 +7823,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..b4bc9ce 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,8 @@ 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_macro_loc_to_exp_point (line_table, line, &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 a40442e..5eed969 100644
--- a/gcc/fortran/cpp.c
+++ b/gcc/fortran/cpp.c
@@ -810,27 +810,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);
@@ -927,7 +929,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..835c95a 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 (in_system_header_at (input_location))
+#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/cpp-id-data.h b/libcpp/include/cpp-id-data.h
index a57edad..8260fd0 100644
--- a/libcpp/include/cpp-id-data.h
+++ b/libcpp/include/cpp-id-data.h
@@ -34,6 +34,12 @@ struct GTY(()) answer {
 /* Each macro definition is recorded in a cpp_macro structure.
    Variadic macros cannot occur with traditional cpp.  */
 struct GTY(()) cpp_macro {
+  /* Name of this macro.  Used only for error reporting.  */
+  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")))
+    name;
+
   /* Parameters, if any.  */
   cpp_hashnode ** GTY ((nested_ptr (union tree_node,
 		"%h ? CPP_HASHNODE (GCC_IDENT_TO_HT_IDENT (%h)) : NULL",
diff --git a/libcpp/include/line-map.h b/libcpp/include/line-map.h
index f1d5bee..05deeba 100644
--- a/libcpp/include/line-map.h
+++ b/libcpp/include/line-map.h
@@ -27,13 +27,23 @@ 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
+  /* stringize */
+  /* paste */
+};
 
 /* The type of line numbers.  */
 typedef unsigned int linenum_type;
@@ -44,37 +54,224 @@ 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 GTY(()) cpp_macro;
+
+/* A macro line map encodes locations 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_macro *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 we are at the expansion point of MACRO.  Each xI is the
+     location of the Ith token of the replacement-list. Now it gets
+     confusing. the xI is the location of the Ith token of the
+     replacement-list at the macro *definition* point. Not at the
+     macro replacement point. Okay, let's try to explain this below.
+
+     Imagine this:
+
+        #define OPERATION(OP0, OPERATOR, OP1) \
+                OP0 OPERATOR OP1 <-- #0
+
+	#define PLUS(A, B) OPERATION (A, +, B)  <--- #1
+
+	int a = PLUS (1,2); <--- #2
+     
+     In #2, there is a macro map for the expansion of PLUS. PLUS is
+     expanded into the replacement-list made of the tokens:
+     
+        OPERATION, (, A, +, B, )
+
+     and then further expanded into the tokens:
+
+        1, +, 2.
+
+     Let's consider the case of token "+" here. That will help us
+     understand what the xI we were talking about earlier means.
+
+     The token '+' has two locations, so to speak. One in the context
+     of the macro *expansion* of PLUS in #2 and one in the context of
+     the macro *definition* of PLUS in #1. These two locations are
+     encoded in the the latter context, somehow in the xI we are
+     talking about.
+
+     xI is roughly the index of the token inside the replacement-list
+     at the expansion point. So for '+', it's index would then be 1
+     [The index of token '1' would be 0 and the index of token 2 would
+     be 1]. So if '+' is our current xI, it is actualy an x1.
+
+     The value of x1 is the location of the token '+' inside the
+     replacement-list of PLUS at the definition point of PLUS. It is
+     its spelling location in #1.
+
+     So x0 would have described the token '1', x1 describes the token
+     '+' and x2 describes the token '2'.
+
+     Now what's the y1 then? Remember, we said macro_locations is an
+     array of pairs (xI,yI). We now know what the xI is, now let's
+     look at the yI.
+
+     Let's look at the token '+' again. We said it has two locations
+     somehow. Actually it has 3. Kind of. As '+' is an argument passed
+     to the macro OPERATION [at the definition point of the macro
+     PLUS], it would be nice to record the source location of the
+     *parameter* of OPERATION that is replaced by the argument '+'.
+     In other words, we want to record the location of the token
+     "OPERATOR" in the replacement-list of OPERATION, at the
+     /definition/ point of OPERATION in #0. And that is y1.
+
+     So when (xI,yI) describes a token that is passed as an argument
+     to a macro M, the yI is the location of the macro parameter that
+     the argument replaces, at the definition point of M. If (xI,yI)
+     does not describe a token that is passed as an argument to a
+     macro, xI == yI.
+   */
+  source_location * GTY((length ("2 * %h.n_tokens"))) macro_locations;
+
+  /* This is the location of the expansion point of the current macro
+     map.  That expansion point 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 {
+    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) (MAP)->d.ordinary.to_file
+
+#define ORDINARY_MAP_STARTING_LINE_NUMBER(MAP) (MAP)->d.ordinary.to_line
+
+#define ORDINARY_MAP_INCLUDER_FILE_INDEX(MAP) (MAP)->d.ordinary.included_from
+
+#define ORDINARY_MAP_IN_SYSTEM_HEADER_P(MAP) (MAP)->d.ordinary.sysp
+
+#define ORDINARY_MAP_NUMBER_OF_COLUMN_BITS(MAP) (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 +294,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 +428,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,39 +444,222 @@ 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 *);
+
+/*
+   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 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_macro*,
+					    source_location,
+					    unsigned int);
+
+/* Create and return a source location for a token that is part of a
+   macro replacement-list at a macro expansion point.
+
+   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 orginal location of the token at the definition
+   point of the macro. If you read the extensive comments of struct
+   line_map_macro in line-map.h, this is the xI.
+
+   If the token is part of a macro argument, ORIG_PARM_REPLACEMENT_LOC
+   is the location of the point at wich the token (the argument)
+   replaces the macro parameter in the context of the relevant macro
+   definition. If you read the comments of struct line_map_macro in
+   line-map.h, this is the yI.  */
+source_location linemap_add_macro_token (const struct line_map *,
+					 unsigned int,
+					 source_location,
+					 source_location);
+
+/* 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 *);
+
+/* 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.  */
+source_location linemap_macro_loc_to_exp_point (struct line_maps *,
+						source_location,
+						const struct line_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_loc_to_def_point (struct line_maps *,
+						source_location,
+						const struct line_map **,
+						bool);
+
+/* If LOCATION is the locus of a token in a replacement-list of a
+   macro expansion return the location of said token in the definition
+   of the macro.
+
+   Read the comments of struct line_map and struct line_map_macro in
+   line-map.h to understand what a macro expansion point is.
+
+   If RETURN_MACRO_PARM_USAGE_POINT_P is TRUE and if LOCATION is the
+   locus of a token that is an argument of a macro M, this function
+   returns the locus of the parameter replaced by the argument, in the
+   definition of M. This is the yI in the comments of struct
+   line_map_macro in line-map.h.
+
+   Note that if the token is a builtin the function returns the
+   location of the expansion point of the macro.  */
+source_location linemap_macro_map_loc_to_def_point (const struct line_map*,
+						    source_location,
+						    bool);
+
+/* 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*,
+						    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 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_source_line (struct line_maps *,
+			     source_location);
+
+/* Return the column number corresponding to location LOCATION.
+
+   If LOCATION is the source location of token that is part of the
+   replacement-list of a macro expansion return the column number of
+   the macro expansion point.
+
+   SET is the line map set LOCATION comes from.  */
+int linemap_get_source_column (struct line_maps *,
+			       source_location);
+
+/* Return the name of the macro associated to MACRO_MAP.  */
+const char* linemap_map_get_macro_name (const struct line_map*);
+
+/* 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_file_path (struct line_maps *,
+				   source_location);
+
+/* 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 0 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)						\
+  (linemap_check_ordinary (MAP),					\
+    ((((LOC) - (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)				\
+  (linemap_check_ordinary (MAP),			\
+   (((LOC) - (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),				\
+   ((((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),					\
+   ((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),		\
+   ((MAP)->d.ordinary.included_from < 0))
 
 /* Set LOC to a source position that is the same line as the most recent
    linemap_line_start, but with the specified TO_COLUMN column number.  */
@@ -180,9 +676,141 @@ extern const struct line_map *linemap_lookup
       set->highest_location = r; \
     (LOC) = r;			 \
   }} while (0)
-    
 
+#ifdef ENABLE_CHECKING
+
+/* 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)
+#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), (MAP)->d.ordinary.to_file)
+
+/* Return the line number this map started encoding location from.  */
+#define LINEMAP_LINE(MAP)					\
+  (linemap_check_ordinary (MAP), (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), (MAP)->d.ordinary.sysp)
+
+/* Return TRUE if PRE denotes a location that is before POST, FALSE
+   otherwise. LINE_MAPS is the set of line maps PRE and POST were
+   allocated from.  */
+bool linemap_location_before_p (struct line_maps *set,
+				source_location   pre,
+				source_location   post);
+
+typedef struct GTY (())
+{
+  /* 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;
+
+enum location_resolution_kind
+{
+  LRK_MACRO_EXPANSION_POINT,
+  LRK_SPELLING_LOCATION,
+  LRK_MACRO_PARM_REPLACEMENT_POINT
+};
+
+
+/* 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.
+
+   Finally, if SPELLING_LOC is not NULL, *RESULTING_LOC is set to the
+   location to which LOC was resolved, and similarly, *LOC_MAP is set
+   to its map.  */
+
+source_location linemap_resolve_location (struct line_maps *,
+					  source_location loc,
+					  enum location_resolution_kind lrk,
+					  const struct line_map **loc_map);
+
+/* Expand source code location LOC and return a user readable source
+   code 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.  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 5ba6666..3a16d33 100644
--- a/libcpp/init.c
+++ b/libcpp/init.c
@@ -574,7 +574,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 d2872c4..c0fbab8 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)
diff --git a/libcpp/line-map.c b/libcpp/line-map.c
index dd3f11c..09d75d6 100644
--- a/libcpp/line-map.c
+++ b/libcpp/line-map.c
@@ -23,24 +23,23 @@ along with this program; see the file COPYING3.  If not see
 #include "config.h"
 #include "system.h"
 #include "line-map.h"
+#include "cpp-id-data.h"
+
 
-static void trace_include (const struct line_maps *, const struct line_map *);
 
+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);
 /* 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 +50,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 +121,25 @@ 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)))));
+
+  /* If we don't keep our line maps consistent, we can easily
+     segfault.  Don't rely on the client to do it for us.  */
+  if (set->depth == 0)
+    reason = LC_ENTER;
 
-  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,30 +147,35 @@ linemap_add (struct line_maps *set, enum lc_reason reason,
   if (reason == LC_RENAME_VERBATIM)
     reason = LC_RENAME;
 
-  /* If we don't keep our line maps consistent, we can easily
-     segfault.  Don't rely on the client to do it for us.  */
-  if (set->depth == 0)
-    reason = LC_ENTER;
-  else if (reason == LC_LEAVE)
+  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
@@ -149,55 +187,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 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_macro *macro,
+		     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;
+  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 source location for a token that is part of a
+   macro replacement-list at a macro expansion point.
+
+   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 orginal location of the token at the definition
+   point of the macro. If you read the extensive comments of struct
+   line_map_macro in line-map.h, this is the xI.
+
+   If the token is part of a macro argument, ORIG_PARM_REPLACEMENT_LOC
+   is the location of the point at wich the token (the argument)
+   replaces the macro parameter in the context of the relevant macro
+   definition. If you read the comments of struct line_map_macro in
+   line-map.h, this is the yI.  */
+
+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;
+
+  linemap_check_ordinary (map);
+
   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;
     }
@@ -211,8 +370,6 @@ linemap_line_start (struct line_maps *set, linenum_type to_line,
 	  /* If the column number is ridiculous or we've allocated a huge
 	     number of source_locations, give up on column numbers. */
 	  max_column_hint = 0;
-	  if (highest >0xF0000000)
-	    return 0;
 	  column_bits = 0;
 	}
       else
@@ -225,16 +382,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;
@@ -242,10 +410,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)
@@ -255,7 +432,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);
 	}
     }
@@ -265,25 +442,56 @@ 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_check_ordinary (map);
+  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;
+
+  if (set ==  NULL || line < RESERVED_LOCATION_COUNT)
+    return NULL;
 
-  mn = set->cache;
-  mx = set->used;
+  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
@@ -295,14 +503,356 @@ 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;
+    }
+
+  while (mx - mn > 1)
+    {
+      md = (mx + mn) / 2;
+      if (MAP_START_LOCATION (LINEMAPS_MACRO_MAP_AT (set, md)) > line)
+	mn = md;
+      else
+	mx = md;
+    }
+
+  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 locus of a token in a replacement-list of a
+   macro expansion return the location of said token in the definition
+   of the macro.
+
+   Read the comments of struct line_map and struct line_map_macro in
+   line-map.h to understand what a macro expansion point is.
+
+   If RETURN_MACRO_PARM_USAGE_POINT_P is TRUE and if LOCATION is the
+   locus of a token that is an argument of a macro M, this function
+   returns the locus of the parameter replaced by the argument, in the
+   definition of M. This is the yI in the comments of struct
+   line_map_macro in line-map.h.
+
+   Note that if the token is a builtin the function returns the
+   location of the expansion point of the macro.  */
+
+source_location
+linemap_macro_map_loc_to_def_point (const struct line_map *map,
+				    source_location location,
+				    bool return_macro_parm_usage_point_p)
+{
+  unsigned token_no;
+  linemap_assert (linemap_macro_expansion_map_p (map)
+		  && location >= MAP_START_LOCATION (map));
+
+  token_no = location - MAP_START_LOCATION (map);
+  linemap_assert (token_no < MACRO_MAP_NUM_MACRO_TOKENS (map));
+
+  if (return_macro_parm_usage_point_p)
+    location = MACRO_MAP_LOCATIONS (map)[2 * token_no + 1];
+  else
+    location = MACRO_MAP_LOCATIONS (map)[2 * token_no];
+
+  if (location >= RESERVED_LOCATION_COUNT)
+    return location;
+  else
+    /* If LOCATION is reserved for the user of libcpp, it means,
+     e.g. for gcc that it's the location of a built-in token. In that
+     case, let's say that the final location is the macro expansion
+     point because otherwise, the built-in location would not make
+     any sense and would violate the invariant that says that every
+     single location must be >= to the MAP_START_LOCATION (MAP) of its
+     map.  */
+    return MACRO_MAP_EXPANSION_POINT_LOCATION (map);
+}
+
+/* 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.  */
+
+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;
+}
+
+/* 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_loc_to_def_point (struct line_maps *set,
+				source_location location,
+				const struct line_map **original_map,
+				bool return_macro_parm_usage_point_p)
+{
+  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,
+					    return_macro_parm_usage_point_p);
+    }
+
+  if (original_map)
+    *original_map = map;
+  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_source_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 column number corresponding to location LOCATION.
+
+   If LOCATION is the source location of token that is part of the
+   replacement-list of a macro expansion return the column number of
+   the macro expansion point.
+
+   SET is the line map set LOCATION comes from.  */
+
+int
+linemap_get_source_column (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);
+  linemap_check_ordinary (map);
+
+  return SOURCE_COLUMN (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_file_path (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);
+  linemap_check_ordinary (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)->name);
+}
+
+/* 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 0 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_macro_loc_to_def_point (set, location, &map, false);
+  linemap_check_ordinary (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);
+}
+
+/* Return TRUE if PRE denotes a location that is before POST, FALSE
+   otherwise. LINE_MAPS is the set of line maps PRE and POST were
+   allocated from.  */
+
+bool
+linemap_location_before_p (struct line_maps *set,
+			   source_location  pre,
+			   source_location post)
+{
+  bool pre_from_macro_p, post_from_macro_p;
+
+  if (pre == post)
+    return false;
+
+  pre_from_macro_p =
+    linemap_location_from_macro_expansion_p (set, pre);
+  post_from_macro_p =
+    linemap_location_from_macro_expansion_p (set, post);
+
+  if (pre_from_macro_p != post_from_macro_p)
+    {
+      if (pre_from_macro_p)
+	pre = linemap_macro_loc_to_exp_point (set, pre, NULL);
+      else
+	post = linemap_macro_loc_to_exp_point (set, post, NULL);
+    }
+
+  return pre < post;
 }
 
 /* Print an include trace, for e.g. the -H option of the preprocessor.  */
@@ -314,5 +864,109 @@ trace_include (const struct line_maps *set, const struct line_map *map)
 
   while (--i)
     putc ('.', stderr);
-  fprintf (stderr, " %s\n", map->to_file);
+  linemap_check_ordinary (map);
+  fprintf (stderr, " %s\n", ORDINARY_MAP_FILE_NAME (map));
+}
+
+/* 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.
+
+   Finally, if SPELLING_LOC is not NULL, *RESULTING_LOC is set to the
+   location to which LOC was resolved, and similarly, *LOC_MAP is set
+   to its map.  */
+
+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_def_point (set, loc, map, false);
+      break;
+    case LRK_MACRO_PARM_REPLACEMENT_POINT:
+      loc = linemap_macro_loc_to_def_point (set, loc, map, true);
+      break;
+    default:
+      abort ();
+    }
+  return loc;
+}
+
+/* Expand source code location LOC and return a user readable source
+   code 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.  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..6f55a1e 100644
--- a/libcpp/macro.c
+++ b/libcpp/macro.c
@@ -177,7 +177,8 @@ _cpp_builtin_macro_text (cpp_reader *pfile, cpp_hashnode *node)
 	  while (! MAIN_FILE_P (map))
 	    map = INCLUDED_FROM (pfile->line_table, map);
 
-	name = map->to_file;
+	linemap_check_ordinary (map);
+	name = ORDINARY_MAP_FILE_NAME (map);
 	len = strlen (name);
 	buf = _cpp_unaligned_alloc (pfile, len * 2 + 3);
 	result = buf;
@@ -196,14 +197,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_source_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.
@@ -1856,6 +1857,7 @@ _cpp_create_definition (cpp_reader *pfile, cpp_hashnode *node)
       (sizeof (cpp_macro));
   else
     macro = (cpp_macro *) _cpp_aligned_alloc (pfile, sizeof (cpp_macro));
+  macro->name = node;
   macro->line = pfile->directive_line;
   macro->params = 0;
   macro->paramc = 0;
-- 
		Dodji

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

* Re: [PATCH 2/7] Generate virtual locations for tokens
  2011-07-16 15:25     ` [PATCH 2/7] Generate virtual locations for tokens Dodji Seketeli
@ 2011-08-09 15:30       ` Dodji Seketeli
  2011-09-12 21:15         ` Jason Merrill
  0 siblings, 1 reply; 135+ messages in thread
From: Dodji Seketeli @ 2011-08-09 15:30 UTC (permalink / raw)
  To: gcc-patches; +Cc: tromey, gdr, joseph, burnus, charlet, bonzini, jason

This is an update of the second patch of the series, to fix a bug I
noticed while looking at something else.

In replace_args, the type of track_macro_exp_p (which is a temporary
variable for the number the user passes to the
-ftrack-macro-expansion=<number> option) was a boolean.  But at some
point I wanted to use that boolean as in integer to read the <number>
argument.

The patch just changes the type of track_macro_exp_p to an integer and
renames that variable into track_macro_exp.

Passed bootstrap and tests on x86_64-unknown-linux-gnu against trunk.

Thanks.

From: Dodji Seketeli <dodji@redhat.com>
Date: Sat, 4 Dec 2010 14:04:29 +0100
Subject: [PATCH 2/7] Generate virtual locations for tokens

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.

The combination of this patch and the previous one bootstraps with
	--enable-languages=all,ada and passes regression tests on
x86_64-unknown-linux-gnu.

gcc/
	* 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/

	* 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/

	* 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>: Change the type of this to void.
	(struct cpp_context)<direct_p>: Remove.
	(_cpp_remaining_tokens_num_in_context): Declare new function.
	* 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)<expanded_capacity, virt_locs>:
	(struct macro_arg)<virt_locs_capacity, 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_args_mem, ensure_expanded_args_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_append_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.
	(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_args_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 and
	maybe_adjust_loc_for_trad_cpp.
	(_cpp_backup_tokens): Support the new kinds of token contexts.
---
 gcc/c-family/c-opts.c   |   13 +
 gcc/c-family/c.opt      |    8 +
 gcc/doc/cppopts.texi    |   17 +
 gcc/doc/invoke.texi     |    6 +-
 libcpp/include/cpplib.h |    8 +
 libcpp/init.c           |    1 +
 libcpp/internal.h       |   54 ++-
 libcpp/lex.c            |   41 ++-
 libcpp/macro.c          | 1308 ++++++++++++++++++++++++++++++++++++++++++-----
 9 files changed, 1321 insertions(+), 135 deletions(-)

diff --git a/gcc/c-family/c-opts.c b/gcc/c-family/c-opts.c
index 3227f7b..28e7ec9 100644
--- a/gcc/c-family/c-opts.c
+++ b/gcc/c-family/c-opts.c
@@ -628,6 +628,19 @@ 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;
+      goto ftrack_macro_expansion_with_arg;
+
+    case OPT_ftrack_macro_expansion_:
+    ftrack_macro_expansion_with_arg:
+      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 617ea2d..18a7727 100644
--- a/gcc/c-family/c.opt
+++ b/gcc/c-family/c.opt
@@ -937,6 +937,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..c75dd70 100644
--- a/gcc/doc/cppopts.texi
+++ b/gcc/doc/cppopts.texi
@@ -583,6 +583,23 @@ 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. It
+is the default value.
+
 @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 264d1b0..fec9a1d 100644
--- a/gcc/doc/invoke.texi
+++ b/gcc/doc/invoke.texi
@@ -426,9 +426,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/libcpp/include/cpplib.h b/libcpp/include/cpplib.h
index 55b0f1b..395d2fe 100644
--- a/libcpp/include/cpplib.h
+++ b/libcpp/include/cpplib.h
@@ -389,6 +389,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. all macro
+     expansion. 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/init.c b/libcpp/init.c
index 3a16d33..a4d4dbd 100644
--- a/libcpp/init.c
+++ b/libcpp/init.c
@@ -152,6 +152,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;
diff --git a/libcpp/internal.h b/libcpp/internal.h
index c0fbab8..c1e1a97 100644
--- a/libcpp/internal.h
+++ b/libcpp/internal.h
@@ -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,20 @@ 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;
-
-  /* True if utoken element is token, else ptoken.  */
-  bool direct_p;
+  /* If tokens_kind is TOKEN_KIND_EXTENDED, then, if we 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
+     hash_node, 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 TKEN_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.  */
+  void *macro;
+
+  /* This determines the type of tokens held by this context.  */
+  enum context_tokens_kind tokens_kind;
 };
 
 struct lexer_state
@@ -601,6 +644,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 d29f36d..af4aaa8 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/macro.c b/libcpp/macro.c
index 6f55a1e..3d89b91 100644
--- a/libcpp/macro.c
+++ b/libcpp/macro.c
@@ -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,64 @@ 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.  */
+  size_t expanded_capacity;     /* total size of expanded array.  */
+  source_location *virt_locs;	/* Where virtual locations for
+				   unexpanded tokens are stored.  */
+  unsigned virt_locs_capacity;	/* Total size of virtual locations
+				   array.  */
+  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 line macro
+   argument.  */
+typedef struct macro_arg_token_iter macro_arg_token_iter;
+struct macro_arg_token_iter
+{
+  /* The cpp_reader the macro comes from.  */
+  cpp_reader *pfile;
+  /* The kind of token over which we are supposed to iterate.  */
+  enum macro_arg_token_kind kind;
+  /* The function-like macro the tokens come from.  */
+  const macro_arg *arg;
+  /* 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 +106,56 @@ 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_args_mem (cpp_reader *, macro_arg *, size_t);
+static void ensure_expanded_args_room (cpp_reader *, macro_arg *, size_t);
+static void delete_macro_args (_cpp_buff*, unsigned num_args);
+static void set_arg_token (cpp_reader *, macro_arg *, const cpp_token *,
+			   source_location, size_t,
+			   enum macro_arg_token_kind);
+static const source_location *get_arg_token_location (cpp_reader *,
+						      const macro_arg *,
+						      enum macro_arg_token_kind);
+static const cpp_token **arg_token_ptr_at (cpp_reader *,
+					   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 *, cpp_reader*,
+				       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 (cpp_reader *,
+						   const cpp_token **,
+						   source_location *, 
+						   const cpp_token *,
+						   source_location,
+						   source_location,
+						   const struct line_map *,
+						   unsigned int *);
+
+static const cpp_token **tokens_buff_append_token (cpp_reader *,
+						   _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 +167,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.  */
@@ -508,7 +610,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
@@ -518,10 +620,26 @@ 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++;
+	  if (context->macro)
+	    {
+	      macro_context *m = (macro_context *) context->macro;
+	      m->cur_virt_loc++;
+	    }
+	}
 
       if (rhs->type == CPP_PADDING)
 	{
@@ -585,23 +703,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;
@@ -616,9 +748,16 @@ collect_args (cpp_reader *pfile, const cpp_hashnode *node,
     {
       unsigned int paren_depth = 0;
       unsigned int ntokens = 0;
+      num_args_alloced++;
 
       argc++;
       arg->first = (const cpp_token **) buff->cur;
+      if (track_macro_expansion_p)
+	{
+	  arg->virt_locs_capacity = DEFAULT_NUM_TOKENS_PER_MACRO_ARG;
+	  arg->virt_locs = XNEWVEC (source_location,
+				    arg->virt_locs_capacity);
+	}
 
       for (;;)
 	{
@@ -626,11 +765,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 > arg->virt_locs_capacity))
+	    {
+	      arg->virt_locs_capacity += ARG_TOKENS_EXTENT;
+	      arg->virt_locs = XRESIZEVEC (source_location,
+					   arg->virt_locs,
+					   arg->virt_locs_capacity);
+	    }
 
-	  token = cpp_get_token (pfile);
+	  token = cpp_get_token_1 (pfile, &virt_loc);
 
 	  if (token->type == CPP_PADDING)
 	    {
@@ -687,7 +835,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);
 
@@ -701,8 +849,9 @@ collect_args (cpp_reader *pfile, const cpp_hashnode *node,
 	      else
 		continue;
 	    }
-
-	  arg->first[ntokens++] = token;
+	  set_arg_token (pfile, arg, token, virt_loc,
+			 ntokens, MACRO_ARG_TOKEN_NORMAL);
+	  ntokens++;
 	}
 
       /* Drop trailing padding.  */
@@ -710,7 +859,8 @@ collect_args (cpp_reader *pfile, const cpp_hashnode *node,
 	ntokens--;
 
       arg->count = ntokens;
-      arg->first[ntokens] = &pfile->eof;
+      set_arg_token (pfile, arg, &pfile->eof, pfile->eof.src_loc,
+		     ntokens, MACRO_ARG_TOKEN_NORMAL);
 
       /* Terminate the argument.  Excess arguments loop back and
 	 overwrite the final legitimate argument, before failing.  */
@@ -753,6 +903,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;
 	}
     }
@@ -766,10 +918,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;
 
@@ -786,7 +940,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
@@ -820,13 +974,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;
@@ -851,11 +1007,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--;
@@ -874,8 +1032,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.  */
@@ -889,13 +1052,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,
+					  macro, location, count);
+	      for (i = 0; i < count; ++i)
+		{
+		  tokens_buff_append_token (pfile, 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)
 	{
@@ -923,33 +1117,311 @@ 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 (cpp_reader *pfile, macro_arg *arg, const cpp_token *token,
+	       source_location location, size_t index,
+	       enum macro_arg_token_kind kind)
+{  
+  const cpp_token **token_ptr;
+  source_location *loc = NULL;
+  bool track_macro_exp_p = CPP_OPTION (pfile, track_macro_expansion);
+
+  token_ptr =
+    arg_token_ptr_at (pfile, 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.  */
+static const source_location *
+get_arg_token_location (cpp_reader *pfile,
+			const macro_arg *arg,
+			enum macro_arg_token_kind kind)
+{
+  source_location *loc = NULL;
+  const cpp_token **token_ptr = arg_token_ptr_at (pfile, arg, 0,
+						  kind, &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 (cpp_reader *pfile, const macro_arg *arg,
+		  size_t index, enum macro_arg_token_kind kind,
+		  source_location **virt_location)
+{
+  bool track_macro_exp_p = CPP_OPTION (pfile, track_macro_expansion);
+  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)
+    return NULL;
+
+  if (virt_location)
+    {
+      if (track_macro_exp_p)
+	{
+	  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;
+	}
+      else
+	*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,
+			   cpp_reader *pfile,
+			   enum macro_arg_token_kind kind,
+			   const macro_arg *arg,
+			   const cpp_token **token_ptr)
+{
+  iter->pfile = pfile;
+  iter->kind = kind;
+  iter->arg = arg;
+  iter->token_ptr = token_ptr;
+  iter->location_ptr = get_arg_token_location (pfile, arg, kind);
+#ifdef ENABLE_CHECKING
+  iter->num_forwards = 0;
+#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)
+{
+  bool track_macro_exp_p = CPP_OPTION (it->pfile,
+				       track_macro_expansion);
+
+  switch (it->kind)
+    {
+    case MACRO_ARG_TOKEN_NORMAL:
+    case MACRO_ARG_TOKEN_EXPANDED:
+      it->token_ptr++;
+      if (track_macro_exp_p)
+	it->location_ptr++;
+      break;
+    case MACRO_ARG_TOKEN_STRINGIFIED:
+#ifdef ENABLE_CHECKING
+      if (it->num_forwards > 0)
+	abort ();
+      it->num_forwards++;
+#endif
+      break;
+    }
+}
+
+/* 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
+  return *it->location_ptr;
+}
+
+/* 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.  */
@@ -971,67 +1443,222 @@ 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, &virt_locs);
+
   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, macro,
+				 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_append_token (pfile, 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, pfile,
+				     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, pfile,
+				     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, pfile,
+				     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, pfile,
+				     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_append_token (pfile, 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_append_token (pfile, 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)
@@ -1047,7 +1674,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_append_token (pfile, buff, virt_locs,
+				    t, t->src_loc, t->src_loc,
+				    NULL, NULL);
+	}
 
       /* Add a new paste flag, or remove an unwanted one.  */
       if (paste_flag)
@@ -1061,13 +1693,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.  */
@@ -1095,6 +1730,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;
@@ -1111,7 +1747,7 @@ push_ptoken_context (cpp_reader *pfile, cpp_hashnode *macro, _cpp_buff *buff,
 {
   cpp_context *context = next_context (pfile);
 
-  context->direct_p = false;
+  context->tokens_kind = TOKENS_KIND_INDIRECT;
   context->macro = macro;
   context->buff = buff;
   FIRST (context).ptoken = first;
@@ -1123,15 +1759,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->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->macro = 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,
@@ -1139,7 +1804,7 @@ _cpp_push_text_context (cpp_reader *pfile, cpp_hashnode *macro,
 {
   cpp_context *context = next_context (pfile);
 
-  context->direct_p = true;
+  context->tokens_kind = TOKENS_KIND_DIRECT;
   context->macro = macro;
   context->buff = NULL;
   CUR (context) = start;
@@ -1147,6 +1812,187 @@ _cpp_push_text_context (cpp_reader *pfile, cpp_hashnode *macro,
   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 the
+   -ftrack-macro-expansion flag is in effect and if VIRT_LOCS is
+   non-null, *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)
+{
+  bool track_macro_exp_p = CPP_OPTION (pfile, track_macro_expansion);
+  size_t tokens_size = len * sizeof (cpp_token *);
+  size_t locs_size = len * sizeof (source_location);
+
+  if (track_macro_exp_p && 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 points to where to
+   insert the virtual location of TOKEN; that is, if the flag
+   -ftrack-macro-expansion is in effect.  TOKEN is the token to
+   insert.  DEF_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 PARM_DEF_LOC can
+   just be set to the same value as 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 (cpp_reader *pfile,
+			  const cpp_token **dest,
+			  source_location *virt_loc_dest,
+			  const cpp_token *token,
+			  source_location def_loc,
+			  source_location parm_def_loc,			  
+			  const struct line_map *map,
+			  unsigned int *macro_token_index)
+{
+  bool track_macro_exp_p = CPP_OPTION (pfile, track_macro_expansion);
+  source_location macro_loc = def_loc;
+  const cpp_token **result;
+
+  if (track_macro_exp_p)
+    {
+      if (map)
+	macro_loc = linemap_add_macro_token (map, *macro_token_index,
+					     def_loc, parm_def_loc);
+      *virt_loc_dest = macro_loc;
+    }
+  *dest = token;
+  result = &dest[1];
+
+  return result;
+}
+
+/* Appends a token to the end of the token buffer BUFFER.  Note that
+   this function doesn't enlarge BUFFER; it overwrite the last memory
+   location of BUFFER that holds a token.
+
+   TOKEN is the token to append. DEF_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 PARM_DEF_LOC can just be set to the
+   same value as 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.  This function adds
+   the virtual location DEF_LOC it 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_append_token (cpp_reader *pfile,
+			  _cpp_buff *buffer,
+			  source_location *virt_locs,
+			  const cpp_token *token,
+			  source_location def_loc,
+			  source_location parm_def_loc,
+			  const struct line_map *map,
+			  unsigned int *macro_token_index)
+{
+  const cpp_token **result;
+  unsigned token_index = 
+    (BUFF_FRONT (buffer) - buffer->base) / sizeof (cpp_token *);
+
+  result =
+    tokens_buff_put_token_to (pfile, (const cpp_token **) BUFF_FRONT (buffer),
+			      &virt_locs[token_index],
+			      token, def_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_args_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);
+  arg->expanded_capacity = 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_args_room (cpp_reader *pfile, macro_arg *arg, size_t size)
+{
+  if (size <= arg->expanded_capacity)
+    return;
+
+  size *= 2;
+
+  arg->expanded =
+    XRESIZEVEC (const cpp_token *, arg->expanded, size);
+  arg->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
@@ -1158,8 +2004,10 @@ expand_arg (cpp_reader *pfile, macro_arg *arg)
 {
   unsigned int 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.  */
@@ -1168,26 +2016,32 @@ expand_arg (cpp_reader *pfile, macro_arg *arg)
 
   /* Loop, reading in the arguments.  */
   capacity = 256;
-  arg->expanded = XNEWVEC (const cpp_token *, capacity);
+  alloc_expanded_args_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_args_room (pfile, arg, arg->expanded_count + 1);
 
-      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 (pfile, arg, token, location,
+		     arg->expanded_count, MACRO_ARG_TOKEN_EXPANDED);
+      arg->expanded_count++;
     }
 
   _cpp_pop_context (pfile);
@@ -1196,25 +2050,127 @@ 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;
+    {
+      cpp_hashnode *macro;
+      if (context->tokens_kind == TOKENS_KIND_EXTENDED)
+	{
+	  macro_context *mc = (macro_context *) context->macro;
+	  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->macro = NULL;
+	}
+      else
+	macro = (cpp_hashnode *) context->macro;
+
+      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 = (macro_context *) c->macro;
+      *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 traditionnal 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 traditionnal 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
@@ -1222,12 +2178,45 @@ _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".  This matters when a token results from macro
+   expansion -- the token's location will indicate where the macro is
+   defined (the spelling location of the token) but *LOC will be a
+   virtual location of the token. Virtual location means a location
+   that possibly encodes many types of locus at once. A virtual
+   location can encode the location of a token resulting from macro
+   expansion or not. If the token results from macro expansion its
+   virtual location encodes (at the same time):
+     - the spelling location of the token
+     - the locus of the macro expansion point
+     - the locus the point where the token got instantiated as part of
+       the macro expansion process.
+     (YES, IT ENCODES ALL THESE THREE AT THE SAME TIME! and maybe more.)
+
+   You can learn more about the different locuses encoded in a map by
+   reading the extensive comments of the line_map_macro and line_map
+   structs in line-map.h.  A virtual location, indeed.
+
+   The linemap API can then be used to retrieve the particular locus
+   we are interested in.
+
+   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 operators
+   '<' and '>'.
+
+   Otherwise *LOC is set to the same location as the location carried
+   by the returned token.  */
+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 (;;)
@@ -1237,20 +2226,23 @@ 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);
+	      if (location)
+		*location = result->src_loc;
+	      return result;
 	    }
 	}
       else
@@ -1258,6 +2250,8 @@ cpp_get_token (cpp_reader *pfile)
 	  _cpp_pop_context (pfile);
 	  if (pfile->state.in_directive)
 	    continue;
+	  if (location)
+	    *location = pfile->avoid_paste.src_loc;
 	  return &pfile->avoid_paste;
 	}
 
@@ -1295,7 +2289,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
@@ -1312,12 +2307,16 @@ 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);
+	      if (location)
+		*location = result->src_loc;
+	      return result;
 	    }
 	}
       else
@@ -1334,27 +2333,79 @@ cpp_get_token (cpp_reader *pfile)
       break;
     }
 
-  return result;
+  if (location)
+    *location = virt_loc;
+  return result;  
+}
+
+/* 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 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.  */
+/* 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 enbled its virtual location encodes (at the
+   same time):
+
+   - the spelling location of the token the locus of the macro
+   - expansion point the locus 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);
+  result = cpp_get_token_1 (pfile, loc);
   if (pfile->context->macro)
-    *loc = pfile->invocation_location;
+    {
+      if (!CPP_OPTION (pfile, track_macro_expansion))
+	*loc = pfile->invocation_location;
+    }
   else
     *loc = result->src_loc;
 
+  *loc = maybe_adjust_loc_for_trad_cpp (pfile, *loc);
   return result;
 }
 
@@ -1364,7 +2415,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 = (cpp_hashnode *) pfile->context->macro;
 
   return node && node->value.macro && node->value.macro->syshdr;
 }
@@ -1421,10 +2472,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->macro)
+	    {
+	      macro_context *m = (macro_context *) pfile->context->macro;
+	      m->cur_virt_loc--;
+#ifdef ENABLE_CHECKING
+	      if (m->cur_virt_loc < m->virt_locs)
+		abort ();
+#endif
+	    }
+	  else
+	    abort ();
+	}
+      else
+	abort ();
     }
 }
 
-- 
		Dodji

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

* Re: [PATCH 1/7] Linemap infrastructure for virtual locations
  2011-08-09 14:56                 ` Dodji Seketeli
@ 2011-08-19  8:46                   ` Jason Merrill
  2011-08-19 14:43                     ` Tom Tromey
  2011-09-01 10:37                     ` Dodji Seketeli
  0 siblings, 2 replies; 135+ messages in thread
From: Jason Merrill @ 2011-08-19  8:46 UTC (permalink / raw)
  To: Dodji Seketeli; +Cc: gcc-patches, tromey, gdr, joseph, burnus, charlet, bonzini

On 08/09/2011 10:31 AM, Dodji Seketeli wrote:
> -#define in_system_header (in_system_header_at (input_location))
> +#define in_system_header  (in_system_header_at (input_location))

Unnecessary whitespace change.

>  struct GTY(()) cpp_macro {
> +  /* Name of this macro.  Used only for error reporting.  */
> +  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")))
> +    name;

Would it make sense to have line_map_macro store a pointer to the 
cpp_hashnode rather than straight to the cpp_macro so we don't have to 
add an extra pointer to cpp_macro?

> +struct GTY(()) cpp_macro;

I don't think we need GTY markers on forward declarations.

> +  LC_ENTER_MACRO
> +  /* stringize */
> +  /* paste */

What is the purpose of these comments?

> +       - when a new preprocessing unit start.
> +       - when a preprocessing unit ends.
> +       - when a macro expansion occurs
> +  */

*/ should go at the end of the line.

> +/*
> +   Create a macro map. A macro map encodes source locations of tokens

And /* at the beginning of the line.

>>> +linemap_check_ordinary (const struct line_map *map)
>>> +{
>>> +  linemap_assert (!linemap_macro_expansion_map_p (map));
>>> +  /* Return any old value.  */
>>> +  return 0;
>>> +}
>> Why does this return int if we aren't interested in the value?  I
>> would change it to a macro that returns 'map' so that it can expand to
>> nothing if !ENABLE_CHECKING and can be used in-line like the gcc
>> TREE_CHECK macros.
>>
> ... this.  This is now implemented by a macro as you are suggesting.

You changed it to a macro that returns 'map' but didn't change the uses 
to inline, i.e.

> +  (linemap_check_ordinary (MAP), (MAP)->d.ordinary.to_file)

to

+  (linemap_check_ordinary (MAP)->d.ordinary.to_file)

For that to work, the !ENABLE_CHECKING version also needs to return MAP. 
  If you aren't going to change the users, the ENABLE_CHECKING version 
might as well not return anything.

> +typedef struct GTY (())
> +{

The GTY(()) can't work here without a tag name associated with it.  And 
we never gc-allocate extended_location, so it isn't needed.

> +enum location_resolution_kind
> +{
> +  LRK_MACRO_EXPANSION_POINT,
> +  LRK_SPELLING_LOCATION,
> +  LRK_MACRO_PARM_REPLACEMENT_POINT
> +};

Let's move this after the comment that explains the values.

> +
>
> -static void trace_include (const struct line_maps *, const struct line_map *);
>
> +static void trace_include (const struct line_maps *, const struct line_map *);

Unnecessary change.

> +  /* If we don't keep our line maps consistent, we can easily
> +     segfault.  Don't rely on the client to do it for us.  */

This sounds like working around a bug.  Wouldn't it be better to fix it?

> So this is a second try.  I have removed that test altogether.  But
> then I figured I should nonetheless handle the case where we run out
> of integer space when allocating locations for both ordinary and macro
> tokens.  So linemap_line_start hands out 0 in that case (for ordinary
> tokens) and linemap_enter_macro hands out NULL in lieu of a macro map
> in that case.  That NULL map is handled (in the second patch of the
> series) so that the spelling location of the macro token is used in
> that case.

Is something still protecting from numeric overflow in the case that 
there aren't any macros?  I'd leave that test the way it was before (and 
keep your new tests too).

> +    /* If LOCATION is reserved for the user of libcpp, it means,
> +     e.g. for gcc that it's the location of a built-in token. In that
> +     case, let's say that the final location is the macro expansion
> +     point because otherwise, the built-in location would not make
> +     any sense and would violate the invariant that says that every
> +     single location must be >= to the MAP_START_LOCATION (MAP) of its
> +     map.  */

How can this happen?  Can't we just assert that it doesn't, like we do 
in the next function?

Jason

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

* Re: [PATCH 1/7] Linemap infrastructure for virtual locations
  2011-08-19  8:46                   ` Jason Merrill
@ 2011-08-19 14:43                     ` Tom Tromey
  2011-09-01 10:37                     ` Dodji Seketeli
  1 sibling, 0 replies; 135+ messages in thread
From: Tom Tromey @ 2011-08-19 14:43 UTC (permalink / raw)
  To: Jason Merrill
  Cc: Dodji Seketeli, gcc-patches, gdr, joseph, burnus, charlet, bonzini

>>>>> "Jason" == Jason Merrill <jason@redhat.com> writes:

>> +  LC_ENTER_MACRO
>> +  /* stringize */
>> +  /* paste */

Jason> What is the purpose of these comments?

That is left over from my initial hack.  The new scheme doesn't (yet?)
properly handle locations arising from stringizing or token pasting.

Tom

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

* Re: [PATCH 4/7] Support -fdebug-cpp option
  2011-07-16 14:39     ` [PATCH 4/7] Support -fdebug-cpp option Dodji Seketeli
@ 2011-08-21 11:02       ` Alexandre Oliva
  2011-08-21 11:40         ` Jakub Jelinek
  2011-08-23 19:52         ` Dodji Seketeli
  2011-09-12 22:07       ` Jason Merrill
  1 sibling, 2 replies; 135+ messages in thread
From: Alexandre Oliva @ 2011-08-21 11:02 UTC (permalink / raw)
  To: Dodji Seketeli
  Cc: gcc-patches, tromey, gdr, joseph, burnus, charlet, paolo, jason

On Jul 16, 2011, Dodji Seketeli <dodji@redhat.com> wrote:

> 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.

Any way to read that back in while compiling a preprocessed file, so
that ccache et al can use this flag to get the same location information
that would have been gotten without separate preprocessing?

-- 
Alexandre Oliva, freedom fighter    http://FSFLA.org/~lxoliva/
You must be the change you wish to see in the world. -- Gandhi
Be Free! -- http://FSFLA.org/   FSF Latin America board member
Free Software Evangelist      Red Hat Brazil Compiler Engineer

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

* Re: [PATCH 4/7] Support -fdebug-cpp option
  2011-08-21 11:02       ` Alexandre Oliva
@ 2011-08-21 11:40         ` Jakub Jelinek
  2011-08-22 14:45           ` Tom Tromey
  2011-08-23 19:52         ` Dodji Seketeli
  1 sibling, 1 reply; 135+ messages in thread
From: Jakub Jelinek @ 2011-08-21 11:40 UTC (permalink / raw)
  To: Alexandre Oliva
  Cc: Dodji Seketeli, gcc-patches, tromey, gdr, joseph, burnus,
	charlet, paolo, jason

On Sun, Aug 21, 2011 at 04:51:54AM -0300, Alexandre Oliva wrote:
> On Jul 16, 2011, Dodji Seketeli <dodji@redhat.com> wrote:
> 
> > 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.
> 
> Any way to read that back in while compiling a preprocessed file, so
> that ccache et al can use this flag to get the same location information
> that would have been gotten without separate preprocessing?

For ccache and friends I think it would be better to have a preprocessing
mode that would output all lines as is (i.e. no macro replacement), except
for processing #include/#include_next directives.

	Jakub

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

* Re: [PATCH 4/7] Support -fdebug-cpp option
  2011-08-21 11:40         ` Jakub Jelinek
@ 2011-08-22 14:45           ` Tom Tromey
  2011-08-22 15:22             ` Jakub Jelinek
  0 siblings, 1 reply; 135+ messages in thread
From: Tom Tromey @ 2011-08-22 14:45 UTC (permalink / raw)
  To: Jakub Jelinek
  Cc: Alexandre Oliva, Dodji Seketeli, gcc-patches, gdr, joseph,
	burnus, charlet, paolo, jason

>>>>> "Jakub" == Jakub Jelinek <jakub@redhat.com> writes:

Jakub> For ccache and friends I think it would be better to have a
Jakub> preprocessing mode that would output all lines as is (i.e. no
Jakub> macro replacement), except for processing #include/#include_next
Jakub> directives.

That exists -- -fdirectives-only.

Tom

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

* Re: [PATCH 4/7] Support -fdebug-cpp option
  2011-08-22 14:45           ` Tom Tromey
@ 2011-08-22 15:22             ` Jakub Jelinek
  0 siblings, 0 replies; 135+ messages in thread
From: Jakub Jelinek @ 2011-08-22 15:22 UTC (permalink / raw)
  To: Tom Tromey
  Cc: Alexandre Oliva, Dodji Seketeli, gcc-patches, gdr, joseph,
	burnus, charlet, paolo, jason

On Mon, Aug 22, 2011 at 08:16:45AM -0600, Tom Tromey wrote:
> >>>>> "Jakub" == Jakub Jelinek <jakub@redhat.com> writes:
> 
> Jakub> For ccache and friends I think it would be better to have a
> Jakub> preprocessing mode that would output all lines as is (i.e. no
> Jakub> macro replacement), except for processing #include/#include_next
> Jakub> directives.
> 
> That exists -- -fdirectives-only.

It isn't exactly what would be needed, as e.g. \\\n are removed from
from #defines and thus they get different location of the tokens.

BTW, I guess we should do something about parsing such an output,
we emit e.g.
# 1 "<stdin>"
# 1 "<built-in>"
#define __STDC__ 1
#define __STDC_HOSTED__ 1
#define __GNUC__ 4
#define __GNUC_MINOR__ 6
#define __GNUC_PATCHLEVEL__ 0
...

For <built-in> we really should assume line 0 for all the defines
in that "file".

	Jakub

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

* Re: [PATCH 4/7] Support -fdebug-cpp option
  2011-08-21 11:02       ` Alexandre Oliva
  2011-08-21 11:40         ` Jakub Jelinek
@ 2011-08-23 19:52         ` Dodji Seketeli
  2011-08-24 15:11           ` Tom Tromey
  1 sibling, 1 reply; 135+ messages in thread
From: Dodji Seketeli @ 2011-08-23 19:52 UTC (permalink / raw)
  To: Alexandre Oliva
  Cc: gcc-patches, tromey, gdr, joseph, burnus, charlet, paolo, jason

Alexandre Oliva <aoliva@redhat.com> writes:

> On Jul 16, 2011, Dodji Seketeli <dodji@redhat.com> wrote:
>
>> 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.
>
> Any way to read that back in while compiling a preprocessed file, so
> that ccache et al can use this flag to get the same location information
> that would have been gotten without separate preprocessing?

Jakub Jelinek <jakub@redhat.com> writes:

>
> For ccache and friends I think it would be better to have a preprocessing
> mode that would output all lines as is (i.e. no macro replacement), except
> for processing #include/#include_next directives.

Would that be enough for, say, when people submit bug reports to GCC?  I
think it would but maybe I am missing some corner cases.

Tom Tromey <tromey@redhat.com> writes:

>>>>>> "Jakub" == Jakub Jelinek <jakub@redhat.com> writes:
>
> Jakub> For ccache and friends I think it would be better to have a
> Jakub> preprocessing mode that would output all lines as is (i.e. no
> Jakub> macro replacement), except for processing #include/#include_next
> Jakub> directives.
>
> That exists -- -fdirectives-only.
>
> Tom

Jakub Jelinek <jakub@redhat.com> writes:

> On Mon, Aug 22, 2011 at 08:16:45AM -0600, Tom Tromey wrote:
>> >>>>> "Jakub" == Jakub Jelinek <jakub@redhat.com> writes:
>> 
>> Jakub> For ccache and friends I think it would be better to have a
>> Jakub> preprocessing mode that would output all lines as is (i.e. no
>> Jakub> macro replacement), except for processing #include/#include_next
>> Jakub> directives.
>> 
>> That exists -- -fdirectives-only.
>
> It isn't exactly what would be needed, as e.g. \\\n are removed from
> from #defines and thus they get different location of the tokens.

Would it be acceptable to just change the output of -fdirective to fit?
Or are we bound to not breaking existing consumers?

>
> BTW, I guess we should do something about parsing such an output,
> we emit e.g.
> # 1 "<stdin>"
> # 1 "<built-in>"
> #define __STDC__ 1
> #define __STDC_HOSTED__ 1
> #define __GNUC__ 4
> #define __GNUC_MINOR__ 6
> #define __GNUC_PATCHLEVEL__ 0
> ...
>
> For <built-in> we really should assume line 0 for all the defines
> in that "file".

Right now BUILTIN_LOCATION is defined to 1 at least the C/C++ FEs.  Are
you saying defines should have UNKNOWN_LOCATION (which is 0) instead?
OTOH, c_builtin_function already sets the location of c builtins to
UNKNOWN_LOCATION.  Which makes me wonder what BUILTIN_LOCATION is
actually good for then.  :-)

-- 
		Dodji

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

* Re: [PATCH 4/7] Support -fdebug-cpp option
  2011-08-23 19:52         ` Dodji Seketeli
@ 2011-08-24 15:11           ` Tom Tromey
  0 siblings, 0 replies; 135+ messages in thread
From: Tom Tromey @ 2011-08-24 15:11 UTC (permalink / raw)
  To: Dodji Seketeli
  Cc: Alexandre Oliva, gcc-patches, gdr, joseph, burnus, charlet, paolo, jason

Tom> That exists -- -fdirectives-only.

Jakub> It isn't exactly what would be needed, as e.g. \\\n are removed from
Jakub> from #defines and thus they get different location of the tokens.

Dodji> Would it be acceptable to just change the output of -fdirective to fit?
Dodji> Or are we bound to not breaking existing consumers?

I think changing it would be fine.

Tom

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

* Re: [PATCH 1/7] Linemap infrastructure for virtual locations
  2011-08-19  8:46                   ` Jason Merrill
  2011-08-19 14:43                     ` Tom Tromey
@ 2011-09-01 10:37                     ` Dodji Seketeli
  2011-09-07 19:26                       ` Jason Merrill
  1 sibling, 1 reply; 135+ messages in thread
From: Dodji Seketeli @ 2011-09-01 10:37 UTC (permalink / raw)
  To: Jason Merrill; +Cc: gcc-patches, tromey, gdr, joseph, burnus, charlet, bonzini

Jason Merrill <jason@redhat.com> writes:

> On 08/09/2011 10:31 AM, Dodji Seketeli wrote:
> > -#define in_system_header (in_system_header_at (input_location))
> > +#define in_system_header  (in_system_header_at (input_location))
> 
> Unnecessary whitespace change.

Fixed.

> >  struct GTY(()) cpp_macro {
> > +  /* Name of this macro.  Used only for error reporting.  */
> > +  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")))
> > +    name;
> 
> Would it make sense to have line_map_macro store a pointer to the
> cpp_hashnode rather than straight to the cpp_macro so we don't have to
> add an extra pointer to cpp_macro?

I think so.  I have made that change in the patch below.

> 
> > +struct GTY(()) cpp_macro;
> 
> I don't think we need GTY markers on forward declarations.

Removed.

> 
> > +  LC_ENTER_MACRO
> > +  /* stringize */
> > +  /* paste */
> 
> What is the purpose of these comments?

As Tom replied in this thread it's a leftover of his initial patch to
remind that location tokens involved in stringizing and pasting aren't
yet tracked.  I have turned this into a FIXME comment.

> 
> > +       - when a new preprocessing unit start.
> > +       - when a preprocessing unit ends.
> > +       - when a macro expansion occurs
> > +  */
> 
> */ should go at the end of the line.

Fixed.

> > +/*
> > +   Create a macro map. A macro map encodes source locations of tokens
> 
> And /* at the beginning of the line.

Fixed.

> 
> >>> +linemap_check_ordinary (const struct line_map *map)
> >>> +{
> >>> +  linemap_assert (!linemap_macro_expansion_map_p (map));
> >>> +  /* Return any old value.  */
> >>> +  return 0;
> >>> +}
> >> Why does this return int if we aren't interested in the value?  I
> >> would change it to a macro that returns 'map' so that it can expand to
> >> nothing if !ENABLE_CHECKING and can be used in-line like the gcc
> >> TREE_CHECK macros.
> >>
> > ... this.  This is now implemented by a macro as you are suggesting.
> 
> You changed it to a macro that returns 'map' but didn't change the
> uses to inline, i.e.
> 
> > +  (linemap_check_ordinary (MAP), (MAP)->d.ordinary.to_file)
> 
> to
> 
> +  (linemap_check_ordinary (MAP)->d.ordinary.to_file)
> 
> For that to work, the !ENABLE_CHECKING version also needs to return
> MAP.

Done now.

> > +typedef struct GTY (())
> > +{
> 
> The GTY(()) can't work here without a tag name associated with it.
> And we never gc-allocate extended_location, so it isn't needed.

GTY marker removed.

> 
> > +enum location_resolution_kind
> > +{
> > +  LRK_MACRO_EXPANSION_POINT,
> > +  LRK_SPELLING_LOCATION,
> > +  LRK_MACRO_PARM_REPLACEMENT_POINT
> > +};
> 
> Let's move this after the comment that explains the values.

The comment that explains the values is for the
linemap_resolve_location, so maybe moving the enum between the comment
and the function would not look pretty.  So I have added a comment to
this enum saying to look at the comment of that function, that comes
right below it.

> 
> > +
> >
> > -static void trace_include (const struct line_maps *, const struct line_map *);
> >
> > +static void trace_include (const struct line_maps *, const struct line_map *);
> 
> Unnecessary change.

Fixed.

> 
> > +  /* If we don't keep our line maps consistent, we can easily
> > +     segfault.  Don't rely on the client to do it for us.  */
> 
> This sounds like working around a bug.  Wouldn't it be better to fix
> it?

Heh.  This looked awkward to me when I was shuffling code around in
that function, but I decided to stay focused on the first purpose of
this patch set.  Then I forgot to send cook a separate patch to fix
that stuff.

I committed the patch
http://gcc.gnu.org/ml/gcc-patches/2011-08/msg01927.html that turns
that kludge into an assert fixes the relevant uses of the linemap_add
function.  I have updated this patch accordingly.

>
> > So this is a second try.  I have removed that test altogether.  But
> > then I figured I should nonetheless handle the case where we run out
> > of integer space when allocating locations for both ordinary and macro
> > tokens.  So linemap_line_start hands out 0 in that case (for ordinary
> > tokens) and linemap_enter_macro hands out NULL in lieu of a macro map
> > in that case.  That NULL map is handled (in the second patch of the
> > series) so that the spelling location of the macro token is used in
> > that case.
> 
> Is something still protecting from numeric overflow in the case that
> there aren't any macros?  I'd leave that test the way it was before
> (and keep your new tests too).

Done.

> 
> > +    /* If LOCATION is reserved for the user of libcpp, it means,
> > +     e.g. for gcc that it's the location of a built-in token. In that
> > +     case, let's say that the final location is the macro expansion
> > +     point because otherwise, the built-in location would not make
> > +     any sense and would violate the invariant that says that every
> > +     single location must be >= to the MAP_START_LOCATION (MAP) of its
> > +     map.  */
> 
> How can this happen?  Can't we just assert that it doesn't, like we do
> in the next function?

Done.

I have re-based, bootstrapped with --enable-languages=all and tested
the whole patch set on x86_64-unknown-linux-gnu against trunk.  Note
that I skipped Ada, because its bootstrap was broken when I tried.

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.

Boostrapped with --enable-languages=all,ada and passed regression
tests on x86_unknown-linux-gnu against trunk.

libcpp/

	* 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): New macros.
	(linemap_position_for_line_and_column)
	(linemap_tracks_macro_expansion_locs_p, linemap_enter_macro)
	(linemap_add_macro_token, linemap_macro_expansion_map_p)
	(linemap_macro_loc_to_exp_point, linemap_macro_loc_to_def_point)
	(linemap_macro_map_loc_to_def_point)
	(linemap_macro_map_loc_to_exp_point, 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
	(linemap_assert): New macro.
	(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_enter_macro)
	(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_loc_to_exp_point, linemap_map_get_index)
	(linemap_macro_loc_to_def_point, 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_location_before_p):
	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.
	* 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/
	* 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/c-family

	* 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/java/

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

gcc/ada/

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

gcc/fortran/

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

gcc/testsuite/

	* gcc.dg/cpp/pragma-diagnostic-1.c: New test.
---
 gcc/ada/gcc-interface/trans.c                  |   10 +-
 gcc/c-family/c-lex.c                           |    6 +-
 gcc/c-family/c-ppoutput.c                      |   41 +-
 gcc/diagnostic.c                               |   11 +-
 gcc/fortran/cpp.c                              |   22 +-
 gcc/input.c                                    |    9 +-
 gcc/input.h                                    |   18 +-
 gcc/java/jcf-parse.c                           |    2 +-
 gcc/testsuite/gcc.dg/cpp/pragma-diagnostic-1.c |   32 +
 libcpp/directives.c                            |   16 +-
 libcpp/files.c                                 |    5 +-
 libcpp/include/line-map.h                      |  710 ++++++++++++++++++++--
 libcpp/init.c                                  |    4 +-
 libcpp/internal.h                              |    3 +-
 libcpp/line-map.c                              |  800 +++++++++++++++++++++---
 libcpp/macro.c                                 |   12 +-
 16 files changed, 1498 insertions(+), 203 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 8e0ccd4..75fd207 100644
--- a/gcc/ada/gcc-interface/trans.c
+++ b/gcc/ada/gcc-interface/trans.c
@@ -278,7 +278,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.  */
@@ -7849,12 +7849,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..b4bc9ce 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,8 @@ 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_macro_loc_to_exp_point (line_table, line, &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..2a6bda7 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,231 @@ 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 locations 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 we are at the expansion point of MACRO.  Each xI is the
+     location of the Ith token of the replacement-list. Now it gets
+     confusing. the xI is the location of the Ith token of the
+     replacement-list at the macro *definition* point. Not at the
+     macro replacement point. Okay, let's try to explain this below.
+
+     Imagine this:
+
+        #define OPERATION(OP0, OPERATOR, OP1) \
+                OP0 OPERATOR OP1 <-- #0
+
+	#define PLUS(A, B) OPERATION (A, +, B)  <--- #1
+
+	int a = PLUS (1,2); <--- #2
+     
+     In #2, there is a macro map for the expansion of PLUS. PLUS is
+     expanded into the replacement-list made of the tokens:
+     
+        OPERATION, (, A, +, B, )
+
+     and then further expanded into the tokens:
+
+        1, +, 2.
+
+     Let's consider the case of token "+" here. That will help us
+     understand what the xI we were talking about earlier means.
+
+     The token '+' has two locations, so to speak. One in the context
+     of the macro *expansion* of PLUS in #2 and one in the context of
+     the macro *definition* of PLUS in #1. These two locations are
+     encoded in the the latter context, somehow in the xI we are
+     talking about.
+
+     xI is roughly the index of the token inside the replacement-list
+     at the expansion point. So for '+', it's index would then be 1
+     [The index of token '1' would be 0 and the index of token 2 would
+     be 1]. So if '+' is our current xI, it is actualy an x1.
+
+     The value of x1 is the location of the token '+' inside the
+     replacement-list of PLUS at the definition point of PLUS. It is
+     its spelling location in #1.
+
+     So x0 would have described the token '1', x1 describes the token
+     '+' and x2 describes the token '2'.
+
+     Now what's the y1 then? Remember, we said macro_locations is an
+     array of pairs (xI,yI). We now know what the xI is, now let's
+     look at the yI.
+
+     Let's look at the token '+' again. We said it has two locations
+     somehow. Actually it has 3. Kind of. As '+' is an argument passed
+     to the macro OPERATION [at the definition point of the macro
+     PLUS], it would be nice to record the source location of the
+     *parameter* of OPERATION that is replaced by the argument '+'.
+     In other words, we want to record the location of the token
+     "OPERATOR" in the replacement-list of OPERATION, at the
+     /definition/ point of OPERATION in #0. And that is y1.
+
+     So when (xI,yI) describes a token that is passed as an argument
+     to a macro M, the yI is the location of the macro parameter that
+     the argument replaces, at the definition point of M. If (xI,yI)
+     does not describe a token that is passed as an argument to a
+     macro, xI == yI.
+   */
+  source_location * GTY((length ("2 * %h.n_tokens"))) macro_locations;
+
+  /* This is the location of the expansion point of the current macro
+     map.  That expansion point 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 +300,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 +434,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 +450,354 @@ 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 *);
+
+/* 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 source location for a token that is part of a
+   macro replacement-list at a macro expansion point.
+
+   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 orginal location of the token at the definition
+   point of the macro. If you read the extensive comments of struct
+   line_map_macro in line-map.h, this is the xI.
+
+   If the token is part of a macro argument, ORIG_PARM_REPLACEMENT_LOC
+   is the location of the point at wich the token (the argument)
+   replaces the macro parameter in the context of the relevant macro
+   definition. If you read the comments of struct line_map_macro in
+   line-map.h, this is the yI.  */
+source_location linemap_add_macro_token (const struct line_map *,
+					 unsigned int,
+					 source_location,
+					 source_location);
+
+/* 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 *);
+
+/* 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.  */
+source_location linemap_macro_loc_to_exp_point (struct line_maps *,
+						source_location,
+						const struct line_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_loc_to_def_point (struct line_maps *,
+						source_location,
+						const struct line_map **,
+						bool);
+
+/* If LOCATION is the locus of a token in a replacement-list of a
+   macro expansion return the location of said token in the definition
+   of the macro.
+
+   Read the comments of struct line_map and struct line_map_macro in
+   line-map.h to understand what a macro expansion point is.
+
+   If RETURN_MACRO_PARM_USAGE_POINT_P is TRUE and if LOCATION is the
+   locus of a token that is an argument of a macro M, this function
+   returns the locus of the parameter replaced by the argument, in the
+   definition of M. This is the yI in the comments of struct
+   line_map_macro in line-map.h.
+
+   Note that if the token is a builtin the function returns the
+   location of the expansion point of the macro.  */
+source_location linemap_macro_map_loc_to_def_point (const struct line_map*,
+						    source_location,
+						    bool);
+
+/* 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*,
+						    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 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_source_line (struct line_maps *,
+			     source_location);
+
+/* Return the column number corresponding to location LOCATION.
+
+   If LOCATION is the source location of token that is part of the
+   replacement-list of a macro expansion return the column number of
+   the macro expansion point.
+
+   SET is the line map set LOCATION comes from.  */
+int linemap_get_source_column (struct line_maps *,
+			       source_location);
+
+/* Return the name of the macro associated to MACRO_MAP.  */
+const char* linemap_map_get_macro_name (const struct line_map*);
+
+/* 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_file_path (struct line_maps *,
+				   source_location);
+
+/* 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 0 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))
+
+#ifdef ENABLE_CHECKING
+
+/* 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 TRUE if PRE denotes a location that is before POST, FALSE
+   otherwise. LINE_MAPS is the set of line maps PRE and POST were
+   allocated from.  */
+bool linemap_location_before_p (struct line_maps *set,
+				source_location   pre,
+				source_location   post);
+
+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_PARM_REPLACEMENT_POINT
+};
+
+/* 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.
+
+   Finally, if SPELLING_LOC is not NULL, *RESULTING_LOC is set to the
+   location to which LOC was resolved, and similarly, *LOC_MAP is set
+   to its map.  */
+
+source_location linemap_resolve_location (struct line_maps *,
+					  source_location loc,
+					  enum location_resolution_kind lrk,
+					  const struct line_map **loc_map);
+
+/* Expand source code location LOC and return a user readable source
+   code 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.  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..588e8ed 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)
diff --git a/libcpp/line-map.c b/libcpp/line-map.c
index 2a0749a..513bc10 100644
--- a/libcpp/line-map.c
+++ b/libcpp/line-map.c
@@ -23,24 +23,21 @@ along with this program; see the file COPYING3.  If not see
 #include "config.h"
 #include "system.h"
 #include "line-map.h"
+#include "cpplib.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);
 /* 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 +48,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 +119,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)))));
 
-  if (set->used == set->allocated)
+  /* When we enter the file for the first time reason cannot be
+     LC_RENAME.  */
+  linemap_assert (!(set->depth == 0 && reason == LC_RENAME));
+
+  /* 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 +144,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 +184,173 @@ 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 source location for a token that is part of a
+   macro replacement-list at a macro expansion point.
+
+   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 orginal location of the token at the definition
+   point of the macro. If you read the extensive comments of struct
+   line_map_macro in line-map.h, this is the xI.
+
+   If the token is part of a macro argument, ORIG_PARM_REPLACEMENT_LOC
+   is the location of the point at wich the token (the argument)
+   replaces the macro parameter in the context of the relevant macro
+   definition. If you read the comments of struct line_map_macro in
+   line-map.h, this is the yI.  */
+
+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;
     }
@@ -210,7 +364,7 @@ linemap_line_start (struct line_maps *set, linenum_type to_line,
 	  /* If the column number is ridiculous or we've allocated a huge
 	     number of source_locations, give up on column numbers. */
 	  max_column_hint = 0;
-	  if (highest >0xF0000000)
+	  if (highest > 0xF0000000)
 	    return 0;
 	  column_bits = 0;
 	}
@@ -224,16 +378,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 +406,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 +428,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 +438,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;
+
+  if (set ==  NULL || line < RESERVED_LOCATION_COUNT)
+    return NULL;
 
-  mn = set->cache;
-  mx = set->used;
+  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 +498,346 @@ 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;
+    }
+
+  while (mx - mn > 1)
+    {
+      md = (mx + mn) / 2;
+      if (MAP_START_LOCATION (LINEMAPS_MACRO_MAP_AT (set, md)) > line)
+	mn = md;
+      else
+	mx = md;
+    }
+
+  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 locus of a token in a replacement-list of a
+   macro expansion return the location of said token in the definition
+   of the macro.
+
+   Read the comments of struct line_map and struct line_map_macro in
+   line-map.h to understand what a macro expansion point is.
+
+   If RETURN_MACRO_PARM_USAGE_POINT_P is TRUE and if LOCATION is the
+   locus of a token that is an argument of a macro M, this function
+   returns the locus of the parameter replaced by the argument, in the
+   definition of M. This is the yI in the comments of struct
+   line_map_macro in line-map.h.
+
+   Note that if the token is a builtin the function returns the
+   location of the expansion point of the macro.  */
+
+source_location
+linemap_macro_map_loc_to_def_point (const struct line_map *map,
+				    source_location location,
+				    bool return_macro_parm_usage_point_p)
+{
+  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));
+
+  if (return_macro_parm_usage_point_p)
+    location = MACRO_MAP_LOCATIONS (map)[2 * token_no + 1];
+  else
+    location = MACRO_MAP_LOCATIONS (map)[2 * token_no];
+
+  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.  */
+
+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;
+}
+
+/* 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_loc_to_def_point (struct line_maps *set,
+				source_location location,
+				const struct line_map **original_map,
+				bool return_macro_parm_usage_point_p)
+{
+  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,
+					    return_macro_parm_usage_point_p);
+    }
+
+  if (original_map)
+    *original_map = map;
+  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_source_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 column number corresponding to location LOCATION.
+
+   If LOCATION is the source location of token that is part of the
+   replacement-list of a macro expansion return the column number of
+   the macro expansion point.
+
+   SET is the line map set LOCATION comes from.  */
+
+int
+linemap_get_source_column (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_COLUMN (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_file_path (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 0 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_macro_loc_to_def_point (set, location, &map, false);
+
+  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);
+}
+
+/* Return TRUE if PRE denotes a location that is before POST, FALSE
+   otherwise. LINE_MAPS is the set of line maps PRE and POST were
+   allocated from.  */
+
+bool
+linemap_location_before_p (struct line_maps *set,
+			   source_location  pre,
+			   source_location post)
+{
+  bool pre_from_macro_p, post_from_macro_p;
+
+  if (pre == post)
+    return false;
+
+  pre_from_macro_p =
+    linemap_location_from_macro_expansion_p (set, pre);
+  post_from_macro_p =
+    linemap_location_from_macro_expansion_p (set, post);
+
+  if (pre_from_macro_p != post_from_macro_p)
+    {
+      if (pre_from_macro_p)
+	pre = linemap_macro_loc_to_exp_point (set, pre, NULL);
+      else
+	post = linemap_macro_loc_to_exp_point (set, post, NULL);
+    }
+
+  return pre < post;
 }
 
 /* Print an include trace, for e.g. the -H option of the preprocessor.  */
@@ -313,5 +849,109 @@ 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));
+}
+
+/* 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.
+
+   Finally, if SPELLING_LOC is not NULL, *RESULTING_LOC is set to the
+   location to which LOC was resolved, and similarly, *LOC_MAP is set
+   to its map.  */
+
+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_def_point (set, loc, map, false);
+      break;
+    case LRK_MACRO_PARM_REPLACEMENT_POINT:
+      loc = linemap_macro_loc_to_def_point (set, loc, map, true);
+      break;
+    default:
+      abort ();
+    }
+  return loc;
+}
+
+/* Expand source code location LOC and return a user readable source
+   code 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.  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..03fe79e 100644
--- a/libcpp/macro.c
+++ b/libcpp/macro.c
@@ -177,7 +177,7 @@ _cpp_builtin_macro_text (cpp_reader *pfile, cpp_hashnode *node)
 	  while (! MAIN_FILE_P (map))
 	    map = INCLUDED_FROM (pfile->line_table, map);
 
-	name = map->to_file;
+	name = ORDINARY_MAP_FILE_NAME (map);
 	len = strlen (name);
 	buf = _cpp_unaligned_alloc (pfile, len * 2 + 3);
 	result = buf;
@@ -196,14 +196,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_source_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

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

* Re: [PATCH 1/7] Linemap infrastructure for virtual locations
  2011-09-01 10:37                     ` Dodji Seketeli
@ 2011-09-07 19:26                       ` Jason Merrill
  2011-09-08 12:41                         ` Dodji Seketeli
  0 siblings, 1 reply; 135+ messages in thread
From: Jason Merrill @ 2011-09-07 19:26 UTC (permalink / raw)
  To: Dodji Seketeli; +Cc: gcc-patches, tromey, gdr, joseph, burnus, charlet, bonzini

On 09/01/2011 06:36 AM, Dodji Seketeli wrote:
> +#ifdef ENABLE_CHECKING
> +
> +/* 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);})

If you're going to use a statement-expression, you need to check that 
you're being compiled with GCC; tree.h uses

  #if defined ENABLE_TREE_CHECKING && (GCC_VERSION >= 2007)

> -         if (highest >0xF0000000)
> +         if (highest > 0xF0000000)

Unnecessary whitespace change.

OK with those changes.

Jason

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

* Re: [PATCH 1/7] Linemap infrastructure for virtual locations
  2011-09-07 19:26                       ` Jason Merrill
@ 2011-09-08 12:41                         ` Dodji Seketeli
  2011-09-09  7:45                           ` Jason Merrill
  2011-09-09  8:57                           ` Jason Merrill
  0 siblings, 2 replies; 135+ messages in thread
From: Dodji Seketeli @ 2011-09-08 12:41 UTC (permalink / raw)
  To: Jason Merrill; +Cc: gcc-patches, tromey, gdr, joseph, burnus, charlet, bonzini

Jason Merrill <jason@redhat.com> writes:

> On 09/01/2011 06:36 AM, Dodji Seketeli wrote:
>> +#ifdef ENABLE_CHECKING
>> +
>> +/* 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);})
>
> If you're going to use a statement-expression, you need to check that
> you're being compiled with GCC; tree.h uses
>
>  #if defined ENABLE_TREE_CHECKING && (GCC_VERSION >= 2007)

Oh, okay.  Thank you for catching that.  I did that in the patch below.

>
>> -         if (highest >0xF0000000)
>> +         if (highest > 0xF0000000)
>
> Unnecessary whitespace change.

Fixed.

I have a question about this.  It seems to me that adding the whitespace
there improves the consistency of the code base, and I thought that it
was allowed to make those changes if there are tangent to other
meaningful changes done in that area.  If even that is not allowed, then
does that mean that such whitespace nits in the code base can never be
fixed?  I am not willing to argue over that change, I am just trying to
understand.

> OK with those changes.

Thanks.  Below is the updated patch.  I have re-based the whole patch
series over the recent trunk, bootstrapped and tested it on
x86_64-unknown-linux-gnu.  I am now going through bootstrap and test of
this patch alone.

From: Dodji Seketeli <dodji@redhat.com>
Date: Fri, 3 Dec 2010 13:20:26 +0100
Subject: [PATCH 1/7] Linemap infrastructure for virtual locations

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.

Boostrapped with --enable-languages=all,ada and passed regression
tests on x86_unknown-linux-gnu against trunk.

libcpp/

	* 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): New macros.
	(linemap_position_for_line_and_column)
	(linemap_tracks_macro_expansion_locs_p, linemap_enter_macro)
	(linemap_add_macro_token, linemap_macro_expansion_map_p)
	(linemap_macro_loc_to_exp_point, linemap_macro_loc_to_def_point)
	(linemap_macro_map_loc_to_def_point)
	(linemap_macro_map_loc_to_exp_point, 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
	(linemap_assert): New macro.
	(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_enter_macro)
	(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_loc_to_exp_point, linemap_map_get_index)
	(linemap_macro_loc_to_def_point, 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_location_before_p):
	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.
	* 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/
	* 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/c-family

	* 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/java/

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

gcc/ada/

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

gcc/fortran/

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

gcc/testsuite/

	* gcc.dg/cpp/pragma-diagnostic-1.c: New test.
---
 gcc/ada/gcc-interface/trans.c                  |   10 +-
 gcc/c-family/c-lex.c                           |    6 +-
 gcc/c-family/c-ppoutput.c                      |   41 +-
 gcc/diagnostic.c                               |   11 +-
 gcc/fortran/cpp.c                              |   22 +-
 gcc/input.c                                    |    9 +-
 gcc/input.h                                    |   18 +-
 gcc/java/jcf-parse.c                           |    2 +-
 gcc/testsuite/gcc.dg/cpp/pragma-diagnostic-1.c |   32 +
 libcpp/directives.c                            |   16 +-
 libcpp/files.c                                 |    5 +-
 libcpp/include/line-map.h                      |  710 ++++++++++++++++++++--
 libcpp/init.c                                  |    4 +-
 libcpp/internal.h                              |    3 +-
 libcpp/line-map.c                              |  798 +++++++++++++++++++++---
 libcpp/macro.c                                 |   12 +-
 16 files changed, 1497 insertions(+), 202 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 13df71f..1f56d5d 100644
--- a/gcc/ada/gcc-interface/trans.c
+++ b/gcc/ada/gcc-interface/trans.c
@@ -278,7 +278,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.  */
@@ -7863,12 +7863,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..b4bc9ce 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,8 @@ 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_macro_loc_to_exp_point (line_table, line, &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..5b7ee9d 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,231 @@ 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 locations 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 we are at the expansion point of MACRO.  Each xI is the
+     location of the Ith token of the replacement-list. Now it gets
+     confusing. the xI is the location of the Ith token of the
+     replacement-list at the macro *definition* point. Not at the
+     macro replacement point. Okay, let's try to explain this below.
+
+     Imagine this:
+
+        #define OPERATION(OP0, OPERATOR, OP1) \
+                OP0 OPERATOR OP1 <-- #0
+
+	#define PLUS(A, B) OPERATION (A, +, B)  <--- #1
+
+	int a = PLUS (1,2); <--- #2
+     
+     In #2, there is a macro map for the expansion of PLUS. PLUS is
+     expanded into the replacement-list made of the tokens:
+     
+        OPERATION, (, A, +, B, )
+
+     and then further expanded into the tokens:
+
+        1, +, 2.
+
+     Let's consider the case of token "+" here. That will help us
+     understand what the xI we were talking about earlier means.
+
+     The token '+' has two locations, so to speak. One in the context
+     of the macro *expansion* of PLUS in #2 and one in the context of
+     the macro *definition* of PLUS in #1. These two locations are
+     encoded in the the latter context, somehow in the xI we are
+     talking about.
+
+     xI is roughly the index of the token inside the replacement-list
+     at the expansion point. So for '+', it's index would then be 1
+     [The index of token '1' would be 0 and the index of token 2 would
+     be 1]. So if '+' is our current xI, it is actualy an x1.
+
+     The value of x1 is the location of the token '+' inside the
+     replacement-list of PLUS at the definition point of PLUS. It is
+     its spelling location in #1.
+
+     So x0 would have described the token '1', x1 describes the token
+     '+' and x2 describes the token '2'.
+
+     Now what's the y1 then? Remember, we said macro_locations is an
+     array of pairs (xI,yI). We now know what the xI is, now let's
+     look at the yI.
+
+     Let's look at the token '+' again. We said it has two locations
+     somehow. Actually it has 3. Kind of. As '+' is an argument passed
+     to the macro OPERATION [at the definition point of the macro
+     PLUS], it would be nice to record the source location of the
+     *parameter* of OPERATION that is replaced by the argument '+'.
+     In other words, we want to record the location of the token
+     "OPERATOR" in the replacement-list of OPERATION, at the
+     /definition/ point of OPERATION in #0. And that is y1.
+
+     So when (xI,yI) describes a token that is passed as an argument
+     to a macro M, the yI is the location of the macro parameter that
+     the argument replaces, at the definition point of M. If (xI,yI)
+     does not describe a token that is passed as an argument to a
+     macro, xI == yI.
+   */
+  source_location * GTY((length ("2 * %h.n_tokens"))) macro_locations;
+
+  /* This is the location of the expansion point of the current macro
+     map.  That expansion point 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 +300,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 +434,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 +450,354 @@ 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 *);
+
+/* 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 source location for a token that is part of a
+   macro replacement-list at a macro expansion point.
+
+   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 orginal location of the token at the definition
+   point of the macro. If you read the extensive comments of struct
+   line_map_macro in line-map.h, this is the xI.
+
+   If the token is part of a macro argument, ORIG_PARM_REPLACEMENT_LOC
+   is the location of the point at wich the token (the argument)
+   replaces the macro parameter in the context of the relevant macro
+   definition. If you read the comments of struct line_map_macro in
+   line-map.h, this is the yI.  */
+source_location linemap_add_macro_token (const struct line_map *,
+					 unsigned int,
+					 source_location,
+					 source_location);
+
+/* 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 *);
+
+/* 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.  */
+source_location linemap_macro_loc_to_exp_point (struct line_maps *,
+						source_location,
+						const struct line_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_loc_to_def_point (struct line_maps *,
+						source_location,
+						const struct line_map **,
+						bool);
+
+/* If LOCATION is the locus of a token in a replacement-list of a
+   macro expansion return the location of said token in the definition
+   of the macro.
+
+   Read the comments of struct line_map and struct line_map_macro in
+   line-map.h to understand what a macro expansion point is.
+
+   If RETURN_MACRO_PARM_USAGE_POINT_P is TRUE and if LOCATION is the
+   locus of a token that is an argument of a macro M, this function
+   returns the locus of the parameter replaced by the argument, in the
+   definition of M. This is the yI in the comments of struct
+   line_map_macro in line-map.h.
+
+   Note that if the token is a builtin the function returns the
+   location of the expansion point of the macro.  */
+source_location linemap_macro_map_loc_to_def_point (const struct line_map*,
+						    source_location,
+						    bool);
+
+/* 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*,
+						    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 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_source_line (struct line_maps *,
+			     source_location);
+
+/* Return the column number corresponding to location LOCATION.
+
+   If LOCATION is the source location of token that is part of the
+   replacement-list of a macro expansion return the column number of
+   the macro expansion point.
+
+   SET is the line map set LOCATION comes from.  */
+int linemap_get_source_column (struct line_maps *,
+			       source_location);
+
+/* Return the name of the macro associated to MACRO_MAP.  */
+const char* linemap_map_get_macro_name (const struct line_map*);
+
+/* 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_file_path (struct line_maps *,
+				   source_location);
+
+/* 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 0 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 TRUE if PRE denotes a location that is before POST, FALSE
+   otherwise. LINE_MAPS is the set of line maps PRE and POST were
+   allocated from.  */
+bool linemap_location_before_p (struct line_maps *set,
+				source_location   pre,
+				source_location   post);
+
+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_PARM_REPLACEMENT_POINT
+};
+
+/* 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.
+
+   Finally, if SPELLING_LOC is not NULL, *RESULTING_LOC is set to the
+   location to which LOC was resolved, and similarly, *LOC_MAP is set
+   to its map.  */
+
+source_location linemap_resolve_location (struct line_maps *,
+					  source_location loc,
+					  enum location_resolution_kind lrk,
+					  const struct line_map **loc_map);
+
+/* Expand source code location LOC and return a user readable source
+   code 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.  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..588e8ed 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)
diff --git a/libcpp/line-map.c b/libcpp/line-map.c
index 2a0749a..959566c 100644
--- a/libcpp/line-map.c
+++ b/libcpp/line-map.c
@@ -23,24 +23,21 @@ along with this program; see the file COPYING3.  If not see
 #include "config.h"
 #include "system.h"
 #include "line-map.h"
+#include "cpplib.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);
 /* 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 +48,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 +119,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)))));
 
-  if (set->used == set->allocated)
+  /* When we enter the file for the first time reason cannot be
+     LC_RENAME.  */
+  linemap_assert (!(set->depth == 0 && reason == LC_RENAME));
+
+  /* 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 +144,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 +184,173 @@ 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 source location for a token that is part of a
+   macro replacement-list at a macro expansion point.
+
+   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 orginal location of the token at the definition
+   point of the macro. If you read the extensive comments of struct
+   line_map_macro in line-map.h, this is the xI.
+
+   If the token is part of a macro argument, ORIG_PARM_REPLACEMENT_LOC
+   is the location of the point at wich the token (the argument)
+   replaces the macro parameter in the context of the relevant macro
+   definition. If you read the comments of struct line_map_macro in
+   line-map.h, this is the yI.  */
+
+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 +378,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 +406,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 +428,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 +438,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;
+
+  if (set ==  NULL || line < RESERVED_LOCATION_COUNT)
+    return NULL;
 
-  mn = set->cache;
-  mx = set->used;
+  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 +498,346 @@ 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;
+    }
+
+  while (mx - mn > 1)
+    {
+      md = (mx + mn) / 2;
+      if (MAP_START_LOCATION (LINEMAPS_MACRO_MAP_AT (set, md)) > line)
+	mn = md;
+      else
+	mx = md;
+    }
+
+  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 locus of a token in a replacement-list of a
+   macro expansion return the location of said token in the definition
+   of the macro.
+
+   Read the comments of struct line_map and struct line_map_macro in
+   line-map.h to understand what a macro expansion point is.
+
+   If RETURN_MACRO_PARM_USAGE_POINT_P is TRUE and if LOCATION is the
+   locus of a token that is an argument of a macro M, this function
+   returns the locus of the parameter replaced by the argument, in the
+   definition of M. This is the yI in the comments of struct
+   line_map_macro in line-map.h.
+
+   Note that if the token is a builtin the function returns the
+   location of the expansion point of the macro.  */
+
+source_location
+linemap_macro_map_loc_to_def_point (const struct line_map *map,
+				    source_location location,
+				    bool return_macro_parm_usage_point_p)
+{
+  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));
+
+  if (return_macro_parm_usage_point_p)
+    location = MACRO_MAP_LOCATIONS (map)[2 * token_no + 1];
+  else
+    location = MACRO_MAP_LOCATIONS (map)[2 * token_no];
+
+  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.  */
+
+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;
+}
+
+/* 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_loc_to_def_point (struct line_maps *set,
+				source_location location,
+				const struct line_map **original_map,
+				bool return_macro_parm_usage_point_p)
+{
+  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,
+					    return_macro_parm_usage_point_p);
+    }
+
+  if (original_map)
+    *original_map = map;
+  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_source_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 column number corresponding to location LOCATION.
+
+   If LOCATION is the source location of token that is part of the
+   replacement-list of a macro expansion return the column number of
+   the macro expansion point.
+
+   SET is the line map set LOCATION comes from.  */
+
+int
+linemap_get_source_column (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_COLUMN (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_file_path (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 0 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_macro_loc_to_def_point (set, location, &map, false);
+
+  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);
+}
+
+/* Return TRUE if PRE denotes a location that is before POST, FALSE
+   otherwise. LINE_MAPS is the set of line maps PRE and POST were
+   allocated from.  */
+
+bool
+linemap_location_before_p (struct line_maps *set,
+			   source_location  pre,
+			   source_location post)
+{
+  bool pre_from_macro_p, post_from_macro_p;
+
+  if (pre == post)
+    return false;
+
+  pre_from_macro_p =
+    linemap_location_from_macro_expansion_p (set, pre);
+  post_from_macro_p =
+    linemap_location_from_macro_expansion_p (set, post);
+
+  if (pre_from_macro_p != post_from_macro_p)
+    {
+      if (pre_from_macro_p)
+	pre = linemap_macro_loc_to_exp_point (set, pre, NULL);
+      else
+	post = linemap_macro_loc_to_exp_point (set, post, NULL);
+    }
+
+  return pre < post;
 }
 
 /* Print an include trace, for e.g. the -H option of the preprocessor.  */
@@ -313,5 +849,109 @@ 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));
+}
+
+/* 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.
+
+   Finally, if SPELLING_LOC is not NULL, *RESULTING_LOC is set to the
+   location to which LOC was resolved, and similarly, *LOC_MAP is set
+   to its map.  */
+
+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_def_point (set, loc, map, false);
+      break;
+    case LRK_MACRO_PARM_REPLACEMENT_POINT:
+      loc = linemap_macro_loc_to_def_point (set, loc, map, true);
+      break;
+    default:
+      abort ();
+    }
+  return loc;
+}
+
+/* Expand source code location LOC and return a user readable source
+   code 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.  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..03fe79e 100644
--- a/libcpp/macro.c
+++ b/libcpp/macro.c
@@ -177,7 +177,7 @@ _cpp_builtin_macro_text (cpp_reader *pfile, cpp_hashnode *node)
 	  while (! MAIN_FILE_P (map))
 	    map = INCLUDED_FROM (pfile->line_table, map);
 
-	name = map->to_file;
+	name = ORDINARY_MAP_FILE_NAME (map);
 	len = strlen (name);
 	buf = _cpp_unaligned_alloc (pfile, len * 2 + 3);
 	result = buf;
@@ -196,14 +196,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_source_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.
-- 
		Dodji

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

* Re: [PATCH 1/7] Linemap infrastructure for virtual locations
  2011-09-08 12:41                         ` Dodji Seketeli
@ 2011-09-09  7:45                           ` Jason Merrill
  2011-09-09  8:57                           ` Jason Merrill
  1 sibling, 0 replies; 135+ messages in thread
From: Jason Merrill @ 2011-09-09  7:45 UTC (permalink / raw)
  To: Dodji Seketeli; +Cc: gcc-patches, tromey, gdr, joseph, burnus, charlet, bonzini

On 09/08/2011 06:32 AM, Dodji Seketeli wrote:
> I have a question about this.  It seems to me that adding the whitespace
> there improves the consistency of the code base, and I thought that it
> was allowed to make those changes if there are tangent to other
> meaningful changes done in that area.  If even that is not allowed, then
> does that mean that such whitespace nits in the code base can never be
> fixed?  I am not willing to argue over that change, I am just trying to
> understand.

I usually only fix whitespace issues when I'm already changing something 
else on that line; I like to keep a patch specific to what it's actually 
fixing.  For other whitespace issues I'd put them in a separate patch.

But other people may feel differently about this question.

Jason

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

* Re: [PATCH 1/7] Linemap infrastructure for virtual locations
  2011-09-08 12:41                         ` Dodji Seketeli
  2011-09-09  7:45                           ` Jason Merrill
@ 2011-09-09  8:57                           ` Jason Merrill
  1 sibling, 0 replies; 135+ messages in thread
From: Jason Merrill @ 2011-09-09  8:57 UTC (permalink / raw)
  To: Dodji Seketeli; +Cc: gcc-patches, tromey, gdr, joseph, burnus, charlet, bonzini

On 09/08/2011 06:32 AM, Dodji Seketeli wrote:
> Below is the updated patch.

OK.

Jason


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

* Re: [PATCH 2/7] Generate virtual locations for tokens
  2011-08-09 15:30       ` Dodji Seketeli
@ 2011-09-12 21:15         ` Jason Merrill
  2011-09-14 10:01           ` Dodji Seketeli
  0 siblings, 1 reply; 135+ messages in thread
From: Jason Merrill @ 2011-09-12 21:15 UTC (permalink / raw)
  To: Dodji Seketeli; +Cc: gcc-patches, tromey, gdr, joseph, burnus, charlet, bonzini

On 08/09/2011 10:54 AM, Dodji Seketeli wrote:
> +      goto ftrack_macro_expansion_with_arg;
> +
> +    case OPT_ftrack_macro_expansion_:
> +    ftrack_macro_expansion_with_arg:

Instead of the goto, just write /* Fall through.  */

> +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. It
> +is the default value.

"It is the default value" sounds to me like no -ftrack-macro-expansion 
option is equivalent to -ftrack-macro-expansion=2, rather than =0.

> +     expansion of arguments of function-like macro. all macro
> +     expansion. 2 Means we do track all macro expansions. This last

Seems like the "all macro expansion" is left over from a previous 
version of this sentence.

> +/* 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;

Why track virtual locations separately rather than use them directly as 
the src_loc of the tokens?

Jason

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

* Re: [PATCH 3/7] Emit macro expansion related diagnostics
  2011-08-04 15:32       ` Dodji Seketeli
@ 2011-09-12 21:54         ` Jason Merrill
  2011-09-16  8:19           ` Dodji Seketeli
  0 siblings, 1 reply; 135+ messages in thread
From: Jason Merrill @ 2011-09-12 21:54 UTC (permalink / raw)
  To: Dodji Seketeli; +Cc: gcc-patches, tromey, gdr, joseph, burnus, charlet, bonzini

On 08/04/2011 11:32 AM, Dodji Seketeli wrote:
> +++ b/gcc/diagnostic.c
> @@ -30,6 +30,7 @@ along with GCC; see the file COPYING3.  If not see
>  #include "input.h"
>  #include "intl.h"
>  #include "diagnostic.h"
> +#include "vec.h"

Do you still need this?

>  // Just discard errors pointing at header files
>  // { dg-prune-output "include" }
> +// { dg-prune-output "        from" }

These should be pruned by testsuite/lib/prune.exp.  I'm surprised they 
aren't already.

> +#define APPEND_LOC_TO_VEC(LOC)                                         \
> +  if (num_locs >=3D loc_vec_capacity)                                  \
> +    {                                                                  \
> +      loc_vec_capacity +=3D 4;                                         \
> +      loc_vec =3D XRESIZEVEC (loc_t, loc_vec, loc_vec_capacity);
> \
> +    }                                                                  \
> +  loc_vec[num_locs++] =3D LOC;

Why not use VEC since we're in gcc/ here?

> +/* Unwind the different macro expansions that lead to the token which
> +   location is WHERE and emit diagnostics showing the resulting
> +   unwound macro expansion stack.  If TOPMOST_EXP_POINT_MAP is
> +   non-null, *TOPMOST_EXP_POINT_MAP is set to the map of the expansion
> +   point of the top most macro of the stack.  This must be an ordinary
> +   map.  */

I find the use of "top" here confusing.  You mean the place in the 
source that first triggered the macro expansion, right?  Can we avoid 
talking about stacks here?

> +  /* Walk the map_vec and print the macro expansion stack, unless the
> +     topmost macro which expansion triggered this stack [assuming the
> +     stack grows downwards] was expanded inside a system header.  */

Abstract stacks are said to grow upwards; only the representation of a 
call stack on certain architectures can grow downwards.  The top of the 
stack is the most recently pushed element, which in the case of macro 
expansion would be the innermost macro expanded.

> +  while (unwind)
> +    {
...
> +      if (!linemap_macro_expansion_map_p (map))
> +       unwind =3D false;
> +    }

This seems like a job for do/while.

> +       diagnostic->location =3D resolved_def_loc;
> +       pp_base_set_prefix (context->printer,
> +                           diagnostic_build_prefix (context,
> +                                                    diagnostic));

Using pp_verbatim like in maybe_print_constexpr_context seems simpler to 
me, but if you prefer this approach that's fine.

Jason

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

* Re: [PATCH 5/7] Add line map statistics to -fmem-report output
  2011-07-16 14:39     ` [PATCH 5/7] Add line map statistics to -fmem-report output Dodji Seketeli
@ 2011-09-12 22:07       ` Jason Merrill
  2011-09-16  8:29         ` Dodji Seketeli
  0 siblings, 1 reply; 135+ messages in thread
From: Jason Merrill @ 2011-09-12 22:07 UTC (permalink / raw)
  To: Dodji Seketeli; +Cc: gcc-patches, tromey, gdr, joseph, burnus, charlet, paolo

On 07/16/2011 10:37 AM, Dodji Seketeli wrote:
> +#define ONE_M ONE_K * ONE_K

Parenthesize this so that users don't need to.

> +  macro_maps_used_size =
> +    LINEMAPS_MACRO_USED (set) * sizeof (struct line_map)
> +    + macro_maps_locations_size;

It seems odd to add in the locations size here since it's also printed 
separately.

> +  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));

This seems oddly sorted.  And why the difference between ordinary and 
macro maps in terms of what is printed?

> +/* Counters defined in libcpp's macro.c.  */
> +extern unsigned num_expanded_macros_counter;
> +extern unsigned num_macro_tokens_counter;

These should be part of struct linemap_stats.

Jason

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

* Re: [PATCH 4/7] Support -fdebug-cpp option
  2011-07-16 14:39     ` [PATCH 4/7] Support -fdebug-cpp option Dodji Seketeli
  2011-08-21 11:02       ` Alexandre Oliva
@ 2011-09-12 22:07       ` Jason Merrill
  2011-09-16  8:23         ` Dodji Seketeli
  1 sibling, 1 reply; 135+ messages in thread
From: Jason Merrill @ 2011-09-12 22:07 UTC (permalink / raw)
  To: Dodji Seketeli; +Cc: gcc-patches, tromey, gdr, joseph, burnus, charlet, paolo

On 08/24/2011 10:06 AM, Tom Tromey wrote:
> Dodji> Would it be acceptable to just change the output of -fdirective to fit?
> Dodji> Or are we bound to not breaking existing consumers?
>
> I think changing it would be fine.

I agree.

On 07/16/2011 10:37 AM, Dodji Seketeli wrote:
>  }
> +
> +void
> +linemap_dump_location (struct line_maps *set,

Comment.

> +@item -fdebug-cpp
> +@opindex fdebug-cpp

Please add something to clarify that this is only useful for debugging GCC.

Jason

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

* Re: [PATCH 6/7] Kill pedantic warnings on system headers macros
  2011-07-16 14:38     ` [PATCH 6/7] Kill pedantic warnings on system headers macros Dodji Seketeli
@ 2011-09-12 22:09       ` Jason Merrill
  2011-09-16 11:25         ` Dodji Seketeli
  0 siblings, 1 reply; 135+ messages in thread
From: Jason Merrill @ 2011-09-12 22:09 UTC (permalink / raw)
  To: Dodji Seketeli; +Cc: gcc-patches, tromey, gdr, joseph, burnus, charlet, paolo

On 07/16/2011 10:37 AM, Dodji Seketeli wrote:
> +  location_t here = c_parser_peek_token (parser)->location;

Perhaps "first_token_loc"?  It's unfortunate that we don't retain the 
locations of the individual declspecs, but I don't expect you to fix that.

> +           SYNTAX_ERROR2_AT (prev_virtual_location,
> +                             "missing binary operator before token \"%s\"",
> +                             cpp_token_as_text (pfile, op.token));

It seems to me that the "missing X before" errors should point to the 
current token, not the previous one.  So you can drop prev_virtual_location.

Jason

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

* Re: [PATCH 7/7] Reduce memory waste due to non-power-of-2 allocs
  2011-07-16 15:34     ` [PATCH 7/7] Reduce memory waste due to non-power-of-2 allocs Dodji Seketeli
@ 2011-09-12 22:25       ` Jason Merrill
  2011-09-17 13:41         ` Dodji Seketeli
  0 siblings, 1 reply; 135+ messages in thread
From: Jason Merrill @ 2011-09-12 22:25 UTC (permalink / raw)
  To: Dodji Seketeli; +Cc: gcc-patches, tromey, gdr, joseph, burnus, charlet, paolo

On 07/16/2011 10:37 AM, Dodji Seketeli wrote:
> Ideally, I'd prefer some parts of this patch to be integrated into the
> memory allocator.  That is, I'd like to see the memory allocator have
> an interface that returns the actual size of memory it has allocated.
> This would help client code like this one to avoid requesting memory
> unnecessarily.

This sounds good to me.  Or, it would probably be simpler to just 
provide a separate function for rounding up the allocated size using the 
same code as the allocator.

Jason

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

* Re: [PATCH 2/7] Generate virtual locations for tokens
  2011-09-12 21:15         ` Jason Merrill
@ 2011-09-14 10:01           ` Dodji Seketeli
  2011-09-14 22:56             ` Jason Merrill
  0 siblings, 1 reply; 135+ messages in thread
From: Dodji Seketeli @ 2011-09-14 10:01 UTC (permalink / raw)
  To: Jason Merrill; +Cc: gcc-patches, tromey, gdr, joseph, burnus, charlet, bonzini

Jason Merrill <jason@redhat.com> writes:

> On 08/09/2011 10:54 AM, Dodji Seketeli wrote:
> > +      goto ftrack_macro_expansion_with_arg;
> > +
> > +    case OPT_ftrack_macro_expansion_:
> > +    ftrack_macro_expansion_with_arg:
> 
> Instead of the goto, just write /* Fall through.  */

Fixed in the patch below.

> 
> > +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. It
> > +is the default value.
> 
> "It is the default value" sounds to me like no -ftrack-macro-expansion
> option is equivalent to -ftrack-macro-expansion=2, rather than =0.

I tried to propose a better wording in the attached patch.

> 
> > +     expansion of arguments of function-like macro. all macro
> > +     expansion. 2 Means we do track all macro expansions. This last
> 
> Seems like the "all macro expansion" is left over from a previous
> version of this sentence.

Oops, good catch.  Fixed.

> 
> > +/* 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;
> 
> Why track virtual locations separately rather than use them directly
> as the src_loc of the tokens?

My understanding is that a cpp_token (yielded by the public entry points
cpp_get_token and cpp_get_token_with_location) is supposed to be
immutable.  E.g:

#define MACRO FOO //#0

MACRO  //#1

MACRO  //#2

The virtual locations of FOO in #1 and #2 must be different and be
able to coexist at a given point in time.  E.g, let T be the token
representing FOO.  Then suppose that a client code would get a hold on
T as a result of calling cpp_get_token (and assume that T->src_loc is
its virtual location) at #1.  Then once we reach #2, the T->src_loc
would magically change and be set to the location #2.  The client code
would mistakenly think that T->src_loc still points to #1.  Oops.

A given virtual location of a token, on the other hand, depends on the
particular macro expansion context of that token, at a point in time.
I guess that's why there is a dedicated function
(cpp_get_token_with_location as opposed to cpp_get_token) to get the
virtual location of that token in the current macro expansion context.

To comply with this invariant, the initial patch of Tom was cloning T
into T', and was using T'->src_loc to track the virtual location of T.
Which is close to what you are proposing, while respecting the
invariant.  But it turned out that was using too much memory :-(.  So
we devised this scheme instead.

Thanks.

From: Dodji Seketeli <dodji@redhat.com>
Date: Sat, 4 Dec 2010 14:04:29 +0100
Subject: [PATCH 2/7] Generate virtual locations for tokens

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.

The combination of this patch and the previous one bootstraps with
	--enable-languages=all,ada and passes regression tests on
x86_64-unknown-linux-gnu.

gcc/
	* 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/

	* 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/

	* 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>: Change the type of this to void.
	(struct cpp_context)<direct_p>: Remove.
	(_cpp_remaining_tokens_num_in_context): Declare new function.
	* 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)<expanded_capacity, virt_locs>:
	(struct macro_arg)<virt_locs_capacity, 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_args_mem, ensure_expanded_args_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_append_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.
	(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_args_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 and
	maybe_adjust_loc_for_trad_cpp.
	(_cpp_backup_tokens): Support the new kinds of token contexts.
---
 gcc/c-family/c-opts.c   |   12 +
 gcc/c-family/c.opt      |    8 +
 gcc/doc/cppopts.texi    |   18 +
 gcc/doc/invoke.texi     |    6 +-
 libcpp/include/cpplib.h |    8 +
 libcpp/init.c           |    1 +
 libcpp/internal.h       |   54 ++-
 libcpp/lex.c            |   41 ++-
 libcpp/macro.c          | 1308 ++++++++++++++++++++++++++++++++++++++++++-----
 9 files changed, 1321 insertions(+), 135 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 957d75c..7e1b7c2 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/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/init.c b/libcpp/init.c
index 6303868..e10648e 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;
diff --git a/libcpp/internal.h b/libcpp/internal.h
index 588e8ed..ee409ea 100644
--- a/libcpp/internal.h
+++ b/libcpp/internal.h
@@ -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,20 @@ 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;
-
-  /* True if utoken element is token, else ptoken.  */
-  bool direct_p;
+  /* If tokens_kind is TOKEN_KIND_EXTENDED, then, if we 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
+     hash_node, 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 TKEN_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.  */
+  void *macro;
+
+  /* This determines the type of tokens held by this context.  */
+  enum context_tokens_kind tokens_kind;
 };
 
 struct lexer_state
@@ -605,6 +648,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/macro.c b/libcpp/macro.c
index 03fe79e..0544eb1 100644
--- a/libcpp/macro.c
+++ b/libcpp/macro.c
@@ -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,64 @@ 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.  */
+  size_t expanded_capacity;     /* total size of expanded array.  */
+  source_location *virt_locs;	/* Where virtual locations for
+				   unexpanded tokens are stored.  */
+  unsigned virt_locs_capacity;	/* Total size of virtual locations
+				   array.  */
+  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 line macro
+   argument.  */
+typedef struct macro_arg_token_iter macro_arg_token_iter;
+struct macro_arg_token_iter
+{
+  /* The cpp_reader the macro comes from.  */
+  cpp_reader *pfile;
+  /* The kind of token over which we are supposed to iterate.  */
+  enum macro_arg_token_kind kind;
+  /* The function-like macro the tokens come from.  */
+  const macro_arg *arg;
+  /* 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 +106,56 @@ 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_args_mem (cpp_reader *, macro_arg *, size_t);
+static void ensure_expanded_args_room (cpp_reader *, macro_arg *, size_t);
+static void delete_macro_args (_cpp_buff*, unsigned num_args);
+static void set_arg_token (cpp_reader *, macro_arg *, const cpp_token *,
+			   source_location, size_t,
+			   enum macro_arg_token_kind);
+static const source_location *get_arg_token_location (cpp_reader *,
+						      const macro_arg *,
+						      enum macro_arg_token_kind);
+static const cpp_token **arg_token_ptr_at (cpp_reader *,
+					   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 *, cpp_reader*,
+				       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 (cpp_reader *,
+						   const cpp_token **,
+						   source_location *, 
+						   const cpp_token *,
+						   source_location,
+						   source_location,
+						   const struct line_map *,
+						   unsigned int *);
+
+static const cpp_token **tokens_buff_append_token (cpp_reader *,
+						   _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 +167,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.  */
@@ -507,7 +609,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
@@ -517,10 +619,26 @@ 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++;
+	  if (context->macro)
+	    {
+	      macro_context *m = (macro_context *) context->macro;
+	      m->cur_virt_loc++;
+	    }
+	}
 
       if (rhs->type == CPP_PADDING)
 	{
@@ -584,23 +702,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;
@@ -615,9 +747,16 @@ collect_args (cpp_reader *pfile, const cpp_hashnode *node,
     {
       unsigned int paren_depth = 0;
       unsigned int ntokens = 0;
+      num_args_alloced++;
 
       argc++;
       arg->first = (const cpp_token **) buff->cur;
+      if (track_macro_expansion_p)
+	{
+	  arg->virt_locs_capacity = DEFAULT_NUM_TOKENS_PER_MACRO_ARG;
+	  arg->virt_locs = XNEWVEC (source_location,
+				    arg->virt_locs_capacity);
+	}
 
       for (;;)
 	{
@@ -625,11 +764,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 > arg->virt_locs_capacity))
+	    {
+	      arg->virt_locs_capacity += ARG_TOKENS_EXTENT;
+	      arg->virt_locs = XRESIZEVEC (source_location,
+					   arg->virt_locs,
+					   arg->virt_locs_capacity);
+	    }
 
-	  token = cpp_get_token (pfile);
+	  token = cpp_get_token_1 (pfile, &virt_loc);
 
 	  if (token->type == CPP_PADDING)
 	    {
@@ -686,7 +834,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);
 
@@ -700,8 +848,9 @@ collect_args (cpp_reader *pfile, const cpp_hashnode *node,
 	      else
 		continue;
 	    }
-
-	  arg->first[ntokens++] = token;
+	  set_arg_token (pfile, arg, token, virt_loc,
+			 ntokens, MACRO_ARG_TOKEN_NORMAL);
+	  ntokens++;
 	}
 
       /* Drop trailing padding.  */
@@ -709,7 +858,8 @@ collect_args (cpp_reader *pfile, const cpp_hashnode *node,
 	ntokens--;
 
       arg->count = ntokens;
-      arg->first[ntokens] = &pfile->eof;
+      set_arg_token (pfile, arg, &pfile->eof, pfile->eof.src_loc,
+		     ntokens, MACRO_ARG_TOKEN_NORMAL);
 
       /* Terminate the argument.  Excess arguments loop back and
 	 overwrite the final legitimate argument, before failing.  */
@@ -752,6 +902,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;
 	}
     }
@@ -765,10 +917,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;
 
@@ -785,7 +939,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
@@ -819,13 +973,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;
@@ -850,11 +1006,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--;
@@ -873,8 +1031,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.  */
@@ -888,13 +1051,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_append_token (pfile, 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)
 	{
@@ -922,33 +1116,311 @@ 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 (cpp_reader *pfile, macro_arg *arg, const cpp_token *token,
+	       source_location location, size_t index,
+	       enum macro_arg_token_kind kind)
+{  
+  const cpp_token **token_ptr;
+  source_location *loc = NULL;
+  bool track_macro_exp_p = CPP_OPTION (pfile, track_macro_expansion);
+
+  token_ptr =
+    arg_token_ptr_at (pfile, 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.  */
+static const source_location *
+get_arg_token_location (cpp_reader *pfile,
+			const macro_arg *arg,
+			enum macro_arg_token_kind kind)
+{
+  source_location *loc = NULL;
+  const cpp_token **token_ptr = arg_token_ptr_at (pfile, arg, 0,
+						  kind, &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 (cpp_reader *pfile, const macro_arg *arg,
+		  size_t index, enum macro_arg_token_kind kind,
+		  source_location **virt_location)
+{
+  bool track_macro_exp_p = CPP_OPTION (pfile, track_macro_expansion);
+  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)
+    return NULL;
+
+  if (virt_location)
+    {
+      if (track_macro_exp_p)
+	{
+	  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;
+	}
+      else
+	*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,
+			   cpp_reader *pfile,
+			   enum macro_arg_token_kind kind,
+			   const macro_arg *arg,
+			   const cpp_token **token_ptr)
+{
+  iter->pfile = pfile;
+  iter->kind = kind;
+  iter->arg = arg;
+  iter->token_ptr = token_ptr;
+  iter->location_ptr = get_arg_token_location (pfile, arg, kind);
+#ifdef ENABLE_CHECKING
+  iter->num_forwards = 0;
+#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)
+{
+  bool track_macro_exp_p = CPP_OPTION (it->pfile,
+				       track_macro_expansion);
+
+  switch (it->kind)
+    {
+    case MACRO_ARG_TOKEN_NORMAL:
+    case MACRO_ARG_TOKEN_EXPANDED:
+      it->token_ptr++;
+      if (track_macro_exp_p)
+	it->location_ptr++;
+      break;
+    case MACRO_ARG_TOKEN_STRINGIFIED:
+#ifdef ENABLE_CHECKING
+      if (it->num_forwards > 0)
+	abort ();
+      it->num_forwards++;
+#endif
+      break;
+    }
+}
+
+/* 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
+  return *it->location_ptr;
+}
+
+/* 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.  */
@@ -970,67 +1442,222 @@ 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, &virt_locs);
+
   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_append_token (pfile, 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, pfile,
+				     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, pfile,
+				     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, pfile,
+				     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, pfile,
+				     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_append_token (pfile, 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_append_token (pfile, 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)
@@ -1046,7 +1673,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_append_token (pfile, buff, virt_locs,
+				    t, t->src_loc, t->src_loc,
+				    NULL, NULL);
+	}
 
       /* Add a new paste flag, or remove an unwanted one.  */
       if (paste_flag)
@@ -1060,13 +1692,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.  */
@@ -1094,6 +1729,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;
@@ -1110,7 +1746,7 @@ push_ptoken_context (cpp_reader *pfile, cpp_hashnode *macro, _cpp_buff *buff,
 {
   cpp_context *context = next_context (pfile);
 
-  context->direct_p = false;
+  context->tokens_kind = TOKENS_KIND_INDIRECT;
   context->macro = macro;
   context->buff = buff;
   FIRST (context).ptoken = first;
@@ -1122,15 +1758,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->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->macro = 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,
@@ -1138,7 +1803,7 @@ _cpp_push_text_context (cpp_reader *pfile, cpp_hashnode *macro,
 {
   cpp_context *context = next_context (pfile);
 
-  context->direct_p = true;
+  context->tokens_kind = TOKENS_KIND_DIRECT;
   context->macro = macro;
   context->buff = NULL;
   CUR (context) = start;
@@ -1146,6 +1811,187 @@ _cpp_push_text_context (cpp_reader *pfile, cpp_hashnode *macro,
   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 the
+   -ftrack-macro-expansion flag is in effect and if VIRT_LOCS is
+   non-null, *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)
+{
+  bool track_macro_exp_p = CPP_OPTION (pfile, track_macro_expansion);
+  size_t tokens_size = len * sizeof (cpp_token *);
+  size_t locs_size = len * sizeof (source_location);
+
+  if (track_macro_exp_p && 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 points to where to
+   insert the virtual location of TOKEN; that is, if the flag
+   -ftrack-macro-expansion is in effect.  TOKEN is the token to
+   insert.  DEF_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 PARM_DEF_LOC can
+   just be set to the same value as 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 (cpp_reader *pfile,
+			  const cpp_token **dest,
+			  source_location *virt_loc_dest,
+			  const cpp_token *token,
+			  source_location def_loc,
+			  source_location parm_def_loc,			  
+			  const struct line_map *map,
+			  unsigned int *macro_token_index)
+{
+  bool track_macro_exp_p = CPP_OPTION (pfile, track_macro_expansion);
+  source_location macro_loc = def_loc;
+  const cpp_token **result;
+
+  if (track_macro_exp_p)
+    {
+      if (map)
+	macro_loc = linemap_add_macro_token (map, *macro_token_index,
+					     def_loc, parm_def_loc);
+      *virt_loc_dest = macro_loc;
+    }
+  *dest = token;
+  result = &dest[1];
+
+  return result;
+}
+
+/* Appends a token to the end of the token buffer BUFFER.  Note that
+   this function doesn't enlarge BUFFER; it overwrite the last memory
+   location of BUFFER that holds a token.
+
+   TOKEN is the token to append. DEF_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 PARM_DEF_LOC can just be set to the
+   same value as 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.  This function adds
+   the virtual location DEF_LOC it 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_append_token (cpp_reader *pfile,
+			  _cpp_buff *buffer,
+			  source_location *virt_locs,
+			  const cpp_token *token,
+			  source_location def_loc,
+			  source_location parm_def_loc,
+			  const struct line_map *map,
+			  unsigned int *macro_token_index)
+{
+  const cpp_token **result;
+  unsigned token_index = 
+    (BUFF_FRONT (buffer) - buffer->base) / sizeof (cpp_token *);
+
+  result =
+    tokens_buff_put_token_to (pfile, (const cpp_token **) BUFF_FRONT (buffer),
+			      &virt_locs[token_index],
+			      token, def_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_args_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);
+  arg->expanded_capacity = 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_args_room (cpp_reader *pfile, macro_arg *arg, size_t size)
+{
+  if (size <= arg->expanded_capacity)
+    return;
+
+  size *= 2;
+
+  arg->expanded =
+    XRESIZEVEC (const cpp_token *, arg->expanded, size);
+  arg->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
@@ -1157,8 +2003,10 @@ expand_arg (cpp_reader *pfile, macro_arg *arg)
 {
   unsigned int 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.  */
@@ -1167,26 +2015,32 @@ expand_arg (cpp_reader *pfile, macro_arg *arg)
 
   /* Loop, reading in the arguments.  */
   capacity = 256;
-  arg->expanded = XNEWVEC (const cpp_token *, capacity);
+  alloc_expanded_args_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_args_room (pfile, arg, arg->expanded_count + 1);
 
-      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 (pfile, arg, token, location,
+		     arg->expanded_count, MACRO_ARG_TOKEN_EXPANDED);
+      arg->expanded_count++;
     }
 
   _cpp_pop_context (pfile);
@@ -1195,25 +2049,127 @@ 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;
+    {
+      cpp_hashnode *macro;
+      if (context->tokens_kind == TOKENS_KIND_EXTENDED)
+	{
+	  macro_context *mc = (macro_context *) context->macro;
+	  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->macro = NULL;
+	}
+      else
+	macro = (cpp_hashnode *) context->macro;
+
+      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 = (macro_context *) c->macro;
+      *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 traditionnal 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 traditionnal 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
@@ -1221,12 +2177,45 @@ _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".  This matters when a token results from macro
+   expansion -- the token's location will indicate where the macro is
+   defined (the spelling location of the token) but *LOC will be a
+   virtual location of the token. Virtual location means a location
+   that possibly encodes many types of locus at once. A virtual
+   location can encode the location of a token resulting from macro
+   expansion or not. If the token results from macro expansion its
+   virtual location encodes (at the same time):
+     - the spelling location of the token
+     - the locus of the macro expansion point
+     - the locus the point where the token got instantiated as part of
+       the macro expansion process.
+     (YES, IT ENCODES ALL THESE THREE AT THE SAME TIME! and maybe more.)
+
+   You can learn more about the different locuses encoded in a map by
+   reading the extensive comments of the line_map_macro and line_map
+   structs in line-map.h.  A virtual location, indeed.
+
+   The linemap API can then be used to retrieve the particular locus
+   we are interested in.
+
+   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 operators
+   '<' and '>'.
+
+   Otherwise *LOC is set to the same location as the location carried
+   by the returned token.  */
+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 (;;)
@@ -1236,20 +2225,23 @@ 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);
+	      if (location)
+		*location = result->src_loc;
+	      return result;
 	    }
 	}
       else
@@ -1257,6 +2249,8 @@ cpp_get_token (cpp_reader *pfile)
 	  _cpp_pop_context (pfile);
 	  if (pfile->state.in_directive)
 	    continue;
+	  if (location)
+	    *location = pfile->avoid_paste.src_loc;
 	  return &pfile->avoid_paste;
 	}
 
@@ -1294,7 +2288,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
@@ -1311,12 +2306,16 @@ 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);
+	      if (location)
+		*location = result->src_loc;
+	      return result;
 	    }
 	}
       else
@@ -1333,27 +2332,79 @@ cpp_get_token (cpp_reader *pfile)
       break;
     }
 
-  return result;
+  if (location)
+    *location = virt_loc;
+  return result;  
+}
+
+/* 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 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.  */
+/* 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 enbled its virtual location encodes (at the
+   same time):
+
+   - the spelling location of the token the locus of the macro
+   - expansion point the locus 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);
+  result = cpp_get_token_1 (pfile, loc);
   if (pfile->context->macro)
-    *loc = pfile->invocation_location;
+    {
+      if (!CPP_OPTION (pfile, track_macro_expansion))
+	*loc = pfile->invocation_location;
+    }
   else
     *loc = result->src_loc;
 
+  *loc = maybe_adjust_loc_for_trad_cpp (pfile, *loc);
   return result;
 }
 
@@ -1363,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 = (cpp_hashnode *) pfile->context->macro;
 
   return node && node->value.macro && node->value.macro->syshdr;
 }
@@ -1420,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->macro)
+	    {
+	      macro_context *m = (macro_context *) pfile->context->macro;
+	      m->cur_virt_loc--;
+#ifdef ENABLE_CHECKING
+	      if (m->cur_virt_loc < m->virt_locs)
+		abort ();
+#endif
+	    }
+	  else
+	    abort ();
+	}
+      else
+	abort ();
     }
 }
 
-- 
		Dodji

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

* Re: [PATCH 2/7] Generate virtual locations for tokens
  2011-09-14 10:01           ` Dodji Seketeli
@ 2011-09-14 22:56             ` Jason Merrill
  2011-09-18 13:44               ` Dodji Seketeli
  0 siblings, 1 reply; 135+ messages in thread
From: Jason Merrill @ 2011-09-14 22:56 UTC (permalink / raw)
  To: Dodji Seketeli; +Cc: gcc-patches, tromey, gdr, joseph, burnus, charlet, bonzini

On 09/14/2011 04:50 AM, Dodji Seketeli wrote:
> To comply with this invariant, the initial patch of Tom was cloning T
> into T', and was using T'->src_loc to track the virtual location of T.
> Which is close to what you are proposing, while respecting the
> invariant.

Yes, that's what I had in mind.  I didn't realize that there was only 
one token for FOO.

> But it turned out that was using too much memory :-(.  So
> we devised this scheme instead.

Ah. :(

> +  void *macro;

This should be a union rather than an untyped pointer.

> +      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++;
> +         if (context->macro)

The other places that deal with TOKENS_KIND_EXTENDED don't test that 
context->macro is non-null.  Why is it needed here?

> +    {
> +      cpp_hashnode *macro;
> +      if (context->tokens_kind == TOKENS_KIND_EXTENDED)
> +       {
> +         macro_context *mc = (macro_context *) context->macro;
> +         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->macro = NULL;
> +       }
> +      else
> +       macro = (cpp_hashnode *) context->macro;
> +
> +      if (macro != NULL)
> +       macro->flags &= ~NODE_DISABLED;

How can macro end up NULL if context->macro was set?

> +/* In the traditionnal mode of the preprocessor, if we are currently
> +   location if we are in the traditionnal mode, and just returns

"traditional"

I don't think we need to talk about virtual locations before 
cpp_get_token_1; it's not an external interface, and it's redundant with 
the description before cpp_get_token_with_location.

> +  result = cpp_get_token_1 (pfile, loc);
>    if (pfile->context->macro)
> -    *loc = pfile->invocation_location;
> +    {
> +      if (!CPP_OPTION (pfile, track_macro_expansion))
> +       *loc = pfile->invocation_location;
> +    }
>    else
>      *loc = result->src_loc;
>
> +  *loc = maybe_adjust_loc_for_trad_cpp (pfile, *loc);

Let's move this code into cpp_get_token_1 so that all the location 
tweaking is in one place.

> +  switch (it->kind)
> +    {
> +    case MACRO_ARG_TOKEN_NORMAL:
> +    case MACRO_ARG_TOKEN_EXPANDED:
> +      it->token_ptr++;
> +      if (track_macro_exp_p)
> +       it->location_ptr++;
> +      break;
> +    case MACRO_ARG_TOKEN_STRINGIFIED:
> +#ifdef ENABLE_CHECKING
> +      if (it->num_forwards > 0)
> +       abort ();
> +      it->num_forwards++;
> +#endif
> +      break;
> +    }

Don't you want to increment num_forwards in the normal/expanded cases, too?

> +tokens_buff_append_token (cpp_reader *pfile,
> +                         _cpp_buff *buffer,
> +                         source_location *virt_locs,
> +                         const cpp_token *token,
> +                         source_location def_loc,
> +                         source_location parm_def_loc,
> +                         const struct line_map *map,
> +                         unsigned int *macro_token_index)

Why is macro_token_index a pointer?  Nothing seems to modify the referent.

> +/* Appends a token to the end of the token buffer BUFFER.  Note that
> +   this function doesn't enlarge BUFFER; it overwrite the last memory
> +   location of BUFFER that holds a token.

That doesn't sound like appending.

Jason

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

* Re: [PATCH 3/7] Emit macro expansion related diagnostics
  2011-09-12 21:54         ` Jason Merrill
@ 2011-09-16  8:19           ` Dodji Seketeli
  2011-09-17 21:27             ` Jason Merrill
  0 siblings, 1 reply; 135+ messages in thread
From: Dodji Seketeli @ 2011-09-16  8:19 UTC (permalink / raw)
  To: Jason Merrill; +Cc: gcc-patches, tromey, gdr, joseph, burnus, charlet, bonzini

> On 08/04/2011 11:32 AM, Dodji Seketeli wrote:
> > +++ b/gcc/diagnostic.c
> > @@ -30,6 +30,7 @@ along with GCC; see the file COPYING3.  If not see
> >  #include "input.h"
> >  #include "intl.h"
> >  #include "diagnostic.h"
> > +#include "vec.h"
> 
> Do you still need this?

Oops, no.  Removed and adjusted gcc/Makefile.in accordingly.

> 
> >  // Just discard errors pointing at header files
> >  // { dg-prune-output "include" }
> > +// { dg-prune-output "        from" }
> 
> These should be pruned by testsuite/lib/prune.exp.  I'm surprised they
> aren't already.

OK.  I have added that pruning to prune.exp and removed it from the
relevant test case files.

> 
> > +#define APPEND_LOC_TO_VEC(LOC)                                         \
> > +  if (num_locs >=3D loc_vec_capacity)                                  \
> > +    {                                                                  \
> > +      loc_vec_capacity +=3D 4;                                         \
> > +      loc_vec =3D XRESIZEVEC (loc_t, loc_vec, loc_vec_capacity);
> > \
> > +    }                                                                  \
> > +  loc_vec[num_locs++] =3D LOC;
> 
> Why not use VEC since we're in gcc/ here?

This is another leftover of when this code wasn't in gcc/.  I am using
VEC now in the amended patch.

> 
> > +/* Unwind the different macro expansions that lead to the token which
> > +   location is WHERE and emit diagnostics showing the resulting
> > +   unwound macro expansion stack.  If TOPMOST_EXP_POINT_MAP is
> > +   non-null, *TOPMOST_EXP_POINT_MAP is set to the map of the expansion
> > +   point of the top most macro of the stack.  This must be an ordinary
> > +   map.  */
> 
> I find the use of "top" here confusing.  You mean the place in the
> source that first triggered the macro expansion, right?

Yes.

>  Can we avoid talking about stacks here?

OK.  Sorry for the confusion.  I have removed the stack analogy added
hopefully more accurate comments.

[...]

> > +  while (unwind)
> > +    {
> ...
> > +      if (!linemap_macro_expansion_map_p (map))
> > +       unwind =3D false;
> > +    }
> 
> This seems like a job for do/while.

Updated accordingly.

I have fixed some other nits in the patch, bootstrapped and tested it on
x86_64-unknown-linux-gnu against a tree based on trunk and containing
the previous patches of the set.

Thanks.

From: Dodji Seketeli <dodji@redhat.com>
Date: Sat, 4 Dec 2010 16:31:35 +0100
Subject: [PATCH 3/7] Emit macro expansion related diagnostics

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

The combination of this patch and the previous ones boostrapped with
--enable-languages=all,ada and passed regression tests on
x86_64-unknown-linux-gnu.

gcc/
	* 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/

	* 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/

	* 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/Makefile.in                                 |    3 +-
 gcc/cp/error.c                                  |    5 +-
 gcc/diagnostic.c                                |   13 +-
 gcc/diagnostic.h                                |    2 +-
 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                           |  208 ++++++++++++++++++++++-
 gcc/tree-diagnostic.h                           |    3 +-
 13 files changed, 331 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 92016f2..d26c682 100644
--- a/gcc/Makefile.in
+++ b/gcc/Makefile.in
@@ -2796,7 +2796,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 598ddf1..8fa163f 100644
--- a/gcc/cp/error.c
+++ b/gcc/cp/error.c
@@ -2767,7 +2767,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);
@@ -2777,8 +2777,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..0344937 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_PARM_REPLACEMENT_POINT,
+			    &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 de0a58a..5f63b69 100644
--- a/gcc/toplev.c
+++ b/gcc/toplev.c
@@ -1132,6 +1132,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..ff7a749 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,208 @@ 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 sixth 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, *resolved_map;
+  source_location resolved_location;
+  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
+         *definition* of the macro of MAP, that got expanded at WHERE.
+         This is basically how we go "down" in the trace of macro
+         expansions that led to WHERE.  */
+      resolved_location =
+        linemap_macro_map_loc_to_def_point (map, where, false);
+      resolved_map = linemap_lookup (line_table, resolved_location);
+
+      /* If the token at RESOLVED_LOCATION [at macro definition point]
+         is itself inside an expanded macro then we keep unwinding the
+         trace by reaching the "parent macro" that got expanded inside
+         the definition of the macro of MAP...  */
+      if (linemap_macro_expansion_map_p (resolved_map))
+        {
+          where = resolved_location;
+          map = resolved_map;
+        }
+      else
+        {
+          /* Otherwise, let's consider the location of the expansion
+             point of the macro of MAP.  Keep in mind that MAP is a
+             macro expansion map.  To get a "normal map" (i.e a non
+             macro expansion map) and be done with the unwinding, we
+             must either consider the location of the expansion point
+             of the macro or the location of the token inside the
+             macro definition that got expanded to WHERE.  */
+          where =
+            linemap_macro_map_loc_to_exp_point (map, where);
+          map = linemap_lookup (line_table, where);
+        }
+    } 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 (resolved_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_PARM_REPLACEMENT_POINT, 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_PARM_REPLACEMENT_POINT, 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 */
-- 
		Dodji

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

* Re: [PATCH 4/7] Support -fdebug-cpp option
  2011-09-12 22:07       ` Jason Merrill
@ 2011-09-16  8:23         ` Dodji Seketeli
  2011-09-17 22:01           ` Jason Merrill
  0 siblings, 1 reply; 135+ messages in thread
From: Dodji Seketeli @ 2011-09-16  8:23 UTC (permalink / raw)
  To: Jason Merrill; +Cc: gcc-patches, tromey, gdr, joseph, burnus, charlet, bonzini

Jason Merrill <jason@redhat.com> writes:

> On 08/24/2011 10:06 AM, Tom Tromey wrote:
>> Dodji> Would it be acceptable to just change the output of -fdirective to fit?
>> Dodji> Or are we bound to not breaking existing consumers?
>>
>> I think changing it would be fine.
>
> I agree.

I have added that to my TODO list.

>
> On 07/16/2011 10:37 AM, Dodji Seketeli wrote:
>>  }
>> +
>> +void
>> +linemap_dump_location (struct line_maps *set,
>
> Comment.

Oops, added.

>> +@item -fdebug-cpp
>> +@opindex fdebug-cpp
>
> Please add something to clarify that this is only useful for debugging
> GCC.

Done.

Below is the updated patch, thanks.

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.

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

libcpp/

	* 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/

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

gcc/c-family/

	* 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.
---
 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/include/cpplib.h   |    4 +++
 libcpp/include/line-map.h |    4 +++
 libcpp/line-map.c         |   38 ++++++++++++++++++++++++++++++
 8 files changed, 114 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 b4bc9ce..cb010c5 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 7e1b7c2..fedcf84 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 5b7ee9d..81fe6c9 100644
--- a/libcpp/include/line-map.h
+++ b/libcpp/include/line-map.h
@@ -800,4 +800,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 959566c..fbd88da 100644
--- a/libcpp/line-map.c
+++ b/libcpp/line-map.c
@@ -955,3 +955,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_macro_loc_to_def_point (set, loc, &map, true);
+  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);
+}
-- 
		Dodji

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

* Re: [PATCH 5/7] Add line map statistics to -fmem-report output
  2011-09-12 22:07       ` Jason Merrill
@ 2011-09-16  8:29         ` Dodji Seketeli
  2011-09-17 22:05           ` Jason Merrill
  0 siblings, 1 reply; 135+ messages in thread
From: Dodji Seketeli @ 2011-09-16  8:29 UTC (permalink / raw)
  To: Jason Merrill; +Cc: gcc-patches, tromey, gdr, joseph, burnus, charlet, bonzini

> On 07/16/2011 10:37 AM, Dodji Seketeli wrote:
> > +#define ONE_M ONE_K * ONE_K
> 
> Parenthesize this so that users don't need to.

OK.

> 
> > +  macro_maps_used_size =
> > +    LINEMAPS_MACRO_USED (set) * sizeof (struct line_map)
> > +    + macro_maps_locations_size;
> 
> It seems odd to add in the locations size here since it's also printed
> separately.

I wanted macro_maps_used_size to really reflect the total used size
for macro maps, without having to mentally do the addition of its two
components.  But at the same time, I was interested in seeing how much
memory were the locations taking inside the macro map memory.  As I
was suspecting them to take a lot of memory.  It turned out I could
gain much more by optimizing things elsewhere.

> 
> > +  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));
> 
> This seems oddly sorted.

I am not sure what you mean exactly, but in the patch below I tried to
actually sort them this time, as opposed to just adding things as I
needed them, in no particular order.  :-) Please tell me if you prefer
any particular order.

> And why the difference between ordinary and macro maps in terms of
> what is printed?

It's related to the difference of memory layout of ordinary and macro
maps.  I wanted to understand how each component of a macro map
impacts the overall size of taken by the macro maps, and where/how I
could gain by working on the macro map encoding.

For macro maps, the memory is allocated in two parts.  First the array
line_maps::info_macro::maps, and then, for each map, there is memory
allocated for the locations it holds.  Then, because of the way the
mapping encoding is done, there can be times where the two locations
of an entry of a macro map are the same.  This space wasted by this
redundancy is what I tried to quantify with
duplicated_macro_maps_locations_size.  I wanted to quantify the memory
consumed by each of these components to see how much memory I could
save by changing the way the macro maps were organized.  E.g, at one
iteration, I realized that it really was the number of macro maps that
was hurting, independently of how each map were encoded.  So we could
work on reducing that.  Then we realized that we were allocating too
much memory, just for line_maps::info_macro::maps alone.  Hence the
memory allocator patch, etc...

The memory of ordinary macros on the other hand is allocated in a much
simpler linear way.  Just the size of line_maps::info_ordinary::maps
tells the story.

> 
> > +/* Counters defined in libcpp's macro.c.  */
> > +extern unsigned num_expanded_macros_counter;
> > +extern unsigned num_macro_tokens_counter;
> 
> These should be part of struct linemap_stats.

Done, and updated input.c accordingly.

Thanks.

Bootstrapped and tested it on x86_64-unknown-linux-gnu against a tree
based on trunk and containing the previous patches of the set.

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.

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

gcc/
	* 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/
	* 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/input.c               |   79 +++++++++++++++++++++++++++++++++++++++++++++
 gcc/input.h               |    2 +
 gcc/toplev.c              |    1 +
 libcpp/include/line-map.h |   20 +++++++++++
 libcpp/line-map.c         |   69 +++++++++++++++++++++++++++++++++++++++
 libcpp/macro.c            |   29 ++++++++++++++--
 6 files changed, 196 insertions(+), 4 deletions(-)

diff --git a/gcc/input.c b/gcc/input.c
index 83344d7..bdd1434 100644
--- a/gcc/input.c
+++ b/gcc/input.c
@@ -46,3 +46,82 @@ 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;
+
+  memset (&s, 0, sizeof (s));
+
+  linemap_get_statistics (line_table, &s);
+
+  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, "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 (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, "\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 5f63b69..2f90261 100644
--- a/gcc/toplev.c
+++ b/gcc/toplev.c
@@ -1788,6 +1788,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 81fe6c9..3989c89 100644
--- a/libcpp/include/line-map.h
+++ b/libcpp/include/line-map.h
@@ -800,6 +800,26 @@ expanded_location linemap_expand_location_full (struct line_maps *,
 						source_location loc,
 						enum location_resolution_kind lrk);
 
+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_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;
+};
+
+/* 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 fbd88da..09dffcb 100644
--- a/libcpp/line-map.c
+++ b/libcpp/line-map.c
@@ -30,6 +30,11 @@ 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);
+
+/* Counters defined in macro.c.  */
+extern unsigned num_expanded_macros_counter;
+extern unsigned num_macro_tokens_counter;
+
 /* Initialize a line map set.  */
 
 void
@@ -993,3 +998,67 @@ 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);
 }
+
+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_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_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;
+}
diff --git a/libcpp/macro.c b/libcpp/macro.c
index 0544eb1..1d35018 100644
--- a/libcpp/macro.c
+++ b/libcpp/macro.c
@@ -173,6 +173,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
@@ -1084,10 +1091,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)
@@ -1097,13 +1109,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;
@@ -1702,6 +1719,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.  */
@@ -2246,6 +2265,8 @@ cpp_get_token_1 (cpp_reader *pfile, source_location *location)
 	}
       else
 	{
+	  if (pfile->context->macro)
+	    ++num_expanded_macros_counter;
 	  _cpp_pop_context (pfile);
 	  if (pfile->state.in_directive)
 	    continue;
-- 
		Dodji

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

* Re: [PATCH 6/7] Kill pedantic warnings on system headers macros
  2011-09-12 22:09       ` Jason Merrill
@ 2011-09-16 11:25         ` Dodji Seketeli
  2011-09-17 22:34           ` Jason Merrill
  0 siblings, 1 reply; 135+ messages in thread
From: Dodji Seketeli @ 2011-09-16 11:25 UTC (permalink / raw)
  To: Jason Merrill; +Cc: gcc-patches, tromey, gdr, joseph, burnus, charlet, bonzini

Jason Merrill <jason@redhat.com> writes:

> On 07/16/2011 10:37 AM, Dodji Seketeli wrote:
> > +  location_t here = c_parser_peek_token (parser)->location;
> 
> Perhaps "first_token_loc"?

OK, changed.

> 
> > +           SYNTAX_ERROR2_AT (prev_virtual_location,
> > +                             "missing binary operator before token \"%s\"",
> > +                             cpp_token_as_text (pfile, op.token));
> 
> It seems to me that the "missing X before" errors should point to the
> current token, not the previous one.  So you can drop
> prev_virtual_location.

OK, dropped.

I have bootstrapped and tested it on x86_64-unknown-linux-gnu against a
tree based on trunk and containing the previous patches of the set.

This patch leverages the virtual location infrastructure to avoid
emitting pedantic warnings related to macros defined in system headers
but expanded in normal TUs.

The point is to make diagnostic routines use virtual locations of
tokens instead of their spelling locations.  The diagnostic routines
in turn indirectly use linemap_location_in_system_header_p to know if
a given virtual location originated from a system header.

The patch has two main parts.

The libcpp part makes diagnostic routines called from the preprocessor
expression parsing and number conversion code use virtual locations.

The C FE part makes diagnostic routines called from the type
specifiers validation code use virtual locations.

This fixes the relevant examples presented in the comments of the bug
but I guess, as usual, libcpp and the FEs will need on-going care to
use more and more virtual locations of tokens instead of spelling
locations.

The combination of the patch and the previous ones boostrapped with
--enable-languages=all,ada and passed regression tests on
x86_64-unknown-linux-gnu.

libcpp/

	* include/cpplib.h (cpp_classify_number): Add a location parameter
	to the declaration.
	* expr.c (SYNTAX_ERROR_AT, SYNTAX_ERROR2_AT): New macros to emit
	syntax error using a virtual location.
	(cpp_classify_number): Add a virtual location parameter.  Use
	SYNTAX_ERROR_AT instead of SYNTAX_ERROR, cpp_error_with_line
	instead of cpp_error and cpp_warning_with_line instead of
	cpp_warning.  Pass the new virtual location parameter to those
	diagnostic routines.
	(eval_token): Add a virtual location parameter.  Pass it down to
	cpp_classify_number.  Use cpp_error_with_line instead of
	cpp_error, cpp_warning_with_line instead of cpp_warning, and pass
	the new virtual location parameter to these.
	(_cpp_parse_expr): Use cpp_get_token_with_location instead of
	cpp_get_token, to get the virtual location of the token. Use
	SYNTAX_ERROR2_AT instead of SYNTAX_ERROR2, cpp_error_with_line
	instead of cpp_error. Use the virtual location instead of the
	spelling location.
	* macro.c (maybe_adjust_loc_for_trad_cpp): Define new static
	function.
	(cpp_get_token_with_location): Use it.

gcc/c-family

	* c-lex.c (c_lex_with_flags): Adjust to pass the virtual location
	to cpp_classify_number.

gcc/

	* c-tree.h (finish_declspecs): Add a virtual location parameter.
	* c-decl.c (finish_declspecs): Add a virtual location parameter.
	Use error_at instead of error and pass down the virtual location
	to pewarn and error_at.
	(declspecs_add_type): Use in_system_header_at instead of
	in_system_header.
	* c-parser.c (c_parser_declaration_or_fndef): Pass virtual
	location of the relevant token to finish_declspecs.
	(c_parser_struct_declaration, c_parser_parameter_declaration):
	Likewise.
	(c_parser_type_name): Likewise.

gcc/testsuite/

	* gcc.dg/cpp/syshdr3.h: New test header.
	* gcc.dg/cpp/syshdr3.c: New test file.
	* gcc.dg/nofixed-point-2.c: Adjust to more precise location.
---
 gcc/c-decl.c                           |   17 ++--
 gcc/c-family/c-lex.c                   |    4 +-
 gcc/c-parser.c                         |   12 ++-
 gcc/c-tree.h                           |    2 +-
 gcc/testsuite/gcc.dg/cpp/syshdr3.c     |   16 +++
 gcc/testsuite/gcc.dg/cpp/syshdr3.h     |    7 ++
 gcc/testsuite/gcc.dg/nofixed-point-2.c |    6 +-
 libcpp/expr.c                          |  173 +++++++++++++++++++-------------
 libcpp/include/cpplib.h                |    3 +-
 9 files changed, 149 insertions(+), 91 deletions(-)
 create mode 100644 gcc/testsuite/gcc.dg/cpp/syshdr3.c
 create mode 100644 gcc/testsuite/gcc.dg/cpp/syshdr3.h

diff --git a/gcc/c-decl.c b/gcc/c-decl.c
index 5d4564a..f139abc 100644
--- a/gcc/c-decl.c
+++ b/gcc/c-decl.c
@@ -8983,7 +8983,7 @@ declspecs_add_type (location_t loc, struct c_declspecs *specs,
 	      break;
 	    case RID_COMPLEX:
 	      dupe = specs->complex_p;
-	      if (!flag_isoc99 && !in_system_header)
+	      if (!flag_isoc99 && !in_system_header_at (loc))
 		pedwarn (loc, OPT_pedantic,
 			 "ISO C90 does not support complex types");
 	      if (specs->typespec_word == cts_void)
@@ -9511,7 +9511,8 @@ declspecs_add_attrs (struct c_declspecs *specs, tree attrs)
    double".  */
 
 struct c_declspecs *
-finish_declspecs (struct c_declspecs *specs)
+finish_declspecs (struct c_declspecs *specs,
+		  location_t where)
 {
   /* If a type was specified as a whole, we have no modifiers and are
      done.  */
@@ -9536,9 +9537,9 @@ finish_declspecs (struct c_declspecs *specs)
     {
       if (specs->saturating_p)
 	{
-	  error ("%<_Sat%> is used without %<_Fract%> or %<_Accum%>");
+	  error_at (where, "%<_Sat%> is used without %<_Fract%> or %<_Accum%>");
 	  if (!targetm.fixed_point_supported_p ())
-	    error ("fixed-point types not supported for this target");
+	    error_at (where, "fixed-point types not supported for this target");
 	  specs->typespec_word = cts_fract;
 	}
       else if (specs->long_p || specs->short_p
@@ -9549,7 +9550,7 @@ finish_declspecs (struct c_declspecs *specs)
       else if (specs->complex_p)
 	{
 	  specs->typespec_word = cts_double;
-	  pedwarn (input_location, OPT_pedantic,
+	  pedwarn (where, OPT_pedantic,
 		   "ISO C does not support plain %<complex%> meaning "
 		   "%<double complex%>");
 	}
@@ -9594,7 +9595,7 @@ finish_declspecs (struct c_declspecs *specs)
 	specs->type = char_type_node;
       if (specs->complex_p)
 	{
-	  pedwarn (input_location, OPT_pedantic,
+	  pedwarn (where, OPT_pedantic,
 		   "ISO C does not support complex integer types");
 	  specs->type = build_complex_type (specs->type);
 	}
@@ -9607,7 +9608,7 @@ finish_declspecs (struct c_declspecs *specs)
 		     : int128_integer_type_node);
       if (specs->complex_p)
 	{
-	  pedwarn (input_location, OPT_pedantic,
+	  pedwarn (where, OPT_pedantic,
 		   "ISO C does not support complex integer types");
 	  specs->type = build_complex_type (specs->type);
 	}
@@ -9633,7 +9634,7 @@ finish_declspecs (struct c_declspecs *specs)
 		       : integer_type_node);
       if (specs->complex_p)
 	{
-	  pedwarn (input_location, OPT_pedantic,
+	  pedwarn (where, OPT_pedantic,
 		   "ISO C does not support complex integer types");
 	  specs->type = build_complex_type (specs->type);
 	}
diff --git a/gcc/c-family/c-lex.c b/gcc/c-family/c-lex.c
index be83b61..ca088a8 100644
--- a/gcc/c-family/c-lex.c
+++ b/gcc/c-family/c-lex.c
@@ -314,7 +314,7 @@ c_lex_with_flags (tree *value, location_t *loc, unsigned char *cpp_flags,
 
     case CPP_NUMBER:
       {
-	unsigned int flags = cpp_classify_number (parse_in, tok);
+	unsigned int flags = cpp_classify_number (parse_in, tok, *loc);
 
 	switch (flags & CPP_N_CATEGORY)
 	  {
@@ -397,7 +397,7 @@ c_lex_with_flags (tree *value, location_t *loc, unsigned char *cpp_flags,
 
 	*cpp_spell_token (parse_in, tok, name, true) = 0;
 
-	error ("stray %qs in program", name);
+	error_at (*loc, "stray %qs in program", name);
       }
 
       goto retry;
diff --git a/gcc/c-parser.c b/gcc/c-parser.c
index ff376df..3544d87 100644
--- a/gcc/c-parser.c
+++ b/gcc/c-parser.c
@@ -1479,7 +1479,7 @@ c_parser_declaration_or_fndef (c_parser *parser, bool fndef_ok,
       c_parser_skip_to_end_of_block_or_statement (parser);
       return;
     }
-  finish_declspecs (specs);
+  finish_declspecs (specs, here);
   if (c_parser_next_token_is (parser, CPP_SEMICOLON))
     {
       if (empty_ok)
@@ -2579,7 +2579,7 @@ c_parser_struct_declaration (c_parser *parser)
       c_parser_error (parser, "expected specifier-qualifier-list");
       return NULL_TREE;
     }
-  finish_declspecs (specs);
+  finish_declspecs (specs, decl_loc);
   if (c_parser_next_token_is (parser, CPP_SEMICOLON)
       || c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
     {
@@ -3242,6 +3242,8 @@ c_parser_parameter_declaration (c_parser *parser, tree attrs)
   tree prefix_attrs;
   tree postfix_attrs = NULL_TREE;
   bool dummy = false;
+  location_t first_token_loc = c_parser_peek_token (parser)->location;
+
   if (!c_parser_next_token_starts_declspecs (parser))
     {
       c_token *token = c_parser_peek_token (parser);
@@ -3268,7 +3270,7 @@ c_parser_parameter_declaration (c_parser *parser, tree attrs)
       attrs = NULL_TREE;
     }
   c_parser_declspecs (parser, specs, true, true, true, cla_nonabstract_decl);
-  finish_declspecs (specs);
+  finish_declspecs (specs, first_token_loc);
   pending_xref_error ();
   prefix_attrs = specs->attrs;
   specs->attrs = NULL_TREE;
@@ -3561,6 +3563,8 @@ c_parser_type_name (c_parser *parser)
   struct c_declarator *declarator;
   struct c_type_name *ret;
   bool dummy = false;
+  location_t here = c_parser_peek_token (parser)->location;
+
   c_parser_declspecs (parser, specs, false, true, true, cla_prefer_type);
   if (!specs->declspecs_seen_p)
     {
@@ -3570,7 +3574,7 @@ c_parser_type_name (c_parser *parser)
   if (specs->type != error_mark_node)
     {
       pending_xref_error ();
-      finish_declspecs (specs);
+      finish_declspecs (specs, here);
     }
   declarator = c_parser_declarator (parser,
 				    specs->typespec_kind != ctsk_none,
diff --git a/gcc/c-tree.h b/gcc/c-tree.h
index cda5d06..79231cb 100644
--- a/gcc/c-tree.h
+++ b/gcc/c-tree.h
@@ -494,7 +494,7 @@ extern struct c_declspecs *declspecs_add_scspec (struct c_declspecs *, tree);
 extern struct c_declspecs *declspecs_add_attrs (struct c_declspecs *, tree);
 extern struct c_declspecs *declspecs_add_addrspace (struct c_declspecs *,
 						    addr_space_t);
-extern struct c_declspecs *finish_declspecs (struct c_declspecs *);
+extern struct c_declspecs *finish_declspecs (struct c_declspecs *, location_t);
 
 /* in c-objc-common.c */
 extern bool c_objc_common_init (void);
diff --git a/gcc/testsuite/gcc.dg/cpp/syshdr3.c b/gcc/testsuite/gcc.dg/cpp/syshdr3.c
new file mode 100644
index 0000000..8850410
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/cpp/syshdr3.c
@@ -0,0 +1,16 @@
+/* Contributed by Dodji Seketeli <dodji@redhat.com> */
+/* Origin: PR preprocessor/7263 */
+/* { dg-options "-pedantic -std=c89 -ftrack-macro-expansion=1" } */
+/* { dg-do compile } */
+
+/* This tests the proprer suppression of warning coming from macro
+   defined in system headers and expanded in a non-system header
+   location.  */
+#include "syshdr3.h"
+
+_Complex c = _Complex_I + _Complex_I; /* These macros are defined in
+					 system header so we should
+					 have no warning here.  */
+U_LL u = ONE_ULL; /* Likewise here.  */
+
+unsigned long long v = 1ULL; /* { dg-warning "long long" } */
diff --git a/gcc/testsuite/gcc.dg/cpp/syshdr3.h b/gcc/testsuite/gcc.dg/cpp/syshdr3.h
new file mode 100644
index 0000000..e5d502a
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/cpp/syshdr3.h
@@ -0,0 +1,7 @@
+#pragma GCC system_header
+
+#define _Complex __complex__
+#define _Complex_I 1.0iF
+
+#define U_LL unsigned long long
+#define ONE_ULL 1ULL
diff --git a/gcc/testsuite/gcc.dg/nofixed-point-2.c b/gcc/testsuite/gcc.dg/nofixed-point-2.c
index 5b2f209..8442a19 100644
--- a/gcc/testsuite/gcc.dg/nofixed-point-2.c
+++ b/gcc/testsuite/gcc.dg/nofixed-point-2.c
@@ -20,10 +20,10 @@ f3 (void)
   return 0k;			/* { dg-error "not supported" "reject fixed-point" } */
 }
 
-_Sat
-f4 (void)			/* { dg-error "not supported" "reject fixed-point" } */
+_Sat                            /* { dg-error "not supported" "reject fixed-point" } */
+f4 (void)
 {
   return 0k;			/* { dg-error "not supported" "reject fixed-point" } */
 }
 
-/* { dg-error "is used without" "" { target *-*-* } 24 } */
+/* { dg-error "is used without" "" { target *-*-* } 23 } */
diff --git a/libcpp/expr.c b/libcpp/expr.c
index 3c36127..b7b4e1a 100644
--- a/libcpp/expr.c
+++ b/libcpp/expr.c
@@ -59,7 +59,7 @@ static cpp_num num_rshift (cpp_num, size_t, size_t);
 
 static cpp_num append_digit (cpp_num, int, int, size_t);
 static cpp_num parse_defined (cpp_reader *);
-static cpp_num eval_token (cpp_reader *, const cpp_token *);
+static cpp_num eval_token (cpp_reader *, const cpp_token *, source_location);
 static struct op *reduce (cpp_reader *, struct op *, enum cpp_ttype);
 static unsigned int interpret_float_suffix (const uchar *, size_t);
 static unsigned int interpret_int_suffix (const uchar *, size_t);
@@ -76,6 +76,12 @@ static void check_promotion (cpp_reader *, const struct op *);
 #define SYNTAX_ERROR2(msgid, arg) \
   do { cpp_error (pfile, CPP_DL_ERROR, msgid, arg); goto syntax_error; } \
   while(0)
+#define SYNTAX_ERROR_AT(loc, msgid) \
+  do { cpp_error_with_line (pfile, CPP_DL_ERROR, (loc), 0, msgid); goto syntax_error; } \
+  while(0)
+#define SYNTAX_ERROR2_AT(loc, msgid, arg)					\
+  do { cpp_error_with_line (pfile, CPP_DL_ERROR, (loc), 0, msgid, arg); goto syntax_error; } \
+  while(0)
 
 /* Subroutine of cpp_classify_number.  S points to a float suffix of
    length LEN, possibly zero.  Returns 0 for an invalid suffix, or a
@@ -223,7 +229,8 @@ interpret_int_suffix (const uchar *s, size_t len)
    floating point, or invalid), radix (decimal, octal, hexadecimal),
    and type suffixes.  */
 unsigned int
-cpp_classify_number (cpp_reader *pfile, const cpp_token *token)
+cpp_classify_number (cpp_reader *pfile, const cpp_token *token,
+		     source_location virtual_location)
 {
   const uchar *str = token->val.str.text;
   const uchar *limit;
@@ -279,7 +286,8 @@ cpp_classify_number (cpp_reader *pfile, const cpp_token *token)
 	  if (float_flag == NOT_FLOAT)
 	    float_flag = AFTER_POINT;
 	  else
-	    SYNTAX_ERROR ("too many decimal points in number");
+	    SYNTAX_ERROR_AT (virtual_location,
+			     "too many decimal points in number");
 	}
       else if ((radix <= 10 && (c == 'e' || c == 'E'))
 	       || (radix == 16 && (c == 'p' || c == 'P')))
@@ -307,8 +315,8 @@ cpp_classify_number (cpp_reader *pfile, const cpp_token *token)
 	    radix = 10;
 
 	  if (CPP_PEDANTIC (pfile))
-	    cpp_error (pfile, CPP_DL_PEDWARN,
-		       "fixed-point constants are a GCC extension");
+	    cpp_error_with_line (pfile, CPP_DL_PEDWARN, virtual_location, 0,
+				 "fixed-point constants are a GCC extension");
 	  goto syntax_ok;
 	}
       else
@@ -321,26 +329,29 @@ cpp_classify_number (cpp_reader *pfile, const cpp_token *token)
   if (max_digit >= radix)
     {
       if (radix == 2)
-	SYNTAX_ERROR2 ("invalid digit \"%c\" in binary constant", '0' + max_digit);
+	SYNTAX_ERROR2_AT (virtual_location,
+			  "invalid digit \"%c\" in binary constant", '0' + max_digit);
       else
-	SYNTAX_ERROR2 ("invalid digit \"%c\" in octal constant", '0' + max_digit);
+	SYNTAX_ERROR2_AT (virtual_location,
+			  "invalid digit \"%c\" in octal constant", '0' + max_digit);
     }
 
   if (float_flag != NOT_FLOAT)
     {
       if (radix == 2)
 	{
-	  cpp_error (pfile, CPP_DL_ERROR,
-		     "invalid prefix \"0b\" for floating constant");
+	  cpp_error_with_line (pfile, CPP_DL_ERROR, virtual_location, 0,
+			       "invalid prefix \"0b\" for floating constant");
 	  return CPP_N_INVALID;
 	}
 
       if (radix == 16 && !seen_digit)
-	SYNTAX_ERROR ("no digits in hexadecimal floating constant");
+	SYNTAX_ERROR_AT (virtual_location,
+			 "no digits in hexadecimal floating constant");
 
       if (radix == 16 && CPP_PEDANTIC (pfile) && !CPP_OPTION (pfile, c99))
-	cpp_error (pfile, CPP_DL_PEDWARN,
-		   "use of C99 hexadecimal floating constant");
+	cpp_error_with_line (pfile, CPP_DL_PEDWARN, virtual_location, 0,
+			     "use of C99 hexadecimal floating constant");
 
       if (float_flag == AFTER_EXPON)
 	{
@@ -349,21 +360,22 @@ cpp_classify_number (cpp_reader *pfile, const cpp_token *token)
 
 	  /* Exponent is decimal, even if string is a hex float.  */
 	  if (!ISDIGIT (*str))
-	    SYNTAX_ERROR ("exponent has no digits");
+	    SYNTAX_ERROR_AT (virtual_location, "exponent has no digits");
 
 	  do
 	    str++;
 	  while (ISDIGIT (*str));
 	}
       else if (radix == 16)
-	SYNTAX_ERROR ("hexadecimal floating constants require an exponent");
+	SYNTAX_ERROR_AT (virtual_location,
+			 "hexadecimal floating constants require an exponent");
 
       result = interpret_float_suffix (str, limit - str);
       if (result == 0)
 	{
-	  cpp_error (pfile, CPP_DL_ERROR,
-		     "invalid suffix \"%.*s\" on floating constant",
-		     (int) (limit - str), str);
+	  cpp_error_with_line (pfile, CPP_DL_ERROR, virtual_location, 0,
+			       "invalid suffix \"%.*s\" on floating constant",
+			       (int) (limit - str), str);
 	  return CPP_N_INVALID;
 	}
 
@@ -371,33 +383,33 @@ cpp_classify_number (cpp_reader *pfile, const cpp_token *token)
       if (limit != str
 	  && CPP_WTRADITIONAL (pfile)
 	  && ! cpp_sys_macro_p (pfile))
-	cpp_warning (pfile, CPP_W_TRADITIONAL,
-		     "traditional C rejects the \"%.*s\" suffix",
-		     (int) (limit - str), str);
+	cpp_warning_with_line (pfile, CPP_W_TRADITIONAL, virtual_location, 0,
+			       "traditional C rejects the \"%.*s\" suffix",
+			       (int) (limit - str), str);
 
       /* A suffix for double is a GCC extension via decimal float support.
 	 If the suffix also specifies an imaginary value we'll catch that
 	 later.  */
       if ((result == CPP_N_MEDIUM) && CPP_PEDANTIC (pfile))
-	cpp_error (pfile, CPP_DL_PEDWARN,
-		   "suffix for double constant is a GCC extension");
+	cpp_error_with_line (pfile, CPP_DL_PEDWARN, virtual_location, 0,
+			     "suffix for double constant is a GCC extension");
 
       /* Radix must be 10 for decimal floats.  */
       if ((result & CPP_N_DFLOAT) && radix != 10)
         {
-          cpp_error (pfile, CPP_DL_ERROR,
-                     "invalid suffix \"%.*s\" with hexadecimal floating constant",
-                     (int) (limit - str), str);
+          cpp_error_with_line (pfile, CPP_DL_ERROR, virtual_location, 0,
+			       "invalid suffix \"%.*s\" with hexadecimal floating constant",
+			       (int) (limit - str), str);
           return CPP_N_INVALID;
         }
 
       if ((result & (CPP_N_FRACT | CPP_N_ACCUM)) && CPP_PEDANTIC (pfile))
-	cpp_error (pfile, CPP_DL_PEDWARN,
-		   "fixed-point constants are a GCC extension");
+	cpp_error_with_line (pfile, CPP_DL_PEDWARN, virtual_location, 0,
+			     "fixed-point constants are a GCC extension");
 
       if ((result & CPP_N_DFLOAT) && CPP_PEDANTIC (pfile))
-	cpp_error (pfile, CPP_DL_PEDWARN,
-		   "decimal float constants are a GCC extension");
+	cpp_error_with_line (pfile, CPP_DL_PEDWARN, virtual_location, 0,
+			     "decimal float constants are a GCC extension");
 
       result |= CPP_N_FLOATING;
     }
@@ -406,9 +418,9 @@ cpp_classify_number (cpp_reader *pfile, const cpp_token *token)
       result = interpret_int_suffix (str, limit - str);
       if (result == 0)
 	{
-	  cpp_error (pfile, CPP_DL_ERROR,
-		     "invalid suffix \"%.*s\" on integer constant",
-		     (int) (limit - str), str);
+	  cpp_error_with_line (pfile, CPP_DL_ERROR, virtual_location, 0,
+			       "invalid suffix \"%.*s\" on integer constant",
+			       (int) (limit - str), str);
 	  return CPP_N_INVALID;
 	}
 
@@ -421,9 +433,10 @@ cpp_classify_number (cpp_reader *pfile, const cpp_token *token)
 		       && CPP_OPTION (pfile, cpp_warn_long_long);
 
 	  if (u_or_i || large)
-	    cpp_warning (pfile, large ? CPP_W_LONG_LONG : CPP_W_TRADITIONAL,
-		         "traditional C rejects the \"%.*s\" suffix",
-		         (int) (limit - str), str);
+	    cpp_warning_with_line (pfile, large ? CPP_W_LONG_LONG : CPP_W_TRADITIONAL,
+				   virtual_location, 0,
+				   "traditional C rejects the \"%.*s\" suffix",
+				   (int) (limit - str), str);
 	}
 
       if ((result & CPP_N_WIDTH) == CPP_N_LARGE
@@ -434,9 +447,11 @@ cpp_classify_number (cpp_reader *pfile, const cpp_token *token)
 		                : N_("use of C99 long long integer constant");
 
 	  if (CPP_OPTION (pfile, c99))
-            cpp_warning (pfile, CPP_W_LONG_LONG, message);
+            cpp_warning_with_line (pfile, CPP_W_LONG_LONG, virtual_location,
+				   0, message);
           else
-            cpp_pedwarning (pfile, CPP_W_LONG_LONG, message);
+            cpp_pedwarning_with_line (pfile, CPP_W_LONG_LONG,
+				      virtual_location, 0, message);
         }
 
       result |= CPP_N_INTEGER;
@@ -444,11 +459,11 @@ cpp_classify_number (cpp_reader *pfile, const cpp_token *token)
 
  syntax_ok:
   if ((result & CPP_N_IMAGINARY) && CPP_PEDANTIC (pfile))
-    cpp_error (pfile, CPP_DL_PEDWARN,
-	       "imaginary constants are a GCC extension");
+    cpp_error_with_line (pfile, CPP_DL_PEDWARN, virtual_location, 0,
+			 "imaginary constants are a GCC extension");
   if (radix == 2 && CPP_PEDANTIC (pfile))
-    cpp_error (pfile, CPP_DL_PEDWARN,
-	       "binary constants are a GCC extension");
+    cpp_error_with_line (pfile, CPP_DL_PEDWARN, virtual_location, 0,
+			 "binary constants are a GCC extension");
 
   if (radix == 10)
     result |= CPP_N_DECIMAL;
@@ -736,7 +751,8 @@ parse_defined (cpp_reader *pfile)
    number or character constant, or the result of the "defined" or "#"
    operators).  */
 static cpp_num
-eval_token (cpp_reader *pfile, const cpp_token *token)
+eval_token (cpp_reader *pfile, const cpp_token *token,
+	    source_location virtual_location)
 {
   cpp_num result;
   unsigned int temp;
@@ -748,18 +764,18 @@ eval_token (cpp_reader *pfile, const cpp_token *token)
   switch (token->type)
     {
     case CPP_NUMBER:
-      temp = cpp_classify_number (pfile, token);
+      temp = cpp_classify_number (pfile, token, virtual_location);
       switch (temp & CPP_N_CATEGORY)
 	{
 	case CPP_N_FLOATING:
-	  cpp_error (pfile, CPP_DL_ERROR,
-		     "floating constant in preprocessor expression");
+	  cpp_error_with_line (pfile, CPP_DL_ERROR, virtual_location, 0,
+			       "floating constant in preprocessor expression");
 	  break;
 	case CPP_N_INTEGER:
 	  if (!(temp & CPP_N_IMAGINARY))
 	    return cpp_interpret_integer (pfile, token, temp);
-	  cpp_error (pfile, CPP_DL_ERROR,
-		     "imaginary number in preprocessor expression");
+	  cpp_error_with_line (pfile, CPP_DL_ERROR, virtual_location, 0,
+			       "imaginary number in preprocessor expression");
 	  break;
 
 	case CPP_N_INVALID:
@@ -806,8 +822,9 @@ eval_token (cpp_reader *pfile, const cpp_token *token)
 	  result.high = 0;
 	  result.low = 0;
 	  if (CPP_OPTION (pfile, warn_undef) && !pfile->state.skip_eval)
-	    cpp_warning (pfile, CPP_W_UNDEF, "\"%s\" is not defined",
-		         NODE_NAME (token->val.node.node));
+	    cpp_warning_with_line (pfile, CPP_W_UNDEF, virtual_location, 0,
+				   "\"%s\" is not defined",
+				   NODE_NAME (token->val.node.node));
 	}
       break;
 
@@ -817,11 +834,12 @@ eval_token (cpp_reader *pfile, const cpp_token *token)
 	  /* A pedantic warning takes precedence over a deprecated
 	     warning here.  */
 	  if (CPP_PEDANTIC (pfile))
-	    cpp_error (pfile, CPP_DL_PEDWARN,
-		       "assertions are a GCC extension");
+	    cpp_error_with_line (pfile, CPP_DL_PEDWARN,
+				 virtual_location, 0,
+				 "assertions are a GCC extension");
 	  else if (CPP_OPTION (pfile, cpp_warn_deprecated))
-	    cpp_warning (pfile, CPP_W_DEPRECATED,
-		         "assertions are a deprecated extension");
+	    cpp_warning_with_line (pfile, CPP_W_DEPRECATED, virtual_location, 0,
+				   "assertions are a deprecated extension");
 	}
       _cpp_test_assertion (pfile, &temp);
       result.high = 0;
@@ -923,6 +941,7 @@ _cpp_parse_expr (cpp_reader *pfile, bool is_if)
   struct op *top = pfile->op_stack;
   unsigned int lex_count;
   bool saw_leading_not, want_value = true;
+  source_location virtual_location = 0;
 
   pfile->state.skip_eval = 0;
 
@@ -939,9 +958,10 @@ _cpp_parse_expr (cpp_reader *pfile, bool is_if)
       struct op op;
 
       lex_count++;
-      op.token = cpp_get_token (pfile);
+
+      op.token = cpp_get_token_with_location (pfile, &virtual_location);
       op.op = op.token->type;
-      op.loc = op.token->src_loc;
+      op.loc = virtual_location;
 
       switch (op.op)
 	{
@@ -954,10 +974,11 @@ _cpp_parse_expr (cpp_reader *pfile, bool is_if)
 	case CPP_NAME:
 	case CPP_HASH:
 	  if (!want_value)
-	    SYNTAX_ERROR2 ("missing binary operator before token \"%s\"",
-			   cpp_token_as_text (pfile, op.token));
+	    SYNTAX_ERROR2_AT (virtual_location,
+			      "missing binary operator before token \"%s\"",
+			      cpp_token_as_text (pfile, op.token));
 	  want_value = false;
-	  top->value = eval_token (pfile, op.token);
+	  top->value = eval_token (pfile, op.token, virtual_location);
 	  continue;
 
 	case CPP_NOT:
@@ -974,8 +995,9 @@ _cpp_parse_expr (cpp_reader *pfile, bool is_if)
 
 	default:
 	  if ((int) op.op <= (int) CPP_EQ || (int) op.op >= (int) CPP_PLUS_EQ)
-	    SYNTAX_ERROR2 ("token \"%s\" is not valid in preprocessor expressions",
-			   cpp_token_as_text (pfile, op.token));
+	    SYNTAX_ERROR2_AT (op.loc,
+			      "token \"%s\" is not valid in preprocessor expressions",
+			      cpp_token_as_text (pfile, op.token));
 	  break;
 	}
 
@@ -983,27 +1005,32 @@ _cpp_parse_expr (cpp_reader *pfile, bool is_if)
       if (optab[op.op].flags & NO_L_OPERAND)
 	{
 	  if (!want_value)
-	    SYNTAX_ERROR2 ("missing binary operator before token \"%s\"",
-			   cpp_token_as_text (pfile, op.token));
+	    SYNTAX_ERROR2_AT (virtual_location,
+			      "missing binary operator before token \"%s\"",
+			      cpp_token_as_text (pfile, op.token));
 	}
       else if (want_value)
 	{
 	  /* We want a number (or expression) and haven't got one.
 	     Try to emit a specific diagnostic.  */
 	  if (op.op == CPP_CLOSE_PAREN && top->op == CPP_OPEN_PAREN)
-	    SYNTAX_ERROR ("missing expression between '(' and ')'");
+	    SYNTAX_ERROR_AT (op.loc,
+			     "missing expression between '(' and ')'");
 
 	  if (op.op == CPP_EOF && top->op == CPP_EOF)
- 	    SYNTAX_ERROR2 ("%s with no expression", is_if ? "#if" : "#elif");
+ 	    SYNTAX_ERROR2_AT (op.loc,
+			      "%s with no expression", is_if ? "#if" : "#elif");
 
  	  if (top->op != CPP_EOF && top->op != CPP_OPEN_PAREN)
- 	    SYNTAX_ERROR2 ("operator '%s' has no right operand",
- 			   cpp_token_as_text (pfile, top->token));
+ 	    SYNTAX_ERROR2_AT (op.loc,
+			      "operator '%s' has no right operand",
+			      cpp_token_as_text (pfile, top->token));
 	  else if (op.op == CPP_CLOSE_PAREN || op.op == CPP_EOF)
 	    /* Complain about missing paren during reduction.  */;
 	  else
-	    SYNTAX_ERROR2 ("operator '%s' has no left operand",
-			   cpp_token_as_text (pfile, op.token));
+	    SYNTAX_ERROR2_AT (op.loc,
+			      "operator '%s' has no left operand",
+			      cpp_token_as_text (pfile, op.token));
 	}
 
       top = reduce (pfile, top, op.op);
@@ -1028,7 +1055,8 @@ _cpp_parse_expr (cpp_reader *pfile, bool is_if)
 	  break;
 	case CPP_COLON:
 	  if (top->op != CPP_QUERY)
-	    SYNTAX_ERROR (" ':' without preceding '?'");
+	    SYNTAX_ERROR_AT (op.loc,
+			     " ':' without preceding '?'");
 	  if (!num_zerop (top[-1].value)) /* Was '?' condition true?  */
 	    pfile->state.skip_eval++;
 	  else
@@ -1045,7 +1073,7 @@ _cpp_parse_expr (cpp_reader *pfile, bool is_if)
 
       top->op = op.op;
       top->token = op.token;
-      top->loc = op.token->src_loc;
+      top->loc = op.loc;
     }
 
   /* The controlling macro expression is only valid if we called lex 3
@@ -1056,8 +1084,9 @@ _cpp_parse_expr (cpp_reader *pfile, bool is_if)
 
   if (top != pfile->op_stack)
     {
-      cpp_error (pfile, CPP_DL_ICE, "unbalanced stack in %s",
-		 is_if ? "#if" : "#elif");
+      cpp_error_with_line (pfile, CPP_DL_ICE, top->loc, 0,
+			   "unbalanced stack in %s",
+			   is_if ? "#if" : "#elif");
     syntax_error:
       return false;  /* Return false on syntax error.  */
     }
diff --git a/libcpp/include/cpplib.h b/libcpp/include/cpplib.h
index 825bf2f..b88c7d7 100644
--- a/libcpp/include/cpplib.h
+++ b/libcpp/include/cpplib.h
@@ -831,7 +831,8 @@ struct cpp_num
 
 /* Classify a CPP_NUMBER token.  The return value is a combination of
    the flags from the above sets.  */
-extern unsigned cpp_classify_number (cpp_reader *, const cpp_token *);
+extern unsigned cpp_classify_number (cpp_reader *, const cpp_token *,
+				     source_location);
 
 /* Evaluate a token classified as category CPP_N_INTEGER.  */
 extern cpp_num cpp_interpret_integer (cpp_reader *, const cpp_token *,
-- 
1.7.6


-- 
		Dodji

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

* Re: [PATCH 7/7] Reduce memory waste due to non-power-of-2 allocs
  2011-09-12 22:25       ` Jason Merrill
@ 2011-09-17 13:41         ` Dodji Seketeli
  2011-09-17 22:22           ` Jason Merrill
  2011-09-19  6:51           ` Laurynas Biveinis
  0 siblings, 2 replies; 135+ messages in thread
From: Dodji Seketeli @ 2011-09-17 13:41 UTC (permalink / raw)
  To: Jason Merrill; +Cc: gcc-patches, tromey, Laurynas Biveinis

Jason Merrill <jason@redhat.com> writes:

> On 07/16/2011 10:37 AM, Dodji Seketeli wrote:
> > Ideally, I'd prefer some parts of this patch to be integrated into the
> > memory allocator.  That is, I'd like to see the memory allocator have
> > an interface that returns the actual size of memory it has allocated.
> > This would help client code like this one to avoid requesting memory
> > unnecessarily.
> 
> This sounds good to me.  Or, it would probably be simpler to just
> provide a separate function for rounding up the allocated size using
> the same code as the allocator.

OK, so the patch below extracts a public ggc_alloced_size_for_request
function from the different implementations of the ggc allocator's
interface, and lets new_linemap use that.

I am adding CC-ing Laurynas Biveinis to the CC list.

Thanks.

From: Dodji Seketeli <dodji@redhat.com>
Date: Tue, 17 May 2011 16:48:01 +0200
Subject: [PATCH 7/7] Reduce memory waste due to non-power-of-2 allocs

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_alloced_size_for_request interface to the ggc
allocator.  In each of the two main allocator implementations of
('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_alloced_size_for_request just returns the requested allocation
size.

Tested on x86_64-unknown-linux-gnu against trunk for each allocator.

libcpp/

	* 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/

	* ggc.h (ggc_alloced_size_for_request): Declare new public entry
	point.
	* ggc-none.c (ggc_alloced_size_for_request): New public stub
	function.
	* ggc-page.c (ggc_alloced_size_order_for_request): New static
	function.  Factorized from ggc_internal_alloc_stat.
	(ggc_alloced_size_for_request): 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_alloced_size_for_request): New public function
	extracted from ggc_internal_alloc_zone_stat.
	(ggc_internal_alloc_zone_stat): Use ggc_alloced_size_for_request.
	* toplev.c (general_init): Initialize
	line_table->alloced_size_for_request.
---
 gcc/ggc-none.c            |    9 +++++++
 gcc/ggc-page.c            |   53 +++++++++++++++++++++++++++++++++++---------
 gcc/ggc-zone.c            |   27 ++++++++++++++++------
 gcc/ggc.h                 |    2 +
 gcc/toplev.c              |    1 +
 libcpp/include/line-map.h |    8 ++++++
 libcpp/line-map.c         |   42 ++++++++++++++++++++++++++++++-----
 7 files changed, 117 insertions(+), 25 deletions(-)

diff --git a/gcc/ggc-none.c b/gcc/ggc-none.c
index 97d25b9..2223cc6 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_alloced_size_for_request (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..dcf9c79 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_alloced_size_order_for_request (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_alloced_size_for_request (size_t requested_size)
+{
+  size_t size = 0;
+  
+  ggc_alloced_size_order_for_request (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_alloced_size_order_for_request (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..ff7b466 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_alloced_size_for_request (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..9319dc1 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_alloced_size_for_request (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 2f90261..dd1eeab 100644
--- a/gcc/toplev.c
+++ b/gcc/toplev.c
@@ -1177,6 +1177,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->alloced_size_for_request = ggc_alloced_size_for_request;
   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 3989c89..37a23f1 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_alloced_size_for_request_func) (size_t);
+
 /* An ordinary line map encodes physical source locations. Those
    physical source locations are called "spelling locations".
    
@@ -298,6 +302,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_alloced_size_for_request_func alloced_size_for_request;
 };
 
 /* Returns the pointer to the memory region where information about
diff --git a/libcpp/line-map.c b/libcpp/line-map.c
index 09dffcb..05f9dd8 100644
--- a/libcpp/line-map.c
+++ b/libcpp/line-map.c
@@ -35,6 +35,9 @@ static const struct line_map* linemap_macro_map_lookup (struct line_maps *,
 extern unsigned num_expanded_macros_counter;
 extern unsigned num_macro_tokens_counter;
 
+/* from ggc.h.  */
+size_t ggc_alloced_size_for_request (size_t requested_size);
+
 /* Initialize a line map set.  */
 
 void
@@ -77,16 +80,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_alloced_size_for_request_func alloced_size_for_request =
+	set->alloced_size_for_request;
+
+      /* 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 = alloced_size_for_request (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,
-- 
		Dodji

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

* Re: [PATCH 3/7] Emit macro expansion related diagnostics
  2011-09-16  8:19           ` Dodji Seketeli
@ 2011-09-17 21:27             ` Jason Merrill
  2011-09-19 14:37               ` Dodji Seketeli
  0 siblings, 1 reply; 135+ messages in thread
From: Jason Merrill @ 2011-09-17 21:27 UTC (permalink / raw)
  To: Dodji Seketeli; +Cc: gcc-patches, tromey, gdr, joseph, burnus, charlet, bonzini

On 09/16/2011 03:55 AM, Dodji Seketeli wrote:
> +    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 sixth 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.

No UTF-8 in the code, please.

Do you mean eighth rather than sixth?

> +      /* If the token at RESOLVED_LOCATION [at macro definition point]
> +         is itself inside an expanded macro then we keep unwinding the
> +         trace by reaching the "parent macro" that got expanded inside
> +         the definition of the macro of MAP...  */

This doesn't make sense to me; a macro definition can't be inside an 
expanded macro.  Can you describe this situation better?

> +  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 (resolved_map))

Should the SYSP check use "map" rather than "resolved_map"?

Jason

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

* Re: [PATCH 4/7] Support -fdebug-cpp option
  2011-09-16  8:23         ` Dodji Seketeli
@ 2011-09-17 22:01           ` Jason Merrill
  0 siblings, 0 replies; 135+ messages in thread
From: Jason Merrill @ 2011-09-17 22:01 UTC (permalink / raw)
  To: Dodji Seketeli; +Cc: gcc-patches, tromey, gdr, joseph, burnus, charlet, bonzini

OK.

Jason

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

* Re: [PATCH 5/7] Add line map statistics to -fmem-report output
  2011-09-16  8:29         ` Dodji Seketeli
@ 2011-09-17 22:05           ` Jason Merrill
  2011-09-20 12:10             ` Dodji Seketeli
  0 siblings, 1 reply; 135+ messages in thread
From: Jason Merrill @ 2011-09-17 22:05 UTC (permalink / raw)
  To: Dodji Seketeli; +Cc: gcc-patches, tromey, gdr, joseph, burnus, charlet, bonzini

On 09/16/2011 04:13 AM, Dodji Seketeli wrote:
>> It seems odd to add in the locations size here since it's also printed
>> separately.
> I wanted macro_maps_used_size to really reflect the total used size
> for macro maps, without having to mentally do the addition of its two
> components.  But at the same time, I was interested in seeing how much
> memory were the locations taking inside the macro map memory.  As I
> was suspecting them to take a lot of memory.

Right.  I suppose my complaint is that the information is oddly 
redundant.  If you have two quantities A and B, is it more useful to 
print A and B, or A+B and B?  I don't object to printing the total of 
the memory used by the maps and the locations, but I think all the 
components should be printed as well.

It's hard to compare macro_maps_allocated_size with macro_maps_used_size 
since you have to do subtraction to discover both values from what is 
printed.

And do the addition in dump_line_table_statistics rather than store sums 
in linemap_stats.

Also, linemap_stats and linemap_get_statistics need comments.

> +/* Display an integer amount as multiple of 1K or 1M (in base 2à).

No UTF-8.

Jason

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

* Re: [PATCH 7/7] Reduce memory waste due to non-power-of-2 allocs
  2011-09-17 13:41         ` Dodji Seketeli
@ 2011-09-17 22:22           ` Jason Merrill
  2011-09-18 22:30             ` Dodji Seketeli
  2011-09-19  6:51           ` Laurynas Biveinis
  1 sibling, 1 reply; 135+ messages in thread
From: Jason Merrill @ 2011-09-17 22:22 UTC (permalink / raw)
  To: Dodji Seketeli; +Cc: gcc-patches, tromey, Laurynas Biveinis

On 09/17/2011 07:08 AM, Dodji Seketeli wrote:
> OK, so the patch below extracts a public ggc_alloced_size_for_request
> function from the different implementations of the ggc allocator's
> interface, and lets new_linemap use that.

Maybe "ggc_round_alloc_size"?  OK with that change if nobody else has 
comments this week.

Jason

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

* Re: [PATCH 6/7] Kill pedantic warnings on system headers macros
  2011-09-16 11:25         ` Dodji Seketeli
@ 2011-09-17 22:34           ` Jason Merrill
  2011-09-18 18:59             ` Dodji Seketeli
  0 siblings, 1 reply; 135+ messages in thread
From: Jason Merrill @ 2011-09-17 22:34 UTC (permalink / raw)
  To: Dodji Seketeli; +Cc: gcc-patches, tromey, gdr, joseph, burnus, charlet, bonzini

On 09/16/2011 04:46 AM, Dodji Seketeli wrote:
>  struct c_declspecs *
> -finish_declspecs (struct c_declspecs *specs)
> +finish_declspecs (struct c_declspecs *specs,
> +                 location_t where)

Let's call this first_token_loc, too.  And mention it in the function 
comment.

OK with that change.

Jason

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

* Re: [PATCH 2/7] Generate virtual locations for tokens
  2011-09-14 22:56             ` Jason Merrill
@ 2011-09-18 13:44               ` Dodji Seketeli
  2011-09-19 22:31                 ` Jason Merrill
  0 siblings, 1 reply; 135+ messages in thread
From: Dodji Seketeli @ 2011-09-18 13:44 UTC (permalink / raw)
  To: Jason Merrill; +Cc: gcc-patches, tromey, gdr, joseph, burnus, charlet, bonzini

Jason Merrill <jason@redhat.com> writes:

> > +  void *macro;
> 
> This should be a union rather than an untyped pointer.

OK, done.  Note that I hijacked that existing macro member to reduce
size of the change set; I agree though that using an union there is
more robust.

> 
> > +      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++;
> > +         if (context->macro)
> 
> The other places that deal with TOKENS_KIND_EXTENDED don't test that
> context->macro is non-null.  Why is it needed here?

You are right, it's not needed.  Fixed.

> 
> > +    {
> > +      cpp_hashnode *macro;
> > +      if (context->tokens_kind == TOKENS_KIND_EXTENDED)
> > +       {
> > +         macro_context *mc = (macro_context *) context->macro;
> > +         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->macro = NULL;
> > +       }
> > +      else
> > +       macro = (cpp_hashnode *) context->macro;
> > +
> > +      if (macro != NULL)
> > +       macro->flags &= ~NODE_DISABLED;
> 
> How can macro end up NULL if context->macro was set?

It's possible when called from, e.g, expand_arg.  In that case, we
want to artificially walk the tokens that make up the argument of the
function-like macro.  A dummy (expanded) context containing the tokens
of the argument is then pushed and cpp_get_token_1 is used to walk
over them.  The dummy context doesn't have a macro field set.  I have
just added a comment explaining this corner case.

> 
> > +/* In the traditionnal mode of the preprocessor, if we are currently
> > +   location if we are in the traditionnal mode, and just returns
> 
> "traditional"
> 
> I don't think we need to talk about virtual locations before
> cpp_get_token_1; it's not an external interface, and it's redundant
> with the description before cpp_get_token_with_location.

Okay, I removed the comment that talks about virtual locations here.

> > +  result = cpp_get_token_1 (pfile, loc);
> >    if (pfile->context->macro)
> > -    *loc = pfile->invocation_location;
> > +    {
> > +      if (!CPP_OPTION (pfile, track_macro_expansion))
> > +       *loc = pfile->invocation_location;
> > +    }
> >    else
> >      *loc = result->src_loc;
> >
> > +  *loc = maybe_adjust_loc_for_trad_cpp (pfile, *loc);
> 
> Let's move this code into cpp_get_token_1 so that all the location
> tweaking is in one place.

Done.

> 
> > +  switch (it->kind)
> > +    {
> > +    case MACRO_ARG_TOKEN_NORMAL:
> > +    case MACRO_ARG_TOKEN_EXPANDED:
> > +      it->token_ptr++;
> > +      if (track_macro_exp_p)
> > +       it->location_ptr++;
> > +      break;
> > +    case MACRO_ARG_TOKEN_STRINGIFIED:
> > +#ifdef ENABLE_CHECKING
> > +      if (it->num_forwards > 0)
> > +       abort ();
> > +      it->num_forwards++;
> > +#endif
> > +      break;
> > +    }
> 
> Don't you want to increment num_forwards in the normal/expanded
> cases, too?

Oops, yes. Fixed.

> 
> > +tokens_buff_append_token (cpp_reader *pfile,
> > +                         _cpp_buff *buffer,
> > +                         source_location *virt_locs,
> > +                         const cpp_token *token,
> > +                         source_location def_loc,
> > +                         source_location parm_def_loc,
> > +                         const struct line_map *map,
> > +                         unsigned int *macro_token_index)
> 
> Why is macro_token_index a pointer?  Nothing seems to modify the
> referent.

I guess it's a leftover over of a previous iteration.  Fixed now.

> 
> > +/* Appends a token to the end of the token buffer BUFFER.  Note that
> > +   this function doesn't enlarge BUFFER; it overwrite the last memory
> > +   location of BUFFER that holds a token.
> 
> That doesn't sound like appending.

I have changed the name into tokens_buff_add_token and I updated the
comments accordingly.

Thanks.

From: Dodji Seketeli <dodji@redhat.com>
Date: Sat, 4 Dec 2010 14:04:29 +0100
Subject: [PATCH 2/7] Generate virtual locations for tokens

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.

The combination of this patch and the previous one bootstraps with
--enable-languages=all,ada and passes regression tests on
x86_64-unknown-linux-gnu.

gcc/
	* 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/

	* 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/

	* 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)<expanded_capacity, virt_locs>:
	(struct macro_arg)<virt_locs_capacity, 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_args_mem, ensure_expanded_args_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_append_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_args_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/c-family/c-opts.c   |   12 +
 gcc/c-family/c.opt      |    8 +
 gcc/doc/cppopts.texi    |   18 +
 gcc/doc/invoke.texi     |    6 +-
 libcpp/directives.c     |    2 +-
 libcpp/include/cpplib.h |    8 +
 libcpp/init.c           |    3 +-
 libcpp/internal.h       |   57 ++-
 libcpp/lex.c            |   41 ++-
 libcpp/macro.c          | 1314 ++++++++++++++++++++++++++++++++++++++++++-----
 libcpp/traditional.c    |    2 +-
 11 files changed, 1326 insertions(+), 145 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 957d75c..7e1b7c2 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/libcpp/directives.c b/libcpp/directives.c
index a62ddeb..31c5e95 100644
--- a/libcpp/directives.c
+++ b/libcpp/directives.c
@@ -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/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 588e8ed..5ddde9f 100644
--- a/libcpp/internal.h
+++ b/libcpp/internal.h
@@ -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,25 @@ 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 +653,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/macro.c b/libcpp/macro.c
index 03fe79e..5a610ee 100644
--- a/libcpp/macro.c
+++ b/libcpp/macro.c
@@ -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,64 @@ 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.  */
+  size_t expanded_capacity;     /* total size of expanded array.  */
+  source_location *virt_locs;	/* Where virtual locations for
+				   unexpanded tokens are stored.  */
+  unsigned virt_locs_capacity;	/* Total size of virtual locations
+				   array.  */
+  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 line macro
+   argument.  */
+typedef struct macro_arg_token_iter macro_arg_token_iter;
+struct macro_arg_token_iter
+{
+  /* The cpp_reader the macro comes from.  */
+  cpp_reader *pfile;
+  /* The kind of token over which we are supposed to iterate.  */
+  enum macro_arg_token_kind kind;
+  /* The function-like macro the tokens come from.  */
+  const macro_arg *arg;
+  /* 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 +106,56 @@ 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_args_mem (cpp_reader *, macro_arg *, size_t);
+static void ensure_expanded_args_room (cpp_reader *, macro_arg *, size_t);
+static void delete_macro_args (_cpp_buff*, unsigned num_args);
+static void set_arg_token (cpp_reader *, macro_arg *, const cpp_token *,
+			   source_location, size_t,
+			   enum macro_arg_token_kind);
+static const source_location *get_arg_token_location (cpp_reader *,
+						      const macro_arg *,
+						      enum macro_arg_token_kind);
+static const cpp_token **arg_token_ptr_at (cpp_reader *,
+					   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 *, cpp_reader*,
+				       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 (cpp_reader *,
+						   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_reader *,
+						_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 +167,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.  */
@@ -507,7 +609,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
@@ -517,10 +619,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)
 	{
@@ -584,23 +701,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;
@@ -615,9 +746,16 @@ collect_args (cpp_reader *pfile, const cpp_hashnode *node,
     {
       unsigned int paren_depth = 0;
       unsigned int ntokens = 0;
+      num_args_alloced++;
 
       argc++;
       arg->first = (const cpp_token **) buff->cur;
+      if (track_macro_expansion_p)
+	{
+	  arg->virt_locs_capacity = DEFAULT_NUM_TOKENS_PER_MACRO_ARG;
+	  arg->virt_locs = XNEWVEC (source_location,
+				    arg->virt_locs_capacity);
+	}
 
       for (;;)
 	{
@@ -625,11 +763,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 > arg->virt_locs_capacity))
+	    {
+	      arg->virt_locs_capacity += ARG_TOKENS_EXTENT;
+	      arg->virt_locs = XRESIZEVEC (source_location,
+					   arg->virt_locs,
+					   arg->virt_locs_capacity);
+	    }
 
-	  token = cpp_get_token (pfile);
+	  token = cpp_get_token_1 (pfile, &virt_loc);
 
 	  if (token->type == CPP_PADDING)
 	    {
@@ -686,7 +833,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);
 
@@ -700,8 +847,9 @@ collect_args (cpp_reader *pfile, const cpp_hashnode *node,
 	      else
 		continue;
 	    }
-
-	  arg->first[ntokens++] = token;
+	  set_arg_token (pfile, arg, token, virt_loc,
+			 ntokens, MACRO_ARG_TOKEN_NORMAL);
+	  ntokens++;
 	}
 
       /* Drop trailing padding.  */
@@ -709,7 +857,8 @@ collect_args (cpp_reader *pfile, const cpp_hashnode *node,
 	ntokens--;
 
       arg->count = ntokens;
-      arg->first[ntokens] = &pfile->eof;
+      set_arg_token (pfile, arg, &pfile->eof, pfile->eof.src_loc,
+		     ntokens, MACRO_ARG_TOKEN_NORMAL);
 
       /* Terminate the argument.  Excess arguments loop back and
 	 overwrite the final legitimate argument, before failing.  */
@@ -752,6 +901,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;
 	}
     }
@@ -765,10 +916,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;
 
@@ -785,7 +938,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
@@ -819,13 +972,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;
@@ -850,11 +1005,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--;
@@ -873,8 +1030,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.  */
@@ -888,13 +1050,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 (pfile, 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)
 	{
@@ -922,33 +1115,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 (cpp_reader *pfile, macro_arg *arg, const cpp_token *token,
+	       source_location location, size_t index,
+	       enum macro_arg_token_kind kind)
+{  
+  const cpp_token **token_ptr;
+  source_location *loc = NULL;
+  bool track_macro_exp_p = CPP_OPTION (pfile, track_macro_expansion);
+
+  token_ptr =
+    arg_token_ptr_at (pfile, 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.  */
+static const source_location *
+get_arg_token_location (cpp_reader *pfile,
+			const macro_arg *arg,
+			enum macro_arg_token_kind kind)
+{
+  source_location *loc = NULL;
+  const cpp_token **token_ptr = arg_token_ptr_at (pfile, arg, 0,
+						  kind, &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 (cpp_reader *pfile, const macro_arg *arg,
+		  size_t index, enum macro_arg_token_kind kind,
+		  source_location **virt_location)
+{
+  bool track_macro_exp_p = CPP_OPTION (pfile, track_macro_expansion);
+  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)
+    return NULL;
+
+  if (virt_location)
+    {
+      if (track_macro_exp_p)
+	{
+	  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;
+	}
+      else
+	*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,
+			   cpp_reader *pfile,
+			   enum macro_arg_token_kind kind,
+			   const macro_arg *arg,
+			   const cpp_token **token_ptr)
+{
+  iter->pfile = pfile;
+  iter->kind = kind;
+  iter->arg = arg;
+  iter->token_ptr = token_ptr;
+  iter->location_ptr = get_arg_token_location (pfile, arg, kind);
+#ifdef ENABLE_CHECKING
+  iter->num_forwards = 0;
+#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)
+{
+  bool track_macro_exp_p = CPP_OPTION (it->pfile,
+				       track_macro_expansion);
+
+  switch (it->kind)
+    {
+    case MACRO_ARG_TOKEN_NORMAL:
+    case MACRO_ARG_TOKEN_EXPANDED:
+      it->token_ptr++;
+      if (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
+  return *it->location_ptr;
+}
+
+/* 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.  */
@@ -970,67 +1444,222 @@ 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, &virt_locs);
+
   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 (pfile, 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, pfile,
+				     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, pfile,
+				     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, pfile,
+				     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, pfile,
+				     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 (pfile, 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 (pfile, 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)
@@ -1046,7 +1675,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 (pfile, 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)
@@ -1060,13 +1694,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.  */
@@ -1094,6 +1731,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;
@@ -1110,8 +1748,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;
@@ -1122,15 +1760,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,
@@ -1138,14 +1805,196 @@ _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 the
+   -ftrack-macro-expansion flag is in effect and if VIRT_LOCS is
+   non-null, *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)
+{
+  bool track_macro_exp_p = CPP_OPTION (pfile, track_macro_expansion);
+  size_t tokens_size = len * sizeof (cpp_token *);
+  size_t locs_size = len * sizeof (source_location);
+
+  if (track_macro_exp_p && 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 points to where to
+   insert the virtual location of TOKEN; that is, if the flag
+   -ftrack-macro-expansion is in effect.  TOKEN is the token to
+   insert.  DEF_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 PARM_DEF_LOC can
+   just be set to the same value as 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 (cpp_reader *pfile,
+			  const cpp_token **dest,
+			  source_location *virt_loc_dest,
+			  const cpp_token *token,
+			  source_location def_loc,
+			  source_location parm_def_loc,			  
+			  const struct line_map *map,
+			  unsigned int macro_token_index)
+{
+  bool track_macro_exp_p = CPP_OPTION (pfile, track_macro_expansion);
+  source_location macro_loc = def_loc;
+  const cpp_token **result;
+
+  if (track_macro_exp_p)
+    {
+      if (map)
+	macro_loc = linemap_add_macro_token (map, macro_token_index,
+					     def_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 then overwrites the last memory location
+   of BUFFER that holds a token.
+
+   TOKEN is the token to append. DEF_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 PARM_DEF_LOC can just be set to the
+   same value as 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.  This function adds
+   the virtual location DEF_LOC it 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_reader *pfile,
+		       _cpp_buff *buffer,
+		       source_location *virt_locs,
+		       const cpp_token *token,
+		       source_location def_loc,
+		       source_location parm_def_loc,
+		       const struct line_map *map,
+		       unsigned int macro_token_index)
+{
+  const cpp_token **result;
+  unsigned token_index = 
+    (BUFF_FRONT (buffer) - buffer->base) / sizeof (cpp_token *);
+
+  result =
+    tokens_buff_put_token_to (pfile, (const cpp_token **) BUFF_FRONT (buffer),
+			      &virt_locs[token_index],
+			      token, def_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_args_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);
+  arg->expanded_capacity = 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_args_room (cpp_reader *pfile, macro_arg *arg, size_t size)
+{
+  if (size <= arg->expanded_capacity)
+    return;
+
+  size *= 2;
+
+  arg->expanded =
+    XRESIZEVEC (const cpp_token *, arg->expanded, size);
+  arg->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
@@ -1157,8 +2006,10 @@ expand_arg (cpp_reader *pfile, macro_arg *arg)
 {
   unsigned int 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.  */
@@ -1167,26 +2018,32 @@ expand_arg (cpp_reader *pfile, macro_arg *arg)
 
   /* Loop, reading in the arguments.  */
   capacity = 256;
-  arg->expanded = XNEWVEC (const cpp_token *, capacity);
+  alloc_expanded_args_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_args_room (pfile, arg, arg->expanded_count + 1);
 
-      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 (pfile, arg, token, location,
+		     arg->expanded_count, MACRO_ARG_TOKEN_EXPANDED);
+      arg->expanded_count++;
     }
 
   _cpp_pop_context (pfile);
@@ -1195,25 +2052,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
@@ -1221,12 +2185,18 @@ _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".  */
+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 (;;)
@@ -1236,20 +2206,23 @@ 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);
+	      if (location)
+		*location = result->src_loc;
+	      goto out;
 	    }
 	}
       else
@@ -1257,7 +2230,10 @@ cpp_get_token (cpp_reader *pfile)
 	  _cpp_pop_context (pfile);
 	  if (pfile->state.in_directive)
 	    continue;
-	  return &pfile->avoid_paste;
+	  if (location)
+	    *location = pfile->avoid_paste.src_loc;
+	  result = &pfile->avoid_paste;
+	  goto out;
 	}
 
       if (pfile->state.in_directive && result->type == CPP_COMMENT)
@@ -1276,7 +2252,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;
@@ -1294,7 +2270,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
@@ -1311,12 +2288,16 @@ 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);
+	      if (location)
+		*location = result->src_loc;
+	      goto out;
 	    }
 	}
       else
@@ -1333,27 +2314,87 @@ cpp_get_token (cpp_reader *pfile)
       break;
     }
 
+  if (location)
+    *location = virt_loc;
+
+ out:
+  if (location != NULL)
+    {
+      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;
 }
 
@@ -1363,7 +2404,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;
 }
@@ -1420,10 +2461,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;
 	}
-- 
		Dodji

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

* Re: [PATCH 6/7] Kill pedantic warnings on system headers macros
  2011-09-17 22:34           ` Jason Merrill
@ 2011-09-18 18:59             ` Dodji Seketeli
  0 siblings, 0 replies; 135+ messages in thread
From: Dodji Seketeli @ 2011-09-18 18:59 UTC (permalink / raw)
  To: Jason Merrill; +Cc: gcc-patches, tromey, gdr, joseph, burnus, charlet, bonzini

Jason Merrill <jason@redhat.com> writes:

> On 09/16/2011 04:46 AM, Dodji Seketeli wrote:
>>  struct c_declspecs *
>> -finish_declspecs (struct c_declspecs *specs)
>> +finish_declspecs (struct c_declspecs *specs,
>> +                 location_t where)
>
> Let's call this first_token_loc, too.  And mention it in the function
> comment.
>
> OK with that change.

Thanks.  For the record, this is the updated patch.

From: Dodji Seketeli <dodji@redhat.com>
Date: Sat, 4 Dec 2010 18:35:47 +0100
Subject: [PATCH 6/7] Kill pedantic warnings on system headers macros

This patch leverages the virtual location infrastructure to avoid
emitting pedantic warnings related to macros defined in system headers
but expanded in normal TUs.

The point is to make diagnostic routines use virtual locations of
tokens instead of their spelling locations.  The diagnostic routines
in turn indirectly use linemap_location_in_system_header_p to know if
a given virtual location originated from a system header.

The patch has two main parts.

The libcpp part makes diagnostic routines called from the preprocessor
expression parsing and number conversion code use virtual locations.

The C FE part makes diagnostic routines called from the type
specifiers validation code use virtual locations.

This fixes the relevant examples presented in the comments of the bug
but I guess, as usual, libcpp and the FEs will need on-going care to
use more and more virtual locations of tokens instead of spelling
locations.

The combination of the patch and the previous ones boostrapped with
--enable-languages=all,ada and passed regression tests on
x86_64-unknown-linux-gnu.

libcpp/

	* include/cpplib.h (cpp_classify_number): Add a location parameter
	to the declaration.
	* expr.c (SYNTAX_ERROR_AT, SYNTAX_ERROR2_AT): New macros to emit
	syntax error using a virtual location.
	(cpp_classify_number): Add a virtual location parameter.  Use
	SYNTAX_ERROR_AT instead of SYNTAX_ERROR, cpp_error_with_line
	instead of cpp_error and cpp_warning_with_line instead of
	cpp_warning.  Pass the new virtual location parameter to those
	diagnostic routines.
	(eval_token): Add a virtual location parameter.  Pass it down to
	cpp_classify_number.  Use cpp_error_with_line instead of
	cpp_error, cpp_warning_with_line instead of cpp_warning, and pass
	the new virtual location parameter to these.
	(_cpp_parse_expr): Use cpp_get_token_with_location instead of
	cpp_get_token, to get the virtual location of the token. Use
	SYNTAX_ERROR2_AT instead of SYNTAX_ERROR2, cpp_error_with_line
	instead of cpp_error. Use the virtual location instead of the
	spelling location.
	* macro.c (maybe_adjust_loc_for_trad_cpp): Define new static
	function.
	(cpp_get_token_with_location): Use it.

gcc/c-family

	* c-lex.c (c_lex_with_flags): Adjust to pass the virtual location
	to cpp_classify_number.

gcc/

	* c-tree.h (finish_declspecs): Add a virtual location parameter.
	* c-decl.c (finish_declspecs): Add a virtual location parameter.
	Use error_at instead of error and pass down the virtual location
	to pewarn and error_at.
	(declspecs_add_type): Use in_system_header_at instead of
	in_system_header.
	* c-parser.c (c_parser_declaration_or_fndef): Pass virtual
	location of the relevant token to finish_declspecs.
	(c_parser_struct_declaration, c_parser_parameter_declaration):
	Likewise.
	(c_parser_type_name): Likewise.

gcc/testsuite/

	* gcc.dg/cpp/syshdr3.h: New test header.
	* gcc.dg/cpp/syshdr3.c: New test file.
	* gcc.dg/nofixed-point-2.c: Adjust to more precise location.
---
 gcc/c-decl.c                           |   22 +++--
 gcc/c-family/c-lex.c                   |    4 +-
 gcc/c-parser.c                         |   12 ++-
 gcc/c-tree.h                           |    2 +-
 gcc/testsuite/gcc.dg/cpp/syshdr3.c     |   16 +++
 gcc/testsuite/gcc.dg/cpp/syshdr3.h     |    7 ++
 gcc/testsuite/gcc.dg/nofixed-point-2.c |    6 +-
 libcpp/expr.c                          |  173 +++++++++++++++++++-------------
 libcpp/include/cpplib.h                |    3 +-
 9 files changed, 153 insertions(+), 92 deletions(-)
 create mode 100644 gcc/testsuite/gcc.dg/cpp/syshdr3.c
 create mode 100644 gcc/testsuite/gcc.dg/cpp/syshdr3.h

diff --git a/gcc/c-decl.c b/gcc/c-decl.c
index 5d4564a..cd1b276 100644
--- a/gcc/c-decl.c
+++ b/gcc/c-decl.c
@@ -8983,7 +8983,7 @@ declspecs_add_type (location_t loc, struct c_declspecs *specs,
 	      break;
 	    case RID_COMPLEX:
 	      dupe = specs->complex_p;
-	      if (!flag_isoc99 && !in_system_header)
+	      if (!flag_isoc99 && !in_system_header_at (loc))
 		pedwarn (loc, OPT_pedantic,
 			 "ISO C90 does not support complex types");
 	      if (specs->typespec_word == cts_void)
@@ -9508,10 +9508,12 @@ declspecs_add_attrs (struct c_declspecs *specs, tree attrs)
    specifiers with any other type specifier to determine the resulting
    type.  This is where ISO C checks on complex types are made, since
    "_Complex long" is a prefix of the valid ISO C type "_Complex long
-   double".  */
+   double".  FIRST_TOKEN_LOC is the location of the first token of
+   the first specifier of SPECS.  */
 
 struct c_declspecs *
-finish_declspecs (struct c_declspecs *specs)
+finish_declspecs (struct c_declspecs *specs,
+		  location_t first_token_loc)
 {
   /* If a type was specified as a whole, we have no modifiers and are
      done.  */
@@ -9536,9 +9538,11 @@ finish_declspecs (struct c_declspecs *specs)
     {
       if (specs->saturating_p)
 	{
-	  error ("%<_Sat%> is used without %<_Fract%> or %<_Accum%>");
+	  error_at (first_token_loc,
+		    "%<_Sat%> is used without %<_Fract%> or %<_Accum%>");
 	  if (!targetm.fixed_point_supported_p ())
-	    error ("fixed-point types not supported for this target");
+	    error_at (first_token_loc,
+		      "fixed-point types not supported for this target");
 	  specs->typespec_word = cts_fract;
 	}
       else if (specs->long_p || specs->short_p
@@ -9549,7 +9553,7 @@ finish_declspecs (struct c_declspecs *specs)
       else if (specs->complex_p)
 	{
 	  specs->typespec_word = cts_double;
-	  pedwarn (input_location, OPT_pedantic,
+	  pedwarn (first_token_loc, OPT_pedantic,
 		   "ISO C does not support plain %<complex%> meaning "
 		   "%<double complex%>");
 	}
@@ -9594,7 +9598,7 @@ finish_declspecs (struct c_declspecs *specs)
 	specs->type = char_type_node;
       if (specs->complex_p)
 	{
-	  pedwarn (input_location, OPT_pedantic,
+	  pedwarn (first_token_loc, OPT_pedantic,
 		   "ISO C does not support complex integer types");
 	  specs->type = build_complex_type (specs->type);
 	}
@@ -9607,7 +9611,7 @@ finish_declspecs (struct c_declspecs *specs)
 		     : int128_integer_type_node);
       if (specs->complex_p)
 	{
-	  pedwarn (input_location, OPT_pedantic,
+	  pedwarn (first_token_loc, OPT_pedantic,
 		   "ISO C does not support complex integer types");
 	  specs->type = build_complex_type (specs->type);
 	}
@@ -9633,7 +9637,7 @@ finish_declspecs (struct c_declspecs *specs)
 		       : integer_type_node);
       if (specs->complex_p)
 	{
-	  pedwarn (input_location, OPT_pedantic,
+	  pedwarn (first_token_loc, OPT_pedantic,
 		   "ISO C does not support complex integer types");
 	  specs->type = build_complex_type (specs->type);
 	}
diff --git a/gcc/c-family/c-lex.c b/gcc/c-family/c-lex.c
index be83b61..ca088a8 100644
--- a/gcc/c-family/c-lex.c
+++ b/gcc/c-family/c-lex.c
@@ -314,7 +314,7 @@ c_lex_with_flags (tree *value, location_t *loc, unsigned char *cpp_flags,
 
     case CPP_NUMBER:
       {
-	unsigned int flags = cpp_classify_number (parse_in, tok);
+	unsigned int flags = cpp_classify_number (parse_in, tok, *loc);
 
 	switch (flags & CPP_N_CATEGORY)
 	  {
@@ -397,7 +397,7 @@ c_lex_with_flags (tree *value, location_t *loc, unsigned char *cpp_flags,
 
 	*cpp_spell_token (parse_in, tok, name, true) = 0;
 
-	error ("stray %qs in program", name);
+	error_at (*loc, "stray %qs in program", name);
       }
 
       goto retry;
diff --git a/gcc/c-parser.c b/gcc/c-parser.c
index ff376df..3544d87 100644
--- a/gcc/c-parser.c
+++ b/gcc/c-parser.c
@@ -1479,7 +1479,7 @@ c_parser_declaration_or_fndef (c_parser *parser, bool fndef_ok,
       c_parser_skip_to_end_of_block_or_statement (parser);
       return;
     }
-  finish_declspecs (specs);
+  finish_declspecs (specs, here);
   if (c_parser_next_token_is (parser, CPP_SEMICOLON))
     {
       if (empty_ok)
@@ -2579,7 +2579,7 @@ c_parser_struct_declaration (c_parser *parser)
       c_parser_error (parser, "expected specifier-qualifier-list");
       return NULL_TREE;
     }
-  finish_declspecs (specs);
+  finish_declspecs (specs, decl_loc);
   if (c_parser_next_token_is (parser, CPP_SEMICOLON)
       || c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
     {
@@ -3242,6 +3242,8 @@ c_parser_parameter_declaration (c_parser *parser, tree attrs)
   tree prefix_attrs;
   tree postfix_attrs = NULL_TREE;
   bool dummy = false;
+  location_t first_token_loc = c_parser_peek_token (parser)->location;
+
   if (!c_parser_next_token_starts_declspecs (parser))
     {
       c_token *token = c_parser_peek_token (parser);
@@ -3268,7 +3270,7 @@ c_parser_parameter_declaration (c_parser *parser, tree attrs)
       attrs = NULL_TREE;
     }
   c_parser_declspecs (parser, specs, true, true, true, cla_nonabstract_decl);
-  finish_declspecs (specs);
+  finish_declspecs (specs, first_token_loc);
   pending_xref_error ();
   prefix_attrs = specs->attrs;
   specs->attrs = NULL_TREE;
@@ -3561,6 +3563,8 @@ c_parser_type_name (c_parser *parser)
   struct c_declarator *declarator;
   struct c_type_name *ret;
   bool dummy = false;
+  location_t here = c_parser_peek_token (parser)->location;
+
   c_parser_declspecs (parser, specs, false, true, true, cla_prefer_type);
   if (!specs->declspecs_seen_p)
     {
@@ -3570,7 +3574,7 @@ c_parser_type_name (c_parser *parser)
   if (specs->type != error_mark_node)
     {
       pending_xref_error ();
-      finish_declspecs (specs);
+      finish_declspecs (specs, here);
     }
   declarator = c_parser_declarator (parser,
 				    specs->typespec_kind != ctsk_none,
diff --git a/gcc/c-tree.h b/gcc/c-tree.h
index cda5d06..79231cb 100644
--- a/gcc/c-tree.h
+++ b/gcc/c-tree.h
@@ -494,7 +494,7 @@ extern struct c_declspecs *declspecs_add_scspec (struct c_declspecs *, tree);
 extern struct c_declspecs *declspecs_add_attrs (struct c_declspecs *, tree);
 extern struct c_declspecs *declspecs_add_addrspace (struct c_declspecs *,
 						    addr_space_t);
-extern struct c_declspecs *finish_declspecs (struct c_declspecs *);
+extern struct c_declspecs *finish_declspecs (struct c_declspecs *, location_t);
 
 /* in c-objc-common.c */
 extern bool c_objc_common_init (void);
diff --git a/gcc/testsuite/gcc.dg/cpp/syshdr3.c b/gcc/testsuite/gcc.dg/cpp/syshdr3.c
new file mode 100644
index 0000000..8850410
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/cpp/syshdr3.c
@@ -0,0 +1,16 @@
+/* Contributed by Dodji Seketeli <dodji@redhat.com> */
+/* Origin: PR preprocessor/7263 */
+/* { dg-options "-pedantic -std=c89 -ftrack-macro-expansion=1" } */
+/* { dg-do compile } */
+
+/* This tests the proprer suppression of warning coming from macro
+   defined in system headers and expanded in a non-system header
+   location.  */
+#include "syshdr3.h"
+
+_Complex c = _Complex_I + _Complex_I; /* These macros are defined in
+					 system header so we should
+					 have no warning here.  */
+U_LL u = ONE_ULL; /* Likewise here.  */
+
+unsigned long long v = 1ULL; /* { dg-warning "long long" } */
diff --git a/gcc/testsuite/gcc.dg/cpp/syshdr3.h b/gcc/testsuite/gcc.dg/cpp/syshdr3.h
new file mode 100644
index 0000000..e5d502a
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/cpp/syshdr3.h
@@ -0,0 +1,7 @@
+#pragma GCC system_header
+
+#define _Complex __complex__
+#define _Complex_I 1.0iF
+
+#define U_LL unsigned long long
+#define ONE_ULL 1ULL
diff --git a/gcc/testsuite/gcc.dg/nofixed-point-2.c b/gcc/testsuite/gcc.dg/nofixed-point-2.c
index 5b2f209..8442a19 100644
--- a/gcc/testsuite/gcc.dg/nofixed-point-2.c
+++ b/gcc/testsuite/gcc.dg/nofixed-point-2.c
@@ -20,10 +20,10 @@ f3 (void)
   return 0k;			/* { dg-error "not supported" "reject fixed-point" } */
 }
 
-_Sat
-f4 (void)			/* { dg-error "not supported" "reject fixed-point" } */
+_Sat                            /* { dg-error "not supported" "reject fixed-point" } */
+f4 (void)
 {
   return 0k;			/* { dg-error "not supported" "reject fixed-point" } */
 }
 
-/* { dg-error "is used without" "" { target *-*-* } 24 } */
+/* { dg-error "is used without" "" { target *-*-* } 23 } */
diff --git a/libcpp/expr.c b/libcpp/expr.c
index 3c36127..b7b4e1a 100644
--- a/libcpp/expr.c
+++ b/libcpp/expr.c
@@ -59,7 +59,7 @@ static cpp_num num_rshift (cpp_num, size_t, size_t);
 
 static cpp_num append_digit (cpp_num, int, int, size_t);
 static cpp_num parse_defined (cpp_reader *);
-static cpp_num eval_token (cpp_reader *, const cpp_token *);
+static cpp_num eval_token (cpp_reader *, const cpp_token *, source_location);
 static struct op *reduce (cpp_reader *, struct op *, enum cpp_ttype);
 static unsigned int interpret_float_suffix (const uchar *, size_t);
 static unsigned int interpret_int_suffix (const uchar *, size_t);
@@ -76,6 +76,12 @@ static void check_promotion (cpp_reader *, const struct op *);
 #define SYNTAX_ERROR2(msgid, arg) \
   do { cpp_error (pfile, CPP_DL_ERROR, msgid, arg); goto syntax_error; } \
   while(0)
+#define SYNTAX_ERROR_AT(loc, msgid) \
+  do { cpp_error_with_line (pfile, CPP_DL_ERROR, (loc), 0, msgid); goto syntax_error; } \
+  while(0)
+#define SYNTAX_ERROR2_AT(loc, msgid, arg)					\
+  do { cpp_error_with_line (pfile, CPP_DL_ERROR, (loc), 0, msgid, arg); goto syntax_error; } \
+  while(0)
 
 /* Subroutine of cpp_classify_number.  S points to a float suffix of
    length LEN, possibly zero.  Returns 0 for an invalid suffix, or a
@@ -223,7 +229,8 @@ interpret_int_suffix (const uchar *s, size_t len)
    floating point, or invalid), radix (decimal, octal, hexadecimal),
    and type suffixes.  */
 unsigned int
-cpp_classify_number (cpp_reader *pfile, const cpp_token *token)
+cpp_classify_number (cpp_reader *pfile, const cpp_token *token,
+		     source_location virtual_location)
 {
   const uchar *str = token->val.str.text;
   const uchar *limit;
@@ -279,7 +286,8 @@ cpp_classify_number (cpp_reader *pfile, const cpp_token *token)
 	  if (float_flag == NOT_FLOAT)
 	    float_flag = AFTER_POINT;
 	  else
-	    SYNTAX_ERROR ("too many decimal points in number");
+	    SYNTAX_ERROR_AT (virtual_location,
+			     "too many decimal points in number");
 	}
       else if ((radix <= 10 && (c == 'e' || c == 'E'))
 	       || (radix == 16 && (c == 'p' || c == 'P')))
@@ -307,8 +315,8 @@ cpp_classify_number (cpp_reader *pfile, const cpp_token *token)
 	    radix = 10;
 
 	  if (CPP_PEDANTIC (pfile))
-	    cpp_error (pfile, CPP_DL_PEDWARN,
-		       "fixed-point constants are a GCC extension");
+	    cpp_error_with_line (pfile, CPP_DL_PEDWARN, virtual_location, 0,
+				 "fixed-point constants are a GCC extension");
 	  goto syntax_ok;
 	}
       else
@@ -321,26 +329,29 @@ cpp_classify_number (cpp_reader *pfile, const cpp_token *token)
   if (max_digit >= radix)
     {
       if (radix == 2)
-	SYNTAX_ERROR2 ("invalid digit \"%c\" in binary constant", '0' + max_digit);
+	SYNTAX_ERROR2_AT (virtual_location,
+			  "invalid digit \"%c\" in binary constant", '0' + max_digit);
       else
-	SYNTAX_ERROR2 ("invalid digit \"%c\" in octal constant", '0' + max_digit);
+	SYNTAX_ERROR2_AT (virtual_location,
+			  "invalid digit \"%c\" in octal constant", '0' + max_digit);
     }
 
   if (float_flag != NOT_FLOAT)
     {
       if (radix == 2)
 	{
-	  cpp_error (pfile, CPP_DL_ERROR,
-		     "invalid prefix \"0b\" for floating constant");
+	  cpp_error_with_line (pfile, CPP_DL_ERROR, virtual_location, 0,
+			       "invalid prefix \"0b\" for floating constant");
 	  return CPP_N_INVALID;
 	}
 
       if (radix == 16 && !seen_digit)
-	SYNTAX_ERROR ("no digits in hexadecimal floating constant");
+	SYNTAX_ERROR_AT (virtual_location,
+			 "no digits in hexadecimal floating constant");
 
       if (radix == 16 && CPP_PEDANTIC (pfile) && !CPP_OPTION (pfile, c99))
-	cpp_error (pfile, CPP_DL_PEDWARN,
-		   "use of C99 hexadecimal floating constant");
+	cpp_error_with_line (pfile, CPP_DL_PEDWARN, virtual_location, 0,
+			     "use of C99 hexadecimal floating constant");
 
       if (float_flag == AFTER_EXPON)
 	{
@@ -349,21 +360,22 @@ cpp_classify_number (cpp_reader *pfile, const cpp_token *token)
 
 	  /* Exponent is decimal, even if string is a hex float.  */
 	  if (!ISDIGIT (*str))
-	    SYNTAX_ERROR ("exponent has no digits");
+	    SYNTAX_ERROR_AT (virtual_location, "exponent has no digits");
 
 	  do
 	    str++;
 	  while (ISDIGIT (*str));
 	}
       else if (radix == 16)
-	SYNTAX_ERROR ("hexadecimal floating constants require an exponent");
+	SYNTAX_ERROR_AT (virtual_location,
+			 "hexadecimal floating constants require an exponent");
 
       result = interpret_float_suffix (str, limit - str);
       if (result == 0)
 	{
-	  cpp_error (pfile, CPP_DL_ERROR,
-		     "invalid suffix \"%.*s\" on floating constant",
-		     (int) (limit - str), str);
+	  cpp_error_with_line (pfile, CPP_DL_ERROR, virtual_location, 0,
+			       "invalid suffix \"%.*s\" on floating constant",
+			       (int) (limit - str), str);
 	  return CPP_N_INVALID;
 	}
 
@@ -371,33 +383,33 @@ cpp_classify_number (cpp_reader *pfile, const cpp_token *token)
       if (limit != str
 	  && CPP_WTRADITIONAL (pfile)
 	  && ! cpp_sys_macro_p (pfile))
-	cpp_warning (pfile, CPP_W_TRADITIONAL,
-		     "traditional C rejects the \"%.*s\" suffix",
-		     (int) (limit - str), str);
+	cpp_warning_with_line (pfile, CPP_W_TRADITIONAL, virtual_location, 0,
+			       "traditional C rejects the \"%.*s\" suffix",
+			       (int) (limit - str), str);
 
       /* A suffix for double is a GCC extension via decimal float support.
 	 If the suffix also specifies an imaginary value we'll catch that
 	 later.  */
       if ((result == CPP_N_MEDIUM) && CPP_PEDANTIC (pfile))
-	cpp_error (pfile, CPP_DL_PEDWARN,
-		   "suffix for double constant is a GCC extension");
+	cpp_error_with_line (pfile, CPP_DL_PEDWARN, virtual_location, 0,
+			     "suffix for double constant is a GCC extension");
 
       /* Radix must be 10 for decimal floats.  */
       if ((result & CPP_N_DFLOAT) && radix != 10)
         {
-          cpp_error (pfile, CPP_DL_ERROR,
-                     "invalid suffix \"%.*s\" with hexadecimal floating constant",
-                     (int) (limit - str), str);
+          cpp_error_with_line (pfile, CPP_DL_ERROR, virtual_location, 0,
+			       "invalid suffix \"%.*s\" with hexadecimal floating constant",
+			       (int) (limit - str), str);
           return CPP_N_INVALID;
         }
 
       if ((result & (CPP_N_FRACT | CPP_N_ACCUM)) && CPP_PEDANTIC (pfile))
-	cpp_error (pfile, CPP_DL_PEDWARN,
-		   "fixed-point constants are a GCC extension");
+	cpp_error_with_line (pfile, CPP_DL_PEDWARN, virtual_location, 0,
+			     "fixed-point constants are a GCC extension");
 
       if ((result & CPP_N_DFLOAT) && CPP_PEDANTIC (pfile))
-	cpp_error (pfile, CPP_DL_PEDWARN,
-		   "decimal float constants are a GCC extension");
+	cpp_error_with_line (pfile, CPP_DL_PEDWARN, virtual_location, 0,
+			     "decimal float constants are a GCC extension");
 
       result |= CPP_N_FLOATING;
     }
@@ -406,9 +418,9 @@ cpp_classify_number (cpp_reader *pfile, const cpp_token *token)
       result = interpret_int_suffix (str, limit - str);
       if (result == 0)
 	{
-	  cpp_error (pfile, CPP_DL_ERROR,
-		     "invalid suffix \"%.*s\" on integer constant",
-		     (int) (limit - str), str);
+	  cpp_error_with_line (pfile, CPP_DL_ERROR, virtual_location, 0,
+			       "invalid suffix \"%.*s\" on integer constant",
+			       (int) (limit - str), str);
 	  return CPP_N_INVALID;
 	}
 
@@ -421,9 +433,10 @@ cpp_classify_number (cpp_reader *pfile, const cpp_token *token)
 		       && CPP_OPTION (pfile, cpp_warn_long_long);
 
 	  if (u_or_i || large)
-	    cpp_warning (pfile, large ? CPP_W_LONG_LONG : CPP_W_TRADITIONAL,
-		         "traditional C rejects the \"%.*s\" suffix",
-		         (int) (limit - str), str);
+	    cpp_warning_with_line (pfile, large ? CPP_W_LONG_LONG : CPP_W_TRADITIONAL,
+				   virtual_location, 0,
+				   "traditional C rejects the \"%.*s\" suffix",
+				   (int) (limit - str), str);
 	}
 
       if ((result & CPP_N_WIDTH) == CPP_N_LARGE
@@ -434,9 +447,11 @@ cpp_classify_number (cpp_reader *pfile, const cpp_token *token)
 		                : N_("use of C99 long long integer constant");
 
 	  if (CPP_OPTION (pfile, c99))
-            cpp_warning (pfile, CPP_W_LONG_LONG, message);
+            cpp_warning_with_line (pfile, CPP_W_LONG_LONG, virtual_location,
+				   0, message);
           else
-            cpp_pedwarning (pfile, CPP_W_LONG_LONG, message);
+            cpp_pedwarning_with_line (pfile, CPP_W_LONG_LONG,
+				      virtual_location, 0, message);
         }
 
       result |= CPP_N_INTEGER;
@@ -444,11 +459,11 @@ cpp_classify_number (cpp_reader *pfile, const cpp_token *token)
 
  syntax_ok:
   if ((result & CPP_N_IMAGINARY) && CPP_PEDANTIC (pfile))
-    cpp_error (pfile, CPP_DL_PEDWARN,
-	       "imaginary constants are a GCC extension");
+    cpp_error_with_line (pfile, CPP_DL_PEDWARN, virtual_location, 0,
+			 "imaginary constants are a GCC extension");
   if (radix == 2 && CPP_PEDANTIC (pfile))
-    cpp_error (pfile, CPP_DL_PEDWARN,
-	       "binary constants are a GCC extension");
+    cpp_error_with_line (pfile, CPP_DL_PEDWARN, virtual_location, 0,
+			 "binary constants are a GCC extension");
 
   if (radix == 10)
     result |= CPP_N_DECIMAL;
@@ -736,7 +751,8 @@ parse_defined (cpp_reader *pfile)
    number or character constant, or the result of the "defined" or "#"
    operators).  */
 static cpp_num
-eval_token (cpp_reader *pfile, const cpp_token *token)
+eval_token (cpp_reader *pfile, const cpp_token *token,
+	    source_location virtual_location)
 {
   cpp_num result;
   unsigned int temp;
@@ -748,18 +764,18 @@ eval_token (cpp_reader *pfile, const cpp_token *token)
   switch (token->type)
     {
     case CPP_NUMBER:
-      temp = cpp_classify_number (pfile, token);
+      temp = cpp_classify_number (pfile, token, virtual_location);
       switch (temp & CPP_N_CATEGORY)
 	{
 	case CPP_N_FLOATING:
-	  cpp_error (pfile, CPP_DL_ERROR,
-		     "floating constant in preprocessor expression");
+	  cpp_error_with_line (pfile, CPP_DL_ERROR, virtual_location, 0,
+			       "floating constant in preprocessor expression");
 	  break;
 	case CPP_N_INTEGER:
 	  if (!(temp & CPP_N_IMAGINARY))
 	    return cpp_interpret_integer (pfile, token, temp);
-	  cpp_error (pfile, CPP_DL_ERROR,
-		     "imaginary number in preprocessor expression");
+	  cpp_error_with_line (pfile, CPP_DL_ERROR, virtual_location, 0,
+			       "imaginary number in preprocessor expression");
 	  break;
 
 	case CPP_N_INVALID:
@@ -806,8 +822,9 @@ eval_token (cpp_reader *pfile, const cpp_token *token)
 	  result.high = 0;
 	  result.low = 0;
 	  if (CPP_OPTION (pfile, warn_undef) && !pfile->state.skip_eval)
-	    cpp_warning (pfile, CPP_W_UNDEF, "\"%s\" is not defined",
-		         NODE_NAME (token->val.node.node));
+	    cpp_warning_with_line (pfile, CPP_W_UNDEF, virtual_location, 0,
+				   "\"%s\" is not defined",
+				   NODE_NAME (token->val.node.node));
 	}
       break;
 
@@ -817,11 +834,12 @@ eval_token (cpp_reader *pfile, const cpp_token *token)
 	  /* A pedantic warning takes precedence over a deprecated
 	     warning here.  */
 	  if (CPP_PEDANTIC (pfile))
-	    cpp_error (pfile, CPP_DL_PEDWARN,
-		       "assertions are a GCC extension");
+	    cpp_error_with_line (pfile, CPP_DL_PEDWARN,
+				 virtual_location, 0,
+				 "assertions are a GCC extension");
 	  else if (CPP_OPTION (pfile, cpp_warn_deprecated))
-	    cpp_warning (pfile, CPP_W_DEPRECATED,
-		         "assertions are a deprecated extension");
+	    cpp_warning_with_line (pfile, CPP_W_DEPRECATED, virtual_location, 0,
+				   "assertions are a deprecated extension");
 	}
       _cpp_test_assertion (pfile, &temp);
       result.high = 0;
@@ -923,6 +941,7 @@ _cpp_parse_expr (cpp_reader *pfile, bool is_if)
   struct op *top = pfile->op_stack;
   unsigned int lex_count;
   bool saw_leading_not, want_value = true;
+  source_location virtual_location = 0;
 
   pfile->state.skip_eval = 0;
 
@@ -939,9 +958,10 @@ _cpp_parse_expr (cpp_reader *pfile, bool is_if)
       struct op op;
 
       lex_count++;
-      op.token = cpp_get_token (pfile);
+
+      op.token = cpp_get_token_with_location (pfile, &virtual_location);
       op.op = op.token->type;
-      op.loc = op.token->src_loc;
+      op.loc = virtual_location;
 
       switch (op.op)
 	{
@@ -954,10 +974,11 @@ _cpp_parse_expr (cpp_reader *pfile, bool is_if)
 	case CPP_NAME:
 	case CPP_HASH:
 	  if (!want_value)
-	    SYNTAX_ERROR2 ("missing binary operator before token \"%s\"",
-			   cpp_token_as_text (pfile, op.token));
+	    SYNTAX_ERROR2_AT (virtual_location,
+			      "missing binary operator before token \"%s\"",
+			      cpp_token_as_text (pfile, op.token));
 	  want_value = false;
-	  top->value = eval_token (pfile, op.token);
+	  top->value = eval_token (pfile, op.token, virtual_location);
 	  continue;
 
 	case CPP_NOT:
@@ -974,8 +995,9 @@ _cpp_parse_expr (cpp_reader *pfile, bool is_if)
 
 	default:
 	  if ((int) op.op <= (int) CPP_EQ || (int) op.op >= (int) CPP_PLUS_EQ)
-	    SYNTAX_ERROR2 ("token \"%s\" is not valid in preprocessor expressions",
-			   cpp_token_as_text (pfile, op.token));
+	    SYNTAX_ERROR2_AT (op.loc,
+			      "token \"%s\" is not valid in preprocessor expressions",
+			      cpp_token_as_text (pfile, op.token));
 	  break;
 	}
 
@@ -983,27 +1005,32 @@ _cpp_parse_expr (cpp_reader *pfile, bool is_if)
       if (optab[op.op].flags & NO_L_OPERAND)
 	{
 	  if (!want_value)
-	    SYNTAX_ERROR2 ("missing binary operator before token \"%s\"",
-			   cpp_token_as_text (pfile, op.token));
+	    SYNTAX_ERROR2_AT (virtual_location,
+			      "missing binary operator before token \"%s\"",
+			      cpp_token_as_text (pfile, op.token));
 	}
       else if (want_value)
 	{
 	  /* We want a number (or expression) and haven't got one.
 	     Try to emit a specific diagnostic.  */
 	  if (op.op == CPP_CLOSE_PAREN && top->op == CPP_OPEN_PAREN)
-	    SYNTAX_ERROR ("missing expression between '(' and ')'");
+	    SYNTAX_ERROR_AT (op.loc,
+			     "missing expression between '(' and ')'");
 
 	  if (op.op == CPP_EOF && top->op == CPP_EOF)
- 	    SYNTAX_ERROR2 ("%s with no expression", is_if ? "#if" : "#elif");
+ 	    SYNTAX_ERROR2_AT (op.loc,
+			      "%s with no expression", is_if ? "#if" : "#elif");
 
  	  if (top->op != CPP_EOF && top->op != CPP_OPEN_PAREN)
- 	    SYNTAX_ERROR2 ("operator '%s' has no right operand",
- 			   cpp_token_as_text (pfile, top->token));
+ 	    SYNTAX_ERROR2_AT (op.loc,
+			      "operator '%s' has no right operand",
+			      cpp_token_as_text (pfile, top->token));
 	  else if (op.op == CPP_CLOSE_PAREN || op.op == CPP_EOF)
 	    /* Complain about missing paren during reduction.  */;
 	  else
-	    SYNTAX_ERROR2 ("operator '%s' has no left operand",
-			   cpp_token_as_text (pfile, op.token));
+	    SYNTAX_ERROR2_AT (op.loc,
+			      "operator '%s' has no left operand",
+			      cpp_token_as_text (pfile, op.token));
 	}
 
       top = reduce (pfile, top, op.op);
@@ -1028,7 +1055,8 @@ _cpp_parse_expr (cpp_reader *pfile, bool is_if)
 	  break;
 	case CPP_COLON:
 	  if (top->op != CPP_QUERY)
-	    SYNTAX_ERROR (" ':' without preceding '?'");
+	    SYNTAX_ERROR_AT (op.loc,
+			     " ':' without preceding '?'");
 	  if (!num_zerop (top[-1].value)) /* Was '?' condition true?  */
 	    pfile->state.skip_eval++;
 	  else
@@ -1045,7 +1073,7 @@ _cpp_parse_expr (cpp_reader *pfile, bool is_if)
 
       top->op = op.op;
       top->token = op.token;
-      top->loc = op.token->src_loc;
+      top->loc = op.loc;
     }
 
   /* The controlling macro expression is only valid if we called lex 3
@@ -1056,8 +1084,9 @@ _cpp_parse_expr (cpp_reader *pfile, bool is_if)
 
   if (top != pfile->op_stack)
     {
-      cpp_error (pfile, CPP_DL_ICE, "unbalanced stack in %s",
-		 is_if ? "#if" : "#elif");
+      cpp_error_with_line (pfile, CPP_DL_ICE, top->loc, 0,
+			   "unbalanced stack in %s",
+			   is_if ? "#if" : "#elif");
     syntax_error:
       return false;  /* Return false on syntax error.  */
     }
diff --git a/libcpp/include/cpplib.h b/libcpp/include/cpplib.h
index 825bf2f..b88c7d7 100644
--- a/libcpp/include/cpplib.h
+++ b/libcpp/include/cpplib.h
@@ -831,7 +831,8 @@ struct cpp_num
 
 /* Classify a CPP_NUMBER token.  The return value is a combination of
    the flags from the above sets.  */
-extern unsigned cpp_classify_number (cpp_reader *, const cpp_token *);
+extern unsigned cpp_classify_number (cpp_reader *, const cpp_token *,
+				     source_location);
 
 /* Evaluate a token classified as category CPP_N_INTEGER.  */
 extern cpp_num cpp_interpret_integer (cpp_reader *, const cpp_token *,
-- 
		Dodji

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

* Re: [PATCH 7/7] Reduce memory waste due to non-power-of-2 allocs
  2011-09-17 22:22           ` Jason Merrill
@ 2011-09-18 22:30             ` Dodji Seketeli
  0 siblings, 0 replies; 135+ messages in thread
From: Dodji Seketeli @ 2011-09-18 22:30 UTC (permalink / raw)
  To: Jason Merrill; +Cc: gcc-patches, tromey, Laurynas Biveinis

Jason Merrill <jason@redhat.com> writes:

> On 09/17/2011 07:08 AM, Dodji Seketeli wrote:
>> OK, so the patch below extracts a public ggc_alloced_size_for_request
>> function from the different implementations of the ggc allocator's
>> interface, and lets new_linemap use that.
>
> Maybe "ggc_round_alloc_size"?

OK, updated the patch below accordingly.

>  OK with that change if nobody else has comments this week.

Thanks.

Below is the updated patch.

From: Dodji Seketeli <dodji@redhat.com>
Date: Tue, 17 May 2011 16:48:01 +0200
Subject: [PATCH 7/7] Reduce memory waste due to non-power-of-2 allocs

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 of
('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.

Tested on x86_64-unknown-linux-gnu against trunk for each allocator.

libcpp/

	* 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/

	* 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.
---
 gcc/ggc-none.c            |    9 +++++++
 gcc/ggc-page.c            |   53 +++++++++++++++++++++++++++++++++++---------
 gcc/ggc-zone.c            |   27 ++++++++++++++++------
 gcc/ggc.h                 |    2 +
 gcc/toplev.c              |    1 +
 libcpp/include/line-map.h |    8 ++++++
 libcpp/line-map.c         |   39 ++++++++++++++++++++++++++++-----
 7 files changed, 114 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 2f90261..7b4bd16 100644
--- a/gcc/toplev.c
+++ b/gcc/toplev.c
@@ -1177,6 +1177,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 3989c89..62b288f 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".
    
@@ -298,6 +302,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 09dffcb..935024c 100644
--- a/libcpp/line-map.c
+++ b/libcpp/line-map.c
@@ -77,16 +77,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,
-- 
		Dodji

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

* Re: [PATCH 7/7] Reduce memory waste due to non-power-of-2 allocs
  2011-09-17 13:41         ` Dodji Seketeli
  2011-09-17 22:22           ` Jason Merrill
@ 2011-09-19  6:51           ` Laurynas Biveinis
  1 sibling, 0 replies; 135+ messages in thread
From: Laurynas Biveinis @ 2011-09-19  6:51 UTC (permalink / raw)
  To: Dodji Seketeli; +Cc: Jason Merrill, gcc-patches, tromey

2011/9/17 Dodji Seketeli <dodji@redhat.com>:
> OK, so the patch below extracts a public ggc_alloced_size_for_request
> function from the different implementations of the ggc allocator's
> interface, and lets new_linemap use that.
> libcpp/
>
>        * 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/
>
>        * ggc.h (ggc_alloced_size_for_request): Declare new public entry
>        point.
>        * ggc-none.c (ggc_alloced_size_for_request): New public stub
>        function.
>        * ggc-page.c (ggc_alloced_size_order_for_request): New static
>        function.  Factorized from ggc_internal_alloc_stat.
>        (ggc_alloced_size_for_request): 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_alloced_size_for_request): New public function
>        extracted from ggc_internal_alloc_zone_stat.
>        (ggc_internal_alloc_zone_stat): Use ggc_alloced_size_for_request.
>        * toplev.c (general_init): Initialize
>        line_table->alloced_size_for_request.

For the record, the patch is fine with me. (I cannot approve it
though, but you already got the approval)

Thanks,
-- 
Laurynas

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

* Re: [PATCH 3/7] Emit macro expansion related diagnostics
  2011-09-17 21:27             ` Jason Merrill
@ 2011-09-19 14:37               ` Dodji Seketeli
  2011-09-19 19:47                 ` Jason Merrill
  0 siblings, 1 reply; 135+ messages in thread
From: Dodji Seketeli @ 2011-09-19 14:37 UTC (permalink / raw)
  To: Jason Merrill; +Cc: gcc-patches, tromey, gdr, joseph, burnus, charlet, bonzini

Jason Merrill <jason@redhat.com> writes:

> On 09/16/2011 03:55 AM, Dodji Seketeli wrote:
> > +    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 sixth 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.
> 
> No UTF-8 in the code, please.

Fixed, sorry.  I actually copy/pasted the output from my terminal
after running gcc on the test case, and the terminal is in UTF-8.  And
so is my editor that runs in that terminal.

> 
> Do you mean eighth rather than sixth?

Yes I do.  Fixed.  Thanks for catching that.

> 
> > +      /* If the token at RESOLVED_LOCATION [at macro definition point]
> > +         is itself inside an expanded macro then we keep unwinding the
> > +         trace by reaching the "parent macro" that got expanded inside
> > +         the definition of the macro of MAP...  */
> 
> This doesn't make sense to me; a macro definition can't be inside an
> expanded macro.

Inside the definition of a macro M, you can have another macro M'.
And M' is going to be eventually expanded into a token FOO.

So, to paraphrase what I said earlier, FOO [which is at a point in the
definition of the macro M] is itself inside the expanded macro M'.

>  Can you describe this situation better?

Yes, sorry for the confusion.  I am trying to provide a better
explanation in the updated patch below.

> 
> > +  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 (resolved_map))
> 
> Should the SYSP check use "map" rather than "resolved_map"?

Using map is clearer (and I changed it to that) but I think
resolved_map works too.

From: Dodji Seketeli <dodji@redhat.com>
Date: Sat, 4 Dec 2010 16:31:35 +0100
Subject: [PATCH 3/7] Emit macro expansion related diagnostics

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

The combination of this patch and the previous ones boostrapped with
--enable-languages=all,ada and passed regression tests on
x86_64-unknown-linux-gnu.

gcc/
	* 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/

	* 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/

	* 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/Makefile.in                                 |    3 +-
 gcc/cp/error.c                                  |    5 +-
 gcc/diagnostic.c                                |   13 +-
 gcc/diagnostic.h                                |    2 +-
 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                           |  212 ++++++++++++++++++++++-
 gcc/tree-diagnostic.h                           |    3 +-
 13 files changed, 335 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 92016f2..d26c682 100644
--- a/gcc/Makefile.in
+++ b/gcc/Makefile.in
@@ -2796,7 +2796,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 598ddf1..8fa163f 100644
--- a/gcc/cp/error.c
+++ b/gcc/cp/error.c
@@ -2767,7 +2767,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);
@@ -2777,8 +2777,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..0344937 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_PARM_REPLACEMENT_POINT,
+			    &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 de0a58a..5f63b69 100644
--- a/gcc/toplev.c
+++ b/gcc/toplev.c
@@ -1132,6 +1132,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..5a7e6c2 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,212 @@ 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 heighth 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, *resolved_map;
+  source_location resolved_location;
+  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
+         *definition* of the macro of MAP, that got expanded at WHERE.
+         This is basically how we go "down" in the trace of macro
+         expansions that led to WHERE.  */
+      resolved_location =
+        linemap_macro_map_loc_to_def_point (map, where, false);
+      resolved_map = linemap_lookup (line_table, resolved_location);
+
+      /* In the definition of a macro M, we can have another macro M'.
+	 M' is eventually going to be expanded into a token FOO, which
+	 location is RESOLVED_LOCATION.  MAP would be the map of M.
+
+	 So if we are in this case, i.e If FOO is itself inside an
+         expanded macro M' then we keep unwinding the trace to reach
+         the "parent macro" M'.  RESOLVED_MAP would then be the map of
+         M'.  */
+      if (linemap_macro_expansion_map_p (resolved_map))
+        {
+          where = resolved_location;
+          map = resolved_map;
+        }
+      else
+        {
+          /* Otherwise, let's consider the location of the expansion
+             point of the macro of MAP.  Keep in mind that MAP is a
+             macro expansion map.  To get a "normal map" (i.e a non
+             macro expansion map) and be done with the unwinding, we
+             must either consider the location of the expansion point
+             of the macro or the location of the token inside the
+             macro definition that got expanded to WHERE.  */
+          where =
+            linemap_macro_map_loc_to_exp_point (map, where);
+          map = linemap_lookup (line_table, where);
+        }
+    } 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_PARM_REPLACEMENT_POINT, 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_PARM_REPLACEMENT_POINT, 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 */
-- 
		Dodji

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

* Re: [PATCH 3/7] Emit macro expansion related diagnostics
  2011-09-19 14:37               ` Dodji Seketeli
@ 2011-09-19 19:47                 ` Jason Merrill
  2011-09-19 21:27                   ` Jason Merrill
  2011-09-20  0:08                   ` Dodji Seketeli
  0 siblings, 2 replies; 135+ messages in thread
From: Jason Merrill @ 2011-09-19 19:47 UTC (permalink / raw)
  To: Dodji Seketeli; +Cc: gcc-patches, tromey, gdr, joseph, burnus, charlet, bonzini

On 09/19/2011 09:58 AM, Dodji Seketeli wrote:
> +   The part that goes from the third to the heighth line of this

An extra 'h' snuck in there. :)

> Inside the definition of a macro M, you can have another macro M'.
> And M' is going to be eventually expanded into a token FOO.
>
> So, to paraphrase what I said earlier, FOO [which is at a point in the
> definition of the macro M] is itself inside the expanded macro M'.

So what we're dealing with here is the token FOO.  We start with its 
fully-expanded location, and then step out to see where it was expanded 
from.  If that location is still within a macro expansion, we step out 
again until we are at the point in the source that originally triggered 
a macro expansion.  Right?

I don't understand how that unwinding operation maps onto a function 
called linemap_macro_map_loc_to_def_point, and why we need to use both 
that function and linemap_macro_map_loc_to_exp_point here.

I'm afraid this is leading me to question the basic model again.

> +      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 "1.0" has the locations 2(OPRD1), 5(A), 8(A), 13(1.0).  "<<" has 
the locations 2(OPRT), 5(<<), 8(SHIFTL), 13(MULT).  Each token has 4 
locations, one for each of the macros involved plus one for the original 
expansion location.  So it seems like we ought to be able to get away 
with only storing one location per token in a macro expansion map.  Am I 
missing something?

Jason

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

* Re: [PATCH 3/7] Emit macro expansion related diagnostics
  2011-09-19 19:47                 ` Jason Merrill
@ 2011-09-19 21:27                   ` Jason Merrill
  2011-09-20  8:47                     ` Dodji Seketeli
  2011-09-20  0:08                   ` Dodji Seketeli
  1 sibling, 1 reply; 135+ messages in thread
From: Jason Merrill @ 2011-09-19 21:27 UTC (permalink / raw)
  To: Dodji Seketeli; +Cc: gcc-patches, tromey, gdr, joseph, burnus, charlet, bonzini

On 09/19/2011 02:29 PM, Jason Merrill wrote:
> expansion location. So it seems like we ought to be able to get away
> with only storing one location per token in a macro expansion map. Am I
> missing something?

I notice that here:

> +        /* 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_PARM_REPLACEMENT_POINT, 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_PARM_REPLACEMENT_POINT, NULL);

You're using LRK_MACRO_PARM_REPLACEMENT_POINT in both places for 
printing (and thus the second of the two token locations), whereas you 
used the first location in the unwinding process.

It is sounding to me like the first location (xI) gets you the next 
virtual location in the unwinding process, whereas the second location 
(yI) gets you the spelling location of the token in the definition of a 
macro.  Certainly all the calls to tokens_buff_add_token pass 
src->src_loc for the second.  So why don't we look up the second 
location in the macro definition when we need it rather than store a 
copy in the map?

Jason

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

* Re: [PATCH 2/7] Generate virtual locations for tokens
  2011-09-18 13:44               ` Dodji Seketeli
@ 2011-09-19 22:31                 ` Jason Merrill
  2011-09-21 14:55                   ` Dodji Seketeli
  0 siblings, 1 reply; 135+ messages in thread
From: Jason Merrill @ 2011-09-19 22:31 UTC (permalink / raw)
  To: Dodji Seketeli; +Cc: gcc-patches, tromey, gdr, joseph, burnus, charlet, bonzini

On 09/18/2011 06:47 AM, Dodji Seketeli wrote:
> +    cpp_hashnode *macro;
> +
> +  }c;

Extra blank line, missing space.

>>> +/* Appends a token to the end of the token buffer BUFFER.  Note that
>>> +   this function doesn't enlarge BUFFER; it overwrite the last memory
>>> +   location of BUFFER that holds a token.
>> That doesn't sound like appending.
> I have changed the name into tokens_buff_add_token and I updated the
> comments accordingly.

Hmm, it seems that what the callers are looking for is appending, so the 
name wasn't really the problem (though the new name is fine too).  And 
the new comment:

> +/* 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 then overwrites the last memory location
> +   of BUFFER that holds a token.

clarifies the text that was confusing me so that it does sound more like 
appending except when the buffer is full.  But clobbering the last token 
when the buffer is full sounds like it's unlikely to be what the caller 
wants; should we abort instead?

> +  size_t expanded_capacity;     /* total size of expanded array.  */
> +  source_location *virt_locs;  /* Where virtual locations for
> +                                  unexpanded tokens are stored.  */
> +  unsigned virt_locs_capacity; /* Total size of virtual locations
> +                                  array.  */
> +  source_location *expanded_virt_locs; /* Where virtual locations for
> +                                         expanded tokens are
> +                                         stored.  */

Why do we need the capacity fields, when we didn't before?

> +         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);

This looks a lot like cloning tokens.  I thought you were storing 
virtual locations separately so you wouldn't have to do that?

Jason

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

* Re: [PATCH 3/7] Emit macro expansion related diagnostics
  2011-09-19 19:47                 ` Jason Merrill
  2011-09-19 21:27                   ` Jason Merrill
@ 2011-09-20  0:08                   ` Dodji Seketeli
  1 sibling, 0 replies; 135+ messages in thread
From: Dodji Seketeli @ 2011-09-20  0:08 UTC (permalink / raw)
  To: Jason Merrill; +Cc: gcc-patches, tromey, gdr, joseph, burnus, charlet, bonzini

Jason Merrill <jason@redhat.com> writes:

> On 09/19/2011 09:58 AM, Dodji Seketeli wrote:
> > +   The part that goes from the third to the heighth line of this
> 
> An extra 'h' snuck in there. :)

Oops, fixed in my local copy, sorry.

> 
> > Inside the definition of a macro M, you can have another macro M'.
> > And M' is going to be eventually expanded into a token FOO.
> >
> > So, to paraphrase what I said earlier, FOO [which is at a point in the
> > definition of the macro M] is itself inside the expanded macro M'.
> 
> So what we're dealing with here is the token FOO.  We start with its
> fully-expanded location, and then step out to see where it was
> expanded from.  If that location is still within a macro expansion, we
> step out again until we are at the point in the source that originally
> triggered a macro expansion.  Right?

Right.

> I don't understand how that unwinding operation maps onto a function
> called linemap_macro_map_loc_to_def_point,

In the example I used in the comment of that function, suppose that
during 'stepping out', as you put it, we don't deal with the place in
the source where '<<' appears in the definition of a macro.  We'd then
only deal with the expansion points of the macros involved.  That
would mean that we would only record in the trace the following
locations (and be able to only display this trace):

1/

    test.c:5:14: error: invalid operands to binary << (have 'double' and 'int')
    test.c:5:3: note: expanded from here
    test.c:8:3: note: expanded from here
    test.c:13:3: note: expanded from here

But I also want to display where "<<" appears in the definition of the
relevant macro ...

> and why we need to use both that function and
> linemap_macro_map_loc_to_exp_point here.

... so that I can have a trace that shows the locations also in the
context of the definitions of the macros:

2/

    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

> 
> I'm afraid this is leading me to question the basic model again.
> 
> > +      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 "1.0" has the locations 2(OPRD1), 5(A), 8(A), 13(1.0).  "<<"
> has the locations 2(OPRT), 5(<<), 8(SHIFTL), 13(MULT).  Each token
> has 4 locations, one for each of the macros involved plus one for
> the original expansion location.  So it seems like we ought to be
> able to get away with only storing one location per token in a macro
> expansion map.

This is essentially what we do, for tokens of macro replacement list
that are not macro parameters.

In a function-like macro, tokens of macro parameters are replaced by
the tokens of the arguments; if we just store the spelling location of
a given parameter (and forget about the location of the matching
argument as you are suggesting), getting the location of that argument
at virtual location resolution time can be difficult.

In your example, suppose we didn't store the location of "<<" inside
the map for OPERATE, and that we only stored the location (2:9) of
OPRT, as you suggest.  How, to generate the trace, would we step out
to get the location (5:14) (of "<<") from that (2:9)?  For that, I
guess we'd need to store the locations of the arguments of OPERATE,
somehow, figure out that OPRT is the parameter for the argument "<<"
and act from there.  This is the argument replacement replay I talked
about in another thread when you first raised this point.  We'd need
to do some parts of what replace_args does and handle cases like
pasting etc.  Tom and I figured this would be a bit more difficult so
we preferred staying with this simpler scheme for now.  The simpler
scheme being to store the location of the argument "<<" and the
location of the parameter OPRT in the map.

-- 
		Dodji

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

* Re: [PATCH 3/7] Emit macro expansion related diagnostics
  2011-09-19 21:27                   ` Jason Merrill
@ 2011-09-20  8:47                     ` Dodji Seketeli
  2011-09-20 14:12                       ` Jason Merrill
  0 siblings, 1 reply; 135+ messages in thread
From: Dodji Seketeli @ 2011-09-20  8:47 UTC (permalink / raw)
  To: Jason Merrill; +Cc: gcc-patches, tromey, gdr, joseph, burnus, charlet, bonzini

Jason Merrill <jason@redhat.com> writes:

> It is sounding to me like the first location (xI) gets you the next
> virtual location in the unwinding process, whereas the second location
> (yI) gets you the spelling location of the token in the definition of
> a macro.

Right.

> Certainly all the calls to tokens_buff_add_token pass src->src_loc for
> the second.  So why don't we look up the second location in the macro
> definition when we need it rather than store a copy in the map?

Because when you have the first location, looking up the second is not
easy.  Note that the information about the arguments of a function-like
macro is freed right in enter_macro_context by delete_macro_args once we
have recorded the locations for the macro expansion.  Getting the src
that matches the loc of a token coming from an argument of the macro
expansion, once that argument has been freed is not easier than just
storing a copy of the src->src_loc we need.  So Tom and I decided to let
that optimization for later once we are sure the whole thing works and
performs well enough.

-- 
		Dodji

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

* Re: [PATCH 5/7] Add line map statistics to -fmem-report output
  2011-09-17 22:05           ` Jason Merrill
@ 2011-09-20 12:10             ` Dodji Seketeli
  2011-09-20 14:08               ` Jason Merrill
  0 siblings, 1 reply; 135+ messages in thread
From: Dodji Seketeli @ 2011-09-20 12:10 UTC (permalink / raw)
  To: Jason Merrill; +Cc: gcc-patches, tromey, gdr, joseph, burnus, charlet, bonzini

Jason Merrill <jason@redhat.com> writes:


> It's hard to compare macro_maps_allocated_size with
> macro_maps_used_size since you have to do subtraction to discover both
> values from what is printed.

OK, I have changed the statistics components accordingly.

> And do the addition in dump_line_table_statistics rather than store
> sums in linemap_stats.

Done.

> Also, linemap_stats and linemap_get_statistics need comments.

Done.

>> +/* Display an integer amount as multiple of 1K or 1M (in base 2à).
>
> No UTF-8.

Fixed.

Thanks.

From: Dodji Seketeli <dodji@redhat.com>
Date: Sat, 4 Dec 2010 17:23:40 +0100
Subject: [PATCH 5/7] Add line map statistics to -fmem-report output

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.

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

gcc/
	* 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/
	* 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/input.c               |   96 +++++++++++++++++++++++++++++++++++++++++++++
 gcc/input.h               |    2 +
 gcc/toplev.c              |    1 +
 libcpp/include/line-map.h |   21 ++++++++++
 libcpp/line-map.c         |   64 ++++++++++++++++++++++++++++++
 libcpp/macro.c            |   29 ++++++++++++--
 6 files changed, 209 insertions(+), 4 deletions(-)

diff --git a/gcc/input.c b/gcc/input.c
index 83344d7..ff72a5c 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 5f63b69..2f90261 100644
--- a/gcc/toplev.c
+++ b/gcc/toplev.c
@@ -1788,6 +1788,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 81fe6c9..6a1a65f 100644
--- a/libcpp/include/line-map.h
+++ b/libcpp/include/line-map.h
@@ -800,6 +800,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 fbd88da..2d51f8e 100644
--- a/libcpp/line-map.c
+++ b/libcpp/line-map.c
@@ -30,6 +30,11 @@ 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);
+
+/* Counters defined in macro.c.  */
+extern unsigned num_expanded_macros_counter;
+extern unsigned num_macro_tokens_counter;
+
 /* Initialize a line map set.  */
 
 void
@@ -993,3 +998,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 100e2fc..834c7ce 100644
--- a/libcpp/macro.c
+++ b/libcpp/macro.c
@@ -173,6 +173,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
@@ -1083,10 +1090,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)
@@ -1096,13 +1108,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;
@@ -1704,6 +1721,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.  */
@@ -2231,6 +2250,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;
-- 
		Dodji

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

* Re: [PATCH 5/7] Add line map statistics to -fmem-report output
  2011-09-20 12:10             ` Dodji Seketeli
@ 2011-09-20 14:08               ` Jason Merrill
  0 siblings, 0 replies; 135+ messages in thread
From: Jason Merrill @ 2011-09-20 14:08 UTC (permalink / raw)
  To: Dodji Seketeli; +Cc: gcc-patches, tromey, gdr, joseph, burnus, charlet, bonzini

OK.

Jason

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

* Re: [PATCH 3/7] Emit macro expansion related diagnostics
  2011-09-20 14:12                       ` Jason Merrill
@ 2011-09-20 14:12                         ` Dodji Seketeli
  2011-09-21  2:51                           ` Jason Merrill
  0 siblings, 1 reply; 135+ messages in thread
From: Dodji Seketeli @ 2011-09-20 14:12 UTC (permalink / raw)
  To: Jason Merrill; +Cc: gcc-patches, tromey, gdr, joseph, burnus, charlet, bonzini

Jason Merrill <jason@redhat.com> writes:

> On 09/20/2011 03:23 AM, Dodji Seketeli wrote:
>> Jason Merrill<jason@redhat.com>  writes:
>
>>> Certainly all the calls to tokens_buff_add_token pass src->src_loc for
>>> the second.  So why don't we look up the second location in the macro
>>> definition when we need it rather than store a copy in the map?
>>
>> Because when you have the first location, looking up the second is not
>> easy.
>
> In linemap_macro_map_loc_to_def_point you get the token number and
> then use that to index into MACRO_MAP_LOCATIONS.  Can't you use the
> same token number to index into macro->exp.tokens instead?

No, because a macro argument can be made of several tokens.  So after
the first macro parameter of the replacement-list has been replaced by
the tokens of the argument, there is a shift between the indexes of the
tokens resulting from the replacement, and the original tokens of the
macro replacement list.

-- 
		Dodji

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

* Re: [PATCH 3/7] Emit macro expansion related diagnostics
  2011-09-20  8:47                     ` Dodji Seketeli
@ 2011-09-20 14:12                       ` Jason Merrill
  2011-09-20 14:12                         ` Dodji Seketeli
  0 siblings, 1 reply; 135+ messages in thread
From: Jason Merrill @ 2011-09-20 14:12 UTC (permalink / raw)
  To: Dodji Seketeli; +Cc: gcc-patches, tromey, gdr, joseph, burnus, charlet, bonzini

On 09/20/2011 03:23 AM, Dodji Seketeli wrote:
> Jason Merrill<jason@redhat.com>  writes:

>> Certainly all the calls to tokens_buff_add_token pass src->src_loc for
>> the second.  So why don't we look up the second location in the macro
>> definition when we need it rather than store a copy in the map?
>
> Because when you have the first location, looking up the second is not
> easy.

In linemap_macro_map_loc_to_def_point you get the token number and then 
use that to index into MACRO_MAP_LOCATIONS.  Can't you use the same 
token number to index into macro->exp.tokens instead?

Jason

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

* Re: [PATCH 3/7] Emit macro expansion related diagnostics
  2011-09-20 14:12                         ` Dodji Seketeli
@ 2011-09-21  2:51                           ` Jason Merrill
  2011-09-21 19:09                             ` Dodji Seketeli
  0 siblings, 1 reply; 135+ messages in thread
From: Jason Merrill @ 2011-09-21  2:51 UTC (permalink / raw)
  To: Dodji Seketeli; +Cc: gcc-patches, tromey, gdr, joseph, burnus, charlet, bonzini

On 09/20/2011 10:05 AM, Dodji Seketeli wrote:
> Jason Merrill<jason@redhat.com>  writes:

>> In linemap_macro_map_loc_to_def_point you get the token number and
>> then use that to index into MACRO_MAP_LOCATIONS.  Can't you use the
>> same token number to index into macro->exp.tokens instead?

> No, because a macro argument can be made of several tokens.

Ah, I see.

>> I don't understand how that unwinding operation maps onto a function
>> called linemap_macro_map_loc_to_def_point,

> In the example I used in the comment of that function, suppose that
> during 'stepping out', as you put it, we don't deal with the place in
> the source where '<<' appears in the definition of a macro.  We'd then
> only deal with the expansion points of the macros involved.

I think I wasn't expressing clearly enough the point I was trying to 
make.  My point was more that the name of the function is confusing; if 
what you get back is another virtual location, that's not what I would 
consider a "def point".  The only time you get a source location in the 
actual definition of the macro is when you ask for the "macro parm usage 
point".  And so when you go to print actual diagnostics you use 
LRK_MACRO_PARM_REPLACEMENT_POINT.

When we start out, we have a virtual location that represents << in the 
expansion of OPERATE.  Then we call linemap_macro_map_loc_to_def_point 
to get a virtual location that represents << in the expansion of SHIFTL. 
  This is not part of the definition of OPERATE, and shouldn't be 
described as such.

It seems that this function steps out until we hit the spelling 
location, and then we have a real source location and stop, which is why 
the loop in maybe_unwind_expanded_macro_loc needs to use 
linemap_macro_map_loc_to_exp_point as well.  Right?  In the example, 
once we hit << in SHIFTL unwinding needs to switch to the expansion point.

It seems to me that we should encapsulate all of this in a function that 
expresses this unwinding operation, say 
"linemap_macro_map_loc_unwind_once".  So the loop would look like

+  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 in 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_once (where, &map);
+    } while (linemap_macro_expansion_map_p (map));

I think that linemap_macro_loc_to_def_point when called with false 
returns the unwound spelling location of the token (and thus is used for 
LRK_SPELLING_LOCATION), or when called with true returns the 
(not-unwound) location in the expanded macro (and so I think 
LRK_MACRO_PARM_REPLACEMENT_POINT should be renamed to 
LRK_MACRO_EXPANDED_LOCATION or something similar).

It seems to me that either we should split those functions apart in to 
two functions with clearer names, or bundle them and 
linemap_macro_loc_to_exp_point into linemap_resolve_location; if 
linemap_location_in_system_header_p used linemap_resolve_location it 
would be more readable anyway.

I'm having trouble thinking of a less misleading name for 
linemap_macro_map_loc_to_def_point, since the two locations serve 
completely different purposes.  Maybe something vague like 
linemap_macro_map_loc_get_stored_loc.  Or split it into two functions 
like linemap_virtual_loc_unwind_once_toward_spelling and 
linemap_virtual_loc_get_expanded_location or something like that.

What do you think?

Jason

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

* Re: [PATCH 2/7] Generate virtual locations for tokens
  2011-09-19 22:31                 ` Jason Merrill
@ 2011-09-21 14:55                   ` Dodji Seketeli
  2011-09-22 17:10                     ` Jason Merrill
  0 siblings, 1 reply; 135+ messages in thread
From: Dodji Seketeli @ 2011-09-21 14:55 UTC (permalink / raw)
  To: Jason Merrill; +Cc: gcc-patches, tromey, gdr, joseph, burnus, charlet, bonzini

Jason Merrill <jason@redhat.com> writes:

> On 09/18/2011 06:47 AM, Dodji Seketeli wrote:
> > +    cpp_hashnode *macro;
> > +
> > +  }c;
> 
> Extra blank line, missing space.

Fixed.

 
> clobbering the last token when the buffer is full sounds like it's
> unlikely to be what the caller wants; should we abort instead?

abort () added in that case.

> 
> > +  size_t expanded_capacity;     /* total size of expanded array.  */
> > +  source_location *virt_locs;  /* Where virtual locations for
> > +                                  unexpanded tokens are stored.  */
> > +  unsigned virt_locs_capacity; /* Total size of virtual locations
> > +                                  array.  */
> > +  source_location *expanded_virt_locs; /* Where virtual locations for
> > +                                         expanded tokens are
> > +                                         stored.  */
> 
> Why do we need the capacity fields, when we didn't before?

Hmmh, I have no idea.  I have removed them.

> 
> > +         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);
> 
> This looks a lot like cloning tokens.  I thought you were storing
> virtual locations separately so you wouldn't have to do that?

This is not cloning as we are not allocating memory for the tokens
themselves.  We are allocating the memory to hold pointers to the
existing tokens and their virtual location.  And the lifetime of that
allocated memory is no longer than the life time of the macro
expansion context (enter_macro_context).  In the previous cloning
scheme, the lifetime of the cloned tokens was the entire compilation.

Thanks.

From: Dodji Seketeli <dodji@redhat.com>
Date: Sat, 4 Dec 2010 14:04:29 +0100
Subject: [PATCH 2/7] Generate virtual locations for tokens

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.

The combination of this patch and the previous one bootstraps with
--enable-languages=all,ada and passes regression tests on
x86_64-unknown-linux-gnu.

gcc/
	* 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/

	* 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/

	* 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/c-family/c-opts.c   |   12 +
 gcc/c-family/c.opt      |    8 +
 gcc/doc/cppopts.texi    |   18 +
 gcc/doc/invoke.texi     |    6 +-
 libcpp/directives.c     |    2 +-
 libcpp/include/cpplib.h |    8 +
 libcpp/init.c           |    3 +-
 libcpp/internal.h       |   56 ++-
 libcpp/lex.c            |   41 ++-
 libcpp/macro.c          | 1321 ++++++++++++++++++++++++++++++++++++++++++-----
 libcpp/traditional.c    |    2 +-
 11 files changed, 1330 insertions(+), 147 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 957d75c..7e1b7c2 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/libcpp/directives.c b/libcpp/directives.c
index a62ddeb..31c5e95 100644
--- a/libcpp/directives.c
+++ b/libcpp/directives.c
@@ -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/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 588e8ed..dd3fdd4 100644
--- a/libcpp/internal.h
+++ b/libcpp/internal.h
@@ -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/macro.c b/libcpp/macro.c
index 03fe79e..06fd0f0 100644
--- a/libcpp/macro.c
+++ b/libcpp/macro.c
@@ -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,61 @@ 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 line macro
+   argument.  */
+typedef struct macro_arg_token_iter macro_arg_token_iter;
+struct macro_arg_token_iter
+{
+  /* The cpp_reader the macro comes from.  */
+  cpp_reader *pfile;
+  /* The kind of token over which we are supposed to iterate.  */
+  enum macro_arg_token_kind kind;
+  /* The function-like macro the tokens come from.  */
+  const macro_arg *arg;
+  /* 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 +103,56 @@ 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 (cpp_reader *, macro_arg *, const cpp_token *,
+			   source_location, size_t,
+			   enum macro_arg_token_kind);
+static const source_location *get_arg_token_location (cpp_reader *,
+						      const macro_arg *,
+						      enum macro_arg_token_kind);
+static const cpp_token **arg_token_ptr_at (cpp_reader *,
+					   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 *, cpp_reader*,
+				       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 (cpp_reader *,
+						   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_reader *,
+						_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 +164,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.  */
@@ -507,7 +606,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
@@ -517,10 +616,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)
 	{
@@ -584,23 +698,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;
@@ -615,9 +743,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 (;;)
 	{
@@ -625,11 +761,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)
 	    {
@@ -686,7 +831,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);
 
@@ -700,8 +845,9 @@ collect_args (cpp_reader *pfile, const cpp_hashnode *node,
 	      else
 		continue;
 	    }
-
-	  arg->first[ntokens++] = token;
+	  set_arg_token (pfile, arg, token, virt_loc,
+			 ntokens, MACRO_ARG_TOKEN_NORMAL);
+	  ntokens++;
 	}
 
       /* Drop trailing padding.  */
@@ -709,7 +855,8 @@ collect_args (cpp_reader *pfile, const cpp_hashnode *node,
 	ntokens--;
 
       arg->count = ntokens;
-      arg->first[ntokens] = &pfile->eof;
+      set_arg_token (pfile, arg, &pfile->eof, pfile->eof.src_loc,
+		     ntokens, MACRO_ARG_TOKEN_NORMAL);
 
       /* Terminate the argument.  Excess arguments loop back and
 	 overwrite the final legitimate argument, before failing.  */
@@ -752,6 +899,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;
 	}
     }
@@ -765,10 +914,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;
 
@@ -785,7 +936,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
@@ -819,13 +970,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;
@@ -850,11 +1003,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--;
@@ -873,8 +1028,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.  */
@@ -888,13 +1048,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 (pfile, 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)
 	{
@@ -922,33 +1113,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 (cpp_reader *pfile, macro_arg *arg, const cpp_token *token,
+	       source_location location, size_t index,
+	       enum macro_arg_token_kind kind)
+{  
+  const cpp_token **token_ptr;
+  source_location *loc = NULL;
+  bool track_macro_exp_p = CPP_OPTION (pfile, track_macro_expansion);
+
+  token_ptr =
+    arg_token_ptr_at (pfile, 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.  */
+static const source_location *
+get_arg_token_location (cpp_reader *pfile,
+			const macro_arg *arg,
+			enum macro_arg_token_kind kind)
+{
+  source_location *loc = NULL;
+  const cpp_token **token_ptr = arg_token_ptr_at (pfile, arg, 0,
+						  kind, &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 (cpp_reader *pfile, const macro_arg *arg,
+		  size_t index, enum macro_arg_token_kind kind,
+		  source_location **virt_location)
+{
+  bool track_macro_exp_p = CPP_OPTION (pfile, track_macro_expansion);
+  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)
+    return NULL;
+
+  if (virt_location)
+    {
+      if (track_macro_exp_p)
+	{
+	  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;
+	}
+      else
+	*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,
+			   cpp_reader *pfile,
+			   enum macro_arg_token_kind kind,
+			   const macro_arg *arg,
+			   const cpp_token **token_ptr)
+{
+  iter->pfile = pfile;
+  iter->kind = kind;
+  iter->arg = arg;
+  iter->token_ptr = token_ptr;
+  iter->location_ptr = get_arg_token_location (pfile, arg, kind);
+#ifdef ENABLE_CHECKING
+  iter->num_forwards = 0;
+#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)
+{
+  bool track_macro_exp_p = CPP_OPTION (it->pfile,
+				       track_macro_expansion);
+
+  switch (it->kind)
+    {
+    case MACRO_ARG_TOKEN_NORMAL:
+    case MACRO_ARG_TOKEN_EXPANDED:
+      it->token_ptr++;
+      if (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
+  return *it->location_ptr;
+}
+
+/* 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.  */
@@ -970,67 +1442,222 @@ 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, &virt_locs);
+
   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 (pfile, 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, pfile,
+				     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, pfile,
+				     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, pfile,
+				     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, pfile,
+				     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 (pfile, 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 (pfile, 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)
@@ -1046,7 +1673,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 (pfile, 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)
@@ -1060,13 +1692,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.  */
@@ -1094,6 +1729,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;
@@ -1110,8 +1746,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;
@@ -1122,15 +1758,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,
@@ -1138,14 +1803,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 the
+   -ftrack-macro-expansion flag is in effect and if VIRT_LOCS is
+   non-null, *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)
+{
+  bool track_macro_exp_p = CPP_OPTION (pfile, track_macro_expansion);
+  size_t tokens_size = len * sizeof (cpp_token *);
+  size_t locs_size = len * sizeof (source_location);
+
+  if (track_macro_exp_p && 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 points to where to
+   insert the virtual location of TOKEN; that is, if the flag
+   -ftrack-macro-expansion is in effect.  TOKEN is the token to
+   insert.  DEF_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 PARM_DEF_LOC can
+   just be set to the same value as 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 (cpp_reader *pfile,
+			  const cpp_token **dest,
+			  source_location *virt_loc_dest,
+			  const cpp_token *token,
+			  source_location def_loc,
+			  source_location parm_def_loc,			  
+			  const struct line_map *map,
+			  unsigned int macro_token_index)
+{
+  bool track_macro_exp_p = CPP_OPTION (pfile, track_macro_expansion);
+  source_location macro_loc = def_loc;
+  const cpp_token **result;
+
+  if (track_macro_exp_p)
+    {
+      if (map)
+	macro_loc = linemap_add_macro_token (map, macro_token_index,
+					     def_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 then overwrites the last memory location
+   of BUFFER that holds a token.
+
+   TOKEN is the token to append. DEF_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 PARM_DEF_LOC can just be set to the
+   same value as 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.  This function adds
+   the virtual location DEF_LOC it 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_reader *pfile,
+		       _cpp_buff *buffer,
+		       source_location *virt_locs,
+		       const cpp_token *token,
+		       source_location def_loc,
+		       source_location parm_def_loc,
+		       const struct line_map *map,
+		       unsigned int macro_token_index)
+{
+  const cpp_token **result;
+  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 ();
+
+  result =
+    tokens_buff_put_token_to (pfile, (const cpp_token **) BUFF_FRONT (buffer),
+			      &virt_locs[token_index],
+			      token, def_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
@@ -1155,38 +2006,47 @@ _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 (pfile, arg, token, location,
+		     arg->expanded_count, MACRO_ARG_TOKEN_EXPANDED);
+      arg->expanded_count++;
     }
 
   _cpp_pop_context (pfile);
@@ -1195,25 +2055,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
@@ -1221,12 +2188,18 @@ _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".  */
+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 (;;)
@@ -1236,20 +2209,23 @@ 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);
+	      if (location)
+		*location = result->src_loc;
+	      goto out;
 	    }
 	}
       else
@@ -1257,7 +2233,10 @@ cpp_get_token (cpp_reader *pfile)
 	  _cpp_pop_context (pfile);
 	  if (pfile->state.in_directive)
 	    continue;
-	  return &pfile->avoid_paste;
+	  if (location)
+	    *location = pfile->avoid_paste.src_loc;
+	  result = &pfile->avoid_paste;
+	  goto out;
 	}
 
       if (pfile->state.in_directive && result->type == CPP_COMMENT)
@@ -1276,7 +2255,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;
@@ -1294,7 +2273,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
@@ -1311,12 +2291,16 @@ 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);
+	      if (location)
+		*location = result->src_loc;
+	      goto out;
 	    }
 	}
       else
@@ -1333,27 +2317,87 @@ cpp_get_token (cpp_reader *pfile)
       break;
     }
 
+  if (location)
+    *location = virt_loc;
+
+ out:
+  if (location != NULL)
+    {
+      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;
 }
 
@@ -1363,7 +2407,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;
 }
@@ -1420,10 +2464,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.2


-- 
		Dodji

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

* Re: [PATCH 3/7] Emit macro expansion related diagnostics
  2011-09-21  2:51                           ` Jason Merrill
@ 2011-09-21 19:09                             ` Dodji Seketeli
  2011-09-22 15:32                               ` Jason Merrill
  0 siblings, 1 reply; 135+ messages in thread
From: Dodji Seketeli @ 2011-09-21 19:09 UTC (permalink / raw)
  To: Jason Merrill; +Cc: gcc-patches, tromey, gdr, joseph, burnus, charlet, bonzini

Jason Merrill <jason@redhat.com> writes:

> My point was more that the name of the function is confusing;

Sorry about that.

> if what you get back is another virtual location, that's not what I
> would consider a "def point".

For tokens that are not function-like macro arguments, I think the
linemap_macro_loc_to_def_point makes sense because what we get then is
the source location in the actual definition of the macro.  Actually I
think that's where the confusion comes from.  I first devised the name
while thinking of the case of macros that are not function-like.  And
now it falls short for the general case.  I should have caught that
but for some reason I was just seeing through the name is if it was
transparent.  Sigh.

I take your point; that name doesn't make sense in the general case.

> The only time you get a source location in the actual definition of
> the macro is when you ask for the "macro parm usage point".

Yes that, and in the case above; in which case xI and yI are equal, by
the way.

> When we start out, we have a virtual location that represents << in
> the expansion of OPERATE.  Then we call
> linemap_macro_map_loc_to_def_point to get a virtual location that
> represents << in the expansion of SHIFTL. This is not part of the
> definition of OPERATE, and shouldn't be described as such.

Agreed.

> It seems that this function steps out until we hit the spelling
> location, and then we have a real source location and stop, which is
> why the loop in maybe_unwind_expanded_macro_loc needs to use
> linemap_macro_map_loc_to_exp_point as well.  Right?

Right.

> It seems to me that we should encapsulate all of this in a function
> that expresses this unwinding operation, say
> "linemap_macro_map_loc_unwind_once".  So the loop would look like
> 
> +  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 in 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_once (where, &map);
> +    } while (linemap_macro_expansion_map_p (map));
> 

OK.

> I think that linemap_macro_loc_to_def_point when called with false
> returns the unwound spelling location of the token (and thus is used
> for LRK_SPELLING_LOCATION),

Right.

> or when called with true returns the (not-unwound) location in the
> expanded macro (and so I think LRK_MACRO_PARM_REPLACEMENT_POINT
> should be renamed to LRK_MACRO_EXPANDED_LOCATION or something
> similar).

FWIW, I'd like the LRK_MACRO_PARM_REPLACEMENT name (or its
replacement.  ha ha) to hint at the fact that it really has to do with
a token that is an /argument/ for a function-like macro.  So maybe
LRK_MACRO_PARM_FOR_ARG_LOCATION?  LRK_MACRO_EXPANDED_LOCATION really
seems too vague to me.  After all, pretty much everything is about
*EXPAND*ing macros here.  :-)

> It seems to me that either we should split those functions apart in
> to two functions with clearer names, or bundle them and
> linemap_macro_loc_to_exp_point into linemap_resolve_location; if
> linemap_location_in_system_header_p used linemap_resolve_location it
> would be more readable anyway.

OK.

> I'm having trouble thinking of a less misleading name for
> linemap_macro_map_loc_to_def_point, since the two locations serve
> completely different purposes.  Maybe something vague like
> linemap_macro_map_loc_get_stored_loc.  Or split it into two
> functions like linemap_virtual_loc_unwind_once_toward_spelling and
> linemap_virtual_loc_get_expanded_location or something like that.

So would a function named linemap_macro_map_loc_to_def_point that
returns the second location (yI) only, and a second function
linemap_macro_map_loc_unwind_once be less confusing to you?  If yes,
then I'll send an updated patch for that in a short while.

Thanks.

-- 
		Dodji

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

* Re: [PATCH 3/7] Emit macro expansion related diagnostics
  2011-09-21 19:09                             ` Dodji Seketeli
@ 2011-09-22 15:32                               ` Jason Merrill
  2011-09-26 21:11                                 ` Dodji Seketeli
  0 siblings, 1 reply; 135+ messages in thread
From: Jason Merrill @ 2011-09-22 15:32 UTC (permalink / raw)
  To: Dodji Seketeli; +Cc: gcc-patches, tromey, gdr, joseph, burnus, charlet, bonzini

On 09/21/2011 02:32 PM, Dodji Seketeli wrote:
> FWIW, I'd like the LRK_MACRO_PARM_REPLACEMENT name (or its
> replacement.  ha ha) to hint at the fact that it really has to do with
> a token that is an /argument/ for a function-like macro.

I disagree; arguments are the situation when the two locations differ, 
but this one (yI) is always the source location in the macro definition, 
while the first one (xI) is either that or, for macro arguments, a 
virtual location.  So the association with arguments seems backwards to me.

> So would a function named linemap_macro_map_loc_to_def_point that
> returns the second location (yI) only, and a second function
> linemap_macro_map_loc_unwind_once be less confusing to you?

Yes, that sounds good.

Jason

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

* Re: [PATCH 2/7] Generate virtual locations for tokens
  2011-09-21 14:55                   ` Dodji Seketeli
@ 2011-09-22 17:10                     ` Jason Merrill
  2011-09-26 14:47                       ` Dodji Seketeli
  0 siblings, 1 reply; 135+ messages in thread
From: Jason Merrill @ 2011-09-22 17:10 UTC (permalink / raw)
  To: Dodji Seketeli; +Cc: gcc-patches, tromey, gdr, joseph, burnus, charlet, bonzini

On 09/21/2011 09:34 AM, Dodji Seketeli wrote:
> Jason Merrill<jason@redhat.com>  writes:

>> clobbering the last token when the buffer is full sounds like it's
>> unlikely to be what the caller wants; should we abort instead?
>
> abort () added in that case.

Please update the comment as well.

> +/* An iterator over tokens coming from a function line macro

"function-like"

> +  /* The function-like macro the tokens come from.  */
> +  const macro_arg *arg;

This field doesn't seem to be used anywhere.

> +  /* The cpp_reader the macro comes from.  */
> +  cpp_reader *pfile;

This seems to only be used to decide whether or not to increment 
location_ptr.  Rather than base that decision on going all the way back 
to the CPP_OPTION, let's just pass a flag to macro_arg_token_iter_init 
and use that to decide whether or not to set location_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
> +  return *it->location_ptr;
> +}

And then here if location_ptr isn't set we should get the location from 
the token.

> +  if (virt_location)
> +    {
> +      if (track_macro_exp_p)
> +       {
> +         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;
> +       }
> +      else

Similarly, here virt_location should only be set when we're tracking 
macro expansions, so the second test becomes redundant.

> +tokens_buff_new (cpp_reader *pfile, size_t len,
> +                source_location **virt_locs)
> +{
> +  bool track_macro_exp_p = CPP_OPTION (pfile, track_macro_expansion);
> +  size_t tokens_size = len * sizeof (cpp_token *);
> +  size_t locs_size = len * sizeof (source_location);
> +
> +  if (track_macro_exp_p && virt_locs != NULL)
> +    *virt_locs = XNEWVEC (source_location, locs_size);

And here.

> +           *num_args = num_args_alloced;;

Extra ;

> +      num_args_alloced++;
>
>        argc++;

How does num_args_alloced differ from argc?

> +  result =
> +    tokens_buff_put_token_to (pfile, (const cpp_token **) BUFF_FRONT (buffer),
> +                             &virt_locs[token_index],
> +                             token, def_loc, parm_def_loc,
> +                             map, macro_token_index);

Here if virt_locs is null we should pass down null as well.

> +  if (track_macro_exp_p)
> +    {
> +      if (map)
> +       macro_loc = linemap_add_macro_token (map, macro_token_index,
> +                                            def_loc, parm_def_loc);
> +      *virt_loc_dest = macro_loc;
> +    }

So that here again we can just check that virt_loc_dest is set rather 
than the CPP_OPTION.

>    pfile->context = context->prev;
> +  /* decrease peak memory consumption by feeing the context.  */
> +  pfile->context->next = NULL;
> +  free (context);

Setting pfile->context->next to NULL seems wrong; either it's already 
NULL or we're making something unreachable.

> +   LOC is an out parameter; *LOC is set to the location "as expected
> +   by the user".  */

This is puzzling without the explanation before 
cpp_get_token_with_location; just refer to that comment here.

In cpp_get_token_1 the distinction between code paths that set virt_loc 
and those that set *location directly seems unfortunate; I would think 
it would be cleaner to do

> +  if (location)
       {
	if (virt_loc == 0) virt_loc = result->src_loc;
> +    *location = virt_loc;

and drop the direct settings of *location/gotos earlier in the function.

Jason

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

* Re: [PATCH 2/7] Generate virtual locations for tokens
  2011-09-22 17:10                     ` Jason Merrill
@ 2011-09-26 14:47                       ` Dodji Seketeli
  2011-09-26 20:39                         ` Jason Merrill
  0 siblings, 1 reply; 135+ messages in thread
From: Dodji Seketeli @ 2011-09-26 14:47 UTC (permalink / raw)
  To: Jason Merrill; +Cc: gcc-patches, tromey, gdr, joseph, burnus, charlet, bonzini

> > abort () added in that case.
> 
> Please update the comment as well.

Done.

> 
> > +/* An iterator over tokens coming from a function line macro
> 
> "function-like"

Fixed.

> 
> > +  /* The function-like macro the tokens come from.  */
> > +  const macro_arg *arg;
> 
> This field doesn't seem to be used anywhere.

Removed.

> 
> > +  /* The cpp_reader the macro comes from.  */
> > +  cpp_reader *pfile;
> 
> This seems to only be used to decide whether or not to increment
> location_ptr.  Rather than base that decision on going all the way
> back to the CPP_OPTION, let's just pass a flag to
> macro_arg_token_iter_init and use that to decide whether or not to set
> location_ptr.

Conceptually, location_ptr is always set, barring one exception: when
the token pointing to by the iterator is empty.  This can happen when
an empty token is passed to a macro.  E.g:

/
| #define M(var...) var
| M();
\

In all the other cases, location_ptr is set to the virtual location of
the token.  In the particular case where we are not tracking macro
expansions (thus virtual location == spelling location) then
location_ptr is set to &token->src_loc.

It's the incrementing of location_ptr that is indeed done only when we
are tracking macro expansions.

In any case, I agree that storing pfile in the iterator is too heavy.
I have added a track_macro_exp_p flag to struct macro_arg_token_iter
and removed the pfile member.  I have updated the functions that were
expecting a pfile in macro_arg_token_iter.

> > +/* 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
> > +  return *it->location_ptr;
> > +}
> 
> And then here if location_ptr isn't set we should get the location
> from the token.

This is not needed here, as it->location_ptr is always set, as I
explained above.

> 
> > +  if (virt_location)
> > +    {
> > +      if (track_macro_exp_p)
> > +       {
> > +         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;
> > +       }
> > +      else
> 
> Similarly, here virt_location should only be set when we're tracking
> macro expansions, so the second test becomes redundant.

Fixed.  I have updated get_arg_token_location to get the location from
the token when we are not tracking macro expansions and adjusted
set_arg_token to avoid updating the separate virtual location if we
are not tracking macro expansions.

> 
> > +tokens_buff_new (cpp_reader *pfile, size_t len,
> > +                source_location **virt_locs)
> > +{
> > +  bool track_macro_exp_p = CPP_OPTION (pfile, track_macro_expansion);
> > +  size_t tokens_size = len * sizeof (cpp_token *);
> > +  size_t locs_size = len * sizeof (source_location);
> > +
> > +  if (track_macro_exp_p && virt_locs != NULL)
> > +    *virt_locs = XNEWVEC (source_location, locs_size);
> 
> And here.

Fixed.

> 
> > +           *num_args = num_args_alloced;;
> 
> Extra ;
> 
> > +      num_args_alloced++;
> >
> >        argc++;
> 
> How does num_args_alloced differ from argc?

because of this:

      /* A single empty argument is counted as no argument.  */
      if (argc == 1 && macro->paramc == 0 && args[0].count == 0)
	argc = 0;

which happens when a variadic function-like macro is passed one empty
argument.  In that case, argc is zero even though one arg has been
allocated.

I need to know the number of args that were allocated by collect_args
so that enter_macro_context can correctly delete those args (after
indirectly collecting them with funlike_invocation_p) by calling
delete_macro_args.

> 
> > +  result =
> > +    tokens_buff_put_token_to (pfile, (const cpp_token **) BUFF_FRONT (buffer),
> > +                             &virt_locs[token_index],
> > +                             token, def_loc, parm_def_loc,
> > +                             map, macro_token_index);
> 
> Here if virt_locs is null we should pass down null as well.

OK.

> 
> > +  if (track_macro_exp_p)
> > +    {
> > +      if (map)
> > +       macro_loc = linemap_add_macro_token (map, macro_token_index,
> > +                                            def_loc, parm_def_loc);
> > +      *virt_loc_dest = macro_loc;
> > +    }
> 
> So that here again we can just check that virt_loc_dest is set rather
> than the CPP_OPTION.

OK.

> 
> >    pfile->context = context->prev;
> > +  /* decrease peak memory consumption by feeing the context.  */
> > +  pfile->context->next = NULL;
> > +  free (context);
> 
> Setting pfile->context->next to NULL seems wrong; either it's already
> NULL or we're making something unreachable.

My understanding is that contexts are allocated with next_context, and
used in a stack-ish way only by _cpp_push_*_context.  And
_cpp_pop_context frees that memory (it was previously recycling it for
reuse by next_context).  next_context tests that context->next is
non-null (which means there is no context to reuse) and then allocates
the memory.  I think the reason why nothing was setting context->next
to null before is because the contexts were being recycled.

So I fail to see what would be made unreachable here.

> 
> > +   LOC is an out parameter; *LOC is set to the location "as expected
> > +   by the user".  */
> 
> This is puzzling without the explanation before
> cpp_get_token_with_location; just refer to that comment here.

Done.

> 
> In cpp_get_token_1 the distinction between code paths that set
> virt_loc and those that set *location directly seems unfortunate; I
> would think it would be cleaner to do
> 
> > +  if (location)
>       {
> 	if (virt_loc == 0) virt_loc = result->src_loc;
> > +    *location = virt_loc;
> 

Done.

> and drop the direct settings of *location/gotos earlier in the
>  function.

Note that the gotos were put there also because we needed to get out
of the for (;;) loop, similarly to what the previous return statements
were doing; so by doing this doesn't we don't do get rid of the gotos.


I have bootstrapped and tested this patch on x86_64-unknown-linux-gnu
against recent trunk.

From: Dodji Seketeli <dodji@redhat.com>
Date: Sat, 4 Dec 2010 14:04:29 +0100
Subject: [PATCH 2/7] Generate virtual locations for tokens

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.

The combination of this patch and the previous one bootstraps with
--enable-languages=all,ada and passes regression tests on
x86_64-unknown-linux-gnu.

gcc/
	* 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/

	* 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/

	* 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/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/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            | 1327 ++++++++++++++++++++++++++++++++++++++++-----
 libcpp/traditional.c      |    2 +-
 14 files changed, 1340 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 957d75c..7e1b7c2 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 5b7ee9d..af94f32 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 588e8ed..fba9a28 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 959566c..9b15bb6 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 03fe79e..bceb128 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,54 @@ 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,
+						      bool);
+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 +160,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.  */
@@ -507,7 +602,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
@@ -517,10 +612,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)
 	{
@@ -584,23 +694,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;
@@ -615,9 +739,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 (;;)
 	{
@@ -625,11 +757,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)
 	    {
@@ -686,7 +827,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);
 
@@ -700,8 +841,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.  */
@@ -709,7 +852,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.  */
@@ -752,6 +897,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;
 	}
     }
@@ -765,10 +912,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;
 
@@ -785,7 +934,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
@@ -819,13 +968,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;
@@ -850,11 +1001,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--;
@@ -873,8 +1026,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.  */
@@ -888,13 +1046,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)
 	{
@@ -922,33 +1111,311 @@ 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.  */
+static const source_location *
+get_arg_token_location (const macro_arg *arg,
+			enum macro_arg_token_kind kind,
+			bool track_macro_exp_p)
+{
+  const source_location *loc = NULL;
+  const cpp_token **token_ptr =
+    arg_token_ptr_at (arg, 0, kind,
+		      track_macro_exp_p ? (source_location **)&loc : NULL);
+
+  if (token_ptr == NULL)
+    return NULL;
+
+  if (loc == NULL)
+    loc = &(*token_ptr)->src_loc;
+  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;
+  iter->location_ptr = get_arg_token_location (arg, kind, track_macro_exp_p);
+#ifdef ENABLE_CHECKING
+  iter->num_forwards = 0;
+  if (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
+  return *it->location_ptr;
+}
+
+/* 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.  */
@@ -970,67 +1437,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)
@@ -1046,7 +1676,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)
@@ -1060,13 +1695,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.  */
@@ -1094,6 +1732,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;
@@ -1110,8 +1749,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;
@@ -1122,15 +1761,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,
@@ -1138,14 +1806,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
@@ -1155,38 +2009,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);
@@ -1195,25 +2059,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
@@ -1221,12 +2192,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 (;;)
@@ -1236,20 +2215,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
@@ -1257,7 +2237,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)
@@ -1276,7 +2257,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;
@@ -1294,7 +2275,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
@@ -1311,12 +2293,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
@@ -1333,27 +2317,91 @@ cpp_get_token (cpp_reader *pfile)
       break;
     }
 
+  if (location)
+    {
+      if (virt_loc == 0)
+	virt_loc = result->src_loc;
+      *location = virt_loc;
+    }
+
+ out:
+  if (location != NULL)
+    {
+      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;
 }
 
@@ -1363,7 +2411,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;
 }
@@ -1420,10 +2468,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.2


-- 
		Dodji

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

* Re: [PATCH 2/7] Generate virtual locations for tokens
  2011-09-26 14:47                       ` Dodji Seketeli
@ 2011-09-26 20:39                         ` Jason Merrill
  2011-09-28  3:23                           ` Dodji Seketeli
  0 siblings, 1 reply; 135+ messages in thread
From: Jason Merrill @ 2011-09-26 20:39 UTC (permalink / raw)
  To: Dodji Seketeli; +Cc: gcc-patches, tromey, gdr, joseph, burnus, charlet, bonzini

On 09/26/2011 10:21 AM, Dodji Seketeli wrote:
>>> >  >  +  /* The cpp_reader the macro comes from.  */
>>> >  >  +  cpp_reader *pfile;
>> >
>> >  This seems to only be used to decide whether or not to increment
>> >  location_ptr.  Rather than base that decision on going all the way
>> >  back to the CPP_OPTION, let's just pass a flag to
>> >  macro_arg_token_iter_init and use that to decide whether or not to set
>> >  location_ptr.

> Conceptually, location_ptr is always set, barring one exception: when
> the token pointing to by the iterator is empty.

Yes, I was suggesting that you change that, since it's only used by 
_get_location, which can get the location from the pointer instead.

> So I fail to see what would be made unreachable here.

You're right, I was misreading the code.

> +  if (location)
> +    {
> +      if (virt_loc == 0)
> +       virt_loc = result->src_loc;
> +      *location = virt_loc;
> +    }
> +
> + out:
> +  if (location != NULL)
> +    {
> +      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);
> +    }

The out: label needs to be before the first if.

Jason

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

* Re: [PATCH 3/7] Emit macro expansion related diagnostics
  2011-09-22 15:32                               ` Jason Merrill
@ 2011-09-26 21:11                                 ` Dodji Seketeli
  2011-09-26 22:30                                   ` Jason Merrill
  0 siblings, 1 reply; 135+ messages in thread
From: Dodji Seketeli @ 2011-09-26 21:11 UTC (permalink / raw)
  To: Jason Merrill; +Cc: gcc-patches, tromey, gdr, joseph, burnus, charlet, bonzini

Jason Merrill <jason@redhat.com> writes:

> On 09/21/2011 02:32 PM, Dodji Seketeli wrote:
>> FWIW, I'd like the LRK_MACRO_PARM_REPLACEMENT name (or its
>> replacement.  ha ha) to hint at the fact that it really has to do with
>> a token that is an /argument/ for a function-like macro.
>
> I disagree; arguments are the situation when the two locations differ,
> but this one (yI) is always the source location in the macro
> definition

I think an interesting question to ask here is "the source location of
what exactly?".

The value of the source location is always the same, yes, but its
meaning is different depending on if the token in question is an
argument or not.

When the token is not an argument, then yI is a source location for *that*
token in the macro definition.  It's then the spelling location for the
token in question.

When the token is an argument, then yI is a source location for
*another* token.  Namely, the parameter for that argument.  Not
necessarily the spelling location for the token we are observing.

> while the first one (xI) is either that or, for macro arguments, a
> virtual location.

Yes, but it's always a locus of the observed token.

Would LRK_MACRO_DEFINITION_LOCATION be better then?  :-)

-- 
		Dodji

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

* Re: [PATCH 3/7] Emit macro expansion related diagnostics
  2011-09-26 21:11                                 ` Dodji Seketeli
@ 2011-09-26 22:30                                   ` Jason Merrill
  2011-09-27 18:43                                     ` Dodji Seketeli
  0 siblings, 1 reply; 135+ messages in thread
From: Jason Merrill @ 2011-09-26 22:30 UTC (permalink / raw)
  To: Dodji Seketeli; +Cc: gcc-patches, tromey, gdr, joseph, burnus, charlet, bonzini

On 09/26/2011 04:21 PM, Dodji Seketeli wrote:
> Jason Merrill<jason@redhat.com>  writes:
>
>> On 09/21/2011 02:32 PM, Dodji Seketeli wrote:
>>> FWIW, I'd like the LRK_MACRO_PARM_REPLACEMENT name (or its
>>> replacement.  ha ha) to hint at the fact that it really has to do with
>>> a token that is an /argument/ for a function-like macro.
>>
>> I disagree; arguments are the situation when the two locations differ,
>> but this one (yI) is always the source location in the macro
>> definition
>
> I think an interesting question to ask here is "the source location of
> what exactly?".

It's the source location of what we're currently looking at, which is a 
virtual token in the macro expansion.

My point is that yI is always a source location, whereas xI may or may 
not be.

> Would LRK_MACRO_DEFINITION_LOCATION be better then?  :-)

Yes.  :)

Jason

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

* Re: [PATCH 3/7] Emit macro expansion related diagnostics
  2011-09-26 22:30                                   ` Jason Merrill
@ 2011-09-27 18:43                                     ` Dodji Seketeli
  2011-09-29  7:05                                       ` Jason Merrill
  0 siblings, 1 reply; 135+ messages in thread
From: Dodji Seketeli @ 2011-09-27 18:43 UTC (permalink / raw)
  To: Jason Merrill; +Cc: gcc-patches, tromey, gdr, joseph, burnus, charlet, bonzini

I am replying to several emails in one go here, as the discussion was a
bit long on this particular patch.

Jason Merrill <jason@redhat.com> writes:

> It seems that this function steps out until we hit the spelling
> location, and then we have a real source location and stop, which is
> why the loop in maybe_unwind_expanded_macro_loc needs to use
> linemap_macro_map_loc_to_exp_point as well.  Right?  In the example,
> once we hit << in SHIFTL unwinding needs to switch to the expansion
> point.
>
> It seems to me that we should encapsulate all of this in a function
> that expresses this unwinding operation, say
> "linemap_macro_map_loc_unwind_once".  So the loop would look like
>
> +  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 in 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_once (where, &map);
> +    } while (linemap_macro_expansion_map_p (map));
>

I have done this.  Though I have called the function
linemap_step_out_once to avoid confusion with
linemap_macro_map_loc_unwind_once above.

> I think that linemap_macro_loc_to_def_point when called with false
> returns the unwound spelling location of the token (and thus is used
> for LRK_SPELLING_LOCATION), or when called with true returns the
> (not-unwound) location in the expanded macro (and so I think
> LRK_MACRO_PARM_REPLACEMENT_POINT should be renamed to
> LRK_MACRO_EXPANDED_LOCATION or something similar).

>> Would LRK_MACRO_DEFINITION_LOCATION be better then?  :-)
>
> Yes.  :)

Done.  Note that I had to mechanically update two other patches that
were OKed previously.

>
> I'm having trouble thinking of a less misleading name for
> linemap_macro_map_loc_to_def_point, since the two locations serve
> completely different purposes.  Maybe something vague like
> linemap_macro_map_loc_get_stored_loc.  Or split it into two functions
> like linemap_virtual_loc_unwind_once_toward_spelling and
> linemap_virtual_loc_get_expanded_location or something like that.

>> So would a function named linemap_macro_map_loc_to_def_point that
>> returns the second location (yI) only, and a second function
>> linemap_macro_map_loc_unwind_once be less confusing to you?
>
> Yes, that sounds good.

I have done this.

> It seems to me that either we should split those functions apart in to
> two functions with clearer names, or bundle them and
> linemap_macro_loc_to_exp_point into linemap_resolve_location; if
> linemap_location_in_system_header_p used linemap_resolve_location it
> would be more readable anyway.

I have essentially made this too.  linemap_macro_loc_to_exp_point,
linemap_macro_map_loc_unwind_once and linemap_macro_map_loc_to_def_point
are now static functions that are only subroutines of
linemap_resolve_location.  They are not part of the public line map API
anymore. linemap_location_in_system_header_p uses
linemap_resolve_location now, and I have updated the other parts of the
compiler that were using those to use linemap_resolve_location instead.

> What do you think?

It's should be less confusing now, hopefully.  :)

Now I am re-posting the two additional patches that I had to
mechanically update due to this changes.  I have bootstrapped and tested
each patch one by one, while having its dependent patches installed.
This was done on x86_64-unknown-linux-gnu against trunk for all
languages including Ada.

From: Dodji Seketeli <dodji@redhat.com>
Date: Sat, 4 Dec 2010 16:31:35 +0100
Subject: [PATCH 3/7] Emit macro expansion related diagnostics

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

The combination of this patch and the previous ones boostrapped with
--enable-languages=all,ada and passed regression tests on
x86_64-unknown-linux-gnu.

gcc/
	* 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/

	* 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/

	* 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.

Addendum patch 0003

gcc/

	* diagnostic.c (diagnostic_report_current_module): Adjust for the
	renaming of LRK_MACRO_PARM_REPLACEMENT_POINT into
	LRK_MACRO_DEFINITION_LOCATION.
	* tree-diagnostic.c (maybe_unwind_expanded_macro_loc): Use the new
	linemap_step_out_once.  Adjust like above.
---
 gcc/Makefile.in                                 |    3 +-
 gcc/cp/error.c                                  |    5 +-
 gcc/diagnostic.c                                |   13 +-
 gcc/diagnostic.h                                |    2 +-
 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 +-
 13 files changed, 305 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 92016f2..d26c682 100644
--- a/gcc/Makefile.in
+++ b/gcc/Makefile.in
@@ -2796,7 +2796,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 598ddf1..8fa163f 100644
--- a/gcc/cp/error.c
+++ b/gcc/cp/error.c
@@ -2767,7 +2767,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);
@@ -2777,8 +2777,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 de0a58a..5f63b69 100644
--- a/gcc/toplev.c
+++ b/gcc/toplev.c
@@ -1132,6 +1132,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..c8416c9 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_step_out_once (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.2

From: Dodji Seketeli <dodji@redhat.com>
Date: Fri, 3 Dec 2010 13:20:26 +0100
Subject: [PATCH 1/7] Linemap infrastructure for virtual locations

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.

Boostrapped with --enable-languages=all,ada and passed regression
tests on x86_unknown-linux-gnu against trunk.

libcpp/

	* 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): New macros.
	(linemap_position_for_line_and_column)
	(linemap_tracks_macro_expansion_locs_p, linemap_enter_macro)
	(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
	(linemap_assert): New macro.
	(linemap_macro_loc_to_exp_point, linemap_macro_loc_to_exp_point)
	(linemap_macro_loc_unwind): 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_enter_macro)
	(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_location_before_p):
	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.
	* 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/
	* 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/c-family

	* 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/java/

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

gcc/ada/

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

gcc/fortran/

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

gcc/testsuite/

	* gcc.dg/cpp/pragma-diagnostic-1.c: New test.
---
 gcc/ada/gcc-interface/trans.c                  |   10 +-
 gcc/c-family/c-lex.c                           |    6 +-
 gcc/c-family/c-ppoutput.c                      |   43 +-
 gcc/diagnostic.c                               |   11 +-
 gcc/fortran/cpp.c                              |   22 +-
 gcc/input.c                                    |    9 +-
 gcc/input.h                                    |   18 +-
 gcc/java/jcf-parse.c                           |    2 +-
 gcc/testsuite/gcc.dg/cpp/pragma-diagnostic-1.c |   32 +
 libcpp/directives.c                            |   16 +-
 libcpp/files.c                                 |    5 +-
 libcpp/include/line-map.h                      |  698 +++++++++++++++++-
 libcpp/init.c                                  |    4 +-
 libcpp/internal.h                              |    3 +-
 libcpp/line-map.c                              |  893 +++++++++++++++++++++--
 libcpp/macro.c                                 |   12 +-
 16 files changed, 1583 insertions(+), 201 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 27e2402..252a360 100644
--- a/gcc/ada/gcc-interface/trans.c
+++ b/gcc/ada/gcc-interface/trans.c
@@ -278,7 +278,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.  */
@@ -7881,12 +7881,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..460ffa7 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,230 @@ 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 locations 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 we are at the expansion point of MACRO.  Each xI is the
+     location of the Ith token of the replacement-list. Now it gets
+     confusing. the xI is the location of the Ith token of the
+     replacement-list at the macro *definition* point. Not at the
+     macro replacement point. Okay, let's try to explain this below.
+
+     Imagine this:
+
+        #define OPERATION(OP0, OPERATOR, OP1) \
+                OP0 OPERATOR OP1 <-- #0
+
+	#define PLUS(A, B) OPERATION (A, +, B)  <--- #1
+
+	int a = PLUS (1,2); <--- #2
+     
+     In #2, there is a macro map for the expansion of PLUS. PLUS is
+     expanded into the replacement-list made of the tokens:
+     
+        OPERATION, (, A, +, B, )
+
+     and then further expanded into the tokens:
+
+        1, +, 2.
+
+     Let's consider the case of token "+" here. That will help us
+     understand what the xI we were talking about earlier means.
+
+     The token '+' has two locations, so to speak. One in the context
+     of the macro *expansion* of PLUS in #2 and one in the context of
+     the macro *definition* of PLUS in #1. These two locations are
+     encoded in the the latter context, somehow in the xI we are
+     talking about.
+
+     xI is roughly the index of the token inside the replacement-list
+     at the expansion point. So for '+', its index would then be 1
+     [The index of token '1' would be 0 and the index of token 2 would
+     be 1]. So if '+' is our current xI, it is actualy an x1.
+
+     The value of x1 is the location of the token '+' inside the
+     replacement-list of PLUS at the definition point of PLUS. It is
+     its spelling location in #1.
+
+     So x0 would have described the token '1', x1 describes the token
+     '+' and x2 describes the token '2'.
+
+     Now what's the y1 then? Remember, we said macro_locations is an
+     array of pairs (xI,yI). We now know what the xI is, now let's
+     look at the yI.
+
+     Let's look at the token '+' again. We said it has two locations
+     somehow. Actually it has 3. Kind of. As '+' is an argument passed
+     to the macro OPERATION [at the definition point of the macro
+     PLUS], it would be nice to record the source location of the
+     *parameter* of OPERATION that is replaced by the argument '+'.
+     In other words, we want to record the location of the token
+     "OPERATOR" in the replacement-list of OPERATION, at the
+     /definition/ point of OPERATION in #0. And that is y1.
+
+     So when (xI,yI) describes a token that is passed as an argument
+     to a macro M, the yI is the location of the macro parameter that
+     the argument replaces, at the definition point of M. If (xI,yI)
+     does not describe a token that is passed as an argument to a
+     macro, xI == yI.  */
+  source_location * GTY((length ("2 * %h.n_tokens"))) macro_locations;
+
+  /* This is the location of the expansion point of the current macro
+     map.  That expansion point 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 +299,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 +433,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 +449,343 @@ 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 *);
+
+/* 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 source location for a token that is part of a
+   macro replacement-list at a macro expansion point.
+
+   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 orginal location of the token at the definition
+   point of the macro. If you read the extensive comments of struct
+   line_map_macro in line-map.h, this is the xI.
+
+   If the token is part of a macro argument, ORIG_PARM_REPLACEMENT_LOC
+   is the location of the point at wich the token (the argument)
+   replaces the macro parameter in the context of the relevant macro
+   definition. If you read the comments of struct line_map_macro in
+   line-map.h, this is the yI.  */
+source_location linemap_add_macro_token (const struct line_map *,
+					 unsigned int,
+					 source_location,
+					 source_location);
+
+/* 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 *);
+
+/* If LOCATION is the locus of a token in a replacement-list of a
+   macro expansion, return the location of said token in the
+   definition of the macro.  If LOCATION is the locus of a token that
+   is an argument of a function-like macro (and that appears in the
+   expansion of a macro), return the location of the parameter (inside
+   the replacement-list of the macro) that the argument replaces.
+
+   In other words, this function returns the yI explained in the
+   comments of line_map_macro above.
+
+   Note that if the token is a builtin the function returns the
+   location of the expansion point of the macro.  */
+source_location linemap_macro_map_loc_to_def_point (const struct line_map*,
+						    source_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_once (const struct line_map*,
+						   source_location);
+
+/* 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*,
+						    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 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_source_line (struct line_maps *,
+			     source_location);
+
+/* Return the column number corresponding to location LOCATION.
+
+   If LOCATION is the source location of token that is part of the
+   replacement-list of a macro expansion return the column number of
+   the macro expansion point.
+
+   SET is the line map set LOCATION comes from.  */
+int linemap_get_source_column (struct line_maps *,
+			       source_location);
+
+/* Return the name of the macro associated to MACRO_MAP.  */
+const char* linemap_map_get_macro_name (const struct line_map*);
+
+/* 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_file_path (struct line_maps *,
+				   source_location);
+
+/* 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 0 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 TRUE if PRE denotes a location that is before POST, FALSE
+   otherwise. LINE_MAPS is the set of line maps PRE and POST were
+   allocated from.  */
+bool linemap_location_before_p (struct line_maps *set,
+				source_location   pre,
+				source_location   post);
+
+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 locus of the expansion
+   point of the macro.
+
+   * 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
+   --------------------------------------
+
+   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.
+
+   Finally, if SPELLING_LOC is not NULL, *RESULTING_LOC is set to the
+   location to which LOC was resolved, and similarly, *LOC_MAP is set
+   to its map.  */
+
+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_step_out_once (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.  */
+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.  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..588e8ed 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)
diff --git a/libcpp/line-map.c b/libcpp/line-map.c
index 2a0749a..b56467c 100644
--- a/libcpp/line-map.c
+++ b/libcpp/line-map.c
@@ -23,24 +23,31 @@ along with this program; see the file COPYING3.  If not see
 #include "config.h"
 #include "system.h"
 #include "line-map.h"
+#include "cpplib.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_loc_unwind (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 +58,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 +129,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)))));
 
-  if (set->used == set->allocated)
+  /* When we enter the file for the first time reason cannot be
+     LC_RENAME.  */
+  linemap_assert (!(set->depth == 0 && reason == LC_RENAME));
+
+  /* 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 +154,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 +194,173 @@ 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 source location for a token that is part of a
+   macro replacement-list at a macro expansion point.
+
+   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 orginal location of the token at the definition
+   point of the macro. If you read the extensive comments of struct
+   line_map_macro in line-map.h, this is the xI.
+
+   If the token is part of a macro argument, ORIG_PARM_REPLACEMENT_LOC
+   is the location of the point at wich the token (the argument)
+   replaces the macro parameter in the context of the relevant macro
+   definition. If you read the comments of struct line_map_macro in
+   line-map.h, this is the yI.  */
+
+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 +388,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 +416,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 +438,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 +448,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;
+
+  if (set ==  NULL || line < RESERVED_LOCATION_COUNT)
+    return NULL;
 
-  mn = set->cache;
-  mx = set->used;
+  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 +508,293 @@ 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;
+    }
+
+  while (mx - mn > 1)
+    {
+      md = (mx + mn) / 2;
+      if (MAP_START_LOCATION (LINEMAPS_MACRO_MAP_AT (set, md)) > line)
+	mn = md;
+      else
+	mx = md;
+    }
+
+  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.  Note
+   that the caller of M is necessarily another macro.  The context of
+   M is a macro definition.
+
+   In other words, this returns the xI location presented in the
+   comments of line_map_macro above.  */
+source_location
+linemap_macro_map_loc_unwind_once (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_source_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 column number corresponding to location LOCATION.
+
+   If LOCATION is the source location of token that is part of the
+   replacement-list of a macro expansion return the column number of
+   the macro expansion point.
+
+   SET is the line map set LOCATION comes from.  */
+
+int
+linemap_get_source_column (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_COLUMN (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_file_path (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 0 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);
+}
+
+/* Return TRUE if PRE denotes a location that is before POST, FALSE
+   otherwise. LINE_MAPS is the set of line maps PRE and POST were
+   allocated from.  */
+
+bool
+linemap_location_before_p (struct line_maps *set,
+			   source_location  pre,
+			   source_location post)
+{
+  bool pre_from_macro_p, post_from_macro_p;
+
+  if (pre == post)
+    return false;
+
+  pre_from_macro_p =
+    linemap_location_from_macro_expansion_p (set, pre);
+  post_from_macro_p =
+    linemap_location_from_macro_expansion_p (set, post);
+
+  if (pre_from_macro_p != post_from_macro_p)
+    {
+      if (pre_from_macro_p)
+	pre = linemap_macro_loc_to_exp_point (set, pre, NULL);
+      else
+	post = linemap_macro_loc_to_exp_point (set, post, NULL);
+    }
+
+  return pre < post;
 }
 
 /* Print an include trace, for e.g. the -H option of the preprocessor.  */
@@ -313,5 +806,249 @@ 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));
+}
+
+/* If LOCATION is the locus of a token that is an argument of a
+   function-like macro M, return the location of that token in the
+   context of the definition of the first macro P which expansion
+   triggered the expansion of M.  Note that the token must be actually
+   present in the source of the definition of P.  If LOCATION is the
+   locus of a token that belongs to a macro replacement-list but is
+   not an argument to a function-like macro, return the same thing as
+   what linemap_macor_loc_to_def_point would have returned.
+   ORIGINAL_MAP is an output parm.  If non NULL, *ORIGINAL_MAP is set
+   to the ordinary (non-macro) map of the returned location comes
+   from.
+
+   This is a subroutine for linemap_resolve_location.  */
+
+static source_location
+linemap_macro_loc_unwind (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_once (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_unwind (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_step_out_once (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_once (*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.  */
+
+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.  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..03fe79e 100644
--- a/libcpp/macro.c
+++ b/libcpp/macro.c
@@ -177,7 +177,7 @@ _cpp_builtin_macro_text (cpp_reader *pfile, cpp_hashnode *node)
 	  while (! MAIN_FILE_P (map))
 	    map = INCLUDED_FROM (pfile->line_table, map);
 
-	name = map->to_file;
+	name = ORDINARY_MAP_FILE_NAME (map);
 	len = strlen (name);
 	buf = _cpp_unaligned_alloc (pfile, len * 2 + 3);
 	result = buf;
@@ -196,14 +196,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_source_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.2


From: Dodji Seketeli <dodji@redhat.com>
Date: Sat, 4 Dec 2010 17:08:56 +0100
Subject: [PATCH 4/7] Support -fdebug-cpp option

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.

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

libcpp/

	* 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/

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

gcc/c-family/

	* 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.
---
 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/include/cpplib.h   |    4 +++
 libcpp/include/line-map.h |    4 +++
 libcpp/line-map.c         |   38 ++++++++++++++++++++++++++++++
 8 files changed, 114 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 7e1b7c2..fedcf84 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 3a6c08a..5dab4d0 100644
--- a/libcpp/include/line-map.h
+++ b/libcpp/include/line-map.h
@@ -788,4 +788,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 ae73ed6..11703fc 100644
--- a/libcpp/line-map.c
+++ b/libcpp/line-map.c
@@ -1052,3 +1052,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.2


-- 
		Dodji

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

* Re: [PATCH 2/7] Generate virtual locations for tokens
  2011-09-26 20:39                         ` Jason Merrill
@ 2011-09-28  3:23                           ` Dodji Seketeli
  2011-09-28 14:49                             ` Jason Merrill
  0 siblings, 1 reply; 135+ messages in thread
From: Dodji Seketeli @ 2011-09-28  3:23 UTC (permalink / raw)
  To: Jason Merrill; +Cc: gcc-patches, tromey, gdr, joseph, burnus, charlet, bonzini

Jason Merrill <jason@redhat.com> writes:

>> Conceptually, location_ptr is always set, barring one exception: when
>> the token pointing to by the iterator is empty.
>
> Yes, I was suggesting that you change that, since it's only used by
> _get_location, which can get the location from the pointer instead.

Done.

>> +  if (location)
>> +    {
>> +      if (virt_loc == 0)
>> +       virt_loc = result->src_loc;
>> +      *location = virt_loc;
>> +    }
>> +
>> + out:
>> +  if (location != NULL)
>> +    {
>> +      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);
>> +    }
>
> The out: label needs to be before the first if.

Oops.  Fixed.  Thanks for catching that.


From: Dodji Seketeli <dodji@redhat.com>
Date: Sat, 4 Dec 2010 14:04:29 +0100
Subject: [PATCH 2/7] Generate virtual locations for tokens

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.

The combination of this patch and the previous one bootstraps with
--enable-languages=all,ada and passes regression tests on
x86_64-unknown-linux-gnu.

gcc/
	* 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/

	* 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/

	* 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/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/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 +-
 14 files changed, 1339 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 957d75c..7e1b7c2 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 460ffa7..3a6c08a 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 588e8ed..fba9a28 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 b56467c..ae73ed6 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 03fe79e..4291cbf 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.  */
@@ -507,7 +601,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
@@ -517,10 +611,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)
 	{
@@ -584,23 +693,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;
@@ -615,9 +738,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 (;;)
 	{
@@ -625,11 +756,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)
 	    {
@@ -686,7 +826,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);
 
@@ -700,8 +840,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.  */
@@ -709,7 +851,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.  */
@@ -752,6 +896,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;
 	}
     }
@@ -765,10 +911,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;
 
@@ -785,7 +933,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
@@ -819,13 +967,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;
@@ -850,11 +1000,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--;
@@ -873,8 +1025,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.  */
@@ -888,13 +1045,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)
 	{
@@ -922,33 +1110,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.  */
@@ -970,67 +1439,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)
@@ -1046,7 +1678,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)
@@ -1060,13 +1697,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.  */
@@ -1094,6 +1734,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;
@@ -1110,8 +1751,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;
@@ -1122,15 +1763,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,
@@ -1138,14 +1808,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
@@ -1155,38 +2011,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);
@@ -1195,25 +2061,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
@@ -1221,12 +2194,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 (;;)
@@ -1236,20 +2217,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
@@ -1257,7 +2239,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)
@@ -1276,7 +2259,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;
@@ -1294,7 +2277,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
@@ -1311,12 +2295,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
@@ -1333,27 +2319,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;
 }
 
@@ -1363,7 +2410,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;
 }
@@ -1420,10 +2467,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;
 	}
-- 
		Dodji

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

* Re: [PATCH 2/7] Generate virtual locations for tokens
  2011-09-28  3:23                           ` Dodji Seketeli
@ 2011-09-28 14:49                             ` Jason Merrill
  2011-09-28 21:24                               ` Dodji Seketeli
  0 siblings, 1 reply; 135+ messages in thread
From: Jason Merrill @ 2011-09-28 14:49 UTC (permalink / raw)
  To: Dodji Seketeli; +Cc: gcc-patches, tromey, gdr, joseph, burnus, charlet, bonzini

On 09/27/2011 08:03 PM, Dodji Seketeli wrote:
> Jason Merrill<jason@redhat.com>  writes:
>
>> Yes, I was suggesting that you change that, since it's only used by
>> _get_location, which can get the location from the pointer instead.
>
> Done.

And then you should be able to drop the track_macro_exp_p field from 
macro_arg_token_iter, and instead check whether location_ptr is set.

Jason

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

* Re: [PATCH 2/7] Generate virtual locations for tokens
  2011-09-28 14:49                             ` Jason Merrill
@ 2011-09-28 21:24                               ` Dodji Seketeli
  2011-09-28 21:45                                 ` Jason Merrill
  0 siblings, 1 reply; 135+ messages in thread
From: Dodji Seketeli @ 2011-09-28 21:24 UTC (permalink / raw)
  To: Jason Merrill; +Cc: gcc-patches, tromey, gdr, joseph, burnus, charlet, bonzini

Jason Merrill <jason@redhat.com> writes:

> And then you should be able to drop the track_macro_exp_p field from
> macro_arg_token_iter, and instead check whether location_ptr is set.

Done.

Bootstrap and testing is under way.

From: Dodji Seketeli <dodji@redhat.com>
Date: Sat, 4 Dec 2010 14:04:29 +0100
Subject: [PATCH 2/7] Generate virtual locations for tokens

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.

The combination of this patch and the previous one bootstraps with
--enable-languages=all,ada and passes regression tests on
x86_64-unknown-linux-gnu.

gcc/
	* 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/

	* 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/

	* 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/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/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            | 1324 ++++++++++++++++++++++++++++++++++++++++-----
 libcpp/traditional.c      |    2 +-
 14 files changed, 1337 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 e166964..4584792 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 460ffa7..3a6c08a 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 588e8ed..fba9a28 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 b56467c..ae73ed6 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 03fe79e..32f4210 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,57 @@ 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
+{
+  /* 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, otherwise, it must be NULL.  */
+  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 +99,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 +157,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.  */
@@ -507,7 +599,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
@@ -517,10 +609,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)
 	{
@@ -584,23 +691,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;
@@ -615,9 +736,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 (;;)
 	{
@@ -625,11 +754,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)
 	    {
@@ -686,7 +824,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);
 
@@ -700,8 +838,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.  */
@@ -709,7 +849,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.  */
@@ -752,6 +894,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;
 	}
     }
@@ -765,10 +909,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;
 
@@ -785,7 +931,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
@@ -819,13 +965,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;
@@ -850,11 +998,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--;
@@ -873,8 +1023,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.  */
@@ -888,13 +1043,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)
 	{
@@ -922,33 +1108,317 @@ 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->kind = kind;
+  iter->token_ptr = token_ptr;
+  if (track_macro_exp_p)
+    iter->location_ptr = get_arg_token_location (arg, kind);
+  else
+    iter->location_ptr = NULL;
+#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->location_ptr)
+	/* This means -ftrack-macro-expansion is on.  */
+	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->location_ptr)
+    /* This means -ftrack-macro-expansion is on.   */
+    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.  */
@@ -970,67 +1440,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)
@@ -1046,7 +1679,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)
@@ -1060,13 +1698,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.  */
@@ -1094,6 +1735,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;
@@ -1110,8 +1752,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;
@@ -1122,15 +1764,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,
@@ -1138,14 +1809,197 @@ _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.   */
+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
@@ -1155,38 +2009,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);
@@ -1195,25 +2059,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
@@ -1221,12 +2192,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 (;;)
@@ -1236,20 +2215,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
@@ -1257,7 +2237,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)
@@ -1276,7 +2257,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;
@@ -1294,7 +2275,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
@@ -1311,12 +2293,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
@@ -1333,27 +2317,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;
 }
 
@@ -1363,7 +2408,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;
 }
@@ -1420,10 +2465,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;
 	}
-- 
		Dodji

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

* Re: [PATCH 2/7] Generate virtual locations for tokens
  2011-09-28 21:24                               ` Dodji Seketeli
@ 2011-09-28 21:45                                 ` Jason Merrill
  2011-09-29  5:49                                   ` Dodji Seketeli
  0 siblings, 1 reply; 135+ messages in thread
From: Jason Merrill @ 2011-09-28 21:45 UTC (permalink / raw)
  To: Dodji Seketeli; +Cc: gcc-patches, tromey, gdr, joseph, burnus, charlet, bonzini

On 09/28/2011 03:15 PM, Dodji Seketeli wrote:
> +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);
...
> +  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;
> +    }

If we make this block conditional on arg->virt_locs being set, then we 
can pass &loc in unconditionally and don't need the track_macro_exp_p 
flag to set_arg_token.

> Note that the gotos were put there also because we needed to get out
> of the for (;;) loop, similarly to what the previous return statements
> were doing; so by doing this doesn't we don't do get rid of the gotos.

Can't you use break instead?

Jason

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

* Re: [PATCH 2/7] Generate virtual locations for tokens
  2011-09-28 21:45                                 ` Jason Merrill
@ 2011-09-29  5:49                                   ` Dodji Seketeli
  0 siblings, 0 replies; 135+ messages in thread
From: Dodji Seketeli @ 2011-09-29  5:49 UTC (permalink / raw)
  To: Jason Merrill; +Cc: gcc-patches, tromey, gdr, joseph, burnus, charlet, bonzini

Jason Merrill <jason@redhat.com> writes:

> On 09/28/2011 03:15 PM, Dodji Seketeli wrote:
>> +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);
> ...
>> +  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;
>> +    }
>
> If we make this block conditional on arg->virt_locs being set, then we
> can pass &loc in unconditionally and don't need the track_macro_exp_p
> flag to set_arg_token.

Done.

>
>> Note that the gotos were put there also because we needed to get out
>> of the for (;;) loop, similarly to what the previous return statements
>> were doing; so by doing this doesn't we don't do get rid of the gotos.
>
> Can't you use break instead?

Yes, done.

Bootstrap underway ....

From: Dodji Seketeli <dodji@redhat.com>
Date: Sat, 4 Dec 2010 14:04:29 +0100
Subject: [PATCH 2/7] Generate virtual locations for tokens

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.

The combination of this patch and the previous one bootstraps with
--enable-languages=all,ada and passes regression tests on
x86_64-unknown-linux-gnu.

gcc/
	* 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/

	* 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/

	* 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/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/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            | 1317 ++++++++++++++++++++++++++++++++++++++++-----
 libcpp/traditional.c      |    2 +-
 14 files changed, 1330 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 e166964..4584792 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 460ffa7..3a6c08a 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 588e8ed..fba9a28 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 b56467c..ae73ed6 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 03fe79e..11374dd 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,57 @@ 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
+{
+  /* 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, otherwise, it must be NULL.  */
+  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 +99,52 @@ 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);
+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 +156,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.  */
@@ -507,7 +598,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
@@ -517,10 +608,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)
 	{
@@ -584,23 +690,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;
@@ -615,9 +735,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 (;;)
 	{
@@ -625,11 +753,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)
 	    {
@@ -686,7 +823,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);
 
@@ -700,8 +837,9 @@ 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);
+	  ntokens++;
 	}
 
       /* Drop trailing padding.  */
@@ -709,7 +847,8 @@ 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);
 
       /* Terminate the argument.  Excess arguments loop back and
 	 overwrite the final legitimate argument, before failing.  */
@@ -752,6 +891,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;
 	}
     }
@@ -765,10 +906,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;
 
@@ -785,7 +928,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
@@ -819,13 +962,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;
@@ -850,11 +995,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--;
@@ -873,8 +1020,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.  */
@@ -888,13 +1040,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)
 	{
@@ -922,33 +1105,315 @@ 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)
+{
+  const cpp_token **token_ptr;
+  source_location *loc = NULL;
+
+  token_ptr = arg_token_ptr_at (arg, index, kind, &loc);
+  *token_ptr = token;
+
+  if (loc != NULL)
+    {
+#ifdef ENABLE_CHECKING
+      if (kind == MACRO_ARG_TOKEN_STRINGIFIED
+	  || arg->virt_locs == NULL)
+	/* 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.
+   *VIRT_LOCATION is set to the address of the virtual location of the
+   returned token if the -ftrack-macro-expansion flag is on; token, so
+   virt_location must be non-NULL in that case.  */
+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 (arg->virt_locs)
+    {
+      /* -ftrack-macro-expansion is on.  So in this case
+          virt_location must be non-NULL.  */
+      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->kind = kind;
+  iter->token_ptr = token_ptr;
+  if (track_macro_exp_p)
+    iter->location_ptr = get_arg_token_location (arg, kind);
+  else
+    iter->location_ptr = NULL;
+#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->location_ptr)
+	/* This means -ftrack-macro-expansion is on.  */
+	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->location_ptr)
+    /* This means -ftrack-macro-expansion is on.   */
+    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.  */
@@ -970,67 +1435,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)
@@ -1046,7 +1674,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)
@@ -1060,13 +1693,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.  */
@@ -1094,6 +1730,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;
@@ -1110,8 +1747,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;
@@ -1122,15 +1759,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,
@@ -1138,14 +1804,197 @@ _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.   */
+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
@@ -1155,38 +2004,47 @@ _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);
+      arg->expanded_count++;
     }
 
   _cpp_pop_context (pfile);
@@ -1195,25 +2053,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
@@ -1221,12 +2186,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 (;;)
@@ -1236,20 +2209,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);
+	      break;
 	    }
 	}
       else
@@ -1257,7 +2231,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;
+	  break;
 	}
 
       if (pfile->state.in_directive && result->type == CPP_COMMENT)
@@ -1276,7 +2251,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;
@@ -1294,7 +2269,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
@@ -1311,12 +2287,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);
+	      break;
 	    }
 	}
       else
@@ -1333,27 +2311,87 @@ cpp_get_token (cpp_reader *pfile)
       break;
     }
 
+  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;
 }
 
@@ -1363,7 +2401,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;
 }
@@ -1420,10 +2458,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;
 	}
-- 
		Dodji

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

* Re: [PATCH 3/7] Emit macro expansion related diagnostics
  2011-09-27 18:43                                     ` Dodji Seketeli
@ 2011-09-29  7:05                                       ` Jason Merrill
  2011-09-29 19:20                                         ` Dodji Seketeli
  0 siblings, 1 reply; 135+ messages in thread
From: Jason Merrill @ 2011-09-29  7:05 UTC (permalink / raw)
  To: Dodji Seketeli; +Cc: gcc-patches, tromey, gdr, joseph, burnus, charlet, bonzini

On 09/27/2011 01:52 PM, Dodji Seketeli wrote:
> +     Remember we are at the expansion point of MACRO.  Each xI is the
> +     location of the Ith token of the replacement-list. Now it gets
> +     confusing. the xI is the location of the Ith token of the
> +     replacement-list at the macro *definition* point. Not at the
> +     macro replacement point. Okay, let's try to explain this below.

This should be yI.

> +     The token '+' has two locations, so to speak. One in the context
> +     of the macro *expansion* of PLUS in #2 and one in the context of
> +     the macro *definition* of PLUS in #1. These two locations are
> +     encoded in the the latter context, somehow in the xI we are
> +     talking about.

The location of '+' in #2 is not encoded in xI or yI, we reach it 
through the expansion point location of the macro.  The location in #1 
is yI (and xI, in this case).

> +     xI is roughly the index of the token inside the replacement-list
> +     at the expansion point. So for '+', it's index would then be 1

"its"

> +     [The index of token '1' would be 0 and the index of token 2 would
> +     be 1]. So if '+' is our current xI, it is actualy an x1.

Are we still talking about #1 here?  It looks to me like the index of 
"1" would be 2, the index of "+" would be 4, and the index of token "2" 
would be 6.  I bet PLUS used to just be "A + B", and this section of 
comment didn't get updated when it changed.

Keep changing xI to yI.

> +     Now what's the y1 then? Remember, we said macro_locations is an
> +     array of pairs (xI,yI). We now know what the xI is, now let's
> +     look at the yI.

xI allows us to find where the token was actually written.  If the 
current macro context is also the spelling location of the token (e.g. 
#1 for "+"), then xI is the same as yI, i.e. the source location of that 
token.

If the current macro context is not the spelling location of the token 
(e.g. #0 or #1 for "1"), then xI is the location outside the current 
macro context.  For "1" in #0, this the location of "1" in #1, which is 
a virtual location.  For "1" in #1, this is the location of "1" in #2, 
which is a source location.

> +   * 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.

The first macro expansion point that led to this macro expansion.

> +   * 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.

(and then keep these two paragraphs)

> +   Finally, if SPELLING_LOC is not NULL, *RESULTING_LOC is set to the
> +   location to which LOC was resolved

SPELLING_LOC doesn't exist anymore.

> +   ORIG_LOC is the orginal location of the token at the definition
> +   point of the macro. If you read the extensive comments of struct
> +   line_map_macro in line-map.h, this is the xI.
> +
> +   If the token is part of a macro argument, ORIG_PARM_REPLACEMENT_LOC
> +   is the location of the point at wich the token (the argument)
> +   replaces the macro parameter in the context of the relevant macro
> +   definition. If you read the comments of struct line_map_macro in
> +   line-map.h, this is the yI.  */
> +
> +source_location
> +linemap_add_macro_token (const struct line_map *map,

ORIG_LOC is the location of the token outside 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 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.

> +/* 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.  Note
> +   that the caller of M is necessarily another macro.

Why is the caller of M necessarily another macro?  In the PLUS example 
above, if we have the location for "1" in #1, won't it give us the 
location of "1" in #2?

>   The context of M is a macro definition.

What does this mean?

> +/* 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_source_line (struct line_maps *set,
> +                        source_location location)

Let's call this linemap_get_expansion_line.

> +linemap_get_source_column (struct line_maps *set,

This seems unused.

> +linemap_get_file_path (struct line_maps *set,

Shouldn't we use this for BT_FILE in _cpp_builtin_macro_text?  And call 
it linemap_get_expansion_filename.

> +   Note that this function returns 0 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);

The comment seems incorrect here.  If the location passed in corresponds 
to a non-parameter token in a macro in a system header, we'll end up 
with the source location of that token, not the expansion location. 
What behavior do we want here?

> +linemap_macro_loc_unwind (struct line_maps *set,

Let's call this linemap_macro_loc_to_spelling_point.

> +/* If LOCATION is the locus of a token that is an argument of a
> +   function-like macro M, return the location of that token in the
> +   context of the definition of the first macro P which expansion
> +   triggered the expansion of M.  Note that the token must be actually
> +   present in the source of the definition of P.  If LOCATION is the
> +   locus of a token that belongs to a macro replacement-list but is
> +   not an argument to a function-like macro, return the same thing as
> +   what linemap_macor_loc_to_def_point would have returned.

This seems unnecessarily complex.  This function returns the spelling 
location of the token wherever it comes from, whether part of a macro 
definition or not.

> linemap_macro_loc_to_exp_point,
> linemap_macro_map_loc_unwind_once and linemap_macro_map_loc_to_def_point
> are now static functions that are only subroutines of
> linemap_resolve_location.  They are not part of the public line map API
> anymore.

I still see them in line-map.h.

Let's rename linemap_macro_map_loc_unwind_once to 
"linemap_macro_map_loc_unwind_toward_spelling", and 
linemap_step_out_once to "linemap_macro_map_loc_unwind_toward_expansion".

Jason

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

* Re: [PATCH 3/7] Emit macro expansion related diagnostics
  2011-09-29  7:05                                       ` Jason Merrill
@ 2011-09-29 19:20                                         ` Dodji Seketeli
  2011-09-29 21:25                                           ` Jason Merrill
  0 siblings, 1 reply; 135+ messages in thread
From: Dodji Seketeli @ 2011-09-29 19:20 UTC (permalink / raw)
  To: Jason Merrill; +Cc: gcc-patches, tromey, gdr, joseph, burnus, charlet, bonzini

Jason Merrill <jason@redhat.com> writes:

I have rewritten the line_map_macro comment.

> > +   * 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.
> 
> The first macro expansion point that led to this macro expansion.

Updated.

> 
> > +   * 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.

Likewise.

> 
> > +   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.
> 
> (and then keep these two paragraphs)

OK.

> 
> > +   Finally, if SPELLING_LOC is not NULL, *RESULTING_LOC is set to the
> > +   location to which LOC was resolved
> 
> SPELLING_LOC doesn't exist anymore.

Removed.

> 
> > +   ORIG_LOC is the orginal location of the token at the definition
> > +   point of the macro. If you read the extensive comments of struct
> > +   line_map_macro in line-map.h, this is the xI.
> > +
> > +   If the token is part of a macro argument, ORIG_PARM_REPLACEMENT_LOC
> > +   is the location of the point at wich the token (the argument)
> > +   replaces the macro parameter in the context of the relevant macro
> > +   definition. If you read the comments of struct line_map_macro in
> > +   line-map.h, this is the yI.  */
> > +
> > +source_location
> > +linemap_add_macro_token (const struct line_map *map,
> 
> ORIG_LOC is the location of the token outside 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
> 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.

Updated.

> 
> > +/* 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.  Note
> > +   that the caller of M is necessarily another macro.
> 
> Why is the caller of M necessarily another macro? 

Hmmh.  I don't know what I was thinking.  It's not.  Updated.

> > +/* 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_source_line (struct line_maps *set,
> > +                        source_location location)
> 
> Let's call this linemap_get_expansion_line.

Done.

> 
> > +linemap_get_source_column (struct line_maps *set,
> 
> This seems unused.

Removed.

> 
> > +linemap_get_file_path (struct line_maps *set,
> 
> Shouldn't we use this for BT_FILE in _cpp_builtin_macro_text?

Yes.  Updated.

>  And call it linemap_get_expansion_filename.

Done.

> 
> > +   Note that this function returns 0 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);
> 
> The comment seems incorrect here.  If the location passed in
> corresponds to a non-parameter token in a macro in a system header,
> we'll end up with the source location of that token, not the expansion
> location. What behavior do we want here?

We want the comment to be

    Note that this function returns 1 if ....

> 
> > +linemap_macro_loc_unwind (struct line_maps *set,
> 
> Let's call this linemap_macro_loc_to_spelling_point.
> 
> > +/* If LOCATION is the locus of a token that is an argument of a
> > +   function-like macro M, return the location of that token in the
> > +   context of the definition of the first macro P which expansion
> > +   triggered the expansion of M.  Note that the token must be actually
> > +   present in the source of the definition of P.  If LOCATION is the
> > +   locus of a token that belongs to a macro replacement-list but is
> > +   not an argument to a function-like macro, return the same thing as
> > +   what linemap_macor_loc_to_def_point would have returned.
> 
> This seems unnecessarily complex.  This function returns the spelling
> location of the token wherever it comes from, whether part of a macro
> definition or not.

Updated.

> Let's rename linemap_macro_map_loc_unwind_once to
> "linemap_macro_map_loc_unwind_toward_spelling", and
> linemap_step_out_once to
> "linemap_macro_map_loc_unwind_toward_expansion".

Done.

Here are the two updated patches that are going through bootstrap at
the moment.

From: Dodji Seketeli <dodji@redhat.com>
Date: Fri, 3 Dec 2010 13:20:26 +0100
Subject: [PATCH 1/7] Linemap infrastructure for virtual locations

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.

Boostrapped with --enable-languages=all,ada and passed regression
tests on x86_unknown-linux-gnu against trunk.

libcpp/

	* 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): New macros.
	(linemap_position_for_line_and_column)
	(linemap_tracks_macro_expansion_locs_p, linemap_enter_macro)
	(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
	(linemap_assert): New macro.
	(linemap_macro_loc_to_exp_point, linemap_macro_loc_to_exp_point)
	(linemap_macro_loc_unwind): 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_enter_macro)
	(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_location_before_p):
	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.
	* 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/
	* 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/c-family

	* 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/java/

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

gcc/ada/

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

gcc/fortran/

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

gcc/testsuite/

	* gcc.dg/cpp/pragma-diagnostic-1.c: New test.
---
 gcc/ada/gcc-interface/trans.c                  |   10 +-
 gcc/c-family/c-lex.c                           |    6 +-
 gcc/c-family/c-ppoutput.c                      |   43 +-
 gcc/diagnostic.c                               |   11 +-
 gcc/fortran/cpp.c                              |   22 +-
 gcc/input.c                                    |    9 +-
 gcc/input.h                                    |   18 +-
 gcc/java/jcf-parse.c                           |    2 +-
 gcc/testsuite/gcc.dg/cpp/pragma-diagnostic-1.c |   32 +
 libcpp/directives.c                            |   16 +-
 libcpp/files.c                                 |    5 +-
 libcpp/include/line-map.h                      |  681 ++++++++++++++++++--
 libcpp/init.c                                  |    4 +-
 libcpp/internal.h                              |    3 +-
 libcpp/line-map.c                              |  863 +++++++++++++++++++++---
 libcpp/macro.c                                 |   28 +-
 16 files changed, 1546 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 71e659e..4c4bb84 100644
--- a/gcc/ada/gcc-interface/trans.c
+++ b/gcc/ada/gcc-interface/trans.c
@@ -279,7 +279,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.  */
@@ -7916,12 +7916,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..9e02e7e 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,218 @@ 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.
+
+     For a token that is /not/ an argument for a parameter of a
+     function-like macro, each yI is the spelling location of the Ith
+     token of the replacement-list in the definition of the macro.
+     "Spelling location" means the location of the place in the source
+     where the token has been spelled.
+
+     For a token that is an argument for a parameter P of a
+     function-like macro, yI is the spelling location of P in the
+     replacement-list of the macro.
+
+     Imagine this:
+
+	#define PLUS(A, B) A + B  <--- #1
+
+	int a = PLUS (1,2); <--- #2
+
+     In #2, there is a macro map for the expansion of PLUS.  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 virtual location of that
+     argument token.  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 virtual location for the argument token "1",
+     and x2 is the virtual 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.  That expansion point 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 +287,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 +421,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 +437,338 @@ 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 *);
+
+/* 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 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 *);
+
+/* If LOCATION is the locus of a token in a replacement-list of a
+   macro expansion, return the location of said token in the
+   definition of the macro.  If LOCATION is the locus of a token that
+   is an argument of a function-like macro (and that appears in the
+   expansion of a macro), return the location of the parameter (inside
+   the replacement-list of the macro) that the argument replaces.
+
+   In other words, this function returns the yI explained in the
+   comments of line_map_macro above.
+
+   Note that if the token is a builtin the function returns the
+   location of the expansion point of the macro.  */
+source_location linemap_macro_map_loc_to_def_point (const struct line_map*,
+						    source_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*,
+							      source_location);
+
+/* 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*,
+						    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 name of the macro associated to MACRO_MAP.  */
+const char* linemap_map_get_macro_name (const struct line_map*);
+
+/* 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);
+
+/* 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 TRUE if PRE denotes a location that is before POST, FALSE
+   otherwise. LINE_MAPS is the set of line maps PRE and POST were
+   allocated from.  */
+bool linemap_location_before_p (struct line_maps *set,
+				source_location   pre,
+				source_location   post);
+
+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.  */
+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.  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..588e8ed 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)
diff --git a/libcpp/line-map.c b/libcpp/line-map.c
index 2a0749a..d0bb471 100644
--- a/libcpp/line-map.c
+++ b/libcpp/line-map.c
@@ -23,24 +23,31 @@ along with this program; see the file COPYING3.  If not see
 #include "config.h"
 #include "system.h"
 #include "line-map.h"
+#include "cpplib.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_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 +58,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 +129,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)))));
 
-  if (set->used == set->allocated)
+  /* When we enter the file for the first time reason cannot be
+     LC_RENAME.  */
+  linemap_assert (!(set->depth == 0 && reason == LC_RENAME));
+
+  /* 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 +154,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 +194,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 +391,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 +419,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 +441,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 +451,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;
+
+  if (set ==  NULL || line < RESERVED_LOCATION_COUNT)
+    return NULL;
 
-  mn = set->cache;
-  mx = set->used;
+  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 +511,268 @@ 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;
+    }
+
+  while (mx - mn > 1)
+    {
+      md = (mx + mn) / 2;
+      if (MAP_START_LOCATION (LINEMAPS_MACRO_MAP_AT (set, md)) > line)
+	mn = md;
+      else
+	mx = md;
+    }
+
+  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);
+}
+
+/* Return TRUE if PRE denotes a location that is before POST, FALSE
+   otherwise. LINE_MAPS is the set of line maps PRE and POST were
+   allocated from.  */
+
+bool
+linemap_location_before_p (struct line_maps *set,
+			   source_location  pre,
+			   source_location post)
+{
+  bool pre_from_macro_p, post_from_macro_p;
+
+  if (pre == post)
+    return false;
+
+  pre_from_macro_p =
+    linemap_location_from_macro_expansion_p (set, pre);
+  post_from_macro_p =
+    linemap_location_from_macro_expansion_p (set, post);
+
+  if (pre_from_macro_p != post_from_macro_p)
+    {
+      if (pre_from_macro_p)
+	pre = linemap_macro_loc_to_exp_point (set, pre, NULL);
+      else
+	post = linemap_macro_loc_to_exp_point (set, post, NULL);
+    }
+
+  return pre < post;
 }
 
 /* Print an include trace, for e.g. the -H option of the preprocessor.  */
@@ -313,5 +784,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.  */
+
+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.  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.2

From: Dodji Seketeli <dodji@redhat.com>
Date: Sat, 4 Dec 2010 16:31:35 +0100
Subject: [PATCH 3/7] Emit macro expansion related diagnostics

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

The combination of this patch and the previous ones boostrapped with
--enable-languages=all,ada and passed regression tests on
x86_64-unknown-linux-gnu.

gcc/
	* 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/

	* 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/

	* 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.

Addendum patch 0003

gcc/

	* diagnostic.c (diagnostic_report_current_module): Adjust for the
	renaming of LRK_MACRO_PARM_REPLACEMENT_POINT into
	LRK_MACRO_DEFINITION_LOCATION.
	* tree-diagnostic.c (maybe_unwind_expanded_macro_loc): Use the new
	linemap_step_out_once.  Adjust like above.
---
 gcc/Makefile.in                                 |    3 +-
 gcc/cp/error.c                                  |    5 +-
 gcc/diagnostic.c                                |   13 +-
 gcc/diagnostic.h                                |    2 +-
 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 +-
 13 files changed, 305 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 2f320a9..aadd50c 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.2

-- 
		Dodji

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

* Re: [PATCH 3/7] Emit macro expansion related diagnostics
  2011-09-29 19:20                                         ` Dodji Seketeli
@ 2011-09-29 21:25                                           ` Jason Merrill
  2011-09-29 23:33                                             ` Dodji Seketeli
  0 siblings, 1 reply; 135+ messages in thread
From: Jason Merrill @ 2011-09-29 21:25 UTC (permalink / raw)
  To: Dodji Seketeli; +Cc: gcc-patches, tromey, gdr, joseph, burnus, charlet, bonzini

On 09/29/2011 01:19 PM, Dodji Seketeli wrote:
> +     For a token that is /not/ an argument for a parameter of a
> +     function-like macro, each yI is the spelling location of the Ith
> +     token of the replacement-list in the definition of the macro.
> +     "Spelling location" means the location of the place in the source
> +     where the token has been spelled.
> +
> +     For a token that is an argument for a parameter P of a
> +     function-like macro, yI is the spelling location of P in the
> +     replacement-list of the macro.

I think think this can be simpler:

yI is the location of the token in the macro definition, either of the 
token itself or of a macro parameter that it replaces.

> +     In #2, there is a macro map for the expansion of PLUS.  PLUS is

The macro map doesn't seem to me to be in #2, but the expansion is. 
Let's reword that to "There is a macro map for the expansion of PLUS in #2".

> +     x0 is the virtual location for the argument token "1",
> +     and x2 is the virtual location for the argument token "2".  */

Are they actually virtual in this case?  I thought they would be the 
source locations in #2 since the expanding context isn't another macro.

Jason

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

* Re: [PATCH 3/7] Emit macro expansion related diagnostics
  2011-09-29 21:25                                           ` Jason Merrill
@ 2011-09-29 23:33                                             ` Dodji Seketeli
  2011-09-30 15:56                                               ` Jason Merrill
  0 siblings, 1 reply; 135+ messages in thread
From: Dodji Seketeli @ 2011-09-29 23:33 UTC (permalink / raw)
  To: Jason Merrill; +Cc: gcc-patches, tromey, gdr, joseph, burnus, charlet, bonzini

Jason Merrill <jason@redhat.com> writes:

> On 09/29/2011 01:19 PM, Dodji Seketeli wrote:
>> +     For a token that is /not/ an argument for a parameter of a
>> +     function-like macro, each yI is the spelling location of the Ith
>> +     token of the replacement-list in the definition of the macro.
>> +     "Spelling location" means the location of the place in the source
>> +     where the token has been spelled.
>> +
>> +     For a token that is an argument for a parameter P of a
>> +     function-like macro, yI is the spelling location of P in the
>> +     replacement-list of the macro.
>
> I think think this can be simpler:
>
> yI is the location of the token in the macro definition, either of the
> token itself or of a macro parameter that it replaces.

Updated.

>
>> +     In #2, there is a macro map for the expansion of PLUS.  PLUS is
>
> The macro map doesn't seem to me to be in #2, but the expansion
> is. Let's reword that to "There is a macro map for the expansion of
> PLUS in #2".

Likewise.

>
>> +     x0 is the virtual location for the argument token "1",
>> +     and x2 is the virtual location for the argument token "2".  */
>
> Are they actually virtual in this case?  I thought they would be the
> source locations in #2 since the expanding context isn't another
> macro.

You are right.  I have updated the comment here.

The previous patch finally passed bootstrap and tests.  It's equal to
this one modulo the comments changes.

From: Dodji Seketeli <dodji@redhat.com>
Date: Fri, 3 Dec 2010 13:20:26 +0100
Subject: [PATCH 1/7] Linemap infrastructure for virtual locations

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.

Boostrapped with --enable-languages=all,ada and passed regression
tests on x86_unknown-linux-gnu against trunk.

libcpp/

	* 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): New macros.
	(linemap_position_for_line_and_column)
	(linemap_tracks_macro_expansion_locs_p, linemap_enter_macro)
	(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
	(linemap_assert): New macro.
	(linemap_macro_loc_to_exp_point, linemap_macro_loc_to_exp_point)
	(linemap_macro_loc_unwind): 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_enter_macro)
	(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_location_before_p):
	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.
	* 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/
	* 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/c-family

	* 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/java/

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

gcc/ada/

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

gcc/fortran/

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

gcc/testsuite/

	* gcc.dg/cpp/pragma-diagnostic-1.c: New test.
---
 gcc/ada/gcc-interface/trans.c                  |   10 +-
 gcc/c-family/c-lex.c                           |    6 +-
 gcc/c-family/c-ppoutput.c                      |   43 +-
 gcc/diagnostic.c                               |   11 +-
 gcc/fortran/cpp.c                              |   22 +-
 gcc/input.c                                    |    9 +-
 gcc/input.h                                    |   18 +-
 gcc/java/jcf-parse.c                           |    2 +-
 gcc/testsuite/gcc.dg/cpp/pragma-diagnostic-1.c |   32 +
 libcpp/directives.c                            |   16 +-
 libcpp/files.c                                 |    5 +-
 libcpp/include/line-map.h                      |  675 +++++++++++++++++--
 libcpp/init.c                                  |    4 +-
 libcpp/internal.h                              |    3 +-
 libcpp/line-map.c                              |  863 +++++++++++++++++++++---
 libcpp/macro.c                                 |   28 +-
 16 files changed, 1540 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 71e659e..4c4bb84 100644
--- a/gcc/ada/gcc-interface/trans.c
+++ b/gcc/ada/gcc-interface/trans.c
@@ -279,7 +279,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.  */
@@ -7916,12 +7916,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..8edd227 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,212 @@ 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 either the virtual location of
+     that argument token if it comes from a macro expansion itself, or
+     its spelling location.
+
+     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.  That expansion point 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 +281,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 +415,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 +431,338 @@ 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 *);
+
+/* 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 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 *);
+
+/* If LOCATION is the locus of a token in a replacement-list of a
+   macro expansion, return the location of said token in the
+   definition of the macro.  If LOCATION is the locus of a token that
+   is an argument of a function-like macro (and that appears in the
+   expansion of a macro), return the location of the parameter (inside
+   the replacement-list of the macro) that the argument replaces.
+
+   In other words, this function returns the yI explained in the
+   comments of line_map_macro above.
+
+   Note that if the token is a builtin the function returns the
+   location of the expansion point of the macro.  */
+source_location linemap_macro_map_loc_to_def_point (const struct line_map*,
+						    source_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*,
+							      source_location);
+
+/* 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*,
+						    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 name of the macro associated to MACRO_MAP.  */
+const char* linemap_map_get_macro_name (const struct line_map*);
+
+/* 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);
+
+/* 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 TRUE if PRE denotes a location that is before POST, FALSE
+   otherwise. LINE_MAPS is the set of line maps PRE and POST were
+   allocated from.  */
+bool linemap_location_before_p (struct line_maps *set,
+				source_location   pre,
+				source_location   post);
+
+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.  */
+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.  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..588e8ed 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)
diff --git a/libcpp/line-map.c b/libcpp/line-map.c
index 2a0749a..d0bb471 100644
--- a/libcpp/line-map.c
+++ b/libcpp/line-map.c
@@ -23,24 +23,31 @@ along with this program; see the file COPYING3.  If not see
 #include "config.h"
 #include "system.h"
 #include "line-map.h"
+#include "cpplib.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_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 +58,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 +129,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)))));
 
-  if (set->used == set->allocated)
+  /* When we enter the file for the first time reason cannot be
+     LC_RENAME.  */
+  linemap_assert (!(set->depth == 0 && reason == LC_RENAME));
+
+  /* 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 +154,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 +194,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 +391,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 +419,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 +441,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 +451,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;
+
+  if (set ==  NULL || line < RESERVED_LOCATION_COUNT)
+    return NULL;
 
-  mn = set->cache;
-  mx = set->used;
+  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 +511,268 @@ 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;
+    }
+
+  while (mx - mn > 1)
+    {
+      md = (mx + mn) / 2;
+      if (MAP_START_LOCATION (LINEMAPS_MACRO_MAP_AT (set, md)) > line)
+	mn = md;
+      else
+	mx = md;
+    }
+
+  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);
+}
+
+/* Return TRUE if PRE denotes a location that is before POST, FALSE
+   otherwise. LINE_MAPS is the set of line maps PRE and POST were
+   allocated from.  */
+
+bool
+linemap_location_before_p (struct line_maps *set,
+			   source_location  pre,
+			   source_location post)
+{
+  bool pre_from_macro_p, post_from_macro_p;
+
+  if (pre == post)
+    return false;
+
+  pre_from_macro_p =
+    linemap_location_from_macro_expansion_p (set, pre);
+  post_from_macro_p =
+    linemap_location_from_macro_expansion_p (set, post);
+
+  if (pre_from_macro_p != post_from_macro_p)
+    {
+      if (pre_from_macro_p)
+	pre = linemap_macro_loc_to_exp_point (set, pre, NULL);
+      else
+	post = linemap_macro_loc_to_exp_point (set, post, NULL);
+    }
+
+  return pre < post;
 }
 
 /* Print an include trace, for e.g. the -H option of the preprocessor.  */
@@ -313,5 +784,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.  */
+
+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.  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.2


-- 
		Dodji

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

* Re: [PATCH 3/7] Emit macro expansion related diagnostics
  2011-09-29 23:33                                             ` Dodji Seketeli
@ 2011-09-30 15:56                                               ` Jason Merrill
  2011-09-30 21:04                                                 ` Jason Merrill
  2011-10-03 20:09                                                 ` Dodji Seketeli
  0 siblings, 2 replies; 135+ messages in thread
From: Jason Merrill @ 2011-09-30 15:56 UTC (permalink / raw)
  To: Dodji Seketeli; +Cc: gcc-patches, tromey, gdr, joseph, burnus, charlet, bonzini

On 09/29/2011 05:21 PM, Dodji Seketeli wrote:
> +     When the token is /not/ an argument for a macro, xI is the same
> +     location as yI.  Otherwise, xI is either the virtual location of
> +     that argument token if it comes from a macro expansion itself, or
> +     its spelling location.

I think this could be a little clearer:

.... 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.

> +  /* This is the location of the expansion point of the current macro
> +     map.  That expansion point 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.  */

This should clarify what location the expansion point is: is it the 
location of the macro name?  the closing paren?

> +source_location linemap_macro_map_loc_to_def_point (const struct line_map*,
> +                                                   source_location);
> +source_location linemap_macro_map_loc_unwind_toward_spelling (const struct line
> _map*,
> +                                                             source_location);
> +source_location linemap_macro_map_loc_to_exp_point (const struct line_map*,
> +                                                   source_location);

These should be static.

> +const struct line_map *linemap_enter_macro (struct line_maps *,
> +                                           struct cpp_hashnode*,
> +                                           source_location,
> +                                           unsigned int);
> +source_location linemap_add_macro_token (const struct line_map *,
> +                                        unsigned int,
> +                                        source_location,
> +                                        source_location);
> +int linemap_get_expansion_line (struct line_maps *,
> +                               source_location);
> +const char* linemap_get_expansion_filename (struct line_maps *,
> +                                           source_location);

And these should be declared in an internal header, not the public 
include/line-map.h.

> +/* Expand source code location LOC and return a user readable source
> +   code 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.  The LRK parameter is the same as for
> +   linemap_resolve_location.  */
> +
> +expanded_location linemap_expand_location_full (struct line_maps *,

These comments should clarify that the first function only takes 
spelling locations, while the second also takes virtual locations.

> -finish_declspecs (struct c_declspecs *specs)
> +finish_declspecs (struct c_declspecs *specs,
> +                 location_t where)

I'm not sure the beginning of the declspecs is a better place for these 
diagnostics than the beginning of the declarator.  Why make this change?

Jason

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

* Re: [PATCH 3/7] Emit macro expansion related diagnostics
  2011-09-30 15:56                                               ` Jason Merrill
@ 2011-09-30 21:04                                                 ` Jason Merrill
  2011-10-03 22:50                                                   ` Dodji Seketeli
  2011-10-03 20:09                                                 ` Dodji Seketeli
  1 sibling, 1 reply; 135+ messages in thread
From: Jason Merrill @ 2011-09-30 21:04 UTC (permalink / raw)
  To: Dodji Seketeli; +Cc: gcc-patches, tromey, gdr, joseph, burnus, charlet, bonzini

On 09/30/2011 11:28 AM, Jason Merrill wrote:
> +linemap_location_before_p (struct line_maps *set,
> +                          source_location  pre,
> +                          source_location post)
> +{
> +  bool pre_from_macro_p, post_from_macro_p;
> +
> +  if (pre == post)
> +    return false;
> +
> +  pre_from_macro_p =
> +    linemap_location_from_macro_expansion_p (set, pre);
> +  post_from_macro_p =
> +    linemap_location_from_macro_expansion_p (set, post);
> +
> +  if (pre_from_macro_p != post_from_macro_p)
> +    {
> +      if (pre_from_macro_p)
> +       pre = linemap_macro_loc_to_exp_point (set, pre, NULL);
> +      else
> +       post = linemap_macro_loc_to_exp_point (set, post, NULL);
> +    }

What if the two locations are from different macros?

Jason

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

* Re: [PATCH 3/7] Emit macro expansion related diagnostics
  2011-09-30 15:56                                               ` Jason Merrill
  2011-09-30 21:04                                                 ` Jason Merrill
@ 2011-10-03 20:09                                                 ` Dodji Seketeli
  2011-10-04 20:03                                                   ` Jason Merrill
  1 sibling, 1 reply; 135+ messages in thread
From: Dodji Seketeli @ 2011-10-03 20:09 UTC (permalink / raw)
  To: Jason Merrill; +Cc: gcc-patches, tromey, gdr, joseph, burnus, charlet, bonzini

Jason Merrill <jason@redhat.com> writes:

> On 09/29/2011 05:21 PM, Dodji Seketeli wrote:
>> +     When the token is /not/ an argument for a macro, xI is the same
>> +     location as yI.  Otherwise, xI is either the virtual location of
>> +     that argument token if it comes from a macro expansion itself, or
>> +     its spelling location.
>
> I think this could be a little clearer:
>
> .... 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.

Done.


>> +  /* This is the location of the expansion point of the current macro
>> +     map.  That expansion point 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.  */
>
> This should clarify what location the expansion point is: is it the
> location of the macro name?  the closing paren?

It's the location of the macro name.  Updated.


>> +source_location linemap_macro_map_loc_to_def_point (const struct line_map*,
>> +                                                   source_location);
>> +source_location linemap_macro_map_loc_unwind_toward_spelling (const struct line
>> _map*,
>> +                                                             source_location);
>> +source_location linemap_macro_map_loc_to_exp_point (const struct line_map*,
>> +                                                   source_location);
>
> These should be static.

Done.


>> +const struct line_map *linemap_enter_macro (struct line_maps *,
>> +                                           struct cpp_hashnode*,
>> +                                           source_location,
>> +                                           unsigned int);
>> +source_location linemap_add_macro_token (const struct line_map *,
>> +                                        unsigned int,
>> +                                        source_location,
>> +                                        source_location);
>> +int linemap_get_expansion_line (struct line_maps *,
>> +                               source_location);
>> +const char* linemap_get_expansion_filename (struct line_maps *,
>> +                                           source_location);
>
> And these should be declared in an internal header, not the public
> include/line-map.h.

Done.  I have put them in internal.h.

>> +/* Expand source code location LOC and return a user readable source
>> +   code 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.  The LRK parameter is the same as for
>> +   linemap_resolve_location.  */
>> +
>> +expanded_location linemap_expand_location_full (struct line_maps *,
>
> These comments should clarify that the first function only takes
> spelling locations, while the second also takes virtual locations.

Added.


>> -finish_declspecs (struct c_declspecs *specs)
>> +finish_declspecs (struct c_declspecs *specs,
>> +                 location_t where)
>
> I'm not sure the beginning of the declspecs is a better place for
> these diagnostics than the beginning of the declarator.

To fix the particular example of PR 7263 (and the example
gcc/testsuite/gcc.dg/cpp/syshdr3.c that I added to that particular
patch) where the declspec is what is defined in the system header, and
the declaration (declspec + declarator) gets assembled in the source, we
want pedwarn[1] (really diagnostic_report_diagnostic) to detect that the
declspec is spelled in a system header.  So we want pedwarn to be passed
the location of a declspec.  I believe you noted this at some point and
agreed with me that ideally each declspec should come with its location
but that's work for another occasion.

[1]:  E.g, in this hunk:

@@ -9647,7 +9651,7 @@ finish_declspecs (struct c_declspecs *specs)
 		       : integer_type_node);
       if (specs->complex_p)
 	{
-	  pedwarn (input_location, OPT_pedantic,
+	  pedwarn (first_token_loc, OPT_pedantic,
 		   "ISO C does not support complex integer types");
 	  specs->type = build_complex_type (specs->type);
 	}

FWIW, here is what I have updated and tested so far.

From: Dodji Seketeli <dodji@redhat.com>
Date: Fri, 3 Dec 2010 13:20:26 +0100
Subject: [PATCH 1/7] Linemap infrastructure for virtual locations

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.

Boostrapped with --enable-languages=all,ada and passed regression
tests on x86_unknown-linux-gnu against trunk.

libcpp/

	* 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): 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): 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_location_before_p):
	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/
	* 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/c-family

	* 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/java/

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

gcc/ada/

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

gcc/fortran/

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

gcc/testsuite/

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

bleah
---
 gcc/ada/gcc-interface/trans.c                  |   10 +-
 gcc/c-family/c-lex.c                           |    6 +-
 gcc/c-family/c-ppoutput.c                      |   43 +-
 gcc/diagnostic.c                               |   11 +-
 gcc/fortran/cpp.c                              |   22 +-
 gcc/input.c                                    |    9 +-
 gcc/input.h                                    |   18 +-
 gcc/java/jcf-parse.c                           |    2 +-
 gcc/testsuite/gcc.dg/cpp/pragma-diagnostic-1.c |   32 +
 libcpp/directives.c                            |   16 +-
 libcpp/files.c                                 |    5 +-
 libcpp/include/line-map.h                      |  578 +++++++++++++++--
 libcpp/init.c                                  |    4 +-
 libcpp/internal.h                              |   73 ++-
 libcpp/line-map.c                              |  869 +++++++++++++++++++++---
 libcpp/macro.c                                 |   28 +-
 16 files changed, 1519 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 71e659e..4c4bb84 100644
--- a/gcc/ada/gcc-interface/trans.c
+++ b/gcc/ada/gcc-interface/trans.c
@@ -279,7 +279,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.  */
@@ -7916,12 +7916,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..c0a9069 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,239 @@ 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 TRUE if PRE denotes a location that is before POST, FALSE
+   otherwise. LINE_MAPS is the set of line maps PRE and POST were
+   allocated from.  */
+bool linemap_location_before_p (struct line_maps *set,
+				source_location   pre,
+				source_location   post);
+
+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..1ac1e7a 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)))));
 
-  if (set->used == set->allocated)
+  /* When we enter the file for the first time reason cannot be
+     LC_RENAME.  */
+  linemap_assert (!(set->depth == 0 && reason == LC_RENAME));
+
+  /* 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;
+
+  if (set ==  NULL || line < RESERVED_LOCATION_COUNT)
+    return NULL;
 
-  mn = set->cache;
-  mx = set->used;
+  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,268 @@ 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;
+    }
+
+  while (mx - mn > 1)
+    {
+      md = (mx + mn) / 2;
+      if (MAP_START_LOCATION (LINEMAPS_MACRO_MAP_AT (set, md)) > line)
+	mn = md;
+      else
+	mx = md;
+    }
+
+  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);
+}
+
+/* Return TRUE if PRE denotes a location that is before POST, FALSE
+   otherwise. LINE_MAPS is the set of line maps PRE and POST were
+   allocated from.  */
+
+bool
+linemap_location_before_p (struct line_maps *set,
+			   source_location  pre,
+			   source_location post)
+{
+  bool pre_from_macro_p, post_from_macro_p;
+
+  if (pre == post)
+    return false;
+
+  pre_from_macro_p =
+    linemap_location_from_macro_expansion_p (set, pre);
+  post_from_macro_p =
+    linemap_location_from_macro_expansion_p (set, post);
+
+  if (pre_from_macro_p != post_from_macro_p)
+    {
+      if (pre_from_macro_p)
+	pre = linemap_macro_loc_to_exp_point (set, pre, NULL);
+      else
+	post = linemap_macro_loc_to_exp_point (set, post, NULL);
+    }
+
+  return pre < post;
 }
 
 /* Print an include trace, for e.g. the -H option of the preprocessor.  */
@@ -313,5 +790,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


-- 
		Dodji

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

* Re: [PATCH 3/7] Emit macro expansion related diagnostics
  2011-09-30 21:04                                                 ` Jason Merrill
@ 2011-10-03 22:50                                                   ` Dodji Seketeli
  2011-10-04 19:59                                                     ` Jason Merrill
  0 siblings, 1 reply; 135+ messages in thread
From: Dodji Seketeli @ 2011-10-03 22:50 UTC (permalink / raw)
  To: Jason Merrill; +Cc: gcc-patches, tromey, gdr, joseph, burnus, charlet, bonzini

Jason Merrill <jason@redhat.com> writes:

> On 09/30/2011 11:28 AM, Jason Merrill wrote:
>> +linemap_location_before_p (struct line_maps *set,
>> +                          source_location  pre,
>> +                          source_location post)
>> +{
>> +  bool pre_from_macro_p, post_from_macro_p;
>> +
>> +  if (pre == post)
>> +    return false;
>> +
>> +  pre_from_macro_p =
>> +    linemap_location_from_macro_expansion_p (set, pre);
>> +  post_from_macro_p =
>> +    linemap_location_from_macro_expansion_p (set, post);
>> +
>> +  if (pre_from_macro_p != post_from_macro_p)
>> +    {
>> +      if (pre_from_macro_p)
>> +       pre = linemap_macro_loc_to_exp_point (set, pre, NULL);
>> +      else
>> +       post = linemap_macro_loc_to_exp_point (set, post, NULL);
>> +    }
>
> What if the two locations are from different macros?

Good question.  Is the below better?

From: Dodji Seketeli <dodji@redhat.com>
Date: Fri, 3 Dec 2010 13:20:26 +0100
Subject: [PATCH 1/7] Linemap infrastructure for virtual locations

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.

Boostrapped with --enable-languages=all,ada and passed regression
tests on x86_unknown-linux-gnu against trunk.

libcpp/

	* 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): 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): 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_location_before_p):
	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/
	* 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/c-family

	* 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/java/

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

gcc/ada/

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

gcc/fortran/

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

gcc/testsuite/

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

bleah
---
 gcc/ada/gcc-interface/trans.c                  |   10 +-
 gcc/c-family/c-lex.c                           |    6 +-
 gcc/c-family/c-ppoutput.c                      |   43 +-
 gcc/diagnostic.c                               |   11 +-
 gcc/fortran/cpp.c                              |   22 +-
 gcc/input.c                                    |    9 +-
 gcc/input.h                                    |   18 +-
 gcc/java/jcf-parse.c                           |    2 +-
 gcc/testsuite/gcc.dg/cpp/pragma-diagnostic-1.c |   32 +
 libcpp/directives.c                            |   16 +-
 libcpp/files.c                                 |    5 +-
 libcpp/include/line-map.h                      |  578 +++++++++++++++--
 libcpp/init.c                                  |    4 +-
 libcpp/internal.h                              |   73 ++-
 libcpp/line-map.c                              |  862 +++++++++++++++++++++---
 libcpp/macro.c                                 |   28 +-
 16 files changed, 1512 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 71e659e..4c4bb84 100644
--- a/gcc/ada/gcc-interface/trans.c
+++ b/gcc/ada/gcc-interface/trans.c
@@ -279,7 +279,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.  */
@@ -7916,12 +7916,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..c0a9069 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,239 @@ 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 TRUE if PRE denotes a location that is before POST, FALSE
+   otherwise. LINE_MAPS is the set of line maps PRE and POST were
+   allocated from.  */
+bool linemap_location_before_p (struct line_maps *set,
+				source_location   pre,
+				source_location   post);
+
+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..05b710c 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)))));
 
-  if (set->used == set->allocated)
+  /* When we enter the file for the first time reason cannot be
+     LC_RENAME.  */
+  linemap_assert (!(set->depth == 0 && reason == LC_RENAME));
+
+  /* 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,261 @@ 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;
+    }
+
+  while (mx - mn > 1)
+    {
+      md = (mx + mn) / 2;
+      if (MAP_START_LOCATION (LINEMAPS_MACRO_MAP_AT (set, md)) > line)
+	mn = md;
+      else
+	mx = md;
+    }
+
+  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);
+}
+
+/* Return TRUE if PRE denotes a location that is before POST, FALSE
+   otherwise. LINE_MAPS is the set of line maps PRE and POST were
+   allocated from.  */
+
+bool
+linemap_location_before_p (struct line_maps *set,
+			   source_location  pre,
+			   source_location post)
+{
+  if (pre == post)
+    return false;
+
+  if (linemap_location_from_macro_expansion_p (set, pre))
+    pre = linemap_resolve_location (set, pre,
+				    LRK_MACRO_EXPANSION_POINT, NULL);
+
+  if (linemap_location_from_macro_expansion_p (set, post))
+    post = linemap_resolve_location (set, post,
+				     LRK_MACRO_EXPANSION_POINT,
+				     NULL);
+  return pre < post;
 }
 
 /* Print an include trace, for e.g. the -H option of the preprocessor.  */
@@ -313,5 +783,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



-- 
		Dodji

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

* Re: [PATCH 3/7] Emit macro expansion related diagnostics
  2011-10-03 22:50                                                   ` Dodji Seketeli
@ 2011-10-04 19:59                                                     ` Jason Merrill
  2011-10-04 20:35                                                       ` Dodji Seketeli
  0 siblings, 1 reply; 135+ messages in thread
From: Jason Merrill @ 2011-10-04 19:59 UTC (permalink / raw)
  To: Dodji Seketeli; +Cc: gcc-patches, tromey, gdr, joseph, burnus, charlet, bonzini

On 10/03/2011 06:49 PM, Dodji Seketeli wrote:
> Good question.  Is the below better?

> +  if (linemap_location_from_macro_expansion_p (set, pre))
> +    pre = linemap_resolve_location (set, pre,
> +                                   LRK_MACRO_EXPANSION_POINT, NULL);
> +
> +  if (linemap_location_from_macro_expansion_p (set, post))
> +    post = linemap_resolve_location (set, post,
> +                                    LRK_MACRO_EXPANSION_POINT,
> +                                    NULL);

This looks like two different virtual locations from the same macro 
expansion will compare equal.

Jason

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

* Re: [PATCH 3/7] Emit macro expansion related diagnostics
  2011-10-03 20:09                                                 ` Dodji Seketeli
@ 2011-10-04 20:03                                                   ` Jason Merrill
  2011-10-04 20:28                                                     ` Dodji Seketeli
  0 siblings, 1 reply; 135+ messages in thread
From: Jason Merrill @ 2011-10-04 20:03 UTC (permalink / raw)
  To: Dodji Seketeli; +Cc: gcc-patches, tromey, gdr, joseph, burnus, charlet, bonzini

On 10/03/2011 04:08 PM, Dodji Seketeli wrote:
> Jason Merrill<jason@redhat.com>  writes:
>>> -finish_declspecs (struct c_declspecs *specs)
>>> +finish_declspecs (struct c_declspecs *specs,
>>> +                 location_t where)
>>
>> I'm not sure the beginning of the declspecs is a better place for
>> these diagnostics than the beginning of the declarator.
>
> To fix the particular example of PR 7263 (and the example
> gcc/testsuite/gcc.dg/cpp/syshdr3.c that I added to that particular
> patch) where the declspec is what is defined in the system header, and
> the declaration (declspec + declarator) gets assembled in the source, we
> want pedwarn[1] (really diagnostic_report_diagnostic) to detect that the
> declspec is spelled in a system header.  So we want pedwarn to be passed
> the location of a declspec.

So then this change would make

_Complex c;

OK, but not

static _Complex c;

because the first declspec is not from a macro, right?

> I believe you noted this at some point and
> agreed with me that ideally each declspec should come with its location
> but that's work for another occasion.

If I'm right about the above example, I think I'd rather hold off this 
declspecs change until that time.

Jason

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

* Re: [PATCH 3/7] Emit macro expansion related diagnostics
  2011-10-04 20:03                                                   ` Jason Merrill
@ 2011-10-04 20:28                                                     ` Dodji Seketeli
  0 siblings, 0 replies; 135+ messages in thread
From: Dodji Seketeli @ 2011-10-04 20:28 UTC (permalink / raw)
  To: Jason Merrill; +Cc: gcc-patches, tromey, gdr, joseph, burnus, charlet, bonzini

Jason Merrill <jason@redhat.com> writes:

> So then this change would make
>
> _Complex c;
>
> OK, but not
>
> static _Complex c;
>
> because the first declspec is not from a macro, right?

Yes.

>
>> I believe you noted this at some point and
>> agreed with me that ideally each declspec should come with its location
>> but that's work for another occasion.
>
> If I'm right about the above example, I think I'd rather hold off this
> declspecs change until that time.

OK.

-- 
		Dodji

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

* Re: [PATCH 3/7] Emit macro expansion related diagnostics
  2011-10-04 19:59                                                     ` Jason Merrill
@ 2011-10-04 20:35                                                       ` Dodji Seketeli
  0 siblings, 0 replies; 135+ messages in thread
From: Dodji Seketeli @ 2011-10-04 20:35 UTC (permalink / raw)
  To: Jason Merrill; +Cc: gcc-patches, tromey, gdr, joseph, burnus, charlet, bonzini

Jason Merrill <jason@redhat.com> writes:

> On 10/03/2011 06:49 PM, Dodji Seketeli wrote:
>> Good question.  Is the below better?
>
>> +  if (linemap_location_from_macro_expansion_p (set, pre))
>> +    pre = linemap_resolve_location (set, pre,
>> +                                   LRK_MACRO_EXPANSION_POINT, NULL);
>> +
>> +  if (linemap_location_from_macro_expansion_p (set, post))
>> +    post = linemap_resolve_location (set, post,
>> +                                    LRK_MACRO_EXPANSION_POINT,
>> +                                    NULL);
>
> This looks like two different virtual locations from the same macro
> expansion will compare equal.

Yes.  I thought this was enough to not regress compared to the current
situation where two tokens from the same macro expansion have the
spelling location of macro expansion point anyway.  This is used for
#pragma disagnostic changes in diagnostic_report_diagnostic.

Otherwise, I am not sure how this should behave wrt nested macro
expansions.

-- 
		Dodji

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

* [PATCH 5/6] Add line map statistics to -fmem-report output
  2011-10-17  9:58 [PATCH 0/6] " Dodji Seketeli
@ 2011-10-17 10:22 ` Dodji Seketeli
  0 siblings, 0 replies; 135+ 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] 135+ messages in thread

end of thread, other threads:[~2011-10-17  9:57 UTC | newest]

Thread overview: 135+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2010-12-10 11:27 [PATCH 0/6] Tracking locations of tokens resulting from macro expansion Dodji Seketeli
2010-12-10 11:16 ` [PATCH 0/6] *** SUBJECT HERE *** Dodji Seketeli
2010-12-10 12:56   ` Dave Korn
2010-12-10 11:16 ` [PATCH 4/6] Support -fdebug-cpp option Dodji Seketeli
2010-12-10 11:27 ` [PATCH 3/6] Emit macro expansion related diagnostics Dodji Seketeli
2010-12-13 15:25   ` Paolo Bonzini
2010-12-13 15:38     ` Paolo Bonzini
2010-12-13 16:30     ` Manuel López-Ibáñez
2010-12-14  7:24     ` Dodji Seketeli
2010-12-14  7:28       ` Gabriel Dos Reis
2010-12-14  8:40         ` Dodji Seketeli
2010-12-14  9:38           ` Gabriel Dos Reis
2010-12-14  9:42             ` Dodji Seketeli
2010-12-14  9:48               ` Gabriel Dos Reis
2010-12-14  7:28     ` Dodji Seketeli
2010-12-14  8:19       ` Gabriel Dos Reis
2010-12-14  8:31         ` Paolo Bonzini
2010-12-14  9:23           ` 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
2010-12-10 11:53 ` [PATCH 1/6] Linemap infrastructure for virtual locations Dodji Seketeli
2011-01-06 16:48   ` Tom Tromey
2011-04-12 14:39     ` Dodji Seketeli
2011-04-14 14:46       ` Tom Tromey
2010-12-10 12:27 ` [PATCH 2/6] Generate virtual locations for tokens Dodji Seketeli
2010-12-10 12:33 ` [PATCH 6/6] Kill pedantic warnings on system headers macros Dodji Seketeli
2010-12-10 12:52 ` [PATCH 0/6] Tracking locations of tokens resulting from macro expansion Gabriel Dos Reis
2010-12-10 18:22   ` Dodji Seketeli
2010-12-10 16:59 ` Jeff Law
2010-12-10 19:00   ` Dodji Seketeli
2010-12-13 15:10     ` Jeff Law
2010-12-13 16:35       ` Gabriel Dos Reis
2010-12-14  9:24         ` Dodji Seketeli
2010-12-14  9:40           ` Gabriel Dos Reis
2010-12-14  9:45             ` Dodji Seketeli
2011-07-16 14:38 ` [PATCH 0/7] " Dodji Seketeli
     [not found]   ` <cover.1310824120.git.dodji@redhat.com>
2011-07-16 14:38     ` [PATCH 3/7] Emit macro expansion related diagnostics Dodji Seketeli
2011-08-04 15:32       ` Dodji Seketeli
2011-09-12 21:54         ` Jason Merrill
2011-09-16  8:19           ` Dodji Seketeli
2011-09-17 21:27             ` Jason Merrill
2011-09-19 14:37               ` Dodji Seketeli
2011-09-19 19:47                 ` Jason Merrill
2011-09-19 21:27                   ` Jason Merrill
2011-09-20  8:47                     ` Dodji Seketeli
2011-09-20 14:12                       ` Jason Merrill
2011-09-20 14:12                         ` Dodji Seketeli
2011-09-21  2:51                           ` Jason Merrill
2011-09-21 19:09                             ` Dodji Seketeli
2011-09-22 15:32                               ` Jason Merrill
2011-09-26 21:11                                 ` Dodji Seketeli
2011-09-26 22:30                                   ` Jason Merrill
2011-09-27 18:43                                     ` Dodji Seketeli
2011-09-29  7:05                                       ` Jason Merrill
2011-09-29 19:20                                         ` Dodji Seketeli
2011-09-29 21:25                                           ` Jason Merrill
2011-09-29 23:33                                             ` Dodji Seketeli
2011-09-30 15:56                                               ` Jason Merrill
2011-09-30 21:04                                                 ` Jason Merrill
2011-10-03 22:50                                                   ` Dodji Seketeli
2011-10-04 19:59                                                     ` Jason Merrill
2011-10-04 20:35                                                       ` Dodji Seketeli
2011-10-03 20:09                                                 ` Dodji Seketeli
2011-10-04 20:03                                                   ` Jason Merrill
2011-10-04 20:28                                                     ` Dodji Seketeli
2011-09-20  0:08                   ` Dodji Seketeli
2011-07-16 14:38     ` [PATCH 6/7] Kill pedantic warnings on system headers macros Dodji Seketeli
2011-09-12 22:09       ` Jason Merrill
2011-09-16 11:25         ` Dodji Seketeli
2011-09-17 22:34           ` Jason Merrill
2011-09-18 18:59             ` Dodji Seketeli
2011-07-16 14:39     ` [PATCH 5/7] Add line map statistics to -fmem-report output Dodji Seketeli
2011-09-12 22:07       ` Jason Merrill
2011-09-16  8:29         ` Dodji Seketeli
2011-09-17 22:05           ` Jason Merrill
2011-09-20 12:10             ` Dodji Seketeli
2011-09-20 14:08               ` Jason Merrill
2011-07-16 14:39     ` [PATCH 4/7] Support -fdebug-cpp option Dodji Seketeli
2011-08-21 11:02       ` Alexandre Oliva
2011-08-21 11:40         ` Jakub Jelinek
2011-08-22 14:45           ` Tom Tromey
2011-08-22 15:22             ` Jakub Jelinek
2011-08-23 19:52         ` Dodji Seketeli
2011-08-24 15:11           ` Tom Tromey
2011-09-12 22:07       ` Jason Merrill
2011-09-16  8:23         ` Dodji Seketeli
2011-09-17 22:01           ` Jason Merrill
2011-07-16 15:25     ` [PATCH 2/7] Generate virtual locations for tokens Dodji Seketeli
2011-08-09 15:30       ` Dodji Seketeli
2011-09-12 21:15         ` Jason Merrill
2011-09-14 10:01           ` Dodji Seketeli
2011-09-14 22:56             ` Jason Merrill
2011-09-18 13:44               ` Dodji Seketeli
2011-09-19 22:31                 ` Jason Merrill
2011-09-21 14:55                   ` Dodji Seketeli
2011-09-22 17:10                     ` Jason Merrill
2011-09-26 14:47                       ` Dodji Seketeli
2011-09-26 20:39                         ` Jason Merrill
2011-09-28  3:23                           ` Dodji Seketeli
2011-09-28 14:49                             ` Jason Merrill
2011-09-28 21:24                               ` Dodji Seketeli
2011-09-28 21:45                                 ` Jason Merrill
2011-09-29  5:49                                   ` Dodji Seketeli
2011-07-16 15:28     ` [PATCH 1/7] Linemap infrastructure for virtual locations Dodji Seketeli
2011-07-18 22:06       ` Jason Merrill
2011-07-19 10:47         ` Dodji Seketeli
2011-07-19 17:26           ` Jason Merrill
2011-07-19 18:03             ` Dodji Seketeli
2011-07-19 23:37       ` Jason Merrill
2011-07-30  6:20       ` Jason Merrill
2011-08-01 18:54         ` Dodji Seketeli
2011-08-01  4:42       ` Jason Merrill
2011-08-02  4:48       ` Jason Merrill
2011-08-04 15:28         ` Dodji Seketeli
2011-08-04 21:30           ` Jason Merrill
2011-08-05 17:12             ` Dodji Seketeli
2011-08-05 17:31               ` Jason Merrill
2011-08-09 14:56                 ` Dodji Seketeli
2011-08-19  8:46                   ` Jason Merrill
2011-08-19 14:43                     ` Tom Tromey
2011-09-01 10:37                     ` Dodji Seketeli
2011-09-07 19:26                       ` Jason Merrill
2011-09-08 12:41                         ` Dodji Seketeli
2011-09-09  7:45                           ` Jason Merrill
2011-09-09  8:57                           ` Jason Merrill
2011-07-16 15:34     ` [PATCH 7/7] Reduce memory waste due to non-power-of-2 allocs Dodji Seketeli
2011-09-12 22:25       ` Jason Merrill
2011-09-17 13:41         ` Dodji Seketeli
2011-09-17 22:22           ` Jason Merrill
2011-09-18 22:30             ` Dodji Seketeli
2011-09-19  6:51           ` Laurynas Biveinis
2011-07-16 16:47   ` [PATCH 0/7] Tracking locations of tokens resulting from macro expansion Tobias Burnus
2011-07-16 17:57     ` Dodji Seketeli
2011-10-17  9:58 [PATCH 0/6] " Dodji Seketeli
2011-10-17 10:22 ` [PATCH 5/6] Add line map statistics to -fmem-report output 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).