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