Special evaluation rules for function argument types:
[smatch.git] / check.c
blobb647ffb60e13ca01f92d50d101832890e531c4e6
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 check_duplicates(sym);
29 expand_symbol(sym);
32 int main(int argc, char **argv)
34 int fd;
35 char *filename = NULL, **args;
36 struct token *token;
38 // Initialize symbol stream first, so that we can add defines etc
39 init_symbols();
41 create_builtin_stream();
42 add_pre_buffer("#define __CHECKER__ 1\n");
43 add_pre_buffer("extern void *__builtin_memcpy(void *, const void *, unsigned long);\n");
44 add_pre_buffer("extern void *__builtin_return_address(int);\n");
45 add_pre_buffer("extern void *__builtin_memset(void *, int, unsigned long);\n");
46 add_pre_buffer("extern void __builtin_trap(void);\n");
47 add_pre_buffer("extern int __builtin_ffs(unsigned long);\n"); /* XXX(cw) check this */
49 args = argv;
50 for (;;) {
51 char *arg = *++args;
52 if (!arg)
53 break;
54 if (arg[0] == '-') {
55 args = handle_switch(arg+1, args);
56 continue;
58 filename = arg;
61 // Initialize type system
62 init_ctype();
64 fd = open(filename, O_RDONLY);
65 if (fd < 0)
66 die("No such file: %s", filename);
68 // Tokenize the input stream
69 token = tokenize(filename, fd, NULL);
70 close(fd);
72 // Prepend any "include" file to the stream.
73 if (include_fd >= 0)
74 token = tokenize(include, include_fd, token);
76 // Prepend the initial built-in stream
77 token = tokenize_buffer(pre_buffer, pre_buffer_size, token);
79 // Pre-process the stream
80 token = preprocess(token);
82 if (preprocess_only) {
83 while (!eof_token(token)) {
84 int prec = 1;
85 struct token *next = token->next;
86 char * separator = "";
87 if (next->pos.whitespace)
88 separator = " ";
89 if (next->pos.newline) {
90 separator = "\n\t\t\t\t\t";
91 prec = next->pos.pos;
92 if (prec > 4)
93 prec = 4;
95 printf("%s%.*s", show_token(token), prec, separator);
96 token = next;
98 putchar('\n');
100 return 0;
103 // Parse the resulting C code
104 translation_unit(token, &used_list);
106 // Do type evaluation and simplify
107 symbol_iterate(used_list, clean_up_symbol, NULL);
108 return 0;