Print this page
libiberty/testsuite: Avoid conflicting getline()
Split |
Close |
Expand all |
Collapse all |
--- old/libiberty/testsuite/test-demangle.c
+++ new/libiberty/testsuite/test-demangle.c
1 1 /* Demangler test program,
2 2 Copyright (C) 2002, 2003, 2004 Free Software Foundation, Inc.
3 3 Written by Zack Weinberg <zack@codesourcery.com
4 4
5 5 This file is part of GNU libiberty.
6 6
7 7 This program is free software; you can redistribute it and/or modify
8 8 it under the terms of the GNU General Public License as published by
9 9 the Free Software Foundation; either version 2 of the License, or
10 10 (at your option) any later version.
11 11
12 12 This program is distributed in the hope that it will be useful,
13 13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 15 GNU General Public License for more details.
16 16
17 17 You should have received a copy of the GNU General Public License
18 18 along with this program; if not, write to the Free Software
19 19 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20 20 */
21 21
22 22 #ifdef HAVE_CONFIG_H
23 23 #include "config.h"
24 24 #endif
25 25 #include "ansidecl.h"
26 26 #include <stdio.h>
27 27 #include "libiberty.h"
28 28 #include "demangle.h"
29 29 #ifdef HAVE_STRING_H
30 30 #include <string.h>
31 31 #endif
32 32 #if HAVE_STDLIB_H
33 33 # include <stdlib.h>
34 34 #endif
35 35
36 36 struct line
37 37 {
38 38 size_t alloced;
↓ open down ↓ |
38 lines elided |
↑ open up ↑ |
39 39 char *data;
40 40 };
41 41
42 42 static unsigned int lineno;
43 43
44 44 /* Safely read a single line of arbitrary length from standard input. */
45 45
46 46 #define LINELEN 80
47 47
48 48 static void
49 -getline(buf)
49 +_getline(buf)
50 50 struct line *buf;
51 51 {
52 52 char *data = buf->data;
53 53 size_t alloc = buf->alloced;
54 54 size_t count = 0;
55 55 int c;
56 56
57 57 if (data == 0)
58 58 {
59 59 data = xmalloc (LINELEN);
60 60 alloc = LINELEN;
61 61 }
62 62
63 63 /* Skip comment lines. */
64 64 while ((c = getchar()) == '#')
65 65 {
66 66 while ((c = getchar()) != EOF && c != '\n');
67 67 lineno++;
68 68 }
69 69
70 70 /* c is the first character on the line, and it's not a comment
71 71 line: copy this line into the buffer and return. */
72 72 while (c != EOF && c != '\n')
73 73 {
74 74 if (count + 1 >= alloc)
75 75 {
76 76 alloc *= 2;
77 77 data = xrealloc (data, alloc);
78 78 }
79 79 data[count++] = c;
80 80 c = getchar();
81 81 }
82 82 lineno++;
83 83 data[count] = '\0';
84 84
85 85 buf->data = data;
86 86 buf->alloced = alloc;
87 87 }
88 88
89 89 /* If we have mmap() and mprotect(), copy the string S just before a
90 90 protected page, so that if the demangler runs over the end of the
91 91 string we'll get a fault, and return the address of the new string.
92 92 If no mmap, or it fails, or it looks too hard, just return S. */
93 93
94 94 #ifdef HAVE_SYS_MMAN_H
95 95 #include <sys/mman.h>
96 96 #endif
97 97 #if defined(MAP_ANON) && ! defined (MAP_ANONYMOUS)
98 98 #define MAP_ANONYMOUS MAP_ANON
99 99 #endif
100 100
101 101 static const char *
102 102 protect_end (const char * s)
103 103 {
104 104 #if defined(HAVE_MMAP) && defined (MAP_ANONYMOUS)
105 105 size_t pagesize = getpagesize();
106 106 static char * buf;
107 107 size_t s_len = strlen (s);
108 108 char * result;
109 109
110 110 /* Don't try if S is too long. */
111 111 if (s_len >= pagesize)
112 112 return s;
113 113
114 114 /* Allocate one page of allocated space followed by an unmapped
115 115 page. */
116 116 if (buf == NULL)
117 117 {
118 118 buf = mmap (NULL, pagesize * 2, PROT_READ | PROT_WRITE,
119 119 MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
120 120 if (! buf)
121 121 return s;
122 122 munmap (buf + pagesize, pagesize);
123 123 }
124 124
125 125 result = buf + (pagesize - s_len - 1);
126 126 memcpy (result, s, s_len + 1);
127 127 return result;
128 128 #else
129 129 return s;
130 130 #endif
131 131 }
132 132
133 133 static void
134 134 fail (lineno, opts, in, out, exp)
135 135 int lineno;
136 136 const char *opts;
137 137 const char *in;
138 138 const char *out;
139 139 const char *exp;
140 140 {
141 141 printf ("\
142 142 FAIL at line %d, options %s:\n\
143 143 in: %s\n\
144 144 out: %s\n\
145 145 exp: %s\n",
146 146 lineno, opts, in, out != NULL ? out : "(null)", exp);
147 147 }
148 148
149 149 /* The tester operates on a data file consisting of groups of lines:
150 150 options
151 151 input to be demangled
152 152 expected output
153 153
154 154 Supported options:
155 155 --format=<name> Sets the demangling style.
156 156 --no-params There are two lines of expected output; the first
157 157 is with DMGL_PARAMS, the second is without it.
158 158 --is-v3-ctor Calls is_gnu_v3_mangled_ctor on input; expected
159 159 output is an integer representing ctor_kind.
160 160 --is-v3-dtor Likewise, but for dtors.
161 161 --ret-postfix Passes the DMGL_RET_POSTFIX option
162 162
163 163 For compatibility, just in case it matters, the options line may be
164 164 empty, to mean --format=auto. If it doesn't start with --, then it
165 165 may contain only a format name.
166 166 */
167 167
168 168 int
169 169 main(argc, argv)
170 170 int argc;
171 171 char **argv;
172 172 {
173 173 enum demangling_styles style = auto_demangling;
174 174 int no_params;
175 175 int is_v3_ctor;
176 176 int is_v3_dtor;
177 177 int ret_postfix;
178 178 struct line format;
179 179 struct line input;
180 180 struct line expect;
181 181 char *result;
182 182 int failures = 0;
183 183 int tests = 0;
184 184
185 185 if (argc > 1)
186 186 {
187 187 fprintf (stderr, "usage: %s < test-set\n", argv[0]);
188 188 return 2;
↓ open down ↓ |
129 lines elided |
↑ open up ↑ |
189 189 }
190 190
191 191 format.data = 0;
192 192 input.data = 0;
193 193 expect.data = 0;
194 194
195 195 for (;;)
196 196 {
197 197 const char *inp;
198 198
199 - getline (&format);
199 + _getline (&format);
200 200 if (feof (stdin))
201 201 break;
202 202
203 - getline (&input);
204 - getline (&expect);
203 + _getline (&input);
204 + _getline (&expect);
205 205
206 206 inp = protect_end (input.data);
207 207
208 208 tests++;
209 209
210 210 no_params = 0;
211 211 ret_postfix = 0;
212 212 is_v3_ctor = 0;
213 213 is_v3_dtor = 0;
214 214 if (format.data[0] == '\0')
215 215 style = auto_demangling;
216 216 else if (format.data[0] != '-')
217 217 {
218 218 style = cplus_demangle_name_to_style (format.data);
219 219 if (style == unknown_demangling)
220 220 {
221 221 printf ("FAIL at line %d: unknown demangling style %s\n",
222 222 lineno, format.data);
223 223 failures++;
224 224 continue;
225 225 }
226 226 }
227 227 else
228 228 {
229 229 char *p;
230 230 char *opt;
231 231
232 232 p = format.data;
233 233 while (*p != '\0')
234 234 {
235 235 char c;
236 236
237 237 opt = p;
238 238 p += strcspn (p, " \t=");
239 239 c = *p;
240 240 *p = '\0';
241 241 if (strcmp (opt, "--format") == 0 && c == '=')
242 242 {
243 243 char *fstyle;
244 244
245 245 *p = c;
246 246 ++p;
247 247 fstyle = p;
248 248 p += strcspn (p, " \t");
249 249 c = *p;
250 250 *p = '\0';
251 251 style = cplus_demangle_name_to_style (fstyle);
252 252 if (style == unknown_demangling)
253 253 {
254 254 printf ("FAIL at line %d: unknown demangling style %s\n",
255 255 lineno, fstyle);
256 256 failures++;
257 257 continue;
258 258 }
259 259 }
260 260 else if (strcmp (opt, "--no-params") == 0)
261 261 no_params = 1;
262 262 else if (strcmp (opt, "--is-v3-ctor") == 0)
263 263 is_v3_ctor = 1;
264 264 else if (strcmp (opt, "--is-v3-dtor") == 0)
265 265 is_v3_dtor = 1;
266 266 else if (strcmp (opt, "--ret-postfix") == 0)
267 267 ret_postfix = 1;
268 268 else
269 269 {
270 270 printf ("FAIL at line %d: unrecognized option %s\n",
271 271 lineno, opt);
272 272 failures++;
273 273 continue;
274 274 }
275 275 *p = c;
276 276 p += strspn (p, " \t");
277 277 }
278 278 }
279 279
280 280 if (is_v3_ctor || is_v3_dtor)
281 281 {
282 282 char buf[20];
283 283
284 284 if (is_v3_ctor)
285 285 {
286 286 enum gnu_v3_ctor_kinds kc;
287 287
288 288 kc = is_gnu_v3_mangled_ctor (inp);
289 289 sprintf (buf, "%d", (int) kc);
290 290 }
291 291 else
292 292 {
293 293 enum gnu_v3_dtor_kinds kd;
294 294
295 295 kd = is_gnu_v3_mangled_dtor (inp);
296 296 sprintf (buf, "%d", (int) kd);
297 297 }
298 298
299 299 if (strcmp (buf, expect.data) != 0)
300 300 {
301 301 fail (lineno, format.data, input.data, buf, expect.data);
302 302 failures++;
303 303 }
304 304
305 305 continue;
306 306 }
307 307
308 308 cplus_demangle_set_style (style);
309 309
310 310 result = cplus_demangle (inp,
311 311 DMGL_PARAMS|DMGL_ANSI|DMGL_TYPES
312 312 |(ret_postfix ? DMGL_RET_POSTFIX : 0));
313 313
314 314 if (result
↓ open down ↓ |
100 lines elided |
↑ open up ↑ |
315 315 ? strcmp (result, expect.data)
316 316 : strcmp (input.data, expect.data))
317 317 {
318 318 fail (lineno, format.data, input.data, result, expect.data);
319 319 failures++;
320 320 }
321 321 free (result);
322 322
323 323 if (no_params)
324 324 {
325 - getline (&expect);
325 + _getline (&expect);
326 326 result = cplus_demangle (inp, DMGL_ANSI|DMGL_TYPES);
327 327
328 328 if (result
329 329 ? strcmp (result, expect.data)
330 330 : strcmp (input.data, expect.data))
331 331 {
332 332 fail (lineno, format.data, input.data, result, expect.data);
333 333 failures++;
334 334 }
335 335 free (result);
336 336 }
337 337 }
338 338
339 339 free (format.data);
340 340 free (input.data);
341 341 free (expect.data);
342 342
343 343 printf ("%s: %d tests, %d failures\n", argv[0], tests, failures);
344 344 return failures ? 1 : 0;
345 345 }
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX