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