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
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_F95 | GFC_STD_F77 | GFC_STD_GNU
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 = 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;
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 = 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;
113 /* Argument pointers cannot point to anything but their argument. */
114 flag_argument_noalias = 3;
118 set_default_std_flags ();
120 gfc_option.warn_nonstd_intrinsics = 0;
122 /* -fshort-enums can be default on some targets. */
123 gfc_option.fshort_enums = targetm.default_short_enums ();
129 /* Determine the source form from the filename extension. We assume
130 case insensitivity. */
132 static gfc_source_form
133 form_from_filename (const char *filename)
137 const char *extension;
138 gfc_source_form form;
159 }; /* sentinel value */
161 gfc_source_form f_form;
165 /* Find end of file name. Note, filename is either a NULL pointer or
166 a NUL terminated string. */
168 while (filename[i] != '\0')
171 /* Find last period. */
172 while (i >= 0 && (filename[i] != '.'))
175 /* Did we see a file extension? */
177 return FORM_UNKNOWN; /* Nope */
179 /* Get file extension and compare it to others. */
180 fileext = &(filename[i]);
183 f_form = FORM_UNKNOWN;
187 if (strcasecmp (fileext, exttype[i].extension) == 0)
189 f_form = exttype[i].form;
193 while (exttype[i].form != FORM_UNKNOWN);
199 /* Finalize commandline options. */
202 gfc_post_options (const char **pfilename)
204 const char *filename = *pfilename, *canon_source_file = NULL;
208 /* Verify the input file name. */
209 if (!filename || strcmp (filename, "-") == 0)
214 if (gfc_option.flag_preprocessed)
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;
222 *pfilename = gfc_source_file;
225 gfc_source_file = filename;
227 if (canon_source_file == NULL)
228 canon_source_file = gfc_source_file;
230 /* Adds the path where the source file is to the list of include files. */
232 i = strlen (canon_source_file);
233 while (i > 0 && !IS_DIR_SEPARATOR (canon_source_file[i]))
238 source_path = alloca (i + 1);
239 memcpy (source_path, canon_source_file, i);
241 gfc_add_include_path (source_path, true);
244 gfc_add_include_path (".", true);
246 if (canon_source_file != gfc_source_file)
247 gfc_free (CONST_CAST (char *, canon_source_file));
249 /* Decide which form the file will be read in as. */
251 if (gfc_option.source_form != FORM_UNKNOWN)
252 gfc_current_form = gfc_option.source_form;
255 gfc_current_form = form_from_filename (filename);
257 if (gfc_current_form == FORM_UNKNOWN)
259 gfc_current_form = FORM_FREE;
260 gfc_warning_now ("Reading file '%s' as free form",
261 (filename[0] == '\0') ? "<stdin>" : filename);
265 /* If the user specified -fd-lines-as-{code|comments} verify that we're
267 if (gfc_current_form == FORM_FREE)
269 if (gfc_option.flag_d_lines == 0)
270 gfc_warning_now ("'-fd-lines-as-comments' has no effect "
272 else if (gfc_option.flag_d_lines == 1)
273 gfc_warning_now ("'-fd-lines-as-code' has no effect in free form");
276 flag_inline_trees = 1;
278 /* Use tree inlining. */
281 if (flag_inline_functions)
282 flag_inline_trees = 2;
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;
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
294 if (gfc_option.flag_second_underscore == -1)
295 gfc_option.flag_second_underscore = gfc_option.flag_f2c;
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 "
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);
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;
321 if (gfc_option.flag_max_stack_var_size == -2)
322 gfc_option.flag_max_stack_var_size = 32768;
324 /* Implement -frecursive as -fmax-stack-var-size=-1. */
325 if (gfc_option.flag_recursive)
326 gfc_option.flag_max_stack_var_size = -1;
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;
334 gfc_option.warn_ampersand = 1;
335 gfc_option.warn_tabs = 0;
338 if (gfc_option.flag_all_intrinsics)
339 gfc_option.warn_nonstd_intrinsics = 0;
345 /* Set the options for -Wall. */
348 set_Wall (int setting)
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;
359 set_Wunused (setting);
360 warn_return_type = setting;
361 warn_switch = setting;
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. */
367 warn_uninitialized = 0;
368 else if (warn_uninitialized != 1)
369 warn_uninitialized = 2;
374 gfc_handle_module_path_options (const char *arg)
377 if (gfc_option.module_dir != NULL)
379 gfc_status ("gfortran: Only one -M option allowed\n");
385 gfc_status ("gfortran: Directory required after -M\n");
389 gfc_option.module_dir = (char *) gfc_getmem (strlen (arg) + 2);
390 strcpy (gfc_option.module_dir, arg);
391 strcat (gfc_option.module_dir, "/");
393 gfc_add_include_path (gfc_option.module_dir, true);
398 gfc_handle_fpe_trap_option (const char *arg)
400 int result, pos = 0, n;
401 static const char * const exception[] = { "invalid", "denormal", "zero",
402 "overflow", "underflow",
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,
414 while (arg[pos] && arg[pos] != ',')
418 for (n = 0; exception[n] != NULL; n++)
420 if (exception[n] && strncmp (exception[n], arg, pos) == 0)
422 gfc_option.fpe |= opt_exception[n];
430 gfc_fatal_error ("Argument to -ffpe-trap is not valid: %s", arg);
435 /* Handle command-line options. Returns 0 if unrecognized, 1 if
436 recognized and handled. */
439 gfc_handle_option (size_t scode, const char *arg, int value)
442 enum opt_code code = (enum opt_code) scode;
444 /* Ignore file names. */
459 gfc_option.warn_aliasing = value;
463 gfc_option.warn_ampersand = value;
466 case OPT_Wcharacter_truncation:
467 gfc_option.warn_character_truncation = value;
470 case OPT_Wconversion:
471 gfc_option.warn_conversion = value;
474 case OPT_Wimplicit_interface:
475 gfc_option.warn_implicit_interface = value;
478 case OPT_Wline_truncation:
479 gfc_option.warn_line_truncation = value;
482 case OPT_Wsurprising:
483 gfc_option.warn_surprising = value;
487 gfc_option.warn_tabs = value;
491 gfc_option.warn_underflow = value;
494 case OPT_fall_intrinsics:
495 gfc_option.flag_all_intrinsics = 1;
499 gfc_option.flag_automatic = value;
502 case OPT_fallow_leading_underscore:
503 gfc_option.flag_allow_leading_underscore = value;
507 gfc_option.flag_backslash = value;
511 gfc_option.flag_backtrace = value;
515 gfc_option.flag_dump_core = value;
518 case OPT_fcray_pointer:
519 gfc_option.flag_cray_pointer = value;
523 gfc_option.flag_f2c = value;
527 gfc_option.flag_dollar_ok = value;
530 case OPT_fexternal_blas:
531 gfc_option.flag_external_blas = value;
534 case OPT_fblas_matmul_limit_:
535 gfc_option.blas_matmul_limit = value;
538 case OPT_fd_lines_as_code:
539 gfc_option.flag_d_lines = 1;
542 case OPT_fd_lines_as_comments:
543 gfc_option.flag_d_lines = 0;
546 case OPT_fdump_parse_tree:
547 gfc_option.verbose = value;
550 case OPT_ffixed_form:
551 gfc_option.source_form = FORM_FIXED;
554 case OPT_ffixed_line_length_none:
555 gfc_option.fixed_line_length = 0;
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;
565 gfc_option.source_form = FORM_FREE;
569 gfc_option.flag_openmp = value;
572 case OPT_ffree_line_length_none:
573 gfc_option.free_line_length = 0;
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;
582 case OPT_funderscoring:
583 gfc_option.flag_underscoring = value;
586 case OPT_fsecond_underscore:
587 gfc_option.flag_second_underscore = value;
590 case OPT_static_libgfortran:
591 #ifndef HAVE_LD_STATIC_DYNAMIC
592 gfc_fatal_error ("-static-libgfortran is not supported in this "
597 case OPT_fimplicit_none:
598 gfc_option.flag_implicit_none = value;
601 case OPT_fintrinsic_modules_path:
602 gfc_add_include_path (arg, false);
603 gfc_add_intrinsic_modules_path (arg);
606 case OPT_fmax_errors_:
607 gfc_option.max_errors = value;
610 case OPT_fmax_stack_var_size_:
611 gfc_option.flag_max_stack_var_size = value;
614 case OPT_fmodule_private:
615 gfc_option.flag_module_private = value;
618 case OPT_frange_check:
619 gfc_option.flag_range_check = value;
622 case OPT_fpack_derived:
623 gfc_option.flag_pack_derived = value;
626 case OPT_frepack_arrays:
627 gfc_option.flag_repack_arrays = value;
630 case OPT_fpreprocessed:
631 gfc_option.flag_preprocessed = value;
634 case OPT_fmax_identifier_length_:
635 if (value > GFC_MAX_SYMBOL_LEN)
636 gfc_fatal_error ("Maximum supported identifier length is %d",
638 gfc_option.max_identifier_length = value;
641 case OPT_fdefault_integer_8:
642 gfc_option.flag_default_integer = value;
645 case OPT_fdefault_real_8:
646 gfc_option.flag_default_real = value;
649 case OPT_fdefault_double_8:
650 gfc_option.flag_default_double = value;
654 gfc_add_include_path (arg, true);
659 gfc_handle_module_path_options (arg);
663 gfc_option.flag_sign_zero = value;
667 gfc_handle_fpe_trap_option (arg);
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;
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;
690 set_default_std_flags ();
694 set_default_std_flags ();
695 gfc_option.warn_std = 0;
698 case OPT_Wnonstd_intrinsics:
699 gfc_option.warn_nonstd_intrinsics = value;
702 case OPT_fshort_enums:
703 gfc_option.fshort_enums = 1;
706 case OPT_fconvert_little_endian:
707 gfc_option.convert = GFC_CONVERT_LITTLE;
710 case OPT_fconvert_big_endian:
711 gfc_option.convert = GFC_CONVERT_BIG;
714 case OPT_fconvert_native:
715 gfc_option.convert = GFC_CONVERT_NATIVE;
718 case OPT_fconvert_swap:
719 gfc_option.convert = GFC_CONVERT_SWAP;
722 case OPT_frecord_marker_4:
723 gfc_option.record_marker = 4;
726 case OPT_frecord_marker_8:
727 gfc_option.record_marker = 8;
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);
735 gfc_option.max_subrecord_length = value;
739 gfc_option.flag_recursive = 1;