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"
51 static int prettify(const char **fnamep
)
53 const char *name
= *fnamep
;
54 int len
= strlen(name
);
56 if (len
> 2 && !memcmp(name
, "./", 2)) {
65 static const char *show_include_chain(int stream
, const char *base
)
67 static char buffer
[200];
70 while ((stream
= stream_prev(stream
)) >= 0) {
71 const char *p
= stream_name(stream
);
77 pretty_len
= prettify(&p
);
82 * At worst, we'll need " (through %s, ...)" in addition to the
85 if (pretty_len
+ len
+ 20 > sizeof(buffer
)) {
88 memcpy(buffer
+len
, ", ...", 5);
94 memcpy(buffer
, " (through ", 10);
101 memcpy(buffer
+len
, p
, pretty_len
);
112 static const char *show_stream_name(struct position pos
)
114 const char *name
= stream_name(pos
.stream
);
115 static const char *last
;
117 if (name
== base_filename
)
123 fprintf(stderr
, "%s: note: in included file%s:\n",
125 show_include_chain(pos
.stream
, base_filename
));
129 static void do_warn(const char *type
, struct position pos
, const char * fmt
, va_list args
)
131 static char buffer
[512];
133 /* Shut up warnings if position is bad_token.pos */
134 if (pos
.type
== TOKEN_BAD
)
137 vsprintf(buffer
, fmt
, args
);
140 fprintf(stderr
, "%s:%d:%d: %s%s%s\n",
141 show_stream_name(pos
), pos
.line
, pos
.pos
,
142 diag_prefix
, type
, buffer
);
145 static int show_info
= 1;
147 void info(struct position pos
, const char * fmt
, ...)
154 do_warn("", pos
, fmt
, args
);
158 static void do_error(struct position pos
, const char * fmt
, va_list args
)
160 static int errors
= 0;
163 /* Shut up warnings if position is bad_token.pos */
164 if (pos
.type
== TOKEN_BAD
)
166 /* Shut up warnings after an error */
167 has_error
|= ERROR_CURR_PHASE
;
168 if (errors
> fmax_errors
) {
173 fmt
= "too many errors";
177 do_warn("error: ", pos
, fmt
, args
);
181 void warning(struct position pos
, const char * fmt
, ...)
187 do_error(pos
, fmt
, args
);
192 if (!fmax_warnings
|| has_error
) {
197 if (!--fmax_warnings
) {
199 fmt
= "too many warnings";
203 do_warn("warning: ", pos
, fmt
, args
);
207 void sparse_error(struct position pos
, const char * fmt
, ...)
211 do_error(pos
, fmt
, args
);
215 void expression_error(struct expression
*expr
, const char *fmt
, ...)
219 do_error(expr
->pos
, fmt
, args
);
221 expr
->ctype
= &bad_ctype
;
225 void error_die(struct position pos
, const char * fmt
, ...)
229 do_warn("error: ", pos
, fmt
, args
);
235 void die(const char *fmt
, ...)
238 static char buffer
[512];
241 vsnprintf(buffer
, sizeof(buffer
), fmt
, args
);
244 fprintf(stderr
, "%s%s\n", diag_prefix
, buffer
);
248 ////////////////////////////////////////////////////////////////////////////////
250 static struct token
*pre_buffer_begin
= NULL
;
251 static struct token
**pre_buffer_next
= &pre_buffer_begin
;
253 void add_pre_buffer(const char *fmt
, ...)
257 struct token
*begin
, *end
;
261 size
= vsnprintf(buffer
, sizeof(buffer
), fmt
, args
);
263 begin
= tokenize_buffer(buffer
, size
, &end
);
264 *pre_buffer_next
= begin
;
265 pre_buffer_next
= &end
->next
;
268 static void create_builtin_stream(void)
271 add_pre_buffer("#define _Pragma(x)\n");
273 /* add the multiarch include directories, if any */
274 if (multiarch_dir
&& *multiarch_dir
) {
275 add_pre_buffer("#add_system \"/usr/include/%s\"\n", multiarch_dir
);
276 add_pre_buffer("#add_system \"/usr/local/include/%s\"\n", multiarch_dir
);
279 /* We add compiler headers path here because we have to parse
280 * the arguments to get it, falling back to default. */
281 add_pre_buffer("#add_system \"%s/include\"\n", gcc_base_dir
);
282 add_pre_buffer("#add_system \"%s/include-fixed\"\n", gcc_base_dir
);
284 add_pre_buffer("#define __builtin_stdarg_start(a,b) ((a) = (__builtin_va_list)(&(b)))\n");
285 add_pre_buffer("#define __builtin_va_start(a,b) ((a) = (__builtin_va_list)(&(b)))\n");
286 add_pre_buffer("#define __builtin_ms_va_start(a,b) ((a) = (__builtin_ms_va_list)(&(b)))\n");
287 add_pre_buffer("#define __builtin_va_arg(arg,type) ({ type __va_arg_ret = *(type *)(arg); arg += sizeof(type); __va_arg_ret; })\n");
288 add_pre_buffer("#define __builtin_va_alist (*(void *)0)\n");
289 add_pre_buffer("#define __builtin_va_arg_incr(x) ((x) + 1)\n");
290 add_pre_buffer("#define __builtin_va_copy(dest, src) ({ dest = src; (void)0; })\n");
291 add_pre_buffer("#define __builtin_ms_va_copy(dest, src) ({ dest = src; (void)0; })\n");
292 add_pre_buffer("#define __builtin_va_end(arg)\n");
293 add_pre_buffer("#define __builtin_ms_va_end(arg)\n");
294 add_pre_buffer("#define __builtin_va_arg_pack()\n");
297 static struct symbol_list
*sparse_tokenstream(struct token
*token
)
299 int builtin
= token
&& !token
->pos
.stream
;
301 // Preprocess the stream
302 token
= preprocess(token
);
304 if (dump_macro_defs
|| dump_macros_only
) {
306 dump_macro_definitions();
307 if (dump_macros_only
)
311 if (preprocess_only
) {
312 while (!eof_token(token
)) {
314 struct token
*next
= token
->next
;
315 const char *separator
= "";
316 if (next
->pos
.whitespace
)
318 if (next
->pos
.newline
) {
319 separator
= "\n\t\t\t\t\t";
320 prec
= next
->pos
.pos
;
324 printf("%s%.*s", show_token(token
), prec
, separator
);
332 // Parse the resulting C code
333 while (!eof_token(token
))
334 token
= external_declaration(token
, &translation_unit_used_list
, NULL
);
335 return translation_unit_used_list
;
338 static struct symbol_list
*sparse_file(const char *filename
)
343 if (strcmp(filename
, "-") == 0) {
346 fd
= open(filename
, O_RDONLY
);
348 die("No such file: %s", filename
);
350 base_filename
= filename
;
352 // Tokenize the input stream
353 token
= tokenize(NULL
, filename
, fd
, NULL
, includepath
);
356 return sparse_tokenstream(token
);
360 * This handles the "-include" directive etc: we're in global
361 * scope, and all types/macros etc will affect all the following
364 * NOTE NOTE NOTE! "#undef" of anything in this stage will
365 * affect all subsequent files too, i.e. we can have non-local
366 * behaviour between files!
368 static struct symbol_list
*sparse_initial(void)
372 // Prepend any "include" file to the stream.
373 // We're in global scope, it will affect all files!
374 for (i
= 0; i
< cmdline_include_nr
; i
++)
375 add_pre_buffer("#argv_include \"%s\"\n", cmdline_include
[i
]);
377 return sparse_tokenstream(pre_buffer_begin
);
380 struct symbol_list
*sparse_initialize(int argc
, char **argv
, struct string_list
**filelist
)
383 struct symbol_list
*list
;
385 base_filename
= "command-line";
387 // Initialize symbol stream first, so that we can add defines etc
390 // initialize the default target to the native 'machine'
391 target_config(MACH_NATIVE
);
399 if (arg
[0] == '-' && arg
[1]) {
400 args
= handle_switch(arg
+1, args
);
403 add_ptr_list(filelist
, arg
);
405 handle_switch_finalize();
407 // Redirect stdout if needed
408 if (dump_macro_defs
|| preprocess_only
)
410 if (do_output
&& outfile
&& strcmp(outfile
, "-")) {
411 if (!freopen(outfile
, "w", stdout
))
412 die("error: cannot open %s: %s", outfile
, strerror(errno
));
416 fdump_ir
= PASS_FINAL
;
420 // Initialize type system
425 create_builtin_stream();
428 list
= sparse_initial();
431 * Protect the initial token allocations, since
432 * they need to survive all the others
434 protect_token_alloc();
437 * Evaluate the complete symbol list
438 * Note: This is not needed for normal cases.
439 * These symbols should only be predefined defines and
440 * declaratons which will be evaluated later, when needed.
441 * This is also the case when a file is directly included via
442 * '-include <file>' on the command line *AND* the file only
443 * contains defines, declarations and inline definitions.
444 * However, in the rare cases where the given file should
445 * contain some definitions, these will never be evaluated
446 * and thus won't be able to be linearized correctly.
447 * Hence the evaluate_symbol_list() here under.
449 evaluate_symbol_list(list
);
453 struct symbol_list
* sparse_keep_tokens(char *filename
)
455 struct symbol_list
*res
;
457 /* Clear previous symbol list */
458 translation_unit_used_list
= NULL
;
461 res
= sparse_file(filename
);
468 struct symbol_list
* __sparse(char *filename
)
470 struct symbol_list
*res
;
472 res
= sparse_keep_tokens(filename
);
474 /* Drop the tokens for this file after parsing */
481 struct symbol_list
* sparse(char *filename
)
483 struct symbol_list
*res
= __sparse(filename
);
485 if (has_error
& ERROR_CURR_PHASE
)
486 has_error
= ERROR_PREV_PHASE
;
487 /* Evaluate the complete symbol list */
488 evaluate_symbol_list(res
);