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. -fno-builtin '-D__attribute__(x)=' -ansi";
38 $CFLAGS{"POSIX"} = "-I. -fno-builtin '-D__attribute__(x)=' -D_POSIX_C_SOURCE=199912";
39 $CFLAGS{"XPG3"} = "-I. -fno-builtin '-D__attribute__(x)=' -D_XOPEN_SOURCE";
40 $CFLAGS{"XPG4"} = "-I. -fno-builtin '-D__attribute__(x)=' -D_XOPEN_SOURCE_EXTENDED";
41 $CFLAGS{"UNIX98"} = "-I. -fno-builtin '-D__attribute__(x)=' -D_XOPEN_SOURCE=500";
42 $CFLAGS{"XOPEN2K"} = "-I. -fno-builtin '-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     $errors{$token} = 1;
228   }
229 }
230
231
232 sub removetoken {
233   my($token) = @_;
234   my($idx);
235
236   return if ($token =~ /^[0-9_]/ || $iskeyword{$token});
237
238   if (exists $errors{$token}) {
239     undef $errors{$token};
240   }
241 }
242
243
244 sub checknamespace {
245   my($h, $fnamebase, @allow) = @_;
246
247   ++$total;
248
249   # Generate a program to get the contents of this header.
250   open (TESTFILE, ">$fnamebase.c");
251   print TESTFILE "#include <$h>\n";
252   close (TESTFILE);
253
254   undef %errors;
255   $nknown = 0;
256   open (CONTENT, "$CC $CFLAGS{$dialect} -E $fnamebase.c -P -Wp,-dN | sed -e '/^# [1-9]/d' -e '/^[[:space:]]*\$/d' |");
257   loop: while (<CONTENT>) {
258     chop;
259     if (/^#define (.*)/) {
260       newtoken ($1, @allow);
261     } elsif (/^#undef (.*)/) {
262       removetoken ($1);
263     } else {
264       # We have to tokenize the line.
265       my($str) = $_;
266       my($index) = 0;
267       my($len) = length ($str);
268
269       foreach $token (split(/[^a-zA-Z0-9_]/, $str)) {
270         if ($token ne "") {
271           newtoken ($token, @allow);
272         }
273       }
274     }
275   }
276   close (CONTENT);
277   unlink "$fnamebase.c";
278   $realerror = 0;
279   if ($#errors != 0) {
280     foreach $f (%errors) {
281       if ($errors{$f} == 1) {
282         if ($realerror == 0) {
283           printf ("FAIL\n    " . "-" x 72 . "\n");
284           $realerror = 1;
285           ++$errors;
286         }
287         printf ("    Namespace violation: \"%s\"\n", $f);
288       }
289     }
290     printf ("    " . "-" x 72 . "\n") if ($realerror != 0);
291   }
292
293   if ($realerror == 0) {
294     if ($nknown > 0) {
295       printf ("EXPECTED FAILURES\n");
296       ++$known;
297     } else {
298       printf ("OK\n");
299     }
300   }
301 }
302
303
304 while ($#headers >= 0) {
305   my($h) = pop (@headers);
306   my($hf) = $h;
307   $hf =~ s|/|-|;
308   my($fnamebase) = "$tmpdir/$hf-test";
309   my($missing);
310   my(@allow) = ();
311   my(@allowheader) = ();
312   my(%seenheader) = ();
313   my($prepend) = $mustprepend{$h};
314
315   printf ("Testing <$h>\n");
316   printf ("----------" . "-" x length ($h) . "\n");
317
318   # Generate a program to test for the availability of this header.
319   open (TESTFILE, ">$fnamebase.c");
320   print TESTFILE "$prepend";
321   print TESTFILE "#include <$h>\n";
322   close (TESTFILE);
323
324   $missing = compiletest ($fnamebase, "Checking whether <$h> is available",
325                           "Header <$h> not available", 0, 0);
326
327   printf ("\n");
328
329   open (CONTROL, "$CC -E -D$dialect - < data/$h-data |");
330   control: while (<CONTROL>) {
331     chop;
332     next control if (/^#/);
333     next control if (/^[        ]*$/);
334
335     if (/^element *({([^}]*)}|([^{ ]*)) *({([^}]*)}|([^{ ]*)) *([A-Za-z0-9_]*) *(.*)/) {
336       my($struct) = "$2$3";
337       my($type) = "$5$6";
338       my($member) = "$7";
339       my($rest) = "$8";
340       my($res) = $missing;
341
342       # Remember that this name is allowed.
343       push @allow, $member;
344
345       # Generate a program to test for the availability of this member.
346       open (TESTFILE, ">$fnamebase.c");
347       print TESTFILE "$prepend";
348       print TESTFILE "#include <$h>\n";
349       print TESTFILE "$struct a;\n";
350       print TESTFILE "$struct b;\n";
351       print TESTFILE "extern void xyzzy (__typeof__ (&b.$member), __typeof__ (&a.$member), unsigned);\n";
352       print TESTFILE "void foobarbaz (void) {\n";
353       print TESTFILE "  xyzzy (&a.$member, &b.$member, sizeof (a.$member));\n";
354       print TESTFILE "}\n";
355       close (TESTFILE);
356
357       $res = compiletest ($fnamebase, "Testing for member $member",
358                           "Member \"$member\" not available.", $res, 0);
359
360
361       # Test the types of the members.
362       open (TESTFILE, ">$fnamebase.c");
363       print TESTFILE "$prepend";
364       print TESTFILE "#include <$h>\n";
365       print TESTFILE "$struct a;\n";
366       print TESTFILE "extern $type b$rest;\n";
367       print TESTFILE "extern __typeof__ (a.$member) b;\n";
368       close (TESTFILE);
369
370       compiletest ($fnamebase, "Testing for type of member $member",
371                    "Member \"$member\" does not have the correct type.",
372                    $res, 0);
373     } elsif (/^optional-element *({([^}]*)}|([^{ ]*)) *({([^}]*)}|([^{ ]*)) *([A-Za-z0-9_]*) *(.*)/) {
374       my($struct) = "$2$3";
375       my($type) = "$5$6";
376       my($member) = "$7";
377       my($rest) = "$8";
378       my($res) = $missing;
379
380       # Remember that this name is allowed.
381       push @allow, $member;
382
383       # Generate a program to test for the availability of this member.
384       open (TESTFILE, ">$fnamebase.c");
385       print TESTFILE "$prepend";
386       print TESTFILE "#include <$h>\n";
387       print TESTFILE "$struct a;\n";
388       print TESTFILE "$struct b;\n";
389       print TESTFILE "extern void xyzzy (__typeof__ (&b.$member), __typeof__ (&a.$member), unsigned);\n";
390       print TESTFILE "void foobarbaz (void) {\n";
391       print TESTFILE "  xyzzy (&a.$member, &b.$member, sizeof (a.$member));\n";
392       print TESTFILE "}\n";
393       close (TESTFILE);
394
395       $res = compiletest ($fnamebase, "Testing for member $member",
396                           "NOT AVAILABLE.", $res, 1);
397
398       if ($res == 0 || $missing != 0) {
399         # Test the types of the members.
400         open (TESTFILE, ">$fnamebase.c");
401         print TESTFILE "$prepend";
402         print TESTFILE "#include <$h>\n";
403         print TESTFILE "$struct a;\n";
404         print TESTFILE "extern $type b$rest;\n";
405         print TESTFILE "extern __typeof__ (a.$member) b;\n";
406         close (TESTFILE);
407
408         compiletest ($fnamebase, "Testing for type of member $member",
409                      "Member \"$member\" does not have the correct type.",
410                      $res, 0);
411       }
412     } elsif (/^optional-constant *([a-zA-Z0-9_]*) ([>=<]+) ([A-Za-z0-9_]*)/) {
413       my($const) = $1;
414       my($op) = $2;
415       my($value) = $3;
416       my($res) = $missing;
417
418       # Remember that this name is allowed.
419       push @allow, $const;
420
421       # Generate a program to test for the availability of this constant.
422       open (TESTFILE, ">$fnamebase.c");
423       print TESTFILE "$prepend";
424       print TESTFILE "#include <$h>\n";
425       print TESTFILE "__typeof__ ($const) a = $const;\n";
426       close (TESTFILE);
427
428       $res = compiletest ($fnamebase, "Testing for constant $const",
429                           "NOT PRESENT", $res, 1);
430
431       if ($value ne "" && $res == 0) {
432         # Generate a program to test for the value of this constant.
433         open (TESTFILE, ">$fnamebase.c");
434         print TESTFILE "$prepend";
435         print TESTFILE "#include <$h>\n";
436         # Negate the value since 0 means ok
437         print TESTFILE "int main (void) { return !($const $op $value); }\n";
438         close (TESTFILE);
439
440         $res = runtest ($fnamebase, "Testing for value of constant $const",
441                         "Constant \"$const\" has not the right value.", $res);
442       }
443     } elsif (/^constant *([a-zA-Z0-9_]*) *([>=<]+) ([A-Za-z0-9_]*)/) {
444       my($const) = $1;
445       my($op) = $2;
446       my($value) = $3;
447       my($res) = $missing;
448
449       # Remember that this name is allowed.
450       push @allow, $const;
451
452       # Generate a program to test for the availability of this constant.
453       open (TESTFILE, ">$fnamebase.c");
454       print TESTFILE "$prepend";
455       print TESTFILE "#include <$h>\n";
456       print TESTFILE "__typeof__ ($const) a = $const;\n";
457       close (TESTFILE);
458
459       $res = compiletest ($fnamebase, "Testing for constant $const",
460                           "Constant \"$const\" not available.", $res, 0);
461
462       if ($value ne "") {
463         # Generate a program to test for the value of this constant.
464         open (TESTFILE, ">$fnamebase.c");
465         print TESTFILE "$prepend";
466         print TESTFILE "#include <$h>\n";
467         # Negate the value since 0 means ok
468         print TESTFILE "int main (void) { return !($const $op $value); }\n";
469         close (TESTFILE);
470
471         $res = runtest ($fnamebase, "Testing for value of constant $const",
472                         "Constant \"$const\" has not the right value.", $res);
473       }
474     } elsif (/^typed-constant *([a-zA-Z0-9_]*) *({([^}]*)}|([^ ]*)) *([A-Za-z0-9_]*)?/) {
475       my($const) = $1;
476       my($type) = "$3$4";
477       my($value) = $5;
478       my($res) = $missing;
479
480       # Remember that this name is allowed.
481       push @allow, $const;
482
483       # Generate a program to test for the availability of this constant.
484       open (TESTFILE, ">$fnamebase.c");
485       print TESTFILE "$prepend";
486       print TESTFILE "#include <$h>\n";
487       print TESTFILE "__typeof__ ($const) a = $const;\n";
488       close (TESTFILE);
489
490       $res = compiletest ($fnamebase, "Testing for constant $const",
491                           "Constant \"$const\" not available.", $res, 0);
492
493       # Test the types of the members.
494       open (TESTFILE, ">$fnamebase.c");
495       print TESTFILE "$prepend";
496       print TESTFILE "#include <$h>\n";
497       print TESTFILE "__typeof__ (($type) 0) a;\n";
498       print TESTFILE "extern __typeof__ ($const) a;\n";
499       close (TESTFILE);
500
501       compiletest ($fnamebase, "Testing for type of constant $const",
502                    "Constant \"$const\" does not have the correct type.",
503                    $res, 0);
504
505       if ($value ne "") {
506         # Generate a program to test for the value of this constant.
507         open (TESTFILE, ">$fnamebase.c");
508         print TESTFILE "$prepend";
509         print TESTFILE "#include <$h>\n";
510         print TESTFILE "int main (void) { return $const != $value; }\n";
511         close (TESTFILE);
512
513         $res = runtest ($fnamebase, "Testing for value of constant $const",
514                         "Constant \"$const\" has not the right value.", $res);
515       }
516     } elsif (/^optional-constant *([a-zA-Z0-9_]*) *([A-Za-z0-9_]*)?/) {
517       my($const) = $1;
518       my($value) = $2;
519       my($res) = $missing;
520
521       # Remember that this name is allowed.
522       push @allow, $const;
523
524       # Generate a program to test for the availability of this constant.
525       open (TESTFILE, ">$fnamebase.c");
526       print TESTFILE "$prepend";
527       print TESTFILE "#include <$h>\n";
528       print TESTFILE "__typeof__ ($const) a = $const;\n";
529       close (TESTFILE);
530
531       $res = compiletest ($fnamebase, "Testing for constant $const",
532                           "NOT PRESENT", $res, 1);
533
534       if ($value ne "" && $res == 0) {
535         # Generate a program to test for the value of this constant.
536         open (TESTFILE, ">$fnamebase.c");
537         print TESTFILE "$prepend";
538         print TESTFILE "#include <$h>\n";
539         print TESTFILE "int main (void) { return $const != $value; }\n";
540         close (TESTFILE);
541
542         $res = runtest ($fnamebase, "Testing for value of constant $const",
543                         "Constant \"$const\" has not the right value.", $res);
544       }
545     } elsif (/^constant *([a-zA-Z0-9_]*) *([A-Za-z0-9_]*)?/) {
546       my($const) = $1;
547       my($value) = $2;
548       my($res) = $missing;
549
550       # Remember that this name is allowed.
551       push @allow, $const;
552
553       # Generate a program to test for the availability of this constant.
554       open (TESTFILE, ">$fnamebase.c");
555       print TESTFILE "$prepend";
556       print TESTFILE "#include <$h>\n";
557       print TESTFILE "__typeof__ ($const) a = $const;\n";
558       close (TESTFILE);
559
560       $res = compiletest ($fnamebase, "Testing for constant $const",
561                           "Constant \"$const\" not available.", $res, 0);
562
563       if ($value ne "") {
564         # Generate a program to test for the value of this constant.
565         open (TESTFILE, ">$fnamebase.c");
566         print TESTFILE "$prepend";
567         print TESTFILE "#include <$h>\n";
568         print TESTFILE "int main (void) { return $const != $value; }\n";
569         close (TESTFILE);
570
571         $res = runtest ($fnamebase, "Testing for value of constant $const",
572                         "Constant \"$const\" has not the right value.", $res);
573       }
574     } elsif (/^symbol *([a-zA-Z0-9_]*) *([A-Za-z0-9_]*)?/) {
575       my($symbol) = $1;
576       my($value) = $2;
577       my($res) = $missing;
578
579       # Remember that this name is allowed.
580       push @allow, $symbol;
581
582       # Generate a program to test for the availability of this constant.
583       open (TESTFILE, ">$fnamebase.c");
584       print TESTFILE "$prepend";
585       print TESTFILE "#include <$h>\n";
586       print TESTFILE "void foobarbaz (void) {\n";
587       print TESTFILE "__typeof__ ($symbol) a = $symbol;\n";
588       print TESTFILE "}\n";
589       close (TESTFILE);
590
591       $res = compiletest ($fnamebase, "Testing for symbol $symbol",
592                           "Symbol \"$symbol\" not available.", $res, 0);
593
594       if ($value ne "") {
595         # Generate a program to test for the value of this constant.
596         open (TESTFILE, ">$fnamebase.c");
597         print TESTFILE "$prepend";
598         print TESTFILE "#include <$h>\n";
599         print TESTFILE "int main (void) { return $symbol != $value; }\n";
600         close (TESTFILE);
601
602         $res = runtest ($fnamebase, "Testing for value of symbol $symbol",
603                         "Symbol \"$symbol\" has not the right value.", $res);
604       }
605     } elsif (/^typed-constant *([a-zA-Z0-9_]*) *({([^}]*)}|([^ ]*)) *([A-Za-z0-9_]*)?/) {
606       my($const) = $1;
607       my($type) = "$3$4";
608       my($value) = $5;
609       my($res) = $missing;
610
611       # Remember that this name is allowed.
612       push @allow, $const;
613
614       # Generate a program to test for the availability of this constant.
615       open (TESTFILE, ">$fnamebase.c");
616       print TESTFILE "$prepend";
617       print TESTFILE "#include <$h>\n";
618       print TESTFILE "__typeof__ ($const) a = $const;\n";
619       close (TESTFILE);
620
621       $res = compiletest ($fnamebase, "Testing for constant $const",
622                           "Constant \"$const\" not available.", $res, 0);
623
624       # Test the types of the members.
625       open (TESTFILE, ">$fnamebase.c");
626       print TESTFILE "$prepend";
627       print TESTFILE "#include <$h>\n";
628       print TESTFILE "__typeof__ (($type) 0) a;\n";
629       print TESTFILE "extern __typeof__ ($const) a;\n";
630       close (TESTFILE);
631
632       compiletest ($fnamebase, "Testing for type of constant $const",
633                    "Constant \"$const\" does not have the correct type.",
634                    $res, 0);
635
636       if ($value ne "") {
637         # Generate a program to test for the value of this constant.
638         open (TESTFILE, ">$fnamebase.c");
639         print TESTFILE "$prepend";
640         print TESTFILE "#include <$h>\n";
641         print TESTFILE "int main (void) { return $const != $value; }\n";
642         close (TESTFILE);
643
644         $res = runtest ($fnamebase, "Testing for value of constant $const",
645                         "Constant \"$const\" has not the right value.", $res);
646       }
647     } elsif (/^optional-type *({([^}]*)|([a-zA-Z0-9_]*))/) {
648       my($type) = "$2$3";
649       my($maybe_opaque) = 0;
650
651       # Remember that this name is allowed.
652       if ($type =~ /^struct *(.*)/) {
653         push @allow, $1;
654       } elsif ($type =~ /^union *(.*)/) {
655         push @allow, $1;
656       } else {
657         push @allow, $type;
658         $maybe_opaque = 1;
659       }
660
661       # Remember that this name is allowed.
662       push @allow, $type;
663
664       # Generate a program to test for the availability of this constant.
665       open (TESTFILE, ">$fnamebase.c");
666       print TESTFILE "$prepend";
667       print TESTFILE "#include <$h>\n";
668       if ($maybe_opaque == 1) {
669         print TESTFILE "$type *a;\n";
670       } else {
671         print TESTFILE "$type a;\n";
672       }
673       close (TESTFILE);
674
675       compiletest ($fnamebase, "Testing for type $type",
676                    "NOT AVAILABLE", $missing, 1);
677     } elsif (/^type *({([^}]*)|([a-zA-Z0-9_]*))/) {
678       my($type) = "$2$3";
679       my($maybe_opaque) = 0;
680
681       # Remember that this name is allowed.
682       if ($type =~ /^struct *(.*)/) {
683         push @allow, $1;
684       } elsif ($type =~ /^union *(.*)/) {
685         push @allow, $1;
686       } else {
687         push @allow, $type;
688         $maybe_opaque = 1;
689       }
690
691       # Remember that this name is allowed.
692       push @allow, $type;
693
694       # Generate a program to test for the availability of this type.
695       open (TESTFILE, ">$fnamebase.c");
696       print TESTFILE "$prepend";
697       print TESTFILE "#include <$h>\n";
698       if ($maybe_opaque == 1) {
699         print TESTFILE "$type *a;\n";
700       } else {
701         print TESTFILE "$type a;\n";
702       }
703       close (TESTFILE);
704
705       compiletest ($fnamebase, "Testing for type $type",
706                    "Type \"$type\" not available.", $missing, 0);
707     } elsif (/^optional-function *({([^}]*)}|([a-zA-Z0-9_]*)) [(][*]([a-zA-Z0-9_]*) ([(].*[)])/) {
708       my($rettype) = "$2$3";
709       my($fname) = "$4";
710       my($args) = "$5";
711       my($res) = $missing;
712
713       # Remember that this name is allowed.
714       push @allow, $fname;
715
716       # Generate a program to test for availability of this function.
717       open (TESTFILE, ">$fnamebase.c");
718       print TESTFILE "$prepend";
719       print TESTFILE "#include <$h>\n";
720       # print TESTFILE "#undef $fname\n";
721       print TESTFILE "$rettype (*(*foobarbaz) $args = $fname;\n";
722       close (TESTFILE);
723
724       $res = compiletest ($fnamebase, "Test availability of function $fname",
725                           "NOT AVAILABLE", $res, 1);
726
727       if ($res == 0 || $missing == 1) {
728         # Generate a program to test for the type of this function.
729         open (TESTFILE, ">$fnamebase.c");
730         print TESTFILE "$prepend";
731         print TESTFILE "#include <$h>\n";
732         # print TESTFILE "#undef $fname\n";
733         print TESTFILE "extern $rettype (*(*foobarbaz) $args;\n";
734         print TESTFILE "extern __typeof__ (&$fname) foobarbaz;\n";
735         close (TESTFILE);
736
737         compiletest ($fnamebase, "Test for type of function $fname",
738                      "Function \"$fname\" has incorrect type.", $res, 0);
739       }
740     } elsif (/^function *({([^}]*)}|([a-zA-Z0-9_]*)) [(][*]([a-zA-Z0-9_]*) ([(].*[)])/) {
741       my($rettype) = "$2$3";
742       my($fname) = "$4";
743       my($args) = "$5";
744       my($res) = $missing;
745
746       # Remember that this name is allowed.
747       push @allow, $fname;
748
749       # Generate a program to test for availability of this function.
750       open (TESTFILE, ">$fnamebase.c");
751       print TESTFILE "$prepend";
752       print TESTFILE "#include <$h>\n";
753       # print TESTFILE "#undef $fname\n";
754       print TESTFILE "$rettype (*(*foobarbaz) $args = $fname;\n";
755       close (TESTFILE);
756
757       $res = compiletest ($fnamebase, "Test availability of function $fname",
758                           "Function \"$fname\" is not available.", $res, 0);
759
760       # Generate a program to test for the type of this function.
761       open (TESTFILE, ">$fnamebase.c");
762       print TESTFILE "$prepend";
763       print TESTFILE "#include <$h>\n";
764       # print TESTFILE "#undef $fname\n";
765       print TESTFILE "extern $rettype (*(*foobarbaz) $args;\n";
766       print TESTFILE "extern __typeof__ (&$fname) foobarbaz;\n";
767       close (TESTFILE);
768
769       compiletest ($fnamebase, "Test for type of function $fname",
770                    "Function \"$fname\" has incorrect type.", $res, 0);
771     } elsif (/^optional-function *({([^}]*)}|([a-zA-Z0-9_]*)) ([a-zA-Z0-9_]*) ([(].*[)])/) {
772       my($rettype) = "$2$3";
773       my($fname) = "$4";
774       my($args) = "$5";
775       my($res) = $missing;
776
777       # Remember that this name is allowed.
778       push @allow, $fname;
779
780       # Generate a program to test for availability of this function.
781       open (TESTFILE, ">$fnamebase.c");
782       print TESTFILE "$prepend";
783       print TESTFILE "#include <$h>\n";
784       # print TESTFILE "#undef $fname\n";
785       print TESTFILE "$rettype (*foobarbaz) $args = $fname;\n";
786       close (TESTFILE);
787
788       $res = compiletest ($fnamebase, "Test availability of function $fname",
789                           "NOT AVAILABLE", $res, 1);
790
791       if ($res == 0 || $missing != 0) {
792         # Generate a program to test for the type of this function.
793         open (TESTFILE, ">$fnamebase.c");
794         print TESTFILE "$prepend";
795         print TESTFILE "#include <$h>\n";
796         # print TESTFILE "#undef $fname\n";
797         print TESTFILE "extern $rettype (*foobarbaz) $args;\n";
798         print TESTFILE "extern __typeof__ (&$fname) foobarbaz;\n";
799         close (TESTFILE);
800
801         compiletest ($fnamebase, "Test for type of function $fname",
802                      "Function \"$fname\" has incorrect type.", $res, 0);
803       }
804     } elsif (/^function *({([^}]*)}|([a-zA-Z0-9_]*)) ([a-zA-Z0-9_]*) ([(].*[)])/) {
805       my($rettype) = "$2$3";
806       my($fname) = "$4";
807       my($args) = "$5";
808       my($res) = $missing;
809
810       # Remember that this name is allowed.
811       push @allow, $fname;
812
813       # Generate a program to test for availability of this function.
814       open (TESTFILE, ">$fnamebase.c");
815       print TESTFILE "$prepend";
816       print TESTFILE "#include <$h>\n";
817       # print TESTFILE "#undef $fname\n";
818       print TESTFILE "$rettype (*foobarbaz) $args = $fname;\n";
819       close (TESTFILE);
820
821       $res = compiletest ($fnamebase, "Test availability of function $fname",
822                           "Function \"$fname\" is not available.", $res, 0);
823
824       # Generate a program to test for the type of this function.
825       open (TESTFILE, ">$fnamebase.c");
826       print TESTFILE "$prepend";
827       print TESTFILE "#include <$h>\n";
828       # print TESTFILE "#undef $fname\n";
829       print TESTFILE "extern $rettype (*foobarbaz) $args;\n";
830       print TESTFILE "extern __typeof__ (&$fname) foobarbaz;\n";
831       close (TESTFILE);
832
833       compiletest ($fnamebase, "Test for type of function $fname",
834                    "Function \"$fname\" has incorrect type.", $res, 0);
835     } elsif (/^variable *({([^}]*)}|([a-zA-Z0-9_]*)) ([a-zA-Z0-9_]*) *(.*)/) {
836       my($type) = "$2$3";
837       my($vname) = "$4";
838       my($rest) = "$5";
839       my($res) = $missing;
840
841       # Remember that this name is allowed.
842       push @allow, $vname;
843
844       # Generate a program to test for availability of this function.
845       open (TESTFILE, ">$fnamebase.c");
846       print TESTFILE "$prepend";
847       print TESTFILE "#include <$h>\n";
848       # print TESTFILE "#undef $fname\n";
849       print TESTFILE "typedef $type xyzzy$rest;\n";
850       print TESTFILE "$xyzzy *foobarbaz = &$vname;\n";
851       close (TESTFILE);
852
853       $res = compiletest ($fnamebase, "Test availability of variable $vname",
854                           "Variable \"$vname\" is not available.", $res, 0);
855
856       # Generate a program to test for the type of this function.
857       open (TESTFILE, ">$fnamebase.c");
858       print TESTFILE "$prepend";
859       print TESTFILE "#include <$h>\n";
860       # print TESTFILE "#undef $fname\n";
861       print TESTFILE "extern $type $vname$rest;\n";
862       close (TESTFILE);
863
864       compiletest ($fnamebase, "Test for type of variable $fname",
865                    "Variable \"$vname\" has incorrect type.", $res, 0);
866     } elsif (/^macro-function *({([^}]*)}|([a-zA-Z0-9_]*)) ([a-zA-Z0-9_]*) ([(].*[)])/) {
867       my($rettype) = "$2$3";
868       my($fname) = "$4";
869       my($args) = "$5";
870       my($res) = $missing;
871
872       # Remember that this name is allowed.
873       push @allow, $fname;
874
875       # Generate a program to test for availability of this function.
876       open (TESTFILE, ">$fnamebase.c");
877       print TESTFILE "$prepend";
878       print TESTFILE "#include <$h>\n";
879       print TESTFILE "#ifndef $fname\n";
880       print TESTFILE "$rettype (*foobarbaz) $args = $fname;\n";
881       print TESTFILE "#endif\n";
882       close (TESTFILE);
883
884       $res = compiletest ($fnamebase, "Test availability of function $fname",
885                           "Function \"$fname\" is not available.", $res, 0);
886
887       # Generate a program to test for the type of this function.
888       open (TESTFILE, ">$fnamebase.c");
889       print TESTFILE "$prepend";
890       print TESTFILE "#include <$h>\n";
891       print TESTFILE "#ifndef $fname\n";
892       print TESTFILE "extern $rettype (*foobarbaz) $args;\n";
893       print TESTFILE "extern __typeof__ (&$fname) foobarbaz;\n";
894       print TESTFILE "#endif\n";
895       close (TESTFILE);
896
897       compiletest ($fnamebase, "Test for type of function $fname",
898                    "Function \"$fname\" has incorrect type.", $res, 0);
899     } elsif (/^macro-str *([^    ]*) *(\".*\")/) {
900       # The above regex doesn't handle a \" in a string.
901       my($macro) = "$1";
902       my($string) = "$2";
903       my($res) = $missing;
904
905       # Remember that this name is allowed.
906       push @allow, $macro;
907
908       # Generate a program to test for availability of this macro.
909       open (TESTFILE, ">$fnamebase.c");
910       print TESTFILE "$prepend";
911       print TESTFILE "#include <$h>\n";
912       print TESTFILE "#ifndef $macro\n";
913       print TESTFILE "# error \"Macro $macro not defined\"\n";
914       print TESTFILE "#endif\n";
915       close (TESTFILE);
916
917       compiletest ($fnamebase, "Test availability of macro $macro",
918                    "Macro \"$macro\" is not available.", $missing, 0);
919
920       # Generate a program to test for the value of this macro.
921       open (TESTFILE, ">$fnamebase.c");
922       print TESTFILE "$prepend";
923       print TESTFILE "#include <$h>\n";
924       # We can't include <string.h> here.
925       print TESTFILE "extern int (strcmp)(const char *, const char *);\n";
926       print TESTFILE "int main (void) { return (strcmp) ($macro, $string) != 0;}\n";
927       close (TESTFILE);
928
929       $res = runtest ($fnamebase, "Testing for value of macro $macro",
930                       "Macro \"$macro\" has not the right value.", $res);
931     } elsif (/^optional-macro *([^      ]*)/) {
932       my($macro) = "$1";
933
934       # Remember that this name is allowed.
935       push @allow, $macro;
936
937       # Generate a program to test for availability of this macro.
938       open (TESTFILE, ">$fnamebase.c");
939       print TESTFILE "$prepend";
940       print TESTFILE "#include <$h>\n";
941       print TESTFILE "#ifndef $macro\n";
942       print TESTFILE "# error \"Macro $macro not defined\"\n";
943       print TESTFILE "#endif\n";
944       close (TESTFILE);
945
946       compiletest ($fnamebase, "Test availability of macro $macro",
947                    "NOT PRESENT", $missing, 1);
948     } elsif (/^macro *([a-zA-Z0-9_]*) *([>=<]+) ([A-Za-z0-9_]*)/) {
949       my($macro) = "$1";
950       my($op) = $2;
951       my($value) = $3;
952       my($res) = $missing;
953
954       # Remember that this name is allowed.
955       push @allow, $macro;
956
957       # Generate a program to test for availability of this macro.
958       open (TESTFILE, ">$fnamebase.c");
959       print TESTFILE "$prepend";
960       print TESTFILE "#include <$h>\n";
961       print TESTFILE "#ifndef $macro\n";
962       print TESTFILE "# error \"Macro $macro not defined\"\n";
963       print TESTFILE "#endif\n";
964       close (TESTFILE);
965
966       $res = compiletest ($fnamebase, "Test availability of macro $macro",
967                           "Macro \"$macro\" is not available.", $res, 0);
968
969       if ($value ne "") {
970         # Generate a program to test for the value of this constant.
971         open (TESTFILE, ">$fnamebase.c");
972         print TESTFILE "$prepend";
973         print TESTFILE "#include <$h>\n";
974         # Negate the value since 0 means ok
975         print TESTFILE "int main (void) { return !($macro $op $value); }\n";
976         close (TESTFILE);
977
978         $res = runtest ($fnamebase, "Testing for value of constant $macro",
979                         "Macro \"$macro\" has not the right value.", $res);
980       }
981     } elsif (/^macro *([^       ]*)/) {
982       my($macro) = "$1";
983
984       # Remember that this name is allowed.
985       push @allow, $macro;
986
987       # Generate a program to test for availability of this macro.
988       open (TESTFILE, ">$fnamebase.c");
989       print TESTFILE "$prepend";
990       print TESTFILE "#include <$h>\n";
991       print TESTFILE "#ifndef $macro\n";
992       print TESTFILE "# error \"Macro $macro not defined\"\n";
993       print TESTFILE "#endif\n";
994       close (TESTFILE);
995
996       compiletest ($fnamebase, "Test availability of macro $macro",
997                    "Macro \"$macro\" is not available.", $missing, 0);
998     } elsif (/^allow-header *(.*)/) {
999       my($pattern) = $1;
1000       if ($seenheader{$pattern} != 1) {
1001         push @allowheader, $pattern;
1002         $seenheader{$pattern} = 1;
1003       }
1004       next control;
1005     } elsif (/^allow *(.*)/) {
1006       my($pattern) = $1;
1007       push @allow, $pattern;
1008       next control;
1009     } else {
1010       # printf ("line is `%s'\n", $_);
1011       next control;
1012     }
1013
1014     printf ("\n");
1015   }
1016   close (CONTROL);
1017
1018   # Read the data files for the header files which are allowed to be included.
1019   while ($#allowheader >= 0) {
1020     my($ah) = pop @allowheader;
1021
1022     open (ALLOW, "$CC -E -D$dialect - < data/$ah-data |");
1023     acontrol: while (<ALLOW>) {
1024       next acontrol if (/^#/);
1025       next acontrol if (/^[     ]*$/);
1026
1027       if (/^element *({([^}]*)}|([^ ]*)) *({([^}]*)}|([^ ]*)) *([A-Za-z0-9_]*) *(.*)/) {
1028         push @allow, $7;
1029       } elsif (/^constant *([a-zA-Z0-9_]*) *([A-Za-z0-9_]*)?/) {
1030         push @allow, $1;
1031       } elsif (/^typed-constant *([a-zA-Z0-9_]*) *({([^}]*)}|([^ ]*)) *([A-Za-z0-9_]*)?/) {
1032         push @allow, 1;
1033       } elsif (/^type *({([^}]*)|([a-zA-Z0-9_]*))/) {
1034         my($type) = "$2$3";
1035
1036         # Remember that this name is allowed.
1037         if ($type =~ /^struct *(.*)/) {
1038           push @allow, $1;
1039         } elsif ($type =~ /^union *(.*)/) {
1040           push @allow, $1;
1041         } else {
1042           push @allow, $type;
1043         }
1044       } elsif (/^function *({([^}]*)}|([a-zA-Z0-9_]*)) [(][*]([a-zA-Z0-9_]*) ([(].*[)])/) {
1045         push @allow, $4;
1046       } elsif (/^function *({([^}]*)}|([a-zA-Z0-9_]*)) ([a-zA-Z0-9_]*) ([(].*[)])/) {
1047         push @allow, $4;
1048       } elsif (/^variable *({([^}]*)}|([a-zA-Z0-9_]*)) ([a-zA-Z0-9_]*)/) {
1049         push @allow, $4;
1050       } elsif (/^macro-function *({([^}]*)}|([a-zA-Z0-9_]*)) ([a-zA-Z0-9_]*) ([(].*[)])/) {
1051         push @allow, $4;
1052       } elsif (/^macro *([^     ]*)/) {
1053         push @allow, $1;
1054       } elsif (/^allow-header *(.*)/) {
1055         if ($seenheader{$1} != 1) {
1056           push @allowheader, $1;
1057           $seenheader{$1} = 1;
1058         }
1059       } elsif (/^allow *(.*)/) {
1060         push @allow, $1;
1061       }
1062     }
1063     close (ALLOW);
1064   }
1065
1066   # Now check the namespace.
1067   printf ("  Checking the namespace of \"%s\"... ", $h);
1068   if ($missing) {
1069     ++$skipped;
1070     printf ("SKIP\n");
1071   } else {
1072     checknamespace ($h, $fnamebase, @allow);
1073   }
1074
1075   printf ("\n\n");
1076 }
1077
1078 printf "-" x 76 . "\n";
1079 printf ("  Total number of tests   : %4d\n", $total);
1080
1081 printf ("  Number of known failures: %4d (", $known);
1082 $percent = ($known * 100) / $total;
1083 if ($known > 0 && $percent < 1.0) {
1084   printf (" <1%%)\n");
1085 } else {
1086   printf ("%3d%%)\n", $percent);
1087 }
1088
1089 printf ("  Number of failed tests  : %4d (", $errors);
1090 $percent = ($errors * 100) / $total;
1091 if ($errors > 0 && $percent < 1.0) {
1092   printf (" <1%%)\n");
1093 } else {
1094   printf ("%3d%%)\n", $percent);
1095 }
1096
1097 printf ("  Number of skipped tests : %4d (", $skipped);
1098 $percent = ($skipped * 100) / $total;
1099 if ($skipped > 0 && $percent < 1.0) {
1100   printf (" <1%%)\n");
1101 } else {
1102   printf ("%3d%%)\n", $percent);
1103 }
1104
1105 exit $errors != 0;