GCC Middle and Back End API Reference
diagnostic-output-file.h
Go to the documentation of this file.
1/* RAII class for managing FILE * for diagnostic formats.
2 Copyright (C) 2024 Free Software Foundation, Inc.
3 Contributed by David Malcolm <dmalcolm@redhat.com>.
4
5This file is part of GCC.
6
7GCC is free software; you can redistribute it and/or modify it under
8the terms of the GNU General Public License as published by the Free
9Software Foundation; either version 3, or (at your option) any later
10version.
11
12GCC is distributed in the hope that it will be useful, but WITHOUT ANY
13WARRANTY; without even the implied warranty of MERCHANTABILITY or
14FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15for more details.
16
17You should have received a copy of the GNU General Public License
18along 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
28{
29public:
31 : m_outf (nullptr),
32 m_owned (false),
33 m_filename ()
34 {
35 }
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))
40 {
41 gcc_assert (m_filename.get ());
42 if (m_owned)
44 }
46 {
47 if (m_owned)
48 {
50 fclose (m_outf);
51 }
52 }
55 : m_outf (other.m_outf),
56 m_owned (other.m_owned),
57 m_filename (std::move (other.m_filename))
58 {
59 other.m_outf = nullptr;
60 other.m_owned = false;
61
62 gcc_assert (m_filename.get ());
63 if (m_owned)
65 }
67 operator= (const diagnostic_output_file &other) = delete;
70 {
71 if (m_owned)
72 {
74 fclose (m_outf);
75 }
76
77 m_outf = other.m_outf;
78 other.m_outf = nullptr;
79
80 m_owned = other.m_owned;
81 other.m_owned = false;
82
83 m_filename = std::move (other.m_filename);
84
85 if (m_owned)
87 return *this;
88 }
89
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 (); }
93
94private:
95 FILE *m_outf;
96 bool m_owned;
97 label_text m_filename;
98};
99
100#endif /* ! GCC_DIAGNOSTIC_OUTPUT_FILE_H */
Definition diagnostic-output-file.h:28
diagnostic_output_file(const diagnostic_output_file &other)=delete
label_text m_filename
Definition diagnostic-output-file.h:97
FILE * m_outf
Definition diagnostic-output-file.h:95
diagnostic_output_file & operator=(const diagnostic_output_file &other)=delete
const char * get_filename() const
Definition diagnostic-output-file.h:92
diagnostic_output_file(FILE *outf, bool owned, label_text filename)
Definition diagnostic-output-file.h:36
diagnostic_output_file()
Definition diagnostic-output-file.h:30
bool m_owned
Definition diagnostic-output-file.h:96
FILE * get_open_file() const
Definition diagnostic-output-file.h:91
diagnostic_output_file(diagnostic_output_file &&other)
Definition diagnostic-output-file.h:54
~diagnostic_output_file()
Definition diagnostic-output-file.h:45
Definition ira-emit.cc:158
Definition gengtype.h:377
#define gcc_assert(EXPR)
Definition system.h:814
#define false
Definition system.h:888
#define bool
Definition system.h:886