Merge redhat.com:/garz/repo/sparse into redhat.com:/garz/repo/sparse.be
[smatch.git] / check.c
blobcdab4a2fe18709eb8ca9956434a976519a0b8eaa
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 check_duplicates(sym);
28 evaluate_symbol(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");
46 args = argv;
47 for (;;) {
48 char *arg = *++args;
49 if (!arg)
50 break;
51 if (arg[0] == '-') {
52 args = handle_switch(arg+1, args);
53 continue;
55 filename = arg;
59 fd = open(filename, O_RDONLY);
60 if (fd < 0)
61 die("No such file: %s", filename);
63 // Tokenize the input stream
64 token = tokenize(filename, fd, NULL);
65 close(fd);
67 // Prepend any "include" file to the stream.
68 if (include_fd >= 0)
69 token = tokenize(include, include_fd, token);
71 // Prepend the initial built-in stream
72 token = tokenize_buffer(pre_buffer, pre_buffer_size, token);
74 // Pre-process the stream
75 token = preprocess(token);
77 if (preprocess_only) {
78 while (!eof_token(token)) {
79 int prec = 1;
80 struct token *next = token->next;
81 char * separator = "";
82 if (next->pos.whitespace)
83 separator = " ";
84 if (next->pos.newline) {
85 separator = "\n\t\t\t\t\t";
86 prec = next->pos.pos;
87 if (prec > 4)
88 prec = 4;
90 printf("%s%.*s", show_token(token), prec, separator);
91 token = next;
93 putchar('\n');
95 return 0;
98 // Parse the resulting C code
99 translation_unit(token, &used_list);
101 // Do type evaluation and simplify
102 symbol_iterate(used_list, clean_up_symbol, NULL);
103 return 0;