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
19 #include <sys/types.h>
26 #include "expression.h"
28 #include "linearize.h"
31 int verbose
, optimize
, preprocessing
;
35 # define __GNUC_MINOR__ 95
38 int gcc_major
= __GNUC__
;
39 int gcc_minor
= __GNUC_MINOR__
;
41 struct token
*skip_to(struct token
*token
, int op
)
43 while (!match_op(token
, op
) && !eof_token(token
))
48 struct token
*expect(struct token
*token
, int op
, const char *where
)
50 if (!match_op(token
, op
)) {
51 static struct token bad_token
;
52 if (token
!= &bad_token
) {
53 bad_token
.next
= token
;
54 warning(token
->pos
, "Expected %s %s", show_special(op
), where
);
55 warning(token
->pos
, "got %s", show_token(token
));
58 return skip_to(token
, op
);
64 unsigned int hexval(unsigned int c
)
72 retval
= c
- 'a' + 10;
75 retval
= c
- 'A' + 10;
81 static void do_warn(const char *type
, struct position pos
, const char * fmt
, va_list args
)
83 static char buffer
[512];
86 vsprintf(buffer
, fmt
, args
);
87 name
= stream_name(pos
.stream
);
89 fprintf(stderr
, "%s:%d:%d: %s%s\n",
90 name
, pos
.line
, pos
.pos
, type
, buffer
);
93 static int max_warnings
= 100;
95 void info(struct position pos
, const char * fmt
, ...)
102 do_warn("", pos
, fmt
, args
);
106 void warning(struct position pos
, const char * fmt
, ...)
114 fmt
= "too many warnings";
117 do_warn("warning: ", pos
, fmt
, args
);
121 void error(struct position pos
, const char * fmt
, ...)
123 static int errors
= 0;
126 /* Shut up warnings after an error */
132 fmt
= "too many errors";
137 do_warn("error: ", pos
, fmt
, args
);
142 void error_die(struct position pos
, const char * fmt
, ...)
146 do_warn("error: ", pos
, fmt
, args
);
151 void die(const char *fmt
, ...)
154 static char buffer
[512];
157 vsnprintf(buffer
, sizeof(buffer
), fmt
, args
);
160 fprintf(stderr
, "%s\n", buffer
);
164 static unsigned int pre_buffer_size
;
165 static char pre_buffer
[8192];
167 int Wdefault_bitfield_sign
= 0;
171 int Wundefined_preprocessor
= 0;
172 int Wptr_subtraction_blows
= 0;
173 int Wtransparent_union
= 1;
178 void add_pre_buffer(const char *fmt
, ...)
184 size
= pre_buffer_size
;
185 size
+= vsnprintf(pre_buffer
+ size
,
186 sizeof(pre_buffer
) - size
,
188 pre_buffer_size
= size
;
192 static char **handle_switch_D(char *arg
, char **next
)
194 const char *name
= arg
+ 1;
195 const char *value
= "1";
201 if (isspace((unsigned char)c
) || c
== '=') {
207 add_pre_buffer("#define %s %s\n", name
, value
);
211 static char **handle_switch_E(char *arg
, char **next
)
217 static char **handle_switch_v(char *arg
, char **next
)
221 } while (*++arg
== 'v');
225 static char **handle_switch_I(char *arg
, char **next
)
231 add_pre_buffer("#split_include\n");
234 case '\0': /* Plain "-I" */
237 die("missing argument for -I option");
240 add_pre_buffer("#add_include \"%s/\"\n", path
);
245 static char **handle_switch_i(char *arg
, char **next
)
247 if (*next
&& !strcmp(arg
, "include")) {
248 char *name
= *++next
;
249 int fd
= open(name
, O_RDONLY
);
256 else if (*next
&& !strcmp(arg
, "isystem")) {
257 char *path
= *++next
;
259 die("missing argument for -isystem option");
260 add_pre_buffer("#add_isystem \"%s/\"\n", path
);
265 static char **handle_switch_M(char *arg
, char **next
)
267 if (!strcmp(arg
, "MF") || !strcmp(arg
,"MQ") || !strcmp(arg
,"MT")) {
269 die("missing argument for -%s option", arg
);
275 static char **handle_switch_m(char *arg
, char **next
)
277 if (!strcmp(arg
, "m64")) {
279 max_int_alignment
= 8;
280 bits_in_pointer
= 64;
281 pointer_alignment
= 8;
286 static char **handle_switch_o(char *arg
, char **next
)
288 if (!strcmp (arg
, "o") && *next
)
289 return next
+ 1; // "-o foo"
291 return next
; // "-ofoo" or (bogus) terminal "-o"
294 static const struct warning
{
298 { "ptr-subtraction-blows", &Wptr_subtraction_blows
},
299 { "default-bitfield-sign", &Wdefault_bitfield_sign
},
300 { "undef", &Wundefined_preprocessor
},
301 { "bitwise", &Wbitwise
},
302 { "typesign", &Wtypesign
},
303 { "context", &Wcontext
},
304 { "transparent-union", &Wtransparent_union
},
308 static char **handle_switch_W(char *arg
, char **next
)
314 // Prefixes "no" and "no-" mean to turn warning off.
315 if (p
[0] == 'n' && p
[1] == 'o') {
322 for (i
= 0; i
< sizeof(warnings
) / sizeof(warnings
[0]); i
++) {
323 if (!strcmp(p
,warnings
[i
].name
)) {
324 *warnings
[i
].flag
= !no
;
333 static char **handle_switch_U(char *arg
, char **next
)
335 const char *name
= arg
+ 1;
336 add_pre_buffer ("#undef %s\n", name
);
340 static char **handle_switch_O(char *arg
, char **next
)
343 if (arg
[1] >= '0' && arg
[1] <= '9')
344 level
= arg
[1] - '0';
349 static char **handle_nostdinc(char *arg
, char **next
)
351 add_pre_buffer("#nostdinc\n");
355 static char **handle_dirafter(char *arg
, char **next
)
357 char *path
= *++next
;
359 die("missing argument for -dirafter option");
360 add_pre_buffer("#add_dirafter \"%s/\"\n", path
);
366 char **(*fn
)(char *, char**);
369 char **handle_switch(char *arg
, char **next
)
371 static struct switches cmd
[] = {
372 { "nostdinc", handle_nostdinc
},
373 { "dirafter", handle_dirafter
},
379 case 'D': return handle_switch_D(arg
, next
);
380 case 'E': return handle_switch_E(arg
, next
);
381 case 'I': return handle_switch_I(arg
, next
);
382 case 'i': return handle_switch_i(arg
, next
);
383 case 'M': return handle_switch_M(arg
, next
);
384 case 'm': return handle_switch_m(arg
, next
);
385 case 'o': return handle_switch_o(arg
, next
);
386 case 'U': return handle_switch_U(arg
, next
);
387 case 'v': return handle_switch_v(arg
, next
);
388 case 'W': return handle_switch_W(arg
, next
);
389 case 'O': return handle_switch_O(arg
, next
);
396 if (!strcmp(s
->name
, arg
))
397 return s
->fn(arg
, next
);
402 * Ignore unknown command line options:
403 * they're probably gcc switches
408 void declare_builtin_functions(void)
410 add_pre_buffer("extern void *__builtin_memcpy(void *, const void *, __SIZE_TYPE__);\n");
411 add_pre_buffer("extern void *__builtin_return_address(unsigned int);\n");
412 add_pre_buffer("extern void *__builtin_frame_address(unsigned int);\n");
413 add_pre_buffer("extern void *__builtin_memset(void *, int, __SIZE_TYPE__);\n");
414 add_pre_buffer("extern void __builtin_trap(void);\n");
415 add_pre_buffer("extern int __builtin_ffs(int);\n");
416 add_pre_buffer("extern void *__builtin_alloca(__SIZE_TYPE__);\n");
419 void create_builtin_stream(void)
421 add_pre_buffer("#define __GNUC__ %d\n", gcc_major
);
422 add_pre_buffer("#define __GNUC_MINOR__ %d\n", gcc_minor
);
423 add_pre_buffer("#define __extension__\n");
424 add_pre_buffer("#define __pragma__\n");
426 // gcc defines __SIZE_TYPE__ to be size_t. For linux/i86 and
427 // solaris/sparc that is really "unsigned int" and for linux/x86_64
428 // it is "long unsigned int". In either case we can probably
429 // get away with this. We need the #ifndef as cgcc will define
430 // the right __SIZE_TYPE__.
431 add_pre_buffer("#weak_define __SIZE_TYPE__ long unsigned int\n");
432 add_pre_buffer("#weak_define __STDC__ 1\n");
434 add_pre_buffer("#define __builtin_stdarg_start(a,b) ((a) = (__builtin_va_list)(&(b)))\n");
435 add_pre_buffer("#define __builtin_va_start(a,b) ((a) = (__builtin_va_list)(&(b)))\n");
436 add_pre_buffer("#define __builtin_va_arg(arg,type) ({ type __va_arg_ret = *(type *)(arg); arg += sizeof(type); __va_arg_ret; })\n");
437 add_pre_buffer("#define __builtin_va_alist (*(void *)0)\n");
438 add_pre_buffer("#define __builtin_va_arg_incr(x) ((x) + 1)\n");
439 add_pre_buffer("#define __builtin_va_end(arg)\n");
440 add_pre_buffer("#define __builtin_offsetof(type, name) ((__SIZE_TYPE__)&((type *)(0ul))->name)\n");
442 /* FIXME! We need to do these as special magic macros at expansion time! */
443 add_pre_buffer("#define __BASE_FILE__ \"base_file.c\"\n");
444 add_pre_buffer("#define __DATE__ \"??? ?? ????\"\n");
445 add_pre_buffer("#define __TIME__ \"??:??:??\"\n");
448 static struct symbol_list
*sparse_tokenstream(struct token
*token
)
450 // Pre-process the stream
451 token
= preprocess(token
);
453 if (preprocess_only
) {
454 while (!eof_token(token
)) {
456 struct token
*next
= token
->next
;
457 const char *separator
= "";
458 if (next
->pos
.whitespace
)
460 if (next
->pos
.newline
) {
461 separator
= "\n\t\t\t\t\t";
462 prec
= next
->pos
.pos
;
466 printf("%s%.*s", show_token(token
), prec
, separator
);
474 // Parse the resulting C code
475 while (!eof_token(token
))
476 token
= external_declaration(token
, &translation_unit_used_list
);
477 return translation_unit_used_list
;
480 static struct symbol_list
*sparse_file(const char *filename
)
485 if (strcmp (filename
, "-") == 0) {
488 fd
= open(filename
, O_RDONLY
);
490 die("No such file: %s", filename
);
493 // Tokenize the input stream
494 token
= tokenize(filename
, fd
, NULL
, includepath
);
497 return sparse_tokenstream(token
);
501 * This handles the "-include" directive etc: we're in global
502 * scope, and all types/macros etc will affect all the following
505 * NOTE NOTE NOTE! "#undef" of anything in this stage will
506 * affect all subsequent files too, ie we can have non-local
507 * behaviour between files!
509 static void sparse_initial(void)
513 // Prepend any "include" file to the stream.
514 // We're in global scope, it will affect all files!
517 token
= tokenize(include
, include_fd
, NULL
, includepath
);
519 // Prepend the initial built-in stream
520 token
= tokenize_buffer(pre_buffer
, pre_buffer_size
, token
);
521 sparse_tokenstream(token
);
524 int sparse_initialize(int argc
, char **argv
)
529 // Initialize symbol stream first, so that we can add defines etc
538 if (arg
[0] == '-' && arg
[1]) {
539 args
= handle_switch(arg
+1, args
);
544 * Hacky hacky hacky: we re-use the argument space
545 * to save the filenames.
552 // Initialize type system
555 create_builtin_stream();
556 add_pre_buffer("#define __CHECKER__ 1\n");
557 if (!preprocess_only
)
558 declare_builtin_functions();
563 * Protect the initial token allocations, since
564 * they need to survive all the others
566 protect_token_alloc();
571 struct symbol_list
* sparse(char **argv
)
573 struct symbol_list
*res
;
574 char *filename
, *next
;
576 /* Clear previous symbol list */
577 translation_unit_used_list
= NULL
;
588 res
= sparse_file(filename
);
591 /* Drop the tokens for this file after parsing */
594 /* Evaluate the complete symbol list */
595 evaluate_symbol_list(res
);