Branch data Line data Source code
1 : : /* Language-independent diagnostic subroutines for the GNU Compiler
2 : : Collection that are only for use in the compilers proper and not
3 : : the driver or other programs.
4 : : Copyright (C) 1999-2025 Free Software Foundation, Inc.
5 : :
6 : : This file is part of GCC.
7 : :
8 : : GCC is free software; you can redistribute it and/or modify it under
9 : : the terms of the GNU General Public License as published by the Free
10 : : Software Foundation; either version 3, or (at your option) any later
11 : : version.
12 : :
13 : : GCC is distributed in the hope that it will be useful, but WITHOUT ANY
14 : : WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 : : FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
16 : : for more details.
17 : :
18 : : You should have received a copy of the GNU General Public License
19 : : along with GCC; see the file COPYING3. If not see
20 : : <http://www.gnu.org/licenses/>. */
21 : :
22 : : #define INCLUDE_VECTOR
23 : : #include "config.h"
24 : : #include "system.h"
25 : : #include "coretypes.h"
26 : : #include "tree.h"
27 : : #include "diagnostic.h"
28 : : #include "tree-pretty-print.h"
29 : : #include "gimple-pretty-print.h"
30 : : #include "tree-diagnostic.h"
31 : : #include "diagnostics/client-data-hooks.h"
32 : : #include "langhooks.h"
33 : : #include "intl.h"
34 : : #include "diagnostics/text-sink.h"
35 : :
36 : : /* Prints out, if necessary, the name of the current function
37 : : that caused an error. */
38 : : void
39 : 105652 : diagnostic_report_current_function (diagnostics::text_sink &text_output,
40 : : const diagnostics::diagnostic_info *diag)
41 : : {
42 : 105652 : location_t loc = diagnostic_location (diag);
43 : 105652 : text_output.report_current_module (loc);
44 : 105652 : lang_hooks.print_error_function (text_output, LOCATION_FILE (loc), diag);
45 : 105652 : }
46 : :
47 : : static void
48 : 105652 : default_tree_diagnostic_text_starter (diagnostics::text_sink &text_output,
49 : : const diagnostics::diagnostic_info *diag)
50 : : {
51 : 105652 : pretty_printer *const pp = text_output.get_printer ();
52 : 105652 : diagnostic_report_current_function (text_output, diag);
53 : 105652 : pp_set_prefix (pp, text_output.build_prefix (*diag));
54 : 105652 : }
55 : :
56 : : /* Default tree printer. Handles declarations only. */
57 : : bool
58 : 39772 : default_tree_printer (pretty_printer *pp, text_info *text, const char *spec,
59 : : int precision, bool wide, bool set_locus, bool hash,
60 : : bool *, pp_token_list &)
61 : : {
62 : 39772 : tree t;
63 : :
64 : : /* FUTURE: %+x should set the locus. */
65 : 39772 : if (precision != 0 || wide || hash)
66 : : return false;
67 : :
68 : 39772 : switch (*spec)
69 : : {
70 : 34594 : case 'E':
71 : 34594 : t = va_arg (*text->m_args_ptr, tree);
72 : 34594 : if (TREE_CODE (t) == IDENTIFIER_NODE)
73 : : {
74 : 168 : pp_identifier (pp, IDENTIFIER_POINTER (t));
75 : 168 : return true;
76 : : }
77 : : break;
78 : :
79 : 4708 : case 'D':
80 : 4708 : t = va_arg (*text->m_args_ptr, tree);
81 : 4708 : if (VAR_P (t) && DECL_HAS_DEBUG_EXPR_P (t))
82 : 22 : t = DECL_DEBUG_EXPR (t);
83 : : break;
84 : :
85 : 470 : case 'F':
86 : 470 : case 'T':
87 : 470 : t = va_arg (*text->m_args_ptr, tree);
88 : 470 : break;
89 : :
90 : : default:
91 : : return false;
92 : : }
93 : :
94 : 39604 : if (set_locus)
95 : 19 : text->set_location (0, DECL_SOURCE_LOCATION (t), SHOW_RANGE_WITH_CARET);
96 : :
97 : 39604 : if (DECL_P (t))
98 : : {
99 : 20866 : const char *n = DECL_NAME (t)
100 : 20866 : ? identifier_to_locale (lang_hooks.decl_printable_name (t, 2))
101 : 262 : : _("<anonymous>");
102 : 20866 : pp_string (pp, n);
103 : : }
104 : : else
105 : 18738 : dump_generic_node (pp, t, 0, TDF_SLIM, 0);
106 : :
107 : : return true;
108 : : }
109 : :
110 : : /* Set the locations of call sites along the inlining stack corresponding
111 : : to the DIAGNOSTIC location. */
112 : :
113 : : static void
114 : 93643522 : set_inlining_locations (const diagnostics::context &,
115 : : diagnostics::diagnostic_info *diagnostic)
116 : : {
117 : 93643522 : location_t loc = diagnostic_location (diagnostic);
118 : 93643522 : tree block = LOCATION_BLOCK (loc);
119 : :
120 : : /* Count the number of locations in system headers. When all are,
121 : : warnings are suppressed by -Wno-system-headers. Otherwise, they
122 : : involve some user code, possibly inlined into a function in a system
123 : : header, and are not treated as coming from system headers. */
124 : : unsigned nsyslocs = 0;
125 : :
126 : : /* Use a reference to the vector of locations for convenience. */
127 : : auto &ilocs = diagnostic->m_iinfo.m_ilocs;
128 : :
129 : 867011 : while (block && TREE_CODE (block) == BLOCK
130 : 94929556 : && BLOCK_ABSTRACT_ORIGIN (block))
131 : : {
132 : 433623 : tree ao = BLOCK_ABSTRACT_ORIGIN (block);
133 : 433623 : if (TREE_CODE (ao) == FUNCTION_DECL)
134 : : {
135 : 154343 : if (!diagnostic->m_iinfo.m_ao)
136 : 89845 : diagnostic->m_iinfo.m_ao = block;
137 : :
138 : 154343 : location_t bsloc = BLOCK_SOURCE_LOCATION (block);
139 : 154343 : ilocs.safe_push (bsloc);
140 : 154343 : if (in_system_header_at (bsloc))
141 : 142 : ++nsyslocs;
142 : : }
143 : 279280 : else if (TREE_CODE (ao) != BLOCK)
144 : : break;
145 : :
146 : 433623 : block = BLOCK_SUPERCONTEXT (block);
147 : : }
148 : :
149 : 93643522 : if (ilocs.length ())
150 : : {
151 : : /* When there is an inlining context use the macro expansion
152 : : location for the original location and bump up NSYSLOCS if
153 : : it's in a system header since it's not counted above. */
154 : 89845 : location_t sysloc = expansion_point_location_if_in_system_header (loc);
155 : 89845 : if (sysloc != loc)
156 : : {
157 : 0 : loc = sysloc;
158 : 0 : ++nsyslocs;
159 : : }
160 : : }
161 : : else
162 : : {
163 : : /* When there's no inlining context use the original location
164 : : and set NSYSLOCS accordingly. */
165 : 93553677 : nsyslocs = in_system_header_at (loc) != 0;
166 : : }
167 : :
168 : 93643522 : ilocs.safe_push (loc);
169 : :
170 : : /* Set if all locations are in a system header. */
171 : 93643522 : diagnostic->m_iinfo.m_allsyslocs = nsyslocs == ilocs.length ();
172 : :
173 : 93643522 : if (tree *ao = pp_ti_abstract_origin (&diagnostic->m_message))
174 : 93435128 : *ao = (tree)diagnostic->m_iinfo.m_ao;
175 : 93643522 : }
176 : :
177 : : /* Sets CONTEXT to use language independent diagnostics. */
178 : : void
179 : 344256 : tree_diagnostics_defaults (diagnostics::context *context)
180 : : {
181 : 344256 : diagnostics::text_starter (context) = default_tree_diagnostic_text_starter;
182 : 344256 : diagnostics::text_finalizer (context) = diagnostics::default_text_finalizer;
183 : 344256 : context->set_format_decoder (default_tree_printer);
184 : 344256 : context->set_set_locations_callback (set_inlining_locations);
185 : 344256 : context->set_client_data_hooks (make_compiler_data_hooks ());
186 : 344256 : }
|