1 /* Helper routines for C++ support in GDB.
2 Copyright (C) 2002-2019 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"
41 #define d_left(dc) (dc)->u.s_binary.left
42 #define d_right(dc) (dc)->u.s_binary.right
44 /* Functions related to demangled name parsing. */
46 static unsigned int cp_find_first_component_aux (const char *name,
49 static void demangled_name_complaint (const char *name);
51 /* Functions related to overload resolution. */
53 static void overload_list_add_symbol (struct symbol *sym,
54 const char *oload_name,
55 std::vector<symbol *> *overload_list);
57 static void add_symbol_overload_list_using
58 (const char *func_name, const char *the_namespace,
59 std::vector<symbol *> *overload_list);
61 static void add_symbol_overload_list_qualified
62 (const char *func_name,
63 std::vector<symbol *> *overload_list);
65 /* The list of "maint cplus" commands. */
67 struct cmd_list_element *maint_cplus_cmd_list = NULL;
69 /* A list of typedefs which should not be substituted by replace_typedefs. */
70 static const char * const ignore_typedefs[] =
72 "std::istream", "std::iostream", "std::ostream", "std::string"
76 replace_typedefs (struct demangle_parse_info *info,
77 struct demangle_component *ret_comp,
78 canonicalization_ftype *finder,
81 /* A convenience function to copy STRING into OBSTACK, returning a pointer
82 to the newly allocated string and saving the number of bytes saved in LEN.
84 It does not copy the terminating '\0' byte! */
87 copy_string_to_obstack (struct obstack *obstack, const char *string,
90 *len = strlen (string);
91 return (char *) obstack_copy (obstack, string, *len);
94 /* Return 1 if STRING is clearly already in canonical form. This
95 function is conservative; things which it does not recognize are
96 assumed to be non-canonical, and the parser will sort them out
97 afterwards. This speeds up the critical path for alphanumeric
101 cp_already_canonical (const char *string)
103 /* Identifier start character [a-zA-Z_]. */
104 if (!ISIDST (string[0]))
107 /* These are the only two identifiers which canonicalize to other
108 than themselves or an error: unsigned -> unsigned int and
110 if (string[0] == 'u' && strcmp (&string[1], "nsigned") == 0)
112 else if (string[0] == 's' && strcmp (&string[1], "igned") == 0)
115 /* Identifier character [a-zA-Z0-9_]. */
116 while (ISIDNUM (string[1]))
119 if (string[1] == '\0')
125 /* Inspect the given RET_COMP for its type. If it is a typedef,
126 replace the node with the typedef's tree.
128 Returns 1 if any typedef substitutions were made, 0 otherwise. */
131 inspect_type (struct demangle_parse_info *info,
132 struct demangle_component *ret_comp,
133 canonicalization_ftype *finder,
139 /* Copy the symbol's name from RET_COMP and look it up
140 in the symbol table. */
141 name = (char *) alloca (ret_comp->u.s_name.len + 1);
142 memcpy (name, ret_comp->u.s_name.s, ret_comp->u.s_name.len);
143 name[ret_comp->u.s_name.len] = '\0';
145 /* Ignore any typedefs that should not be substituted. */
146 for (int i = 0; i < ARRAY_SIZE (ignore_typedefs); ++i)
148 if (strcmp (name, ignore_typedefs[i]) == 0)
156 sym = lookup_symbol (name, 0, VAR_DOMAIN, 0).symbol;
158 catch (const gdb_exception &except)
165 struct type *otype = SYMBOL_TYPE (sym);
169 const char *new_name = (*finder) (otype, data);
171 if (new_name != NULL)
173 ret_comp->u.s_name.s = new_name;
174 ret_comp->u.s_name.len = strlen (new_name);
181 /* If the type is a typedef or namespace alias, replace it. */
182 if (TYPE_CODE (otype) == TYPE_CODE_TYPEDEF
183 || TYPE_CODE (otype) == TYPE_CODE_NAMESPACE)
188 std::unique_ptr<demangle_parse_info> i;
190 /* Get the real type of the typedef. */
191 type = check_typedef (otype);
193 /* If the symbol name is the same as the original type name,
194 don't substitute. That would cause infinite recursion in
195 symbol lookups, as the typedef symbol is often the first
196 found symbol in the symbol table.
198 However, this can happen in a number of situations, such as:
200 If the symbol is a namespace and its type name is no different
201 than the name we looked up, this symbol is not a namespace
202 alias and does not need to be substituted.
204 If the symbol is typedef and its type name is the same
205 as the symbol's name, e.g., "typedef struct foo foo;". */
206 if (TYPE_NAME (type) != nullptr
207 && strcmp (TYPE_NAME (type), name) == 0)
210 is_anon = (TYPE_NAME (type) == NULL
211 && (TYPE_CODE (type) == TYPE_CODE_ENUM
212 || TYPE_CODE (type) == TYPE_CODE_STRUCT
213 || TYPE_CODE (type) == TYPE_CODE_UNION));
216 struct type *last = otype;
218 /* Find the last typedef for the type. */
219 while (TYPE_TARGET_TYPE (last) != NULL
220 && (TYPE_CODE (TYPE_TARGET_TYPE (last))
221 == TYPE_CODE_TYPEDEF))
222 last = TYPE_TARGET_TYPE (last);
224 /* If there is only one typedef for this anonymous type,
225 do not substitute it. */
229 /* Use the last typedef seen as the type for this
237 type_print (type, "", &buf, -1);
239 /* If type_print threw an exception, there is little point
240 in continuing, so just bow out gracefully. */
241 catch (const gdb_exception_error &except)
247 name = obstack_strdup (&info->obstack, buf.string ());
249 /* Turn the result into a new tree. Note that this
250 tree will contain pointers into NAME, so NAME cannot
251 be free'd until all typedef conversion is done and
252 the final result is converted into a string. */
253 i = cp_demangled_name_to_comp (name, NULL);
256 /* Merge the two trees. */
257 cp_merge_demangle_parse_infos (info, ret_comp, i.get ());
259 /* Replace any newly introduced typedefs -- but not
260 if the type is anonymous (that would lead to infinite
263 replace_typedefs (info, ret_comp, finder, data);
267 /* This shouldn't happen unless the type printer has
268 output something that the name parser cannot grok.
269 Nonetheless, an ounce of prevention...
271 Canonicalize the name again, and store it in the
272 current node (RET_COMP). */
273 std::string canon = cp_canonicalize_string_no_typedefs (name);
277 /* Copy the canonicalization into the obstack. */
278 name = copy_string_to_obstack (&info->obstack, canon.c_str (), &len);
281 ret_comp->u.s_name.s = name;
282 ret_comp->u.s_name.len = len;
292 /* Replace any typedefs appearing in the qualified name
293 (DEMANGLE_COMPONENT_QUAL_NAME) represented in RET_COMP for the name parse
297 replace_typedefs_qualified_name (struct demangle_parse_info *info,
298 struct demangle_component *ret_comp,
299 canonicalization_ftype *finder,
303 struct demangle_component *comp = ret_comp;
305 /* Walk each node of the qualified name, reconstructing the name of
306 this element. With every node, check for any typedef substitutions.
307 If a substitution has occurred, replace the qualified name node
308 with a DEMANGLE_COMPONENT_NAME node representing the new, typedef-
310 while (comp->type == DEMANGLE_COMPONENT_QUAL_NAME)
312 if (d_left (comp)->type == DEMANGLE_COMPONENT_NAME)
314 struct demangle_component newobj;
316 buf.write (d_left (comp)->u.s_name.s, d_left (comp)->u.s_name.len);
317 newobj.type = DEMANGLE_COMPONENT_NAME;
318 newobj.u.s_name.s = obstack_strdup (&info->obstack, buf.string ());
319 newobj.u.s_name.len = buf.size ();
320 if (inspect_type (info, &newobj, finder, data))
325 /* A typedef was substituted in NEW. Convert it to a
326 string and replace the top DEMANGLE_COMPONENT_QUAL_NAME
330 gdb::unique_xmalloc_ptr<char> n
331 = cp_comp_to_string (&newobj, 100);
334 /* If something went astray, abort typedef substitutions. */
338 s = copy_string_to_obstack (&info->obstack, n.get (), &slen);
340 d_left (ret_comp)->type = DEMANGLE_COMPONENT_NAME;
341 d_left (ret_comp)->u.s_name.s = s;
342 d_left (ret_comp)->u.s_name.len = slen;
343 d_right (ret_comp) = d_right (comp);
350 /* The current node is not a name, so simply replace any
351 typedefs in it. Then print it to the stream to continue
352 checking for more typedefs in the tree. */
353 replace_typedefs (info, d_left (comp), finder, data);
354 gdb::unique_xmalloc_ptr<char> name
355 = cp_comp_to_string (d_left (comp), 100);
358 /* If something went astray, abort typedef substitutions. */
361 buf.puts (name.get ());
365 comp = d_right (comp);
368 /* If the next component is DEMANGLE_COMPONENT_NAME, save the qualified
369 name assembled above and append the name given by COMP. Then use this
370 reassembled name to check for a typedef. */
372 if (comp->type == DEMANGLE_COMPONENT_NAME)
374 buf.write (comp->u.s_name.s, comp->u.s_name.len);
376 /* Replace the top (DEMANGLE_COMPONENT_QUAL_NAME) node
377 with a DEMANGLE_COMPONENT_NAME node containing the whole
379 ret_comp->type = DEMANGLE_COMPONENT_NAME;
380 ret_comp->u.s_name.s = obstack_strdup (&info->obstack, buf.string ());
381 ret_comp->u.s_name.len = buf.size ();
382 inspect_type (info, ret_comp, finder, data);
385 replace_typedefs (info, comp, finder, data);
389 /* A function to check const and volatile qualifiers for argument types.
391 "Parameter declarations that differ only in the presence
392 or absence of `const' and/or `volatile' are equivalent."
393 C++ Standard N3290, clause 13.1.3 #4. */
396 check_cv_qualifiers (struct demangle_component *ret_comp)
398 while (d_left (ret_comp) != NULL
399 && (d_left (ret_comp)->type == DEMANGLE_COMPONENT_CONST
400 || d_left (ret_comp)->type == DEMANGLE_COMPONENT_VOLATILE))
402 d_left (ret_comp) = d_left (d_left (ret_comp));
406 /* Walk the parse tree given by RET_COMP, replacing any typedefs with
407 their basic types. */
410 replace_typedefs (struct demangle_parse_info *info,
411 struct demangle_component *ret_comp,
412 canonicalization_ftype *finder,
418 && (ret_comp->type == DEMANGLE_COMPONENT_NAME
419 || ret_comp->type == DEMANGLE_COMPONENT_QUAL_NAME
420 || ret_comp->type == DEMANGLE_COMPONENT_TEMPLATE
421 || ret_comp->type == DEMANGLE_COMPONENT_BUILTIN_TYPE))
423 gdb::unique_xmalloc_ptr<char> local_name
424 = cp_comp_to_string (ret_comp, 10);
426 if (local_name != NULL)
428 struct symbol *sym = NULL;
433 sym = lookup_symbol (local_name.get (), 0,
434 VAR_DOMAIN, 0).symbol;
436 catch (const gdb_exception &except)
442 struct type *otype = SYMBOL_TYPE (sym);
443 const char *new_name = (*finder) (otype, data);
445 if (new_name != NULL)
447 ret_comp->type = DEMANGLE_COMPONENT_NAME;
448 ret_comp->u.s_name.s = new_name;
449 ret_comp->u.s_name.len = strlen (new_name);
456 switch (ret_comp->type)
458 case DEMANGLE_COMPONENT_ARGLIST:
459 check_cv_qualifiers (ret_comp);
462 case DEMANGLE_COMPONENT_FUNCTION_TYPE:
463 case DEMANGLE_COMPONENT_TEMPLATE:
464 case DEMANGLE_COMPONENT_TEMPLATE_ARGLIST:
465 case DEMANGLE_COMPONENT_TYPED_NAME:
466 replace_typedefs (info, d_left (ret_comp), finder, data);
467 replace_typedefs (info, d_right (ret_comp), finder, data);
470 case DEMANGLE_COMPONENT_NAME:
471 inspect_type (info, ret_comp, finder, data);
474 case DEMANGLE_COMPONENT_QUAL_NAME:
475 replace_typedefs_qualified_name (info, ret_comp, finder, data);
478 case DEMANGLE_COMPONENT_LOCAL_NAME:
479 case DEMANGLE_COMPONENT_CTOR:
480 case DEMANGLE_COMPONENT_ARRAY_TYPE:
481 case DEMANGLE_COMPONENT_PTRMEM_TYPE:
482 replace_typedefs (info, d_right (ret_comp), finder, data);
485 case DEMANGLE_COMPONENT_CONST:
486 case DEMANGLE_COMPONENT_RESTRICT:
487 case DEMANGLE_COMPONENT_VOLATILE:
488 case DEMANGLE_COMPONENT_VOLATILE_THIS:
489 case DEMANGLE_COMPONENT_CONST_THIS:
490 case DEMANGLE_COMPONENT_RESTRICT_THIS:
491 case DEMANGLE_COMPONENT_POINTER:
492 case DEMANGLE_COMPONENT_REFERENCE:
493 case DEMANGLE_COMPONENT_RVALUE_REFERENCE:
494 replace_typedefs (info, d_left (ret_comp), finder, data);
503 /* Parse STRING and convert it to canonical form, resolving any
504 typedefs. If parsing fails, or if STRING is already canonical,
505 return the empty string. Otherwise return the canonical form. If
506 FINDER is not NULL, then type components are passed to FINDER to be
507 looked up. DATA is passed verbatim to FINDER. */
510 cp_canonicalize_string_full (const char *string,
511 canonicalization_ftype *finder,
515 unsigned int estimated_len;
516 std::unique_ptr<demangle_parse_info> info;
518 estimated_len = strlen (string) * 2;
519 info = cp_demangled_name_to_comp (string, NULL);
522 /* Replace all the typedefs in the tree. */
523 replace_typedefs (info.get (), info->tree, finder, data);
525 /* Convert the tree back into a string. */
526 gdb::unique_xmalloc_ptr<char> us = cp_comp_to_string (info->tree,
531 /* Finally, compare the original string with the computed
532 name, returning NULL if they are the same. */
534 return std::string ();
540 /* Like cp_canonicalize_string_full, but always passes NULL for
544 cp_canonicalize_string_no_typedefs (const char *string)
546 return cp_canonicalize_string_full (string, NULL, NULL);
549 /* Parse STRING and convert it to canonical form. If parsing fails,
550 or if STRING is already canonical, return the empty string.
551 Otherwise return the canonical form. */
554 cp_canonicalize_string (const char *string)
556 std::unique_ptr<demangle_parse_info> info;
557 unsigned int estimated_len;
559 if (cp_already_canonical (string))
560 return std::string ();
562 info = cp_demangled_name_to_comp (string, NULL);
564 return std::string ();
566 estimated_len = strlen (string) * 2;
567 gdb::unique_xmalloc_ptr<char> us (cp_comp_to_string (info->tree,
572 warning (_("internal error: string \"%s\" failed to be canonicalized"),
574 return std::string ();
577 std::string ret (us.get ());
580 return std::string ();
585 /* Convert a mangled name to a demangle_component tree. *MEMORY is
586 set to the block of used memory that should be freed when finished
587 with the tree. DEMANGLED_P is set to the char * that should be
588 freed when finished with the tree, or NULL if none was needed.
589 OPTIONS will be passed to the demangler. */
591 static std::unique_ptr<demangle_parse_info>
592 mangled_name_to_comp (const char *mangled_name, int options,
593 void **memory, char **demangled_p)
595 char *demangled_name;
597 /* If it looks like a v3 mangled name, then try to go directly
599 if (mangled_name[0] == '_' && mangled_name[1] == 'Z')
601 struct demangle_component *ret;
603 ret = cplus_demangle_v3_components (mangled_name,
607 std::unique_ptr<demangle_parse_info> info (new demangle_parse_info);
614 /* If it doesn't, or if that failed, then try to demangle the
616 demangled_name = gdb_demangle (mangled_name, options);
617 if (demangled_name == NULL)
620 /* If we could demangle the name, parse it to build the component
622 std::unique_ptr<demangle_parse_info> info
623 = cp_demangled_name_to_comp (demangled_name, NULL);
627 xfree (demangled_name);
631 *demangled_p = demangled_name;
635 /* Return the name of the class containing method PHYSNAME. */
638 cp_class_name_from_physname (const char *physname)
640 void *storage = NULL;
641 char *demangled_name = NULL;
642 gdb::unique_xmalloc_ptr<char> ret;
643 struct demangle_component *ret_comp, *prev_comp, *cur_comp;
644 std::unique_ptr<demangle_parse_info> info;
647 info = mangled_name_to_comp (physname, DMGL_ANSI,
648 &storage, &demangled_name);
653 ret_comp = info->tree;
655 /* First strip off any qualifiers, if we have a function or
658 switch (ret_comp->type)
660 case DEMANGLE_COMPONENT_CONST:
661 case DEMANGLE_COMPONENT_RESTRICT:
662 case DEMANGLE_COMPONENT_VOLATILE:
663 case DEMANGLE_COMPONENT_CONST_THIS:
664 case DEMANGLE_COMPONENT_RESTRICT_THIS:
665 case DEMANGLE_COMPONENT_VOLATILE_THIS:
666 case DEMANGLE_COMPONENT_VENDOR_TYPE_QUAL:
667 ret_comp = d_left (ret_comp);
674 /* If what we have now is a function, discard the argument list. */
675 if (ret_comp->type == DEMANGLE_COMPONENT_TYPED_NAME)
676 ret_comp = d_left (ret_comp);
678 /* If what we have now is a template, strip off the template
679 arguments. The left subtree may be a qualified name. */
680 if (ret_comp->type == DEMANGLE_COMPONENT_TEMPLATE)
681 ret_comp = d_left (ret_comp);
683 /* What we have now should be a name, possibly qualified.
684 Additional qualifiers could live in the left subtree or the right
685 subtree. Find the last piece. */
690 switch (cur_comp->type)
692 case DEMANGLE_COMPONENT_QUAL_NAME:
693 case DEMANGLE_COMPONENT_LOCAL_NAME:
694 prev_comp = cur_comp;
695 cur_comp = d_right (cur_comp);
697 case DEMANGLE_COMPONENT_TEMPLATE:
698 case DEMANGLE_COMPONENT_NAME:
699 case DEMANGLE_COMPONENT_CTOR:
700 case DEMANGLE_COMPONENT_DTOR:
701 case DEMANGLE_COMPONENT_OPERATOR:
702 case DEMANGLE_COMPONENT_EXTENDED_OPERATOR:
711 if (cur_comp != NULL && prev_comp != NULL)
713 /* We want to discard the rightmost child of PREV_COMP. */
714 *prev_comp = *d_left (prev_comp);
715 /* The ten is completely arbitrary; we don't have a good
717 ret = cp_comp_to_string (ret_comp, 10);
721 xfree (demangled_name);
722 return ret.release ();
725 /* Return the child of COMP which is the basename of a method,
726 variable, et cetera. All scope qualifiers are discarded, but
727 template arguments will be included. The component tree may be
730 static struct demangle_component *
731 unqualified_name_from_comp (struct demangle_component *comp)
733 struct demangle_component *ret_comp = comp, *last_template;
737 last_template = NULL;
739 switch (ret_comp->type)
741 case DEMANGLE_COMPONENT_QUAL_NAME:
742 case DEMANGLE_COMPONENT_LOCAL_NAME:
743 ret_comp = d_right (ret_comp);
745 case DEMANGLE_COMPONENT_TYPED_NAME:
746 ret_comp = d_left (ret_comp);
748 case DEMANGLE_COMPONENT_TEMPLATE:
749 gdb_assert (last_template == NULL);
750 last_template = ret_comp;
751 ret_comp = d_left (ret_comp);
753 case DEMANGLE_COMPONENT_CONST:
754 case DEMANGLE_COMPONENT_RESTRICT:
755 case DEMANGLE_COMPONENT_VOLATILE:
756 case DEMANGLE_COMPONENT_CONST_THIS:
757 case DEMANGLE_COMPONENT_RESTRICT_THIS:
758 case DEMANGLE_COMPONENT_VOLATILE_THIS:
759 case DEMANGLE_COMPONENT_VENDOR_TYPE_QUAL:
760 ret_comp = d_left (ret_comp);
762 case DEMANGLE_COMPONENT_NAME:
763 case DEMANGLE_COMPONENT_CTOR:
764 case DEMANGLE_COMPONENT_DTOR:
765 case DEMANGLE_COMPONENT_OPERATOR:
766 case DEMANGLE_COMPONENT_EXTENDED_OPERATOR:
776 d_left (last_template) = ret_comp;
777 return last_template;
783 /* Return the name of the method whose linkage name is PHYSNAME. */
786 method_name_from_physname (const char *physname)
788 void *storage = NULL;
789 char *demangled_name = NULL;
790 gdb::unique_xmalloc_ptr<char> ret;
791 struct demangle_component *ret_comp;
792 std::unique_ptr<demangle_parse_info> info;
794 info = mangled_name_to_comp (physname, DMGL_ANSI,
795 &storage, &demangled_name);
799 ret_comp = unqualified_name_from_comp (info->tree);
801 if (ret_comp != NULL)
802 /* The ten is completely arbitrary; we don't have a good
804 ret = cp_comp_to_string (ret_comp, 10);
807 xfree (demangled_name);
808 return ret.release ();
811 /* If FULL_NAME is the demangled name of a C++ function (including an
812 arg list, possibly including namespace/class qualifications),
813 return a new string containing only the function name (without the
814 arg list/class qualifications). Otherwise, return NULL. */
816 gdb::unique_xmalloc_ptr<char>
817 cp_func_name (const char *full_name)
819 gdb::unique_xmalloc_ptr<char> ret;
820 struct demangle_component *ret_comp;
821 std::unique_ptr<demangle_parse_info> info;
823 info = cp_demangled_name_to_comp (full_name, NULL);
827 ret_comp = unqualified_name_from_comp (info->tree);
829 if (ret_comp != NULL)
830 ret = cp_comp_to_string (ret_comp, 10);
835 /* Helper for cp_remove_params. DEMANGLED_NAME is the name of a
836 function, including parameters and (optionally) a return type.
837 Return the name of the function without parameters or return type,
838 or NULL if we can not parse the name. If REQUIRE_PARAMS is false,
839 then tolerate a non-existing or unbalanced parameter list. */
841 static gdb::unique_xmalloc_ptr<char>
842 cp_remove_params_1 (const char *demangled_name, bool require_params)
845 struct demangle_component *ret_comp;
846 std::unique_ptr<demangle_parse_info> info;
847 gdb::unique_xmalloc_ptr<char> ret;
849 if (demangled_name == NULL)
852 info = cp_demangled_name_to_comp (demangled_name, NULL);
856 /* First strip off any qualifiers, if we have a function or method. */
857 ret_comp = info->tree;
859 switch (ret_comp->type)
861 case DEMANGLE_COMPONENT_CONST:
862 case DEMANGLE_COMPONENT_RESTRICT:
863 case DEMANGLE_COMPONENT_VOLATILE:
864 case DEMANGLE_COMPONENT_CONST_THIS:
865 case DEMANGLE_COMPONENT_RESTRICT_THIS:
866 case DEMANGLE_COMPONENT_VOLATILE_THIS:
867 case DEMANGLE_COMPONENT_VENDOR_TYPE_QUAL:
868 ret_comp = d_left (ret_comp);
875 /* What we have now should be a function. Return its name. */
876 if (ret_comp->type == DEMANGLE_COMPONENT_TYPED_NAME)
877 ret = cp_comp_to_string (d_left (ret_comp), 10);
878 else if (!require_params
879 && (ret_comp->type == DEMANGLE_COMPONENT_NAME
880 || ret_comp->type == DEMANGLE_COMPONENT_QUAL_NAME
881 || ret_comp->type == DEMANGLE_COMPONENT_TEMPLATE))
882 ret = cp_comp_to_string (ret_comp, 10);
887 /* DEMANGLED_NAME is the name of a function, including parameters and
888 (optionally) a return type. Return the name of the function
889 without parameters or return type, or NULL if we can not parse the
892 gdb::unique_xmalloc_ptr<char>
893 cp_remove_params (const char *demangled_name)
895 return cp_remove_params_1 (demangled_name, true);
898 /* See cp-support.h. */
900 gdb::unique_xmalloc_ptr<char>
901 cp_remove_params_if_any (const char *demangled_name, bool completion_mode)
903 /* Trying to remove parameters from the empty string fails. If
904 we're completing / matching everything, avoid returning NULL
905 which would make callers interpret the result as an error. */
906 if (demangled_name[0] == '\0' && completion_mode)
907 return make_unique_xstrdup ("");
909 gdb::unique_xmalloc_ptr<char> without_params
910 = cp_remove_params_1 (demangled_name, false);
912 if (without_params == NULL && completion_mode)
914 std::string copy = demangled_name;
916 while (!copy.empty ())
919 without_params = cp_remove_params_1 (copy.c_str (), false);
920 if (without_params != NULL)
925 return without_params;
928 /* Here are some random pieces of trivia to keep in mind while trying
929 to take apart demangled names:
931 - Names can contain function arguments or templates, so the process
932 has to be, to some extent recursive: maybe keep track of your
933 depth based on encountering <> and ().
935 - Parentheses don't just have to happen at the end of a name: they
936 can occur even if the name in question isn't a function, because
937 a template argument might be a type that's a function.
939 - Conversely, even if you're trying to deal with a function, its
940 demangled name might not end with ')': it could be a const or
941 volatile class method, in which case it ends with "const" or
944 - Parentheses are also used in anonymous namespaces: a variable
945 'foo' in an anonymous namespace gets demangled as "(anonymous
948 - And operator names can contain parentheses or angle brackets. */
950 /* FIXME: carlton/2003-03-13: We have several functions here with
951 overlapping functionality; can we combine them? Also, do they
952 handle all the above considerations correctly? */
955 /* This returns the length of first component of NAME, which should be
956 the demangled name of a C++ variable/function/method/etc.
957 Specifically, it returns the index of the first colon forming the
958 boundary of the first component: so, given 'A::foo' or 'A::B::foo'
959 it returns the 1, and given 'foo', it returns 0. */
961 /* The character in NAME indexed by the return value is guaranteed to
962 always be either ':' or '\0'. */
964 /* NOTE: carlton/2003-03-13: This function is currently only intended
965 for internal use: it's probably not entirely safe when called on
966 user-generated input, because some of the 'index += 2' lines in
967 cp_find_first_component_aux might go past the end of malformed
971 cp_find_first_component (const char *name)
973 return cp_find_first_component_aux (name, 0);
976 /* Helper function for cp_find_first_component. Like that function,
977 it returns the length of the first component of NAME, but to make
978 the recursion easier, it also stops if it reaches an unexpected ')'
979 or '>' if the value of PERMISSIVE is nonzero. */
982 cp_find_first_component_aux (const char *name, int permissive)
984 unsigned int index = 0;
985 /* Operator names can show up in unexpected places. Since these can
986 contain parentheses or angle brackets, they can screw up the
987 recursion. But not every string 'operator' is part of an
988 operater name: e.g. you could have a variable 'cooperator'. So
989 this variable tells us whether or not we should treat the string
990 'operator' as starting an operator. */
991 int operator_possible = 1;
998 /* Template; eat it up. The calls to cp_first_component
999 should only return (I hope!) when they reach the '>'
1000 terminating the component or a '::' between two
1001 components. (Hence the '+ 2'.) */
1003 for (index += cp_find_first_component_aux (name + index, 1);
1005 index += cp_find_first_component_aux (name + index, 1))
1007 if (name[index] != ':')
1009 demangled_name_complaint (name);
1010 return strlen (name);
1014 operator_possible = 1;
1017 /* Similar comment as to '<'. */
1019 for (index += cp_find_first_component_aux (name + index, 1);
1021 index += cp_find_first_component_aux (name + index, 1))
1023 if (name[index] != ':')
1025 demangled_name_complaint (name);
1026 return strlen (name);
1030 operator_possible = 1;
1038 demangled_name_complaint (name);
1039 return strlen (name);
1044 /* ':' marks a component iff the next character is also a ':'.
1045 Otherwise it is probably malformed input. */
1046 if (name[index + 1] == ':')
1050 /* Operator names can screw up the recursion. */
1051 if (operator_possible
1052 && startswith (name + index, CP_OPERATOR_STR))
1054 index += CP_OPERATOR_LEN;
1055 while (ISSPACE(name[index]))
1057 switch (name[index])
1061 /* Skip over one less than the appropriate number of
1062 characters: the for loop will skip over the last
1065 if (name[index + 1] == '<')
1072 if (name[index + 1] == '>')
1085 operator_possible = 0;
1092 /* NOTE: carlton/2003-04-18: I'm not sure what the precise
1093 set of relevant characters are here: it's necessary to
1094 include any character that can show up before 'operator'
1095 in a demangled name, and it's safe to include any
1096 character that can't be part of an identifier's name. */
1097 operator_possible = 1;
1100 operator_possible = 0;
1106 /* Complain about a demangled name that we don't know how to parse.
1107 NAME is the demangled name in question. */
1110 demangled_name_complaint (const char *name)
1112 complaint ("unexpected demangled name '%s'", name);
1115 /* If NAME is the fully-qualified name of a C++
1116 function/variable/method/etc., this returns the length of its
1117 entire prefix: all of the namespaces and classes that make up its
1118 name. Given 'A::foo', it returns 1, given 'A::B::foo', it returns
1119 4, given 'foo', it returns 0. */
1122 cp_entire_prefix_len (const char *name)
1124 unsigned int current_len = cp_find_first_component (name);
1125 unsigned int previous_len = 0;
1127 while (name[current_len] != '\0')
1129 gdb_assert (name[current_len] == ':');
1130 previous_len = current_len;
1131 /* Skip the '::'. */
1133 current_len += cp_find_first_component (name + current_len);
1136 return previous_len;
1139 /* Overload resolution functions. */
1141 /* Test to see if SYM is a symbol that we haven't seen corresponding
1142 to a function named OLOAD_NAME. If so, add it to
1146 overload_list_add_symbol (struct symbol *sym,
1147 const char *oload_name,
1148 std::vector<symbol *> *overload_list)
1150 /* If there is no type information, we can't do anything, so
1152 if (SYMBOL_TYPE (sym) == NULL)
1155 /* skip any symbols that we've already considered. */
1156 for (symbol *listed_sym : *overload_list)
1157 if (strcmp (SYMBOL_LINKAGE_NAME (sym),
1158 SYMBOL_LINKAGE_NAME (listed_sym)) == 0)
1161 /* Get the demangled name without parameters */
1162 gdb::unique_xmalloc_ptr<char> sym_name
1163 = cp_remove_params (SYMBOL_NATURAL_NAME (sym));
1167 /* skip symbols that cannot match */
1168 if (strcmp (sym_name.get (), oload_name) != 0)
1171 overload_list->push_back (sym);
1174 /* Return a null-terminated list of pointers to function symbols that
1175 are named FUNC_NAME and are visible within NAMESPACE. */
1177 struct std::vector<symbol *>
1178 make_symbol_overload_list (const char *func_name,
1179 const char *the_namespace)
1182 std::vector<symbol *> overload_list;
1184 overload_list.reserve (100);
1186 add_symbol_overload_list_using (func_name, the_namespace, &overload_list);
1188 if (the_namespace[0] == '\0')
1192 char *concatenated_name
1193 = (char *) alloca (strlen (the_namespace) + 2 + strlen (func_name) + 1);
1194 strcpy (concatenated_name, the_namespace);
1195 strcat (concatenated_name, "::");
1196 strcat (concatenated_name, func_name);
1197 name = concatenated_name;
1200 add_symbol_overload_list_qualified (name, &overload_list);
1201 return overload_list;
1204 /* Add all symbols with a name matching NAME in BLOCK to the overload
1208 add_symbol_overload_list_block (const char *name,
1209 const struct block *block,
1210 std::vector<symbol *> *overload_list)
1212 struct block_iterator iter;
1215 lookup_name_info lookup_name (name, symbol_name_match_type::FULL);
1217 ALL_BLOCK_SYMBOLS_WITH_NAME (block, lookup_name, iter, sym)
1218 overload_list_add_symbol (sym, name, overload_list);
1221 /* Adds the function FUNC_NAME from NAMESPACE to the overload set. */
1224 add_symbol_overload_list_namespace (const char *func_name,
1225 const char *the_namespace,
1226 std::vector<symbol *> *overload_list)
1229 const struct block *block = NULL;
1231 if (the_namespace[0] == '\0')
1235 char *concatenated_name
1236 = (char *) alloca (strlen (the_namespace) + 2 + strlen (func_name) + 1);
1238 strcpy (concatenated_name, the_namespace);
1239 strcat (concatenated_name, "::");
1240 strcat (concatenated_name, func_name);
1241 name = concatenated_name;
1244 /* Look in the static block. */
1245 block = block_static_block (get_selected_block (0));
1247 add_symbol_overload_list_block (name, block, overload_list);
1249 /* Look in the global block. */
1250 block = block_global_block (block);
1252 add_symbol_overload_list_block (name, block, overload_list);
1256 /* Search the namespace of the given type and namespace of and public
1260 add_symbol_overload_list_adl_namespace (struct type *type,
1261 const char *func_name,
1262 std::vector<symbol *> *overload_list)
1264 char *the_namespace;
1265 const char *type_name;
1268 while (TYPE_CODE (type) == TYPE_CODE_PTR
1269 || TYPE_IS_REFERENCE (type)
1270 || TYPE_CODE (type) == TYPE_CODE_ARRAY
1271 || TYPE_CODE (type) == TYPE_CODE_TYPEDEF)
1273 if (TYPE_CODE (type) == TYPE_CODE_TYPEDEF)
1274 type = check_typedef(type);
1276 type = TYPE_TARGET_TYPE (type);
1279 type_name = TYPE_NAME (type);
1281 if (type_name == NULL)
1284 prefix_len = cp_entire_prefix_len (type_name);
1286 if (prefix_len != 0)
1288 the_namespace = (char *) alloca (prefix_len + 1);
1289 strncpy (the_namespace, type_name, prefix_len);
1290 the_namespace[prefix_len] = '\0';
1292 add_symbol_overload_list_namespace (func_name, the_namespace,
1296 /* Check public base type */
1297 if (TYPE_CODE (type) == TYPE_CODE_STRUCT)
1298 for (i = 0; i < TYPE_N_BASECLASSES (type); i++)
1300 if (BASETYPE_VIA_PUBLIC (type, i))
1301 add_symbol_overload_list_adl_namespace (TYPE_BASECLASS (type, i),
1307 /* Adds to OVERLOAD_LIST the overload list overload candidates for
1308 FUNC_NAME found through argument dependent lookup. */
1311 add_symbol_overload_list_adl (gdb::array_view<type *> arg_types,
1312 const char *func_name,
1313 std::vector<symbol *> *overload_list)
1315 for (type *arg_type : arg_types)
1316 add_symbol_overload_list_adl_namespace (arg_type, func_name,
1320 /* This applies the using directives to add namespaces to search in,
1321 and then searches for overloads in all of those namespaces. It
1322 adds the symbols found to sym_return_val. Arguments are as in
1323 make_symbol_overload_list. */
1326 add_symbol_overload_list_using (const char *func_name,
1327 const char *the_namespace,
1328 std::vector<symbol *> *overload_list)
1330 struct using_direct *current;
1331 const struct block *block;
1333 /* First, go through the using directives. If any of them apply,
1334 look in the appropriate namespaces for new functions to match
1337 for (block = get_selected_block (0);
1339 block = BLOCK_SUPERBLOCK (block))
1340 for (current = block_using (block);
1342 current = current->next)
1344 /* Prevent recursive calls. */
1345 if (current->searched)
1348 /* If this is a namespace alias or imported declaration ignore
1350 if (current->alias != NULL || current->declaration != NULL)
1353 if (strcmp (the_namespace, current->import_dest) == 0)
1355 /* Mark this import as searched so that the recursive call
1356 does not search it again. */
1357 scoped_restore reset_directive_searched
1358 = make_scoped_restore (¤t->searched, 1);
1360 add_symbol_overload_list_using (func_name,
1361 current->import_src,
1366 /* Now, add names for this namespace. */
1367 add_symbol_overload_list_namespace (func_name, the_namespace,
1371 /* This does the bulk of the work of finding overloaded symbols.
1372 FUNC_NAME is the name of the overloaded function we're looking for
1373 (possibly including namespace info). */
1376 add_symbol_overload_list_qualified (const char *func_name,
1377 std::vector<symbol *> *overload_list)
1379 const struct block *b, *surrounding_static_block = 0;
1381 /* Look through the partial symtabs for all symbols which begin by
1382 matching FUNC_NAME. Make sure we read that symbol table in. */
1384 for (objfile *objf : current_program_space->objfiles ())
1387 objf->sf->qf->expand_symtabs_for_function (objf, func_name);
1390 /* Search upwards from currently selected frame (so that we can
1391 complete on local vars. */
1393 for (b = get_selected_block (0); b != NULL; b = BLOCK_SUPERBLOCK (b))
1394 add_symbol_overload_list_block (func_name, b, overload_list);
1396 surrounding_static_block = block_static_block (get_selected_block (0));
1398 /* Go through the symtabs and check the externs and statics for
1399 symbols which match. */
1401 for (objfile *objfile : current_program_space->objfiles ())
1403 for (compunit_symtab *cust : objfile->compunits ())
1406 b = BLOCKVECTOR_BLOCK (COMPUNIT_BLOCKVECTOR (cust), GLOBAL_BLOCK);
1407 add_symbol_overload_list_block (func_name, b, overload_list);
1411 for (objfile *objfile : current_program_space->objfiles ())
1413 for (compunit_symtab *cust : objfile->compunits ())
1416 b = BLOCKVECTOR_BLOCK (COMPUNIT_BLOCKVECTOR (cust), STATIC_BLOCK);
1417 /* Don't do this block twice. */
1418 if (b == surrounding_static_block)
1420 add_symbol_overload_list_block (func_name, b, overload_list);
1425 /* Lookup the rtti type for a class name. */
1428 cp_lookup_rtti_type (const char *name, const struct block *block)
1430 struct symbol * rtti_sym;
1431 struct type * rtti_type;
1433 /* Use VAR_DOMAIN here as NAME may be a typedef. PR 18141, 18417.
1434 Classes "live" in both STRUCT_DOMAIN and VAR_DOMAIN. */
1435 rtti_sym = lookup_symbol (name, block, VAR_DOMAIN, NULL).symbol;
1437 if (rtti_sym == NULL)
1439 warning (_("RTTI symbol not found for class '%s'"), name);
1443 if (SYMBOL_CLASS (rtti_sym) != LOC_TYPEDEF)
1445 warning (_("RTTI symbol for class '%s' is not a type"), name);
1449 rtti_type = check_typedef (SYMBOL_TYPE (rtti_sym));
1451 switch (TYPE_CODE (rtti_type))
1453 case TYPE_CODE_STRUCT:
1455 case TYPE_CODE_NAMESPACE:
1456 /* chastain/2003-11-26: the symbol tables often contain fake
1457 symbols for namespaces with the same name as the struct.
1458 This warning is an indication of a bug in the lookup order
1459 or a bug in the way that the symbol tables are populated. */
1460 warning (_("RTTI symbol for class '%s' is a namespace"), name);
1463 warning (_("RTTI symbol for class '%s' has bad type"), name);
1470 #ifdef HAVE_WORKING_FORK
1472 /* If nonzero, attempt to catch crashes in the demangler and print
1473 useful debugging information. */
1475 static int catch_demangler_crashes = 1;
1477 /* Stack context and environment for demangler crash recovery. */
1479 static SIGJMP_BUF gdb_demangle_jmp_buf;
1481 /* If nonzero, attempt to dump core from the signal handler. */
1483 static int gdb_demangle_attempt_core_dump = 1;
1485 /* Signal handler for gdb_demangle. */
1488 gdb_demangle_signal_handler (int signo)
1490 if (gdb_demangle_attempt_core_dump)
1495 gdb_demangle_attempt_core_dump = 0;
1498 SIGLONGJMP (gdb_demangle_jmp_buf, signo);
1503 /* A wrapper for bfd_demangle. */
1506 gdb_demangle (const char *name, int options)
1508 char *result = NULL;
1509 int crash_signal = 0;
1511 #ifdef HAVE_WORKING_FORK
1512 #if defined (HAVE_SIGACTION) && defined (SA_RESTART)
1513 struct sigaction sa, old_sa;
1517 static int core_dump_allowed = -1;
1519 if (core_dump_allowed == -1)
1521 core_dump_allowed = can_dump_core (LIMIT_CUR);
1523 if (!core_dump_allowed)
1524 gdb_demangle_attempt_core_dump = 0;
1527 if (catch_demangler_crashes)
1529 #if defined (HAVE_SIGACTION) && defined (SA_RESTART)
1530 sa.sa_handler = gdb_demangle_signal_handler;
1531 sigemptyset (&sa.sa_mask);
1532 #ifdef HAVE_SIGALTSTACK
1533 sa.sa_flags = SA_ONSTACK;
1537 sigaction (SIGSEGV, &sa, &old_sa);
1539 ofunc = signal (SIGSEGV, gdb_demangle_signal_handler);
1542 crash_signal = SIGSETJMP (gdb_demangle_jmp_buf);
1546 if (crash_signal == 0)
1547 result = bfd_demangle (NULL, name, options);
1549 #ifdef HAVE_WORKING_FORK
1550 if (catch_demangler_crashes)
1552 #if defined (HAVE_SIGACTION) && defined (SA_RESTART)
1553 sigaction (SIGSEGV, &old_sa, NULL);
1555 signal (SIGSEGV, ofunc);
1558 if (crash_signal != 0)
1560 static int error_reported = 0;
1562 if (!error_reported)
1564 std::string short_msg
1565 = string_printf (_("unable to demangle '%s' "
1566 "(demangler failed with signal %d)"),
1567 name, crash_signal);
1569 std::string long_msg
1570 = string_printf ("%s:%d: %s: %s", __FILE__, __LINE__,
1571 "demangler-warning", short_msg.c_str ());
1573 target_terminal::scoped_restore_terminal_state term_state;
1574 target_terminal::ours_for_output ();
1577 if (core_dump_allowed)
1578 fprintf_unfiltered (gdb_stderr,
1579 _("%s\nAttempting to dump core.\n"),
1582 warn_cant_dump_core (long_msg.c_str ());
1584 demangler_warning (__FILE__, __LINE__, "%s", short_msg.c_str ());
1597 /* See cp-support.h. */
1600 gdb_sniff_from_mangled_name (const char *mangled, char **demangled)
1602 *demangled = gdb_demangle (mangled, DMGL_PARAMS | DMGL_ANSI);
1603 return *demangled != NULL;
1606 /* See cp-support.h. */
1609 cp_search_name_hash (const char *search_name)
1611 /* cp_entire_prefix_len assumes a fully-qualified name with no
1613 if (startswith (search_name, "::"))
1616 unsigned int prefix_len = cp_entire_prefix_len (search_name);
1617 if (prefix_len != 0)
1618 search_name += prefix_len + 2;
1620 unsigned int hash = 0;
1621 for (const char *string = search_name; *string != '\0'; ++string)
1623 string = skip_spaces (string);
1628 /* Ignore ABI tags such as "[abi:cxx11]. */
1630 && startswith (string + 1, "abi:")
1631 && string[5] != ':')
1634 hash = SYMBOL_HASH_NEXT (hash, *string);
1639 /* Helper for cp_symbol_name_matches (i.e., symbol_name_matcher_ftype
1640 implementation for symbol_name_match_type::WILD matching). Split
1641 to a separate function for unit-testing convenience.
1643 If SYMBOL_SEARCH_NAME has more scopes than LOOKUP_NAME, we try to
1644 match ignoring the extra leading scopes of SYMBOL_SEARCH_NAME.
1645 This allows conveniently setting breakpoints on functions/methods
1646 inside any namespace/class without specifying the fully-qualified
1651 [symbol search name] [lookup name]
1652 foo::bar::func foo::bar::func
1653 foo::bar::func bar::func
1658 [symbol search name] [lookup name]
1659 foo::zbar::func bar::func
1660 foo::bar::func foo::func
1662 See more examples in the test_cp_symbol_name_matches selftest
1665 See symbol_name_matcher_ftype for description of SYMBOL_SEARCH_NAME
1668 LOOKUP_NAME/LOOKUP_NAME_LEN is the name we're looking up.
1670 See strncmp_iw_with_mode for description of MODE.
1674 cp_symbol_name_matches_1 (const char *symbol_search_name,
1675 const char *lookup_name,
1676 size_t lookup_name_len,
1677 strncmp_iw_mode mode,
1678 completion_match_result *comp_match_res)
1680 const char *sname = symbol_search_name;
1681 completion_match_for_lcd *match_for_lcd
1682 = (comp_match_res != NULL ? &comp_match_res->match_for_lcd : NULL);
1686 if (strncmp_iw_with_mode (sname, lookup_name, lookup_name_len,
1687 mode, language_cplus, match_for_lcd) == 0)
1689 if (comp_match_res != NULL)
1691 /* Note here we set different MATCH and MATCH_FOR_LCD
1692 strings. This is because with
1694 (gdb) b push_bac[TAB]
1696 we want the completion matches to list
1698 std::vector<int>::push_back(...)
1699 std::vector<char>::push_back(...)
1701 etc., which are SYMBOL_SEARCH_NAMEs, while we want
1702 the input line to auto-complete to
1704 (gdb) push_back(...)
1706 which is SNAME, not to
1710 which would be the regular common prefix between all
1711 the matches otherwise. */
1712 comp_match_res->set_match (symbol_search_name, sname);
1717 unsigned int len = cp_find_first_component (sname);
1719 if (sname[len] == '\0')
1722 gdb_assert (sname[len] == ':');
1723 /* Skip the '::'. */
1728 /* C++ symbol_name_matcher_ftype implementation. */
1731 cp_fq_symbol_name_matches (const char *symbol_search_name,
1732 const lookup_name_info &lookup_name,
1733 completion_match_result *comp_match_res)
1735 /* Get the demangled name. */
1736 const std::string &name = lookup_name.cplus ().lookup_name ();
1737 completion_match_for_lcd *match_for_lcd
1738 = (comp_match_res != NULL ? &comp_match_res->match_for_lcd : NULL);
1739 strncmp_iw_mode mode = (lookup_name.completion_mode ()
1740 ? strncmp_iw_mode::NORMAL
1741 : strncmp_iw_mode::MATCH_PARAMS);
1743 if (strncmp_iw_with_mode (symbol_search_name,
1744 name.c_str (), name.size (),
1745 mode, language_cplus, match_for_lcd) == 0)
1747 if (comp_match_res != NULL)
1748 comp_match_res->set_match (symbol_search_name);
1755 /* C++ symbol_name_matcher_ftype implementation for wild matches.
1756 Defers work to cp_symbol_name_matches_1. */
1759 cp_symbol_name_matches (const char *symbol_search_name,
1760 const lookup_name_info &lookup_name,
1761 completion_match_result *comp_match_res)
1763 /* Get the demangled name. */
1764 const std::string &name = lookup_name.cplus ().lookup_name ();
1766 strncmp_iw_mode mode = (lookup_name.completion_mode ()
1767 ? strncmp_iw_mode::NORMAL
1768 : strncmp_iw_mode::MATCH_PARAMS);
1770 return cp_symbol_name_matches_1 (symbol_search_name,
1771 name.c_str (), name.size (),
1772 mode, comp_match_res);
1775 /* See cp-support.h. */
1777 symbol_name_matcher_ftype *
1778 cp_get_symbol_name_matcher (const lookup_name_info &lookup_name)
1780 switch (lookup_name.match_type ())
1782 case symbol_name_match_type::FULL:
1783 case symbol_name_match_type::EXPRESSION:
1784 case symbol_name_match_type::SEARCH_NAME:
1785 return cp_fq_symbol_name_matches;
1786 case symbol_name_match_type::WILD:
1787 return cp_symbol_name_matches;
1790 gdb_assert_not_reached ("");
1795 namespace selftests {
1798 test_cp_symbol_name_matches ()
1800 #define CHECK_MATCH(SYMBOL, INPUT) \
1801 SELF_CHECK (cp_symbol_name_matches_1 (SYMBOL, \
1802 INPUT, sizeof (INPUT) - 1, \
1803 strncmp_iw_mode::MATCH_PARAMS, \
1806 #define CHECK_NOT_MATCH(SYMBOL, INPUT) \
1807 SELF_CHECK (!cp_symbol_name_matches_1 (SYMBOL, \
1808 INPUT, sizeof (INPUT) - 1, \
1809 strncmp_iw_mode::MATCH_PARAMS, \
1812 /* Like CHECK_MATCH, and also check that INPUT (and all substrings
1813 that start at index 0) completes to SYMBOL. */
1814 #define CHECK_MATCH_C(SYMBOL, INPUT) \
1817 CHECK_MATCH (SYMBOL, INPUT); \
1818 for (size_t i = 0; i < sizeof (INPUT) - 1; i++) \
1819 SELF_CHECK (cp_symbol_name_matches_1 (SYMBOL, INPUT, i, \
1820 strncmp_iw_mode::NORMAL, \
1824 /* Like CHECK_NOT_MATCH, and also check that INPUT does NOT complete
1826 #define CHECK_NOT_MATCH_C(SYMBOL, INPUT) \
1829 CHECK_NOT_MATCH (SYMBOL, INPUT); \
1830 SELF_CHECK (!cp_symbol_name_matches_1 (SYMBOL, INPUT, \
1831 sizeof (INPUT) - 1, \
1832 strncmp_iw_mode::NORMAL, \
1836 /* Lookup name without parens matches all overloads. */
1837 CHECK_MATCH_C ("function()", "function");
1838 CHECK_MATCH_C ("function(int)", "function");
1840 /* Check whitespace around parameters is ignored. */
1841 CHECK_MATCH_C ("function()", "function ()");
1842 CHECK_MATCH_C ("function ( )", "function()");
1843 CHECK_MATCH_C ("function ()", "function( )");
1844 CHECK_MATCH_C ("func(int)", "func( int )");
1845 CHECK_MATCH_C ("func(int)", "func ( int ) ");
1846 CHECK_MATCH_C ("func ( int )", "func( int )");
1847 CHECK_MATCH_C ("func ( int )", "func ( int ) ");
1849 /* Check symbol name prefixes aren't incorrectly matched. */
1850 CHECK_NOT_MATCH ("func", "function");
1851 CHECK_NOT_MATCH ("function", "func");
1852 CHECK_NOT_MATCH ("function()", "func");
1854 /* Check that if the lookup name includes parameters, only the right
1855 overload matches. */
1856 CHECK_MATCH_C ("function(int)", "function(int)");
1857 CHECK_NOT_MATCH_C ("function(int)", "function()");
1859 /* Check that whitespace within symbol names is not ignored. */
1860 CHECK_NOT_MATCH_C ("function", "func tion");
1861 CHECK_NOT_MATCH_C ("func__tion", "func_ _tion");
1862 CHECK_NOT_MATCH_C ("func11tion", "func1 1tion");
1864 /* Check the converse, which can happen with template function,
1865 where the return type is part of the demangled name. */
1866 CHECK_NOT_MATCH_C ("func tion", "function");
1867 CHECK_NOT_MATCH_C ("func1 1tion", "func11tion");
1868 CHECK_NOT_MATCH_C ("func_ _tion", "func__tion");
1870 /* Within parameters too. */
1871 CHECK_NOT_MATCH_C ("func(param)", "func(par am)");
1873 /* Check handling of whitespace around C++ operators. */
1874 CHECK_NOT_MATCH_C ("operator<<", "opera tor<<");
1875 CHECK_NOT_MATCH_C ("operator<<", "operator< <");
1876 CHECK_NOT_MATCH_C ("operator<<", "operator < <");
1877 CHECK_NOT_MATCH_C ("operator==", "operator= =");
1878 CHECK_NOT_MATCH_C ("operator==", "operator = =");
1879 CHECK_MATCH_C ("operator<<", "operator <<");
1880 CHECK_MATCH_C ("operator<<()", "operator <<");
1881 CHECK_NOT_MATCH_C ("operator<<()", "operator<<(int)");
1882 CHECK_NOT_MATCH_C ("operator<<(int)", "operator<<()");
1883 CHECK_MATCH_C ("operator==", "operator ==");
1884 CHECK_MATCH_C ("operator==()", "operator ==");
1885 CHECK_MATCH_C ("operator <<", "operator<<");
1886 CHECK_MATCH_C ("operator ==", "operator==");
1887 CHECK_MATCH_C ("operator bool", "operator bool");
1888 CHECK_MATCH_C ("operator bool ()", "operator bool");
1889 CHECK_MATCH_C ("operatorX<<", "operatorX < <");
1890 CHECK_MATCH_C ("Xoperator<<", "Xoperator < <");
1892 CHECK_MATCH_C ("operator()(int)", "operator()(int)");
1893 CHECK_MATCH_C ("operator()(int)", "operator ( ) ( int )");
1894 CHECK_MATCH_C ("operator()<long>(int)", "operator ( ) < long > ( int )");
1895 /* The first "()" is not the parameter list. */
1896 CHECK_NOT_MATCH ("operator()(int)", "operator");
1898 /* Misc user-defined operator tests. */
1900 CHECK_NOT_MATCH_C ("operator/=()", "operator ^=");
1901 /* Same length at end of input. */
1902 CHECK_NOT_MATCH_C ("operator>>", "operator[]");
1903 /* Same length but not at end of input. */
1904 CHECK_NOT_MATCH_C ("operator>>()", "operator[]()");
1906 CHECK_MATCH_C ("base::operator char*()", "base::operator char*()");
1907 CHECK_MATCH_C ("base::operator char*()", "base::operator char * ()");
1908 CHECK_MATCH_C ("base::operator char**()", "base::operator char * * ()");
1909 CHECK_MATCH ("base::operator char**()", "base::operator char * *");
1910 CHECK_MATCH_C ("base::operator*()", "base::operator*()");
1911 CHECK_NOT_MATCH_C ("base::operator char*()", "base::operatorc");
1912 CHECK_NOT_MATCH ("base::operator char*()", "base::operator char");
1913 CHECK_NOT_MATCH ("base::operator char*()", "base::operat");
1915 /* Check handling of whitespace around C++ scope operators. */
1916 CHECK_NOT_MATCH_C ("foo::bar", "foo: :bar");
1917 CHECK_MATCH_C ("foo::bar", "foo :: bar");
1918 CHECK_MATCH_C ("foo :: bar", "foo::bar");
1920 CHECK_MATCH_C ("abc::def::ghi()", "abc::def::ghi()");
1921 CHECK_MATCH_C ("abc::def::ghi ( )", "abc::def::ghi()");
1922 CHECK_MATCH_C ("abc::def::ghi()", "abc::def::ghi ( )");
1923 CHECK_MATCH_C ("function()", "function()");
1924 CHECK_MATCH_C ("bar::function()", "bar::function()");
1926 /* Wild matching tests follow. */
1928 /* Tests matching symbols in some scope. */
1929 CHECK_MATCH_C ("foo::function()", "function");
1930 CHECK_MATCH_C ("foo::function(int)", "function");
1931 CHECK_MATCH_C ("foo::bar::function()", "function");
1932 CHECK_MATCH_C ("bar::function()", "bar::function");
1933 CHECK_MATCH_C ("foo::bar::function()", "bar::function");
1934 CHECK_MATCH_C ("foo::bar::function(int)", "bar::function");
1936 /* Same, with parameters in the lookup name. */
1937 CHECK_MATCH_C ("foo::function()", "function()");
1938 CHECK_MATCH_C ("foo::bar::function()", "function()");
1939 CHECK_MATCH_C ("foo::function(int)", "function(int)");
1940 CHECK_MATCH_C ("foo::function()", "foo::function()");
1941 CHECK_MATCH_C ("foo::bar::function()", "bar::function()");
1942 CHECK_MATCH_C ("foo::bar::function(int)", "bar::function(int)");
1943 CHECK_MATCH_C ("bar::function()", "bar::function()");
1945 CHECK_NOT_MATCH_C ("foo::bar::function(int)", "bar::function()");
1947 CHECK_MATCH_C ("(anonymous namespace)::bar::function(int)",
1948 "bar::function(int)");
1949 CHECK_MATCH_C ("foo::(anonymous namespace)::bar::function(int)",
1952 /* Lookup scope wider than symbol scope, should not match. */
1953 CHECK_NOT_MATCH_C ("function()", "bar::function");
1954 CHECK_NOT_MATCH_C ("function()", "bar::function()");
1956 /* Explicit global scope doesn't match. */
1957 CHECK_NOT_MATCH_C ("foo::function()", "::function");
1958 CHECK_NOT_MATCH_C ("foo::function()", "::function()");
1959 CHECK_NOT_MATCH_C ("foo::function(int)", "::function()");
1960 CHECK_NOT_MATCH_C ("foo::function(int)", "::function(int)");
1962 /* Test ABI tag matching/ignoring. */
1964 /* If the symbol name has an ABI tag, but the lookup name doesn't,
1965 then the ABI tag in the symbol name is ignored. */
1966 CHECK_MATCH_C ("function[abi:foo]()", "function");
1967 CHECK_MATCH_C ("function[abi:foo](int)", "function");
1968 CHECK_MATCH_C ("function[abi:foo]()", "function ()");
1969 CHECK_NOT_MATCH_C ("function[abi:foo]()", "function (int)");
1971 CHECK_MATCH_C ("function[abi:foo]()", "function[abi:foo]");
1972 CHECK_MATCH_C ("function[abi:foo](int)", "function[abi:foo]");
1973 CHECK_MATCH_C ("function[abi:foo]()", "function[abi:foo] ()");
1974 CHECK_MATCH_C ("function[abi:foo][abi:bar]()", "function");
1975 CHECK_MATCH_C ("function[abi:foo][abi:bar](int)", "function");
1976 CHECK_MATCH_C ("function[abi:foo][abi:bar]()", "function[abi:foo]");
1977 CHECK_MATCH_C ("function[abi:foo][abi:bar](int)", "function[abi:foo]");
1978 CHECK_MATCH_C ("function[abi:foo][abi:bar]()", "function[abi:foo] ()");
1979 CHECK_NOT_MATCH_C ("function[abi:foo][abi:bar]()", "function[abi:foo] (int)");
1981 CHECK_MATCH_C ("function [abi:foo][abi:bar] ( )", "function [abi:foo]");
1983 /* If the symbol name does not have an ABI tag, while the lookup
1984 name has one, then there's no match. */
1985 CHECK_NOT_MATCH_C ("function()", "function[abi:foo]()");
1986 CHECK_NOT_MATCH_C ("function()", "function[abi:foo]");
1989 /* If non-NULL, return STR wrapped in quotes. Otherwise, return a
1990 "<null>" string (with no quotes). */
1993 quote (const char *str)
1996 return std::string (1, '\"') + str + '\"';
2001 /* Check that removing parameter info out of NAME produces EXPECTED.
2002 COMPLETION_MODE indicates whether we're testing normal and
2003 completion mode. FILE and LINE are used to provide better test
2004 location information in case ithe check fails. */
2007 check_remove_params (const char *file, int line,
2008 const char *name, const char *expected,
2009 bool completion_mode)
2011 gdb::unique_xmalloc_ptr<char> result
2012 = cp_remove_params_if_any (name, completion_mode);
2014 if ((expected == NULL) != (result == NULL)
2015 || (expected != NULL
2016 && strcmp (result.get (), expected) != 0))
2018 error (_("%s:%d: make-paramless self-test failed: (completion=%d) "
2019 "\"%s\" -> %s, expected %s"),
2020 file, line, completion_mode, name,
2021 quote (result.get ()).c_str (), quote (expected).c_str ());
2025 /* Entry point for cp_remove_params unit tests. */
2028 test_cp_remove_params ()
2030 /* Check that removing parameter info out of NAME produces EXPECTED.
2031 Checks both normal and completion modes. */
2032 #define CHECK(NAME, EXPECTED) \
2035 check_remove_params (__FILE__, __LINE__, NAME, EXPECTED, false); \
2036 check_remove_params (__FILE__, __LINE__, NAME, EXPECTED, true); \
2040 /* Similar, but used when NAME is incomplete -- i.e., is has
2041 unbalanced parentheses. In this case, looking for the exact name
2042 should fail / return empty. */
2043 #define CHECK_INCOMPL(NAME, EXPECTED) \
2046 check_remove_params (__FILE__, __LINE__, NAME, NULL, false); \
2047 check_remove_params (__FILE__, __LINE__, NAME, EXPECTED, true); \
2051 CHECK ("function()", "function");
2052 CHECK_INCOMPL ("function(", "function");
2053 CHECK ("function() const", "function");
2055 CHECK ("(anonymous namespace)::A::B::C",
2056 "(anonymous namespace)::A::B::C");
2058 CHECK ("A::(anonymous namespace)",
2059 "A::(anonymous namespace)");
2061 CHECK_INCOMPL ("A::(anonymou", "A");
2063 CHECK ("A::foo<int>()",
2066 CHECK_INCOMPL ("A::foo<int>(",
2069 CHECK ("A::foo<(anonymous namespace)::B>::func(int)",
2070 "A::foo<(anonymous namespace)::B>::func");
2072 CHECK_INCOMPL ("A::foo<(anonymous namespace)::B>::func(in",
2073 "A::foo<(anonymous namespace)::B>::func");
2075 CHECK_INCOMPL ("A::foo<(anonymous namespace)::B>::",
2076 "A::foo<(anonymous namespace)::B>");
2078 CHECK_INCOMPL ("A::foo<(anonymous namespace)::B>:",
2079 "A::foo<(anonymous namespace)::B>");
2081 CHECK ("A::foo<(anonymous namespace)::B>",
2082 "A::foo<(anonymous namespace)::B>");
2084 CHECK_INCOMPL ("A::foo<(anonymous namespace)::B",
2087 /* Shouldn't this parse? Looks like a bug in
2088 cp_demangled_name_to_comp. See PR c++/22411. */
2090 CHECK ("A::foo<void(int)>::func(int)",
2091 "A::foo<void(int)>::func");
2093 CHECK_INCOMPL ("A::foo<void(int)>::func(int)",
2097 CHECK_INCOMPL ("A::foo<void(int",
2101 #undef CHECK_INCOMPL
2104 } // namespace selftests
2106 #endif /* GDB_SELF_CHECK */
2108 /* Don't allow just "maintenance cplus". */
2111 maint_cplus_command (const char *arg, int from_tty)
2113 printf_unfiltered (_("\"maintenance cplus\" must be followed "
2114 "by the name of a command.\n"));
2115 help_list (maint_cplus_cmd_list,
2116 "maintenance cplus ",
2117 all_commands, gdb_stdout);
2120 /* This is a front end for cp_find_first_component, for unit testing.
2121 Be careful when using it: see the NOTE above
2122 cp_find_first_component. */
2125 first_component_command (const char *arg, int from_tty)
2133 len = cp_find_first_component (arg);
2134 prefix = (char *) alloca (len + 1);
2136 memcpy (prefix, arg, len);
2139 printf_unfiltered ("%s\n", prefix);
2142 /* Implement "info vtbl". */
2145 info_vtbl_command (const char *arg, int from_tty)
2147 struct value *value;
2149 value = parse_and_eval (arg);
2150 cplus_print_vtable (value);
2154 _initialize_cp_support (void)
2156 add_prefix_cmd ("cplus", class_maintenance,
2157 maint_cplus_command,
2158 _("C++ maintenance commands."),
2159 &maint_cplus_cmd_list,
2160 "maintenance cplus ",
2161 0, &maintenancelist);
2162 add_alias_cmd ("cp", "cplus",
2163 class_maintenance, 1,
2166 add_cmd ("first_component",
2168 first_component_command,
2169 _("Print the first class/namespace component of NAME."),
2170 &maint_cplus_cmd_list);
2172 add_info ("vtbl", info_vtbl_command,
2173 _("Show the virtual function table for a C++ object.\n\
2174 Usage: info vtbl EXPRESSION\n\
2175 Evaluate EXPRESSION and display the virtual function table for the\n\
2176 resulting object."));
2178 #ifdef HAVE_WORKING_FORK
2179 add_setshow_boolean_cmd ("catch-demangler-crashes", class_maintenance,
2180 &catch_demangler_crashes, _("\
2181 Set whether to attempt to catch demangler crashes."), _("\
2182 Show whether to attempt to catch demangler crashes."), _("\
2183 If enabled GDB will attempt to catch demangler crashes and\n\
2184 display the offending symbol."),
2187 &maintenance_set_cmdlist,
2188 &maintenance_show_cmdlist);
2192 selftests::register_test ("cp_symbol_name_matches",
2193 selftests::test_cp_symbol_name_matches);
2194 selftests::register_test ("cp_remove_params",
2195 selftests::test_cp_remove_params);