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
36 #include <sys/types.h>
43 #include "expression.h"
46 #include "linearize.h"
53 static int prettify(const char **fnamep
)
55 const char *name
= *fnamep
;
56 int len
= strlen(name
);
58 if (len
> 2 && !memcmp(name
, "./", 2)) {
67 static const char *show_include_chain(int stream
, const char *base
)
69 static char buffer
[200];
72 while ((stream
= stream_prev(stream
)) >= 0) {
73 const char *p
= stream_name(stream
);
79 pretty_len
= prettify(&p
);
84 * At worst, we'll need " (through %s, ...)" in addition to the
87 if (pretty_len
+ len
+ 20 > sizeof(buffer
)) {
90 memcpy(buffer
+len
, ", ...", 5);
96 memcpy(buffer
, " (through ", 10);
103 memcpy(buffer
+len
, p
, pretty_len
);
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
)
125 fprintf(stderr
, "%s: note: in included file%s:\n",
127 show_include_chain(pos
.stream
, base_filename
));
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
)
139 vsprintf(buffer
, fmt
, args
);
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
, ...)
156 do_warn("", pos
, fmt
, args
);
160 static void do_error(struct position pos
, const char * fmt
, va_list args
)
162 static int errors
= 0;
167 /* Shut up warnings if position is bad_token.pos */
168 if (pos
.type
== TOKEN_BAD
)
170 /* Shut up warnings after an error */
171 has_error
|= ERROR_CURR_PHASE
;
172 if (errors
> fmax_errors
) {
177 fmt
= "too many errors";
181 do_warn("error: ", pos
, fmt
, args
);
185 void warning(struct position pos
, const char * fmt
, ...)
191 do_error(pos
, fmt
, args
);
196 if (!fmax_warnings
|| has_error
) {
201 if (!--fmax_warnings
) {
203 fmt
= "too many warnings";
207 do_warn("warning: ", pos
, fmt
, args
);
211 void sparse_error(struct position pos
, const char * fmt
, ...)
215 do_error(pos
, fmt
, args
);
219 void expression_error(struct expression
*expr
, const char *fmt
, ...)
223 do_error(expr
->pos
, fmt
, args
);
225 expr
->ctype
= &bad_ctype
;
229 void error_die(struct position pos
, const char * fmt
, ...)
233 do_warn("error: ", pos
, fmt
, args
);
239 void die(const char *fmt
, ...)
242 static char buffer
[512];
245 vsnprintf(buffer
, sizeof(buffer
), fmt
, args
);
248 fprintf(stderr
, "%s%s\n", diag_prefix
, buffer
);
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
, ...)
261 struct token
*begin
, *end
;
265 size
= vsnprintf(buffer
, sizeof(buffer
), fmt
, 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)
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
) {
310 dump_macro_definitions();
311 if (dump_macros_only
)
315 if (preprocess_only
) {
316 while (!eof_token(token
)) {
318 struct token
*next
= token
->next
;
319 const char *separator
= "";
320 if (next
->pos
.whitespace
)
322 if (next
->pos
.newline
) {
323 separator
= "\n\t\t\t\t\t";
324 prec
= next
->pos
.pos
;
328 printf("%s%.*s", show_token(token
), prec
, separator
);
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
)
347 if (strcmp(filename
, "-") == 0) {
350 fd
= open(filename
, O_RDONLY
);
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
);
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
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)
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
)
389 struct symbol_list
*list
;
391 base_filename
= "command-line";
393 // Initialize symbol stream first, so that we can add defines etc
397 // initialize the default target to the native 'machine'
398 target_config(MACH_NATIVE
);
406 if (arg
[0] == '-' && arg
[1]) {
407 args
= handle_switch(arg
+1, args
);
410 add_ptr_list(filelist
, arg
);
412 handle_switch_finalize();
414 // Redirect stdout if needed
415 if (dump_macro_defs
|| preprocess_only
)
417 if (do_output
&& outfile
&& strcmp(outfile
, "-")) {
418 if (!freopen(outfile
, "w", stdout
))
419 die("error: cannot open %s: %s", outfile
, strerror(errno
));
423 fdump_ir
= PASS_FINAL
;
427 // Initialize type system
432 create_builtin_stream();
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
);
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
;
468 res
= sparse_file(filename
);
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 */
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
);