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