[PATCH] sparse_error() should not silence info() after sparse_error()s
[smatch.git] / lib.c
blobcd79453c0a05b192011f0b02fadb83b5ba0ee081
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 Wbitwise = 0;
180 int Wtypesign = 0;
181 int Wcontext = 0;
182 int Wundefined_preprocessor = 0;
183 int Wptr_subtraction_blows = 0;
184 int Wcast_to_address_space = 0;
185 int Wdecl = 0;
186 int Wtransparent_union = 1;
187 int Wshadow = 0;
188 int preprocess_only;
189 char *include;
190 int include_fd = -1;
193 void add_pre_buffer(const char *fmt, ...)
195 va_list args;
196 unsigned int size;
198 va_start(args, fmt);
199 size = pre_buffer_size;
200 size += vsnprintf(pre_buffer + size,
201 sizeof(pre_buffer) - size,
202 fmt, args);
203 pre_buffer_size = size;
204 va_end(args);
207 static char **handle_switch_D(char *arg, char **next)
209 const char *name = arg + 1;
210 const char *value = "1";
211 for (;;) {
212 char c;
213 c = *++arg;
214 if (!c)
215 break;
216 if (isspace((unsigned char)c) || c == '=') {
217 *arg = '\0';
218 value = arg + 1;
219 break;
222 add_pre_buffer("#define %s %s\n", name, value);
223 return next;
226 static char **handle_switch_E(char *arg, char **next)
228 preprocess_only = 1;
229 return next;
232 static char **handle_switch_v(char *arg, char **next)
234 do {
235 verbose++;
236 } while (*++arg == 'v');
237 return next;
240 static char **handle_switch_I(char *arg, char **next)
242 char *path = arg+1;
244 switch (arg[1]) {
245 case '-':
246 add_pre_buffer("#split_include\n");
247 break;
249 case '\0': /* Plain "-I" */
250 path = *++next;
251 if (!path)
252 die("missing argument for -I option");
253 /* Fallthrough */
254 default:
255 add_pre_buffer("#add_include \"%s/\"\n", path);
257 return next;
260 static char **handle_switch_i(char *arg, char **next)
262 if (*next && !strcmp(arg, "include")) {
263 char *name = *++next;
264 int fd = open(name, O_RDONLY);
266 include_fd = fd;
267 include = name;
268 if (fd < 0)
269 perror(name);
271 if (*next && !strcmp(arg, "imacros")) {
272 char *name = *++next;
273 int fd = open(name, O_RDONLY);
275 include_fd = fd;
276 include = name;
277 if (fd < 0)
278 perror(name);
280 else if (*next && !strcmp(arg, "isystem")) {
281 char *path = *++next;
282 if (!path)
283 die("missing argument for -isystem option");
284 add_pre_buffer("#add_isystem \"%s/\"\n", path);
286 return next;
289 static char **handle_switch_M(char *arg, char **next)
291 if (!strcmp(arg, "MF") || !strcmp(arg,"MQ") || !strcmp(arg,"MT")) {
292 if (!*next)
293 die("missing argument for -%s option", arg);
294 return next + 1;
296 return next;
299 static char **handle_switch_m(char *arg, char **next)
301 if (!strcmp(arg, "m64")) {
302 bits_in_long = 64;
303 max_int_alignment = 8;
304 bits_in_pointer = 64;
305 pointer_alignment = 8;
307 return next;
310 static char **handle_switch_o(char *arg, char **next)
312 if (!strcmp (arg, "o") && *next)
313 return next + 1; // "-o foo"
314 else
315 return next; // "-ofoo" or (bogus) terminal "-o"
318 static const struct warning {
319 const char *name;
320 int *flag;
321 } warnings[] = {
322 { "cast-to-as", &Wcast_to_address_space },
323 { "decl", &Wdecl },
324 { "one-bit-signed-bitfield", &Wone_bit_signed_bitfield },
325 { "ptr-subtraction-blows", &Wptr_subtraction_blows },
326 { "default-bitfield-sign", &Wdefault_bitfield_sign },
327 { "undef", &Wundefined_preprocessor },
328 { "bitwise", &Wbitwise },
329 { "typesign", &Wtypesign },
330 { "context", &Wcontext },
331 { "transparent-union", &Wtransparent_union },
332 { "shadow", &Wshadow },
336 static char **handle_switch_W(char *arg, char **next)
338 int no = 0;
339 char *p = arg + 1;
340 unsigned i;
342 // Prefixes "no" and "no-" mean to turn warning off.
343 if (p[0] == 'n' && p[1] == 'o') {
344 p += 2;
345 if (p[0] == '-')
346 p++;
347 no = 1;
350 for (i = 0; i < sizeof(warnings) / sizeof(warnings[0]); i++) {
351 if (!strcmp(p,warnings[i].name)) {
352 *warnings[i].flag = !no;
353 return next;
357 // Unknown.
358 return next;
361 static char **handle_switch_U(char *arg, char **next)
363 const char *name = arg + 1;
364 add_pre_buffer ("#undef %s\n", name);
365 return next;
368 static char **handle_switch_O(char *arg, char **next)
370 int level = 1;
371 if (arg[1] >= '0' && arg[1] <= '9')
372 level = arg[1] - '0';
373 optimize = level;
374 optimize_size = arg[1] == 's';
375 return next;
378 static char **handle_switch_f(char *arg, char **next)
380 int flag = 1;
382 arg++;
383 if (!strncmp(arg, "no-", 3)) {
384 flag = 0;
385 arg += 3;
387 /* handle switch here.. */
388 return next;
391 static char **handle_switch_G(char *arg, char **next)
393 if (!strcmp (arg, "G") && *next)
394 return next + 1; // "-G 0"
395 else
396 return next; // "-G0" or (bogus) terminal "-G"
399 static char **handle_nostdinc(char *arg, char **next)
401 add_pre_buffer("#nostdinc\n");
402 return next;
405 static char **handle_dirafter(char *arg, char **next)
407 char *path = *++next;
408 if (!path)
409 die("missing argument for -dirafter option");
410 add_pre_buffer("#add_dirafter \"%s/\"\n", path);
411 return next;
414 struct switches {
415 const char *name;
416 char **(*fn)(char *, char**);
419 char **handle_switch(char *arg, char **next)
421 static struct switches cmd[] = {
422 { "nostdinc", handle_nostdinc },
423 { "dirafter", handle_dirafter },
424 { NULL, NULL }
426 struct switches *s;
428 switch (*arg) {
429 case 'D': return handle_switch_D(arg, next);
430 case 'E': return handle_switch_E(arg, next);
431 case 'I': return handle_switch_I(arg, next);
432 case 'i': return handle_switch_i(arg, next);
433 case 'M': return handle_switch_M(arg, next);
434 case 'm': return handle_switch_m(arg, next);
435 case 'o': return handle_switch_o(arg, next);
436 case 'U': return handle_switch_U(arg, next);
437 case 'v': return handle_switch_v(arg, next);
438 case 'W': return handle_switch_W(arg, next);
439 case 'O': return handle_switch_O(arg, next);
440 case 'f': return handle_switch_f(arg, next);
441 case 'G': return handle_switch_G(arg, next);
442 default:
443 break;
446 s = cmd;
447 while (s->name) {
448 if (!strcmp(s->name, arg))
449 return s->fn(arg, next);
450 s++;
454 * Ignore unknown command line options:
455 * they're probably gcc switches
457 return next;
460 void declare_builtin_functions(void)
462 /* Gaah. gcc knows tons of builtin <string.h> functions */
463 add_pre_buffer("extern void *__builtin_memcpy(void *, const void *, __SIZE_TYPE__);\n");
464 add_pre_buffer("extern void *__builtin_memset(void *, int, __SIZE_TYPE__);\n");
465 add_pre_buffer("extern int __builtin_memcmp(const void *, const void *, __SIZE_TYPE__);\n");
466 add_pre_buffer("extern int __builtin_strcmp(const char *, const char *);\n");
467 add_pre_buffer("extern char *__builtin_strchr(const char *, int);\n");
468 add_pre_buffer("extern char *__builtin_strcpy(char *, const char *);\n");
469 add_pre_buffer("extern char *__builtin_strncpy(char *, const char *, __SIZE_TYPE__);\n");
470 add_pre_buffer("extern __SIZE_TYPE__ __builtin_strspn(const char *, const char *);\n");
471 add_pre_buffer("extern __SIZE_TYPE__ __builtin_strcspn(const char *, const char *);\n");
473 /* And some random ones.. */
474 add_pre_buffer("extern void *__builtin_return_address(unsigned int);\n");
475 add_pre_buffer("extern void *__builtin_extract_return_addr(void *);\n");
476 add_pre_buffer("extern void *__builtin_frame_address(unsigned int);\n");
477 add_pre_buffer("extern void __builtin_trap(void);\n");
478 add_pre_buffer("extern int __builtin_ffs(int);\n");
479 add_pre_buffer("extern void *__builtin_alloca(__SIZE_TYPE__);\n");
482 void create_builtin_stream(void)
484 add_pre_buffer("#weak_define __GNUC__ %d\n", gcc_major);
485 add_pre_buffer("#weak_define __GNUC_MINOR__ %d\n", gcc_minor);
486 add_pre_buffer("#weak_define __GNUC_PATCHLEVEL__ %d\n", gcc_patchlevel);
487 add_pre_buffer("#define __extension__\n");
488 add_pre_buffer("#define __pragma__\n");
490 // gcc defines __SIZE_TYPE__ to be size_t. For linux/i86 and
491 // solaris/sparc that is really "unsigned int" and for linux/x86_64
492 // it is "long unsigned int". In either case we can probably
493 // get away with this. We need the #ifndef as cgcc will define
494 // the right __SIZE_TYPE__.
495 add_pre_buffer("#weak_define __SIZE_TYPE__ long unsigned int\n");
496 add_pre_buffer("#weak_define __STDC__ 1\n");
498 add_pre_buffer("#define __builtin_stdarg_start(a,b) ((a) = (__builtin_va_list)(&(b)))\n");
499 add_pre_buffer("#define __builtin_va_start(a,b) ((a) = (__builtin_va_list)(&(b)))\n");
500 add_pre_buffer("#define __builtin_va_arg(arg,type) ({ type __va_arg_ret = *(type *)(arg); arg += sizeof(type); __va_arg_ret; })\n");
501 add_pre_buffer("#define __builtin_va_alist (*(void *)0)\n");
502 add_pre_buffer("#define __builtin_va_arg_incr(x) ((x) + 1)\n");
503 add_pre_buffer("#define __builtin_va_copy(dest, src) ({ dest = src; (void)0; })\n");
504 add_pre_buffer("#define __builtin_va_end(arg)\n");
505 add_pre_buffer("#define __builtin_offsetof(type, name) ((__SIZE_TYPE__)&((type *)(0ul))->name)\n");
507 /* FIXME! We need to do these as special magic macros at expansion time! */
508 add_pre_buffer("#define __BASE_FILE__ \"base_file.c\"\n");
509 add_pre_buffer("#define __DATE__ \"??? ?? ????\"\n");
510 add_pre_buffer("#define __TIME__ \"??:??:??\"\n");
512 if (optimize)
513 add_pre_buffer("#define __OPTIMIZE__ 1\n");
514 if (optimize_size)
515 add_pre_buffer("#define __OPTIMIZE_SIZE__ 1\n");
518 static struct symbol_list *sparse_tokenstream(struct token *token)
520 // Pre-process the stream
521 token = preprocess(token);
523 if (preprocess_only) {
524 while (!eof_token(token)) {
525 int prec = 1;
526 struct token *next = token->next;
527 const char *separator = "";
528 if (next->pos.whitespace)
529 separator = " ";
530 if (next->pos.newline) {
531 separator = "\n\t\t\t\t\t";
532 prec = next->pos.pos;
533 if (prec > 4)
534 prec = 4;
536 printf("%s%.*s", show_token(token), prec, separator);
537 token = next;
539 putchar('\n');
541 return NULL;
544 // Parse the resulting C code
545 while (!eof_token(token))
546 token = external_declaration(token, &translation_unit_used_list);
547 return translation_unit_used_list;
550 static struct symbol_list *sparse_file(const char *filename)
552 int fd;
553 struct token *token;
555 if (strcmp (filename, "-") == 0) {
556 fd = 0;
557 } else {
558 fd = open(filename, O_RDONLY);
559 if (fd < 0)
560 die("No such file: %s", filename);
563 // Tokenize the input stream
564 token = tokenize(filename, fd, NULL, includepath);
565 close(fd);
567 return sparse_tokenstream(token);
571 * This handles the "-include" directive etc: we're in global
572 * scope, and all types/macros etc will affect all the following
573 * files.
575 * NOTE NOTE NOTE! "#undef" of anything in this stage will
576 * affect all subsequent files too, ie we can have non-local
577 * behaviour between files!
579 static struct symbol_list *sparse_initial(void)
581 struct token *token;
583 // Prepend any "include" file to the stream.
584 // We're in global scope, it will affect all files!
585 token = NULL;
586 if (include_fd >= 0)
587 token = tokenize(include, include_fd, NULL, includepath);
589 // Prepend the initial built-in stream
590 token = tokenize_buffer(pre_buffer, pre_buffer_size, token);
591 return sparse_tokenstream(token);
594 struct symbol_list *sparse_initialize(int argc, char **argv)
596 char **args;
597 int files = 0;
598 struct symbol_list *list;
600 // Initialize symbol stream first, so that we can add defines etc
601 init_symbols();
603 args = argv;
604 for (;;) {
605 char *arg = *++args;
606 if (!arg)
607 break;
609 if (arg[0] == '-' && arg[1]) {
610 args = handle_switch(arg+1, args);
611 continue;
615 * Hacky hacky hacky: we re-use the argument space
616 * to save the filenames.
618 argv[files++] = arg;
621 list = NULL;
622 argv[files] = NULL;
623 if (files) {
624 // Initialize type system
625 init_ctype();
627 create_builtin_stream();
628 add_pre_buffer("#define __CHECKER__ 1\n");
629 if (!preprocess_only)
630 declare_builtin_functions();
632 list = sparse_initial();
635 * Protect the initial token allocations, since
636 * they need to survive all the others
638 protect_token_alloc();
640 return list;
643 struct symbol_list * __sparse(char **argv)
645 struct symbol_list *res;
646 char *filename, *next;
648 /* Clear previous symbol list */
649 translation_unit_used_list = NULL;
651 filename = *argv;
652 if (!filename)
653 return NULL;
654 do {
655 next = argv[1];
656 *argv++ = next;
657 } while (next);
659 start_file_scope();
660 res = sparse_file(filename);
661 end_file_scope();
663 /* Drop the tokens for this file after parsing */
664 clear_token_alloc();
666 /* And return it */
667 return res;
670 struct symbol_list * sparse(char **argv)
672 struct symbol_list *res = __sparse(argv);
674 /* Evaluate the complete symbol list */
675 evaluate_symbol_list(res);
677 return res;