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 = 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;
89 gfc_option.flag_max_stack_var_size = 32768;
90 gfc_option.flag_range_check = 1;
91 gfc_option.flag_pack_derived = 0;
92 gfc_option.flag_repack_arrays = 0;
93 gfc_option.flag_preprocessed = 0;
94 gfc_option.flag_automatic = 1;
95 gfc_option.flag_backslash = 1;
96 gfc_option.flag_module_private = 0;
97 gfc_option.flag_backtrace = 0;
98 gfc_option.flag_allow_leading_underscore = 0;
99 gfc_option.flag_dump_core = 0;
100 gfc_option.flag_external_blas = 0;
101 gfc_option.blas_matmul_limit = 30;
102 gfc_option.flag_cray_pointer = 0;
103 gfc_option.flag_d_lines = -1;
104 gfc_option.flag_openmp = 0;
105 gfc_option.flag_sign_zero = 1;
109 /* Argument pointers cannot point to anything but their argument. */
110 flag_argument_noalias = 3;
114 set_default_std_flags ();
116 gfc_option.warn_nonstd_intrinsics = 0;
118 /* -fshort-enums can be default on some targets. */
119 gfc_option.fshort_enums = targetm.default_short_enums ();
125 /* Determine the source form from the filename extension. We assume
126 case insensitivity. */
128 static gfc_source_form
129 form_from_filename (const char *filename)
133 const char *extension;
134 gfc_source_form form;
155 }; /* sentinel value */
157 gfc_source_form f_form;
161 /* Find end of file name. Note, filename is either a NULL pointer or
162 a NUL terminated string. */
164 while (filename[i] != '\0')
167 /* Find last period. */
168 while (i >= 0 && (filename[i] != '.'))
171 /* Did we see a file extension? */
173 return FORM_UNKNOWN; /* Nope */
175 /* Get file extension and compare it to others. */
176 fileext = &(filename[i]);
179 f_form = FORM_UNKNOWN;
183 if (strcasecmp (fileext, exttype[i].extension) == 0)
185 f_form = exttype[i].form;
189 while (exttype[i].form != FORM_UNKNOWN);
195 /* Finalize commandline options. */
198 gfc_post_options (const char **pfilename)
200 const char *filename = *pfilename, *canon_source_file = NULL;
204 /* Verify the input file name. */
205 if (!filename || strcmp (filename, "-") == 0)
210 if (gfc_option.flag_preprocessed)
212 /* For preprocessed files, if the first tokens are of the form # NUM.
213 handle the directives so we know the original file name. */
214 gfc_source_file = gfc_read_orig_filename (filename, &canon_source_file);
215 if (gfc_source_file == NULL)
216 gfc_source_file = filename;
218 *pfilename = gfc_source_file;
221 gfc_source_file = filename;
223 if (canon_source_file == NULL)
224 canon_source_file = gfc_source_file;
226 /* Adds the path where the source file is to the list of include files. */
228 i = strlen (canon_source_file);
229 while (i > 0 && !IS_DIR_SEPARATOR (canon_source_file[i]))
234 source_path = alloca (i + 1);
235 memcpy (source_path, canon_source_file, i);
237 gfc_add_include_path (source_path, true);
240 gfc_add_include_path (".", true);
242 if (canon_source_file != gfc_source_file)
243 gfc_free (CONST_CAST (canon_source_file));
245 /* Decide which form the file will be read in as. */
247 if (gfc_option.source_form != FORM_UNKNOWN)
248 gfc_current_form = gfc_option.source_form;
251 gfc_current_form = form_from_filename (filename);
253 if (gfc_current_form == FORM_UNKNOWN)
255 gfc_current_form = FORM_FREE;
256 gfc_warning_now ("Reading file '%s' as free form",
257 (filename[0] == '\0') ? "<stdin>" : filename);
261 /* If the user specified -fd-lines-as-{code|comments} verify that we're
263 if (gfc_current_form == FORM_FREE)
265 if (gfc_option.flag_d_lines == 0)
266 gfc_warning_now ("'-fd-lines-as-comments' has no effect "
268 else if (gfc_option.flag_d_lines == 1)
269 gfc_warning_now ("'-fd-lines-as-code' has no effect in free form");
272 flag_inline_trees = 1;
274 /* Use tree inlining. */
277 if (flag_inline_functions)
278 flag_inline_trees = 2;
280 /* If -pedantic, warn about the use of GNU extensions. */
281 if (pedantic && (gfc_option.allow_std & GFC_STD_GNU) != 0)
282 gfc_option.warn_std |= GFC_STD_GNU;
283 /* -std=legacy -pedantic is effectively -std=gnu. */
284 if (pedantic && (gfc_option.allow_std & GFC_STD_LEGACY) != 0)
285 gfc_option.warn_std |= GFC_STD_F95_OBS | GFC_STD_F95_DEL | GFC_STD_LEGACY;
287 /* If the user didn't explicitly specify -f(no)-second-underscore we
288 use it if we're trying to be compatible with f2c, and not
290 if (gfc_option.flag_second_underscore == -1)
291 gfc_option.flag_second_underscore = gfc_option.flag_f2c;
293 /* Implement -fno-automatic as -fmax-stack-var-size=0. */
294 if (!gfc_option.flag_automatic)
295 gfc_option.flag_max_stack_var_size = 0;
299 gfc_option.warn_ampersand = 1;
300 gfc_option.warn_tabs = 0;
303 if (gfc_option.flag_all_intrinsics)
304 gfc_option.warn_nonstd_intrinsics = 0;
310 /* Set the options for -Wall. */
313 set_Wall (int setting)
315 gfc_option.warn_aliasing = setting;
316 gfc_option.warn_ampersand = setting;
317 gfc_option.warn_line_truncation = setting;
318 gfc_option.warn_nonstd_intrinsics = setting;
319 gfc_option.warn_surprising = setting;
320 gfc_option.warn_tabs = !setting;
321 gfc_option.warn_underflow = setting;
322 gfc_option.warn_character_truncation = setting;
324 set_Wunused (setting);
325 warn_return_type = setting;
326 warn_switch = setting;
328 /* We save the value of warn_uninitialized, since if they put
329 -Wuninitialized on the command line, we need to generate a
330 warning about not using it without also specifying -O. */
332 warn_uninitialized = 0;
333 else if (warn_uninitialized != 1)
334 warn_uninitialized = 2;
339 gfc_handle_module_path_options (const char *arg)
342 if (gfc_option.module_dir != NULL)
344 gfc_status ("gfortran: Only one -M option allowed\n");
350 gfc_status ("gfortran: Directory required after -M\n");
354 gfc_option.module_dir = (char *) gfc_getmem (strlen (arg) + 2);
355 strcpy (gfc_option.module_dir, arg);
356 strcat (gfc_option.module_dir, "/");
358 gfc_add_include_path (gfc_option.module_dir, true);
363 gfc_handle_fpe_trap_option (const char *arg)
365 int result, pos = 0, n;
366 static const char * const exception[] = { "invalid", "denormal", "zero",
367 "overflow", "underflow",
369 static const int opt_exception[] = { GFC_FPE_INVALID, GFC_FPE_DENORMAL,
370 GFC_FPE_ZERO, GFC_FPE_OVERFLOW,
371 GFC_FPE_UNDERFLOW, GFC_FPE_PRECISION,
379 while (arg[pos] && arg[pos] != ',')
383 for (n = 0; exception[n] != NULL; n++)
385 if (exception[n] && strncmp (exception[n], arg, pos) == 0)
387 gfc_option.fpe |= opt_exception[n];
395 gfc_fatal_error ("Argument to -ffpe-trap is not valid: %s", arg);
400 /* Handle command-line options. Returns 0 if unrecognized, 1 if
401 recognized and handled. */
404 gfc_handle_option (size_t scode, const char *arg, int value)
407 enum opt_code code = (enum opt_code) scode;
409 /* Ignore file names. */
424 gfc_option.warn_aliasing = value;
428 gfc_option.warn_ampersand = value;
431 case OPT_Wcharacter_truncation:
432 gfc_option.warn_character_truncation = value;
435 case OPT_Wconversion:
436 gfc_option.warn_conversion = value;
439 case OPT_Wimplicit_interface:
440 gfc_option.warn_implicit_interface = value;
443 case OPT_Wline_truncation:
444 gfc_option.warn_line_truncation = value;
447 case OPT_Wsurprising:
448 gfc_option.warn_surprising = value;
452 gfc_option.warn_tabs = value;
456 gfc_option.warn_underflow = value;
459 case OPT_fall_intrinsics:
460 gfc_option.flag_all_intrinsics = 1;
464 gfc_option.flag_automatic = value;
467 case OPT_fallow_leading_underscore:
468 gfc_option.flag_allow_leading_underscore = value;
472 gfc_option.flag_backslash = value;
476 gfc_option.flag_backtrace = value;
480 gfc_option.flag_dump_core = value;
483 case OPT_fcray_pointer:
484 gfc_option.flag_cray_pointer = value;
488 gfc_option.flag_f2c = value;
492 gfc_option.flag_dollar_ok = value;
495 case OPT_fexternal_blas:
496 gfc_option.flag_external_blas = value;
499 case OPT_fblas_matmul_limit_:
500 gfc_option.blas_matmul_limit = value;
503 case OPT_fd_lines_as_code:
504 gfc_option.flag_d_lines = 1;
507 case OPT_fd_lines_as_comments:
508 gfc_option.flag_d_lines = 0;
511 case OPT_fdump_parse_tree:
512 gfc_option.verbose = value;
515 case OPT_ffixed_form:
516 gfc_option.source_form = FORM_FIXED;
519 case OPT_ffixed_line_length_none:
520 gfc_option.fixed_line_length = 0;
523 case OPT_ffixed_line_length_:
524 if (value != 0 && value < 7)
525 gfc_fatal_error ("Fixed line length must be at least seven.");
526 gfc_option.fixed_line_length = value;
530 gfc_option.source_form = FORM_FREE;
534 gfc_option.flag_openmp = value;
537 case OPT_ffree_line_length_none:
538 gfc_option.free_line_length = 0;
541 case OPT_ffree_line_length_:
542 if (value != 0 && value < 4)
543 gfc_fatal_error ("Free line length must be at least three.");
544 gfc_option.free_line_length = value;
547 case OPT_funderscoring:
548 gfc_option.flag_underscoring = value;
551 case OPT_fsecond_underscore:
552 gfc_option.flag_second_underscore = value;
555 case OPT_static_libgfortran:
556 #ifndef HAVE_LD_STATIC_DYNAMIC
557 gfc_fatal_error ("-static-libgfortran is not supported in this "
562 case OPT_fimplicit_none:
563 gfc_option.flag_implicit_none = value;
566 case OPT_fintrinsic_modules_path:
567 gfc_add_include_path (arg, false);
568 gfc_add_intrinsic_modules_path (arg);
571 case OPT_fmax_errors_:
572 gfc_option.max_errors = value;
575 case OPT_fmax_stack_var_size_:
576 gfc_option.flag_max_stack_var_size = value;
579 case OPT_fmodule_private:
580 gfc_option.flag_module_private = value;
583 case OPT_frange_check:
584 gfc_option.flag_range_check = value;
587 case OPT_fpack_derived:
588 gfc_option.flag_pack_derived = value;
591 case OPT_frepack_arrays:
592 gfc_option.flag_repack_arrays = value;
595 case OPT_fpreprocessed:
596 gfc_option.flag_preprocessed = value;
599 case OPT_fmax_identifier_length_:
600 if (value > GFC_MAX_SYMBOL_LEN)
601 gfc_fatal_error ("Maximum supported identifier length is %d",
603 gfc_option.max_identifier_length = value;
606 case OPT_fdefault_integer_8:
607 gfc_option.flag_default_integer = value;
610 case OPT_fdefault_real_8:
611 gfc_option.flag_default_real = value;
614 case OPT_fdefault_double_8:
615 gfc_option.flag_default_double = value;
619 gfc_add_include_path (arg, true);
624 gfc_handle_module_path_options (arg);
628 gfc_option.flag_sign_zero = value;
632 gfc_handle_fpe_trap_option (arg);
636 gfc_option.allow_std = GFC_STD_F95_OBS | GFC_STD_F95 | GFC_STD_F77;
637 gfc_option.warn_std = GFC_STD_F95_OBS;
638 gfc_option.max_identifier_length = 31;
639 gfc_option.warn_ampersand = 1;
640 gfc_option.warn_tabs = 0;
644 gfc_option.allow_std = GFC_STD_F95_OBS | GFC_STD_F77
645 | GFC_STD_F2003 | GFC_STD_F95;
646 gfc_option.warn_std = GFC_STD_F95_OBS;
647 gfc_option.max_continue_fixed = 255;
648 gfc_option.max_continue_free = 255;
649 gfc_option.max_identifier_length = 63;
650 gfc_option.warn_ampersand = 1;
651 gfc_option.warn_tabs = 0;
655 set_default_std_flags ();
659 set_default_std_flags ();
660 gfc_option.warn_std = 0;
663 case OPT_Wnonstd_intrinsics:
664 gfc_option.warn_nonstd_intrinsics = value;
667 case OPT_fshort_enums:
668 gfc_option.fshort_enums = 1;
671 case OPT_fconvert_little_endian:
672 gfc_option.convert = CONVERT_LITTLE;
675 case OPT_fconvert_big_endian:
676 gfc_option.convert = CONVERT_BIG;
679 case OPT_fconvert_native:
680 gfc_option.convert = CONVERT_NATIVE;
683 case OPT_fconvert_swap:
684 gfc_option.convert = CONVERT_SWAP;
687 case OPT_frecord_marker_4:
688 gfc_option.record_marker = 4;
691 case OPT_frecord_marker_8:
692 gfc_option.record_marker = 8;
695 case OPT_fmax_subrecord_length_:
696 if (value > MAX_SUBRECORD_LENGTH)
697 gfc_fatal_error ("Maximum subrecord length cannot exceed %d",
698 MAX_SUBRECORD_LENGTH);
700 gfc_option.max_subrecord_length = value;