2 * Example trivial client program that uses the sparse library
3 * to tokenize, pre-process and parse a C file, and prints out
6 * Copyright (C) 2003 Transmeta Corp.
9 * Licensed under the Open Software License version 1.1
23 #include "expression.h"
24 #include "linearize.h"
26 static void clean_up_symbol(struct symbol
*sym
, void *_parent
, int flags
)
29 check_duplicates(sym
);
31 linearize_symbol(sym
);
34 static void do_predefined(char *filename
)
36 add_pre_buffer("#define __BASE_FILE__ \"%s\"\n", filename
);
37 add_pre_buffer("#define __DATE__ \"??? ?? ????\"\n");
38 add_pre_buffer("#define __TIME__ \"??:??:??\"\n");
41 int main(int argc
, char **argv
)
44 char *filename
= NULL
, **args
;
47 // Initialize symbol stream first, so that we can add defines etc
50 create_builtin_stream();
51 add_pre_buffer("#define __CHECKER__ 1\n");
52 add_pre_buffer("extern void *__builtin_memcpy(void *, const void *, unsigned long);\n");
53 add_pre_buffer("extern void *__builtin_return_address(int);\n");
54 add_pre_buffer("extern void *__builtin_memset(void *, int, unsigned long);\n");
55 add_pre_buffer("extern void __builtin_trap(void);\n");
56 add_pre_buffer("extern int __builtin_ffs(unsigned long);\n"); /* XXX(cw) check this */
64 args
= handle_switch(arg
+1, args
);
71 die("no input files given");
73 // Initialize type system
76 do_predefined(filename
);
78 fd
= open(filename
, O_RDONLY
);
80 die("No such file: %s", filename
);
82 // Tokenize the input stream
83 token
= tokenize(filename
, fd
, NULL
);
86 // Prepend any "include" file to the stream.
88 token
= tokenize(include
, include_fd
, token
);
90 // Prepend the initial built-in stream
91 token
= tokenize_buffer(pre_buffer
, pre_buffer_size
, token
);
93 // Pre-process the stream
94 token
= preprocess(token
);
96 if (preprocess_only
) {
97 while (!eof_token(token
)) {
99 struct token
*next
= token
->next
;
100 char * separator
= "";
101 if (next
->pos
.whitespace
)
103 if (next
->pos
.newline
) {
104 separator
= "\n\t\t\t\t\t";
105 prec
= next
->pos
.pos
;
109 printf("%s%.*s", show_token(token
), prec
, separator
);
117 // Parse the resulting C code
118 translation_unit(token
, &used_list
);
120 // Do type evaluation and simplify
121 symbol_iterate(used_list
, clean_up_symbol
, NULL
);