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