]>
Commit | Line | Data |
---|---|---|
670637ee DF |
1 | #include "config.h" |
2 | #include "system.h" | |
3 | #include "coretypes.h" | |
4 | #include "tm.h" | |
5 | #include "tree.h" | |
6 | #include "version.h" | |
7 | #include "flags.h" | |
8 | ||
9 | ||
10 | #include "options.h" | |
11 | #include "gfortran.h" | |
3c2a68b3 | 12 | #include "tm_p.h" /* Target prototypes. */ |
670637ee DF |
13 | #include "target.h" |
14 | #include "toplev.h" | |
15 | #include "diagnostic.h" | |
16 | ||
17 | #include "../../libcpp/internal.h" | |
18 | #include "cpp.h" | |
19 | #include "incpath.h" | |
20 | ||
21 | #ifndef TARGET_OS_CPP_BUILTINS | |
22 | # define TARGET_OS_CPP_BUILTINS() | |
23 | #endif | |
24 | ||
25 | #ifndef TARGET_OBJFMT_CPP_BUILTINS | |
26 | # define TARGET_OBJFMT_CPP_BUILTINS() | |
27 | #endif | |
28 | ||
29 | ||
30 | /* Holds switches parsed by gfc_cpp_handle_option (), but whose | |
31 | handling is deferred to gfc_cpp_init (). */ | |
32 | typedef struct | |
33 | { | |
34 | enum opt_code code; | |
35 | const char *arg; | |
36 | } | |
37 | gfc_cpp_deferred_opt_t; | |
38 | ||
39 | ||
40 | /* Defined and undefined macros being queued for output with -dU at | |
41 | the next newline. */ | |
42 | typedef struct gfc_cpp_macro_queue | |
43 | { | |
44 | struct gfc_cpp_macro_queue *next; /* Next macro in the list. */ | |
45 | char *macro; /* The name of the macro if not | |
46 | defined, the full definition if | |
47 | defined. */ | |
48 | } gfc_cpp_macro_queue; | |
49 | static gfc_cpp_macro_queue *cpp_define_queue, *cpp_undefine_queue; | |
50 | ||
51 | struct | |
52 | { | |
53 | /* Argument of -cpp, implied by SPEC; | |
54 | if NULL, preprocessing disabled. */ | |
55 | const char *temporary_filename; | |
56 | ||
57 | const char *output_filename; /* -o <arg> */ | |
58 | int preprocess_only; /* -E */ | |
59 | int discard_comments; /* -C */ | |
60 | int discard_comments_in_macro_exp; /* -CC */ | |
61 | int print_include_names; /* -H */ | |
62 | int no_line_commands; /* -P */ | |
63 | char dump_macros; /* -d[DMNU] */ | |
64 | int dump_includes; /* -dI */ | |
65 | int working_directory; /* -fworking-directory */ | |
66 | int no_predefined; /* -undef */ | |
67 | int standard_include_paths; /* -nostdinc */ | |
68 | int verbose; /* -v */ | |
69 | ||
70 | const char *multilib; /* -imultilib <dir> */ | |
71 | const char *prefix; /* -iprefix <dir> */ | |
72 | const char *sysroot; /* -isysroot <dir> */ | |
73 | ||
74 | /* Options whose handling needs to be deferred until the | |
75 | appropriate cpp-objects are created: | |
76 | -A predicate=answer | |
77 | -D <macro>[=<val>] | |
78 | -U <macro> */ | |
79 | gfc_cpp_deferred_opt_t *deferred_opt; | |
80 | int deferred_opt_count; | |
81 | } | |
82 | gfc_cpp_option; | |
83 | ||
84 | /* Structures used with libcpp: */ | |
85 | static cpp_options *cpp_option = NULL; | |
86 | static cpp_reader *cpp_in = NULL; | |
87 | ||
88 | /* Defined in toplev.c. */ | |
89 | extern const char *asm_file_name; | |
90 | ||
91 | ||
92 | ||
93 | ||
94 | /* Encapsulates state used to convert a stream of cpp-tokens into | |
95 | a text file. */ | |
96 | static struct | |
97 | { | |
98 | FILE *outf; /* Stream to write to. */ | |
99 | const cpp_token *prev; /* Previous token. */ | |
100 | const cpp_token *source; /* Source token for spacing. */ | |
101 | int src_line; /* Line number currently being written. */ | |
102 | unsigned char printed; /* Nonzero if something output at line. */ | |
103 | bool first_time; /* cb_file_change hasn't been called yet. */ | |
104 | } print; | |
105 | ||
106 | /* General output routines. */ | |
107 | static void scan_translation_unit (cpp_reader *); | |
108 | static void scan_translation_unit_trad (cpp_reader *); | |
109 | ||
110 | /* Callback routines for the parser. Most of these are active only | |
111 | in specific modes. */ | |
112 | static void cb_file_change (cpp_reader *, const struct line_map *); | |
113 | static void cb_line_change (cpp_reader *, const cpp_token *, int); | |
114 | static void cb_define (cpp_reader *, source_location, cpp_hashnode *); | |
115 | static void cb_undef (cpp_reader *, source_location, cpp_hashnode *); | |
116 | static void cb_def_pragma (cpp_reader *, source_location); | |
117 | static void cb_include (cpp_reader *, source_location, const unsigned char *, | |
118 | const char *, int, const cpp_token **); | |
119 | static void cb_ident (cpp_reader *, source_location, const cpp_string *); | |
120 | static void cb_used_define (cpp_reader *, source_location, cpp_hashnode *); | |
121 | static void cb_used_undef (cpp_reader *, source_location, cpp_hashnode *); | |
122 | void pp_dir_change (cpp_reader *, const char *); | |
123 | ||
124 | static int dump_macro (cpp_reader *, cpp_hashnode *, void *); | |
125 | static void dump_queued_macros (cpp_reader *); | |
126 | ||
127 | ||
128 | static void | |
129 | cpp_define_builtins (cpp_reader *pfile) | |
130 | { | |
131 | int major, minor, patchlevel; | |
132 | ||
133 | /* Initialize CPP built-ins; '1' corresponds to 'flag_hosted' | |
134 | in C, defines __STDC_HOSTED__?! */ | |
135 | cpp_init_builtins (pfile, 0); | |
136 | ||
137 | /* Initialize GFORTRAN specific builtins. | |
138 | These are documented. */ | |
139 | if (sscanf (BASEVER, "%d.%d.%d", &major, &minor, &patchlevel) != 3) | |
140 | { | |
141 | sscanf (BASEVER, "%d.%d", &major, &minor); | |
142 | patchlevel = 0; | |
143 | } | |
144 | cpp_define_formatted (pfile, "__GNUC__=%d", major); | |
145 | cpp_define_formatted (pfile, "__GNUC_MINOR__=%d", minor); | |
146 | cpp_define_formatted (pfile, "__GNUC_PATCHLEVEL__=%d", patchlevel); | |
147 | ||
148 | cpp_define (pfile, "__GFORTRAN__=1"); | |
149 | cpp_define (pfile, "_LANGUAGE_FORTRAN=1"); | |
150 | ||
151 | if (gfc_option.flag_openmp) | |
152 | cpp_define (pfile, "_OPENMP=200505"); | |
153 | ||
154 | ||
155 | /* More builtins that might be useful, but are not documented | |
156 | (in no particular order). */ | |
157 | cpp_define_formatted (pfile, "__VERSION__=\"%s\"", version_string); | |
158 | ||
159 | if (flag_pic) | |
160 | { | |
161 | cpp_define_formatted (pfile, "__pic__=%d", flag_pic); | |
162 | cpp_define_formatted (pfile, "__PIC__=%d", flag_pic); | |
163 | } | |
164 | if (flag_pie) | |
165 | { | |
166 | cpp_define_formatted (pfile, "__pie__=%d", flag_pie); | |
167 | cpp_define_formatted (pfile, "__PIE__=%d", flag_pie); | |
168 | } | |
169 | ||
170 | if (optimize_size) | |
171 | cpp_define (pfile, "__OPTIMIZE_SIZE__"); | |
172 | if (optimize) | |
173 | cpp_define (pfile, "__OPTIMIZE__"); | |
174 | ||
175 | if (fast_math_flags_set_p ()) | |
176 | cpp_define (pfile, "__FAST_MATH__"); | |
177 | if (flag_signaling_nans) | |
178 | cpp_define (pfile, "__SUPPORT_SNAN__"); | |
179 | ||
180 | cpp_define_formatted (pfile, "__FINITE_MATH_ONLY__=%d", flag_finite_math_only); | |
181 | ||
182 | /* Definitions for LP64 model. */ | |
183 | if (TYPE_PRECISION (long_integer_type_node) == 64 | |
184 | && POINTER_SIZE == 64 | |
185 | && TYPE_PRECISION (integer_type_node) == 32) | |
186 | { | |
187 | cpp_define (pfile, "_LP64"); | |
188 | cpp_define (pfile, "__LP64__"); | |
189 | } | |
190 | ||
191 | /* Define NAME with value TYPE size_unit. | |
192 | The C-side also defines __SIZEOF_WCHAR_T__, __SIZEOF_WINT_T__ | |
193 | __SIZEOF_PTRDIFF_T__, however, fortran seems to lack the | |
194 | appropriate type nodes. */ | |
195 | ||
196 | #define define_type_sizeof(NAME, TYPE) \ | |
197 | cpp_define_formatted (pfile, NAME"="HOST_WIDE_INT_PRINT_DEC, \ | |
198 | tree_low_cst (TYPE_SIZE_UNIT (TYPE), 1)) | |
199 | ||
200 | define_type_sizeof ("__SIZEOF_INT__", integer_type_node); | |
201 | define_type_sizeof ("__SIZEOF_LONG__", long_integer_type_node); | |
202 | define_type_sizeof ("__SIZEOF_LONG_LONG__", long_long_integer_type_node); | |
203 | define_type_sizeof ("__SIZEOF_SHORT__", short_integer_type_node); | |
204 | define_type_sizeof ("__SIZEOF_FLOAT__", float_type_node); | |
205 | define_type_sizeof ("__SIZEOF_DOUBLE__", double_type_node); | |
206 | define_type_sizeof ("__SIZEOF_LONG_DOUBLE__", long_double_type_node); | |
207 | define_type_sizeof ("__SIZEOF_SIZE_T__", size_type_node); | |
208 | ||
209 | #undef define_type_sizeof | |
210 | ||
211 | /* The defines below are necessary for the TARGET_* macros. | |
212 | ||
213 | FIXME: Note that builtin_define_std() actually is a function | |
214 | in c-cppbuiltin.c which uses flags undefined for Fortran. | |
215 | Let's skip this for now. If needed, one needs to look into it | |
216 | once more. */ | |
217 | ||
218 | # define builtin_define(TXT) cpp_define (pfile, TXT) | |
219 | # define builtin_define_std(TXT) | |
220 | # define builtin_assert(TXT) cpp_assert (pfile, TXT) | |
221 | ||
3c2a68b3 DF |
222 | /* FIXME: Pandora's Box |
223 | Using the macros below results in multiple breakages: | |
224 | - mingw will fail to compile this file as dependent macros | |
225 | assume to be used in c-cppbuiltin.c only. Further, they use | |
226 | flags only valid/defined in C (same as noted above). | |
227 | [config/i386/mingw32.h, config/i386/cygming.h] | |
228 | - other platforms (not as popular) break similarly | |
229 | [grep for 'builtin_define_with_int_value' in config/*] | |
230 | - darwin will compile but not link | |
231 | (Makefile.in needs to link f951 with some target library or | |
232 | object files. Adding $(C_TARGET_OBJS) to $(F951_OBJS) does | |
233 | seemingly not fix the problem.) | |
234 | ||
670637ee DF |
235 | TARGET_CPU_CPP_BUILTINS (); |
236 | TARGET_OS_CPP_BUILTINS (); | |
3c2a68b3 | 237 | TARGET_OBJFMT_CPP_BUILTINS (); */ |
670637ee DF |
238 | |
239 | #undef builtin_define | |
240 | #undef builtin_define_std | |
241 | #undef builtin_assert | |
242 | } | |
243 | ||
244 | bool | |
245 | gfc_cpp_enabled (void) | |
246 | { | |
247 | return gfc_cpp_option.temporary_filename != NULL; | |
248 | } | |
249 | ||
250 | bool | |
251 | gfc_cpp_preprocess_only (void) | |
252 | { | |
253 | return gfc_cpp_option.preprocess_only; | |
254 | } | |
255 | ||
256 | const char * | |
257 | gfc_cpp_temporary_file (void) | |
258 | { | |
259 | return gfc_cpp_option.temporary_filename; | |
260 | } | |
261 | ||
262 | void | |
263 | gfc_cpp_init_options (unsigned int argc, | |
264 | const char **argv ATTRIBUTE_UNUSED) | |
265 | { | |
266 | /* Do not create any objects from libcpp here. If no | |
267 | preprocessing is requested, this would be wasted | |
268 | time and effort. | |
269 | ||
270 | See gfc_cpp_post_options() instead. */ | |
271 | ||
272 | gfc_cpp_option.temporary_filename = NULL; | |
273 | gfc_cpp_option.output_filename = NULL; | |
274 | gfc_cpp_option.preprocess_only = 0; | |
275 | gfc_cpp_option.discard_comments = 1; | |
276 | gfc_cpp_option.discard_comments_in_macro_exp = 1; | |
277 | gfc_cpp_option.print_include_names = 0; | |
278 | gfc_cpp_option.no_line_commands = 0; | |
279 | gfc_cpp_option.dump_macros = '\0'; | |
280 | gfc_cpp_option.dump_includes = 0; | |
281 | gfc_cpp_option.working_directory = -1; | |
282 | gfc_cpp_option.no_predefined = 0; | |
283 | gfc_cpp_option.standard_include_paths = 1; | |
284 | gfc_cpp_option.verbose = 0; | |
285 | ||
286 | gfc_cpp_option.multilib = NULL; | |
287 | gfc_cpp_option.prefix = NULL; | |
288 | gfc_cpp_option.sysroot = NULL; | |
289 | ||
290 | gfc_cpp_option.deferred_opt = XNEWVEC (gfc_cpp_deferred_opt_t, argc); | |
291 | gfc_cpp_option.deferred_opt_count = 0; | |
292 | } | |
293 | ||
294 | int | |
295 | gfc_cpp_handle_option (size_t scode, const char *arg, int value ATTRIBUTE_UNUSED) | |
296 | { | |
297 | int result = 1; | |
298 | enum opt_code code = (enum opt_code) scode; | |
299 | ||
300 | switch (code) | |
301 | { | |
302 | default: | |
303 | result = 0; | |
304 | break; | |
305 | ||
306 | case OPT_cpp: | |
307 | gfc_cpp_option.temporary_filename = arg; | |
308 | break; | |
309 | ||
310 | case OPT_nocpp: | |
311 | gfc_cpp_option.temporary_filename = 0L; | |
312 | break; | |
313 | ||
314 | case OPT_d: | |
315 | for ( ; *arg; ++arg) | |
316 | switch (*arg) | |
317 | { | |
318 | case 'D': | |
319 | case 'M': | |
320 | case 'N': | |
321 | case 'U': | |
322 | gfc_cpp_option.dump_macros = *arg; | |
323 | break; | |
324 | ||
325 | case 'I': | |
326 | gfc_cpp_option.dump_includes = 1; | |
327 | break; | |
328 | } | |
329 | break; | |
330 | ||
331 | case OPT_fworking_directory: | |
332 | gfc_cpp_option.working_directory = value; | |
333 | break; | |
334 | ||
335 | case OPT_imultilib: | |
336 | gfc_cpp_option.multilib = arg; | |
337 | break; | |
338 | ||
339 | case OPT_iprefix: | |
340 | gfc_cpp_option.prefix = arg; | |
341 | break; | |
342 | ||
343 | case OPT_isysroot: | |
344 | gfc_cpp_option.sysroot = arg; | |
345 | break; | |
346 | ||
347 | case OPT_iquote: | |
348 | case OPT_isystem: | |
349 | gfc_cpp_add_include_path (xstrdup(arg), true); | |
350 | break; | |
351 | ||
352 | case OPT_nostdinc: | |
353 | gfc_cpp_option.standard_include_paths = value; | |
354 | break; | |
355 | ||
356 | case OPT_o: | |
357 | if (!gfc_cpp_option.output_filename) | |
358 | gfc_cpp_option.output_filename = arg; | |
359 | else | |
360 | gfc_fatal_error ("output filename specified twice"); | |
361 | break; | |
362 | ||
363 | case OPT_undef: | |
364 | gfc_cpp_option.no_predefined = value; | |
365 | break; | |
366 | ||
367 | case OPT_v: | |
368 | gfc_cpp_option.verbose = value; | |
369 | break; | |
370 | ||
371 | case OPT_A: | |
372 | case OPT_D: | |
373 | case OPT_U: | |
374 | gfc_cpp_option.deferred_opt[gfc_cpp_option.deferred_opt_count].code = code; | |
375 | gfc_cpp_option.deferred_opt[gfc_cpp_option.deferred_opt_count].arg = arg; | |
376 | gfc_cpp_option.deferred_opt_count++; | |
377 | break; | |
378 | ||
379 | case OPT_C: | |
380 | gfc_cpp_option.discard_comments = 0; | |
381 | break; | |
382 | ||
383 | case OPT_CC: | |
384 | gfc_cpp_option.discard_comments = 0; | |
385 | gfc_cpp_option.discard_comments_in_macro_exp = 0; | |
386 | break; | |
387 | ||
388 | case OPT_E: | |
389 | gfc_cpp_option.preprocess_only = 1; | |
390 | break; | |
391 | ||
392 | case OPT_H: | |
393 | gfc_cpp_option.print_include_names = 1; | |
394 | break; | |
395 | ||
396 | case OPT_P: | |
397 | gfc_cpp_option.no_line_commands = 1; | |
398 | break; | |
399 | } | |
400 | ||
401 | return result; | |
402 | } | |
403 | ||
404 | ||
405 | void | |
406 | gfc_cpp_post_options (void) | |
407 | { | |
408 | /* Any preprocessing-related option without '-cpp' is considered | |
409 | an error. */ | |
410 | if (!gfc_cpp_enabled () | |
411 | && (gfc_cpp_preprocess_only () | |
412 | || !gfc_cpp_option.discard_comments | |
413 | || !gfc_cpp_option.discard_comments_in_macro_exp | |
414 | || gfc_cpp_option.print_include_names | |
415 | || gfc_cpp_option.no_line_commands | |
416 | || gfc_cpp_option.dump_macros | |
417 | || gfc_cpp_option.dump_includes)) | |
418 | gfc_fatal_error("To enable preprocessing, use -cpp"); | |
419 | ||
420 | cpp_in = cpp_create_reader (CLK_GNUC89, NULL, line_table); | |
421 | if (!gfc_cpp_enabled()) | |
422 | return; | |
423 | ||
424 | gcc_assert (cpp_in); | |
425 | ||
426 | /* The cpp_options-structure defines far more flags than those set here. | |
427 | If any other is implemented, see c-opt.c (sanitize_cpp_opts) for | |
428 | inter-option dependencies that may need to be enforced. */ | |
429 | cpp_option = cpp_get_options (cpp_in); | |
430 | gcc_assert (cpp_option); | |
431 | ||
432 | /* TODO: allow non-traditional modes, e.g. by -cpp-std=...? */ | |
433 | cpp_option->traditional = 1; | |
434 | cpp_option->cplusplus_comments = 0; | |
435 | ||
436 | cpp_option->pedantic = pedantic; | |
437 | cpp_option->inhibit_warnings = inhibit_warnings; | |
438 | ||
439 | cpp_option->dollars_in_ident = gfc_option.flag_dollar_ok; | |
440 | cpp_option->discard_comments = gfc_cpp_option.discard_comments; | |
441 | cpp_option->discard_comments_in_macro_exp = gfc_cpp_option.discard_comments_in_macro_exp; | |
442 | cpp_option->print_include_names = gfc_cpp_option.print_include_names; | |
443 | cpp_option->preprocessed = gfc_option.flag_preprocessed; | |
444 | ||
445 | if (gfc_cpp_option.working_directory == -1) | |
446 | gfc_cpp_option.working_directory = (debug_info_level != DINFO_LEVEL_NONE); | |
447 | ||
448 | cpp_post_options (cpp_in); | |
449 | ||
450 | /* If an error has occurred in cpplib, note it so we fail immediately. */ | |
451 | errorcount += cpp_errors (cpp_in); | |
452 | ||
453 | gfc_cpp_register_include_paths (); | |
454 | } | |
455 | ||
456 | ||
457 | void | |
458 | gfc_cpp_init_0 (void) | |
459 | { | |
460 | struct cpp_callbacks *cb; | |
461 | ||
462 | cb = cpp_get_callbacks (cpp_in); | |
463 | cb->file_change = cb_file_change; | |
464 | cb->line_change = cb_line_change; | |
465 | cb->ident = cb_ident; | |
466 | cb->def_pragma = cb_def_pragma; | |
467 | ||
468 | if (gfc_cpp_option.dump_includes) | |
469 | cb->include = cb_include; | |
470 | ||
471 | if ((gfc_cpp_option.dump_macros == 'D') | |
472 | || (gfc_cpp_option.dump_macros == 'N')) | |
473 | { | |
474 | cb->define = cb_define; | |
475 | cb->undef = cb_undef; | |
476 | } | |
477 | ||
478 | if (gfc_cpp_option.dump_macros == 'U') | |
479 | { | |
480 | cb->before_define = dump_queued_macros; | |
481 | cb->used_define = cb_used_define; | |
482 | cb->used_undef = cb_used_undef; | |
483 | } | |
484 | ||
485 | /* Initialize the print structure. Setting print.src_line to -1 here is | |
486 | a trick to guarantee that the first token of the file will cause | |
487 | a linemarker to be output by maybe_print_line. */ | |
488 | print.src_line = -1; | |
489 | print.printed = 0; | |
490 | print.prev = 0; | |
491 | print.first_time = 1; | |
492 | ||
493 | if (gfc_cpp_preprocess_only ()) | |
494 | { | |
495 | if (gfc_cpp_option.output_filename) | |
496 | { | |
497 | /* This needs cheating: with "-E -o <file>", the user wants the | |
498 | preprocessed output in <file>. However, if nothing is done | |
499 | about it <file> is also used for assembler output. Hence, it | |
500 | is necessary to redirect assembler output (actually nothing | |
501 | as -E implies -fsyntax-only) to another file, otherwise the | |
502 | output from preprocessing is lost. */ | |
503 | asm_file_name = gfc_cpp_option.temporary_filename; | |
504 | ||
505 | print.outf = fopen (gfc_cpp_option.output_filename, "w"); | |
506 | if (print.outf == NULL) | |
507 | gfc_fatal_error ("opening output file %s: %s", | |
508 | gfc_cpp_option.output_filename, strerror(errno)); | |
509 | } | |
510 | else | |
511 | print.outf = stdout; | |
512 | } | |
513 | else | |
514 | { | |
515 | print.outf = fopen (gfc_cpp_option.temporary_filename, "w"); | |
516 | if (print.outf == NULL) | |
517 | gfc_fatal_error ("opening output file %s: %s", | |
518 | gfc_cpp_option.temporary_filename, strerror(errno)); | |
519 | } | |
520 | ||
521 | gcc_assert(cpp_in); | |
522 | if (!cpp_read_main_file (cpp_in, gfc_source_file)) | |
523 | errorcount++; | |
524 | } | |
525 | ||
526 | void | |
527 | gfc_cpp_init (void) | |
528 | { | |
529 | int i; | |
530 | ||
531 | cpp_change_file (cpp_in, LC_RENAME, _("<built-in>")); | |
532 | if (!gfc_cpp_option.no_predefined) | |
533 | cpp_define_builtins (cpp_in); | |
534 | ||
535 | /* Handle deferred options from command-line. */ | |
536 | cpp_change_file (cpp_in, LC_RENAME, _("<command-line>")); | |
537 | ||
538 | for (i = 0; i < gfc_cpp_option.deferred_opt_count; i++) | |
539 | { | |
540 | gfc_cpp_deferred_opt_t *opt = &gfc_cpp_option.deferred_opt[i]; | |
541 | ||
542 | if (opt->code == OPT_D) | |
543 | cpp_define (cpp_in, opt->arg); | |
544 | else if (opt->code == OPT_U) | |
545 | cpp_undef (cpp_in, opt->arg); | |
546 | else if (opt->code == OPT_A) | |
547 | { | |
548 | if (opt->arg[0] == '-') | |
549 | cpp_unassert (cpp_in, opt->arg + 1); | |
550 | else | |
551 | cpp_assert (cpp_in, opt->arg); | |
552 | } | |
553 | } | |
554 | ||
555 | if (gfc_cpp_option.working_directory | |
556 | && gfc_cpp_option.preprocess_only && !gfc_cpp_option.no_line_commands) | |
557 | pp_dir_change (cpp_in, get_src_pwd ()); | |
558 | } | |
559 | ||
560 | try | |
561 | gfc_cpp_preprocess (const char *source_file) | |
562 | { | |
563 | if (!gfc_cpp_enabled ()) | |
564 | return FAILURE; | |
565 | ||
566 | cpp_change_file (cpp_in, LC_RENAME, source_file); | |
567 | ||
568 | if (cpp_option->traditional) | |
569 | scan_translation_unit_trad (cpp_in); | |
570 | else | |
571 | scan_translation_unit (cpp_in); | |
572 | ||
573 | /* -dM command line option. */ | |
574 | if (gfc_cpp_preprocess_only () && | |
575 | gfc_cpp_option.dump_macros == 'M') | |
576 | { | |
577 | putc ('\n', print.outf); | |
578 | cpp_forall_identifiers (cpp_in, dump_macro, NULL); | |
579 | } | |
580 | ||
581 | if (!gfc_cpp_preprocess_only () | |
582 | || (gfc_cpp_preprocess_only () && gfc_cpp_option.output_filename)) | |
583 | fclose (print.outf); | |
584 | ||
585 | return SUCCESS; | |
586 | } | |
587 | ||
588 | void | |
589 | gfc_cpp_done (void) | |
590 | { | |
591 | if (!gfc_cpp_enabled ()) | |
592 | return; | |
593 | ||
594 | /* TODO: if dependency tracking was enabled, call | |
595 | cpp_finish() here to write dependencies. | |
596 | ||
597 | Use cpp_get_deps() to access the current source's | |
598 | dependencies during parsing. Add dependencies using | |
599 | the mkdeps-interface (defined in libcpp). */ | |
600 | ||
601 | gcc_assert (cpp_in); | |
602 | cpp_undef_all (cpp_in); | |
603 | cpp_clear_file_cache (cpp_in); | |
604 | } | |
605 | ||
606 | /* PATH must be malloc-ed and NULL-terminated. */ | |
607 | void | |
608 | gfc_cpp_add_include_path (char *path, bool user_supplied) | |
609 | { | |
610 | /* CHAIN sets cpp_dir->sysp which differs from 0 if PATH is a system | |
611 | include path. Fortran does not define any system include paths. */ | |
612 | int chain = 0; | |
613 | int cxx_aware = 0; | |
614 | ||
615 | add_path (path, chain, cxx_aware, user_supplied); | |
616 | } | |
617 | ||
618 | void | |
619 | gfc_cpp_register_include_paths (void) | |
620 | { | |
621 | int cxx_stdinc = 0; | |
622 | register_include_chains (cpp_in, gfc_cpp_option.sysroot, | |
623 | gfc_cpp_option.prefix, gfc_cpp_option.multilib, | |
624 | gfc_cpp_option.standard_include_paths, cxx_stdinc, | |
625 | gfc_cpp_option.verbose); | |
626 | } | |
627 | ||
628 | ||
629 | ||
630 | static void scan_translation_unit_trad (cpp_reader *); | |
631 | static void account_for_newlines (const unsigned char *, size_t); | |
632 | static int dump_macro (cpp_reader *, cpp_hashnode *, void *); | |
633 | ||
634 | static void print_line (source_location, const char *); | |
635 | static void maybe_print_line (source_location); | |
636 | ||
637 | ||
638 | /* Writes out the preprocessed file, handling spacing and paste | |
639 | avoidance issues. */ | |
640 | static void | |
641 | scan_translation_unit (cpp_reader *pfile) | |
642 | { | |
643 | bool avoid_paste = false; | |
644 | ||
645 | print.source = NULL; | |
646 | for (;;) | |
647 | { | |
648 | const cpp_token *token = cpp_get_token (pfile); | |
649 | ||
650 | if (token->type == CPP_PADDING) | |
651 | { | |
652 | avoid_paste = true; | |
653 | if (print.source == NULL | |
654 | || (!(print.source->flags & PREV_WHITE) | |
655 | && token->val.source == NULL)) | |
656 | print.source = token->val.source; | |
657 | continue; | |
658 | } | |
659 | ||
660 | if (token->type == CPP_EOF) | |
661 | break; | |
662 | ||
663 | /* Subtle logic to output a space if and only if necessary. */ | |
664 | if (avoid_paste) | |
665 | { | |
666 | if (print.source == NULL) | |
667 | print.source = token; | |
668 | if (print.source->flags & PREV_WHITE | |
669 | || (print.prev | |
670 | && cpp_avoid_paste (pfile, print.prev, token)) | |
671 | || (print.prev == NULL && token->type == CPP_HASH)) | |
672 | putc (' ', print.outf); | |
673 | } | |
674 | else if (token->flags & PREV_WHITE) | |
675 | putc (' ', print.outf); | |
676 | ||
677 | avoid_paste = false; | |
678 | print.source = NULL; | |
679 | print.prev = token; | |
680 | cpp_output_token (token, print.outf); | |
681 | ||
682 | if (token->type == CPP_COMMENT) | |
683 | account_for_newlines (token->val.str.text, token->val.str.len); | |
684 | } | |
685 | } | |
686 | ||
687 | /* Adjust print.src_line for newlines embedded in output. */ | |
688 | static void | |
689 | account_for_newlines (const unsigned char *str, size_t len) | |
690 | { | |
691 | while (len--) | |
692 | if (*str++ == '\n') | |
693 | print.src_line++; | |
694 | } | |
695 | ||
696 | /* Writes out a traditionally preprocessed file. */ | |
697 | static void | |
698 | scan_translation_unit_trad (cpp_reader *pfile) | |
699 | { | |
700 | while (_cpp_read_logical_line_trad (pfile)) | |
701 | { | |
702 | size_t len = pfile->out.cur - pfile->out.base; | |
703 | maybe_print_line (pfile->out.first_line); | |
704 | fwrite (pfile->out.base, 1, len, print.outf); | |
705 | print.printed = 1; | |
706 | if (!CPP_OPTION (pfile, discard_comments)) | |
707 | account_for_newlines (pfile->out.base, len); | |
708 | } | |
709 | } | |
710 | ||
711 | /* If the token read on logical line LINE needs to be output on a | |
712 | different line to the current one, output the required newlines or | |
713 | a line marker. */ | |
714 | static void | |
715 | maybe_print_line (source_location src_loc) | |
716 | { | |
717 | const struct line_map *map = linemap_lookup (line_table, src_loc); | |
718 | int src_line = SOURCE_LINE (map, src_loc); | |
719 | ||
720 | /* End the previous line of text. */ | |
721 | if (print.printed) | |
722 | { | |
723 | putc ('\n', print.outf); | |
724 | print.src_line++; | |
725 | print.printed = 0; | |
726 | } | |
727 | ||
728 | if (src_line >= print.src_line && src_line < print.src_line + 8) | |
729 | { | |
730 | while (src_line > print.src_line) | |
731 | { | |
732 | putc ('\n', print.outf); | |
733 | print.src_line++; | |
734 | } | |
735 | } | |
736 | else | |
737 | print_line (src_loc, ""); | |
738 | } | |
739 | ||
740 | /* Output a line marker for logical line LINE. Special flags are "1" | |
741 | or "2" indicating entering or leaving a file. */ | |
742 | static void | |
743 | print_line (source_location src_loc, const char *special_flags) | |
744 | { | |
745 | /* End any previous line of text. */ | |
746 | if (print.printed) | |
747 | putc ('\n', print.outf); | |
748 | print.printed = 0; | |
749 | ||
750 | if (!gfc_cpp_option.no_line_commands) | |
751 | { | |
752 | const struct line_map *map = linemap_lookup (line_table, src_loc); | |
753 | ||
754 | size_t to_file_len = strlen (map->to_file); | |
755 | unsigned char *to_file_quoted = | |
756 | (unsigned char *) alloca (to_file_len * 4 + 1); | |
757 | unsigned char *p; | |
758 | ||
759 | print.src_line = SOURCE_LINE (map, src_loc); | |
760 | ||
761 | /* cpp_quote_string does not nul-terminate, so we have to do it | |
762 | ourselves. */ | |
763 | p = cpp_quote_string (to_file_quoted, | |
764 | (const unsigned char *) map->to_file, to_file_len); | |
765 | *p = '\0'; | |
766 | fprintf (print.outf, "# %u \"%s\"%s", | |
767 | print.src_line == 0 ? 1 : print.src_line, | |
768 | to_file_quoted, special_flags); | |
769 | ||
770 | if (map->sysp == 2) | |
771 | fputs (" 3 4", print.outf); | |
772 | else if (map->sysp == 1) | |
773 | fputs (" 3", print.outf); | |
774 | ||
775 | putc ('\n', print.outf); | |
776 | } | |
777 | } | |
778 | ||
779 | static void | |
780 | cb_file_change (cpp_reader * ARG_UNUSED (pfile), const struct line_map *map) | |
781 | { | |
782 | const char *flags = ""; | |
783 | ||
784 | if (gfc_cpp_option.no_line_commands) | |
785 | return; | |
786 | ||
787 | if (!map) | |
788 | return; | |
789 | ||
790 | if (print.first_time) | |
791 | { | |
792 | /* Avoid printing foo.i when the main file is foo.c. */ | |
793 | if (!cpp_get_options (cpp_in)->preprocessed) | |
794 | print_line (map->start_location, flags); | |
795 | print.first_time = 0; | |
796 | } | |
797 | else | |
798 | { | |
799 | /* Bring current file to correct line when entering a new file. */ | |
800 | if (map->reason == LC_ENTER) | |
801 | { | |
802 | const struct line_map *from = INCLUDED_FROM (line_table, map); | |
803 | maybe_print_line (LAST_SOURCE_LINE_LOCATION (from)); | |
804 | } | |
805 | if (map->reason == LC_ENTER) | |
806 | flags = " 1"; | |
807 | else if (map->reason == LC_LEAVE) | |
808 | flags = " 2"; | |
809 | print_line (map->start_location, flags); | |
810 | } | |
811 | ||
812 | } | |
813 | ||
814 | /* Called when a line of output is started. TOKEN is the first token | |
815 | of the line, and at end of file will be CPP_EOF. */ | |
816 | static void | |
817 | cb_line_change (cpp_reader *pfile, const cpp_token *token, | |
818 | int parsing_args) | |
819 | { | |
820 | source_location src_loc = token->src_loc; | |
821 | ||
822 | if (token->type == CPP_EOF || parsing_args) | |
823 | return; | |
824 | ||
825 | maybe_print_line (src_loc); | |
826 | print.prev = 0; | |
827 | print.source = 0; | |
828 | ||
829 | /* Supply enough spaces to put this token in its original column, | |
830 | one space per column greater than 2, since scan_translation_unit | |
831 | will provide a space if PREV_WHITE. Don't bother trying to | |
832 | reconstruct tabs; we can't get it right in general, and nothing | |
833 | ought to care. Some things do care; the fault lies with them. */ | |
834 | if (!CPP_OPTION (pfile, traditional)) | |
835 | { | |
836 | const struct line_map *map = linemap_lookup (line_table, src_loc); | |
837 | int spaces = SOURCE_COLUMN (map, src_loc) - 2; | |
838 | print.printed = 1; | |
839 | ||
840 | while (-- spaces >= 0) | |
841 | putc (' ', print.outf); | |
842 | } | |
843 | } | |
844 | ||
845 | static void | |
846 | cb_ident (cpp_reader *pfile ATTRIBUTE_UNUSED, source_location line, | |
847 | const cpp_string *str) | |
848 | { | |
849 | maybe_print_line (line); | |
850 | fprintf (print.outf, "#ident %s\n", str->text); | |
851 | print.src_line++; | |
852 | } | |
853 | ||
854 | static void | |
855 | cb_define (cpp_reader *pfile ATTRIBUTE_UNUSED, source_location line, | |
856 | cpp_hashnode *node ATTRIBUTE_UNUSED) | |
857 | { | |
858 | maybe_print_line (line); | |
859 | fputs ("#define ", print.outf); | |
860 | ||
861 | /* 'D' is whole definition; 'N' is name only. */ | |
862 | if (gfc_cpp_option.dump_macros == 'D') | |
863 | fputs ((const char *) cpp_macro_definition (pfile, node), | |
864 | print.outf); | |
865 | else | |
866 | fputs ((const char *) NODE_NAME (node), print.outf); | |
867 | ||
868 | putc ('\n', print.outf); | |
869 | if (linemap_lookup (line_table, line)->to_line != 0) | |
870 | print.src_line++; | |
871 | } | |
872 | ||
873 | static void | |
874 | cb_undef (cpp_reader *pfile ATTRIBUTE_UNUSED, source_location line, | |
875 | cpp_hashnode *node) | |
876 | { | |
877 | maybe_print_line (line); | |
878 | fprintf (print.outf, "#undef %s\n", NODE_NAME (node)); | |
879 | print.src_line++; | |
880 | } | |
881 | ||
882 | static void | |
883 | cb_include (cpp_reader *pfile ATTRIBUTE_UNUSED, source_location line, | |
884 | const unsigned char *dir, const char *header, int angle_brackets, | |
885 | const cpp_token **comments) | |
886 | { | |
887 | maybe_print_line (line); | |
888 | if (angle_brackets) | |
889 | fprintf (print.outf, "#%s <%s>", dir, header); | |
890 | else | |
891 | fprintf (print.outf, "#%s \"%s\"", dir, header); | |
892 | ||
893 | if (comments != NULL) | |
894 | { | |
895 | while (*comments != NULL) | |
896 | { | |
897 | if ((*comments)->flags & PREV_WHITE) | |
898 | putc (' ', print.outf); | |
899 | cpp_output_token (*comments, print.outf); | |
900 | ++comments; | |
901 | } | |
902 | } | |
903 | ||
904 | putc ('\n', print.outf); | |
905 | print.src_line++; | |
906 | } | |
907 | ||
908 | /* Dump out the hash table. */ | |
909 | static int | |
910 | dump_macro (cpp_reader *pfile, cpp_hashnode *node, void *v ATTRIBUTE_UNUSED) | |
911 | { | |
912 | if (node->type == NT_MACRO && !(node->flags & NODE_BUILTIN)) | |
913 | { | |
914 | fputs ("#define ", print.outf); | |
915 | fputs ((const char *) cpp_macro_definition (pfile, node), | |
916 | print.outf); | |
917 | putc ('\n', print.outf); | |
918 | print.src_line++; | |
919 | } | |
920 | ||
921 | return 1; | |
922 | } | |
923 | ||
924 | static void | |
925 | cb_used_define (cpp_reader *pfile, source_location line ATTRIBUTE_UNUSED, | |
926 | cpp_hashnode *node) | |
927 | { | |
928 | gfc_cpp_macro_queue *q; | |
929 | q = XNEW (gfc_cpp_macro_queue); | |
930 | q->macro = xstrdup ((const char *) cpp_macro_definition (pfile, node)); | |
931 | q->next = cpp_define_queue; | |
932 | cpp_define_queue = q; | |
933 | } | |
934 | ||
935 | ||
936 | /* Callback called when -fworking-director and -E to emit working | |
937 | directory in cpp output file. */ | |
938 | ||
939 | void | |
940 | pp_dir_change (cpp_reader *pfile ATTRIBUTE_UNUSED, const char *dir) | |
941 | { | |
942 | size_t to_file_len = strlen (dir); | |
943 | unsigned char *to_file_quoted = | |
944 | (unsigned char *) alloca (to_file_len * 4 + 1); | |
945 | unsigned char *p; | |
946 | ||
947 | /* cpp_quote_string does not nul-terminate, so we have to do it ourselves. */ | |
948 | p = cpp_quote_string (to_file_quoted, (const unsigned char *) dir, to_file_len); | |
949 | *p = '\0'; | |
950 | fprintf (print.outf, "# 1 \"%s//\"\n", to_file_quoted); | |
951 | } | |
952 | ||
953 | /* Copy a #pragma directive to the preprocessed output. */ | |
954 | static void | |
955 | cb_def_pragma (cpp_reader *pfile, source_location line) | |
956 | { | |
957 | maybe_print_line (line); | |
958 | fputs ("#pragma ", print.outf); | |
959 | cpp_output_line (pfile, print.outf); | |
960 | print.src_line++; | |
961 | } | |
962 | ||
963 | static void | |
964 | cb_used_undef (cpp_reader *pfile ATTRIBUTE_UNUSED, | |
965 | source_location line ATTRIBUTE_UNUSED, | |
966 | cpp_hashnode *node) | |
967 | { | |
968 | gfc_cpp_macro_queue *q; | |
969 | q = XNEW (gfc_cpp_macro_queue); | |
970 | q->macro = xstrdup ((const char *) NODE_NAME (node)); | |
971 | q->next = cpp_undefine_queue; | |
972 | cpp_undefine_queue = q; | |
973 | } | |
974 | ||
975 | static void | |
976 | dump_queued_macros (cpp_reader *pfile ATTRIBUTE_UNUSED) | |
977 | { | |
978 | gfc_cpp_macro_queue *q; | |
979 | ||
980 | /* End the previous line of text. */ | |
981 | if (print.printed) | |
982 | { | |
983 | putc ('\n', print.outf); | |
984 | print.src_line++; | |
985 | print.printed = 0; | |
986 | } | |
987 | ||
988 | for (q = cpp_define_queue; q;) | |
989 | { | |
990 | gfc_cpp_macro_queue *oq; | |
991 | fputs ("#define ", print.outf); | |
992 | fputs (q->macro, print.outf); | |
993 | putc ('\n', print.outf); | |
994 | print.src_line++; | |
995 | oq = q; | |
996 | q = q->next; | |
997 | gfc_free (oq->macro); | |
998 | gfc_free (oq); | |
999 | } | |
1000 | cpp_define_queue = NULL; | |
1001 | for (q = cpp_undefine_queue; q;) | |
1002 | { | |
1003 | gfc_cpp_macro_queue *oq; | |
1004 | fprintf (print.outf, "#undef %s\n", q->macro); | |
1005 | print.src_line++; | |
1006 | oq = q; | |
1007 | q = q->next; | |
1008 | gfc_free (oq->macro); | |
1009 | gfc_free (oq); | |
1010 | } | |
1011 | cpp_undefine_queue = NULL; | |
1012 | } | |
1013 | ||
1014 |