Make sparse sources themselves be sparse-clean.
[smatch.git] / check.c
blobf7f2be63edb8787914665da987fd2714a81cdb80
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");
45 add_pre_buffer("extern void *__builtin_memset(void *, int, unsigned long);\n");
47 args = argv;
48 for (;;) {
49 char *arg = *++args;
50 if (!arg)
51 break;
52 if (arg[0] == '-') {
53 args = handle_switch(arg+1, args);
54 continue;
56 filename = arg;
59 // Initialize type system
60 init_ctype();
62 fd = open(filename, O_RDONLY);
63 if (fd < 0)
64 die("No such file: %s", filename);
66 // Tokenize the input stream
67 token = tokenize(filename, fd, NULL);
68 close(fd);
70 // Prepend any "include" file to the stream.
71 if (include_fd >= 0)
72 token = tokenize(include, include_fd, token);
74 // Prepend the initial built-in stream
75 token = tokenize_buffer(pre_buffer, pre_buffer_size, token);
77 // Pre-process the stream
78 token = preprocess(token);
80 if (preprocess_only) {
81 while (!eof_token(token)) {
82 int prec = 1;
83 struct token *next = token->next;
84 char * separator = "";
85 if (next->pos.whitespace)
86 separator = " ";
87 if (next->pos.newline) {
88 separator = "\n\t\t\t\t\t";
89 prec = next->pos.pos;
90 if (prec > 4)
91 prec = 4;
93 printf("%s%.*s", show_token(token), prec, separator);
94 token = next;
96 putchar('\n');
98 return 0;
101 // Parse the resulting C code
102 translation_unit(token, &used_list);
104 // Do type evaluation and simplify
105 symbol_iterate(used_list, clean_up_symbol, NULL);
106 return 0;