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 Transmeta Corp.
8 * Licensed under the Open Software License version 1.1
22 #include "expression.h"
24 static unsigned int pre_buffer_size
= 0;
25 static unsigned char pre_buffer
[8192];
27 static char *include
= NULL
;
28 static int include_fd
= -1;
30 static void add_pre_buffer(const char *fmt
, ...)
36 size
= pre_buffer_size
;
37 size
+= vsnprintf(pre_buffer
+ size
,
38 sizeof(pre_buffer
) - size
,
40 pre_buffer_size
= size
;
44 static char ** handle_switch(char *arg
, char **next
)
48 const char *name
= arg
+1;
49 const char *value
= "";
55 if (isspace(c
) || c
== '=') {
61 add_pre_buffer("#define %s %s\n", name
, value
);
66 add_pre_buffer("#add_include \"%s/\"\n", arg
+1);
69 if (*next
&& !strcmp(arg
, "include")) {
71 int fd
= open(name
, O_RDONLY
);
80 /* Ignore unknown command line options - they're probably gcc switches */
86 static void clean_up_symbol(struct symbol
*sym
, void *_parent
, int flags
)
88 check_duplicates(sym
);
92 int main(int argc
, char **argv
)
95 char *filename
= NULL
, **args
;
98 // Initialize symbol stream first, so that we can add defines etc
101 add_pre_buffer("#define __linux__ 1\n");
102 add_pre_buffer("#define __CHECKER__ 1\n");
103 add_pre_buffer("#define cond_syscall(x)\n");
104 add_pre_buffer("#define __GNUC__ 2\n");
105 add_pre_buffer("#define __GNUC_MINOR__ 95\n");
106 add_pre_buffer("#define __func__ \"function\"\n");
107 add_pre_buffer("#define __extension__\n");
108 add_pre_buffer("extern void *__builtin_memcpy(void *, const void *, unsigned long);\n");
109 add_pre_buffer("extern void * __builtin_return_address(int);\n");
110 add_pre_buffer("#define __builtin_stdarg_start(a,b) ((a) = (__builtin_va_list)(&(b)))\n");
111 add_pre_buffer("#define __builtin_va_arg(arg,type) ((type)0)\n");
112 add_pre_buffer("#define __builtin_va_end(arg)\n");
120 args
= handle_switch(arg
+1, args
);
127 fd
= open(filename
, O_RDONLY
);
129 die("No such file: %s", argv
[1]);
131 // Tokenize the input stream
132 token
= tokenize(filename
, fd
, NULL
);
135 // Prepend any "include" file to the stream.
137 token
= tokenize(include
, include_fd
, token
);
139 // Prepend the initial built-in stream
140 token
= tokenize_buffer(pre_buffer
, pre_buffer_size
, token
);
142 // Pre-process the stream
143 token
= preprocess(token
);
145 // Parse the resulting C code
146 translation_unit(token
, &used_list
);
148 // Do type evaluation and simplify
149 symbol_iterate(used_list
, clean_up_symbol
, NULL
);