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-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
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 ana {
29
30/* A logger encapsulates a logging stream: a way to send
31 lines of pertinent information to a FILE *. */
32
33class logger
34{
35 public:
38
39 void incref (const char *reason);
40 void decref (const char *reason);
41
42 void log (const char *fmt, ...)
44 void log_va (const char *fmt, va_list *ap)
47 void log_partial (const char *fmt, ...)
51 void end_log_line ();
52
59
60 pretty_printer *get_printer () const { return m_pp; }
61 FILE *get_file () const { return m_f_out; }
62
63private:
65
71};
72
73/* The class log_scope is an RAII-style class intended to make
74 it easy to notify a logger about entering and exiting the body of a
75 given function. */
76
78{
79public:
80 log_scope (logger *logger, const char *name);
81 log_scope (logger *logger, const char *name, const char *fmt, ...)
83 ~log_scope ();
84
85 private:
87
89 const char *m_name;
90};
91
92/* The constructor for log_scope.
93
94 The normal case is that the logger is NULL, in which case this should
95 be largely a no-op.
96
97 If we do have a logger, notify it that we're entering the given scope.
98 We also need to hold a reference on it, to avoid a use-after-free
99 when logging the cleanup of the owner of the logger. */
100
101inline
102log_scope::log_scope (logger *logger, const char *name) :
103 m_logger (logger),
104 m_name (name)
105{
106 if (m_logger)
107 {
108 m_logger->incref ("log_scope ctor");
110 }
111}
112
113inline
114log_scope::log_scope (logger *logger, const char *name, const char *fmt, ...):
115 m_logger (logger),
116 m_name (name)
117{
118 if (m_logger)
119 {
120 m_logger->incref ("log_scope ctor");
121 va_list ap;
122 va_start (ap, fmt);
124 va_end (ap);
125 }
126}
127
128
129/* The destructor for log_scope; essentially the opposite of
130 the constructor. */
131
132inline
134{
135 if (m_logger)
136 {
138 m_logger->decref ("log_scope dtor");
139 }
140}
141
142/* A log_user is something that potentially uses a logger (which could be NULL).
143
144 The log_user class keeps the reference-count of a logger up-to-date. */
145
147{
148 public:
151
152 logger * get_logger () const { return m_logger; }
154
155 void log (const char *fmt, ...) const
156 ATTRIBUTE_GCC_DIAG(2, 3);
157
158 void start_log_line () const;
159 void end_log_line () const;
160
161 void enter_scope (const char *scope_name);
162 void exit_scope (const char *scope_name);
163
165 {
167 return m_logger->get_printer ();
168 }
169
171 {
172 if (m_logger == NULL)
173 return NULL;
174 return m_logger->get_file ();
175 }
176
177 private:
179
181};
182
183/* A shortcut for calling log from a log_user, handling the common
184 case where the underlying logger is NULL via a no-op. */
185
186inline void
187log_user::log (const char *fmt, ...) const
188{
189 if (m_logger)
190 {
191 va_list ap;
192 va_start (ap, fmt);
193 m_logger->log_va (fmt, &ap);
194 va_end (ap);
195 }
196}
197
198/* A shortcut for starting a log line from a log_user,
199 handling the common case where the underlying logger is NULL via
200 a no-op. */
201
202inline void
204{
205 if (m_logger)
207}
208
209/* A shortcut for ending a log line from a log_user,
210 handling the common case where the underlying logger is NULL via
211 a no-op. */
212
213inline void
215{
216 if (m_logger)
218}
219
220/* A shortcut for recording entry into a scope from a log_user,
221 handling the common case where the underlying logger is NULL via
222 a no-op. */
223
224inline void
230
231/* A shortcut for recording exit from a scope from a log_user,
232 handling the common case where the underlying logger is NULL via
233 a no-op. */
234
235inline void
237{
238 if (m_logger)
240}
241
242/* If the given logger is non-NULL, log entry/exit of this scope to
243 it, identifying it using __PRETTY_FUNCTION__. */
244
245#define LOG_SCOPE(LOGGER) \
246 log_scope s (LOGGER, __PRETTY_FUNCTION__)
247
248/* If the given logger is non-NULL, log entry/exit of this scope to
249 it, identifying it using __func__. */
250
251#define LOG_FUNC(LOGGER) \
252 log_scope s (LOGGER, __func__)
253
254#define LOG_FUNC_1(LOGGER, FMT, A0) \
255 log_scope s (LOGGER, __func__, FMT, A0)
256
257#define LOG_FUNC_2(LOGGER, FMT, A0, A1) \
258 log_scope s (LOGGER, __func__, FMT, A0, A1)
259
260#define LOG_FUNC_3(LOGGER, FMT, A0, A1, A2) \
261 log_scope s (LOGGER, __func__, FMT, A0, A1, A2)
262
263#define LOG_FUNC_4(LOGGER, FMT, A0, A1, A2, A3) \
264 log_scope s (LOGGER, __func__, FMT, A0, A1, A2, A3)
265
266} // namespace ana
267
268#endif /* ANALYZER_LOGGING_H */
Definition analyzer-logging.h:78
const char * m_name
Definition analyzer-logging.h:89
logger * m_logger
Definition analyzer-logging.h:88
~log_scope()
Definition analyzer-logging.h:133
DISABLE_COPY_AND_ASSIGN(log_scope)
log_scope(logger *logger, const char *name)
Definition analyzer-logging.h:102
Definition analyzer-logging.h:147
DISABLE_COPY_AND_ASSIGN(log_user)
logger * get_logger() const
Definition analyzer-logging.h:152
logger * m_logger
Definition analyzer-logging.h:180
void log(const char *fmt,...) const ATTRIBUTE_GCC_DIAG(2
Definition analyzer-logging.h:187
void enter_scope(const char *scope_name)
Definition analyzer-logging.h:225
void void start_log_line() const
Definition analyzer-logging.h:203
log_user(logger *logger)
void set_logger(logger *logger)
void exit_scope(const char *scope_name)
Definition analyzer-logging.h:236
FILE * get_logger_file() const
Definition analyzer-logging.h:170
void end_log_line() const
Definition analyzer-logging.h:214
pretty_printer * get_logger_pp() const
Definition analyzer-logging.h:164
Definition analyzer-logging.h:34
void dec_indent()
Definition analyzer-logging.h:58
int m_indent_level
Definition analyzer-logging.h:68
pretty_printer * get_printer() const
Definition analyzer-logging.h:60
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:61
void enter_scope(const char *scope_name)
bool m_log_refcount_changes
Definition analyzer-logging.h:69
FILE * m_f_out
Definition analyzer-logging.h:67
int m_refcount
Definition analyzer-logging.h:66
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
pretty_printer * m_pp
Definition analyzer-logging.h:70
void inc_indent()
Definition analyzer-logging.h:57
void decref(const char *reason)
void void void end_log_line()
void void exit_scope(const char *scope_name)
Definition pretty-print.h:244
#define ATTRIBUTE_GCC_DIAG(m, n)
Definition diagnostic-core.h:67
T * ggc_alloc(ALONE_CXX_MEM_STAT_INFO)
Definition ggc.h:184
Definition access-diagram.h:30
static void const char va_list ap
Definition read-md.cc:205
#define NULL
Definition system.h:50
#define gcc_assert(EXPR)
Definition system.h:821