1 /* Demangler test program,
2 Copyright (C) 2002, 2003, 2004 Free Software Foundation, Inc.
3 Written by Zack Weinberg <zack@codesourcery.com
5 This file is part of GNU libiberty.
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
27 #include "libiberty.h"
42 static unsigned int lineno;
44 /* Safely read a single line of arbitrary length from standard input. */
52 char *data = buf->data;
53 size_t alloc = buf->alloced;
59 data = xmalloc (LINELEN);
63 /* Skip comment lines. */
64 while ((c = getchar()) == '#')
66 while ((c = getchar()) != EOF && c != '\n');
70 /* c is the first character on the line, and it's not a comment
71 line: copy this line into the buffer and return. */
72 while (c != EOF && c != '\n')
74 if (count + 1 >= alloc)
77 data = xrealloc (data, alloc);
89 /* If we have mmap() and mprotect(), copy the string S just before a
90 protected page, so that if the demangler runs over the end of the
91 string we'll get a fault, and return the address of the new string.
92 If no mmap, or it fails, or it looks too hard, just return S. */
94 #ifdef HAVE_SYS_MMAN_H
97 #if defined(MAP_ANON) && ! defined (MAP_ANONYMOUS)
98 #define MAP_ANONYMOUS MAP_ANON
102 protect_end (const char * s)
104 #if defined(HAVE_MMAP) && defined (MAP_ANONYMOUS)
105 size_t pagesize = getpagesize();
107 size_t s_len = strlen (s);
110 /* Don't try if S is too long. */
111 if (s_len >= pagesize)
114 /* Allocate one page of allocated space followed by an unmapped
118 buf = mmap (NULL, pagesize * 2, PROT_READ | PROT_WRITE,
119 MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
122 munmap (buf + pagesize, pagesize);
125 result = buf + (pagesize - s_len - 1);
126 memcpy (result, s, s_len + 1);
134 fail (lineno, opts, in, out, exp)
142 FAIL at line %d, options %s:\n\
146 lineno, opts, in, out != NULL ? out : "(null)", exp);
149 /* The tester operates on a data file consisting of groups of lines:
151 input to be demangled
155 --format=<name> Sets the demangling style.
156 --no-params There are two lines of expected output; the first
157 is with DMGL_PARAMS, the second is without it.
158 --is-v3-ctor Calls is_gnu_v3_mangled_ctor on input; expected
159 output is an integer representing ctor_kind.
160 --is-v3-dtor Likewise, but for dtors.
161 --ret-postfix Passes the DMGL_RET_POSTFIX option
163 For compatibility, just in case it matters, the options line may be
164 empty, to mean --format=auto. If it doesn't start with --, then it
165 may contain only a format name.
173 enum demangling_styles style = auto_demangling;
187 fprintf (stderr, "usage: %s < test-set\n", argv[0]);
206 inp = protect_end (input.data);
214 if (format.data[0] == '\0')
215 style = auto_demangling;
216 else if (format.data[0] != '-')
218 style = cplus_demangle_name_to_style (format.data);
219 if (style == unknown_demangling)
221 printf ("FAIL at line %d: unknown demangling style %s\n",
222 lineno, format.data);
238 p += strcspn (p, " \t=");
241 if (strcmp (opt, "--format") == 0 && c == '=')
248 p += strcspn (p, " \t");
251 style = cplus_demangle_name_to_style (fstyle);
252 if (style == unknown_demangling)
254 printf ("FAIL at line %d: unknown demangling style %s\n",
260 else if (strcmp (opt, "--no-params") == 0)
262 else if (strcmp (opt, "--is-v3-ctor") == 0)
264 else if (strcmp (opt, "--is-v3-dtor") == 0)
266 else if (strcmp (opt, "--ret-postfix") == 0)
270 printf ("FAIL at line %d: unrecognized option %s\n",
276 p += strspn (p, " \t");
280 if (is_v3_ctor || is_v3_dtor)
286 enum gnu_v3_ctor_kinds kc;
288 kc = is_gnu_v3_mangled_ctor (inp);
289 sprintf (buf, "%d", (int) kc);
293 enum gnu_v3_dtor_kinds kd;
295 kd = is_gnu_v3_mangled_dtor (inp);
296 sprintf (buf, "%d", (int) kd);
299 if (strcmp (buf, expect.data) != 0)
301 fail (lineno, format.data, input.data, buf, expect.data);
308 cplus_demangle_set_style (style);
310 result = cplus_demangle (inp,
311 DMGL_PARAMS|DMGL_ANSI|DMGL_TYPES
312 |(ret_postfix ? DMGL_RET_POSTFIX : 0));
315 ? strcmp (result, expect.data)
316 : strcmp (input.data, expect.data))
318 fail (lineno, format.data, input.data, result, expect.data);
326 result = cplus_demangle (inp, DMGL_ANSI|DMGL_TYPES);
329 ? strcmp (result, expect.data)
330 : strcmp (input.data, expect.data))
332 fail (lineno, format.data, input.data, result, expect.data);
343 printf ("%s: %d tests, %d failures\n", argv[0], tests, failures);
344 return failures ? 1 : 0;