]> gcc.gnu.org Git - gcc.git/blob - gcc/fortran/options.c
re PR fortran/31675 (Fortran front-end and libgfortran should have a common header...
[gcc.git] / gcc / fortran / options.c
1 /* Parse and display command line options.
2 Copyright (C) 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007
3 Free Software Foundation, Inc.
4 Contributed by Andy Vaught
5
6 This file is part of GCC.
7
8 GCC is free software; you can redistribute it and/or modify it under
9 the terms of the GNU General Public License as published by the Free
10 Software Foundation; either version 3, or (at your option) any later
11 version.
12
13 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
14 WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
16 for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING3. If not see
20 <http://www.gnu.org/licenses/>. */
21
22 #include "config.h"
23 #include "system.h"
24 #include "coretypes.h"
25 #include "tree.h"
26 #include "flags.h"
27 #include "intl.h"
28 #include "opts.h"
29 #include "options.h"
30 #include "params.h"
31 #include "tree-inline.h"
32 #include "gfortran.h"
33 #include "target.h"
34
35 gfc_option_t gfc_option;
36
37
38 /* Set flags that control warnings and errors for different
39 Fortran standards to their default values. */
40
41 static void
42 set_default_std_flags (void)
43 {
44 gfc_option.allow_std = GFC_STD_F95_OBS | GFC_STD_F95_DEL
45 | GFC_STD_F2003 | GFC_STD_F95 | GFC_STD_F77 | GFC_STD_GNU
46 | GFC_STD_LEGACY;
47 gfc_option.warn_std = GFC_STD_F95_DEL | GFC_STD_LEGACY;
48 }
49
50 /* Get ready for options handling. */
51
52 unsigned int
53 gfc_init_options (unsigned int argc ATTRIBUTE_UNUSED,
54 const char **argv ATTRIBUTE_UNUSED)
55 {
56 gfc_source_file = NULL;
57 gfc_option.module_dir = NULL;
58 gfc_option.source_form = FORM_UNKNOWN;
59 gfc_option.fixed_line_length = 72;
60 gfc_option.free_line_length = 132;
61 gfc_option.max_continue_fixed = 19;
62 gfc_option.max_continue_free = 39;
63 gfc_option.max_identifier_length = GFC_MAX_SYMBOL_LEN;
64 gfc_option.max_subrecord_length = 0;
65 gfc_option.convert = GFC_CONVERT_NATIVE;
66 gfc_option.record_marker = 0;
67 gfc_option.verbose = 0;
68
69 gfc_option.warn_aliasing = 0;
70 gfc_option.warn_ampersand = 0;
71 gfc_option.warn_character_truncation = 0;
72 gfc_option.warn_conversion = 0;
73 gfc_option.warn_implicit_interface = 0;
74 gfc_option.warn_line_truncation = 0;
75 gfc_option.warn_surprising = 0;
76 gfc_option.warn_tabs = 1;
77 gfc_option.warn_underflow = 1;
78 gfc_option.max_errors = 25;
79
80 gfc_option.flag_all_intrinsics = 0;
81 gfc_option.flag_default_double = 0;
82 gfc_option.flag_default_integer = 0;
83 gfc_option.flag_default_real = 0;
84 gfc_option.flag_dollar_ok = 0;
85 gfc_option.flag_underscoring = 1;
86 gfc_option.flag_f2c = 0;
87 gfc_option.flag_second_underscore = -1;
88 gfc_option.flag_implicit_none = 0;
89
90 /* Default value of flag_max_stack_var_size is set in gfc_post_options. */
91 gfc_option.flag_max_stack_var_size = -2;
92
93 gfc_option.flag_range_check = 1;
94 gfc_option.flag_pack_derived = 0;
95 gfc_option.flag_repack_arrays = 0;
96 gfc_option.flag_preprocessed = 0;
97 gfc_option.flag_automatic = 1;
98 gfc_option.flag_backslash = 1;
99 gfc_option.flag_module_private = 0;
100 gfc_option.flag_backtrace = 0;
101 gfc_option.flag_allow_leading_underscore = 0;
102 gfc_option.flag_dump_core = 0;
103 gfc_option.flag_external_blas = 0;
104 gfc_option.blas_matmul_limit = 30;
105 gfc_option.flag_cray_pointer = 0;
106 gfc_option.flag_d_lines = -1;
107 gfc_option.flag_openmp = 0;
108 gfc_option.flag_sign_zero = 1;
109 gfc_option.flag_recursive = 0;
110
111 gfc_option.fpe = 0;
112
113 /* Argument pointers cannot point to anything but their argument. */
114 flag_argument_noalias = 3;
115
116 flag_errno_math = 0;
117
118 set_default_std_flags ();
119
120 gfc_option.warn_nonstd_intrinsics = 0;
121
122 /* -fshort-enums can be default on some targets. */
123 gfc_option.fshort_enums = targetm.default_short_enums ();
124
125 return CL_Fortran;
126 }
127
128
129 /* Determine the source form from the filename extension. We assume
130 case insensitivity. */
131
132 static gfc_source_form
133 form_from_filename (const char *filename)
134 {
135 static const struct
136 {
137 const char *extension;
138 gfc_source_form form;
139 }
140 exttype[] =
141 {
142 {
143 ".f90", FORM_FREE}
144 ,
145 {
146 ".f95", FORM_FREE}
147 ,
148 {
149 ".f03", FORM_FREE}
150 ,
151 {
152 ".f", FORM_FIXED}
153 ,
154 {
155 ".for", FORM_FIXED}
156 ,
157 {
158 "", FORM_UNKNOWN}
159 }; /* sentinel value */
160
161 gfc_source_form f_form;
162 const char *fileext;
163 int i;
164
165 /* Find end of file name. Note, filename is either a NULL pointer or
166 a NUL terminated string. */
167 i = 0;
168 while (filename[i] != '\0')
169 i++;
170
171 /* Find last period. */
172 while (i >= 0 && (filename[i] != '.'))
173 i--;
174
175 /* Did we see a file extension? */
176 if (i < 0)
177 return FORM_UNKNOWN; /* Nope */
178
179 /* Get file extension and compare it to others. */
180 fileext = &(filename[i]);
181
182 i = -1;
183 f_form = FORM_UNKNOWN;
184 do
185 {
186 i++;
187 if (strcasecmp (fileext, exttype[i].extension) == 0)
188 {
189 f_form = exttype[i].form;
190 break;
191 }
192 }
193 while (exttype[i].form != FORM_UNKNOWN);
194
195 return f_form;
196 }
197
198
199 /* Finalize commandline options. */
200
201 bool
202 gfc_post_options (const char **pfilename)
203 {
204 const char *filename = *pfilename, *canon_source_file = NULL;
205 char *source_path;
206 int i;
207
208 /* Verify the input file name. */
209 if (!filename || strcmp (filename, "-") == 0)
210 {
211 filename = "";
212 }
213
214 if (gfc_option.flag_preprocessed)
215 {
216 /* For preprocessed files, if the first tokens are of the form # NUM.
217 handle the directives so we know the original file name. */
218 gfc_source_file = gfc_read_orig_filename (filename, &canon_source_file);
219 if (gfc_source_file == NULL)
220 gfc_source_file = filename;
221 else
222 *pfilename = gfc_source_file;
223 }
224 else
225 gfc_source_file = filename;
226
227 if (canon_source_file == NULL)
228 canon_source_file = gfc_source_file;
229
230 /* Adds the path where the source file is to the list of include files. */
231
232 i = strlen (canon_source_file);
233 while (i > 0 && !IS_DIR_SEPARATOR (canon_source_file[i]))
234 i--;
235
236 if (i != 0)
237 {
238 source_path = alloca (i + 1);
239 memcpy (source_path, canon_source_file, i);
240 source_path[i] = 0;
241 gfc_add_include_path (source_path, true);
242 }
243 else
244 gfc_add_include_path (".", true);
245
246 if (canon_source_file != gfc_source_file)
247 gfc_free (CONST_CAST (canon_source_file));
248
249 /* Decide which form the file will be read in as. */
250
251 if (gfc_option.source_form != FORM_UNKNOWN)
252 gfc_current_form = gfc_option.source_form;
253 else
254 {
255 gfc_current_form = form_from_filename (filename);
256
257 if (gfc_current_form == FORM_UNKNOWN)
258 {
259 gfc_current_form = FORM_FREE;
260 gfc_warning_now ("Reading file '%s' as free form",
261 (filename[0] == '\0') ? "<stdin>" : filename);
262 }
263 }
264
265 /* If the user specified -fd-lines-as-{code|comments} verify that we're
266 in fixed form. */
267 if (gfc_current_form == FORM_FREE)
268 {
269 if (gfc_option.flag_d_lines == 0)
270 gfc_warning_now ("'-fd-lines-as-comments' has no effect "
271 "in free form");
272 else if (gfc_option.flag_d_lines == 1)
273 gfc_warning_now ("'-fd-lines-as-code' has no effect in free form");
274 }
275
276 flag_inline_trees = 1;
277
278 /* Use tree inlining. */
279 if (!flag_no_inline)
280 flag_no_inline = 1;
281 if (flag_inline_functions)
282 flag_inline_trees = 2;
283
284 /* If -pedantic, warn about the use of GNU extensions. */
285 if (pedantic && (gfc_option.allow_std & GFC_STD_GNU) != 0)
286 gfc_option.warn_std |= GFC_STD_GNU;
287 /* -std=legacy -pedantic is effectively -std=gnu. */
288 if (pedantic && (gfc_option.allow_std & GFC_STD_LEGACY) != 0)
289 gfc_option.warn_std |= GFC_STD_F95_OBS | GFC_STD_F95_DEL | GFC_STD_LEGACY;
290
291 /* If the user didn't explicitly specify -f(no)-second-underscore we
292 use it if we're trying to be compatible with f2c, and not
293 otherwise. */
294 if (gfc_option.flag_second_underscore == -1)
295 gfc_option.flag_second_underscore = gfc_option.flag_f2c;
296
297 if (!gfc_option.flag_automatic && gfc_option.flag_max_stack_var_size != -2
298 && gfc_option.flag_max_stack_var_size != 0)
299 gfc_warning_now ("Flag -fno-automatic overwrites -fmax-stack-var-size=%d",
300 gfc_option.flag_max_stack_var_size);
301 else if (!gfc_option.flag_automatic && gfc_option.flag_recursive)
302 gfc_warning_now ("Flag -fno-automatic overwrites -frecursive");
303 else if (!gfc_option.flag_automatic && gfc_option.flag_openmp)
304 gfc_warning_now ("Flag -fno-automatic overwrites -frecursive implied by "
305 "-fopenmp");
306 else if (gfc_option.flag_max_stack_var_size != -2
307 && gfc_option.flag_recursive)
308 gfc_warning_now ("Flag -frecursive overwrites -fmax-stack-var-size=%d",
309 gfc_option.flag_max_stack_var_size);
310 else if (gfc_option.flag_max_stack_var_size != -2
311 && gfc_option.flag_openmp)
312 gfc_warning_now ("Flag -fmax-stack-var-size=%d overwrites -frecursive "
313 "implied by -fopenmp",
314 gfc_option.flag_max_stack_var_size);
315
316 /* Implied -frecursive; implemented as -fmax-stack-var-size=-1. */
317 if (gfc_option.flag_max_stack_var_size == -2 && gfc_option.flag_openmp)
318 gfc_option.flag_max_stack_var_size = -1;
319
320 /* Set default. */
321 if (gfc_option.flag_max_stack_var_size == -2)
322 gfc_option.flag_max_stack_var_size = 32768;
323
324 /* Implement -frecursive as -fmax-stack-var-size=-1. */
325 if (gfc_option.flag_recursive)
326 gfc_option.flag_max_stack_var_size = -1;
327
328 /* Implement -fno-automatic as -fmax-stack-var-size=0. */
329 if (!gfc_option.flag_automatic)
330 gfc_option.flag_max_stack_var_size = 0;
331
332 if (pedantic)
333 {
334 gfc_option.warn_ampersand = 1;
335 gfc_option.warn_tabs = 0;
336 }
337
338 if (gfc_option.flag_all_intrinsics)
339 gfc_option.warn_nonstd_intrinsics = 0;
340
341 return false;
342 }
343
344
345 /* Set the options for -Wall. */
346
347 static void
348 set_Wall (int setting)
349 {
350 gfc_option.warn_aliasing = setting;
351 gfc_option.warn_ampersand = setting;
352 gfc_option.warn_line_truncation = setting;
353 gfc_option.warn_nonstd_intrinsics = setting;
354 gfc_option.warn_surprising = setting;
355 gfc_option.warn_tabs = !setting;
356 gfc_option.warn_underflow = setting;
357 gfc_option.warn_character_truncation = setting;
358
359 set_Wunused (setting);
360 warn_return_type = setting;
361 warn_switch = setting;
362
363 /* We save the value of warn_uninitialized, since if they put
364 -Wuninitialized on the command line, we need to generate a
365 warning about not using it without also specifying -O. */
366 if (setting == 0)
367 warn_uninitialized = 0;
368 else if (warn_uninitialized != 1)
369 warn_uninitialized = 2;
370 }
371
372
373 static void
374 gfc_handle_module_path_options (const char *arg)
375 {
376
377 if (gfc_option.module_dir != NULL)
378 {
379 gfc_status ("gfortran: Only one -M option allowed\n");
380 exit (3);
381 }
382
383 if (arg == NULL)
384 {
385 gfc_status ("gfortran: Directory required after -M\n");
386 exit (3);
387 }
388
389 gfc_option.module_dir = (char *) gfc_getmem (strlen (arg) + 2);
390 strcpy (gfc_option.module_dir, arg);
391 strcat (gfc_option.module_dir, "/");
392
393 gfc_add_include_path (gfc_option.module_dir, true);
394 }
395
396
397 static void
398 gfc_handle_fpe_trap_option (const char *arg)
399 {
400 int result, pos = 0, n;
401 static const char * const exception[] = { "invalid", "denormal", "zero",
402 "overflow", "underflow",
403 "precision", NULL };
404 static const int opt_exception[] = { GFC_FPE_INVALID, GFC_FPE_DENORMAL,
405 GFC_FPE_ZERO, GFC_FPE_OVERFLOW,
406 GFC_FPE_UNDERFLOW, GFC_FPE_PRECISION,
407 0 };
408
409 while (*arg)
410 {
411 while (*arg == ',')
412 arg++;
413
414 while (arg[pos] && arg[pos] != ',')
415 pos++;
416
417 result = 0;
418 for (n = 0; exception[n] != NULL; n++)
419 {
420 if (exception[n] && strncmp (exception[n], arg, pos) == 0)
421 {
422 gfc_option.fpe |= opt_exception[n];
423 arg += pos;
424 pos = 0;
425 result = 1;
426 break;
427 }
428 }
429 if (!result)
430 gfc_fatal_error ("Argument to -ffpe-trap is not valid: %s", arg);
431 }
432 }
433
434
435 /* Handle command-line options. Returns 0 if unrecognized, 1 if
436 recognized and handled. */
437
438 int
439 gfc_handle_option (size_t scode, const char *arg, int value)
440 {
441 int result = 1;
442 enum opt_code code = (enum opt_code) scode;
443
444 /* Ignore file names. */
445 if (code == N_OPTS)
446 return 1;
447
448 switch (code)
449 {
450 default:
451 result = 0;
452 break;
453
454 case OPT_Wall:
455 set_Wall (value);
456 break;
457
458 case OPT_Waliasing:
459 gfc_option.warn_aliasing = value;
460 break;
461
462 case OPT_Wampersand:
463 gfc_option.warn_ampersand = value;
464 break;
465
466 case OPT_Wcharacter_truncation:
467 gfc_option.warn_character_truncation = value;
468 break;
469
470 case OPT_Wconversion:
471 gfc_option.warn_conversion = value;
472 break;
473
474 case OPT_Wimplicit_interface:
475 gfc_option.warn_implicit_interface = value;
476 break;
477
478 case OPT_Wline_truncation:
479 gfc_option.warn_line_truncation = value;
480 break;
481
482 case OPT_Wsurprising:
483 gfc_option.warn_surprising = value;
484 break;
485
486 case OPT_Wtabs:
487 gfc_option.warn_tabs = value;
488 break;
489
490 case OPT_Wunderflow:
491 gfc_option.warn_underflow = value;
492 break;
493
494 case OPT_fall_intrinsics:
495 gfc_option.flag_all_intrinsics = 1;
496 break;
497
498 case OPT_fautomatic:
499 gfc_option.flag_automatic = value;
500 break;
501
502 case OPT_fallow_leading_underscore:
503 gfc_option.flag_allow_leading_underscore = value;
504 break;
505
506 case OPT_fbackslash:
507 gfc_option.flag_backslash = value;
508 break;
509
510 case OPT_fbacktrace:
511 gfc_option.flag_backtrace = value;
512 break;
513
514 case OPT_fdump_core:
515 gfc_option.flag_dump_core = value;
516 break;
517
518 case OPT_fcray_pointer:
519 gfc_option.flag_cray_pointer = value;
520 break;
521
522 case OPT_ff2c:
523 gfc_option.flag_f2c = value;
524 break;
525
526 case OPT_fdollar_ok:
527 gfc_option.flag_dollar_ok = value;
528 break;
529
530 case OPT_fexternal_blas:
531 gfc_option.flag_external_blas = value;
532 break;
533
534 case OPT_fblas_matmul_limit_:
535 gfc_option.blas_matmul_limit = value;
536 break;
537
538 case OPT_fd_lines_as_code:
539 gfc_option.flag_d_lines = 1;
540 break;
541
542 case OPT_fd_lines_as_comments:
543 gfc_option.flag_d_lines = 0;
544 break;
545
546 case OPT_fdump_parse_tree:
547 gfc_option.verbose = value;
548 break;
549
550 case OPT_ffixed_form:
551 gfc_option.source_form = FORM_FIXED;
552 break;
553
554 case OPT_ffixed_line_length_none:
555 gfc_option.fixed_line_length = 0;
556 break;
557
558 case OPT_ffixed_line_length_:
559 if (value != 0 && value < 7)
560 gfc_fatal_error ("Fixed line length must be at least seven.");
561 gfc_option.fixed_line_length = value;
562 break;
563
564 case OPT_ffree_form:
565 gfc_option.source_form = FORM_FREE;
566 break;
567
568 case OPT_fopenmp:
569 gfc_option.flag_openmp = value;
570 break;
571
572 case OPT_ffree_line_length_none:
573 gfc_option.free_line_length = 0;
574 break;
575
576 case OPT_ffree_line_length_:
577 if (value != 0 && value < 4)
578 gfc_fatal_error ("Free line length must be at least three.");
579 gfc_option.free_line_length = value;
580 break;
581
582 case OPT_funderscoring:
583 gfc_option.flag_underscoring = value;
584 break;
585
586 case OPT_fsecond_underscore:
587 gfc_option.flag_second_underscore = value;
588 break;
589
590 case OPT_static_libgfortran:
591 #ifndef HAVE_LD_STATIC_DYNAMIC
592 gfc_fatal_error ("-static-libgfortran is not supported in this "
593 "configuration");
594 #endif
595 break;
596
597 case OPT_fimplicit_none:
598 gfc_option.flag_implicit_none = value;
599 break;
600
601 case OPT_fintrinsic_modules_path:
602 gfc_add_include_path (arg, false);
603 gfc_add_intrinsic_modules_path (arg);
604 break;
605
606 case OPT_fmax_errors_:
607 gfc_option.max_errors = value;
608 break;
609
610 case OPT_fmax_stack_var_size_:
611 gfc_option.flag_max_stack_var_size = value;
612 break;
613
614 case OPT_fmodule_private:
615 gfc_option.flag_module_private = value;
616 break;
617
618 case OPT_frange_check:
619 gfc_option.flag_range_check = value;
620 break;
621
622 case OPT_fpack_derived:
623 gfc_option.flag_pack_derived = value;
624 break;
625
626 case OPT_frepack_arrays:
627 gfc_option.flag_repack_arrays = value;
628 break;
629
630 case OPT_fpreprocessed:
631 gfc_option.flag_preprocessed = value;
632 break;
633
634 case OPT_fmax_identifier_length_:
635 if (value > GFC_MAX_SYMBOL_LEN)
636 gfc_fatal_error ("Maximum supported identifier length is %d",
637 GFC_MAX_SYMBOL_LEN);
638 gfc_option.max_identifier_length = value;
639 break;
640
641 case OPT_fdefault_integer_8:
642 gfc_option.flag_default_integer = value;
643 break;
644
645 case OPT_fdefault_real_8:
646 gfc_option.flag_default_real = value;
647 break;
648
649 case OPT_fdefault_double_8:
650 gfc_option.flag_default_double = value;
651 break;
652
653 case OPT_I:
654 gfc_add_include_path (arg, true);
655 break;
656
657 case OPT_J:
658 case OPT_M:
659 gfc_handle_module_path_options (arg);
660 break;
661
662 case OPT_fsign_zero:
663 gfc_option.flag_sign_zero = value;
664 break;
665
666 case OPT_ffpe_trap_:
667 gfc_handle_fpe_trap_option (arg);
668 break;
669
670 case OPT_std_f95:
671 gfc_option.allow_std = GFC_STD_F95_OBS | GFC_STD_F95 | GFC_STD_F77;
672 gfc_option.warn_std = GFC_STD_F95_OBS;
673 gfc_option.max_identifier_length = 31;
674 gfc_option.warn_ampersand = 1;
675 gfc_option.warn_tabs = 0;
676 break;
677
678 case OPT_std_f2003:
679 gfc_option.allow_std = GFC_STD_F95_OBS | GFC_STD_F77
680 | GFC_STD_F2003 | GFC_STD_F95;
681 gfc_option.warn_std = GFC_STD_F95_OBS;
682 gfc_option.max_continue_fixed = 255;
683 gfc_option.max_continue_free = 255;
684 gfc_option.max_identifier_length = 63;
685 gfc_option.warn_ampersand = 1;
686 gfc_option.warn_tabs = 0;
687 break;
688
689 case OPT_std_gnu:
690 set_default_std_flags ();
691 break;
692
693 case OPT_std_legacy:
694 set_default_std_flags ();
695 gfc_option.warn_std = 0;
696 break;
697
698 case OPT_Wnonstd_intrinsics:
699 gfc_option.warn_nonstd_intrinsics = value;
700 break;
701
702 case OPT_fshort_enums:
703 gfc_option.fshort_enums = 1;
704 break;
705
706 case OPT_fconvert_little_endian:
707 gfc_option.convert = GFC_CONVERT_LITTLE;
708 break;
709
710 case OPT_fconvert_big_endian:
711 gfc_option.convert = GFC_CONVERT_BIG;
712 break;
713
714 case OPT_fconvert_native:
715 gfc_option.convert = GFC_CONVERT_NATIVE;
716 break;
717
718 case OPT_fconvert_swap:
719 gfc_option.convert = GFC_CONVERT_SWAP;
720 break;
721
722 case OPT_frecord_marker_4:
723 gfc_option.record_marker = 4;
724 break;
725
726 case OPT_frecord_marker_8:
727 gfc_option.record_marker = 8;
728 break;
729
730 case OPT_fmax_subrecord_length_:
731 if (value > MAX_SUBRECORD_LENGTH)
732 gfc_fatal_error ("Maximum subrecord length cannot exceed %d",
733 MAX_SUBRECORD_LENGTH);
734
735 gfc_option.max_subrecord_length = value;
736 break;
737
738 case OPT_frecursive:
739 gfc_option.flag_recursive = 1;
740 break;
741 }
742
743 return result;
744 }
This page took 0.0718839999999999 seconds and 6 git commands to generate.