[PATCH] new flag - -Wdecl
[smatch.git] / lib.c
blobafec1e6acd4a68946a1be48dc6f9b66baff19bde
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;
98 void info(struct position pos, const char * fmt, ...)
100 va_list args;
102 if (!max_warnings)
103 return;
104 va_start(args, fmt);
105 do_warn("", pos, fmt, args);
106 va_end(args);
109 void warning(struct position pos, const char * fmt, ...)
111 va_list args;
113 if (!max_warnings)
114 return;
116 if (!--max_warnings)
117 fmt = "too many warnings";
119 va_start(args, fmt);
120 do_warn("warning: ", pos, fmt, args);
121 va_end(args);
124 void sparse_error(struct position pos, const char * fmt, ...)
126 static int errors = 0;
127 va_list args;
128 die_if_error = 1;
129 /* Shut up warnings after an error */
130 max_warnings = 0;
131 if (errors > 100) {
132 static int once = 0;
133 if (once)
134 return;
135 fmt = "too many errors";
136 once = 1;
139 va_start(args, fmt);
140 do_warn("error: ", pos, fmt, args);
141 va_end(args);
142 errors++;
145 void error_die(struct position pos, const char * fmt, ...)
147 va_list args;
148 va_start(args, fmt);
149 do_warn("error: ", pos, fmt, args);
150 va_end(args);
151 exit(1);
154 void die(const char *fmt, ...)
156 va_list args;
157 static char buffer[512];
159 va_start(args, fmt);
160 vsnprintf(buffer, sizeof(buffer), fmt, args);
161 va_end(args);
163 fprintf(stderr, "%s\n", buffer);
164 exit(1);
167 static unsigned int pre_buffer_size;
168 static char pre_buffer[8192];
170 int Wdefault_bitfield_sign = 0;
171 int Wbitwise = 0;
172 int Wtypesign = 0;
173 int Wcontext = 0;
174 int Wundefined_preprocessor = 0;
175 int Wptr_subtraction_blows = 0;
176 int Wcast_to_address_space = 0;
177 int Wdecl = 0;
178 int Wtransparent_union = 1;
179 int preprocess_only;
180 char *include;
181 int include_fd = -1;
184 void add_pre_buffer(const char *fmt, ...)
186 va_list args;
187 unsigned int size;
189 va_start(args, fmt);
190 size = pre_buffer_size;
191 size += vsnprintf(pre_buffer + size,
192 sizeof(pre_buffer) - size,
193 fmt, args);
194 pre_buffer_size = size;
195 va_end(args);
198 static char **handle_switch_D(char *arg, char **next)
200 const char *name = arg + 1;
201 const char *value = "1";
202 for (;;) {
203 char c;
204 c = *++arg;
205 if (!c)
206 break;
207 if (isspace((unsigned char)c) || c == '=') {
208 *arg = '\0';
209 value = arg + 1;
210 break;
213 add_pre_buffer("#define %s %s\n", name, value);
214 return next;
217 static char **handle_switch_E(char *arg, char **next)
219 preprocess_only = 1;
220 return next;
223 static char **handle_switch_v(char *arg, char **next)
225 do {
226 verbose++;
227 } while (*++arg == 'v');
228 return next;
231 static char **handle_switch_I(char *arg, char **next)
233 char *path = arg+1;
235 switch (arg[1]) {
236 case '-':
237 add_pre_buffer("#split_include\n");
238 break;
240 case '\0': /* Plain "-I" */
241 path = *++next;
242 if (!path)
243 die("missing argument for -I option");
244 /* Fallthrough */
245 default:
246 add_pre_buffer("#add_include \"%s/\"\n", path);
248 return next;
251 static char **handle_switch_i(char *arg, char **next)
253 if (*next && !strcmp(arg, "include")) {
254 char *name = *++next;
255 int fd = open(name, O_RDONLY);
257 include_fd = fd;
258 include = name;
259 if (fd < 0)
260 perror(name);
262 if (*next && !strcmp(arg, "imacros")) {
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 else if (*next && !strcmp(arg, "isystem")) {
272 char *path = *++next;
273 if (!path)
274 die("missing argument for -isystem option");
275 add_pre_buffer("#add_isystem \"%s/\"\n", path);
277 return next;
280 static char **handle_switch_M(char *arg, char **next)
282 if (!strcmp(arg, "MF") || !strcmp(arg,"MQ") || !strcmp(arg,"MT")) {
283 if (!*next)
284 die("missing argument for -%s option", arg);
285 return next + 1;
287 return next;
290 static char **handle_switch_m(char *arg, char **next)
292 if (!strcmp(arg, "m64")) {
293 bits_in_long = 64;
294 max_int_alignment = 8;
295 bits_in_pointer = 64;
296 pointer_alignment = 8;
298 return next;
301 static char **handle_switch_o(char *arg, char **next)
303 if (!strcmp (arg, "o") && *next)
304 return next + 1; // "-o foo"
305 else
306 return next; // "-ofoo" or (bogus) terminal "-o"
309 static const struct warning {
310 const char *name;
311 int *flag;
312 } warnings[] = {
313 { "cast-to-as", &Wcast_to_address_space },
314 { "decl", &Wdecl },
315 { "ptr-subtraction-blows", &Wptr_subtraction_blows },
316 { "default-bitfield-sign", &Wdefault_bitfield_sign },
317 { "undef", &Wundefined_preprocessor },
318 { "bitwise", &Wbitwise },
319 { "typesign", &Wtypesign },
320 { "context", &Wcontext },
321 { "transparent-union", &Wtransparent_union },
325 static char **handle_switch_W(char *arg, char **next)
327 int no = 0;
328 char *p = arg + 1;
329 unsigned i;
331 // Prefixes "no" and "no-" mean to turn warning off.
332 if (p[0] == 'n' && p[1] == 'o') {
333 p += 2;
334 if (p[0] == '-')
335 p++;
336 no = 1;
339 for (i = 0; i < sizeof(warnings) / sizeof(warnings[0]); i++) {
340 if (!strcmp(p,warnings[i].name)) {
341 *warnings[i].flag = !no;
342 return next;
346 // Unknown.
347 return next;
350 static char **handle_switch_U(char *arg, char **next)
352 const char *name = arg + 1;
353 add_pre_buffer ("#undef %s\n", name);
354 return next;
357 static char **handle_switch_O(char *arg, char **next)
359 int level = 1;
360 if (arg[1] >= '0' && arg[1] <= '9')
361 level = arg[1] - '0';
362 optimize = level;
363 optimize_size = arg[1] == 's';
364 return next;
367 static char **handle_switch_f(char *arg, char **next)
369 int flag = 1;
371 arg++;
372 if (!strncmp(arg, "no-", 3)) {
373 flag = 0;
374 arg += 3;
376 /* handle switch here.. */
377 return next;
380 static char **handle_switch_G(char *arg, char **next)
382 if (!strcmp (arg, "G") && *next)
383 return next + 1; // "-G 0"
384 else
385 return next; // "-G0" or (bogus) terminal "-G"
388 static char **handle_nostdinc(char *arg, char **next)
390 add_pre_buffer("#nostdinc\n");
391 return next;
394 static char **handle_dirafter(char *arg, char **next)
396 char *path = *++next;
397 if (!path)
398 die("missing argument for -dirafter option");
399 add_pre_buffer("#add_dirafter \"%s/\"\n", path);
400 return next;
403 struct switches {
404 const char *name;
405 char **(*fn)(char *, char**);
408 char **handle_switch(char *arg, char **next)
410 static struct switches cmd[] = {
411 { "nostdinc", handle_nostdinc },
412 { "dirafter", handle_dirafter },
413 { NULL, NULL }
415 struct switches *s;
417 switch (*arg) {
418 case 'D': return handle_switch_D(arg, next);
419 case 'E': return handle_switch_E(arg, next);
420 case 'I': return handle_switch_I(arg, next);
421 case 'i': return handle_switch_i(arg, next);
422 case 'M': return handle_switch_M(arg, next);
423 case 'm': return handle_switch_m(arg, next);
424 case 'o': return handle_switch_o(arg, next);
425 case 'U': return handle_switch_U(arg, next);
426 case 'v': return handle_switch_v(arg, next);
427 case 'W': return handle_switch_W(arg, next);
428 case 'O': return handle_switch_O(arg, next);
429 case 'f': return handle_switch_f(arg, next);
430 case 'G': return handle_switch_G(arg, next);
431 default:
432 break;
435 s = cmd;
436 while (s->name) {
437 if (!strcmp(s->name, arg))
438 return s->fn(arg, next);
439 s++;
443 * Ignore unknown command line options:
444 * they're probably gcc switches
446 return next;
449 void declare_builtin_functions(void)
451 /* Gaah. gcc knows tons of builtin <string.h> functions */
452 add_pre_buffer("extern void *__builtin_memcpy(void *, const void *, __SIZE_TYPE__);\n");
453 add_pre_buffer("extern void *__builtin_memset(void *, int, __SIZE_TYPE__);\n");
454 add_pre_buffer("extern int __builtin_memcmp(const void *, const void *, __SIZE_TYPE__);\n");
455 add_pre_buffer("extern int __builtin_strcmp(const char *, const char *);\n");
456 add_pre_buffer("extern char *__builtin_strchr(const char *, int);\n");
457 add_pre_buffer("extern char *__builtin_strcpy(char *, const char *);\n");
458 add_pre_buffer("extern char *__builtin_strncpy(char *, const char *, __SIZE_TYPE__);\n");
459 add_pre_buffer("extern __SIZE_TYPE__ __builtin_strspn(const char *, const char *);\n");
460 add_pre_buffer("extern __SIZE_TYPE__ __builtin_strcspn(const char *, const char *);\n");
462 /* And some random ones.. */
463 add_pre_buffer("extern void *__builtin_return_address(unsigned int);\n");
464 add_pre_buffer("extern void *__builtin_frame_address(unsigned int);\n");
465 add_pre_buffer("extern void __builtin_trap(void);\n");
466 add_pre_buffer("extern int __builtin_ffs(int);\n");
467 add_pre_buffer("extern void *__builtin_alloca(__SIZE_TYPE__);\n");
470 void create_builtin_stream(void)
472 add_pre_buffer("#define __GNUC__ %d\n", gcc_major);
473 add_pre_buffer("#define __GNUC_MINOR__ %d\n", gcc_minor);
474 add_pre_buffer("#define __GNUC_PATCHLEVEL__ %d\n", gcc_patchlevel);
475 add_pre_buffer("#define __extension__\n");
476 add_pre_buffer("#define __pragma__\n");
478 // gcc defines __SIZE_TYPE__ to be size_t. For linux/i86 and
479 // solaris/sparc that is really "unsigned int" and for linux/x86_64
480 // it is "long unsigned int". In either case we can probably
481 // get away with this. We need the #ifndef as cgcc will define
482 // the right __SIZE_TYPE__.
483 add_pre_buffer("#weak_define __SIZE_TYPE__ long unsigned int\n");
484 add_pre_buffer("#weak_define __STDC__ 1\n");
486 add_pre_buffer("#define __builtin_stdarg_start(a,b) ((a) = (__builtin_va_list)(&(b)))\n");
487 add_pre_buffer("#define __builtin_va_start(a,b) ((a) = (__builtin_va_list)(&(b)))\n");
488 add_pre_buffer("#define __builtin_va_arg(arg,type) ({ type __va_arg_ret = *(type *)(arg); arg += sizeof(type); __va_arg_ret; })\n");
489 add_pre_buffer("#define __builtin_va_alist (*(void *)0)\n");
490 add_pre_buffer("#define __builtin_va_arg_incr(x) ((x) + 1)\n");
491 add_pre_buffer("#define __builtin_va_end(arg)\n");
492 add_pre_buffer("#define __builtin_offsetof(type, name) ((__SIZE_TYPE__)&((type *)(0ul))->name)\n");
494 /* FIXME! We need to do these as special magic macros at expansion time! */
495 add_pre_buffer("#define __BASE_FILE__ \"base_file.c\"\n");
496 add_pre_buffer("#define __DATE__ \"??? ?? ????\"\n");
497 add_pre_buffer("#define __TIME__ \"??:??:??\"\n");
499 if (optimize)
500 add_pre_buffer("#define __OPTIMIZE__ 1\n");
501 if (optimize_size)
502 add_pre_buffer("#define __OPTIMIZE_SIZE__ 1\n");
505 static struct symbol_list *sparse_tokenstream(struct token *token)
507 // Pre-process the stream
508 token = preprocess(token);
510 if (preprocess_only) {
511 while (!eof_token(token)) {
512 int prec = 1;
513 struct token *next = token->next;
514 const char *separator = "";
515 if (next->pos.whitespace)
516 separator = " ";
517 if (next->pos.newline) {
518 separator = "\n\t\t\t\t\t";
519 prec = next->pos.pos;
520 if (prec > 4)
521 prec = 4;
523 printf("%s%.*s", show_token(token), prec, separator);
524 token = next;
526 putchar('\n');
528 return NULL;
531 // Parse the resulting C code
532 while (!eof_token(token))
533 token = external_declaration(token, &translation_unit_used_list);
534 return translation_unit_used_list;
537 static struct symbol_list *sparse_file(const char *filename)
539 int fd;
540 struct token *token;
542 if (strcmp (filename, "-") == 0) {
543 fd = 0;
544 } else {
545 fd = open(filename, O_RDONLY);
546 if (fd < 0)
547 die("No such file: %s", filename);
550 // Tokenize the input stream
551 token = tokenize(filename, fd, NULL, includepath);
552 close(fd);
554 return sparse_tokenstream(token);
558 * This handles the "-include" directive etc: we're in global
559 * scope, and all types/macros etc will affect all the following
560 * files.
562 * NOTE NOTE NOTE! "#undef" of anything in this stage will
563 * affect all subsequent files too, ie we can have non-local
564 * behaviour between files!
566 static struct symbol_list *sparse_initial(void)
568 struct token *token;
570 // Prepend any "include" file to the stream.
571 // We're in global scope, it will affect all files!
572 token = NULL;
573 if (include_fd >= 0)
574 token = tokenize(include, include_fd, NULL, includepath);
576 // Prepend the initial built-in stream
577 token = tokenize_buffer(pre_buffer, pre_buffer_size, token);
578 return sparse_tokenstream(token);
581 struct symbol_list *sparse_initialize(int argc, char **argv)
583 char **args;
584 int files = 0;
585 struct symbol_list *list;
587 // Initialize symbol stream first, so that we can add defines etc
588 init_symbols();
590 args = argv;
591 for (;;) {
592 char *arg = *++args;
593 if (!arg)
594 break;
596 if (arg[0] == '-' && arg[1]) {
597 args = handle_switch(arg+1, args);
598 continue;
602 * Hacky hacky hacky: we re-use the argument space
603 * to save the filenames.
605 argv[files++] = arg;
608 list = NULL;
609 argv[files] = NULL;
610 if (files) {
611 // Initialize type system
612 init_ctype();
614 create_builtin_stream();
615 add_pre_buffer("#define __CHECKER__ 1\n");
616 if (!preprocess_only)
617 declare_builtin_functions();
619 list = sparse_initial();
622 * Protect the initial token allocations, since
623 * they need to survive all the others
625 protect_token_alloc();
627 return list;
630 struct symbol_list * sparse(char **argv)
632 struct symbol_list *res;
633 char *filename, *next;
635 /* Clear previous symbol list */
636 translation_unit_used_list = NULL;
638 filename = *argv;
639 if (!filename)
640 return NULL;
641 do {
642 next = argv[1];
643 *argv++ = next;
644 } while (next);
646 start_file_scope();
647 res = sparse_file(filename);
648 end_file_scope();
650 /* Drop the tokens for this file after parsing */
651 clear_token_alloc();
653 /* Evaluate the complete symbol list */
654 evaluate_symbol_list(res);
656 /* And return it */
657 return res;