Update.
[platform/upstream/glibc.git] / conform / conformtest.pl
1 #! /usr/bin/perl
2
3 $CC = "gcc";
4 $CFLAGS = "-I. '-D__attribute__(x)=' -D_XOPEN_SOURCE=500";
5
6 # List of the headers we are testing.
7 @headers = ("search.h", "sched.h", "regex.h", "pwd.h", "pthread.h",
8             "poll.h", "nl_types.h", "ndbm.h", "mqueue.h", "monetary.h",
9             "math.h", "locale.h", "libgen.h", "langinfo.h", "iso646.h",
10             "inttypes.h", "iconv.h", "grp.h", "glob.h", "ftw.h", "fnmatch.h",
11             "fmtmsg.h", "float.h", "fcntl.h", "errno.h", "dlfcn.h", "dirent.h",
12             "ctype.h", "cpio.h", "assert.h", "aio.h");
13
14 # These are the ISO C9x keywords.
15 @keywords = ('auto', 'break', 'case', 'char', 'const', 'continue', 'default',
16              'do', 'double', 'else', 'enum', 'extern', 'float', 'for', 'goto',
17              'if', 'inline', 'int', 'long', 'register', 'restrict', 'return',
18              'short', 'signed', 'sizeof', 'static', 'struct', 'switch',
19              'typedef', 'union', 'unsigned', 'void', 'volatile', 'while');
20
21 # Some headers need a bit more attention.
22 $mustprepend{'regex.h'} = "#include <sys/types.h>\n";
23
24 # Make an hash table from this information.
25 while ($#keywords) {
26   $iskeyword{pop (@keywords)} = 1;
27 }
28
29 $tmpdir = "/tmp";
30
31 $verbose = 1;
32
33 $total = 0;
34 $skipped = 0;
35 $errors = 0;
36
37 #$dialect = "ISO";
38 #$dialect = "POSIX";
39 #$dialect = "XPG3";
40 #$dialect = "XPG4";
41 $dialect = "UNIX98";
42
43
44 sub poorfnmatch {
45   my($pattern, $string) = @_;
46   my($strlen) = length ($string);
47   my($res);
48
49   if (substr ($pattern, 0, 1) eq '*') {
50     my($patlen) = length ($pattern) - 1;
51     $res = ($strlen >= $patlen
52             && substr ($pattern, -$patlen, $patlen) eq substr ($string, -$patlen, $patlen));
53   } elsif (substr ($pattern, -1, 1) eq '*') {
54     my($patlen) = length ($pattern) - 1;
55     $res = ($strlen >= $patlen
56             && substr ($pattern, 0, $patlen) eq substr ($string, 0, $patlen));
57   } else {
58     $res = $pattern eq $string;
59   }
60   return $res;
61 }
62
63
64 sub compiletest
65 {
66   my($fnamebase, $msg, $errmsg, $skip) = @_;
67   my($result) = $skip;
68   my($printlog) = 0;
69
70   ++$total;
71   printf ("  $msg...");
72
73   if ($skip != 0) {
74     ++$skipped;
75     printf (" SKIP\n");
76   } else {
77     $ret = system "$CC $CFLAGS -c $fnamebase.c -o $fnamebase.o > $fnamebase.out 2>&1";
78     if ($ret != 0) {
79       printf (" FAIL\n");
80       if ($verbose != 0) {
81         printf ("    $errmsg  Compiler message:\n");
82         $printlog = 1;
83       }
84       ++$errors;
85       $result = 1;
86     } else {
87       printf (" OK\n");
88       if ($verbose > 1 && -s "$fnamebase.out") {
89         # We print all warnings issued.
90         $printlog = 1;
91       }
92     }
93     if ($printlog != 0) {
94       printf ("    " . "-" x 71 . "\n");
95       open (MESSAGE, "< $fnamebase.out");
96       while (<MESSAGE>) {
97         printf ("    %s", $_);
98       }
99       close (MESSAGE);
100       printf ("    " . "-" x 71 . "\n");
101     }
102   }
103   unlink "$fnamebase.c";
104   unlink "$fnamebase.o";
105   unlink "$fnamebase.out";
106
107   $result;
108 }
109
110
111 sub runtest
112 {
113   my($fnamebase, $msg, $errmsg, $skip) = @_;
114   my($result) = $skip;
115   my($printlog) = 0;
116
117   ++$total;
118   printf ("  $msg...");
119
120   if ($skip != 0) {
121     ++$skipped;
122     printf (" SKIP\n");
123   } else {
124     $ret = system "$CC $CFLAGS -o $fnamebase $fnamebase.c > $fnamebase.out 2>&1";
125     if ($ret != 0) {
126       printf (" FAIL\n");
127       if ($verbose != 0) {
128         printf ("    $errmsg  Compiler message:\n");
129         $printlog = 1;
130       }
131       ++$errors;
132       $result = 1;
133     } else {
134       # Now run the program.  If the exit code is not zero something is wrong.
135       $result = system "$fnamebase > $fnamebase.out2 2>&1";
136       if ($result == 0) {
137         printf (" OK\n");
138         if ($verbose > 1 && -s "$fnamebase.out") {
139           # We print all warnings issued.
140           $printlog = 1;
141           system "cat $fnamebase.out2 >> $fnamebase.out";
142         }
143       } else {
144         printf (" FAIL\n");
145         $printlog = 1;
146         unlink "$fnamebase.out";
147         rename "$fnamebase.out2", "$fnamebase.out";
148       }
149     }
150     if ($printlog != 0) {
151       printf ("    " . "-" x 71 . "\n");
152       open (MESSAGE, "< $fnamebase.out");
153       while (<MESSAGE>) {
154         printf ("    %s", $_);
155       }
156       close (MESSAGE);
157       printf ("    " . "-" x 71 . "\n");
158     }
159   }
160   unlink "$fnamebase";
161   unlink "$fnamebase.c";
162   unlink "$fnamebase.o";
163   unlink "$fnamebase.out";
164   unlink "$fnamebase.out2";
165
166   $result;
167 }
168
169
170 sub newtoken {
171   my($token, $nerrors, @allow) = @_;
172   my($idx);
173
174   for ($idx = 0; $idx <= $#allow; ++$idx) {
175     if ($token =~ /^[0-9_]/ || $iskeyword{$token} || poorfnmatch ($allow[$idx], $token)) {
176       return $nerrors;
177     }
178   }
179
180   ++$nerrors;
181   if ($nerrors == 1) {
182     printf ("FAIL\n    " . "-" x 72 . "\n");
183   }
184   printf ("    Namespace violation: \"%s\"\n", $token);
185   return $nerrors;
186 }
187
188
189 sub checknamespace {
190   my($h, $fnamebase, @allow) = @_;
191   my($nerrors) = 0;
192
193   ++$total;
194
195   # Generate a program to get the contents of this header.
196   open (TESTFILE, ">$fnamebase.c");
197   print TESTFILE "#include <$h>\n";
198   close (TESTFILE);
199
200   open (CONTENT, "$CC $CFLAGS -E $fnamebase.c -Wp,-dN | sed -e '/^# [1-9]/d' -e '/^[[:space:]]*\$/d' |");
201   while (<CONTENT>) {
202     chop;
203     if (/^#define (.*)/) {
204       $nerrors = newtoken ($1, $nerrors, @allow);
205     } else {
206       # We have to tokenize the line.
207       my($str) = $_;
208       my($index) = 0;
209       my($len) = length ($str);
210
211       foreach $token (split(/[^a-zA-Z0-9_]/, $str)) {
212         if ($token ne "") {
213           $nerrors = newtoken ($token, $nerrors, @allow);
214         }
215       }
216     }
217   }
218   close (CONTENT);
219   unlink "$fnamebase.c";
220   if ($nerrors != 0) {
221     printf ("    " . "-" x 72 . "\n");
222     ++$errors;
223   } else {
224     printf ("OK\n");
225   }
226 }
227
228
229 while ($#headers >= 0) {
230   my($h) = pop (@headers);
231   my($fnamebase) = "$tmpdir/$h-test";
232   my($missing);
233   my(@allow) = ();
234   my($prepend) = $mustprepend{$h};
235
236   printf ("Testing <$h>\n");
237   printf ("----------" . "-" x length ($h) . "\n");
238
239   # Generate a program to test for the availability of this header.
240   open (TESTFILE, ">$fnamebase.c");
241   print TESTFILE "$prepend";
242   print TESTFILE "#include <$h>\n";
243   close (TESTFILE);
244
245   $missing = compiletest ($fnamebase, "Checking whether <$h> is available",
246                           "Header <$h> not available", 0);
247
248   printf ("\n");
249
250   open (CONTROL, "$CC -E -D$dialect - < data/$h-data |");
251   control: while (<CONTROL>) {
252     chop;
253     next control if (/^#/);
254     next control if (/^[        ]*$/);
255
256     if (/^element *({([^}]*)}|([^ ]*)) *({([^}]*)}|([^ ]*)) *([A-Za-z0-9_]*) *(.*)/) {
257       my($struct) = "$2$3";
258       my($type) = "$5$6";
259       my($member) = "$7";
260       my($rest) = "$8";
261       my($res) = $missing;
262
263       # Remember that this name is allowed.
264       push @allow, $member;
265
266       # Generate a program to test for the availability of this member.
267       open (TESTFILE, ">$fnamebase.c");
268       print TESTFILE "$prepend";
269       print TESTFILE "#include <$h>\n";
270       print TESTFILE "$struct a;\n";
271       print TESTFILE "$struct b;\n";
272       print TESTFILE "extern void xyzzy (__typeof__ (&b.$member), __typeof__ (&a.$member), unsigned);\n";
273       print TESTFILE "void foobarbaz (void) {\n";
274       print TESTFILE "  xyzzy (&a.$member, &b.$member, sizeof (a.$member));\n";
275       print TESTFILE "}\n";
276       close (TESTFILE);
277
278       $res = compiletest ($fnamebase, "Testing for member $member",
279                           "Member \"$member\" not available.", $res);
280
281
282       # Test the types of the members.
283       open (TESTFILE, ">$fnamebase.c");
284       print TESTFILE "$prepend";
285       print TESTFILE "#include <$h>\n";
286       print TESTFILE "$struct a;\n";
287       print TESTFILE "extern $type b$rest;\n";
288       print TESTFILE "extern __typeof__ (a.$member) b;\n";
289       close (TESTFILE);
290
291       compiletest ($fnamebase, "Testing for type of member $member",
292                    "Member \"$member\" does not have the correct type.", $res);
293     } elsif (/^constant *([a-zA-Z0-9_]*) *([A-Za-z0-9_]*)?/) {
294       my($const) = $1;
295       my($value) = $2;
296       my($res) = $missing;
297
298       # Remember that this name is allowed.
299       push @allow, $const;
300
301       # Generate a program to test for the availability of this constant.
302       open (TESTFILE, ">$fnamebase.c");
303       print TESTFILE "$prepend";
304       print TESTFILE "#include <$h>\n";
305       print TESTFILE "__typeof__ ($const) a = $const;\n";
306       close (TESTFILE);
307
308       $res = compiletest ($fnamebase, "Testing for constant $const",
309                           "Constant \"$const\" not available.", $res);
310
311       if ($value ne "") {
312         # Generate a program to test for the value of this constant.
313         open (TESTFILE, ">$fnamebase.c");
314         print TESTFILE "$prepend";
315         print TESTFILE "#include <$h>\n";
316         print TESTFILE "int main (void) { return $const != $value; }\n";
317         close (TESTFILE);
318
319         $res = runtest ($fnamebase, "Testing for value of constant $const",
320                         "Constant \"$const\" has not the right value.", $res);
321       }
322     } elsif (/^type *({([^}]*)|([a-zA-Z0-9_]*))/) {
323       my($type) = "$2$3";
324
325       # Remember that this name is allowed.
326       if ($type =~ /^struct *(.*)/) {
327         push @allow, $1;
328       } elsif ($type =~ /^union *(.*)/) {
329         push @allow, $1;
330       } else {
331         push @allow, $type;
332       }
333
334       # Remember that this name is allowed.
335       push @allow, $type;
336
337       # Generate a program to test for the availability of this constant.
338       open (TESTFILE, ">$fnamebase.c");
339       print TESTFILE "$prepend";
340       print TESTFILE "#include <$h>\n";
341       print TESTFILE "$type *a;\n";
342       close (TESTFILE);
343
344       compiletest ($fnamebase, "Testing for type $type",
345                    "Type \"$type\" not available.", $missing);
346     } elsif (/^function *({([^}]*)}|([a-zA-Z0-9_]*)) ([a-zA-Z0-9_]*) ([(][^)]*[)])/) {
347       my($rettype) = "$2$3";
348       my($fname) = "$4";
349       my($args) = "$5";
350       my($res) = $missing;
351
352       # Remember that this name is allowed.
353       push @allow, $fname;
354
355       # Generate a program to test for availability of this function.
356       open (TESTFILE, ">$fnamebase.c");
357       print TESTFILE "$prepend";
358       print TESTFILE "#include <$h>\n";
359       # print TESTFILE "#undef $fname\n";
360       print TESTFILE "$rettype (*foobarbaz) $args = $fname;\n";
361       close (TESTFILE);
362
363       $res = compiletest ($fnamebase, "Test availability of function $fname",
364                           "Function \"$fname\" is not available.", $res);
365
366       # Generate a program to test for the type of this function.
367       open (TESTFILE, ">$fnamebase.c");
368       print TESTFILE "$prepend";
369       print TESTFILE "#include <$h>\n";
370       # print TESTFILE "#undef $fname\n";
371       print TESTFILE "extern $rettype (*foobarbaz) $args;\n";
372       print TESTFILE "extern __typeof__ (&$fname) foobarbaz;\n";
373       close (TESTFILE);
374
375       compiletest ($fnamebase, "Test for type of function $fname",
376                    "Function \"$fname\" has incorrect type.", $res);
377     } elsif (/^macro *([^       ]*)/) {
378       my($macro) = "$1";
379
380       # Remember that this name is allowed.
381       push @allow, $macro;
382
383       # Generate a program to test for availability of this macro.
384       open (TESTFILE, ">$fnamebase.c");
385       print TESTFILE "$prepend";
386       print TESTFILE "#include <$h>\n";
387       print TESTFILE "#ifndef $macro\n";
388       print TESTFILE "# error \"Macro $macro not defined\"\n";
389       print TESTFILE "#endif\n";
390       close (TESTFILE);
391
392       compiletest ($fnamebase, "Test availability of macro $macro",
393                    "Macro \"$macro\" is not available.", $missing);
394     } elsif (/^allow *(.*)/) {
395       my($pattern) = $1;
396       push @allow, $pattern;
397       next control;
398     } else {
399       # printf ("line is `%s'\n", $_);
400       next control;
401     }
402
403     printf ("\n");
404   }
405   close (CONTROL);
406
407   # Now check the namespace.
408   printf ("  Checking the namespace of \"%s\"... ", $h);
409   if ($missing) {
410     ++$skipped;
411     printf ("SKIP\n");
412   } else {
413     checknamespace ($h, $fnamebase, @allow);
414   }
415
416   printf ("\n\n");
417 }
418
419 printf "-" x 76 . "\n";
420 printf ("  Total number of tests  : %4d\n", $total);
421 printf ("  Number of failed tests : %4d (%3d%%)\n", $errors, ($errors * 100) / $total);
422 printf ("  Number of skipped tests: %4d (%3d%%)\n", $skipped, ($skipped * 100) / $total);
423
424 exit $errors != 0;