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