x86: Add a test for PR rtl-optimization/111673
[official-gcc.git] / gcc / diagnostic-output-file.h
blob2e877c998cc2db579258dd10a26d7354ff56be7e
1 /* RAII class for managing FILE * for diagnostic formats.
2 Copyright (C) 2024-2025 Free Software Foundation, Inc.
3 Contributed by David Malcolm <dmalcolm@redhat.com>.
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify it under
8 the terms of the GNU General Public License as published by the Free
9 Software Foundation; either version 3, or (at your option) any later
10 version.
12 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15 for more details.
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING3. If not see
19 <http://www.gnu.org/licenses/>. */
21 #ifndef GCC_DIAGNOSTIC_OUTPUT_FILE_H
22 #define GCC_DIAGNOSTIC_OUTPUT_FILE_H
24 /* RAII class for wrapping a FILE * that could be borrowed or owned,
25 along with the underlying filename. */
27 class diagnostic_output_file
29 public:
30 diagnostic_output_file ()
31 : m_outf (nullptr),
32 m_owned (false),
33 m_filename ()
36 diagnostic_output_file (FILE *outf, bool owned, label_text filename)
37 : m_outf (outf),
38 m_owned (owned),
39 m_filename (std::move (filename))
41 gcc_assert (m_filename.get ());
42 if (m_owned)
43 gcc_assert (m_outf);
45 ~diagnostic_output_file ()
47 if (m_owned)
49 gcc_assert (m_outf);
50 fclose (m_outf);
53 diagnostic_output_file (const diagnostic_output_file &other) = delete;
54 diagnostic_output_file (diagnostic_output_file &&other)
55 : m_outf (other.m_outf),
56 m_owned (other.m_owned),
57 m_filename (std::move (other.m_filename))
59 other.m_outf = nullptr;
60 other.m_owned = false;
62 gcc_assert (m_filename.get ());
63 if (m_owned)
64 gcc_assert (m_outf);
66 diagnostic_output_file &
67 operator= (const diagnostic_output_file &other) = delete;
68 diagnostic_output_file &
69 operator= (diagnostic_output_file &&other)
71 if (m_owned)
73 gcc_assert (m_outf);
74 fclose (m_outf);
77 m_outf = other.m_outf;
78 other.m_outf = nullptr;
80 m_owned = other.m_owned;
81 other.m_owned = false;
83 m_filename = std::move (other.m_filename);
85 if (m_owned)
86 gcc_assert (m_outf);
87 return *this;
90 operator bool () const { return m_outf != nullptr; }
91 FILE *get_open_file () const { return m_outf; }
92 const char *get_filename () const { return m_filename.get (); }
94 private:
95 FILE *m_outf;
96 bool m_owned;
97 label_text m_filename;
100 #endif /* ! GCC_DIAGNOSTIC_OUTPUT_FILE_H */