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