Update.
[platform/upstream/glibc.git] / conform / conformtest.pl
1 #! /usr/bin/perl
2
3 use Getopt::Long;
4
5 $CC = "gcc";
6
7 $dialect="XOPEN2K";
8 GetOptions ('headers=s' => \@headers, 'dialect=s' => \$dialect);
9 @headers = split(/,/,join(',',@headers));
10
11 # List of the headers we are testing.
12 if (@headers == ()) {
13   @headers = ("wordexp.h", "wctype.h", "wchar.h", "varargs.h", "utmpx.h",
14               "utime.h", "unistd.h", "ulimit.h", "ucontext.h", "time.h",
15               "tgmath.h", "termios.h", "tar.h", "sys/wait.h", "sys/utsname.h",
16               "sys/un.h", "sys/uio.h", "sys/types.h", "sys/times.h",
17               "sys/timeb.h", "sys/time.h", "sys/statvfs.h", "sys/stat.h",
18               "sys/socket.h", "sys/shm.h", "sys/sem.h", "sys/select.h",
19               "sys/resource.h", "sys/msg.h", "sys/mman.h", "sys/ipc.h",
20               "syslog.h", "stropts.h", "strings.h", "string.h", "stdlib.h",
21               "stdio.h", "stdint.h", "stddef.h", "stdarg.h", "spawn.h",
22               "signal.h", "setjmp.h", "semaphore.h", "search.h", "sched.h",
23               "regex.h", "pwd.h", "pthread.h", "poll.h", "nl_types.h",
24               "netinet/tcp.h", "netinet/in.h", "net/if.h", "netdb.h", "ndbm.h",
25               "mqueue.h", "monetary.h", "math.h", "locale.h", "libgen.h",
26               "limits.h", "langinfo.h", "iso646.h", "inttypes.h", "iconv.h",
27               "grp.h", "glob.h", "ftw.h", "fnmatch.h", "fmtmsg.h", "float.h",
28               "fcntl.h", "errno.h", "dlfcn.h", "dirent.h", "ctype.h", "cpio.h",
29               "complex.h", "assert.h", "arpa/inet.h", "aio.h");
30 }
31
32 if ($dialect ne "ISO" && $dialect ne "POSIX" && $dialect ne "XPG3"
33     && $dialect ne "XPG4" && $dialect ne "UNIX98" && $dialect ne "XOPEN2K") {
34   die "unknown dialect \"$dialect\"";
35 }
36
37 $CFLAGS{"ISO"} = "-I. '-D__attribute__(x)=' -ansi";
38 $CFLAGS{"POSIX"} = "-I. '-D__attribute__(x)=' -D_POSIX_C_SOURCE=199912";
39 $CFLAGS{"XPG3"} = "-I. '-D__attribute__(x)=' -D_XOPEN_SOURCE";
40 $CFLAGS{"XPG4"} = "-I. '-D__attribute__(x)=' -D_XOPEN_SOURCE_EXTENDED";
41 $CFLAGS{"UNIX98"} = "-I. '-D__attribute__(x)=' -D_XOPEN_SOURCE=500";
42 $CFLAGS{"XOPEN2K"} = "-I. '-D__attribute__(x)=' -D_XOPEN_SOURCE=600";
43
44
45 # These are the ISO C99 keywords.
46 @keywords = ('auto', 'break', 'case', 'char', 'const', 'continue', 'default',
47              'do', 'double', 'else', 'enum', 'extern', 'float', 'for', 'goto',
48              'if', 'inline', 'int', 'long', 'register', 'restrict', 'return',
49              'short', 'signed', 'sizeof', 'static', 'struct', 'switch',
50              'typedef', 'union', 'unsigned', 'void', 'volatile', 'while');
51
52 # These are symbols which are known to pollute the namespace.
53 @knownproblems = ('unix', 'linux', 'i386');
54
55 # Some headers need a bit more attention.
56 $mustprepend{'regex.h'} = "#include <sys/types.h>\n";
57 $mustprepend{'sched.h'} = "#include <sys/types.h>\n";
58 $mustprepend{'signal.h'} = "#include <pthread.h>\n";
59 $mustprepend{'stdio.h'} = "#include <sys/types.h>\n";
60 $mustprepend{'wchar.h'} = "#include <stdarg.h>\n";
61 $mustprepend{'wordexp.h'} = "#include <stddef.h>\n";
62
63 # Make a hash table from this information.
64 while ($#keywords >= 0) {
65   $iskeyword{pop (@keywords)} = 1;
66 }
67
68 # Make a hash table from the known problems.
69 while ($#knownproblems >= 0) {
70   $isknown{pop (@knownproblems)} = 1;
71 }
72
73 $tmpdir = "/tmp";
74
75 $verbose = 1;
76
77 $total = 0;
78 $skipped = 0;
79 $errors = 0;
80
81
82 sub poorfnmatch {
83   my($pattern, $string) = @_;
84   my($strlen) = length ($string);
85   my($res);
86
87   if (substr ($pattern, 0, 1) eq '*') {
88     my($patlen) = length ($pattern) - 1;
89     $res = ($strlen >= $patlen
90             && substr ($pattern, -$patlen, $patlen) eq substr ($string, -$patlen, $patlen));
91   } elsif (substr ($pattern, -1, 1) eq '*') {
92     my($patlen) = length ($pattern) - 1;
93     $res = ($strlen >= $patlen
94             && substr ($pattern, 0, $patlen) eq substr ($string, 0, $patlen));
95   } else {
96     $res = $pattern eq $string;
97   }
98   return $res;
99 }
100
101
102 sub compiletest
103 {
104   my($fnamebase, $msg, $errmsg, $skip, $optional) = @_;
105   my($result) = $skip;
106   my($printlog) = 0;
107
108   ++$total;
109   printf ("  $msg...");
110
111   if ($skip != 0) {
112     ++$skipped;
113     printf (" SKIP\n");
114   } else {
115     $ret = system "$CC $CFLAGS{$dialect} -c $fnamebase.c -o $fnamebase.o > $fnamebase.out 2>&1";
116     if ($ret != 0) {
117       if ($optional != 0) {
118         printf (" $errmsg\n");
119         $result = 1;
120       } else {
121         printf (" FAIL\n");
122         if ($verbose != 0) {
123           printf ("    $errmsg  Compiler message:\n");
124           $printlog = 1;
125         }
126         ++$errors;
127         $result = 1;
128       }
129     } else {
130       printf (" OK\n");
131       if ($verbose > 1 && -s "$fnamebase.out") {
132         # We print all warnings issued.
133         $printlog = 1;
134       }
135     }
136     if ($printlog != 0) {
137       printf ("    " . "-" x 71 . "\n");
138       open (MESSAGE, "< $fnamebase.out");
139       while (<MESSAGE>) {
140         printf ("    %s", $_);
141       }
142       close (MESSAGE);
143       printf ("    " . "-" x 71 . "\n");
144     }
145   }
146   unlink "$fnamebase.c";
147   unlink "$fnamebase.o";
148   unlink "$fnamebase.out";
149
150   $result;
151 }
152
153
154 sub runtest
155 {
156   my($fnamebase, $msg, $errmsg, $skip) = @_;
157   my($result) = $skip;
158   my($printlog) = 0;
159
160   ++$total;
161   printf ("  $msg...");
162
163   if ($skip != 0) {
164     ++$skipped;
165     printf (" SKIP\n");
166   } else {
167     $ret = system "$CC $CFLAGS{$dialect} -o $fnamebase $fnamebase.c > $fnamebase.out 2>&1";
168     if ($ret != 0) {
169       printf (" FAIL\n");
170       if ($verbose != 0) {
171         printf ("    $errmsg  Compiler message:\n");
172         $printlog = 1;
173       }
174       ++$errors;
175       $result = 1;
176     } else {
177       # Now run the program.  If the exit code is not zero something is wrong.
178       $result = system "$fnamebase > $fnamebase.out2 2>&1";
179       if ($result == 0) {
180         printf (" OK\n");
181         if ($verbose > 1 && -s "$fnamebase.out") {
182           # We print all warnings issued.
183           $printlog = 1;
184           system "cat $fnamebase.out2 >> $fnamebase.out";
185         }
186       } else {
187         printf (" FAIL\n");
188         ++$errors;
189         $printlog = 1;
190         unlink "$fnamebase.out";
191         rename "$fnamebase.out2", "$fnamebase.out";
192       }
193     }
194     if ($printlog != 0) {
195       printf ("    " . "-" x 71 . "\n");
196       open (MESSAGE, "< $fnamebase.out");
197       while (<MESSAGE>) {
198         printf ("    %s", $_);
199       }
200       close (MESSAGE);
201       printf ("    " . "-" x 71 . "\n");
202     }
203   }
204   unlink "$fnamebase";
205   unlink "$fnamebase.c";
206   unlink "$fnamebase.o";
207   unlink "$fnamebase.out";
208   unlink "$fnamebase.out2";
209
210   $result;
211 }
212
213
214 sub newtoken {
215   my($token, @allow) = @_;
216   my($idx);
217
218   return if ($token =~ /^[0-9_]/ || $iskeyword{$token});
219
220   for ($idx = 0; $idx <= $#allow; ++$idx) {
221     return if (poorfnmatch ($allow[$idx], $token));
222   }
223
224   if ($isknown{$token}) {
225     ++$nknown;
226   } else {
227     ++$nerrors;
228     if ($nerrors == 1) {
229       printf ("FAIL\n    " . "-" x 72 . "\n");
230     }
231     printf ("    Namespace violation: \"%s\"\n", $token);
232   }
233 }
234
235
236 sub checknamespace {
237   my($h, $fnamebase, @allow) = @_;
238
239   ++$total;
240
241   # Generate a program to get the contents of this header.
242   open (TESTFILE, ">$fnamebase.c");
243   print TESTFILE "#include <$h>\n";
244   close (TESTFILE);
245
246   $nerrors = 0;
247   $nknown = 0;
248   open (CONTENT, "$CC $CFLAGS{$dialect} -E $fnamebase.c -P -Wp,-dN | sed -e '/^# [1-9]/d' -e '/^[[:space:]]*\$/d' |");
249   loop: while (<CONTENT>) {
250     next loop if (/^#undef /);
251     chop;
252     if (/^#define (.*)/) {
253       newtoken ($1, @allow);
254     } else {
255       # We have to tokenize the line.
256       my($str) = $_;
257       my($index) = 0;
258       my($len) = length ($str);
259
260       foreach $token (split(/[^a-zA-Z0-9_]/, $str)) {
261         if ($token ne "") {
262           newtoken ($token, @allow);
263         }
264       }
265     }
266   }
267   close (CONTENT);
268   unlink "$fnamebase.c";
269   if ($nerrors != 0) {
270     printf ("    " . "-" x 72 . "\n");
271     ++$errors;
272   } elsif ($nknown > 0) {
273     printf ("EXPECTED FAILURES\n");
274     ++$known;
275   } else {
276     printf ("OK\n");
277   }
278 }
279
280
281 while ($#headers >= 0) {
282   my($h) = pop (@headers);
283   my($hf) = $h;
284   $hf =~ s|/|-|;
285   my($fnamebase) = "$tmpdir/$hf-test";
286   my($missing);
287   my(@allow) = ();
288   my(@allowheader) = ();
289   my(%seenheader) = ();
290   my($prepend) = $mustprepend{$h};
291
292   printf ("Testing <$h>\n");
293   printf ("----------" . "-" x length ($h) . "\n");
294
295   # Generate a program to test for the availability of this header.
296   open (TESTFILE, ">$fnamebase.c");
297   print TESTFILE "$prepend";
298   print TESTFILE "#include <$h>\n";
299   close (TESTFILE);
300
301   $missing = compiletest ($fnamebase, "Checking whether <$h> is available",
302                           "Header <$h> not available", 0, 0);
303
304   printf ("\n");
305
306   open (CONTROL, "$CC -E -D$dialect - < data/$h-data |");
307   control: while (<CONTROL>) {
308     chop;
309     next control if (/^#/);
310     next control if (/^[        ]*$/);
311
312     if (/^element *({([^}]*)}|([^{ ]*)) *({([^}]*)}|([^{ ]*)) *([A-Za-z0-9_]*) *(.*)/) {
313       my($struct) = "$2$3";
314       my($type) = "$5$6";
315       my($member) = "$7";
316       my($rest) = "$8";
317       my($res) = $missing;
318
319       # Remember that this name is allowed.
320       push @allow, $member;
321
322       # Generate a program to test for the availability of this member.
323       open (TESTFILE, ">$fnamebase.c");
324       print TESTFILE "$prepend";
325       print TESTFILE "#include <$h>\n";
326       print TESTFILE "$struct a;\n";
327       print TESTFILE "$struct b;\n";
328       print TESTFILE "extern void xyzzy (__typeof__ (&b.$member), __typeof__ (&a.$member), unsigned);\n";
329       print TESTFILE "void foobarbaz (void) {\n";
330       print TESTFILE "  xyzzy (&a.$member, &b.$member, sizeof (a.$member));\n";
331       print TESTFILE "}\n";
332       close (TESTFILE);
333
334       $res = compiletest ($fnamebase, "Testing for member $member",
335                           "Member \"$member\" not available.", $res, 0);
336
337
338       # Test the types of the members.
339       open (TESTFILE, ">$fnamebase.c");
340       print TESTFILE "$prepend";
341       print TESTFILE "#include <$h>\n";
342       print TESTFILE "$struct a;\n";
343       print TESTFILE "extern $type b$rest;\n";
344       print TESTFILE "extern __typeof__ (a.$member) b;\n";
345       close (TESTFILE);
346
347       compiletest ($fnamebase, "Testing for type of member $member",
348                    "Member \"$member\" does not have the correct type.",
349                    $res, 0);
350     } elsif (/^optional-element *({([^}]*)}|([^{ ]*)) *({([^}]*)}|([^{ ]*)) *([A-Za-z0-9_]*) *(.*)/) {
351       my($struct) = "$2$3";
352       my($type) = "$5$6";
353       my($member) = "$7";
354       my($rest) = "$8";
355       my($res) = $missing;
356
357       # Remember that this name is allowed.
358       push @allow, $member;
359
360       # Generate a program to test for the availability of this member.
361       open (TESTFILE, ">$fnamebase.c");
362       print TESTFILE "$prepend";
363       print TESTFILE "#include <$h>\n";
364       print TESTFILE "$struct a;\n";
365       print TESTFILE "$struct b;\n";
366       print TESTFILE "extern void xyzzy (__typeof__ (&b.$member), __typeof__ (&a.$member), unsigned);\n";
367       print TESTFILE "void foobarbaz (void) {\n";
368       print TESTFILE "  xyzzy (&a.$member, &b.$member, sizeof (a.$member));\n";
369       print TESTFILE "}\n";
370       close (TESTFILE);
371
372       $res = compiletest ($fnamebase, "Testing for member $member",
373                           "NOT AVAILABLE.", $res, 1);
374
375       if ($res == 0 || $missing != 0) {
376         # Test the types of the members.
377         open (TESTFILE, ">$fnamebase.c");
378         print TESTFILE "$prepend";
379         print TESTFILE "#include <$h>\n";
380         print TESTFILE "$struct a;\n";
381         print TESTFILE "extern $type b$rest;\n";
382         print TESTFILE "extern __typeof__ (a.$member) b;\n";
383         close (TESTFILE);
384
385         compiletest ($fnamebase, "Testing for type of member $member",
386                      "Member \"$member\" does not have the correct type.",
387                      $res, 0);
388       }
389     } elsif (/^optional-constant *([a-zA-Z0-9_]*) ([>=<]+) ([A-Za-z0-9_]*)/) {
390       my($const) = $1;
391       my($op) = $2;
392       my($value) = $3;
393       my($res) = $missing;
394
395       # Remember that this name is allowed.
396       push @allow, $const;
397
398       # Generate a program to test for the availability of this constant.
399       open (TESTFILE, ">$fnamebase.c");
400       print TESTFILE "$prepend";
401       print TESTFILE "#include <$h>\n";
402       print TESTFILE "__typeof__ ($const) a = $const;\n";
403       close (TESTFILE);
404
405       $res = compiletest ($fnamebase, "Testing for constant $const",
406                           "NOT PRESENT", $res, 1);
407
408       if ($value ne "" && $res == 0) {
409         # Generate a program to test for the value of this constant.
410         open (TESTFILE, ">$fnamebase.c");
411         print TESTFILE "$prepend";
412         print TESTFILE "#include <$h>\n";
413         # Negate the value since 0 means ok
414         print TESTFILE "int main (void) { return !($const $op $value); }\n";
415         close (TESTFILE);
416
417         $res = runtest ($fnamebase, "Testing for value of constant $const",
418                         "Constant \"$const\" has not the right value.", $res);
419       }
420     } elsif (/^constant *([a-zA-Z0-9_]*) *([>=<]+) ([A-Za-z0-9_]*)/) {
421       my($const) = $1;
422       my($op) = $2;
423       my($value) = $3;
424       my($res) = $missing;
425
426       # Remember that this name is allowed.
427       push @allow, $const;
428
429       # Generate a program to test for the availability of this constant.
430       open (TESTFILE, ">$fnamebase.c");
431       print TESTFILE "$prepend";
432       print TESTFILE "#include <$h>\n";
433       print TESTFILE "__typeof__ ($const) a = $const;\n";
434       close (TESTFILE);
435
436       $res = compiletest ($fnamebase, "Testing for constant $const",
437                           "Constant \"$const\" not available.", $res, 0);
438
439       if ($value ne "") {
440         # Generate a program to test for the value of this constant.
441         open (TESTFILE, ">$fnamebase.c");
442         print TESTFILE "$prepend";
443         print TESTFILE "#include <$h>\n";
444         # Negate the value since 0 means ok
445         print TESTFILE "int main (void) { return !($const $op $value); }\n";
446         close (TESTFILE);
447
448         $res = runtest ($fnamebase, "Testing for value of constant $const",
449                         "Constant \"$const\" has not the right value.", $res);
450       }
451     } elsif (/^typed-constant *([a-zA-Z0-9_]*) *({([^}]*)}|([^ ]*)) *([A-Za-z0-9_]*)?/) {
452       my($const) = $1;
453       my($type) = "$3$4";
454       my($value) = $5;
455       my($res) = $missing;
456
457       # Remember that this name is allowed.
458       push @allow, $const;
459
460       # Generate a program to test for the availability of this constant.
461       open (TESTFILE, ">$fnamebase.c");
462       print TESTFILE "$prepend";
463       print TESTFILE "#include <$h>\n";
464       print TESTFILE "__typeof__ ($const) a = $const;\n";
465       close (TESTFILE);
466
467       $res = compiletest ($fnamebase, "Testing for constant $const",
468                           "Constant \"$const\" not available.", $res, 0);
469
470       # Test the types of the members.
471       open (TESTFILE, ">$fnamebase.c");
472       print TESTFILE "$prepend";
473       print TESTFILE "#include <$h>\n";
474       print TESTFILE "__typeof__ (($type) 0) a;\n";
475       print TESTFILE "extern __typeof__ ($const) a;\n";
476       close (TESTFILE);
477
478       compiletest ($fnamebase, "Testing for type of constant $const",
479                    "Constant \"$const\" does not have the correct type.",
480                    $res, 0);
481
482       if ($value ne "") {
483         # Generate a program to test for the value of this constant.
484         open (TESTFILE, ">$fnamebase.c");
485         print TESTFILE "$prepend";
486         print TESTFILE "#include <$h>\n";
487         print TESTFILE "int main (void) { return $const != $value; }\n";
488         close (TESTFILE);
489
490         $res = runtest ($fnamebase, "Testing for value of constant $const",
491                         "Constant \"$const\" has not the right value.", $res);
492       }
493     } elsif (/^optional-constant *([a-zA-Z0-9_]*) *([A-Za-z0-9_]*)?/) {
494       my($const) = $1;
495       my($value) = $2;
496       my($res) = $missing;
497
498       # Remember that this name is allowed.
499       push @allow, $const;
500
501       # Generate a program to test for the availability of this constant.
502       open (TESTFILE, ">$fnamebase.c");
503       print TESTFILE "$prepend";
504       print TESTFILE "#include <$h>\n";
505       print TESTFILE "__typeof__ ($const) a = $const;\n";
506       close (TESTFILE);
507
508       $res = compiletest ($fnamebase, "Testing for constant $const",
509                           "NOT PRESENT", $res, 1);
510
511       if ($value ne "" && $res == 0) {
512         # Generate a program to test for the value of this constant.
513         open (TESTFILE, ">$fnamebase.c");
514         print TESTFILE "$prepend";
515         print TESTFILE "#include <$h>\n";
516         print TESTFILE "int main (void) { return $const != $value; }\n";
517         close (TESTFILE);
518
519         $res = runtest ($fnamebase, "Testing for value of constant $const",
520                         "Constant \"$const\" has not the right value.", $res);
521       }
522     } elsif (/^constant *([a-zA-Z0-9_]*) *([A-Za-z0-9_]*)?/) {
523       my($const) = $1;
524       my($value) = $2;
525       my($res) = $missing;
526
527       # Remember that this name is allowed.
528       push @allow, $const;
529
530       # Generate a program to test for the availability of this constant.
531       open (TESTFILE, ">$fnamebase.c");
532       print TESTFILE "$prepend";
533       print TESTFILE "#include <$h>\n";
534       print TESTFILE "__typeof__ ($const) a = $const;\n";
535       close (TESTFILE);
536
537       $res = compiletest ($fnamebase, "Testing for constant $const",
538                           "Constant \"$const\" not available.", $res, 0);
539
540       if ($value ne "") {
541         # Generate a program to test for the value of this constant.
542         open (TESTFILE, ">$fnamebase.c");
543         print TESTFILE "$prepend";
544         print TESTFILE "#include <$h>\n";
545         print TESTFILE "int main (void) { return $const != $value; }\n";
546         close (TESTFILE);
547
548         $res = runtest ($fnamebase, "Testing for value of constant $const",
549                         "Constant \"$const\" has not the right value.", $res);
550       }
551     } elsif (/^symbol *([a-zA-Z0-9_]*) *([A-Za-z0-9_]*)?/) {
552       my($symbol) = $1;
553       my($value) = $2;
554       my($res) = $missing;
555
556       # Remember that this name is allowed.
557       push @allow, $symbol;
558
559       # Generate a program to test for the availability of this constant.
560       open (TESTFILE, ">$fnamebase.c");
561       print TESTFILE "$prepend";
562       print TESTFILE "#include <$h>\n";
563       print TESTFILE "void foobarbaz (void) {\n";
564       print TESTFILE "__typeof__ ($symbol) a = $symbol;\n";
565       print TESTFILE "}\n";
566       close (TESTFILE);
567
568       $res = compiletest ($fnamebase, "Testing for symbol $symbol",
569                           "Symbol \"$symbol\" not available.", $res, 0);
570
571       if ($value ne "") {
572         # Generate a program to test for the value of this constant.
573         open (TESTFILE, ">$fnamebase.c");
574         print TESTFILE "$prepend";
575         print TESTFILE "#include <$h>\n";
576         print TESTFILE "int main (void) { return $symbol != $value; }\n";
577         close (TESTFILE);
578
579         $res = runtest ($fnamebase, "Testing for value of symbol $symbol",
580                         "Symbol \"$symbol\" has not the right value.", $res);
581       }
582     } elsif (/^typed-constant *([a-zA-Z0-9_]*) *({([^}]*)}|([^ ]*)) *([A-Za-z0-9_]*)?/) {
583       my($const) = $1;
584       my($type) = "$3$4";
585       my($value) = $5;
586       my($res) = $missing;
587
588       # Remember that this name is allowed.
589       push @allow, $const;
590
591       # Generate a program to test for the availability of this constant.
592       open (TESTFILE, ">$fnamebase.c");
593       print TESTFILE "$prepend";
594       print TESTFILE "#include <$h>\n";
595       print TESTFILE "__typeof__ ($const) a = $const;\n";
596       close (TESTFILE);
597
598       $res = compiletest ($fnamebase, "Testing for constant $const",
599                           "Constant \"$const\" not available.", $res, 0);
600
601       # Test the types of the members.
602       open (TESTFILE, ">$fnamebase.c");
603       print TESTFILE "$prepend";
604       print TESTFILE "#include <$h>\n";
605       print TESTFILE "__typeof__ (($type) 0) a;\n";
606       print TESTFILE "extern __typeof__ ($const) a;\n";
607       close (TESTFILE);
608
609       compiletest ($fnamebase, "Testing for type of constant $const",
610                    "Constant \"$const\" does not have the correct type.",
611                    $res, 0);
612
613       if ($value ne "") {
614         # Generate a program to test for the value of this constant.
615         open (TESTFILE, ">$fnamebase.c");
616         print TESTFILE "$prepend";
617         print TESTFILE "#include <$h>\n";
618         print TESTFILE "int main (void) { return $const != $value; }\n";
619         close (TESTFILE);
620
621         $res = runtest ($fnamebase, "Testing for value of constant $const",
622                         "Constant \"$const\" has not the right value.", $res);
623       }
624     } elsif (/^optional-type *({([^}]*)|([a-zA-Z0-9_]*))/) {
625       my($type) = "$2$3";
626       my($maybe_opaque) = 0;
627
628       # Remember that this name is allowed.
629       if ($type =~ /^struct *(.*)/) {
630         push @allow, $1;
631       } elsif ($type =~ /^union *(.*)/) {
632         push @allow, $1;
633       } else {
634         push @allow, $type;
635         $maybe_opaque = 1;
636       }
637
638       # Remember that this name is allowed.
639       push @allow, $type;
640
641       # Generate a program to test for the availability of this constant.
642       open (TESTFILE, ">$fnamebase.c");
643       print TESTFILE "$prepend";
644       print TESTFILE "#include <$h>\n";
645       if ($maybe_opaque == 1) {
646         print TESTFILE "$type *a;\n";
647       } else {
648         print TESTFILE "$type a;\n";
649       }
650       close (TESTFILE);
651
652       compiletest ($fnamebase, "Testing for type $type",
653                    "NOT AVAILABLE", $missing, 1);
654     } elsif (/^type *({([^}]*)|([a-zA-Z0-9_]*))/) {
655       my($type) = "$2$3";
656       my($maybe_opaque) = 0;
657
658       # Remember that this name is allowed.
659       if ($type =~ /^struct *(.*)/) {
660         push @allow, $1;
661       } elsif ($type =~ /^union *(.*)/) {
662         push @allow, $1;
663       } else {
664         push @allow, $type;
665         $maybe_opaque = 1;
666       }
667
668       # Remember that this name is allowed.
669       push @allow, $type;
670
671       # Generate a program to test for the availability of this type.
672       open (TESTFILE, ">$fnamebase.c");
673       print TESTFILE "#include <$h>\n";
674       if ($maybe_opaque == 1) {
675         print TESTFILE "$type *a;\n";
676       } else {
677         print TESTFILE "$type a;\n";
678       }
679       close (TESTFILE);
680
681       compiletest ($fnamebase, "Testing for type $type",
682                    "Type \"$type\" not available.", $missing, 0);
683     } elsif (/^optional-function *({([^}]*)}|([a-zA-Z0-9_]*)) [(][*]([a-zA-Z0-9_]*) ([(].*[)])/) {
684       my($rettype) = "$2$3";
685       my($fname) = "$4";
686       my($args) = "$5";
687       my($res) = $missing;
688
689       # Remember that this name is allowed.
690       push @allow, $fname;
691
692       # Generate a program to test for availability of this function.
693       open (TESTFILE, ">$fnamebase.c");
694       print TESTFILE "$prepend";
695       print TESTFILE "#include <$h>\n";
696       # print TESTFILE "#undef $fname\n";
697       print TESTFILE "$rettype (*(*foobarbaz) $args = $fname;\n";
698       close (TESTFILE);
699
700       $res = compiletest ($fnamebase, "Test availability of function $fname",
701                           "NOT AVAILABLE", $res, 1);
702
703       if ($res == 0 || $missing == 1) {
704         # Generate a program to test for the type of this function.
705         open (TESTFILE, ">$fnamebase.c");
706         print TESTFILE "$prepend";
707         print TESTFILE "#include <$h>\n";
708         # print TESTFILE "#undef $fname\n";
709         print TESTFILE "extern $rettype (*(*foobarbaz) $args;\n";
710         print TESTFILE "extern __typeof__ (&$fname) foobarbaz;\n";
711         close (TESTFILE);
712
713         compiletest ($fnamebase, "Test for type of function $fname",
714                      "Function \"$fname\" has incorrect type.", $res, 0);
715       }
716     } elsif (/^function *({([^}]*)}|([a-zA-Z0-9_]*)) [(][*]([a-zA-Z0-9_]*) ([(].*[)])/) {
717       my($rettype) = "$2$3";
718       my($fname) = "$4";
719       my($args) = "$5";
720       my($res) = $missing;
721
722       # Remember that this name is allowed.
723       push @allow, $fname;
724
725       # Generate a program to test for availability of this function.
726       open (TESTFILE, ">$fnamebase.c");
727       print TESTFILE "$prepend";
728       print TESTFILE "#include <$h>\n";
729       # print TESTFILE "#undef $fname\n";
730       print TESTFILE "$rettype (*(*foobarbaz) $args = $fname;\n";
731       close (TESTFILE);
732
733       $res = compiletest ($fnamebase, "Test availability of function $fname",
734                           "Function \"$fname\" is not available.", $res, 0);
735
736       # Generate a program to test for the type of this function.
737       open (TESTFILE, ">$fnamebase.c");
738       print TESTFILE "$prepend";
739       print TESTFILE "#include <$h>\n";
740       # print TESTFILE "#undef $fname\n";
741       print TESTFILE "extern $rettype (*(*foobarbaz) $args;\n";
742       print TESTFILE "extern __typeof__ (&$fname) foobarbaz;\n";
743       close (TESTFILE);
744
745       compiletest ($fnamebase, "Test for type of function $fname",
746                    "Function \"$fname\" has incorrect type.", $res, 0);
747     } elsif (/^optional-function *({([^}]*)}|([a-zA-Z0-9_]*)) ([a-zA-Z0-9_]*) ([(].*[)])/) {
748       my($rettype) = "$2$3";
749       my($fname) = "$4";
750       my($args) = "$5";
751       my($res) = $missing;
752
753       # Remember that this name is allowed.
754       push @allow, $fname;
755
756       # Generate a program to test for availability of this function.
757       open (TESTFILE, ">$fnamebase.c");
758       print TESTFILE "$prepend";
759       print TESTFILE "#include <$h>\n";
760       # print TESTFILE "#undef $fname\n";
761       print TESTFILE "$rettype (*foobarbaz) $args = $fname;\n";
762       close (TESTFILE);
763
764       $res = compiletest ($fnamebase, "Test availability of function $fname",
765                           "NOT AVAILABLE", $res, 1);
766
767       if ($res == 0 || $missing != 0) {
768         # Generate a program to test for the type of this function.
769         open (TESTFILE, ">$fnamebase.c");
770         print TESTFILE "$prepend";
771         print TESTFILE "#include <$h>\n";
772         # print TESTFILE "#undef $fname\n";
773         print TESTFILE "extern $rettype (*foobarbaz) $args;\n";
774         print TESTFILE "extern __typeof__ (&$fname) foobarbaz;\n";
775         close (TESTFILE);
776
777         compiletest ($fnamebase, "Test for type of function $fname",
778                      "Function \"$fname\" has incorrect type.", $res, 0);
779       }
780     } elsif (/^function *({([^}]*)}|([a-zA-Z0-9_]*)) ([a-zA-Z0-9_]*) ([(].*[)])/) {
781       my($rettype) = "$2$3";
782       my($fname) = "$4";
783       my($args) = "$5";
784       my($res) = $missing;
785
786       # Remember that this name is allowed.
787       push @allow, $fname;
788
789       # Generate a program to test for availability of this function.
790       open (TESTFILE, ">$fnamebase.c");
791       print TESTFILE "$prepend";
792       print TESTFILE "#include <$h>\n";
793       # print TESTFILE "#undef $fname\n";
794       print TESTFILE "$rettype (*foobarbaz) $args = $fname;\n";
795       close (TESTFILE);
796
797       $res = compiletest ($fnamebase, "Test availability of function $fname",
798                           "Function \"$fname\" is not available.", $res, 0);
799
800       # Generate a program to test for the type of this function.
801       open (TESTFILE, ">$fnamebase.c");
802       print TESTFILE "$prepend";
803       print TESTFILE "#include <$h>\n";
804       # print TESTFILE "#undef $fname\n";
805       print TESTFILE "extern $rettype (*foobarbaz) $args;\n";
806       print TESTFILE "extern __typeof__ (&$fname) foobarbaz;\n";
807       close (TESTFILE);
808
809       compiletest ($fnamebase, "Test for type of function $fname",
810                    "Function \"$fname\" has incorrect type.", $res, 0);
811     } elsif (/^variable *({([^}]*)}|([a-zA-Z0-9_]*)) ([a-zA-Z0-9_]*) *(.*)/) {
812       my($type) = "$2$3";
813       my($vname) = "$4";
814       my($rest) = "$5";
815       my($res) = $missing;
816
817       # Remember that this name is allowed.
818       push @allow, $vname;
819
820       # Generate a program to test for availability of this function.
821       open (TESTFILE, ">$fnamebase.c");
822       print TESTFILE "$prepend";
823       print TESTFILE "#include <$h>\n";
824       # print TESTFILE "#undef $fname\n";
825       print TESTFILE "typedef $type xyzzy$rest;\n";
826       print TESTFILE "$xyzzy *foobarbaz = &$vname;\n";
827       close (TESTFILE);
828
829       $res = compiletest ($fnamebase, "Test availability of variable $vname",
830                           "Variable \"$vname\" is not available.", $res, 0);
831
832       # Generate a program to test for the type of this function.
833       open (TESTFILE, ">$fnamebase.c");
834       print TESTFILE "$prepend";
835       print TESTFILE "#include <$h>\n";
836       # print TESTFILE "#undef $fname\n";
837       print TESTFILE "extern $type $vname$rest;\n";
838       close (TESTFILE);
839
840       compiletest ($fnamebase, "Test for type of variable $fname",
841                    "Variable \"$vname\" has incorrect type.", $res, 0);
842     } elsif (/^macro-function *({([^}]*)}|([a-zA-Z0-9_]*)) ([a-zA-Z0-9_]*) ([(].*[)])/) {
843       my($rettype) = "$2$3";
844       my($fname) = "$4";
845       my($args) = "$5";
846       my($res) = $missing;
847
848       # Remember that this name is allowed.
849       push @allow, $fname;
850
851       # Generate a program to test for availability of this function.
852       open (TESTFILE, ">$fnamebase.c");
853       print TESTFILE "$prepend";
854       print TESTFILE "#include <$h>\n";
855       print TESTFILE "#ifndef $fname\n";
856       print TESTFILE "$rettype (*foobarbaz) $args = $fname;\n";
857       print TESTFILE "#endif\n";
858       close (TESTFILE);
859
860       $res = compiletest ($fnamebase, "Test availability of function $fname",
861                           "Function \"$fname\" is not available.", $res, 0);
862
863       # Generate a program to test for the type of this function.
864       open (TESTFILE, ">$fnamebase.c");
865       print TESTFILE "$prepend";
866       print TESTFILE "#include <$h>\n";
867       print TESTFILE "#ifndef $fname\n";
868       print TESTFILE "extern $rettype (*foobarbaz) $args;\n";
869       print TESTFILE "extern __typeof__ (&$fname) foobarbaz;\n";
870       print TESTFILE "#endif\n";
871       close (TESTFILE);
872
873       compiletest ($fnamebase, "Test for type of function $fname",
874                    "Function \"$fname\" has incorrect type.", $res, 0);
875     } elsif (/^macro-str *([^    ]*) *(\".*\")/) {
876       # The above regex doesn't handle a \" in a string.
877       my($macro) = "$1";
878       my($string) = "$2";
879       my($res) = $missing;
880
881       # Remember that this name is allowed.
882       push @allow, $macro;
883
884       # Generate a program to test for availability of this macro.
885       open (TESTFILE, ">$fnamebase.c");
886       print TESTFILE "$prepend";
887       print TESTFILE "#include <$h>\n";
888       print TESTFILE "#ifndef $macro\n";
889       print TESTFILE "# error \"Macro $macro not defined\"\n";
890       print TESTFILE "#endif\n";
891       close (TESTFILE);
892
893       compiletest ($fnamebase, "Test availability of macro $macro",
894                    "Macro \"$macro\" is not available.", $missing, 0);
895
896       # Generate a program to test for the value of this macro.
897       open (TESTFILE, ">$fnamebase.c");
898       print TESTFILE "$prepend";
899       print TESTFILE "#include <$h>\n";
900       # We can't include <string.h> here.
901       print TESTFILE "extern int (strcmp)(const char *, const char *);\n";
902       print TESTFILE "int main (void) { return (strcmp) ($macro, $string) != 0;}\n";
903       close (TESTFILE);
904
905       $res = runtest ($fnamebase, "Testing for value of macro $macro",
906                       "Macro \"$macro\" has not the right value.", $res);
907     } elsif (/^optional-macro *([^      ]*)/) {
908       my($macro) = "$1";
909
910       # Remember that this name is allowed.
911       push @allow, $macro;
912
913       # Generate a program to test for availability of this macro.
914       open (TESTFILE, ">$fnamebase.c");
915       print TESTFILE "$prepend";
916       print TESTFILE "#include <$h>\n";
917       print TESTFILE "#ifndef $macro\n";
918       print TESTFILE "# error \"Macro $macro not defined\"\n";
919       print TESTFILE "#endif\n";
920       close (TESTFILE);
921
922       compiletest ($fnamebase, "Test availability of macro $macro",
923                    "NOT PRESENT", $missing, 1);
924     } elsif (/^macro *([a-zA-Z0-9_]*) *([>=<]+) ([A-Za-z0-9_]*)/) {
925       my($macro) = "$1";
926       my($op) = $2;
927       my($value) = $3;
928       my($res) = $missing;
929
930       # Remember that this name is allowed.
931       push @allow, $macro;
932
933       # Generate a program to test for availability of this macro.
934       open (TESTFILE, ">$fnamebase.c");
935       print TESTFILE "$prepend";
936       print TESTFILE "#include <$h>\n";
937       print TESTFILE "#ifndef $macro\n";
938       print TESTFILE "# error \"Macro $macro not defined\"\n";
939       print TESTFILE "#endif\n";
940       close (TESTFILE);
941
942       $res = compiletest ($fnamebase, "Test availability of macro $macro",
943                           "Macro \"$macro\" is not available.", $res, 0);
944
945       if ($value ne "") {
946         # Generate a program to test for the value of this constant.
947         open (TESTFILE, ">$fnamebase.c");
948         print TESTFILE "$prepend";
949         print TESTFILE "#include <$h>\n";
950         # Negate the value since 0 means ok
951         print TESTFILE "int main (void) { return !($macro $op $value); }\n";
952         close (TESTFILE);
953
954         $res = runtest ($fnamebase, "Testing for value of constant $macro",
955                         "Macro \"$macro\" has not the right value.", $res);
956       }
957     } elsif (/^macro *([^       ]*)/) {
958       my($macro) = "$1";
959
960       # Remember that this name is allowed.
961       push @allow, $macro;
962
963       # Generate a program to test for availability of this macro.
964       open (TESTFILE, ">$fnamebase.c");
965       print TESTFILE "$prepend";
966       print TESTFILE "#include <$h>\n";
967       print TESTFILE "#ifndef $macro\n";
968       print TESTFILE "# error \"Macro $macro not defined\"\n";
969       print TESTFILE "#endif\n";
970       close (TESTFILE);
971
972       compiletest ($fnamebase, "Test availability of macro $macro",
973                    "Macro \"$macro\" is not available.", $missing, 0);
974     } elsif (/^allow-header *(.*)/) {
975       my($pattern) = $1;
976       if ($seenheader{$pattern} != 1) {
977         push @allowheader, $pattern;
978         $seenheader{$pattern} = 1;
979       }
980       next control;
981     } elsif (/^allow *(.*)/) {
982       my($pattern) = $1;
983       push @allow, $pattern;
984       next control;
985     } else {
986       # printf ("line is `%s'\n", $_);
987       next control;
988     }
989
990     printf ("\n");
991   }
992   close (CONTROL);
993
994   # Read the data files for the header files which are allowed to be included.
995   while ($#allowheader >= 0) {
996     my($ah) = pop @allowheader;
997
998     open (ALLOW, "$CC -E -D$dialect - < data/$ah-data |");
999     acontrol: while (<ALLOW>) {
1000       next acontrol if (/^#/);
1001       next acontrol if (/^[     ]*$/);
1002
1003       if (/^element *({([^}]*)}|([^ ]*)) *({([^}]*)}|([^ ]*)) *([A-Za-z0-9_]*) *(.*)/) {
1004         push @allow, $7;
1005       } elsif (/^constant *([a-zA-Z0-9_]*) *([A-Za-z0-9_]*)?/) {
1006         push @allow, $1;
1007       } elsif (/^typed-constant *([a-zA-Z0-9_]*) *({([^}]*)}|([^ ]*)) *([A-Za-z0-9_]*)?/) {
1008         push @allow, 1;
1009       } elsif (/^type *({([^}]*)|([a-zA-Z0-9_]*))/) {
1010         my($type) = "$2$3";
1011
1012         # Remember that this name is allowed.
1013         if ($type =~ /^struct *(.*)/) {
1014           push @allow, $1;
1015         } elsif ($type =~ /^union *(.*)/) {
1016           push @allow, $1;
1017         } else {
1018           push @allow, $type;
1019         }
1020       } elsif (/^function *({([^}]*)}|([a-zA-Z0-9_]*)) [(][*]([a-zA-Z0-9_]*) ([(].*[)])/) {
1021         push @allow, $4;
1022       } elsif (/^function *({([^}]*)}|([a-zA-Z0-9_]*)) ([a-zA-Z0-9_]*) ([(].*[)])/) {
1023         push @allow, $4;
1024       } elsif (/^variable *({([^}]*)}|([a-zA-Z0-9_]*)) ([a-zA-Z0-9_]*)/) {
1025         push @allow, $4;
1026       } elsif (/^macro-function *({([^}]*)}|([a-zA-Z0-9_]*)) ([a-zA-Z0-9_]*) ([(].*[)])/) {
1027         push @allow, $4;
1028       } elsif (/^macro *([^     ]*)/) {
1029         push @allow, $1;
1030       } elsif (/^allow-header *(.*)/) {
1031         if ($seenheader{$1} != 1) {
1032           push @allowheader, $1;
1033           $seenheader{$1} = 1;
1034         }
1035       } elsif (/^allow *(.*)/) {
1036         push @allow, $1;
1037       }
1038     }
1039     close (ALLOW);
1040   }
1041
1042   # Now check the namespace.
1043   printf ("  Checking the namespace of \"%s\"... ", $h);
1044   if ($missing) {
1045     ++$skipped;
1046     printf ("SKIP\n");
1047   } else {
1048     checknamespace ($h, $fnamebase, @allow);
1049   }
1050
1051   printf ("\n\n");
1052 }
1053
1054 printf "-" x 76 . "\n";
1055 printf ("  Total number of tests   : %4d\n", $total);
1056
1057 printf ("  Number of known failures: %4d (", $known);
1058 $percent = ($known * 100) / $total;
1059 if ($known > 0 && $percent < 1.0) {
1060   printf (" <1%%)\n");
1061 } else {
1062   printf ("%3d%%)\n", $percent);
1063 }
1064
1065 printf ("  Number of failed tests  : %4d (", $errors);
1066 $percent = ($errors * 100) / $total;
1067 if ($errors > 0 && $percent < 1.0) {
1068   printf (" <1%%)\n");
1069 } else {
1070   printf ("%3d%%)\n", $percent);
1071 }
1072
1073 printf ("  Number of skipped tests : %4d (", $skipped);
1074 $percent = ($skipped * 100) / $total;
1075 if ($skipped > 0 && $percent < 1.0) {
1076   printf (" <1%%)\n");
1077 } else {
1078   printf ("%3d%%)\n", $percent);
1079 }
1080
1081 exit $errors != 0;