Merge from mainline
[official-gcc.git] / gcc / fortran / options.c
blob6a9bec9b3a002618f58c7df543ed16aa1b96a5c4
1 /* Parse and display command line options.
2 Copyright (C) 2000, 2001, 2002, 2003, 2004, 2005, 2006
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 2, or (at your option) any later
11 version.
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.
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING. If not, write to the Free
20 Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA
21 02110-1301, USA. */
24 #include "config.h"
25 #include "system.h"
26 #include "coretypes.h"
27 #include "tree.h"
28 #include "flags.h"
29 #include "intl.h"
30 #include "opts.h"
31 #include "options.h"
32 #include "tree-inline.h"
34 #include "gfortran.h"
35 #include "target.h"
37 gfc_option_t gfc_option;
40 /* Get ready for options handling. */
42 unsigned int
43 gfc_init_options (unsigned int argc ATTRIBUTE_UNUSED,
44 const char **argv ATTRIBUTE_UNUSED)
46 gfc_source_file = NULL;
47 gfc_option.module_dir = NULL;
48 gfc_option.source_form = FORM_UNKNOWN;
49 gfc_option.fixed_line_length = -1;
50 gfc_option.free_line_length = -1;
51 gfc_option.max_identifier_length = GFC_MAX_SYMBOL_LEN;
52 gfc_option.verbose = 0;
54 gfc_option.warn_aliasing = 0;
55 gfc_option.warn_conversion = 0;
56 gfc_option.warn_implicit_interface = 0;
57 gfc_option.warn_line_truncation = 0;
58 gfc_option.warn_underflow = 1;
59 gfc_option.warn_surprising = 0;
60 gfc_option.warn_unused_labels = 0;
62 gfc_option.flag_default_double = 0;
63 gfc_option.flag_default_integer = 0;
64 gfc_option.flag_default_real = 0;
65 gfc_option.flag_dollar_ok = 0;
66 gfc_option.flag_underscoring = 1;
67 gfc_option.flag_f2c = 0;
68 gfc_option.flag_second_underscore = -1;
69 gfc_option.flag_implicit_none = 0;
70 gfc_option.flag_max_stack_var_size = 32768;
71 gfc_option.flag_module_access_private = 0;
72 gfc_option.flag_no_backend = 0;
73 gfc_option.flag_pack_derived = 0;
74 gfc_option.flag_repack_arrays = 0;
75 gfc_option.flag_preprocessed = 0;
76 gfc_option.flag_automatic = 1;
77 gfc_option.flag_backslash = 1;
78 gfc_option.flag_cray_pointer = 0;
79 gfc_option.flag_d_lines = -1;
80 gfc_option.flag_openmp = 0;
82 gfc_option.q_kind = gfc_default_double_kind;
84 gfc_option.fpe = 0;
86 flag_argument_noalias = 2;
87 flag_errno_math = 0;
89 gfc_option.allow_std = GFC_STD_F95_OBS | GFC_STD_F95_DEL
90 | GFC_STD_F2003 | GFC_STD_F95 | GFC_STD_F77 | GFC_STD_GNU
91 | GFC_STD_LEGACY;
92 gfc_option.warn_std = GFC_STD_F95_OBS | GFC_STD_F95_DEL
93 | GFC_STD_LEGACY;
95 gfc_option.warn_nonstd_intrinsics = 0;
97 /* -fshort-enums can be default on some targets. */
98 gfc_option.fshort_enums = targetm.default_short_enums ();
100 return CL_Fortran;
104 /* Determine the source form from the filename extension. We assume
105 case insensitivity. */
107 static gfc_source_form
108 form_from_filename (const char *filename)
111 static const struct
113 const char *extension;
114 gfc_source_form form;
116 exttype[] =
119 ".f90", FORM_FREE}
122 ".f95", FORM_FREE}
125 ".f", FORM_FIXED}
128 ".for", FORM_FIXED}
131 "", FORM_UNKNOWN}
132 }; /* sentinel value */
134 gfc_source_form f_form;
135 const char *fileext;
136 int i;
138 /* Find end of file name. Note, filename is either a NULL pointer or
139 a NUL terminated string. */
140 i = 0;
141 while (filename[i] != '\0')
142 i++;
144 /* Find last period. */
145 while (i >= 0 && (filename[i] != '.'))
146 i--;
148 /* Did we see a file extension? */
149 if (i < 0)
150 return FORM_UNKNOWN; /* Nope */
152 /* Get file extension and compare it to others. */
153 fileext = &(filename[i]);
155 i = -1;
156 f_form = FORM_UNKNOWN;
159 i++;
160 if (strcasecmp (fileext, exttype[i].extension) == 0)
162 f_form = exttype[i].form;
163 break;
166 while (exttype[i].form != FORM_UNKNOWN);
168 return f_form;
172 /* Finalize commandline options. */
174 bool
175 gfc_post_options (const char **pfilename)
177 const char *filename = *pfilename, *canon_source_file = NULL;
178 char *source_path;
179 int i;
181 /* Verify the input file name. */
182 if (!filename || strcmp (filename, "-") == 0)
184 filename = "";
187 if (gfc_option.flag_preprocessed)
189 /* For preprocessed files, if the first tokens are of the form # NUM.
190 handle the directives so we know the original file name. */
191 gfc_source_file = gfc_read_orig_filename (filename, &canon_source_file);
192 if (gfc_source_file == NULL)
193 gfc_source_file = filename;
194 else
195 *pfilename = gfc_source_file;
197 else
198 gfc_source_file = filename;
200 if (canon_source_file == NULL)
201 canon_source_file = gfc_source_file;
203 /* Adds the path where the source file is to the list of include files. */
205 i = strlen (canon_source_file);
206 while (i > 0 && !IS_DIR_SEPARATOR (canon_source_file[i]))
207 i--;
208 if (i != 0)
210 source_path = alloca (i + 1);
211 memcpy (source_path, canon_source_file, i);
212 source_path[i] = 0;
213 gfc_add_include_path (source_path);
215 else
216 gfc_add_include_path (".");
218 if (canon_source_file != gfc_source_file)
219 gfc_free ((void *) canon_source_file);
221 /* Decide which form the file will be read in as. */
223 if (gfc_option.source_form != FORM_UNKNOWN)
224 gfc_current_form = gfc_option.source_form;
225 else
227 gfc_current_form = form_from_filename (filename);
229 if (gfc_current_form == FORM_UNKNOWN)
231 gfc_current_form = FORM_FREE;
232 gfc_warning_now ("Reading file '%s' as free form.",
233 (filename[0] == '\0') ? "<stdin>" : filename);
237 /* If the user specified -fd-lines-as-{code|comments} verify that we're
238 in fixed form. */
239 if (gfc_current_form == FORM_FREE)
241 if (gfc_option.flag_d_lines == 0)
242 gfc_warning_now ("'-fd-lines-as-comments' has no effect "
243 "in free form.");
244 else if (gfc_option.flag_d_lines == 1)
245 gfc_warning_now ("'-fd-lines-as-code' has no effect "
246 "in free form.");
249 flag_inline_trees = 1;
251 /* Use tree inlining. */
252 if (!flag_no_inline)
253 flag_no_inline = 1;
254 if (flag_inline_functions)
255 flag_inline_trees = 2;
257 /* If -pedantic, warn about the use of GNU extensions. */
258 if (pedantic && (gfc_option.allow_std & GFC_STD_GNU) != 0)
259 gfc_option.warn_std |= GFC_STD_GNU;
260 /* -std=legacy -pedantic is effectively -std=gnu. */
261 if (pedantic && (gfc_option.allow_std & GFC_STD_LEGACY) != 0)
262 gfc_option.warn_std |= GFC_STD_F95_OBS | GFC_STD_F95_DEL | GFC_STD_LEGACY;
264 /* If the user didn't explicitly specify -f(no)-second-underscore we
265 use it if we're trying to be compatible with f2c, and not
266 otherwise. */
267 if (gfc_option.flag_second_underscore == -1)
268 gfc_option.flag_second_underscore = gfc_option.flag_f2c;
270 /* Implement -fno-automatic as -fmax-stack-var-size=0. */
271 if (!gfc_option.flag_automatic)
272 gfc_option.flag_max_stack_var_size = 0;
274 return false;
278 /* Set the options for -Wall. */
280 static void
281 set_Wall (void)
284 gfc_option.warn_aliasing = 1;
285 gfc_option.warn_line_truncation = 1;
286 gfc_option.warn_underflow = 1;
287 gfc_option.warn_surprising = 1;
288 gfc_option.warn_unused_labels = 1;
289 gfc_option.warn_nonstd_intrinsics = 1;
291 set_Wunused (1);
292 warn_return_type = 1;
293 warn_switch = 1;
295 /* We save the value of warn_uninitialized, since if they put
296 -Wuninitialized on the command line, we need to generate a
297 warning about not using it without also specifying -O. */
299 if (warn_uninitialized != 1)
300 warn_uninitialized = 2;
304 static void
305 gfc_handle_module_path_options (const char *arg)
308 if (gfc_option.module_dir != NULL)
310 gfc_status ("gfortran: Only one -M option allowed\n");
311 exit (3);
314 if (arg == NULL)
316 gfc_status ("gfortran: Directory required after -M\n");
317 exit (3);
320 gfc_option.module_dir = (char *) gfc_getmem (strlen (arg) + 2);
321 strcpy (gfc_option.module_dir, arg);
322 strcat (gfc_option.module_dir, "/");
325 static void
326 gfc_handle_fpe_trap_option (const char *arg)
328 int result, pos = 0, n;
329 static const char * const exception[] = { "invalid", "denormal", "zero",
330 "overflow", "underflow",
331 "precision", NULL };
332 static const int opt_exception[] = { GFC_FPE_INVALID, GFC_FPE_DENORMAL,
333 GFC_FPE_ZERO, GFC_FPE_OVERFLOW,
334 GFC_FPE_UNDERFLOW, GFC_FPE_PRECISION,
335 0 };
337 while (*arg)
339 while (*arg == ',')
340 arg++;
341 while (arg[pos] && arg[pos] != ',')
342 pos++;
343 result = 0;
344 for (n = 0; exception[n] != NULL; n++)
346 if (exception[n] && strncmp (exception[n], arg, pos) == 0)
348 gfc_option.fpe |= opt_exception[n];
349 arg += pos;
350 pos = 0;
351 result = 1;
352 break;
355 if (! result)
356 gfc_fatal_error ("Argument to -ffpe-trap is not valid: %s", arg);
360 /* Handle command-line options. Returns 0 if unrecognized, 1 if
361 recognized and handled. */
363 gfc_handle_option (size_t scode, const char *arg, int value)
365 int result = 1;
366 enum opt_code code = (enum opt_code) scode;
368 /* Ignore file names. */
369 if (code == N_OPTS)
370 return 1;
372 switch (code)
374 default:
375 result = 0;
376 break;
378 case OPT_Wall:
379 set_Wall ();
380 break;
382 case OPT_Waliasing:
383 gfc_option.warn_aliasing = value;
384 break;
386 case OPT_Wconversion:
387 gfc_option.warn_conversion = value;
388 break;
390 case OPT_Wimplicit_interface:
391 gfc_option.warn_implicit_interface = value;
392 break;
394 case OPT_Wline_truncation:
395 gfc_option.warn_line_truncation = value;
396 break;
398 case OPT_Wunderflow:
399 gfc_option.warn_underflow = value;
400 break;
402 case OPT_Wsurprising:
403 gfc_option.warn_surprising = value;
404 break;
406 case OPT_Wunused_labels:
407 gfc_option.warn_unused_labels = value;
408 break;
410 case OPT_fcray_pointer:
411 gfc_option.flag_cray_pointer = value;
412 break;
414 case OPT_ff2c:
415 gfc_option.flag_f2c = value;
416 break;
418 case OPT_fdollar_ok:
419 gfc_option.flag_dollar_ok = value;
420 break;
422 case OPT_fautomatic:
423 gfc_option.flag_automatic = value;
424 break;
426 case OPT_fbackslash:
427 gfc_option.flag_backslash = value;
428 break;
430 case OPT_fd_lines_as_code:
431 gfc_option.flag_d_lines = 1;
432 break;
434 case OPT_fd_lines_as_comments:
435 gfc_option.flag_d_lines = 0;
436 break;
438 case OPT_fdump_parse_tree:
439 gfc_option.verbose = value;
440 break;
442 case OPT_ffixed_form:
443 gfc_option.source_form = FORM_FIXED;
444 break;
446 case OPT_ffixed_line_length_none:
447 gfc_option.fixed_line_length = 0;
448 break;
450 case OPT_ffixed_line_length_:
451 if (value != 0 && value < 7)
452 gfc_fatal_error ("Fixed line length must be at least seven.");
453 gfc_option.fixed_line_length = value;
454 break;
456 case OPT_ffree_form:
457 gfc_option.source_form = FORM_FREE;
458 break;
460 case OPT_fopenmp:
461 gfc_option.flag_openmp = value;
462 break;
464 case OPT_ffree_line_length_none:
465 gfc_option.free_line_length = 0;
466 break;
468 case OPT_ffree_line_length_:
469 gfc_option.free_line_length = value;
470 break;
472 case OPT_funderscoring:
473 gfc_option.flag_underscoring = value;
474 break;
476 case OPT_fsecond_underscore:
477 gfc_option.flag_second_underscore = value;
478 break;
480 case OPT_fimplicit_none:
481 gfc_option.flag_implicit_none = value;
482 break;
484 case OPT_fmax_stack_var_size_:
485 gfc_option.flag_max_stack_var_size = value;
486 break;
488 case OPT_fmodule_private:
489 gfc_option.flag_module_access_private = value;
490 break;
492 case OPT_fno_backend:
493 gfc_option.flag_no_backend = value;
494 break;
496 case OPT_fpack_derived:
497 gfc_option.flag_pack_derived = value;
498 break;
500 case OPT_frepack_arrays:
501 gfc_option.flag_repack_arrays = value;
502 break;
504 case OPT_fpreprocessed:
505 gfc_option.flag_preprocessed = value;
506 break;
508 case OPT_fmax_identifier_length_:
509 if (value > GFC_MAX_SYMBOL_LEN)
510 gfc_fatal_error ("Maximum supported idenitifier length is %d",
511 GFC_MAX_SYMBOL_LEN);
512 gfc_option.max_identifier_length = value;
513 break;
515 case OPT_qkind_:
516 if (gfc_validate_kind (BT_REAL, value, true) < 0)
517 gfc_fatal_error ("Argument to -fqkind isn't a valid real kind");
518 gfc_option.q_kind = value;
519 break;
521 case OPT_fdefault_integer_8:
522 gfc_option.flag_default_integer = value;
523 break;
525 case OPT_fdefault_real_8:
526 gfc_option.flag_default_real = value;
527 break;
529 case OPT_fdefault_double_8:
530 gfc_option.flag_default_double = value;
531 break;
533 case OPT_I:
534 gfc_add_include_path (arg);
535 break;
537 case OPT_J:
538 case OPT_M:
539 gfc_handle_module_path_options (arg);
540 break;
542 case OPT_ffpe_trap_:
543 gfc_handle_fpe_trap_option (arg);
544 break;
546 case OPT_std_f95:
547 gfc_option.allow_std = GFC_STD_F95_OBS | GFC_STD_F95 | GFC_STD_F77;
548 gfc_option.warn_std = GFC_STD_F95_OBS;
549 gfc_option.max_identifier_length = 31;
550 break;
552 case OPT_std_f2003:
553 gfc_option.allow_std = GFC_STD_F95_OBS | GFC_STD_F77
554 | GFC_STD_F2003 | GFC_STD_F95;
555 gfc_option.warn_std = GFC_STD_F95_OBS;
556 gfc_option.max_identifier_length = 63;
557 break;
559 case OPT_std_gnu:
560 gfc_option.allow_std = GFC_STD_F95_OBS | GFC_STD_F95_DEL
561 | GFC_STD_F77 | GFC_STD_F95 | GFC_STD_F2003
562 | GFC_STD_GNU | GFC_STD_LEGACY;
563 gfc_option.warn_std = GFC_STD_F95_OBS | GFC_STD_F95_DEL
564 | GFC_STD_LEGACY;
565 break;
567 case OPT_std_legacy:
568 gfc_option.allow_std = GFC_STD_F95_OBS | GFC_STD_F95_DEL
569 | GFC_STD_F77 | GFC_STD_F95 | GFC_STD_F2003
570 | GFC_STD_GNU | GFC_STD_LEGACY;
571 gfc_option.warn_std = 0;
572 break;
574 case OPT_Wnonstd_intrinsics:
575 gfc_option.warn_nonstd_intrinsics = 1;
576 break;
578 case OPT_fshort_enums:
579 gfc_option.fshort_enums = 1;
580 break;
582 case OPT_fconvert_little_endian:
583 gfc_option.convert = CONVERT_LITTLE;
584 break;
586 case OPT_fconvert_big_endian:
587 gfc_option.convert = CONVERT_BIG;
588 break;
590 case OPT_fconvert_native:
591 gfc_option.convert = CONVERT_NATIVE;
592 break;
594 case OPT_fconvert_swap:
595 gfc_option.convert = CONVERT_SWAP;
596 break;
599 return result;