Print this page
12257 resync smatch to 0.6.1-rc1-il-4
Split |
Close |
Expand all |
Collapse all |
--- old/usr/src/tools/smatch/src/lib.c
+++ new/usr/src/tools/smatch/src/lib.c
1 1 /*
2 2 * 'sparse' library helper routines.
3 3 *
4 4 * Copyright (C) 2003 Transmeta Corp.
5 5 * 2003-2004 Linus Torvalds
6 6 *
7 7 * Permission is hereby granted, free of charge, to any person obtaining a copy
8 8 * of this software and associated documentation files (the "Software"), to deal
9 9 * in the Software without restriction, including without limitation the rights
10 10 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11 11 * copies of the Software, and to permit persons to whom the Software is
12 12 * furnished to do so, subject to the following conditions:
13 13 *
14 14 * The above copyright notice and this permission notice shall be included in
15 15 * all copies or substantial portions of the Software.
16 16 *
17 17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 18 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20 20 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21 21 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22 22 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
23 23 * THE SOFTWARE.
24 24 */
25 25 #include <ctype.h>
26 26 #include <errno.h>
27 27 #include <fcntl.h>
28 28 #include <stdarg.h>
29 29 #include <stddef.h>
30 30 #include <stdio.h>
31 31 #include <stdlib.h>
32 32 #include <string.h>
33 33 #include <unistd.h>
34 34 #include <assert.h>
35 35
36 36 #include <sys/types.h>
37 37
38 38 #include "lib.h"
39 39 #include "allocate.h"
40 40 #include "token.h"
41 41 #include "parse.h"
42 42 #include "symbol.h"
43 43 #include "expression.h"
44 44 #include "evaluate.h"
45 45 #include "scope.h"
46 46 #include "linearize.h"
47 47 #include "target.h"
48 48 #include "machine.h"
49 49 #include "version.h"
50 50 #include "bits.h"
51 51
52 52 int verbose, optimize_level, optimize_size, preprocessing;
53 53 int die_if_error = 0;
54 54 int parse_error;
55 55 int has_error = 0;
56 56 int do_output = 0;
57 57
58 58 #ifndef __GNUC__
59 59 # define __GNUC__ 2
60 60 # define __GNUC_MINOR__ 95
61 61 # define __GNUC_PATCHLEVEL__ 0
62 62 #endif
63 63
64 64 int gcc_major = __GNUC__;
65 65 int gcc_minor = __GNUC_MINOR__;
66 66 int gcc_patchlevel = __GNUC_PATCHLEVEL__;
67 67
68 68 const char *base_filename;
69 69
70 70 static const char *diag_prefix = "";
71 71 static const char *gcc_base_dir = GCC_BASE;
72 72 static const char *multiarch_dir = MULTIARCH_TRIPLET;
73 73 static const char *outfile = NULL;
74 74
75 75 struct token *skip_to(struct token *token, int op)
76 76 {
77 77 while (!match_op(token, op) && !eof_token(token))
78 78 token = token->next;
79 79 return token;
80 80 }
81 81
82 82 static struct token bad_token = { .pos.type = TOKEN_BAD };
83 83 struct token *expect(struct token *token, int op, const char *where)
84 84 {
85 85 if (!match_op(token, op)) {
86 86 if (token != &bad_token) {
87 87 bad_token.next = token;
88 88 sparse_error(token->pos, "Expected %s %s", show_special(op), where);
89 89 sparse_error(token->pos, "got %s", show_token(token));
90 90 }
91 91 if (op == ';')
92 92 return skip_to(token, op);
93 93 return &bad_token;
94 94 }
95 95 return token->next;
96 96 }
97 97
98 98 ///
99 99 // issue an error message on new parsing errors
100 100 // @token: the current token
101 101 // @errmsg: the error message
102 102 // If the current token is from a previous error, an error message
103 103 // has already been issued, so nothing more is done.
104 104 // Otherwise, @errmsg is displayed followed by the current token.
105 105 void unexpected(struct token *token, const char *errmsg)
106 106 {
107 107 if (token == &bad_token)
108 108 return;
109 109 sparse_error(token->pos, "%s", errmsg);
110 110 sparse_error(token->pos, "got %s", show_token(token));
111 111 }
112 112
113 113 unsigned int hexval(unsigned int c)
114 114 {
115 115 int retval = 256;
116 116 switch (c) {
117 117 case '0'...'9':
118 118 retval = c - '0';
119 119 break;
120 120 case 'a'...'f':
121 121 retval = c - 'a' + 10;
122 122 break;
123 123 case 'A'...'F':
124 124 retval = c - 'A' + 10;
125 125 break;
126 126 }
127 127 return retval;
128 128 }
129 129
130 130 static void do_warn(const char *type, struct position pos, const char * fmt, va_list args)
131 131 {
132 132 static char buffer[512];
133 133 const char *name;
134 134
135 135 /* Shut up warnings if position is bad_token.pos */
136 136 if (pos.type == TOKEN_BAD)
137 137 return;
138 138
139 139 vsprintf(buffer, fmt, args);
140 140 name = stream_name(pos.stream);
141 141
142 142 fflush(stdout);
143 143 fprintf(stderr, "%s: %s:%d:%d: %s%s\n",
144 144 diag_prefix, name, pos.line, pos.pos, type, buffer);
145 145 }
146 146
147 147 unsigned int fmax_warnings = 100;
148 148 static int show_info = 1;
149 149
150 150 void info(struct position pos, const char * fmt, ...)
151 151 {
152 152 va_list args;
153 153
154 154 if (!show_info)
155 155 return;
156 156 va_start(args, fmt);
157 157 do_warn("", pos, fmt, args);
158 158 va_end(args);
159 159 }
160 160
161 161 static void do_error(struct position pos, const char * fmt, va_list args)
162 162 {
163 163 static int errors = 0;
164 164
165 165 parse_error = 1;
166 166 die_if_error = 1;
167 167 show_info = 1;
168 168 /* Shut up warnings if position is bad_token.pos */
169 169 if (pos.type == TOKEN_BAD)
170 170 return;
171 171 /* Shut up warnings after an error */
172 172 has_error |= ERROR_CURR_PHASE;
173 173 if (errors > 100) {
174 174 static int once = 0;
175 175 show_info = 0;
176 176 if (once)
177 177 return;
178 178 fmt = "too many errors";
179 179 once = 1;
180 180 }
181 181
182 182 do_warn("error: ", pos, fmt, args);
183 183 errors++;
184 184 }
185 185
186 186 void warning(struct position pos, const char * fmt, ...)
187 187 {
188 188 va_list args;
189 189
190 190 if (Wsparse_error) {
191 191 va_start(args, fmt);
192 192 do_error(pos, fmt, args);
193 193 va_end(args);
194 194 return;
195 195 }
196 196
197 197 if (!fmax_warnings || has_error) {
198 198 show_info = 0;
199 199 return;
200 200 }
201 201
202 202 if (!--fmax_warnings) {
203 203 show_info = 0;
204 204 fmt = "too many warnings";
205 205 }
206 206
207 207 va_start(args, fmt);
208 208 do_warn("warning: ", pos, fmt, args);
209 209 va_end(args);
210 210 }
211 211
212 212 void sparse_error(struct position pos, const char * fmt, ...)
213 213 {
214 214 va_list args;
215 215 va_start(args, fmt);
216 216 do_error(pos, fmt, args);
217 217 va_end(args);
218 218 }
219 219
220 220 void expression_error(struct expression *expr, const char *fmt, ...)
221 221 {
222 222 va_list args;
223 223 va_start(args, fmt);
224 224 do_error(expr->pos, fmt, args);
225 225 va_end(args);
226 226 expr->ctype = &bad_ctype;
227 227 }
228 228
229 229 NORETURN_ATTR
230 230 void error_die(struct position pos, const char * fmt, ...)
231 231 {
232 232 va_list args;
233 233 va_start(args, fmt);
234 234 do_warn("error: ", pos, fmt, args);
235 235 va_end(args);
236 236 exit(1);
237 237 }
238 238
239 239 NORETURN_ATTR
240 240 void die(const char *fmt, ...)
241 241 {
242 242 va_list args;
243 243 static char buffer[512];
244 244
245 245 va_start(args, fmt);
246 246 vsnprintf(buffer, sizeof(buffer), fmt, args);
247 247 va_end(args);
248 248
249 249 fprintf(stderr, "%s: %s\n", diag_prefix, buffer);
250 250 exit(1);
251 251 }
252 252
253 253 static struct token *pre_buffer_begin = NULL;
254 254 static struct token *pre_buffer_end = NULL;
255 255
256 256 int Waddress = 0;
257 257 int Waddress_space = 1;
258 258 int Wbitwise = 1;
259 259 int Wbitwise_pointer = 0;
260 260 int Wcast_from_as = 0;
261 261 int Wcast_to_as = 0;
262 262 int Wcast_truncate = 1;
263 263 int Wconstant_suffix = 0;
264 264 int Wconstexpr_not_const = 0;
265 265 int Wcontext = 1;
266 266 int Wdecl = 1;
267 267 int Wdeclarationafterstatement = -1;
268 268 int Wdefault_bitfield_sign = 0;
269 269 int Wdesignated_init = 1;
270 270 int Wdo_while = 0;
271 271 int Wimplicit_int = 1;
272 272 int Winit_cstring = 0;
273 273 int Wint_to_pointer_cast = 1;
274 274 int Wenum_mismatch = 1;
275 275 int Wexternal_function_has_definition = 1;
276 276 int Wsparse_error = 0;
277 277 int Wmemcpy_max_count = 1;
278 278 int Wnon_pointer_null = 1;
279 279 int Wold_initializer = 1;
280 280 int Wold_style_definition = 1;
281 281 int Wone_bit_signed_bitfield = 1;
282 282 int Woverride_init = 1;
283 283 int Woverride_init_all = 0;
284 284 int Woverride_init_whole_range = 0;
285 285 int Wparen_string = 0;
286 286 int Wpointer_arith = 0;
287 287 int Wpointer_to_int_cast = 1;
288 288 int Wptr_subtraction_blows = 0;
289 289 int Wreturn_void = 0;
290 290 int Wshadow = 0;
291 291 int Wshift_count_negative = 1;
292 292 int Wshift_count_overflow = 1;
293 293 int Wsizeof_bool = 0;
294 294 int Wstrict_prototypes = 1;
295 295 int Wtautological_compare = 0;
296 296 int Wtransparent_union = 0;
297 297 int Wtypesign = 0;
298 298 int Wundef = 0;
299 299 int Wuninitialized = 1;
300 300 int Wunknown_attribute = 0;
301 301 int Wvla = 1;
302 302
303 303 int dump_macro_defs = 0;
304 304 int dump_macros_only = 0;
305 305
306 306 int dbg_compound = 0;
307 307 int dbg_dead = 0;
308 308 int dbg_domtree = 0;
309 309 int dbg_entry = 0;
310 310 int dbg_ir = 0;
↓ open down ↓ |
310 lines elided |
↑ open up ↑ |
311 311 int dbg_postorder = 0;
312 312
313 313 unsigned long fdump_ir;
314 314 int fmem_report = 0;
315 315 unsigned long long fmemcpy_max_count = 100000;
316 316 unsigned long fpasses = ~0UL;
317 317 int funsigned_char = UNSIGNED_CHAR;
318 318
319 319 int preprocess_only;
320 320
321 -static enum { STANDARD_C89,
322 - STANDARD_C94,
323 - STANDARD_C99,
324 - STANDARD_C11,
325 - STANDARD_GNU11,
326 - STANDARD_GNU89,
327 - STANDARD_GNU99, } standard = STANDARD_GNU89;
321 +enum standard standard = STANDARD_GNU89;
328 322
329 323 int arch_m64 = ARCH_M64_DEFAULT;
330 324 int arch_msize_long = 0;
331 325 int arch_big_endian = ARCH_BIG_ENDIAN;
332 326 int arch_mach = MACH_NATIVE;
333 327
334 328
335 329 #define CMDLINE_INCLUDE 20
336 330 static int cmdline_include_nr = 0;
337 331 static char *cmdline_include[CMDLINE_INCLUDE];
338 332
339 333
340 334 void add_pre_buffer(const char *fmt, ...)
341 335 {
342 336 va_list args;
343 337 unsigned int size;
344 338 struct token *begin, *end;
345 339 char buffer[4096];
346 340
347 341 va_start(args, fmt);
348 342 size = vsnprintf(buffer, sizeof(buffer), fmt, args);
349 343 va_end(args);
350 344 begin = tokenize_buffer(buffer, size, &end);
351 345 if (!pre_buffer_begin)
352 346 pre_buffer_begin = begin;
353 347 if (pre_buffer_end)
354 348 pre_buffer_end->next = begin;
355 349 pre_buffer_end = end;
356 350 }
357 351
358 352 static char **handle_switch_D(char *arg, char **next)
359 353 {
360 354 const char *name = arg + 1;
361 355 const char *value = "1";
362 356
363 357 if (!*name) {
364 358 arg = *++next;
365 359 if (!arg)
366 360 die("argument to `-D' is missing");
367 361 name = arg;
368 362 }
369 363
370 364 for (;;arg++) {
371 365 char c;
372 366 c = *arg;
373 367 if (!c)
374 368 break;
375 369 if (c == '=') {
376 370 *arg = '\0';
377 371 value = arg + 1;
378 372 break;
379 373 }
380 374 }
381 375 add_pre_buffer("#define %s %s\n", name, value);
382 376 return next;
383 377 }
384 378
385 379 static char **handle_switch_E(char *arg, char **next)
386 380 {
387 381 if (arg[1] == '\0')
388 382 preprocess_only = 1;
389 383 return next;
390 384 }
391 385
392 386 static char **handle_switch_I(char *arg, char **next)
393 387 {
394 388 char *path = arg+1;
395 389
396 390 switch (arg[1]) {
397 391 case '-':
398 392 add_pre_buffer("#split_include\n");
399 393 break;
400 394
401 395 case '\0': /* Plain "-I" */
402 396 path = *++next;
403 397 if (!path)
404 398 die("missing argument for -I option");
405 399 /* Fall through */
406 400 default:
407 401 add_pre_buffer("#add_include \"%s/\"\n", path);
408 402 }
409 403 return next;
410 404 }
411 405
412 406 static void add_cmdline_include(char *filename)
413 407 {
414 408 if (cmdline_include_nr >= CMDLINE_INCLUDE)
415 409 die("too many include files for %s\n", filename);
416 410 cmdline_include[cmdline_include_nr++] = filename;
417 411 }
418 412
419 413 static char **handle_switch_i(char *arg, char **next)
420 414 {
421 415 if (*next && !strcmp(arg, "include"))
422 416 add_cmdline_include(*++next);
423 417 else if (*next && !strcmp(arg, "imacros"))
424 418 add_cmdline_include(*++next);
425 419 else if (*next && !strcmp(arg, "isystem")) {
426 420 char *path = *++next;
427 421 if (!path)
428 422 die("missing argument for -isystem option");
429 423 add_pre_buffer("#add_isystem \"%s/\"\n", path);
430 424 } else if (*next && !strcmp(arg, "idirafter")) {
431 425 char *path = *++next;
432 426 if (!path)
433 427 die("missing argument for -idirafter option");
434 428 add_pre_buffer("#add_dirafter \"%s/\"\n", path);
435 429 }
436 430 return next;
437 431 }
438 432
439 433 static char **handle_switch_M(char *arg, char **next)
440 434 {
441 435 if (!strcmp(arg, "MF") || !strcmp(arg,"MQ") || !strcmp(arg,"MT")) {
442 436 if (!*next)
443 437 die("missing argument for -%s option", arg);
444 438 return next + 1;
445 439 }
446 440 return next;
447 441 }
448 442
449 443 static char **handle_multiarch_dir(char *arg, char **next)
450 444 {
451 445 multiarch_dir = *++next;
452 446 if (!multiarch_dir)
453 447 die("missing argument for -multiarch-dir option");
454 448 return next;
455 449 }
456 450
457 451 static char **handle_switch_m(char *arg, char **next)
458 452 {
459 453 if (!strcmp(arg, "m64")) {
460 454 arch_m64 = ARCH_LP64;
461 455 } else if (!strcmp(arg, "m32") || !strcmp(arg, "m16")) {
462 456 arch_m64 = ARCH_LP32;
463 457 } else if (!strcmp(arg, "mx32")) {
464 458 arch_m64 = ARCH_X32;
465 459 } else if (!strcmp(arg, "msize-llp64")) {
466 460 arch_m64 = ARCH_LLP64;
467 461 } else if (!strcmp(arg, "msize-long")) {
468 462 arch_msize_long = 1;
469 463 } else if (!strcmp(arg, "multiarch-dir")) {
470 464 return handle_multiarch_dir(arg, next);
471 465 } else if (!strcmp(arg, "mbig-endian")) {
472 466 arch_big_endian = 1;
473 467 } else if (!strcmp(arg, "mlittle-endian")) {
474 468 arch_big_endian = 0;
475 469 }
476 470 return next;
477 471 }
478 472
479 473 static void handle_arch_msize_long_finalize(void)
480 474 {
481 475 if (arch_msize_long) {
482 476 size_t_ctype = &ulong_ctype;
483 477 ssize_t_ctype = &long_ctype;
484 478 }
485 479 }
486 480
487 481 static void handle_arch_finalize(void)
488 482 {
489 483 handle_arch_msize_long_finalize();
490 484 }
491 485
492 486 static const char *match_option(const char *arg, const char *prefix)
493 487 {
494 488 unsigned int n = strlen(prefix);
495 489 if (strncmp(arg, prefix, n) == 0)
496 490 return arg + n;
497 491 return NULL;
498 492 }
499 493
500 494
501 495 struct mask_map {
502 496 const char *name;
503 497 unsigned long mask;
504 498 };
505 499
506 500 static int apply_mask(unsigned long *val, const char *str, unsigned len, const struct mask_map *map, int neg)
507 501 {
508 502 const char *name;
509 503
510 504 for (;(name = map->name); map++) {
511 505 if (!strncmp(name, str, len) && !name[len]) {
512 506 if (neg == 0)
513 507 *val |= map->mask;
514 508 else
515 509 *val &= ~map->mask;
516 510 return 0;
517 511 }
518 512 }
519 513 return 1;
520 514 }
521 515
522 516 static int handle_suboption_mask(const char *arg, const char *opt, const struct mask_map *map, unsigned long *flag)
523 517 {
524 518 if (*opt == '\0') {
525 519 apply_mask(flag, "", 0, map, 0);
526 520 return 1;
527 521 }
528 522 if (*opt++ != '=')
529 523 return 0;
530 524 while (1) {
531 525 unsigned int len = strcspn(opt, ",+");
532 526 int neg = 0;
533 527 if (len == 0)
534 528 goto end;
535 529 if (!strncmp(opt, "no-", 3)) {
536 530 opt += 3;
537 531 len -= 3;
538 532 neg = 1;
539 533 }
540 534 if (apply_mask(flag, opt, len, map, neg))
541 535 die("error: wrong option '%.*s' for \'%s\'", len, opt, arg);
542 536
543 537 end:
544 538 opt += len;
545 539 if (*opt++ == '\0')
546 540 break;
547 541 }
548 542 return 1;
549 543 }
550 544
551 545
552 546 #define OPT_INVERSE 1
553 547 struct flag {
554 548 const char *name;
555 549 int *flag;
556 550 int (*fun)(const char *arg, const char *opt, const struct flag *, int options);
557 551 unsigned long mask;
558 552 };
559 553
560 554 static int handle_switches(const char *ori, const char *opt, const struct flag *flags)
561 555 {
562 556 const char *arg = opt;
563 557 int val = 1;
564 558
565 559 // Prefixe "no-" mean to turn flag off.
566 560 if (strncmp(arg, "no-", 3) == 0) {
567 561 arg += 3;
568 562 val = 0;
569 563 }
570 564
571 565 for (; flags->name; flags++) {
572 566 const char *opt = match_option(arg, flags->name);
573 567 int rc;
574 568
575 569 if (!opt)
576 570 continue;
577 571
578 572 if (flags->fun) {
579 573 int options = 0;
580 574 if (!val)
581 575 options |= OPT_INVERSE;
582 576 if ((rc = flags->fun(ori, opt, flags, options)))
583 577 return rc;
584 578 }
585 579
586 580 // boolean flag
587 581 if (opt[0] == '\0' && flags->flag) {
588 582 if (flags->mask & OPT_INVERSE)
589 583 val = !val;
590 584 *flags->flag = val;
591 585 return 1;
592 586 }
593 587 }
594 588
595 589 // not handled
596 590 return 0;
597 591 }
598 592
599 593
600 594 #define OPTNUM_ZERO_IS_INF 1
601 595 #define OPTNUM_UNLIMITED 2
602 596
603 597 #define OPT_NUMERIC(NAME, TYPE, FUNCTION) \
604 598 static int opt_##NAME(const char *arg, const char *opt, TYPE *ptr, int flag) \
605 599 { \
606 600 char *end; \
607 601 TYPE val; \
608 602 \
609 603 val = FUNCTION(opt, &end, 0); \
610 604 if (*end != '\0' || end == opt) { \
611 605 if ((flag & OPTNUM_UNLIMITED) && !strcmp(opt, "unlimited")) \
612 606 val = ~val; \
613 607 else \
614 608 die("error: wrong argument to \'%s\'", arg); \
615 609 } \
616 610 if ((flag & OPTNUM_ZERO_IS_INF) && val == 0) \
617 611 val = ~val; \
618 612 *ptr = val; \
619 613 return 1; \
620 614 }
621 615
622 616 OPT_NUMERIC(ullong, unsigned long long, strtoull)
623 617 OPT_NUMERIC(uint, unsigned int, strtoul)
624 618
625 619
626 620 static char **handle_switch_o(char *arg, char **next)
627 621 {
628 622 if (!strcmp (arg, "o")) { // "-o foo"
629 623 if (!*++next)
630 624 die("argument to '-o' is missing");
631 625 outfile = *next;
632 626 }
633 627 // else "-ofoo"
634 628
635 629 return next;
636 630 }
637 631
638 632 static const struct flag warnings[] = {
639 633 { "address", &Waddress },
640 634 { "address-space", &Waddress_space },
641 635 { "bitwise", &Wbitwise },
642 636 { "bitwise-pointer", &Wbitwise_pointer},
643 637 { "cast-from-as", &Wcast_from_as },
644 638 { "cast-to-as", &Wcast_to_as },
645 639 { "cast-truncate", &Wcast_truncate },
646 640 { "constant-suffix", &Wconstant_suffix },
647 641 { "constexpr-not-const", &Wconstexpr_not_const},
648 642 { "context", &Wcontext },
649 643 { "decl", &Wdecl },
650 644 { "declaration-after-statement", &Wdeclarationafterstatement },
651 645 { "default-bitfield-sign", &Wdefault_bitfield_sign },
652 646 { "designated-init", &Wdesignated_init },
653 647 { "do-while", &Wdo_while },
654 648 { "enum-mismatch", &Wenum_mismatch },
655 649 { "external-function-has-definition", &Wexternal_function_has_definition },
656 650 { "implicit-int", &Wimplicit_int },
657 651 { "init-cstring", &Winit_cstring },
658 652 { "int-to-pointer-cast", &Wint_to_pointer_cast },
659 653 { "memcpy-max-count", &Wmemcpy_max_count },
660 654 { "non-pointer-null", &Wnon_pointer_null },
661 655 { "old-initializer", &Wold_initializer },
662 656 { "old-style-definition", &Wold_style_definition },
663 657 { "one-bit-signed-bitfield", &Wone_bit_signed_bitfield },
664 658 { "override-init", &Woverride_init },
665 659 { "override-init-all", &Woverride_init_all },
666 660 { "paren-string", &Wparen_string },
667 661 { "pointer-to-int-cast", &Wpointer_to_int_cast },
668 662 { "ptr-subtraction-blows", &Wptr_subtraction_blows },
669 663 { "return-void", &Wreturn_void },
670 664 { "shadow", &Wshadow },
671 665 { "shift-count-negative", &Wshift_count_negative },
672 666 { "shift-count-overflow", &Wshift_count_overflow },
673 667 { "sizeof-bool", &Wsizeof_bool },
674 668 { "strict-prototypes", &Wstrict_prototypes },
675 669 { "pointer-arith", &Wpointer_arith },
676 670 { "sparse-error", &Wsparse_error },
677 671 { "tautological-compare", &Wtautological_compare },
678 672 { "transparent-union", &Wtransparent_union },
679 673 { "typesign", &Wtypesign },
680 674 { "undef", &Wundef },
681 675 { "uninitialized", &Wuninitialized },
682 676 { "unknown-attribute", &Wunknown_attribute },
683 677 { "vla", &Wvla },
684 678 };
685 679
686 680 enum {
687 681 WARNING_OFF,
688 682 WARNING_ON,
689 683 WARNING_FORCE_OFF
690 684 };
691 685
692 686
693 687 static char **handle_onoff_switch(char *arg, char **next, const struct flag warnings[], int n)
694 688 {
695 689 int flag = WARNING_ON;
696 690 char *p = arg + 1;
697 691 unsigned i;
698 692
699 693 if (!strcmp(p, "sparse-all")) {
700 694 for (i = 0; i < n; i++) {
701 695 if (*warnings[i].flag != WARNING_FORCE_OFF && warnings[i].flag != &Wsparse_error)
702 696 *warnings[i].flag = WARNING_ON;
703 697 }
704 698 return NULL;
705 699 }
706 700
707 701 // Prefixes "no" and "no-" mean to turn warning off.
708 702 if (p[0] == 'n' && p[1] == 'o') {
709 703 p += 2;
710 704 if (p[0] == '-')
711 705 p++;
712 706 flag = WARNING_FORCE_OFF;
713 707 }
714 708
715 709 for (i = 0; i < n; i++) {
716 710 if (!strcmp(p,warnings[i].name)) {
717 711 *warnings[i].flag = flag;
718 712 return next;
719 713 }
720 714 }
721 715
722 716 // Unknown.
723 717 return NULL;
724 718 }
725 719
726 720 static char **handle_switch_W(char *arg, char **next)
727 721 {
728 722 char ** ret = handle_onoff_switch(arg, next, warnings, ARRAY_SIZE(warnings));
729 723 if (ret)
730 724 return ret;
731 725
732 726 // Unknown.
733 727 return next;
734 728 }
735 729
736 730 static struct flag debugs[] = {
737 731 { "compound", &dbg_compound},
738 732 { "dead", &dbg_dead},
739 733 { "domtree", &dbg_domtree},
740 734 { "entry", &dbg_entry},
741 735 { "ir", &dbg_ir},
742 736 { "postorder", &dbg_postorder},
743 737 };
744 738
745 739
746 740 static char **handle_switch_v(char *arg, char **next)
747 741 {
748 742 char ** ret = handle_onoff_switch(arg, next, debugs, ARRAY_SIZE(debugs));
749 743 if (ret)
750 744 return ret;
751 745
752 746 // Unknown.
753 747 do {
754 748 verbose++;
755 749 } while (*++arg == 'v');
756 750 return next;
757 751 }
758 752
759 753 static char **handle_switch_d(char *arg, char **next)
760 754 {
761 755 char *arg_char = arg + 1;
762 756
763 757 /*
764 758 * -d<CHARS>, where <CHARS> is a sequence of characters, not preceded
765 759 * by a space. If you specify characters whose behaviour conflicts,
766 760 * the result is undefined.
767 761 */
768 762 while (*arg_char) {
769 763 switch (*arg_char) {
770 764 case 'M': /* dump just the macro definitions */
771 765 dump_macros_only = 1;
772 766 dump_macro_defs = 0;
773 767 break;
774 768 case 'D': /* like 'M', but also output pre-processed text */
775 769 dump_macro_defs = 1;
776 770 dump_macros_only = 0;
777 771 break;
778 772 case 'N': /* like 'D', but only output macro names not bodies */
779 773 break;
780 774 case 'I': /* like 'D', but also output #include directives */
781 775 break;
782 776 case 'U': /* like 'D', but only output expanded macros */
783 777 break;
784 778 }
785 779 arg_char++;
786 780 }
787 781 return next;
788 782 }
789 783
790 784
791 785 static void handle_onoff_switch_finalize(const struct flag warnings[], int n)
792 786 {
793 787 unsigned i;
794 788
795 789 for (i = 0; i < n; i++) {
796 790 if (*warnings[i].flag == WARNING_FORCE_OFF)
797 791 *warnings[i].flag = WARNING_OFF;
798 792 }
799 793 }
800 794
801 795 static void handle_switch_W_finalize(void)
802 796 {
803 797 handle_onoff_switch_finalize(warnings, ARRAY_SIZE(warnings));
804 798
805 799 /* default Wdeclarationafterstatement based on the C dialect */
806 800 if (-1 == Wdeclarationafterstatement)
807 801 {
808 802 switch (standard)
809 803 {
810 804 case STANDARD_C89:
811 805 case STANDARD_C94:
812 806 Wdeclarationafterstatement = 1;
813 807 break;
814 808
815 809 case STANDARD_C99:
816 810 case STANDARD_GNU89:
817 811 case STANDARD_GNU99:
818 812 case STANDARD_C11:
819 813 case STANDARD_GNU11:
820 814 Wdeclarationafterstatement = 0;
821 815 break;
822 816
823 817 default:
824 818 assert (0);
825 819 }
826 820
827 821 }
828 822 }
829 823
830 824 static void handle_switch_v_finalize(void)
831 825 {
832 826 handle_onoff_switch_finalize(debugs, ARRAY_SIZE(debugs));
833 827 }
834 828
835 829 static char **handle_switch_U(char *arg, char **next)
836 830 {
837 831 const char *name = arg + 1;
838 832 if (*name == '\0')
839 833 name = *++next;
840 834 add_pre_buffer ("#undef %s\n", name);
841 835 return next;
842 836 }
843 837
844 838 static char **handle_switch_O(char *arg, char **next)
845 839 {
846 840 int level = 1;
847 841 if (arg[1] >= '0' && arg[1] <= '9')
848 842 level = arg[1] - '0';
849 843 optimize_level = level;
850 844 optimize_size = arg[1] == 's';
851 845 return next;
852 846 }
853 847
854 848 static int handle_ftabstop(const char *arg, const char *opt, const struct flag *flag, int options)
855 849 {
856 850 unsigned long val;
857 851 char *end;
858 852
859 853 if (*opt == '\0')
860 854 die("error: missing argument to \"%s\"", arg);
861 855
862 856 /* we silently ignore silly values */
863 857 val = strtoul(opt, &end, 10);
864 858 if (*end == '\0' && 1 <= val && val <= 100)
865 859 tabstop = val;
866 860
867 861 return 1;
868 862 }
869 863
870 864 static int handle_fpasses(const char *arg, const char *opt, const struct flag *flag, int options)
871 865 {
872 866 unsigned long mask;
873 867
874 868 mask = flag->mask;
875 869 if (*opt == '\0') {
876 870 if (options & OPT_INVERSE)
877 871 fpasses &= ~mask;
878 872 else
879 873 fpasses |= mask;
880 874 return 1;
881 875 }
882 876 if (options & OPT_INVERSE)
883 877 return 0;
884 878 if (!strcmp(opt, "-enable")) {
885 879 fpasses |= mask;
886 880 return 1;
887 881 }
888 882 if (!strcmp(opt, "-disable")) {
889 883 fpasses &= ~mask;
890 884 return 1;
891 885 }
892 886 if (!strcmp(opt, "=last")) {
893 887 // clear everything above
894 888 mask |= mask - 1;
895 889 fpasses &= mask;
896 890 return 1;
897 891 }
898 892 return 0;
899 893 }
900 894
901 895 static int handle_fdiagnostic_prefix(const char *arg, const char *opt, const struct flag *flag, int options)
902 896 {
903 897 switch (*opt) {
904 898 case '\0':
905 899 diag_prefix = "sparse";
906 900 return 1;
907 901 case '=':
908 902 diag_prefix = xasprintf("%s", opt+1);
909 903 return 1;
910 904 default:
911 905 return 0;
912 906 }
913 907 }
914 908
915 909 static int handle_fdump_ir(const char *arg, const char *opt, const struct flag *flag, int options)
916 910 {
917 911 static const struct mask_map dump_ir_options[] = {
918 912 { "", PASS_LINEARIZE },
919 913 { "linearize", PASS_LINEARIZE },
920 914 { "mem2reg", PASS_MEM2REG },
921 915 { "final", PASS_FINAL },
922 916 { },
923 917 };
924 918
925 919 return handle_suboption_mask(arg, opt, dump_ir_options, &fdump_ir);
926 920 }
927 921
928 922 static int handle_fmemcpy_max_count(const char *arg, const char *opt, const struct flag *flag, int options)
929 923 {
930 924 opt_ullong(arg, opt, &fmemcpy_max_count, OPTNUM_ZERO_IS_INF|OPTNUM_UNLIMITED);
931 925 return 1;
932 926 }
933 927
934 928 static int handle_fmax_warnings(const char *arg, const char *opt, const struct flag *flag, int options)
935 929 {
936 930 opt_uint(arg, opt, &fmax_warnings, OPTNUM_UNLIMITED);
937 931 return 1;
938 932 }
939 933
940 934 static struct flag fflags[] = {
941 935 { "diagnostic-prefix", NULL, handle_fdiagnostic_prefix },
942 936 { "dump-ir", NULL, handle_fdump_ir },
943 937 { "linearize", NULL, handle_fpasses, PASS_LINEARIZE },
944 938 { "max-warnings=", NULL, handle_fmax_warnings },
945 939 { "mem-report", &fmem_report },
946 940 { "memcpy-max-count=", NULL, handle_fmemcpy_max_count },
947 941 { "tabstop=", NULL, handle_ftabstop },
948 942 { "mem2reg", NULL, handle_fpasses, PASS_MEM2REG },
949 943 { "optim", NULL, handle_fpasses, PASS_OPTIM },
950 944 { "signed-char", &funsigned_char, NULL, OPT_INVERSE },
951 945 { "unsigned-char", &funsigned_char, NULL, },
952 946 { },
953 947 };
954 948
955 949 static char **handle_switch_f(char *arg, char **next)
956 950 {
957 951 if (handle_switches(arg-1, arg+1, fflags))
958 952 return next;
959 953
960 954 return next;
961 955 }
962 956
963 957 static char **handle_switch_G(char *arg, char **next)
964 958 {
965 959 if (!strcmp (arg, "G") && *next)
966 960 return next + 1; // "-G 0"
967 961 else
968 962 return next; // "-G0" or (bogus) terminal "-G"
969 963 }
970 964
971 965 static char **handle_switch_a(char *arg, char **next)
972 966 {
973 967 if (!strcmp (arg, "ansi"))
974 968 standard = STANDARD_C89;
975 969
976 970 return next;
977 971 }
978 972
979 973 static char **handle_switch_s(const char *arg, char **next)
980 974 {
981 975 if ((arg = match_option(arg, "std="))) {
982 976 if (!strcmp (arg, "c89") ||
983 977 !strcmp (arg, "iso9899:1990"))
984 978 standard = STANDARD_C89;
985 979
986 980 else if (!strcmp (arg, "iso9899:199409"))
987 981 standard = STANDARD_C94;
988 982
989 983 else if (!strcmp (arg, "c99") ||
990 984 !strcmp (arg, "c9x") ||
991 985 !strcmp (arg, "iso9899:1999") ||
992 986 !strcmp (arg, "iso9899:199x"))
993 987 standard = STANDARD_C99;
994 988
995 989 else if (!strcmp (arg, "gnu89"))
996 990 standard = STANDARD_GNU89;
997 991
998 992 else if (!strcmp (arg, "gnu99") || !strcmp (arg, "gnu9x"))
999 993 standard = STANDARD_GNU99;
1000 994
1001 995 else if (!strcmp(arg, "c11") ||
1002 996 !strcmp(arg, "c1x") ||
1003 997 !strcmp(arg, "iso9899:2011"))
1004 998 standard = STANDARD_C11;
1005 999
1006 1000 else if (!strcmp(arg, "gnu11"))
1007 1001 standard = STANDARD_GNU11;
1008 1002
1009 1003 else
1010 1004 die ("Unsupported C dialect");
1011 1005 }
1012 1006
1013 1007 return next;
1014 1008 }
1015 1009
1016 1010 static char **handle_nostdinc(char *arg, char **next)
1017 1011 {
1018 1012 add_pre_buffer("#nostdinc\n");
1019 1013 return next;
1020 1014 }
1021 1015
1022 1016 static char **handle_switch_n(char *arg, char **next)
1023 1017 {
1024 1018 if (!strcmp (arg, "nostdinc"))
1025 1019 return handle_nostdinc(arg, next);
1026 1020
1027 1021 return next;
1028 1022 }
1029 1023
1030 1024 static char **handle_base_dir(char *arg, char **next)
1031 1025 {
1032 1026 gcc_base_dir = *++next;
1033 1027 if (!gcc_base_dir)
1034 1028 die("missing argument for -gcc-base-dir option");
1035 1029 return next;
1036 1030 }
1037 1031
1038 1032 static char **handle_no_lineno(char *arg, char **next)
1039 1033 {
1040 1034 no_lineno = 1;
1041 1035 return next;
1042 1036 }
1043 1037
1044 1038 static char **handle_switch_g(char *arg, char **next)
1045 1039 {
1046 1040 if (!strcmp (arg, "gcc-base-dir"))
1047 1041 return handle_base_dir(arg, next);
1048 1042
1049 1043 return next;
1050 1044 }
1051 1045
1052 1046 static char **handle_switch_x(char *arg, char **next)
1053 1047 {
1054 1048 if (!*++next)
1055 1049 die("missing argument for -x option");
1056 1050 return next;
1057 1051 }
1058 1052
1059 1053 static char **handle_version(char *arg, char **next)
1060 1054 {
1061 1055 printf("%s\n", SPARSE_VERSION);
1062 1056 exit(0);
1063 1057 }
1064 1058
1065 1059 static char **handle_param(char *arg, char **next)
1066 1060 {
1067 1061 char *value = NULL;
1068 1062
1069 1063 /* Ignore smatch's --param-mapper */
1070 1064 if (strcmp(arg, "-mapper") == 0)
1071 1065 return next;
1072 1066
1073 1067 /* For now just skip any '--param=*' or '--param *' */
1074 1068 if (*arg == '\0') {
1075 1069 value = *++next;
1076 1070 } else if (isspace((unsigned char)*arg) || *arg == '=') {
1077 1071 value = ++arg;
1078 1072 }
1079 1073
1080 1074 if (!value)
1081 1075 die("missing argument for --param option");
1082 1076
1083 1077 return next;
1084 1078 }
1085 1079
1086 1080 struct switches {
1087 1081 const char *name;
1088 1082 char **(*fn)(char *, char **);
1089 1083 unsigned int prefix:1;
1090 1084 };
1091 1085
1092 1086 static char **handle_long_options(char *arg, char **next)
1093 1087 {
1094 1088 static struct switches cmd[] = {
1095 1089 { "param", handle_param, 1 },
1096 1090 { "version", handle_version },
1097 1091 { "nostdinc", handle_nostdinc },
1098 1092 { "gcc-base-dir", handle_base_dir},
1099 1093 { "no-lineno", handle_no_lineno},
1100 1094 { NULL, NULL }
1101 1095 };
1102 1096 struct switches *s = cmd;
1103 1097
1104 1098 while (s->name) {
1105 1099 int optlen = strlen(s->name);
1106 1100 if (!strncmp(s->name, arg, optlen + !s->prefix))
1107 1101 return s->fn(arg + optlen, next);
1108 1102 s++;
1109 1103 }
1110 1104 return next;
1111 1105 }
1112 1106
1113 1107 static char **handle_switch(char *arg, char **next)
1114 1108 {
1115 1109 switch (*arg) {
1116 1110 case 'a': return handle_switch_a(arg, next);
1117 1111 case 'D': return handle_switch_D(arg, next);
1118 1112 case 'd': return handle_switch_d(arg, next);
1119 1113 case 'E': return handle_switch_E(arg, next);
1120 1114 case 'f': return handle_switch_f(arg, next);
1121 1115 case 'g': return handle_switch_g(arg, next);
1122 1116 case 'G': return handle_switch_G(arg, next);
1123 1117 case 'I': return handle_switch_I(arg, next);
1124 1118 case 'i': return handle_switch_i(arg, next);
1125 1119 case 'M': return handle_switch_M(arg, next);
1126 1120 case 'm': return handle_switch_m(arg, next);
1127 1121 case 'n': return handle_switch_n(arg, next);
1128 1122 case 'o': return handle_switch_o(arg, next);
1129 1123 case 'O': return handle_switch_O(arg, next);
1130 1124 case 's': return handle_switch_s(arg, next);
1131 1125 case 'U': return handle_switch_U(arg, next);
1132 1126 case 'v': return handle_switch_v(arg, next);
1133 1127 case 'W': return handle_switch_W(arg, next);
1134 1128 case 'x': return handle_switch_x(arg, next);
1135 1129 case '-': return handle_long_options(arg + 1, next);
1136 1130 default:
1137 1131 break;
1138 1132 }
1139 1133
1140 1134 /*
1141 1135 * Ignore unknown command line options:
1142 1136 * they're probably gcc switches
1143 1137 */
1144 1138 return next;
1145 1139 }
1146 1140
1147 1141 #define PTYPE_SIZEOF (1U << 0)
1148 1142 #define PTYPE_T (1U << 1)
1149 1143 #define PTYPE_MAX (1U << 2)
1150 1144 #define PTYPE_MIN (1U << 3)
1151 1145 #define PTYPE_WIDTH (1U << 4)
1152 1146 #define PTYPE_TYPE (1U << 5)
1153 1147 #define PTYPE_ALL (PTYPE_MAX|PTYPE_SIZEOF|PTYPE_WIDTH)
1154 1148 #define PTYPE_ALL_T (PTYPE_MAX|PTYPE_SIZEOF|PTYPE_WIDTH|PTYPE_T)
1155 1149
1156 1150 static void predefined_sizeof(const char *name, const char *suffix, unsigned bits)
1157 1151 {
1158 1152 char buf[32];
1159 1153
1160 1154 snprintf(buf, sizeof(buf), "__SIZEOF_%s%s__", name, suffix);
1161 1155 predefine(buf, 1, "%d", bits/8);
1162 1156 }
1163 1157
1164 1158 static void predefined_width(const char *name, unsigned bits)
1165 1159 {
1166 1160 char buf[32];
1167 1161
1168 1162 snprintf(buf, sizeof(buf), "__%s_WIDTH__", name);
1169 1163 predefine(buf, 1, "%d", bits);
1170 1164 }
1171 1165
1172 1166 static void predefined_max(const char *name, struct symbol *type)
1173 1167 {
1174 1168 const char *suffix = builtin_type_suffix(type);
1175 1169 unsigned bits = type->bit_size - is_signed_type(type);
1176 1170 unsigned long long max = bits_mask(bits);
1177 1171 char buf[32];
1178 1172
1179 1173 snprintf(buf, sizeof(buf), "__%s_MAX__", name);
1180 1174 predefine(buf, 1, "%#llx%s", max, suffix);
1181 1175 }
1182 1176
1183 1177 static void predefined_min(const char *name, struct symbol *type)
1184 1178 {
1185 1179 const char *suffix = builtin_type_suffix(type);
1186 1180 char buf[32];
1187 1181
1188 1182 snprintf(buf, sizeof(buf), "__%s_MIN__", name);
1189 1183
1190 1184 if (is_signed_type(type))
1191 1185 predefine(buf, 1, "(-__%s_MAX__ - 1)", name);
1192 1186 else
1193 1187 predefine(buf, 1, "0%s", suffix);
1194 1188 }
1195 1189
1196 1190 static void predefined_type(const char *name, struct symbol *type)
1197 1191 {
1198 1192 const char *typename = builtin_typename(type);
1199 1193 add_pre_buffer("#weak_define __%s_TYPE__ %s\n", name, typename);
1200 1194 }
1201 1195
1202 1196 static void predefined_ctype(const char *name, struct symbol *type, int flags)
1203 1197 {
1204 1198 unsigned bits = type->bit_size;
1205 1199
1206 1200 if (flags & PTYPE_SIZEOF) {
1207 1201 const char *suffix = (flags & PTYPE_T) ? "_T" : "";
1208 1202 predefined_sizeof(name, suffix, bits);
1209 1203 }
1210 1204 if (flags & PTYPE_MAX)
1211 1205 predefined_max(name, type);
1212 1206 if (flags & PTYPE_MIN)
1213 1207 predefined_min(name, type);
1214 1208 if (flags & PTYPE_TYPE)
1215 1209 predefined_type(name, type);
1216 1210 if (flags & PTYPE_WIDTH)
1217 1211 predefined_width(name, bits);
1218 1212 }
1219 1213
1220 1214 static void predefined_macros(void)
1221 1215 {
1222 1216 predefine("__CHECKER__", 0, "1");
1223 1217 predefine("__GNUC__", 1, "%d", gcc_major);
1224 1218 predefine("__GNUC_MINOR__", 1, "%d", gcc_minor);
1225 1219 predefine("__GNUC_PATCHLEVEL__", 1, "%d", gcc_patchlevel);
1226 1220
1227 1221 predefine("__STDC__", 1, "1");
1228 1222 switch (standard) {
1229 1223 case STANDARD_C89:
1230 1224 predefine("__STRICT_ANSI__", 1, "1");
1231 1225 break;
1232 1226
1233 1227 case STANDARD_C94:
1234 1228 predefine("__STDC_VERSION__", 1, "199409L");
1235 1229 predefine("__STRICT_ANSI__", 1, "1");
1236 1230 break;
1237 1231
1238 1232 case STANDARD_C99:
1239 1233 predefine("__STDC_VERSION__", 1, "199901L");
1240 1234 predefine("__STRICT_ANSI__", 1, "1");
1241 1235 break;
1242 1236
1243 1237 case STANDARD_GNU89:
1244 1238 default:
1245 1239 break;
1246 1240
1247 1241 case STANDARD_GNU99:
1248 1242 predefine("__STDC_VERSION__", 1, "199901L");
1249 1243 break;
1250 1244
1251 1245 case STANDARD_C11:
1252 1246 predefine("__STRICT_ANSI__", 1, "1");
1253 1247 case STANDARD_GNU11:
1254 1248 predefine("__STDC_NO_ATOMICS__", 1, "1");
1255 1249 predefine("__STDC_NO_COMPLEX__", 1, "1");
1256 1250 predefine("__STDC_NO_THREADS__", 1, "1");
1257 1251 predefine("__STDC_VERSION__", 1, "201112L");
1258 1252 break;
1259 1253 }
1260 1254
1261 1255 predefine("__CHAR_BIT__", 1, "%d", bits_in_char);
1262 1256 if (funsigned_char)
1263 1257 predefine("__CHAR_UNSIGNED__", 1, "1");
1264 1258
1265 1259 predefined_ctype("SHORT", &short_ctype, PTYPE_SIZEOF);
1266 1260 predefined_ctype("SHRT", &short_ctype, PTYPE_MAX|PTYPE_WIDTH);
1267 1261 predefined_ctype("SCHAR", &schar_ctype, PTYPE_MAX|PTYPE_WIDTH);
1268 1262 predefined_ctype("WCHAR", wchar_ctype, PTYPE_ALL_T|PTYPE_MIN|PTYPE_TYPE);
1269 1263 predefined_ctype("WINT", wint_ctype, PTYPE_ALL_T|PTYPE_MIN|PTYPE_TYPE);
1270 1264 predefined_ctype("CHAR16", &ushort_ctype, PTYPE_TYPE);
1271 1265 predefined_ctype("CHAR32", &uint_ctype, PTYPE_TYPE);
1272 1266
1273 1267 predefined_ctype("INT", &int_ctype, PTYPE_ALL);
1274 1268 predefined_ctype("LONG", &long_ctype, PTYPE_ALL);
1275 1269 predefined_ctype("LONG_LONG", &llong_ctype, PTYPE_ALL);
1276 1270
1277 1271 predefined_ctype("INT8", &schar_ctype, PTYPE_MAX|PTYPE_TYPE);
1278 1272 predefined_ctype("UINT8", &uchar_ctype, PTYPE_MAX|PTYPE_TYPE);
1279 1273 predefined_ctype("INT16", &short_ctype, PTYPE_MAX|PTYPE_TYPE);
1280 1274 predefined_ctype("UINT16", &ushort_ctype, PTYPE_MAX|PTYPE_TYPE);
1281 1275 predefined_ctype("INT32", int32_ctype, PTYPE_MAX|PTYPE_TYPE);
1282 1276 predefined_ctype("UINT32", uint32_ctype, PTYPE_MAX|PTYPE_TYPE);
1283 1277 predefined_ctype("INT64", int64_ctype, PTYPE_MAX|PTYPE_TYPE);
1284 1278 predefined_ctype("UINT64", uint64_ctype, PTYPE_MAX|PTYPE_TYPE);
1285 1279
1286 1280 predefined_sizeof("INT128", "", 128);
1287 1281
1288 1282 predefined_ctype("INTMAX", intmax_ctype, PTYPE_MAX|PTYPE_TYPE|PTYPE_WIDTH);
1289 1283 predefined_ctype("UINTMAX", uintmax_ctype, PTYPE_MAX|PTYPE_TYPE);
1290 1284 predefined_ctype("INTPTR", ssize_t_ctype, PTYPE_MAX|PTYPE_TYPE|PTYPE_WIDTH);
1291 1285 predefined_ctype("UINTPTR", size_t_ctype, PTYPE_MAX|PTYPE_TYPE);
1292 1286 predefined_ctype("PTRDIFF", ssize_t_ctype, PTYPE_ALL_T|PTYPE_TYPE);
1293 1287 predefined_ctype("SIZE", size_t_ctype, PTYPE_ALL_T|PTYPE_TYPE);
1294 1288 predefined_ctype("POINTER", &ptr_ctype, PTYPE_SIZEOF);
1295 1289
1296 1290 predefined_sizeof("FLOAT", "", bits_in_float);
1297 1291 predefined_sizeof("DOUBLE", "", bits_in_double);
1298 1292 predefined_sizeof("LONG_DOUBLE", "", bits_in_longdouble);
1299 1293
1300 1294 predefine("__ORDER_LITTLE_ENDIAN__", 1, "1234");
1301 1295 predefine("__ORDER_BIG_ENDIAN__", 1, "4321");
1302 1296 predefine("__ORDER_PDP_ENDIAN__", 1, "3412");
1303 1297 if (arch_big_endian) {
1304 1298 predefine("__BIG_ENDIAN__", 1, "1");
1305 1299 predefine("__BYTE_ORDER__", 1, "__ORDER_BIG_ENDIAN__");
↓ open down ↓ |
968 lines elided |
↑ open up ↑ |
1306 1300 } else {
1307 1301 predefine("__LITTLE_ENDIAN__", 1, "1");
1308 1302 predefine("__BYTE_ORDER__", 1, "__ORDER_LITTLE_ENDIAN__");
1309 1303 }
1310 1304
1311 1305 if (optimize_level)
1312 1306 predefine("__OPTIMIZE__", 0, "1");
1313 1307 if (optimize_size)
1314 1308 predefine("__OPTIMIZE_SIZE__", 0, "1");
1315 1309
1310 + predefine("__PRAGMA_REDEFINE_EXTNAME", 1, "1");
1311 +
1316 1312 // Temporary hacks
1317 1313 predefine("__extension__", 0, NULL);
1318 1314 predefine("__pragma__", 0, NULL);
1319 1315
1320 1316 switch (arch_m64) {
1321 1317 case ARCH_LP32:
1322 1318 break;
1323 1319 case ARCH_X32:
1324 1320 predefine("__ILP32__", 1, "1");
1325 1321 predefine("_ILP32", 1, "1");
1326 1322 break;
1327 1323 case ARCH_LP64:
1328 1324 predefine("__LP64__", 1, "1");
1329 - predefine("__LP64", 1, "1");
1330 1325 predefine("_LP64", 1, "1");
1331 1326 break;
1332 1327 case ARCH_LLP64:
1333 1328 predefine("__LLP64__", 1, "1");
1334 1329 break;
1335 1330 }
1336 1331
1337 1332 switch (arch_mach) {
1338 1333 case MACH_ARM64:
1339 1334 predefine("__aarch64__", 1, "1");
1340 1335 break;
1341 1336 case MACH_ARM:
1342 1337 predefine("__arm__", 1, "1");
1343 1338 break;
1344 1339 case MACH_M68K:
1345 1340 predefine("__m68k__", 1, "1");
1346 1341 break;
1347 1342 case MACH_MIPS64:
1348 1343 if (arch_m64 == ARCH_LP64)
1349 1344 predefine("__mips64", 1, "64");
1350 1345 /* fall-through */
1351 1346 case MACH_MIPS32:
1352 1347 predefine("__mips", 1, "%d", ptr_ctype.bit_size);
1353 1348 predefine("_MIPS_SZINT", 1, "%d", int_ctype.bit_size);
1354 1349 predefine("_MIPS_SZLONG", 1, "%d", long_ctype.bit_size);
1355 1350 predefine("_MIPS_SZPTR", 1, "%d", ptr_ctype.bit_size);
1356 1351 break;
1357 1352 case MACH_PPC64:
1358 1353 if (arch_m64 == ARCH_LP64) {
1359 1354 predefine("__powerpc64__", 1, "1");
1360 1355 predefine("__ppc64__", 1, "1");
1361 1356 predefine("__PPC64__", 1, "1");
1362 1357 }
1363 1358 /* fall-through */
1364 1359 case MACH_PPC32:
1365 1360 predefine("__powerpc__", 1, "1");
1366 1361 predefine("__powerpc", 1, "1");
1367 1362 predefine("__ppc__", 1, "1");
1368 1363 predefine("__PPC__", 1, "1");
1369 1364 break;
1370 1365 case MACH_RISCV64:
1371 1366 case MACH_RISCV32:
1372 1367 predefine("__riscv", 1, "1");
1373 1368 predefine("__riscv_xlen", 1, "%d", ptr_ctype.bit_size);
1374 1369 break;
1375 1370 case MACH_S390X:
1376 1371 predefine("__zarch__", 1, "1");
1377 1372 predefine("__s390x__", 1, "1");
1378 1373 predefine("__s390__", 1, "1");
1379 1374 break;
1380 1375 case MACH_SPARC64:
1381 1376 if (arch_m64 == ARCH_LP64) {
↓ open down ↓ |
42 lines elided |
↑ open up ↑ |
1382 1377 predefine("__sparc_v9__", 1, "1");
1383 1378 predefine("__sparcv9__", 1, "1");
1384 1379 predefine("__sparcv9", 1, "1");
1385 1380 predefine("__sparc64__", 1, "1");
1386 1381 predefine("__arch64__", 1, "1");
1387 1382 }
1388 1383 /* fall-through */
1389 1384 case MACH_SPARC32:
1390 1385 predefine("__sparc__", 1, "1");
1391 1386 predefine("__sparc", 1, "1");
1387 + predefine_nostd("sparc");
1392 1388 break;
1393 1389 case MACH_X86_64:
1394 1390 if (arch_m64 != ARCH_LP32) {
1395 1391 predefine("__x86_64__", 1, "1");
1396 1392 predefine("__x86_64", 1, "1");
1393 + predefine("__amd64__", 1, "1");
1394 + predefine("__amd64", 1, "1");
1397 1395 break;
1398 1396 }
1399 1397 /* fall-through */
1400 1398 case MACH_I386:
1401 1399 predefine("__i386__", 1, "1");
1402 1400 predefine("__i386", 1, "1");
1403 - predefine("i386", 1, "1");
1401 + predefine_nostd("i386");
1404 1402 break;
1405 1403 }
1406 1404
1407 - predefine("__PRAGMA_REDEFINE_EXTNAME", 1, "1");
1408 -
1409 -#ifdef __sun
1405 +#if defined(__unix__)
1410 1406 predefine("__unix__", 1, "1");
1411 1407 predefine("__unix", 1, "1");
1412 - predefine("unix", 1, "1");
1408 + predefine_nostd("unix");
1409 +#endif
1410 +
1411 +
1412 +#if defined(__sun__) || defined(__sun)
1413 1413 predefine("__sun__", 1, "1");
1414 1414 predefine("__sun", 1, "1");
1415 - predefine("sun", 1, "1");
1415 + predefine_nostd("sun");
1416 1416 predefine("__svr4__", 1, "1");
1417 1417 #endif
1418 +
1418 1419 }
1419 1420
1420 1421 static void create_builtin_stream(void)
1421 1422 {
1422 1423 // Temporary hack
1423 1424 add_pre_buffer("#define _Pragma(x)\n");
1424 1425
1425 1426 /* add the multiarch include directories, if any */
1426 1427 if (multiarch_dir && *multiarch_dir) {
1427 1428 add_pre_buffer("#add_system \"/usr/include/%s\"\n", multiarch_dir);
1428 1429 add_pre_buffer("#add_system \"/usr/local/include/%s\"\n", multiarch_dir);
1429 1430 }
1430 1431
1431 1432 /* We add compiler headers path here because we have to parse
1432 1433 * the arguments to get it, falling back to default. */
1433 1434 add_pre_buffer("#add_system \"%s/include\"\n", gcc_base_dir);
1434 1435 add_pre_buffer("#add_system \"%s/include-fixed\"\n", gcc_base_dir);
1435 1436
1436 1437 add_pre_buffer("#define __has_builtin(x) 0\n");
1437 1438 add_pre_buffer("#define __has_attribute(x) 0\n");
1438 1439 add_pre_buffer("#define __builtin_stdarg_start(a,b) ((a) = (__builtin_va_list)(&(b)))\n");
1439 1440 add_pre_buffer("#define __builtin_va_start(a,b) ((a) = (__builtin_va_list)(&(b)))\n");
1440 1441 add_pre_buffer("#define __builtin_ms_va_start(a,b) ((a) = (__builtin_ms_va_list)(&(b)))\n");
1441 1442 add_pre_buffer("#define __builtin_va_arg(arg,type) ({ type __va_arg_ret = *(type *)(arg); arg += sizeof(type); __va_arg_ret; })\n");
1442 1443 add_pre_buffer("#define __builtin_va_alist (*(void *)0)\n");
1443 1444 add_pre_buffer("#define __builtin_va_arg_incr(x) ((x) + 1)\n");
1444 1445 add_pre_buffer("#define __builtin_va_copy(dest, src) ({ dest = src; (void)0; })\n");
1445 1446 add_pre_buffer("#define __builtin_ms_va_copy(dest, src) ({ dest = src; (void)0; })\n");
1446 1447 add_pre_buffer("#define __builtin_va_end(arg)\n");
1447 1448 add_pre_buffer("#define __builtin_ms_va_end(arg)\n");
1448 1449 add_pre_buffer("#define __builtin_va_arg_pack()\n");
1449 1450 }
1450 1451
1451 1452 static struct symbol_list *sparse_tokenstream(struct token *token)
1452 1453 {
1453 1454 int builtin = token && !token->pos.stream;
1454 1455
1455 1456 // Preprocess the stream
1456 1457 token = preprocess(token);
1457 1458
1458 1459 if (dump_macro_defs || dump_macros_only) {
1459 1460 if (!builtin)
1460 1461 dump_macro_definitions();
1461 1462 if (dump_macros_only)
1462 1463 return NULL;
1463 1464 }
1464 1465
1465 1466 if (preprocess_only) {
1466 1467 while (!eof_token(token)) {
1467 1468 int prec = 1;
1468 1469 struct token *next = token->next;
1469 1470 const char *separator = "";
1470 1471 if (next->pos.whitespace)
1471 1472 separator = " ";
1472 1473 if (next->pos.newline) {
1473 1474 separator = "\n\t\t\t\t\t";
1474 1475 prec = next->pos.pos;
1475 1476 if (prec > 4)
1476 1477 prec = 4;
1477 1478 }
1478 1479 printf("%s%.*s", show_token(token), prec, separator);
1479 1480 token = next;
1480 1481 }
1481 1482 putchar('\n');
1482 1483
1483 1484 return NULL;
1484 1485 }
1485 1486
1486 1487 // Parse the resulting C code
1487 1488 while (!eof_token(token))
1488 1489 token = external_declaration(token, &translation_unit_used_list, NULL);
1489 1490 return translation_unit_used_list;
1490 1491 }
1491 1492
1492 1493 static struct symbol_list *sparse_file(const char *filename)
1493 1494 {
1494 1495 int fd;
1495 1496 struct token *token;
1496 1497
1497 1498 if (strcmp (filename, "-") == 0) {
1498 1499 fd = 0;
1499 1500 } else {
1500 1501 fd = open(filename, O_RDONLY);
1501 1502 if (fd < 0)
1502 1503 die("No such file: %s", filename);
1503 1504 }
1504 1505 base_filename = filename;
1505 1506
1506 1507 // Tokenize the input stream
1507 1508 token = tokenize(filename, fd, NULL, includepath);
1508 1509 store_all_tokens(token);
1509 1510 close(fd);
1510 1511
1511 1512 return sparse_tokenstream(token);
1512 1513 }
1513 1514
1514 1515 /*
1515 1516 * This handles the "-include" directive etc: we're in global
1516 1517 * scope, and all types/macros etc will affect all the following
1517 1518 * files.
1518 1519 *
1519 1520 * NOTE NOTE NOTE! "#undef" of anything in this stage will
1520 1521 * affect all subsequent files too, i.e. we can have non-local
1521 1522 * behaviour between files!
1522 1523 */
1523 1524 static struct symbol_list *sparse_initial(void)
1524 1525 {
1525 1526 int i;
1526 1527
1527 1528 // Prepend any "include" file to the stream.
1528 1529 // We're in global scope, it will affect all files!
1529 1530 for (i = 0; i < cmdline_include_nr; i++)
1530 1531 add_pre_buffer("#argv_include \"%s\"\n", cmdline_include[i]);
1531 1532
1532 1533 return sparse_tokenstream(pre_buffer_begin);
1533 1534 }
1534 1535
1535 1536 static int endswith(const char *str, const char *suffix)
1536 1537 {
1537 1538 const char *found = strstr(str, suffix);
1538 1539 return (found && strcmp(found, suffix) == 0);
1539 1540 }
1540 1541
1541 1542 struct symbol_list *sparse_initialize(int argc, char **argv, struct string_list **filelist)
1542 1543 {
1543 1544 char **args;
1544 1545 struct symbol_list *list;
1545 1546
1546 1547 // Initialize symbol stream first, so that we can add defines etc
1547 1548 init_symbols();
1548 1549 init_include_path();
1549 1550
1550 1551 diag_prefix = argv[0];
1551 1552
1552 1553 args = argv;
1553 1554 for (;;) {
1554 1555 char *arg = *++args;
1555 1556 if (!arg)
1556 1557 break;
1557 1558
1558 1559 if (arg[0] == '-' && arg[1]) {
1559 1560 args = handle_switch(arg+1, args);
1560 1561 continue;
1561 1562 }
1562 1563
1563 1564 if (endswith(arg, ".a") || endswith(arg, ".so") ||
1564 1565 endswith(arg, ".so.1") || endswith(arg, ".o"))
1565 1566 continue;
1566 1567
1567 1568 add_ptr_list(filelist, arg);
1568 1569 }
1569 1570 handle_switch_W_finalize();
1570 1571 handle_switch_v_finalize();
1571 1572
1572 1573 // Redirect stdout if needed
1573 1574 if (dump_macro_defs || preprocess_only)
1574 1575 do_output = 1;
1575 1576 if (do_output && outfile && strcmp(outfile, "-")) {
1576 1577 if (!freopen(outfile, "w", stdout))
1577 1578 die("error: cannot open %s: %s", outfile, strerror(errno));
1578 1579 }
1579 1580
1580 1581 if (fdump_ir == 0)
1581 1582 fdump_ir = PASS_FINAL;
1582 1583
1583 1584 list = NULL;
1584 1585 if (filelist) {
1585 1586 // Initialize type system
1586 1587 init_target();
1587 1588 handle_arch_finalize();
1588 1589 init_ctype();
1589 1590
1590 1591 predefined_macros();
1591 1592 create_builtin_stream();
1592 1593 declare_builtins();
1593 1594
1594 1595 list = sparse_initial();
1595 1596
1596 1597 /*
1597 1598 * Protect the initial token allocations, since
1598 1599 * they need to survive all the others
1599 1600 */
1600 1601 protect_token_alloc();
1601 1602 }
1602 1603 /*
1603 1604 * Evaluate the complete symbol list
1604 1605 * Note: This is not needed for normal cases.
1605 1606 * These symbols should only be predefined defines and
1606 1607 * declaratons which will be evaluated later, when needed.
1607 1608 * This is also the case when a file is directly included via
1608 1609 * '-include <file>' on the command line *AND* the file only
1609 1610 * contains defines, declarations and inline definitions.
1610 1611 * However, in the rare cases where the given file should
1611 1612 * contain some definitions, these will never be evaluated
1612 1613 * and thus won't be able to be linearized correctly.
1613 1614 * Hence the evaluate_symbol_list() here under.
1614 1615 */
1615 1616 evaluate_symbol_list(list);
1616 1617 return list;
1617 1618 }
1618 1619
1619 1620 struct symbol_list * sparse_keep_tokens(char *filename)
1620 1621 {
1621 1622 struct symbol_list *res;
1622 1623
1623 1624 /* Clear previous symbol list */
1624 1625 translation_unit_used_list = NULL;
1625 1626
1626 1627 new_file_scope();
1627 1628 res = sparse_file(filename);
1628 1629
1629 1630 /* And return it */
1630 1631 return res;
1631 1632 }
1632 1633
1633 1634
1634 1635 struct symbol_list * __sparse(char *filename)
1635 1636 {
1636 1637 struct symbol_list *res;
1637 1638
1638 1639 res = sparse_keep_tokens(filename);
1639 1640
1640 1641 /* Drop the tokens for this file after parsing */
1641 1642 clear_token_alloc();
1642 1643
1643 1644 /* And return it */
1644 1645 return res;
1645 1646 }
1646 1647
1647 1648 struct symbol_list * sparse(char *filename)
1648 1649 {
1649 1650 struct symbol_list *res = __sparse(filename);
1650 1651
1651 1652 if (has_error & ERROR_CURR_PHASE)
1652 1653 has_error = ERROR_PREV_PHASE;
1653 1654 /* Evaluate the complete symbol list */
1654 1655 evaluate_symbol_list(res);
1655 1656
1656 1657 return res;
1657 1658 }
↓ open down ↓ |
230 lines elided |
↑ open up ↑ |
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX