expmed.c (emit_store_flag_1): Fix choice of zero vs.
[official-gcc.git] / libiberty / testsuite / test-demangle.c
blob1c982d6ef20cabb742575bb7ba300f51a19fc6bc
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.
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"
29 #ifdef HAVE_STRING_H
30 #include <string.h>
31 #endif
32 #if HAVE_STDLIB_H
33 # include <stdlib.h>
34 #endif
36 struct line
38 size_t alloced;
39 char *data;
42 static unsigned int lineno;
44 /* Safely read a single line of arbitrary length from standard input. */
46 #define LINELEN 80
48 static void
49 get_line(buf)
50 struct line *buf;
52 char *data = buf->data;
53 size_t alloc = buf->alloced;
54 size_t count = 0;
55 int c;
57 if (data == 0)
59 data = xmalloc (LINELEN);
60 alloc = LINELEN;
63 /* Skip comment lines. */
64 while ((c = getchar()) == '#')
66 while ((c = getchar()) != EOF && c != '\n');
67 lineno++;
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)
76 alloc *= 2;
77 data = xrealloc (data, alloc);
79 data[count++] = c;
80 c = getchar();
82 lineno++;
83 data[count] = '\0';
85 buf->data = data;
86 buf->alloced = 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
95 #include <sys/mman.h>
96 #endif
97 #if defined(MAP_ANON) && ! defined (MAP_ANONYMOUS)
98 #define MAP_ANONYMOUS MAP_ANON
99 #endif
101 static const char *
102 protect_end (const char * s)
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;
110 /* Don't try if S is too long. */
111 if (s_len >= pagesize)
112 return s;
114 /* Allocate one page of allocated space followed by an unmapped
115 page. */
116 if (buf == NULL)
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);
125 result = buf + (pagesize - s_len - 1);
126 memcpy (result, s, s_len + 1);
127 return result;
128 #else
129 return s;
130 #endif
133 static void
134 fail (lineno, opts, in, out, exp)
135 int lineno;
136 const char *opts;
137 const char *in;
138 const char *out;
139 const char *exp;
141 printf ("\
142 FAIL at line %d, options %s:\n\
143 in: %s\n\
144 out: %s\n\
145 exp: %s\n",
146 lineno, opts, in, out != NULL ? out : "(null)", exp);
149 /* The tester operates on a data file consisting of groups of lines:
150 options
151 input to be demangled
152 expected output
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.
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.
169 main(argc, argv)
170 int argc;
171 char **argv;
173 enum demangling_styles style = auto_demangling;
174 int no_params;
175 int is_v3_ctor;
176 int is_v3_dtor;
177 int ret_postfix;
178 struct line format;
179 struct line input;
180 struct line expect;
181 char *result;
182 int failures = 0;
183 int tests = 0;
185 if (argc > 1)
187 fprintf (stderr, "usage: %s < test-set\n", argv[0]);
188 return 2;
191 format.data = 0;
192 input.data = 0;
193 expect.data = 0;
195 for (;;)
197 const char *inp;
199 get_line (&format);
200 if (feof (stdin))
201 break;
203 get_line (&input);
204 get_line (&expect);
206 inp = protect_end (input.data);
208 tests++;
210 no_params = 0;
211 ret_postfix = 0;
212 is_v3_ctor = 0;
213 is_v3_dtor = 0;
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);
223 failures++;
224 continue;
227 else
229 char *p;
230 char *opt;
232 p = format.data;
233 while (*p != '\0')
235 char c;
237 opt = p;
238 p += strcspn (p, " \t=");
239 c = *p;
240 *p = '\0';
241 if (strcmp (opt, "--format") == 0 && c == '=')
243 char *fstyle;
245 *p = c;
246 ++p;
247 fstyle = p;
248 p += strcspn (p, " \t");
249 c = *p;
250 *p = '\0';
251 style = cplus_demangle_name_to_style (fstyle);
252 if (style == unknown_demangling)
254 printf ("FAIL at line %d: unknown demangling style %s\n",
255 lineno, fstyle);
256 failures++;
257 continue;
260 else if (strcmp (opt, "--no-params") == 0)
261 no_params = 1;
262 else if (strcmp (opt, "--is-v3-ctor") == 0)
263 is_v3_ctor = 1;
264 else if (strcmp (opt, "--is-v3-dtor") == 0)
265 is_v3_dtor = 1;
266 else if (strcmp (opt, "--ret-postfix") == 0)
267 ret_postfix = 1;
268 else
270 printf ("FAIL at line %d: unrecognized option %s\n",
271 lineno, opt);
272 failures++;
273 continue;
275 *p = c;
276 p += strspn (p, " \t");
280 if (is_v3_ctor || is_v3_dtor)
282 char buf[20];
284 if (is_v3_ctor)
286 enum gnu_v3_ctor_kinds kc;
288 kc = is_gnu_v3_mangled_ctor (inp);
289 sprintf (buf, "%d", (int) kc);
291 else
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);
302 failures++;
305 continue;
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));
314 if (result
315 ? strcmp (result, expect.data)
316 : strcmp (input.data, expect.data))
318 fail (lineno, format.data, input.data, result, expect.data);
319 failures++;
321 free (result);
323 if (no_params)
325 get_line (&expect);
326 result = cplus_demangle (inp, DMGL_ANSI|DMGL_TYPES);
328 if (result
329 ? strcmp (result, expect.data)
330 : strcmp (input.data, expect.data))
332 fail (lineno, format.data, input.data, result, expect.data);
333 failures++;
335 free (result);
339 free (format.data);
340 free (input.data);
341 free (expect.data);
343 printf ("%s: %d tests, %d failures\n", argv[0], tests, failures);
344 return failures ? 1 : 0;