Adding debug option for showing the linearized instruction.
[smatch.git] / lib.c
blobad66d93f543fe7a0872fd2094a8fc2fad9f7ace4
1 /*
2 * 'sparse' library helper routines.
4 * Copyright (C) 2003 Transmeta Corp.
5 * 2003-2004 Linus Torvalds
7 * Licensed under the Open Software License version 1.1
8 */
9 #include <ctype.h>
10 #include <fcntl.h>
11 #include <stdarg.h>
12 #include <stddef.h>
13 #include <stdio.h>
14 #include <stdlib.h>
15 #include <string.h>
16 #include <unistd.h>
17 #include <assert.h>
19 #include <sys/types.h>
21 #include "lib.h"
22 #include "allocate.h"
23 #include "token.h"
24 #include "parse.h"
25 #include "symbol.h"
26 #include "expression.h"
27 #include "scope.h"
28 #include "linearize.h"
29 #include "target.h"
31 int verbose, optimize, optimize_size, preprocessing;
32 int die_if_error = 0;
34 #ifndef __GNUC__
35 # define __GNUC__ 2
36 # define __GNUC_MINOR__ 95
37 # define __GNUC_PATCHLEVEL__ 0
38 #endif
40 int gcc_major = __GNUC__;
41 int gcc_minor = __GNUC_MINOR__;
42 int gcc_patchlevel = __GNUC_PATCHLEVEL__;
44 struct token *skip_to(struct token *token, int op)
46 while (!match_op(token, op) && !eof_token(token))
47 token = token->next;
48 return token;
51 struct token *expect(struct token *token, int op, const char *where)
53 if (!match_op(token, op)) {
54 static struct token bad_token;
55 if (token != &bad_token) {
56 bad_token.next = token;
57 sparse_error(token->pos, "Expected %s %s", show_special(op), where);
58 sparse_error(token->pos, "got %s", show_token(token));
60 if (op == ';')
61 return skip_to(token, op);
62 return &bad_token;
64 return token->next;
67 unsigned int hexval(unsigned int c)
69 int retval = 256;
70 switch (c) {
71 case '0'...'9':
72 retval = c - '0';
73 break;
74 case 'a'...'f':
75 retval = c - 'a' + 10;
76 break;
77 case 'A'...'F':
78 retval = c - 'A' + 10;
79 break;
81 return retval;
84 static void do_warn(const char *type, struct position pos, const char * fmt, va_list args)
86 static char buffer[512];
87 const char *name;
89 vsprintf(buffer, fmt, args);
90 name = stream_name(pos.stream);
92 fprintf(stderr, "%s:%d:%d: %s%s\n",
93 name, pos.line, pos.pos, type, buffer);
96 static int max_warnings = 100;
97 static int show_info = 1;
99 void info(struct position pos, const char * fmt, ...)
101 va_list args;
103 if (!show_info)
104 return;
105 va_start(args, fmt);
106 do_warn("", pos, fmt, args);
107 va_end(args);
110 void warning(struct position pos, const char * fmt, ...)
112 va_list args;
114 if (!max_warnings) {
115 show_info = 0;
116 return;
119 if (!--max_warnings) {
120 show_info = 0;
121 fmt = "too many warnings";
124 va_start(args, fmt);
125 do_warn("warning: ", pos, fmt, args);
126 va_end(args);
129 void sparse_error(struct position pos, const char * fmt, ...)
131 static int errors = 0;
132 va_list args;
133 die_if_error = 1;
134 show_info = 1;
135 /* Shut up warnings after an error */
136 max_warnings = 0;
137 if (errors > 100) {
138 static int once = 0;
139 show_info = 0;
140 if (once)
141 return;
142 fmt = "too many errors";
143 once = 1;
146 va_start(args, fmt);
147 do_warn("error: ", pos, fmt, args);
148 va_end(args);
149 errors++;
152 void error_die(struct position pos, const char * fmt, ...)
154 va_list args;
155 va_start(args, fmt);
156 do_warn("error: ", pos, fmt, args);
157 va_end(args);
158 exit(1);
161 void die(const char *fmt, ...)
163 va_list args;
164 static char buffer[512];
166 va_start(args, fmt);
167 vsnprintf(buffer, sizeof(buffer), fmt, args);
168 va_end(args);
170 fprintf(stderr, "%s\n", buffer);
171 exit(1);
174 static unsigned int pre_buffer_size;
175 static char pre_buffer[8192];
177 int Wdefault_bitfield_sign = 0;
178 int Wone_bit_signed_bitfield = 1;
179 int Wcast_truncate = 1;
180 int Wbitwise = 0;
181 int Wtypesign = 0;
182 int Wcontext = 1;
183 int Wundefined_preprocessor = 0;
184 int Wptr_subtraction_blows = 0;
185 int Wcast_to_address_space = 0;
186 int Wdecl = 0;
187 int Wtransparent_union = 1;
188 int Wshadow = 0;
189 int Waddress_space = 1;
190 int Wenum_mismatch = 1;
191 int Wdo_while = 1;
192 int Wuninitialized = 1;
194 int dbg_entry;
196 int preprocess_only;
197 char *include;
199 #define CMDLINE_INCLUDE 20
200 int cmdline_include_nr = 0;
201 struct cmdline_include cmdline_include[CMDLINE_INCLUDE];
204 void add_pre_buffer(const char *fmt, ...)
206 va_list args;
207 unsigned int size;
209 va_start(args, fmt);
210 size = pre_buffer_size;
211 size += vsnprintf(pre_buffer + size,
212 sizeof(pre_buffer) - size,
213 fmt, args);
214 pre_buffer_size = size;
215 va_end(args);
218 static char **handle_switch_D(char *arg, char **next)
220 const char *name = arg + 1;
221 const char *value = "1";
222 for (;;) {
223 char c;
224 c = *++arg;
225 if (!c)
226 break;
227 if (isspace((unsigned char)c) || c == '=') {
228 *arg = '\0';
229 value = arg + 1;
230 break;
233 add_pre_buffer("#define %s %s\n", name, value);
234 return next;
237 static char **handle_switch_E(char *arg, char **next)
239 preprocess_only = 1;
240 return next;
243 static char **handle_switch_I(char *arg, char **next)
245 char *path = arg+1;
247 switch (arg[1]) {
248 case '-':
249 add_pre_buffer("#split_include\n");
250 break;
252 case '\0': /* Plain "-I" */
253 path = *++next;
254 if (!path)
255 die("missing argument for -I option");
256 /* Fallthrough */
257 default:
258 add_pre_buffer("#add_include \"%s/\"\n", path);
260 return next;
263 static void add_cmdline_include(char *filename)
265 int fd = open(filename, O_RDONLY);
266 if (fd < 0) {
267 perror(filename);
268 return;
270 if (cmdline_include_nr >= CMDLINE_INCLUDE)
271 die("too many include files for %s\n", filename);
272 cmdline_include[cmdline_include_nr].filename = filename;
273 cmdline_include[cmdline_include_nr].fd = fd;
274 cmdline_include_nr++;
277 static char **handle_switch_i(char *arg, char **next)
279 if (*next && !strcmp(arg, "include"))
280 add_cmdline_include(*++next);
281 else if (*next && !strcmp(arg, "imacros"))
282 add_cmdline_include(*++next);
283 else if (*next && !strcmp(arg, "isystem")) {
284 char *path = *++next;
285 if (!path)
286 die("missing argument for -isystem option");
287 add_pre_buffer("#add_isystem \"%s/\"\n", path);
289 return next;
292 static char **handle_switch_M(char *arg, char **next)
294 if (!strcmp(arg, "MF") || !strcmp(arg,"MQ") || !strcmp(arg,"MT")) {
295 if (!*next)
296 die("missing argument for -%s option", arg);
297 return next + 1;
299 return next;
302 static char **handle_switch_m(char *arg, char **next)
304 if (!strcmp(arg, "m64")) {
305 bits_in_long = 64;
306 max_int_alignment = 8;
307 bits_in_pointer = 64;
308 pointer_alignment = 8;
310 return next;
313 static char **handle_switch_o(char *arg, char **next)
315 if (!strcmp (arg, "o") && *next)
316 return next + 1; // "-o foo"
317 else
318 return next; // "-ofoo" or (bogus) terminal "-o"
321 static const struct warning {
322 const char *name;
323 int *flag;
324 } warnings[] = {
325 { "cast-to-as", &Wcast_to_address_space },
326 { "decl", &Wdecl },
327 { "one-bit-signed-bitfield", &Wone_bit_signed_bitfield },
328 { "cast-truncate", &Wcast_truncate },
329 { "ptr-subtraction-blows", &Wptr_subtraction_blows },
330 { "default-bitfield-sign", &Wdefault_bitfield_sign },
331 { "undef", &Wundefined_preprocessor },
332 { "bitwise", &Wbitwise },
333 { "typesign", &Wtypesign },
334 { "context", &Wcontext },
335 { "transparent-union", &Wtransparent_union },
336 { "shadow", &Wshadow },
337 { "address-space", &Waddress_space },
338 { "enum-mismatch", &Wenum_mismatch },
339 { "do-while", &Wdo_while },
340 { "uninitialized", &Wuninitialized },
343 enum {
344 WARNING_OFF,
345 WARNING_ON,
346 WARNING_FORCE_OFF
350 static char **handle_onoff_switch(char *arg, char **next, const struct warning warnings[], int n)
352 int flag = WARNING_ON;
353 char *p = arg + 1;
354 unsigned i;
356 if (!strcmp(p, "all")) {
357 for (i = 0; i < n; i++) {
358 if (*warnings[i].flag != WARNING_FORCE_OFF)
359 *warnings[i].flag = WARNING_ON;
363 // Prefixes "no" and "no-" mean to turn warning off.
364 if (p[0] == 'n' && p[1] == 'o') {
365 p += 2;
366 if (p[0] == '-')
367 p++;
368 flag = WARNING_FORCE_OFF;
371 for (i = 0; i < n; i++) {
372 if (!strcmp(p,warnings[i].name)) {
373 *warnings[i].flag = flag;
374 return next;
378 // Unknown.
379 return NULL;
382 static char **handle_switch_W(char *arg, char **next)
384 char ** ret = handle_onoff_switch(arg, next, warnings, sizeof warnings/sizeof warnings[0]);
385 if (ret)
386 return ret;
388 // Unknown.
389 return next;
392 static struct warning debugs[] = {
393 { "entry", &dbg_entry},
397 static char **handle_switch_v(char *arg, char **next)
399 char ** ret = handle_onoff_switch(arg, next, debugs, sizeof debugs/sizeof debugs[0]);
400 if (ret)
401 return ret;
403 // Unknown.
404 do {
405 verbose++;
406 } while (*++arg == 'v');
407 return next;
411 static void handle_onoff_switch_finalize(const struct warning warnings[], int n)
413 unsigned i;
415 for (i = 0; i < n; i++) {
416 if (*warnings[i].flag == WARNING_FORCE_OFF)
417 *warnings[i].flag = WARNING_OFF;
421 static void handle_switch_W_finalize(void)
423 handle_onoff_switch_finalize(warnings, sizeof(warnings) / sizeof(warnings[0]));
426 static void handle_switch_v_finalize(void)
428 handle_onoff_switch_finalize(debugs, sizeof(debugs) / sizeof(debugs[0]));
431 static char **handle_switch_U(char *arg, char **next)
433 const char *name = arg + 1;
434 add_pre_buffer ("#undef %s\n", name);
435 return next;
438 static char **handle_switch_O(char *arg, char **next)
440 int level = 1;
441 if (arg[1] >= '0' && arg[1] <= '9')
442 level = arg[1] - '0';
443 optimize = level;
444 optimize_size = arg[1] == 's';
445 return next;
448 static char **handle_switch_f(char *arg, char **next)
450 int flag = 1;
452 arg++;
453 if (!strncmp(arg, "no-", 3)) {
454 flag = 0;
455 arg += 3;
457 /* handle switch here.. */
458 return next;
461 static char **handle_switch_G(char *arg, char **next)
463 if (!strcmp (arg, "G") && *next)
464 return next + 1; // "-G 0"
465 else
466 return next; // "-G0" or (bogus) terminal "-G"
469 static char **handle_nostdinc(char *arg, char **next)
471 add_pre_buffer("#nostdinc\n");
472 return next;
475 static char **handle_dirafter(char *arg, char **next)
477 char *path = *++next;
478 if (!path)
479 die("missing argument for -dirafter option");
480 add_pre_buffer("#add_dirafter \"%s/\"\n", path);
481 return next;
484 struct switches {
485 const char *name;
486 char **(*fn)(char *, char **);
489 char **handle_switch(char *arg, char **next)
491 static struct switches cmd[] = {
492 { "nostdinc", handle_nostdinc },
493 { "dirafter", handle_dirafter },
494 { NULL, NULL }
496 struct switches *s;
498 switch (*arg) {
499 case 'D': return handle_switch_D(arg, next);
500 case 'E': return handle_switch_E(arg, next);
501 case 'I': return handle_switch_I(arg, next);
502 case 'i': return handle_switch_i(arg, next);
503 case 'M': return handle_switch_M(arg, next);
504 case 'm': return handle_switch_m(arg, next);
505 case 'o': return handle_switch_o(arg, next);
506 case 'U': return handle_switch_U(arg, next);
507 case 'v': return handle_switch_v(arg, next);
508 case 'W': return handle_switch_W(arg, next);
509 case 'O': return handle_switch_O(arg, next);
510 case 'f': return handle_switch_f(arg, next);
511 case 'G': return handle_switch_G(arg, next);
512 default:
513 break;
516 s = cmd;
517 while (s->name) {
518 if (!strcmp(s->name, arg))
519 return s->fn(arg, next);
520 s++;
524 * Ignore unknown command line options:
525 * they're probably gcc switches
527 return next;
530 void declare_builtin_functions(void)
532 /* Gaah. gcc knows tons of builtin <string.h> functions */
533 add_pre_buffer("extern void *__builtin_memcpy(void *, const void *, __SIZE_TYPE__);\n");
534 add_pre_buffer("extern void *__builtin_memset(void *, int, __SIZE_TYPE__);\n");
535 add_pre_buffer("extern int __builtin_memcmp(const void *, const void *, __SIZE_TYPE__);\n");
536 add_pre_buffer("extern int __builtin_strcmp(const char *, const char *);\n");
537 add_pre_buffer("extern char *__builtin_strchr(const char *, int);\n");
538 add_pre_buffer("extern char *__builtin_strcpy(char *, const char *);\n");
539 add_pre_buffer("extern char *__builtin_strncpy(char *, const char *, __SIZE_TYPE__);\n");
540 add_pre_buffer("extern __SIZE_TYPE__ __builtin_strspn(const char *, const char *);\n");
541 add_pre_buffer("extern __SIZE_TYPE__ __builtin_strcspn(const char *, const char *);\n");
542 add_pre_buffer("extern char * __builtin_strpbrk(const char *, const char *);\n");
544 /* And some random ones.. */
545 add_pre_buffer("extern void *__builtin_return_address(unsigned int);\n");
546 add_pre_buffer("extern void *__builtin_extract_return_addr(void *);\n");
547 add_pre_buffer("extern void *__builtin_frame_address(unsigned int);\n");
548 add_pre_buffer("extern void __builtin_trap(void);\n");
549 add_pre_buffer("extern int __builtin_ffs(int);\n");
550 add_pre_buffer("extern void *__builtin_alloca(__SIZE_TYPE__);\n");
551 add_pre_buffer("extern int __builtin_popcount(unsigned int);\n");
552 add_pre_buffer("extern int __builtin_popcountl(unsigned long);\n");
553 add_pre_buffer("extern void __builtin_prefetch (const void *, ...);\n");
554 add_pre_buffer("extern long __builtin_alpha_extbl(long, long);\n");
555 add_pre_buffer("extern long __builtin_alpha_extwl(long, long);\n");
556 add_pre_buffer("extern long __builtin_alpha_insbl(long, long);\n");
557 add_pre_buffer("extern long __builtin_alpha_inswl(long, long);\n");
558 add_pre_buffer("extern long __builtin_alpha_insql(long, long);\n");
559 add_pre_buffer("extern long __builtin_alpha_inslh(long, long);\n");
560 add_pre_buffer("extern long __builtin_alpha_cmpbge(long, long);\n");
563 void create_builtin_stream(void)
565 add_pre_buffer("#weak_define __GNUC__ %d\n", gcc_major);
566 add_pre_buffer("#weak_define __GNUC_MINOR__ %d\n", gcc_minor);
567 add_pre_buffer("#weak_define __GNUC_PATCHLEVEL__ %d\n", gcc_patchlevel);
568 add_pre_buffer("#define __extension__\n");
569 add_pre_buffer("#define __pragma__\n");
571 // gcc defines __SIZE_TYPE__ to be size_t. For linux/i86 and
572 // solaris/sparc that is really "unsigned int" and for linux/x86_64
573 // it is "long unsigned int". In either case we can probably
574 // get away with this. We need the #ifndef as cgcc will define
575 // the right __SIZE_TYPE__.
576 add_pre_buffer("#weak_define __SIZE_TYPE__ long unsigned int\n");
577 add_pre_buffer("#weak_define __STDC__ 1\n");
579 add_pre_buffer("#define __builtin_stdarg_start(a,b) ((a) = (__builtin_va_list)(&(b)))\n");
580 add_pre_buffer("#define __builtin_va_start(a,b) ((a) = (__builtin_va_list)(&(b)))\n");
581 add_pre_buffer("#define __builtin_va_arg(arg,type) ({ type __va_arg_ret = *(type *)(arg); arg += sizeof(type); __va_arg_ret; })\n");
582 add_pre_buffer("#define __builtin_va_alist (*(void *)0)\n");
583 add_pre_buffer("#define __builtin_va_arg_incr(x) ((x) + 1)\n");
584 add_pre_buffer("#define __builtin_va_copy(dest, src) ({ dest = src; (void)0; })\n");
585 add_pre_buffer("#define __builtin_va_end(arg)\n");
586 add_pre_buffer("#define __builtin_offsetof(type, name) ((__SIZE_TYPE__)&((type *)(0ul))->name)\n");
588 /* FIXME! We need to do these as special magic macros at expansion time! */
589 add_pre_buffer("#define __BASE_FILE__ \"base_file.c\"\n");
590 add_pre_buffer("#define __DATE__ \"??? ?? ????\"\n");
591 add_pre_buffer("#define __TIME__ \"??:??:??\"\n");
593 if (optimize)
594 add_pre_buffer("#define __OPTIMIZE__ 1\n");
595 if (optimize_size)
596 add_pre_buffer("#define __OPTIMIZE_SIZE__ 1\n");
599 static struct symbol_list *sparse_tokenstream(struct token *token)
601 // Pre-process the stream
602 token = preprocess(token);
604 if (preprocess_only) {
605 while (!eof_token(token)) {
606 int prec = 1;
607 struct token *next = token->next;
608 const char *separator = "";
609 if (next->pos.whitespace)
610 separator = " ";
611 if (next->pos.newline) {
612 separator = "\n\t\t\t\t\t";
613 prec = next->pos.pos;
614 if (prec > 4)
615 prec = 4;
617 printf("%s%.*s", show_token(token), prec, separator);
618 token = next;
620 putchar('\n');
622 return NULL;
625 // Parse the resulting C code
626 while (!eof_token(token))
627 token = external_declaration(token, &translation_unit_used_list);
628 return translation_unit_used_list;
631 static struct symbol_list *sparse_file(const char *filename)
633 int fd;
634 struct token *token;
636 if (strcmp (filename, "-") == 0) {
637 fd = 0;
638 } else {
639 fd = open(filename, O_RDONLY);
640 if (fd < 0)
641 die("No such file: %s", filename);
644 // Tokenize the input stream
645 token = tokenize(filename, fd, NULL, includepath);
646 close(fd);
648 return sparse_tokenstream(token);
652 * This handles the "-include" directive etc: we're in global
653 * scope, and all types/macros etc will affect all the following
654 * files.
656 * NOTE NOTE NOTE! "#undef" of anything in this stage will
657 * affect all subsequent files too, ie we can have non-local
658 * behaviour between files!
660 static struct symbol_list *sparse_initial(void)
662 struct token *token;
663 int i;
665 // Prepend any "include" file to the stream.
666 // We're in global scope, it will affect all files!
667 token = NULL;
668 for (i = cmdline_include_nr - 1; i >= 0; i--)
669 token = tokenize(cmdline_include[i].filename, cmdline_include[i].fd,
670 token, includepath);
672 // Prepend the initial built-in stream
673 token = tokenize_buffer(pre_buffer, pre_buffer_size, token);
674 return sparse_tokenstream(token);
677 struct symbol_list *sparse_initialize(int argc, char **argv, struct string_list **filelist)
679 char **args;
680 struct symbol_list *list;
682 // Initialize symbol stream first, so that we can add defines etc
683 init_symbols();
685 args = argv;
686 for (;;) {
687 char *arg = *++args;
688 if (!arg)
689 break;
691 if (arg[0] == '-' && arg[1]) {
692 args = handle_switch(arg+1, args);
693 continue;
695 add_ptr_list_notag(filelist, arg);
697 handle_switch_W_finalize();
698 handle_switch_v_finalize();
700 list = NULL;
701 if (!ptr_list_empty(filelist)) {
702 // Initialize type system
703 init_ctype();
705 create_builtin_stream();
706 add_pre_buffer("#define __CHECKER__ 1\n");
707 if (!preprocess_only)
708 declare_builtin_functions();
710 list = sparse_initial();
713 * Protect the initial token allocations, since
714 * they need to survive all the others
716 protect_token_alloc();
718 return list;
721 struct symbol_list * __sparse(char *filename)
723 struct symbol_list *res;
725 /* Clear previous symbol list */
726 translation_unit_used_list = NULL;
728 new_file_scope();
729 res = sparse_file(filename);
731 /* Drop the tokens for this file after parsing */
732 clear_token_alloc();
734 /* And return it */
735 return res;
738 struct symbol_list * sparse(char *filename)
740 struct symbol_list *res = __sparse(filename);
742 /* Evaluate the complete symbol list */
743 evaluate_symbol_list(res);
745 return res;