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