aboutsummaryrefslogtreecommitdiff
path: root/gcc/diagnostic-output-file.h
diff options
context:
space:
mode:
authorDavid Malcolm <dmalcolm@redhat.com>2024-10-04 18:31:17 -0400
committerDavid Malcolm <dmalcolm@redhat.com>2024-10-04 18:31:17 -0400
commit385a232229a5b4ee3f4d2a2472bcda28cd8d17b2 (patch)
treeafd52325efdfcefdd7d5d6997672d26f3c9ac0c8 /gcc/diagnostic-output-file.h
parent7d2845da112214f064e7b24531cc67e256b5177e (diff)
downloadgcc-385a232229a5b4ee3f4d2a2472bcda28cd8d17b2.zip
gcc-385a232229a5b4ee3f4d2a2472bcda28cd8d17b2.tar.gz
gcc-385a232229a5b4ee3f4d2a2472bcda28cd8d17b2.tar.bz2
diagnostics: bulletproof opening of SARIF output [PR116978]
Introduce a new RAII class diagnostic_output_file to track ownership of the FILE * for SARIF output. In particular, the .sarif file is now opened immediately, rather than at the end of the compile, and so will fail earlier if the file can't be opened. Doing so fixes a couple of ICEs in -fdiagnostics-format=sarif-file when invoking, say, cc1 directly, rather than from the driver. gcc/ChangeLog: PR other/116978 * diagnostic-format-sarif.cc (sarif_builder::sarif_builder): Gracefully handle "main_input_filename_" being NULL. (sarif_output_format::sarif_output_format): Replace param "base_file_name" with "output_file" and assert that the file was opened successfully and has a non-NULL filename. (sarif_output_format::~sarif_file_output_format): Move responsibility for building the filename and opening the file from here to the creator of the instance. (sarif_output_format::m_base_file_name): Replace with... (sarif_output_format::m_output_file): ...this. (diagnostic_output_format_init_sarif_file): Make "line_maps" param non-const. Gracefully handle "base_file_name" being NULL. Construct the filename and open the file here, rather than in ~sarif_file_output_format, and handle failures immediately here, rather than at the end of the compile. * diagnostic-format-sarif.h: Include "diagnostic-output-file.h". (diagnostic_output_format_init_sarif_file): Make "line_maps" param non-const. * diagnostic-output-file.h: New file. * diagnostic.cc (diagnostic_context::emit_diagnostic): New. (diagnostic_context::emit_diagnostic_va): New. * diagnostic.h (diagnostic_context::emit_diagnostic): New decl. (diagnostic_context::emit_diagnostic_va): New decl. Signed-off-by: David Malcolm <dmalcolm@redhat.com>
Diffstat (limited to 'gcc/diagnostic-output-file.h')
-rw-r--r--gcc/diagnostic-output-file.h75
1 files changed, 75 insertions, 0 deletions
diff --git a/gcc/diagnostic-output-file.h b/gcc/diagnostic-output-file.h
new file mode 100644
index 0000000..f0ae5e1
--- /dev/null
+++ b/gcc/diagnostic-output-file.h
@@ -0,0 +1,75 @@
+/* RAII class for managing FILE * for diagnostic formats.
+ Copyright (C) 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_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 (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) = delete;
+
+ operator bool () const { return m_outf != nullptr; }
+ FILE *get_open_file () const { return m_outf; }
+ const char *get_filename () const { return m_filename.get (); }
+
+private:
+ FILE *m_outf;
+ bool m_owned;
+ label_text m_filename;
+};
+
+#endif /* ! GCC_DIAGNOSTIC_OUTPUT_FILE_H */