Support -Wall flag
[smatch.git] / lib.c
blob51c415d2094d8ed12f698374994d5873277c9e47
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;
193 int preprocess_only;
194 char *include;
195 int include_fd = -1;
198 void add_pre_buffer(const char *fmt, ...)
200 va_list args;
201 unsigned int size;
203 va_start(args, fmt);
204 size = pre_buffer_size;
205 size += vsnprintf(pre_buffer + size,
206 sizeof(pre_buffer) - size,
207 fmt, args);
208 pre_buffer_size = size;
209 va_end(args);
212 static char **handle_switch_D(char *arg, char **next)
214 const char *name = arg + 1;
215 const char *value = "1";
216 for (;;) {
217 char c;
218 c = *++arg;
219 if (!c)
220 break;
221 if (isspace((unsigned char)c) || c == '=') {
222 *arg = '\0';
223 value = arg + 1;
224 break;
227 add_pre_buffer("#define %s %s\n", name, value);
228 return next;
231 static char **handle_switch_E(char *arg, char **next)
233 preprocess_only = 1;
234 return next;
237 static char **handle_switch_v(char *arg, char **next)
239 do {
240 verbose++;
241 } while (*++arg == 'v');
242 return next;
245 static char **handle_switch_I(char *arg, char **next)
247 char *path = arg+1;
249 switch (arg[1]) {
250 case '-':
251 add_pre_buffer("#split_include\n");
252 break;
254 case '\0': /* Plain "-I" */
255 path = *++next;
256 if (!path)
257 die("missing argument for -I option");
258 /* Fallthrough */
259 default:
260 add_pre_buffer("#add_include \"%s/\"\n", path);
262 return next;
265 static char **handle_switch_i(char *arg, char **next)
267 if (*next && !strcmp(arg, "include")) {
268 char *name = *++next;
269 int fd = open(name, O_RDONLY);
271 include_fd = fd;
272 include = name;
273 if (fd < 0)
274 perror(name);
276 if (*next && !strcmp(arg, "imacros")) {
277 char *name = *++next;
278 int fd = open(name, O_RDONLY);
280 include_fd = fd;
281 include = name;
282 if (fd < 0)
283 perror(name);
285 else if (*next && !strcmp(arg, "isystem")) {
286 char *path = *++next;
287 if (!path)
288 die("missing argument for -isystem option");
289 add_pre_buffer("#add_isystem \"%s/\"\n", path);
291 return next;
294 static char **handle_switch_M(char *arg, char **next)
296 if (!strcmp(arg, "MF") || !strcmp(arg,"MQ") || !strcmp(arg,"MT")) {
297 if (!*next)
298 die("missing argument for -%s option", arg);
299 return next + 1;
301 return next;
304 static char **handle_switch_m(char *arg, char **next)
306 if (!strcmp(arg, "m64")) {
307 bits_in_long = 64;
308 max_int_alignment = 8;
309 bits_in_pointer = 64;
310 pointer_alignment = 8;
312 return next;
315 static char **handle_switch_o(char *arg, char **next)
317 if (!strcmp (arg, "o") && *next)
318 return next + 1; // "-o foo"
319 else
320 return next; // "-ofoo" or (bogus) terminal "-o"
323 static const struct warning {
324 const char *name;
325 int *flag;
326 } warnings[] = {
327 { "cast-to-as", &Wcast_to_address_space },
328 { "decl", &Wdecl },
329 { "one-bit-signed-bitfield", &Wone_bit_signed_bitfield },
330 { "cast-truncate", &Wcast_truncate },
331 { "ptr-subtraction-blows", &Wptr_subtraction_blows },
332 { "default-bitfield-sign", &Wdefault_bitfield_sign },
333 { "undef", &Wundefined_preprocessor },
334 { "bitwise", &Wbitwise },
335 { "typesign", &Wtypesign },
336 { "context", &Wcontext },
337 { "transparent-union", &Wtransparent_union },
338 { "shadow", &Wshadow },
339 { "address-space", &Waddress_space },
340 { "enum-mismatch", &Wenum_mismatch },
341 { "do-while", &Wdo_while },
342 { "uninitialized", &Wuninitialized },
345 enum {
346 WARNING_OFF,
347 WARNING_ON,
348 WARNING_FORCE_OFF
352 static char **handle_switch_W(char *arg, char **next)
354 int flag = WARNING_ON;
355 char *p = arg + 1;
356 unsigned i;
358 if (!strcmp(p, "all")) {
359 for (i = 0; i < sizeof(warnings) / sizeof(warnings[0]); i++) {
360 if (*warnings[i].flag != WARNING_FORCE_OFF)
361 *warnings[i].flag = WARNING_ON;
365 // Prefixes "no" and "no-" mean to turn warning off.
366 if (p[0] == 'n' && p[1] == 'o') {
367 p += 2;
368 if (p[0] == '-')
369 p++;
370 flag = WARNING_FORCE_OFF;
373 for (i = 0; i < sizeof(warnings) / sizeof(warnings[0]); i++) {
374 if (!strcmp(p,warnings[i].name)) {
375 *warnings[i].flag = flag;
376 return next;
380 // Unknown.
381 return next;
384 static void handle_switch_W_finalize(void)
386 unsigned i;
388 for (i = 0; i < sizeof(warnings) / sizeof(warnings[0]); i++) {
389 if (*warnings[i].flag == WARNING_FORCE_OFF)
390 *warnings[i].flag = WARNING_OFF;
394 static char **handle_switch_U(char *arg, char **next)
396 const char *name = arg + 1;
397 add_pre_buffer ("#undef %s\n", name);
398 return next;
401 static char **handle_switch_O(char *arg, char **next)
403 int level = 1;
404 if (arg[1] >= '0' && arg[1] <= '9')
405 level = arg[1] - '0';
406 optimize = level;
407 optimize_size = arg[1] == 's';
408 return next;
411 static char **handle_switch_f(char *arg, char **next)
413 int flag = 1;
415 arg++;
416 if (!strncmp(arg, "no-", 3)) {
417 flag = 0;
418 arg += 3;
420 /* handle switch here.. */
421 return next;
424 static char **handle_switch_G(char *arg, char **next)
426 if (!strcmp (arg, "G") && *next)
427 return next + 1; // "-G 0"
428 else
429 return next; // "-G0" or (bogus) terminal "-G"
432 static char **handle_nostdinc(char *arg, char **next)
434 add_pre_buffer("#nostdinc\n");
435 return next;
438 static char **handle_dirafter(char *arg, char **next)
440 char *path = *++next;
441 if (!path)
442 die("missing argument for -dirafter option");
443 add_pre_buffer("#add_dirafter \"%s/\"\n", path);
444 return next;
447 struct switches {
448 const char *name;
449 char **(*fn)(char *, char**);
452 char **handle_switch(char *arg, char **next)
454 static struct switches cmd[] = {
455 { "nostdinc", handle_nostdinc },
456 { "dirafter", handle_dirafter },
457 { NULL, NULL }
459 struct switches *s;
461 switch (*arg) {
462 case 'D': return handle_switch_D(arg, next);
463 case 'E': return handle_switch_E(arg, next);
464 case 'I': return handle_switch_I(arg, next);
465 case 'i': return handle_switch_i(arg, next);
466 case 'M': return handle_switch_M(arg, next);
467 case 'm': return handle_switch_m(arg, next);
468 case 'o': return handle_switch_o(arg, next);
469 case 'U': return handle_switch_U(arg, next);
470 case 'v': return handle_switch_v(arg, next);
471 case 'W': return handle_switch_W(arg, next);
472 case 'O': return handle_switch_O(arg, next);
473 case 'f': return handle_switch_f(arg, next);
474 case 'G': return handle_switch_G(arg, next);
475 default:
476 break;
479 s = cmd;
480 while (s->name) {
481 if (!strcmp(s->name, arg))
482 return s->fn(arg, next);
483 s++;
487 * Ignore unknown command line options:
488 * they're probably gcc switches
490 return next;
493 void declare_builtin_functions(void)
495 /* Gaah. gcc knows tons of builtin <string.h> functions */
496 add_pre_buffer("extern void *__builtin_memcpy(void *, const void *, __SIZE_TYPE__);\n");
497 add_pre_buffer("extern void *__builtin_memset(void *, int, __SIZE_TYPE__);\n");
498 add_pre_buffer("extern int __builtin_memcmp(const void *, const void *, __SIZE_TYPE__);\n");
499 add_pre_buffer("extern int __builtin_strcmp(const char *, const char *);\n");
500 add_pre_buffer("extern char *__builtin_strchr(const char *, int);\n");
501 add_pre_buffer("extern char *__builtin_strcpy(char *, const char *);\n");
502 add_pre_buffer("extern char *__builtin_strncpy(char *, const char *, __SIZE_TYPE__);\n");
503 add_pre_buffer("extern __SIZE_TYPE__ __builtin_strspn(const char *, const char *);\n");
504 add_pre_buffer("extern __SIZE_TYPE__ __builtin_strcspn(const char *, const char *);\n");
505 add_pre_buffer("extern char * __builtin_strpbrk(const char *, const char *);\n");
507 /* And some random ones.. */
508 add_pre_buffer("extern void *__builtin_return_address(unsigned int);\n");
509 add_pre_buffer("extern void *__builtin_extract_return_addr(void *);\n");
510 add_pre_buffer("extern void *__builtin_frame_address(unsigned int);\n");
511 add_pre_buffer("extern void __builtin_trap(void);\n");
512 add_pre_buffer("extern int __builtin_ffs(int);\n");
513 add_pre_buffer("extern void *__builtin_alloca(__SIZE_TYPE__);\n");
514 add_pre_buffer("extern int __builtin_popcount(unsigned int);\n");
515 add_pre_buffer("extern int __builtin_popcountl(unsigned long);\n");
516 add_pre_buffer("extern void __builtin_prefetch (const void *, ...);\n");
517 add_pre_buffer("extern long __builtin_alpha_extbl(long, long);\n");
518 add_pre_buffer("extern long __builtin_alpha_extwl(long, long);\n");
519 add_pre_buffer("extern long __builtin_alpha_insbl(long, long);\n");
520 add_pre_buffer("extern long __builtin_alpha_inswl(long, long);\n");
521 add_pre_buffer("extern long __builtin_alpha_insql(long, long);\n");
522 add_pre_buffer("extern long __builtin_alpha_inslh(long, long);\n");
523 add_pre_buffer("extern long __builtin_alpha_cmpbge(long, long);\n");
526 void create_builtin_stream(void)
528 add_pre_buffer("#weak_define __GNUC__ %d\n", gcc_major);
529 add_pre_buffer("#weak_define __GNUC_MINOR__ %d\n", gcc_minor);
530 add_pre_buffer("#weak_define __GNUC_PATCHLEVEL__ %d\n", gcc_patchlevel);
531 add_pre_buffer("#define __extension__\n");
532 add_pre_buffer("#define __pragma__\n");
534 // gcc defines __SIZE_TYPE__ to be size_t. For linux/i86 and
535 // solaris/sparc that is really "unsigned int" and for linux/x86_64
536 // it is "long unsigned int". In either case we can probably
537 // get away with this. We need the #ifndef as cgcc will define
538 // the right __SIZE_TYPE__.
539 add_pre_buffer("#weak_define __SIZE_TYPE__ long unsigned int\n");
540 add_pre_buffer("#weak_define __STDC__ 1\n");
542 add_pre_buffer("#define __builtin_stdarg_start(a,b) ((a) = (__builtin_va_list)(&(b)))\n");
543 add_pre_buffer("#define __builtin_va_start(a,b) ((a) = (__builtin_va_list)(&(b)))\n");
544 add_pre_buffer("#define __builtin_va_arg(arg,type) ({ type __va_arg_ret = *(type *)(arg); arg += sizeof(type); __va_arg_ret; })\n");
545 add_pre_buffer("#define __builtin_va_alist (*(void *)0)\n");
546 add_pre_buffer("#define __builtin_va_arg_incr(x) ((x) + 1)\n");
547 add_pre_buffer("#define __builtin_va_copy(dest, src) ({ dest = src; (void)0; })\n");
548 add_pre_buffer("#define __builtin_va_end(arg)\n");
549 add_pre_buffer("#define __builtin_offsetof(type, name) ((__SIZE_TYPE__)&((type *)(0ul))->name)\n");
551 /* FIXME! We need to do these as special magic macros at expansion time! */
552 add_pre_buffer("#define __BASE_FILE__ \"base_file.c\"\n");
553 add_pre_buffer("#define __DATE__ \"??? ?? ????\"\n");
554 add_pre_buffer("#define __TIME__ \"??:??:??\"\n");
556 if (optimize)
557 add_pre_buffer("#define __OPTIMIZE__ 1\n");
558 if (optimize_size)
559 add_pre_buffer("#define __OPTIMIZE_SIZE__ 1\n");
562 static struct symbol_list *sparse_tokenstream(struct token *token)
564 // Pre-process the stream
565 token = preprocess(token);
567 if (preprocess_only) {
568 while (!eof_token(token)) {
569 int prec = 1;
570 struct token *next = token->next;
571 const char *separator = "";
572 if (next->pos.whitespace)
573 separator = " ";
574 if (next->pos.newline) {
575 separator = "\n\t\t\t\t\t";
576 prec = next->pos.pos;
577 if (prec > 4)
578 prec = 4;
580 printf("%s%.*s", show_token(token), prec, separator);
581 token = next;
583 putchar('\n');
585 return NULL;
588 // Parse the resulting C code
589 while (!eof_token(token))
590 token = external_declaration(token, &translation_unit_used_list);
591 return translation_unit_used_list;
594 static struct symbol_list *sparse_file(const char *filename)
596 int fd;
597 struct token *token;
599 if (strcmp (filename, "-") == 0) {
600 fd = 0;
601 } else {
602 fd = open(filename, O_RDONLY);
603 if (fd < 0)
604 die("No such file: %s", filename);
607 // Tokenize the input stream
608 token = tokenize(filename, fd, NULL, includepath);
609 close(fd);
611 return sparse_tokenstream(token);
615 * This handles the "-include" directive etc: we're in global
616 * scope, and all types/macros etc will affect all the following
617 * files.
619 * NOTE NOTE NOTE! "#undef" of anything in this stage will
620 * affect all subsequent files too, ie we can have non-local
621 * behaviour between files!
623 static struct symbol_list *sparse_initial(void)
625 struct token *token;
627 // Prepend any "include" file to the stream.
628 // We're in global scope, it will affect all files!
629 token = NULL;
630 if (include_fd >= 0)
631 token = tokenize(include, include_fd, NULL, includepath);
633 // Prepend the initial built-in stream
634 token = tokenize_buffer(pre_buffer, pre_buffer_size, token);
635 return sparse_tokenstream(token);
638 struct symbol_list *sparse_initialize(int argc, char **argv, struct string_list **filelist)
640 char **args;
641 struct symbol_list *list;
643 // Initialize symbol stream first, so that we can add defines etc
644 init_symbols();
646 args = argv;
647 for (;;) {
648 char *arg = *++args;
649 if (!arg)
650 break;
652 if (arg[0] == '-' && arg[1]) {
653 args = handle_switch(arg+1, args);
654 continue;
656 add_ptr_list_notag(filelist, arg);
658 handle_switch_W_finalize();
660 list = NULL;
661 if (!ptr_list_empty(filelist)) {
662 // Initialize type system
663 init_ctype();
665 create_builtin_stream();
666 add_pre_buffer("#define __CHECKER__ 1\n");
667 if (!preprocess_only)
668 declare_builtin_functions();
670 list = sparse_initial();
673 * Protect the initial token allocations, since
674 * they need to survive all the others
676 protect_token_alloc();
678 return list;
681 struct symbol_list * __sparse(char *filename)
683 struct symbol_list *res;
685 /* Clear previous symbol list */
686 translation_unit_used_list = NULL;
688 new_file_scope();
689 res = sparse_file(filename);
691 /* Drop the tokens for this file after parsing */
692 clear_token_alloc();
694 /* And return it */
695 return res;
698 struct symbol_list * sparse(char *filename)
700 struct symbol_list *res = __sparse(filename);
702 /* Evaluate the complete symbol list */
703 evaluate_symbol_list(res);
705 return res;