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"
54 static void do_warn(const char *type
, struct position pos
, const char * fmt
, va_list args
)
56 static char buffer
[512];
59 /* Shut up warnings if position is bad_token.pos */
60 if (pos
.type
== TOKEN_BAD
)
63 vsprintf(buffer
, fmt
, args
);
64 name
= stream_name(pos
.stream
);
67 fprintf(stderr
, "%s:%d:%d: %s%s%s\n",
68 name
, pos
.line
, pos
.pos
, diag_prefix
, type
, buffer
);
71 static int show_info
= 1;
73 void info(struct position pos
, const char * fmt
, ...)
80 do_warn("", pos
, fmt
, args
);
84 static void do_error(struct position pos
, const char * fmt
, va_list args
)
86 static int errors
= 0;
91 /* Shut up warnings if position is bad_token.pos */
92 if (pos
.type
== TOKEN_BAD
)
94 /* Shut up warnings after an error */
95 has_error
|= ERROR_CURR_PHASE
;
101 fmt
= "too many errors";
105 do_warn("error: ", pos
, fmt
, args
);
109 void warning(struct position pos
, const char * fmt
, ...)
115 do_error(pos
, fmt
, args
);
120 if (!fmax_warnings
|| has_error
) {
125 if (!--fmax_warnings
) {
127 fmt
= "too many warnings";
131 do_warn("warning: ", pos
, fmt
, args
);
135 void sparse_error(struct position pos
, const char * fmt
, ...)
139 do_error(pos
, fmt
, args
);
143 void expression_error(struct expression
*expr
, const char *fmt
, ...)
147 do_error(expr
->pos
, fmt
, args
);
149 expr
->ctype
= &bad_ctype
;
153 void error_die(struct position pos
, const char * fmt
, ...)
157 do_warn("error: ", pos
, fmt
, args
);
163 void die(const char *fmt
, ...)
166 static char buffer
[512];
169 vsnprintf(buffer
, sizeof(buffer
), fmt
, args
);
172 fprintf(stderr
, "%s%s\n", diag_prefix
, buffer
);
176 ////////////////////////////////////////////////////////////////////////////////
178 static struct token
*pre_buffer_begin
= NULL
;
179 static struct token
**pre_buffer_next
= &pre_buffer_begin
;
181 void add_pre_buffer(const char *fmt
, ...)
185 struct token
*begin
, *end
;
189 size
= vsnprintf(buffer
, sizeof(buffer
), fmt
, args
);
191 begin
= tokenize_buffer(buffer
, size
, &end
);
192 *pre_buffer_next
= begin
;
193 pre_buffer_next
= &end
->next
;
196 static void create_builtin_stream(void)
199 add_pre_buffer("#define _Pragma(x)\n");
201 /* add the multiarch include directories, if any */
202 if (multiarch_dir
&& *multiarch_dir
) {
203 add_pre_buffer("#add_system \"/usr/include/%s\"\n", multiarch_dir
);
204 add_pre_buffer("#add_system \"/usr/local/include/%s\"\n", multiarch_dir
);
207 /* We add compiler headers path here because we have to parse
208 * the arguments to get it, falling back to default. */
209 add_pre_buffer("#add_system \"%s/include\"\n", gcc_base_dir
);
210 add_pre_buffer("#add_system \"%s/include-fixed\"\n", gcc_base_dir
);
212 add_pre_buffer("#define __builtin_stdarg_start(a,b) ((a) = (__builtin_va_list)(&(b)))\n");
213 add_pre_buffer("#define __builtin_va_start(a,b) ((a) = (__builtin_va_list)(&(b)))\n");
214 add_pre_buffer("#define __builtin_ms_va_start(a,b) ((a) = (__builtin_ms_va_list)(&(b)))\n");
215 add_pre_buffer("#define __builtin_va_arg(arg,type) ({ type __va_arg_ret = *(type *)(arg); arg += sizeof(type); __va_arg_ret; })\n");
216 add_pre_buffer("#define __builtin_va_alist (*(void *)0)\n");
217 add_pre_buffer("#define __builtin_va_arg_incr(x) ((x) + 1)\n");
218 add_pre_buffer("#define __builtin_va_copy(dest, src) ({ dest = src; (void)0; })\n");
219 add_pre_buffer("#define __builtin_ms_va_copy(dest, src) ({ dest = src; (void)0; })\n");
220 add_pre_buffer("#define __builtin_va_end(arg)\n");
221 add_pre_buffer("#define __builtin_ms_va_end(arg)\n");
222 add_pre_buffer("#define __builtin_va_arg_pack()\n");
225 static struct symbol_list
*sparse_tokenstream(struct token
*token
)
227 int builtin
= token
&& !token
->pos
.stream
;
229 // Preprocess the stream
230 token
= preprocess(token
);
232 if (dump_macro_defs
|| dump_macros_only
) {
234 dump_macro_definitions();
235 if (dump_macros_only
)
239 if (preprocess_only
) {
240 while (!eof_token(token
)) {
242 struct token
*next
= token
->next
;
243 const char *separator
= "";
244 if (next
->pos
.whitespace
)
246 if (next
->pos
.newline
) {
247 separator
= "\n\t\t\t\t\t";
248 prec
= next
->pos
.pos
;
252 printf("%s%.*s", show_token(token
), prec
, separator
);
260 // Parse the resulting C code
261 while (!eof_token(token
))
262 token
= external_declaration(token
, &translation_unit_used_list
, NULL
);
263 return translation_unit_used_list
;
266 static struct symbol_list
*sparse_file(const char *filename
)
271 if (strcmp(filename
, "-") == 0) {
274 fd
= open(filename
, O_RDONLY
);
276 die("No such file: %s", filename
);
278 base_filename
= filename
;
280 // Tokenize the input stream
281 token
= tokenize(filename
, fd
, NULL
, includepath
);
282 store_all_tokens(token
);
285 return sparse_tokenstream(token
);
289 * This handles the "-include" directive etc: we're in global
290 * scope, and all types/macros etc will affect all the following
293 * NOTE NOTE NOTE! "#undef" of anything in this stage will
294 * affect all subsequent files too, i.e. we can have non-local
295 * behaviour between files!
297 static struct symbol_list
*sparse_initial(void)
301 // Prepend any "include" file to the stream.
302 // We're in global scope, it will affect all files!
303 for (i
= 0; i
< cmdline_include_nr
; i
++)
304 add_pre_buffer("#argv_include \"%s\"\n", cmdline_include
[i
]);
306 return sparse_tokenstream(pre_buffer_begin
);
309 struct symbol_list
*sparse_initialize(int argc
, char **argv
, struct string_list
**filelist
)
312 struct symbol_list
*list
;
314 // Initialize symbol stream first, so that we can add defines etc
318 // initialize the default target to the native 'machine'
319 target_config(MACH_NATIVE
);
327 if (arg
[0] == '-' && arg
[1]) {
328 args
= handle_switch(arg
+1, args
);
331 add_ptr_list(filelist
, arg
);
333 handle_switch_finalize();
335 // Redirect stdout if needed
336 if (dump_macro_defs
|| preprocess_only
)
338 if (do_output
&& outfile
&& strcmp(outfile
, "-")) {
339 if (!freopen(outfile
, "w", stdout
))
340 die("error: cannot open %s: %s", outfile
, strerror(errno
));
344 fdump_ir
= PASS_FINAL
;
348 // Initialize type system
353 create_builtin_stream();
356 list
= sparse_initial();
359 * Protect the initial token allocations, since
360 * they need to survive all the others
362 protect_token_alloc();
365 * Evaluate the complete symbol list
366 * Note: This is not needed for normal cases.
367 * These symbols should only be predefined defines and
368 * declaratons which will be evaluated later, when needed.
369 * This is also the case when a file is directly included via
370 * '-include <file>' on the command line *AND* the file only
371 * contains defines, declarations and inline definitions.
372 * However, in the rare cases where the given file should
373 * contain some definitions, these will never be evaluated
374 * and thus won't be able to be linearized correctly.
375 * Hence the evaluate_symbol_list() here under.
377 evaluate_symbol_list(list
);
381 struct symbol_list
* sparse_keep_tokens(char *filename
)
383 struct symbol_list
*res
;
385 /* Clear previous symbol list */
386 translation_unit_used_list
= NULL
;
389 res
= sparse_file(filename
);
396 struct symbol_list
* __sparse(char *filename
)
398 struct symbol_list
*res
;
400 res
= sparse_keep_tokens(filename
);
402 /* Drop the tokens for this file after parsing */
409 struct symbol_list
* sparse(char *filename
)
411 struct symbol_list
*res
= __sparse(filename
);
413 if (has_error
& ERROR_CURR_PHASE
)
414 has_error
= ERROR_PREV_PHASE
;
415 /* Evaluate the complete symbol list */
416 evaluate_symbol_list(res
);