1 /* Helper routines for C++ support in GDB.
2 Copyright (C) 2002-2017 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 "gdb_setjmp.h"
38 #include "safe-ctype.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/variables related to overload resolution. */
53 static int sym_return_val_size = -1;
54 static int sym_return_val_index;
55 static struct symbol **sym_return_val;
57 static void overload_list_add_symbol (struct symbol *sym,
58 const char *oload_name);
60 static void make_symbol_overload_list_using (const char *func_name,
61 const char *the_namespace);
63 static void make_symbol_overload_list_qualified (const char *func_name);
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,
140 /* Copy the symbol's name from RET_COMP and look it up
141 in the symbol table. */
142 name = (char *) alloca (ret_comp->u.s_name.len + 1);
143 memcpy (name, ret_comp->u.s_name.s, ret_comp->u.s_name.len);
144 name[ret_comp->u.s_name.len] = '\0';
146 /* Ignore any typedefs that should not be substituted. */
147 for (i = 0; i < ARRAY_SIZE (ignore_typedefs); ++i)
149 if (strcmp (name, ignore_typedefs[i]) == 0)
157 sym = lookup_symbol (name, 0, VAR_DOMAIN, 0).symbol;
159 CATCH (except, RETURN_MASK_ALL)
167 struct type *otype = SYMBOL_TYPE (sym);
171 const char *new_name = (*finder) (otype, data);
173 if (new_name != NULL)
175 ret_comp->u.s_name.s = new_name;
176 ret_comp->u.s_name.len = strlen (new_name);
183 /* If the type is a typedef or namespace alias, replace it. */
184 if (TYPE_CODE (otype) == TYPE_CODE_TYPEDEF
185 || TYPE_CODE (otype) == TYPE_CODE_NAMESPACE)
190 std::unique_ptr<demangle_parse_info> i;
192 /* Get the real type of the typedef. */
193 type = check_typedef (otype);
195 /* If the symbol is a namespace and its type name is no different
196 than the name we looked up, this symbol is not a namespace
197 alias and does not need to be substituted. */
198 if (TYPE_CODE (otype) == TYPE_CODE_NAMESPACE
199 && strcmp (TYPE_NAME (type), name) == 0)
202 is_anon = (TYPE_TAG_NAME (type) == NULL
203 && (TYPE_CODE (type) == TYPE_CODE_ENUM
204 || TYPE_CODE (type) == TYPE_CODE_STRUCT
205 || TYPE_CODE (type) == TYPE_CODE_UNION));
208 struct type *last = otype;
210 /* Find the last typedef for the type. */
211 while (TYPE_TARGET_TYPE (last) != NULL
212 && (TYPE_CODE (TYPE_TARGET_TYPE (last))
213 == TYPE_CODE_TYPEDEF))
214 last = TYPE_TARGET_TYPE (last);
216 /* If there is only one typedef for this anonymous type,
217 do not substitute it. */
221 /* Use the last typedef seen as the type for this
229 type_print (type, "", &buf, -1);
231 /* If type_print threw an exception, there is little point
232 in continuing, so just bow out gracefully. */
233 CATCH (except, RETURN_MASK_ERROR)
240 name = (char *) obstack_copy0 (&info->obstack, buf.c_str (), len);
242 /* Turn the result into a new tree. Note that this
243 tree will contain pointers into NAME, so NAME cannot
244 be free'd until all typedef conversion is done and
245 the final result is converted into a string. */
246 i = cp_demangled_name_to_comp (name, NULL);
249 /* Merge the two trees. */
250 cp_merge_demangle_parse_infos (info, ret_comp, i.get ());
252 /* Replace any newly introduced typedefs -- but not
253 if the type is anonymous (that would lead to infinite
256 replace_typedefs (info, ret_comp, finder, data);
260 /* This shouldn't happen unless the type printer has
261 output something that the name parser cannot grok.
262 Nonetheless, an ounce of prevention...
264 Canonicalize the name again, and store it in the
265 current node (RET_COMP). */
266 std::string canon = cp_canonicalize_string_no_typedefs (name);
270 /* Copy the canonicalization into the obstack. */
271 name = copy_string_to_obstack (&info->obstack, canon.c_str (), &len);
274 ret_comp->u.s_name.s = name;
275 ret_comp->u.s_name.len = len;
285 /* Replace any typedefs appearing in the qualified name
286 (DEMANGLE_COMPONENT_QUAL_NAME) represented in RET_COMP for the name parse
290 replace_typedefs_qualified_name (struct demangle_parse_info *info,
291 struct demangle_component *ret_comp,
292 canonicalization_ftype *finder,
296 struct demangle_component *comp = ret_comp;
298 /* Walk each node of the qualified name, reconstructing the name of
299 this element. With every node, check for any typedef substitutions.
300 If a substitution has occurred, replace the qualified name node
301 with a DEMANGLE_COMPONENT_NAME node representing the new, typedef-
303 while (comp->type == DEMANGLE_COMPONENT_QUAL_NAME)
305 if (d_left (comp)->type == DEMANGLE_COMPONENT_NAME)
307 struct demangle_component newobj;
309 buf.write (d_left (comp)->u.s_name.s, d_left (comp)->u.s_name.len);
310 newobj.type = DEMANGLE_COMPONENT_NAME;
312 = (char *) obstack_copy0 (&info->obstack,
313 buf.c_str (), buf.size ());
314 newobj.u.s_name.len = buf.size ();
315 if (inspect_type (info, &newobj, finder, data))
320 /* A typedef was substituted in NEW. Convert it to a
321 string and replace the top DEMANGLE_COMPONENT_QUAL_NAME
325 gdb::unique_xmalloc_ptr<char> n
326 = cp_comp_to_string (&newobj, 100);
329 /* If something went astray, abort typedef substitutions. */
333 s = copy_string_to_obstack (&info->obstack, n.get (), &slen);
335 d_left (ret_comp)->type = DEMANGLE_COMPONENT_NAME;
336 d_left (ret_comp)->u.s_name.s = s;
337 d_left (ret_comp)->u.s_name.len = slen;
338 d_right (ret_comp) = d_right (comp);
345 /* The current node is not a name, so simply replace any
346 typedefs in it. Then print it to the stream to continue
347 checking for more typedefs in the tree. */
348 replace_typedefs (info, d_left (comp), finder, data);
349 gdb::unique_xmalloc_ptr<char> name
350 = cp_comp_to_string (d_left (comp), 100);
353 /* If something went astray, abort typedef substitutions. */
356 buf.puts (name.get ());
360 comp = d_right (comp);
363 /* If the next component is DEMANGLE_COMPONENT_NAME, save the qualified
364 name assembled above and append the name given by COMP. Then use this
365 reassembled name to check for a typedef. */
367 if (comp->type == DEMANGLE_COMPONENT_NAME)
369 buf.write (comp->u.s_name.s, comp->u.s_name.len);
371 /* Replace the top (DEMANGLE_COMPONENT_QUAL_NAME) node
372 with a DEMANGLE_COMPONENT_NAME node containing the whole
374 ret_comp->type = DEMANGLE_COMPONENT_NAME;
376 = (char *) obstack_copy0 (&info->obstack,
377 buf.c_str (), buf.size ());
378 ret_comp->u.s_name.len = buf.size ();
379 inspect_type (info, ret_comp, finder, data);
382 replace_typedefs (info, comp, finder, data);
386 /* A function to check const and volatile qualifiers for argument types.
388 "Parameter declarations that differ only in the presence
389 or absence of `const' and/or `volatile' are equivalent."
390 C++ Standard N3290, clause 13.1.3 #4. */
393 check_cv_qualifiers (struct demangle_component *ret_comp)
395 while (d_left (ret_comp) != NULL
396 && (d_left (ret_comp)->type == DEMANGLE_COMPONENT_CONST
397 || d_left (ret_comp)->type == DEMANGLE_COMPONENT_VOLATILE))
399 d_left (ret_comp) = d_left (d_left (ret_comp));
403 /* Walk the parse tree given by RET_COMP, replacing any typedefs with
404 their basic types. */
407 replace_typedefs (struct demangle_parse_info *info,
408 struct demangle_component *ret_comp,
409 canonicalization_ftype *finder,
415 && (ret_comp->type == DEMANGLE_COMPONENT_NAME
416 || ret_comp->type == DEMANGLE_COMPONENT_QUAL_NAME
417 || ret_comp->type == DEMANGLE_COMPONENT_TEMPLATE
418 || ret_comp->type == DEMANGLE_COMPONENT_BUILTIN_TYPE))
420 gdb::unique_xmalloc_ptr<char> local_name
421 = cp_comp_to_string (ret_comp, 10);
423 if (local_name != NULL)
425 struct symbol *sym = NULL;
430 sym = lookup_symbol (local_name.get (), 0,
431 VAR_DOMAIN, 0).symbol;
433 CATCH (except, RETURN_MASK_ALL)
440 struct type *otype = SYMBOL_TYPE (sym);
441 const char *new_name = (*finder) (otype, data);
443 if (new_name != NULL)
445 ret_comp->type = DEMANGLE_COMPONENT_NAME;
446 ret_comp->u.s_name.s = new_name;
447 ret_comp->u.s_name.len = strlen (new_name);
454 switch (ret_comp->type)
456 case DEMANGLE_COMPONENT_ARGLIST:
457 check_cv_qualifiers (ret_comp);
460 case DEMANGLE_COMPONENT_FUNCTION_TYPE:
461 case DEMANGLE_COMPONENT_TEMPLATE:
462 case DEMANGLE_COMPONENT_TEMPLATE_ARGLIST:
463 case DEMANGLE_COMPONENT_TYPED_NAME:
464 replace_typedefs (info, d_left (ret_comp), finder, data);
465 replace_typedefs (info, d_right (ret_comp), finder, data);
468 case DEMANGLE_COMPONENT_NAME:
469 inspect_type (info, ret_comp, finder, data);
472 case DEMANGLE_COMPONENT_QUAL_NAME:
473 replace_typedefs_qualified_name (info, ret_comp, finder, data);
476 case DEMANGLE_COMPONENT_LOCAL_NAME:
477 case DEMANGLE_COMPONENT_CTOR:
478 case DEMANGLE_COMPONENT_ARRAY_TYPE:
479 case DEMANGLE_COMPONENT_PTRMEM_TYPE:
480 replace_typedefs (info, d_right (ret_comp), finder, data);
483 case DEMANGLE_COMPONENT_CONST:
484 case DEMANGLE_COMPONENT_RESTRICT:
485 case DEMANGLE_COMPONENT_VOLATILE:
486 case DEMANGLE_COMPONENT_VOLATILE_THIS:
487 case DEMANGLE_COMPONENT_CONST_THIS:
488 case DEMANGLE_COMPONENT_RESTRICT_THIS:
489 case DEMANGLE_COMPONENT_POINTER:
490 case DEMANGLE_COMPONENT_REFERENCE:
491 case DEMANGLE_COMPONENT_RVALUE_REFERENCE:
492 replace_typedefs (info, d_left (ret_comp), finder, data);
501 /* Parse STRING and convert it to canonical form, resolving any
502 typedefs. If parsing fails, or if STRING is already canonical,
503 return the empty string. Otherwise return the canonical form. If
504 FINDER is not NULL, then type components are passed to FINDER to be
505 looked up. DATA is passed verbatim to FINDER. */
508 cp_canonicalize_string_full (const char *string,
509 canonicalization_ftype *finder,
513 unsigned int estimated_len;
514 std::unique_ptr<demangle_parse_info> info;
516 estimated_len = strlen (string) * 2;
517 info = cp_demangled_name_to_comp (string, NULL);
520 /* Replace all the typedefs in the tree. */
521 replace_typedefs (info.get (), info->tree, finder, data);
523 /* Convert the tree back into a string. */
524 gdb::unique_xmalloc_ptr<char> us = cp_comp_to_string (info->tree,
529 /* Finally, compare the original string with the computed
530 name, returning NULL if they are the same. */
532 return std::string ();
538 /* Like cp_canonicalize_string_full, but always passes NULL for
542 cp_canonicalize_string_no_typedefs (const char *string)
544 return cp_canonicalize_string_full (string, NULL, NULL);
547 /* Parse STRING and convert it to canonical form. If parsing fails,
548 or if STRING is already canonical, return the empty string.
549 Otherwise return the canonical form. */
552 cp_canonicalize_string (const char *string)
554 std::unique_ptr<demangle_parse_info> info;
555 unsigned int estimated_len;
557 if (cp_already_canonical (string))
558 return std::string ();
560 info = cp_demangled_name_to_comp (string, NULL);
562 return std::string ();
564 estimated_len = strlen (string) * 2;
565 gdb::unique_xmalloc_ptr<char> us (cp_comp_to_string (info->tree,
570 warning (_("internal error: string \"%s\" failed to be canonicalized"),
572 return std::string ();
575 std::string ret (us.get ());
578 return std::string ();
583 /* Convert a mangled name to a demangle_component tree. *MEMORY is
584 set to the block of used memory that should be freed when finished
585 with the tree. DEMANGLED_P is set to the char * that should be
586 freed when finished with the tree, or NULL if none was needed.
587 OPTIONS will be passed to the demangler. */
589 static std::unique_ptr<demangle_parse_info>
590 mangled_name_to_comp (const char *mangled_name, int options,
591 void **memory, char **demangled_p)
593 char *demangled_name;
595 /* If it looks like a v3 mangled name, then try to go directly
597 if (mangled_name[0] == '_' && mangled_name[1] == 'Z')
599 struct demangle_component *ret;
601 ret = cplus_demangle_v3_components (mangled_name,
605 std::unique_ptr<demangle_parse_info> info (new demangle_parse_info);
612 /* If it doesn't, or if that failed, then try to demangle the
614 demangled_name = gdb_demangle (mangled_name, options);
615 if (demangled_name == NULL)
618 /* If we could demangle the name, parse it to build the component
620 std::unique_ptr<demangle_parse_info> info
621 = cp_demangled_name_to_comp (demangled_name, NULL);
625 xfree (demangled_name);
629 *demangled_p = demangled_name;
633 /* Return the name of the class containing method PHYSNAME. */
636 cp_class_name_from_physname (const char *physname)
638 void *storage = NULL;
639 char *demangled_name = NULL;
640 gdb::unique_xmalloc_ptr<char> ret;
641 struct demangle_component *ret_comp, *prev_comp, *cur_comp;
642 std::unique_ptr<demangle_parse_info> info;
645 info = mangled_name_to_comp (physname, DMGL_ANSI,
646 &storage, &demangled_name);
651 ret_comp = info->tree;
653 /* First strip off any qualifiers, if we have a function or
656 switch (ret_comp->type)
658 case DEMANGLE_COMPONENT_CONST:
659 case DEMANGLE_COMPONENT_RESTRICT:
660 case DEMANGLE_COMPONENT_VOLATILE:
661 case DEMANGLE_COMPONENT_CONST_THIS:
662 case DEMANGLE_COMPONENT_RESTRICT_THIS:
663 case DEMANGLE_COMPONENT_VOLATILE_THIS:
664 case DEMANGLE_COMPONENT_VENDOR_TYPE_QUAL:
665 ret_comp = d_left (ret_comp);
672 /* If what we have now is a function, discard the argument list. */
673 if (ret_comp->type == DEMANGLE_COMPONENT_TYPED_NAME)
674 ret_comp = d_left (ret_comp);
676 /* If what we have now is a template, strip off the template
677 arguments. The left subtree may be a qualified name. */
678 if (ret_comp->type == DEMANGLE_COMPONENT_TEMPLATE)
679 ret_comp = d_left (ret_comp);
681 /* What we have now should be a name, possibly qualified.
682 Additional qualifiers could live in the left subtree or the right
683 subtree. Find the last piece. */
688 switch (cur_comp->type)
690 case DEMANGLE_COMPONENT_QUAL_NAME:
691 case DEMANGLE_COMPONENT_LOCAL_NAME:
692 prev_comp = cur_comp;
693 cur_comp = d_right (cur_comp);
695 case DEMANGLE_COMPONENT_TEMPLATE:
696 case DEMANGLE_COMPONENT_NAME:
697 case DEMANGLE_COMPONENT_CTOR:
698 case DEMANGLE_COMPONENT_DTOR:
699 case DEMANGLE_COMPONENT_OPERATOR:
700 case DEMANGLE_COMPONENT_EXTENDED_OPERATOR:
709 if (cur_comp != NULL && prev_comp != NULL)
711 /* We want to discard the rightmost child of PREV_COMP. */
712 *prev_comp = *d_left (prev_comp);
713 /* The ten is completely arbitrary; we don't have a good
715 ret = cp_comp_to_string (ret_comp, 10);
719 xfree (demangled_name);
720 return ret.release ();
723 /* Return the child of COMP which is the basename of a method,
724 variable, et cetera. All scope qualifiers are discarded, but
725 template arguments will be included. The component tree may be
728 static struct demangle_component *
729 unqualified_name_from_comp (struct demangle_component *comp)
731 struct demangle_component *ret_comp = comp, *last_template;
735 last_template = NULL;
737 switch (ret_comp->type)
739 case DEMANGLE_COMPONENT_QUAL_NAME:
740 case DEMANGLE_COMPONENT_LOCAL_NAME:
741 ret_comp = d_right (ret_comp);
743 case DEMANGLE_COMPONENT_TYPED_NAME:
744 ret_comp = d_left (ret_comp);
746 case DEMANGLE_COMPONENT_TEMPLATE:
747 gdb_assert (last_template == NULL);
748 last_template = ret_comp;
749 ret_comp = d_left (ret_comp);
751 case DEMANGLE_COMPONENT_CONST:
752 case DEMANGLE_COMPONENT_RESTRICT:
753 case DEMANGLE_COMPONENT_VOLATILE:
754 case DEMANGLE_COMPONENT_CONST_THIS:
755 case DEMANGLE_COMPONENT_RESTRICT_THIS:
756 case DEMANGLE_COMPONENT_VOLATILE_THIS:
757 case DEMANGLE_COMPONENT_VENDOR_TYPE_QUAL:
758 ret_comp = d_left (ret_comp);
760 case DEMANGLE_COMPONENT_NAME:
761 case DEMANGLE_COMPONENT_CTOR:
762 case DEMANGLE_COMPONENT_DTOR:
763 case DEMANGLE_COMPONENT_OPERATOR:
764 case DEMANGLE_COMPONENT_EXTENDED_OPERATOR:
774 d_left (last_template) = ret_comp;
775 return last_template;
781 /* Return the name of the method whose linkage name is PHYSNAME. */
784 method_name_from_physname (const char *physname)
786 void *storage = NULL;
787 char *demangled_name = NULL;
788 gdb::unique_xmalloc_ptr<char> ret;
789 struct demangle_component *ret_comp;
790 std::unique_ptr<demangle_parse_info> info;
792 info = mangled_name_to_comp (physname, DMGL_ANSI,
793 &storage, &demangled_name);
797 ret_comp = unqualified_name_from_comp (info->tree);
799 if (ret_comp != NULL)
800 /* The ten is completely arbitrary; we don't have a good
802 ret = cp_comp_to_string (ret_comp, 10);
805 xfree (demangled_name);
806 return ret.release ();
809 /* If FULL_NAME is the demangled name of a C++ function (including an
810 arg list, possibly including namespace/class qualifications),
811 return a new string containing only the function name (without the
812 arg list/class qualifications). Otherwise, return NULL. The
813 caller is responsible for freeing the memory in question. */
816 cp_func_name (const char *full_name)
818 gdb::unique_xmalloc_ptr<char> ret;
819 struct demangle_component *ret_comp;
820 std::unique_ptr<demangle_parse_info> info;
822 info = cp_demangled_name_to_comp (full_name, NULL);
826 ret_comp = unqualified_name_from_comp (info->tree);
828 if (ret_comp != NULL)
829 ret = cp_comp_to_string (ret_comp, 10);
831 return ret.release ();
834 /* Helper for cp_remove_params. DEMANGLED_NAME is the name of a
835 function, including parameters and (optionally) a return type.
836 Return the name of the function without parameters or return type,
837 or NULL if we can not parse the name. If REQUIRE_PARAMS is false,
838 then tolerate a non-existing or unbalanced parameter list. */
840 static gdb::unique_xmalloc_ptr<char>
841 cp_remove_params_1 (const char *demangled_name, bool require_params)
844 struct demangle_component *ret_comp;
845 std::unique_ptr<demangle_parse_info> info;
846 gdb::unique_xmalloc_ptr<char> ret;
848 if (demangled_name == NULL)
851 info = cp_demangled_name_to_comp (demangled_name, NULL);
855 /* First strip off any qualifiers, if we have a function or method. */
856 ret_comp = info->tree;
858 switch (ret_comp->type)
860 case DEMANGLE_COMPONENT_CONST:
861 case DEMANGLE_COMPONENT_RESTRICT:
862 case DEMANGLE_COMPONENT_VOLATILE:
863 case DEMANGLE_COMPONENT_CONST_THIS:
864 case DEMANGLE_COMPONENT_RESTRICT_THIS:
865 case DEMANGLE_COMPONENT_VOLATILE_THIS:
866 case DEMANGLE_COMPONENT_VENDOR_TYPE_QUAL:
867 ret_comp = d_left (ret_comp);
874 /* What we have now should be a function. Return its name. */
875 if (ret_comp->type == DEMANGLE_COMPONENT_TYPED_NAME)
876 ret = cp_comp_to_string (d_left (ret_comp), 10);
877 else if (!require_params
878 && (ret_comp->type == DEMANGLE_COMPONENT_NAME
879 || ret_comp->type == DEMANGLE_COMPONENT_QUAL_NAME
880 || ret_comp->type == DEMANGLE_COMPONENT_TEMPLATE))
881 ret = cp_comp_to_string (ret_comp, 10);
886 /* DEMANGLED_NAME is the name of a function, including parameters and
887 (optionally) a return type. Return the name of the function
888 without parameters or return type, or NULL if we can not parse the
891 gdb::unique_xmalloc_ptr<char>
892 cp_remove_params (const char *demangled_name)
894 return cp_remove_params_1 (demangled_name, true);
897 /* See cp-support.h. */
899 gdb::unique_xmalloc_ptr<char>
900 cp_remove_params_if_any (const char *demangled_name, bool completion_mode)
902 /* Trying to remove parameters from the empty string fails. If
903 we're completing / matching everything, avoid returning NULL
904 which would make callers interpret the result as an error. */
905 if (demangled_name[0] == '\0' && completion_mode)
906 return gdb::unique_xmalloc_ptr<char> (xstrdup (""));
908 gdb::unique_xmalloc_ptr<char> without_params
909 = cp_remove_params_1 (demangled_name, false);
911 if (without_params == NULL && completion_mode)
913 std::string copy = demangled_name;
915 while (!copy.empty ())
918 without_params = cp_remove_params_1 (copy.c_str (), false);
919 if (without_params != NULL)
924 return without_params;
927 /* Here are some random pieces of trivia to keep in mind while trying
928 to take apart demangled names:
930 - Names can contain function arguments or templates, so the process
931 has to be, to some extent recursive: maybe keep track of your
932 depth based on encountering <> and ().
934 - Parentheses don't just have to happen at the end of a name: they
935 can occur even if the name in question isn't a function, because
936 a template argument might be a type that's a function.
938 - Conversely, even if you're trying to deal with a function, its
939 demangled name might not end with ')': it could be a const or
940 volatile class method, in which case it ends with "const" or
943 - Parentheses are also used in anonymous namespaces: a variable
944 'foo' in an anonymous namespace gets demangled as "(anonymous
947 - And operator names can contain parentheses or angle brackets. */
949 /* FIXME: carlton/2003-03-13: We have several functions here with
950 overlapping functionality; can we combine them? Also, do they
951 handle all the above considerations correctly? */
954 /* This returns the length of first component of NAME, which should be
955 the demangled name of a C++ variable/function/method/etc.
956 Specifically, it returns the index of the first colon forming the
957 boundary of the first component: so, given 'A::foo' or 'A::B::foo'
958 it returns the 1, and given 'foo', it returns 0. */
960 /* The character in NAME indexed by the return value is guaranteed to
961 always be either ':' or '\0'. */
963 /* NOTE: carlton/2003-03-13: This function is currently only intended
964 for internal use: it's probably not entirely safe when called on
965 user-generated input, because some of the 'index += 2' lines in
966 cp_find_first_component_aux might go past the end of malformed
970 cp_find_first_component (const char *name)
972 return cp_find_first_component_aux (name, 0);
975 /* Helper function for cp_find_first_component. Like that function,
976 it returns the length of the first component of NAME, but to make
977 the recursion easier, it also stops if it reaches an unexpected ')'
978 or '>' if the value of PERMISSIVE is nonzero. */
981 cp_find_first_component_aux (const char *name, int permissive)
983 unsigned int index = 0;
984 /* Operator names can show up in unexpected places. Since these can
985 contain parentheses or angle brackets, they can screw up the
986 recursion. But not every string 'operator' is part of an
987 operater name: e.g. you could have a variable 'cooperator'. So
988 this variable tells us whether or not we should treat the string
989 'operator' as starting an operator. */
990 int operator_possible = 1;
997 /* Template; eat it up. The calls to cp_first_component
998 should only return (I hope!) when they reach the '>'
999 terminating the component or a '::' between two
1000 components. (Hence the '+ 2'.) */
1002 for (index += cp_find_first_component_aux (name + index, 1);
1004 index += cp_find_first_component_aux (name + index, 1))
1006 if (name[index] != ':')
1008 demangled_name_complaint (name);
1009 return strlen (name);
1013 operator_possible = 1;
1016 /* Similar comment as to '<'. */
1018 for (index += cp_find_first_component_aux (name + index, 1);
1020 index += cp_find_first_component_aux (name + index, 1))
1022 if (name[index] != ':')
1024 demangled_name_complaint (name);
1025 return strlen (name);
1029 operator_possible = 1;
1037 demangled_name_complaint (name);
1038 return strlen (name);
1043 /* ':' marks a component iff the next character is also a ':'.
1044 Otherwise it is probably malformed input. */
1045 if (name[index + 1] == ':')
1049 /* Operator names can screw up the recursion. */
1050 if (operator_possible
1051 && startswith (name + index, CP_OPERATOR_STR))
1053 index += CP_OPERATOR_LEN;
1054 while (ISSPACE(name[index]))
1056 switch (name[index])
1060 /* Skip over one less than the appropriate number of
1061 characters: the for loop will skip over the last
1064 if (name[index + 1] == '<')
1071 if (name[index + 1] == '>')
1084 operator_possible = 0;
1091 /* NOTE: carlton/2003-04-18: I'm not sure what the precise
1092 set of relevant characters are here: it's necessary to
1093 include any character that can show up before 'operator'
1094 in a demangled name, and it's safe to include any
1095 character that can't be part of an identifier's name. */
1096 operator_possible = 1;
1099 operator_possible = 0;
1105 /* Complain about a demangled name that we don't know how to parse.
1106 NAME is the demangled name in question. */
1109 demangled_name_complaint (const char *name)
1111 complaint (&symfile_complaints,
1112 "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 the current
1146 overload_list_add_symbol (struct symbol *sym,
1147 const char *oload_name)
1151 gdb::unique_xmalloc_ptr<char> sym_name;
1153 /* If there is no type information, we can't do anything, so
1155 if (SYMBOL_TYPE (sym) == NULL)
1158 /* skip any symbols that we've already considered. */
1159 for (i = 0; i < sym_return_val_index; ++i)
1160 if (strcmp (SYMBOL_LINKAGE_NAME (sym),
1161 SYMBOL_LINKAGE_NAME (sym_return_val[i])) == 0)
1164 /* Get the demangled name without parameters */
1165 sym_name = cp_remove_params (SYMBOL_NATURAL_NAME (sym));
1169 /* skip symbols that cannot match */
1170 if (strcmp (sym_name.get (), oload_name) != 0)
1173 /* We have a match for an overload instance, so add SYM to the
1174 current list of overload instances */
1175 if (sym_return_val_index + 3 > sym_return_val_size)
1177 newsize = (sym_return_val_size *= 2) * sizeof (struct symbol *);
1178 sym_return_val = (struct symbol **)
1179 xrealloc ((char *) sym_return_val, newsize);
1181 sym_return_val[sym_return_val_index++] = sym;
1182 sym_return_val[sym_return_val_index] = NULL;
1185 /* Return a null-terminated list of pointers to function symbols that
1186 are named FUNC_NAME and are visible within NAMESPACE. */
1189 make_symbol_overload_list (const char *func_name,
1190 const char *the_namespace)
1192 struct cleanup *old_cleanups;
1195 sym_return_val_size = 100;
1196 sym_return_val_index = 0;
1197 sym_return_val = XNEWVEC (struct symbol *, sym_return_val_size + 1);
1198 sym_return_val[0] = NULL;
1200 old_cleanups = make_cleanup (xfree, sym_return_val);
1202 make_symbol_overload_list_using (func_name, the_namespace);
1204 if (the_namespace[0] == '\0')
1208 char *concatenated_name
1209 = (char *) alloca (strlen (the_namespace) + 2 + strlen (func_name) + 1);
1210 strcpy (concatenated_name, the_namespace);
1211 strcat (concatenated_name, "::");
1212 strcat (concatenated_name, func_name);
1213 name = concatenated_name;
1216 make_symbol_overload_list_qualified (name);
1218 discard_cleanups (old_cleanups);
1220 return sym_return_val;
1223 /* Add all symbols with a name matching NAME in BLOCK to the overload
1227 make_symbol_overload_list_block (const char *name,
1228 const struct block *block)
1230 struct block_iterator iter;
1233 lookup_name_info lookup_name (name, symbol_name_match_type::FULL);
1235 ALL_BLOCK_SYMBOLS_WITH_NAME (block, lookup_name, iter, sym)
1236 overload_list_add_symbol (sym, name);
1239 /* Adds the function FUNC_NAME from NAMESPACE to the overload set. */
1242 make_symbol_overload_list_namespace (const char *func_name,
1243 const char *the_namespace)
1246 const struct block *block = NULL;
1248 if (the_namespace[0] == '\0')
1252 char *concatenated_name
1253 = (char *) alloca (strlen (the_namespace) + 2 + strlen (func_name) + 1);
1255 strcpy (concatenated_name, the_namespace);
1256 strcat (concatenated_name, "::");
1257 strcat (concatenated_name, func_name);
1258 name = concatenated_name;
1261 /* Look in the static block. */
1262 block = block_static_block (get_selected_block (0));
1264 make_symbol_overload_list_block (name, block);
1266 /* Look in the global block. */
1267 block = block_global_block (block);
1269 make_symbol_overload_list_block (name, block);
1273 /* Search the namespace of the given type and namespace of and public
1277 make_symbol_overload_list_adl_namespace (struct type *type,
1278 const char *func_name)
1280 char *the_namespace;
1281 const char *type_name;
1284 while (TYPE_CODE (type) == TYPE_CODE_PTR
1285 || TYPE_IS_REFERENCE (type)
1286 || TYPE_CODE (type) == TYPE_CODE_ARRAY
1287 || TYPE_CODE (type) == TYPE_CODE_TYPEDEF)
1289 if (TYPE_CODE (type) == TYPE_CODE_TYPEDEF)
1290 type = check_typedef(type);
1292 type = TYPE_TARGET_TYPE (type);
1295 type_name = TYPE_NAME (type);
1297 if (type_name == NULL)
1300 prefix_len = cp_entire_prefix_len (type_name);
1302 if (prefix_len != 0)
1304 the_namespace = (char *) alloca (prefix_len + 1);
1305 strncpy (the_namespace, type_name, prefix_len);
1306 the_namespace[prefix_len] = '\0';
1308 make_symbol_overload_list_namespace (func_name, the_namespace);
1311 /* Check public base type */
1312 if (TYPE_CODE (type) == TYPE_CODE_STRUCT)
1313 for (i = 0; i < TYPE_N_BASECLASSES (type); i++)
1315 if (BASETYPE_VIA_PUBLIC (type, i))
1316 make_symbol_overload_list_adl_namespace (TYPE_BASECLASS (type,
1322 /* Adds the overload list overload candidates for FUNC_NAME found
1323 through argument dependent lookup. */
1326 make_symbol_overload_list_adl (struct type **arg_types, int nargs,
1327 const char *func_name)
1331 gdb_assert (sym_return_val_size != -1);
1333 for (i = 1; i <= nargs; i++)
1334 make_symbol_overload_list_adl_namespace (arg_types[i - 1],
1337 return sym_return_val;
1340 /* This applies the using directives to add namespaces to search in,
1341 and then searches for overloads in all of those namespaces. It
1342 adds the symbols found to sym_return_val. Arguments are as in
1343 make_symbol_overload_list. */
1346 make_symbol_overload_list_using (const char *func_name,
1347 const char *the_namespace)
1349 struct using_direct *current;
1350 const struct block *block;
1352 /* First, go through the using directives. If any of them apply,
1353 look in the appropriate namespaces for new functions to match
1356 for (block = get_selected_block (0);
1358 block = BLOCK_SUPERBLOCK (block))
1359 for (current = block_using (block);
1361 current = current->next)
1363 /* Prevent recursive calls. */
1364 if (current->searched)
1367 /* If this is a namespace alias or imported declaration ignore
1369 if (current->alias != NULL || current->declaration != NULL)
1372 if (strcmp (the_namespace, current->import_dest) == 0)
1374 /* Mark this import as searched so that the recursive call
1375 does not search it again. */
1376 scoped_restore reset_directive_searched
1377 = make_scoped_restore (¤t->searched, 1);
1379 make_symbol_overload_list_using (func_name,
1380 current->import_src);
1384 /* Now, add names for this namespace. */
1385 make_symbol_overload_list_namespace (func_name, the_namespace);
1388 /* This does the bulk of the work of finding overloaded symbols.
1389 FUNC_NAME is the name of the overloaded function we're looking for
1390 (possibly including namespace info). */
1393 make_symbol_overload_list_qualified (const char *func_name)
1395 struct compunit_symtab *cust;
1396 struct objfile *objfile;
1397 const struct block *b, *surrounding_static_block = 0;
1399 /* Look through the partial symtabs for all symbols which begin by
1400 matching FUNC_NAME. Make sure we read that symbol table in. */
1402 ALL_OBJFILES (objfile)
1405 objfile->sf->qf->expand_symtabs_for_function (objfile, func_name);
1408 /* Search upwards from currently selected frame (so that we can
1409 complete on local vars. */
1411 for (b = get_selected_block (0); b != NULL; b = BLOCK_SUPERBLOCK (b))
1412 make_symbol_overload_list_block (func_name, b);
1414 surrounding_static_block = block_static_block (get_selected_block (0));
1416 /* Go through the symtabs and check the externs and statics for
1417 symbols which match. */
1419 ALL_COMPUNITS (objfile, cust)
1422 b = BLOCKVECTOR_BLOCK (COMPUNIT_BLOCKVECTOR (cust), GLOBAL_BLOCK);
1423 make_symbol_overload_list_block (func_name, b);
1426 ALL_COMPUNITS (objfile, cust)
1429 b = BLOCKVECTOR_BLOCK (COMPUNIT_BLOCKVECTOR (cust), STATIC_BLOCK);
1430 /* Don't do this block twice. */
1431 if (b == surrounding_static_block)
1433 make_symbol_overload_list_block (func_name, b);
1437 /* Lookup the rtti type for a class name. */
1440 cp_lookup_rtti_type (const char *name, struct block *block)
1442 struct symbol * rtti_sym;
1443 struct type * rtti_type;
1445 /* Use VAR_DOMAIN here as NAME may be a typedef. PR 18141, 18417.
1446 Classes "live" in both STRUCT_DOMAIN and VAR_DOMAIN. */
1447 rtti_sym = lookup_symbol (name, block, VAR_DOMAIN, NULL).symbol;
1449 if (rtti_sym == NULL)
1451 warning (_("RTTI symbol not found for class '%s'"), name);
1455 if (SYMBOL_CLASS (rtti_sym) != LOC_TYPEDEF)
1457 warning (_("RTTI symbol for class '%s' is not a type"), name);
1461 rtti_type = check_typedef (SYMBOL_TYPE (rtti_sym));
1463 switch (TYPE_CODE (rtti_type))
1465 case TYPE_CODE_STRUCT:
1467 case TYPE_CODE_NAMESPACE:
1468 /* chastain/2003-11-26: the symbol tables often contain fake
1469 symbols for namespaces with the same name as the struct.
1470 This warning is an indication of a bug in the lookup order
1471 or a bug in the way that the symbol tables are populated. */
1472 warning (_("RTTI symbol for class '%s' is a namespace"), name);
1475 warning (_("RTTI symbol for class '%s' has bad type"), name);
1482 #ifdef HAVE_WORKING_FORK
1484 /* If nonzero, attempt to catch crashes in the demangler and print
1485 useful debugging information. */
1487 static int catch_demangler_crashes = 1;
1489 /* Stack context and environment for demangler crash recovery. */
1491 static SIGJMP_BUF gdb_demangle_jmp_buf;
1493 /* If nonzero, attempt to dump core from the signal handler. */
1495 static int gdb_demangle_attempt_core_dump = 1;
1497 /* Signal handler for gdb_demangle. */
1500 gdb_demangle_signal_handler (int signo)
1502 if (gdb_demangle_attempt_core_dump)
1507 gdb_demangle_attempt_core_dump = 0;
1510 SIGLONGJMP (gdb_demangle_jmp_buf, signo);
1515 /* A wrapper for bfd_demangle. */
1518 gdb_demangle (const char *name, int options)
1520 char *result = NULL;
1521 int crash_signal = 0;
1523 #ifdef HAVE_WORKING_FORK
1524 #if defined (HAVE_SIGACTION) && defined (SA_RESTART)
1525 struct sigaction sa, old_sa;
1529 static int core_dump_allowed = -1;
1531 if (core_dump_allowed == -1)
1533 core_dump_allowed = can_dump_core (LIMIT_CUR);
1535 if (!core_dump_allowed)
1536 gdb_demangle_attempt_core_dump = 0;
1539 if (catch_demangler_crashes)
1541 #if defined (HAVE_SIGACTION) && defined (SA_RESTART)
1542 sa.sa_handler = gdb_demangle_signal_handler;
1543 sigemptyset (&sa.sa_mask);
1544 #ifdef HAVE_SIGALTSTACK
1545 sa.sa_flags = SA_ONSTACK;
1549 sigaction (SIGSEGV, &sa, &old_sa);
1551 ofunc = signal (SIGSEGV, gdb_demangle_signal_handler);
1554 crash_signal = SIGSETJMP (gdb_demangle_jmp_buf);
1558 if (crash_signal == 0)
1559 result = bfd_demangle (NULL, name, options);
1561 #ifdef HAVE_WORKING_FORK
1562 if (catch_demangler_crashes)
1564 #if defined (HAVE_SIGACTION) && defined (SA_RESTART)
1565 sigaction (SIGSEGV, &old_sa, NULL);
1567 signal (SIGSEGV, ofunc);
1570 if (crash_signal != 0)
1572 static int error_reported = 0;
1574 if (!error_reported)
1576 std::string short_msg
1577 = string_printf (_("unable to demangle '%s' "
1578 "(demangler failed with signal %d)"),
1579 name, crash_signal);
1581 std::string long_msg
1582 = string_printf ("%s:%d: %s: %s", __FILE__, __LINE__,
1583 "demangler-warning", short_msg.c_str ());
1585 target_terminal::scoped_restore_terminal_state term_state;
1586 target_terminal::ours_for_output ();
1589 if (core_dump_allowed)
1590 fprintf_unfiltered (gdb_stderr,
1591 _("%s\nAttempting to dump core.\n"),
1594 warn_cant_dump_core (long_msg.c_str ());
1596 demangler_warning (__FILE__, __LINE__, "%s", short_msg.c_str ());
1609 /* See cp-support.h. */
1612 gdb_sniff_from_mangled_name (const char *mangled, char **demangled)
1614 *demangled = gdb_demangle (mangled, DMGL_PARAMS | DMGL_ANSI);
1615 return *demangled != NULL;
1618 /* C++ symbol_name_matcher_ftype implementation. */
1620 /* Helper for cp_fq_symbol_name_matches (i.e.,
1621 symbol_name_matcher_ftype implementation). Split to a separate
1622 function for unit-testing convenience.
1624 See symbol_name_matcher_ftype for description of SYMBOL_SEARCH_NAME
1627 LOOKUP_NAME/LOOKUP_NAME_LEN is the name we're looking up.
1629 See strncmp_iw_with_mode for description of MODE.
1633 cp_symbol_name_matches_1 (const char *symbol_search_name,
1634 const char *lookup_name,
1635 size_t lookup_name_len,
1636 strncmp_iw_mode mode,
1637 completion_match *match)
1639 if (strncmp_iw_with_mode (symbol_search_name,
1640 lookup_name, lookup_name_len,
1641 mode, language_cplus) == 0)
1644 match->set_match (symbol_search_name);
1651 /* C++ symbol_name_matcher_ftype implementation. */
1654 cp_fq_symbol_name_matches (const char *symbol_search_name,
1655 const lookup_name_info &lookup_name,
1656 completion_match *match)
1658 /* Get the demangled name. */
1659 const std::string &name = lookup_name.cplus ().lookup_name ();
1661 strncmp_iw_mode mode = (lookup_name.completion_mode ()
1662 ? strncmp_iw_mode::NORMAL
1663 : strncmp_iw_mode::MATCH_PARAMS);
1665 return cp_symbol_name_matches_1 (symbol_search_name,
1666 name.c_str (), name.size (),
1670 /* See cp-support.h. */
1672 symbol_name_matcher_ftype *
1673 cp_get_symbol_name_matcher (const lookup_name_info &lookup_name)
1675 return cp_fq_symbol_name_matches;
1680 namespace selftests {
1683 test_cp_symbol_name_matches ()
1685 #define CHECK_MATCH(SYMBOL, INPUT) \
1686 SELF_CHECK (cp_symbol_name_matches_1 (SYMBOL, \
1687 INPUT, sizeof (INPUT) - 1, \
1688 strncmp_iw_mode::MATCH_PARAMS, \
1691 #define CHECK_NOT_MATCH(SYMBOL, INPUT) \
1692 SELF_CHECK (!cp_symbol_name_matches_1 (SYMBOL, \
1693 INPUT, sizeof (INPUT) - 1, \
1694 strncmp_iw_mode::MATCH_PARAMS, \
1697 /* Like CHECK_MATCH, and also check that INPUT (and all substrings
1698 that start at index 0) completes to SYMBOL. */
1699 #define CHECK_MATCH_C(SYMBOL, INPUT) \
1702 CHECK_MATCH (SYMBOL, INPUT); \
1703 for (size_t i = 0; i < sizeof (INPUT) - 1; i++) \
1704 SELF_CHECK (cp_symbol_name_matches_1 (SYMBOL, INPUT, i, \
1705 strncmp_iw_mode::NORMAL, \
1709 /* Like CHECK_NOT_MATCH, and also check that INPUT does NOT complete
1711 #define CHECK_NOT_MATCH_C(SYMBOL, INPUT) \
1714 CHECK_NOT_MATCH (SYMBOL, INPUT); \
1715 SELF_CHECK (!cp_symbol_name_matches_1 (SYMBOL, INPUT, \
1716 sizeof (INPUT) - 1, \
1717 strncmp_iw_mode::NORMAL, \
1721 /* Lookup name without parens matches all overloads. */
1722 CHECK_MATCH_C ("function()", "function");
1723 CHECK_MATCH_C ("function(int)", "function");
1725 /* Check whitespace around parameters is ignored. */
1726 CHECK_MATCH_C ("function()", "function ()");
1727 CHECK_MATCH_C ("function ( )", "function()");
1728 CHECK_MATCH_C ("function ()", "function( )");
1729 CHECK_MATCH_C ("func(int)", "func( int )");
1730 CHECK_MATCH_C ("func(int)", "func ( int ) ");
1731 CHECK_MATCH_C ("func ( int )", "func( int )");
1732 CHECK_MATCH_C ("func ( int )", "func ( int ) ");
1734 /* Check symbol name prefixes aren't incorrectly matched. */
1735 CHECK_NOT_MATCH ("func", "function");
1736 CHECK_NOT_MATCH ("function", "func");
1737 CHECK_NOT_MATCH ("function()", "func");
1739 /* Check that if the lookup name includes parameters, only the right
1740 overload matches. */
1741 CHECK_MATCH_C ("function(int)", "function(int)");
1742 CHECK_NOT_MATCH_C ("function(int)", "function()");
1744 /* Check that whitespace within symbol names is not ignored. */
1745 CHECK_NOT_MATCH_C ("function", "func tion");
1746 CHECK_NOT_MATCH_C ("func__tion", "func_ _tion");
1747 CHECK_NOT_MATCH_C ("func11tion", "func1 1tion");
1749 /* Check the converse, which can happen with template function,
1750 where the return type is part of the demangled name. */
1751 CHECK_NOT_MATCH_C ("func tion", "function");
1752 CHECK_NOT_MATCH_C ("func1 1tion", "func11tion");
1753 CHECK_NOT_MATCH_C ("func_ _tion", "func__tion");
1755 /* Within parameters too. */
1756 CHECK_NOT_MATCH_C ("func(param)", "func(par am)");
1758 /* Check handling of whitespace around C++ operators. */
1759 CHECK_NOT_MATCH_C ("operator<<", "opera tor<<");
1760 CHECK_NOT_MATCH_C ("operator<<", "operator< <");
1761 CHECK_NOT_MATCH_C ("operator<<", "operator < <");
1762 CHECK_NOT_MATCH_C ("operator==", "operator= =");
1763 CHECK_NOT_MATCH_C ("operator==", "operator = =");
1764 CHECK_MATCH_C ("operator<<", "operator <<");
1765 CHECK_MATCH_C ("operator<<()", "operator <<");
1766 CHECK_NOT_MATCH_C ("operator<<()", "operator<<(int)");
1767 CHECK_NOT_MATCH_C ("operator<<(int)", "operator<<()");
1768 CHECK_MATCH_C ("operator==", "operator ==");
1769 CHECK_MATCH_C ("operator==()", "operator ==");
1770 CHECK_MATCH_C ("operator <<", "operator<<");
1771 CHECK_MATCH_C ("operator ==", "operator==");
1772 CHECK_MATCH_C ("operator bool", "operator bool");
1773 CHECK_MATCH_C ("operator bool ()", "operator bool");
1774 CHECK_MATCH_C ("operatorX<<", "operatorX < <");
1775 CHECK_MATCH_C ("Xoperator<<", "Xoperator < <");
1777 CHECK_MATCH_C ("operator()(int)", "operator()(int)");
1778 CHECK_MATCH_C ("operator()(int)", "operator ( ) ( int )");
1779 CHECK_MATCH_C ("operator()<long>(int)", "operator ( ) < long > ( int )");
1780 /* The first "()" is not the parameter list. */
1781 CHECK_NOT_MATCH ("operator()(int)", "operator");
1783 /* Misc user-defined operator tests. */
1785 CHECK_NOT_MATCH_C ("operator/=()", "operator ^=");
1786 /* Same length at end of input. */
1787 CHECK_NOT_MATCH_C ("operator>>", "operator[]");
1788 /* Same length but not at end of input. */
1789 CHECK_NOT_MATCH_C ("operator>>()", "operator[]()");
1791 CHECK_MATCH_C ("base::operator char*()", "base::operator char*()");
1792 CHECK_MATCH_C ("base::operator char*()", "base::operator char * ()");
1793 CHECK_MATCH_C ("base::operator char**()", "base::operator char * * ()");
1794 CHECK_MATCH ("base::operator char**()", "base::operator char * *");
1795 CHECK_MATCH_C ("base::operator*()", "base::operator*()");
1796 CHECK_NOT_MATCH_C ("base::operator char*()", "base::operatorc");
1797 CHECK_NOT_MATCH ("base::operator char*()", "base::operator char");
1798 CHECK_NOT_MATCH ("base::operator char*()", "base::operat");
1800 /* Check handling of whitespace around C++ scope operators. */
1801 CHECK_NOT_MATCH_C ("foo::bar", "foo: :bar");
1802 CHECK_MATCH_C ("foo::bar", "foo :: bar");
1803 CHECK_MATCH_C ("foo :: bar", "foo::bar");
1805 CHECK_MATCH_C ("abc::def::ghi()", "abc::def::ghi()");
1806 CHECK_MATCH_C ("abc::def::ghi ( )", "abc::def::ghi()");
1807 CHECK_MATCH_C ("abc::def::ghi()", "abc::def::ghi ( )");
1808 CHECK_MATCH_C ("function()", "function()");
1809 CHECK_MATCH_C ("bar::function()", "bar::function()");
1812 /* If non-NULL, return STR wrapped in quotes. Otherwise, return a
1813 "<null>" string (with no quotes). */
1816 quote (const char *str)
1819 return std::string (1, '\"') + str + '\"';
1824 /* Check that removing parameter info out of NAME produces EXPECTED.
1825 COMPLETION_MODE indicates whether we're testing normal and
1826 completion mode. FILE and LINE are used to provide better test
1827 location information in case ithe check fails. */
1830 check_remove_params (const char *file, int line,
1831 const char *name, const char *expected,
1832 bool completion_mode)
1834 gdb::unique_xmalloc_ptr<char> result
1835 = cp_remove_params_if_any (name, completion_mode);
1837 if ((expected == NULL) != (result == NULL)
1838 || (expected != NULL
1839 && strcmp (result.get (), expected) != 0))
1841 error (_("%s:%d: make-paramless self-test failed: (completion=%d) "
1842 "\"%s\" -> %s, expected %s"),
1843 file, line, completion_mode, name,
1844 quote (result.get ()).c_str (), quote (expected).c_str ());
1848 /* Entry point for cp_remove_params unit tests. */
1851 test_cp_remove_params ()
1853 /* Check that removing parameter info out of NAME produces EXPECTED.
1854 Checks both normal and completion modes. */
1855 #define CHECK(NAME, EXPECTED) \
1858 check_remove_params (__FILE__, __LINE__, NAME, EXPECTED, false); \
1859 check_remove_params (__FILE__, __LINE__, NAME, EXPECTED, true); \
1863 /* Similar, but used when NAME is incomplete -- i.e., is has
1864 unbalanced parentheses. In this case, looking for the exact name
1865 should fail / return empty. */
1866 #define CHECK_INCOMPL(NAME, EXPECTED) \
1869 check_remove_params (__FILE__, __LINE__, NAME, NULL, false); \
1870 check_remove_params (__FILE__, __LINE__, NAME, EXPECTED, true); \
1874 CHECK ("function()", "function");
1875 CHECK_INCOMPL ("function(", "function");
1876 CHECK ("function() const", "function");
1878 CHECK ("(anonymous namespace)::A::B::C",
1879 "(anonymous namespace)::A::B::C");
1881 CHECK ("A::(anonymous namespace)",
1882 "A::(anonymous namespace)");
1884 CHECK_INCOMPL ("A::(anonymou", "A");
1886 CHECK ("A::foo<int>()",
1889 CHECK_INCOMPL ("A::foo<int>(",
1892 CHECK ("A::foo<(anonymous namespace)::B>::func(int)",
1893 "A::foo<(anonymous namespace)::B>::func");
1895 CHECK_INCOMPL ("A::foo<(anonymous namespace)::B>::func(in",
1896 "A::foo<(anonymous namespace)::B>::func");
1898 CHECK_INCOMPL ("A::foo<(anonymous namespace)::B>::",
1899 "A::foo<(anonymous namespace)::B>");
1901 CHECK_INCOMPL ("A::foo<(anonymous namespace)::B>:",
1902 "A::foo<(anonymous namespace)::B>");
1904 CHECK ("A::foo<(anonymous namespace)::B>",
1905 "A::foo<(anonymous namespace)::B>");
1907 CHECK_INCOMPL ("A::foo<(anonymous namespace)::B",
1910 /* Shouldn't this parse? Looks like a bug in
1911 cp_demangled_name_to_comp. See PR c++/22411. */
1913 CHECK ("A::foo<void(int)>::func(int)",
1914 "A::foo<void(int)>::func");
1916 CHECK_INCOMPL ("A::foo<void(int)>::func(int)",
1920 CHECK_INCOMPL ("A::foo<void(int",
1924 #undef CHECK_INCOMPL
1927 } // namespace selftests
1929 #endif /* GDB_SELF_CHECK */
1931 /* Don't allow just "maintenance cplus". */
1934 maint_cplus_command (const char *arg, int from_tty)
1936 printf_unfiltered (_("\"maintenance cplus\" must be followed "
1937 "by the name of a command.\n"));
1938 help_list (maint_cplus_cmd_list,
1939 "maintenance cplus ",
1940 all_commands, gdb_stdout);
1943 /* This is a front end for cp_find_first_component, for unit testing.
1944 Be careful when using it: see the NOTE above
1945 cp_find_first_component. */
1948 first_component_command (const char *arg, int from_tty)
1956 len = cp_find_first_component (arg);
1957 prefix = (char *) alloca (len + 1);
1959 memcpy (prefix, arg, len);
1962 printf_unfiltered ("%s\n", prefix);
1965 /* Implement "info vtbl". */
1968 info_vtbl_command (const char *arg, int from_tty)
1970 struct value *value;
1972 value = parse_and_eval (arg);
1973 cplus_print_vtable (value);
1977 _initialize_cp_support (void)
1979 add_prefix_cmd ("cplus", class_maintenance,
1980 maint_cplus_command,
1981 _("C++ maintenance commands."),
1982 &maint_cplus_cmd_list,
1983 "maintenance cplus ",
1984 0, &maintenancelist);
1985 add_alias_cmd ("cp", "cplus",
1986 class_maintenance, 1,
1989 add_cmd ("first_component",
1991 first_component_command,
1992 _("Print the first class/namespace component of NAME."),
1993 &maint_cplus_cmd_list);
1995 add_info ("vtbl", info_vtbl_command,
1996 _("Show the virtual function table for a C++ object.\n\
1997 Usage: info vtbl EXPRESSION\n\
1998 Evaluate EXPRESSION and display the virtual function table for the\n\
1999 resulting object."));
2001 #ifdef HAVE_WORKING_FORK
2002 add_setshow_boolean_cmd ("catch-demangler-crashes", class_maintenance,
2003 &catch_demangler_crashes, _("\
2004 Set whether to attempt to catch demangler crashes."), _("\
2005 Show whether to attempt to catch demangler crashes."), _("\
2006 If enabled GDB will attempt to catch demangler crashes and\n\
2007 display the offending symbol."),
2010 &maintenance_set_cmdlist,
2011 &maintenance_show_cmdlist);
2015 selftests::register_test ("cp_symbol_name_matches",
2016 selftests::test_cp_symbol_name_matches);
2017 selftests::register_test ("cp_remove_params",
2018 selftests::test_cp_remove_params);