diff mbox

[4/7] Support -fdebug-cpp option

Message ID 1902ac92ebf0b213bfafdeb31edaac8812b5399b.1310824121.git.dodji@redhat.com
State New
Headers show

Commit Message

Dodji Seketeli July 16, 2011, 2:37 p.m. UTC
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(-)

Comments

Alexandre Oliva Aug. 21, 2011, 7:51 a.m. UTC | #1
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 Aug. 21, 2011, 9:48 a.m. UTC | #2
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
Tom Tromey Aug. 22, 2011, 2:16 p.m. UTC | #3
>>>>> "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 Aug. 22, 2011, 2:33 p.m. UTC | #4
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
Dodji Seketeli Aug. 23, 2011, 6:58 p.m. UTC | #5
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.  :-)
Tom Tromey Aug. 24, 2011, 2:06 p.m. UTC | #6
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
Jason Merrill Sept. 12, 2011, 9:14 p.m. UTC | #7
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
diff mbox

Patch

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