GCC Middle and Back End API Reference
analyzer-logging.h
Go to the documentation of this file.
1/* Hierarchical log messages for the analyzer.
2 Copyright (C) 2014-2026 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
8under the terms of the GNU General Public License as published by
9the Free Software Foundation; either version 3, or (at your option)
10any later version.
11
12GCC is distributed in the hope that it will be useful, but
13WITHOUT ANY WARRANTY; without even the implied warranty of
14MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15General Public License for 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/* Adapted from jit-logging.h. */
22
23#ifndef ANALYZER_LOGGING_H
24#define ANALYZER_LOGGING_H
25
26#include "diagnostic-core.h"
27
28namespace text_art { class canvas; }
29
30namespace ana {
31
32/* A logger encapsulates a logging stream: a way to send
33 lines of pertinent information to a FILE *. */
34
35class logger
36{
37 public:
38 logger (FILE *f_out, int flags, int verbosity, const pretty_printer &reference_pp);
40
41 void incref (const char *reason);
42 void decref (const char *reason);
43
44 void log (const char *fmt, ...)
46 void log_va (const char *fmt, va_list *ap)
49 void log_partial (const char *fmt, ...)
51 void log_va_partial (const char *fmt, va_list *ap)
53 void end_log_line ();
54
55 void log_canvas (const text_art::canvas &);
56
57 void enter_scope (const char *scope_name);
58 void enter_scope (const char *scope_name, const char *fmt, va_list *ap)
60 void exit_scope (const char *scope_name);
63
64 pretty_printer *get_printer () const { return m_pp.get (); }
65 FILE *get_file () const { return m_f_out; }
66
67private:
69
71 FILE *m_f_out;
74 std::unique_ptr<pretty_printer> m_pp;
75};
76
77/* The class log_scope is an RAII-style class intended to make
78 it easy to notify a logger about entering and exiting the body of a
79 given function. */
80
82{
83public:
84 log_scope (logger *logger, const char *name);
85 log_scope (logger *logger, const char *name, const char *fmt, ...)
87 ~log_scope ();
88
89 private:
91
93 const char *m_name;
94};
95
96/* The constructor for log_scope.
97
98 The normal case is that the logger is nullptr, in which case this should
99 be largely a no-op.
100
101 If we do have a logger, notify it that we're entering the given scope.
102 We also need to hold a reference on it, to avoid a use-after-free
103 when logging the cleanup of the owner of the logger. */
104
105inline
106log_scope::log_scope (logger *logger, const char *name) :
108 m_name (name)
109{
110 if (m_logger)
111 {
112 m_logger->incref ("log_scope ctor");
113 m_logger->enter_scope (m_name);
114 }
115}
116
117inline
118log_scope::log_scope (logger *logger, const char *name, const char *fmt, ...):
120 m_name (name)
121{
122 if (m_logger)
123 {
124 m_logger->incref ("log_scope ctor");
125 va_list ap;
126 va_start (ap, fmt);
127 m_logger->enter_scope (m_name, fmt, &ap);
128 va_end (ap);
129 }
130}
131
132
133/* The destructor for log_scope; essentially the opposite of
134 the constructor. */
135
136inline
138{
139 if (m_logger)
140 {
141 m_logger->exit_scope (m_name);
142 m_logger->decref ("log_scope dtor");
143 }
144}
145
147{
148public:
149 log_nesting_level (logger *logger, const char *fmt, ...)
150 ATTRIBUTE_GCC_DIAG(3, 4);
152
153private:
155};
156
157inline
160{
161 if (logger)
162 {
163 va_list ap;
164 va_start (ap, fmt);
165
167 logger->log_va_partial (fmt, &ap);
169
170 logger->inc_indent ();
171
172 va_end (ap);
173 }
174}
175
176
177/* The destructor for log_nesting_level; essentially the opposite of
178 the constructor. */
179
180inline
182{
183 if (m_logger)
184 m_logger->dec_indent ();
185}
186
187/* A log_user is something that potentially uses a logger (which could be
188 nullptr).
189
190 The log_user class keeps the reference-count of a logger up-to-date. */
191
193{
194 public:
197
198 logger * get_logger () const { return m_logger; }
200
201 void log (const char *fmt, ...) const
202 ATTRIBUTE_GCC_DIAG(2, 3);
203
204 void start_log_line () const;
205 void end_log_line () const;
206
207 void enter_scope (const char *scope_name);
208 void exit_scope (const char *scope_name);
209
211 {
213 return m_logger->get_printer ();
214 }
215
216 FILE *get_logger_file () const
217 {
218 if (m_logger == nullptr)
219 return nullptr;
220 return m_logger->get_file ();
221 }
222
223 private:
225
227};
228
229/* A shortcut for calling log from a log_user, handling the common
230 case where the underlying logger is nullptr via a no-op. */
231
232inline void
233log_user::log (const char *fmt, ...) const
234{
235 if (m_logger)
236 {
237 va_list ap;
238 va_start (ap, fmt);
239 m_logger->log_va (fmt, &ap);
240 va_end (ap);
241 }
242}
243
244/* A shortcut for starting a log line from a log_user,
245 handling the common case where the underlying logger is nullptr via
246 a no-op. */
247
248inline void
250{
251 if (m_logger)
252 m_logger->start_log_line ();
253}
254
255/* A shortcut for ending a log line from a log_user,
256 handling the common case where the underlying logger is nullptr via
257 a no-op. */
258
259inline void
261{
262 if (m_logger)
263 m_logger->end_log_line ();
264}
265
266/* A shortcut for recording entry into a scope from a log_user,
267 handling the common case where the underlying logger is nullptr via
268 a no-op. */
269
270inline void
271log_user::enter_scope (const char *scope_name)
272{
273 if (m_logger)
274 m_logger->enter_scope (scope_name);
275}
276
277/* A shortcut for recording exit from a scope from a log_user,
278 handling the common case where the underlying logger is nullptr via
279 a no-op. */
280
281inline void
282log_user::exit_scope (const char *scope_name)
283{
284 if (m_logger)
285 m_logger->exit_scope (scope_name);
286}
287
288/* If the given logger is non-NULL, log entry/exit of this scope to
289 it, identifying it using __PRETTY_FUNCTION__. */
290
291#define LOG_SCOPE(LOGGER) \
292 log_scope s (LOGGER, __PRETTY_FUNCTION__)
293
294/* If the given logger is non-NULL, log entry/exit of this scope to
295 it, identifying it using __func__. */
296
297#define LOG_FUNC(LOGGER) \
298 log_scope s (LOGGER, __func__)
299
300#define LOG_FUNC_1(LOGGER, FMT, A0) \
301 log_scope s (LOGGER, __func__, FMT, A0)
302
303#define LOG_FUNC_2(LOGGER, FMT, A0, A1) \
304 log_scope s (LOGGER, __func__, FMT, A0, A1)
305
306#define LOG_FUNC_3(LOGGER, FMT, A0, A1, A2) \
307 log_scope s (LOGGER, __func__, FMT, A0, A1, A2)
308
309#define LOG_FUNC_4(LOGGER, FMT, A0, A1, A2, A3) \
310 log_scope s (LOGGER, __func__, FMT, A0, A1, A2, A3)
311
312} // namespace ana
313
314#endif /* ANALYZER_LOGGING_H */
log_nesting_level(logger *logger, const char *fmt,...) ATTRIBUTE_GCC_DIAG(3
Definition analyzer-logging.h:158
logger * m_logger
Definition analyzer-logging.h:154
~log_nesting_level()
Definition analyzer-logging.h:181
const char * m_name
Definition analyzer-logging.h:93
logger * m_logger
Definition analyzer-logging.h:92
~log_scope()
Definition analyzer-logging.h:137
DISABLE_COPY_AND_ASSIGN(log_scope)
log_scope(logger *logger, const char *name)
Definition analyzer-logging.h:106
DISABLE_COPY_AND_ASSIGN(log_user)
logger * get_logger() const
Definition analyzer-logging.h:198
logger * m_logger
Definition analyzer-logging.h:226
void log(const char *fmt,...) const ATTRIBUTE_GCC_DIAG(2
Definition analyzer-logging.h:233
void enter_scope(const char *scope_name)
Definition analyzer-logging.h:271
void void start_log_line() const
Definition analyzer-logging.h:249
log_user(logger *logger)
void set_logger(logger *logger)
void exit_scope(const char *scope_name)
Definition analyzer-logging.h:282
FILE * get_logger_file() const
Definition analyzer-logging.h:216
void end_log_line() const
Definition analyzer-logging.h:260
pretty_printer * get_logger_pp() const
Definition analyzer-logging.h:210
Definition analyzer-logging.h:36
void dec_indent()
Definition analyzer-logging.h:62
int m_indent_level
Definition analyzer-logging.h:72
pretty_printer * get_printer() const
Definition analyzer-logging.h:64
void void log_va_partial(const char *fmt, va_list *ap) ATTRIBUTE_GCC_DIAG(2
void log_partial(const char *fmt,...) ATTRIBUTE_GCC_DIAG(2
void log(const char *fmt,...) ATTRIBUTE_GCC_DIAG(2
DISABLE_COPY_AND_ASSIGN(logger)
FILE * get_file() const
Definition analyzer-logging.h:65
void enter_scope(const char *scope_name)
bool m_log_refcount_changes
Definition analyzer-logging.h:73
FILE * m_f_out
Definition analyzer-logging.h:71
int m_refcount
Definition analyzer-logging.h:70
void void void start_log_line()
logger(FILE *f_out, int flags, int verbosity, const pretty_printer &reference_pp)
void incref(const char *reason)
void void log_va(const char *fmt, va_list *ap) ATTRIBUTE_GCC_DIAG(2
void log_canvas(const text_art::canvas &)
void inc_indent()
Definition analyzer-logging.h:61
void decref(const char *reason)
std::unique_ptr< pretty_printer > m_pp
Definition analyzer-logging.h:74
void void void end_log_line()
void void exit_scope(const char *scope_name)
Definition pretty-print.h:241
Definition canvas.h:38
#define ATTRIBUTE_GCC_DIAG(m, n)
Definition diagnostic-core.h:71
Definition access-diagram.h:30
Definition analyzer-logging.h:28
static void const char va_list ap
Definition read-md.cc:205
#define gcc_assert(EXPR)
Definition system.h:817