Commit 717ebe91 by David Malcolm Committed by David Malcolm

Add -fdiagnostics-generate-patch

gcc/ChangeLog:
	* common.opt (fdiagnostics-generate-patch): New option.
	* diagnostic.c: Include "edit-context.h".
	(diagnostic_initialize): Initialize context->edit_context_ptr.
	(diagnostic_finish): Delete context->edit_context_ptr.
	(diagnostic_report_diagnostic): Add fix-it hints from the
	diagnostic to context->edit_context_ptr, if any.
	* diagnostic.h (class edit_context): Add forward decl.
	(struct diagnostic_context): Add field "edit_context_ptr".
	* doc/invoke.texi (Diagnostic Message Formatting Options): Add
	-fdiagnostics-generate-patch.
	(-fdiagnostics-generate-patch): New item.
	* toplev.c: Include "edit-context.h".
	(process_options): Set global_dc->edit_context_ptr to a new
	edit_context if the options need one.
	(toplev::main): Handle -fdiagnostics-generate-patch by using
	global_dc->edit_context_ptr.

gcc/testsuite/ChangeLog:
	* gcc.dg/plugin/diagnostic-test-show-locus-generate-patch.c: New
	test case.
	* gcc.dg/plugin/plugin.exp (plugin_test_list): Add
	diagnostic-test-show-locus-generate-patch.c to the sources
	for diagnostic_plugin_test_show_locus.c.

