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