test-suite: integrate unhandled proprocessor tests
[smatch.git] / cgcc
blobfdda6d17193ea9c95b0aa9ae491fd67edcd4b169
1 #!/usr/bin/perl -w
2 # -----------------------------------------------------------------------------
4 my $cc = $ENV{'REAL_CC'} || 'cc';
5 my $check = $ENV{'CHECK'} || 'sparse';
7 my $m32 = 0;
8 my $m64 = 0;
9 my $has_specs = 0;
10 my $gendeps = 0;
11 my $do_check = 0;
12 my $do_compile = 1;
13 my $gcc_base_dir;
14 my $verbose = 0;
16 while (@ARGV) {
17 $_ = shift(@ARGV);
18 # Look for a .c file. We don't want to run the checker on .o or .so files
19 # in the link run. (This simplistic check knows nothing about options
20 # with arguments, but it seems to do the job.)
21 $do_check = 1 if /^[^-].*\.c$/;
23 # Ditto for stdin.
24 $do_check = 1 if $_ eq '-';
26 $m32 = 1 if /^-m32$/;
27 $m64 = 1 if /^-m64$/;
28 $gendeps = 1 if /^-M$/;
30 if (/^-target=(.*)$/) {
31 $check .= &add_specs ($1);
32 $has_specs = 1;
33 next;
36 if ($_ eq '-no-compile') {
37 $do_compile = 0;
38 next;
41 if (/^-gcc-base-dir$/) {
42 $gcc_base_dir = shift @ARGV;
43 die ("$0: missing argument for -gcc-base-dir option") if !$gcc_base_dir;
44 next;
47 # If someone adds "-E", don't pre-process twice.
48 $do_compile = 0 if $_ eq '-E';
50 $verbose = 1 if $_ eq '-v';
52 my $this_arg = ' ' . &quote_arg ($_);
53 $cc .= $this_arg unless &check_only_option ($_);
54 $check .= $this_arg unless &cc_only_option ($_);
57 if ($gendeps) {
58 $do_compile = 1;
59 $do_check = 0;
62 if ($do_check) {
63 if (!$has_specs) {
64 $check .= &add_specs ('host_arch_specs');
65 $check .= &add_specs ('host_os_specs');
68 $gcc_base_dir = qx($cc -print-file-name=) if !$gcc_base_dir;
69 $check .= " -gcc-base-dir " . $gcc_base_dir if $gcc_base_dir;
71 print "$check\n" if $verbose;
72 if ($do_compile) {
73 system ($check);
74 } else {
75 exec ($check);
79 if ($do_compile) {
80 print "$cc\n" if $verbose;
81 exec ($cc);
84 exit 0;
86 # -----------------------------------------------------------------------------
87 # Check if an option is for "check" only.
89 sub check_only_option {
90 my ($arg) = @_;
91 return 1 if $arg =~ /^-W(no-?)?(default-bitfield-sign|one-bit-signed-bitfield|cast-truncate|bitwise|typesign|context|undef|ptr-subtraction-blows|cast-to-as|decl|transparent-union|address-space|enum-mismatch|do-while|old-initializer|non-pointer-null|paren-string|return-void)$/;
92 return 1 if $arg =~ /^-v(no-?)?(entry|dead)$/;
93 return 0;
96 # -----------------------------------------------------------------------------
97 # Check if an option is for "cc" only.
99 sub cc_only_option {
100 my ($arg) = @_;
101 # -Wall turns on all Sparse warnings, including experimental and noisy
102 # ones. Don't include it just because a project wants to pass -Wall to cc.
103 # If you really want cgcc to run sparse with -Wall, use
104 # CHECK="sparse -Wall".
105 return 1 if $arg =~ /^-Wall$/;
106 return 0;
109 # -----------------------------------------------------------------------------
110 # Simple arg-quoting function. Just adds backslashes when needed.
112 sub quote_arg {
113 my ($arg) = @_;
114 return "''" if $arg eq '';
115 return join ('',
116 map {
117 m|^[-a-zA-Z0-9._/,=]+$| ? $_ : "\\" . $_;
118 } (split (//, $arg)));
121 # -----------------------------------------------------------------------------
123 sub integer_types {
124 my ($char,@dummy) = @_;
126 my %pow2m1 =
127 (8 => '127',
128 16 => '32767',
129 32 => '2147483647',
130 64 => '9223372036854775807',
132 my @types = (['SCHAR',''], ['SHRT',''], ['INT',''], ['LONG','L'], ['LONG_LONG','LL']);
134 my $result = " -D__CHAR_BIT__=$char";
135 while (@types) {
136 my $bits = shift @_;
137 my ($name,$suffix) = @{ shift @types };
138 die "$0: weird number of bits." unless exists $pow2m1{$bits};
139 $result .= " -D__${name}_MAX__=" . $pow2m1{$bits} . $suffix;
141 return $result;
144 # -----------------------------------------------------------------------------
146 sub float_types {
147 my ($has_inf,$has_qnan,$dec_dig,@bitsizes) = @_;
148 my $result = " -D__FLT_RADIX__=2";
149 $result .= " -D__FINITE_MATH_ONLY__=" . ($has_inf || $has_qnan ? '0' : '1');
150 $result .= " -D__DECIMAL_DIG__=$dec_dig";
152 my %constants =
153 (24 =>
155 'MIN' => '1.17549435e-38',
156 'MAX' => '3.40282347e+38',
157 'EPSILON' => '1.19209290e-7',
158 'DENORM_MIN' => '1.40129846e-45',
160 53 =>
162 'MIN' => '2.2250738585072014e-308',
163 'MAX' => '1.7976931348623157e+308',
164 'EPSILON' => '2.2204460492503131e-16',
165 'DENORM_MIN' => '4.9406564584124654e-324',
167 64 =>
169 'MIN' => '3.36210314311209350626e-4932',
170 'MAX' => '1.18973149535723176502e+4932',
171 'EPSILON' => '1.08420217248550443401e-19',
172 'DENORM_MIN' => '3.64519953188247460253e-4951',
174 113 =>
176 'MIN' => '3.36210314311209350626267781732175260e-4932',
177 'MAX' => '1.18973149535723176508575932662800702e+4932',
178 'EPSILON' => '1.92592994438723585305597794258492732e-34',
179 'DENORM_MIN' => '6.47517511943802511092443895822764655e-4966',
183 my @types = (['FLT','F'], ['DBL',''], ['LDBL','L']);
184 while (@types) {
185 my ($mant_bits,$exp_bits) = @{ shift @bitsizes };
186 my ($name,$suffix) = @{ shift @types };
188 my $h = $constants{$mant_bits};
189 die "$0: weird number of mantissa bits." unless $h;
191 my $mant_dig = int (($mant_bits - 1) * log (2) / log (10));
192 my $max_exp = 1 << ($exp_bits - 1);
193 my $min_exp = 3 - $max_exp;
194 my $max_10_exp = int ($max_exp * log (2) / log (10));
195 my $min_10_exp = -int (-$min_exp * log (2) / log (10));
197 $result .= " -D__${name}_MANT_DIG__=$mant_bits";
198 $result .= " -D__${name}_DIG__=$mant_dig";
199 $result .= " -D__${name}_MIN_EXP__='($min_exp)'";
200 $result .= " -D__${name}_MAX_EXP__=$max_exp";
201 $result .= " -D__${name}_MIN_10_EXP__='($min_10_exp)'";
202 $result .= " -D__${name}_MAX_10_EXP__=$max_10_exp";
203 $result .= " -D__${name}_HAS_INFINITY__=" . ($has_inf ? '1' : '0');
204 $result .= " -D__${name}_HAS_QUIET_NAN__=" . ($has_qnan ? '1' : '0');;
206 foreach my $inf (sort keys %$h) {
207 $result .= " -D__${name}_${inf}__=" . $h->{$inf} . $suffix;
210 return $result;
213 # -----------------------------------------------------------------------------
215 sub define_size_t {
216 my ($text) = @_;
217 # We have to undef in order to override check's internal definition.
218 return ' -U__SIZE_TYPE__ ' . &quote_arg ("-D__SIZE_TYPE__=$text");
221 # -----------------------------------------------------------------------------
223 sub add_specs {
224 my ($spec) = @_;
225 if ($spec eq 'sunos') {
226 return &add_specs ('unix') .
227 ' -D__sun__=1 -D__sun=1 -Dsun=1' .
228 ' -D__svr4__=1 -DSVR4=1' .
229 ' -D__STDC__=0' .
230 ' -D_REENTRANT' .
231 ' -D_SOLARIS_THREADS' .
232 ' -DNULL="((void *)0)"';
233 } elsif ($spec eq 'linux') {
234 return &add_specs ('unix') .
235 ' -D__linux__=1 -D__linux=1 -Dlinux=linux';
236 } elsif ($spec eq 'openbsd') {
237 return &add_specs ('unix') .
238 ' -D__OpenBSD__=1';
239 } elsif ($spec eq 'unix') {
240 return ' -Dunix=1 -D__unix=1 -D__unix__=1';
241 } elsif ( $spec =~ /^cygwin/) {
242 return &add_specs ('unix') .
243 ' -D__CYGWIN__=1 -D__CYGWIN32__=1' .
244 " -D'_cdecl=__attribute__((__cdecl__))'" .
245 " -D'__cdecl=__attribute__((__cdecl__))'" .
246 " -D'_stdcall=__attribute__((__stdcall__))'" .
247 " -D'__stdcall=__attribute__((__stdcall__))'" .
248 " -D'_fastcall=__attribute__((__fastcall__))'" .
249 " -D'__fastcall=__attribute__((__fastcall__))'" .
250 " -D'__declspec(x)=__attribute__((x))'";
251 } elsif ($spec eq 'i86') {
252 return (' -Di386=1 -D__i386=1 -D__i386__=1' .
253 &integer_types (8, 16, 32, $m64 ? 64 : 32, 64) .
254 &float_types (1, 1, 21, [24,8], [53,11], [64,15]) .
255 &define_size_t ($m64 ? "long unsigned int" : "unsigned int"));
256 } elsif ($spec eq 'sparc') {
257 return (' -Dsparc=1 -D__sparc=1 -D__sparc__=1' .
258 &integer_types (8, 16, 32, $m64 ? 64 : 32, 64) .
259 &float_types (1, 1, 33, [24,8], [53,11], [113,15]) .
260 &define_size_t ($m64 ? "long unsigned int" : "unsigned int"));
261 } elsif ($spec eq 'sparc64') {
262 return (' -Dsparc=1 -D__sparc=1 -D__sparc__=1 -D__sparcv9__=1 -D__sparc64__=1 -D__arch64__=1 -D__LP64__=1' .
263 &integer_types (8, 16, 32, 64, 64) .
264 &float_types (1, 1, 33, [24,8], [53,11], [113,15]) .
265 &define_size_t ("long unsigned int"));
266 } elsif ($spec eq 'x86_64') {
267 return (' -Dx86_64=1 -D__x86_64=1 -D__x86_64__=1' .
268 &integer_types (8, 16, 32, $m32 ? 32 : 64, 64) .
269 &float_types (1, 1, 33, [24,8], [53,11], [113,15]) .
270 &define_size_t ($m32 ? "unsigned int" : "long unsigned int"));
271 } elsif ($spec eq 'ppc') {
272 return (' -D__powerpc__=1 -D_BIG_ENDIAN -D_STRING_ARCH_unaligned=1' .
273 &integer_types (8, 16, 32, $m64 ? 64 : 32, 64) .
274 &float_types (1, 1, 21, [24,8], [53,11], [113,15]) .
275 &define_size_t ($m64 ? "long unsigned int" : "unsigned int"));
276 } elsif ($spec eq 'host_os_specs') {
277 my $os = `uname -s`;
278 chomp $os;
279 return &add_specs (lc $os);
280 } elsif ($spec eq 'host_arch_specs') {
281 my $arch = `uname -m`;
282 chomp $arch;
283 if ($arch =~ /^(i.?86|athlon)$/i) {
284 return &add_specs ('i86');
285 } elsif ($arch =~ /^(sun4u)$/i) {
286 return &add_specs ('sparc');
287 } elsif ($arch =~ /^(x86_64)$/i) {
288 return &add_specs ('x86_64');
289 } elsif ($arch =~ /^(ppc)$/i) {
290 return &add_specs ('ppc');
291 } elsif ($arch =~ /^(sparc64)$/i) {
292 return &add_specs ('sparc64');
294 } else {
295 die "$0: invalid specs: $spec\n";
299 # -----------------------------------------------------------------------------