fix evaluation of a function or array symbol in conditionals
[smatch.git] / cgcc
blobc29fa587ea8abe2dfae04d466ade12c02c43ea30
1 #!/usr/bin/perl -w
2 # -----------------------------------------------------------------------------
4 my $cc = $ENV{'REAL_CC'} || 'cc';
5 my $check = $ENV{'CHECK'} || 'sparse';
6 my $ccom = $cc;
8 my $m32 = 0;
9 my $m64 = 0;
10 my $has_specs = 0;
11 my $gendeps = 0;
12 my $do_check = 0;
13 my $do_compile = 1;
14 my $gcc_base_dir;
15 my $multiarch_dir;
16 my $verbose = 0;
18 while (@ARGV) {
19 $_ = shift(@ARGV);
20 # Look for a .c file. We don't want to run the checker on .o or .so files
21 # in the link run. (This simplistic check knows nothing about options
22 # with arguments, but it seems to do the job.)
23 $do_check = 1 if /^[^-].*\.c$/;
25 # Ditto for stdin.
26 $do_check = 1 if $_ eq '-';
28 $m32 = 1 if /^-m32$/;
29 $m64 = 1 if /^-m64$/;
30 $gendeps = 1 if /^-M$/;
32 if (/^-target=(.*)$/) {
33 $check .= &add_specs ($1);
34 $has_specs = 1;
35 next;
38 if ($_ eq '-no-compile') {
39 $do_compile = 0;
40 next;
43 if (/^-gcc-base-dir$/) {
44 $gcc_base_dir = shift @ARGV;
45 die ("$0: missing argument for -gcc-base-dir option") if !$gcc_base_dir;
46 next;
49 if (/^-multiarch-dir$/) {
50 $multiarch_dir = shift @ARGV;
51 die ("$0: missing argument for -multiarch-dir option") if !$multiarch_dir;
52 next;
55 # If someone adds "-E", don't pre-process twice.
56 $do_compile = 0 if $_ eq '-E';
58 $verbose = 1 if $_ eq '-v';
60 my $this_arg = ' ' . &quote_arg ($_);
61 $cc .= $this_arg unless &check_only_option ($_);
62 $check .= $this_arg;
65 if ($gendeps) {
66 $do_compile = 1;
67 $do_check = 0;
70 if ($do_check) {
71 if (!$has_specs) {
72 $check .= &add_specs ('host_arch_specs');
73 $check .= &add_specs ('host_os_specs');
76 $gcc_base_dir = qx($ccom -print-file-name=) if !$gcc_base_dir;
77 chomp($gcc_base_dir); # possibly remove '\n' from compiler
78 $check .= " -gcc-base-dir " . $gcc_base_dir if $gcc_base_dir;
80 $multiarch_dir = qx($ccom -print-multiarch) if ! defined $multiarch_dir;
81 chomp($multiarch_dir); # possibly remove '\n' from compiler
82 $check .= " -multiarch-dir " . $multiarch_dir if $multiarch_dir;
84 print "$check\n" if $verbose;
85 if ($do_compile) {
86 system ($check) == 0 or exit 1;
87 } else {
88 exec ($check);
92 if ($do_compile) {
93 print "$cc\n" if $verbose;
94 exec ($cc);
97 exit 0;
99 # -----------------------------------------------------------------------------
100 # Check if an option is for "check" only.
102 sub check_only_option {
103 my ($arg) = @_;
104 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|designated-init|sparse-all|sparse-error)$/;
105 return 1 if $arg =~ /^-v(no-?)?(entry|dead)$/;
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',
131 128 => '170141183460469231731687303715884105727',
133 my @types = (['SCHAR',''], ['SHRT',''], ['INT',''], ['LONG','L'], ['LONG_LONG','LL'], ['LONG_LONG_LONG','LLL']);
135 my $result = " -D__CHAR_BIT__=$char";
136 while (@types && @_) {
137 my $bits = shift @_;
138 my ($name,$suffix) = @{ shift @types };
139 die "$0: weird number of bits." unless exists $pow2m1{$bits};
140 $result .= " -D__${name}_MAX__=" . $pow2m1{$bits} . $suffix;
142 return $result;
145 # -----------------------------------------------------------------------------
147 sub float_types {
148 my ($has_inf,$has_qnan,$dec_dig,@bitsizes) = @_;
149 my $result = " -D__FLT_RADIX__=2";
150 $result .= " -D__FINITE_MATH_ONLY__=" . ($has_inf || $has_qnan ? '0' : '1');
151 $result .= " -D__DECIMAL_DIG__=$dec_dig";
153 my %constants =
154 (24 =>
156 'MIN' => '1.17549435e-38',
157 'MAX' => '3.40282347e+38',
158 'EPSILON' => '1.19209290e-7',
159 'DENORM_MIN' => '1.40129846e-45',
161 53 =>
163 'MIN' => '2.2250738585072014e-308',
164 'MAX' => '1.7976931348623157e+308',
165 'EPSILON' => '2.2204460492503131e-16',
166 'DENORM_MIN' => '4.9406564584124654e-324',
168 64 =>
170 'MIN' => '3.36210314311209350626e-4932',
171 'MAX' => '1.18973149535723176502e+4932',
172 'EPSILON' => '1.08420217248550443401e-19',
173 'DENORM_MIN' => '3.64519953188247460253e-4951',
175 113 =>
177 'MIN' => '3.36210314311209350626267781732175260e-4932',
178 'MAX' => '1.18973149535723176508575932662800702e+4932',
179 'EPSILON' => '1.92592994438723585305597794258492732e-34',
180 'DENORM_MIN' => '6.47517511943802511092443895822764655e-4966',
184 my @types = (['FLT','F'], ['DBL',''], ['LDBL','L']);
185 while (@types) {
186 my ($mant_bits,$exp_bits) = @{ shift @bitsizes };
187 my ($name,$suffix) = @{ shift @types };
189 my $h = $constants{$mant_bits};
190 die "$0: weird number of mantissa bits." unless $h;
192 my $mant_dig = int (($mant_bits - 1) * log (2) / log (10));
193 my $max_exp = 1 << ($exp_bits - 1);
194 my $min_exp = 3 - $max_exp;
195 my $max_10_exp = int ($max_exp * log (2) / log (10));
196 my $min_10_exp = -int (-$min_exp * log (2) / log (10));
198 $result .= " -D__${name}_MANT_DIG__=$mant_bits";
199 $result .= " -D__${name}_DIG__=$mant_dig";
200 $result .= " -D__${name}_MIN_EXP__='($min_exp)'";
201 $result .= " -D__${name}_MAX_EXP__=$max_exp";
202 $result .= " -D__${name}_MIN_10_EXP__='($min_10_exp)'";
203 $result .= " -D__${name}_MAX_10_EXP__=$max_10_exp";
204 $result .= " -D__${name}_HAS_INFINITY__=" . ($has_inf ? '1' : '0');
205 $result .= " -D__${name}_HAS_QUIET_NAN__=" . ($has_qnan ? '1' : '0');;
207 foreach my $inf (sort keys %$h) {
208 $result .= " -D__${name}_${inf}__=" . $h->{$inf} . $suffix;
211 return $result;
214 # -----------------------------------------------------------------------------
216 sub define_size_t {
217 my ($text) = @_;
218 # We have to undef in order to override check's internal definition.
219 return ' -U__SIZE_TYPE__ ' . &quote_arg ("-D__SIZE_TYPE__=$text");
222 # -----------------------------------------------------------------------------
224 sub add_specs {
225 my ($spec) = @_;
226 if ($spec eq 'sunos') {
227 return &add_specs ('unix') .
228 ' -D__sun__=1 -D__sun=1 -Dsun=1' .
229 ' -D__svr4__=1 -DSVR4=1' .
230 ' -D__STDC__=0' .
231 ' -D_REENTRANT' .
232 ' -D_SOLARIS_THREADS' .
233 ' -DNULL="((void *)0)"';
234 } elsif ($spec eq 'linux') {
235 return &add_specs ('unix') .
236 ' -D__linux__=1 -D__linux=1 -Dlinux=linux';
237 } elsif ($spec eq 'openbsd') {
238 return &add_specs ('unix') .
239 ' -D__OpenBSD__=1';
240 } elsif ($spec eq 'unix') {
241 return ' -Dunix=1 -D__unix=1 -D__unix__=1';
242 } elsif ( $spec =~ /^cygwin/) {
243 return &add_specs ('unix') .
244 ' -D__CYGWIN__=1 -D__CYGWIN32__=1' .
245 " -D'_cdecl=__attribute__((__cdecl__))'" .
246 " -D'__cdecl=__attribute__((__cdecl__))'" .
247 " -D'_stdcall=__attribute__((__stdcall__))'" .
248 " -D'__stdcall=__attribute__((__stdcall__))'" .
249 " -D'_fastcall=__attribute__((__fastcall__))'" .
250 " -D'__fastcall=__attribute__((__fastcall__))'" .
251 " -D'__declspec(x)=__attribute__((x))'";
252 } elsif ($spec eq 'i86') {
253 return (' -D__i386=1 -D__i386__=1' .
254 &integer_types (8, 16, 32, $m64 ? 64 : 32, 64) .
255 &float_types (1, 1, 21, [24,8], [53,11], [64,15]) .
256 &define_size_t ($m64 ? "long unsigned int" : "unsigned int") .
257 ' -D__SIZEOF_POINTER__=' . ($m64 ? '8' : '4'));
258 } elsif ($spec eq 'sparc') {
259 return (' -D__sparc=1 -D__sparc__=1' .
260 &integer_types (8, 16, 32, $m64 ? 64 : 32, 64) .
261 &float_types (1, 1, 33, [24,8], [53,11], [113,15]) .
262 &define_size_t ($m64 ? "long unsigned int" : "unsigned int") .
263 ' -D__SIZEOF_POINTER__=' . ($m64 ? '8' : '4'));
264 } elsif ($spec eq 'sparc64') {
265 return (' -D__sparc=1 -D__sparc__=1 -D__sparcv9__=1 -D__sparc64__=1 -D__arch64__=1 -D__LP64__=1' .
266 &integer_types (8, 16, 32, 64, 64, 128) .
267 &float_types (1, 1, 33, [24,8], [53,11], [113,15]) .
268 &define_size_t ("long unsigned int") .
269 ' -D__SIZEOF_POINTER__=8');
270 } elsif ($spec eq 'x86_64') {
271 return (' -D__x86_64=1 -D__x86_64__=1' . ($m32 ? '' : ' -D__LP64__=1') .
272 &integer_types (8, 16, 32, $m32 ? 32 : 64, 64, 128) .
273 &float_types (1, 1, 33, [24,8], [53,11], [113,15]) .
274 &define_size_t ($m32 ? "unsigned int" : "long unsigned int") .
275 ' -D__SIZEOF_POINTER__=' . ($m32 ? '4' : '8'));
276 } elsif ($spec eq 'ppc') {
277 return (' -D__powerpc__=1 -D_BIG_ENDIAN -D_STRING_ARCH_unaligned=1' .
278 &integer_types (8, 16, 32, $m64 ? 64 : 32, 64) .
279 &float_types (1, 1, 21, [24,8], [53,11], [113,15]) .
280 &define_size_t ($m64 ? "long unsigned int" : "unsigned int") .
281 ' -D__SIZEOF_POINTER__=' . ($m64 ? '8' : '4'));
282 } elsif ($spec eq 's390x') {
283 return (' -D__s390x__ -D__s390__ -D_BIG_ENDIAN' .
284 &integer_types (8, 16, 32, $m64 ? 64 : 32, 64) .
285 &float_types (1, 1, 36, [24,8], [53,11], [113,15]) .
286 &define_size_t ("long unsigned int") .
287 ' -D__SIZEOF_POINTER__=' . ($m64 ? '8' : '4'));
288 } elsif ($spec eq 'host_os_specs') {
289 my $os = `uname -s`;
290 chomp $os;
291 return &add_specs (lc $os);
292 } elsif ($spec eq 'host_arch_specs') {
293 my $arch = `uname -m`;
294 chomp $arch;
295 if ($arch =~ /^(i.?86|athlon)$/i) {
296 return &add_specs ('i86');
297 } elsif ($arch =~ /^(sun4u)$/i) {
298 return &add_specs ('sparc');
299 } elsif ($arch =~ /^(x86_64)$/i) {
300 return &add_specs ('x86_64');
301 } elsif ($arch =~ /^(ppc)$/i) {
302 return &add_specs ('ppc');
303 } elsif ($arch =~ /^(s390x)$/i) {
304 return &add_specs ('s390x');
305 } elsif ($arch =~ /^(sparc64)$/i) {
306 return &add_specs ('sparc64');
308 } else {
309 die "$0: invalid specs: $spec\n";
313 # -----------------------------------------------------------------------------