Daily bump.
[official-gcc.git] / gcc / analyzer / analyzer-logging.h
blob88b5f0a4a3faa22958931e4754e0288aad0c80d6
1 /* Hierarchical log messages for the analyzer.
2 Copyright (C) 2014-2021 Free Software Foundation, Inc.
3 Contributed by David Malcolm <dmalcolm@redhat.com>.
5 This file is part of GCC.
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.
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.
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/>. */
21 /* Adapted from jit-logging.h. */
23 #ifndef ANALYZER_LOGGING_H
24 #define ANALYZER_LOGGING_H
26 namespace ana {
28 /* A logger encapsulates a logging stream: a way to send
29 lines of pertinent information to a FILE *. */
31 class logger
33 public:
34 logger (FILE *f_out, int flags, int verbosity, const pretty_printer &reference_pp);
35 ~logger ();
37 void incref (const char *reason);
38 void decref (const char *reason);
40 void log (const char *fmt, ...)
41 ATTRIBUTE_GCC_DIAG(2, 3);
42 void log_va (const char *fmt, va_list *ap)
43 ATTRIBUTE_GCC_DIAG(2, 0);
44 void start_log_line ();
45 void log_partial (const char *fmt, ...)
46 ATTRIBUTE_GCC_DIAG(2, 3);
47 void log_va_partial (const char *fmt, va_list *ap)
48 ATTRIBUTE_GCC_DIAG(2, 0);
49 void end_log_line ();
51 void enter_scope (const char *scope_name);
52 void enter_scope (const char *scope_name, const char *fmt, va_list *ap)
53 ATTRIBUTE_GCC_DIAG(3, 0);
54 void exit_scope (const char *scope_name);
55 void inc_indent () { m_indent_level++; }
56 void dec_indent () { m_indent_level--; }
58 pretty_printer *get_printer () const { return m_pp; }
59 FILE *get_file () const { return m_f_out; }
61 private:
62 DISABLE_COPY_AND_ASSIGN (logger);
64 int m_refcount;
65 FILE *m_f_out;
66 int m_indent_level;
67 bool m_log_refcount_changes;
68 pretty_printer *m_pp;
71 /* The class log_scope is an RAII-style class intended to make
72 it easy to notify a logger about entering and exiting the body of a
73 given function. */
75 class log_scope
77 public:
78 log_scope (logger *logger, const char *name);
79 log_scope (logger *logger, const char *name, const char *fmt, ...)
80 ATTRIBUTE_GCC_DIAG(4, 5);
81 ~log_scope ();
83 private:
84 DISABLE_COPY_AND_ASSIGN (log_scope);
86 logger *m_logger;
87 const char *m_name;
90 /* The constructor for log_scope.
92 The normal case is that the logger is NULL, in which case this should
93 be largely a no-op.
95 If we do have a logger, notify it that we're entering the given scope.
96 We also need to hold a reference on it, to avoid a use-after-free
97 when logging the cleanup of the owner of the logger. */
99 inline
100 log_scope::log_scope (logger *logger, const char *name) :
101 m_logger (logger),
102 m_name (name)
104 if (m_logger)
106 m_logger->incref ("log_scope ctor");
107 m_logger->enter_scope (m_name);
111 inline
112 log_scope::log_scope (logger *logger, const char *name, const char *fmt, ...):
113 m_logger (logger),
114 m_name (name)
116 if (m_logger)
118 m_logger->incref ("log_scope ctor");
119 va_list ap;
120 va_start (ap, fmt);
121 m_logger->enter_scope (m_name, fmt, &ap);
122 va_end (ap);
127 /* The destructor for log_scope; essentially the opposite of
128 the constructor. */
130 inline
131 log_scope::~log_scope ()
133 if (m_logger)
135 m_logger->exit_scope (m_name);
136 m_logger->decref ("log_scope dtor");
140 /* A log_user is something that potentially uses a logger (which could be NULL).
142 The log_user class keeps the reference-count of a logger up-to-date. */
144 class log_user
146 public:
147 log_user (logger *logger);
148 ~log_user ();
150 logger * get_logger () const { return m_logger; }
151 void set_logger (logger * logger);
153 void log (const char *fmt, ...) const
154 ATTRIBUTE_GCC_DIAG(2, 3);
156 void start_log_line () const;
157 void end_log_line () const;
159 void enter_scope (const char *scope_name);
160 void exit_scope (const char *scope_name);
162 pretty_printer *get_logger_pp () const
164 gcc_assert (m_logger);
165 return m_logger->get_printer ();
168 FILE *get_logger_file () const
170 if (m_logger == NULL)
171 return NULL;
172 return m_logger->get_file ();
175 private:
176 DISABLE_COPY_AND_ASSIGN (log_user);
178 logger *m_logger;
181 /* A shortcut for calling log from a log_user, handling the common
182 case where the underlying logger is NULL via a no-op. */
184 inline void
185 log_user::log (const char *fmt, ...) const
187 if (m_logger)
189 va_list ap;
190 va_start (ap, fmt);
191 m_logger->log_va (fmt, &ap);
192 va_end (ap);
196 /* A shortcut for starting a log line from a log_user,
197 handling the common case where the underlying logger is NULL via
198 a no-op. */
200 inline void
201 log_user::start_log_line () const
203 if (m_logger)
204 m_logger->start_log_line ();
207 /* A shortcut for ending a log line from a log_user,
208 handling the common case where the underlying logger is NULL via
209 a no-op. */
211 inline void
212 log_user::end_log_line () const
214 if (m_logger)
215 m_logger->end_log_line ();
218 /* A shortcut for recording entry into a scope from a log_user,
219 handling the common case where the underlying logger is NULL via
220 a no-op. */
222 inline void
223 log_user::enter_scope (const char *scope_name)
225 if (m_logger)
226 m_logger->enter_scope (scope_name);
229 /* A shortcut for recording exit from a scope from a log_user,
230 handling the common case where the underlying logger is NULL via
231 a no-op. */
233 inline void
234 log_user::exit_scope (const char *scope_name)
236 if (m_logger)
237 m_logger->exit_scope (scope_name);
240 /* If the given logger is non-NULL, log entry/exit of this scope to
241 it, identifying it using __PRETTY_FUNCTION__. */
243 #define LOG_SCOPE(LOGGER) \
244 log_scope s (LOGGER, __PRETTY_FUNCTION__)
246 /* If the given logger is non-NULL, log entry/exit of this scope to
247 it, identifying it using __func__. */
249 #define LOG_FUNC(LOGGER) \
250 log_scope s (LOGGER, __func__)
252 #define LOG_FUNC_1(LOGGER, FMT, A0) \
253 log_scope s (LOGGER, __func__, FMT, A0)
255 #define LOG_FUNC_2(LOGGER, FMT, A0, A1) \
256 log_scope s (LOGGER, __func__, FMT, A0, A1)
258 #define LOG_FUNC_3(LOGGER, FMT, A0, A1, A2) \
259 log_scope s (LOGGER, __func__, FMT, A0, A1, A2)
261 #define LOG_FUNC_4(LOGGER, FMT, A0, A1, A2, A3) \
262 log_scope s (LOGGER, __func__, FMT, A0, A1, A2, A3)
264 } // namespace ana
266 #endif /* ANALYZER_LOGGING_H */