1 /* Demangler test program,
2    Copyright (C) 2002, 2003, 2004 Free Software Foundation, Inc.
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
19    Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
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"
29 
30 struct line
31 {
32   size_t alloced;
33   char *data;
34 };
35 
36 static unsigned int lineno;
37 
38 /* Safely read a single line of arbitrary length from standard input.  */
39 
40 #define LINELEN 80
41 
42 static void
getline(buf)43 getline(buf)
44      struct line *buf;
45 {
46   char *data = buf->data;
47   size_t alloc = buf->alloced;
48   size_t count = 0;
49   int c;
50 
51   if (data == 0)
52     {
53       data = xmalloc (LINELEN);
54       alloc = LINELEN;
55     }
56 
57   /* Skip comment lines.  */
58   while ((c = getchar()) == '#')
59     {
60       while ((c = getchar()) != EOF && c != '\n');
61       lineno++;
62     }
63 
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')
67     {
68       if (count + 1 >= alloc)
69 	{
70 	  alloc *= 2;
71 	  data = xrealloc (data, alloc);
72 	}
73       data[count++] = c;
74       c = getchar();
75     }
76   lineno++;
77   data[count] = '\0';
78 
79   buf->data = data;
80   buf->alloced = alloc;
81 }
82 
83 static void
fail(lineno,opts,in,out,exp)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;
90 {
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);
97 }
98 
99 /* The tester operates on a data file consisting of groups of lines:
100    options
101    input to be demangled
102    expected output
103 
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.
111 
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.
115 */
116 
117 int
main(argc,argv)118 main(argc, argv)
119      int argc;
120      char **argv;
121 {
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;
132 
133   if (argc > 1)
134     {
135       fprintf (stderr, "usage: %s < test-set\n", argv[0]);
136       return 2;
137     }
138 
139   format.data = 0;
140   input.data = 0;
141   expect.data = 0;
142 
143   for (;;)
144     {
145       getline (&format);
146       if (feof (stdin))
147 	break;
148 
149       getline (&input);
150       getline (&expect);
151 
152       tests++;
153 
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] != '-')
160 	{
161 	  style = cplus_demangle_name_to_style (format.data);
162 	  if (style == unknown_demangling)
163 	    {
164 	      printf ("FAIL at line %d: unknown demangling style %s\n",
165 		      lineno, format.data);
166 	      failures++;
167 	      continue;
168 	    }
169 	}
170       else
171 	{
172 	  char *p;
173 	  char *opt;
174 
175 	  p = format.data;
176 	  while (*p != '\0')
177 	    {
178 	      char c;
179 
180 	      opt = p;
181 	      p += strcspn (p, " \t=");
182 	      c = *p;
183 	      *p = '\0';
184 	      if (strcmp (opt, "--format") == 0 && c == '=')
185 		{
186 		  char *fstyle;
187 
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)
196 		    {
197 		      printf ("FAIL at line %d: unknown demangling style %s\n",
198 			      lineno, fstyle);
199 		      failures++;
200 		      continue;
201 		    }
202 		}
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
210 		{
211 		  printf ("FAIL at line %d: unrecognized option %s\n",
212 			  lineno, opt);
213 		  failures++;
214 		  continue;
215 		}
216 	      *p = c;
217 	      p += strspn (p, " \t");
218 	    }
219 	}
220 
221       if (is_v3_ctor || is_v3_dtor)
222 	{
223 	  char buf[20];
224 
225 	  if (is_v3_ctor)
226 	    {
227 	      enum gnu_v3_ctor_kinds kc;
228 
229 	      kc = is_gnu_v3_mangled_ctor (input.data);
230 	      sprintf (buf, "%d", (int) kc);
231 	    }
232 	  else
233 	    {
234 	      enum gnu_v3_dtor_kinds kd;
235 
236 	      kd = is_gnu_v3_mangled_dtor (input.data);
237 	      sprintf (buf, "%d", (int) kd);
238 	    }
239 
240 	  if (strcmp (buf, expect.data) != 0)
241 	    {
242 	      fail (lineno, format.data, input.data, buf, expect.data);
243 	      failures++;
244 	    }
245 
246 	  continue;
247 	}
248 
249       cplus_demangle_set_style (style);
250 
251       result = cplus_demangle (input.data,
252 			       DMGL_PARAMS|DMGL_ANSI|DMGL_TYPES);
253 
254       if (result
255 	  ? strcmp (result, expect.data)
256 	  : strcmp (input.data, expect.data))
257 	{
258 	  fail (lineno, format.data, input.data, result, expect.data);
259 	  failures++;
260 	}
261       free (result);
262 
263       if (no_params)
264 	{
265 	  getline (&expect);
266 	  result = cplus_demangle (input.data, DMGL_ANSI|DMGL_TYPES);
267 
268 	  if (result
269 	      ? strcmp (result, expect.data)
270 	      : strcmp (input.data, expect.data))
271 	    {
272 	      fail (lineno, format.data, input.data, result, expect.data);
273 	      failures++;
274 	    }
275 	  free (result);
276 	}
277     }
278 
279   free (format.data);
280   free (input.data);
281   free (expect.data);
282 
283   printf ("%s: %d tests, %d failures\n", argv[0], tests, failures);
284   return failures ? 1 : 0;
285 }
286