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