1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
|
/*
* Example trivial client program that uses the sparse library
* to tokenize, pre-process and parse a C file, and prints out
* the results.
*
* Copyright (C) 2003 Transmeta Corp.
* 2003 Linus Torvalds
*
* Licensed under the Open Software License version 1.1
*/
#include <stdarg.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include <unistd.h>
#include <fcntl.h>
#include "lib.h"
#include "token.h"
#include "parse.h"
#include "symbol.h"
#include "expression.h"
static unsigned int pre_buffer_size = 0;
static unsigned char pre_buffer[8192];
static int preprocess_only;
static char *include = NULL;
static int include_fd = -1;
static void add_pre_buffer(const char *fmt, ...)
{
va_list args;
unsigned int size;
va_start(args, fmt);
size = pre_buffer_size;
size += vsnprintf(pre_buffer + size,
sizeof(pre_buffer) - size,
fmt, args);
pre_buffer_size = size;
va_end(args);
}
static char ** handle_switch(char *arg, char **next)
{
switch (*arg) {
case 'D': {
const char *name = arg+1;
const char *value = "";
for (;;) {
char c;
c = *++arg;
if (!c)
break;
if (isspace(c) || c == '=') {
*arg = '\0';
value = arg+1;
break;
}
}
add_pre_buffer("#define %s %s\n", name, value);
return next;
}
case 'E':
preprocess_only = 1;
return next;
case 'v':
verbose = 1;
return next;
case 'I':
add_pre_buffer("#add_include \"%s/\"\n", arg+1);
return next;
case 'i':
if (*next && !strcmp(arg, "include")) {
char *name = *++next;
int fd = open(name, O_RDONLY);
include_fd = fd;
include = name;
if (fd < 0)
perror(name);
return next;
}
/* Fallthrough */
default:
/* Ignore unknown command line options - they're probably gcc switches */
break;
}
return next;
}
static void clean_up_symbol(struct symbol *sym, void *_parent, int flags)
{
check_duplicates(sym);
evaluate_symbol(sym);
expand_symbol(sym);
}
int main(int argc, char **argv)
{
int fd;
char *filename = NULL, **args;
struct token *token;
// Initialize symbol stream first, so that we can add defines etc
init_symbols();
add_pre_buffer("#define __i386__ 1\n");
add_pre_buffer("#define __linux__ 1\n");
add_pre_buffer("#define __STDC__ 1\n");
add_pre_buffer("#define linux linux\n");
add_pre_buffer("#define __CHECKER__ 1\n");
add_pre_buffer("#define cond_syscall(x)\n");
add_pre_buffer("#define __GNUC__ 2\n");
add_pre_buffer("#define __GNUC_MINOR__ 95\n");
add_pre_buffer("#define __func__ \"function\"\n");
add_pre_buffer("#define __extension__\n");
add_pre_buffer("#define __pragma__\n");
add_pre_buffer("extern void *__builtin_memcpy(void *, const void *, unsigned long);\n");
add_pre_buffer("extern void * __builtin_return_address(int);\n");
add_pre_buffer("#define __builtin_stdarg_start(a,b) ((a) = (__builtin_va_list)(&(b)))\n");
add_pre_buffer("#define __builtin_va_arg(arg,type) ((type)0)\n");
add_pre_buffer("#define __builtin_va_end(arg)\n");
args = argv;
for (;;) {
char *arg = *++args;
if (!arg)
break;
if (arg[0] == '-') {
args = handle_switch(arg+1, args);
continue;
}
filename = arg;
}
fd = open(filename, O_RDONLY);
if (fd < 0)
die("No such file: %s", filename);
// Tokenize the input stream
token = tokenize(filename, fd, NULL);
close(fd);
// Prepend any "include" file to the stream.
if (include_fd >= 0)
token = tokenize(include, include_fd, token);
// Prepend the initial built-in stream
token = tokenize_buffer(pre_buffer, pre_buffer_size, token);
// Pre-process the stream
token = preprocess(token);
if (preprocess_only) {
while (!eof_token(token)) {
int prec = 1;
struct token *next = token->next;
char * separator = "";
if (next->pos.whitespace)
separator = " ";
if (next->pos.newline) {
separator = "\n\t\t\t\t\t";
prec = next->pos.pos;
if (prec > 4)
prec = 4;
}
printf("%s%.*s", show_token(token), prec, separator);
token = next;
}
putchar('\n');
return 0;
}
// Parse the resulting C code
translation_unit(token, &used_list);
// Do type evaluation and simplify
symbol_iterate(used_list, clean_up_symbol, NULL);
return 0;
}
|