GCC Middle and Back End API Reference
output-file.h
Go to the documentation of this file.
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
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_DIAGNOSTICS_OUTPUT_FILE_H
22#define GCC_DIAGNOSTICS_OUTPUT_FILE_H
23
24#include "label-text.h"
25
26namespace diagnostics {
27
28/* RAII class for wrapping a FILE * that could be borrowed or owned,
29 along with the underlying filename. */
30
32{
33public:
35 : m_outf (nullptr),
36 m_owned (false),
37 m_filename ()
38 {
39 }
40 output_file (FILE *outf, bool owned, label_text filename)
41 : m_outf (outf),
42 m_owned (owned),
43 m_filename (std::move (filename))
44 {
45 gcc_assert (m_filename.get ());
46 if (m_owned)
48 }
50 {
51 if (m_owned)
52 {
54 fclose (m_outf);
55 }
56 }
57 output_file (const output_file &other) = delete;
59 : m_outf (other.m_outf),
60 m_owned (other.m_owned),
61 m_filename (std::move (other.m_filename))
62 {
63 other.m_outf = nullptr;
64 other.m_owned = false;
65
66 gcc_assert (m_filename.get ());
67 if (m_owned)
69 }
71 operator= (const output_file &other) = delete;
74 {
75 if (m_owned)
76 {
78 fclose (m_outf);
79 }
80
81 m_outf = other.m_outf;
82 other.m_outf = nullptr;
83
84 m_owned = other.m_owned;
85 other.m_owned = false;
86
87 m_filename = std::move (other.m_filename);
88
89 if (m_owned)
91 return *this;
92 }
93
94 operator bool () const { return m_outf != nullptr; }
95 FILE *get_open_file () const { return m_outf; }
96 const char *get_filename () const { return m_filename.get (); }
97
98 static output_file
100 line_maps *line_maps,
101 const char *base_file_name,
102 const char *extension,
103 bool binary);
104
105private:
106 FILE *m_outf;
108 label_text m_filename;
109};
110
111} // namespace diagnostics
112
113#endif /* ! GCC_DIAGNOSTICS_OUTPUT_FILE_H */
Definition diagnostics/context.h:253
Definition output-file.h:32
~output_file()
Definition output-file.h:49
output_file & operator=(const output_file &other)=delete
label_text m_filename
Definition output-file.h:108
output_file(output_file &&other)
Definition output-file.h:58
static output_file try_to_open(context &dc, line_maps *line_maps, const char *base_file_name, const char *extension, bool binary)
Definition sarif-sink.cc:4213
const char * get_filename() const
Definition output-file.h:96
output_file(FILE *outf, bool owned, label_text filename)
Definition output-file.h:40
FILE * get_open_file() const
Definition output-file.h:95
output_file()
Definition output-file.h:34
FILE * m_outf
Definition output-file.h:106
output_file(const output_file &other)=delete
bool m_owned
Definition output-file.h:107
static const char * base_file_name(const char *file_name)
Definition genautomata.cc:9229
Definition coretypes.h:167
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