1 /* Demangler test program,
2    Copyright (C) 2002 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
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 >= 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 /* The tester operates on a data file consisting of triples of lines:
84    format switch
85    input to be demangled
86    expected output
87 
88    The format switch is expected to be either the empty string, a
89    line of the form --format=<name>, or just <name> by itself.  */
90 
91 #define FORMATS "--format="
92 #define FORMATL (sizeof FORMATS - 1)
93 
94 int
95 main(argc, argv)
96      int argc;
97      char **argv;
98 {
99   enum demangling_styles style;
100   struct line format;
101   struct line input;
102   struct line expect;
103   char *fstyle;
104   char *result;
105   int failures = 0;
106   int tests = 0;
107 
108   if (argc > 1)
109     {
110       fprintf (stderr, "usage: %s < test-set\n", argv[0]);
111       return 2;
112     }
113 
114   format.data = 0;
115   input.data = 0;
116   expect.data = 0;
117 
118   for (;;)
119     {
120       getline (&format);
121       if (feof (stdin))
122 	break;
123 
124       getline (&input);
125       getline (&expect);
126 
127       tests++;
128 
129       fstyle = format.data;
130       if (!strncmp (fstyle, FORMATS, FORMATL))
131 	fstyle += FORMATL;
132 
133       if (fstyle[0] == '\0')
134 	style = auto_demangling;
135       else
136 	style = cplus_demangle_name_to_style (fstyle);
137 
138       if (style == unknown_demangling)
139 	{
140 	  printf ("FAIL at line %d: unknown demangling style %s\n",
141 		  lineno, fstyle);
142 	  failures++;
143 	  continue;
144 	}
145 
146       cplus_demangle_set_style (style);
147 
148       result = cplus_demangle (input.data,
149 			       DMGL_PARAMS|DMGL_ANSI|DMGL_VERBOSE|DMGL_TYPES);
150 
151       if (result
152 	  ? strcmp (result, expect.data)
153 	  : strcmp (input.data, expect.data))
154 	{
155 	  printf ("\
156 FAIL at line %d, style %s:\n\
157 in:  %s\n\
158 out: %s\n\
159 exp: %s\n",
160 		   lineno, fstyle,
161 		   input.data,
162 		   result,
163 		   expect.data);
164 	  failures++;
165 	}
166       free (result);
167     }
168 
169   free (format.data);
170   free (input.data);
171   free (expect.data);
172 
173   printf ("%s: %d tests, %d failures\n", argv[0], tests, failures);
174   return failures ? 1 : 0;
175 }
176