From accd28c1aa02439901c05a9a41d9a3611978d809 Mon Sep 17 00:00:00 2001 From: David Malcolm Date: Fri, 25 Jul 2025 15:13:36 -0400 Subject: diagnostics: rename diagnostic_output_file to diagnostics::output_file No functional change intended. gcc/ChangeLog: * diagnostic-format-html.cc: Update #include for move of diagnostic-output-file.h to diagnostics/output-file.h. Update for move of diagnostic_output_file to diagnostics::output_file. * diagnostic-format-html.h: Likewise. * diagnostic-format-sarif.cc: Likewise. * diagnostic-format-sarif.h: Likewise. * diagnostic-output-spec.cc: Likewise. * diagnostic-output-spec.h (along): Likewise. * diagnostic-output-file.h: Move to... * diagnostics/output-file.h: ...here, updating header guard. (class diagnostic_output_file): Move to... (class diagnostics::output_file): ...here. * libgdiagnostics.cc (sarif_sink::sarif_sink): Update for move of diagnostic_output_file to diagnostics::output_file. Signed-off-by: David Malcolm --- gcc/diagnostic-format-html.cc | 24 ++++----- gcc/diagnostic-format-html.h | 6 +-- gcc/diagnostic-format-sarif.cc | 54 ++++++++++---------- gcc/diagnostic-format-sarif.h | 6 +-- gcc/diagnostic-output-file.h | 107 --------------------------------------- gcc/diagnostic-output-spec.cc | 10 ++-- gcc/diagnostic-output-spec.h | 4 +- gcc/diagnostics/output-file.h | 111 +++++++++++++++++++++++++++++++++++++++++ gcc/libgdiagnostics.cc | 4 +- 9 files changed, 165 insertions(+), 161 deletions(-) delete mode 100644 gcc/diagnostic-output-file.h create mode 100644 gcc/diagnostics/output-file.h (limited to 'gcc') diff --git a/gcc/diagnostic-format-html.cc b/gcc/diagnostic-format-html.cc index 90a2ee2..df0a32a 100644 --- a/gcc/diagnostic-format-html.cc +++ b/gcc/diagnostic-format-html.cc @@ -30,7 +30,7 @@ along with GCC; see the file COPYING3. If not see #include "diagnostic-format-html.h" #include "diagnostic-format-text.h" #include "diagnostic-format-sarif.h" -#include "diagnostic-output-file.h" +#include "diagnostics/output-file.h" #include "diagnostic-buffer.h" #include "diagnostic-path.h" #include "diagnostics/client-data-hooks.h" @@ -1422,9 +1422,9 @@ public: html_file_output_format (diagnostic_context &context, const line_maps *line_maps, const html_generation_options &html_gen_opts, - diagnostic_output_file output_file) + output_file output_file_) : html_output_format (context, line_maps, html_gen_opts), - m_output_file (std::move (output_file)) + m_output_file (std::move (output_file_)) { gcc_assert (m_output_file.get_open_file ()); gcc_assert (m_output_file.get_filename ()); @@ -1446,15 +1446,15 @@ public: } private: - diagnostic_output_file m_output_file; + output_file m_output_file; }; /* Attempt to open BASE_FILE_NAME.html for writing. - Return a non-null diagnostic_output_file, - or return a null diagnostic_output_file and complain to CONTEXT + Return a non-null output_file, + or return a null output_file and complain to CONTEXT using LINE_MAPS. */ -diagnostic_output_file +output_file diagnostic_output_format_open_html_file (diagnostic_context &context, line_maps *line_maps, const char *base_file_name) @@ -1465,7 +1465,7 @@ diagnostic_output_format_open_html_file (diagnostic_context &context, context.emit_diagnostic_with_group (DK_ERROR, richloc, nullptr, 0, "unable to determine filename for HTML output"); - return diagnostic_output_file (); + return output_file (); } label_text filename = label_text::take (concat (base_file_name, @@ -1479,22 +1479,22 @@ diagnostic_output_format_open_html_file (diagnostic_context &context, (DK_ERROR, richloc, nullptr, 0, "unable to open %qs for HTML output: %m", filename.get ()); - return diagnostic_output_file (); + return output_file (); } - return diagnostic_output_file (outf, true, std::move (filename)); + return output_file (outf, true, std::move (filename)); } std::unique_ptr make_html_sink (diagnostic_context &context, const line_maps &line_maps, const html_generation_options &html_gen_opts, - diagnostic_output_file output_file) + output_file output_file_) { auto sink = std::make_unique (context, &line_maps, html_gen_opts, - std::move (output_file)); + std::move (output_file_)); sink->update_printer (); return sink; } diff --git a/gcc/diagnostic-format-html.h b/gcc/diagnostic-format-html.h index 537ba54..d55cd30 100644 --- a/gcc/diagnostic-format-html.h +++ b/gcc/diagnostic-format-html.h @@ -22,7 +22,7 @@ along with GCC; see the file COPYING3. If not see #define GCC_DIAGNOSTIC_FORMAT_HTML_H #include "diagnostic-format.h" -#include "diagnostic-output-file.h" +#include "diagnostics/output-file.h" struct html_generation_options { @@ -43,7 +43,7 @@ struct html_generation_options bool m_show_state_diagrams_dot_src; }; -extern diagnostic_output_file +extern diagnostics::output_file diagnostic_output_format_open_html_file (diagnostic_context &context, line_maps *line_maps, const char *base_file_name); @@ -52,7 +52,7 @@ extern std::unique_ptr make_html_sink (diagnostic_context &context, const line_maps &line_maps, const html_generation_options &html_gen_opts, - diagnostic_output_file output_file); + diagnostics::output_file output_file_); extern void print_path_as_html (xml::printer &xp, diff --git a/gcc/diagnostic-format-sarif.cc b/gcc/diagnostic-format-sarif.cc index 75451cb..4f1cd0b 100644 --- a/gcc/diagnostic-format-sarif.cc +++ b/gcc/diagnostic-format-sarif.cc @@ -3916,7 +3916,7 @@ public: { m_builder.on_report_diagnostic (diagnostic, orig_diag_kind, m_buffer); } - void on_diagram (const diagnostics::diagram &d) final override + void on_diagram (const diagram &d) final override { m_builder.emit_diagram (d); } @@ -3983,10 +3983,10 @@ public: const line_maps *line_maps, std::unique_ptr serialization_format, const sarif_generation_options &sarif_gen_opts, - diagnostic_output_file output_file) + output_file output_file_) : sarif_output_format (context, line_maps, std::move (serialization_format), sarif_gen_opts), - m_output_file (std::move (output_file)) + m_output_file (std::move (output_file_)) { gcc_assert (m_output_file.get_open_file ()); gcc_assert (m_output_file.get_filename ()); @@ -4008,7 +4008,7 @@ public: } private: - diagnostic_output_file m_output_file; + output_file m_output_file; }; /* Print the start of an embedded link to PP, as per 3.11.6. */ @@ -4167,16 +4167,16 @@ diagnostic_output_format_init_sarif_stderr (diagnostic_context &context, } /* Attempt to open "BASE_FILE_NAME""EXTENSION" for writing. - Return a non-null diagnostic_output_file, - or return a null diagnostic_output_file and complain to CONTEXT + Return a non-null output_file, + or return a null output_file and complain to CONTEXT using LINE_MAPS. */ -diagnostic_output_file -diagnostic_output_file::try_to_open (diagnostic_context &context, - line_maps *line_maps, - const char *base_file_name, - const char *extension, - bool is_binary) +output_file +output_file::try_to_open (diagnostic_context &context, + line_maps *line_maps, + const char *base_file_name, + const char *extension, + bool is_binary) { gcc_assert (extension); gcc_assert (extension[0] == '.'); @@ -4187,7 +4187,7 @@ diagnostic_output_file::try_to_open (diagnostic_context &context, context.emit_diagnostic_with_group (DK_ERROR, richloc, nullptr, 0, "unable to determine filename for SARIF output"); - return diagnostic_output_file (); + return output_file (); } label_text filename = label_text::take (concat (base_file_name, @@ -4201,17 +4201,17 @@ diagnostic_output_file::try_to_open (diagnostic_context &context, (DK_ERROR, richloc, nullptr, 0, "unable to open %qs for diagnostic output: %m", filename.get ()); - return diagnostic_output_file (); + return output_file (); } - return diagnostic_output_file (outf, true, std::move (filename)); + return output_file (outf, true, std::move (filename)); } /* Attempt to open BASE_FILE_NAME.sarif for writing JSON. - Return a non-null diagnostic_output_file, - or return a null diagnostic_output_file and complain to CONTEXT + Return a non-null output_file, + or return a null output_file and complain to CONTEXT using LINE_MAPS. */ -diagnostic_output_file +output_file diagnostic_output_format_open_sarif_file (diagnostic_context &context, line_maps *line_maps, const char *base_file_name, @@ -4229,11 +4229,11 @@ diagnostic_output_format_open_sarif_file (diagnostic_context &context, break; } - return diagnostic_output_file::try_to_open (context, - line_maps, - base_file_name, - suffix, - is_binary); + return output_file::try_to_open (context, + line_maps, + base_file_name, + suffix, + is_binary); } /* Populate CONTEXT in preparation for SARIF output to a file named @@ -4248,7 +4248,7 @@ diagnostic_output_format_init_sarif_file (diagnostic_context &context, { gcc_assert (line_maps); - diagnostic_output_file output_file + output_file output_file_ = diagnostic_output_format_open_sarif_file (context, line_maps, base_file_name, @@ -4263,7 +4263,7 @@ diagnostic_output_format_init_sarif_file (diagnostic_context &context, line_maps, std::move (serialization), sarif_gen_opts, - std::move (output_file))); + std::move (output_file_))); } /* Populate CONTEXT in preparation for SARIF output to STREAM. @@ -4293,14 +4293,14 @@ make_sarif_sink (diagnostic_context &context, const line_maps &line_maps, std::unique_ptr serialization, const sarif_generation_options &sarif_gen_opts, - diagnostic_output_file output_file) + output_file output_file_) { auto sink = std::make_unique (context, &line_maps, std::move (serialization), sarif_gen_opts, - std::move (output_file)); + std::move (output_file_)); sink->update_printer (); return sink; } diff --git a/gcc/diagnostic-format-sarif.h b/gcc/diagnostic-format-sarif.h index 29ee9a8..b5efd7a 100644 --- a/gcc/diagnostic-format-sarif.h +++ b/gcc/diagnostic-format-sarif.h @@ -23,7 +23,7 @@ along with GCC; see the file COPYING3. If not see #include "json.h" #include "diagnostic-format.h" -#include "diagnostic-output-file.h" +#include "diagnostics/output-file.h" #include "diagnostics/logical-locations.h" /* Enum for choosing what format to serializing the generated SARIF into. */ @@ -35,7 +35,7 @@ enum class sarif_serialization_kind num_values }; -extern diagnostic_output_file +extern diagnostics::output_file diagnostic_output_format_open_sarif_file (diagnostic_context &context, line_maps *line_maps, const char *base_file_name, @@ -109,7 +109,7 @@ make_sarif_sink (diagnostic_context &context, const line_maps &line_maps, std::unique_ptr serialization_format, const sarif_generation_options &sarif_gen_opts, - diagnostic_output_file output_file); + diagnostics::output_file output_file_); class sarif_builder; class sarif_location_manager; diff --git a/gcc/diagnostic-output-file.h b/gcc/diagnostic-output-file.h deleted file mode 100644 index a0b2e1b..0000000 --- a/gcc/diagnostic-output-file.h +++ /dev/null @@ -1,107 +0,0 @@ -/* RAII class for managing FILE * for diagnostic formats. - Copyright (C) 2024-2025 Free Software Foundation, Inc. - Contributed by David Malcolm . - -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 -. */ - -#ifndef GCC_DIAGNOSTIC_OUTPUT_FILE_H -#define GCC_DIAGNOSTIC_OUTPUT_FILE_H - -/* RAII class for wrapping a FILE * that could be borrowed or owned, - along with the underlying filename. */ - -class diagnostic_output_file -{ -public: - diagnostic_output_file () - : m_outf (nullptr), - m_owned (false), - m_filename () - { - } - diagnostic_output_file (FILE *outf, bool owned, label_text filename) - : m_outf (outf), - m_owned (owned), - m_filename (std::move (filename)) - { - gcc_assert (m_filename.get ()); - if (m_owned) - gcc_assert (m_outf); - } - ~diagnostic_output_file () - { - if (m_owned) - { - gcc_assert (m_outf); - fclose (m_outf); - } - } - diagnostic_output_file (const diagnostic_output_file &other) = delete; - diagnostic_output_file (diagnostic_output_file &&other) - : m_outf (other.m_outf), - m_owned (other.m_owned), - m_filename (std::move (other.m_filename)) - { - other.m_outf = nullptr; - other.m_owned = false; - - gcc_assert (m_filename.get ()); - if (m_owned) - gcc_assert (m_outf); - } - diagnostic_output_file & - operator= (const diagnostic_output_file &other) = delete; - diagnostic_output_file & - operator= (diagnostic_output_file &&other) - { - if (m_owned) - { - gcc_assert (m_outf); - fclose (m_outf); - } - - m_outf = other.m_outf; - other.m_outf = nullptr; - - m_owned = other.m_owned; - other.m_owned = false; - - m_filename = std::move (other.m_filename); - - if (m_owned) - gcc_assert (m_outf); - return *this; - } - - operator bool () const { return m_outf != nullptr; } - FILE *get_open_file () const { return m_outf; } - const char *get_filename () const { return m_filename.get (); } - - static diagnostic_output_file - try_to_open (diagnostic_context &context, - line_maps *line_maps, - const char *base_file_name, - const char *extension, - bool binary); - -private: - FILE *m_outf; - bool m_owned; - label_text m_filename; -}; - -#endif /* ! GCC_DIAGNOSTIC_OUTPUT_FILE_H */ diff --git a/gcc/diagnostic-output-spec.cc b/gcc/diagnostic-output-spec.cc index 4fa3176..b028482c 100644 --- a/gcc/diagnostic-output-spec.cc +++ b/gcc/diagnostic-output-spec.cc @@ -241,16 +241,16 @@ context::report_missing_key (const char *unparsed_arg, get_option_name (), scheme_name.c_str (), key.c_str (), metavar); } -diagnostic_output_file +diagnostics::output_file context::open_output_file (label_text &&filename) const { FILE *outf = fopen (filename.get (), "w"); if (!outf) { report_error ("unable to open %qs: %m", filename.get ()); - return diagnostic_output_file (nullptr, false, std::move (filename)); + return diagnostics::output_file (nullptr, false, std::move (filename)); } - return diagnostic_output_file (outf, true, std::move (filename)); + return diagnostics::output_file (outf, true, std::move (filename)); } static std::unique_ptr @@ -488,7 +488,7 @@ sarif_scheme_handler::make_sink (const context &ctxt, return nullptr; } - diagnostic_output_file output_file; + diagnostics::output_file output_file; if (filename.get ()) output_file = ctxt.open_output_file (std::move (filename)); else @@ -621,7 +621,7 @@ html_scheme_handler::make_sink (const context &ctxt, return nullptr; } - diagnostic_output_file output_file; + diagnostics::output_file output_file; if (filename.get ()) output_file = ctxt.open_output_file (std::move (filename)); else diff --git a/gcc/diagnostic-output-spec.h b/gcc/diagnostic-output-spec.h index e02cdfe..1ffe4a3 100644 --- a/gcc/diagnostic-output-spec.h +++ b/gcc/diagnostic-output-spec.h @@ -22,7 +22,7 @@ along with GCC; see the file COPYING3. If not see #define GCC_DIAGNOSTIC_OUTPUT_SPEC_H #include "diagnostic-format.h" -#include "diagnostic-output-file.h" +#include "diagnostics/output-file.h" namespace diagnostics_output_spec { @@ -52,7 +52,7 @@ class context const std::string &scheme_name, const char *metavar) const; - diagnostic_output_file + diagnostics::output_file open_output_file (label_text &&filename) const; const char * diff --git a/gcc/diagnostics/output-file.h b/gcc/diagnostics/output-file.h new file mode 100644 index 0000000..09df417 --- /dev/null +++ b/gcc/diagnostics/output-file.h @@ -0,0 +1,111 @@ +/* RAII class for managing FILE * for diagnostic formats. + Copyright (C) 2024-2025 Free Software Foundation, Inc. + Contributed by David Malcolm . + +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 +. */ + +#ifndef GCC_DIAGNOSTICS_OUTPUT_FILE_H +#define GCC_DIAGNOSTICS_OUTPUT_FILE_H + +namespace diagnostics { + +/* RAII class for wrapping a FILE * that could be borrowed or owned, + along with the underlying filename. */ + +class output_file +{ +public: + output_file () + : m_outf (nullptr), + m_owned (false), + m_filename () + { + } + output_file (FILE *outf, bool owned, label_text filename) + : m_outf (outf), + m_owned (owned), + m_filename (std::move (filename)) + { + gcc_assert (m_filename.get ()); + if (m_owned) + gcc_assert (m_outf); + } + ~output_file () + { + if (m_owned) + { + gcc_assert (m_outf); + fclose (m_outf); + } + } + output_file (const output_file &other) = delete; + output_file (output_file &&other) + : m_outf (other.m_outf), + m_owned (other.m_owned), + m_filename (std::move (other.m_filename)) + { + other.m_outf = nullptr; + other.m_owned = false; + + gcc_assert (m_filename.get ()); + if (m_owned) + gcc_assert (m_outf); + } + output_file & + operator= (const output_file &other) = delete; + output_file & + operator= (output_file &&other) + { + if (m_owned) + { + gcc_assert (m_outf); + fclose (m_outf); + } + + m_outf = other.m_outf; + other.m_outf = nullptr; + + m_owned = other.m_owned; + other.m_owned = false; + + m_filename = std::move (other.m_filename); + + if (m_owned) + gcc_assert (m_outf); + return *this; + } + + operator bool () const { return m_outf != nullptr; } + FILE *get_open_file () const { return m_outf; } + const char *get_filename () const { return m_filename.get (); } + + static output_file + try_to_open (diagnostic_context &context, + line_maps *line_maps, + const char *base_file_name, + const char *extension, + bool binary); + +private: + FILE *m_outf; + bool m_owned; + label_text m_filename; +}; + +} // namespace diagnostics + +#endif /* ! GCC_DIAGNOSTICS_OUTPUT_FILE_H */ diff --git a/gcc/libgdiagnostics.cc b/gcc/libgdiagnostics.cc index 65bad53..692fca7 100644 --- a/gcc/libgdiagnostics.cc +++ b/gcc/libgdiagnostics.cc @@ -1518,8 +1518,8 @@ sarif_sink::sarif_sink (diagnostic_manager &mgr, const sarif_generation_options &sarif_gen_opts) : sink (mgr) { - diagnostic_output_file output_file (dst_stream, false, - label_text::borrow ("sarif_sink")); + diagnostics::output_file output_file (dst_stream, false, + label_text::borrow ("sarif_sink")); auto serialization = std::make_unique (true); auto inner_sink = make_sarif_sink (mgr.get_dc (), *mgr.get_line_table (), -- cgit v1.1