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-2024 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 : : #include "config.h"
23 : : #include "system.h"
24 : : #include "coretypes.h"
25 : : #include "tree.h"
26 : : #include "diagnostic.h"
27 : : #include "tree-pretty-print.h"
28 : : #include "gimple-pretty-print.h"
29 : : #include "tree-diagnostic.h"
30 : : #include "diagnostic-client-data-hooks.h"
31 : : #include "langhooks.h"
32 : : #include "intl.h"
33 : : #include "diagnostic-format-text.h"
34 : :
35 : : /* Prints out, if necessary, the name of the current function
36 : : that caused an error. */
37 : : void
38 : 100620 : diagnostic_report_current_function (diagnostic_text_output_format &text_output,
39 : : const diagnostic_info *diagnostic)
40 : : {
41 : 100620 : location_t loc = diagnostic_location (diagnostic);
42 : 100620 : text_output.report_current_module (loc);
43 : 100620 : lang_hooks.print_error_function (text_output, LOCATION_FILE (loc), diagnostic);
44 : 100620 : }
45 : :
46 : : static void
47 : 100620 : default_tree_diagnostic_text_starter (diagnostic_text_output_format &text_output,
48 : : const diagnostic_info *diagnostic)
49 : : {
50 : 100620 : pretty_printer *const pp = text_output.get_printer ();
51 : 100620 : diagnostic_report_current_function (text_output, diagnostic);
52 : 100620 : pp_set_prefix (pp, text_output.build_prefix (*diagnostic));
53 : 100620 : }
54 : :
55 : : /* Default tree printer. Handles declarations only. */
56 : : bool
57 : 38413 : default_tree_printer (pretty_printer *pp, text_info *text, const char *spec,
58 : : int precision, bool wide, bool set_locus, bool hash,
59 : : bool *, pp_token_list &)
60 : : {
61 : 38413 : tree t;
62 : :
63 : : /* FUTURE: %+x should set the locus. */
64 : 38413 : if (precision != 0 || wide || hash)
65 : : return false;
66 : :
67 : 38413 : switch (*spec)
68 : : {
69 : 33649 : case 'E':
70 : 33649 : t = va_arg (*text->m_args_ptr, tree);
71 : 33649 : if (TREE_CODE (t) == IDENTIFIER_NODE)
72 : : {
73 : 130 : pp_identifier (pp, IDENTIFIER_POINTER (t));
74 : 130 : return true;
75 : : }
76 : : break;
77 : :
78 : 4574 : case 'D':
79 : 4574 : t = va_arg (*text->m_args_ptr, tree);
80 : 4574 : if (VAR_P (t) && DECL_HAS_DEBUG_EXPR_P (t))
81 : 22 : t = DECL_DEBUG_EXPR (t);
82 : : break;
83 : :
84 : 190 : case 'F':
85 : 190 : case 'T':
86 : 190 : t = va_arg (*text->m_args_ptr, tree);
87 : 190 : break;
88 : :
89 : : default:
90 : : return false;
91 : : }
92 : :
93 : 38283 : if (set_locus)
94 : 19 : text->set_location (0, DECL_SOURCE_LOCATION (t), SHOW_RANGE_WITH_CARET);
95 : :
96 : 38283 : if (DECL_P (t))
97 : : {
98 : 20314 : const char *n = DECL_NAME (t)
99 : 20314 : ? identifier_to_locale (lang_hooks.decl_printable_name (t, 2))
100 : 238 : : _("<anonymous>");
101 : 20314 : pp_string (pp, n);
102 : : }
103 : : else
104 : 17969 : dump_generic_node (pp, t, 0, TDF_SLIM, 0);
105 : :
106 : : return true;
107 : : }
108 : :
109 : : /* Set the locations of call sites along the inlining stack corresponding
110 : : to the DIAGNOSTIC location. */
111 : :
112 : : static void
113 : 94730834 : set_inlining_locations (diagnostic_context *,
114 : : diagnostic_info *diagnostic)
115 : : {
116 : 94730834 : location_t loc = diagnostic_location (diagnostic);
117 : 94730834 : tree block = LOCATION_BLOCK (loc);
118 : :
119 : : /* Count the number of locations in system headers. When all are,
120 : : warnings are suppressed by -Wno-system-headers. Otherwise, they
121 : : involve some user code, possibly inlined into a function in a system
122 : : header, and are not treated as coming from system headers. */
123 : : unsigned nsyslocs = 0;
124 : :
125 : : /* Use a reference to the vector of locations for convenience. */
126 : : auto &ilocs = diagnostic->m_iinfo.m_ilocs;
127 : :
128 : 800751 : while (block && TREE_CODE (block) == BLOCK
129 : 95901780 : && BLOCK_ABSTRACT_ORIGIN (block))
130 : : {
131 : 383718 : tree ao = BLOCK_ABSTRACT_ORIGIN (block);
132 : 383718 : if (TREE_CODE (ao) == FUNCTION_DECL)
133 : : {
134 : 130759 : if (!diagnostic->m_iinfo.m_ao)
135 : 83116 : diagnostic->m_iinfo.m_ao = block;
136 : :
137 : 130759 : location_t bsloc = BLOCK_SOURCE_LOCATION (block);
138 : 130759 : ilocs.safe_push (bsloc);
139 : 130759 : if (in_system_header_at (bsloc))
140 : 25 : ++nsyslocs;
141 : : }
142 : 252959 : else if (TREE_CODE (ao) != BLOCK)
143 : : break;
144 : :
145 : 383718 : block = BLOCK_SUPERCONTEXT (block);
146 : : }
147 : :
148 : 94730834 : if (ilocs.length ())
149 : : {
150 : : /* When there is an inlining context use the macro expansion
151 : : location for the original location and bump up NSYSLOCS if
152 : : it's in a system header since it's not counted above. */
153 : 83116 : location_t sysloc = expansion_point_location_if_in_system_header (loc);
154 : 83116 : if (sysloc != loc)
155 : : {
156 : 0 : loc = sysloc;
157 : 0 : ++nsyslocs;
158 : : }
159 : : }
160 : : else
161 : : {
162 : : /* When there's no inlining context use the original location
163 : : and set NSYSLOCS accordingly. */
164 : 94647718 : nsyslocs = in_system_header_at (loc) != 0;
165 : : }
166 : :
167 : 94730834 : ilocs.safe_push (loc);
168 : :
169 : : /* Set if all locations are in a system header. */
170 : 94730834 : diagnostic->m_iinfo.m_allsyslocs = nsyslocs == ilocs.length ();
171 : :
172 : 94730834 : if (tree *ao = pp_ti_abstract_origin (&diagnostic->message))
173 : 94557349 : *ao = (tree)diagnostic->m_iinfo.m_ao;
174 : 94730834 : }
175 : :
176 : : /* Sets CONTEXT to use language independent diagnostics. */
177 : : void
178 : 336202 : tree_diagnostics_defaults (diagnostic_context *context)
179 : : {
180 : 336202 : diagnostic_text_starter (context) = default_tree_diagnostic_text_starter;
181 : 336202 : diagnostic_text_finalizer (context) = default_diagnostic_text_finalizer;
182 : 336202 : context->set_format_decoder (default_tree_printer);
183 : 336202 : context->set_set_locations_callback (set_inlining_locations);
184 : 336202 : context->set_client_data_hooks (make_compiler_data_hooks ());
185 : 336202 : }
|