* Makefile.def (target_modules): Remove libmudflap (languages): Remove check-target...
[deliverable/binutils-gdb.git] / libiberty / testsuite / test-demangle.c
CommitLineData
a54ba43f 1/* Demangler test program,
820542c9 2 Copyright (C) 2002, 2003, 2004 Free Software Foundation, Inc.
a54ba43f
DD
3 Written by Zack Weinberg <zack@codesourcery.com
4
5 This file is part of GNU libiberty.
6
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.
11
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.
16
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
a9479c3f 19 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
a54ba43f
DD
20*/
21
22#ifdef HAVE_CONFIG_H
23#include "config.h"
24#endif
25#include "ansidecl.h"
26#include <stdio.h>
27#include "libiberty.h"
28#include "demangle.h"
758d77be
DD
29#ifdef HAVE_STRING_H
30#include <string.h>
31#endif
32#if HAVE_STDLIB_H
33# include <stdlib.h>
34#endif
a54ba43f
DD
35
36struct line
37{
38 size_t alloced;
39 char *data;
40};
41
42static unsigned int lineno;
43
44/* Safely read a single line of arbitrary length from standard input. */
45
46#define LINELEN 80
47
48static void
5d7aee10 49get_line(buf)
a54ba43f
DD
50 struct line *buf;
51{
52 char *data = buf->data;
53 size_t alloc = buf->alloced;
54 size_t count = 0;
55 int c;
56
57 if (data == 0)
58 {
59 data = xmalloc (LINELEN);
60 alloc = LINELEN;
61 }
62
63 /* Skip comment lines. */
64 while ((c = getchar()) == '#')
65 {
66 while ((c = getchar()) != EOF && c != '\n');
67 lineno++;
68 }
69
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')
73 {
54c60684 74 if (count + 1 >= alloc)
a54ba43f
DD
75 {
76 alloc *= 2;
77 data = xrealloc (data, alloc);
78 }
79 data[count++] = c;
80 c = getchar();
81 }
82 lineno++;
83 data[count] = '\0';
84
85 buf->data = data;
86 buf->alloced = alloc;
87}
88
6ef6358e
GK
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. */
93
94#ifdef HAVE_SYS_MMAN_H
95#include <sys/mman.h>
96#endif
97#if defined(MAP_ANON) && ! defined (MAP_ANONYMOUS)
98#define MAP_ANONYMOUS MAP_ANON
99#endif
100
101static const char *
102protect_end (const char * s)
103{
104#if defined(HAVE_MMAP) && defined (MAP_ANONYMOUS)
105 size_t pagesize = getpagesize();
106 static char * buf;
107 size_t s_len = strlen (s);
108 char * result;
109
110 /* Don't try if S is too long. */
111 if (s_len >= pagesize)
112 return s;
113
114 /* Allocate one page of allocated space followed by an unmapped
115 page. */
116 if (buf == NULL)
117 {
118 buf = mmap (NULL, pagesize * 2, PROT_READ | PROT_WRITE,
119 MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
120 if (! buf)
121 return s;
122 munmap (buf + pagesize, pagesize);
123 }
124
125 result = buf + (pagesize - s_len - 1);
126 memcpy (result, s, s_len + 1);
127 return result;
128#else
129 return s;
130#endif
131}
132
820542c9
DD
133static void
134fail (lineno, opts, in, out, exp)
135 int lineno;
136 const char *opts;
137 const char *in;
138 const char *out;
139 const char *exp;
140{
141 printf ("\
142FAIL at line %d, options %s:\n\
143in: %s\n\
144out: %s\n\
145exp: %s\n",
146 lineno, opts, in, out != NULL ? out : "(null)", exp);
147}
148
149/* The tester operates on a data file consisting of groups of lines:
150 options
a54ba43f
DD
151 input to be demangled
152 expected output
153
820542c9
DD
154 Supported options:
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.
7887b2ce 161 --ret-postfix Passes the DMGL_RET_POSTFIX option
ddee5e46 162 --ret-drop Passes the DMGL_RET_DROP option
a54ba43f 163
820542c9
DD
164 For compatibility, just in case it matters, the options line may be
165 empty, to mean --format=auto. If it doesn't start with --, then it
166 may contain only a format name.
167*/
a54ba43f
DD
168
169int
170main(argc, argv)
171 int argc;
172 char **argv;
173{
758d77be 174 enum demangling_styles style = auto_demangling;
820542c9
DD
175 int no_params;
176 int is_v3_ctor;
177 int is_v3_dtor;
ddee5e46 178 int ret_postfix, ret_drop;
a54ba43f
DD
179 struct line format;
180 struct line input;
181 struct line expect;
a54ba43f
DD
182 char *result;
183 int failures = 0;
184 int tests = 0;
185
186 if (argc > 1)
187 {
188 fprintf (stderr, "usage: %s < test-set\n", argv[0]);
189 return 2;
190 }
191
192 format.data = 0;
193 input.data = 0;
194 expect.data = 0;
195
196 for (;;)
197 {
6ef6358e
GK
198 const char *inp;
199
5d7aee10 200 get_line (&format);
a54ba43f
DD
201 if (feof (stdin))
202 break;
203
5d7aee10
NC
204 get_line (&input);
205 get_line (&expect);
a54ba43f 206
6ef6358e
GK
207 inp = protect_end (input.data);
208
a54ba43f
DD
209 tests++;
210
820542c9 211 no_params = 0;
7887b2ce 212 ret_postfix = 0;
ddee5e46 213 ret_drop = 0;
820542c9
DD
214 is_v3_ctor = 0;
215 is_v3_dtor = 0;
216 if (format.data[0] == '\0')
a54ba43f 217 style = auto_demangling;
820542c9
DD
218 else if (format.data[0] != '-')
219 {
220 style = cplus_demangle_name_to_style (format.data);
221 if (style == unknown_demangling)
222 {
223 printf ("FAIL at line %d: unknown demangling style %s\n",
224 lineno, format.data);
225 failures++;
226 continue;
227 }
228 }
a54ba43f 229 else
820542c9
DD
230 {
231 char *p;
232 char *opt;
a54ba43f 233
820542c9
DD
234 p = format.data;
235 while (*p != '\0')
236 {
237 char c;
238
239 opt = p;
240 p += strcspn (p, " \t=");
241 c = *p;
242 *p = '\0';
243 if (strcmp (opt, "--format") == 0 && c == '=')
244 {
245 char *fstyle;
246
247 *p = c;
248 ++p;
249 fstyle = p;
250 p += strcspn (p, " \t");
251 c = *p;
252 *p = '\0';
253 style = cplus_demangle_name_to_style (fstyle);
254 if (style == unknown_demangling)
255 {
256 printf ("FAIL at line %d: unknown demangling style %s\n",
257 lineno, fstyle);
258 failures++;
259 continue;
260 }
261 }
262 else if (strcmp (opt, "--no-params") == 0)
263 no_params = 1;
264 else if (strcmp (opt, "--is-v3-ctor") == 0)
265 is_v3_ctor = 1;
266 else if (strcmp (opt, "--is-v3-dtor") == 0)
267 is_v3_dtor = 1;
7887b2ce
DD
268 else if (strcmp (opt, "--ret-postfix") == 0)
269 ret_postfix = 1;
ddee5e46
DD
270 else if (strcmp (opt, "--ret-drop") == 0)
271 ret_drop = 1;
820542c9
DD
272 else
273 {
274 printf ("FAIL at line %d: unrecognized option %s\n",
275 lineno, opt);
276 failures++;
277 continue;
278 }
279 *p = c;
280 p += strspn (p, " \t");
281 }
282 }
283
284 if (is_v3_ctor || is_v3_dtor)
a54ba43f 285 {
820542c9
DD
286 char buf[20];
287
288 if (is_v3_ctor)
289 {
290 enum gnu_v3_ctor_kinds kc;
291
6ef6358e 292 kc = is_gnu_v3_mangled_ctor (inp);
820542c9
DD
293 sprintf (buf, "%d", (int) kc);
294 }
295 else
296 {
297 enum gnu_v3_dtor_kinds kd;
298
6ef6358e 299 kd = is_gnu_v3_mangled_dtor (inp);
820542c9
DD
300 sprintf (buf, "%d", (int) kd);
301 }
302
303 if (strcmp (buf, expect.data) != 0)
304 {
305 fail (lineno, format.data, input.data, buf, expect.data);
306 failures++;
307 }
308
a54ba43f
DD
309 continue;
310 }
311
312 cplus_demangle_set_style (style);
313
ddee5e46
DD
314 result = cplus_demangle (inp, (DMGL_PARAMS | DMGL_ANSI | DMGL_TYPES
315 | (ret_postfix ? DMGL_RET_POSTFIX : 0)
316 | (ret_drop ? DMGL_RET_DROP : 0)));
a54ba43f
DD
317
318 if (result
319 ? strcmp (result, expect.data)
320 : strcmp (input.data, expect.data))
321 {
820542c9 322 fail (lineno, format.data, input.data, result, expect.data);
a54ba43f
DD
323 failures++;
324 }
325 free (result);
820542c9
DD
326
327 if (no_params)
328 {
5d7aee10 329 get_line (&expect);
6ef6358e 330 result = cplus_demangle (inp, DMGL_ANSI|DMGL_TYPES);
820542c9
DD
331
332 if (result
333 ? strcmp (result, expect.data)
334 : strcmp (input.data, expect.data))
335 {
336 fail (lineno, format.data, input.data, result, expect.data);
337 failures++;
338 }
339 free (result);
340 }
a54ba43f
DD
341 }
342
343 free (format.data);
344 free (input.data);
345 free (expect.data);
346
347 printf ("%s: %d tests, %d failures\n", argv[0], tests, failures);
348 return failures ? 1 : 0;
349}
This page took 0.493172 seconds and 4 git commands to generate.