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