From-SVN: r239965
parent 9dc5773f
2016-09-02 David Malcolm <dmalcolm@redhat.com>
* common.opt (fdiagnostics-generate-patch): New option.
* diagnostic.c: Include "edit-context.h".
(diagnostic_initialize): Initialize context->edit_context_ptr.
(diagnostic_finish): Delete context->edit_context_ptr.
(diagnostic_report_diagnostic): Add fix-it hints from the
diagnostic to context->edit_context_ptr, if any.
* diagnostic.h (class edit_context): Add forward decl.
(struct diagnostic_context): Add field "edit_context_ptr".
* doc/invoke.texi (Diagnostic Message Formatting Options): Add
-fdiagnostics-generate-patch.
(-fdiagnostics-generate-patch): New item.
* toplev.c: Include "edit-context.h".
(process_options): Set global_dc->edit_context_ptr to a new
edit_context if the options need one.
(toplev::main): Handle -fdiagnostics-generate-patch by using
global_dc->edit_context_ptr.
2016-09-02 Jakub Jelinek <jakub@redhat.com> 2016-09-02 Jakub Jelinek <jakub@redhat.com>
PR c/65467 PR c/65467
......
...@@ -1196,6 +1196,10 @@ fdiagnostics-parseable-fixits ...@@ -1196,6 +1196,10 @@ fdiagnostics-parseable-fixits
Common Var(flag_diagnostics_parseable_fixits) Common Var(flag_diagnostics_parseable_fixits)
Print fixit hints in machine-readable form. Print fixit hints in machine-readable form.
fdiagnostics-generate-patch
Common Var(flag_diagnostics_generate_patch)
Print fix-it hints to stderr in unified diff format.
fdiagnostics-show-option fdiagnostics-show-option
Common Var(flag_diagnostics_show_option) Init(1) Common Var(flag_diagnostics_show_option) Init(1)
Amend appropriate diagnostic messages with the command line option that controls them. Amend appropriate diagnostic messages with the command line option that controls them.
......
...@@ -31,6 +31,7 @@ along with GCC; see the file COPYING3. If not see ...@@ -31,6 +31,7 @@ along with GCC; see the file COPYING3. If not see
#include "backtrace.h" #include "backtrace.h"
#include "diagnostic.h" #include "diagnostic.h"
#include "diagnostic-color.h" #include "diagnostic-color.h"
#include "edit-context.h"
#include "selftest.h" #include "selftest.h"
#ifdef HAVE_TERMIOS_H #ifdef HAVE_TERMIOS_H
...@@ -174,6 +175,7 @@ diagnostic_initialize (diagnostic_context *context, int n_opts) ...@@ -174,6 +175,7 @@ diagnostic_initialize (diagnostic_context *context, int n_opts)
context->colorize_source_p = false; context->colorize_source_p = false;
context->show_ruler_p = false; context->show_ruler_p = false;
context->parseable_fixits_p = false; context->parseable_fixits_p = false;
context->edit_context_ptr = NULL;
} }
/* Maybe initialize the color support. We require clients to do this /* Maybe initialize the color support. We require clients to do this
...@@ -235,6 +237,12 @@ diagnostic_finish (diagnostic_context *context) ...@@ -235,6 +237,12 @@ diagnostic_finish (diagnostic_context *context)
context->printer->~pretty_printer (); context->printer->~pretty_printer ();
XDELETE (context->printer); XDELETE (context->printer);
context->printer = NULL; context->printer = NULL;
if (context->edit_context_ptr)
{
delete context->edit_context_ptr;
context->edit_context_ptr = NULL;
}
} }
/* Initialize DIAGNOSTIC, where the message MSG has already been /* Initialize DIAGNOSTIC, where the message MSG has already been
...@@ -943,6 +951,9 @@ diagnostic_report_diagnostic (diagnostic_context *context, ...@@ -943,6 +951,9 @@ diagnostic_report_diagnostic (diagnostic_context *context,
diagnostic->message.format_spec = saved_format_spec; diagnostic->message.format_spec = saved_format_spec;
diagnostic->x_data = NULL; diagnostic->x_data = NULL;
if (context->edit_context_ptr)
context->edit_context_ptr->add_fixits (diagnostic->richloc);
context->lock--; context->lock--;
return true; return true;
......
...@@ -62,6 +62,8 @@ typedef void (*diagnostic_start_span_fn) (diagnostic_context *, ...@@ -62,6 +62,8 @@ typedef void (*diagnostic_start_span_fn) (diagnostic_context *,
typedef diagnostic_starter_fn diagnostic_finalizer_fn; typedef diagnostic_starter_fn diagnostic_finalizer_fn;
class edit_context;
/* This data structure bundles altogether any information relevant to /* This data structure bundles altogether any information relevant to
the context of a diagnostic message. */ the context of a diagnostic message. */
struct diagnostic_context struct diagnostic_context
...@@ -209,6 +211,10 @@ struct diagnostic_context ...@@ -209,6 +211,10 @@ struct diagnostic_context
/* If true, print fixits in machine-parseable form after the /* If true, print fixits in machine-parseable form after the
rest of the diagnostic. */ rest of the diagnostic. */
bool parseable_fixits_p; bool parseable_fixits_p;
/* If non-NULL, an edit_context to which fix-it hints should be
applied, for generating patches. */
edit_context *edit_context_ptr;
}; };
static inline void static inline void
......
...@@ -248,7 +248,7 @@ Objective-C and Objective-C++ Dialects}. ...@@ -248,7 +248,7 @@ Objective-C and Objective-C++ Dialects}.
-fdiagnostics-show-location=@r{[}once@r{|}every-line@r{]} @gol -fdiagnostics-show-location=@r{[}once@r{|}every-line@r{]} @gol
-fdiagnostics-color=@r{[}auto@r{|}never@r{|}always@r{]} @gol -fdiagnostics-color=@r{[}auto@r{|}never@r{|}always@r{]} @gol
-fno-diagnostics-show-option -fno-diagnostics-show-caret @gol -fno-diagnostics-show-option -fno-diagnostics-show-caret @gol
-fdiagnostics-parseable-fixits} -fdiagnostics-parseable-fixits -fdiagnostics-generate-patch}
@item Warning Options @item Warning Options
@xref{Warning Options,,Options to Request or Suppress Warnings}. @xref{Warning Options,,Options to Request or Suppress Warnings}.
...@@ -3460,6 +3460,27 @@ An empty replacement string indicates that the given range is to be removed. ...@@ -3460,6 +3460,27 @@ An empty replacement string indicates that the given range is to be removed.
An empty range (e.g. ``45:3-45:3'') indicates that the string is to An empty range (e.g. ``45:3-45:3'') indicates that the string is to
be inserted at the given position. be inserted at the given position.
@item -fdiagnostics-generate-patch
@opindex fdiagnostics-generate-patch
Print fix-it hints to stderr in unified diff format, after any diagnostics
are printed. For example:
@smallexample
--- test.c
+++ test.c
@@ -42,5 +42,5 @@
void show_cb(GtkDialog *dlg)
@{
- gtk_widget_showall(dlg);
+ gtk_widget_show_all(dlg);
@}
@end smallexample
The diff may or may not be colorized, following the same rules
as for diagnostics (see @option{-fdiagnostics-color}).
@end table @end table
@node Warning Options @node Warning Options
......
2016-09-02 David Malcolm <dmalcolm@redhat.com>
* gcc.dg/plugin/diagnostic-test-show-locus-generate-patch.c: New
test case.
* gcc.dg/plugin/plugin.exp (plugin_test_list): Add
diagnostic-test-show-locus-generate-patch.c to the sources
for diagnostic_plugin_test_show_locus.c.
2016-09-02 Jakub Jelinek <jakub@redhat.com> 2016-09-02 Jakub Jelinek <jakub@redhat.com>
PR c/65467 PR c/65467
......
/* { dg-do compile } */
/* { dg-options "-O -fdiagnostics-generate-patch" } */
/* This is a collection of unittests for diagnostic_show_locus;
see the overview in diagnostic_plugin_test_show_locus.c.
In particular, note the discussion of why we need a very long line here:
01234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789
and that we can't use macros in this file. */
/* Unit test for rendering of insertion fixit hints
(example taken from PR 62316). */
void test_fixit_insert (void)
{
#if 0
int a[2][2] = { 0, 1 , 2, 3 }; /* { dg-warning "insertion hints" } */
#endif
}
/* Unit test for rendering of "remove" fixit hints. */
void test_fixit_remove (void)
{
#if 0
int a;; /* { dg-warning "example of a removal hint" } */
#endif
}
/* Unit test for rendering of "replace" fixit hints. */
void test_fixit_replace (void)
{
#if 0
gtk_widget_showall (dlg); /* { dg-warning "example of a replacement hint" } */
#endif
}
/* Verify the output from -fdiagnostics-generate-patch.
We expect a header, containing the filename. This is the absolute path,
so we can only capture it via regexps. */
/* { dg-regexp "\\-\\-\\- .*" } */
/* { dg-regexp "\\+\\+\\+ .*" } */
/* Next, we expect the diff itself. */
/* { dg-begin-multiline-output "" }
@@ -14,7 +14,7 @@
void test_fixit_insert (void)
{
#if 0
- int a[2][2] = { 0, 1 , 2, 3 };
+ int a[2][2] = { {0, 1} , 2, 3 };
#endif
}
@@ -23,7 +23,7 @@
void test_fixit_remove (void)
{
#if 0
- int a;;
+ int a;
#endif
}
@@ -32,7 +32,7 @@
void test_fixit_replace (void)
{
#if 0
- gtk_widget_showall (dlg);
+ gtk_widget_show_all (dlg);
#endif
}
{ dg-end-multiline-output "" } */
...@@ -65,7 +65,8 @@ set plugin_test_list [list \ ...@@ -65,7 +65,8 @@ set plugin_test_list [list \
{ diagnostic_plugin_test_show_locus.c \ { diagnostic_plugin_test_show_locus.c \
diagnostic-test-show-locus-bw.c \ diagnostic-test-show-locus-bw.c \
diagnostic-test-show-locus-color.c \ diagnostic-test-show-locus-color.c \
diagnostic-test-show-locus-parseable-fixits.c } \ diagnostic-test-show-locus-parseable-fixits.c \
diagnostic-test-show-locus-generate-patch.c } \
{ diagnostic_plugin_test_tree_expression_range.c \ { diagnostic_plugin_test_tree_expression_range.c \
diagnostic-test-expressions-1.c } \ diagnostic-test-expressions-1.c } \
{ diagnostic_plugin_show_trees.c \ { diagnostic_plugin_show_trees.c \
......
...@@ -76,6 +76,7 @@ along with GCC; see the file COPYING3. If not see ...@@ -76,6 +76,7 @@ along with GCC; see the file COPYING3. If not see
#include "tree-chkp.h" #include "tree-chkp.h"
#include "omp-low.h" #include "omp-low.h"
#include "hsa.h" #include "hsa.h"
#include "edit-context.h"
#if defined(DBX_DEBUGGING_INFO) || defined(XCOFF_DEBUGGING_INFO) #if defined(DBX_DEBUGGING_INFO) || defined(XCOFF_DEBUGGING_INFO)
#include "dbxout.h" #include "dbxout.h"
...@@ -1221,6 +1222,9 @@ process_options (void) ...@@ -1221,6 +1222,9 @@ process_options (void)
/* Some machines may reject certain combinations of options. */ /* Some machines may reject certain combinations of options. */
targetm.target_option.override (); targetm.target_option.override ();
if (flag_diagnostics_generate_patch)
global_dc->edit_context_ptr = new edit_context ();
/* Avoid any informative notes in the second run of -fcompare-debug. */ /* Avoid any informative notes in the second run of -fcompare-debug. */
if (flag_compare_debug) if (flag_compare_debug)
diagnostic_inhibit_notes (global_dc); diagnostic_inhibit_notes (global_dc);
...@@ -2147,6 +2151,16 @@ toplev::main (int argc, char **argv) ...@@ -2147,6 +2151,16 @@ toplev::main (int argc, char **argv)
emit some diagnostics here. */ emit some diagnostics here. */
invoke_plugin_callbacks (PLUGIN_FINISH, NULL); invoke_plugin_callbacks (PLUGIN_FINISH, NULL);
if (flag_diagnostics_generate_patch)
{
gcc_assert (global_dc->edit_context_ptr);
pretty_printer (pp);
pp_show_color (&pp) = pp_show_color (global_dc->printer);
global_dc->edit_context_ptr->print_diff (&pp, true);
pp_flush (&pp);
}
diagnostic_finish (global_dc); diagnostic_finish (global_dc);
finalize_plugins (); finalize_plugins ();
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment