merge from gcc
[binutils.git] / libiberty / testsuite / test-demangle.c
blob6e00d1416d789dbbfbbed531091ec359605e652e
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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, 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"
30 struct line
32 size_t alloced;
33 char *data;
36 static unsigned int lineno;
38 /* Safely read a single line of arbitrary length from standard input. */
40 #define LINELEN 80
42 static void
43 getline(buf)
44 struct line *buf;
46 char *data = buf->data;
47 size_t alloc = buf->alloced;
48 size_t count = 0;
49 int c;
51 if (data == 0)
53 data = xmalloc (LINELEN);
54 alloc = LINELEN;
57 /* Skip comment lines. */
58 while ((c = getchar()) == '#')
60 while ((c = getchar()) != EOF && c != '\n');
61 lineno++;
64 /* c is the first character on the line, and it's not a comment
65 line: copy this line into the buffer and return. */
66 while (c != EOF && c != '\n')
68 if (count + 1 >= alloc)
70 alloc *= 2;
71 data = xrealloc (data, alloc);
73 data[count++] = c;
74 c = getchar();
76 lineno++;
77 data[count] = '\0';
79 buf->data = data;
80 buf->alloced = alloc;
83 static void
84 fail (lineno, opts, in, out, exp)
85 int lineno;
86 const char *opts;
87 const char *in;
88 const char *out;
89 const char *exp;
91 printf ("\
92 FAIL at line %d, options %s:\n\
93 in: %s\n\
94 out: %s\n\
95 exp: %s\n",
96 lineno, opts, in, out != NULL ? out : "(null)", exp);
99 /* The tester operates on a data file consisting of groups of lines:
100 options
101 input to be demangled
102 expected output
104 Supported options:
105 --format=<name> Sets the demangling style.
106 --no-params There are two lines of expected output; the first
107 is with DMGL_PARAMS, the second is without it.
108 --is-v3-ctor Calls is_gnu_v3_mangled_ctor on input; expected
109 output is an integer representing ctor_kind.
110 --is-v3-dtor Likewise, but for dtors.
112 For compatibility, just in case it matters, the options line may be
113 empty, to mean --format=auto. If it doesn't start with --, then it
114 may contain only a format name.
118 main(argc, argv)
119 int argc;
120 char **argv;
122 enum demangling_styles style;
123 int no_params;
124 int is_v3_ctor;
125 int is_v3_dtor;
126 struct line format;
127 struct line input;
128 struct line expect;
129 char *result;
130 int failures = 0;
131 int tests = 0;
133 if (argc > 1)
135 fprintf (stderr, "usage: %s < test-set\n", argv[0]);
136 return 2;
139 format.data = 0;
140 input.data = 0;
141 expect.data = 0;
143 for (;;)
145 getline (&format);
146 if (feof (stdin))
147 break;
149 getline (&input);
150 getline (&expect);
152 tests++;
154 no_params = 0;
155 is_v3_ctor = 0;
156 is_v3_dtor = 0;
157 if (format.data[0] == '\0')
158 style = auto_demangling;
159 else if (format.data[0] != '-')
161 style = cplus_demangle_name_to_style (format.data);
162 if (style == unknown_demangling)
164 printf ("FAIL at line %d: unknown demangling style %s\n",
165 lineno, format.data);
166 failures++;
167 continue;
170 else
172 char *p;
173 char *opt;
175 p = format.data;
176 while (*p != '\0')
178 char c;
180 opt = p;
181 p += strcspn (p, " \t=");
182 c = *p;
183 *p = '\0';
184 if (strcmp (opt, "--format") == 0 && c == '=')
186 char *fstyle;
188 *p = c;
189 ++p;
190 fstyle = p;
191 p += strcspn (p, " \t");
192 c = *p;
193 *p = '\0';
194 style = cplus_demangle_name_to_style (fstyle);
195 if (style == unknown_demangling)
197 printf ("FAIL at line %d: unknown demangling style %s\n",
198 lineno, fstyle);
199 failures++;
200 continue;
203 else if (strcmp (opt, "--no-params") == 0)
204 no_params = 1;
205 else if (strcmp (opt, "--is-v3-ctor") == 0)
206 is_v3_ctor = 1;
207 else if (strcmp (opt, "--is-v3-dtor") == 0)
208 is_v3_dtor = 1;
209 else
211 printf ("FAIL at line %d: unrecognized option %s\n",
212 lineno, opt);
213 failures++;
214 continue;
216 *p = c;
217 p += strspn (p, " \t");
221 if (is_v3_ctor || is_v3_dtor)
223 char buf[20];
225 if (is_v3_ctor)
227 enum gnu_v3_ctor_kinds kc;
229 kc = is_gnu_v3_mangled_ctor (input.data);
230 sprintf (buf, "%d", (int) kc);
232 else
234 enum gnu_v3_dtor_kinds kd;
236 kd = is_gnu_v3_mangled_dtor (input.data);
237 sprintf (buf, "%d", (int) kd);
240 if (strcmp (buf, expect.data) != 0)
242 fail (lineno, format.data, input.data, buf, expect.data);
243 failures++;
246 continue;
249 cplus_demangle_set_style (style);
251 result = cplus_demangle (input.data,
252 DMGL_PARAMS|DMGL_ANSI|DMGL_TYPES);
254 if (result
255 ? strcmp (result, expect.data)
256 : strcmp (input.data, expect.data))
258 fail (lineno, format.data, input.data, result, expect.data);
259 failures++;
261 free (result);
263 if (no_params)
265 getline (&expect);
266 result = cplus_demangle (input.data, DMGL_ANSI|DMGL_TYPES);
268 if (result
269 ? strcmp (result, expect.data)
270 : strcmp (input.data, expect.data))
272 fail (lineno, format.data, input.data, result, expect.data);
273 failures++;
275 free (result);
279 free (format.data);
280 free (input.data);
281 free (expect.data);
283 printf ("%s: %d tests, %d failures\n", argv[0], tests, failures);
284 return failures ? 1 : 0;