[PATCH] replaced warnings with errors.
[smatch.git] / lib.c
blob3a814d625f4597d1d9b66ba9a2e92876a6a8c85d
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 error(token->pos, "Expected %s %s", show_special(op), where);
58 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 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 Wtransparent_union = 1;
178 int preprocess_only;
179 char *include;
180 int include_fd = -1;
183 void add_pre_buffer(const char *fmt, ...)
185 va_list args;
186 unsigned int size;
188 va_start(args, fmt);
189 size = pre_buffer_size;
190 size += vsnprintf(pre_buffer + size,
191 sizeof(pre_buffer) - size,
192 fmt, args);
193 pre_buffer_size = size;
194 va_end(args);
197 static char **handle_switch_D(char *arg, char **next)
199 const char *name = arg + 1;
200 const char *value = "1";
201 for (;;) {
202 char c;
203 c = *++arg;
204 if (!c)
205 break;
206 if (isspace((unsigned char)c) || c == '=') {
207 *arg = '\0';
208 value = arg + 1;
209 break;
212 add_pre_buffer("#define %s %s\n", name, value);
213 return next;
216 static char **handle_switch_E(char *arg, char **next)
218 preprocess_only = 1;
219 return next;
222 static char **handle_switch_v(char *arg, char **next)
224 do {
225 verbose++;
226 } while (*++arg == 'v');
227 return next;
230 static char **handle_switch_I(char *arg, char **next)
232 char *path = arg+1;
234 switch (arg[1]) {
235 case '-':
236 add_pre_buffer("#split_include\n");
237 break;
239 case '\0': /* Plain "-I" */
240 path = *++next;
241 if (!path)
242 die("missing argument for -I option");
243 /* Fallthrough */
244 default:
245 add_pre_buffer("#add_include \"%s/\"\n", path);
247 return next;
250 static char **handle_switch_i(char *arg, char **next)
252 if (*next && !strcmp(arg, "include")) {
253 char *name = *++next;
254 int fd = open(name, O_RDONLY);
256 include_fd = fd;
257 include = name;
258 if (fd < 0)
259 perror(name);
261 if (*next && !strcmp(arg, "imacros")) {
262 char *name = *++next;
263 int fd = open(name, O_RDONLY);
265 include_fd = fd;
266 include = name;
267 if (fd < 0)
268 perror(name);
270 else if (*next && !strcmp(arg, "isystem")) {
271 char *path = *++next;
272 if (!path)
273 die("missing argument for -isystem option");
274 add_pre_buffer("#add_isystem \"%s/\"\n", path);
276 return next;
279 static char **handle_switch_M(char *arg, char **next)
281 if (!strcmp(arg, "MF") || !strcmp(arg,"MQ") || !strcmp(arg,"MT")) {
282 if (!*next)
283 die("missing argument for -%s option", arg);
284 return next + 1;
286 return next;
289 static char **handle_switch_m(char *arg, char **next)
291 if (!strcmp(arg, "m64")) {
292 bits_in_long = 64;
293 max_int_alignment = 8;
294 bits_in_pointer = 64;
295 pointer_alignment = 8;
297 return next;
300 static char **handle_switch_o(char *arg, char **next)
302 if (!strcmp (arg, "o") && *next)
303 return next + 1; // "-o foo"
304 else
305 return next; // "-ofoo" or (bogus) terminal "-o"
308 static const struct warning {
309 const char *name;
310 int *flag;
311 } warnings[] = {
312 { "cast-to-as", &Wcast_to_address_space },
313 { "ptr-subtraction-blows", &Wptr_subtraction_blows },
314 { "default-bitfield-sign", &Wdefault_bitfield_sign },
315 { "undef", &Wundefined_preprocessor },
316 { "bitwise", &Wbitwise },
317 { "typesign", &Wtypesign },
318 { "context", &Wcontext },
319 { "transparent-union", &Wtransparent_union },
323 static char **handle_switch_W(char *arg, char **next)
325 int no = 0;
326 char *p = arg + 1;
327 unsigned i;
329 // Prefixes "no" and "no-" mean to turn warning off.
330 if (p[0] == 'n' && p[1] == 'o') {
331 p += 2;
332 if (p[0] == '-')
333 p++;
334 no = 1;
337 for (i = 0; i < sizeof(warnings) / sizeof(warnings[0]); i++) {
338 if (!strcmp(p,warnings[i].name)) {
339 *warnings[i].flag = !no;
340 return next;
344 // Unknown.
345 return next;
348 static char **handle_switch_U(char *arg, char **next)
350 const char *name = arg + 1;
351 add_pre_buffer ("#undef %s\n", name);
352 return next;
355 static char **handle_switch_O(char *arg, char **next)
357 int level = 1;
358 if (arg[1] >= '0' && arg[1] <= '9')
359 level = arg[1] - '0';
360 optimize = level;
361 optimize_size = arg[1] == 's';
362 return next;
365 static char **handle_switch_f(char *arg, char **next)
367 int flag = 1;
369 arg++;
370 if (!strncmp(arg, "no-", 3)) {
371 flag = 0;
372 arg += 3;
374 /* handle switch here.. */
375 return next;
378 static char **handle_nostdinc(char *arg, char **next)
380 add_pre_buffer("#nostdinc\n");
381 return next;
384 static char **handle_dirafter(char *arg, char **next)
386 char *path = *++next;
387 if (!path)
388 die("missing argument for -dirafter option");
389 add_pre_buffer("#add_dirafter \"%s/\"\n", path);
390 return next;
393 struct switches {
394 const char *name;
395 char **(*fn)(char *, char**);
398 char **handle_switch(char *arg, char **next)
400 static struct switches cmd[] = {
401 { "nostdinc", handle_nostdinc },
402 { "dirafter", handle_dirafter },
403 { NULL, NULL }
405 struct switches *s;
407 switch (*arg) {
408 case 'D': return handle_switch_D(arg, next);
409 case 'E': return handle_switch_E(arg, next);
410 case 'I': return handle_switch_I(arg, next);
411 case 'i': return handle_switch_i(arg, next);
412 case 'M': return handle_switch_M(arg, next);
413 case 'm': return handle_switch_m(arg, next);
414 case 'o': return handle_switch_o(arg, next);
415 case 'U': return handle_switch_U(arg, next);
416 case 'v': return handle_switch_v(arg, next);
417 case 'W': return handle_switch_W(arg, next);
418 case 'O': return handle_switch_O(arg, next);
419 case 'f': return handle_switch_f(arg, next);
420 default:
421 break;
424 s = cmd;
425 while (s->name) {
426 if (!strcmp(s->name, arg))
427 return s->fn(arg, next);
428 s++;
432 * Ignore unknown command line options:
433 * they're probably gcc switches
435 return next;
438 void declare_builtin_functions(void)
440 /* Gaah. gcc knows tons of builtin <string.h> functions */
441 add_pre_buffer("extern void *__builtin_memcpy(void *, const void *, __SIZE_TYPE__);\n");
442 add_pre_buffer("extern void *__builtin_memset(void *, int, __SIZE_TYPE__);\n");
443 add_pre_buffer("extern int __builtin_strcmp(const char *, const char *);\n");
444 add_pre_buffer("extern char *__builtin_strchr(const char *, int);\n");
445 add_pre_buffer("extern char *__builtin_strcpy(char *, const char *);\n");
446 add_pre_buffer("extern char *__builtin_strncpy(char *, const char *, __SIZE_TYPE__);\n");
447 add_pre_buffer("extern __SIZE_TYPE__ __builtin_strspn(const char *, const char *);\n");
448 add_pre_buffer("extern __SIZE_TYPE__ __builtin_strcspn(const char *, const char *);\n");
450 /* And some random ones.. */
451 add_pre_buffer("extern void *__builtin_return_address(unsigned int);\n");
452 add_pre_buffer("extern void *__builtin_frame_address(unsigned int);\n");
453 add_pre_buffer("extern void __builtin_trap(void);\n");
454 add_pre_buffer("extern int __builtin_ffs(int);\n");
455 add_pre_buffer("extern void *__builtin_alloca(__SIZE_TYPE__);\n");
458 void create_builtin_stream(void)
460 add_pre_buffer("#define __GNUC__ %d\n", gcc_major);
461 add_pre_buffer("#define __GNUC_MINOR__ %d\n", gcc_minor);
462 add_pre_buffer("#define __GNUC_PATCHLEVEL__ %d\n", gcc_patchlevel);
463 add_pre_buffer("#define __extension__\n");
464 add_pre_buffer("#define __pragma__\n");
466 // gcc defines __SIZE_TYPE__ to be size_t. For linux/i86 and
467 // solaris/sparc that is really "unsigned int" and for linux/x86_64
468 // it is "long unsigned int". In either case we can probably
469 // get away with this. We need the #ifndef as cgcc will define
470 // the right __SIZE_TYPE__.
471 add_pre_buffer("#weak_define __SIZE_TYPE__ long unsigned int\n");
472 add_pre_buffer("#weak_define __STDC__ 1\n");
474 add_pre_buffer("#define __builtin_stdarg_start(a,b) ((a) = (__builtin_va_list)(&(b)))\n");
475 add_pre_buffer("#define __builtin_va_start(a,b) ((a) = (__builtin_va_list)(&(b)))\n");
476 add_pre_buffer("#define __builtin_va_arg(arg,type) ({ type __va_arg_ret = *(type *)(arg); arg += sizeof(type); __va_arg_ret; })\n");
477 add_pre_buffer("#define __builtin_va_alist (*(void *)0)\n");
478 add_pre_buffer("#define __builtin_va_arg_incr(x) ((x) + 1)\n");
479 add_pre_buffer("#define __builtin_va_end(arg)\n");
480 add_pre_buffer("#define __builtin_offsetof(type, name) ((__SIZE_TYPE__)&((type *)(0ul))->name)\n");
482 /* FIXME! We need to do these as special magic macros at expansion time! */
483 add_pre_buffer("#define __BASE_FILE__ \"base_file.c\"\n");
484 add_pre_buffer("#define __DATE__ \"??? ?? ????\"\n");
485 add_pre_buffer("#define __TIME__ \"??:??:??\"\n");
487 if (optimize)
488 add_pre_buffer("#define __OPTIMIZE__ 1\n");
489 if (optimize_size)
490 add_pre_buffer("#define __OPTIMIZE_SIZE__ 1\n");
493 static struct symbol_list *sparse_tokenstream(struct token *token)
495 // Pre-process the stream
496 token = preprocess(token);
498 if (preprocess_only) {
499 while (!eof_token(token)) {
500 int prec = 1;
501 struct token *next = token->next;
502 const char *separator = "";
503 if (next->pos.whitespace)
504 separator = " ";
505 if (next->pos.newline) {
506 separator = "\n\t\t\t\t\t";
507 prec = next->pos.pos;
508 if (prec > 4)
509 prec = 4;
511 printf("%s%.*s", show_token(token), prec, separator);
512 token = next;
514 putchar('\n');
516 return NULL;
519 // Parse the resulting C code
520 while (!eof_token(token))
521 token = external_declaration(token, &translation_unit_used_list);
522 return translation_unit_used_list;
525 static struct symbol_list *sparse_file(const char *filename)
527 int fd;
528 struct token *token;
530 if (strcmp (filename, "-") == 0) {
531 fd = 0;
532 } else {
533 fd = open(filename, O_RDONLY);
534 if (fd < 0)
535 die("No such file: %s", filename);
538 // Tokenize the input stream
539 token = tokenize(filename, fd, NULL, includepath);
540 close(fd);
542 return sparse_tokenstream(token);
546 * This handles the "-include" directive etc: we're in global
547 * scope, and all types/macros etc will affect all the following
548 * files.
550 * NOTE NOTE NOTE! "#undef" of anything in this stage will
551 * affect all subsequent files too, ie we can have non-local
552 * behaviour between files!
554 static void sparse_initial(void)
556 struct token *token;
558 // Prepend any "include" file to the stream.
559 // We're in global scope, it will affect all files!
560 token = NULL;
561 if (include_fd >= 0)
562 token = tokenize(include, include_fd, NULL, includepath);
564 // Prepend the initial built-in stream
565 token = tokenize_buffer(pre_buffer, pre_buffer_size, token);
566 sparse_tokenstream(token);
569 int sparse_initialize(int argc, char **argv)
571 char **args;
572 int files = 0;
574 // Initialize symbol stream first, so that we can add defines etc
575 init_symbols();
577 args = argv;
578 for (;;) {
579 char *arg = *++args;
580 if (!arg)
581 break;
583 if (arg[0] == '-' && arg[1]) {
584 args = handle_switch(arg+1, args);
585 continue;
589 * Hacky hacky hacky: we re-use the argument space
590 * to save the filenames.
592 argv[files++] = arg;
595 argv[files] = NULL;
596 if (files) {
597 // Initialize type system
598 init_ctype();
600 create_builtin_stream();
601 add_pre_buffer("#define __CHECKER__ 1\n");
602 if (!preprocess_only)
603 declare_builtin_functions();
605 sparse_initial();
608 * Protect the initial token allocations, since
609 * they need to survive all the others
611 protect_token_alloc();
613 return files;
616 struct symbol_list * sparse(char **argv)
618 struct symbol_list *res;
619 char *filename, *next;
621 /* Clear previous symbol list */
622 translation_unit_used_list = NULL;
624 filename = *argv;
625 if (!filename)
626 return NULL;
627 do {
628 next = argv[1];
629 *argv++ = next;
630 } while (next);
632 start_file_scope();
633 res = sparse_file(filename);
634 end_file_scope();
636 /* Drop the tokens for this file after parsing */
637 clear_token_alloc();
639 /* Evaluate the complete symbol list */
640 evaluate_symbol_list(res);
642 /* And return it */
643 return res;