d: Add test for PR d/108167 to the testsuite [PR108167]
[official-gcc.git] / gcc / analyzer / analyzer-logging.cc
blobb78481c4098b0a9ee52d44f967e491a3491377b2
1 /* Hierarchical log messages for the analyzer.
2 Copyright (C) 2014-2023 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 #include "config.h"
22 #include "system.h"
23 #include "coretypes.h"
24 #include "toplev.h" /* for print_version */
25 #include "pretty-print.h" /* for print_version */
26 #include "diagnostic.h"
27 #include "tree-diagnostic.h"
29 #include "analyzer/analyzer-logging.h"
31 #if ENABLE_ANALYZER
33 #if __GNUC__ >= 10
34 #pragma GCC diagnostic ignored "-Wformat-diag"
35 #endif
37 namespace ana {
39 /* Implementation of class logger. */
41 /* ctor for logger. */
43 logger::logger (FILE *f_out,
44 int, /* flags */
45 int /* verbosity */,
46 const pretty_printer &reference_pp) :
47 m_refcount (0),
48 m_f_out (f_out),
49 m_indent_level (0),
50 m_log_refcount_changes (false),
51 m_pp (reference_pp.clone ())
53 pp_show_color (m_pp) = 0;
54 pp_buffer (m_pp)->stream = f_out;
56 /* %qE in logs for SSA_NAMEs should show the ssa names, rather than
57 trying to prettify things by showing the underlying var. */
58 pp_format_decoder (m_pp) = default_tree_printer;
60 /* Begin the log by writing the GCC version. */
61 print_version (f_out, "", false);
64 /* The destructor for logger, invoked via
65 the decref method when the refcount hits zero.
66 Note that we do not close the underlying FILE * (m_f_out). */
68 logger::~logger ()
70 /* This should be the last message emitted. */
71 log ("%s", __PRETTY_FUNCTION__);
72 gcc_assert (m_refcount == 0);
73 delete m_pp;
76 /* Increment the reference count of the logger. */
78 void
79 logger::incref (const char *reason)
81 m_refcount++;
82 if (m_log_refcount_changes)
83 log ("%s: reason: %s refcount now %i ",
84 __PRETTY_FUNCTION__, reason, m_refcount);
87 /* Decrement the reference count of the logger,
88 deleting it if nothing is referring to it. */
90 void
91 logger::decref (const char *reason)
93 gcc_assert (m_refcount > 0);
94 --m_refcount;
95 if (m_log_refcount_changes)
96 log ("%s: reason: %s refcount now %i",
97 __PRETTY_FUNCTION__, reason, m_refcount);
98 if (m_refcount == 0)
99 delete this;
102 /* Write a formatted message to the log, by calling the log_va method. */
104 void
105 logger::log (const char *fmt, ...)
107 va_list ap;
108 va_start (ap, fmt);
109 log_va (fmt, &ap);
110 va_end (ap);
113 /* Write an indented line to the log file.
115 We explicitly flush after each line: if something crashes the process,
116 we want the logfile/stream to contain the most up-to-date hint about the
117 last thing that was happening, without it being hidden in an in-process
118 buffer. */
120 void
121 logger::log_va (const char *fmt, va_list *ap)
123 start_log_line ();
124 log_va_partial (fmt, ap);
125 end_log_line ();
128 void
129 logger::start_log_line ()
131 for (int i = 0; i < m_indent_level; i++)
132 fputc (' ', m_f_out);
135 void
136 logger::log_partial (const char *fmt, ...)
138 va_list ap;
139 va_start (ap, fmt);
140 log_va_partial (fmt, &ap);
141 va_end (ap);
144 void
145 logger::log_va_partial (const char *fmt, va_list *ap)
147 text_info text;
148 text.format_spec = fmt;
149 text.args_ptr = ap;
150 text.err_no = 0;
151 pp_format (m_pp, &text);
152 pp_output_formatted_text (m_pp);
155 void
156 logger::end_log_line ()
158 pp_flush (m_pp);
159 pp_clear_output_area (m_pp);
160 fprintf (m_f_out, "\n");
161 fflush (m_f_out);
164 /* Record the entry within a particular scope, indenting subsequent
165 log lines accordingly. */
167 void
168 logger::enter_scope (const char *scope_name)
170 log ("entering: %s", scope_name);
171 inc_indent ();
174 void
175 logger::enter_scope (const char *scope_name, const char *fmt, va_list *ap)
177 start_log_line ();
178 log_partial ("entering: %s: ", scope_name);
179 log_va_partial (fmt, ap);
180 end_log_line ();
182 inc_indent ();
186 /* Record the exit from a particular scope, restoring the indent level to
187 before the scope was entered. */
189 void
190 logger::exit_scope (const char *scope_name)
192 if (m_indent_level)
193 dec_indent ();
194 else
195 log ("(mismatching indentation)");
196 log ("exiting: %s", scope_name);
199 /* Implementation of class log_user. */
201 /* The constructor for log_user. */
203 log_user::log_user (logger *logger) : m_logger (logger)
205 if (m_logger)
206 m_logger->incref("log_user ctor");
209 /* The destructor for log_user. */
211 log_user::~log_user ()
213 if (m_logger)
214 m_logger->decref("log_user dtor");
217 /* Set the logger for a log_user, managing the reference counts
218 of the old and new logger (either of which might be NULL). */
220 void
221 log_user::set_logger (logger *logger)
223 if (logger)
224 logger->incref ("log_user::set_logger");
225 if (m_logger)
226 m_logger->decref ("log_user::set_logger");
227 m_logger = logger;
230 } // namespace ana
232 #endif /* #if ENABLE_ANALYZER */