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"
25 static void clean_up_symbol(struct symbol
*sym
, void *_parent
, int flags
)
28 check_duplicates(sym
);
32 static void do_predefined(char *filename
)
34 add_pre_buffer("#define __BASE_FILE__ \"%s\"\n", filename
);
35 add_pre_buffer("#define __DATE__ \"??? ?? ????\"\n");
36 add_pre_buffer("#define __TIME__ \"??:??:??\"\n");
39 int main(int argc
, char **argv
)
42 char *filename
= NULL
, **args
;
45 // Initialize symbol stream first, so that we can add defines etc
48 create_builtin_stream();
49 add_pre_buffer("#define __CHECKER__ 1\n");
50 add_pre_buffer("extern void *__builtin_memcpy(void *, const void *, unsigned long);\n");
51 add_pre_buffer("extern void *__builtin_return_address(int);\n");
52 add_pre_buffer("extern void *__builtin_memset(void *, int, unsigned long);\n");
53 add_pre_buffer("extern void __builtin_trap(void);\n");
54 add_pre_buffer("extern int __builtin_ffs(unsigned long);\n"); /* XXX(cw) check this */
62 args
= handle_switch(arg
+1, args
);
68 // Initialize type system
71 do_predefined(filename
);
73 fd
= open(filename
, O_RDONLY
);
75 die("No such file: %s", filename
);
77 // Tokenize the input stream
78 token
= tokenize(filename
, fd
, NULL
);
81 // Prepend any "include" file to the stream.
83 token
= tokenize(include
, include_fd
, token
);
85 // Prepend the initial built-in stream
86 token
= tokenize_buffer(pre_buffer
, pre_buffer_size
, token
);
88 // Pre-process the stream
89 token
= preprocess(token
);
91 if (preprocess_only
) {
92 while (!eof_token(token
)) {
94 struct token
*next
= token
->next
;
95 char * separator
= "";
96 if (next
->pos
.whitespace
)
98 if (next
->pos
.newline
) {
99 separator
= "\n\t\t\t\t\t";
100 prec
= next
->pos
.pos
;
104 printf("%s%.*s", show_token(token
), prec
, separator
);
112 // Parse the resulting C code
113 translation_unit(token
, &used_list
);
115 // Do type evaluation and simplify
116 symbol_iterate(used_list
, clean_up_symbol
, NULL
);