1 /* Demangler test program,
2 Copyright (C) 2002-2024 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.
27 #include "libiberty.h"
45 static unsigned int lineno
;
47 /* Safely read a single line of arbitrary length from standard input. */
52 get_line(struct line
*buf
)
54 char *data
= buf
->data
;
55 size_t alloc
= buf
->alloced
;
61 data
= xmalloc (LINELEN
);
65 /* Skip comment lines. */
66 while ((c
= getchar()) == '#')
68 while ((c
= getchar()) != EOF
&& c
!= '\n');
72 /* c is the first character on the line, and it's not a comment
73 line: copy this line into the buffer and return. */
74 while (c
!= EOF
&& c
!= '\n')
76 if (count
+ 1 >= alloc
)
79 data
= xrealloc (data
, alloc
);
91 /* If we have mmap() and mprotect(), copy the string S just before a
92 protected page, so that if the demangler runs over the end of the
93 string we'll get a fault, and return the address of the new string.
94 If no mmap, or it fails, or it looks too hard, just return S. */
96 #ifdef HAVE_SYS_MMAN_H
99 #if defined(MAP_ANON) && ! defined (MAP_ANONYMOUS)
100 #define MAP_ANONYMOUS MAP_ANON
104 protect_end (const char * s
)
106 #if defined(HAVE_MMAP) && defined (MAP_ANONYMOUS)
107 size_t pagesize
= getpagesize();
109 size_t s_len
= strlen (s
);
112 /* Don't try if S is too long. */
113 if (s_len
>= pagesize
)
116 /* Allocate one page of allocated space followed by an unmapped
120 buf
= mmap (NULL
, pagesize
* 2, PROT_READ
| PROT_WRITE
,
121 MAP_PRIVATE
| MAP_ANONYMOUS
, -1, 0);
124 munmap (buf
+ pagesize
, pagesize
);
127 result
= buf
+ (pagesize
- s_len
- 1);
128 memcpy (result
, s
, s_len
+ 1);
136 fail (int lineno
, const char *opts
, const char *in
,
137 const char *out
, const char *exp
)
140 FAIL at line %d, options %s:\n\
144 lineno
, opts
, in
, out
!= NULL
? out
: "(null)", exp
);
147 /* The tester operates on a data file consisting of groups of lines:
149 input to be demangled
153 --format=<name> Sets the demangling style.
154 --no-params There are two lines of expected output; the first
155 is with DMGL_PARAMS, the second is without it.
156 --is-v3-ctor Calls is_gnu_v3_mangled_ctor on input; expected
157 output is an integer representing ctor_kind.
158 --is-v3-dtor Likewise, but for dtors.
159 --ret-postfix Passes the DMGL_RET_POSTFIX option
160 --ret-drop Passes the DMGL_RET_DROP option
162 For compatibility, just in case it matters, the options line may be
163 empty, to mean --format=auto. If it doesn't start with --, then it
164 may contain only a format name.
168 main(int argc
, char **argv
)
170 enum demangling_styles style
= auto_demangling
;
174 int ret_postfix
, ret_drop
;
184 fprintf (stderr
, "usage: %s < test-set\n", argv
[0]);
203 inp
= protect_end (input
.data
);
212 if (format
.data
[0] == '\0')
213 style
= auto_demangling
;
214 else if (format
.data
[0] != '-')
216 style
= cplus_demangle_name_to_style (format
.data
);
217 if (style
== unknown_demangling
)
219 printf ("FAIL at line %d: unknown demangling style %s\n",
220 lineno
, format
.data
);
236 p
+= strcspn (p
, " \t=");
239 if (strcmp (opt
, "--format") == 0 && c
== '=')
246 p
+= strcspn (p
, " \t");
249 style
= cplus_demangle_name_to_style (fstyle
);
250 if (style
== unknown_demangling
)
252 printf ("FAIL at line %d: unknown demangling style %s\n",
258 else if (strcmp (opt
, "--no-params") == 0)
260 else if (strcmp (opt
, "--is-v3-ctor") == 0)
262 else if (strcmp (opt
, "--is-v3-dtor") == 0)
264 else if (strcmp (opt
, "--ret-postfix") == 0)
266 else if (strcmp (opt
, "--ret-drop") == 0)
270 printf ("FAIL at line %d: unrecognized option %s\n",
276 p
+= strspn (p
, " \t");
280 if (is_v3_ctor
|| is_v3_dtor
)
286 enum gnu_v3_ctor_kinds kc
;
288 kc
= is_gnu_v3_mangled_ctor (inp
);
289 sprintf (buf
, "%d", (int) kc
);
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
);
308 cplus_demangle_set_style (style
);
310 result
= cplus_demangle (inp
, (DMGL_PARAMS
| DMGL_ANSI
| DMGL_TYPES
311 | (ret_postfix
? DMGL_RET_POSTFIX
: 0)
312 | (ret_drop
? DMGL_RET_DROP
: 0)));
315 ? strcmp (result
, expect
.data
)
316 : strcmp (input
.data
, expect
.data
))
318 fail (lineno
, format
.data
, input
.data
, result
, expect
.data
);
326 result
= cplus_demangle (inp
, DMGL_ANSI
|DMGL_TYPES
);
329 ? strcmp (result
, expect
.data
)
330 : strcmp (input
.data
, expect
.data
))
332 fail (lineno
, format
.data
, input
.data
, result
, expect
.data
);
343 printf ("%s: %d tests, %d failures\n", argv
[0], tests
, failures
);
344 return failures
? 1 : 0;