Make #include handling do the right thing for absolute paths.
[smatch.git] / check.c
blobad69dfd7361113848c5f0090c737fd397ccd4d37
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 static void do_predefined(char *filename)
34 add_pre_buffer("#define __BASE_FILE__ \"%s\"\n", filename);
35 add_pre_buffer("#define __DATE__ \"??? ?? ????\"\n");
36 add_pre_buffer("#define __TIME__ \"??:??:??\"\n");
39 int main(int argc, char **argv)
41 int fd;
42 char *filename = NULL, **args;
43 struct token *token;
45 // Initialize symbol stream first, so that we can add defines etc
46 init_symbols();
48 create_builtin_stream();
49 add_pre_buffer("#define __CHECKER__ 1\n");
50 add_pre_buffer("extern void *__builtin_memcpy(void *, const void *, unsigned long);\n");
51 add_pre_buffer("extern void *__builtin_return_address(int);\n");
52 add_pre_buffer("extern void *__builtin_memset(void *, int, unsigned long);\n");
53 add_pre_buffer("extern void __builtin_trap(void);\n");
54 add_pre_buffer("extern int __builtin_ffs(unsigned long);\n"); /* XXX(cw) check this */
56 args = argv;
57 for (;;) {
58 char *arg = *++args;
59 if (!arg)
60 break;
61 if (arg[0] == '-') {
62 args = handle_switch(arg+1, args);
63 continue;
65 filename = arg;
68 // Initialize type system
69 init_ctype();
71 do_predefined(filename);
73 fd = open(filename, O_RDONLY);
74 if (fd < 0)
75 die("No such file: %s", filename);
77 // Tokenize the input stream
78 token = tokenize(filename, fd, NULL);
79 close(fd);
81 // Prepend any "include" file to the stream.
82 if (include_fd >= 0)
83 token = tokenize(include, include_fd, token);
85 // Prepend the initial built-in stream
86 token = tokenize_buffer(pre_buffer, pre_buffer_size, token);
88 // Pre-process the stream
89 token = preprocess(token);
91 if (preprocess_only) {
92 while (!eof_token(token)) {
93 int prec = 1;
94 struct token *next = token->next;
95 char * separator = "";
96 if (next->pos.whitespace)
97 separator = " ";
98 if (next->pos.newline) {
99 separator = "\n\t\t\t\t\t";
100 prec = next->pos.pos;
101 if (prec > 4)
102 prec = 4;
104 printf("%s%.*s", show_token(token), prec, separator);
105 token = next;
107 putchar('\n');
109 return 0;
112 // Parse the resulting C code
113 translation_unit(token, &used_list);
115 // Do type evaluation and simplify
116 symbol_iterate(used_list, clean_up_symbol, NULL);
117 return 0;