]> git.ipfire.org Git - thirdparty/gcc.git/blob - gcc/analyzer/analyzer-logging.h
9d5364bfa8fa58edf3c8d4d22709bd329592b924
[thirdparty/gcc.git] / gcc / analyzer / analyzer-logging.h
1 /* Hierarchical log messages for the analyzer.
2 Copyright (C) 2014-2020 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
8 under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3, or (at your option)
10 any later version.
11
12 GCC is distributed in the hope that it will be useful, but
13 WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 General Public License 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 /* Adapted from jit-logging.h. */
22
23 #ifndef ANALYZER_LOGGING_H
24 #define ANALYZER_LOGGING_H
25
26 /* A logger encapsulates a logging stream: a way to send
27 lines of pertinent information to a FILE *. */
28
29 class logger
30 {
31 public:
32 logger (FILE *f_out, int flags, int verbosity, const pretty_printer &reference_pp);
33 ~logger ();
34
35 void incref (const char *reason);
36 void decref (const char *reason);
37
38 void log (const char *fmt, ...)
39 ATTRIBUTE_GCC_DIAG(2, 3);
40 void log_va (const char *fmt, va_list *ap)
41 ATTRIBUTE_GCC_DIAG(2, 0);
42 void start_log_line ();
43 void log_partial (const char *fmt, ...)
44 ATTRIBUTE_GCC_DIAG(2, 3);
45 void log_va_partial (const char *fmt, va_list *ap)
46 ATTRIBUTE_GCC_DIAG(2, 0);
47 void end_log_line ();
48
49 void enter_scope (const char *scope_name);
50 void enter_scope (const char *scope_name, const char *fmt, va_list *ap)
51 ATTRIBUTE_GCC_DIAG(3, 0);
52 void exit_scope (const char *scope_name);
53
54 pretty_printer *get_printer () const { return m_pp; }
55 FILE *get_file () const { return m_f_out; }
56
57 private:
58 DISABLE_COPY_AND_ASSIGN (logger);
59
60 int m_refcount;
61 FILE *m_f_out;
62 int m_indent_level;
63 bool m_log_refcount_changes;
64 pretty_printer *m_pp;
65 };
66
67 /* The class log_scope is an RAII-style class intended to make
68 it easy to notify a logger about entering and exiting the body of a
69 given function. */
70
71 class log_scope
72 {
73 public:
74 log_scope (logger *logger, const char *name);
75 log_scope (logger *logger, const char *name, const char *fmt, ...)
76 ATTRIBUTE_GCC_DIAG(4, 5);
77 ~log_scope ();
78
79 private:
80 DISABLE_COPY_AND_ASSIGN (log_scope);
81
82 logger *m_logger;
83 const char *m_name;
84 };
85
86 /* The constructor for log_scope.
87
88 The normal case is that the logger is NULL, in which case this should
89 be largely a no-op.
90
91 If we do have a logger, notify it that we're entering the given scope.
92 We also need to hold a reference on it, to avoid a use-after-free
93 when logging the cleanup of the owner of the logger. */
94
95 inline
96 log_scope::log_scope (logger *logger, const char *name) :
97 m_logger (logger),
98 m_name (name)
99 {
100 if (m_logger)
101 {
102 m_logger->incref ("log_scope ctor");
103 m_logger->enter_scope (m_name);
104 }
105 }
106
107 inline
108 log_scope::log_scope (logger *logger, const char *name, const char *fmt, ...):
109 m_logger (logger),
110 m_name (name)
111 {
112 if (m_logger)
113 {
114 m_logger->incref ("log_scope ctor");
115 va_list ap;
116 va_start (ap, fmt);
117 m_logger->enter_scope (m_name, fmt, &ap);
118 va_end (ap);
119 }
120 }
121
122
123 /* The destructor for log_scope; essentially the opposite of
124 the constructor. */
125
126 inline
127 log_scope::~log_scope ()
128 {
129 if (m_logger)
130 {
131 m_logger->exit_scope (m_name);
132 m_logger->decref ("log_scope dtor");
133 }
134 }
135
136 /* A log_user is something that potentially uses a logger (which could be NULL).
137
138 The log_user class keeps the reference-count of a logger up-to-date. */
139
140 class log_user
141 {
142 public:
143 log_user (logger *logger);
144 ~log_user ();
145
146 logger * get_logger () const { return m_logger; }
147 void set_logger (logger * logger);
148
149 void log (const char *fmt, ...) const
150 ATTRIBUTE_GCC_DIAG(2, 3);
151
152 void start_log_line () const;
153 void end_log_line () const;
154
155 void enter_scope (const char *scope_name);
156 void exit_scope (const char *scope_name);
157
158 pretty_printer *get_logger_pp () const
159 {
160 gcc_assert (m_logger);
161 return m_logger->get_printer ();
162 }
163
164 FILE *get_logger_file () const
165 {
166 if (m_logger == NULL)
167 return NULL;
168 return m_logger->get_file ();
169 }
170
171 private:
172 DISABLE_COPY_AND_ASSIGN (log_user);
173
174 logger *m_logger;
175 };
176
177 /* A shortcut for calling log from a log_user, handling the common
178 case where the underlying logger is NULL via a no-op. */
179
180 inline void
181 log_user::log (const char *fmt, ...) const
182 {
183 if (m_logger)
184 {
185 va_list ap;
186 va_start (ap, fmt);
187 m_logger->log_va (fmt, &ap);
188 va_end (ap);
189 }
190 }
191
192 /* A shortcut for starting a log line from a log_user,
193 handling the common case where the underlying logger is NULL via
194 a no-op. */
195
196 inline void
197 log_user::start_log_line () const
198 {
199 if (m_logger)
200 m_logger->start_log_line ();
201 }
202
203 /* A shortcut for ending a log line from a log_user,
204 handling the common case where the underlying logger is NULL via
205 a no-op. */
206
207 inline void
208 log_user::end_log_line () const
209 {
210 if (m_logger)
211 m_logger->end_log_line ();
212 }
213
214 /* A shortcut for recording entry into a scope from a log_user,
215 handling the common case where the underlying logger is NULL via
216 a no-op. */
217
218 inline void
219 log_user::enter_scope (const char *scope_name)
220 {
221 if (m_logger)
222 m_logger->enter_scope (scope_name);
223 }
224
225 /* A shortcut for recording exit from a scope from a log_user,
226 handling the common case where the underlying logger is NULL via
227 a no-op. */
228
229 inline void
230 log_user::exit_scope (const char *scope_name)
231 {
232 if (m_logger)
233 m_logger->exit_scope (scope_name);
234 }
235
236 /* If the given logger is non-NULL, log entry/exit of this scope to
237 it, identifying it using __PRETTY_FUNCTION__. */
238
239 #define LOG_SCOPE(LOGGER) \
240 log_scope s (LOGGER, __PRETTY_FUNCTION__)
241
242 /* If the given logger is non-NULL, log entry/exit of this scope to
243 it, identifying it using __func__. */
244
245 #define LOG_FUNC(LOGGER) \
246 log_scope s (LOGGER, __func__)
247
248 #define LOG_FUNC_1(LOGGER, FMT, A0) \
249 log_scope s (LOGGER, __func__, FMT, A0)
250
251 #define LOG_FUNC_2(LOGGER, FMT, A0, A1) \
252 log_scope s (LOGGER, __func__, FMT, A0, A1)
253
254 #define LOG_FUNC_3(LOGGER, FMT, A0, A1, A2) \
255 log_scope s (LOGGER, __func__, FMT, A0, A1, A2)
256
257 #define LOG_FUNC_4(LOGGER, FMT, A0, A1, A2, A3) \
258 log_scope s (LOGGER, __func__, FMT, A0, A1, A2, A3)
259
260 #endif /* ANALYZER_LOGGING_H */