Fix indirect type parsing (functions, arrays, bitfields). Update
[smatch.git] / test-parsing.c
blobbc08397965115129a77b59bdcac070fd79b7fa4c
1 /*
2 * Example trivial client program that uses the sparse library
3 * to tokenize, pre-process and parse a C file, and prints out
4 * the results.
6 * Copyright (C) 2003 Linus Torvalds, all rights reserved.
7 */
8 #include <stdarg.h>
9 #include <stdlib.h>
10 #include <stdio.h>
11 #include <string.h>
12 #include <ctype.h>
13 #include <unistd.h>
14 #include <fcntl.h>
16 #include "lib.h"
17 #include "token.h"
18 #include "parse.h"
19 #include "symbol.h"
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)
31 if (!stmt)
32 return;
33 switch (stmt->type) {
34 case STMT_RETURN:
35 case STMT_EXPRESSION:
36 evaluate_expression(stmt->expression);
37 return;
38 case STMT_COMPOUND:
39 symbol_iterate(stmt->syms, clean_up_symbol, fn);
40 statement_iterate(stmt->stmts, clean_up_statement, fn);
41 return;
42 case STMT_IF:
43 evaluate_expression(stmt->if_conditional);
44 simplify_statement(stmt->if_true, fn);
45 simplify_statement(stmt->if_false, fn);
46 return;
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;
59 struct symbol *type;
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);
65 if (type->stmt)
66 simplify_statement(type->stmt, sym);
70 int main(int argc, char **argv)
72 int i, fd;
73 char *filename = NULL;
74 struct token *token;
75 struct symbol_list *list = NULL;
77 // Initialize symbol stream first, so that we can add defines etc
78 init_symbols();
80 for (i = 1; i < argc; i++) {
81 char *arg = argv[i];
82 if (arg[0] == '-') {
83 handle_switch(arg+1);
84 continue;
86 filename = arg;
90 fd = open(filename, O_RDONLY);
91 if (fd < 0)
92 die("No such file: %s", argv[1]);
94 // Tokenize the input stream
95 token = tokenize(filename, fd, NULL);
96 close(fd);
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);
107 #if 1
108 // Show the end result.
109 show_symbol_list(list, "\n\n");
110 printf("\n\n");
111 #endif
113 // And show the allocation statistics
114 show_ident_alloc();
115 show_token_alloc();
116 show_symbol_alloc();
117 show_expression_alloc();
118 show_statement_alloc();
119 show_string_alloc();
120 show_bytes_alloc();
121 return 0;