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.
8 * Licensed under the Open Software License version 1.1
22 #include "expression.h"
24 static unsigned int pre_buffer_size
= 0;
25 static unsigned char pre_buffer
[8192];
27 static void add_pre_buffer(const char *fmt
, ...)
33 size
= pre_buffer_size
;
34 size
+= vsnprintf(pre_buffer
+ size
,
35 sizeof(pre_buffer
) - size
,
37 pre_buffer_size
= size
;
41 static void handle_switch(char *arg
)
45 const char *name
= arg
+1;
46 const char *value
= "";
52 if (isspace(c
) || c
== '=') {
58 add_pre_buffer("#define %s %s\n", name
, value
);
63 add_pre_buffer("#add_include \"%s/\"\n", arg
+1);
66 fprintf(stderr
, "unknown switch '%s'\n", arg
);
70 static void clean_up_symbol(struct symbol
*sym
, void *_parent
, int flags
)
75 int main(int argc
, char **argv
)
78 char *filename
= NULL
;
81 // Initialize symbol stream first, so that we can add defines etc
84 // Stupid defines to make various headers happy
85 add_pre_buffer("#define __GNUC__ 2\n");
86 add_pre_buffer("#define __GNUC_MINOR__ 95\n");
88 for (i
= 1; i
< argc
; i
++) {
98 fd
= open(filename
, O_RDONLY
);
100 die("No such file: %s", argv
[1]);
102 // Tokenize the input stream
103 token
= tokenize(filename
, fd
, NULL
);
106 // Prepend the initial built-in stream
107 token
= tokenize_buffer(pre_buffer
, pre_buffer_size
, token
);
109 // Pre-process the stream
110 token
= preprocess(token
);
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
);
119 // Show the end result.
120 show_symbol_list(used_list
, "\n\n");
125 // And show the allocation statistics
129 show_expression_alloc();
130 show_statement_alloc();