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/uio.h", "sys/types.h",
10 "sys/times.h", "sys/timeb.h", "sys/time.h", "sys/statvfs.h",
11 "sys/stat.h", "sys/shm.h", "sys/sem.h", "sys/resource.h",
12 "sys/msg.h", "sys/mman.h", "sys/ipc.h", "syslog.h",
13 "stropts.h", "strings.h", "string.h", "stdlib.h", "stdio.h",
14 "stddef.h", "stdarg.h", "signal.h", "setjmp.h", "semaphore.h",
15 "search.h", "sched.h", "regex.h", "pwd.h", "pthread.h",
16 "poll.h", "nl_types.h", "ndbm.h", "mqueue.h", "monetary.h",
17 "math.h", "locale.h", "libgen.h", "langinfo.h", "iso646.h",
18 "inttypes.h", "iconv.h", "grp.h", "glob.h", "ftw.h", "fnmatch.h",
19 "fmtmsg.h", "float.h", "fcntl.h", "errno.h", "dlfcn.h", "dirent.h",
20 "ctype.h", "cpio.h", "assert.h", "aio.h");
22 # These are the ISO C99 keywords.
23 @keywords = ('auto', 'break', 'case', 'char', 'const', 'continue', 'default',
24 'do', 'double', 'else', 'enum', 'extern', 'float', 'for', 'goto',
25 'if', 'inline', 'int', 'long', 'register', 'restrict', 'return',
26 'short', 'signed', 'sizeof', 'static', 'struct', 'switch',
27 'typedef', 'union', 'unsigned', 'void', 'volatile', 'while');
29 # Some headers need a bit more attention.
30 $mustprepend{'regex.h'} = "#include <sys/types.h>\n";
32 # Make an hash table from this information.
34 $iskeyword{pop (@keywords)} = 1;
53 my($pattern, $string) = @_;
54 my($strlen) = length ($string);
57 if (substr ($pattern, 0, 1) eq '*') {
58 my($patlen) = length ($pattern) - 1;
59 $res = ($strlen >= $patlen
60 && substr ($pattern, -$patlen, $patlen) eq substr ($string, -$patlen, $patlen));
61 } elsif (substr ($pattern, -1, 1) eq '*') {
62 my($patlen) = length ($pattern) - 1;
63 $res = ($strlen >= $patlen
64 && substr ($pattern, 0, $patlen) eq substr ($string, 0, $patlen));
66 $res = $pattern eq $string;
74 my($fnamebase, $msg, $errmsg, $skip) = @_;
85 $ret = system "$CC $CFLAGS -c $fnamebase.c -o $fnamebase.o > $fnamebase.out 2>&1";
89 printf (" $errmsg Compiler message:\n");
96 if ($verbose > 1 && -s "$fnamebase.out") {
97 # We print all warnings issued.
101 if ($printlog != 0) {
102 printf (" " . "-" x 71 . "\n");
103 open (MESSAGE, "< $fnamebase.out");
108 printf (" " . "-" x 71 . "\n");
111 unlink "$fnamebase.c";
112 unlink "$fnamebase.o";
113 unlink "$fnamebase.out";
121 my($fnamebase, $msg, $errmsg, $skip) = @_;
132 $ret = system "$CC $CFLAGS -o $fnamebase $fnamebase.c > $fnamebase.out 2>&1";
136 printf (" $errmsg Compiler message:\n");
142 # Now run the program. If the exit code is not zero something is wrong.
143 $result = system "$fnamebase > $fnamebase.out2 2>&1";
146 if ($verbose > 1 && -s "$fnamebase.out") {
147 # We print all warnings issued.
149 system "cat $fnamebase.out2 >> $fnamebase.out";
154 unlink "$fnamebase.out";
155 rename "$fnamebase.out2", "$fnamebase.out";
158 if ($printlog != 0) {
159 printf (" " . "-" x 71 . "\n");
160 open (MESSAGE, "< $fnamebase.out");
165 printf (" " . "-" x 71 . "\n");
169 unlink "$fnamebase.c";
170 unlink "$fnamebase.o";
171 unlink "$fnamebase.out";
172 unlink "$fnamebase.out2";
179 my($token, $nerrors, @allow) = @_;
182 if ($token =~ /^[0-9_]/ || $iskeyword{$token}) {
186 for ($idx = 0; $idx <= $#allow; ++$idx) {
187 if (poorfnmatch ($allow[$idx], $token)) {
194 printf ("FAIL\n " . "-" x 72 . "\n");
196 printf (" Namespace violation: \"%s\"\n", $token);
202 my($h, $fnamebase, @allow) = @_;
207 # Generate a program to get the contents of this header.
208 open (TESTFILE, ">$fnamebase.c");
209 print TESTFILE "#include <$h>\n";
212 open (CONTENT, "$CC $CFLAGS -E $fnamebase.c -Wp,-dN | sed -e '/^# [1-9]/d' -e '/^[[:space:]]*\$/d' |");
215 if (/^#define (.*)/) {
216 $nerrors = newtoken ($1, $nerrors, @allow);
218 # We have to tokenize the line.
221 my($len) = length ($str);
223 foreach $token (split(/[^a-zA-Z0-9_]/, $str)) {
225 $nerrors = newtoken ($token, $nerrors, @allow);
231 unlink "$fnamebase.c";
233 printf (" " . "-" x 72 . "\n");
241 while ($#headers >= 0) {
242 my($h) = pop (@headers);
245 my($fnamebase) = "$tmpdir/$hf-test";
248 my(@allowheader) = ();
249 my($prepend) = $mustprepend{$h};
251 printf ("Testing <$h>\n");
252 printf ("----------" . "-" x length ($h) . "\n");
254 # Generate a program to test for the availability of this header.
255 open (TESTFILE, ">$fnamebase.c");
256 print TESTFILE "$prepend";
257 print TESTFILE "#include <$h>\n";
260 $missing = compiletest ($fnamebase, "Checking whether <$h> is available",
261 "Header <$h> not available", 0);
265 open (CONTROL, "$CC -E -D$dialect - < data/$h-data |");
266 control: while (<CONTROL>) {
268 next control if (/^#/);
269 next control if (/^[ ]*$/);
271 if (/^element *({([^}]*)}|([^ ]*)) *({([^}]*)}|([^ ]*)) *([A-Za-z0-9_]*) *(.*)/) {
272 my($struct) = "$2$3";
278 # Remember that this name is allowed.
279 push @allow, $member;
281 # Generate a program to test for the availability of this member.
282 open (TESTFILE, ">$fnamebase.c");
283 print TESTFILE "$prepend";
284 print TESTFILE "#include <$h>\n";
285 print TESTFILE "$struct a;\n";
286 print TESTFILE "$struct b;\n";
287 print TESTFILE "extern void xyzzy (__typeof__ (&b.$member), __typeof__ (&a.$member), unsigned);\n";
288 print TESTFILE "void foobarbaz (void) {\n";
289 print TESTFILE " xyzzy (&a.$member, &b.$member, sizeof (a.$member));\n";
290 print TESTFILE "}\n";
293 $res = compiletest ($fnamebase, "Testing for member $member",
294 "Member \"$member\" not available.", $res);
297 # Test the types of the members.
298 open (TESTFILE, ">$fnamebase.c");
299 print TESTFILE "$prepend";
300 print TESTFILE "#include <$h>\n";
301 print TESTFILE "$struct a;\n";
302 print TESTFILE "extern $type b$rest;\n";
303 print TESTFILE "extern __typeof__ (a.$member) b;\n";
306 compiletest ($fnamebase, "Testing for type of member $member",
307 "Member \"$member\" does not have the correct type.", $res);
308 } elsif (/^constant *([a-zA-Z0-9_]*) *([A-Za-z0-9_]*)?/) {
313 # Remember that this name is allowed.
316 # Generate a program to test for the availability of this constant.
317 open (TESTFILE, ">$fnamebase.c");
318 print TESTFILE "$prepend";
319 print TESTFILE "#include <$h>\n";
320 print TESTFILE "__typeof__ ($const) a = $const;\n";
323 $res = compiletest ($fnamebase, "Testing for constant $const",
324 "Constant \"$const\" not available.", $res);
327 # Generate a program to test for the value of this constant.
328 open (TESTFILE, ">$fnamebase.c");
329 print TESTFILE "$prepend";
330 print TESTFILE "#include <$h>\n";
331 print TESTFILE "int main (void) { return $const != $value; }\n";
334 $res = runtest ($fnamebase, "Testing for value of constant $const",
335 "Constant \"$const\" has not the right value.", $res);
337 } elsif (/^typed-constant *([a-zA-Z0-9_]*) *({([^}]*)}|([^ ]*)) *([A-Za-z0-9_]*)?/) {
343 # Remember that this name is allowed.
346 # Generate a program to test for the availability of this constant.
347 open (TESTFILE, ">$fnamebase.c");
348 print TESTFILE "$prepend";
349 print TESTFILE "#include <$h>\n";
350 print TESTFILE "__typeof__ ($const) a = $const;\n";
353 $res = compiletest ($fnamebase, "Testing for constant $const",
354 "Constant \"$const\" not available.", $res);
356 # Test the types of the members.
357 open (TESTFILE, ">$fnamebase.c");
358 print TESTFILE "$prepend";
359 print TESTFILE "#include <$h>\n";
360 print TESTFILE "__typeof__ (($type) 0) a;\n";
361 print TESTFILE "extern __typeof__ ($const) a;\n";
364 compiletest ($fnamebase, "Testing for type of constant $const",
365 "Constant \"$const\" does not have the correct type.",
369 # Generate a program to test for the value of this constant.
370 open (TESTFILE, ">$fnamebase.c");
371 print TESTFILE "$prepend";
372 print TESTFILE "#include <$h>\n";
373 print TESTFILE "int main (void) { return $const != $value; }\n";
376 $res = runtest ($fnamebase, "Testing for value of constant $const",
377 "Constant \"$const\" has not the right value.", $res);
379 } elsif (/^type *({([^}]*)|([a-zA-Z0-9_]*))/) {
382 # Remember that this name is allowed.
383 if ($type =~ /^struct *(.*)/) {
385 } elsif ($type =~ /^union *(.*)/) {
391 # Remember that this name is allowed.
394 # Generate a program to test for the availability of this constant.
395 open (TESTFILE, ">$fnamebase.c");
396 print TESTFILE "$prepend";
397 print TESTFILE "#include <$h>\n";
398 print TESTFILE "$type *a;\n";
401 compiletest ($fnamebase, "Testing for type $type",
402 "Type \"$type\" not available.", $missing);
403 } elsif (/^function *({([^}]*)}|([a-zA-Z0-9_]*)) [(][*]([a-zA-Z0-9_]*) ([(].*[)])/) {
404 my($rettype) = "$2$3";
409 # Remember that this name is allowed.
412 # Generate a program to test for availability of this function.
413 open (TESTFILE, ">$fnamebase.c");
414 print TESTFILE "$prepend";
415 print TESTFILE "#include <$h>\n";
416 # print TESTFILE "#undef $fname\n";
417 print TESTFILE "$rettype (*(*foobarbaz) $args = $fname;\n";
420 $res = compiletest ($fnamebase, "Test availability of function $fname",
421 "Function \"$fname\" is not available.", $res);
423 # Generate a program to test for the type of this function.
424 open (TESTFILE, ">$fnamebase.c");
425 print TESTFILE "$prepend";
426 print TESTFILE "#include <$h>\n";
427 # print TESTFILE "#undef $fname\n";
428 print TESTFILE "extern $rettype (*(*foobarbaz) $args;\n";
429 print TESTFILE "extern __typeof__ (&$fname) foobarbaz;\n";
432 compiletest ($fnamebase, "Test for type of function $fname",
433 "Function \"$fname\" has incorrect type.", $res);
434 } elsif (/^function *({([^}]*)}|([a-zA-Z0-9_]*)) ([a-zA-Z0-9_]*) ([(].*[)])/) {
435 my($rettype) = "$2$3";
440 # Remember that this name is allowed.
443 # Generate a program to test for availability of this function.
444 open (TESTFILE, ">$fnamebase.c");
445 print TESTFILE "$prepend";
446 print TESTFILE "#include <$h>\n";
447 # print TESTFILE "#undef $fname\n";
448 print TESTFILE "$rettype (*foobarbaz) $args = $fname;\n";
451 $res = compiletest ($fnamebase, "Test availability of function $fname",
452 "Function \"$fname\" is not available.", $res);
454 # Generate a program to test for the type of this function.
455 open (TESTFILE, ">$fnamebase.c");
456 print TESTFILE "$prepend";
457 print TESTFILE "#include <$h>\n";
458 # print TESTFILE "#undef $fname\n";
459 print TESTFILE "extern $rettype (*foobarbaz) $args;\n";
460 print TESTFILE "extern __typeof__ (&$fname) foobarbaz;\n";
463 compiletest ($fnamebase, "Test for type of function $fname",
464 "Function \"$fname\" has incorrect type.", $res);
465 } elsif (/^variable *({([^}]*)}|([a-zA-Z0-9_]*)) ([a-zA-Z0-9_]*)/) {
470 # Remember that this name is allowed.
473 # Generate a program to test for availability of this function.
474 open (TESTFILE, ">$fnamebase.c");
475 print TESTFILE "$prepend";
476 print TESTFILE "#include <$h>\n";
477 # print TESTFILE "#undef $fname\n";
478 print TESTFILE "$type *foobarbaz = &$vname;\n";
481 $res = compiletest ($fnamebase, "Test availability of variable $vname",
482 "Variable \"$vname\" is not available.", $res);
484 # Generate a program to test for the type of this function.
485 open (TESTFILE, ">$fnamebase.c");
486 print TESTFILE "$prepend";
487 print TESTFILE "#include <$h>\n";
488 # print TESTFILE "#undef $fname\n";
489 print TESTFILE "extern $type $vname;\n";
492 compiletest ($fnamebase, "Test for type of variable $fname",
493 "Variable \"$vname\" has incorrect type.", $res);
494 } elsif (/^macro-function *({([^}]*)}|([a-zA-Z0-9_]*)) ([a-zA-Z0-9_]*) ([(].*[)])/) {
495 my($rettype) = "$2$3";
500 # Remember that this name is allowed.
503 # Generate a program to test for availability of this function.
504 open (TESTFILE, ">$fnamebase.c");
505 print TESTFILE "$prepend";
506 print TESTFILE "#include <$h>\n";
507 print TESTFILE "#ifndef $fname\n";
508 print TESTFILE "$rettype (*foobarbaz) $args = $fname;\n";
509 print TESTFILE "#endif\n";
512 $res = compiletest ($fnamebase, "Test availability of function $fname",
513 "Function \"$fname\" is not available.", $res);
515 # Generate a program to test for the type of this function.
516 open (TESTFILE, ">$fnamebase.c");
517 print TESTFILE "$prepend";
518 print TESTFILE "#include <$h>\n";
519 print TESTFILE "#ifndef $fname\n";
520 print TESTFILE "extern $rettype (*foobarbaz) $args;\n";
521 print TESTFILE "extern __typeof__ (&$fname) foobarbaz;\n";
522 print TESTFILE "#endif\n";
525 compiletest ($fnamebase, "Test for type of function $fname",
526 "Function \"$fname\" has incorrect type.", $res);
527 } elsif (/^macro *([^ ]*)/) {
530 # Remember that this name is allowed.
533 # Generate a program to test for availability of this macro.
534 open (TESTFILE, ">$fnamebase.c");
535 print TESTFILE "$prepend";
536 print TESTFILE "#include <$h>\n";
537 print TESTFILE "#ifndef $macro\n";
538 print TESTFILE "# error \"Macro $macro not defined\"\n";
539 print TESTFILE "#endif\n";
542 compiletest ($fnamebase, "Test availability of macro $macro",
543 "Macro \"$macro\" is not available.", $missing);
544 } elsif (/^allow *(.*)/) {
546 push @allow, $pattern;
548 } elsif (/^allow-header *(.*)/) {
550 push @allowheader, $pattern;
553 # printf ("line is `%s'\n", $_);
561 # Read the data files for the header files which are allowed to be included.
562 while ($#allowheader >= 0) {
563 my($ah) = pop @allowheader;
565 open (ALLOW, "$CC -E -D$dialect - < data/$ah-data |");
566 acontrol: while (<ALLOW>) {
567 next acontrol if (/^#/);
568 next acontrol if (/^[ ]*$/);
570 if (/^element *({([^}]*)}|([^ ]*)) *({([^}]*)}|([^ ]*)) *([A-Za-z0-9_]*) *(.*)/) {
572 } elsif (/^constant *([a-zA-Z0-9_]*) *([A-Za-z0-9_]*)?/) {
574 } elsif (/^typed-constant *([a-zA-Z0-9_]*) *({([^}]*)}|([^ ]*)) *([A-Za-z0-9_]*)?/) {
576 } elsif (/^type *({([^}]*)|([a-zA-Z0-9_]*))/) {
579 # Remember that this name is allowed.
580 if ($type =~ /^struct *(.*)/) {
582 } elsif ($type =~ /^union *(.*)/) {
587 } elsif (/^function *({([^}]*)}|([a-zA-Z0-9_]*)) [(][*]([a-zA-Z0-9_]*) ([(].*[)])/) {
589 } elsif (/^function *({([^}]*)}|([a-zA-Z0-9_]*)) ([a-zA-Z0-9_]*) ([(].*[)])/) {
591 } elsif (/^variable *({([^}]*)}|([a-zA-Z0-9_]*)) ([a-zA-Z0-9_]*)/) {
593 } elsif (/^macro-function *({([^}]*)}|([a-zA-Z0-9_]*)) ([a-zA-Z0-9_]*) ([(].*[)])/) {
595 } elsif (/^macro *([^ ]*)/) {
597 } elsif (/^allow *(.*)/) {
599 } elsif (/^allow-header *(.*)/) {
600 push @allowheader, $1;
606 # Now check the namespace.
607 printf (" Checking the namespace of \"%s\"... ", $h);
612 checknamespace ($h, $fnamebase, @allow);
618 printf "-" x 76 . "\n";
619 printf (" Total number of tests : %4d\n", $total);
620 printf (" Number of failed tests : %4d (%3d%%)\n", $errors, ($errors * 100) / $total);
621 printf (" Number of skipped tests: %4d (%3d%%)\n", $skipped, ($skipped * 100) / $total);