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