[PATCH] fix of compound literals on inlining
[smatch.git] / lib.c
blob8082011e02baa9f235486f872685282922e89604
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 Wone_bit_signed_bitfield = 1;
172 int Wbitwise = 0;
173 int Wtypesign = 0;
174 int Wcontext = 0;
175 int Wundefined_preprocessor = 0;
176 int Wptr_subtraction_blows = 0;
177 int Wcast_to_address_space = 0;
178 int Wdecl = 0;
179 int Wtransparent_union = 1;
180 int preprocess_only;
181 char *include;
182 int include_fd = -1;
185 void add_pre_buffer(const char *fmt, ...)
187 va_list args;
188 unsigned int size;
190 va_start(args, fmt);
191 size = pre_buffer_size;
192 size += vsnprintf(pre_buffer + size,
193 sizeof(pre_buffer) - size,
194 fmt, args);
195 pre_buffer_size = size;
196 va_end(args);
199 static char **handle_switch_D(char *arg, char **next)
201 const char *name = arg + 1;
202 const char *value = "1";
203 for (;;) {
204 char c;
205 c = *++arg;
206 if (!c)
207 break;
208 if (isspace((unsigned char)c) || c == '=') {
209 *arg = '\0';
210 value = arg + 1;
211 break;
214 add_pre_buffer("#define %s %s\n", name, value);
215 return next;
218 static char **handle_switch_E(char *arg, char **next)
220 preprocess_only = 1;
221 return next;
224 static char **handle_switch_v(char *arg, char **next)
226 do {
227 verbose++;
228 } while (*++arg == 'v');
229 return next;
232 static char **handle_switch_I(char *arg, char **next)
234 char *path = arg+1;
236 switch (arg[1]) {
237 case '-':
238 add_pre_buffer("#split_include\n");
239 break;
241 case '\0': /* Plain "-I" */
242 path = *++next;
243 if (!path)
244 die("missing argument for -I option");
245 /* Fallthrough */
246 default:
247 add_pre_buffer("#add_include \"%s/\"\n", path);
249 return next;
252 static char **handle_switch_i(char *arg, char **next)
254 if (*next && !strcmp(arg, "include")) {
255 char *name = *++next;
256 int fd = open(name, O_RDONLY);
258 include_fd = fd;
259 include = name;
260 if (fd < 0)
261 perror(name);
263 if (*next && !strcmp(arg, "imacros")) {
264 char *name = *++next;
265 int fd = open(name, O_RDONLY);
267 include_fd = fd;
268 include = name;
269 if (fd < 0)
270 perror(name);
272 else if (*next && !strcmp(arg, "isystem")) {
273 char *path = *++next;
274 if (!path)
275 die("missing argument for -isystem option");
276 add_pre_buffer("#add_isystem \"%s/\"\n", path);
278 return next;
281 static char **handle_switch_M(char *arg, char **next)
283 if (!strcmp(arg, "MF") || !strcmp(arg,"MQ") || !strcmp(arg,"MT")) {
284 if (!*next)
285 die("missing argument for -%s option", arg);
286 return next + 1;
288 return next;
291 static char **handle_switch_m(char *arg, char **next)
293 if (!strcmp(arg, "m64")) {
294 bits_in_long = 64;
295 max_int_alignment = 8;
296 bits_in_pointer = 64;
297 pointer_alignment = 8;
299 return next;
302 static char **handle_switch_o(char *arg, char **next)
304 if (!strcmp (arg, "o") && *next)
305 return next + 1; // "-o foo"
306 else
307 return next; // "-ofoo" or (bogus) terminal "-o"
310 static const struct warning {
311 const char *name;
312 int *flag;
313 } warnings[] = {
314 { "cast-to-as", &Wcast_to_address_space },
315 { "decl", &Wdecl },
316 { "one-bit-signed-bitfield", &Wone_bit_signed_bitfield },
317 { "ptr-subtraction-blows", &Wptr_subtraction_blows },
318 { "default-bitfield-sign", &Wdefault_bitfield_sign },
319 { "undef", &Wundefined_preprocessor },
320 { "bitwise", &Wbitwise },
321 { "typesign", &Wtypesign },
322 { "context", &Wcontext },
323 { "transparent-union", &Wtransparent_union },
327 static char **handle_switch_W(char *arg, char **next)
329 int no = 0;
330 char *p = arg + 1;
331 unsigned i;
333 // Prefixes "no" and "no-" mean to turn warning off.
334 if (p[0] == 'n' && p[1] == 'o') {
335 p += 2;
336 if (p[0] == '-')
337 p++;
338 no = 1;
341 for (i = 0; i < sizeof(warnings) / sizeof(warnings[0]); i++) {
342 if (!strcmp(p,warnings[i].name)) {
343 *warnings[i].flag = !no;
344 return next;
348 // Unknown.
349 return next;
352 static char **handle_switch_U(char *arg, char **next)
354 const char *name = arg + 1;
355 add_pre_buffer ("#undef %s\n", name);
356 return next;
359 static char **handle_switch_O(char *arg, char **next)
361 int level = 1;
362 if (arg[1] >= '0' && arg[1] <= '9')
363 level = arg[1] - '0';
364 optimize = level;
365 optimize_size = arg[1] == 's';
366 return next;
369 static char **handle_switch_f(char *arg, char **next)
371 int flag = 1;
373 arg++;
374 if (!strncmp(arg, "no-", 3)) {
375 flag = 0;
376 arg += 3;
378 /* handle switch here.. */
379 return next;
382 static char **handle_switch_G(char *arg, char **next)
384 if (!strcmp (arg, "G") && *next)
385 return next + 1; // "-G 0"
386 else
387 return next; // "-G0" or (bogus) terminal "-G"
390 static char **handle_nostdinc(char *arg, char **next)
392 add_pre_buffer("#nostdinc\n");
393 return next;
396 static char **handle_dirafter(char *arg, char **next)
398 char *path = *++next;
399 if (!path)
400 die("missing argument for -dirafter option");
401 add_pre_buffer("#add_dirafter \"%s/\"\n", path);
402 return next;
405 struct switches {
406 const char *name;
407 char **(*fn)(char *, char**);
410 char **handle_switch(char *arg, char **next)
412 static struct switches cmd[] = {
413 { "nostdinc", handle_nostdinc },
414 { "dirafter", handle_dirafter },
415 { NULL, NULL }
417 struct switches *s;
419 switch (*arg) {
420 case 'D': return handle_switch_D(arg, next);
421 case 'E': return handle_switch_E(arg, next);
422 case 'I': return handle_switch_I(arg, next);
423 case 'i': return handle_switch_i(arg, next);
424 case 'M': return handle_switch_M(arg, next);
425 case 'm': return handle_switch_m(arg, next);
426 case 'o': return handle_switch_o(arg, next);
427 case 'U': return handle_switch_U(arg, next);
428 case 'v': return handle_switch_v(arg, next);
429 case 'W': return handle_switch_W(arg, next);
430 case 'O': return handle_switch_O(arg, next);
431 case 'f': return handle_switch_f(arg, next);
432 case 'G': return handle_switch_G(arg, next);
433 default:
434 break;
437 s = cmd;
438 while (s->name) {
439 if (!strcmp(s->name, arg))
440 return s->fn(arg, next);
441 s++;
445 * Ignore unknown command line options:
446 * they're probably gcc switches
448 return next;
451 void declare_builtin_functions(void)
453 /* Gaah. gcc knows tons of builtin <string.h> functions */
454 add_pre_buffer("extern void *__builtin_memcpy(void *, const void *, __SIZE_TYPE__);\n");
455 add_pre_buffer("extern void *__builtin_memset(void *, int, __SIZE_TYPE__);\n");
456 add_pre_buffer("extern int __builtin_memcmp(const void *, const void *, __SIZE_TYPE__);\n");
457 add_pre_buffer("extern int __builtin_strcmp(const char *, const char *);\n");
458 add_pre_buffer("extern char *__builtin_strchr(const char *, int);\n");
459 add_pre_buffer("extern char *__builtin_strcpy(char *, const char *);\n");
460 add_pre_buffer("extern char *__builtin_strncpy(char *, const char *, __SIZE_TYPE__);\n");
461 add_pre_buffer("extern __SIZE_TYPE__ __builtin_strspn(const char *, const char *);\n");
462 add_pre_buffer("extern __SIZE_TYPE__ __builtin_strcspn(const char *, const char *);\n");
464 /* And some random ones.. */
465 add_pre_buffer("extern void *__builtin_return_address(unsigned int);\n");
466 add_pre_buffer("extern void *__builtin_frame_address(unsigned int);\n");
467 add_pre_buffer("extern void __builtin_trap(void);\n");
468 add_pre_buffer("extern int __builtin_ffs(int);\n");
469 add_pre_buffer("extern void *__builtin_alloca(__SIZE_TYPE__);\n");
472 void create_builtin_stream(void)
474 add_pre_buffer("#define __GNUC__ %d\n", gcc_major);
475 add_pre_buffer("#define __GNUC_MINOR__ %d\n", gcc_minor);
476 add_pre_buffer("#define __GNUC_PATCHLEVEL__ %d\n", gcc_patchlevel);
477 add_pre_buffer("#define __extension__\n");
478 add_pre_buffer("#define __pragma__\n");
480 // gcc defines __SIZE_TYPE__ to be size_t. For linux/i86 and
481 // solaris/sparc that is really "unsigned int" and for linux/x86_64
482 // it is "long unsigned int". In either case we can probably
483 // get away with this. We need the #ifndef as cgcc will define
484 // the right __SIZE_TYPE__.
485 add_pre_buffer("#weak_define __SIZE_TYPE__ long unsigned int\n");
486 add_pre_buffer("#weak_define __STDC__ 1\n");
488 add_pre_buffer("#define __builtin_stdarg_start(a,b) ((a) = (__builtin_va_list)(&(b)))\n");
489 add_pre_buffer("#define __builtin_va_start(a,b) ((a) = (__builtin_va_list)(&(b)))\n");
490 add_pre_buffer("#define __builtin_va_arg(arg,type) ({ type __va_arg_ret = *(type *)(arg); arg += sizeof(type); __va_arg_ret; })\n");
491 add_pre_buffer("#define __builtin_va_alist (*(void *)0)\n");
492 add_pre_buffer("#define __builtin_va_arg_incr(x) ((x) + 1)\n");
493 add_pre_buffer("#define __builtin_va_end(arg)\n");
494 add_pre_buffer("#define __builtin_offsetof(type, name) ((__SIZE_TYPE__)&((type *)(0ul))->name)\n");
496 /* FIXME! We need to do these as special magic macros at expansion time! */
497 add_pre_buffer("#define __BASE_FILE__ \"base_file.c\"\n");
498 add_pre_buffer("#define __DATE__ \"??? ?? ????\"\n");
499 add_pre_buffer("#define __TIME__ \"??:??:??\"\n");
501 if (optimize)
502 add_pre_buffer("#define __OPTIMIZE__ 1\n");
503 if (optimize_size)
504 add_pre_buffer("#define __OPTIMIZE_SIZE__ 1\n");
507 static struct symbol_list *sparse_tokenstream(struct token *token)
509 // Pre-process the stream
510 token = preprocess(token);
512 if (preprocess_only) {
513 while (!eof_token(token)) {
514 int prec = 1;
515 struct token *next = token->next;
516 const char *separator = "";
517 if (next->pos.whitespace)
518 separator = " ";
519 if (next->pos.newline) {
520 separator = "\n\t\t\t\t\t";
521 prec = next->pos.pos;
522 if (prec > 4)
523 prec = 4;
525 printf("%s%.*s", show_token(token), prec, separator);
526 token = next;
528 putchar('\n');
530 return NULL;
533 // Parse the resulting C code
534 while (!eof_token(token))
535 token = external_declaration(token, &translation_unit_used_list);
536 return translation_unit_used_list;
539 static struct symbol_list *sparse_file(const char *filename)
541 int fd;
542 struct token *token;
544 if (strcmp (filename, "-") == 0) {
545 fd = 0;
546 } else {
547 fd = open(filename, O_RDONLY);
548 if (fd < 0)
549 die("No such file: %s", filename);
552 // Tokenize the input stream
553 token = tokenize(filename, fd, NULL, includepath);
554 close(fd);
556 return sparse_tokenstream(token);
560 * This handles the "-include" directive etc: we're in global
561 * scope, and all types/macros etc will affect all the following
562 * files.
564 * NOTE NOTE NOTE! "#undef" of anything in this stage will
565 * affect all subsequent files too, ie we can have non-local
566 * behaviour between files!
568 static struct symbol_list *sparse_initial(void)
570 struct token *token;
572 // Prepend any "include" file to the stream.
573 // We're in global scope, it will affect all files!
574 token = NULL;
575 if (include_fd >= 0)
576 token = tokenize(include, include_fd, NULL, includepath);
578 // Prepend the initial built-in stream
579 token = tokenize_buffer(pre_buffer, pre_buffer_size, token);
580 return sparse_tokenstream(token);
583 struct symbol_list *sparse_initialize(int argc, char **argv)
585 char **args;
586 int files = 0;
587 struct symbol_list *list;
589 // Initialize symbol stream first, so that we can add defines etc
590 init_symbols();
592 args = argv;
593 for (;;) {
594 char *arg = *++args;
595 if (!arg)
596 break;
598 if (arg[0] == '-' && arg[1]) {
599 args = handle_switch(arg+1, args);
600 continue;
604 * Hacky hacky hacky: we re-use the argument space
605 * to save the filenames.
607 argv[files++] = arg;
610 list = NULL;
611 argv[files] = NULL;
612 if (files) {
613 // Initialize type system
614 init_ctype();
616 create_builtin_stream();
617 add_pre_buffer("#define __CHECKER__ 1\n");
618 if (!preprocess_only)
619 declare_builtin_functions();
621 list = sparse_initial();
624 * Protect the initial token allocations, since
625 * they need to survive all the others
627 protect_token_alloc();
629 return list;
632 struct symbol_list * sparse(char **argv)
634 struct symbol_list *res;
635 char *filename, *next;
637 /* Clear previous symbol list */
638 translation_unit_used_list = NULL;
640 filename = *argv;
641 if (!filename)
642 return NULL;
643 do {
644 next = argv[1];
645 *argv++ = next;
646 } while (next);
648 start_file_scope();
649 res = sparse_file(filename);
650 end_file_scope();
652 /* Drop the tokens for this file after parsing */
653 clear_token_alloc();
655 /* Evaluate the complete symbol list */
656 evaluate_symbol_list(res);
658 /* And return it */
659 return res;