Commit | Line | Data |
---|---|---|
de17c821 | 1 | /* Helper routines for C++ support in GDB. |
b811d2c2 | 2 | Copyright (C) 2002-2020 Free Software Foundation, Inc. |
de17c821 DJ |
3 | |
4 | Contributed by MontaVista Software. | |
5 | ||
6 | This file is part of GDB. | |
7 | ||
8 | This program is free software; you can redistribute it and/or modify | |
9 | it under the terms of the GNU General Public License as published by | |
a9762ec7 | 10 | the Free Software Foundation; either version 3 of the License, or |
de17c821 DJ |
11 | (at your option) any later version. |
12 | ||
13 | This program is distributed in the hope that it will be useful, | |
14 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
15 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
16 | GNU General Public License for more details. | |
17 | ||
18 | You should have received a copy of the GNU General Public License | |
a9762ec7 | 19 | along with this program. If not, see <http://www.gnu.org/licenses/>. */ |
de17c821 DJ |
20 | |
21 | #include "defs.h" | |
22 | #include "cp-support.h" | |
de17c821 | 23 | #include "demangle.h" |
4de283e4 | 24 | #include "gdbcmd.h" |
b6429628 | 25 | #include "dictionary.h" |
4de283e4 | 26 | #include "objfiles.h" |
b6429628 | 27 | #include "frame.h" |
4de283e4 TT |
28 | #include "symtab.h" |
29 | #include "block.h" | |
30 | #include "complaints.h" | |
362ff856 | 31 | #include "gdbtypes.h" |
4de283e4 TT |
32 | #include "expression.h" |
33 | #include "value.h" | |
34 | #include "cp-abi.h" | |
22cee43f | 35 | #include "namespace.h" |
4de283e4 | 36 | #include <signal.h> |
268a13a5 | 37 | #include "gdbsupport/gdb_setjmp.h" |
f88e9fd3 | 38 | #include "safe-ctype.h" |
268a13a5 | 39 | #include "gdbsupport/selftest.h" |
21987b9c | 40 | #include "gdbsupport/gdb-sigmask.h" |
3b3978bc TT |
41 | #include <atomic> |
42 | #include "event-top.h" | |
43 | #include "run-on-main-thread.h" | |
f88e9fd3 | 44 | |
fb4c6eba DJ |
45 | #define d_left(dc) (dc)->u.s_binary.left |
46 | #define d_right(dc) (dc)->u.s_binary.right | |
b2a7f303 | 47 | |
fb4c6eba | 48 | /* Functions related to demangled name parsing. */ |
b2a7f303 DC |
49 | |
50 | static unsigned int cp_find_first_component_aux (const char *name, | |
51 | int permissive); | |
52 | ||
53 | static void demangled_name_complaint (const char *name); | |
b6429628 | 54 | |
0891c3cc | 55 | /* Functions related to overload resolution. */ |
b6429628 | 56 | |
8d577d32 | 57 | static void overload_list_add_symbol (struct symbol *sym, |
0891c3cc PA |
58 | const char *oload_name, |
59 | std::vector<symbol *> *overload_list); | |
8d577d32 | 60 | |
0891c3cc PA |
61 | static void add_symbol_overload_list_using |
62 | (const char *func_name, const char *the_namespace, | |
63 | std::vector<symbol *> *overload_list); | |
8d577d32 | 64 | |
0891c3cc PA |
65 | static void add_symbol_overload_list_qualified |
66 | (const char *func_name, | |
67 | std::vector<symbol *> *overload_list); | |
8d577d32 | 68 | |
9219021c DC |
69 | /* The list of "maint cplus" commands. */ |
70 | ||
5c4e30ca | 71 | struct cmd_list_element *maint_cplus_cmd_list = NULL; |
9219021c | 72 | |
3a93a0c2 KS |
73 | /* A list of typedefs which should not be substituted by replace_typedefs. */ |
74 | static const char * const ignore_typedefs[] = | |
75 | { | |
76 | "std::istream", "std::iostream", "std::ostream", "std::string" | |
77 | }; | |
78 | ||
79 | static void | |
80 | replace_typedefs (struct demangle_parse_info *info, | |
2621e0fd TT |
81 | struct demangle_component *ret_comp, |
82 | canonicalization_ftype *finder, | |
83 | void *data); | |
3a93a0c2 KS |
84 | |
85 | /* A convenience function to copy STRING into OBSTACK, returning a pointer | |
86 | to the newly allocated string and saving the number of bytes saved in LEN. | |
87 | ||
88 | It does not copy the terminating '\0' byte! */ | |
89 | ||
90 | static char * | |
91 | copy_string_to_obstack (struct obstack *obstack, const char *string, | |
92 | long *len) | |
93 | { | |
94 | *len = strlen (string); | |
224c3ddb | 95 | return (char *) obstack_copy (obstack, string, *len); |
3a93a0c2 KS |
96 | } |
97 | ||
f88e9fd3 DJ |
98 | /* Return 1 if STRING is clearly already in canonical form. This |
99 | function is conservative; things which it does not recognize are | |
100 | assumed to be non-canonical, and the parser will sort them out | |
101 | afterwards. This speeds up the critical path for alphanumeric | |
102 | identifiers. */ | |
103 | ||
104 | static int | |
105 | cp_already_canonical (const char *string) | |
106 | { | |
107 | /* Identifier start character [a-zA-Z_]. */ | |
108 | if (!ISIDST (string[0])) | |
109 | return 0; | |
110 | ||
111 | /* These are the only two identifiers which canonicalize to other | |
112 | than themselves or an error: unsigned -> unsigned int and | |
113 | signed -> int. */ | |
114 | if (string[0] == 'u' && strcmp (&string[1], "nsigned") == 0) | |
115 | return 0; | |
116 | else if (string[0] == 's' && strcmp (&string[1], "igned") == 0) | |
117 | return 0; | |
118 | ||
119 | /* Identifier character [a-zA-Z0-9_]. */ | |
120 | while (ISIDNUM (string[1])) | |
121 | string++; | |
122 | ||
123 | if (string[1] == '\0') | |
124 | return 1; | |
125 | else | |
126 | return 0; | |
127 | } | |
9219021c | 128 | |
3a93a0c2 KS |
129 | /* Inspect the given RET_COMP for its type. If it is a typedef, |
130 | replace the node with the typedef's tree. | |
131 | ||
132 | Returns 1 if any typedef substitutions were made, 0 otherwise. */ | |
133 | ||
134 | static int | |
135 | inspect_type (struct demangle_parse_info *info, | |
2621e0fd TT |
136 | struct demangle_component *ret_comp, |
137 | canonicalization_ftype *finder, | |
138 | void *data) | |
3a93a0c2 | 139 | { |
3a93a0c2 KS |
140 | char *name; |
141 | struct symbol *sym; | |
3a93a0c2 KS |
142 | |
143 | /* Copy the symbol's name from RET_COMP and look it up | |
144 | in the symbol table. */ | |
145 | name = (char *) alloca (ret_comp->u.s_name.len + 1); | |
146 | memcpy (name, ret_comp->u.s_name.s, ret_comp->u.s_name.len); | |
147 | name[ret_comp->u.s_name.len] = '\0'; | |
148 | ||
149 | /* Ignore any typedefs that should not be substituted. */ | |
b926417a | 150 | for (int i = 0; i < ARRAY_SIZE (ignore_typedefs); ++i) |
3a93a0c2 KS |
151 | { |
152 | if (strcmp (name, ignore_typedefs[i]) == 0) | |
153 | return 0; | |
154 | } | |
155 | ||
156 | sym = NULL; | |
3a93a0c2 | 157 | |
a70b8144 | 158 | try |
492d29ea | 159 | { |
d12307c1 | 160 | sym = lookup_symbol (name, 0, VAR_DOMAIN, 0).symbol; |
492d29ea | 161 | } |
230d2906 | 162 | catch (const gdb_exception &except) |
492d29ea PA |
163 | { |
164 | return 0; | |
165 | } | |
492d29ea PA |
166 | |
167 | if (sym != NULL) | |
3a93a0c2 KS |
168 | { |
169 | struct type *otype = SYMBOL_TYPE (sym); | |
170 | ||
2621e0fd TT |
171 | if (finder != NULL) |
172 | { | |
173 | const char *new_name = (*finder) (otype, data); | |
174 | ||
175 | if (new_name != NULL) | |
176 | { | |
177 | ret_comp->u.s_name.s = new_name; | |
178 | ret_comp->u.s_name.len = strlen (new_name); | |
179 | return 1; | |
180 | } | |
181 | ||
182 | return 0; | |
183 | } | |
184 | ||
74921315 KS |
185 | /* If the type is a typedef or namespace alias, replace it. */ |
186 | if (TYPE_CODE (otype) == TYPE_CODE_TYPEDEF | |
187 | || TYPE_CODE (otype) == TYPE_CODE_NAMESPACE) | |
3a93a0c2 KS |
188 | { |
189 | long len; | |
190 | int is_anon; | |
191 | struct type *type; | |
c8b23b3f | 192 | std::unique_ptr<demangle_parse_info> i; |
3a93a0c2 KS |
193 | |
194 | /* Get the real type of the typedef. */ | |
195 | type = check_typedef (otype); | |
196 | ||
725cbb63 KS |
197 | /* If the symbol name is the same as the original type name, |
198 | don't substitute. That would cause infinite recursion in | |
199 | symbol lookups, as the typedef symbol is often the first | |
200 | found symbol in the symbol table. | |
201 | ||
202 | However, this can happen in a number of situations, such as: | |
203 | ||
204 | If the symbol is a namespace and its type name is no different | |
74921315 | 205 | than the name we looked up, this symbol is not a namespace |
725cbb63 KS |
206 | alias and does not need to be substituted. |
207 | ||
208 | If the symbol is typedef and its type name is the same | |
209 | as the symbol's name, e.g., "typedef struct foo foo;". */ | |
210 | if (TYPE_NAME (type) != nullptr | |
74921315 KS |
211 | && strcmp (TYPE_NAME (type), name) == 0) |
212 | return 0; | |
213 | ||
e86ca25f | 214 | is_anon = (TYPE_NAME (type) == NULL |
3a93a0c2 KS |
215 | && (TYPE_CODE (type) == TYPE_CODE_ENUM |
216 | || TYPE_CODE (type) == TYPE_CODE_STRUCT | |
217 | || TYPE_CODE (type) == TYPE_CODE_UNION)); | |
218 | if (is_anon) | |
219 | { | |
220 | struct type *last = otype; | |
221 | ||
222 | /* Find the last typedef for the type. */ | |
223 | while (TYPE_TARGET_TYPE (last) != NULL | |
224 | && (TYPE_CODE (TYPE_TARGET_TYPE (last)) | |
225 | == TYPE_CODE_TYPEDEF)) | |
226 | last = TYPE_TARGET_TYPE (last); | |
227 | ||
228 | /* If there is only one typedef for this anonymous type, | |
229 | do not substitute it. */ | |
230 | if (type == otype) | |
231 | return 0; | |
232 | else | |
233 | /* Use the last typedef seen as the type for this | |
234 | anonymous type. */ | |
235 | type = last; | |
236 | } | |
237 | ||
d7e74731 | 238 | string_file buf; |
a70b8144 | 239 | try |
d7e74731 PA |
240 | { |
241 | type_print (type, "", &buf, -1); | |
242 | } | |
3a93a0c2 KS |
243 | /* If type_print threw an exception, there is little point |
244 | in continuing, so just bow out gracefully. */ | |
230d2906 | 245 | catch (const gdb_exception_error &except) |
3a93a0c2 | 246 | { |
3a93a0c2 KS |
247 | return 0; |
248 | } | |
249 | ||
d7e74731 | 250 | len = buf.size (); |
efba19b0 | 251 | name = obstack_strdup (&info->obstack, buf.string ()); |
3a93a0c2 KS |
252 | |
253 | /* Turn the result into a new tree. Note that this | |
254 | tree will contain pointers into NAME, so NAME cannot | |
255 | be free'd until all typedef conversion is done and | |
256 | the final result is converted into a string. */ | |
257 | i = cp_demangled_name_to_comp (name, NULL); | |
258 | if (i != NULL) | |
259 | { | |
260 | /* Merge the two trees. */ | |
c8b23b3f | 261 | cp_merge_demangle_parse_infos (info, ret_comp, i.get ()); |
3a93a0c2 KS |
262 | |
263 | /* Replace any newly introduced typedefs -- but not | |
264 | if the type is anonymous (that would lead to infinite | |
265 | looping). */ | |
266 | if (!is_anon) | |
2621e0fd | 267 | replace_typedefs (info, ret_comp, finder, data); |
3a93a0c2 KS |
268 | } |
269 | else | |
270 | { | |
271 | /* This shouldn't happen unless the type printer has | |
272 | output something that the name parser cannot grok. | |
273 | Nonetheless, an ounce of prevention... | |
274 | ||
275 | Canonicalize the name again, and store it in the | |
276 | current node (RET_COMP). */ | |
2f408ecb | 277 | std::string canon = cp_canonicalize_string_no_typedefs (name); |
3a93a0c2 | 278 | |
2f408ecb | 279 | if (!canon.empty ()) |
3a93a0c2 | 280 | { |
2f408ecb PA |
281 | /* Copy the canonicalization into the obstack. */ |
282 | name = copy_string_to_obstack (&info->obstack, canon.c_str (), &len); | |
3a93a0c2 KS |
283 | } |
284 | ||
285 | ret_comp->u.s_name.s = name; | |
286 | ret_comp->u.s_name.len = len; | |
287 | } | |
288 | ||
289 | return 1; | |
290 | } | |
291 | } | |
292 | ||
293 | return 0; | |
294 | } | |
295 | ||
296 | /* Replace any typedefs appearing in the qualified name | |
297 | (DEMANGLE_COMPONENT_QUAL_NAME) represented in RET_COMP for the name parse | |
298 | given in INFO. */ | |
299 | ||
300 | static void | |
301 | replace_typedefs_qualified_name (struct demangle_parse_info *info, | |
2621e0fd TT |
302 | struct demangle_component *ret_comp, |
303 | canonicalization_ftype *finder, | |
304 | void *data) | |
3a93a0c2 | 305 | { |
d7e74731 | 306 | string_file buf; |
3a93a0c2 KS |
307 | struct demangle_component *comp = ret_comp; |
308 | ||
309 | /* Walk each node of the qualified name, reconstructing the name of | |
310 | this element. With every node, check for any typedef substitutions. | |
311 | If a substitution has occurred, replace the qualified name node | |
312 | with a DEMANGLE_COMPONENT_NAME node representing the new, typedef- | |
313 | substituted name. */ | |
314 | while (comp->type == DEMANGLE_COMPONENT_QUAL_NAME) | |
315 | { | |
316 | if (d_left (comp)->type == DEMANGLE_COMPONENT_NAME) | |
317 | { | |
fe978cb0 | 318 | struct demangle_component newobj; |
3a93a0c2 | 319 | |
d7e74731 | 320 | buf.write (d_left (comp)->u.s_name.s, d_left (comp)->u.s_name.len); |
fe978cb0 | 321 | newobj.type = DEMANGLE_COMPONENT_NAME; |
efba19b0 | 322 | newobj.u.s_name.s = obstack_strdup (&info->obstack, buf.string ()); |
29592bde | 323 | newobj.u.s_name.len = buf.size (); |
fe978cb0 | 324 | if (inspect_type (info, &newobj, finder, data)) |
3a93a0c2 | 325 | { |
29592bde | 326 | char *s; |
3a93a0c2 KS |
327 | long slen; |
328 | ||
329 | /* A typedef was substituted in NEW. Convert it to a | |
330 | string and replace the top DEMANGLE_COMPONENT_QUAL_NAME | |
331 | node. */ | |
332 | ||
d7e74731 | 333 | buf.clear (); |
29592bde PA |
334 | gdb::unique_xmalloc_ptr<char> n |
335 | = cp_comp_to_string (&newobj, 100); | |
3a93a0c2 KS |
336 | if (n == NULL) |
337 | { | |
338 | /* If something went astray, abort typedef substitutions. */ | |
3a93a0c2 KS |
339 | return; |
340 | } | |
341 | ||
29592bde | 342 | s = copy_string_to_obstack (&info->obstack, n.get (), &slen); |
3a93a0c2 KS |
343 | |
344 | d_left (ret_comp)->type = DEMANGLE_COMPONENT_NAME; | |
345 | d_left (ret_comp)->u.s_name.s = s; | |
346 | d_left (ret_comp)->u.s_name.len = slen; | |
347 | d_right (ret_comp) = d_right (comp); | |
348 | comp = ret_comp; | |
349 | continue; | |
350 | } | |
351 | } | |
352 | else | |
353 | { | |
354 | /* The current node is not a name, so simply replace any | |
355 | typedefs in it. Then print it to the stream to continue | |
356 | checking for more typedefs in the tree. */ | |
2621e0fd | 357 | replace_typedefs (info, d_left (comp), finder, data); |
29592bde PA |
358 | gdb::unique_xmalloc_ptr<char> name |
359 | = cp_comp_to_string (d_left (comp), 100); | |
3a93a0c2 KS |
360 | if (name == NULL) |
361 | { | |
362 | /* If something went astray, abort typedef substitutions. */ | |
3a93a0c2 KS |
363 | return; |
364 | } | |
29592bde | 365 | buf.puts (name.get ()); |
3a93a0c2 | 366 | } |
2621e0fd | 367 | |
d7e74731 | 368 | buf.write ("::", 2); |
3a93a0c2 KS |
369 | comp = d_right (comp); |
370 | } | |
371 | ||
372 | /* If the next component is DEMANGLE_COMPONENT_NAME, save the qualified | |
373 | name assembled above and append the name given by COMP. Then use this | |
374 | reassembled name to check for a typedef. */ | |
375 | ||
376 | if (comp->type == DEMANGLE_COMPONENT_NAME) | |
377 | { | |
d7e74731 | 378 | buf.write (comp->u.s_name.s, comp->u.s_name.len); |
3a93a0c2 KS |
379 | |
380 | /* Replace the top (DEMANGLE_COMPONENT_QUAL_NAME) node | |
381 | with a DEMANGLE_COMPONENT_NAME node containing the whole | |
382 | name. */ | |
383 | ret_comp->type = DEMANGLE_COMPONENT_NAME; | |
efba19b0 | 384 | ret_comp->u.s_name.s = obstack_strdup (&info->obstack, buf.string ()); |
29592bde | 385 | ret_comp->u.s_name.len = buf.size (); |
2621e0fd | 386 | inspect_type (info, ret_comp, finder, data); |
3a93a0c2 KS |
387 | } |
388 | else | |
2621e0fd | 389 | replace_typedefs (info, comp, finder, data); |
3a93a0c2 KS |
390 | } |
391 | ||
392 | ||
393 | /* A function to check const and volatile qualifiers for argument types. | |
394 | ||
395 | "Parameter declarations that differ only in the presence | |
396 | or absence of `const' and/or `volatile' are equivalent." | |
397 | C++ Standard N3290, clause 13.1.3 #4. */ | |
398 | ||
399 | static void | |
400 | check_cv_qualifiers (struct demangle_component *ret_comp) | |
401 | { | |
402 | while (d_left (ret_comp) != NULL | |
403 | && (d_left (ret_comp)->type == DEMANGLE_COMPONENT_CONST | |
404 | || d_left (ret_comp)->type == DEMANGLE_COMPONENT_VOLATILE)) | |
405 | { | |
406 | d_left (ret_comp) = d_left (d_left (ret_comp)); | |
407 | } | |
408 | } | |
409 | ||
410 | /* Walk the parse tree given by RET_COMP, replacing any typedefs with | |
411 | their basic types. */ | |
412 | ||
413 | static void | |
414 | replace_typedefs (struct demangle_parse_info *info, | |
2621e0fd TT |
415 | struct demangle_component *ret_comp, |
416 | canonicalization_ftype *finder, | |
417 | void *data) | |
3a93a0c2 KS |
418 | { |
419 | if (ret_comp) | |
420 | { | |
2621e0fd TT |
421 | if (finder != NULL |
422 | && (ret_comp->type == DEMANGLE_COMPONENT_NAME | |
423 | || ret_comp->type == DEMANGLE_COMPONENT_QUAL_NAME | |
424 | || ret_comp->type == DEMANGLE_COMPONENT_TEMPLATE | |
425 | || ret_comp->type == DEMANGLE_COMPONENT_BUILTIN_TYPE)) | |
426 | { | |
29592bde PA |
427 | gdb::unique_xmalloc_ptr<char> local_name |
428 | = cp_comp_to_string (ret_comp, 10); | |
2621e0fd TT |
429 | |
430 | if (local_name != NULL) | |
431 | { | |
492d29ea | 432 | struct symbol *sym = NULL; |
2621e0fd TT |
433 | |
434 | sym = NULL; | |
a70b8144 | 435 | try |
2621e0fd | 436 | { |
29592bde PA |
437 | sym = lookup_symbol (local_name.get (), 0, |
438 | VAR_DOMAIN, 0).symbol; | |
2621e0fd | 439 | } |
230d2906 | 440 | catch (const gdb_exception &except) |
492d29ea PA |
441 | { |
442 | } | |
492d29ea | 443 | |
492d29ea | 444 | if (sym != NULL) |
2621e0fd TT |
445 | { |
446 | struct type *otype = SYMBOL_TYPE (sym); | |
447 | const char *new_name = (*finder) (otype, data); | |
448 | ||
449 | if (new_name != NULL) | |
450 | { | |
451 | ret_comp->type = DEMANGLE_COMPONENT_NAME; | |
452 | ret_comp->u.s_name.s = new_name; | |
453 | ret_comp->u.s_name.len = strlen (new_name); | |
454 | return; | |
455 | } | |
456 | } | |
457 | } | |
458 | } | |
459 | ||
3a93a0c2 KS |
460 | switch (ret_comp->type) |
461 | { | |
462 | case DEMANGLE_COMPONENT_ARGLIST: | |
463 | check_cv_qualifiers (ret_comp); | |
464 | /* Fall through */ | |
465 | ||
466 | case DEMANGLE_COMPONENT_FUNCTION_TYPE: | |
467 | case DEMANGLE_COMPONENT_TEMPLATE: | |
468 | case DEMANGLE_COMPONENT_TEMPLATE_ARGLIST: | |
469 | case DEMANGLE_COMPONENT_TYPED_NAME: | |
2621e0fd TT |
470 | replace_typedefs (info, d_left (ret_comp), finder, data); |
471 | replace_typedefs (info, d_right (ret_comp), finder, data); | |
3a93a0c2 KS |
472 | break; |
473 | ||
474 | case DEMANGLE_COMPONENT_NAME: | |
2621e0fd | 475 | inspect_type (info, ret_comp, finder, data); |
3a93a0c2 KS |
476 | break; |
477 | ||
478 | case DEMANGLE_COMPONENT_QUAL_NAME: | |
2621e0fd | 479 | replace_typedefs_qualified_name (info, ret_comp, finder, data); |
3a93a0c2 KS |
480 | break; |
481 | ||
482 | case DEMANGLE_COMPONENT_LOCAL_NAME: | |
483 | case DEMANGLE_COMPONENT_CTOR: | |
484 | case DEMANGLE_COMPONENT_ARRAY_TYPE: | |
485 | case DEMANGLE_COMPONENT_PTRMEM_TYPE: | |
2621e0fd | 486 | replace_typedefs (info, d_right (ret_comp), finder, data); |
3a93a0c2 KS |
487 | break; |
488 | ||
489 | case DEMANGLE_COMPONENT_CONST: | |
490 | case DEMANGLE_COMPONENT_RESTRICT: | |
491 | case DEMANGLE_COMPONENT_VOLATILE: | |
492 | case DEMANGLE_COMPONENT_VOLATILE_THIS: | |
493 | case DEMANGLE_COMPONENT_CONST_THIS: | |
494 | case DEMANGLE_COMPONENT_RESTRICT_THIS: | |
495 | case DEMANGLE_COMPONENT_POINTER: | |
496 | case DEMANGLE_COMPONENT_REFERENCE: | |
e4347c89 | 497 | case DEMANGLE_COMPONENT_RVALUE_REFERENCE: |
2621e0fd | 498 | replace_typedefs (info, d_left (ret_comp), finder, data); |
3a93a0c2 KS |
499 | break; |
500 | ||
501 | default: | |
502 | break; | |
503 | } | |
504 | } | |
505 | } | |
506 | ||
2f408ecb PA |
507 | /* Parse STRING and convert it to canonical form, resolving any |
508 | typedefs. If parsing fails, or if STRING is already canonical, | |
509 | return the empty string. Otherwise return the canonical form. If | |
510 | FINDER is not NULL, then type components are passed to FINDER to be | |
511 | looked up. DATA is passed verbatim to FINDER. */ | |
3a93a0c2 | 512 | |
2f408ecb | 513 | std::string |
2621e0fd TT |
514 | cp_canonicalize_string_full (const char *string, |
515 | canonicalization_ftype *finder, | |
516 | void *data) | |
3a93a0c2 | 517 | { |
2f408ecb | 518 | std::string ret; |
3a93a0c2 | 519 | unsigned int estimated_len; |
c8b23b3f | 520 | std::unique_ptr<demangle_parse_info> info; |
3a93a0c2 | 521 | |
3a93a0c2 KS |
522 | estimated_len = strlen (string) * 2; |
523 | info = cp_demangled_name_to_comp (string, NULL); | |
524 | if (info != NULL) | |
525 | { | |
526 | /* Replace all the typedefs in the tree. */ | |
c8b23b3f | 527 | replace_typedefs (info.get (), info->tree, finder, data); |
3a93a0c2 KS |
528 | |
529 | /* Convert the tree back into a string. */ | |
29592bde PA |
530 | gdb::unique_xmalloc_ptr<char> us = cp_comp_to_string (info->tree, |
531 | estimated_len); | |
e88e8651 | 532 | gdb_assert (us); |
3a93a0c2 | 533 | |
e88e8651 | 534 | ret = us.get (); |
3a93a0c2 KS |
535 | /* Finally, compare the original string with the computed |
536 | name, returning NULL if they are the same. */ | |
2f408ecb PA |
537 | if (ret == string) |
538 | return std::string (); | |
3a93a0c2 KS |
539 | } |
540 | ||
541 | return ret; | |
542 | } | |
543 | ||
2621e0fd TT |
544 | /* Like cp_canonicalize_string_full, but always passes NULL for |
545 | FINDER. */ | |
546 | ||
2f408ecb | 547 | std::string |
2621e0fd TT |
548 | cp_canonicalize_string_no_typedefs (const char *string) |
549 | { | |
550 | return cp_canonicalize_string_full (string, NULL, NULL); | |
551 | } | |
552 | ||
f88e9fd3 | 553 | /* Parse STRING and convert it to canonical form. If parsing fails, |
2f408ecb PA |
554 | or if STRING is already canonical, return the empty string. |
555 | Otherwise return the canonical form. */ | |
9219021c | 556 | |
2f408ecb | 557 | std::string |
fb4c6eba DJ |
558 | cp_canonicalize_string (const char *string) |
559 | { | |
c8b23b3f | 560 | std::unique_ptr<demangle_parse_info> info; |
f88e9fd3 | 561 | unsigned int estimated_len; |
9219021c | 562 | |
f88e9fd3 | 563 | if (cp_already_canonical (string)) |
2f408ecb | 564 | return std::string (); |
9219021c | 565 | |
3a93a0c2 KS |
566 | info = cp_demangled_name_to_comp (string, NULL); |
567 | if (info == NULL) | |
2f408ecb | 568 | return std::string (); |
9219021c | 569 | |
f88e9fd3 | 570 | estimated_len = strlen (string) * 2; |
e88e8651 YQ |
571 | gdb::unique_xmalloc_ptr<char> us (cp_comp_to_string (info->tree, |
572 | estimated_len)); | |
9219021c | 573 | |
e88e8651 | 574 | if (!us) |
9934703b JK |
575 | { |
576 | warning (_("internal error: string \"%s\" failed to be canonicalized"), | |
577 | string); | |
2f408ecb | 578 | return std::string (); |
9934703b JK |
579 | } |
580 | ||
e88e8651 YQ |
581 | std::string ret (us.get ()); |
582 | ||
2f408ecb PA |
583 | if (ret == string) |
584 | return std::string (); | |
de17c821 | 585 | |
fb4c6eba DJ |
586 | return ret; |
587 | } | |
de17c821 | 588 | |
aff410f1 MS |
589 | /* Convert a mangled name to a demangle_component tree. *MEMORY is |
590 | set to the block of used memory that should be freed when finished | |
591 | with the tree. DEMANGLED_P is set to the char * that should be | |
592 | freed when finished with the tree, or NULL if none was needed. | |
593 | OPTIONS will be passed to the demangler. */ | |
de17c821 | 594 | |
c8b23b3f | 595 | static std::unique_ptr<demangle_parse_info> |
fb4c6eba DJ |
596 | mangled_name_to_comp (const char *mangled_name, int options, |
597 | void **memory, char **demangled_p) | |
de17c821 | 598 | { |
fb4c6eba | 599 | char *demangled_name; |
de17c821 | 600 | |
fb4c6eba DJ |
601 | /* If it looks like a v3 mangled name, then try to go directly |
602 | to trees. */ | |
603 | if (mangled_name[0] == '_' && mangled_name[1] == 'Z') | |
de17c821 | 604 | { |
3a93a0c2 KS |
605 | struct demangle_component *ret; |
606 | ||
aff410f1 MS |
607 | ret = cplus_demangle_v3_components (mangled_name, |
608 | options, memory); | |
fb4c6eba DJ |
609 | if (ret) |
610 | { | |
c8b23b3f | 611 | std::unique_ptr<demangle_parse_info> info (new demangle_parse_info); |
3a93a0c2 | 612 | info->tree = ret; |
fb4c6eba | 613 | *demangled_p = NULL; |
3a93a0c2 | 614 | return info; |
fb4c6eba | 615 | } |
de17c821 DJ |
616 | } |
617 | ||
aff410f1 MS |
618 | /* If it doesn't, or if that failed, then try to demangle the |
619 | name. */ | |
8de20a37 | 620 | demangled_name = gdb_demangle (mangled_name, options); |
fb4c6eba DJ |
621 | if (demangled_name == NULL) |
622 | return NULL; | |
623 | ||
aff410f1 MS |
624 | /* If we could demangle the name, parse it to build the component |
625 | tree. */ | |
c8b23b3f TT |
626 | std::unique_ptr<demangle_parse_info> info |
627 | = cp_demangled_name_to_comp (demangled_name, NULL); | |
de17c821 | 628 | |
3a93a0c2 | 629 | if (info == NULL) |
fb4c6eba | 630 | { |
6c761d9c | 631 | xfree (demangled_name); |
fb4c6eba DJ |
632 | return NULL; |
633 | } | |
de17c821 | 634 | |
fb4c6eba | 635 | *demangled_p = demangled_name; |
3a93a0c2 | 636 | return info; |
de17c821 DJ |
637 | } |
638 | ||
639 | /* Return the name of the class containing method PHYSNAME. */ | |
640 | ||
641 | char * | |
31c27f77 | 642 | cp_class_name_from_physname (const char *physname) |
de17c821 | 643 | { |
de237128 | 644 | void *storage = NULL; |
29592bde PA |
645 | char *demangled_name = NULL; |
646 | gdb::unique_xmalloc_ptr<char> ret; | |
5e5100cb | 647 | struct demangle_component *ret_comp, *prev_comp, *cur_comp; |
c8b23b3f | 648 | std::unique_ptr<demangle_parse_info> info; |
fb4c6eba DJ |
649 | int done; |
650 | ||
3a93a0c2 KS |
651 | info = mangled_name_to_comp (physname, DMGL_ANSI, |
652 | &storage, &demangled_name); | |
653 | if (info == NULL) | |
de17c821 DJ |
654 | return NULL; |
655 | ||
fb4c6eba | 656 | done = 0; |
3a93a0c2 | 657 | ret_comp = info->tree; |
5e5100cb | 658 | |
aff410f1 MS |
659 | /* First strip off any qualifiers, if we have a function or |
660 | method. */ | |
fb4c6eba DJ |
661 | while (!done) |
662 | switch (ret_comp->type) | |
663 | { | |
fb4c6eba DJ |
664 | case DEMANGLE_COMPONENT_CONST: |
665 | case DEMANGLE_COMPONENT_RESTRICT: | |
666 | case DEMANGLE_COMPONENT_VOLATILE: | |
667 | case DEMANGLE_COMPONENT_CONST_THIS: | |
668 | case DEMANGLE_COMPONENT_RESTRICT_THIS: | |
669 | case DEMANGLE_COMPONENT_VOLATILE_THIS: | |
670 | case DEMANGLE_COMPONENT_VENDOR_TYPE_QUAL: | |
fb4c6eba DJ |
671 | ret_comp = d_left (ret_comp); |
672 | break; | |
5e5100cb DJ |
673 | default: |
674 | done = 1; | |
675 | break; | |
676 | } | |
677 | ||
678 | /* If what we have now is a function, discard the argument list. */ | |
679 | if (ret_comp->type == DEMANGLE_COMPONENT_TYPED_NAME) | |
680 | ret_comp = d_left (ret_comp); | |
681 | ||
682 | /* If what we have now is a template, strip off the template | |
683 | arguments. The left subtree may be a qualified name. */ | |
684 | if (ret_comp->type == DEMANGLE_COMPONENT_TEMPLATE) | |
685 | ret_comp = d_left (ret_comp); | |
686 | ||
aff410f1 MS |
687 | /* What we have now should be a name, possibly qualified. |
688 | Additional qualifiers could live in the left subtree or the right | |
689 | subtree. Find the last piece. */ | |
5e5100cb DJ |
690 | done = 0; |
691 | prev_comp = NULL; | |
692 | cur_comp = ret_comp; | |
693 | while (!done) | |
694 | switch (cur_comp->type) | |
695 | { | |
696 | case DEMANGLE_COMPONENT_QUAL_NAME: | |
697 | case DEMANGLE_COMPONENT_LOCAL_NAME: | |
698 | prev_comp = cur_comp; | |
699 | cur_comp = d_right (cur_comp); | |
700 | break; | |
fb4c6eba | 701 | case DEMANGLE_COMPONENT_TEMPLATE: |
5e5100cb | 702 | case DEMANGLE_COMPONENT_NAME: |
fb4c6eba DJ |
703 | case DEMANGLE_COMPONENT_CTOR: |
704 | case DEMANGLE_COMPONENT_DTOR: | |
705 | case DEMANGLE_COMPONENT_OPERATOR: | |
706 | case DEMANGLE_COMPONENT_EXTENDED_OPERATOR: | |
707 | done = 1; | |
708 | break; | |
709 | default: | |
710 | done = 1; | |
5e5100cb | 711 | cur_comp = NULL; |
fb4c6eba DJ |
712 | break; |
713 | } | |
714 | ||
5e5100cb | 715 | if (cur_comp != NULL && prev_comp != NULL) |
de17c821 | 716 | { |
5e5100cb | 717 | /* We want to discard the rightmost child of PREV_COMP. */ |
fb4c6eba | 718 | *prev_comp = *d_left (prev_comp); |
aff410f1 MS |
719 | /* The ten is completely arbitrary; we don't have a good |
720 | estimate. */ | |
5e5100cb | 721 | ret = cp_comp_to_string (ret_comp, 10); |
de17c821 DJ |
722 | } |
723 | ||
fb4c6eba | 724 | xfree (storage); |
3a93a0c2 | 725 | xfree (demangled_name); |
29592bde | 726 | return ret.release (); |
de17c821 DJ |
727 | } |
728 | ||
aff410f1 MS |
729 | /* Return the child of COMP which is the basename of a method, |
730 | variable, et cetera. All scope qualifiers are discarded, but | |
731 | template arguments will be included. The component tree may be | |
732 | modified. */ | |
de17c821 | 733 | |
5e5100cb DJ |
734 | static struct demangle_component * |
735 | unqualified_name_from_comp (struct demangle_component *comp) | |
de17c821 | 736 | { |
5e5100cb | 737 | struct demangle_component *ret_comp = comp, *last_template; |
fb4c6eba DJ |
738 | int done; |
739 | ||
fb4c6eba | 740 | done = 0; |
5e5100cb | 741 | last_template = NULL; |
fb4c6eba DJ |
742 | while (!done) |
743 | switch (ret_comp->type) | |
744 | { | |
745 | case DEMANGLE_COMPONENT_QUAL_NAME: | |
746 | case DEMANGLE_COMPONENT_LOCAL_NAME: | |
fb4c6eba DJ |
747 | ret_comp = d_right (ret_comp); |
748 | break; | |
5e5100cb DJ |
749 | case DEMANGLE_COMPONENT_TYPED_NAME: |
750 | ret_comp = d_left (ret_comp); | |
751 | break; | |
752 | case DEMANGLE_COMPONENT_TEMPLATE: | |
753 | gdb_assert (last_template == NULL); | |
754 | last_template = ret_comp; | |
755 | ret_comp = d_left (ret_comp); | |
756 | break; | |
fb4c6eba DJ |
757 | case DEMANGLE_COMPONENT_CONST: |
758 | case DEMANGLE_COMPONENT_RESTRICT: | |
759 | case DEMANGLE_COMPONENT_VOLATILE: | |
760 | case DEMANGLE_COMPONENT_CONST_THIS: | |
761 | case DEMANGLE_COMPONENT_RESTRICT_THIS: | |
762 | case DEMANGLE_COMPONENT_VOLATILE_THIS: | |
763 | case DEMANGLE_COMPONENT_VENDOR_TYPE_QUAL: | |
764 | ret_comp = d_left (ret_comp); | |
765 | break; | |
766 | case DEMANGLE_COMPONENT_NAME: | |
fb4c6eba DJ |
767 | case DEMANGLE_COMPONENT_CTOR: |
768 | case DEMANGLE_COMPONENT_DTOR: | |
769 | case DEMANGLE_COMPONENT_OPERATOR: | |
770 | case DEMANGLE_COMPONENT_EXTENDED_OPERATOR: | |
771 | done = 1; | |
772 | break; | |
773 | default: | |
5e5100cb | 774 | return NULL; |
fb4c6eba DJ |
775 | break; |
776 | } | |
777 | ||
5e5100cb DJ |
778 | if (last_template) |
779 | { | |
780 | d_left (last_template) = ret_comp; | |
781 | return last_template; | |
782 | } | |
783 | ||
784 | return ret_comp; | |
785 | } | |
786 | ||
787 | /* Return the name of the method whose linkage name is PHYSNAME. */ | |
788 | ||
789 | char * | |
790 | method_name_from_physname (const char *physname) | |
791 | { | |
de237128 | 792 | void *storage = NULL; |
29592bde PA |
793 | char *demangled_name = NULL; |
794 | gdb::unique_xmalloc_ptr<char> ret; | |
5e5100cb | 795 | struct demangle_component *ret_comp; |
c8b23b3f | 796 | std::unique_ptr<demangle_parse_info> info; |
5e5100cb | 797 | |
3a93a0c2 KS |
798 | info = mangled_name_to_comp (physname, DMGL_ANSI, |
799 | &storage, &demangled_name); | |
800 | if (info == NULL) | |
5e5100cb DJ |
801 | return NULL; |
802 | ||
3a93a0c2 | 803 | ret_comp = unqualified_name_from_comp (info->tree); |
5e5100cb | 804 | |
fb4c6eba | 805 | if (ret_comp != NULL) |
aff410f1 MS |
806 | /* The ten is completely arbitrary; we don't have a good |
807 | estimate. */ | |
fb4c6eba DJ |
808 | ret = cp_comp_to_string (ret_comp, 10); |
809 | ||
810 | xfree (storage); | |
3a93a0c2 | 811 | xfree (demangled_name); |
29592bde | 812 | return ret.release (); |
fb4c6eba | 813 | } |
de17c821 | 814 | |
5e5100cb DJ |
815 | /* If FULL_NAME is the demangled name of a C++ function (including an |
816 | arg list, possibly including namespace/class qualifications), | |
817 | return a new string containing only the function name (without the | |
06d3e5b0 | 818 | arg list/class qualifications). Otherwise, return NULL. */ |
5e5100cb | 819 | |
06d3e5b0 | 820 | gdb::unique_xmalloc_ptr<char> |
5e5100cb DJ |
821 | cp_func_name (const char *full_name) |
822 | { | |
29592bde | 823 | gdb::unique_xmalloc_ptr<char> ret; |
5e5100cb | 824 | struct demangle_component *ret_comp; |
c8b23b3f | 825 | std::unique_ptr<demangle_parse_info> info; |
5e5100cb | 826 | |
3a93a0c2 KS |
827 | info = cp_demangled_name_to_comp (full_name, NULL); |
828 | if (!info) | |
06d3e5b0 | 829 | return nullptr; |
5e5100cb | 830 | |
3a93a0c2 | 831 | ret_comp = unqualified_name_from_comp (info->tree); |
5e5100cb | 832 | |
5e5100cb DJ |
833 | if (ret_comp != NULL) |
834 | ret = cp_comp_to_string (ret_comp, 10); | |
835 | ||
06d3e5b0 | 836 | return ret; |
5e5100cb DJ |
837 | } |
838 | ||
c62446b1 PA |
839 | /* Helper for cp_remove_params. DEMANGLED_NAME is the name of a |
840 | function, including parameters and (optionally) a return type. | |
841 | Return the name of the function without parameters or return type, | |
842 | or NULL if we can not parse the name. If REQUIRE_PARAMS is false, | |
843 | then tolerate a non-existing or unbalanced parameter list. */ | |
5e5100cb | 844 | |
c62446b1 PA |
845 | static gdb::unique_xmalloc_ptr<char> |
846 | cp_remove_params_1 (const char *demangled_name, bool require_params) | |
5e5100cb | 847 | { |
109483d9 | 848 | bool done = false; |
5e5100cb | 849 | struct demangle_component *ret_comp; |
c8b23b3f | 850 | std::unique_ptr<demangle_parse_info> info; |
29592bde | 851 | gdb::unique_xmalloc_ptr<char> ret; |
5e5100cb DJ |
852 | |
853 | if (demangled_name == NULL) | |
854 | return NULL; | |
855 | ||
3a93a0c2 KS |
856 | info = cp_demangled_name_to_comp (demangled_name, NULL); |
857 | if (info == NULL) | |
5e5100cb DJ |
858 | return NULL; |
859 | ||
860 | /* First strip off any qualifiers, if we have a function or method. */ | |
3a93a0c2 | 861 | ret_comp = info->tree; |
5e5100cb DJ |
862 | while (!done) |
863 | switch (ret_comp->type) | |
864 | { | |
865 | case DEMANGLE_COMPONENT_CONST: | |
866 | case DEMANGLE_COMPONENT_RESTRICT: | |
867 | case DEMANGLE_COMPONENT_VOLATILE: | |
868 | case DEMANGLE_COMPONENT_CONST_THIS: | |
869 | case DEMANGLE_COMPONENT_RESTRICT_THIS: | |
870 | case DEMANGLE_COMPONENT_VOLATILE_THIS: | |
871 | case DEMANGLE_COMPONENT_VENDOR_TYPE_QUAL: | |
872 | ret_comp = d_left (ret_comp); | |
873 | break; | |
874 | default: | |
109483d9 | 875 | done = true; |
5e5100cb DJ |
876 | break; |
877 | } | |
878 | ||
879 | /* What we have now should be a function. Return its name. */ | |
880 | if (ret_comp->type == DEMANGLE_COMPONENT_TYPED_NAME) | |
881 | ret = cp_comp_to_string (d_left (ret_comp), 10); | |
c62446b1 PA |
882 | else if (!require_params |
883 | && (ret_comp->type == DEMANGLE_COMPONENT_NAME | |
884 | || ret_comp->type == DEMANGLE_COMPONENT_QUAL_NAME | |
885 | || ret_comp->type == DEMANGLE_COMPONENT_TEMPLATE)) | |
886 | ret = cp_comp_to_string (ret_comp, 10); | |
5e5100cb | 887 | |
109483d9 | 888 | return ret; |
5e5100cb DJ |
889 | } |
890 | ||
c62446b1 PA |
891 | /* DEMANGLED_NAME is the name of a function, including parameters and |
892 | (optionally) a return type. Return the name of the function | |
893 | without parameters or return type, or NULL if we can not parse the | |
894 | name. */ | |
895 | ||
896 | gdb::unique_xmalloc_ptr<char> | |
897 | cp_remove_params (const char *demangled_name) | |
898 | { | |
899 | return cp_remove_params_1 (demangled_name, true); | |
900 | } | |
901 | ||
902 | /* See cp-support.h. */ | |
903 | ||
904 | gdb::unique_xmalloc_ptr<char> | |
905 | cp_remove_params_if_any (const char *demangled_name, bool completion_mode) | |
906 | { | |
907 | /* Trying to remove parameters from the empty string fails. If | |
908 | we're completing / matching everything, avoid returning NULL | |
909 | which would make callers interpret the result as an error. */ | |
910 | if (demangled_name[0] == '\0' && completion_mode) | |
b02f78f9 | 911 | return make_unique_xstrdup (""); |
c62446b1 PA |
912 | |
913 | gdb::unique_xmalloc_ptr<char> without_params | |
914 | = cp_remove_params_1 (demangled_name, false); | |
915 | ||
916 | if (without_params == NULL && completion_mode) | |
917 | { | |
918 | std::string copy = demangled_name; | |
919 | ||
920 | while (!copy.empty ()) | |
921 | { | |
922 | copy.pop_back (); | |
923 | without_params = cp_remove_params_1 (copy.c_str (), false); | |
924 | if (without_params != NULL) | |
925 | break; | |
926 | } | |
927 | } | |
928 | ||
929 | return without_params; | |
930 | } | |
931 | ||
fb4c6eba DJ |
932 | /* Here are some random pieces of trivia to keep in mind while trying |
933 | to take apart demangled names: | |
de17c821 | 934 | |
fb4c6eba DJ |
935 | - Names can contain function arguments or templates, so the process |
936 | has to be, to some extent recursive: maybe keep track of your | |
937 | depth based on encountering <> and (). | |
938 | ||
939 | - Parentheses don't just have to happen at the end of a name: they | |
940 | can occur even if the name in question isn't a function, because | |
941 | a template argument might be a type that's a function. | |
942 | ||
943 | - Conversely, even if you're trying to deal with a function, its | |
944 | demangled name might not end with ')': it could be a const or | |
945 | volatile class method, in which case it ends with "const" or | |
946 | "volatile". | |
947 | ||
948 | - Parentheses are also used in anonymous namespaces: a variable | |
949 | 'foo' in an anonymous namespace gets demangled as "(anonymous | |
950 | namespace)::foo". | |
951 | ||
952 | - And operator names can contain parentheses or angle brackets. */ | |
953 | ||
954 | /* FIXME: carlton/2003-03-13: We have several functions here with | |
955 | overlapping functionality; can we combine them? Also, do they | |
956 | handle all the above considerations correctly? */ | |
de17c821 | 957 | |
9219021c DC |
958 | |
959 | /* This returns the length of first component of NAME, which should be | |
960 | the demangled name of a C++ variable/function/method/etc. | |
961 | Specifically, it returns the index of the first colon forming the | |
962 | boundary of the first component: so, given 'A::foo' or 'A::B::foo' | |
963 | it returns the 1, and given 'foo', it returns 0. */ | |
964 | ||
b2a7f303 DC |
965 | /* The character in NAME indexed by the return value is guaranteed to |
966 | always be either ':' or '\0'. */ | |
9219021c DC |
967 | |
968 | /* NOTE: carlton/2003-03-13: This function is currently only intended | |
969 | for internal use: it's probably not entirely safe when called on | |
b2a7f303 DC |
970 | user-generated input, because some of the 'index += 2' lines in |
971 | cp_find_first_component_aux might go past the end of malformed | |
972 | input. */ | |
973 | ||
974 | unsigned int | |
975 | cp_find_first_component (const char *name) | |
976 | { | |
977 | return cp_find_first_component_aux (name, 0); | |
978 | } | |
979 | ||
980 | /* Helper function for cp_find_first_component. Like that function, | |
981 | it returns the length of the first component of NAME, but to make | |
982 | the recursion easier, it also stops if it reaches an unexpected ')' | |
983 | or '>' if the value of PERMISSIVE is nonzero. */ | |
9219021c | 984 | |
b2a7f303 DC |
985 | static unsigned int |
986 | cp_find_first_component_aux (const char *name, int permissive) | |
9219021c | 987 | { |
9219021c | 988 | unsigned int index = 0; |
0f20eeea DC |
989 | /* Operator names can show up in unexpected places. Since these can |
990 | contain parentheses or angle brackets, they can screw up the | |
991 | recursion. But not every string 'operator' is part of an | |
30baf67b | 992 | operator name: e.g. you could have a variable 'cooperator'. So |
0f20eeea DC |
993 | this variable tells us whether or not we should treat the string |
994 | 'operator' as starting an operator. */ | |
995 | int operator_possible = 1; | |
9219021c DC |
996 | |
997 | for (;; ++index) | |
998 | { | |
999 | switch (name[index]) | |
1000 | { | |
1001 | case '<': | |
1002 | /* Template; eat it up. The calls to cp_first_component | |
1003 | should only return (I hope!) when they reach the '>' | |
1004 | terminating the component or a '::' between two | |
1005 | components. (Hence the '+ 2'.) */ | |
1006 | index += 1; | |
b2a7f303 | 1007 | for (index += cp_find_first_component_aux (name + index, 1); |
9219021c | 1008 | name[index] != '>'; |
b2a7f303 | 1009 | index += cp_find_first_component_aux (name + index, 1)) |
9219021c | 1010 | { |
b2a7f303 DC |
1011 | if (name[index] != ':') |
1012 | { | |
1013 | demangled_name_complaint (name); | |
1014 | return strlen (name); | |
1015 | } | |
9219021c DC |
1016 | index += 2; |
1017 | } | |
0f20eeea | 1018 | operator_possible = 1; |
9219021c DC |
1019 | break; |
1020 | case '(': | |
1021 | /* Similar comment as to '<'. */ | |
1022 | index += 1; | |
b2a7f303 | 1023 | for (index += cp_find_first_component_aux (name + index, 1); |
9219021c | 1024 | name[index] != ')'; |
b2a7f303 | 1025 | index += cp_find_first_component_aux (name + index, 1)) |
9219021c | 1026 | { |
b2a7f303 DC |
1027 | if (name[index] != ':') |
1028 | { | |
1029 | demangled_name_complaint (name); | |
1030 | return strlen (name); | |
1031 | } | |
9219021c DC |
1032 | index += 2; |
1033 | } | |
0f20eeea | 1034 | operator_possible = 1; |
9219021c DC |
1035 | break; |
1036 | case '>': | |
1037 | case ')': | |
b2a7f303 | 1038 | if (permissive) |
7a20f2c2 | 1039 | return index; |
b2a7f303 DC |
1040 | else |
1041 | { | |
1042 | demangled_name_complaint (name); | |
1043 | return strlen (name); | |
1044 | } | |
9219021c | 1045 | case '\0': |
9219021c | 1046 | return index; |
1cafadb4 DB |
1047 | case ':': |
1048 | /* ':' marks a component iff the next character is also a ':'. | |
1049 | Otherwise it is probably malformed input. */ | |
1050 | if (name[index + 1] == ':') | |
1051 | return index; | |
1052 | break; | |
0f20eeea DC |
1053 | case 'o': |
1054 | /* Operator names can screw up the recursion. */ | |
1055 | if (operator_possible | |
8090b426 | 1056 | && startswith (name + index, CP_OPERATOR_STR)) |
0f20eeea | 1057 | { |
8090b426 | 1058 | index += CP_OPERATOR_LEN; |
f88e9fd3 | 1059 | while (ISSPACE(name[index])) |
0f20eeea DC |
1060 | ++index; |
1061 | switch (name[index]) | |
1062 | { | |
cf325299 PA |
1063 | case '\0': |
1064 | return index; | |
0f20eeea DC |
1065 | /* Skip over one less than the appropriate number of |
1066 | characters: the for loop will skip over the last | |
1067 | one. */ | |
1068 | case '<': | |
1069 | if (name[index + 1] == '<') | |
1070 | index += 1; | |
1071 | else | |
1072 | index += 0; | |
1073 | break; | |
1074 | case '>': | |
1075 | case '-': | |
1076 | if (name[index + 1] == '>') | |
1077 | index += 1; | |
1078 | else | |
1079 | index += 0; | |
1080 | break; | |
1081 | case '(': | |
1082 | index += 1; | |
1083 | break; | |
1084 | default: | |
1085 | index += 0; | |
1086 | break; | |
1087 | } | |
1088 | } | |
1089 | operator_possible = 0; | |
1090 | break; | |
1091 | case ' ': | |
1092 | case ',': | |
1093 | case '.': | |
1094 | case '&': | |
1095 | case '*': | |
1096 | /* NOTE: carlton/2003-04-18: I'm not sure what the precise | |
1097 | set of relevant characters are here: it's necessary to | |
1098 | include any character that can show up before 'operator' | |
1099 | in a demangled name, and it's safe to include any | |
1100 | character that can't be part of an identifier's name. */ | |
1101 | operator_possible = 1; | |
1102 | break; | |
9219021c | 1103 | default: |
0f20eeea | 1104 | operator_possible = 0; |
9219021c DC |
1105 | break; |
1106 | } | |
1107 | } | |
1108 | } | |
1109 | ||
b2a7f303 DC |
1110 | /* Complain about a demangled name that we don't know how to parse. |
1111 | NAME is the demangled name in question. */ | |
1112 | ||
1113 | static void | |
1114 | demangled_name_complaint (const char *name) | |
1115 | { | |
b98664d3 | 1116 | complaint ("unexpected demangled name '%s'", name); |
b2a7f303 DC |
1117 | } |
1118 | ||
9219021c DC |
1119 | /* If NAME is the fully-qualified name of a C++ |
1120 | function/variable/method/etc., this returns the length of its | |
1121 | entire prefix: all of the namespaces and classes that make up its | |
1122 | name. Given 'A::foo', it returns 1, given 'A::B::foo', it returns | |
1123 | 4, given 'foo', it returns 0. */ | |
1124 | ||
1125 | unsigned int | |
1126 | cp_entire_prefix_len (const char *name) | |
1127 | { | |
1128 | unsigned int current_len = cp_find_first_component (name); | |
1129 | unsigned int previous_len = 0; | |
1130 | ||
1131 | while (name[current_len] != '\0') | |
1132 | { | |
1133 | gdb_assert (name[current_len] == ':'); | |
1134 | previous_len = current_len; | |
1135 | /* Skip the '::'. */ | |
1136 | current_len += 2; | |
1137 | current_len += cp_find_first_component (name + current_len); | |
1138 | } | |
1139 | ||
1140 | return previous_len; | |
1141 | } | |
1142 | ||
b6429628 DC |
1143 | /* Overload resolution functions. */ |
1144 | ||
8d577d32 | 1145 | /* Test to see if SYM is a symbol that we haven't seen corresponding |
0891c3cc PA |
1146 | to a function named OLOAD_NAME. If so, add it to |
1147 | OVERLOAD_LIST. */ | |
b6429628 DC |
1148 | |
1149 | static void | |
aff410f1 | 1150 | overload_list_add_symbol (struct symbol *sym, |
0891c3cc PA |
1151 | const char *oload_name, |
1152 | std::vector<symbol *> *overload_list) | |
b6429628 | 1153 | { |
aff410f1 MS |
1154 | /* If there is no type information, we can't do anything, so |
1155 | skip. */ | |
b6429628 DC |
1156 | if (SYMBOL_TYPE (sym) == NULL) |
1157 | return; | |
1158 | ||
aff410f1 | 1159 | /* skip any symbols that we've already considered. */ |
0891c3cc | 1160 | for (symbol *listed_sym : *overload_list) |
987012b8 | 1161 | if (strcmp (sym->linkage_name (), listed_sym->linkage_name ()) == 0) |
b6429628 DC |
1162 | return; |
1163 | ||
1164 | /* Get the demangled name without parameters */ | |
0891c3cc | 1165 | gdb::unique_xmalloc_ptr<char> sym_name |
987012b8 | 1166 | = cp_remove_params (sym->natural_name ()); |
b6429628 DC |
1167 | if (!sym_name) |
1168 | return; | |
1169 | ||
1170 | /* skip symbols that cannot match */ | |
109483d9 PA |
1171 | if (strcmp (sym_name.get (), oload_name) != 0) |
1172 | return; | |
b6429628 | 1173 | |
0891c3cc | 1174 | overload_list->push_back (sym); |
b6429628 DC |
1175 | } |
1176 | ||
1177 | /* Return a null-terminated list of pointers to function symbols that | |
8d577d32 | 1178 | are named FUNC_NAME and are visible within NAMESPACE. */ |
b6429628 | 1179 | |
0891c3cc | 1180 | struct std::vector<symbol *> |
8d577d32 | 1181 | make_symbol_overload_list (const char *func_name, |
fe978cb0 | 1182 | const char *the_namespace) |
b6429628 | 1183 | { |
245040d7 | 1184 | const char *name; |
0891c3cc | 1185 | std::vector<symbol *> overload_list; |
b6429628 | 1186 | |
0891c3cc | 1187 | overload_list.reserve (100); |
8d577d32 | 1188 | |
0891c3cc | 1189 | add_symbol_overload_list_using (func_name, the_namespace, &overload_list); |
8d577d32 | 1190 | |
fe978cb0 | 1191 | if (the_namespace[0] == '\0') |
245040d7 SW |
1192 | name = func_name; |
1193 | else | |
1194 | { | |
1195 | char *concatenated_name | |
224c3ddb | 1196 | = (char *) alloca (strlen (the_namespace) + 2 + strlen (func_name) + 1); |
fe978cb0 | 1197 | strcpy (concatenated_name, the_namespace); |
245040d7 SW |
1198 | strcat (concatenated_name, "::"); |
1199 | strcat (concatenated_name, func_name); | |
1200 | name = concatenated_name; | |
1201 | } | |
1202 | ||
0891c3cc PA |
1203 | add_symbol_overload_list_qualified (name, &overload_list); |
1204 | return overload_list; | |
8d577d32 DC |
1205 | } |
1206 | ||
245040d7 SW |
1207 | /* Add all symbols with a name matching NAME in BLOCK to the overload |
1208 | list. */ | |
1209 | ||
1210 | static void | |
0891c3cc PA |
1211 | add_symbol_overload_list_block (const char *name, |
1212 | const struct block *block, | |
1213 | std::vector<symbol *> *overload_list) | |
245040d7 | 1214 | { |
8157b174 | 1215 | struct block_iterator iter; |
245040d7 SW |
1216 | struct symbol *sym; |
1217 | ||
b5ec771e PA |
1218 | lookup_name_info lookup_name (name, symbol_name_match_type::FULL); |
1219 | ||
1220 | ALL_BLOCK_SYMBOLS_WITH_NAME (block, lookup_name, iter, sym) | |
0891c3cc | 1221 | overload_list_add_symbol (sym, name, overload_list); |
245040d7 SW |
1222 | } |
1223 | ||
7322dca9 SW |
1224 | /* Adds the function FUNC_NAME from NAMESPACE to the overload set. */ |
1225 | ||
1226 | static void | |
0891c3cc PA |
1227 | add_symbol_overload_list_namespace (const char *func_name, |
1228 | const char *the_namespace, | |
1229 | std::vector<symbol *> *overload_list) | |
7322dca9 | 1230 | { |
245040d7 SW |
1231 | const char *name; |
1232 | const struct block *block = NULL; | |
1233 | ||
fe978cb0 | 1234 | if (the_namespace[0] == '\0') |
245040d7 | 1235 | name = func_name; |
7322dca9 SW |
1236 | else |
1237 | { | |
1238 | char *concatenated_name | |
224c3ddb | 1239 | = (char *) alloca (strlen (the_namespace) + 2 + strlen (func_name) + 1); |
c5504eaf | 1240 | |
fe978cb0 | 1241 | strcpy (concatenated_name, the_namespace); |
7322dca9 SW |
1242 | strcat (concatenated_name, "::"); |
1243 | strcat (concatenated_name, func_name); | |
245040d7 | 1244 | name = concatenated_name; |
7322dca9 | 1245 | } |
245040d7 SW |
1246 | |
1247 | /* Look in the static block. */ | |
1248 | block = block_static_block (get_selected_block (0)); | |
eeaafae2 | 1249 | if (block) |
0891c3cc | 1250 | add_symbol_overload_list_block (name, block, overload_list); |
245040d7 SW |
1251 | |
1252 | /* Look in the global block. */ | |
1253 | block = block_global_block (block); | |
eeaafae2 | 1254 | if (block) |
0891c3cc | 1255 | add_symbol_overload_list_block (name, block, overload_list); |
245040d7 | 1256 | |
7322dca9 SW |
1257 | } |
1258 | ||
aff410f1 MS |
1259 | /* Search the namespace of the given type and namespace of and public |
1260 | base types. */ | |
7322dca9 SW |
1261 | |
1262 | static void | |
0891c3cc PA |
1263 | add_symbol_overload_list_adl_namespace (struct type *type, |
1264 | const char *func_name, | |
1265 | std::vector<symbol *> *overload_list) | |
7322dca9 | 1266 | { |
fe978cb0 | 1267 | char *the_namespace; |
0d5cff50 | 1268 | const char *type_name; |
7322dca9 SW |
1269 | int i, prefix_len; |
1270 | ||
aff410f1 | 1271 | while (TYPE_CODE (type) == TYPE_CODE_PTR |
aa006118 | 1272 | || TYPE_IS_REFERENCE (type) |
7322dca9 SW |
1273 | || TYPE_CODE (type) == TYPE_CODE_ARRAY |
1274 | || TYPE_CODE (type) == TYPE_CODE_TYPEDEF) | |
1275 | { | |
1276 | if (TYPE_CODE (type) == TYPE_CODE_TYPEDEF) | |
1277 | type = check_typedef(type); | |
1278 | else | |
1279 | type = TYPE_TARGET_TYPE (type); | |
1280 | } | |
1281 | ||
1282 | type_name = TYPE_NAME (type); | |
1283 | ||
7d3fe98e SW |
1284 | if (type_name == NULL) |
1285 | return; | |
1286 | ||
7322dca9 SW |
1287 | prefix_len = cp_entire_prefix_len (type_name); |
1288 | ||
1289 | if (prefix_len != 0) | |
1290 | { | |
224c3ddb | 1291 | the_namespace = (char *) alloca (prefix_len + 1); |
fe978cb0 PA |
1292 | strncpy (the_namespace, type_name, prefix_len); |
1293 | the_namespace[prefix_len] = '\0'; | |
7322dca9 | 1294 | |
0891c3cc PA |
1295 | add_symbol_overload_list_namespace (func_name, the_namespace, |
1296 | overload_list); | |
7322dca9 SW |
1297 | } |
1298 | ||
1299 | /* Check public base type */ | |
4753d33b | 1300 | if (TYPE_CODE (type) == TYPE_CODE_STRUCT) |
7322dca9 SW |
1301 | for (i = 0; i < TYPE_N_BASECLASSES (type); i++) |
1302 | { | |
1303 | if (BASETYPE_VIA_PUBLIC (type, i)) | |
0891c3cc PA |
1304 | add_symbol_overload_list_adl_namespace (TYPE_BASECLASS (type, i), |
1305 | func_name, | |
1306 | overload_list); | |
7322dca9 SW |
1307 | } |
1308 | } | |
1309 | ||
0891c3cc PA |
1310 | /* Adds to OVERLOAD_LIST the overload list overload candidates for |
1311 | FUNC_NAME found through argument dependent lookup. */ | |
7322dca9 | 1312 | |
0891c3cc PA |
1313 | void |
1314 | add_symbol_overload_list_adl (gdb::array_view<type *> arg_types, | |
1315 | const char *func_name, | |
1316 | std::vector<symbol *> *overload_list) | |
7322dca9 | 1317 | { |
0891c3cc PA |
1318 | for (type *arg_type : arg_types) |
1319 | add_symbol_overload_list_adl_namespace (arg_type, func_name, | |
1320 | overload_list); | |
7322dca9 SW |
1321 | } |
1322 | ||
8d577d32 DC |
1323 | /* This applies the using directives to add namespaces to search in, |
1324 | and then searches for overloads in all of those namespaces. It | |
1325 | adds the symbols found to sym_return_val. Arguments are as in | |
1326 | make_symbol_overload_list. */ | |
1327 | ||
1328 | static void | |
0891c3cc PA |
1329 | add_symbol_overload_list_using (const char *func_name, |
1330 | const char *the_namespace, | |
1331 | std::vector<symbol *> *overload_list) | |
8d577d32 | 1332 | { |
19c0c0f8 | 1333 | struct using_direct *current; |
4c3376c8 | 1334 | const struct block *block; |
8d577d32 DC |
1335 | |
1336 | /* First, go through the using directives. If any of them apply, | |
1337 | look in the appropriate namespaces for new functions to match | |
1338 | on. */ | |
b6429628 | 1339 | |
4c3376c8 SW |
1340 | for (block = get_selected_block (0); |
1341 | block != NULL; | |
1342 | block = BLOCK_SUPERBLOCK (block)) | |
1343 | for (current = block_using (block); | |
1344 | current != NULL; | |
1345 | current = current->next) | |
1346 | { | |
19c0c0f8 UW |
1347 | /* Prevent recursive calls. */ |
1348 | if (current->searched) | |
1349 | continue; | |
1350 | ||
aff410f1 MS |
1351 | /* If this is a namespace alias or imported declaration ignore |
1352 | it. */ | |
4c3376c8 SW |
1353 | if (current->alias != NULL || current->declaration != NULL) |
1354 | continue; | |
1355 | ||
fe978cb0 | 1356 | if (strcmp (the_namespace, current->import_dest) == 0) |
19c0c0f8 | 1357 | { |
aff410f1 MS |
1358 | /* Mark this import as searched so that the recursive call |
1359 | does not search it again. */ | |
167b0be1 TT |
1360 | scoped_restore reset_directive_searched |
1361 | = make_scoped_restore (¤t->searched, 1); | |
19c0c0f8 | 1362 | |
0891c3cc PA |
1363 | add_symbol_overload_list_using (func_name, |
1364 | current->import_src, | |
1365 | overload_list); | |
19c0c0f8 | 1366 | } |
4c3376c8 | 1367 | } |
b6429628 | 1368 | |
8d577d32 | 1369 | /* Now, add names for this namespace. */ |
0891c3cc PA |
1370 | add_symbol_overload_list_namespace (func_name, the_namespace, |
1371 | overload_list); | |
8d577d32 | 1372 | } |
b6429628 | 1373 | |
8d577d32 DC |
1374 | /* This does the bulk of the work of finding overloaded symbols. |
1375 | FUNC_NAME is the name of the overloaded function we're looking for | |
1376 | (possibly including namespace info). */ | |
b6429628 | 1377 | |
8d577d32 | 1378 | static void |
0891c3cc PA |
1379 | add_symbol_overload_list_qualified (const char *func_name, |
1380 | std::vector<symbol *> *overload_list) | |
8d577d32 | 1381 | { |
8d577d32 | 1382 | const struct block *b, *surrounding_static_block = 0; |
b6429628 | 1383 | |
aff410f1 MS |
1384 | /* Look through the partial symtabs for all symbols which begin by |
1385 | matching FUNC_NAME. Make sure we read that symbol table in. */ | |
b6429628 | 1386 | |
2030c079 | 1387 | for (objfile *objf : current_program_space->objfiles ()) |
aed57c53 TT |
1388 | { |
1389 | if (objf->sf) | |
1390 | objf->sf->qf->expand_symtabs_for_function (objf, func_name); | |
1391 | } | |
b6429628 DC |
1392 | |
1393 | /* Search upwards from currently selected frame (so that we can | |
1394 | complete on local vars. */ | |
1395 | ||
1396 | for (b = get_selected_block (0); b != NULL; b = BLOCK_SUPERBLOCK (b)) | |
0891c3cc | 1397 | add_symbol_overload_list_block (func_name, b, overload_list); |
b6429628 | 1398 | |
8d577d32 DC |
1399 | surrounding_static_block = block_static_block (get_selected_block (0)); |
1400 | ||
b6429628 DC |
1401 | /* Go through the symtabs and check the externs and statics for |
1402 | symbols which match. */ | |
1403 | ||
2030c079 | 1404 | for (objfile *objfile : current_program_space->objfiles ()) |
d8aeb77f | 1405 | { |
b669c953 | 1406 | for (compunit_symtab *cust : objfile->compunits ()) |
d8aeb77f TT |
1407 | { |
1408 | QUIT; | |
1409 | b = BLOCKVECTOR_BLOCK (COMPUNIT_BLOCKVECTOR (cust), GLOBAL_BLOCK); | |
1410 | add_symbol_overload_list_block (func_name, b, overload_list); | |
1411 | } | |
1412 | } | |
b6429628 | 1413 | |
2030c079 | 1414 | for (objfile *objfile : current_program_space->objfiles ()) |
d8aeb77f | 1415 | { |
b669c953 | 1416 | for (compunit_symtab *cust : objfile->compunits ()) |
d8aeb77f TT |
1417 | { |
1418 | QUIT; | |
1419 | b = BLOCKVECTOR_BLOCK (COMPUNIT_BLOCKVECTOR (cust), STATIC_BLOCK); | |
1420 | /* Don't do this block twice. */ | |
1421 | if (b == surrounding_static_block) | |
1422 | continue; | |
1423 | add_symbol_overload_list_block (func_name, b, overload_list); | |
1424 | } | |
1425 | } | |
8d577d32 DC |
1426 | } |
1427 | ||
aff410f1 | 1428 | /* Lookup the rtti type for a class name. */ |
362ff856 MC |
1429 | |
1430 | struct type * | |
582942f4 | 1431 | cp_lookup_rtti_type (const char *name, const struct block *block) |
362ff856 MC |
1432 | { |
1433 | struct symbol * rtti_sym; | |
1434 | struct type * rtti_type; | |
1435 | ||
82c7be31 DE |
1436 | /* Use VAR_DOMAIN here as NAME may be a typedef. PR 18141, 18417. |
1437 | Classes "live" in both STRUCT_DOMAIN and VAR_DOMAIN. */ | |
d12307c1 | 1438 | rtti_sym = lookup_symbol (name, block, VAR_DOMAIN, NULL).symbol; |
362ff856 MC |
1439 | |
1440 | if (rtti_sym == NULL) | |
1441 | { | |
8a3fe4f8 | 1442 | warning (_("RTTI symbol not found for class '%s'"), name); |
362ff856 MC |
1443 | return NULL; |
1444 | } | |
1445 | ||
1446 | if (SYMBOL_CLASS (rtti_sym) != LOC_TYPEDEF) | |
1447 | { | |
8a3fe4f8 | 1448 | warning (_("RTTI symbol for class '%s' is not a type"), name); |
362ff856 MC |
1449 | return NULL; |
1450 | } | |
1451 | ||
82c7be31 | 1452 | rtti_type = check_typedef (SYMBOL_TYPE (rtti_sym)); |
362ff856 MC |
1453 | |
1454 | switch (TYPE_CODE (rtti_type)) | |
1455 | { | |
4753d33b | 1456 | case TYPE_CODE_STRUCT: |
362ff856 MC |
1457 | break; |
1458 | case TYPE_CODE_NAMESPACE: | |
1459 | /* chastain/2003-11-26: the symbol tables often contain fake | |
1460 | symbols for namespaces with the same name as the struct. | |
1461 | This warning is an indication of a bug in the lookup order | |
1462 | or a bug in the way that the symbol tables are populated. */ | |
8a3fe4f8 | 1463 | warning (_("RTTI symbol for class '%s' is a namespace"), name); |
362ff856 MC |
1464 | return NULL; |
1465 | default: | |
8a3fe4f8 | 1466 | warning (_("RTTI symbol for class '%s' has bad type"), name); |
362ff856 MC |
1467 | return NULL; |
1468 | } | |
1469 | ||
1470 | return rtti_type; | |
1471 | } | |
b6429628 | 1472 | |
992c7d70 GB |
1473 | #ifdef HAVE_WORKING_FORK |
1474 | ||
491144b5 | 1475 | /* If true, attempt to catch crashes in the demangler and print |
992c7d70 GB |
1476 | useful debugging information. */ |
1477 | ||
491144b5 | 1478 | static bool catch_demangler_crashes = true; |
992c7d70 | 1479 | |
992c7d70 GB |
1480 | /* Stack context and environment for demangler crash recovery. */ |
1481 | ||
3b3978bc | 1482 | static thread_local SIGJMP_BUF *gdb_demangle_jmp_buf; |
992c7d70 | 1483 | |
3b3978bc | 1484 | /* If true, attempt to dump core from the signal handler. */ |
992c7d70 | 1485 | |
3b3978bc | 1486 | static std::atomic<bool> gdb_demangle_attempt_core_dump; |
992c7d70 GB |
1487 | |
1488 | /* Signal handler for gdb_demangle. */ | |
1489 | ||
1490 | static void | |
1491 | gdb_demangle_signal_handler (int signo) | |
1492 | { | |
1493 | if (gdb_demangle_attempt_core_dump) | |
1494 | { | |
1495 | if (fork () == 0) | |
1496 | dump_core (); | |
1497 | ||
3b3978bc | 1498 | gdb_demangle_attempt_core_dump = false; |
992c7d70 GB |
1499 | } |
1500 | ||
3b3978bc TT |
1501 | SIGLONGJMP (*gdb_demangle_jmp_buf, signo); |
1502 | } | |
1503 | ||
1504 | /* A helper for gdb_demangle that reports a demangling failure. */ | |
1505 | ||
1506 | static void | |
1507 | report_failed_demangle (const char *name, bool core_dump_allowed, | |
1508 | int crash_signal) | |
1509 | { | |
1510 | static bool error_reported = false; | |
1511 | ||
1512 | if (!error_reported) | |
1513 | { | |
1514 | std::string short_msg | |
1515 | = string_printf (_("unable to demangle '%s' " | |
1516 | "(demangler failed with signal %d)"), | |
1517 | name, crash_signal); | |
1518 | ||
1519 | std::string long_msg | |
1520 | = string_printf ("%s:%d: %s: %s", __FILE__, __LINE__, | |
1521 | "demangler-warning", short_msg.c_str ()); | |
1522 | ||
1523 | target_terminal::scoped_restore_terminal_state term_state; | |
1524 | target_terminal::ours_for_output (); | |
1525 | ||
1526 | begin_line (); | |
1527 | if (core_dump_allowed) | |
1528 | fprintf_unfiltered (gdb_stderr, | |
1529 | _("%s\nAttempting to dump core.\n"), | |
1530 | long_msg.c_str ()); | |
1531 | else | |
1532 | warn_cant_dump_core (long_msg.c_str ()); | |
1533 | ||
1534 | demangler_warning (__FILE__, __LINE__, "%s", short_msg.c_str ()); | |
1535 | ||
1536 | error_reported = true; | |
1537 | } | |
992c7d70 GB |
1538 | } |
1539 | ||
1540 | #endif | |
1541 | ||
8de20a37 TT |
1542 | /* A wrapper for bfd_demangle. */ |
1543 | ||
1544 | char * | |
1545 | gdb_demangle (const char *name, int options) | |
1546 | { | |
992c7d70 GB |
1547 | char *result = NULL; |
1548 | int crash_signal = 0; | |
1549 | ||
1550 | #ifdef HAVE_WORKING_FORK | |
3b3978bc TT |
1551 | scoped_restore restore_segv |
1552 | = make_scoped_restore (&thread_local_segv_handler, | |
1553 | catch_demangler_crashes | |
1554 | ? gdb_demangle_signal_handler | |
1555 | : nullptr); | |
1556 | ||
1557 | bool core_dump_allowed = gdb_demangle_attempt_core_dump; | |
1558 | SIGJMP_BUF jmp_buf; | |
1559 | scoped_restore restore_jmp_buf | |
1560 | = make_scoped_restore (&gdb_demangle_jmp_buf, &jmp_buf); | |
992c7d70 GB |
1561 | if (catch_demangler_crashes) |
1562 | { | |
17bfe554 CB |
1563 | /* The signal handler may keep the signal blocked when we longjmp out |
1564 | of it. If we have sigprocmask, we can use it to unblock the signal | |
1565 | afterwards and we can avoid the performance overhead of saving the | |
1566 | signal mask just in case the signal gets triggered. Otherwise, just | |
1567 | tell sigsetjmp to save the mask. */ | |
1568 | #ifdef HAVE_SIGPROCMASK | |
3b3978bc | 1569 | crash_signal = SIGSETJMP (*gdb_demangle_jmp_buf, 0); |
17bfe554 | 1570 | #else |
3b3978bc | 1571 | crash_signal = SIGSETJMP (*gdb_demangle_jmp_buf, 1); |
17bfe554 | 1572 | #endif |
992c7d70 GB |
1573 | } |
1574 | #endif | |
1575 | ||
1576 | if (crash_signal == 0) | |
1577 | result = bfd_demangle (NULL, name, options); | |
1578 | ||
1579 | #ifdef HAVE_WORKING_FORK | |
1580 | if (catch_demangler_crashes) | |
1581 | { | |
992c7d70 | 1582 | if (crash_signal != 0) |
3b3978bc | 1583 | { |
17bfe554 CB |
1584 | #ifdef HAVE_SIGPROCMASK |
1585 | /* If we got the signal, SIGSEGV may still be blocked; restore it. */ | |
1586 | sigset_t segv_sig_set; | |
1587 | sigemptyset (&segv_sig_set); | |
1588 | sigaddset (&segv_sig_set, SIGSEGV); | |
21987b9c | 1589 | gdb_sigmask (SIG_UNBLOCK, &segv_sig_set, NULL); |
17bfe554 CB |
1590 | #endif |
1591 | ||
3b3978bc TT |
1592 | /* If there was a failure, we can't report it here, because |
1593 | we might be in a background thread. Instead, arrange for | |
1594 | the reporting to happen on the main thread. */ | |
1595 | std::string copy = name; | |
1596 | run_on_main_thread ([=] () | |
1597 | { | |
1598 | report_failed_demangle (copy.c_str (), core_dump_allowed, | |
1599 | crash_signal); | |
1600 | }); | |
1601 | ||
1602 | result = NULL; | |
1603 | } | |
992c7d70 GB |
1604 | } |
1605 | #endif | |
1606 | ||
1607 | return result; | |
8de20a37 TT |
1608 | } |
1609 | ||
8b302db8 TT |
1610 | /* See cp-support.h. */ |
1611 | ||
1612 | int | |
1613 | gdb_sniff_from_mangled_name (const char *mangled, char **demangled) | |
1614 | { | |
1615 | *demangled = gdb_demangle (mangled, DMGL_PARAMS | DMGL_ANSI); | |
1616 | return *demangled != NULL; | |
1617 | } | |
1618 | ||
a20714ff PA |
1619 | /* See cp-support.h. */ |
1620 | ||
1621 | unsigned int | |
1622 | cp_search_name_hash (const char *search_name) | |
1623 | { | |
1624 | /* cp_entire_prefix_len assumes a fully-qualified name with no | |
1625 | leading "::". */ | |
1626 | if (startswith (search_name, "::")) | |
1627 | search_name += 2; | |
1628 | ||
1629 | unsigned int prefix_len = cp_entire_prefix_len (search_name); | |
1630 | if (prefix_len != 0) | |
1631 | search_name += prefix_len + 2; | |
1632 | ||
bd69330d PA |
1633 | unsigned int hash = 0; |
1634 | for (const char *string = search_name; *string != '\0'; ++string) | |
1635 | { | |
1636 | string = skip_spaces (string); | |
1637 | ||
1638 | if (*string == '(') | |
1639 | break; | |
1640 | ||
1641 | /* Ignore ABI tags such as "[abi:cxx11]. */ | |
1642 | if (*string == '[' | |
1643 | && startswith (string + 1, "abi:") | |
1644 | && string[5] != ':') | |
1645 | break; | |
1646 | ||
1647 | hash = SYMBOL_HASH_NEXT (hash, *string); | |
1648 | } | |
1649 | return hash; | |
a20714ff PA |
1650 | } |
1651 | ||
1652 | /* Helper for cp_symbol_name_matches (i.e., symbol_name_matcher_ftype | |
1653 | implementation for symbol_name_match_type::WILD matching). Split | |
1654 | to a separate function for unit-testing convenience. | |
1655 | ||
1656 | If SYMBOL_SEARCH_NAME has more scopes than LOOKUP_NAME, we try to | |
1657 | match ignoring the extra leading scopes of SYMBOL_SEARCH_NAME. | |
1658 | This allows conveniently setting breakpoints on functions/methods | |
1659 | inside any namespace/class without specifying the fully-qualified | |
1660 | name. | |
1661 | ||
1662 | E.g., these match: | |
b5ec771e | 1663 | |
a20714ff PA |
1664 | [symbol search name] [lookup name] |
1665 | foo::bar::func foo::bar::func | |
1666 | foo::bar::func bar::func | |
1667 | foo::bar::func func | |
1668 | ||
1669 | While these don't: | |
1670 | ||
1671 | [symbol search name] [lookup name] | |
1672 | foo::zbar::func bar::func | |
1673 | foo::bar::func foo::func | |
1674 | ||
1675 | See more examples in the test_cp_symbol_name_matches selftest | |
1676 | function below. | |
0662b6a7 PA |
1677 | |
1678 | See symbol_name_matcher_ftype for description of SYMBOL_SEARCH_NAME | |
1679 | and COMP_MATCH_RES. | |
1680 | ||
1681 | LOOKUP_NAME/LOOKUP_NAME_LEN is the name we're looking up. | |
1682 | ||
1683 | See strncmp_iw_with_mode for description of MODE. | |
1684 | */ | |
1685 | ||
1686 | static bool | |
1687 | cp_symbol_name_matches_1 (const char *symbol_search_name, | |
1688 | const char *lookup_name, | |
1689 | size_t lookup_name_len, | |
1690 | strncmp_iw_mode mode, | |
a207cff2 | 1691 | completion_match_result *comp_match_res) |
0662b6a7 | 1692 | { |
a20714ff | 1693 | const char *sname = symbol_search_name; |
bd69330d PA |
1694 | completion_match_for_lcd *match_for_lcd |
1695 | = (comp_match_res != NULL ? &comp_match_res->match_for_lcd : NULL); | |
a20714ff PA |
1696 | |
1697 | while (true) | |
1698 | { | |
1699 | if (strncmp_iw_with_mode (sname, lookup_name, lookup_name_len, | |
bd69330d | 1700 | mode, language_cplus, match_for_lcd) == 0) |
a20714ff PA |
1701 | { |
1702 | if (comp_match_res != NULL) | |
1703 | { | |
1704 | /* Note here we set different MATCH and MATCH_FOR_LCD | |
1705 | strings. This is because with | |
1706 | ||
1707 | (gdb) b push_bac[TAB] | |
1708 | ||
1709 | we want the completion matches to list | |
1710 | ||
1711 | std::vector<int>::push_back(...) | |
1712 | std::vector<char>::push_back(...) | |
1713 | ||
1714 | etc., which are SYMBOL_SEARCH_NAMEs, while we want | |
1715 | the input line to auto-complete to | |
1716 | ||
1717 | (gdb) push_back(...) | |
1718 | ||
1719 | which is SNAME, not to | |
1720 | ||
1721 | (gdb) std::vector< | |
1722 | ||
1723 | which would be the regular common prefix between all | |
1724 | the matches otherwise. */ | |
1725 | comp_match_res->set_match (symbol_search_name, sname); | |
1726 | } | |
1727 | return true; | |
1728 | } | |
1729 | ||
1730 | unsigned int len = cp_find_first_component (sname); | |
1731 | ||
1732 | if (sname[len] == '\0') | |
1733 | return false; | |
1734 | ||
1735 | gdb_assert (sname[len] == ':'); | |
1736 | /* Skip the '::'. */ | |
1737 | sname += len + 2; | |
1738 | } | |
1739 | } | |
1740 | ||
1741 | /* C++ symbol_name_matcher_ftype implementation. */ | |
1742 | ||
1743 | static bool | |
1744 | cp_fq_symbol_name_matches (const char *symbol_search_name, | |
1745 | const lookup_name_info &lookup_name, | |
1746 | completion_match_result *comp_match_res) | |
1747 | { | |
1748 | /* Get the demangled name. */ | |
1749 | const std::string &name = lookup_name.cplus ().lookup_name (); | |
bd69330d PA |
1750 | completion_match_for_lcd *match_for_lcd |
1751 | = (comp_match_res != NULL ? &comp_match_res->match_for_lcd : NULL); | |
a20714ff PA |
1752 | strncmp_iw_mode mode = (lookup_name.completion_mode () |
1753 | ? strncmp_iw_mode::NORMAL | |
1754 | : strncmp_iw_mode::MATCH_PARAMS); | |
1755 | ||
0662b6a7 | 1756 | if (strncmp_iw_with_mode (symbol_search_name, |
a20714ff | 1757 | name.c_str (), name.size (), |
bd69330d | 1758 | mode, language_cplus, match_for_lcd) == 0) |
0662b6a7 | 1759 | { |
a207cff2 PA |
1760 | if (comp_match_res != NULL) |
1761 | comp_match_res->set_match (symbol_search_name); | |
0662b6a7 PA |
1762 | return true; |
1763 | } | |
1764 | ||
1765 | return false; | |
1766 | } | |
1767 | ||
a20714ff PA |
1768 | /* C++ symbol_name_matcher_ftype implementation for wild matches. |
1769 | Defers work to cp_symbol_name_matches_1. */ | |
0662b6a7 | 1770 | |
b5ec771e | 1771 | static bool |
a20714ff PA |
1772 | cp_symbol_name_matches (const char *symbol_search_name, |
1773 | const lookup_name_info &lookup_name, | |
1774 | completion_match_result *comp_match_res) | |
b5ec771e PA |
1775 | { |
1776 | /* Get the demangled name. */ | |
1777 | const std::string &name = lookup_name.cplus ().lookup_name (); | |
1778 | ||
1779 | strncmp_iw_mode mode = (lookup_name.completion_mode () | |
1780 | ? strncmp_iw_mode::NORMAL | |
1781 | : strncmp_iw_mode::MATCH_PARAMS); | |
1782 | ||
0662b6a7 PA |
1783 | return cp_symbol_name_matches_1 (symbol_search_name, |
1784 | name.c_str (), name.size (), | |
a207cff2 | 1785 | mode, comp_match_res); |
b5ec771e PA |
1786 | } |
1787 | ||
1788 | /* See cp-support.h. */ | |
1789 | ||
1790 | symbol_name_matcher_ftype * | |
1791 | cp_get_symbol_name_matcher (const lookup_name_info &lookup_name) | |
1792 | { | |
a20714ff PA |
1793 | switch (lookup_name.match_type ()) |
1794 | { | |
1795 | case symbol_name_match_type::FULL: | |
1796 | case symbol_name_match_type::EXPRESSION: | |
de63c46b | 1797 | case symbol_name_match_type::SEARCH_NAME: |
a20714ff PA |
1798 | return cp_fq_symbol_name_matches; |
1799 | case symbol_name_match_type::WILD: | |
1800 | return cp_symbol_name_matches; | |
1801 | } | |
1802 | ||
1803 | gdb_assert_not_reached (""); | |
b5ec771e PA |
1804 | } |
1805 | ||
c62446b1 PA |
1806 | #if GDB_SELF_TEST |
1807 | ||
1808 | namespace selftests { | |
1809 | ||
cb8c24b6 | 1810 | static void |
0662b6a7 PA |
1811 | test_cp_symbol_name_matches () |
1812 | { | |
1813 | #define CHECK_MATCH(SYMBOL, INPUT) \ | |
1814 | SELF_CHECK (cp_symbol_name_matches_1 (SYMBOL, \ | |
1815 | INPUT, sizeof (INPUT) - 1, \ | |
1816 | strncmp_iw_mode::MATCH_PARAMS, \ | |
1817 | NULL)) | |
1818 | ||
1819 | #define CHECK_NOT_MATCH(SYMBOL, INPUT) \ | |
1820 | SELF_CHECK (!cp_symbol_name_matches_1 (SYMBOL, \ | |
1821 | INPUT, sizeof (INPUT) - 1, \ | |
1822 | strncmp_iw_mode::MATCH_PARAMS, \ | |
1823 | NULL)) | |
1824 | ||
1825 | /* Like CHECK_MATCH, and also check that INPUT (and all substrings | |
1826 | that start at index 0) completes to SYMBOL. */ | |
1827 | #define CHECK_MATCH_C(SYMBOL, INPUT) \ | |
1828 | do \ | |
1829 | { \ | |
1830 | CHECK_MATCH (SYMBOL, INPUT); \ | |
1831 | for (size_t i = 0; i < sizeof (INPUT) - 1; i++) \ | |
1832 | SELF_CHECK (cp_symbol_name_matches_1 (SYMBOL, INPUT, i, \ | |
1833 | strncmp_iw_mode::NORMAL, \ | |
1834 | NULL)); \ | |
1835 | } while (0) | |
1836 | ||
1837 | /* Like CHECK_NOT_MATCH, and also check that INPUT does NOT complete | |
1838 | to SYMBOL. */ | |
1839 | #define CHECK_NOT_MATCH_C(SYMBOL, INPUT) \ | |
1840 | do \ | |
1841 | { \ | |
1842 | CHECK_NOT_MATCH (SYMBOL, INPUT); \ | |
1843 | SELF_CHECK (!cp_symbol_name_matches_1 (SYMBOL, INPUT, \ | |
1844 | sizeof (INPUT) - 1, \ | |
1845 | strncmp_iw_mode::NORMAL, \ | |
1846 | NULL)); \ | |
1847 | } while (0) | |
1848 | ||
1849 | /* Lookup name without parens matches all overloads. */ | |
1850 | CHECK_MATCH_C ("function()", "function"); | |
1851 | CHECK_MATCH_C ("function(int)", "function"); | |
1852 | ||
1853 | /* Check whitespace around parameters is ignored. */ | |
1854 | CHECK_MATCH_C ("function()", "function ()"); | |
1855 | CHECK_MATCH_C ("function ( )", "function()"); | |
1856 | CHECK_MATCH_C ("function ()", "function( )"); | |
1857 | CHECK_MATCH_C ("func(int)", "func( int )"); | |
1858 | CHECK_MATCH_C ("func(int)", "func ( int ) "); | |
1859 | CHECK_MATCH_C ("func ( int )", "func( int )"); | |
1860 | CHECK_MATCH_C ("func ( int )", "func ( int ) "); | |
1861 | ||
1862 | /* Check symbol name prefixes aren't incorrectly matched. */ | |
1863 | CHECK_NOT_MATCH ("func", "function"); | |
1864 | CHECK_NOT_MATCH ("function", "func"); | |
1865 | CHECK_NOT_MATCH ("function()", "func"); | |
1866 | ||
1867 | /* Check that if the lookup name includes parameters, only the right | |
1868 | overload matches. */ | |
1869 | CHECK_MATCH_C ("function(int)", "function(int)"); | |
1870 | CHECK_NOT_MATCH_C ("function(int)", "function()"); | |
1871 | ||
1872 | /* Check that whitespace within symbol names is not ignored. */ | |
1873 | CHECK_NOT_MATCH_C ("function", "func tion"); | |
1874 | CHECK_NOT_MATCH_C ("func__tion", "func_ _tion"); | |
1875 | CHECK_NOT_MATCH_C ("func11tion", "func1 1tion"); | |
1876 | ||
1877 | /* Check the converse, which can happen with template function, | |
1878 | where the return type is part of the demangled name. */ | |
1879 | CHECK_NOT_MATCH_C ("func tion", "function"); | |
1880 | CHECK_NOT_MATCH_C ("func1 1tion", "func11tion"); | |
1881 | CHECK_NOT_MATCH_C ("func_ _tion", "func__tion"); | |
1882 | ||
1883 | /* Within parameters too. */ | |
1884 | CHECK_NOT_MATCH_C ("func(param)", "func(par am)"); | |
1885 | ||
1886 | /* Check handling of whitespace around C++ operators. */ | |
1887 | CHECK_NOT_MATCH_C ("operator<<", "opera tor<<"); | |
1888 | CHECK_NOT_MATCH_C ("operator<<", "operator< <"); | |
1889 | CHECK_NOT_MATCH_C ("operator<<", "operator < <"); | |
1890 | CHECK_NOT_MATCH_C ("operator==", "operator= ="); | |
1891 | CHECK_NOT_MATCH_C ("operator==", "operator = ="); | |
1892 | CHECK_MATCH_C ("operator<<", "operator <<"); | |
1893 | CHECK_MATCH_C ("operator<<()", "operator <<"); | |
1894 | CHECK_NOT_MATCH_C ("operator<<()", "operator<<(int)"); | |
1895 | CHECK_NOT_MATCH_C ("operator<<(int)", "operator<<()"); | |
1896 | CHECK_MATCH_C ("operator==", "operator =="); | |
1897 | CHECK_MATCH_C ("operator==()", "operator =="); | |
1898 | CHECK_MATCH_C ("operator <<", "operator<<"); | |
1899 | CHECK_MATCH_C ("operator ==", "operator=="); | |
1900 | CHECK_MATCH_C ("operator bool", "operator bool"); | |
1901 | CHECK_MATCH_C ("operator bool ()", "operator bool"); | |
1902 | CHECK_MATCH_C ("operatorX<<", "operatorX < <"); | |
1903 | CHECK_MATCH_C ("Xoperator<<", "Xoperator < <"); | |
1904 | ||
1905 | CHECK_MATCH_C ("operator()(int)", "operator()(int)"); | |
1906 | CHECK_MATCH_C ("operator()(int)", "operator ( ) ( int )"); | |
1907 | CHECK_MATCH_C ("operator()<long>(int)", "operator ( ) < long > ( int )"); | |
1908 | /* The first "()" is not the parameter list. */ | |
1909 | CHECK_NOT_MATCH ("operator()(int)", "operator"); | |
1910 | ||
1911 | /* Misc user-defined operator tests. */ | |
1912 | ||
1913 | CHECK_NOT_MATCH_C ("operator/=()", "operator ^="); | |
1914 | /* Same length at end of input. */ | |
1915 | CHECK_NOT_MATCH_C ("operator>>", "operator[]"); | |
1916 | /* Same length but not at end of input. */ | |
1917 | CHECK_NOT_MATCH_C ("operator>>()", "operator[]()"); | |
1918 | ||
1919 | CHECK_MATCH_C ("base::operator char*()", "base::operator char*()"); | |
1920 | CHECK_MATCH_C ("base::operator char*()", "base::operator char * ()"); | |
1921 | CHECK_MATCH_C ("base::operator char**()", "base::operator char * * ()"); | |
1922 | CHECK_MATCH ("base::operator char**()", "base::operator char * *"); | |
1923 | CHECK_MATCH_C ("base::operator*()", "base::operator*()"); | |
1924 | CHECK_NOT_MATCH_C ("base::operator char*()", "base::operatorc"); | |
1925 | CHECK_NOT_MATCH ("base::operator char*()", "base::operator char"); | |
1926 | CHECK_NOT_MATCH ("base::operator char*()", "base::operat"); | |
1927 | ||
1928 | /* Check handling of whitespace around C++ scope operators. */ | |
1929 | CHECK_NOT_MATCH_C ("foo::bar", "foo: :bar"); | |
1930 | CHECK_MATCH_C ("foo::bar", "foo :: bar"); | |
1931 | CHECK_MATCH_C ("foo :: bar", "foo::bar"); | |
1932 | ||
1933 | CHECK_MATCH_C ("abc::def::ghi()", "abc::def::ghi()"); | |
1934 | CHECK_MATCH_C ("abc::def::ghi ( )", "abc::def::ghi()"); | |
1935 | CHECK_MATCH_C ("abc::def::ghi()", "abc::def::ghi ( )"); | |
1936 | CHECK_MATCH_C ("function()", "function()"); | |
1937 | CHECK_MATCH_C ("bar::function()", "bar::function()"); | |
a20714ff PA |
1938 | |
1939 | /* Wild matching tests follow. */ | |
1940 | ||
1941 | /* Tests matching symbols in some scope. */ | |
1942 | CHECK_MATCH_C ("foo::function()", "function"); | |
1943 | CHECK_MATCH_C ("foo::function(int)", "function"); | |
1944 | CHECK_MATCH_C ("foo::bar::function()", "function"); | |
1945 | CHECK_MATCH_C ("bar::function()", "bar::function"); | |
1946 | CHECK_MATCH_C ("foo::bar::function()", "bar::function"); | |
1947 | CHECK_MATCH_C ("foo::bar::function(int)", "bar::function"); | |
1948 | ||
1949 | /* Same, with parameters in the lookup name. */ | |
1950 | CHECK_MATCH_C ("foo::function()", "function()"); | |
1951 | CHECK_MATCH_C ("foo::bar::function()", "function()"); | |
1952 | CHECK_MATCH_C ("foo::function(int)", "function(int)"); | |
1953 | CHECK_MATCH_C ("foo::function()", "foo::function()"); | |
1954 | CHECK_MATCH_C ("foo::bar::function()", "bar::function()"); | |
1955 | CHECK_MATCH_C ("foo::bar::function(int)", "bar::function(int)"); | |
1956 | CHECK_MATCH_C ("bar::function()", "bar::function()"); | |
1957 | ||
1958 | CHECK_NOT_MATCH_C ("foo::bar::function(int)", "bar::function()"); | |
1959 | ||
1960 | CHECK_MATCH_C ("(anonymous namespace)::bar::function(int)", | |
1961 | "bar::function(int)"); | |
1962 | CHECK_MATCH_C ("foo::(anonymous namespace)::bar::function(int)", | |
1963 | "function(int)"); | |
1964 | ||
1965 | /* Lookup scope wider than symbol scope, should not match. */ | |
1966 | CHECK_NOT_MATCH_C ("function()", "bar::function"); | |
1967 | CHECK_NOT_MATCH_C ("function()", "bar::function()"); | |
1968 | ||
1969 | /* Explicit global scope doesn't match. */ | |
1970 | CHECK_NOT_MATCH_C ("foo::function()", "::function"); | |
1971 | CHECK_NOT_MATCH_C ("foo::function()", "::function()"); | |
1972 | CHECK_NOT_MATCH_C ("foo::function(int)", "::function()"); | |
1973 | CHECK_NOT_MATCH_C ("foo::function(int)", "::function(int)"); | |
bd69330d PA |
1974 | |
1975 | /* Test ABI tag matching/ignoring. */ | |
1976 | ||
1977 | /* If the symbol name has an ABI tag, but the lookup name doesn't, | |
1978 | then the ABI tag in the symbol name is ignored. */ | |
1979 | CHECK_MATCH_C ("function[abi:foo]()", "function"); | |
1980 | CHECK_MATCH_C ("function[abi:foo](int)", "function"); | |
1981 | CHECK_MATCH_C ("function[abi:foo]()", "function ()"); | |
1982 | CHECK_NOT_MATCH_C ("function[abi:foo]()", "function (int)"); | |
1983 | ||
1984 | CHECK_MATCH_C ("function[abi:foo]()", "function[abi:foo]"); | |
1985 | CHECK_MATCH_C ("function[abi:foo](int)", "function[abi:foo]"); | |
1986 | CHECK_MATCH_C ("function[abi:foo]()", "function[abi:foo] ()"); | |
1987 | CHECK_MATCH_C ("function[abi:foo][abi:bar]()", "function"); | |
1988 | CHECK_MATCH_C ("function[abi:foo][abi:bar](int)", "function"); | |
1989 | CHECK_MATCH_C ("function[abi:foo][abi:bar]()", "function[abi:foo]"); | |
1990 | CHECK_MATCH_C ("function[abi:foo][abi:bar](int)", "function[abi:foo]"); | |
1991 | CHECK_MATCH_C ("function[abi:foo][abi:bar]()", "function[abi:foo] ()"); | |
1992 | CHECK_NOT_MATCH_C ("function[abi:foo][abi:bar]()", "function[abi:foo] (int)"); | |
1993 | ||
1994 | CHECK_MATCH_C ("function [abi:foo][abi:bar] ( )", "function [abi:foo]"); | |
1995 | ||
1996 | /* If the symbol name does not have an ABI tag, while the lookup | |
1997 | name has one, then there's no match. */ | |
1998 | CHECK_NOT_MATCH_C ("function()", "function[abi:foo]()"); | |
1999 | CHECK_NOT_MATCH_C ("function()", "function[abi:foo]"); | |
0662b6a7 PA |
2000 | } |
2001 | ||
c62446b1 PA |
2002 | /* If non-NULL, return STR wrapped in quotes. Otherwise, return a |
2003 | "<null>" string (with no quotes). */ | |
2004 | ||
2005 | static std::string | |
2006 | quote (const char *str) | |
2007 | { | |
2008 | if (str != NULL) | |
fad03f6e | 2009 | return std::string (1, '"') + str + '"'; |
c62446b1 PA |
2010 | else |
2011 | return "<null>"; | |
2012 | } | |
2013 | ||
2014 | /* Check that removing parameter info out of NAME produces EXPECTED. | |
2015 | COMPLETION_MODE indicates whether we're testing normal and | |
2016 | completion mode. FILE and LINE are used to provide better test | |
2017 | location information in case ithe check fails. */ | |
2018 | ||
2019 | static void | |
2020 | check_remove_params (const char *file, int line, | |
2021 | const char *name, const char *expected, | |
2022 | bool completion_mode) | |
2023 | { | |
2024 | gdb::unique_xmalloc_ptr<char> result | |
2025 | = cp_remove_params_if_any (name, completion_mode); | |
2026 | ||
2027 | if ((expected == NULL) != (result == NULL) | |
2028 | || (expected != NULL | |
2029 | && strcmp (result.get (), expected) != 0)) | |
2030 | { | |
2031 | error (_("%s:%d: make-paramless self-test failed: (completion=%d) " | |
2032 | "\"%s\" -> %s, expected %s"), | |
2033 | file, line, completion_mode, name, | |
2034 | quote (result.get ()).c_str (), quote (expected).c_str ()); | |
2035 | } | |
2036 | } | |
2037 | ||
2038 | /* Entry point for cp_remove_params unit tests. */ | |
2039 | ||
2040 | static void | |
2041 | test_cp_remove_params () | |
2042 | { | |
2043 | /* Check that removing parameter info out of NAME produces EXPECTED. | |
2044 | Checks both normal and completion modes. */ | |
2045 | #define CHECK(NAME, EXPECTED) \ | |
2046 | do \ | |
2047 | { \ | |
2048 | check_remove_params (__FILE__, __LINE__, NAME, EXPECTED, false); \ | |
2049 | check_remove_params (__FILE__, __LINE__, NAME, EXPECTED, true); \ | |
2050 | } \ | |
2051 | while (0) | |
2052 | ||
2053 | /* Similar, but used when NAME is incomplete -- i.e., is has | |
2054 | unbalanced parentheses. In this case, looking for the exact name | |
2055 | should fail / return empty. */ | |
2056 | #define CHECK_INCOMPL(NAME, EXPECTED) \ | |
2057 | do \ | |
2058 | { \ | |
2059 | check_remove_params (__FILE__, __LINE__, NAME, NULL, false); \ | |
2060 | check_remove_params (__FILE__, __LINE__, NAME, EXPECTED, true); \ | |
2061 | } \ | |
2062 | while (0) | |
2063 | ||
2064 | CHECK ("function()", "function"); | |
2065 | CHECK_INCOMPL ("function(", "function"); | |
2066 | CHECK ("function() const", "function"); | |
2067 | ||
2068 | CHECK ("(anonymous namespace)::A::B::C", | |
2069 | "(anonymous namespace)::A::B::C"); | |
2070 | ||
2071 | CHECK ("A::(anonymous namespace)", | |
2072 | "A::(anonymous namespace)"); | |
2073 | ||
2074 | CHECK_INCOMPL ("A::(anonymou", "A"); | |
2075 | ||
2076 | CHECK ("A::foo<int>()", | |
2077 | "A::foo<int>"); | |
2078 | ||
2079 | CHECK_INCOMPL ("A::foo<int>(", | |
2080 | "A::foo<int>"); | |
2081 | ||
2082 | CHECK ("A::foo<(anonymous namespace)::B>::func(int)", | |
2083 | "A::foo<(anonymous namespace)::B>::func"); | |
2084 | ||
2085 | CHECK_INCOMPL ("A::foo<(anonymous namespace)::B>::func(in", | |
2086 | "A::foo<(anonymous namespace)::B>::func"); | |
2087 | ||
2088 | CHECK_INCOMPL ("A::foo<(anonymous namespace)::B>::", | |
2089 | "A::foo<(anonymous namespace)::B>"); | |
2090 | ||
2091 | CHECK_INCOMPL ("A::foo<(anonymous namespace)::B>:", | |
2092 | "A::foo<(anonymous namespace)::B>"); | |
2093 | ||
2094 | CHECK ("A::foo<(anonymous namespace)::B>", | |
2095 | "A::foo<(anonymous namespace)::B>"); | |
2096 | ||
2097 | CHECK_INCOMPL ("A::foo<(anonymous namespace)::B", | |
2098 | "A::foo"); | |
2099 | ||
2100 | /* Shouldn't this parse? Looks like a bug in | |
2101 | cp_demangled_name_to_comp. See PR c++/22411. */ | |
2102 | #if 0 | |
2103 | CHECK ("A::foo<void(int)>::func(int)", | |
2104 | "A::foo<void(int)>::func"); | |
2105 | #else | |
2106 | CHECK_INCOMPL ("A::foo<void(int)>::func(int)", | |
2107 | "A::foo"); | |
2108 | #endif | |
2109 | ||
2110 | CHECK_INCOMPL ("A::foo<void(int", | |
2111 | "A::foo"); | |
2112 | ||
2113 | #undef CHECK | |
2114 | #undef CHECK_INCOMPL | |
2115 | } | |
2116 | ||
2117 | } // namespace selftests | |
2118 | ||
2119 | #endif /* GDB_SELF_CHECK */ | |
2120 | ||
9219021c DC |
2121 | /* Don't allow just "maintenance cplus". */ |
2122 | ||
2123 | static void | |
981a3fb3 | 2124 | maint_cplus_command (const char *arg, int from_tty) |
9219021c | 2125 | { |
3e43a32a MS |
2126 | printf_unfiltered (_("\"maintenance cplus\" must be followed " |
2127 | "by the name of a command.\n")); | |
aff410f1 MS |
2128 | help_list (maint_cplus_cmd_list, |
2129 | "maintenance cplus ", | |
635c7e8a | 2130 | all_commands, gdb_stdout); |
9219021c DC |
2131 | } |
2132 | ||
2133 | /* This is a front end for cp_find_first_component, for unit testing. | |
2134 | Be careful when using it: see the NOTE above | |
2135 | cp_find_first_component. */ | |
2136 | ||
2137 | static void | |
4a475551 | 2138 | first_component_command (const char *arg, int from_tty) |
9219021c | 2139 | { |
c836824f AR |
2140 | int len; |
2141 | char *prefix; | |
2142 | ||
2143 | if (!arg) | |
2144 | return; | |
2145 | ||
2146 | len = cp_find_first_component (arg); | |
224c3ddb | 2147 | prefix = (char *) alloca (len + 1); |
9219021c DC |
2148 | |
2149 | memcpy (prefix, arg, len); | |
2150 | prefix[len] = '\0'; | |
2151 | ||
2152 | printf_unfiltered ("%s\n", prefix); | |
2153 | } | |
2154 | ||
57651221 | 2155 | /* Implement "info vtbl". */ |
c4aeac85 TT |
2156 | |
2157 | static void | |
1d12d88f | 2158 | info_vtbl_command (const char *arg, int from_tty) |
c4aeac85 TT |
2159 | { |
2160 | struct value *value; | |
2161 | ||
2162 | value = parse_and_eval (arg); | |
2163 | cplus_print_vtable (value); | |
2164 | } | |
2165 | ||
9219021c DC |
2166 | void |
2167 | _initialize_cp_support (void) | |
2168 | { | |
aff410f1 MS |
2169 | add_prefix_cmd ("cplus", class_maintenance, |
2170 | maint_cplus_command, | |
2171 | _("C++ maintenance commands."), | |
2172 | &maint_cplus_cmd_list, | |
2173 | "maintenance cplus ", | |
2174 | 0, &maintenancelist); | |
2175 | add_alias_cmd ("cp", "cplus", | |
2176 | class_maintenance, 1, | |
2177 | &maintenancelist); | |
2178 | ||
2179 | add_cmd ("first_component", | |
2180 | class_maintenance, | |
2181 | first_component_command, | |
1a966eab | 2182 | _("Print the first class/namespace component of NAME."), |
9219021c | 2183 | &maint_cplus_cmd_list); |
c4aeac85 TT |
2184 | |
2185 | add_info ("vtbl", info_vtbl_command, | |
57651221 | 2186 | _("Show the virtual function table for a C++ object.\n\ |
c4aeac85 TT |
2187 | Usage: info vtbl EXPRESSION\n\ |
2188 | Evaluate EXPRESSION and display the virtual function table for the\n\ | |
2189 | resulting object.")); | |
992c7d70 GB |
2190 | |
2191 | #ifdef HAVE_WORKING_FORK | |
2192 | add_setshow_boolean_cmd ("catch-demangler-crashes", class_maintenance, | |
2193 | &catch_demangler_crashes, _("\ | |
2194 | Set whether to attempt to catch demangler crashes."), _("\ | |
2195 | Show whether to attempt to catch demangler crashes."), _("\ | |
2196 | If enabled GDB will attempt to catch demangler crashes and\n\ | |
2197 | display the offending symbol."), | |
2198 | NULL, | |
2199 | NULL, | |
2200 | &maintenance_set_cmdlist, | |
2201 | &maintenance_show_cmdlist); | |
57357d9d TT |
2202 | |
2203 | gdb_demangle_attempt_core_dump = can_dump_core (LIMIT_CUR); | |
992c7d70 | 2204 | #endif |
c62446b1 PA |
2205 | |
2206 | #if GDB_SELF_TEST | |
0662b6a7 PA |
2207 | selftests::register_test ("cp_symbol_name_matches", |
2208 | selftests::test_cp_symbol_name_matches); | |
c62446b1 PA |
2209 | selftests::register_test ("cp_remove_params", |
2210 | selftests::test_cp_remove_params); | |
2211 | #endif | |
9219021c | 2212 | } |