Line data Source code
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 :
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 : #define INCLUDE_VECTOR
22 : #include "config.h"
23 : #include "system.h"
24 : #include "coretypes.h"
25 : #include "toplev.h" /* for print_version */
26 : #include "pretty-print.h" /* for print_version */
27 : #include "diagnostic.h"
28 : #include "tree-diagnostic.h"
29 :
30 : #include "text-art/theme.h"
31 :
32 : #include "analyzer/analyzer-logging.h"
33 :
34 : #if ENABLE_ANALYZER
35 :
36 : #if __GNUC__ >= 10
37 : #pragma GCC diagnostic ignored "-Wformat-diag"
38 : #endif
39 :
40 : namespace ana {
41 :
42 : /* Implementation of class logger. */
43 :
44 : /* ctor for logger. */
45 :
46 10 : logger::logger (FILE *f_out,
47 : int, /* flags */
48 : int /* verbosity */,
49 : const pretty_printer &reference_pp) :
50 10 : m_refcount (0),
51 10 : m_f_out (f_out),
52 10 : m_indent_level (0),
53 10 : m_log_refcount_changes (false),
54 10 : m_pp (reference_pp.clone ())
55 : {
56 10 : pp_show_color (m_pp.get ()) = 0;
57 10 : pp_buffer (m_pp.get ())->m_stream = f_out;
58 :
59 : /* %qE in logs for SSA_NAMEs should show the ssa names, rather than
60 : trying to prettify things by showing the underlying var. */
61 10 : pp_format_decoder (m_pp.get ()) = default_tree_printer;
62 :
63 10 : m_text_art_theme = std::make_unique<text_art::unicode_theme> ();
64 :
65 : /* Begin the log by writing the GCC version. */
66 10 : print_version (f_out, "", false);
67 10 : }
68 :
69 : /* The destructor for logger, invoked via
70 : the decref method when the refcount hits zero.
71 : Note that we do not close the underlying FILE * (m_f_out). */
72 :
73 10 : logger::~logger ()
74 : {
75 : /* This should be the last message emitted. */
76 10 : log ("%s", __PRETTY_FUNCTION__);
77 10 : gcc_assert (m_refcount == 0);
78 10 : }
79 :
80 : /* Increment the reference count of the logger. */
81 :
82 : void
83 2800 : logger::incref (const char *reason)
84 : {
85 2800 : m_refcount++;
86 2800 : if (m_log_refcount_changes)
87 0 : log ("%s: reason: %s refcount now %i ",
88 : __PRETTY_FUNCTION__, reason, m_refcount);
89 2800 : }
90 :
91 : /* Decrement the reference count of the logger,
92 : deleting it if nothing is referring to it. */
93 :
94 : void
95 2800 : logger::decref (const char *reason)
96 : {
97 2800 : gcc_assert (m_refcount > 0);
98 2800 : --m_refcount;
99 2800 : if (m_log_refcount_changes)
100 0 : log ("%s: reason: %s refcount now %i",
101 : __PRETTY_FUNCTION__, reason, m_refcount);
102 2800 : if (m_refcount == 0)
103 10 : delete this;
104 2800 : }
105 :
106 : /* Write a formatted message to the log, by calling the log_va method. */
107 :
108 : void
109 5984 : logger::log (const char *fmt, ...)
110 : {
111 5984 : va_list ap;
112 5984 : va_start (ap, fmt);
113 5984 : log_va (fmt, &ap);
114 5984 : va_end (ap);
115 5984 : }
116 :
117 : /* Write an indented line to the log file.
118 :
119 : We explicitly flush after each line: if something crashes the process,
120 : we want the logfile/stream to contain the most up-to-date hint about the
121 : last thing that was happening, without it being hidden in an in-process
122 : buffer. */
123 :
124 : void
125 6239 : logger::log_va (const char *fmt, va_list *ap)
126 : {
127 6239 : start_log_line ();
128 6239 : log_va_partial (fmt, ap);
129 6239 : end_log_line ();
130 6239 : }
131 :
132 : void
133 8899 : logger::start_log_line ()
134 : {
135 51373 : for (int i = 0; i < m_indent_level; i++)
136 42474 : fputc (' ', m_f_out);
137 8899 : }
138 :
139 : void
140 477 : logger::log_partial (const char *fmt, ...)
141 : {
142 477 : va_list ap;
143 477 : va_start (ap, fmt);
144 477 : log_va_partial (fmt, &ap);
145 477 : va_end (ap);
146 477 : }
147 :
148 : void
149 7282 : logger::log_va_partial (const char *fmt, va_list *ap)
150 : {
151 7282 : text_info text (fmt, ap, 0);
152 7282 : pp_format (m_pp.get (), &text);
153 7282 : pp_output_formatted_text (m_pp.get ());
154 7282 : }
155 :
156 : void
157 8899 : logger::end_log_line ()
158 : {
159 8899 : pp_flush (m_pp.get ());
160 8899 : pp_clear_output_area (m_pp.get ());
161 8899 : fprintf (m_f_out, "\n");
162 8899 : fflush (m_f_out);
163 8899 : }
164 :
165 : /* Record the entry within a particular scope, indenting subsequent
166 : log lines accordingly. */
167 :
168 : void
169 1412 : logger::enter_scope (const char *scope_name)
170 : {
171 1412 : log ("entering: %s", scope_name);
172 1412 : inc_indent ();
173 1412 : }
174 :
175 : void
176 320 : logger::enter_scope (const char *scope_name, const char *fmt, va_list *ap)
177 : {
178 320 : start_log_line ();
179 320 : log_partial ("entering: %s: ", scope_name);
180 320 : log_va_partial (fmt, ap);
181 320 : end_log_line ();
182 :
183 320 : inc_indent ();
184 320 : }
185 :
186 :
187 : /* Record the exit from a particular scope, restoring the indent level to
188 : before the scope was entered. */
189 :
190 : void
191 1732 : logger::exit_scope (const char *scope_name)
192 : {
193 1732 : if (m_indent_level)
194 1732 : dec_indent ();
195 : else
196 0 : log ("(mismatching indentation)");
197 1732 : log ("exiting: %s", scope_name);
198 1732 : }
199 :
200 : /* Implementation of class log_user. */
201 :
202 : /* The constructor for log_user. */
203 :
204 3621646 : log_user::log_user (logger *logger) : m_logger (logger)
205 : {
206 3621646 : if (m_logger)
207 1058 : m_logger->incref("log_user ctor");
208 3621646 : }
209 :
210 : /* The destructor for log_user. */
211 :
212 3621646 : log_user::~log_user ()
213 : {
214 3621646 : if (m_logger)
215 1068 : m_logger->decref("log_user dtor");
216 3621646 : }
217 :
218 : /* Set the logger for a log_user, managing the reference counts
219 : of the old and new logger (either of which might be NULL). */
220 :
221 : void
222 10 : log_user::set_logger (logger *logger)
223 : {
224 10 : if (logger)
225 10 : logger->incref ("log_user::set_logger");
226 10 : if (m_logger)
227 0 : m_logger->decref ("log_user::set_logger");
228 10 : m_logger = logger;
229 10 : }
230 :
231 : } // namespace ana
232 :
233 : #endif /* #if ENABLE_ANALYZER */
|