[PATCH] sparse: add __GNUC_PATCHLEVEL__
[smatch.git] / lib.c
blob59edad6a15d84ef025f922e59515159fd69fa70a
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 # define __GNUC_PATCHLEVEL__ 0
37 #endif
39 int gcc_major = __GNUC__;
40 int gcc_minor = __GNUC_MINOR__;
41 int gcc_patchlevel = __GNUC_PATCHLEVEL__;
43 struct token *skip_to(struct token *token, int op)
45 while (!match_op(token, op) && !eof_token(token))
46 token = token->next;
47 return token;
50 struct token *expect(struct token *token, int op, const char *where)
52 if (!match_op(token, op)) {
53 static struct token bad_token;
54 if (token != &bad_token) {
55 bad_token.next = token;
56 warning(token->pos, "Expected %s %s", show_special(op), where);
57 warning(token->pos, "got %s", show_token(token));
59 if (op == ';')
60 return skip_to(token, op);
61 return &bad_token;
63 return token->next;
66 unsigned int hexval(unsigned int c)
68 int retval = 256;
69 switch (c) {
70 case '0'...'9':
71 retval = c - '0';
72 break;
73 case 'a'...'f':
74 retval = c - 'a' + 10;
75 break;
76 case 'A'...'F':
77 retval = c - 'A' + 10;
78 break;
80 return retval;
83 static void do_warn(const char *type, struct position pos, const char * fmt, va_list args)
85 static char buffer[512];
86 const char *name;
88 vsprintf(buffer, fmt, args);
89 name = stream_name(pos.stream);
91 fprintf(stderr, "%s:%d:%d: %s%s\n",
92 name, pos.line, pos.pos, type, buffer);
95 static int max_warnings = 100;
97 void info(struct position pos, const char * fmt, ...)
99 va_list args;
101 if (!max_warnings)
102 return;
103 va_start(args, fmt);
104 do_warn("", pos, fmt, args);
105 va_end(args);
108 void warning(struct position pos, const char * fmt, ...)
110 va_list args;
112 if (!max_warnings)
113 return;
115 if (!--max_warnings)
116 fmt = "too many warnings";
118 va_start(args, fmt);
119 do_warn("warning: ", pos, fmt, args);
120 va_end(args);
123 void error(struct position pos, const char * fmt, ...)
125 static int errors = 0;
126 va_list args;
128 /* Shut up warnings after an error */
129 max_warnings = 0;
130 if (errors > 100) {
131 static int once = 0;
132 if (once)
133 return;
134 fmt = "too many errors";
135 once = 1;
138 va_start(args, fmt);
139 do_warn("error: ", pos, fmt, args);
140 va_end(args);
141 errors++;
144 void error_die(struct position pos, const char * fmt, ...)
146 va_list args;
147 va_start(args, fmt);
148 do_warn("error: ", pos, fmt, args);
149 va_end(args);
150 exit(1);
153 void die(const char *fmt, ...)
155 va_list args;
156 static char buffer[512];
158 va_start(args, fmt);
159 vsnprintf(buffer, sizeof(buffer), fmt, args);
160 va_end(args);
162 fprintf(stderr, "%s\n", buffer);
163 exit(1);
166 static unsigned int pre_buffer_size;
167 static char pre_buffer[8192];
169 int Wdefault_bitfield_sign = 0;
170 int Wbitwise = 0;
171 int Wtypesign = 0;
172 int Wcontext = 0;
173 int Wundefined_preprocessor = 0;
174 int Wptr_subtraction_blows = 0;
175 int Wcast_to_address_space = 0;
176 int Wtransparent_union = 1;
177 int preprocess_only;
178 char *include;
179 int include_fd = -1;
181 void add_pre_buffer(const char *fmt, ...)
183 va_list args;
184 unsigned int size;
186 va_start(args, fmt);
187 size = pre_buffer_size;
188 size += vsnprintf(pre_buffer + size,
189 sizeof(pre_buffer) - size,
190 fmt, args);
191 pre_buffer_size = size;
192 va_end(args);
195 static char **handle_switch_D(char *arg, char **next)
197 const char *name = arg + 1;
198 const char *value = "1";
199 for (;;) {
200 char c;
201 c = *++arg;
202 if (!c)
203 break;
204 if (isspace((unsigned char)c) || c == '=') {
205 *arg = '\0';
206 value = arg + 1;
207 break;
210 add_pre_buffer("#define %s %s\n", name, value);
211 return next;
214 static char **handle_switch_E(char *arg, char **next)
216 preprocess_only = 1;
217 return next;
220 static char **handle_switch_v(char *arg, char **next)
222 do {
223 verbose++;
224 } while (*++arg == 'v');
225 return next;
228 static char **handle_switch_I(char *arg, char **next)
230 char *path = arg+1;
232 switch (arg[1]) {
233 case '-':
234 add_pre_buffer("#split_include\n");
235 break;
237 case '\0': /* Plain "-I" */
238 path = *++next;
239 if (!path)
240 die("missing argument for -I option");
241 /* Fallthrough */
242 default:
243 add_pre_buffer("#add_include \"%s/\"\n", path);
245 return next;
248 static char **handle_switch_i(char *arg, char **next)
250 if (*next && !strcmp(arg, "include")) {
251 char *name = *++next;
252 int fd = open(name, O_RDONLY);
254 include_fd = fd;
255 include = name;
256 if (fd < 0)
257 perror(name);
259 else if (*next && !strcmp(arg, "isystem")) {
260 char *path = *++next;
261 if (!path)
262 die("missing argument for -isystem option");
263 add_pre_buffer("#add_isystem \"%s/\"\n", path);
265 return next;
268 static char **handle_switch_M(char *arg, char **next)
270 if (!strcmp(arg, "MF") || !strcmp(arg,"MQ") || !strcmp(arg,"MT")) {
271 if (!*next)
272 die("missing argument for -%s option", arg);
273 return next + 1;
275 return next;
278 static char **handle_switch_m(char *arg, char **next)
280 if (!strcmp(arg, "m64")) {
281 bits_in_long = 64;
282 max_int_alignment = 8;
283 bits_in_pointer = 64;
284 pointer_alignment = 8;
286 return next;
289 static char **handle_switch_o(char *arg, char **next)
291 if (!strcmp (arg, "o") && *next)
292 return next + 1; // "-o foo"
293 else
294 return next; // "-ofoo" or (bogus) terminal "-o"
297 static const struct warning {
298 const char *name;
299 int *flag;
300 } warnings[] = {
301 { "cast-to-as", &Wcast_to_address_space },
302 { "ptr-subtraction-blows", &Wptr_subtraction_blows },
303 { "default-bitfield-sign", &Wdefault_bitfield_sign },
304 { "undef", &Wundefined_preprocessor },
305 { "bitwise", &Wbitwise },
306 { "typesign", &Wtypesign },
307 { "context", &Wcontext },
308 { "transparent-union", &Wtransparent_union },
312 static char **handle_switch_W(char *arg, char **next)
314 int no = 0;
315 char *p = arg + 1;
316 unsigned i;
318 // Prefixes "no" and "no-" mean to turn warning off.
319 if (p[0] == 'n' && p[1] == 'o') {
320 p += 2;
321 if (p[0] == '-')
322 p++;
323 no = 1;
326 for (i = 0; i < sizeof(warnings) / sizeof(warnings[0]); i++) {
327 if (!strcmp(p,warnings[i].name)) {
328 *warnings[i].flag = !no;
329 return next;
333 // Unknown.
334 return next;
337 static char **handle_switch_U(char *arg, char **next)
339 const char *name = arg + 1;
340 add_pre_buffer ("#undef %s\n", name);
341 return next;
344 static char **handle_switch_O(char *arg, char **next)
346 int level = 1;
347 if (arg[1] >= '0' && arg[1] <= '9')
348 level = arg[1] - '0';
349 optimize = level;
350 optimize_size = arg[1] == 's';
351 return next;
354 static char **handle_switch_f(char *arg, char **next)
356 int flag = 1;
358 arg++;
359 if (!strncmp(arg, "no-", 3)) {
360 flag = 0;
361 arg += 3;
363 /* handle switch here.. */
364 return next;
367 static char **handle_nostdinc(char *arg, char **next)
369 add_pre_buffer("#nostdinc\n");
370 return next;
373 static char **handle_dirafter(char *arg, char **next)
375 char *path = *++next;
376 if (!path)
377 die("missing argument for -dirafter option");
378 add_pre_buffer("#add_dirafter \"%s/\"\n", path);
379 return next;
382 struct switches {
383 const char *name;
384 char **(*fn)(char *, char**);
387 char **handle_switch(char *arg, char **next)
389 static struct switches cmd[] = {
390 { "nostdinc", handle_nostdinc },
391 { "dirafter", handle_dirafter },
392 { NULL, NULL }
394 struct switches *s;
396 switch (*arg) {
397 case 'D': return handle_switch_D(arg, next);
398 case 'E': return handle_switch_E(arg, next);
399 case 'I': return handle_switch_I(arg, next);
400 case 'i': return handle_switch_i(arg, next);
401 case 'M': return handle_switch_M(arg, next);
402 case 'm': return handle_switch_m(arg, next);
403 case 'o': return handle_switch_o(arg, next);
404 case 'U': return handle_switch_U(arg, next);
405 case 'v': return handle_switch_v(arg, next);
406 case 'W': return handle_switch_W(arg, next);
407 case 'O': return handle_switch_O(arg, next);
408 case 'f': return handle_switch_f(arg, next);
409 default:
410 break;
413 s = cmd;
414 while (s->name) {
415 if (!strcmp(s->name, arg))
416 return s->fn(arg, next);
417 s++;
421 * Ignore unknown command line options:
422 * they're probably gcc switches
424 return next;
427 void declare_builtin_functions(void)
429 add_pre_buffer("extern void *__builtin_memcpy(void *, const void *, __SIZE_TYPE__);\n");
430 add_pre_buffer("extern void *__builtin_return_address(unsigned int);\n");
431 add_pre_buffer("extern void *__builtin_frame_address(unsigned int);\n");
432 add_pre_buffer("extern void *__builtin_memset(void *, int, __SIZE_TYPE__);\n");
433 add_pre_buffer("extern void __builtin_trap(void);\n");
434 add_pre_buffer("extern int __builtin_ffs(int);\n");
435 add_pre_buffer("extern void *__builtin_alloca(__SIZE_TYPE__);\n");
438 void create_builtin_stream(void)
440 add_pre_buffer("#define __GNUC__ %d\n", gcc_major);
441 add_pre_buffer("#define __GNUC_MINOR__ %d\n", gcc_minor);
442 add_pre_buffer("#define __GNUC_PATCHLEVEL__ %d\n", gcc_patchlevel);
443 add_pre_buffer("#define __extension__\n");
444 add_pre_buffer("#define __pragma__\n");
446 // gcc defines __SIZE_TYPE__ to be size_t. For linux/i86 and
447 // solaris/sparc that is really "unsigned int" and for linux/x86_64
448 // it is "long unsigned int". In either case we can probably
449 // get away with this. We need the #ifndef as cgcc will define
450 // the right __SIZE_TYPE__.
451 add_pre_buffer("#weak_define __SIZE_TYPE__ long unsigned int\n");
452 add_pre_buffer("#weak_define __STDC__ 1\n");
454 add_pre_buffer("#define __builtin_stdarg_start(a,b) ((a) = (__builtin_va_list)(&(b)))\n");
455 add_pre_buffer("#define __builtin_va_start(a,b) ((a) = (__builtin_va_list)(&(b)))\n");
456 add_pre_buffer("#define __builtin_va_arg(arg,type) ({ type __va_arg_ret = *(type *)(arg); arg += sizeof(type); __va_arg_ret; })\n");
457 add_pre_buffer("#define __builtin_va_alist (*(void *)0)\n");
458 add_pre_buffer("#define __builtin_va_arg_incr(x) ((x) + 1)\n");
459 add_pre_buffer("#define __builtin_va_end(arg)\n");
460 add_pre_buffer("#define __builtin_offsetof(type, name) ((__SIZE_TYPE__)&((type *)(0ul))->name)\n");
462 /* FIXME! We need to do these as special magic macros at expansion time! */
463 add_pre_buffer("#define __BASE_FILE__ \"base_file.c\"\n");
464 add_pre_buffer("#define __DATE__ \"??? ?? ????\"\n");
465 add_pre_buffer("#define __TIME__ \"??:??:??\"\n");
467 if (optimize)
468 add_pre_buffer("#define __OPTIMIZE__\n");
469 if (optimize_size)
470 add_pre_buffer("#define __OPTIMIZE_SIZE__\n");
473 static struct symbol_list *sparse_tokenstream(struct token *token)
475 // Pre-process the stream
476 token = preprocess(token);
478 if (preprocess_only) {
479 while (!eof_token(token)) {
480 int prec = 1;
481 struct token *next = token->next;
482 const char *separator = "";
483 if (next->pos.whitespace)
484 separator = " ";
485 if (next->pos.newline) {
486 separator = "\n\t\t\t\t\t";
487 prec = next->pos.pos;
488 if (prec > 4)
489 prec = 4;
491 printf("%s%.*s", show_token(token), prec, separator);
492 token = next;
494 putchar('\n');
496 return NULL;
499 // Parse the resulting C code
500 while (!eof_token(token))
501 token = external_declaration(token, &translation_unit_used_list);
502 return translation_unit_used_list;
505 static struct symbol_list *sparse_file(const char *filename)
507 int fd;
508 struct token *token;
510 if (strcmp (filename, "-") == 0) {
511 fd = 0;
512 } else {
513 fd = open(filename, O_RDONLY);
514 if (fd < 0)
515 die("No such file: %s", filename);
518 // Tokenize the input stream
519 token = tokenize(filename, fd, NULL, includepath);
520 close(fd);
522 return sparse_tokenstream(token);
526 * This handles the "-include" directive etc: we're in global
527 * scope, and all types/macros etc will affect all the following
528 * files.
530 * NOTE NOTE NOTE! "#undef" of anything in this stage will
531 * affect all subsequent files too, ie we can have non-local
532 * behaviour between files!
534 static void sparse_initial(void)
536 struct token *token;
538 // Prepend any "include" file to the stream.
539 // We're in global scope, it will affect all files!
540 token = NULL;
541 if (include_fd >= 0)
542 token = tokenize(include, include_fd, NULL, includepath);
544 // Prepend the initial built-in stream
545 token = tokenize_buffer(pre_buffer, pre_buffer_size, token);
546 sparse_tokenstream(token);
549 int sparse_initialize(int argc, char **argv)
551 char **args;
552 int files = 0;
554 // Initialize symbol stream first, so that we can add defines etc
555 init_symbols();
557 args = argv;
558 for (;;) {
559 char *arg = *++args;
560 if (!arg)
561 break;
563 if (arg[0] == '-' && arg[1]) {
564 args = handle_switch(arg+1, args);
565 continue;
569 * Hacky hacky hacky: we re-use the argument space
570 * to save the filenames.
572 argv[files++] = arg;
575 argv[files] = NULL;
576 if (files) {
577 // Initialize type system
578 init_ctype();
580 create_builtin_stream();
581 add_pre_buffer("#define __CHECKER__ 1\n");
582 if (!preprocess_only)
583 declare_builtin_functions();
585 sparse_initial();
588 * Protect the initial token allocations, since
589 * they need to survive all the others
591 protect_token_alloc();
593 return files;
596 struct symbol_list * sparse(char **argv)
598 struct symbol_list *res;
599 char *filename, *next;
601 /* Clear previous symbol list */
602 translation_unit_used_list = NULL;
604 filename = *argv;
605 if (!filename)
606 return NULL;
607 do {
608 next = argv[1];
609 *argv++ = next;
610 } while (next);
612 start_file_scope();
613 res = sparse_file(filename);
614 end_file_scope();
616 /* Drop the tokens for this file after parsing */
617 clear_token_alloc();
619 /* Evaluate the complete symbol list */
620 evaluate_symbol_list(res);
622 /* And return it */
623 return res;