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"
36 gfc_option_t gfc_option;
39 /* Set flags that control warnings and errors for different
40 Fortran standards to their default values. */
43 set_default_std_flags (void)
45 gfc_option.allow_std = GFC_STD_F95_OBS | GFC_STD_F95_DEL
46 | GFC_STD_F2003 | GFC_STD_F2008 | GFC_STD_F95 | GFC_STD_F77
47 | GFC_STD_GNU | GFC_STD_LEGACY;
48 gfc_option.warn_std = GFC_STD_F95_DEL | GFC_STD_LEGACY;
51 /* Get ready for options handling. */
54 gfc_init_options (unsigned int argc, const char **argv)
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_array_temp = 0;
73 gfc_option.warn_conversion = 0;
74 gfc_option.warn_implicit_interface = 0;
75 gfc_option.warn_line_truncation = 0;
76 gfc_option.warn_surprising = 0;
77 gfc_option.warn_tabs = 1;
78 gfc_option.warn_underflow = 1;
79 gfc_option.max_errors = 25;
81 gfc_option.flag_all_intrinsics = 0;
82 gfc_option.flag_default_double = 0;
83 gfc_option.flag_default_integer = 0;
84 gfc_option.flag_default_real = 0;
85 gfc_option.flag_dollar_ok = 0;
86 gfc_option.flag_underscoring = 1;
87 gfc_option.flag_f2c = 0;
88 gfc_option.flag_second_underscore = -1;
89 gfc_option.flag_implicit_none = 0;
91 /* Default value of flag_max_stack_var_size is set in gfc_post_options. */
92 gfc_option.flag_max_stack_var_size = -2;
94 gfc_option.flag_range_check = 1;
95 gfc_option.flag_pack_derived = 0;
96 gfc_option.flag_repack_arrays = 0;
97 gfc_option.flag_preprocessed = 0;
98 gfc_option.flag_automatic = 1;
99 gfc_option.flag_backslash = 0;
100 gfc_option.flag_module_private = 0;
101 gfc_option.flag_backtrace = 0;
102 gfc_option.flag_allow_leading_underscore = 0;
103 gfc_option.flag_dump_core = 0;
104 gfc_option.flag_external_blas = 0;
105 gfc_option.blas_matmul_limit = 30;
106 gfc_option.flag_cray_pointer = 0;
107 gfc_option.flag_d_lines = -1;
108 gfc_option.flag_openmp = 0;
109 gfc_option.flag_sign_zero = 1;
110 gfc_option.flag_recursive = 0;
111 gfc_option.flag_init_integer = GFC_INIT_INTEGER_OFF;
112 gfc_option.flag_init_integer_value = 0;
113 gfc_option.flag_init_real = GFC_INIT_REAL_OFF;
114 gfc_option.flag_init_logical = GFC_INIT_LOGICAL_OFF;
115 gfc_option.flag_init_character = GFC_INIT_CHARACTER_OFF;
116 gfc_option.flag_init_character_value = (char)0;
120 /* Argument pointers cannot point to anything but their argument. */
121 flag_argument_noalias = 3;
125 set_default_std_flags ();
127 gfc_option.warn_nonstd_intrinsics = 0;
129 /* -fshort-enums can be default on some targets. */
130 gfc_option.fshort_enums = targetm.default_short_enums ();
132 /* Initialize cpp-related options. */
133 gfc_cpp_init_options(argc, argv);
139 /* Determine the source form from the filename extension. We assume
140 case insensitivity. */
142 static gfc_source_form
143 form_from_filename (const char *filename)
147 const char *extension;
148 gfc_source_form form;
175 }; /* sentinel value */
177 gfc_source_form f_form;
181 /* Find end of file name. Note, filename is either a NULL pointer or
182 a NUL terminated string. */
184 while (filename[i] != '\0')
187 /* Find last period. */
188 while (i >= 0 && (filename[i] != '.'))
191 /* Did we see a file extension? */
193 return FORM_UNKNOWN; /* Nope */
195 /* Get file extension and compare it to others. */
196 fileext = &(filename[i]);
199 f_form = FORM_UNKNOWN;
203 if (strcasecmp (fileext, exttype[i].extension) == 0)
205 f_form = exttype[i].form;
209 while (exttype[i].form != FORM_UNKNOWN);
215 /* Finalize commandline options. */
218 gfc_post_options (const char **pfilename)
220 const char *filename = *pfilename, *canon_source_file = NULL;
224 /* Issue an error if -fwhole-program was used. */
225 if (flag_whole_program)
226 gfc_fatal_error ("Option -fwhole-program is not supported for Fortran");
228 /* Verify the input file name. */
229 if (!filename || strcmp (filename, "-") == 0)
234 if (gfc_option.flag_preprocessed)
236 /* For preprocessed files, if the first tokens are of the form # NUM.
237 handle the directives so we know the original file name. */
238 gfc_source_file = gfc_read_orig_filename (filename, &canon_source_file);
239 if (gfc_source_file == NULL)
240 gfc_source_file = filename;
242 *pfilename = gfc_source_file;
245 gfc_source_file = filename;
247 if (canon_source_file == NULL)
248 canon_source_file = gfc_source_file;
250 /* Adds the path where the source file is to the list of include files. */
252 i = strlen (canon_source_file);
253 while (i > 0 && !IS_DIR_SEPARATOR (canon_source_file[i]))
258 source_path = (char *) alloca (i + 1);
259 memcpy (source_path, canon_source_file, i);
261 gfc_add_include_path (source_path, true);
264 gfc_add_include_path (".", true);
266 if (canon_source_file != gfc_source_file)
267 gfc_free (CONST_CAST (char *, canon_source_file));
269 /* Decide which form the file will be read in as. */
271 if (gfc_option.source_form != FORM_UNKNOWN)
272 gfc_current_form = gfc_option.source_form;
275 gfc_current_form = form_from_filename (filename);
277 if (gfc_current_form == FORM_UNKNOWN)
279 gfc_current_form = FORM_FREE;
280 gfc_warning_now ("Reading file '%s' as free form",
281 (filename[0] == '\0') ? "<stdin>" : filename);
285 /* If the user specified -fd-lines-as-{code|comments} verify that we're
287 if (gfc_current_form == FORM_FREE)
289 if (gfc_option.flag_d_lines == 0)
290 gfc_warning_now ("'-fd-lines-as-comments' has no effect "
292 else if (gfc_option.flag_d_lines == 1)
293 gfc_warning_now ("'-fd-lines-as-code' has no effect in free form");
296 flag_inline_trees = 1;
298 /* Use tree inlining. */
301 if (flag_inline_functions)
302 flag_inline_trees = 2;
304 /* If -pedantic, warn about the use of GNU extensions. */
305 if (pedantic && (gfc_option.allow_std & GFC_STD_GNU) != 0)
306 gfc_option.warn_std |= GFC_STD_GNU;
307 /* -std=legacy -pedantic is effectively -std=gnu. */
308 if (pedantic && (gfc_option.allow_std & GFC_STD_LEGACY) != 0)
309 gfc_option.warn_std |= GFC_STD_F95_OBS | GFC_STD_F95_DEL | GFC_STD_LEGACY;
311 /* If the user didn't explicitly specify -f(no)-second-underscore we
312 use it if we're trying to be compatible with f2c, and not
314 if (gfc_option.flag_second_underscore == -1)
315 gfc_option.flag_second_underscore = gfc_option.flag_f2c;
317 if (!gfc_option.flag_automatic && gfc_option.flag_max_stack_var_size != -2
318 && gfc_option.flag_max_stack_var_size != 0)
319 gfc_warning_now ("Flag -fno-automatic overwrites -fmax-stack-var-size=%d",
320 gfc_option.flag_max_stack_var_size);
321 else if (!gfc_option.flag_automatic && gfc_option.flag_recursive)
322 gfc_warning_now ("Flag -fno-automatic overwrites -frecursive");
323 else if (!gfc_option.flag_automatic && gfc_option.flag_openmp)
324 gfc_warning_now ("Flag -fno-automatic overwrites -frecursive implied by "
326 else if (gfc_option.flag_max_stack_var_size != -2
327 && gfc_option.flag_recursive)
328 gfc_warning_now ("Flag -frecursive overwrites -fmax-stack-var-size=%d",
329 gfc_option.flag_max_stack_var_size);
330 else if (gfc_option.flag_max_stack_var_size != -2
331 && gfc_option.flag_openmp)
332 gfc_warning_now ("Flag -fmax-stack-var-size=%d overwrites -frecursive "
333 "implied by -fopenmp",
334 gfc_option.flag_max_stack_var_size);
336 /* Implied -frecursive; implemented as -fmax-stack-var-size=-1. */
337 if (gfc_option.flag_max_stack_var_size == -2 && gfc_option.flag_openmp)
338 gfc_option.flag_max_stack_var_size = -1;
341 if (gfc_option.flag_max_stack_var_size == -2)
342 gfc_option.flag_max_stack_var_size = 32768;
344 /* Implement -frecursive as -fmax-stack-var-size=-1. */
345 if (gfc_option.flag_recursive)
346 gfc_option.flag_max_stack_var_size = -1;
348 /* Implement -fno-automatic as -fmax-stack-var-size=0. */
349 if (!gfc_option.flag_automatic)
350 gfc_option.flag_max_stack_var_size = 0;
354 gfc_option.warn_ampersand = 1;
355 gfc_option.warn_tabs = 0;
358 if (gfc_option.flag_all_intrinsics)
359 gfc_option.warn_nonstd_intrinsics = 0;
361 gfc_cpp_post_options ();
363 /* FIXME: return gfc_cpp_preprocess_only ();
365 The return value of this function indicates whether the
366 backend needs to be initialized. On -E, we don't need
367 the backend. However, if we return 'true' here, an
368 ICE occurs. Initializing the backend doesn't hurt much,
369 hence, for now we can live with it as is. */
374 /* Set the options for -Wall. */
377 set_Wall (int setting)
379 gfc_option.warn_aliasing = setting;
380 gfc_option.warn_ampersand = setting;
381 gfc_option.warn_line_truncation = setting;
382 gfc_option.warn_nonstd_intrinsics = setting;
383 gfc_option.warn_surprising = setting;
384 gfc_option.warn_tabs = !setting;
385 gfc_option.warn_underflow = setting;
386 gfc_option.warn_character_truncation = setting;
388 set_Wunused (setting);
389 warn_return_type = setting;
390 warn_switch = setting;
392 /* We save the value of warn_uninitialized, since if they put
393 -Wuninitialized on the command line, we need to generate a
394 warning about not using it without also specifying -O. */
396 warn_uninitialized = 0;
397 else if (warn_uninitialized != 1)
398 warn_uninitialized = 2;
403 gfc_handle_module_path_options (const char *arg)
406 if (gfc_option.module_dir != NULL)
407 gfc_fatal_error ("gfortran: Only one -J option allowed");
409 gfc_option.module_dir = (char *) gfc_getmem (strlen (arg) + 2);
410 strcpy (gfc_option.module_dir, arg);
411 strcat (gfc_option.module_dir, "/");
413 gfc_add_include_path (gfc_option.module_dir, true);
418 gfc_handle_fpe_trap_option (const char *arg)
420 int result, pos = 0, n;
421 static const char * const exception[] = { "invalid", "denormal", "zero",
422 "overflow", "underflow",
424 static const int opt_exception[] = { GFC_FPE_INVALID, GFC_FPE_DENORMAL,
425 GFC_FPE_ZERO, GFC_FPE_OVERFLOW,
426 GFC_FPE_UNDERFLOW, GFC_FPE_PRECISION,
434 while (arg[pos] && arg[pos] != ',')
438 for (n = 0; exception[n] != NULL; n++)
440 if (exception[n] && strncmp (exception[n], arg, pos) == 0)
442 gfc_option.fpe |= opt_exception[n];
450 gfc_fatal_error ("Argument to -ffpe-trap is not valid: %s", arg);
455 /* Handle command-line options. Returns 0 if unrecognized, 1 if
456 recognized and handled. */
459 gfc_handle_option (size_t scode, const char *arg, int value)
462 enum opt_code code = (enum opt_code) scode;
464 /* Ignore file names. */
468 if (gfc_cpp_handle_option (scode, arg, value) == 1)
482 gfc_option.warn_aliasing = value;
486 gfc_option.warn_ampersand = value;
489 case OPT_Warray_temporaries:
490 gfc_option.warn_array_temp = value;
493 case OPT_Wcharacter_truncation:
494 gfc_option.warn_character_truncation = value;
497 case OPT_Wconversion:
498 gfc_option.warn_conversion = value;
501 case OPT_Wimplicit_interface:
502 gfc_option.warn_implicit_interface = value;
505 case OPT_Wline_truncation:
506 gfc_option.warn_line_truncation = value;
509 case OPT_Wreturn_type:
510 warn_return_type = value;
513 case OPT_Wsurprising:
514 gfc_option.warn_surprising = value;
518 gfc_option.warn_tabs = value;
522 gfc_option.warn_underflow = value;
525 case OPT_fall_intrinsics:
526 gfc_option.flag_all_intrinsics = 1;
530 gfc_option.flag_automatic = value;
533 case OPT_fallow_leading_underscore:
534 gfc_option.flag_allow_leading_underscore = value;
538 gfc_option.flag_backslash = value;
542 gfc_option.flag_backtrace = value;
546 gfc_option.flag_dump_core = value;
549 case OPT_fcray_pointer:
550 gfc_option.flag_cray_pointer = value;
554 gfc_option.flag_f2c = value;
558 gfc_option.flag_dollar_ok = value;
561 case OPT_fexternal_blas:
562 gfc_option.flag_external_blas = value;
565 case OPT_fblas_matmul_limit_:
566 gfc_option.blas_matmul_limit = value;
569 case OPT_fd_lines_as_code:
570 gfc_option.flag_d_lines = 1;
573 case OPT_fd_lines_as_comments:
574 gfc_option.flag_d_lines = 0;
577 case OPT_fdump_parse_tree:
578 gfc_option.dump_parse_tree = value;
581 case OPT_ffixed_form:
582 gfc_option.source_form = FORM_FIXED;
585 case OPT_ffixed_line_length_none:
586 gfc_option.fixed_line_length = 0;
589 case OPT_ffixed_line_length_:
590 if (value != 0 && value < 7)
591 gfc_fatal_error ("Fixed line length must be at least seven.");
592 gfc_option.fixed_line_length = value;
596 gfc_option.source_form = FORM_FREE;
600 gfc_option.flag_openmp = value;
603 case OPT_ffree_line_length_none:
604 gfc_option.free_line_length = 0;
607 case OPT_ffree_line_length_:
608 if (value != 0 && value < 4)
609 gfc_fatal_error ("Free line length must be at least three.");
610 gfc_option.free_line_length = value;
613 case OPT_funderscoring:
614 gfc_option.flag_underscoring = value;
617 case OPT_fsecond_underscore:
618 gfc_option.flag_second_underscore = value;
621 case OPT_static_libgfortran:
622 #ifndef HAVE_LD_STATIC_DYNAMIC
623 gfc_fatal_error ("-static-libgfortran is not supported in this "
628 case OPT_fimplicit_none:
629 gfc_option.flag_implicit_none = value;
632 case OPT_fintrinsic_modules_path:
633 gfc_add_include_path (arg, false);
634 gfc_add_intrinsic_modules_path (arg);
637 case OPT_fmax_errors_:
638 gfc_option.max_errors = value;
641 case OPT_fmax_stack_var_size_:
642 gfc_option.flag_max_stack_var_size = value;
645 case OPT_fmodule_private:
646 gfc_option.flag_module_private = value;
649 case OPT_frange_check:
650 gfc_option.flag_range_check = value;
653 case OPT_fpack_derived:
654 gfc_option.flag_pack_derived = value;
657 case OPT_frepack_arrays:
658 gfc_option.flag_repack_arrays = value;
661 case OPT_fpreprocessed:
662 gfc_option.flag_preprocessed = value;
665 case OPT_fmax_identifier_length_:
666 if (value > GFC_MAX_SYMBOL_LEN)
667 gfc_fatal_error ("Maximum supported identifier length is %d",
669 gfc_option.max_identifier_length = value;
672 case OPT_fdefault_integer_8:
673 gfc_option.flag_default_integer = value;
676 case OPT_fdefault_real_8:
677 gfc_option.flag_default_real = value;
680 case OPT_fdefault_double_8:
681 gfc_option.flag_default_double = value;
684 case OPT_finit_local_zero:
685 gfc_option.flag_init_integer = GFC_INIT_INTEGER_ON;
686 gfc_option.flag_init_integer_value = 0;
687 gfc_option.flag_init_real = GFC_INIT_REAL_ZERO;
688 gfc_option.flag_init_logical = GFC_INIT_LOGICAL_FALSE;
689 gfc_option.flag_init_character = GFC_INIT_CHARACTER_ON;
690 gfc_option.flag_init_character_value = (char)0;
693 case OPT_finit_logical_:
694 if (!strcasecmp (arg, "false"))
695 gfc_option.flag_init_logical = GFC_INIT_LOGICAL_FALSE;
696 else if (!strcasecmp (arg, "true"))
697 gfc_option.flag_init_logical = GFC_INIT_LOGICAL_TRUE;
699 gfc_fatal_error ("Unrecognized option to -finit-logical: %s",
703 case OPT_finit_real_:
704 if (!strcasecmp (arg, "zero"))
705 gfc_option.flag_init_real = GFC_INIT_REAL_ZERO;
706 else if (!strcasecmp (arg, "nan"))
707 gfc_option.flag_init_real = GFC_INIT_REAL_NAN;
708 else if (!strcasecmp (arg, "inf"))
709 gfc_option.flag_init_real = GFC_INIT_REAL_INF;
710 else if (!strcasecmp (arg, "-inf"))
711 gfc_option.flag_init_real = GFC_INIT_REAL_NEG_INF;
713 gfc_fatal_error ("Unrecognized option to -finit-real: %s",
717 case OPT_finit_integer_:
718 gfc_option.flag_init_integer = GFC_INIT_INTEGER_ON;
719 gfc_option.flag_init_integer_value = atoi (arg);
722 case OPT_finit_character_:
723 if (value >= 0 && value <= 127)
725 gfc_option.flag_init_character = GFC_INIT_CHARACTER_ON;
726 gfc_option.flag_init_character_value = (char)value;
729 gfc_fatal_error ("The value of n in -finit-character=n must be "
730 "between 0 and 127");
734 gfc_add_include_path (arg, true);
738 gfc_handle_module_path_options (arg);
742 gfc_option.flag_sign_zero = value;
746 gfc_handle_fpe_trap_option (arg);
750 gfc_option.allow_std = GFC_STD_F95_OBS | GFC_STD_F95 | GFC_STD_F77;
751 gfc_option.warn_std = GFC_STD_F95_OBS;
752 gfc_option.max_continue_fixed = 19;
753 gfc_option.max_continue_free = 39;
754 gfc_option.max_identifier_length = 31;
755 gfc_option.warn_ampersand = 1;
756 gfc_option.warn_tabs = 0;
760 gfc_option.allow_std = GFC_STD_F95_OBS | GFC_STD_F77
761 | GFC_STD_F2003 | GFC_STD_F95;
762 gfc_option.warn_std = GFC_STD_F95_OBS;
763 gfc_option.max_identifier_length = 63;
764 gfc_option.warn_ampersand = 1;
765 gfc_option.warn_tabs = 0;
769 gfc_option.allow_std = GFC_STD_F95_OBS | GFC_STD_F77
770 | GFC_STD_F2003 | GFC_STD_F95 | GFC_STD_F2008;
771 gfc_option.warn_std = GFC_STD_F95_OBS;
772 gfc_option.max_identifier_length = 63;
773 gfc_option.warn_ampersand = 1;
774 gfc_option.warn_tabs = 0;
778 set_default_std_flags ();
782 set_default_std_flags ();
783 gfc_option.warn_std = 0;
786 case OPT_Wnonstd_intrinsics:
787 gfc_option.warn_nonstd_intrinsics = value;
790 case OPT_fshort_enums:
791 gfc_option.fshort_enums = 1;
794 case OPT_fconvert_little_endian:
795 gfc_option.convert = GFC_CONVERT_LITTLE;
798 case OPT_fconvert_big_endian:
799 gfc_option.convert = GFC_CONVERT_BIG;
802 case OPT_fconvert_native:
803 gfc_option.convert = GFC_CONVERT_NATIVE;
806 case OPT_fconvert_swap:
807 gfc_option.convert = GFC_CONVERT_SWAP;
810 case OPT_frecord_marker_4:
811 gfc_option.record_marker = 4;
814 case OPT_frecord_marker_8:
815 gfc_option.record_marker = 8;
818 case OPT_fmax_subrecord_length_:
819 if (value > MAX_SUBRECORD_LENGTH)
820 gfc_fatal_error ("Maximum subrecord length cannot exceed %d",
821 MAX_SUBRECORD_LENGTH);
823 gfc_option.max_subrecord_length = value;
827 gfc_option.flag_recursive = 1;