gcc/gcc/diagnostic-format-text.h
David Malcolm 99a909aba3 Add libdiagnostics (v4)
This patch adds a new libdiagnostics shared library available as
part of the GCC build via --enable-libdiagnostics when
configuring GCC.

It combines the following patches from:
  https://gcc.gnu.org/pipermail/gcc-patches/2024-November/668632.html
    [PATCH 1/8] libdiagnostics v4: header
    [PATCH 2/8] libdiagnostics v4: implementation
    [PATCH 3/8] libdiagnostics: add API docs
    [PATCH 4/8] libdiagnostics v4: add C++ wrapper API
    [PATCH 6/8] libdiagnostics v4: test suite

ChangeLog:
	* configure.ac (--enable-libdiagnostics): New.
	* configure: Regenerate.

gcc/ChangeLog:
	* configure.ac (check_languages): Add check-libdiagnostics.
	(--enable-libdiagnostics): New.
	* configure: Regenerate.
	* Makefile.in (enable_libdiagnostics): New.
	(lang_checks): If libdiagnostics is enabled, add
	check-libdiagnostics.
	(ALL_HOST_OBJS): If libdiagnostics is enabled, add
	$(libdiagnostics_OBJS).
	(start.encap): Add LIBDIAGNOSTICS.
	(libdiagnostics_OBJS): New.
	(LIBDIAGNOSTICS_VERSION_NUM): New, adapted from code in
	jit/Make-lang.in.
	(LIBDIAGNOSTICS_MINOR_NUM): Likewise.
	(LIBDIAGNOSTICS_RELEASE_NUM): Likewise.
	(LIBDIAGNOSTICS_FILENAME): Likewise.
	(LIBDIAGNOSTICS_IMPORT_LIB): Likewise.
	(libdiagnostics): Likewise.
	(LIBDIAGNOSTICS_AGE): Likewise.
	(LIBDIAGNOSTICS_BASENAME): Likewise.
	(LIBDIAGNOSTICS_SONAME): Likewise.
	(LIBDIAGNOSTICS_LINKER_NAME): Likewise.
	(LIBDIAGNOSTICS_COMMA): Likewise.
	(LIBDIAGNOSTICS_VERSION_SCRIPT_OPTION): Likewise.
	(LIBDIAGNOSTICS_SONAME_OPTION): Likewise.
	(LIBDIAGNOSTICS_SONAME_SYMLINK): Likewise.
	(LIBDIAGNOSTICS_LINKER_NAME_SYMLINK): Likewise.
	(LIBDIAGNOSTICS_FILENAME): Likewise.
	(libdiagnostics.serial): Likewise.
	(LIBDIAGNOSTICS_EXTRA_OPTS): Likewise.
	(install): If libdiagnostics is enabled, add
	install-libdiagnostics.
	(libdiagnostics.install-headers): New.
	(libdiagnostics.install-common): New, adapted from code in
	jit/Make-lang.in.
	(install-libdiagnostics): New.
	* diagnostic-format-text.h
	(diagnostic_text_output_format::get_location_text): Make public.
	* doc/install.texi (--enable-libdiagnostics): New.
	* doc/libdiagnostics/Makefile: New file.
	* doc/libdiagnostics/conf.py: New file.
	* doc/libdiagnostics/index.rst: New file.
	* doc/libdiagnostics/make.bat: New file.
	* doc/libdiagnostics/topics/diagnostic-manager.rst: New file.
	* doc/libdiagnostics/topics/diagnostics.rst: New file.
	* doc/libdiagnostics/topics/execution-paths.rst: New file.
	* doc/libdiagnostics/topics/fix-it-hints.rst: New file.
	* doc/libdiagnostics/topics/index.rst: New file.
	* doc/libdiagnostics/topics/logical-locations.rst: New file.
	* doc/libdiagnostics/topics/message-formatting.rst: New file.
	* doc/libdiagnostics/topics/metadata.rst: New file.
	* doc/libdiagnostics/topics/physical-locations.rst: New file.
	* doc/libdiagnostics/topics/retrofitting.rst: New file.
	* doc/libdiagnostics/topics/sarif.rst: New file.
	* doc/libdiagnostics/topics/text-output.rst: New file.
	* doc/libdiagnostics/topics/ux.rst: New file.
	* doc/libdiagnostics/tutorial/01-hello-world.rst: New file.
	* doc/libdiagnostics/tutorial/02-physical-locations.rst: New file.
	* doc/libdiagnostics/tutorial/03-logical-locations.rst: New file.
	* doc/libdiagnostics/tutorial/04-notes.rst: New file.
	* doc/libdiagnostics/tutorial/05-warnings.rst: New file.
	* doc/libdiagnostics/tutorial/06-fix-it-hints.rst: New file.
	* doc/libdiagnostics/tutorial/07-execution-paths.rst: New file.
	* doc/libdiagnostics/tutorial/index.rst: New file.
	* libdiagnostics++.h: New file.
	* libdiagnostics.cc: New file.
	* libdiagnostics.h: New file.
	* libdiagnostics.map: New file.

