conformtest: Fix typo in handling typed-constant from 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 (/^typed-constant *([a-zA-Z0-9_]*) *({([^}]*)}|([^ ]*)) *([A-Za-z0-9_-]*)?/) {
624       my($const) = $1;
625       my($type) = "$3$4";
626       my($value) = $5;
627       my($res) = $missing;
628
629       # Remember that this name is allowed.
630       push @allow, $const;
631
632       # Generate a program to test for the availability of this constant.
633       open (TESTFILE, ">$fnamebase.c");
634       print TESTFILE "$prepend";
635       print TESTFILE "#include <$h>\n";
636       print TESTFILE "__typeof__ ($const) a = $const;\n";
637       close (TESTFILE);
638
639       $res = compiletest ($fnamebase, "Testing for constant $const",
640                           "Constant \"$const\" not available.", $res, 0);
641
642       # Test the types of the members.
643       open (TESTFILE, ">$fnamebase.c");
644       print TESTFILE "$prepend";
645       print TESTFILE "#include <$h>\n";
646       print TESTFILE "__typeof__ (($type) 0) a;\n";
647       print TESTFILE "extern __typeof__ ($const) a;\n";
648       close (TESTFILE);
649
650       compiletest ($fnamebase, "Testing for type of constant $const",
651                    "Constant \"$const\" does not have the correct type.",
652                    $res, 0);
653
654       if ($value ne "") {
655         # Generate a program to test for the value of this constant.
656         open (TESTFILE, ">$fnamebase.c");
657         print TESTFILE "$prepend";
658         print TESTFILE "#include <$h>\n";
659         print TESTFILE "int main (void) { return $const != $value; }\n";
660         close (TESTFILE);
661
662         $res = runtest ($fnamebase, "Testing for value of constant $const",
663                         "Constant \"$const\" has not the right value.", $res);
664       }
665     } elsif (/^optional-type *({([^}]*)|([a-zA-Z0-9_]*))/) {
666       my($type) = "$2$3";
667       my($maybe_opaque) = 0;
668
669       # Remember that this name is allowed.
670       if ($type =~ /^struct *(.*)/) {
671         push @allow, $1;
672       } elsif ($type =~ /^union *(.*)/) {
673         push @allow, $1;
674       } else {
675         push @allow, $type;
676         $maybe_opaque = 1;
677       }
678
679       # Generate a program to test for the availability of this constant.
680       open (TESTFILE, ">$fnamebase.c");
681       print TESTFILE "$prepend";
682       print TESTFILE "#include <$h>\n";
683       if ($maybe_opaque == 1) {
684         print TESTFILE "$type *a;\n";
685       } else {
686         print TESTFILE "$type a;\n";
687       }
688       close (TESTFILE);
689
690       compiletest ($fnamebase, "Testing for type $type",
691                    "NOT AVAILABLE", $missing, 1);
692     } elsif (/^type *({([^}]*)|([a-zA-Z0-9_]*))/) {
693       my($type) = "$2$3";
694       my($maybe_opaque) = 0;
695
696       # Remember that this name is allowed.
697       if ($type =~ /^struct *(.*)/) {
698         push @allow, $1;
699       } elsif ($type =~ /^union *(.*)/) {
700         push @allow, $1;
701       } else {
702         push @allow, $type;
703         $maybe_opaque = 1;
704       }
705
706       # Generate a program to test for the availability of this type.
707       open (TESTFILE, ">$fnamebase.c");
708       print TESTFILE "$prepend";
709       print TESTFILE "#include <$h>\n";
710       if ($maybe_opaque == 1) {
711         print TESTFILE "$type *a;\n";
712       } else {
713         print TESTFILE "$type a;\n";
714       }
715       close (TESTFILE);
716
717       compiletest ($fnamebase, "Testing for type $type",
718                    "Type \"$type\" not available.", $missing, 0);
719     } elsif (/^tag *({([^}]*)|([a-zA-Z0-9_]*))/) {
720       my($type) = "$2$3";
721
722       # Remember that this name is allowed.
723       if ($type =~ /^struct *(.*)/) {
724         push @allow, $1;
725       } elsif ($type =~ /^union *(.*)/) {
726         push @allow, $1;
727       } else {
728         push @allow, $type;
729       }
730
731       # Generate a program to test for the availability of this type.
732       open (TESTFILE, ">$fnamebase.c");
733       print TESTFILE "$prepend";
734       print TESTFILE "#include <$h>\n";
735       print TESTFILE "$type;\n";
736       close (TESTFILE);
737
738       compiletest ($fnamebase, "Testing for type $type",
739                    "Type \"$type\" not available.", $missing, 0);
740     } elsif (/^optional-function *({([^}]*)}|([a-zA-Z0-9_]*)) [(][*]([a-zA-Z0-9_]*) ([(].*[)])/) {
741       my($rettype) = "$2$3";
742       my($fname) = "$4";
743       my($args) = "$5";
744       my($res) = $missing;
745
746       # Remember that this name is allowed.
747       push @allow, $fname;
748
749       # Generate a program to test for availability of this function.
750       open (TESTFILE, ">$fnamebase.c");
751       print TESTFILE "$prepend";
752       print TESTFILE "#include <$h>\n";
753       # print TESTFILE "#undef $fname\n";
754       print TESTFILE "$rettype (*(*foobarbaz) $args = $fname;\n";
755       close (TESTFILE);
756
757       $res = compiletest ($fnamebase, "Test availability of function $fname",
758                           "NOT AVAILABLE", $res, 1);
759
760       if ($res == 0 || $missing == 1) {
761         # Generate a program to test for the type of this function.
762         open (TESTFILE, ">$fnamebase.c");
763         print TESTFILE "$prepend";
764         print TESTFILE "#include <$h>\n";
765         # print TESTFILE "#undef $fname\n";
766         print TESTFILE "extern $rettype (*(*foobarbaz) $args;\n";
767         print TESTFILE "extern __typeof__ (&$fname) foobarbaz;\n";
768         close (TESTFILE);
769
770         compiletest ($fnamebase, "Test for type of function $fname",
771                      "Function \"$fname\" has incorrect type.", $res, 0);
772       }
773     } elsif (/^function *({([^}]*)}|([a-zA-Z0-9_]*)) [(][*]([a-zA-Z0-9_]*) ([(].*[)])/) {
774       my($rettype) = "$2$3";
775       my($fname) = "$4";
776       my($args) = "$5";
777       my($res) = $missing;
778
779       # Remember that this name is allowed.
780       push @allow, $fname;
781
782       # Generate a program to test for availability of this function.
783       open (TESTFILE, ">$fnamebase.c");
784       print TESTFILE "$prepend";
785       print TESTFILE "#include <$h>\n";
786       # print TESTFILE "#undef $fname\n";
787       print TESTFILE "$rettype (*(*foobarbaz) $args = $fname;\n";
788       close (TESTFILE);
789
790       $res = compiletest ($fnamebase, "Test availability of function $fname",
791                           "Function \"$fname\" is not available.", $res, 0);
792
793       # Generate a program to test for the type of this function.
794       open (TESTFILE, ">$fnamebase.c");
795       print TESTFILE "$prepend";
796       print TESTFILE "#include <$h>\n";
797       # print TESTFILE "#undef $fname\n";
798       print TESTFILE "extern $rettype (*(*foobarbaz) $args;\n";
799       print TESTFILE "extern __typeof__ (&$fname) foobarbaz;\n";
800       close (TESTFILE);
801
802       compiletest ($fnamebase, "Test for type of function $fname",
803                    "Function \"$fname\" has incorrect type.", $res, 0);
804     } elsif (/^optional-function *({([^}]*)}|([a-zA-Z0-9_]*)) ([a-zA-Z0-9_]*) ([(].*[)])/) {
805       my($rettype) = "$2$3";
806       my($fname) = "$4";
807       my($args) = "$5";
808       my($res) = $missing;
809
810       # Remember that this name is allowed.
811       push @allow, $fname;
812
813       # Generate a program to test for availability of this function.
814       open (TESTFILE, ">$fnamebase.c");
815       print TESTFILE "$prepend";
816       print TESTFILE "#include <$h>\n";
817       # print TESTFILE "#undef $fname\n";
818       print TESTFILE "$rettype (*foobarbaz) $args = $fname;\n";
819       close (TESTFILE);
820
821       $res = compiletest ($fnamebase, "Test availability of function $fname",
822                           "NOT AVAILABLE", $res, 1);
823
824       if ($res == 0 || $missing != 0) {
825         # Generate a program to test for the type of this function.
826         open (TESTFILE, ">$fnamebase.c");
827         print TESTFILE "$prepend";
828         print TESTFILE "#include <$h>\n";
829         # print TESTFILE "#undef $fname\n";
830         print TESTFILE "extern $rettype (*foobarbaz) $args;\n";
831         print TESTFILE "extern __typeof__ (&$fname) foobarbaz;\n";
832         close (TESTFILE);
833
834         compiletest ($fnamebase, "Test for type of function $fname",
835                      "Function \"$fname\" has incorrect type.", $res, 0);
836       }
837     } elsif (/^function *({([^}]*)}|([a-zA-Z0-9_]*)) ([a-zA-Z0-9_]*) ([(].*[)])/) {
838       my($rettype) = "$2$3";
839       my($fname) = "$4";
840       my($args) = "$5";
841       my($res) = $missing;
842
843       # Remember that this name is allowed.
844       push @allow, $fname;
845
846       # Generate a program to test for availability of this function.
847       open (TESTFILE, ">$fnamebase.c");
848       print TESTFILE "$prepend";
849       print TESTFILE "#include <$h>\n";
850       # print TESTFILE "#undef $fname\n";
851       print TESTFILE "$rettype (*foobarbaz) $args = $fname;\n";
852       close (TESTFILE);
853
854       $res = compiletest ($fnamebase, "Test availability of function $fname",
855                           "Function \"$fname\" is not available.", $res, 0);
856
857       # Generate a program to test for the type of this function.
858       open (TESTFILE, ">$fnamebase.c");
859       print TESTFILE "$prepend";
860       print TESTFILE "#include <$h>\n";
861       # print TESTFILE "#undef $fname\n";
862       print TESTFILE "extern $rettype (*foobarbaz) $args;\n";
863       print TESTFILE "extern __typeof__ (&$fname) foobarbaz;\n";
864       close (TESTFILE);
865
866       compiletest ($fnamebase, "Test for type of function $fname",
867                    "Function \"$fname\" has incorrect type.", $res, 0);
868     } elsif (/^variable *({([^}]*)}|([a-zA-Z0-9_]*)) ([a-zA-Z0-9_]*) *(.*)/) {
869       my($type) = "$2$3";
870       my($vname) = "$4";
871       my($rest) = "$5";
872       my($res) = $missing;
873
874       # Remember that this name is allowed.
875       push @allow, $vname;
876
877       # Generate a program to test for availability of this function.
878       open (TESTFILE, ">$fnamebase.c");
879       print TESTFILE "$prepend";
880       print TESTFILE "#include <$h>\n";
881       # print TESTFILE "#undef $fname\n";
882       print TESTFILE "typedef $type xyzzy$rest;\n";
883       print TESTFILE "$xyzzy *foobarbaz = &$vname;\n";
884       close (TESTFILE);
885
886       $res = compiletest ($fnamebase, "Test availability of variable $vname",
887                           "Variable \"$vname\" is not available.", $res, 0);
888
889       # Generate a program to test for the type of this function.
890       open (TESTFILE, ">$fnamebase.c");
891       print TESTFILE "$prepend";
892       print TESTFILE "#include <$h>\n";
893       # print TESTFILE "#undef $fname\n";
894       print TESTFILE "extern $type $vname$rest;\n";
895       close (TESTFILE);
896
897       compiletest ($fnamebase, "Test for type of variable $fname",
898                    "Variable \"$vname\" has incorrect type.", $res, 0);
899     } elsif (/^macro-function *({([^}]*)}|([a-zA-Z0-9_]*)) ([a-zA-Z0-9_]*) ([(].*[)])/) {
900       my($rettype) = "$2$3";
901       my($fname) = "$4";
902       my($args) = "$5";
903       my($res) = $missing;
904
905       # Remember that this name is allowed.
906       push @allow, $fname;
907
908       # Generate a program to test for availability of this function.
909       open (TESTFILE, ">$fnamebase.c");
910       print TESTFILE "$prepend";
911       print TESTFILE "#include <$h>\n";
912       print TESTFILE "#ifndef $fname\n";
913       print TESTFILE "$rettype (*foobarbaz) $args = $fname;\n";
914       print TESTFILE "#endif\n";
915       close (TESTFILE);
916
917       $res = compiletest ($fnamebase, "Test availability of macro $fname",
918                           "Function \"$fname\" is not available.", $res, 0);
919
920       # Generate a program to test for the type of this function.
921       open (TESTFILE, ">$fnamebase.c");
922       print TESTFILE "$prepend";
923       print TESTFILE "#include <$h>\n";
924       print TESTFILE "#ifndef $fname\n";
925       print TESTFILE "extern $rettype (*foobarbaz) $args;\n";
926       print TESTFILE "extern __typeof__ (&$fname) foobarbaz;\n";
927       print TESTFILE "#endif\n";
928       close (TESTFILE);
929
930       compiletest ($fnamebase, "Test for type of macro $fname",
931                    "Function \"$fname\" has incorrect type.", $res, 0);
932     } elsif (/^macro-str *([^    ]*) *(\".*\")/) {
933       # The above regex doesn't handle a \" in a string.
934       my($macro) = "$1";
935       my($string) = "$2";
936       my($res) = $missing;
937
938       # Remember that this name is allowed.
939       push @allow, $macro;
940
941       # Generate a program to test for availability of this macro.
942       open (TESTFILE, ">$fnamebase.c");
943       print TESTFILE "$prepend";
944       print TESTFILE "#include <$h>\n";
945       print TESTFILE "#ifndef $macro\n";
946       print TESTFILE "# error \"Macro $macro not defined\"\n";
947       print TESTFILE "#endif\n";
948       close (TESTFILE);
949
950       compiletest ($fnamebase, "Test availability of macro $macro",
951                    "Macro \"$macro\" is not available.", $missing, 0);
952
953       # Generate a program to test for the value of this macro.
954       open (TESTFILE, ">$fnamebase.c");
955       print TESTFILE "$prepend";
956       print TESTFILE "#include <$h>\n";
957       # We can't include <string.h> here.
958       print TESTFILE "extern int (strcmp)(const char *, const char *);\n";
959       print TESTFILE "int main (void) { return (strcmp) ($macro, $string) != 0;}\n";
960       close (TESTFILE);
961
962       $res = runtest ($fnamebase, "Testing for value of macro $macro",
963                       "Macro \"$macro\" has not the right value.", $res);
964     } elsif (/^optional-macro *([^      ]*)/) {
965       my($macro) = "$1";
966
967       # Remember that this name is allowed.
968       push @allow, $macro;
969
970       # Generate a program to test for availability of this macro.
971       open (TESTFILE, ">$fnamebase.c");
972       print TESTFILE "$prepend";
973       print TESTFILE "#include <$h>\n";
974       print TESTFILE "#ifndef $macro\n";
975       print TESTFILE "# error \"Macro $macro not defined\"\n";
976       print TESTFILE "#endif\n";
977       close (TESTFILE);
978
979       compiletest ($fnamebase, "Test availability of macro $macro",
980                    "NOT PRESENT", $missing, 1);
981     } elsif (/^macro *([a-zA-Z0-9_]*) *([>=<!]+) ([A-Za-z0-9_]*)/) {
982       my($macro) = "$1";
983       my($op) = $2;
984       my($value) = $3;
985       my($res) = $missing;
986
987       # Remember that this name is allowed.
988       push @allow, $macro;
989
990       # Generate a program to test for availability of this macro.
991       open (TESTFILE, ">$fnamebase.c");
992       print TESTFILE "$prepend";
993       print TESTFILE "#include <$h>\n";
994       print TESTFILE "#ifndef $macro\n";
995       print TESTFILE "# error \"Macro $macro not defined\"\n";
996       print TESTFILE "#endif\n";
997       close (TESTFILE);
998
999       $res = compiletest ($fnamebase, "Test availability of macro $macro",
1000                           "Macro \"$macro\" is not available.", $res, 0);
1001
1002       if ($value ne "") {
1003         # Generate a program to test for the value of this constant.
1004         open (TESTFILE, ">$fnamebase.c");
1005         print TESTFILE "$prepend";
1006         print TESTFILE "#include <$h>\n";
1007         # Negate the value since 0 means ok
1008         print TESTFILE "int main (void) { return !($macro $op $value); }\n";
1009         close (TESTFILE);
1010
1011         $res = runtest ($fnamebase, "Testing for value of macro $macro",
1012                         "Macro \"$macro\" has not the right value.", $res);
1013       }
1014     } elsif (/^macro *([a-zA-Z0-9_]*) *([A-Za-z0-9_]*)/) {
1015       my($macro) = "$1";
1016       my($value) = $2;
1017       my($res) = $missing;
1018
1019       # Remember that this name is allowed.
1020       push @allow, $macro;
1021
1022       # Generate a program to test for availability of this macro.
1023       open (TESTFILE, ">$fnamebase.c");
1024       print TESTFILE "$prepend";
1025       print TESTFILE "#include <$h>\n";
1026       print TESTFILE "#ifndef $macro\n";
1027       print TESTFILE "# error \"Macro $macro not defined\"\n";
1028       print TESTFILE "#endif\n";
1029       close (TESTFILE);
1030
1031       $res = compiletest ($fnamebase, "Test availability of macro $macro",
1032                           "Macro \"$macro\" is not available.", $res, 0);
1033
1034       if ($value ne "") {
1035         # Generate a program to test for the value of this constant.
1036         open (TESTFILE, ">$fnamebase.c");
1037         print TESTFILE "$prepend";
1038         print TESTFILE "#include <$h>\n";
1039         # Negate the value since 0 means ok
1040         print TESTFILE "int main (void) { return !($macro == $value); }\n";
1041         close (TESTFILE);
1042
1043         $res = runtest ($fnamebase, "Testing for value of macro $macro",
1044                         "Macro \"$macro\" has not the right value.", $res);
1045       }
1046     } elsif (/^macro *([^       ]*)/) {
1047       my($macro) = "$1";
1048
1049       # Remember that this name is allowed.
1050       push @allow, $macro;
1051
1052       # Generate a program to test for availability of this macro.
1053       open (TESTFILE, ">$fnamebase.c");
1054       print TESTFILE "$prepend";
1055       print TESTFILE "#include <$h>\n";
1056       print TESTFILE "#ifndef $macro\n";
1057       print TESTFILE "# error \"Macro $macro not defined\"\n";
1058       print TESTFILE "#endif\n";
1059       close (TESTFILE);
1060
1061       compiletest ($fnamebase, "Test availability of macro $macro",
1062                    "Macro \"$macro\" is not available.", $missing, 0);
1063     } elsif (/^allow-header *(.*)/) {
1064       my($pattern) = $1;
1065       if ($seenheader{$pattern} != 1) {
1066         push @allowheader, $pattern;
1067         $seenheader{$pattern} = 1;
1068       }
1069       next control;
1070     } elsif (/^allow *(.*)/) {
1071       my($pattern) = $1;
1072       push @allow, $pattern;
1073       next control;
1074     } else {
1075       # printf ("line is `%s'\n", $_);
1076       next control;
1077     }
1078
1079     printf ("\n");
1080   }
1081   close (CONTROL);
1082
1083   # Read the data files for the header files which are allowed to be included.
1084   while ($#allowheader >= 0) {
1085     my($ah) = pop @allowheader;
1086
1087     open (ALLOW, "$CC -E -D$standard - < data/$ah-data |");
1088     acontrol: while (<ALLOW>) {
1089       chop;
1090       next acontrol if (/^#/);
1091       next acontrol if (/^[     ]*$/);
1092
1093       if (/^element *({([^}]*)}|([^ ]*)) *({([^}]*)}|([^ ]*)) *([A-Za-z0-9_]*) *(.*)/) {
1094         push @allow, $7;
1095       } elsif (/^constant *([a-zA-Z0-9_]*) *([A-Za-z0-9_]*)?/) {
1096         push @allow, $1;
1097       } elsif (/^typed-constant *([a-zA-Z0-9_]*) *({([^}]*)}|([^ ]*)) *([A-Za-z0-9_]*)?/) {
1098         push @allow, $1;
1099       } elsif (/^(type|tag) *({([^}]*)|([a-zA-Z0-9_]*))/) {
1100         my($type) = "$3$4";
1101
1102         # Remember that this name is allowed.
1103         if ($type =~ /^struct *(.*)/) {
1104           push @allow, $1;
1105         } elsif ($type =~ /^union *(.*)/) {
1106           push @allow, $1;
1107         } else {
1108           push @allow, $type;
1109         }
1110       } elsif (/^function *({([^}]*)}|([a-zA-Z0-9_]*)) [(][*]([a-zA-Z0-9_]*) ([(].*[)])/) {
1111         push @allow, $4;
1112       } elsif (/^function *({([^}]*)}|([a-zA-Z0-9_]*)) ([a-zA-Z0-9_]*) ([(].*[)])/) {
1113         push @allow, $4;
1114       } elsif (/^variable *({([^}]*)}|([a-zA-Z0-9_]*)) ([a-zA-Z0-9_]*)/) {
1115         push @allow, $4;
1116       } elsif (/^macro-function *({([^}]*)}|([a-zA-Z0-9_]*)) ([a-zA-Z0-9_]*) ([(].*[)])/) {
1117         push @allow, $4;
1118       } elsif (/^macro *([^     ]*)/) {
1119         push @allow, $1;
1120       } elsif (/^allow-header *(.*)/) {
1121         if ($seenheader{$1} != 1) {
1122           push @allowheader, $1;
1123           $seenheader{$1} = 1;
1124         }
1125       } elsif (/^allow *(.*)/) {
1126         push @allow, $1;
1127       }
1128     }
1129     close (ALLOW);
1130   }
1131
1132   if ($test_exist) {
1133     printf ("  Not defined\n");
1134   } else {
1135     # Now check the namespace.
1136     printf ("  Checking the namespace of \"%s\"... ", $h);
1137     if ($missing) {
1138       ++$skipped;
1139       printf ("SKIP\n");
1140     } else {
1141       checknamespace ($h, $fnamebase, @allow);
1142     }
1143   }
1144
1145   printf ("\n\n");
1146 }
1147
1148 printf "-" x 76 . "\n";
1149 printf ("  Total number of tests   : %4d\n", $total);
1150
1151 printf ("  Number of failed tests  : %4d (", $errors);
1152 $percent = ($errors * 100) / $total;
1153 if ($errors > 0 && $percent < 1.0) {
1154   printf (" <1%%)\n");
1155 } else {
1156   printf ("%3d%%)\n", $percent);
1157 }
1158
1159 printf ("  Number of skipped tests : %4d (", $skipped);
1160 $percent = ($skipped * 100) / $total;
1161 if ($skipped > 0 && $percent < 1.0) {
1162   printf (" <1%%)\n");
1163 } else {
1164   printf ("%3d%%)\n", $percent);
1165 }
1166
1167 exit $errors != 0;
1168 # Local Variables:
1169 #  perl-indent-level: 2
1170 # End: