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. */
62 /* FIXME: carlton/2003-03-13: We have several functions here with
63 overlapping functionality; can we combine them? Also, do they
64 handle all the above considerations correctly? */
66 /* Find the last component of the demangled C++ name NAME. NAME
67 must be a method name including arguments, in order to correctly
68 locate the last component.
70 This function return a pointer to the first colon before the
71 last component, or NULL if the name had only one component. */
74 find_last_component (const char *name)
79 /* Functions can have local classes, so we need to find the
80 beginning of the last argument list, not the end of the first
82 p = name + strlen (name) - 1;
83 while (p > name && *p != ')')
89 /* P now points at the `)' at the end of the argument list. Walk
90 back to the beginning. */
93 while (p > name && depth > 0)
95 if (*p == '<' || *p == '(')
97 else if (*p == '>' || *p == ')')
105 while (p > name && *p != ':')
108 if (p == name || p == name + 1 || p[-1] != ':')
114 /* Return the name of the class containing method PHYSNAME. */
117 class_name_from_physname (const char *physname)
122 char *demangled_name = cplus_demangle (physname, DMGL_ANSI);
124 if (demangled_name == NULL)
127 end = find_last_component (demangled_name);
130 ret = xmalloc (end - demangled_name + 1);
131 memcpy (ret, demangled_name, end - demangled_name);
132 ret[end - demangled_name] = '\0';
135 xfree (demangled_name);
139 /* Return the name of the method whose linkage name is PHYSNAME. */
142 method_name_from_physname (const char *physname)
147 char *demangled_name = cplus_demangle (physname, DMGL_ANSI);
149 if (demangled_name == NULL)
152 end = find_last_component (demangled_name);
161 /* Find the argument list, if any. */
162 args = strchr (end, '(');
164 len = strlen (end + 2);
170 len = args - end + 1;
172 ret = xmalloc (len + 1);
173 memcpy (ret, end, len);
177 xfree (demangled_name);
181 /* This returns the length of first component of NAME, which should be
182 the demangled name of a C++ variable/function/method/etc.
183 Specifically, it returns the index of the first colon forming the
184 boundary of the first component: so, given 'A::foo' or 'A::B::foo'
185 it returns the 1, and given 'foo', it returns 0. */
187 /* Well, that's what it should do when called externally, but to make
188 the recursion easier, it also stops if it reaches an unexpected ')'
191 /* NOTE: carlton/2003-03-13: This function is currently only intended
192 for internal use: it's probably not entirely safe when called on
193 user-generated input, because some of the 'index += 2' lines might
194 go past the end of malformed input. */
196 /* Let's optimize away calls to strlen("operator"). */
198 #define LENGTH_OF_OPERATOR 8
201 cp_find_first_component (const char *name)
203 unsigned int index = 0;
204 /* Operator names can show up in unexpected places. Since these can
205 contain parentheses or angle brackets, they can screw up the
206 recursion. But not every string 'operator' is part of an
207 operater name: e.g. you could have a variable 'cooperator'. So
208 this variable tells us whether or not we should treat the string
209 'operator' as starting an operator. */
210 int operator_possible = 1;
217 /* Template; eat it up. The calls to cp_first_component
218 should only return (I hope!) when they reach the '>'
219 terminating the component or a '::' between two
220 components. (Hence the '+ 2'.) */
222 for (index += cp_find_first_component (name + index);
224 index += cp_find_first_component (name + index))
226 gdb_assert (name[index] == ':');
229 operator_possible = 1;
232 /* Similar comment as to '<'. */
234 for (index += cp_find_first_component (name + index);
236 index += cp_find_first_component (name + index))
238 gdb_assert (name[index] == ':');
241 operator_possible = 1;
249 /* Operator names can screw up the recursion. */
250 if (operator_possible
251 && strncmp (name + index, "operator", LENGTH_OF_OPERATOR) == 0)
253 index += LENGTH_OF_OPERATOR;
254 while (isspace(name[index]))
258 /* Skip over one less than the appropriate number of
259 characters: the for loop will skip over the last
262 if (name[index + 1] == '<')
269 if (name[index + 1] == '>')
282 operator_possible = 0;
289 /* NOTE: carlton/2003-04-18: I'm not sure what the precise
290 set of relevant characters are here: it's necessary to
291 include any character that can show up before 'operator'
292 in a demangled name, and it's safe to include any
293 character that can't be part of an identifier's name. */
294 operator_possible = 1;
297 operator_possible = 0;
303 /* If NAME is the fully-qualified name of a C++
304 function/variable/method/etc., this returns the length of its
305 entire prefix: all of the namespaces and classes that make up its
306 name. Given 'A::foo', it returns 1, given 'A::B::foo', it returns
307 4, given 'foo', it returns 0. */
310 cp_entire_prefix_len (const char *name)
312 unsigned int current_len = cp_find_first_component (name);
313 unsigned int previous_len = 0;
315 while (name[current_len] != '\0')
317 gdb_assert (name[current_len] == ':');
318 previous_len = current_len;
321 current_len += cp_find_first_component (name + current_len);
327 /* Don't allow just "maintenance cplus". */
330 maint_cplus_command (char *arg, int from_tty)
332 printf_unfiltered ("\"maintenance cplus\" must be followed by the name of a command.\n");
333 help_list (maint_cplus_cmd_list, "maintenance cplus ", -1, gdb_stdout);
336 /* This is a front end for cp_find_first_component, for unit testing.
337 Be careful when using it: see the NOTE above
338 cp_find_first_component. */
341 first_component_command (char *arg, int from_tty)
343 int len = cp_find_first_component (arg);
344 char *prefix = alloca (len + 1);
346 memcpy (prefix, arg, len);
349 printf_unfiltered ("%s\n", prefix);
352 extern initialize_file_ftype _initialize_cp_support; /* -Wmissing-prototypes */
355 _initialize_cp_support (void)
357 add_prefix_cmd ("cplus", class_maintenance, maint_cplus_command,
358 "C++ maintenance commands.", &maint_cplus_cmd_list,
359 "maintenance cplus ", 0, &maintenancelist);
360 add_alias_cmd ("cp", "cplus", class_maintenance, 1, &maintenancelist);
362 add_cmd ("first_component", class_maintenance, first_component_command,
363 "Print the first class/namespace component of NAME.",
364 &maint_cplus_cmd_list);