Branch data Line data Source code
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>.
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_DIAGNOSTIC_OUTPUT_FILE_H
22 : : #define GCC_DIAGNOSTIC_OUTPUT_FILE_H
23 : :
24 : : /* RAII class for wrapping a FILE * that could be borrowed or owned,
25 : : along with the underlying filename. */
26 : :
27 : : class diagnostic_output_file
28 : : {
29 : : public:
30 : 13 : diagnostic_output_file ()
31 : 13 : : m_outf (nullptr),
32 : 13 : m_owned (false),
33 : 13 : m_filename ()
34 : : {
35 : : }
36 : 102 : diagnostic_output_file (FILE *outf, bool owned, label_text filename)
37 : 102 : : m_outf (outf),
38 : 102 : m_owned (owned),
39 : 102 : m_filename (std::move (filename))
40 : : {
41 : 102 : gcc_assert (m_filename.get ());
42 : 102 : if (m_owned)
43 : 102 : gcc_assert (m_outf);
44 : 102 : }
45 : 332 : ~diagnostic_output_file ()
46 : : {
47 : 332 : if (m_owned)
48 : : {
49 : 102 : gcc_assert (m_outf);
50 : 102 : fclose (m_outf);
51 : : }
52 : 332 : }
53 : : diagnostic_output_file (const diagnostic_output_file &other) = delete;
54 : 217 : diagnostic_output_file (diagnostic_output_file &&other)
55 : 217 : : m_outf (other.m_outf),
56 : 217 : m_owned (other.m_owned),
57 : 217 : m_filename (std::move (other.m_filename))
58 : : {
59 : 217 : other.m_outf = nullptr;
60 : 217 : other.m_owned = false;
61 : :
62 : 217 : gcc_assert (m_filename.get ());
63 : 217 : if (m_owned)
64 : 217 : gcc_assert (m_outf);
65 : 217 : }
66 : : diagnostic_output_file &
67 : : operator= (const diagnostic_output_file &other) = delete;
68 : : diagnostic_output_file &
69 : 13 : operator= (diagnostic_output_file &&other)
70 : : {
71 : 13 : if (m_owned)
72 : : {
73 : 0 : gcc_assert (m_outf);
74 : 0 : fclose (m_outf);
75 : : }
76 : :
77 : 13 : m_outf = other.m_outf;
78 : 13 : other.m_outf = nullptr;
79 : :
80 : 13 : m_owned = other.m_owned;
81 : 13 : other.m_owned = false;
82 : :
83 : 13 : m_filename = std::move (other.m_filename);
84 : :
85 : 13 : if (m_owned)
86 : 13 : gcc_assert (m_outf);
87 : 13 : return *this;
88 : : }
89 : :
90 : 13 : operator bool () const { return m_outf != nullptr; }
91 : 204 : FILE *get_open_file () const { return m_outf; }
92 : 102 : const char *get_filename () const { return m_filename.get (); }
93 : :
94 : : static diagnostic_output_file
95 : : try_to_open (diagnostic_context &context,
96 : : line_maps *line_maps,
97 : : const char *base_file_name,
98 : : const char *extension,
99 : : bool binary);
100 : :
101 : : private:
102 : : FILE *m_outf;
103 : : bool m_owned;
104 : : label_text m_filename;
105 : : };
106 : :
107 : : #endif /* ! GCC_DIAGNOSTIC_OUTPUT_FILE_H */
|