cp-support.c 67 KB

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