[PATCH] More address space checking
[smatch.git] / lib.c
blob808c7133e5c2a17eaa2a947a1ae126bdb48b3bf0
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;
33 #ifndef __GNUC__
34 # define __GNUC__ 2
35 # define __GNUC_MINOR__ 95
36 #endif
38 int gcc_major = __GNUC__;
39 int gcc_minor = __GNUC_MINOR__;
41 struct token *skip_to(struct token *token, int op)
43 while (!match_op(token, op) && !eof_token(token))
44 token = token->next;
45 return token;
48 struct token *expect(struct token *token, int op, const char *where)
50 if (!match_op(token, op)) {
51 static struct token bad_token;
52 if (token != &bad_token) {
53 bad_token.next = token;
54 warning(token->pos, "Expected %s %s", show_special(op), where);
55 warning(token->pos, "got %s", show_token(token));
57 if (op == ';')
58 return skip_to(token, op);
59 return &bad_token;
61 return token->next;
64 unsigned int hexval(unsigned int c)
66 int retval = 256;
67 switch (c) {
68 case '0'...'9':
69 retval = c - '0';
70 break;
71 case 'a'...'f':
72 retval = c - 'a' + 10;
73 break;
74 case 'A'...'F':
75 retval = c - 'A' + 10;
76 break;
78 return retval;
81 static void do_warn(const char *type, struct position pos, const char * fmt, va_list args)
83 static char buffer[512];
84 const char *name;
86 vsprintf(buffer, fmt, args);
87 name = stream_name(pos.stream);
89 fprintf(stderr, "%s:%d:%d: %s%s\n",
90 name, pos.line, pos.pos, type, buffer);
93 static int max_warnings = 100;
95 void info(struct position pos, const char * fmt, ...)
97 va_list args;
99 if (!max_warnings)
100 return;
101 va_start(args, fmt);
102 do_warn("", pos, fmt, args);
103 va_end(args);
106 void warning(struct position pos, const char * fmt, ...)
108 va_list args;
110 if (!max_warnings)
111 return;
113 if (!--max_warnings)
114 fmt = "too many warnings";
116 va_start(args, fmt);
117 do_warn("warning: ", pos, fmt, args);
118 va_end(args);
121 void error(struct position pos, const char * fmt, ...)
123 static int errors = 0;
124 va_list args;
126 /* Shut up warnings after an error */
127 max_warnings = 0;
128 if (errors > 100) {
129 static int once = 0;
130 if (once)
131 return;
132 fmt = "too many errors";
133 once = 1;
136 va_start(args, fmt);
137 do_warn("error: ", pos, fmt, args);
138 va_end(args);
139 errors++;
142 void error_die(struct position pos, const char * fmt, ...)
144 va_list args;
145 va_start(args, fmt);
146 do_warn("error: ", pos, fmt, args);
147 va_end(args);
148 exit(1);
151 void die(const char *fmt, ...)
153 va_list args;
154 static char buffer[512];
156 va_start(args, fmt);
157 vsnprintf(buffer, sizeof(buffer), fmt, args);
158 va_end(args);
160 fprintf(stderr, "%s\n", buffer);
161 exit(1);
164 static unsigned int pre_buffer_size;
165 static char pre_buffer[8192];
167 int Wdefault_bitfield_sign = 0;
168 int Wbitwise = 0;
169 int Wtypesign = 0;
170 int Wcontext = 0;
171 int Wundefined_preprocessor = 0;
172 int Wptr_subtraction_blows = 0;
173 int Wcast_to_address_space = 0;
174 int Wtransparent_union = 1;
175 int preprocess_only;
176 char *include;
177 int include_fd = -1;
179 void add_pre_buffer(const char *fmt, ...)
181 va_list args;
182 unsigned int size;
184 va_start(args, fmt);
185 size = pre_buffer_size;
186 size += vsnprintf(pre_buffer + size,
187 sizeof(pre_buffer) - size,
188 fmt, args);
189 pre_buffer_size = size;
190 va_end(args);
193 static char **handle_switch_D(char *arg, char **next)
195 const char *name = arg + 1;
196 const char *value = "1";
197 for (;;) {
198 char c;
199 c = *++arg;
200 if (!c)
201 break;
202 if (isspace((unsigned char)c) || c == '=') {
203 *arg = '\0';
204 value = arg + 1;
205 break;
208 add_pre_buffer("#define %s %s\n", name, value);
209 return next;
212 static char **handle_switch_E(char *arg, char **next)
214 preprocess_only = 1;
215 return next;
218 static char **handle_switch_v(char *arg, char **next)
220 do {
221 verbose++;
222 } while (*++arg == 'v');
223 return next;
226 static char **handle_switch_I(char *arg, char **next)
228 char *path = arg+1;
230 switch (arg[1]) {
231 case '-':
232 add_pre_buffer("#split_include\n");
233 break;
235 case '\0': /* Plain "-I" */
236 path = *++next;
237 if (!path)
238 die("missing argument for -I option");
239 /* Fallthrough */
240 default:
241 add_pre_buffer("#add_include \"%s/\"\n", path);
243 return next;
246 static char **handle_switch_i(char *arg, char **next)
248 if (*next && !strcmp(arg, "include")) {
249 char *name = *++next;
250 int fd = open(name, O_RDONLY);
252 include_fd = fd;
253 include = name;
254 if (fd < 0)
255 perror(name);
257 else if (*next && !strcmp(arg, "isystem")) {
258 char *path = *++next;
259 if (!path)
260 die("missing argument for -isystem option");
261 add_pre_buffer("#add_isystem \"%s/\"\n", path);
263 return next;
266 static char **handle_switch_M(char *arg, char **next)
268 if (!strcmp(arg, "MF") || !strcmp(arg,"MQ") || !strcmp(arg,"MT")) {
269 if (!*next)
270 die("missing argument for -%s option", arg);
271 return next + 1;
273 return next;
276 static char **handle_switch_m(char *arg, char **next)
278 if (!strcmp(arg, "m64")) {
279 bits_in_long = 64;
280 max_int_alignment = 8;
281 bits_in_pointer = 64;
282 pointer_alignment = 8;
284 return next;
287 static char **handle_switch_o(char *arg, char **next)
289 if (!strcmp (arg, "o") && *next)
290 return next + 1; // "-o foo"
291 else
292 return next; // "-ofoo" or (bogus) terminal "-o"
295 static const struct warning {
296 const char *name;
297 int *flag;
298 } warnings[] = {
299 { "cast-to-as", &Wcast_to_address_space },
300 { "ptr-subtraction-blows", &Wptr_subtraction_blows },
301 { "default-bitfield-sign", &Wdefault_bitfield_sign },
302 { "undef", &Wundefined_preprocessor },
303 { "bitwise", &Wbitwise },
304 { "typesign", &Wtypesign },
305 { "context", &Wcontext },
306 { "transparent-union", &Wtransparent_union },
310 static char **handle_switch_W(char *arg, char **next)
312 int no = 0;
313 char *p = arg + 1;
314 unsigned i;
316 // Prefixes "no" and "no-" mean to turn warning off.
317 if (p[0] == 'n' && p[1] == 'o') {
318 p += 2;
319 if (p[0] == '-')
320 p++;
321 no = 1;
324 for (i = 0; i < sizeof(warnings) / sizeof(warnings[0]); i++) {
325 if (!strcmp(p,warnings[i].name)) {
326 *warnings[i].flag = !no;
327 return next;
331 // Unknown.
332 return next;
335 static char **handle_switch_U(char *arg, char **next)
337 const char *name = arg + 1;
338 add_pre_buffer ("#undef %s\n", name);
339 return next;
342 static char **handle_switch_O(char *arg, char **next)
344 int level = 1;
345 if (arg[1] >= '0' && arg[1] <= '9')
346 level = arg[1] - '0';
347 optimize = level;
348 optimize_size = arg[1] == 's';
349 return next;
352 static char **handle_switch_f(char *arg, char **next)
354 int flag = 1;
356 arg++;
357 if (!strncmp(arg, "no-", 3)) {
358 flag = 0;
359 arg += 3;
361 /* handle switch here.. */
362 return next;
365 static char **handle_nostdinc(char *arg, char **next)
367 add_pre_buffer("#nostdinc\n");
368 return next;
371 static char **handle_dirafter(char *arg, char **next)
373 char *path = *++next;
374 if (!path)
375 die("missing argument for -dirafter option");
376 add_pre_buffer("#add_dirafter \"%s/\"\n", path);
377 return next;
380 struct switches {
381 const char *name;
382 char **(*fn)(char *, char**);
385 char **handle_switch(char *arg, char **next)
387 static struct switches cmd[] = {
388 { "nostdinc", handle_nostdinc },
389 { "dirafter", handle_dirafter },
390 { NULL, NULL }
392 struct switches *s;
394 switch (*arg) {
395 case 'D': return handle_switch_D(arg, next);
396 case 'E': return handle_switch_E(arg, next);
397 case 'I': return handle_switch_I(arg, next);
398 case 'i': return handle_switch_i(arg, next);
399 case 'M': return handle_switch_M(arg, next);
400 case 'm': return handle_switch_m(arg, next);
401 case 'o': return handle_switch_o(arg, next);
402 case 'U': return handle_switch_U(arg, next);
403 case 'v': return handle_switch_v(arg, next);
404 case 'W': return handle_switch_W(arg, next);
405 case 'O': return handle_switch_O(arg, next);
406 case 'f': return handle_switch_f(arg, next);
407 default:
408 break;
411 s = cmd;
412 while (s->name) {
413 if (!strcmp(s->name, arg))
414 return s->fn(arg, next);
415 s++;
419 * Ignore unknown command line options:
420 * they're probably gcc switches
422 return next;
425 void declare_builtin_functions(void)
427 add_pre_buffer("extern void *__builtin_memcpy(void *, const void *, __SIZE_TYPE__);\n");
428 add_pre_buffer("extern void *__builtin_return_address(unsigned int);\n");
429 add_pre_buffer("extern void *__builtin_frame_address(unsigned int);\n");
430 add_pre_buffer("extern void *__builtin_memset(void *, int, __SIZE_TYPE__);\n");
431 add_pre_buffer("extern void __builtin_trap(void);\n");
432 add_pre_buffer("extern int __builtin_ffs(int);\n");
433 add_pre_buffer("extern void *__builtin_alloca(__SIZE_TYPE__);\n");
436 void create_builtin_stream(void)
438 add_pre_buffer("#define __GNUC__ %d\n", gcc_major);
439 add_pre_buffer("#define __GNUC_MINOR__ %d\n", gcc_minor);
440 add_pre_buffer("#define __extension__\n");
441 add_pre_buffer("#define __pragma__\n");
443 // gcc defines __SIZE_TYPE__ to be size_t. For linux/i86 and
444 // solaris/sparc that is really "unsigned int" and for linux/x86_64
445 // it is "long unsigned int". In either case we can probably
446 // get away with this. We need the #ifndef as cgcc will define
447 // the right __SIZE_TYPE__.
448 add_pre_buffer("#weak_define __SIZE_TYPE__ long unsigned int\n");
449 add_pre_buffer("#weak_define __STDC__ 1\n");
451 add_pre_buffer("#define __builtin_stdarg_start(a,b) ((a) = (__builtin_va_list)(&(b)))\n");
452 add_pre_buffer("#define __builtin_va_start(a,b) ((a) = (__builtin_va_list)(&(b)))\n");
453 add_pre_buffer("#define __builtin_va_arg(arg,type) ({ type __va_arg_ret = *(type *)(arg); arg += sizeof(type); __va_arg_ret; })\n");
454 add_pre_buffer("#define __builtin_va_alist (*(void *)0)\n");
455 add_pre_buffer("#define __builtin_va_arg_incr(x) ((x) + 1)\n");
456 add_pre_buffer("#define __builtin_va_end(arg)\n");
457 add_pre_buffer("#define __builtin_offsetof(type, name) ((__SIZE_TYPE__)&((type *)(0ul))->name)\n");
459 /* FIXME! We need to do these as special magic macros at expansion time! */
460 add_pre_buffer("#define __BASE_FILE__ \"base_file.c\"\n");
461 add_pre_buffer("#define __DATE__ \"??? ?? ????\"\n");
462 add_pre_buffer("#define __TIME__ \"??:??:??\"\n");
464 if (optimize)
465 add_pre_buffer("#define __OPTIMIZE__\n");
466 if (optimize_size)
467 add_pre_buffer("#define __OPTIMIZE_SIZE__\n");
470 static struct symbol_list *sparse_tokenstream(struct token *token)
472 // Pre-process the stream
473 token = preprocess(token);
475 if (preprocess_only) {
476 while (!eof_token(token)) {
477 int prec = 1;
478 struct token *next = token->next;
479 const char *separator = "";
480 if (next->pos.whitespace)
481 separator = " ";
482 if (next->pos.newline) {
483 separator = "\n\t\t\t\t\t";
484 prec = next->pos.pos;
485 if (prec > 4)
486 prec = 4;
488 printf("%s%.*s", show_token(token), prec, separator);
489 token = next;
491 putchar('\n');
493 return NULL;
496 // Parse the resulting C code
497 while (!eof_token(token))
498 token = external_declaration(token, &translation_unit_used_list);
499 return translation_unit_used_list;
502 static struct symbol_list *sparse_file(const char *filename)
504 int fd;
505 struct token *token;
507 if (strcmp (filename, "-") == 0) {
508 fd = 0;
509 } else {
510 fd = open(filename, O_RDONLY);
511 if (fd < 0)
512 die("No such file: %s", filename);
515 // Tokenize the input stream
516 token = tokenize(filename, fd, NULL, includepath);
517 close(fd);
519 return sparse_tokenstream(token);
523 * This handles the "-include" directive etc: we're in global
524 * scope, and all types/macros etc will affect all the following
525 * files.
527 * NOTE NOTE NOTE! "#undef" of anything in this stage will
528 * affect all subsequent files too, ie we can have non-local
529 * behaviour between files!
531 static void sparse_initial(void)
533 struct token *token;
535 // Prepend any "include" file to the stream.
536 // We're in global scope, it will affect all files!
537 token = NULL;
538 if (include_fd >= 0)
539 token = tokenize(include, include_fd, NULL, includepath);
541 // Prepend the initial built-in stream
542 token = tokenize_buffer(pre_buffer, pre_buffer_size, token);
543 sparse_tokenstream(token);
546 int sparse_initialize(int argc, char **argv)
548 char **args;
549 int files = 0;
551 // Initialize symbol stream first, so that we can add defines etc
552 init_symbols();
554 args = argv;
555 for (;;) {
556 char *arg = *++args;
557 if (!arg)
558 break;
560 if (arg[0] == '-' && arg[1]) {
561 args = handle_switch(arg+1, args);
562 continue;
566 * Hacky hacky hacky: we re-use the argument space
567 * to save the filenames.
569 argv[files++] = arg;
572 argv[files] = NULL;
573 if (files) {
574 // Initialize type system
575 init_ctype();
577 create_builtin_stream();
578 add_pre_buffer("#define __CHECKER__ 1\n");
579 if (!preprocess_only)
580 declare_builtin_functions();
582 sparse_initial();
585 * Protect the initial token allocations, since
586 * they need to survive all the others
588 protect_token_alloc();
590 return files;
593 struct symbol_list * sparse(char **argv)
595 struct symbol_list *res;
596 char *filename, *next;
598 /* Clear previous symbol list */
599 translation_unit_used_list = NULL;
601 filename = *argv;
602 if (!filename)
603 return NULL;
604 do {
605 next = argv[1];
606 *argv++ = next;
607 } while (next);
609 start_file_scope();
610 res = sparse_file(filename);
611 end_file_scope();
613 /* Drop the tokens for this file after parsing */
614 clear_token_alloc();
616 /* Evaluate the complete symbol list */
617 evaluate_symbol_list(res);
619 /* And return it */
620 return res;