OBJS-libcommon = diagnostic-spec.o diagnostic.o diagnostic-color.o \
diagnostic-format-json.o \
diagnostic-format-sarif.o \
+ diagnostic-format-text.o \
diagnostic-global-context.o \
diagnostic-macro-unwinding.o \
diagnostic-path.o \
#include "selftest-diagnostic.h"
#include "diagnostic-metadata.h"
#include "diagnostic-path.h"
+#include "diagnostic-format.h"
#include "json.h"
#include "selftest.h"
#include "logical-location.h"
#include "diagnostic.h"
#include "diagnostic-metadata.h"
#include "diagnostic-path.h"
+#include "diagnostic-format.h"
#include "json.h"
#include "cpplib.h"
#include "logical-location.h"
--- /dev/null
+/* Classic text-based output of diagnostics.
+ Copyright (C) 1999-2024 Free Software Foundation, Inc.
+
+This file is part of GCC.
+
+GCC is free software; you can redistribute it and/or modify it under
+the terms of the GNU General Public License as published by the Free
+Software Foundation; either version 3, or (at your option) any later
+version.
+
+GCC is distributed in the hope that it will be useful, but WITHOUT ANY
+WARRANTY; without even the implied warranty of MERCHANTABILITY or
+FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
+for more details.
+
+You should have received a copy of the GNU General Public License
+along with GCC; see the file COPYING3. If not see
+<http://www.gnu.org/licenses/>. */
+
+
+#include "config.h"
+#define INCLUDE_VECTOR
+#include "system.h"
+#include "coretypes.h"
+#include "version.h"
+#include "intl.h"
+#include "diagnostic.h"
+#include "diagnostic-color.h"
+#include "diagnostic-url.h"
+#include "diagnostic-metadata.h"
+#include "diagnostic-path.h"
+#include "diagnostic-client-data-hooks.h"
+#include "diagnostic-diagram.h"
+#include "diagnostic-format-text.h"
+#include "text-art/theme.h"
+
+/* class diagnostic_text_output_format : public diagnostic_output_format. */
+
+diagnostic_text_output_format::~diagnostic_text_output_format ()
+{
+ /* Some of the errors may actually have been warnings. */
+ if (m_context.diagnostic_count (DK_WERROR))
+ {
+ /* -Werror was given. */
+ if (m_context.warning_as_error_requested_p ())
+ pp_verbatim (m_context.printer,
+ _("%s: all warnings being treated as errors"),
+ progname);
+ /* At least one -Werror= was given. */
+ else
+ pp_verbatim (m_context.printer,
+ _("%s: some warnings being treated as errors"),
+ progname);
+ pp_newline_and_flush (m_context.printer);
+ }
+}
+
+/* Implementation of diagnostic_output_format::on_report_diagnostic vfunc
+ for GCC's standard textual output. */
+
+void
+diagnostic_text_output_format::
+on_report_diagnostic (const diagnostic_info &diagnostic,
+ diagnostic_t orig_diag_kind)
+{
+ (*diagnostic_starter (&m_context)) (&m_context, &diagnostic);
+
+ pp_output_formatted_text (m_context.printer, m_context.get_urlifier ());
+
+ if (m_context.m_show_cwe)
+ print_any_cwe (diagnostic);
+
+ if (m_context.m_show_rules)
+ print_any_rules (diagnostic);
+
+ if (m_context.m_show_option_requested)
+ print_option_information (diagnostic, orig_diag_kind);
+
+ (*diagnostic_finalizer (&m_context)) (&m_context, &diagnostic,
+ orig_diag_kind);
+}
+
+void
+diagnostic_text_output_format::on_diagram (const diagnostic_diagram &diagram)
+{
+ char *saved_prefix = pp_take_prefix (m_context.printer);
+ pp_set_prefix (m_context.printer, NULL);
+ /* Use a newline before and after and a two-space indent
+ to make the diagram stand out a little from the wall of text. */
+ pp_newline (m_context.printer);
+ diagram.get_canvas ().print_to_pp (m_context.printer, " ");
+ pp_newline (m_context.printer);
+ pp_set_prefix (m_context.printer, saved_prefix);
+ pp_flush (m_context.printer);
+}
+
+/* If DIAGNOSTIC has a CWE identifier, print it.
+
+ For example, if the diagnostic metadata associates it with CWE-119,
+ " [CWE-119]" will be printed, suitably colorized, and with a URL of a
+ description of the security issue. */
+
+void
+diagnostic_text_output_format::print_any_cwe (const diagnostic_info &diagnostic)
+{
+ if (diagnostic.metadata == NULL)
+ return;
+
+ int cwe = diagnostic.metadata->get_cwe ();
+ if (cwe)
+ {
+ pretty_printer * const pp = m_context.printer;
+ char *saved_prefix = pp_take_prefix (pp);
+ pp_string (pp, " [");
+ const char *kind_color = diagnostic_get_color_for_kind (diagnostic.kind);
+ pp_string (pp, colorize_start (pp_show_color (pp), kind_color));
+ if (pp->supports_urls_p ())
+ {
+ char *cwe_url = get_cwe_url (cwe);
+ pp_begin_url (pp, cwe_url);
+ free (cwe_url);
+ }
+ pp_printf (pp, "CWE-%i", cwe);
+ pp_set_prefix (pp, saved_prefix);
+ if (pp->supports_urls_p ())
+ pp_end_url (pp);
+ pp_string (pp, colorize_stop (pp_show_color (pp)));
+ pp_character (pp, ']');
+ }
+}
+
+/* If DIAGNOSTIC has any rules associated with it, print them.
+
+ For example, if the diagnostic metadata associates it with a rule
+ named "STR34-C", then " [STR34-C]" will be printed, suitably colorized,
+ with any URL provided by the rule. */
+
+void
+diagnostic_text_output_format::
+print_any_rules (const diagnostic_info &diagnostic)
+{
+ if (diagnostic.metadata == NULL)
+ return;
+
+ for (unsigned idx = 0; idx < diagnostic.metadata->get_num_rules (); idx++)
+ {
+ const diagnostic_metadata::rule &rule
+ = diagnostic.metadata->get_rule (idx);
+ if (char *desc = rule.make_description ())
+ {
+ pretty_printer * const pp = m_context.printer;
+ char *saved_prefix = pp_take_prefix (pp);
+ pp_string (pp, " [");
+ const char *kind_color
+ = diagnostic_get_color_for_kind (diagnostic.kind);
+ pp_string (pp, colorize_start (pp_show_color (pp), kind_color));
+ char *url = NULL;
+ if (pp->supports_urls_p ())
+ {
+ url = rule.make_url ();
+ if (url)
+ pp_begin_url (pp, url);
+ }
+ pp_string (pp, desc);
+ pp_set_prefix (pp, saved_prefix);
+ if (pp->supports_urls_p ())
+ if (url)
+ pp_end_url (pp);
+ free (url);
+ pp_string (pp, colorize_stop (pp_show_color (pp)));
+ pp_character (pp, ']');
+ free (desc);
+ }
+ }
+}
+
+/* Print any metadata about the option used to control DIAGNOSTIC to CONTEXT's
+ printer, e.g. " [-Werror=uninitialized]".
+ Subroutine of diagnostic_context::report_diagnostic. */
+
+void
+diagnostic_text_output_format::
+print_option_information (const diagnostic_info &diagnostic,
+ diagnostic_t orig_diag_kind)
+{
+ if (char *option_text
+ = m_context.make_option_name (diagnostic.option_index,
+ orig_diag_kind, diagnostic.kind))
+ {
+ char *option_url = nullptr;
+ pretty_printer * const pp = m_context.printer;
+ if (pp->supports_urls_p ())
+ option_url = m_context.make_option_url (diagnostic.option_index);
+ pp_string (pp, " [");
+ const char *kind_color = diagnostic_get_color_for_kind (diagnostic.kind);
+ pp_string (pp, colorize_start (pp_show_color (pp), kind_color));
+ if (option_url)
+ pp_begin_url (pp, option_url);
+ pp_string (pp, option_text);
+ if (option_url)
+ {
+ pp_end_url (pp);
+ free (option_url);
+ }
+ pp_string (pp, colorize_stop (pp_show_color (pp)));
+ pp_character (pp, ']');
+ free (option_text);
+ }
+}
--- /dev/null
+/* Classic text-based output of diagnostics.
+ Copyright (C) 2023-2024 Free Software Foundation, Inc.
+ Contributed by David Malcolm <dmalcolm@redhat.com>.
+
+This file is part of GCC.
+
+GCC is free software; you can redistribute it and/or modify it under
+the terms of the GNU General Public License as published by the Free
+Software Foundation; either version 3, or (at your option) any later
+version.
+
+GCC is distributed in the hope that it will be useful, but WITHOUT ANY
+WARRANTY; without even the implied warranty of MERCHANTABILITY or
+FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
+for more details.
+
+You should have received a copy of the GNU General Public License
+along with GCC; see the file COPYING3. If not see
+<http://www.gnu.org/licenses/>. */
+
+#ifndef GCC_DIAGNOSTIC_FORMAT_TEXT_H
+#define GCC_DIAGNOSTIC_FORMAT_TEXT_H
+
+#include "diagnostic-format.h"
+
+/* Subclass of diagnostic_output_format for classic text-based output
+ to stderr.
+
+ Uses diagnostic_context.m_text_callbacks to provide client-specific
+ textual output (e.g. include paths, macro expansions, etc). */
+
+class diagnostic_text_output_format : public diagnostic_output_format
+{
+public:
+ diagnostic_text_output_format (diagnostic_context &context)
+ : diagnostic_output_format (context)
+ {}
+ ~diagnostic_text_output_format ();
+ void on_begin_group () override {}
+ void on_end_group () override {}
+ void on_report_diagnostic (const diagnostic_info &,
+ diagnostic_t orig_diag_kind) override;
+ void on_diagram (const diagnostic_diagram &diagram) override;
+ bool machine_readable_stderr_p () const final override
+ {
+ return false;
+ }
+
+private:
+ void print_any_cwe (const diagnostic_info &diagnostic);
+ void print_any_rules (const diagnostic_info &diagnostic);
+ void print_option_information (const diagnostic_info &diagnostic,
+ diagnostic_t orig_diag_kind);
+};
+
+#endif /* ! GCC_DIAGNOSTIC_FORMAT_TEXT_H */
--- /dev/null
+/* Declarations for managing different output formats for diagnostics.
+ Copyright (C) 2023-2024 Free Software Foundation, Inc.
+ Contributed by David Malcolm <dmalcolm@redhat.com>.
+
+This file is part of GCC.
+
+GCC is free software; you can redistribute it and/or modify it under
+the terms of the GNU General Public License as published by the Free
+Software Foundation; either version 3, or (at your option) any later
+version.
+
+GCC is distributed in the hope that it will be useful, but WITHOUT ANY
+WARRANTY; without even the implied warranty of MERCHANTABILITY or
+FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
+for more details.
+
+You should have received a copy of the GNU General Public License
+along with GCC; see the file COPYING3. If not see
+<http://www.gnu.org/licenses/>. */
+
+#ifndef GCC_DIAGNOSTIC_FORMAT_H
+#define GCC_DIAGNOSTIC_FORMAT_H
+
+/* Abstract base class for a particular output format for diagnostics;
+ each value of -fdiagnostics-output-format= will have its own
+ implementation. */
+
+class diagnostic_output_format
+{
+public:
+ virtual ~diagnostic_output_format () {}
+
+ virtual void on_begin_group () = 0;
+ virtual void on_end_group () = 0;
+
+ /* Vfunc with responsibility for phase 3 of formatting the message
+ and "printing" the result. */
+ virtual void on_report_diagnostic (const diagnostic_info &,
+ diagnostic_t orig_diag_kind) = 0;
+
+ virtual void on_diagram (const diagnostic_diagram &diagram) = 0;
+ virtual bool machine_readable_stderr_p () const = 0;
+
+protected:
+ diagnostic_output_format (diagnostic_context &context)
+ : m_context (context)
+ {}
+
+ diagnostic_context &m_context;
+};
+
+extern void
+diagnostic_output_format_init (diagnostic_context &,
+ const char *main_input_filename_,
+ const char *base_file_name,
+ enum diagnostics_output_format,
+ bool json_formatting);
+extern void
+diagnostic_output_format_init_json_stderr (diagnostic_context &context,
+ bool formatted);
+extern void
+diagnostic_output_format_init_json_file (diagnostic_context &context,
+ bool formatted,
+ const char *base_file_name);
+extern void
+diagnostic_output_format_init_sarif_stderr (diagnostic_context &context,
+ const line_maps *line_maps,
+ const char *main_input_filename_,
+ bool formatted);
+extern void
+diagnostic_output_format_init_sarif_file (diagnostic_context &context,
+ const line_maps *line_maps,
+ const char *main_input_filename_,
+ bool formatted,
+ const char *base_file_name);
+extern void
+diagnostic_output_format_init_sarif_stream (diagnostic_context &context,
+ const line_maps *line_maps,
+ const char *main_input_filename_,
+ bool formatted,
+ FILE *stream);
+
+#endif /* ! GCC_DIAGNOSTIC_FORMAT_H */
#include "coretypes.h"
#include "intl.h"
#include "diagnostic.h"
+#include "diagnostic-format.h"
/* A diagnostic_context surrogate for stderr. */
static diagnostic_context global_diagnostic_context;
#include "diagnostic-path.h"
#include "diagnostic-client-data-hooks.h"
#include "diagnostic-diagram.h"
+#include "diagnostic-format.h"
+#include "diagnostic-format-text.h"
#include "edit-context.h"
#include "selftest.h"
#include "selftest-diagnostic.h"
}
}
-/* class diagnostic_text_output_format : public diagnostic_output_format. */
-
-diagnostic_text_output_format::~diagnostic_text_output_format ()
-{
- /* Some of the errors may actually have been warnings. */
- if (m_context.diagnostic_count (DK_WERROR))
- {
- /* -Werror was given. */
- if (m_context.warning_as_error_requested_p ())
- pp_verbatim (m_context.printer,
- _("%s: all warnings being treated as errors"),
- progname);
- /* At least one -Werror= was given. */
- else
- pp_verbatim (m_context.printer,
- _("%s: some warnings being treated as errors"),
- progname);
- pp_newline_and_flush (m_context.printer);
- }
-}
-
-/* Implementation of diagnostic_output_format::on_report_diagnostic vfunc
- for GCC's standard textual output. */
-
-void
-diagnostic_text_output_format::
-on_report_diagnostic (const diagnostic_info &diagnostic,
- diagnostic_t orig_diag_kind)
-{
- (*diagnostic_starter (&m_context)) (&m_context, &diagnostic);
-
- pp_output_formatted_text (m_context.printer, m_context.get_urlifier ());
-
- if (m_context.m_show_cwe)
- print_any_cwe (diagnostic);
-
- if (m_context.m_show_rules)
- print_any_rules (diagnostic);
-
- if (m_context.m_show_option_requested)
- print_option_information (diagnostic, orig_diag_kind);
-
- (*diagnostic_finalizer (&m_context)) (&m_context, &diagnostic,
- orig_diag_kind);
-}
-
-void
-diagnostic_text_output_format::on_diagram (const diagnostic_diagram &diagram)
-{
- char *saved_prefix = pp_take_prefix (m_context.printer);
- pp_set_prefix (m_context.printer, NULL);
- /* Use a newline before and after and a two-space indent
- to make the diagram stand out a little from the wall of text. */
- pp_newline (m_context.printer);
- diagram.get_canvas ().print_to_pp (m_context.printer, " ");
- pp_newline (m_context.printer);
- pp_set_prefix (m_context.printer, saved_prefix);
- pp_flush (m_context.printer);
-}
-
-/* If DIAGNOSTIC has a CWE identifier, print it.
-
- For example, if the diagnostic metadata associates it with CWE-119,
- " [CWE-119]" will be printed, suitably colorized, and with a URL of a
- description of the security issue. */
-
-void
-diagnostic_text_output_format::print_any_cwe (const diagnostic_info &diagnostic)
-{
- if (diagnostic.metadata == NULL)
- return;
-
- int cwe = diagnostic.metadata->get_cwe ();
- if (cwe)
- {
- pretty_printer * const pp = m_context.printer;
- char *saved_prefix = pp_take_prefix (pp);
- pp_string (pp, " [");
- pp_string (pp, colorize_start (pp_show_color (pp),
- diagnostic_kind_color[diagnostic.kind]));
- if (pp->supports_urls_p ())
- {
- char *cwe_url = get_cwe_url (cwe);
- pp_begin_url (pp, cwe_url);
- free (cwe_url);
- }
- pp_printf (pp, "CWE-%i", cwe);
- pp_set_prefix (pp, saved_prefix);
- if (pp->supports_urls_p ())
- pp_end_url (pp);
- pp_string (pp, colorize_stop (pp_show_color (pp)));
- pp_character (pp, ']');
- }
-}
-
-/* If DIAGNOSTIC has any rules associated with it, print them.
-
- For example, if the diagnostic metadata associates it with a rule
- named "STR34-C", then " [STR34-C]" will be printed, suitably colorized,
- with any URL provided by the rule. */
-
-void
-diagnostic_text_output_format::
-print_any_rules (const diagnostic_info &diagnostic)
-{
- if (diagnostic.metadata == NULL)
- return;
-
- for (unsigned idx = 0; idx < diagnostic.metadata->get_num_rules (); idx++)
- {
- const diagnostic_metadata::rule &rule
- = diagnostic.metadata->get_rule (idx);
- if (char *desc = rule.make_description ())
- {
- pretty_printer * const pp = m_context.printer;
- char *saved_prefix = pp_take_prefix (pp);
- pp_string (pp, " [");
- pp_string (pp,
- colorize_start (pp_show_color (pp),
- diagnostic_kind_color[diagnostic.kind]));
- char *url = NULL;
- if (pp->supports_urls_p ())
- {
- url = rule.make_url ();
- if (url)
- pp_begin_url (pp, url);
- }
- pp_string (pp, desc);
- pp_set_prefix (pp, saved_prefix);
- if (pp->supports_urls_p ())
- if (url)
- pp_end_url (pp);
- free (url);
- pp_string (pp, colorize_stop (pp_show_color (pp)));
- pp_character (pp, ']');
- free (desc);
- }
- }
-}
-
-/* Print any metadata about the option used to control DIAGNOSTIC to CONTEXT's
- printer, e.g. " [-Werror=uninitialized]".
- Subroutine of diagnostic_context::report_diagnostic. */
-
-void
-diagnostic_text_output_format::
-print_option_information (const diagnostic_info &diagnostic,
- diagnostic_t orig_diag_kind)
-{
- if (char *option_text
- = m_context.make_option_name (diagnostic.option_index,
- orig_diag_kind, diagnostic.kind))
- {
- char *option_url = nullptr;
- pretty_printer * const pp = m_context.printer;
- if (pp->supports_urls_p ())
- option_url = m_context.make_option_url (diagnostic.option_index);
- pp_string (pp, " [");
- pp_string (pp, colorize_start (pp_show_color (pp),
- diagnostic_kind_color[diagnostic.kind]));
- if (option_url)
- pp_begin_url (pp, option_url);
- pp_string (pp, option_text);
- if (option_url)
- {
- pp_end_url (pp);
- free (option_url);
- }
- pp_string (pp, colorize_stop (pp_show_color (pp)));
- pp_character (pp, ']');
- free (option_text);
- }
-}
-
/* Set the output format for CONTEXT to FORMAT, using BASE_FILE_NAME for
file-based output formats. */
class logical_location;
class diagnostic_diagram;
class diagnostic_source_effect_info;
-
-/* Abstract base class for a particular output format for diagnostics;
- each value of -fdiagnostics-output-format= will have its own
- implementation. */
-
-class diagnostic_output_format
-{
-public:
- virtual ~diagnostic_output_format () {}
-
- virtual void on_begin_group () = 0;
- virtual void on_end_group () = 0;
-
- /* Vfunc with responsibility for phase 3 of formatting the message
- and "printing" the result. */
- virtual void on_report_diagnostic (const diagnostic_info &,
- diagnostic_t orig_diag_kind) = 0;
-
- virtual void on_diagram (const diagnostic_diagram &diagram) = 0;
- virtual bool machine_readable_stderr_p () const = 0;
-
-protected:
- diagnostic_output_format (diagnostic_context &context)
- : m_context (context)
- {}
-
- diagnostic_context &m_context;
-};
-
-/* Subclass of diagnostic_output_format for classic text-based output
- to stderr.
-
- Uses diagnostic_context.m_text_callbacks to provide client-specific
- textual output (e.g. include paths, macro expansions, etc). */
-
-class diagnostic_text_output_format : public diagnostic_output_format
-{
-public:
- diagnostic_text_output_format (diagnostic_context &context)
- : diagnostic_output_format (context)
- {}
- ~diagnostic_text_output_format ();
- void on_begin_group () override {}
- void on_end_group () override {}
- void on_report_diagnostic (const diagnostic_info &,
- diagnostic_t orig_diag_kind) override;
- void on_diagram (const diagnostic_diagram &diagram) override;
- bool machine_readable_stderr_p () const final override
- {
- return false;
- }
-
-private:
- void print_any_cwe (const diagnostic_info &diagnostic);
- void print_any_rules (const diagnostic_info &diagnostic);
- void print_option_information (const diagnostic_info &diagnostic,
- diagnostic_t orig_diag_kind);
-};
+class diagnostic_output_format;
+ class diagnostic_text_output_format;
/* A stack of sets of classifications: each entry in the stack is
a mapping from option index to diagnostic severity that can be changed
extern char *build_message_string (const char *, ...) ATTRIBUTE_PRINTF_1;
-extern void diagnostic_output_format_init (diagnostic_context &,
- const char *main_input_filename_,
- const char *base_file_name,
- enum diagnostics_output_format,
- bool json_formatting);
-extern void diagnostic_output_format_init_json_stderr (diagnostic_context &context,
- bool formatted);
-extern void diagnostic_output_format_init_json_file (diagnostic_context &context,
- bool formatted,
- const char *base_file_name);
-extern void diagnostic_output_format_init_sarif_stderr (diagnostic_context &context,
- const line_maps *line_maps,
- const char *main_input_filename_,
- bool formatted);
-extern void diagnostic_output_format_init_sarif_file (diagnostic_context &context,
- const line_maps *line_maps,
- const char *main_input_filename_,
- bool formatted,
- const char *base_file_name);
-extern void diagnostic_output_format_init_sarif_stream (diagnostic_context &context,
- const line_maps *line_maps,
- const char *main_input_filename_,
- bool formatted,
- FILE *stream);
-
/* Compute the number of digits in the decimal representation of an integer. */
extern int num_digits (int);
#include "opt-suggestions.h"
#include "gcc.h"
#include "diagnostic.h"
+#include "diagnostic-format.h"
#include "flags.h"
#include "opts.h"
#include "filenames.h"
#include "spellcheck.h"
#include "opt-suggestions.h"
#include "diagnostic-color.h"
+#include "diagnostic-format.h"
#include "version.h"
#include "selftest.h"
#include "file-prefix-map.h"
#include "plugin-version.h"
#include "c-family/c-common.h"
#include "diagnostic.h"
+#include "diagnostic-format-text.h"
#include "context.h"
int plugin_is_GPL_compatible;