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, all rights reserved.
20 #include "expression.h"
22 unsigned int pre_buffer_size
= 0;
23 unsigned char pre_buffer
[8192];
25 static void add_pre_buffer(const char *fmt
, ...)
31 size
= pre_buffer_size
;
32 size
+= vsnprintf(pre_buffer
+ size
,
33 sizeof(pre_buffer
) - size
,
35 pre_buffer_size
= size
;
39 static void handle_switch(char *arg
)
43 const char *name
= arg
+1;
44 const char *value
= "";
50 if (isspace(c
) || c
== '=') {
56 add_pre_buffer("#define %s %s\n", name
, value
);
61 add_pre_buffer("#add_include \"%s/\"\n", arg
+1);
64 fprintf(stderr
, "unknown switch '%s'\n", arg
);
68 void clean_up_statement(struct statement
*stmt
, void *_parent
, int flags
)
70 evaluate_statement(stmt
);
73 void clean_up_symbol(struct symbol
*sym
, void *_parent
, int flags
)
78 int main(int argc
, char **argv
)
81 char *filename
= NULL
;
83 struct symbol_list
*list
= NULL
;
85 // Initialize symbol stream first, so that we can add defines etc
88 // Stupid defines to make various headers happy
89 add_pre_buffer("#define __GNUC__ 2\n");
90 add_pre_buffer("#define __GNUC_MINOR__ 95\n");
92 for (i
= 1; i
< argc
; i
++) {
102 fd
= open(filename
, O_RDONLY
);
104 die("No such file: %s", argv
[1]);
106 // Tokenize the input stream
107 token
= tokenize(filename
, fd
, NULL
);
110 // Prepend the initial built-in stream
111 token
= tokenize_buffer(pre_buffer
, pre_buffer_size
, token
);
113 // Pre-process the stream
114 token
= preprocess(token
);
116 // Parse the resulting C code
117 translation_unit(token
, &list
);
119 // Do type evaluation and simplify
120 symbol_iterate(list
, clean_up_symbol
, NULL
);
123 // Show the end result.
124 show_symbol_list(list
, "\n\n");
128 // And show the allocation statistics
132 show_expression_alloc();
133 show_statement_alloc();