Line data Source code
1 : /* RAII class for managing FILE * for diagnostic formats.
2 : Copyright (C) 2024-2026 Free Software Foundation, Inc.
3 : Contributed by David Malcolm <dmalcolm@redhat.com>.
4 :
5 : This file is part of GCC.
6 :
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.
11 :
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.
16 :
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/>. */
20 :
21 : #ifndef GCC_DIAGNOSTICS_OUTPUT_FILE_H
22 : #define GCC_DIAGNOSTICS_OUTPUT_FILE_H
23 :
24 : #include "label-text.h"
25 :
26 : namespace diagnostics {
27 :
28 : /* RAII class for wrapping a FILE * that could be borrowed or owned,
29 : along with the underlying filename. */
30 :
31 : class output_file
32 : {
33 : public:
34 39 : output_file ()
35 39 : : m_outf (nullptr),
36 39 : m_owned (false),
37 39 : m_filename ()
38 : {
39 : }
40 129 : output_file (FILE *outf, bool owned, label_text filename)
41 129 : : m_outf (outf),
42 129 : m_owned (owned),
43 129 : m_filename (std::move (filename))
44 : {
45 129 : gcc_assert (m_filename.get ());
46 129 : if (m_owned)
47 129 : gcc_assert (m_outf);
48 129 : }
49 465 : ~output_file ()
50 : {
51 465 : if (m_owned)
52 : {
53 129 : gcc_assert (m_outf);
54 129 : fclose (m_outf);
55 : }
56 465 : }
57 : output_file (const output_file &other) = delete;
58 297 : output_file (output_file &&other)
59 297 : : m_outf (other.m_outf),
60 297 : m_owned (other.m_owned),
61 297 : m_filename (std::move (other.m_filename))
62 : {
63 297 : other.m_outf = nullptr;
64 297 : other.m_owned = false;
65 :
66 297 : gcc_assert (m_filename.get ());
67 297 : if (m_owned)
68 297 : gcc_assert (m_outf);
69 297 : }
70 : output_file &
71 : operator= (const output_file &other) = delete;
72 : output_file &
73 39 : operator= (output_file &&other)
74 : {
75 39 : if (m_owned)
76 : {
77 0 : gcc_assert (m_outf);
78 0 : fclose (m_outf);
79 : }
80 :
81 39 : m_outf = other.m_outf;
82 39 : other.m_outf = nullptr;
83 :
84 39 : m_owned = other.m_owned;
85 39 : other.m_owned = false;
86 :
87 39 : m_filename = std::move (other.m_filename);
88 :
89 39 : if (m_owned)
90 39 : gcc_assert (m_outf);
91 39 : return *this;
92 : }
93 :
94 39 : operator bool () const { return m_outf != nullptr; }
95 258 : FILE *get_open_file () const { return m_outf; }
96 129 : const char *get_filename () const { return m_filename.get (); }
97 :
98 : static output_file
99 : try_to_open (context &dc,
100 : line_maps *line_maps,
101 : const char *base_file_name,
102 : const char *extension,
103 : bool binary);
104 :
105 : private:
106 : FILE *m_outf;
107 : bool m_owned;
108 : label_text m_filename;
109 : };
110 :
111 : } // namespace diagnostics
112 :
113 : #endif /* ! GCC_DIAGNOSTICS_OUTPUT_FILE_H */
|