1 /* Helper routines for C++ support in GDB.
2 Copyright (C) 2002-2022 Free Software Foundation, Inc.
4 Contributed by MontaVista Software.
6 This file is part of GDB.
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.
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.
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/>. */
22 #include "cp-support.h"
25 #include "dictionary.h"
30 #include "complaints.h"
32 #include "expression.h"
35 #include "namespace.h"
37 #include "gdbsupport/gdb_setjmp.h"
38 #include "safe-ctype.h"
39 #include "gdbsupport/selftest.h"
40 #include "gdbsupport/gdb-sigmask.h"
42 #include "event-top.h"
43 #include "run-on-main-thread.h"
45 #define d_left(dc) (dc)->u.s_binary.left
46 #define d_right(dc) (dc)->u.s_binary.right
48 /* Functions related to demangled name parsing. */
50 static unsigned int cp_find_first_component_aux (const char *name
,
53 static void demangled_name_complaint (const char *name
);
55 /* Functions related to overload resolution. */
57 static void overload_list_add_symbol (struct symbol
*sym
,
58 const char *oload_name
,
59 std::vector
<symbol
*> *overload_list
);
61 static void add_symbol_overload_list_using
62 (const char *func_name
, const char *the_namespace
,
63 std::vector
<symbol
*> *overload_list
);
65 static void add_symbol_overload_list_qualified
66 (const char *func_name
,
67 std::vector
<symbol
*> *overload_list
);
69 /* The list of "maint cplus" commands. */
71 struct cmd_list_element
*maint_cplus_cmd_list
= NULL
;
73 /* A list of typedefs which should not be substituted by replace_typedefs. */
74 static const char * const ignore_typedefs
[] =
76 "std::istream", "std::iostream", "std::ostream", "std::string"
80 replace_typedefs (struct demangle_parse_info
*info
,
81 struct demangle_component
*ret_comp
,
82 canonicalization_ftype
*finder
,
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.
88 It does not copy the terminating '\0' byte! */
91 copy_string_to_obstack (struct obstack
*obstack
, const char *string
,
94 *len
= strlen (string
);
95 return (char *) obstack_copy (obstack
, string
, *len
);
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
105 cp_already_canonical (const char *string
)
107 /* Identifier start character [a-zA-Z_]. */
108 if (!ISIDST (string
[0]))
111 /* These are the only two identifiers which canonicalize to other
112 than themselves or an error: unsigned -> unsigned int and
114 if (string
[0] == 'u' && strcmp (&string
[1], "nsigned") == 0)
116 else if (string
[0] == 's' && strcmp (&string
[1], "igned") == 0)
119 /* Identifier character [a-zA-Z0-9_]. */
120 while (ISIDNUM (string
[1]))
123 if (string
[1] == '\0')
129 /* Inspect the given RET_COMP for its type. If it is a typedef,
130 replace the node with the typedef's tree.
132 Returns 1 if any typedef substitutions were made, 0 otherwise. */
135 inspect_type (struct demangle_parse_info
*info
,
136 struct demangle_component
*ret_comp
,
137 canonicalization_ftype
*finder
,
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';
149 /* Ignore any typedefs that should not be substituted. */
150 for (const char *ignorable
: ignore_typedefs
)
152 if (strcmp (name
, ignorable
) == 0)
160 sym
= lookup_symbol (name
, 0, VAR_DOMAIN
, 0).symbol
;
162 catch (const gdb_exception
&except
)
169 struct type
*otype
= SYMBOL_TYPE (sym
);
173 const char *new_name
= (*finder
) (otype
, data
);
175 if (new_name
!= NULL
)
177 ret_comp
->u
.s_name
.s
= new_name
;
178 ret_comp
->u
.s_name
.len
= strlen (new_name
);
185 /* If the type is a typedef or namespace alias, replace it. */
186 if (otype
->code () == TYPE_CODE_TYPEDEF
187 || otype
->code () == TYPE_CODE_NAMESPACE
)
192 std::unique_ptr
<demangle_parse_info
> i
;
194 /* Get the real type of the typedef. */
195 type
= check_typedef (otype
);
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.
202 However, this can happen in a number of situations, such as:
204 If the symbol is a namespace and its type name is no different
205 than the name we looked up, this symbol is not a namespace
206 alias and does not need to be substituted.
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 () != nullptr
211 && strcmp (type
->name (), name
) == 0)
214 is_anon
= (type
->name () == NULL
215 && (type
->code () == TYPE_CODE_ENUM
216 || type
->code () == TYPE_CODE_STRUCT
217 || type
->code () == TYPE_CODE_UNION
));
220 struct type
*last
= otype
;
222 /* Find the last typedef for the type. */
223 while (TYPE_TARGET_TYPE (last
) != NULL
224 && (TYPE_TARGET_TYPE (last
)->code ()
225 == TYPE_CODE_TYPEDEF
))
226 last
= TYPE_TARGET_TYPE (last
);
228 /* If there is only one typedef for this anonymous type,
229 do not substitute it. */
233 /* Use the last typedef seen as the type for this
241 type_print (type
, "", &buf
, -1);
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
)
251 name
= obstack_strdup (&info
->obstack
, buf
.string ());
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
);
260 /* Merge the two trees. */
261 cp_merge_demangle_parse_infos (info
, ret_comp
, i
.get ());
263 /* Replace any newly introduced typedefs -- but not
264 if the type is anonymous (that would lead to infinite
267 replace_typedefs (info
, ret_comp
, finder
, data
);
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...
275 Canonicalize the name again, and store it in the
276 current node (RET_COMP). */
277 gdb::unique_xmalloc_ptr
<char> canon
278 = cp_canonicalize_string_no_typedefs (name
);
280 if (canon
!= nullptr)
282 /* Copy the canonicalization into the obstack. */
283 name
= copy_string_to_obstack (&info
->obstack
, canon
.get (), &len
);
286 ret_comp
->u
.s_name
.s
= name
;
287 ret_comp
->u
.s_name
.len
= len
;
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. */
305 replace_typedefs_template (struct demangle_parse_info
*info
,
307 struct demangle_component
*tmpl
,
308 struct demangle_component
*repl
,
309 canonicalization_ftype
*finder
,
312 demangle_component
*tmpl_arglist
= d_right (tmpl
);
314 /* Replace typedefs in the template argument list. */
315 replace_typedefs (info
, tmpl_arglist
, finder
, data
);
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)
322 /* If something went astray, abort typedef substitutions. */
325 buf
.puts (tmpl_str
.get ());
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 ();
333 /* Replace any typedefs appearing in the qualified name
334 (DEMANGLE_COMPONENT_QUAL_NAME) represented in RET_COMP for the name parse
338 replace_typedefs_qualified_name (struct demangle_parse_info
*info
,
339 struct demangle_component
*ret_comp
,
340 canonicalization_ftype
*finder
,
344 struct demangle_component
*comp
= ret_comp
;
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-
351 while (comp
->type
== DEMANGLE_COMPONENT_QUAL_NAME
)
353 if (d_left (comp
)->type
== DEMANGLE_COMPONENT_TEMPLATE
)
355 /* Convert 'template + replaced template argument list' to a
356 string and replace the top DEMANGLE_COMPONENT_QUAL_NAME
358 if (!replace_typedefs_template (info
, buf
,
359 d_left (comp
), d_left (ret_comp
),
364 d_right (ret_comp
) = d_right (comp
);
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>
376 if (d_left (comp
)->type
== DEMANGLE_COMPONENT_NAME
)
378 struct demangle_component newobj
;
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
))
389 /* A typedef was substituted in NEW. Convert it to a
390 string and replace the top DEMANGLE_COMPONENT_QUAL_NAME
394 gdb::unique_xmalloc_ptr
<char> n
395 = cp_comp_to_string (&newobj
, 100);
398 /* If something went astray, abort typedef substitutions. */
402 s
= copy_string_to_obstack (&info
->obstack
, n
.get (), &slen
);
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
);
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);
422 /* If something went astray, abort typedef substitutions. */
425 buf
.puts (name
.get ());
429 comp
= d_right (comp
);
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. */
437 if (comp
->type
== DEMANGLE_COMPONENT_TEMPLATE
)
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
))
443 inspect_type (info
, ret_comp
, finder
, data
);
445 else if (comp
->type
== DEMANGLE_COMPONENT_NAME
)
447 buf
.write (comp
->u
.s_name
.s
, comp
->u
.s_name
.len
);
449 /* Replace the top (DEMANGLE_COMPONENT_QUAL_NAME) node
450 with a DEMANGLE_COMPONENT_NAME node containing the whole
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
);
458 replace_typedefs (info
, comp
, finder
, data
);
462 /* A function to check const and volatile qualifiers for argument types.
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. */
469 check_cv_qualifiers (struct demangle_component
*ret_comp
)
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
))
475 d_left (ret_comp
) = d_left (d_left (ret_comp
));
479 /* Walk the parse tree given by RET_COMP, replacing any typedefs with
480 their basic types. */
483 replace_typedefs (struct demangle_parse_info
*info
,
484 struct demangle_component
*ret_comp
,
485 canonicalization_ftype
*finder
,
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
))
496 gdb::unique_xmalloc_ptr
<char> local_name
497 = cp_comp_to_string (ret_comp
, 10);
499 if (local_name
!= NULL
)
501 struct symbol
*sym
= NULL
;
506 sym
= lookup_symbol (local_name
.get (), 0,
507 VAR_DOMAIN
, 0).symbol
;
509 catch (const gdb_exception
&except
)
515 struct type
*otype
= SYMBOL_TYPE (sym
);
516 const char *new_name
= (*finder
) (otype
, data
);
518 if (new_name
!= NULL
)
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
);
529 switch (ret_comp
->type
)
531 case DEMANGLE_COMPONENT_ARGLIST
:
532 check_cv_qualifiers (ret_comp
);
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
);
543 case DEMANGLE_COMPONENT_NAME
:
544 inspect_type (info
, ret_comp
, finder
, data
);
547 case DEMANGLE_COMPONENT_QUAL_NAME
:
548 replace_typedefs_qualified_name (info
, ret_comp
, finder
, data
);
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
);
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
);
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. */
582 gdb::unique_xmalloc_ptr
<char>
583 cp_canonicalize_string_full (const char *string
,
584 canonicalization_ftype
*finder
,
587 unsigned int estimated_len
;
588 std::unique_ptr
<demangle_parse_info
> info
;
590 estimated_len
= strlen (string
) * 2;
591 info
= cp_demangled_name_to_comp (string
, NULL
);
594 /* Replace all the typedefs in the tree. */
595 replace_typedefs (info
.get (), info
->tree
, finder
, data
);
597 /* Convert the tree back into a string. */
598 gdb::unique_xmalloc_ptr
<char> us
= cp_comp_to_string (info
->tree
,
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)
613 /* Like cp_canonicalize_string_full, but always passes NULL for
616 gdb::unique_xmalloc_ptr
<char>
617 cp_canonicalize_string_no_typedefs (const char *string
)
619 return cp_canonicalize_string_full (string
, NULL
, NULL
);
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. */
626 gdb::unique_xmalloc_ptr
<char>
627 cp_canonicalize_string (const char *string
)
629 std::unique_ptr
<demangle_parse_info
> info
;
630 unsigned int estimated_len
;
632 if (cp_already_canonical (string
))
635 info
= cp_demangled_name_to_comp (string
, NULL
);
639 estimated_len
= strlen (string
) * 2;
640 gdb::unique_xmalloc_ptr
<char> us (cp_comp_to_string (info
->tree
,
645 warning (_("internal error: string \"%s\" failed to be canonicalized"),
650 if (strcmp (us
.get (), string
) == 0)
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. */
662 static std::unique_ptr
<demangle_parse_info
>
663 mangled_name_to_comp (const char *mangled_name
, int options
,
665 gdb::unique_xmalloc_ptr
<char> *demangled_p
)
667 /* If it looks like a v3 mangled name, then try to go directly
669 if (mangled_name
[0] == '_' && mangled_name
[1] == 'Z')
671 struct demangle_component
*ret
;
673 ret
= cplus_demangle_v3_components (mangled_name
,
677 std::unique_ptr
<demangle_parse_info
> info (new demangle_parse_info
);
684 /* If it doesn't, or if that failed, then try to demangle the
686 gdb::unique_xmalloc_ptr
<char> demangled_name
= gdb_demangle (mangled_name
,
688 if (demangled_name
== NULL
)
691 /* If we could demangle the name, parse it to build the component
693 std::unique_ptr
<demangle_parse_info
> info
694 = cp_demangled_name_to_comp (demangled_name
.get (), NULL
);
699 *demangled_p
= std::move (demangled_name
);
703 /* Return the name of the class containing method PHYSNAME. */
706 cp_class_name_from_physname (const char *physname
)
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
;
715 info
= mangled_name_to_comp (physname
, DMGL_ANSI
,
716 &storage
, &demangled_name
);
721 ret_comp
= info
->tree
;
723 /* First strip off any qualifiers, if we have a function or
726 switch (ret_comp
->type
)
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
);
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
);
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
);
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. */
758 switch (cur_comp
->type
)
760 case DEMANGLE_COMPONENT_QUAL_NAME
:
761 case DEMANGLE_COMPONENT_LOCAL_NAME
:
762 prev_comp
= cur_comp
;
763 cur_comp
= d_right (cur_comp
);
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
:
779 if (cur_comp
!= NULL
&& prev_comp
!= NULL
)
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
785 ret
= cp_comp_to_string (ret_comp
, 10);
789 return ret
.release ();
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
797 static struct demangle_component
*
798 unqualified_name_from_comp (struct demangle_component
*comp
)
800 struct demangle_component
*ret_comp
= comp
, *last_template
;
804 last_template
= NULL
;
806 switch (ret_comp
->type
)
808 case DEMANGLE_COMPONENT_QUAL_NAME
:
809 case DEMANGLE_COMPONENT_LOCAL_NAME
:
810 ret_comp
= d_right (ret_comp
);
812 case DEMANGLE_COMPONENT_TYPED_NAME
:
813 ret_comp
= d_left (ret_comp
);
815 case DEMANGLE_COMPONENT_TEMPLATE
:
816 gdb_assert (last_template
== NULL
);
817 last_template
= ret_comp
;
818 ret_comp
= d_left (ret_comp
);
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
);
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
:
843 d_left (last_template
) = ret_comp
;
844 return last_template
;
850 /* Return the name of the method whose linkage name is PHYSNAME. */
853 method_name_from_physname (const char *physname
)
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
;
861 info
= mangled_name_to_comp (physname
, DMGL_ANSI
,
862 &storage
, &demangled_name
);
866 ret_comp
= unqualified_name_from_comp (info
->tree
);
868 if (ret_comp
!= NULL
)
869 /* The ten is completely arbitrary; we don't have a good
871 ret
= cp_comp_to_string (ret_comp
, 10);
874 return ret
.release ();
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. */
882 gdb::unique_xmalloc_ptr
<char>
883 cp_func_name (const char *full_name
)
885 gdb::unique_xmalloc_ptr
<char> ret
;
886 struct demangle_component
*ret_comp
;
887 std::unique_ptr
<demangle_parse_info
> info
;
889 info
= cp_demangled_name_to_comp (full_name
, NULL
);
893 ret_comp
= unqualified_name_from_comp (info
->tree
);
895 if (ret_comp
!= NULL
)
896 ret
= cp_comp_to_string (ret_comp
, 10);
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. */
907 static gdb::unique_xmalloc_ptr
<char>
908 cp_remove_params_1 (const char *demangled_name
, bool require_params
)
911 struct demangle_component
*ret_comp
;
912 std::unique_ptr
<demangle_parse_info
> info
;
913 gdb::unique_xmalloc_ptr
<char> ret
;
915 if (demangled_name
== NULL
)
918 info
= cp_demangled_name_to_comp (demangled_name
, NULL
);
922 /* First strip off any qualifiers, if we have a function or method. */
923 ret_comp
= info
->tree
;
925 switch (ret_comp
->type
)
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
);
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);
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
958 gdb::unique_xmalloc_ptr
<char>
959 cp_remove_params (const char *demangled_name
)
961 return cp_remove_params_1 (demangled_name
, true);
964 /* See cp-support.h. */
966 gdb::unique_xmalloc_ptr
<char>
967 cp_remove_params_if_any (const char *demangled_name
, bool completion_mode
)
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 ("");
975 gdb::unique_xmalloc_ptr
<char> without_params
976 = cp_remove_params_1 (demangled_name
, false);
978 if (without_params
== NULL
&& completion_mode
)
980 std::string copy
= demangled_name
;
982 while (!copy
.empty ())
985 without_params
= cp_remove_params_1 (copy
.c_str (), false);
986 if (without_params
!= NULL
)
991 return without_params
;
994 /* Here are some random pieces of trivia to keep in mind while trying
995 to take apart demangled names:
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 ().
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.
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
1010 - Parentheses are also used in anonymous namespaces: a variable
1011 'foo' in an anonymous namespace gets demangled as "(anonymous
1014 - And operator names can contain parentheses or angle brackets. */
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? */
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. */
1027 /* The character in NAME indexed by the return value is guaranteed to
1028 always be either ':' or '\0'. */
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
1037 cp_find_first_component (const char *name
)
1039 return cp_find_first_component_aux (name
, 0);
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. */
1048 cp_find_first_component_aux (const char *name
, int permissive
)
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;
1061 switch (name
[index
])
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'.) */
1069 for (index
+= cp_find_first_component_aux (name
+ index
, 1);
1071 index
+= cp_find_first_component_aux (name
+ index
, 1))
1073 if (name
[index
] != ':')
1075 demangled_name_complaint (name
);
1076 return strlen (name
);
1080 operator_possible
= 1;
1083 /* Similar comment as to '<'. */
1085 for (index
+= cp_find_first_component_aux (name
+ index
, 1);
1087 index
+= cp_find_first_component_aux (name
+ index
, 1))
1089 if (name
[index
] != ':')
1091 demangled_name_complaint (name
);
1092 return strlen (name
);
1096 operator_possible
= 1;
1104 demangled_name_complaint (name
);
1105 return strlen (name
);
1110 /* ':' marks a component iff the next character is also a ':'.
1111 Otherwise it is probably malformed input. */
1112 if (name
[index
+ 1] == ':')
1116 /* Operator names can screw up the recursion. */
1117 if (operator_possible
1118 && startswith (name
+ index
, CP_OPERATOR_STR
))
1120 index
+= CP_OPERATOR_LEN
;
1121 while (ISSPACE(name
[index
]))
1123 switch (name
[index
])
1127 /* Skip over one less than the appropriate number of
1128 characters: the for loop will skip over the last
1131 if (name
[index
+ 1] == '<')
1138 if (name
[index
+ 1] == '>')
1151 operator_possible
= 0;
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;
1166 operator_possible
= 0;
1172 /* Complain about a demangled name that we don't know how to parse.
1173 NAME is the demangled name in question. */
1176 demangled_name_complaint (const char *name
)
1178 complaint ("unexpected demangled name '%s'", name
);
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. */
1188 cp_entire_prefix_len (const char *name
)
1190 unsigned int current_len
= cp_find_first_component (name
);
1191 unsigned int previous_len
= 0;
1193 while (name
[current_len
] != '\0')
1195 gdb_assert (name
[current_len
] == ':');
1196 previous_len
= current_len
;
1197 /* Skip the '::'. */
1199 current_len
+= cp_find_first_component (name
+ current_len
);
1202 return previous_len
;
1205 /* Overload resolution functions. */
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
1212 overload_list_add_symbol (struct symbol
*sym
,
1213 const char *oload_name
,
1214 std::vector
<symbol
*> *overload_list
)
1216 /* If there is no type information, we can't do anything, so
1218 if (SYMBOL_TYPE (sym
) == NULL
)
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)
1226 /* Get the demangled name without parameters */
1227 gdb::unique_xmalloc_ptr
<char> sym_name
1228 = cp_remove_params (sym
->natural_name ());
1232 /* skip symbols that cannot match */
1233 if (strcmp (sym_name
.get (), oload_name
) != 0)
1236 overload_list
->push_back (sym
);
1239 /* Return a null-terminated list of pointers to function symbols that
1240 are named FUNC_NAME and are visible within NAMESPACE. */
1242 struct std::vector
<symbol
*>
1243 make_symbol_overload_list (const char *func_name
,
1244 const char *the_namespace
)
1247 std::vector
<symbol
*> overload_list
;
1249 overload_list
.reserve (100);
1251 add_symbol_overload_list_using (func_name
, the_namespace
, &overload_list
);
1253 if (the_namespace
[0] == '\0')
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
;
1265 add_symbol_overload_list_qualified (name
, &overload_list
);
1266 return overload_list
;
1269 /* Add all symbols with a name matching NAME in BLOCK to the overload
1273 add_symbol_overload_list_block (const char *name
,
1274 const struct block
*block
,
1275 std::vector
<symbol
*> *overload_list
)
1277 struct block_iterator iter
;
1280 lookup_name_info
lookup_name (name
, symbol_name_match_type::FULL
);
1282 ALL_BLOCK_SYMBOLS_WITH_NAME (block
, lookup_name
, iter
, sym
)
1283 overload_list_add_symbol (sym
, name
, overload_list
);
1286 /* Adds the function FUNC_NAME from NAMESPACE to the overload set. */
1289 add_symbol_overload_list_namespace (const char *func_name
,
1290 const char *the_namespace
,
1291 std::vector
<symbol
*> *overload_list
)
1294 const struct block
*block
= NULL
;
1296 if (the_namespace
[0] == '\0')
1300 char *concatenated_name
1301 = (char *) alloca (strlen (the_namespace
) + 2 + strlen (func_name
) + 1);
1303 strcpy (concatenated_name
, the_namespace
);
1304 strcat (concatenated_name
, "::");
1305 strcat (concatenated_name
, func_name
);
1306 name
= concatenated_name
;
1309 /* Look in the static block. */
1310 block
= block_static_block (get_selected_block (0));
1312 add_symbol_overload_list_block (name
, block
, overload_list
);
1314 /* Look in the global block. */
1315 block
= block_global_block (block
);
1317 add_symbol_overload_list_block (name
, block
, overload_list
);
1321 /* Search the namespace of the given type and namespace of and public
1325 add_symbol_overload_list_adl_namespace (struct type
*type
,
1326 const char *func_name
,
1327 std::vector
<symbol
*> *overload_list
)
1329 char *the_namespace
;
1330 const char *type_name
;
1333 while (type
->is_pointer_or_reference ()
1334 || type
->code () == TYPE_CODE_ARRAY
1335 || type
->code () == TYPE_CODE_TYPEDEF
)
1337 if (type
->code () == TYPE_CODE_TYPEDEF
)
1338 type
= check_typedef (type
);
1340 type
= TYPE_TARGET_TYPE (type
);
1343 type_name
= type
->name ();
1345 if (type_name
== NULL
)
1348 prefix_len
= cp_entire_prefix_len (type_name
);
1350 if (prefix_len
!= 0)
1352 the_namespace
= (char *) alloca (prefix_len
+ 1);
1353 strncpy (the_namespace
, type_name
, prefix_len
);
1354 the_namespace
[prefix_len
] = '\0';
1356 add_symbol_overload_list_namespace (func_name
, the_namespace
,
1360 /* Check public base type */
1361 if (type
->code () == TYPE_CODE_STRUCT
)
1362 for (i
= 0; i
< TYPE_N_BASECLASSES (type
); i
++)
1364 if (BASETYPE_VIA_PUBLIC (type
, i
))
1365 add_symbol_overload_list_adl_namespace (TYPE_BASECLASS (type
, i
),
1371 /* Adds to OVERLOAD_LIST the overload list overload candidates for
1372 FUNC_NAME found through argument dependent lookup. */
1375 add_symbol_overload_list_adl (gdb::array_view
<type
*> arg_types
,
1376 const char *func_name
,
1377 std::vector
<symbol
*> *overload_list
)
1379 for (type
*arg_type
: arg_types
)
1380 add_symbol_overload_list_adl_namespace (arg_type
, func_name
,
1384 /* This applies the using directives to add namespaces to search in,
1385 and then searches for overloads in all of those namespaces. It
1386 adds the symbols found to sym_return_val. Arguments are as in
1387 make_symbol_overload_list. */
1390 add_symbol_overload_list_using (const char *func_name
,
1391 const char *the_namespace
,
1392 std::vector
<symbol
*> *overload_list
)
1394 struct using_direct
*current
;
1395 const struct block
*block
;
1397 /* First, go through the using directives. If any of them apply,
1398 look in the appropriate namespaces for new functions to match
1401 for (block
= get_selected_block (0);
1403 block
= BLOCK_SUPERBLOCK (block
))
1404 for (current
= block_using (block
);
1406 current
= current
->next
)
1408 /* Prevent recursive calls. */
1409 if (current
->searched
)
1412 /* If this is a namespace alias or imported declaration ignore
1414 if (current
->alias
!= NULL
|| current
->declaration
!= NULL
)
1417 if (strcmp (the_namespace
, current
->import_dest
) == 0)
1419 /* Mark this import as searched so that the recursive call
1420 does not search it again. */
1421 scoped_restore reset_directive_searched
1422 = make_scoped_restore (¤t
->searched
, 1);
1424 add_symbol_overload_list_using (func_name
,
1425 current
->import_src
,
1430 /* Now, add names for this namespace. */
1431 add_symbol_overload_list_namespace (func_name
, the_namespace
,
1435 /* This does the bulk of the work of finding overloaded symbols.
1436 FUNC_NAME is the name of the overloaded function we're looking for
1437 (possibly including namespace info). */
1440 add_symbol_overload_list_qualified (const char *func_name
,
1441 std::vector
<symbol
*> *overload_list
)
1443 const struct block
*b
, *surrounding_static_block
= 0;
1445 /* Look through the partial symtabs for all symbols which begin by
1446 matching FUNC_NAME. Make sure we read that symbol table in. */
1448 for (objfile
*objf
: current_program_space
->objfiles ())
1449 objf
->expand_symtabs_for_function (func_name
);
1451 /* Search upwards from currently selected frame (so that we can
1452 complete on local vars. */
1454 for (b
= get_selected_block (0); b
!= NULL
; b
= BLOCK_SUPERBLOCK (b
))
1455 add_symbol_overload_list_block (func_name
, b
, overload_list
);
1457 surrounding_static_block
= block_static_block (get_selected_block (0));
1459 /* Go through the symtabs and check the externs and statics for
1460 symbols which match. */
1462 for (objfile
*objfile
: current_program_space
->objfiles ())
1464 for (compunit_symtab
*cust
: objfile
->compunits ())
1467 b
= BLOCKVECTOR_BLOCK (COMPUNIT_BLOCKVECTOR (cust
), GLOBAL_BLOCK
);
1468 add_symbol_overload_list_block (func_name
, b
, overload_list
);
1472 for (objfile
*objfile
: current_program_space
->objfiles ())
1474 for (compunit_symtab
*cust
: objfile
->compunits ())
1477 b
= BLOCKVECTOR_BLOCK (COMPUNIT_BLOCKVECTOR (cust
), STATIC_BLOCK
);
1478 /* Don't do this block twice. */
1479 if (b
== surrounding_static_block
)
1481 add_symbol_overload_list_block (func_name
, b
, overload_list
);
1486 /* Lookup the rtti type for a class name. */
1489 cp_lookup_rtti_type (const char *name
, const struct block
*block
)
1491 struct symbol
* rtti_sym
;
1492 struct type
* rtti_type
;
1494 /* Use VAR_DOMAIN here as NAME may be a typedef. PR 18141, 18417.
1495 Classes "live" in both STRUCT_DOMAIN and VAR_DOMAIN. */
1496 rtti_sym
= lookup_symbol (name
, block
, VAR_DOMAIN
, NULL
).symbol
;
1498 if (rtti_sym
== NULL
)
1500 warning (_("RTTI symbol not found for class '%s'"), name
);
1504 if (SYMBOL_CLASS (rtti_sym
) != LOC_TYPEDEF
)
1506 warning (_("RTTI symbol for class '%s' is not a type"), name
);
1510 rtti_type
= check_typedef (SYMBOL_TYPE (rtti_sym
));
1512 switch (rtti_type
->code ())
1514 case TYPE_CODE_STRUCT
:
1516 case TYPE_CODE_NAMESPACE
:
1517 /* chastain/2003-11-26: the symbol tables often contain fake
1518 symbols for namespaces with the same name as the struct.
1519 This warning is an indication of a bug in the lookup order
1520 or a bug in the way that the symbol tables are populated. */
1521 warning (_("RTTI symbol for class '%s' is a namespace"), name
);
1524 warning (_("RTTI symbol for class '%s' has bad type"), name
);
1531 #ifdef HAVE_WORKING_FORK
1533 /* If true, attempt to catch crashes in the demangler and print
1534 useful debugging information. */
1536 static bool catch_demangler_crashes
= true;
1538 /* Stack context and environment for demangler crash recovery. */
1540 static thread_local SIGJMP_BUF
*gdb_demangle_jmp_buf
;
1542 /* If true, attempt to dump core from the signal handler. */
1544 static std::atomic
<bool> gdb_demangle_attempt_core_dump
;
1546 /* Signal handler for gdb_demangle. */
1549 gdb_demangle_signal_handler (int signo
)
1551 if (gdb_demangle_attempt_core_dump
)
1556 gdb_demangle_attempt_core_dump
= false;
1559 SIGLONGJMP (*gdb_demangle_jmp_buf
, signo
);
1562 /* A helper for gdb_demangle that reports a demangling failure. */
1565 report_failed_demangle (const char *name
, bool core_dump_allowed
,
1568 static bool error_reported
= false;
1570 if (!error_reported
)
1572 std::string short_msg
1573 = string_printf (_("unable to demangle '%s' "
1574 "(demangler failed with signal %d)"),
1575 name
, crash_signal
);
1577 std::string long_msg
1578 = string_printf ("%s:%d: %s: %s", __FILE__
, __LINE__
,
1579 "demangler-warning", short_msg
.c_str ());
1581 target_terminal::scoped_restore_terminal_state term_state
;
1582 target_terminal::ours_for_output ();
1585 if (core_dump_allowed
)
1586 fprintf_unfiltered (gdb_stderr
,
1587 _("%s\nAttempting to dump core.\n"),
1590 warn_cant_dump_core (long_msg
.c_str ());
1592 demangler_warning (__FILE__
, __LINE__
, "%s", short_msg
.c_str ());
1594 error_reported
= true;
1600 /* A wrapper for bfd_demangle. */
1602 gdb::unique_xmalloc_ptr
<char>
1603 gdb_demangle (const char *name
, int options
)
1605 gdb::unique_xmalloc_ptr
<char> result
;
1606 int crash_signal
= 0;
1608 #ifdef HAVE_WORKING_FORK
1609 scoped_segv_handler_restore restore_segv
1610 (catch_demangler_crashes
1611 ? gdb_demangle_signal_handler
1614 bool core_dump_allowed
= gdb_demangle_attempt_core_dump
;
1616 scoped_restore restore_jmp_buf
1617 = make_scoped_restore (&gdb_demangle_jmp_buf
, &jmp_buf);
1618 if (catch_demangler_crashes
)
1620 /* The signal handler may keep the signal blocked when we longjmp out
1621 of it. If we have sigprocmask, we can use it to unblock the signal
1622 afterwards and we can avoid the performance overhead of saving the
1623 signal mask just in case the signal gets triggered. Otherwise, just
1624 tell sigsetjmp to save the mask. */
1625 #ifdef HAVE_SIGPROCMASK
1626 crash_signal
= SIGSETJMP (*gdb_demangle_jmp_buf
, 0);
1628 crash_signal
= SIGSETJMP (*gdb_demangle_jmp_buf
, 1);
1633 if (crash_signal
== 0)
1634 result
.reset (bfd_demangle (NULL
, name
, options
));
1636 #ifdef HAVE_WORKING_FORK
1637 if (catch_demangler_crashes
)
1639 if (crash_signal
!= 0)
1641 #ifdef HAVE_SIGPROCMASK
1642 /* If we got the signal, SIGSEGV may still be blocked; restore it. */
1643 sigset_t segv_sig_set
;
1644 sigemptyset (&segv_sig_set
);
1645 sigaddset (&segv_sig_set
, SIGSEGV
);
1646 gdb_sigmask (SIG_UNBLOCK
, &segv_sig_set
, NULL
);
1649 /* If there was a failure, we can't report it here, because
1650 we might be in a background thread. Instead, arrange for
1651 the reporting to happen on the main thread. */
1652 std::string copy
= name
;
1653 run_on_main_thread ([=] ()
1655 report_failed_demangle (copy
.c_str (), core_dump_allowed
,
1667 /* See cp-support.h. */
1670 cp_search_name_hash (const char *search_name
)
1672 /* cp_entire_prefix_len assumes a fully-qualified name with no
1674 if (startswith (search_name
, "::"))
1677 unsigned int prefix_len
= cp_entire_prefix_len (search_name
);
1678 if (prefix_len
!= 0)
1679 search_name
+= prefix_len
+ 2;
1681 unsigned int hash
= 0;
1682 for (const char *string
= search_name
; *string
!= '\0'; ++string
)
1684 string
= skip_spaces (string
);
1689 /* Ignore ABI tags such as "[abi:cxx11]. */
1691 && startswith (string
+ 1, "abi:")
1692 && string
[5] != ':')
1695 hash
= SYMBOL_HASH_NEXT (hash
, *string
);
1700 /* Helper for cp_symbol_name_matches (i.e., symbol_name_matcher_ftype
1701 implementation for symbol_name_match_type::WILD matching). Split
1702 to a separate function for unit-testing convenience.
1704 If SYMBOL_SEARCH_NAME has more scopes than LOOKUP_NAME, we try to
1705 match ignoring the extra leading scopes of SYMBOL_SEARCH_NAME.
1706 This allows conveniently setting breakpoints on functions/methods
1707 inside any namespace/class without specifying the fully-qualified
1712 [symbol search name] [lookup name]
1713 foo::bar::func foo::bar::func
1714 foo::bar::func bar::func
1719 [symbol search name] [lookup name]
1720 foo::zbar::func bar::func
1721 foo::bar::func foo::func
1723 See more examples in the test_cp_symbol_name_matches selftest
1726 See symbol_name_matcher_ftype for description of SYMBOL_SEARCH_NAME
1729 LOOKUP_NAME/LOOKUP_NAME_LEN is the name we're looking up.
1731 See strncmp_iw_with_mode for description of MODE.
1735 cp_symbol_name_matches_1 (const char *symbol_search_name
,
1736 const char *lookup_name
,
1737 size_t lookup_name_len
,
1738 strncmp_iw_mode mode
,
1739 completion_match_result
*comp_match_res
)
1741 const char *sname
= symbol_search_name
;
1742 completion_match_for_lcd
*match_for_lcd
1743 = (comp_match_res
!= NULL
? &comp_match_res
->match_for_lcd
: NULL
);
1747 if (strncmp_iw_with_mode (sname
, lookup_name
, lookup_name_len
,
1748 mode
, language_cplus
, match_for_lcd
) == 0)
1750 if (comp_match_res
!= NULL
)
1752 /* Note here we set different MATCH and MATCH_FOR_LCD
1753 strings. This is because with
1755 (gdb) b push_bac[TAB]
1757 we want the completion matches to list
1759 std::vector<int>::push_back(...)
1760 std::vector<char>::push_back(...)
1762 etc., which are SYMBOL_SEARCH_NAMEs, while we want
1763 the input line to auto-complete to
1765 (gdb) push_back(...)
1767 which is SNAME, not to
1771 which would be the regular common prefix between all
1772 the matches otherwise. */
1773 comp_match_res
->set_match (symbol_search_name
, sname
);
1778 unsigned int len
= cp_find_first_component (sname
);
1780 if (sname
[len
] == '\0')
1783 gdb_assert (sname
[len
] == ':');
1784 /* Skip the '::'. */
1789 /* C++ symbol_name_matcher_ftype implementation. */
1792 cp_fq_symbol_name_matches (const char *symbol_search_name
,
1793 const lookup_name_info
&lookup_name
,
1794 completion_match_result
*comp_match_res
)
1796 /* Get the demangled name. */
1797 const std::string
&name
= lookup_name
.cplus ().lookup_name ();
1798 completion_match_for_lcd
*match_for_lcd
1799 = (comp_match_res
!= NULL
? &comp_match_res
->match_for_lcd
: NULL
);
1800 strncmp_iw_mode mode
= (lookup_name
.completion_mode ()
1801 ? strncmp_iw_mode::NORMAL
1802 : strncmp_iw_mode::MATCH_PARAMS
);
1804 if (strncmp_iw_with_mode (symbol_search_name
,
1805 name
.c_str (), name
.size (),
1806 mode
, language_cplus
, match_for_lcd
) == 0)
1808 if (comp_match_res
!= NULL
)
1809 comp_match_res
->set_match (symbol_search_name
);
1816 /* C++ symbol_name_matcher_ftype implementation for wild matches.
1817 Defers work to cp_symbol_name_matches_1. */
1820 cp_symbol_name_matches (const char *symbol_search_name
,
1821 const lookup_name_info
&lookup_name
,
1822 completion_match_result
*comp_match_res
)
1824 /* Get the demangled name. */
1825 const std::string
&name
= lookup_name
.cplus ().lookup_name ();
1827 strncmp_iw_mode mode
= (lookup_name
.completion_mode ()
1828 ? strncmp_iw_mode::NORMAL
1829 : strncmp_iw_mode::MATCH_PARAMS
);
1831 return cp_symbol_name_matches_1 (symbol_search_name
,
1832 name
.c_str (), name
.size (),
1833 mode
, comp_match_res
);
1836 /* See cp-support.h. */
1838 symbol_name_matcher_ftype
*
1839 cp_get_symbol_name_matcher (const lookup_name_info
&lookup_name
)
1841 switch (lookup_name
.match_type ())
1843 case symbol_name_match_type::FULL
:
1844 case symbol_name_match_type::EXPRESSION
:
1845 case symbol_name_match_type::SEARCH_NAME
:
1846 return cp_fq_symbol_name_matches
;
1847 case symbol_name_match_type::WILD
:
1848 return cp_symbol_name_matches
;
1851 gdb_assert_not_reached ("");
1856 namespace selftests
{
1859 test_cp_symbol_name_matches ()
1861 #define CHECK_MATCH(SYMBOL, INPUT) \
1862 SELF_CHECK (cp_symbol_name_matches_1 (SYMBOL, \
1863 INPUT, sizeof (INPUT) - 1, \
1864 strncmp_iw_mode::MATCH_PARAMS, \
1867 #define CHECK_NOT_MATCH(SYMBOL, INPUT) \
1868 SELF_CHECK (!cp_symbol_name_matches_1 (SYMBOL, \
1869 INPUT, sizeof (INPUT) - 1, \
1870 strncmp_iw_mode::MATCH_PARAMS, \
1873 /* Like CHECK_MATCH, and also check that INPUT (and all substrings
1874 that start at index 0) completes to SYMBOL. */
1875 #define CHECK_MATCH_C(SYMBOL, INPUT) \
1878 CHECK_MATCH (SYMBOL, INPUT); \
1879 for (size_t i = 0; i < sizeof (INPUT) - 1; i++) \
1880 SELF_CHECK (cp_symbol_name_matches_1 (SYMBOL, INPUT, i, \
1881 strncmp_iw_mode::NORMAL, \
1885 /* Like CHECK_NOT_MATCH, and also check that INPUT does NOT complete
1887 #define CHECK_NOT_MATCH_C(SYMBOL, INPUT) \
1890 CHECK_NOT_MATCH (SYMBOL, INPUT); \
1891 SELF_CHECK (!cp_symbol_name_matches_1 (SYMBOL, INPUT, \
1892 sizeof (INPUT) - 1, \
1893 strncmp_iw_mode::NORMAL, \
1897 /* Lookup name without parens matches all overloads. */
1898 CHECK_MATCH_C ("function()", "function");
1899 CHECK_MATCH_C ("function(int)", "function");
1901 /* Check whitespace around parameters is ignored. */
1902 CHECK_MATCH_C ("function()", "function ()");
1903 CHECK_MATCH_C ("function ( )", "function()");
1904 CHECK_MATCH_C ("function ()", "function( )");
1905 CHECK_MATCH_C ("func(int)", "func( int )");
1906 CHECK_MATCH_C ("func(int)", "func ( int ) ");
1907 CHECK_MATCH_C ("func ( int )", "func( int )");
1908 CHECK_MATCH_C ("func ( int )", "func ( int ) ");
1910 /* Check symbol name prefixes aren't incorrectly matched. */
1911 CHECK_NOT_MATCH ("func", "function");
1912 CHECK_NOT_MATCH ("function", "func");
1913 CHECK_NOT_MATCH ("function()", "func");
1915 /* Check that if the lookup name includes parameters, only the right
1916 overload matches. */
1917 CHECK_MATCH_C ("function(int)", "function(int)");
1918 CHECK_NOT_MATCH_C ("function(int)", "function()");
1920 /* Check that whitespace within symbol names is not ignored. */
1921 CHECK_NOT_MATCH_C ("function", "func tion");
1922 CHECK_NOT_MATCH_C ("func__tion", "func_ _tion");
1923 CHECK_NOT_MATCH_C ("func11tion", "func1 1tion");
1925 /* Check the converse, which can happen with template function,
1926 where the return type is part of the demangled name. */
1927 CHECK_NOT_MATCH_C ("func tion", "function");
1928 CHECK_NOT_MATCH_C ("func1 1tion", "func11tion");
1929 CHECK_NOT_MATCH_C ("func_ _tion", "func__tion");
1931 /* Within parameters too. */
1932 CHECK_NOT_MATCH_C ("func(param)", "func(par am)");
1934 /* Check handling of whitespace around C++ operators. */
1935 CHECK_NOT_MATCH_C ("operator<<", "opera tor<<");
1936 CHECK_NOT_MATCH_C ("operator<<", "operator< <");
1937 CHECK_NOT_MATCH_C ("operator<<", "operator < <");
1938 CHECK_NOT_MATCH_C ("operator==", "operator= =");
1939 CHECK_NOT_MATCH_C ("operator==", "operator = =");
1940 CHECK_MATCH_C ("operator<<", "operator <<");
1941 CHECK_MATCH_C ("operator<<()", "operator <<");
1942 CHECK_NOT_MATCH_C ("operator<<()", "operator<<(int)");
1943 CHECK_NOT_MATCH_C ("operator<<(int)", "operator<<()");
1944 CHECK_MATCH_C ("operator==", "operator ==");
1945 CHECK_MATCH_C ("operator==()", "operator ==");
1946 CHECK_MATCH_C ("operator <<", "operator<<");
1947 CHECK_MATCH_C ("operator ==", "operator==");
1948 CHECK_MATCH_C ("operator bool", "operator bool");
1949 CHECK_MATCH_C ("operator bool ()", "operator bool");
1950 CHECK_MATCH_C ("operatorX<<", "operatorX < <");
1951 CHECK_MATCH_C ("Xoperator<<", "Xoperator < <");
1953 CHECK_MATCH_C ("operator()(int)", "operator()(int)");
1954 CHECK_MATCH_C ("operator()(int)", "operator ( ) ( int )");
1955 CHECK_MATCH_C ("operator()<long>(int)", "operator ( ) < long > ( int )");
1956 /* The first "()" is not the parameter list. */
1957 CHECK_NOT_MATCH ("operator()(int)", "operator");
1959 /* Misc user-defined operator tests. */
1961 CHECK_NOT_MATCH_C ("operator/=()", "operator ^=");
1962 /* Same length at end of input. */
1963 CHECK_NOT_MATCH_C ("operator>>", "operator[]");
1964 /* Same length but not at end of input. */
1965 CHECK_NOT_MATCH_C ("operator>>()", "operator[]()");
1967 CHECK_MATCH_C ("base::operator char*()", "base::operator char*()");
1968 CHECK_MATCH_C ("base::operator char*()", "base::operator char * ()");
1969 CHECK_MATCH_C ("base::operator char**()", "base::operator char * * ()");
1970 CHECK_MATCH ("base::operator char**()", "base::operator char * *");
1971 CHECK_MATCH_C ("base::operator*()", "base::operator*()");
1972 CHECK_NOT_MATCH_C ("base::operator char*()", "base::operatorc");
1973 CHECK_NOT_MATCH ("base::operator char*()", "base::operator char");
1974 CHECK_NOT_MATCH ("base::operator char*()", "base::operat");
1976 /* Check handling of whitespace around C++ scope operators. */
1977 CHECK_NOT_MATCH_C ("foo::bar", "foo: :bar");
1978 CHECK_MATCH_C ("foo::bar", "foo :: bar");
1979 CHECK_MATCH_C ("foo :: bar", "foo::bar");
1981 CHECK_MATCH_C ("abc::def::ghi()", "abc::def::ghi()");
1982 CHECK_MATCH_C ("abc::def::ghi ( )", "abc::def::ghi()");
1983 CHECK_MATCH_C ("abc::def::ghi()", "abc::def::ghi ( )");
1984 CHECK_MATCH_C ("function()", "function()");
1985 CHECK_MATCH_C ("bar::function()", "bar::function()");
1987 /* Wild matching tests follow. */
1989 /* Tests matching symbols in some scope. */
1990 CHECK_MATCH_C ("foo::function()", "function");
1991 CHECK_MATCH_C ("foo::function(int)", "function");
1992 CHECK_MATCH_C ("foo::bar::function()", "function");
1993 CHECK_MATCH_C ("bar::function()", "bar::function");
1994 CHECK_MATCH_C ("foo::bar::function()", "bar::function");
1995 CHECK_MATCH_C ("foo::bar::function(int)", "bar::function");
1997 /* Same, with parameters in the lookup name. */
1998 CHECK_MATCH_C ("foo::function()", "function()");
1999 CHECK_MATCH_C ("foo::bar::function()", "function()");
2000 CHECK_MATCH_C ("foo::function(int)", "function(int)");
2001 CHECK_MATCH_C ("foo::function()", "foo::function()");
2002 CHECK_MATCH_C ("foo::bar::function()", "bar::function()");
2003 CHECK_MATCH_C ("foo::bar::function(int)", "bar::function(int)");
2004 CHECK_MATCH_C ("bar::function()", "bar::function()");
2006 CHECK_NOT_MATCH_C ("foo::bar::function(int)", "bar::function()");
2008 CHECK_MATCH_C ("(anonymous namespace)::bar::function(int)",
2009 "bar::function(int)");
2010 CHECK_MATCH_C ("foo::(anonymous namespace)::bar::function(int)",
2013 /* Lookup scope wider than symbol scope, should not match. */
2014 CHECK_NOT_MATCH_C ("function()", "bar::function");
2015 CHECK_NOT_MATCH_C ("function()", "bar::function()");
2017 /* Explicit global scope doesn't match. */
2018 CHECK_NOT_MATCH_C ("foo::function()", "::function");
2019 CHECK_NOT_MATCH_C ("foo::function()", "::function()");
2020 CHECK_NOT_MATCH_C ("foo::function(int)", "::function()");
2021 CHECK_NOT_MATCH_C ("foo::function(int)", "::function(int)");
2023 /* Test ABI tag matching/ignoring. */
2025 /* If the symbol name has an ABI tag, but the lookup name doesn't,
2026 then the ABI tag in the symbol name is ignored. */
2027 CHECK_MATCH_C ("function[abi:foo]()", "function");
2028 CHECK_MATCH_C ("function[abi:foo](int)", "function");
2029 CHECK_MATCH_C ("function[abi:foo]()", "function ()");
2030 CHECK_NOT_MATCH_C ("function[abi:foo]()", "function (int)");
2032 CHECK_MATCH_C ("function[abi:foo]()", "function[abi:foo]");
2033 CHECK_MATCH_C ("function[abi:foo](int)", "function[abi:foo]");
2034 CHECK_MATCH_C ("function[abi:foo]()", "function[abi:foo] ()");
2035 CHECK_MATCH_C ("function[abi:foo][abi:bar]()", "function");
2036 CHECK_MATCH_C ("function[abi:foo][abi:bar](int)", "function");
2037 CHECK_MATCH_C ("function[abi:foo][abi:bar]()", "function[abi:foo]");
2038 CHECK_MATCH_C ("function[abi:foo][abi:bar](int)", "function[abi:foo]");
2039 CHECK_MATCH_C ("function[abi:foo][abi:bar]()", "function[abi:foo] ()");
2040 CHECK_NOT_MATCH_C ("function[abi:foo][abi:bar]()", "function[abi:foo] (int)");
2042 CHECK_MATCH_C ("function [abi:foo][abi:bar] ( )", "function [abi:foo]");
2044 /* If the symbol name does not have an ABI tag, while the lookup
2045 name has one, then there's no match. */
2046 CHECK_NOT_MATCH_C ("function()", "function[abi:foo]()");
2047 CHECK_NOT_MATCH_C ("function()", "function[abi:foo]");
2050 /* If non-NULL, return STR wrapped in quotes. Otherwise, return a
2051 "<null>" string (with no quotes). */
2054 quote (const char *str
)
2057 return std::string (1, '"') + str
+ '"';
2062 /* Check that removing parameter info out of NAME produces EXPECTED.
2063 COMPLETION_MODE indicates whether we're testing normal and
2064 completion mode. FILE and LINE are used to provide better test
2065 location information in case ithe check fails. */
2068 check_remove_params (const char *file
, int line
,
2069 const char *name
, const char *expected
,
2070 bool completion_mode
)
2072 gdb::unique_xmalloc_ptr
<char> result
2073 = cp_remove_params_if_any (name
, completion_mode
);
2075 if ((expected
== NULL
) != (result
== NULL
)
2076 || (expected
!= NULL
2077 && strcmp (result
.get (), expected
) != 0))
2079 error (_("%s:%d: make-paramless self-test failed: (completion=%d) "
2080 "\"%s\" -> %s, expected %s"),
2081 file
, line
, completion_mode
, name
,
2082 quote (result
.get ()).c_str (), quote (expected
).c_str ());
2086 /* Entry point for cp_remove_params unit tests. */
2089 test_cp_remove_params ()
2091 /* Check that removing parameter info out of NAME produces EXPECTED.
2092 Checks both normal and completion modes. */
2093 #define CHECK(NAME, EXPECTED) \
2096 check_remove_params (__FILE__, __LINE__, NAME, EXPECTED, false); \
2097 check_remove_params (__FILE__, __LINE__, NAME, EXPECTED, true); \
2101 /* Similar, but used when NAME is incomplete -- i.e., is has
2102 unbalanced parentheses. In this case, looking for the exact name
2103 should fail / return empty. */
2104 #define CHECK_INCOMPL(NAME, EXPECTED) \
2107 check_remove_params (__FILE__, __LINE__, NAME, NULL, false); \
2108 check_remove_params (__FILE__, __LINE__, NAME, EXPECTED, true); \
2112 CHECK ("function()", "function");
2113 CHECK_INCOMPL ("function(", "function");
2114 CHECK ("function() const", "function");
2116 CHECK ("(anonymous namespace)::A::B::C",
2117 "(anonymous namespace)::A::B::C");
2119 CHECK ("A::(anonymous namespace)",
2120 "A::(anonymous namespace)");
2122 CHECK_INCOMPL ("A::(anonymou", "A");
2124 CHECK ("A::foo<int>()",
2127 CHECK_INCOMPL ("A::foo<int>(",
2130 CHECK ("A::foo<(anonymous namespace)::B>::func(int)",
2131 "A::foo<(anonymous namespace)::B>::func");
2133 CHECK_INCOMPL ("A::foo<(anonymous namespace)::B>::func(in",
2134 "A::foo<(anonymous namespace)::B>::func");
2136 CHECK_INCOMPL ("A::foo<(anonymous namespace)::B>::",
2137 "A::foo<(anonymous namespace)::B>");
2139 CHECK_INCOMPL ("A::foo<(anonymous namespace)::B>:",
2140 "A::foo<(anonymous namespace)::B>");
2142 CHECK ("A::foo<(anonymous namespace)::B>",
2143 "A::foo<(anonymous namespace)::B>");
2145 CHECK_INCOMPL ("A::foo<(anonymous namespace)::B",
2148 /* Shouldn't this parse? Looks like a bug in
2149 cp_demangled_name_to_comp. See PR c++/22411. */
2151 CHECK ("A::foo<void(int)>::func(int)",
2152 "A::foo<void(int)>::func");
2154 CHECK_INCOMPL ("A::foo<void(int)>::func(int)",
2158 CHECK_INCOMPL ("A::foo<void(int",
2162 #undef CHECK_INCOMPL
2165 } // namespace selftests
2167 #endif /* GDB_SELF_CHECK */
2169 /* This is a front end for cp_find_first_component, for unit testing.
2170 Be careful when using it: see the NOTE above
2171 cp_find_first_component. */
2174 first_component_command (const char *arg
, int from_tty
)
2182 len
= cp_find_first_component (arg
);
2183 prefix
= (char *) alloca (len
+ 1);
2185 memcpy (prefix
, arg
, len
);
2188 printf_unfiltered ("%s\n", prefix
);
2191 /* Implement "info vtbl". */
2194 info_vtbl_command (const char *arg
, int from_tty
)
2196 struct value
*value
;
2198 value
= parse_and_eval (arg
);
2199 cplus_print_vtable (value
);
2202 void _initialize_cp_support ();
2204 _initialize_cp_support ()
2206 cmd_list_element
*maintenance_cplus
2207 = add_basic_prefix_cmd ("cplus", class_maintenance
,
2208 _("C++ maintenance commands."),
2209 &maint_cplus_cmd_list
,
2210 0, &maintenancelist
);
2211 add_alias_cmd ("cp", maintenance_cplus
, class_maintenance
, 1,
2214 add_cmd ("first_component",
2216 first_component_command
,
2217 _("Print the first class/namespace component of NAME."),
2218 &maint_cplus_cmd_list
);
2220 add_info ("vtbl", info_vtbl_command
,
2221 _("Show the virtual function table for a C++ object.\n\
2222 Usage: info vtbl EXPRESSION\n\
2223 Evaluate EXPRESSION and display the virtual function table for the\n\
2224 resulting object."));
2226 #ifdef HAVE_WORKING_FORK
2227 add_setshow_boolean_cmd ("catch-demangler-crashes", class_maintenance
,
2228 &catch_demangler_crashes
, _("\
2229 Set whether to attempt to catch demangler crashes."), _("\
2230 Show whether to attempt to catch demangler crashes."), _("\
2231 If enabled GDB will attempt to catch demangler crashes and\n\
2232 display the offending symbol."),
2235 &maintenance_set_cmdlist
,
2236 &maintenance_show_cmdlist
);
2238 gdb_demangle_attempt_core_dump
= can_dump_core (LIMIT_CUR
);
2242 selftests::register_test ("cp_symbol_name_matches",
2243 selftests::test_cp_symbol_name_matches
);
2244 selftests::register_test ("cp_remove_params",
2245 selftests::test_cp_remove_params
);