Fix up function inlining:
[smatch.git] / test-parsing.c
blobf63be0737f52531b727096e1b4c3c6e4cab6c896
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.
8 * Licensed under the Open Software License version 1.1
9 */
10 #include <stdarg.h>
11 #include <stdlib.h>
12 #include <stdio.h>
13 #include <string.h>
14 #include <ctype.h>
15 #include <unistd.h>
16 #include <fcntl.h>
18 #include "lib.h"
19 #include "token.h"
20 #include "parse.h"
21 #include "symbol.h"
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, ...)
29 va_list args;
30 unsigned int size;
32 va_start(args, fmt);
33 size = pre_buffer_size;
34 size += vsnprintf(pre_buffer + size,
35 sizeof(pre_buffer) - size,
36 fmt, args);
37 pre_buffer_size = size;
38 va_end(args);
41 static void handle_switch(char *arg)
43 switch (*arg) {
44 case 'D': {
45 const char *name = arg+1;
46 const char *value = "";
47 for (;;) {
48 char c;
49 c = *++arg;
50 if (!c)
51 break;
52 if (isspace(c) || c == '=') {
53 *arg = '\0';
54 value = arg+1;
55 break;
58 add_pre_buffer("#define %s %s\n", name, value);
59 return;
62 case 'I':
63 add_pre_buffer("#add_include \"%s/\"\n", arg+1);
64 return;
65 default:
66 fprintf(stderr, "unknown switch '%s'\n", arg);
70 static void clean_up_symbol(struct symbol *sym, void *_parent, int flags)
72 evaluate_symbol(sym);
73 expand_symbol(sym);
76 int main(int argc, char **argv)
78 int i, fd;
79 char *filename = NULL;
80 struct token *token;
82 // Initialize symbol stream first, so that we can add defines etc
83 init_symbols();
85 // Stupid defines to make various headers happy
86 add_pre_buffer("#define __GNUC__ 2\n");
87 add_pre_buffer("#define __GNUC_MINOR__ 95\n");
89 for (i = 1; i < argc; i++) {
90 char *arg = argv[i];
91 if (arg[0] == '-') {
92 handle_switch(arg+1);
93 continue;
95 filename = arg;
99 fd = open(filename, O_RDONLY);
100 if (fd < 0)
101 die("No such file: %s", argv[1]);
103 // Tokenize the input stream
104 token = tokenize(filename, fd, NULL);
105 close(fd);
107 // Prepend the initial built-in stream
108 token = tokenize_buffer(pre_buffer, pre_buffer_size, token);
110 // Pre-process the stream
111 token = preprocess(token);
113 // Parse the resulting C code
114 translation_unit(token, &used_list);
116 // Do type evaluation and simplification
117 symbol_iterate(used_list, clean_up_symbol, NULL);
119 #if 1
120 // Show the end result.
121 show_symbol_list(used_list, "\n\n");
122 printf("\n\n");
123 #endif
125 #if 0
126 // And show the allocation statistics
127 show_ident_alloc();
128 show_token_alloc();
129 show_symbol_alloc();
130 show_expression_alloc();
131 show_statement_alloc();
132 show_string_alloc();
133 show_bytes_alloc();
134 #endif
135 return 0;