[PATCH] line-splicing fixes in sparse
[smatch.git] / check.c
blob4bddfb0d002e32f24ba927d50a411e1a304d41c6
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");
46 add_pre_buffer("extern void __builtin_trap(void);\n");
48 args = argv;
49 for (;;) {
50 char *arg = *++args;
51 if (!arg)
52 break;
53 if (arg[0] == '-') {
54 args = handle_switch(arg+1, args);
55 continue;
57 filename = arg;
60 // Initialize type system
61 init_ctype();
63 fd = open(filename, O_RDONLY);
64 if (fd < 0)
65 die("No such file: %s", filename);
67 // Tokenize the input stream
68 token = tokenize(filename, fd, NULL);
69 close(fd);
71 // Prepend any "include" file to the stream.
72 if (include_fd >= 0)
73 token = tokenize(include, include_fd, token);
75 // Prepend the initial built-in stream
76 token = tokenize_buffer(pre_buffer, pre_buffer_size, token);
78 // Pre-process the stream
79 token = preprocess(token);
81 if (preprocess_only) {
82 while (!eof_token(token)) {
83 int prec = 1;
84 struct token *next = token->next;
85 char * separator = "";
86 if (next->pos.whitespace)
87 separator = " ";
88 if (next->pos.newline) {
89 separator = "\n\t\t\t\t\t";
90 prec = next->pos.pos;
91 if (prec > 4)
92 prec = 4;
94 printf("%s%.*s", show_token(token), prec, separator);
95 token = next;
97 putchar('\n');
99 return 0;
102 // Parse the resulting C code
103 translation_unit(token, &used_list);
105 // Do type evaluation and simplify
106 symbol_iterate(used_list, clean_up_symbol, NULL);
107 return 0;