conformtest: Unify "macro" cases.
[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 (/^constant *([a-zA-Z0-9_]*) *(?:{([^}]*)} *)?(?:([>=<!]+) ([A-Za-z0-9_-]*))?/) {
389       my($const) = $1;
390       my($type) = $2;
391       my($op) = $3;
392       my($value) = $4;
393       my($res) = $missing;
394
395       # Remember that this name is allowed.
396       push @allow, $const;
397
398       # Generate a program to test for the availability of this constant.
399       open (TESTFILE, ">$fnamebase.c");
400       print TESTFILE "$prepend";
401       print TESTFILE "#include <$h>\n";
402       print TESTFILE "__typeof__ ($const) a = $const;\n";
403       close (TESTFILE);
404
405       $res = compiletest ($fnamebase, "Testing for constant $const",
406                           ($optional
407                            ? "NOT PRESENT"
408                            : "Constant \"$const\" not available."), $res,
409                           $optional);
410
411       if (defined ($type) && ($res == 0 || !$optional)) {
412         # Test the types of the members.
413         open (TESTFILE, ">$fnamebase.c");
414         print TESTFILE "$prepend";
415         print TESTFILE "#include <$h>\n";
416         print TESTFILE "__typeof__ (($type) 0) a;\n";
417         print TESTFILE "extern __typeof__ ($const) a;\n";
418         close (TESTFILE);
419
420         compiletest ($fnamebase, "Testing for type of constant $const",
421                      "Constant \"$const\" does not have the correct type.",
422                      $res, 0);
423       }
424
425       if (defined ($op) && ($res == 0 || !$optional)) {
426         # Generate a program to test for the value of this constant.
427         open (TESTFILE, ">$fnamebase.c");
428         print TESTFILE "$prepend";
429         print TESTFILE "#include <$h>\n";
430         # Negate the value since 0 means ok
431         print TESTFILE "int main (void) { return !($const $op $value); }\n";
432         close (TESTFILE);
433
434         $res = runtest ($fnamebase, "Testing for value of constant $const",
435                         "Constant \"$const\" has not the right value.", $res);
436       }
437     } elsif (/^symbol *([a-zA-Z0-9_]*) *([A-Za-z0-9_-]*)?/) {
438       my($symbol) = $1;
439       my($value) = $2;
440       my($res) = $missing;
441
442       # Remember that this name is allowed.
443       push @allow, $symbol;
444
445       # Generate a program to test for the availability of this constant.
446       open (TESTFILE, ">$fnamebase.c");
447       print TESTFILE "$prepend";
448       print TESTFILE "#include <$h>\n";
449       print TESTFILE "void foobarbaz (void) {\n";
450       print TESTFILE "__typeof__ ($symbol) a = $symbol;\n";
451       print TESTFILE "}\n";
452       close (TESTFILE);
453
454       $res = compiletest ($fnamebase, "Testing for symbol $symbol",
455                           "Symbol \"$symbol\" not available.", $res, 0);
456
457       if ($value ne "") {
458         # Generate a program to test for the value of this constant.
459         open (TESTFILE, ">$fnamebase.c");
460         print TESTFILE "$prepend";
461         print TESTFILE "#include <$h>\n";
462         print TESTFILE "int main (void) { return $symbol != $value; }\n";
463         close (TESTFILE);
464
465         $res = runtest ($fnamebase, "Testing for value of symbol $symbol",
466                         "Symbol \"$symbol\" has not the right value.", $res);
467       }
468     } elsif (/^type *({([^}]*)|([a-zA-Z0-9_]*))/) {
469       my($type) = "$2$3";
470       my($maybe_opaque) = 0;
471
472       # Remember that this name is allowed.
473       if ($type =~ /^struct *(.*)/) {
474         push @allow, $1;
475       } elsif ($type =~ /^union *(.*)/) {
476         push @allow, $1;
477       } else {
478         push @allow, $type;
479         $maybe_opaque = 1;
480       }
481
482       # Generate a program to test for the availability of this type.
483       open (TESTFILE, ">$fnamebase.c");
484       print TESTFILE "$prepend";
485       print TESTFILE "#include <$h>\n";
486       if ($maybe_opaque == 1) {
487         print TESTFILE "$type *a;\n";
488       } else {
489         print TESTFILE "$type a;\n";
490       }
491       close (TESTFILE);
492
493       compiletest ($fnamebase, "Testing for type $type",
494                    ($optional
495                     ? "NOT AVAILABLE"
496                     : "Type \"$type\" not available."), $missing, $optional);
497     } elsif (/^tag *({([^}]*)|([a-zA-Z0-9_]*))/) {
498       my($type) = "$2$3";
499
500       # Remember that this name is allowed.
501       if ($type =~ /^struct *(.*)/) {
502         push @allow, $1;
503       } elsif ($type =~ /^union *(.*)/) {
504         push @allow, $1;
505       } else {
506         push @allow, $type;
507       }
508
509       # Generate a program to test for the availability of this type.
510       open (TESTFILE, ">$fnamebase.c");
511       print TESTFILE "$prepend";
512       print TESTFILE "#include <$h>\n";
513       print TESTFILE "$type;\n";
514       close (TESTFILE);
515
516       compiletest ($fnamebase, "Testing for type $type",
517                    "Type \"$type\" not available.", $missing, 0);
518     } elsif (/^function *({([^}]*)}|([a-zA-Z0-9_]*)) [(][*]([a-zA-Z0-9_]*) ([(].*[)])/) {
519       my($rettype) = "$2$3";
520       my($fname) = "$4";
521       my($args) = "$5";
522       my($res) = $missing;
523
524       # Remember that this name is allowed.
525       push @allow, $fname;
526
527       # Generate a program to test for availability of this function.
528       open (TESTFILE, ">$fnamebase.c");
529       print TESTFILE "$prepend";
530       print TESTFILE "#include <$h>\n";
531       # print TESTFILE "#undef $fname\n";
532       print TESTFILE "$rettype (*(*foobarbaz) $args = $fname;\n";
533       close (TESTFILE);
534
535       $res = compiletest ($fnamebase, "Test availability of function $fname",
536                           ($optional
537                            ? "NOT AVAILABLE"
538                            : "Function \"$fname\" is not available."), $res,
539                           $optional);
540
541       if ($res == 0 || $missing == 1 || !$optional) {
542         # Generate a program to test for the type of this function.
543         open (TESTFILE, ">$fnamebase.c");
544         print TESTFILE "$prepend";
545         print TESTFILE "#include <$h>\n";
546         # print TESTFILE "#undef $fname\n";
547         print TESTFILE "extern $rettype (*(*foobarbaz) $args;\n";
548         print TESTFILE "extern __typeof__ (&$fname) foobarbaz;\n";
549         close (TESTFILE);
550
551         compiletest ($fnamebase, "Test for type of function $fname",
552                      "Function \"$fname\" has incorrect type.", $res, 0);
553       }
554     } elsif (/^function *({([^}]*)}|([a-zA-Z0-9_]*)) ([a-zA-Z0-9_]*) ([(].*[)])/) {
555       my($rettype) = "$2$3";
556       my($fname) = "$4";
557       my($args) = "$5";
558       my($res) = $missing;
559
560       # Remember that this name is allowed.
561       push @allow, $fname;
562
563       # Generate a program to test for availability of this function.
564       open (TESTFILE, ">$fnamebase.c");
565       print TESTFILE "$prepend";
566       print TESTFILE "#include <$h>\n";
567       # print TESTFILE "#undef $fname\n";
568       print TESTFILE "$rettype (*foobarbaz) $args = $fname;\n";
569       close (TESTFILE);
570
571       $res = compiletest ($fnamebase, "Test availability of function $fname",
572                           ($optional
573                            ? "NOT AVAILABLE"
574                            : "Function \"$fname\" is not available."), $res,
575                           $optional);
576
577       if ($res == 0 || $missing != 0 || !$optional) {
578         # Generate a program to test for the type of this function.
579         open (TESTFILE, ">$fnamebase.c");
580         print TESTFILE "$prepend";
581         print TESTFILE "#include <$h>\n";
582         # print TESTFILE "#undef $fname\n";
583         print TESTFILE "extern $rettype (*foobarbaz) $args;\n";
584         print TESTFILE "extern __typeof__ (&$fname) foobarbaz;\n";
585         close (TESTFILE);
586
587         compiletest ($fnamebase, "Test for type of function $fname",
588                      "Function \"$fname\" has incorrect type.", $res, 0);
589       }
590     } elsif (/^variable *({([^}]*)}|([a-zA-Z0-9_]*)) ([a-zA-Z0-9_]*) *(.*)/) {
591       my($type) = "$2$3";
592       my($vname) = "$4";
593       my($rest) = "$5";
594       my($res) = $missing;
595
596       # Remember that this name is allowed.
597       push @allow, $vname;
598
599       # Generate a program to test for availability of this function.
600       open (TESTFILE, ">$fnamebase.c");
601       print TESTFILE "$prepend";
602       print TESTFILE "#include <$h>\n";
603       # print TESTFILE "#undef $fname\n";
604       print TESTFILE "typedef $type xyzzy$rest;\n";
605       print TESTFILE "$xyzzy *foobarbaz = &$vname;\n";
606       close (TESTFILE);
607
608       $res = compiletest ($fnamebase, "Test availability of variable $vname",
609                           "Variable \"$vname\" is not available.", $res, 0);
610
611       # Generate a program to test for the type of this function.
612       open (TESTFILE, ">$fnamebase.c");
613       print TESTFILE "$prepend";
614       print TESTFILE "#include <$h>\n";
615       # print TESTFILE "#undef $fname\n";
616       print TESTFILE "extern $type $vname$rest;\n";
617       close (TESTFILE);
618
619       compiletest ($fnamebase, "Test for type of variable $fname",
620                    "Variable \"$vname\" has incorrect type.", $res, 0);
621     } elsif (/^macro-function *({([^}]*)}|([a-zA-Z0-9_]*)) ([a-zA-Z0-9_]*) ([(].*[)])/) {
622       my($rettype) = "$2$3";
623       my($fname) = "$4";
624       my($args) = "$5";
625       my($res) = $missing;
626
627       # Remember that this name is allowed.
628       push @allow, $fname;
629
630       # Generate a program to test for availability of this function.
631       open (TESTFILE, ">$fnamebase.c");
632       print TESTFILE "$prepend";
633       print TESTFILE "#include <$h>\n";
634       print TESTFILE "#ifndef $fname\n";
635       print TESTFILE "$rettype (*foobarbaz) $args = $fname;\n";
636       print TESTFILE "#endif\n";
637       close (TESTFILE);
638
639       $res = compiletest ($fnamebase, "Test availability of macro $fname",
640                           "Function \"$fname\" is not available.", $res, 0);
641
642       # Generate a program to test for the type of this function.
643       open (TESTFILE, ">$fnamebase.c");
644       print TESTFILE "$prepend";
645       print TESTFILE "#include <$h>\n";
646       print TESTFILE "#ifndef $fname\n";
647       print TESTFILE "extern $rettype (*foobarbaz) $args;\n";
648       print TESTFILE "extern __typeof__ (&$fname) foobarbaz;\n";
649       print TESTFILE "#endif\n";
650       close (TESTFILE);
651
652       compiletest ($fnamebase, "Test for type of macro $fname",
653                    "Function \"$fname\" has incorrect type.", $res, 0);
654     } elsif (/^macro-str *([^    ]*) *(\".*\")/) {
655       # The above regex doesn't handle a \" in a string.
656       my($macro) = "$1";
657       my($string) = "$2";
658       my($res) = $missing;
659
660       # Remember that this name is allowed.
661       push @allow, $macro;
662
663       # Generate a program to test for availability of this macro.
664       open (TESTFILE, ">$fnamebase.c");
665       print TESTFILE "$prepend";
666       print TESTFILE "#include <$h>\n";
667       print TESTFILE "#ifndef $macro\n";
668       print TESTFILE "# error \"Macro $macro not defined\"\n";
669       print TESTFILE "#endif\n";
670       close (TESTFILE);
671
672       compiletest ($fnamebase, "Test availability of macro $macro",
673                    "Macro \"$macro\" is not available.", $missing, 0);
674
675       # Generate a program to test for the value of this macro.
676       open (TESTFILE, ">$fnamebase.c");
677       print TESTFILE "$prepend";
678       print TESTFILE "#include <$h>\n";
679       # We can't include <string.h> here.
680       print TESTFILE "extern int (strcmp)(const char *, const char *);\n";
681       print TESTFILE "int main (void) { return (strcmp) ($macro, $string) != 0;}\n";
682       close (TESTFILE);
683
684       $res = runtest ($fnamebase, "Testing for value of macro $macro",
685                       "Macro \"$macro\" has not the right value.", $res);
686     } elsif (/^macro *([a-zA-Z0-9_]*) *(?:([>=<!]+) ([A-Za-z0-9_]*))?/) {
687       my($macro) = "$1";
688       my($op) = $2;
689       my($value) = $3;
690       my($res) = $missing;
691
692       # Remember that this name is allowed.
693       push @allow, $macro;
694
695       # Generate a program to test for availability of this macro.
696       open (TESTFILE, ">$fnamebase.c");
697       print TESTFILE "$prepend";
698       print TESTFILE "#include <$h>\n";
699       print TESTFILE "#ifndef $macro\n";
700       print TESTFILE "# error \"Macro $macro not defined\"\n";
701       print TESTFILE "#endif\n";
702       close (TESTFILE);
703
704       $res = compiletest ($fnamebase, "Test availability of macro $macro",
705                           ($optional
706                            ? "NOT PRESENT"
707                            : "Macro \"$macro\" is not available."), $res,
708                           $optional);
709
710       if (defined ($op) && ($res == 0 || !$optional)) {
711         # Generate a program to test for the value of this constant.
712         open (TESTFILE, ">$fnamebase.c");
713         print TESTFILE "$prepend";
714         print TESTFILE "#include <$h>\n";
715         # Negate the value since 0 means ok
716         print TESTFILE "int main (void) { return !($macro $op $value); }\n";
717         close (TESTFILE);
718
719         $res = runtest ($fnamebase, "Testing for value of macro $macro",
720                         "Macro \"$macro\" has not the right value.", $res);
721       }
722     } elsif (/^allow-header *(.*)/) {
723       my($pattern) = $1;
724       if ($seenheader{$pattern} != 1) {
725         push @allowheader, $pattern;
726         $seenheader{$pattern} = 1;
727       }
728       next control;
729     } elsif (/^allow *(.*)/) {
730       my($pattern) = $1;
731       push @allow, $pattern;
732       next control;
733     } else {
734       # printf ("line is `%s'\n", $_);
735       next control;
736     }
737
738     printf ("\n");
739   }
740   close (CONTROL);
741
742   # Read the data files for the header files which are allowed to be included.
743   while ($#allowheader >= 0) {
744     my($ah) = pop @allowheader;
745
746     open (ALLOW, "$CC -E -D$standard - < data/$ah-data |");
747     acontrol: while (<ALLOW>) {
748       chop;
749       next acontrol if (/^#/);
750       next acontrol if (/^[     ]*$/);
751
752       if (/^element *({([^}]*)}|([^ ]*)) *({([^}]*)}|([^ ]*)) *([A-Za-z0-9_]*) *(.*)/) {
753         push @allow, $7;
754       } elsif (/^constant *([a-zA-Z0-9_]*) *(?:{([^}]*)} *)?(?:([>=<!]+) ([A-Za-z0-9_-]*))?/) {
755         push @allow, $1;
756       } elsif (/^(type|tag) *({([^}]*)|([a-zA-Z0-9_]*))/) {
757         my($type) = "$3$4";
758
759         # Remember that this name is allowed.
760         if ($type =~ /^struct *(.*)/) {
761           push @allow, $1;
762         } elsif ($type =~ /^union *(.*)/) {
763           push @allow, $1;
764         } else {
765           push @allow, $type;
766         }
767       } elsif (/^function *({([^}]*)}|([a-zA-Z0-9_]*)) [(][*]([a-zA-Z0-9_]*) ([(].*[)])/) {
768         push @allow, $4;
769       } elsif (/^function *({([^}]*)}|([a-zA-Z0-9_]*)) ([a-zA-Z0-9_]*) ([(].*[)])/) {
770         push @allow, $4;
771       } elsif (/^variable *({([^}]*)}|([a-zA-Z0-9_]*)) ([a-zA-Z0-9_]*)/) {
772         push @allow, $4;
773       } elsif (/^macro-function *({([^}]*)}|([a-zA-Z0-9_]*)) ([a-zA-Z0-9_]*) ([(].*[)])/) {
774         push @allow, $4;
775       } elsif (/^macro *([a-zA-Z0-9_]*) *(?:([>=<!]+) ([A-Za-z0-9_]*))?/) {
776         push @allow, $1;
777       } elsif (/^symbol *([a-zA-Z0-9_]*) *([A-Za-z0-9_-]*)?/) {
778         push @allow, $1;
779       } elsif (/^allow-header *(.*)/) {
780         if ($seenheader{$1} != 1) {
781           push @allowheader, $1;
782           $seenheader{$1} = 1;
783         }
784       } elsif (/^allow *(.*)/) {
785         push @allow, $1;
786       }
787     }
788     close (ALLOW);
789   }
790
791   if ($test_exist) {
792     printf ("  Not defined\n");
793   } else {
794     # Now check the namespace.
795     printf ("  Checking the namespace of \"%s\"... ", $h);
796     if ($missing) {
797       ++$skipped;
798       printf ("SKIP\n");
799     } else {
800       checknamespace ($h, $fnamebase, @allow);
801     }
802   }
803
804   printf ("\n\n");
805 }
806
807 printf "-" x 76 . "\n";
808 printf ("  Total number of tests   : %4d\n", $total);
809
810 printf ("  Number of failed tests  : %4d (", $errors);
811 $percent = ($errors * 100) / $total;
812 if ($errors > 0 && $percent < 1.0) {
813   printf (" <1%%)\n");
814 } else {
815   printf ("%3d%%)\n", $percent);
816 }
817
818 printf ("  Number of skipped tests : %4d (", $skipped);
819 $percent = ($skipped * 100) / $total;
820 if ($skipped > 0 && $percent < 1.0) {
821   printf (" <1%%)\n");
822 } else {
823   printf ("%3d%%)\n", $percent);
824 }
825
826 exit $errors != 0;
827 # Local Variables:
828 #  perl-indent-level: 2
829 # End: