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