[PATCH] #line
[smatch.git] / check.c
blobc4286ba8ad0d0b3fc403502707316e33ac411ad3
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"
24 #include "linearize.h"
26 static void clean_up_symbol(struct symbol *sym, void *_parent, int flags)
28 evaluate_symbol(sym);
29 check_duplicates(sym);
30 expand_symbol(sym);
31 linearize_symbol(sym);
34 static void do_predefined(char *filename)
36 add_pre_buffer("#define __BASE_FILE__ \"%s\"\n", filename);
37 add_pre_buffer("#define __DATE__ \"??? ?? ????\"\n");
38 add_pre_buffer("#define __TIME__ \"??:??:??\"\n");
41 int main(int argc, char **argv)
43 int fd;
44 char *filename = NULL, **args;
45 struct token *token;
47 // Initialize symbol stream first, so that we can add defines etc
48 init_symbols();
50 create_builtin_stream();
51 add_pre_buffer("#define __CHECKER__ 1\n");
52 add_pre_buffer("extern void *__builtin_memcpy(void *, const void *, unsigned long);\n");
53 add_pre_buffer("extern void *__builtin_return_address(int);\n");
54 add_pre_buffer("extern void *__builtin_memset(void *, int, unsigned long);\n");
55 add_pre_buffer("extern void __builtin_trap(void);\n");
56 add_pre_buffer("extern int __builtin_ffs(unsigned long);\n"); /* XXX(cw) check this */
58 args = argv;
59 for (;;) {
60 char *arg = *++args;
61 if (!arg)
62 break;
63 if (arg[0] == '-') {
64 args = handle_switch(arg+1, args);
65 continue;
67 filename = arg;
70 if (!filename)
71 die("no input files given");
73 // Initialize type system
74 init_ctype();
76 do_predefined(filename);
78 fd = open(filename, O_RDONLY);
79 if (fd < 0)
80 die("No such file: %s", filename);
82 // Tokenize the input stream
83 token = tokenize(filename, fd, NULL);
84 close(fd);
86 // Prepend any "include" file to the stream.
87 if (include_fd >= 0)
88 token = tokenize(include, include_fd, token);
90 // Prepend the initial built-in stream
91 token = tokenize_buffer(pre_buffer, pre_buffer_size, token);
93 // Pre-process the stream
94 token = preprocess(token);
96 if (preprocess_only) {
97 while (!eof_token(token)) {
98 int prec = 1;
99 struct token *next = token->next;
100 char * separator = "";
101 if (next->pos.whitespace)
102 separator = " ";
103 if (next->pos.newline) {
104 separator = "\n\t\t\t\t\t";
105 prec = next->pos.pos;
106 if (prec > 4)
107 prec = 4;
109 printf("%s%.*s", show_token(token), prec, separator);
110 token = next;
112 putchar('\n');
114 return 0;
117 // Parse the resulting C code
118 translation_unit(token, &used_list);
120 // Do type evaluation and simplify
121 symbol_iterate(used_list, clean_up_symbol, NULL);
122 return 0;