kernel.return_fixes: add mipi_dsi_device_transfer(), timer_delete() and get_device()
[smatch.git] / lib.c
blobb6f41a7f351ca26a19f5a4c119c996f116bbd27f
1 /*
2 * 'sparse' library helper routines.
4 * Copyright (C) 2003 Transmeta Corp.
5 * 2003-2004 Linus Torvalds
7 * Permission is hereby granted, free of charge, to any person obtaining a copy
8 * of this software and associated documentation files (the "Software"), to deal
9 * in the Software without restriction, including without limitation the rights
10 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11 * copies of the Software, and to permit persons to whom the Software is
12 * furnished to do so, subject to the following conditions:
14 * The above copyright notice and this permission notice shall be included in
15 * all copies or substantial portions of the Software.
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
23 * THE SOFTWARE.
25 #include <ctype.h>
26 #include <errno.h>
27 #include <fcntl.h>
28 #include <stdarg.h>
29 #include <stddef.h>
30 #include <stdio.h>
31 #include <stdlib.h>
32 #include <string.h>
33 #include <unistd.h>
34 #include <assert.h>
36 #include <sys/types.h>
38 #include "lib.h"
39 #include "allocate.h"
40 #include "token.h"
41 #include "parse.h"
42 #include "symbol.h"
43 #include "expression.h"
44 #include "evaluate.h"
45 #include "scope.h"
46 #include "linearize.h"
47 #include "target.h"
48 #include "machine.h"
49 #include "bits.h"
51 int parse_error;
53 static int prettify(const char **fnamep)
55 const char *name = *fnamep;
56 int len = strlen(name);
58 if (len > 2 && !memcmp(name, "./", 2)) {
59 name += 2;
60 len -= 2;
63 *fnamep = name;
64 return len;
67 static const char *show_include_chain(int stream, const char *base)
69 static char buffer[200];
70 int len = 0;
72 while ((stream = stream_prev(stream)) >= 0) {
73 const char *p = stream_name(stream);
74 int pretty_len;
76 if (p == base)
77 break;
79 pretty_len = prettify(&p);
80 if (pretty_len <= 0)
81 break;
84 * At worst, we'll need " (through %s, ...)" in addition to the
85 * new filename
87 if (pretty_len + len + 20 > sizeof(buffer)) {
88 if (!len)
89 return "";
90 memcpy(buffer+len, ", ...", 5);
91 len += 5;
92 break;
95 if (!len) {
96 memcpy(buffer, " (through ", 10);
97 len = 10;
98 } else {
99 buffer[len++] = ',';
100 buffer[len++] = ' ';
103 memcpy(buffer+len, p, pretty_len);
104 len += pretty_len;
106 if (!len)
107 return "";
109 buffer[len] = ')';
110 buffer[len+1] = 0;
111 return buffer;
114 static const char *show_stream_name(struct position pos)
116 const char *name = stream_name(pos.stream);
117 static const char *last;
119 if (name == base_filename)
120 return name;
121 if (name == last)
122 return name;
123 last = name;
125 fprintf(stderr, "%s: note: in included file%s:\n",
126 base_filename,
127 show_include_chain(pos.stream, base_filename));
128 return name;
131 static void do_warn(const char *type, struct position pos, const char * fmt, va_list args)
133 static char buffer[512];
135 /* Shut up warnings if position is bad_token.pos */
136 if (pos.type == TOKEN_BAD)
137 return;
139 vsprintf(buffer, fmt, args);
141 fflush(stdout);
142 fprintf(stderr, "%s:%d:%d: %s%s%s\n",
143 show_stream_name(pos), pos.line, pos.pos,
144 diag_prefix, type, buffer);
147 static int show_info = 1;
149 void info(struct position pos, const char * fmt, ...)
151 va_list args;
153 if (!show_info)
154 return;
155 va_start(args, fmt);
156 do_warn("", pos, fmt, args);
157 va_end(args);
160 static void do_error(struct position pos, const char * fmt, va_list args)
162 static int errors = 0;
164 parse_error = 1;
165 die_if_error = 1;
166 show_info = 1;
167 /* Shut up warnings if position is bad_token.pos */
168 if (pos.type == TOKEN_BAD)
169 return;
170 /* Shut up warnings after an error */
171 has_error |= ERROR_CURR_PHASE;
172 if (errors > fmax_errors) {
173 static int once = 0;
174 show_info = 0;
175 if (once)
176 return;
177 fmt = "too many errors";
178 once = 1;
181 do_warn("error: ", pos, fmt, args);
182 errors++;
185 void warning(struct position pos, const char * fmt, ...)
187 va_list args;
189 if (Wsparse_error) {
190 va_start(args, fmt);
191 do_error(pos, fmt, args);
192 va_end(args);
193 return;
196 if (!fmax_warnings || has_error) {
197 show_info = 0;
198 return;
201 if (!--fmax_warnings) {
202 show_info = 0;
203 fmt = "too many warnings";
206 va_start(args, fmt);
207 do_warn("warning: ", pos, fmt, args);
208 va_end(args);
211 void sparse_error(struct position pos, const char * fmt, ...)
213 va_list args;
214 va_start(args, fmt);
215 do_error(pos, fmt, args);
216 va_end(args);
219 void expression_error(struct expression *expr, const char *fmt, ...)
221 va_list args;
222 va_start(args, fmt);
223 do_error(expr->pos, fmt, args);
224 va_end(args);
225 expr->ctype = &bad_ctype;
228 NORETURN_ATTR
229 void error_die(struct position pos, const char * fmt, ...)
231 va_list args;
232 va_start(args, fmt);
233 do_warn("error: ", pos, fmt, args);
234 va_end(args);
235 exit(1);
238 NORETURN_ATTR
239 void die(const char *fmt, ...)
241 va_list args;
242 static char buffer[512];
244 va_start(args, fmt);
245 vsnprintf(buffer, sizeof(buffer), fmt, args);
246 va_end(args);
248 fprintf(stderr, "%s%s\n", diag_prefix, buffer);
249 exit(1);
252 ////////////////////////////////////////////////////////////////////////////////
254 static struct token *pre_buffer_begin = NULL;
255 static struct token **pre_buffer_next = &pre_buffer_begin;
257 void add_pre_buffer(const char *fmt, ...)
259 va_list args;
260 unsigned int size;
261 struct token *begin, *end;
262 char buffer[4096];
264 va_start(args, fmt);
265 size = vsnprintf(buffer, sizeof(buffer), fmt, args);
266 va_end(args);
267 begin = tokenize_buffer(buffer, size, &end);
268 *pre_buffer_next = begin;
269 pre_buffer_next = &end->next;
272 static void create_builtin_stream(void)
274 // Temporary hack
275 add_pre_buffer("#define _Pragma(x)\n");
277 /* add the multiarch include directories, if any */
278 if (multiarch_dir && *multiarch_dir) {
279 add_pre_buffer("#add_system \"/usr/include/%s\"\n", multiarch_dir);
280 add_pre_buffer("#add_system \"/usr/local/include/%s\"\n", multiarch_dir);
283 /* We add compiler headers path here because we have to parse
284 * the arguments to get it, falling back to default. */
285 add_pre_buffer("#add_system \"%s/include\"\n", gcc_base_dir);
286 add_pre_buffer("#add_system \"%s/include-fixed\"\n", gcc_base_dir);
288 add_pre_buffer("#define __builtin_stdarg_start(a,b) ((a) = (__builtin_va_list)(&(b)))\n");
289 add_pre_buffer("#define __builtin_va_start(a,b) ((a) = (__builtin_va_list)(&(b)))\n");
290 add_pre_buffer("#define __builtin_ms_va_start(a,b) ((a) = (__builtin_ms_va_list)(&(b)))\n");
291 add_pre_buffer("#define __builtin_va_arg(arg,type) ({ type __va_arg_ret = *(type *)(arg); arg += sizeof(type); __va_arg_ret; })\n");
292 add_pre_buffer("#define __builtin_va_alist (*(void *)0)\n");
293 add_pre_buffer("#define __builtin_va_arg_incr(x) ((x) + 1)\n");
294 add_pre_buffer("#define __builtin_va_copy(dest, src) ({ dest = src; (void)0; })\n");
295 add_pre_buffer("#define __builtin_ms_va_copy(dest, src) ({ dest = src; (void)0; })\n");
296 add_pre_buffer("#define __builtin_va_end(arg)\n");
297 add_pre_buffer("#define __builtin_ms_va_end(arg)\n");
298 add_pre_buffer("#define __builtin_va_arg_pack()\n");
301 static struct symbol_list *sparse_tokenstream(struct token *token)
303 int builtin = token && !token->pos.stream;
305 // Preprocess the stream
306 token = preprocess(token);
308 if (dump_macro_defs || dump_macros_only) {
309 if (!builtin)
310 dump_macro_definitions();
311 if (dump_macros_only)
312 return NULL;
315 if (preprocess_only) {
316 while (!eof_token(token)) {
317 int prec = 1;
318 struct token *next = token->next;
319 const char *separator = "";
320 if (next->pos.whitespace)
321 separator = " ";
322 if (next->pos.newline) {
323 separator = "\n\t\t\t\t\t";
324 prec = next->pos.pos;
325 if (prec > 4)
326 prec = 4;
328 printf("%s%.*s", show_token(token), prec, separator);
329 token = next;
331 putchar('\n');
333 return NULL;
336 // Parse the resulting C code
337 while (!eof_token(token))
338 token = external_declaration(token, &translation_unit_used_list, NULL);
339 return translation_unit_used_list;
342 static struct symbol_list *sparse_file(const char *filename)
344 int fd;
345 struct token *token;
347 if (strcmp(filename, "-") == 0) {
348 fd = 0;
349 } else {
350 fd = open(filename, O_RDONLY);
351 if (fd < 0)
352 die("No such file: %s", filename);
354 base_filename = filename;
356 // Tokenize the input stream
357 token = tokenize(NULL, filename, fd, NULL, includepath);
358 store_all_tokens(token);
360 close(fd);
362 return sparse_tokenstream(token);
366 * This handles the "-include" directive etc: we're in global
367 * scope, and all types/macros etc will affect all the following
368 * files.
370 * NOTE NOTE NOTE! "#undef" of anything in this stage will
371 * affect all subsequent files too, i.e. we can have non-local
372 * behaviour between files!
374 static struct symbol_list *sparse_initial(void)
376 int i;
378 // Prepend any "include" file to the stream.
379 // We're in global scope, it will affect all files!
380 for (i = 0; i < cmdline_include_nr; i++)
381 add_pre_buffer("#argv_include \"%s\"\n", cmdline_include[i]);
383 return sparse_tokenstream(pre_buffer_begin);
386 struct symbol_list *sparse_initialize(int argc, char **argv, struct string_list **filelist)
388 char **args;
389 struct symbol_list *list;
391 base_filename = "command-line";
393 // Initialize symbol stream first, so that we can add defines etc
394 init_symbols();
395 init_include_path();
397 // initialize the default target to the native 'machine'
398 target_config(MACH_NATIVE);
400 args = argv;
401 for (;;) {
402 char *arg = *++args;
403 if (!arg)
404 break;
406 if (arg[0] == '-' && arg[1]) {
407 args = handle_switch(arg+1, args);
408 continue;
410 add_ptr_list(filelist, arg);
412 handle_switch_finalize();
414 // Redirect stdout if needed
415 if (dump_macro_defs || preprocess_only)
416 do_output = 1;
417 if (do_output && outfile && strcmp(outfile, "-")) {
418 if (!freopen(outfile, "w", stdout))
419 die("error: cannot open %s: %s", outfile, strerror(errno));
422 if (fdump_ir == 0)
423 fdump_ir = PASS_FINAL;
425 list = NULL;
426 if (filelist) {
427 // Initialize type system
428 target_init();
429 init_ctype();
431 predefined_macros();
432 create_builtin_stream();
433 init_builtins(0);
435 list = sparse_initial();
438 * Protect the initial token allocations, since
439 * they need to survive all the others
441 protect_token_alloc();
444 * Evaluate the complete symbol list
445 * Note: This is not needed for normal cases.
446 * These symbols should only be predefined defines and
447 * declaratons which will be evaluated later, when needed.
448 * This is also the case when a file is directly included via
449 * '-include <file>' on the command line *AND* the file only
450 * contains defines, declarations and inline definitions.
451 * However, in the rare cases where the given file should
452 * contain some definitions, these will never be evaluated
453 * and thus won't be able to be linearized correctly.
454 * Hence the evaluate_symbol_list() here under.
456 evaluate_symbol_list(list);
457 return list;
460 struct symbol_list * sparse_keep_tokens(char *filename)
462 struct symbol_list *res;
464 /* Clear previous symbol list */
465 translation_unit_used_list = NULL;
467 new_file_scope();
468 res = sparse_file(filename);
470 /* And return it */
471 return res;
475 struct symbol_list * __sparse(char *filename)
477 struct symbol_list *res;
479 res = sparse_keep_tokens(filename);
481 /* Drop the tokens for this file after parsing */
482 clear_token_alloc();
484 /* And return it */
485 return res;
488 struct symbol_list * sparse(char *filename)
490 struct symbol_list *res = __sparse(filename);
492 if (has_error & ERROR_CURR_PHASE)
493 has_error = ERROR_PREV_PHASE;
494 /* Evaluate the complete symbol list */
495 evaluate_symbol_list(res);
497 return res;