1 /* Parse and display command line options.
2 Copyright (C) 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008
3 Free Software Foundation, Inc.
4 Contributed by Andy Vaught
6 This file is part of GCC.
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
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
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/>. */
24 #include "coretypes.h"
31 #include "tree-inline.h"
35 gfc_option_t gfc_option
;
38 /* Set flags that control warnings and errors for different
39 Fortran standards to their default values. */
42 set_default_std_flags (void)
44 gfc_option
.allow_std
= GFC_STD_F95_OBS
| GFC_STD_F95_DEL
45 | GFC_STD_F2003
| GFC_STD_F2008
| GFC_STD_F95
| GFC_STD_F77
46 | GFC_STD_GNU
| GFC_STD_LEGACY
;
47 gfc_option
.warn_std
= GFC_STD_F95_DEL
| GFC_STD_LEGACY
;
50 /* Get ready for options handling. */
53 gfc_init_options (unsigned int argc ATTRIBUTE_UNUSED
,
54 const char **argv ATTRIBUTE_UNUSED
)
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
= 255;
62 gfc_option
.max_continue_free
= 255;
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
.dump_parse_tree
= 0;
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;
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;
90 /* Default value of flag_max_stack_var_size is set in gfc_post_options. */
91 gfc_option
.flag_max_stack_var_size
= -2;
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
= 0;
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 gfc_option
.flag_init_integer
= GFC_INIT_INTEGER_OFF
;
111 gfc_option
.flag_init_integer_value
= 0;
112 gfc_option
.flag_init_real
= GFC_INIT_REAL_OFF
;
113 gfc_option
.flag_init_logical
= GFC_INIT_LOGICAL_OFF
;
114 gfc_option
.flag_init_character
= GFC_INIT_CHARACTER_OFF
;
115 gfc_option
.flag_init_character_value
= (char)0;
119 /* Argument pointers cannot point to anything but their argument. */
120 flag_argument_noalias
= 3;
124 set_default_std_flags ();
126 gfc_option
.warn_nonstd_intrinsics
= 0;
128 /* -fshort-enums can be default on some targets. */
129 gfc_option
.fshort_enums
= targetm
.default_short_enums ();
135 /* Determine the source form from the filename extension. We assume
136 case insensitivity. */
138 static gfc_source_form
139 form_from_filename (const char *filename
)
143 const char *extension
;
144 gfc_source_form form
;
171 }; /* sentinel value */
173 gfc_source_form f_form
;
177 /* Find end of file name. Note, filename is either a NULL pointer or
178 a NUL terminated string. */
180 while (filename
[i
] != '\0')
183 /* Find last period. */
184 while (i
>= 0 && (filename
[i
] != '.'))
187 /* Did we see a file extension? */
189 return FORM_UNKNOWN
; /* Nope */
191 /* Get file extension and compare it to others. */
192 fileext
= &(filename
[i
]);
195 f_form
= FORM_UNKNOWN
;
199 if (strcasecmp (fileext
, exttype
[i
].extension
) == 0)
201 f_form
= exttype
[i
].form
;
205 while (exttype
[i
].form
!= FORM_UNKNOWN
);
211 /* Finalize commandline options. */
214 gfc_post_options (const char **pfilename
)
216 const char *filename
= *pfilename
, *canon_source_file
= NULL
;
220 /* Issue an error if -fwhole-program was used. */
221 if (flag_whole_program
)
222 gfc_fatal_error ("Option -fwhole-program is not supported for Fortran");
224 /* Verify the input file name. */
225 if (!filename
|| strcmp (filename
, "-") == 0)
230 if (gfc_option
.flag_preprocessed
)
232 /* For preprocessed files, if the first tokens are of the form # NUM.
233 handle the directives so we know the original file name. */
234 gfc_source_file
= gfc_read_orig_filename (filename
, &canon_source_file
);
235 if (gfc_source_file
== NULL
)
236 gfc_source_file
= filename
;
238 *pfilename
= gfc_source_file
;
241 gfc_source_file
= filename
;
243 if (canon_source_file
== NULL
)
244 canon_source_file
= gfc_source_file
;
246 /* Adds the path where the source file is to the list of include files. */
248 i
= strlen (canon_source_file
);
249 while (i
> 0 && !IS_DIR_SEPARATOR (canon_source_file
[i
]))
254 source_path
= alloca (i
+ 1);
255 memcpy (source_path
, canon_source_file
, i
);
257 gfc_add_include_path (source_path
, true);
260 gfc_add_include_path (".", true);
262 if (canon_source_file
!= gfc_source_file
)
263 gfc_free (CONST_CAST (char *, canon_source_file
));
265 /* Decide which form the file will be read in as. */
267 if (gfc_option
.source_form
!= FORM_UNKNOWN
)
268 gfc_current_form
= gfc_option
.source_form
;
271 gfc_current_form
= form_from_filename (filename
);
273 if (gfc_current_form
== FORM_UNKNOWN
)
275 gfc_current_form
= FORM_FREE
;
276 gfc_warning_now ("Reading file '%s' as free form",
277 (filename
[0] == '\0') ? "<stdin>" : filename
);
281 /* If the user specified -fd-lines-as-{code|comments} verify that we're
283 if (gfc_current_form
== FORM_FREE
)
285 if (gfc_option
.flag_d_lines
== 0)
286 gfc_warning_now ("'-fd-lines-as-comments' has no effect "
288 else if (gfc_option
.flag_d_lines
== 1)
289 gfc_warning_now ("'-fd-lines-as-code' has no effect in free form");
292 flag_inline_trees
= 1;
294 /* Use tree inlining. */
297 if (flag_inline_functions
)
298 flag_inline_trees
= 2;
300 /* If -pedantic, warn about the use of GNU extensions. */
301 if (pedantic
&& (gfc_option
.allow_std
& GFC_STD_GNU
) != 0)
302 gfc_option
.warn_std
|= GFC_STD_GNU
;
303 /* -std=legacy -pedantic is effectively -std=gnu. */
304 if (pedantic
&& (gfc_option
.allow_std
& GFC_STD_LEGACY
) != 0)
305 gfc_option
.warn_std
|= GFC_STD_F95_OBS
| GFC_STD_F95_DEL
| GFC_STD_LEGACY
;
307 /* If the user didn't explicitly specify -f(no)-second-underscore we
308 use it if we're trying to be compatible with f2c, and not
310 if (gfc_option
.flag_second_underscore
== -1)
311 gfc_option
.flag_second_underscore
= gfc_option
.flag_f2c
;
313 if (!gfc_option
.flag_automatic
&& gfc_option
.flag_max_stack_var_size
!= -2
314 && gfc_option
.flag_max_stack_var_size
!= 0)
315 gfc_warning_now ("Flag -fno-automatic overwrites -fmax-stack-var-size=%d",
316 gfc_option
.flag_max_stack_var_size
);
317 else if (!gfc_option
.flag_automatic
&& gfc_option
.flag_recursive
)
318 gfc_warning_now ("Flag -fno-automatic overwrites -frecursive");
319 else if (!gfc_option
.flag_automatic
&& gfc_option
.flag_openmp
)
320 gfc_warning_now ("Flag -fno-automatic overwrites -frecursive implied by "
322 else if (gfc_option
.flag_max_stack_var_size
!= -2
323 && gfc_option
.flag_recursive
)
324 gfc_warning_now ("Flag -frecursive overwrites -fmax-stack-var-size=%d",
325 gfc_option
.flag_max_stack_var_size
);
326 else if (gfc_option
.flag_max_stack_var_size
!= -2
327 && gfc_option
.flag_openmp
)
328 gfc_warning_now ("Flag -fmax-stack-var-size=%d overwrites -frecursive "
329 "implied by -fopenmp",
330 gfc_option
.flag_max_stack_var_size
);
332 /* Implied -frecursive; implemented as -fmax-stack-var-size=-1. */
333 if (gfc_option
.flag_max_stack_var_size
== -2 && gfc_option
.flag_openmp
)
334 gfc_option
.flag_max_stack_var_size
= -1;
337 if (gfc_option
.flag_max_stack_var_size
== -2)
338 gfc_option
.flag_max_stack_var_size
= 32768;
340 /* Implement -frecursive as -fmax-stack-var-size=-1. */
341 if (gfc_option
.flag_recursive
)
342 gfc_option
.flag_max_stack_var_size
= -1;
344 /* Implement -fno-automatic as -fmax-stack-var-size=0. */
345 if (!gfc_option
.flag_automatic
)
346 gfc_option
.flag_max_stack_var_size
= 0;
350 gfc_option
.warn_ampersand
= 1;
351 gfc_option
.warn_tabs
= 0;
354 if (gfc_option
.flag_all_intrinsics
)
355 gfc_option
.warn_nonstd_intrinsics
= 0;
361 /* Set the options for -Wall. */
364 set_Wall (int setting
)
366 gfc_option
.warn_aliasing
= setting
;
367 gfc_option
.warn_ampersand
= setting
;
368 gfc_option
.warn_line_truncation
= setting
;
369 gfc_option
.warn_nonstd_intrinsics
= setting
;
370 gfc_option
.warn_surprising
= setting
;
371 gfc_option
.warn_tabs
= !setting
;
372 gfc_option
.warn_underflow
= setting
;
373 gfc_option
.warn_character_truncation
= setting
;
375 set_Wunused (setting
);
376 warn_return_type
= setting
;
377 warn_switch
= setting
;
379 /* We save the value of warn_uninitialized, since if they put
380 -Wuninitialized on the command line, we need to generate a
381 warning about not using it without also specifying -O. */
383 warn_uninitialized
= 0;
384 else if (warn_uninitialized
!= 1)
385 warn_uninitialized
= 2;
390 gfc_handle_module_path_options (const char *arg
)
393 if (gfc_option
.module_dir
!= NULL
)
394 gfc_fatal_error ("gfortran: Only one -J option allowed");
396 gfc_option
.module_dir
= (char *) gfc_getmem (strlen (arg
) + 2);
397 strcpy (gfc_option
.module_dir
, arg
);
398 strcat (gfc_option
.module_dir
, "/");
400 gfc_add_include_path (gfc_option
.module_dir
, true);
405 gfc_handle_fpe_trap_option (const char *arg
)
407 int result
, pos
= 0, n
;
408 static const char * const exception
[] = { "invalid", "denormal", "zero",
409 "overflow", "underflow",
411 static const int opt_exception
[] = { GFC_FPE_INVALID
, GFC_FPE_DENORMAL
,
412 GFC_FPE_ZERO
, GFC_FPE_OVERFLOW
,
413 GFC_FPE_UNDERFLOW
, GFC_FPE_PRECISION
,
421 while (arg
[pos
] && arg
[pos
] != ',')
425 for (n
= 0; exception
[n
] != NULL
; n
++)
427 if (exception
[n
] && strncmp (exception
[n
], arg
, pos
) == 0)
429 gfc_option
.fpe
|= opt_exception
[n
];
437 gfc_fatal_error ("Argument to -ffpe-trap is not valid: %s", arg
);
442 /* Handle command-line options. Returns 0 if unrecognized, 1 if
443 recognized and handled. */
446 gfc_handle_option (size_t scode
, const char *arg
, int value
)
449 enum opt_code code
= (enum opt_code
) scode
;
451 /* Ignore file names. */
466 gfc_option
.warn_aliasing
= value
;
470 gfc_option
.warn_ampersand
= value
;
473 case OPT_Wcharacter_truncation
:
474 gfc_option
.warn_character_truncation
= value
;
477 case OPT_Wconversion
:
478 gfc_option
.warn_conversion
= value
;
481 case OPT_Wimplicit_interface
:
482 gfc_option
.warn_implicit_interface
= value
;
485 case OPT_Wline_truncation
:
486 gfc_option
.warn_line_truncation
= value
;
489 case OPT_Wreturn_type
:
490 warn_return_type
= value
;
493 case OPT_Wsurprising
:
494 gfc_option
.warn_surprising
= value
;
498 gfc_option
.warn_tabs
= value
;
502 gfc_option
.warn_underflow
= value
;
505 case OPT_fall_intrinsics
:
506 gfc_option
.flag_all_intrinsics
= 1;
510 gfc_option
.flag_automatic
= value
;
513 case OPT_fallow_leading_underscore
:
514 gfc_option
.flag_allow_leading_underscore
= value
;
518 gfc_option
.flag_backslash
= value
;
522 gfc_option
.flag_backtrace
= value
;
526 gfc_option
.flag_dump_core
= value
;
529 case OPT_fcray_pointer
:
530 gfc_option
.flag_cray_pointer
= value
;
534 gfc_option
.flag_f2c
= value
;
538 gfc_option
.flag_dollar_ok
= value
;
541 case OPT_fexternal_blas
:
542 gfc_option
.flag_external_blas
= value
;
545 case OPT_fblas_matmul_limit_
:
546 gfc_option
.blas_matmul_limit
= value
;
549 case OPT_fd_lines_as_code
:
550 gfc_option
.flag_d_lines
= 1;
553 case OPT_fd_lines_as_comments
:
554 gfc_option
.flag_d_lines
= 0;
557 case OPT_fdump_parse_tree
:
558 gfc_option
.dump_parse_tree
= value
;
561 case OPT_ffixed_form
:
562 gfc_option
.source_form
= FORM_FIXED
;
565 case OPT_ffixed_line_length_none
:
566 gfc_option
.fixed_line_length
= 0;
569 case OPT_ffixed_line_length_
:
570 if (value
!= 0 && value
< 7)
571 gfc_fatal_error ("Fixed line length must be at least seven.");
572 gfc_option
.fixed_line_length
= value
;
576 gfc_option
.source_form
= FORM_FREE
;
580 gfc_option
.flag_openmp
= value
;
583 case OPT_ffree_line_length_none
:
584 gfc_option
.free_line_length
= 0;
587 case OPT_ffree_line_length_
:
588 if (value
!= 0 && value
< 4)
589 gfc_fatal_error ("Free line length must be at least three.");
590 gfc_option
.free_line_length
= value
;
593 case OPT_funderscoring
:
594 gfc_option
.flag_underscoring
= value
;
597 case OPT_fsecond_underscore
:
598 gfc_option
.flag_second_underscore
= value
;
601 case OPT_static_libgfortran
:
602 #ifndef HAVE_LD_STATIC_DYNAMIC
603 gfc_fatal_error ("-static-libgfortran is not supported in this "
608 case OPT_fimplicit_none
:
609 gfc_option
.flag_implicit_none
= value
;
612 case OPT_fintrinsic_modules_path
:
613 gfc_add_include_path (arg
, false);
614 gfc_add_intrinsic_modules_path (arg
);
617 case OPT_fmax_errors_
:
618 gfc_option
.max_errors
= value
;
621 case OPT_fmax_stack_var_size_
:
622 gfc_option
.flag_max_stack_var_size
= value
;
625 case OPT_fmodule_private
:
626 gfc_option
.flag_module_private
= value
;
629 case OPT_frange_check
:
630 gfc_option
.flag_range_check
= value
;
633 case OPT_fpack_derived
:
634 gfc_option
.flag_pack_derived
= value
;
637 case OPT_frepack_arrays
:
638 gfc_option
.flag_repack_arrays
= value
;
641 case OPT_fpreprocessed
:
642 gfc_option
.flag_preprocessed
= value
;
645 case OPT_fmax_identifier_length_
:
646 if (value
> GFC_MAX_SYMBOL_LEN
)
647 gfc_fatal_error ("Maximum supported identifier length is %d",
649 gfc_option
.max_identifier_length
= value
;
652 case OPT_fdefault_integer_8
:
653 gfc_option
.flag_default_integer
= value
;
656 case OPT_fdefault_real_8
:
657 gfc_option
.flag_default_real
= value
;
660 case OPT_fdefault_double_8
:
661 gfc_option
.flag_default_double
= value
;
664 case OPT_finit_local_zero
:
665 gfc_option
.flag_init_integer
= GFC_INIT_INTEGER_ON
;
666 gfc_option
.flag_init_integer_value
= 0;
667 gfc_option
.flag_init_real
= GFC_INIT_REAL_ZERO
;
668 gfc_option
.flag_init_logical
= GFC_INIT_LOGICAL_FALSE
;
669 gfc_option
.flag_init_character
= GFC_INIT_CHARACTER_ON
;
670 gfc_option
.flag_init_character_value
= (char)0;
673 case OPT_finit_logical_
:
674 if (!strcasecmp (arg
, "false"))
675 gfc_option
.flag_init_logical
= GFC_INIT_LOGICAL_FALSE
;
676 else if (!strcasecmp (arg
, "true"))
677 gfc_option
.flag_init_logical
= GFC_INIT_LOGICAL_TRUE
;
679 gfc_fatal_error ("Unrecognized option to -finit-logical: %s",
683 case OPT_finit_real_
:
684 if (!strcasecmp (arg
, "zero"))
685 gfc_option
.flag_init_real
= GFC_INIT_REAL_ZERO
;
686 else if (!strcasecmp (arg
, "nan"))
687 gfc_option
.flag_init_real
= GFC_INIT_REAL_NAN
;
688 else if (!strcasecmp (arg
, "inf"))
689 gfc_option
.flag_init_real
= GFC_INIT_REAL_INF
;
690 else if (!strcasecmp (arg
, "-inf"))
691 gfc_option
.flag_init_real
= GFC_INIT_REAL_NEG_INF
;
693 gfc_fatal_error ("Unrecognized option to -finit-real: %s",
697 case OPT_finit_integer_
:
698 gfc_option
.flag_init_integer
= GFC_INIT_INTEGER_ON
;
699 gfc_option
.flag_init_integer_value
= atoi (arg
);
702 case OPT_finit_character_
:
703 if (value
>= 0 && value
<= 127)
705 gfc_option
.flag_init_character
= GFC_INIT_CHARACTER_ON
;
706 gfc_option
.flag_init_character_value
= (char)value
;
709 gfc_fatal_error ("The value of n in -finit-character=n must be "
710 "between 0 and 127");
714 gfc_add_include_path (arg
, true);
718 gfc_handle_module_path_options (arg
);
722 gfc_option
.flag_sign_zero
= value
;
726 gfc_handle_fpe_trap_option (arg
);
730 gfc_option
.allow_std
= GFC_STD_F95_OBS
| GFC_STD_F95
| GFC_STD_F77
;
731 gfc_option
.warn_std
= GFC_STD_F95_OBS
;
732 gfc_option
.max_continue_fixed
= 19;
733 gfc_option
.max_continue_free
= 39;
734 gfc_option
.max_identifier_length
= 31;
735 gfc_option
.warn_ampersand
= 1;
736 gfc_option
.warn_tabs
= 0;
740 gfc_option
.allow_std
= GFC_STD_F95_OBS
| GFC_STD_F77
741 | GFC_STD_F2003
| GFC_STD_F95
;
742 gfc_option
.warn_std
= GFC_STD_F95_OBS
;
743 gfc_option
.max_identifier_length
= 63;
744 gfc_option
.warn_ampersand
= 1;
745 gfc_option
.warn_tabs
= 0;
749 gfc_option
.allow_std
= GFC_STD_F95_OBS
| GFC_STD_F77
750 | GFC_STD_F2003
| GFC_STD_F95
| GFC_STD_F2008
;
751 gfc_option
.warn_std
= GFC_STD_F95_OBS
;
752 gfc_option
.max_identifier_length
= 63;
753 gfc_option
.warn_ampersand
= 1;
754 gfc_option
.warn_tabs
= 0;
758 set_default_std_flags ();
762 set_default_std_flags ();
763 gfc_option
.warn_std
= 0;
766 case OPT_Wnonstd_intrinsics
:
767 gfc_option
.warn_nonstd_intrinsics
= value
;
770 case OPT_fshort_enums
:
771 gfc_option
.fshort_enums
= 1;
774 case OPT_fconvert_little_endian
:
775 gfc_option
.convert
= GFC_CONVERT_LITTLE
;
778 case OPT_fconvert_big_endian
:
779 gfc_option
.convert
= GFC_CONVERT_BIG
;
782 case OPT_fconvert_native
:
783 gfc_option
.convert
= GFC_CONVERT_NATIVE
;
786 case OPT_fconvert_swap
:
787 gfc_option
.convert
= GFC_CONVERT_SWAP
;
790 case OPT_frecord_marker_4
:
791 gfc_option
.record_marker
= 4;
794 case OPT_frecord_marker_8
:
795 gfc_option
.record_marker
= 8;
798 case OPT_fmax_subrecord_length_
:
799 if (value
> MAX_SUBRECORD_LENGTH
)
800 gfc_fatal_error ("Maximum subrecord length cannot exceed %d",
801 MAX_SUBRECORD_LENGTH
);
803 gfc_option
.max_subrecord_length
= value
;
807 gfc_option
.flag_recursive
= 1;