libstdc++: Simplify std::any to fix -Wdeprecated-declarations warning
[official-gcc.git] / gcc / diagnostic-macro-unwinding.cc
blob3056d8c8afb12b387098c2f1378c1807a134916f
1 /* Code for unwinding macro expansions in diagnostics.
2 Copyright (C) 1999-2024 Free Software Foundation, Inc.
4 This file is part of GCC.
6 GCC is free software; you can redistribute it and/or modify it under
7 the terms of the GNU General Public License as published by the Free
8 Software Foundation; either version 3, or (at your option) any later
9 version.
11 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
12 WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 for more details.
16 You should have received a copy of the GNU General Public License
17 along with GCC; see the file COPYING3. If not see
18 <http://www.gnu.org/licenses/>. */
20 #include "config.h"
21 #include "system.h"
22 #include "coretypes.h"
23 #include "tree.h"
24 #include "diagnostic.h"
25 #include "diagnostic-macro-unwinding.h"
26 #include "intl.h"
28 /* This is a pair made of a location and the line map it originated
29 from. It's used in the maybe_unwind_expanded_macro_loc function
30 below. */
31 struct loc_map_pair
33 const line_map_macro *map;
34 location_t where;
38 /* Unwind the different macro expansions that lead to the token which
39 location is WHERE and emit diagnostics showing the resulting
40 unwound macro expansion trace. Let's look at an example to see how
41 the trace looks like. Suppose we have this piece of code,
42 artificially annotated with the line numbers to increase
43 legibility:
45 $ cat -n test.c
46 1 #define OPERATE(OPRD1, OPRT, OPRD2) \
47 2 OPRD1 OPRT OPRD2;
49 4 #define SHIFTL(A,B) \
50 5 OPERATE (A,<<,B)
52 7 #define MULT(A) \
53 8 SHIFTL (A,1)
55 10 void
56 11 g ()
57 12 {
58 13 MULT (1.0);// 1.0 << 1; <-- so this is an error.
59 14 }
61 Here is the diagnostic that we want the compiler to generate:
63 test.c: In function ‘g’:
64 test.c:5:14: error: invalid operands to binary << (have ‘double’ and ‘int’)
65 test.c:2:9: note: in definition of macro 'OPERATE'
66 test.c:8:3: note: in expansion of macro 'SHIFTL'
67 test.c:13:3: note: in expansion of macro 'MULT'
69 The part that goes from the third to the fifth line of this
70 diagnostic (the lines containing the 'note:' string) is called the
71 unwound macro expansion trace. That's the part generated by this
72 function. */
74 void
75 maybe_unwind_expanded_macro_loc (diagnostic_context *context,
76 location_t where)
78 const struct line_map *map;
79 auto_vec<loc_map_pair> loc_vec;
80 unsigned ix;
81 loc_map_pair loc, *iter;
83 const location_t original_loc = where;
85 map = linemap_lookup (line_table, where);
86 if (!linemap_macro_expansion_map_p (map))
87 return;
89 /* Let's unwind the macros that got expanded and led to the token
90 which location is WHERE. We are going to store these macros into
91 LOC_VEC, so that we can later walk it at our convenience to
92 display a somewhat meaningful trace of the macro expansion
93 history to the user. Note that the first macro of the trace
94 (which is OPERATE in the example above) is going to be stored at
95 the beginning of LOC_VEC. */
99 loc.where = where;
100 loc.map = linemap_check_macro (map);
102 loc_vec.safe_push (loc);
104 /* WHERE is the location of a token inside the expansion of a
105 macro. MAP is the map holding the locations of that macro
106 expansion. Let's get the location of the token inside the
107 context that triggered the expansion of this macro.
108 This is basically how we go "down" in the trace of macro
109 expansions that led to WHERE. */
110 where = linemap_unwind_toward_expansion (line_table, where, &map);
111 } while (linemap_macro_expansion_map_p (map));
113 /* Now map is set to the map of the location in the source that
114 first triggered the macro expansion. This must be an ordinary map. */
115 const line_map_ordinary *ord_map = linemap_check_ordinary (map);
117 /* Walk LOC_VEC and print the macro expansion trace, unless the
118 first macro which expansion triggered this trace was expanded
119 inside a system header. */
120 int saved_location_line =
121 expand_location_to_spelling_point (original_loc).line;
123 if (!LINEMAP_SYSP (ord_map))
124 FOR_EACH_VEC_ELT (loc_vec, ix, iter)
126 /* Sometimes, in the unwound macro expansion trace, we want to
127 print a part of the context that shows where, in the
128 definition of the relevant macro, is the token (we are
129 looking at) used. That is the case in the introductory
130 comment of this function, where we print:
132 test.c:2:9: note: in definition of macro 'OPERATE'.
134 We print that "macro definition context" because the
135 diagnostic line (emitted by the call to
136 pp_ouput_formatted_text in diagnostic_report_diagnostic):
138 test.c:5:14: error: invalid operands to binary << (have ‘double’ and ‘int’)
140 does not point into the definition of the macro where the
141 token '<<' (that is an argument to the function-like macro
142 OPERATE) is used. So we must "display" the line of that
143 macro definition context to the user somehow.
145 A contrario, when the first interesting diagnostic line
146 points into the definition of the macro, we don't need to
147 display any line for that macro definition in the trace
148 anymore, otherwise it'd be redundant. */
150 /* Okay, now here is what we want. For each token resulting
151 from macro expansion we want to show: 1/ where in the
152 definition of the macro the token comes from; 2/ where the
153 macro got expanded. */
155 /* Resolve the location iter->where into the locus 1/ of the
156 comment above. */
157 location_t resolved_def_loc =
158 linemap_resolve_location (line_table, iter->where,
159 LRK_MACRO_DEFINITION_LOCATION, NULL);
161 /* Don't print trace for locations that are reserved or from
162 within a system header. */
163 const line_map_ordinary *m = NULL;
164 location_t l =
165 linemap_resolve_location (line_table, resolved_def_loc,
166 LRK_SPELLING_LOCATION, &m);
167 location_t l0 = l;
168 if (IS_ADHOC_LOC (l0))
169 l0 = get_location_from_adhoc_loc (line_table, l0);
170 if (l0 < RESERVED_LOCATION_COUNT || LINEMAP_SYSP (m))
171 continue;
173 /* We need to print the context of the macro definition only
174 when the locus of the first displayed diagnostic (displayed
175 before this trace) was inside the definition of the
176 macro. */
177 const int resolved_def_loc_line = SOURCE_LINE (m, l0);
178 if (ix == 0 && saved_location_line != resolved_def_loc_line)
180 diagnostic_append_note (context, resolved_def_loc,
181 "in definition of macro %qs",
182 linemap_map_get_macro_name (iter->map));
183 /* At this step, as we've printed the context of the macro
184 definition, we don't want to print the context of its
185 expansion, otherwise, it'd be redundant. */
186 continue;
189 /* Resolve the location of the expansion point of the macro
190 which expansion gave the token represented by def_loc.
191 This is the locus 2/ of the earlier comment. */
192 location_t resolved_exp_loc =
193 linemap_resolve_location (line_table,
194 iter->map->get_expansion_point_location (),
195 LRK_MACRO_DEFINITION_LOCATION, NULL);
197 diagnostic_append_note (context, resolved_exp_loc,
198 "in expansion of macro %qs",
199 linemap_map_get_macro_name (iter->map));
203 /* This is a diagnostic finalizer implementation that is aware of
204 virtual locations produced by libcpp.
206 It has to be called by the diagnostic finalizer of front ends that
207 uses libcpp and wish to get diagnostics involving tokens resulting
208 from macro expansion.
210 For a given location, if said location belongs to a token
211 resulting from a macro expansion, this starter prints the context
212 of the token. E.g, for multiply nested macro expansion, it
213 unwinds the nested macro expansions and prints them in a manner
214 that is similar to what is done for function call stacks, or
215 template instantiation contexts. */
216 void
217 virt_loc_aware_diagnostic_finalizer (diagnostic_context *context,
218 const diagnostic_info *diagnostic)
220 maybe_unwind_expanded_macro_loc (context, diagnostic_location (diagnostic));