4 $CFLAGS = "-I. '-D__attribute__(x)=' -D_XOPEN_SOURCE=500";
6 # List of the headers we are testing.
7 @headers = ("wordexp.h", "wctype.h", "wchar.h", "varargs.h", "utmpx.h",
8 "utime.h", "unistd.h", "ulimit.h", "ucontext.h", "time.h",
9 "termios.h", "tar.h", "sys/wait.h", "sys/utsname.h", "sys/un.h",
10 "sys/uio.h", "sys/types.h", "sys/times.h", "sys/timeb.h",
11 "sys/time.h", "sys/statvfs.h", "sys/stat.h", "sys/socket.h",
12 "sys/shm.h", "sys/sem.h", "sys/resource.h", "sys/msg.h",
13 "sys/mman.h", "sys/ipc.h", "syslog.h", "stropts.h", "strings.h",
14 "string.h", "stdlib.h", "stdio.h", "stddef.h", "stdarg.h",
15 "spawn.h", "signal.h", "setjmp.h", "semaphore.h",
16 "search.h", "sched.h", "regex.h", "pwd.h", "pthread.h",
17 "poll.h", "nl_types.h", "netinet/tcp.h", "netinet/in.h",
18 "net/if.h", "netdb.h", "ndbm.h", "mqueue.h", "monetary.h",
19 "math.h", "locale.h", "libgen.h", "langinfo.h", "iso646.h",
20 "inttypes.h", "iconv.h", "grp.h", "glob.h", "ftw.h", "fnmatch.h",
21 "fmtmsg.h", "float.h", "fcntl.h", "errno.h", "dlfcn.h", "dirent.h",
22 "ctype.h", "cpio.h", "assert.h", "arpa/inet.h", "aio.h");
24 # These are the ISO C99 keywords.
25 @keywords = ('auto', 'break', 'case', 'char', 'const', 'continue', 'default',
26 'do', 'double', 'else', 'enum', 'extern', 'float', 'for', 'goto',
27 'if', 'inline', 'int', 'long', 'register', 'restrict', 'return',
28 'short', 'signed', 'sizeof', 'static', 'struct', 'switch',
29 'typedef', 'union', 'unsigned', 'void', 'volatile', 'while');
31 # Some headers need a bit more attention.
32 $mustprepend{'regex.h'} = "#include <sys/types.h>\n";
34 # Make an hash table from this information.
36 $iskeyword{pop (@keywords)} = 1;
55 my($pattern, $string) = @_;
56 my($strlen) = length ($string);
59 if (substr ($pattern, 0, 1) eq '*') {
60 my($patlen) = length ($pattern) - 1;
61 $res = ($strlen >= $patlen
62 && substr ($pattern, -$patlen, $patlen) eq substr ($string, -$patlen, $patlen));
63 } elsif (substr ($pattern, -1, 1) eq '*') {
64 my($patlen) = length ($pattern) - 1;
65 $res = ($strlen >= $patlen
66 && substr ($pattern, 0, $patlen) eq substr ($string, 0, $patlen));
68 $res = $pattern eq $string;
76 my($fnamebase, $msg, $errmsg, $skip) = @_;
87 $ret = system "$CC $CFLAGS -c $fnamebase.c -o $fnamebase.o > $fnamebase.out 2>&1";
91 printf (" $errmsg Compiler message:\n");
98 if ($verbose > 1 && -s "$fnamebase.out") {
99 # We print all warnings issued.
103 if ($printlog != 0) {
104 printf (" " . "-" x 71 . "\n");
105 open (MESSAGE, "< $fnamebase.out");
110 printf (" " . "-" x 71 . "\n");
113 unlink "$fnamebase.c";
114 unlink "$fnamebase.o";
115 unlink "$fnamebase.out";
123 my($fnamebase, $msg, $errmsg, $skip) = @_;
134 $ret = system "$CC $CFLAGS -o $fnamebase $fnamebase.c > $fnamebase.out 2>&1";
138 printf (" $errmsg Compiler message:\n");
144 # Now run the program. If the exit code is not zero something is wrong.
145 $result = system "$fnamebase > $fnamebase.out2 2>&1";
148 if ($verbose > 1 && -s "$fnamebase.out") {
149 # We print all warnings issued.
151 system "cat $fnamebase.out2 >> $fnamebase.out";
156 unlink "$fnamebase.out";
157 rename "$fnamebase.out2", "$fnamebase.out";
160 if ($printlog != 0) {
161 printf (" " . "-" x 71 . "\n");
162 open (MESSAGE, "< $fnamebase.out");
167 printf (" " . "-" x 71 . "\n");
171 unlink "$fnamebase.c";
172 unlink "$fnamebase.o";
173 unlink "$fnamebase.out";
174 unlink "$fnamebase.out2";
181 my($token, $nerrors, @allow) = @_;
184 if ($token =~ /^[0-9_]/ || $iskeyword{$token}) {
188 for ($idx = 0; $idx <= $#allow; ++$idx) {
189 if (poorfnmatch ($allow[$idx], $token)) {
196 printf ("FAIL\n " . "-" x 72 . "\n");
198 printf (" Namespace violation: \"%s\"\n", $token);
204 my($h, $fnamebase, @allow) = @_;
209 # Generate a program to get the contents of this header.
210 open (TESTFILE, ">$fnamebase.c");
211 print TESTFILE "#include <$h>\n";
214 open (CONTENT, "$CC $CFLAGS -E $fnamebase.c -Wp,-dN | sed -e '/^# [1-9]/d' -e '/^[[:space:]]*\$/d' |");
217 if (/^#define (.*)/) {
218 $nerrors = newtoken ($1, $nerrors, @allow);
220 # We have to tokenize the line.
223 my($len) = length ($str);
225 foreach $token (split(/[^a-zA-Z0-9_]/, $str)) {
227 $nerrors = newtoken ($token, $nerrors, @allow);
233 unlink "$fnamebase.c";
235 printf (" " . "-" x 72 . "\n");
243 while ($#headers >= 0) {
244 my($h) = pop (@headers);
247 my($fnamebase) = "$tmpdir/$hf-test";
250 my(@allowheader) = ();
251 my($prepend) = $mustprepend{$h};
253 printf ("Testing <$h>\n");
254 printf ("----------" . "-" x length ($h) . "\n");
256 # Generate a program to test for the availability of this header.
257 open (TESTFILE, ">$fnamebase.c");
258 print TESTFILE "$prepend";
259 print TESTFILE "#include <$h>\n";
262 $missing = compiletest ($fnamebase, "Checking whether <$h> is available",
263 "Header <$h> not available", 0);
267 open (CONTROL, "$CC -E -D$dialect - < data/$h-data |");
268 control: while (<CONTROL>) {
270 next control if (/^#/);
271 next control if (/^[ ]*$/);
273 if (/^element *({([^}]*)}|([^ ]*)) *({([^}]*)}|([^ ]*)) *([A-Za-z0-9_]*) *(.*)/) {
274 my($struct) = "$2$3";
280 # Remember that this name is allowed.
281 push @allow, $member;
283 # Generate a program to test for the availability of this member.
284 open (TESTFILE, ">$fnamebase.c");
285 print TESTFILE "$prepend";
286 print TESTFILE "#include <$h>\n";
287 print TESTFILE "$struct a;\n";
288 print TESTFILE "$struct b;\n";
289 print TESTFILE "extern void xyzzy (__typeof__ (&b.$member), __typeof__ (&a.$member), unsigned);\n";
290 print TESTFILE "void foobarbaz (void) {\n";
291 print TESTFILE " xyzzy (&a.$member, &b.$member, sizeof (a.$member));\n";
292 print TESTFILE "}\n";
295 $res = compiletest ($fnamebase, "Testing for member $member",
296 "Member \"$member\" not available.", $res);
299 # Test the types of the members.
300 open (TESTFILE, ">$fnamebase.c");
301 print TESTFILE "$prepend";
302 print TESTFILE "#include <$h>\n";
303 print TESTFILE "$struct a;\n";
304 print TESTFILE "extern $type b$rest;\n";
305 print TESTFILE "extern __typeof__ (a.$member) b;\n";
308 compiletest ($fnamebase, "Testing for type of member $member",
309 "Member \"$member\" does not have the correct type.", $res);
310 } elsif (/^constant *([a-zA-Z0-9_]*) *([A-Za-z0-9_]*)?/) {
315 # Remember that this name is allowed.
318 # Generate a program to test for the availability of this constant.
319 open (TESTFILE, ">$fnamebase.c");
320 print TESTFILE "$prepend";
321 print TESTFILE "#include <$h>\n";
322 print TESTFILE "__typeof__ ($const) a = $const;\n";
325 $res = compiletest ($fnamebase, "Testing for constant $const",
326 "Constant \"$const\" not available.", $res);
329 # Generate a program to test for the value of this constant.
330 open (TESTFILE, ">$fnamebase.c");
331 print TESTFILE "$prepend";
332 print TESTFILE "#include <$h>\n";
333 print TESTFILE "int main (void) { return $const != $value; }\n";
336 $res = runtest ($fnamebase, "Testing for value of constant $const",
337 "Constant \"$const\" has not the right value.", $res);
339 } elsif (/^typed-constant *([a-zA-Z0-9_]*) *({([^}]*)}|([^ ]*)) *([A-Za-z0-9_]*)?/) {
345 # Remember that this name is allowed.
348 # Generate a program to test for the availability of this constant.
349 open (TESTFILE, ">$fnamebase.c");
350 print TESTFILE "$prepend";
351 print TESTFILE "#include <$h>\n";
352 print TESTFILE "__typeof__ ($const) a = $const;\n";
355 $res = compiletest ($fnamebase, "Testing for constant $const",
356 "Constant \"$const\" not available.", $res);
358 # Test the types of the members.
359 open (TESTFILE, ">$fnamebase.c");
360 print TESTFILE "$prepend";
361 print TESTFILE "#include <$h>\n";
362 print TESTFILE "__typeof__ (($type) 0) a;\n";
363 print TESTFILE "extern __typeof__ ($const) a;\n";
366 compiletest ($fnamebase, "Testing for type of constant $const",
367 "Constant \"$const\" does not have the correct type.",
371 # Generate a program to test for the value of this constant.
372 open (TESTFILE, ">$fnamebase.c");
373 print TESTFILE "$prepend";
374 print TESTFILE "#include <$h>\n";
375 print TESTFILE "int main (void) { return $const != $value; }\n";
378 $res = runtest ($fnamebase, "Testing for value of constant $const",
379 "Constant \"$const\" has not the right value.", $res);
381 } elsif (/^type *({([^}]*)|([a-zA-Z0-9_]*))/) {
384 # Remember that this name is allowed.
385 if ($type =~ /^struct *(.*)/) {
387 } elsif ($type =~ /^union *(.*)/) {
393 # Remember that this name is allowed.
396 # Generate a program to test for the availability of this constant.
397 open (TESTFILE, ">$fnamebase.c");
398 print TESTFILE "$prepend";
399 print TESTFILE "#include <$h>\n";
400 print TESTFILE "$type *a;\n";
403 compiletest ($fnamebase, "Testing for type $type",
404 "Type \"$type\" not available.", $missing);
405 } elsif (/^function *({([^}]*)}|([a-zA-Z0-9_]*)) [(][*]([a-zA-Z0-9_]*) ([(].*[)])/) {
406 my($rettype) = "$2$3";
411 # Remember that this name is allowed.
414 # Generate a program to test for availability of this function.
415 open (TESTFILE, ">$fnamebase.c");
416 print TESTFILE "$prepend";
417 print TESTFILE "#include <$h>\n";
418 # print TESTFILE "#undef $fname\n";
419 print TESTFILE "$rettype (*(*foobarbaz) $args = $fname;\n";
422 $res = compiletest ($fnamebase, "Test availability of function $fname",
423 "Function \"$fname\" is not available.", $res);
425 # Generate a program to test for the type of this function.
426 open (TESTFILE, ">$fnamebase.c");
427 print TESTFILE "$prepend";
428 print TESTFILE "#include <$h>\n";
429 # print TESTFILE "#undef $fname\n";
430 print TESTFILE "extern $rettype (*(*foobarbaz) $args;\n";
431 print TESTFILE "extern __typeof__ (&$fname) foobarbaz;\n";
434 compiletest ($fnamebase, "Test for type of function $fname",
435 "Function \"$fname\" has incorrect type.", $res);
436 } elsif (/^function *({([^}]*)}|([a-zA-Z0-9_]*)) ([a-zA-Z0-9_]*) ([(].*[)])/) {
437 my($rettype) = "$2$3";
442 # Remember that this name is allowed.
445 # Generate a program to test for availability of this function.
446 open (TESTFILE, ">$fnamebase.c");
447 print TESTFILE "$prepend";
448 print TESTFILE "#include <$h>\n";
449 # print TESTFILE "#undef $fname\n";
450 print TESTFILE "$rettype (*foobarbaz) $args = $fname;\n";
453 $res = compiletest ($fnamebase, "Test availability of function $fname",
454 "Function \"$fname\" is not available.", $res);
456 # Generate a program to test for the type of this function.
457 open (TESTFILE, ">$fnamebase.c");
458 print TESTFILE "$prepend";
459 print TESTFILE "#include <$h>\n";
460 # print TESTFILE "#undef $fname\n";
461 print TESTFILE "extern $rettype (*foobarbaz) $args;\n";
462 print TESTFILE "extern __typeof__ (&$fname) foobarbaz;\n";
465 compiletest ($fnamebase, "Test for type of function $fname",
466 "Function \"$fname\" has incorrect type.", $res);
467 } elsif (/^variable *({([^}]*)}|([a-zA-Z0-9_]*)) ([a-zA-Z0-9_]*)/) {
472 # Remember that this name is allowed.
475 # Generate a program to test for availability of this function.
476 open (TESTFILE, ">$fnamebase.c");
477 print TESTFILE "$prepend";
478 print TESTFILE "#include <$h>\n";
479 # print TESTFILE "#undef $fname\n";
480 print TESTFILE "$type *foobarbaz = &$vname;\n";
483 $res = compiletest ($fnamebase, "Test availability of variable $vname",
484 "Variable \"$vname\" is not available.", $res);
486 # Generate a program to test for the type of this function.
487 open (TESTFILE, ">$fnamebase.c");
488 print TESTFILE "$prepend";
489 print TESTFILE "#include <$h>\n";
490 # print TESTFILE "#undef $fname\n";
491 print TESTFILE "extern $type $vname;\n";
494 compiletest ($fnamebase, "Test for type of variable $fname",
495 "Variable \"$vname\" has incorrect type.", $res);
496 } elsif (/^macro-function *({([^}]*)}|([a-zA-Z0-9_]*)) ([a-zA-Z0-9_]*) ([(].*[)])/) {
497 my($rettype) = "$2$3";
502 # Remember that this name is allowed.
505 # Generate a program to test for availability of this function.
506 open (TESTFILE, ">$fnamebase.c");
507 print TESTFILE "$prepend";
508 print TESTFILE "#include <$h>\n";
509 print TESTFILE "#ifndef $fname\n";
510 print TESTFILE "$rettype (*foobarbaz) $args = $fname;\n";
511 print TESTFILE "#endif\n";
514 $res = compiletest ($fnamebase, "Test availability of function $fname",
515 "Function \"$fname\" is not available.", $res);
517 # Generate a program to test for the type of this function.
518 open (TESTFILE, ">$fnamebase.c");
519 print TESTFILE "$prepend";
520 print TESTFILE "#include <$h>\n";
521 print TESTFILE "#ifndef $fname\n";
522 print TESTFILE "extern $rettype (*foobarbaz) $args;\n";
523 print TESTFILE "extern __typeof__ (&$fname) foobarbaz;\n";
524 print TESTFILE "#endif\n";
527 compiletest ($fnamebase, "Test for type of function $fname",
528 "Function \"$fname\" has incorrect type.", $res);
529 } elsif (/^macro *([^ ]*)/) {
532 # Remember that this name is allowed.
535 # Generate a program to test for availability of this macro.
536 open (TESTFILE, ">$fnamebase.c");
537 print TESTFILE "$prepend";
538 print TESTFILE "#include <$h>\n";
539 print TESTFILE "#ifndef $macro\n";
540 print TESTFILE "# error \"Macro $macro not defined\"\n";
541 print TESTFILE "#endif\n";
544 compiletest ($fnamebase, "Test availability of macro $macro",
545 "Macro \"$macro\" is not available.", $missing);
546 } elsif (/^allow *(.*)/) {
548 push @allow, $pattern;
550 } elsif (/^allow-header *(.*)/) {
552 push @allowheader, $pattern;
555 # printf ("line is `%s'\n", $_);
563 # Read the data files for the header files which are allowed to be included.
564 while ($#allowheader >= 0) {
565 my($ah) = pop @allowheader;
567 open (ALLOW, "$CC -E -D$dialect - < data/$ah-data |");
568 acontrol: while (<ALLOW>) {
569 next acontrol if (/^#/);
570 next acontrol if (/^[ ]*$/);
572 if (/^element *({([^}]*)}|([^ ]*)) *({([^}]*)}|([^ ]*)) *([A-Za-z0-9_]*) *(.*)/) {
574 } elsif (/^constant *([a-zA-Z0-9_]*) *([A-Za-z0-9_]*)?/) {
576 } elsif (/^typed-constant *([a-zA-Z0-9_]*) *({([^}]*)}|([^ ]*)) *([A-Za-z0-9_]*)?/) {
578 } elsif (/^type *({([^}]*)|([a-zA-Z0-9_]*))/) {
581 # Remember that this name is allowed.
582 if ($type =~ /^struct *(.*)/) {
584 } elsif ($type =~ /^union *(.*)/) {
589 } elsif (/^function *({([^}]*)}|([a-zA-Z0-9_]*)) [(][*]([a-zA-Z0-9_]*) ([(].*[)])/) {
591 } elsif (/^function *({([^}]*)}|([a-zA-Z0-9_]*)) ([a-zA-Z0-9_]*) ([(].*[)])/) {
593 } elsif (/^variable *({([^}]*)}|([a-zA-Z0-9_]*)) ([a-zA-Z0-9_]*)/) {
595 } elsif (/^macro-function *({([^}]*)}|([a-zA-Z0-9_]*)) ([a-zA-Z0-9_]*) ([(].*[)])/) {
597 } elsif (/^macro *([^ ]*)/) {
599 } elsif (/^allow *(.*)/) {
601 } elsif (/^allow-header *(.*)/) {
602 push @allowheader, $1;
608 # Now check the namespace.
609 printf (" Checking the namespace of \"%s\"... ", $h);
614 checknamespace ($h, $fnamebase, @allow);
620 printf "-" x 76 . "\n";
621 printf (" Total number of tests : %4d\n", $total);
622 printf (" Number of failed tests : %4d (%3d%%)\n", $errors, ($errors * 100) / $total);
623 printf (" Number of skipped tests: %4d (%3d%%)\n", $skipped, ($skipped * 100) / $total);