1 /* Helper routines for C++ support in GDB.
2 Copyright 2002, 2003 Free Software Foundation, Inc.
4 Contributed by MontaVista Software.
6 This file is part of GDB.
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 59 Temple Place - Suite 330,
21 Boston, MA 02111-1307, USA. */
25 #include "cp-support.h"
26 #include "gdb_string.h"
28 #include "gdb_assert.h"
31 /* The list of "maint cplus" commands. */
33 static struct cmd_list_element *maint_cplus_cmd_list = NULL;
35 /* The actual commands. */
37 static void maint_cplus_command (char *arg, int from_tty);
38 static void first_component_command (char *arg, int from_tty);
40 /* Here are some random pieces of trivia to keep in mind while trying
41 to take apart demangled names:
43 - Names can contain function arguments or templates, so the process
44 has to be, to some extent recursive: maybe keep track of your
45 depth based on encountering <> and ().
47 - Parentheses don't just have to happen at the end of a name: they
48 can occur even if the name in question isn't a function, because
49 a template argument might be a type that's a function.
51 - Conversely, even if you're trying to deal with a function, its
52 demangled name might not end with ')': it could be a const or
53 volatile class method, in which case it ends with "const" or
56 - Parentheses are also used in anonymous namespaces: a variable
57 'foo' in an anonymous namespace gets demangled as "(anonymous
60 - And operator names can contain parentheses or angle brackets.
61 Fortunately, I _think_ that operator names can only occur in a
62 fairly restrictive set of locations (in particular, they have be
63 at depth 0, don't they?). */
65 /* NOTE: carlton/2003-02-21: Daniel Jacobowitz came up with an example
66 where operator names don't occur at depth 0. Sigh. (It involved a
67 template argument that was a pointer: I hadn't realized that was
68 possible.) Handling such edge cases does not seem like a
69 high-priority problem to me. */
71 /* FIXME: carlton/2003-03-13: We have several functions here with
72 overlapping functionality; can we combine them? Also, do they
73 handle all the above considerations correctly? */
75 /* Find the last component of the demangled C++ name NAME. NAME
76 must be a method name including arguments, in order to correctly
77 locate the last component.
79 This function return a pointer to the first colon before the
80 last component, or NULL if the name had only one component. */
83 find_last_component (const char *name)
88 /* Functions can have local classes, so we need to find the
89 beginning of the last argument list, not the end of the first
91 p = name + strlen (name) - 1;
92 while (p > name && *p != ')')
98 /* P now points at the `)' at the end of the argument list. Walk
99 back to the beginning. */
102 while (p > name && depth > 0)
104 if (*p == '<' || *p == '(')
106 else if (*p == '>' || *p == ')')
114 while (p > name && *p != ':')
117 if (p == name || p == name + 1 || p[-1] != ':')
123 /* Return the name of the class containing method PHYSNAME. */
126 class_name_from_physname (const char *physname)
131 char *demangled_name = cplus_demangle (physname, DMGL_ANSI);
133 if (demangled_name == NULL)
136 end = find_last_component (demangled_name);
139 ret = xmalloc (end - demangled_name + 1);
140 memcpy (ret, demangled_name, end - demangled_name);
141 ret[end - demangled_name] = '\0';
144 xfree (demangled_name);
148 /* Return the name of the method whose linkage name is PHYSNAME. */
151 method_name_from_physname (const char *physname)
156 char *demangled_name = cplus_demangle (physname, DMGL_ANSI);
158 if (demangled_name == NULL)
161 end = find_last_component (demangled_name);
170 /* Find the argument list, if any. */
171 args = strchr (end, '(');
173 len = strlen (end + 2);
179 len = args - end + 1;
181 ret = xmalloc (len + 1);
182 memcpy (ret, end, len);
186 xfree (demangled_name);
190 /* This returns the length of first component of NAME, which should be
191 the demangled name of a C++ variable/function/method/etc.
192 Specifically, it returns the index of the first colon forming the
193 boundary of the first component: so, given 'A::foo' or 'A::B::foo'
194 it returns the 1, and given 'foo', it returns 0. */
196 /* Well, that's what it should do when called externally, but to make
197 the recursion easier, it also stops if it reaches an unexpected ')'
200 /* NOTE: carlton/2003-03-13: This function is currently only intended
201 for internal use: it's probably not entirely safe when called on
202 user-generated input, because some of the 'index += 2' lines might
203 go past the end of malformed input. */
205 /* Let's optimize away calls to strlen("operator"). */
207 #define LENGTH_OF_OPERATOR 8
210 cp_find_first_component (const char *name)
212 /* Names like 'operator<<' screw up the recursion, so let's
213 special-case them. I _hope_ they can only occur at the start of
216 unsigned int index = 0;
218 if (strncmp (name, "operator", LENGTH_OF_OPERATOR) == 0)
220 index += LENGTH_OF_OPERATOR;
221 while (isspace(name[index]))
226 if (name[index + 1] == '<')
233 if (name[index + 1] == '>')
252 /* Template; eat it up. The calls to cp_first_component
253 should only return (I hope!) when they reach the '>'
254 terminating the component or a '::' between two
255 components. (Hence the '+ 2'.) */
257 for (index += cp_find_first_component (name + index);
259 index += cp_find_first_component (name + index))
261 gdb_assert (name[index] == ':');
266 /* Similar comment as to '<'. */
268 for (index += cp_find_first_component (name + index);
270 index += cp_find_first_component (name + index))
272 gdb_assert (name[index] == ':');
287 /* If NAME is the fully-qualified name of a C++
288 function/variable/method/etc., this returns the length of its
289 entire prefix: all of the namespaces and classes that make up its
290 name. Given 'A::foo', it returns 1, given 'A::B::foo', it returns
291 4, given 'foo', it returns 0. */
294 cp_entire_prefix_len (const char *name)
296 unsigned int current_len = cp_find_first_component (name);
297 unsigned int previous_len = 0;
299 while (name[current_len] != '\0')
301 gdb_assert (name[current_len] == ':');
302 previous_len = current_len;
305 current_len += cp_find_first_component (name + current_len);
311 /* Don't allow just "maintenance cplus". */
314 maint_cplus_command (char *arg, int from_tty)
316 printf_unfiltered ("\"maintenance cplus\" must be followed by the name of a command.\n");
317 help_list (maint_cplus_cmd_list, "maintenance cplus ", -1, gdb_stdout);
320 /* This is a front end for cp_find_first_component, for unit testing.
321 Be careful when using it: see the NOTE above
322 cp_find_first_component. */
325 first_component_command (char *arg, int from_tty)
327 int len = cp_find_first_component (arg);
328 char *prefix = alloca (len + 1);
330 memcpy (prefix, arg, len);
333 printf_unfiltered ("%s\n", prefix);
337 _initialize_cp_support (void)
339 add_prefix_cmd ("cplus", class_maintenance, maint_cplus_command,
340 "C++ maintenance commands.", &maint_cplus_cmd_list,
341 "maintenance cplus ", 0, &maintenancelist);
342 add_alias_cmd ("cp", "cplus", class_maintenance, 1, &maintenancelist);
344 add_cmd ("first_component", class_maintenance, first_component_command,
345 "Print the first class/namespace component of NAME.",
346 &maint_cplus_cmd_list);