gcc/testsuite/ChangeLog:
	* libdiagnostics.dg/libdiagnostics.exp: New, adapted from jit.exp.
	* libdiagnostics.dg/sarif.py: New.
	* libdiagnostics.dg/test-dump.c: New test.
	* libdiagnostics.dg/test-error-c.py: New test.
	* libdiagnostics.dg/test-error-with-note-c.py: New test.
	* libdiagnostics.dg/test-error-with-note.c: New test.
	* libdiagnostics.dg/test-error-with-note.cc: New test.
	* libdiagnostics.dg/test-error.c: New test.
	* libdiagnostics.dg/test-error.cc: New test.
	* libdiagnostics.dg/test-example-1.c: New test.
	* libdiagnostics.dg/test-fix-it-hint-c.py: New test.
	* libdiagnostics.dg/test-fix-it-hint.c: New test.
	* libdiagnostics.dg/test-fix-it-hint.cc: New test.
	* libdiagnostics.dg/test-helpers++.h: New test.
	* libdiagnostics.dg/test-helpers.h: New test.
	* libdiagnostics.dg/test-labelled-ranges.c: New test.
	* libdiagnostics.dg/test-labelled-ranges.cc: New test.
	* libdiagnostics.dg/test-labelled-ranges.py: New test.
	* libdiagnostics.dg/test-logical-location-c.py: New test.
	* libdiagnostics.dg/test-logical-location.c: New test.
	* libdiagnostics.dg/test-metadata-c.py: New test.
	* libdiagnostics.dg/test-metadata.c: New test.
	* libdiagnostics.dg/test-multiple-lines-c.py: New test.
	* libdiagnostics.dg/test-multiple-lines.c: New test.
	* libdiagnostics.dg/test-no-column-c.py: New test.
	* libdiagnostics.dg/test-no-column.c: New test.
	* libdiagnostics.dg/test-no-diagnostics-c.py: New test.
	* libdiagnostics.dg/test-no-diagnostics.c: New test.
	* libdiagnostics.dg/test-note-with-fix-it-hint-c.py: New test.
	* libdiagnostics.dg/test-note-with-fix-it-hint.c: New test.
	* libdiagnostics.dg/test-text-sink-options.c: New test.
	* libdiagnostics.dg/test-warning-c.py: New test.
	* libdiagnostics.dg/test-warning-with-path-c.py: New test.
	* libdiagnostics.dg/test-warning-with-path.c: New test.
	* libdiagnostics.dg/test-warning.c: New test.
	* libdiagnostics.dg/test-write-sarif-to-file-c.py: New test.
	* libdiagnostics.dg/test-write-sarif-to-file.c: New test.
	* libdiagnostics.dg/test-write-text-to-file.c: New test.

Signed-off-by: David Malcolm <dmalcolm@redhat.com>
2024-11-18 17:08:36 -05:00

150 lines
5.1 KiB
C++

/* 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,
bool follows_reference_printer = false)
: diagnostic_output_format (context),
m_saved_output_buffer (nullptr),
m_column_policy (context),
m_last_module (nullptr),
m_includes_seen (nullptr),
m_follows_reference_printer (follows_reference_printer),
m_show_nesting (false),
m_show_nesting_levels (false)
{}
~diagnostic_text_output_format ();
void dump (FILE *out, int indent) const override;
std::unique_ptr<diagnostic_per_format_buffer>
make_per_format_buffer () final override;
void set_buffer (diagnostic_per_format_buffer *) final override;
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_report_verbatim (text_info &) final override;
void on_diagram (const diagnostic_diagram &diagram) override;
void after_diagnostic (const diagnostic_info &) override;
bool machine_readable_stderr_p () const final override
{
return false;
}
bool follows_reference_printer_p () const final override;
void update_printer () override;
/* Helpers for writing lang-specific starters/finalizers for text output. */
char *build_prefix (const diagnostic_info &) const;
void report_current_module (location_t where);
void append_note (location_t location,
const char * gmsgid, ...) ATTRIBUTE_GCC_DIAG(3,4);
char *file_name_as_prefix (const char *) const;
char *build_indent_prefix (bool with_bullet) const;
void print_path (const diagnostic_path &path);
bool show_column_p () const { return get_context ().m_show_column; }
const diagnostic_column_policy &get_column_policy () const
{
return m_column_policy;
}
diagnostic_location_print_policy get_location_print_policy () const;
bool show_nesting_p () const { return m_show_nesting; }
bool show_locations_in_nesting_p () const
{
return m_show_locations_in_nesting;
}
void set_show_nesting (bool show_nesting) { m_show_nesting = show_nesting; }
void set_show_locations_in_nesting (bool val)
{
m_show_locations_in_nesting = val;
}
void set_show_nesting_levels (bool show_nesting_levels)
{
m_show_nesting_levels = show_nesting_levels;
}
label_text get_location_text (const expanded_location &s) const;
protected:
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);
bool includes_seen_p (const line_map_ordinary *map);
/* For handling diagnostic_buffer. */
output_buffer *m_saved_output_buffer;
diagnostic_column_policy m_column_policy;
/* Used to detect when the input file stack has changed since last
described. */
const line_map_ordinary *m_last_module;
/* Include files that report_current_module has already listed the
include path for. */
hash_set<location_t, false, location_hash> *m_includes_seen;
/* If true, this is the initial default text output format created
when the diagnostic_context was created, and, in particular, before
initializations of color and m_url_format. Hence this should follow
the dc's reference printer for these.
If false, this text output was created after the dc was created, and
thus tracks its own values for color and m_url_format. */
bool m_follows_reference_printer;
/* If true, then use indentation to show the nesting structure of
nested diagnostics, and print locations on separate lines after the
diagnostic message, rather than as a prefix to the message. */
bool m_show_nesting;
/* Set to false to suppress location-printing when showing nested
diagnostics, for use in DejaGnu tests. */
bool m_show_locations_in_nesting;
/* If true, then add "(level N):" when printing nested diagnostics. */
bool m_show_nesting_levels;
};
#endif /* ! GCC_DIAGNOSTIC_FORMAT_TEXT_H */