9424203ec977463570e69a1326f35e7d2f2d5425
[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", "stddef.h", "stdarg.h",
21               "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         $printlog = 1;
185         unlink "$fnamebase.out";
186         rename "$fnamebase.out2", "$fnamebase.out";
187       }
188     }
189     if ($printlog != 0) {
190       printf ("    " . "-" x 71 . "\n");
191       open (MESSAGE, "< $fnamebase.out");
192       while (<MESSAGE>) {
193         printf ("    %s", $_);
194       }
195       close (MESSAGE);
196       printf ("    " . "-" x 71 . "\n");
197     }
198   }
199   unlink "$fnamebase";
200   unlink "$fnamebase.c";
201   unlink "$fnamebase.o";
202   unlink "$fnamebase.out";
203   unlink "$fnamebase.out2";
204
205   $result;
206 }
207
208
209 sub newtoken {
210   my($token, @allow) = @_;
211   my($idx);
212
213   return if ($token =~ /^[0-9_]/ || $iskeyword{$token});
214
215   for ($idx = 0; $idx <= $#allow; ++$idx) {
216     return if (poorfnmatch ($allow[$idx], $token));
217   }
218
219   if ($isknown{$token}) {
220     ++$nknown;
221   } else {
222     ++$nerrors;
223     if ($nerrors == 1) {
224       printf ("FAIL\n    " . "-" x 72 . "\n");
225     }
226     printf ("    Namespace violation: \"%s\"\n", $token);
227   }
228 }
229
230
231 sub checknamespace {
232   my($h, $fnamebase, @allow) = @_;
233
234   ++$total;
235
236   # Generate a program to get the contents of this header.
237   open (TESTFILE, ">$fnamebase.c");
238   print TESTFILE "#include <$h>\n";
239   close (TESTFILE);
240
241   $nerrors = 0;
242   $nknown = 0;
243   open (CONTENT, "$CC $CFLAGS{$dialect} -E $fnamebase.c -P -Wp,-dN | sed -e '/^# [1-9]/d' -e '/^[[:space:]]*\$/d' |");
244   loop: while (<CONTENT>) {
245     next loop if (/^#undef /);
246     chop;
247     if (/^#define (.*)/) {
248       newtoken ($1, @allow);
249     } else {
250       # We have to tokenize the line.
251       my($str) = $_;
252       my($index) = 0;
253       my($len) = length ($str);
254
255       foreach $token (split(/[^a-zA-Z0-9_]/, $str)) {
256         if ($token ne "") {
257           newtoken ($token, @allow);
258         }
259       }
260     }
261   }
262   close (CONTENT);
263   unlink "$fnamebase.c";
264   if ($nerrors != 0) {
265     printf ("    " . "-" x 72 . "\n");
266     ++$errors;
267   } elsif ($nknown > 0) {
268     printf ("EXPECTED FAILURES\n");
269     ++$known;
270   } else {
271     printf ("OK\n");
272   }
273 }
274
275
276 while ($#headers >= 0) {
277   my($h) = pop (@headers);
278   my($hf) = $h;
279   $hf =~ s|/|-|;
280   my($fnamebase) = "$tmpdir/$hf-test";
281   my($missing);
282   my(@allow) = ();
283   my(@allowheader) = ();
284   my(%seenheader) = ();
285   my($prepend) = $mustprepend{$h};
286
287   printf ("Testing <$h>\n");
288   printf ("----------" . "-" x length ($h) . "\n");
289
290   # Generate a program to test for the availability of this header.
291   open (TESTFILE, ">$fnamebase.c");
292   print TESTFILE "$prepend";
293   print TESTFILE "#include <$h>\n";
294   close (TESTFILE);
295
296   $missing = compiletest ($fnamebase, "Checking whether <$h> is available",
297                           "Header <$h> not available", 0, 0);
298
299   printf ("\n");
300
301   open (CONTROL, "$CC -E -D$dialect - < data/$h-data |");
302   control: while (<CONTROL>) {
303     chop;
304     next control if (/^#/);
305     next control if (/^[        ]*$/);
306
307     if (/^element *({([^}]*)}|([^{ ]*)) *({([^}]*)}|([^{ ]*)) *([A-Za-z0-9_]*) *(.*)/) {
308       my($struct) = "$2$3";
309       my($type) = "$5$6";
310       my($member) = "$7";
311       my($rest) = "$8";
312       my($res) = $missing;
313
314       # Remember that this name is allowed.
315       push @allow, $member;
316
317       # Generate a program to test for the availability of this member.
318       open (TESTFILE, ">$fnamebase.c");
319       print TESTFILE "$prepend";
320       print TESTFILE "#include <$h>\n";
321       print TESTFILE "$struct a;\n";
322       print TESTFILE "$struct b;\n";
323       print TESTFILE "extern void xyzzy (__typeof__ (&b.$member), __typeof__ (&a.$member), unsigned);\n";
324       print TESTFILE "void foobarbaz (void) {\n";
325       print TESTFILE "  xyzzy (&a.$member, &b.$member, sizeof (a.$member));\n";
326       print TESTFILE "}\n";
327       close (TESTFILE);
328
329       $res = compiletest ($fnamebase, "Testing for member $member",
330                           "Member \"$member\" not available.", $res, 0);
331
332
333       # Test the types of the members.
334       open (TESTFILE, ">$fnamebase.c");
335       print TESTFILE "$prepend";
336       print TESTFILE "#include <$h>\n";
337       print TESTFILE "$struct a;\n";
338       print TESTFILE "extern $type b$rest;\n";
339       print TESTFILE "extern __typeof__ (a.$member) b;\n";
340       close (TESTFILE);
341
342       compiletest ($fnamebase, "Testing for type of member $member",
343                    "Member \"$member\" does not have the correct type.", $res);
344     } elsif (/^optional-constant *([a-zA-Z0-9_]*) ([>=<]+) ([A-Za-z0-9_]*)/) {
345       my($const) = $1;
346       my($op) = $2;
347       my($value) = $3;
348       my($res) = $missing;
349
350       # Remember that this name is allowed.
351       push @allow, $const;
352
353       # Generate a program to test for the availability of this constant.
354       open (TESTFILE, ">$fnamebase.c");
355       print TESTFILE "$prepend";
356       print TESTFILE "#include <$h>\n";
357       print TESTFILE "__typeof__ ($const) a = $const;\n";
358       close (TESTFILE);
359
360       $res = compiletest ($fnamebase, "Testing for constant $const",
361                           "NOT PRESENT", $res, 1);
362
363       if ($value ne "") {
364         # Generate a program to test for the value of this constant.
365         open (TESTFILE, ">$fnamebase.c");
366         print TESTFILE "$prepend";
367         print TESTFILE "#include <$h>\n";
368         # Negate the value since 0 means ok
369         print TESTFILE "int main (void) { return !($const $op $value); }\n";
370         close (TESTFILE);
371
372         $res = runtest ($fnamebase, "Testing for value of constant $const",
373                         "Constant \"$const\" has not the right value.", $res);
374       }
375     } elsif (/^constant *([a-zA-Z0-9_]*) ([>=<]+) ([A-Za-z0-9_]*)/) {
376       my($const) = $1;
377       my($op) = $2;
378       my($value) = $3;
379       my($res) = $missing;
380
381       # Remember that this name is allowed.
382       push @allow, $const;
383
384       # Generate a program to test for the availability of this constant.
385       open (TESTFILE, ">$fnamebase.c");
386       print TESTFILE "$prepend";
387       print TESTFILE "#include <$h>\n";
388       print TESTFILE "__typeof__ ($const) a = $const;\n";
389       close (TESTFILE);
390
391       $res = compiletest ($fnamebase, "Testing for constant $const",
392                           "Constant \"$const\" not available.", $res, 0);
393
394       if ($value ne "") {
395         # Generate a program to test for the value of this constant.
396         open (TESTFILE, ">$fnamebase.c");
397         print TESTFILE "$prepend";
398         print TESTFILE "#include <$h>\n";
399         # Negate the value since 0 means ok
400         print TESTFILE "int main (void) { return !($const $op $value); }\n";
401         close (TESTFILE);
402
403         $res = runtest ($fnamebase, "Testing for value of constant $const",
404                         "Constant \"$const\" has not the right value.", $res);
405       }
406     } elsif (/^typed-constant *([a-zA-Z0-9_]*) *({([^}]*)}|([^ ]*)) *([A-Za-z0-9_]*)?/) {
407       my($const) = $1;
408       my($type) = "$3$4";
409       my($value) = $5;
410       my($res) = $missing;
411
412       # Remember that this name is allowed.
413       push @allow, $const;
414
415       # Generate a program to test for the availability of this constant.
416       open (TESTFILE, ">$fnamebase.c");
417       print TESTFILE "$prepend";
418       print TESTFILE "#include <$h>\n";
419       print TESTFILE "__typeof__ ($const) a = $const;\n";
420       close (TESTFILE);
421
422       $res = compiletest ($fnamebase, "Testing for constant $const",
423                           "Constant \"$const\" not available.", $res, 0);
424
425       # Test the types of the members.
426       open (TESTFILE, ">$fnamebase.c");
427       print TESTFILE "$prepend";
428       print TESTFILE "#include <$h>\n";
429       print TESTFILE "__typeof__ (($type) 0) a;\n";
430       print TESTFILE "extern __typeof__ ($const) a;\n";
431       close (TESTFILE);
432
433       compiletest ($fnamebase, "Testing for type of constant $const",
434                    "Constant \"$const\" does not have the correct type.",
435                    $res, 0);
436
437       if ($value ne "") {
438         # Generate a program to test for the value of this constant.
439         open (TESTFILE, ">$fnamebase.c");
440         print TESTFILE "$prepend";
441         print TESTFILE "#include <$h>\n";
442         print TESTFILE "int main (void) { return $const != $value; }\n";
443         close (TESTFILE);
444
445         $res = runtest ($fnamebase, "Testing for value of constant $const",
446                         "Constant \"$const\" has not the right value.", $res);
447       }
448     } elsif (/^optional-constant *([a-zA-Z0-9_]*) *([A-Za-z0-9_]*)?/) {
449       my($const) = $1;
450       my($value) = $2;
451       my($res) = $missing;
452
453       # Remember that this name is allowed.
454       push @allow, $const;
455
456       # Generate a program to test for the availability of this constant.
457       open (TESTFILE, ">$fnamebase.c");
458       print TESTFILE "$prepend";
459       print TESTFILE "#include <$h>\n";
460       print TESTFILE "__typeof__ ($const) a = $const;\n";
461       close (TESTFILE);
462
463       $res = compiletest ($fnamebase, "Testing for constant $const",
464                           "NOT PRESENT", $res, 1);
465
466       if ($value ne "") {
467         # Generate a program to test for the value of this constant.
468         open (TESTFILE, ">$fnamebase.c");
469         print TESTFILE "$prepend";
470         print TESTFILE "#include <$h>\n";
471         print TESTFILE "int main (void) { return $const != $value; }\n";
472         close (TESTFILE);
473
474         $res = runtest ($fnamebase, "Testing for value of constant $const",
475                         "Constant \"$const\" has not the right value.", $res);
476       }
477     } elsif (/^constant *([a-zA-Z0-9_]*) *([A-Za-z0-9_]*)?/) {
478       my($const) = $1;
479       my($value) = $2;
480       my($res) = $missing;
481
482       # Remember that this name is allowed.
483       push @allow, $const;
484
485       # Generate a program to test for the availability of this constant.
486       open (TESTFILE, ">$fnamebase.c");
487       print TESTFILE "$prepend";
488       print TESTFILE "#include <$h>\n";
489       print TESTFILE "__typeof__ ($const) a = $const;\n";
490       close (TESTFILE);
491
492       $res = compiletest ($fnamebase, "Testing for constant $const",
493                           "Constant \"$const\" not available.", $res, 0);
494
495       if ($value ne "") {
496         # Generate a program to test for the value of this constant.
497         open (TESTFILE, ">$fnamebase.c");
498         print TESTFILE "$prepend";
499         print TESTFILE "#include <$h>\n";
500         print TESTFILE "int main (void) { return $const != $value; }\n";
501         close (TESTFILE);
502
503         $res = runtest ($fnamebase, "Testing for value of constant $const",
504                         "Constant \"$const\" has not the right value.", $res);
505       }
506     } elsif (/^typed-constant *([a-zA-Z0-9_]*) *({([^}]*)}|([^ ]*)) *([A-Za-z0-9_]*)?/) {
507       my($const) = $1;
508       my($type) = "$3$4";
509       my($value) = $5;
510       my($res) = $missing;
511
512       # Remember that this name is allowed.
513       push @allow, $const;
514
515       # Generate a program to test for the availability of this constant.
516       open (TESTFILE, ">$fnamebase.c");
517       print TESTFILE "$prepend";
518       print TESTFILE "#include <$h>\n";
519       print TESTFILE "__typeof__ ($const) a = $const;\n";
520       close (TESTFILE);
521
522       $res = compiletest ($fnamebase, "Testing for constant $const",
523                           "Constant \"$const\" not available.", $res, 0);
524
525       # Test the types of the members.
526       open (TESTFILE, ">$fnamebase.c");
527       print TESTFILE "$prepend";
528       print TESTFILE "#include <$h>\n";
529       print TESTFILE "__typeof__ (($type) 0) a;\n";
530       print TESTFILE "extern __typeof__ ($const) a;\n";
531       close (TESTFILE);
532
533       compiletest ($fnamebase, "Testing for type of constant $const",
534                    "Constant \"$const\" does not have the correct type.",
535                    $res, 0);
536
537       if ($value ne "") {
538         # Generate a program to test for the value of this constant.
539         open (TESTFILE, ">$fnamebase.c");
540         print TESTFILE "$prepend";
541         print TESTFILE "#include <$h>\n";
542         print TESTFILE "int main (void) { return $const != $value; }\n";
543         close (TESTFILE);
544
545         $res = runtest ($fnamebase, "Testing for value of constant $const",
546                         "Constant \"$const\" has not the right value.", $res);
547       }
548     } elsif (/^type *({([^}]*)|([a-zA-Z0-9_]*))/) {
549       my($type) = "$2$3";
550
551       # Remember that this name is allowed.
552       if ($type =~ /^struct *(.*)/) {
553         push @allow, $1;
554       } elsif ($type =~ /^union *(.*)/) {
555         push @allow, $1;
556       } else {
557         push @allow, $type;
558       }
559
560       # Remember that this name is allowed.
561       push @allow, $type;
562
563       # Generate a program to test for the availability of this constant.
564       open (TESTFILE, ">$fnamebase.c");
565       print TESTFILE "$prepend";
566       print TESTFILE "#include <$h>\n";
567       print TESTFILE "$type *a;\n";
568       close (TESTFILE);
569
570       compiletest ($fnamebase, "Testing for type $type",
571                    "Type \"$type\" not available.", $missing, 0);
572     } elsif (/^function *({([^}]*)}|([a-zA-Z0-9_]*)) [(][*]([a-zA-Z0-9_]*) ([(].*[)])/) {
573       my($rettype) = "$2$3";
574       my($fname) = "$4";
575       my($args) = "$5";
576       my($res) = $missing;
577
578       # Remember that this name is allowed.
579       push @allow, $fname;
580
581       # Generate a program to test for availability of this function.
582       open (TESTFILE, ">$fnamebase.c");
583       print TESTFILE "$prepend";
584       print TESTFILE "#include <$h>\n";
585       # print TESTFILE "#undef $fname\n";
586       print TESTFILE "$rettype (*(*foobarbaz) $args = $fname;\n";
587       close (TESTFILE);
588
589       $res = compiletest ($fnamebase, "Test availability of function $fname",
590                           "Function \"$fname\" is not available.", $res, 0);
591
592       # Generate a program to test for the type of this function.
593       open (TESTFILE, ">$fnamebase.c");
594       print TESTFILE "$prepend";
595       print TESTFILE "#include <$h>\n";
596       # print TESTFILE "#undef $fname\n";
597       print TESTFILE "extern $rettype (*(*foobarbaz) $args;\n";
598       print TESTFILE "extern __typeof__ (&$fname) foobarbaz;\n";
599       close (TESTFILE);
600
601       compiletest ($fnamebase, "Test for type of function $fname",
602                    "Function \"$fname\" has incorrect type.", $res, 0);
603     } elsif (/^function *({([^}]*)}|([a-zA-Z0-9_]*)) ([a-zA-Z0-9_]*) ([(].*[)])/) {
604       my($rettype) = "$2$3";
605       my($fname) = "$4";
606       my($args) = "$5";
607       my($res) = $missing;
608
609       # Remember that this name is allowed.
610       push @allow, $fname;
611
612       # Generate a program to test for availability of this function.
613       open (TESTFILE, ">$fnamebase.c");
614       print TESTFILE "$prepend";
615       print TESTFILE "#include <$h>\n";
616       # print TESTFILE "#undef $fname\n";
617       print TESTFILE "$rettype (*foobarbaz) $args = $fname;\n";
618       close (TESTFILE);
619
620       $res = compiletest ($fnamebase, "Test availability of function $fname",
621                           "Function \"$fname\" is not available.", $res, 0);
622
623       # Generate a program to test for the type of this function.
624       open (TESTFILE, ">$fnamebase.c");
625       print TESTFILE "$prepend";
626       print TESTFILE "#include <$h>\n";
627       # print TESTFILE "#undef $fname\n";
628       print TESTFILE "extern $rettype (*foobarbaz) $args;\n";
629       print TESTFILE "extern __typeof__ (&$fname) foobarbaz;\n";
630       close (TESTFILE);
631
632       compiletest ($fnamebase, "Test for type of function $fname",
633                    "Function \"$fname\" has incorrect type.", $res, 0);
634     } elsif (/^variable *({([^}]*)}|([a-zA-Z0-9_]*)) ([a-zA-Z0-9_]*)/) {
635       my($type) = "$2$3";
636       my($vname) = "$4";
637       my($res) = $missing;
638
639       # Remember that this name is allowed.
640       push @allow, $vname;
641
642       # Generate a program to test for availability of this function.
643       open (TESTFILE, ">$fnamebase.c");
644       print TESTFILE "$prepend";
645       print TESTFILE "#include <$h>\n";
646       # print TESTFILE "#undef $fname\n";
647       print TESTFILE "$type *foobarbaz = &$vname;\n";
648       close (TESTFILE);
649
650       $res = compiletest ($fnamebase, "Test availability of variable $vname",
651                           "Variable \"$vname\" is not available.", $res, 0);
652
653       # Generate a program to test for the type of this function.
654       open (TESTFILE, ">$fnamebase.c");
655       print TESTFILE "$prepend";
656       print TESTFILE "#include <$h>\n";
657       # print TESTFILE "#undef $fname\n";
658       print TESTFILE "extern $type $vname;\n";
659       close (TESTFILE);
660
661       compiletest ($fnamebase, "Test for type of variable $fname",
662                    "Variable \"$vname\" has incorrect type.", $res, 0);
663     } elsif (/^macro-function *({([^}]*)}|([a-zA-Z0-9_]*)) ([a-zA-Z0-9_]*) ([(].*[)])/) {
664       my($rettype) = "$2$3";
665       my($fname) = "$4";
666       my($args) = "$5";
667       my($res) = $missing;
668
669       # Remember that this name is allowed.
670       push @allow, $fname;
671
672       # Generate a program to test for availability of this function.
673       open (TESTFILE, ">$fnamebase.c");
674       print TESTFILE "$prepend";
675       print TESTFILE "#include <$h>\n";
676       print TESTFILE "#ifndef $fname\n";
677       print TESTFILE "$rettype (*foobarbaz) $args = $fname;\n";
678       print TESTFILE "#endif\n";
679       close (TESTFILE);
680
681       $res = compiletest ($fnamebase, "Test availability of function $fname",
682                           "Function \"$fname\" is not available.", $res, 0);
683
684       # Generate a program to test for the type of this function.
685       open (TESTFILE, ">$fnamebase.c");
686       print TESTFILE "$prepend";
687       print TESTFILE "#include <$h>\n";
688       print TESTFILE "#ifndef $fname\n";
689       print TESTFILE "extern $rettype (*foobarbaz) $args;\n";
690       print TESTFILE "extern __typeof__ (&$fname) foobarbaz;\n";
691       print TESTFILE "#endif\n";
692       close (TESTFILE);
693
694       compiletest ($fnamebase, "Test for type of function $fname",
695                    "Function \"$fname\" has incorrect type.", $res, 0);
696     } elsif (/^macro-str *([^   ]*)\s*(\".*\")/) {
697       # The above regex doesn't handle a \" in a string.
698       my($macro) = "$1";
699       my($string) = "$2";
700       my($res) = $missing;
701
702       # Remember that this name is allowed.
703       push @allow, $macro;
704
705       # Generate a program to test for availability of this macro.
706       open (TESTFILE, ">$fnamebase.c");
707       print TESTFILE "$prepend";
708       print TESTFILE "#include <$h>\n";
709       print TESTFILE "#ifndef $macro\n";
710       print TESTFILE "# error \"Macro $macro not defined\"\n";
711       print TESTFILE "#endif\n";
712       close (TESTFILE);
713
714       compiletest ($fnamebase, "Test availability of macro $macro",
715                    "Macro \"$macro\" is not available.", $missing, 0);
716
717       # Generate a program to test for the value of this macro.
718       open (TESTFILE, ">$fnamebase.c");
719       print TESTFILE "$prepend";
720       print TESTFILE "#include <$h>\n";
721       # We can't include <string.h> here.
722       print TESTFILE "extern int (strcmp)(const char *, const char *);\n";
723       print TESTFILE "int main (void) { return strcmp ($macro, $string) != 0;}\n";
724       close (TESTFILE);
725
726       $res = runtest ($fnamebase, "Testing for value of macro $macro",
727                       "Macro \"$macro\" has not the right value.", $res);
728     } elsif (/^macro *([^       ]*)/) {
729       my($macro) = "$1";
730
731       # Remember that this name is allowed.
732       push @allow, $macro;
733
734       # Generate a program to test for availability of this macro.
735       open (TESTFILE, ">$fnamebase.c");
736       print TESTFILE "$prepend";
737       print TESTFILE "#include <$h>\n";
738       print TESTFILE "#ifndef $macro\n";
739       print TESTFILE "# error \"Macro $macro not defined\"\n";
740       print TESTFILE "#endif\n";
741       close (TESTFILE);
742
743       compiletest ($fnamebase, "Test availability of macro $macro",
744                    "Macro \"$macro\" is not available.", $missing, 0);
745     } elsif (/^allow-header *(.*)/) {
746       my($pattern) = $1;
747       if ($seenheader{$pattern} != 1) {
748         push @allowheader, $pattern;
749         $seenheader{$pattern} = 1;
750       }
751       next control;
752     } elsif (/^allow *(.*)/) {
753       my($pattern) = $1;
754       push @allow, $pattern;
755       next control;
756     } else {
757       # printf ("line is `%s'\n", $_);
758       next control;
759     }
760
761     printf ("\n");
762   }
763   close (CONTROL);
764
765   # Read the data files for the header files which are allowed to be included.
766   while ($#allowheader >= 0) {
767     my($ah) = pop @allowheader;
768
769     open (ALLOW, "$CC -E -D$dialect - < data/$ah-data |");
770     acontrol: while (<ALLOW>) {
771       next acontrol if (/^#/);
772       next acontrol if (/^[     ]*$/);
773
774       if (/^element *({([^}]*)}|([^ ]*)) *({([^}]*)}|([^ ]*)) *([A-Za-z0-9_]*) *(.*)/) {
775         push @allow, $7;
776       } elsif (/^constant *([a-zA-Z0-9_]*) *([A-Za-z0-9_]*)?/) {
777         push @allow, $1;
778       } elsif (/^typed-constant *([a-zA-Z0-9_]*) *({([^}]*)}|([^ ]*)) *([A-Za-z0-9_]*)?/) {
779         push @allow, 1;
780       } elsif (/^type *({([^}]*)|([a-zA-Z0-9_]*))/) {
781         my($type) = "$2$3";
782
783         # Remember that this name is allowed.
784         if ($type =~ /^struct *(.*)/) {
785           push @allow, $1;
786         } elsif ($type =~ /^union *(.*)/) {
787           push @allow, $1;
788         } else {
789           push @allow, $type;
790         }
791       } elsif (/^function *({([^}]*)}|([a-zA-Z0-9_]*)) [(][*]([a-zA-Z0-9_]*) ([(].*[)])/) {
792         push @allow, $4;
793       } elsif (/^function *({([^}]*)}|([a-zA-Z0-9_]*)) ([a-zA-Z0-9_]*) ([(].*[)])/) {
794         push @allow, $4;
795       } elsif (/^variable *({([^}]*)}|([a-zA-Z0-9_]*)) ([a-zA-Z0-9_]*)/) {
796         push @allow, $4;
797       } elsif (/^macro-function *({([^}]*)}|([a-zA-Z0-9_]*)) ([a-zA-Z0-9_]*) ([(].*[)])/) {
798         push @allow, $4;
799       } elsif (/^macro *([^     ]*)/) {
800         push @allow, $1;
801       } elsif (/^allow-header *(.*)/) {
802         if ($seenheader{$1} != 1) {
803           push @allowheader, $1;
804           $seenheader{$1} = 1;
805         }
806       } elsif (/^allow *(.*)/) {
807         push @allow, $1;
808       }
809     }
810     close (ALLOW);
811   }
812
813   # Now check the namespace.
814   printf ("  Checking the namespace of \"%s\"... ", $h);
815   if ($missing) {
816     ++$skipped;
817     printf ("SKIP\n");
818   } else {
819     checknamespace ($h, $fnamebase, @allow);
820   }
821
822   printf ("\n\n");
823 }
824
825 printf "-" x 76 . "\n";
826 printf ("  Total number of tests   : %4d\n", $total);
827
828 printf ("  Number of known failures: %4d (", $known);
829 $percent = ($known * 100) / $total;
830 if ($known > 0 && $percent < 1.0) {
831   printf (" <1%%)\n");
832 } else {
833   printf ("%3d%%)\n", $percent);
834 }
835
836 printf ("  Number of failed tests  : %4d (", $errors);
837 $percent = ($errors * 100) / $total;
838 if ($errors > 0 && $percent < 1.0) {
839   printf (" <1%%)\n");
840 } else {
841   printf ("%3d%%)\n", $percent);
842 }
843
844 printf ("  Number of skipped tests : %4d (", $skipped);
845 $percent = ($skipped * 100) / $total;
846 if ($skipped > 0 && $percent < 1.0) {
847   printf (" <1%%)\n");
848 } else {
849   printf ("%3d%%)\n", $percent);
850 }
851
852 exit $errors != 0;