o split handle_switch, to make it more lib friendly
[smatch.git] / test-parsing.c
blobd777b013682e4af3a26c7d6b79da1041fef86ee7
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 Transmeta Corp.
7 * 2003 Linus Torvalds
9 * Licensed under the Open Software License version 1.1
11 #include <stdarg.h>
12 #include <stdlib.h>
13 #include <stdio.h>
14 #include <string.h>
15 #include <ctype.h>
16 #include <unistd.h>
17 #include <fcntl.h>
19 #include "lib.h"
20 #include "token.h"
21 #include "parse.h"
22 #include "symbol.h"
23 #include "expression.h"
25 static void clean_up_symbol(struct symbol *sym, void *_parent, int flags)
27 evaluate_symbol(sym);
28 expand_symbol(sym);
31 int main(int argc, char **argv)
33 int fd;
34 char *filename = NULL, **args;;
35 struct token *token;
37 // Initialize symbol stream first, so that we can add defines etc
38 init_symbols();
40 create_builtin_stream();
42 args = argv;
43 for (;;) {
44 char *arg = *++args;
45 if (!arg)
46 break;
47 if (arg[0] == '-') {
48 args = handle_switch(arg + 1, args);
49 continue;
51 filename = arg;
54 fd = open(filename, O_RDONLY);
55 if (fd < 0)
56 die("No such file: %s", argv[1]);
58 // Tokenize the input stream
59 token = tokenize(filename, fd, NULL);
60 close(fd);
62 // Prepend the initial built-in stream
63 token = tokenize_buffer(pre_buffer, pre_buffer_size, token);
65 // Pre-process the stream
66 token = preprocess(token);
68 // Parse the resulting C code
69 translation_unit(token, &used_list);
71 // Do type evaluation and simplification
72 symbol_iterate(used_list, clean_up_symbol, NULL);
74 #if 1
75 // Show the end result.
76 show_symbol_list(used_list, "\n\n");
77 printf("\n\n");
78 #endif
80 #if 0
81 // And show the allocation statistics
82 show_ident_alloc();
83 show_token_alloc();
84 show_symbol_alloc();
85 show_expression_alloc();
86 show_statement_alloc();
87 show_string_alloc();
88 show_bytes_alloc();
89 #endif
90 return 0;