Add -Wno-non-pointer-null to turn off warning about using a plain integer as a NULL...
[smatch.git] / lib.c
blob1cd1d2390ceb58921b288f767532567796f29b05
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 static void do_error(struct position pos, const char * fmt, va_list args)
131 static int errors = 0;
132 die_if_error = 1;
133 show_info = 1;
134 /* Shut up warnings after an error */
135 max_warnings = 0;
136 if (errors > 100) {
137 static int once = 0;
138 show_info = 0;
139 if (once)
140 return;
141 fmt = "too many errors";
142 once = 1;
145 do_warn("error: ", pos, fmt, args);
146 errors++;
149 void sparse_error(struct position pos, const char * fmt, ...)
151 va_list args;
152 va_start(args, fmt);
153 do_error(pos, fmt, args);
154 va_end(args);
157 void expression_error(struct expression *expr, const char *fmt, ...)
159 va_list args;
160 va_start(args, fmt);
161 do_error(expr->pos, fmt, args);
162 va_end(args);
163 expr->ctype = &bad_ctype;
166 void error_die(struct position pos, const char * fmt, ...)
168 va_list args;
169 va_start(args, fmt);
170 do_warn("error: ", pos, fmt, args);
171 va_end(args);
172 exit(1);
175 void die(const char *fmt, ...)
177 va_list args;
178 static char buffer[512];
180 va_start(args, fmt);
181 vsnprintf(buffer, sizeof(buffer), fmt, args);
182 va_end(args);
184 fprintf(stderr, "%s\n", buffer);
185 exit(1);
188 static unsigned int pre_buffer_size;
189 static char pre_buffer[8192];
191 int Wdefault_bitfield_sign = 0;
192 int Wone_bit_signed_bitfield = 1;
193 int Wcast_truncate = 1;
194 int Wbitwise = 0;
195 int Wtypesign = 0;
196 int Wcontext = 1;
197 int Wundefined_preprocessor = 0;
198 int Wptr_subtraction_blows = 0;
199 int Wcast_to_address_space = 0;
200 int Wdecl = 0;
201 int Wtransparent_union = 1;
202 int Wshadow = 0;
203 int Waddress_space = 1;
204 int Wenum_mismatch = 1;
205 int Wdo_while = 1;
206 int Wuninitialized = 1;
207 int Wold_initializer = 1;
208 int Wnon_pointer_null = 1;
210 int dbg_entry = 0;
211 int dbg_dead = 0;
213 int preprocess_only;
215 #define CMDLINE_INCLUDE 20
216 int cmdline_include_nr = 0;
217 struct cmdline_include cmdline_include[CMDLINE_INCLUDE];
220 void add_pre_buffer(const char *fmt, ...)
222 va_list args;
223 unsigned int size;
225 va_start(args, fmt);
226 size = pre_buffer_size;
227 size += vsnprintf(pre_buffer + size,
228 sizeof(pre_buffer) - size,
229 fmt, args);
230 pre_buffer_size = size;
231 va_end(args);
234 static char **handle_switch_D(char *arg, char **next)
236 const char *name = arg + 1;
237 const char *value = "1";
238 for (;;) {
239 char c;
240 c = *++arg;
241 if (!c)
242 break;
243 if (isspace((unsigned char)c) || c == '=') {
244 *arg = '\0';
245 value = arg + 1;
246 break;
249 add_pre_buffer("#define %s %s\n", name, value);
250 return next;
253 static char **handle_switch_E(char *arg, char **next)
255 preprocess_only = 1;
256 return next;
259 static char **handle_switch_I(char *arg, char **next)
261 char *path = arg+1;
263 switch (arg[1]) {
264 case '-':
265 add_pre_buffer("#split_include\n");
266 break;
268 case '\0': /* Plain "-I" */
269 path = *++next;
270 if (!path)
271 die("missing argument for -I option");
272 /* Fall through */
273 default:
274 add_pre_buffer("#add_include \"%s/\"\n", path);
276 return next;
279 static void add_cmdline_include(char *filename)
281 int fd = open(filename, O_RDONLY);
282 if (fd < 0) {
283 perror(filename);
284 return;
286 if (cmdline_include_nr >= CMDLINE_INCLUDE)
287 die("too many include files for %s\n", filename);
288 cmdline_include[cmdline_include_nr].filename = filename;
289 cmdline_include[cmdline_include_nr].fd = fd;
290 cmdline_include_nr++;
293 static char **handle_switch_i(char *arg, char **next)
295 if (*next && !strcmp(arg, "include"))
296 add_cmdline_include(*++next);
297 else if (*next && !strcmp(arg, "imacros"))
298 add_cmdline_include(*++next);
299 else if (*next && !strcmp(arg, "isystem")) {
300 char *path = *++next;
301 if (!path)
302 die("missing argument for -isystem option");
303 add_pre_buffer("#add_isystem \"%s/\"\n", path);
305 return next;
308 static char **handle_switch_M(char *arg, char **next)
310 if (!strcmp(arg, "MF") || !strcmp(arg,"MQ") || !strcmp(arg,"MT")) {
311 if (!*next)
312 die("missing argument for -%s option", arg);
313 return next + 1;
315 return next;
318 static char **handle_switch_m(char *arg, char **next)
320 if (!strcmp(arg, "m64")) {
321 bits_in_long = 64;
322 max_int_alignment = 8;
323 bits_in_pointer = 64;
324 pointer_alignment = 8;
326 return next;
329 static char **handle_switch_o(char *arg, char **next)
331 if (!strcmp (arg, "o") && *next)
332 return next + 1; // "-o foo"
333 else
334 return next; // "-ofoo" or (bogus) terminal "-o"
337 static const struct warning {
338 const char *name;
339 int *flag;
340 } warnings[] = {
341 { "cast-to-as", &Wcast_to_address_space },
342 { "decl", &Wdecl },
343 { "one-bit-signed-bitfield", &Wone_bit_signed_bitfield },
344 { "cast-truncate", &Wcast_truncate },
345 { "ptr-subtraction-blows", &Wptr_subtraction_blows },
346 { "default-bitfield-sign", &Wdefault_bitfield_sign },
347 { "undef", &Wundefined_preprocessor },
348 { "bitwise", &Wbitwise },
349 { "typesign", &Wtypesign },
350 { "context", &Wcontext },
351 { "transparent-union", &Wtransparent_union },
352 { "shadow", &Wshadow },
353 { "address-space", &Waddress_space },
354 { "enum-mismatch", &Wenum_mismatch },
355 { "do-while", &Wdo_while },
356 { "uninitialized", &Wuninitialized },
357 { "old-initializer", &Wold_initializer },
358 { "non-pointer-null", &Wnon_pointer_null },
361 enum {
362 WARNING_OFF,
363 WARNING_ON,
364 WARNING_FORCE_OFF
368 static char **handle_onoff_switch(char *arg, char **next, const struct warning warnings[], int n)
370 int flag = WARNING_ON;
371 char *p = arg + 1;
372 unsigned i;
374 if (!strcmp(p, "all")) {
375 for (i = 0; i < n; i++) {
376 if (*warnings[i].flag != WARNING_FORCE_OFF)
377 *warnings[i].flag = WARNING_ON;
381 // Prefixes "no" and "no-" mean to turn warning off.
382 if (p[0] == 'n' && p[1] == 'o') {
383 p += 2;
384 if (p[0] == '-')
385 p++;
386 flag = WARNING_FORCE_OFF;
389 for (i = 0; i < n; i++) {
390 if (!strcmp(p,warnings[i].name)) {
391 *warnings[i].flag = flag;
392 return next;
396 // Unknown.
397 return NULL;
400 static char **handle_switch_W(char *arg, char **next)
402 char ** ret = handle_onoff_switch(arg, next, warnings, sizeof warnings/sizeof warnings[0]);
403 if (ret)
404 return ret;
406 // Unknown.
407 return next;
410 static struct warning debugs[] = {
411 { "entry", &dbg_entry},
412 { "dead", &dbg_dead},
416 static char **handle_switch_v(char *arg, char **next)
418 char ** ret = handle_onoff_switch(arg, next, debugs, sizeof debugs/sizeof debugs[0]);
419 if (ret)
420 return ret;
422 // Unknown.
423 do {
424 verbose++;
425 } while (*++arg == 'v');
426 return next;
430 static void handle_onoff_switch_finalize(const struct warning warnings[], int n)
432 unsigned i;
434 for (i = 0; i < n; i++) {
435 if (*warnings[i].flag == WARNING_FORCE_OFF)
436 *warnings[i].flag = WARNING_OFF;
440 static void handle_switch_W_finalize(void)
442 handle_onoff_switch_finalize(warnings, sizeof(warnings) / sizeof(warnings[0]));
445 static void handle_switch_v_finalize(void)
447 handle_onoff_switch_finalize(debugs, sizeof(debugs) / sizeof(debugs[0]));
450 static char **handle_switch_U(char *arg, char **next)
452 const char *name = arg + 1;
453 add_pre_buffer ("#undef %s\n", name);
454 return next;
457 static char **handle_switch_O(char *arg, char **next)
459 int level = 1;
460 if (arg[1] >= '0' && arg[1] <= '9')
461 level = arg[1] - '0';
462 optimize = level;
463 optimize_size = arg[1] == 's';
464 return next;
467 static char **handle_switch_f(char *arg, char **next)
469 int flag = 1;
471 arg++;
472 if (!strncmp(arg, "no-", 3)) {
473 flag = 0;
474 arg += 3;
476 /* handle switch here.. */
477 return next;
480 static char **handle_switch_G(char *arg, char **next)
482 if (!strcmp (arg, "G") && *next)
483 return next + 1; // "-G 0"
484 else
485 return next; // "-G0" or (bogus) terminal "-G"
488 static char **handle_nostdinc(char *arg, char **next)
490 add_pre_buffer("#nostdinc\n");
491 return next;
494 static char **handle_dirafter(char *arg, char **next)
496 char *path = *++next;
497 if (!path)
498 die("missing argument for -dirafter option");
499 add_pre_buffer("#add_dirafter \"%s/\"\n", path);
500 return next;
503 struct switches {
504 const char *name;
505 char **(*fn)(char *, char **);
508 char **handle_switch(char *arg, char **next)
510 static struct switches cmd[] = {
511 { "nostdinc", handle_nostdinc },
512 { "dirafter", handle_dirafter },
513 { NULL, NULL }
515 struct switches *s;
517 switch (*arg) {
518 case 'D': return handle_switch_D(arg, next);
519 case 'E': return handle_switch_E(arg, next);
520 case 'I': return handle_switch_I(arg, next);
521 case 'i': return handle_switch_i(arg, next);
522 case 'M': return handle_switch_M(arg, next);
523 case 'm': return handle_switch_m(arg, next);
524 case 'o': return handle_switch_o(arg, next);
525 case 'U': return handle_switch_U(arg, next);
526 case 'v': return handle_switch_v(arg, next);
527 case 'W': return handle_switch_W(arg, next);
528 case 'O': return handle_switch_O(arg, next);
529 case 'f': return handle_switch_f(arg, next);
530 case 'G': return handle_switch_G(arg, next);
531 default:
532 break;
535 s = cmd;
536 while (s->name) {
537 if (!strcmp(s->name, arg))
538 return s->fn(arg, next);
539 s++;
543 * Ignore unknown command line options:
544 * they're probably gcc switches
546 return next;
549 void declare_builtin_functions(void)
551 /* Gaah. gcc knows tons of builtin <string.h> functions */
552 add_pre_buffer("extern void *__builtin_memcpy(void *, const void *, __SIZE_TYPE__);\n");
553 add_pre_buffer("extern void *__builtin_memset(void *, int, __SIZE_TYPE__);\n");
554 add_pre_buffer("extern int __builtin_memcmp(const void *, const void *, __SIZE_TYPE__);\n");
555 add_pre_buffer("extern int __builtin_strcmp(const char *, const char *);\n");
556 add_pre_buffer("extern char *__builtin_strchr(const char *, int);\n");
557 add_pre_buffer("extern char *__builtin_strcpy(char *, const char *);\n");
558 add_pre_buffer("extern char *__builtin_strncpy(char *, const char *, __SIZE_TYPE__);\n");
559 add_pre_buffer("extern __SIZE_TYPE__ __builtin_strspn(const char *, const char *);\n");
560 add_pre_buffer("extern __SIZE_TYPE__ __builtin_strcspn(const char *, const char *);\n");
561 add_pre_buffer("extern char * __builtin_strpbrk(const char *, const char *);\n");
563 /* And some random ones.. */
564 add_pre_buffer("extern void *__builtin_return_address(unsigned int);\n");
565 add_pre_buffer("extern void *__builtin_extract_return_addr(void *);\n");
566 add_pre_buffer("extern void *__builtin_frame_address(unsigned int);\n");
567 add_pre_buffer("extern void __builtin_trap(void);\n");
568 add_pre_buffer("extern int __builtin_ffs(int);\n");
569 add_pre_buffer("extern void *__builtin_alloca(__SIZE_TYPE__);\n");
570 add_pre_buffer("extern int __builtin_popcount(unsigned int);\n");
571 add_pre_buffer("extern int __builtin_popcountl(unsigned long);\n");
572 add_pre_buffer("extern void __builtin_prefetch (const void *, ...);\n");
573 add_pre_buffer("extern long __builtin_alpha_extbl(long, long);\n");
574 add_pre_buffer("extern long __builtin_alpha_extwl(long, long);\n");
575 add_pre_buffer("extern long __builtin_alpha_insbl(long, long);\n");
576 add_pre_buffer("extern long __builtin_alpha_inswl(long, long);\n");
577 add_pre_buffer("extern long __builtin_alpha_insql(long, long);\n");
578 add_pre_buffer("extern long __builtin_alpha_inslh(long, long);\n");
579 add_pre_buffer("extern long __builtin_alpha_cmpbge(long, long);\n");
582 void create_builtin_stream(void)
584 add_pre_buffer("#weak_define __GNUC__ %d\n", gcc_major);
585 add_pre_buffer("#weak_define __GNUC_MINOR__ %d\n", gcc_minor);
586 add_pre_buffer("#weak_define __GNUC_PATCHLEVEL__ %d\n", gcc_patchlevel);
587 add_pre_buffer("#define __extension__\n");
588 add_pre_buffer("#define __pragma__\n");
590 // gcc defines __SIZE_TYPE__ to be size_t. For linux/i86 and
591 // solaris/sparc that is really "unsigned int" and for linux/x86_64
592 // it is "long unsigned int". In either case we can probably
593 // get away with this. We need the #weak_define as cgcc will define
594 // the right __SIZE_TYPE__.
595 add_pre_buffer("#weak_define __SIZE_TYPE__ long unsigned int\n");
596 add_pre_buffer("#weak_define __STDC__ 1\n");
598 add_pre_buffer("#define __builtin_stdarg_start(a,b) ((a) = (__builtin_va_list)(&(b)))\n");
599 add_pre_buffer("#define __builtin_va_start(a,b) ((a) = (__builtin_va_list)(&(b)))\n");
600 add_pre_buffer("#define __builtin_va_arg(arg,type) ({ type __va_arg_ret = *(type *)(arg); arg += sizeof(type); __va_arg_ret; })\n");
601 add_pre_buffer("#define __builtin_va_alist (*(void *)0)\n");
602 add_pre_buffer("#define __builtin_va_arg_incr(x) ((x) + 1)\n");
603 add_pre_buffer("#define __builtin_va_copy(dest, src) ({ dest = src; (void)0; })\n");
604 add_pre_buffer("#define __builtin_va_end(arg)\n");
605 add_pre_buffer("#define __builtin_offsetof(type, name) ((__SIZE_TYPE__)&((type *)(0ul))->name)\n");
607 /* FIXME! We need to do these as special magic macros at expansion time! */
608 add_pre_buffer("#define __BASE_FILE__ \"base_file.c\"\n");
610 if (optimize)
611 add_pre_buffer("#define __OPTIMIZE__ 1\n");
612 if (optimize_size)
613 add_pre_buffer("#define __OPTIMIZE_SIZE__ 1\n");
616 static struct symbol_list *sparse_tokenstream(struct token *token)
618 // Preprocess the stream
619 token = preprocess(token);
621 if (preprocess_only) {
622 while (!eof_token(token)) {
623 int prec = 1;
624 struct token *next = token->next;
625 const char *separator = "";
626 if (next->pos.whitespace)
627 separator = " ";
628 if (next->pos.newline) {
629 separator = "\n\t\t\t\t\t";
630 prec = next->pos.pos;
631 if (prec > 4)
632 prec = 4;
634 printf("%s%.*s", show_token(token), prec, separator);
635 token = next;
637 putchar('\n');
639 return NULL;
642 // Parse the resulting C code
643 while (!eof_token(token))
644 token = external_declaration(token, &translation_unit_used_list);
645 return translation_unit_used_list;
648 static struct symbol_list *sparse_file(const char *filename)
650 int fd;
651 struct token *token;
653 if (strcmp (filename, "-") == 0) {
654 fd = 0;
655 } else {
656 fd = open(filename, O_RDONLY);
657 if (fd < 0)
658 die("No such file: %s", filename);
661 // Tokenize the input stream
662 token = tokenize(filename, fd, NULL, includepath);
663 close(fd);
665 return sparse_tokenstream(token);
669 * This handles the "-include" directive etc: we're in global
670 * scope, and all types/macros etc will affect all the following
671 * files.
673 * NOTE NOTE NOTE! "#undef" of anything in this stage will
674 * affect all subsequent files too, i.e. we can have non-local
675 * behaviour between files!
677 static struct symbol_list *sparse_initial(void)
679 struct token *token;
680 int i;
682 // Prepend any "include" file to the stream.
683 // We're in global scope, it will affect all files!
684 token = NULL;
685 for (i = cmdline_include_nr - 1; i >= 0; i--)
686 token = tokenize(cmdline_include[i].filename, cmdline_include[i].fd,
687 token, includepath);
689 // Prepend the initial built-in stream
690 token = tokenize_buffer(pre_buffer, pre_buffer_size, token);
691 return sparse_tokenstream(token);
694 struct symbol_list *sparse_initialize(int argc, char **argv, struct string_list **filelist)
696 char **args;
697 struct symbol_list *list;
699 // Initialize symbol stream first, so that we can add defines etc
700 init_symbols();
702 args = argv;
703 for (;;) {
704 char *arg = *++args;
705 if (!arg)
706 break;
708 if (arg[0] == '-' && arg[1]) {
709 args = handle_switch(arg+1, args);
710 continue;
712 add_ptr_list_notag(filelist, arg);
714 handle_switch_W_finalize();
715 handle_switch_v_finalize();
717 list = NULL;
718 if (!ptr_list_empty(filelist)) {
719 // Initialize type system
720 init_ctype();
722 create_builtin_stream();
723 add_pre_buffer("#define __CHECKER__ 1\n");
724 if (!preprocess_only)
725 declare_builtin_functions();
727 list = sparse_initial();
730 * Protect the initial token allocations, since
731 * they need to survive all the others
733 protect_token_alloc();
735 return list;
738 struct symbol_list * __sparse(char *filename)
740 struct symbol_list *res;
742 /* Clear previous symbol list */
743 translation_unit_used_list = NULL;
745 new_file_scope();
746 res = sparse_file(filename);
748 /* Drop the tokens for this file after parsing */
749 clear_token_alloc();
751 /* And return it */
752 return res;
755 struct symbol_list * sparse(char *filename)
757 struct symbol_list *res = __sparse(filename);
759 /* Evaluate the complete symbol list */
760 evaluate_symbol_list(res);
762 return res;