Update.
[platform/upstream/glibc.git] / conform / conformtest.pl
1 #! /usr/bin/perl
2
3 $CC = "gcc";
4 $CFLAGS = "-I. '-D__attribute__(x)=' -D_XOPEN_SOURCE=500";
5
6 # List of the headers we are testing.
7 @headers = ("poll.h", "nl_types.h", "ndbm.h", "mqueue.h", "monetary.h",
8             "math.h", "locale.h", "libgen.h", "langinfo.h", "iso646.h",
9             "inttypes.h", "iconv.h", "grp.h", "glob.h", "ftw.h", "fnmatch.h",
10             "fmtmsg.h", "float.h", "fcntl.h", "errno.h", "dlfcn.h", "dirent.h",
11             "ctype.h", "cpio.h", "assert.h", "aio.h");
12
13 # These are the ISO C9x keywords.
14 @keywords = ('auto', 'break', 'case', 'char', 'const', 'continue', 'default',
15              'do', 'double', 'else', 'enum', 'extern', 'float', 'for', 'goto',
16              'if', 'inline', 'int', 'long', 'register', 'restrict', 'return',
17              'short', 'signed', 'sizeof', 'static', 'struct', 'switch',
18              'typedef', 'union', 'unsigned', 'void', 'volatile', 'while');
19
20 # Make an hash table from this information.
21 while ($#keywords) {
22   $iskeyword{pop (@keywords)} = 1;
23 }
24
25 $tmpdir = "/tmp";
26
27 $verbose = 1;
28
29 $total = 0;
30 $skipped = 0;
31 $errors = 0;
32
33 #$dialect = "ISO";
34 #$dialect = "POSIX";
35 #$dialect = "XPG3";
36 #$dialect = "XPG4";
37 $dialect = "UNIX98";
38
39
40 sub poorfnmatch {
41   my($pattern, $string) = @_;
42   my($strlen) = length ($string);
43   my($res);
44
45   if (substr ($pattern, 0, 1) eq '*') {
46     my($patlen) = length ($pattern) - 1;
47     $res = ($strlen >= $patlen
48             && substr ($pattern, -$patlen, $patlen) eq substr ($string, -$patlen, $patlen));
49   } elsif (substr ($pattern, -1, 1) eq '*') {
50     my($patlen) = length ($pattern) - 1;
51     $res = ($strlen >= $patlen
52             && substr ($pattern, 0, $patlen) eq substr ($string, 0, $patlen));
53   } else {
54     $res = $pattern eq $string;
55   }
56   return $res;
57 }
58
59
60 sub compiletest
61 {
62   my($fnamebase, $msg, $errmsg, $skip) = @_;
63   my($result) = $skip;
64   my($printlog) = 0;
65
66   ++$total;
67   printf ("  $msg...");
68
69   if ($skip != 0) {
70     ++$skipped;
71     printf (" SKIP\n");
72   } else {
73     $ret = system "$CC $CFLAGS -c $fnamebase.c -o $fnamebase.o > $fnamebase.out 2>&1";
74     if ($ret != 0) {
75       printf (" FAIL\n");
76       if ($verbose != 0) {
77         printf ("    $errmsg  Compiler message:\n");
78         $printlog = 1;
79       }
80       ++$errors;
81       $result = 1;
82     } else {
83       printf (" OK\n");
84       if ($verbose > 1 && -s "$fnamebase.out") {
85         # We print all warnings issued.
86         $printlog = 1;
87       }
88     }
89     if ($printlog != 0) {
90       printf ("    " . "-" x 71 . "\n");
91       open (MESSAGE, "< $fnamebase.out");
92       while (<MESSAGE>) {
93         printf ("    %s", $_);
94       }
95       close (MESSAGE);
96       printf ("    " . "-" x 71 . "\n");
97     }
98   }
99   unlink "$fnamebase.c";
100   unlink "$fnamebase.o";
101   unlink "$fnamebase.out";
102
103   $result;
104 }
105
106
107 sub runtest
108 {
109   my($fnamebase, $msg, $errmsg, $skip) = @_;
110   my($result) = $skip;
111   my($printlog) = 0;
112
113   ++$total;
114   printf ("  $msg...");
115
116   if ($skip != 0) {
117     ++$skipped;
118     printf (" SKIP\n");
119   } else {
120     $ret = system "$CC $CFLAGS -o $fnamebase $fnamebase.c > $fnamebase.out 2>&1";
121     if ($ret != 0) {
122       printf (" FAIL\n");
123       if ($verbose != 0) {
124         printf ("    $errmsg  Compiler message:\n");
125         $printlog = 1;
126       }
127       ++$errors;
128       $result = 1;
129     } else {
130       # Now run the program.  If the exit code is not zero something is wrong.
131       $result = system "$fnamebase > $fnamebase.out2 2>&1";
132       if ($result == 0) {
133         printf (" OK\n");
134         if ($verbose > 1 && -s "$fnamebase.out") {
135           # We print all warnings issued.
136           $printlog = 1;
137           system "cat $fnamebase.out2 >> $fnamebase.out";
138         }
139       } else {
140         printf (" FAIL\n");
141         $printlog = 1;
142         unlink "$fnamebase.out";
143         rename "$fnamebase.out2", "$fnamebase.out";
144       }
145     }
146     if ($printlog != 0) {
147       printf ("    " . "-" x 71 . "\n");
148       open (MESSAGE, "< $fnamebase.out");
149       while (<MESSAGE>) {
150         printf ("    %s", $_);
151       }
152       close (MESSAGE);
153       printf ("    " . "-" x 71 . "\n");
154     }
155   }
156   unlink "$fnamebase";
157   unlink "$fnamebase.c";
158   unlink "$fnamebase.o";
159   unlink "$fnamebase.out";
160   unlink "$fnamebase.out2";
161
162   $result;
163 }
164
165
166 sub newtoken {
167   my($token, $nerrors, @allow) = @_;
168   my($idx);
169
170   for ($idx = 0; $idx <= $#allow; ++$idx) {
171     if ($token =~ /^[0-9_]/ || $iskeyword{$token} || poorfnmatch ($allow[$idx], $token)) {
172       return $nerrors;
173     }
174   }
175
176   ++$nerrors;
177   if ($nerrors == 1) {
178     printf ("FAIL\n    " . "-" x 72 . "\n");
179   }
180   printf ("    Namespace violation: \"%s\"\n", $token);
181   return $nerrors;
182 }
183
184
185 sub checknamespace {
186   my($h, $fnamebase, @allow) = @_;
187   my($nerrors) = 0;
188
189   ++$total;
190
191   # Generate a program to get the contents of this header.
192   open (TESTFILE, ">$fnamebase.c");
193   print TESTFILE "#include <$h>\n";
194   close (TESTFILE);
195
196   open (CONTENT, "$CC $CFLAGS -E $fnamebase.c -Wp,-dN | sed -e '/^# [1-9]/d' -e '/^[[:space:]]*\$/d' |");
197   while (<CONTENT>) {
198     chop;
199     if (/^#define (.*)/) {
200       $nerrors = newtoken ($1, $nerrors, @allow);
201     } else {
202       # We have to tokenize the line.
203       my($str) = $_;
204       my($index) = 0;
205       my($len) = length ($str);
206
207       foreach $token (split(/[^a-zA-Z0-9_]/, $str)) {
208         if ($token ne "") {
209           $nerrors = newtoken ($token, $nerrors, @allow);
210         }
211       }
212     }
213   }
214   close (CONTENT);
215   unlink "$fnamebase.c";
216   if ($nerrors != 0) {
217     printf ("    " . "-" x 72 . "\n");
218     ++$errors;
219   } else {
220     printf ("OK\n");
221   }
222 }
223
224
225 while ($#headers >= 0) {
226   my($h) = pop (@headers);
227   my($fnamebase) = "$tmpdir/$h-test";
228   my($missing);
229   my(@allow) = ();
230
231   printf ("Testing <$h>\n");
232   printf ("----------" . "-" x length ($h) . "\n");
233
234   # Generate a program to test for the availability of this header.
235   open (TESTFILE, ">$fnamebase.c");
236   print TESTFILE "#include <$h>\n";
237   close (TESTFILE);
238
239   $missing = compiletest ($fnamebase, "Checking whether <$h> is available",
240                           "Header <$h> not available", 0);
241
242   printf ("\n");
243
244   open (CONTROL, "$CC -E -D$dialect - < data/$h-data |");
245   control: while (<CONTROL>) {
246     chop;
247     next control if (/^#/);
248     next control if (/^[        ]*$/);
249
250     if (/^element *({([^}]*)}|([^ ]*)) *({([^}]*)}|([^ ]*)) *([A-Za-z0-9_]*) *(.*)/) {
251       my($struct) = "$2$3";
252       my($type) = "$5$6";
253       my($member) = "$7";
254       my($rest) = "$8";
255       my($res) = $missing;
256
257       # Remember that this name is allowed.
258       push @allow, $member;
259
260       # Generate a program to test for the availability of this member.
261       open (TESTFILE, ">$fnamebase.c");
262       print TESTFILE "#include <$h>\n";
263       print TESTFILE "$struct a;\n";
264       print TESTFILE "$struct b;\n";
265       print TESTFILE "extern void xyzzy (__typeof__ (&b.$member), __typeof__ (&a.$member), unsigned);\n";
266       print TESTFILE "void foobarbaz (void) {\n";
267       print TESTFILE "  xyzzy (&a.$member, &b.$member, sizeof (a.$member));\n";
268       print TESTFILE "}\n";
269       close (TESTFILE);
270
271       $res = compiletest ($fnamebase, "Testing for member $member",
272                           "Member \"$member\" not available.", $res);
273
274
275       # Test the types of the members.
276       open (TESTFILE, ">$fnamebase.c");
277       print TESTFILE "#include <$h>\n";
278       print TESTFILE "$struct a;\n";
279       print TESTFILE "extern $type b$rest;\n";
280       print TESTFILE "extern __typeof__ (a.$member) b;\n";
281       close (TESTFILE);
282
283       compiletest ($fnamebase, "Testing for type of member $member",
284                    "Member \"$member\" does not have the correct type.", $res);
285     } elsif (/^constant *([a-zA-Z0-9_]*) *([A-Za-z0-9_]*)?/) {
286       my($const) = $1;
287       my($value) = $2;
288       my($res) = $missing;
289
290       # Remember that this name is allowed.
291       push @allow, $const;
292
293       # Generate a program to test for the availability of this constant.
294       open (TESTFILE, ">$fnamebase.c");
295       print TESTFILE "#include <$h>\n";
296       print TESTFILE "__typeof__ ($const) a = $const;\n";
297       close (TESTFILE);
298
299       $res = compiletest ($fnamebase, "Testing for constant $const",
300                           "Constant \"$const\" not available.", $res);
301
302       if ($value ne "") {
303         # Generate a program to test for the value of this constant.
304         open (TESTFILE, ">$fnamebase.c");
305         print TESTFILE "#include <$h>\n";
306         print TESTFILE "int main (void) { return $const != $value; }\n";
307         close (TESTFILE);
308
309         $res = runtest ($fnamebase, "Testing for value of constant $const",
310                         "Constant \"$const\" has not the right value.", $res);
311       }
312     } elsif (/^type *({([^}]*)|([a-zA-Z0-9_]*))/) {
313       my($type) = "$2$3";
314
315       # Remember that this name is allowed.
316       if ($type =~ /^struct *(.*)/) {
317         push @allow, $1;
318       } elsif ($type =~ /^union *(.*)/) {
319         push @allow, $1;
320       } else {
321         push @allow, $type;
322       }
323
324       # Remember that this name is allowed.
325       push @allow, $type;
326
327       # Generate a program to test for the availability of this constant.
328       open (TESTFILE, ">$fnamebase.c");
329       print TESTFILE "#include <$h>\n";
330       print TESTFILE "$type *a;\n";
331       close (TESTFILE);
332
333       compiletest ($fnamebase, "Testing for type $type",
334                    "Type \"$type\" not available.", $missing);
335     } elsif (/^function *({([^}]*)}|([a-zA-Z0-9_]*)) ([a-zA-Z0-9_]*) ([(][^)]*[)])/) {
336       my($rettype) = "$2$3";
337       my($fname) = "$4";
338       my($args) = "$5";
339       my($res) = $missing;
340
341       # Remember that this name is allowed.
342       push @allow, $fname;
343
344       # Generate a program to test for availability of this function.
345       open (TESTFILE, ">$fnamebase.c");
346       print TESTFILE "#include <$h>\n";
347       # print TESTFILE "#undef $fname\n";
348       print TESTFILE "$rettype (*foobarbaz) $args = $fname;\n";
349       close (TESTFILE);
350
351       $res = compiletest ($fnamebase, "Test availability of function $fname",
352                           "Function \"$fname\" is not available.", $res);
353
354       # Generate a program to test for the type of this function.
355       open (TESTFILE, ">$fnamebase.c");
356       print TESTFILE "#include <$h>\n";
357       # print TESTFILE "#undef $fname\n";
358       print TESTFILE "extern $rettype (*foobarbaz) $args;\n";
359       print TESTFILE "extern __typeof__ (&$fname) foobarbaz;\n";
360       close (TESTFILE);
361
362       compiletest ($fnamebase, "Test for type of function $fname",
363                    "Function \"$fname\" has incorrect type.", $res);
364     } elsif (/^macro *([^       ]*)/) {
365       my($macro) = "$1";
366
367       # Remember that this name is allowed.
368       push @allow, $macro;
369
370       # Generate a program to test for availability of this macro.
371       open (TESTFILE, ">$fnamebase.c");
372       print TESTFILE "#include <$h>\n";
373       print TESTFILE "#ifndef $macro\n";
374       print TESTFILE "# error \"Macro $macro not defined\"\n";
375       print TESTFILE "#endif\n";
376       close (TESTFILE);
377
378       compiletest ($fnamebase, "Test availability of macro $macro",
379                    "Macro \"$macro\" is not available.", $missing);
380     } elsif (/^allow *(.*)/) {
381       my($pattern) = $1;
382       push @allow, $pattern;
383       next control;
384     } else {
385       # printf ("line is `%s'\n", $_);
386       next control;
387     }
388
389     printf ("\n");
390   }
391   close (CONTROL);
392
393   # Now check the namespace.
394   printf ("  Checking the namespace of \"%s\"... ", $h);
395   if ($missing) {
396     ++$skipped;
397     printf ("SKIP\n");
398   } else {
399     checknamespace ($h, $fnamebase, @allow);
400   }
401
402   printf ("\n\n");
403 }
404
405 printf "-" x 76 . "\n";
406 printf ("  Total number of tests  : %4d\n", $total);
407 printf ("  Number of failed tests : %4d (%3d%%)\n", $errors, ($errors * 100) / $total);
408 printf ("  Number of skipped tests: %4d (%3d%%)\n", $skipped, ($skipped * 100) / $total);
409
410 exit $errors != 0;