aboutsummaryrefslogtreecommitdiff
path: root/gcc/doc
diff options
context:
space:
mode:
authorDavid Malcolm <dmalcolm@redhat.com>2021-01-14 16:25:02 -0500
committerDavid Malcolm <dmalcolm@redhat.com>2021-01-14 16:28:38 -0500
commitf10960558540636800cf5d3d6355969621fbc17e (patch)
treec61fc5a250896931c162ac0466c461e8eac8084c /gcc/doc
parent59832db9a711591bcee0746ae33ccf5eb4978121 (diff)
downloadgcc-f10960558540636800cf5d3d6355969621fbc17e.zip
gcc-f10960558540636800cf5d3d6355969621fbc17e.tar.gz
gcc-f10960558540636800cf5d3d6355969621fbc17e.tar.bz2
Add GCC_EXTRA_DIAGNOSTIC_OUTPUT environment variable for fix-it hints
GCC has had the ability to emit fix-it hints in machine-readable form since GCC 7 via -fdiagnostics-parseable-fixits and -fdiagnostics-generate-patch. The former emits additional specially-formatted lines to stderr; the option and its format were directly taken from a pre-existing option in clang. Ideally this could be used by IDEs so that the user can select specific fix-it hints and have the IDE apply them to the user's source code (perhaps turning them into clickable elements, perhaps with an "Apply All" option, etc). Eclipse CDT has supported this option in this way for a few years: https://bugs.eclipse.org/bugs/show_bug.cgi?id=497670 As a user of Emacs I would like Emacs to support such a feature. https://debbugs.gnu.org/cgi/bugreport.cgi?bug=25987 tracks supporting GCC fix-it output in Emacs. The discussion there identifies two issues with the existing option: (a) columns in the output are specified as byte-offsets within the line (for exact compatibility with the option in clang), whereas emacs would prefer to consume them as what GCC 11 calls "display columns". https://gcc.gnu.org/onlinedocs/gcc/Diagnostic-Message-Formatting-Options.html#index-fdiagnostics-column-unit (b) injecting a command-line option into the build is a fiddly manual step, varying between build systems. It's far easier for the user if Emacs simply sets an environment variable when compiling, GCC uses this to enable the option if it recognizes the value, and the emacs compilation buffer decodes the additional lines of output and adds appropriate widgets. In some ways it is a workaround for not having a language server. Doing it this way means that for the various combinations of older and newer GCC and older and newer Emacs that a sufficiently modern combination of both can automatically support the rich fix-it UI, whereas other combinations will either not provide the envvar, or silently ignore it, gracefully doing nothing extra. Hence this patch adds a new GCC_EXTRA_DIAGNOSTIC_OUTPUT environment variable to GCC which enables output of machine-parseable fix-it hints. GCC_EXTRA_DIAGNOSTIC_OUTPUT=fixits-v1 is equivalent to the existing -fdiagnostics-parseable-fixits option. GCC_EXTRA_DIAGNOSTIC_OUTPUT=fixits-v2 is the same, but changes the column output mode to "display columns" rather than bytes, as required by Emacs. The discussion in that Emacs bug has some concerns about the encoding of these lines, and, indeed, the encoding of GCC's stderr in general: currently we emit a mixture of bytes and UTF-8; I believe we emit filenames as bytes, diagnostic messages as UTF-8, and quote source code in the original encoding (PR other/93067 covers converting it to UTF-8 on output). This patch prints octal-escaped bytes for bytes within filenames and replacement text that aren't printable (as per -fdiagnostics-parseable-fixits). gcc/ChangeLog: * diagnostic.c (diagnostic_initialize): Eliminate parseable_fixits_p in favor of initializing extra_output_kind from GCC_EXTRA_DIAGNOSTIC_OUTPUT. (convert_column_unit): New function, split out from... (diagnostic_converted_column): ...this. (print_parseable_fixits): Add "column_unit" and "tabstop" params. Use them to call convert_column_unit on the column values. (diagnostic_report_diagnostic): Eliminate conditional on parseable_fixits_p in favor of a switch statement on extra_output_kind, passing the appropriate values to the new params of print_parseable_fixits. (selftest::test_print_parseable_fixits_none): Update for new params of print_parseable_fixits. (selftest::test_print_parseable_fixits_insert): Likewise. (selftest::test_print_parseable_fixits_remove): Likewise. (selftest::test_print_parseable_fixits_replace): Likewise. (selftest::test_print_parseable_fixits_bytes_vs_display_columns): New. (selftest::diagnostic_c_tests): Call it. * diagnostic.h (enum diagnostics_extra_output_kind): New. (diagnostic_context::parseable_fixits_p): Delete field in favor of... (diagnostic_context::extra_output_kind): ...this new field. * doc/invoke.texi (Environment Variables): Add GCC_EXTRA_DIAGNOSTIC_OUTPUT. * opts.c (common_handle_option): Update handling of OPT_fdiagnostics_parseable_fixits for change to diagnostic_context fields. gcc/testsuite/ChangeLog: * gcc.dg/plugin/diagnostic-test-show-locus-GCC_EXTRA_DIAGNOSTIC_OUTPUT-fixits-v1.c: New file. * gcc.dg/plugin/diagnostic-test-show-locus-GCC_EXTRA_DIAGNOSTIC_OUTPUT-fixits-v2.c: New file. * gcc.dg/plugin/plugin.exp (plugin_test_list): Add them.
Diffstat (limited to 'gcc/doc')
-rw-r--r--gcc/doc/invoke.texi19
1 files changed, 19 insertions, 0 deletions
diff --git a/gcc/doc/invoke.texi b/gcc/doc/invoke.texi
index 298f1f8..8f10091 100644
--- a/gcc/doc/invoke.texi
+++ b/gcc/doc/invoke.texi
@@ -32711,6 +32711,25 @@ Recognize EUCJP characters.
If @env{LANG} is not defined, or if it has some other value, then the
compiler uses @code{mblen} and @code{mbtowc} as defined by the default locale to
recognize and translate multibyte characters.
+
+@item GCC_EXTRA_DIAGNOSTIC_OUTPUT
+If @env{GCC_EXTRA_DIAGNOSTIC_OUTPUT} is set to one of the following values,
+then additional text will be emitted to stderr when fix-it hints are
+emitted. @option{-fdiagnostics-parseable-fixits} and
+@option{-fno-diagnostics-parseable-fixits} take precedence over this
+environment variable.
+
+@table @samp
+@item fixits-v1
+Emit parseable fix-it hints, equivalent to
+@option{-fdiagnostics-parseable-fixits}. In particular, columns are
+expressed as a count of bytes, starting at byte 1 for the initial column.
+
+@item fixits-v2
+As @code{fixits-v1}, but columns are expressed as display columns,
+as per @option{-fdiagnostics-column-unit=display}.
+@end table
+
@end table
@noindent