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 Linus Torvalds, all rights reserved.
20 #include "expression.h"
22 static void handle_switch(char *arg
)
26 static void clean_up_statement(struct statement
*stmt
, void *_parent
, int flags
);
27 static void clean_up_symbol(struct symbol
*sym
, void *_parent
, int flags
);
29 static void simplify_statement(struct statement
*stmt
, struct symbol
*fn
)
36 evaluate_expression(stmt
->expression
);
39 symbol_iterate(stmt
->syms
, clean_up_symbol
, fn
);
40 statement_iterate(stmt
->stmts
, clean_up_statement
, fn
);
43 evaluate_expression(stmt
->if_conditional
);
44 simplify_statement(stmt
->if_true
, fn
);
45 simplify_statement(stmt
->if_false
, fn
);
50 static void clean_up_statement(struct statement
*stmt
, void *_parent
, int flags
)
52 struct symbol
*parent
= _parent
;
53 simplify_statement(stmt
, parent
);
56 static void clean_up_symbol(struct symbol
*sym
, void *_parent
, int flags
)
58 struct symbol
*parent
= _parent
;
61 examine_symbol_type(sym
);
62 type
= sym
->ctype
.base_type
;
63 if (type
&& type
->type
== SYM_FN
) {
64 symbol_iterate(type
->arguments
, clean_up_symbol
, parent
);
66 simplify_statement(type
->stmt
, sym
);
70 int main(int argc
, char **argv
)
73 char *filename
= NULL
;
75 struct symbol_list
*list
= NULL
;
77 // Initialize symbol stream first, so that we can add defines etc
80 for (i
= 1; i
< argc
; i
++) {
90 fd
= open(filename
, O_RDONLY
);
92 die("No such file: %s", argv
[1]);
94 // Tokenize the input stream
95 token
= tokenize(filename
, fd
, NULL
);
98 // Pre-process the stream
99 token
= preprocess(token
);
101 // Parse the resulting C code
102 translation_unit(token
, &list
);
104 // Do type evaluation and simplify
105 symbol_iterate(list
, clean_up_symbol
, NULL
);
108 // Show the end result.
109 show_symbol_list(list
, "\n\n");
113 // And show the allocation statistics
117 show_expression_alloc();
118 show_statement_alloc();