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