re PR fortran/31270 (print subscript value and array bounds when out-of-bounds error...
[gcc.git] / gcc / fortran / options.c
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
5
6 This file is part of GCC.
7
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
11 version.
12
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.
17
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/>. */
21
22 #include "config.h"
23 #include "system.h"
24 #include "coretypes.h"
25 #include "tree.h"
26 #include "flags.h"
27 #include "intl.h"
28 #include "opts.h"
29 #include "options.h"
30 #include "params.h"
31 #include "tree-inline.h"
32 #include "gfortran.h"
33 #include "target.h"
34
35 gfc_option_t gfc_option;
36
37
38 /* Set flags that control warnings and errors for different
39 Fortran standards to their default values. */
40
41 static void
42 set_default_std_flags (void)
43 {
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
46 | GFC_STD_LEGACY;
47 gfc_option.warn_std = GFC_STD_F95_DEL | GFC_STD_LEGACY;
48 }
49
50 /* Get ready for options handling. */
51
52 unsigned int
53 gfc_init_options (unsigned int argc ATTRIBUTE_UNUSED,
54 const char **argv ATTRIBUTE_UNUSED)
55 {
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;
68
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;
79
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_backtrace = 0;
97 gfc_option.flag_allow_leading_underscore = 0;
98 gfc_option.flag_dump_core = 0;
99 gfc_option.flag_external_blas = 0;
100 gfc_option.blas_matmul_limit = 30;
101 gfc_option.flag_cray_pointer = 0;
102 gfc_option.flag_d_lines = -1;
103 gfc_option.flag_openmp = 0;
104 gfc_option.flag_sign_zero = 1;
105
106 gfc_option.fpe = 0;
107
108 /* Argument pointers cannot point to anything but their argument. */
109 flag_argument_noalias = 3;
110
111 flag_errno_math = 0;
112
113 set_default_std_flags ();
114
115 gfc_option.warn_nonstd_intrinsics = 0;
116
117 /* -fshort-enums can be default on some targets. */
118 gfc_option.fshort_enums = targetm.default_short_enums ();
119
120 return CL_Fortran;
121 }
122
123
124 /* Determine the source form from the filename extension. We assume
125 case insensitivity. */
126
127 static gfc_source_form
128 form_from_filename (const char *filename)
129 {
130 static const struct
131 {
132 const char *extension;
133 gfc_source_form form;
134 }
135 exttype[] =
136 {
137 {
138 ".f90", FORM_FREE}
139 ,
140 {
141 ".f95", FORM_FREE}
142 ,
143 {
144 ".f03", FORM_FREE}
145 ,
146 {
147 ".f", FORM_FIXED}
148 ,
149 {
150 ".for", FORM_FIXED}
151 ,
152 {
153 "", FORM_UNKNOWN}
154 }; /* sentinel value */
155
156 gfc_source_form f_form;
157 const char *fileext;
158 int i;
159
160 /* Find end of file name. Note, filename is either a NULL pointer or
161 a NUL terminated string. */
162 i = 0;
163 while (filename[i] != '\0')
164 i++;
165
166 /* Find last period. */
167 while (i >= 0 && (filename[i] != '.'))
168 i--;
169
170 /* Did we see a file extension? */
171 if (i < 0)
172 return FORM_UNKNOWN; /* Nope */
173
174 /* Get file extension and compare it to others. */
175 fileext = &(filename[i]);
176
177 i = -1;
178 f_form = FORM_UNKNOWN;
179 do
180 {
181 i++;
182 if (strcasecmp (fileext, exttype[i].extension) == 0)
183 {
184 f_form = exttype[i].form;
185 break;
186 }
187 }
188 while (exttype[i].form != FORM_UNKNOWN);
189
190 return f_form;
191 }
192
193
194 /* Finalize commandline options. */
195
196 bool
197 gfc_post_options (const char **pfilename)
198 {
199 const char *filename = *pfilename, *canon_source_file = NULL;
200 char *source_path;
201 int i;
202
203 /* Verify the input file name. */
204 if (!filename || strcmp (filename, "-") == 0)
205 {
206 filename = "";
207 }
208
209 if (gfc_option.flag_preprocessed)
210 {
211 /* For preprocessed files, if the first tokens are of the form # NUM.
212 handle the directives so we know the original file name. */
213 gfc_source_file = gfc_read_orig_filename (filename, &canon_source_file);
214 if (gfc_source_file == NULL)
215 gfc_source_file = filename;
216 else
217 *pfilename = gfc_source_file;
218 }
219 else
220 gfc_source_file = filename;
221
222 if (canon_source_file == NULL)
223 canon_source_file = gfc_source_file;
224
225 /* Adds the path where the source file is to the list of include files. */
226
227 i = strlen (canon_source_file);
228 while (i > 0 && !IS_DIR_SEPARATOR (canon_source_file[i]))
229 i--;
230
231 if (i != 0)
232 {
233 source_path = alloca (i + 1);
234 memcpy (source_path, canon_source_file, i);
235 source_path[i] = 0;
236 gfc_add_include_path (source_path, true);
237 }
238 else
239 gfc_add_include_path (".", true);
240
241 if (canon_source_file != gfc_source_file)
242 gfc_free (CONST_CAST (canon_source_file));
243
244 /* Decide which form the file will be read in as. */
245
246 if (gfc_option.source_form != FORM_UNKNOWN)
247 gfc_current_form = gfc_option.source_form;
248 else
249 {
250 gfc_current_form = form_from_filename (filename);
251
252 if (gfc_current_form == FORM_UNKNOWN)
253 {
254 gfc_current_form = FORM_FREE;
255 gfc_warning_now ("Reading file '%s' as free form",
256 (filename[0] == '\0') ? "<stdin>" : filename);
257 }
258 }
259
260 /* If the user specified -fd-lines-as-{code|comments} verify that we're
261 in fixed form. */
262 if (gfc_current_form == FORM_FREE)
263 {
264 if (gfc_option.flag_d_lines == 0)
265 gfc_warning_now ("'-fd-lines-as-comments' has no effect "
266 "in free form");
267 else if (gfc_option.flag_d_lines == 1)
268 gfc_warning_now ("'-fd-lines-as-code' has no effect in free form");
269 }
270
271 flag_inline_trees = 1;
272
273 /* Use tree inlining. */
274 if (!flag_no_inline)
275 flag_no_inline = 1;
276 if (flag_inline_functions)
277 flag_inline_trees = 2;
278
279 /* If -pedantic, warn about the use of GNU extensions. */
280 if (pedantic && (gfc_option.allow_std & GFC_STD_GNU) != 0)
281 gfc_option.warn_std |= GFC_STD_GNU;
282 /* -std=legacy -pedantic is effectively -std=gnu. */
283 if (pedantic && (gfc_option.allow_std & GFC_STD_LEGACY) != 0)
284 gfc_option.warn_std |= GFC_STD_F95_OBS | GFC_STD_F95_DEL | GFC_STD_LEGACY;
285
286 /* If the user didn't explicitly specify -f(no)-second-underscore we
287 use it if we're trying to be compatible with f2c, and not
288 otherwise. */
289 if (gfc_option.flag_second_underscore == -1)
290 gfc_option.flag_second_underscore = gfc_option.flag_f2c;
291
292 /* Implement -fno-automatic as -fmax-stack-var-size=0. */
293 if (!gfc_option.flag_automatic)
294 gfc_option.flag_max_stack_var_size = 0;
295
296 if (pedantic)
297 {
298 gfc_option.warn_ampersand = 1;
299 gfc_option.warn_tabs = 0;
300 }
301
302 if (gfc_option.flag_all_intrinsics)
303 gfc_option.warn_nonstd_intrinsics = 0;
304
305 return false;
306 }
307
308
309 /* Set the options for -Wall. */
310
311 static void
312 set_Wall (int setting)
313 {
314 gfc_option.warn_aliasing = setting;
315 gfc_option.warn_ampersand = setting;
316 gfc_option.warn_line_truncation = setting;
317 gfc_option.warn_nonstd_intrinsics = setting;
318 gfc_option.warn_surprising = setting;
319 gfc_option.warn_tabs = !setting;
320 gfc_option.warn_underflow = setting;
321 gfc_option.warn_character_truncation = setting;
322
323 set_Wunused (setting);
324 warn_return_type = setting;
325 warn_switch = setting;
326
327 /* We save the value of warn_uninitialized, since if they put
328 -Wuninitialized on the command line, we need to generate a
329 warning about not using it without also specifying -O. */
330 if (setting == 0)
331 warn_uninitialized = 0;
332 else if (warn_uninitialized != 1)
333 warn_uninitialized = 2;
334 }
335
336
337 static void
338 gfc_handle_module_path_options (const char *arg)
339 {
340
341 if (gfc_option.module_dir != NULL)
342 {
343 gfc_status ("gfortran: Only one -M option allowed\n");
344 exit (3);
345 }
346
347 if (arg == NULL)
348 {
349 gfc_status ("gfortran: Directory required after -M\n");
350 exit (3);
351 }
352
353 gfc_option.module_dir = (char *) gfc_getmem (strlen (arg) + 2);
354 strcpy (gfc_option.module_dir, arg);
355 strcat (gfc_option.module_dir, "/");
356
357 gfc_add_include_path (gfc_option.module_dir, true);
358 }
359
360
361 static void
362 gfc_handle_fpe_trap_option (const char *arg)
363 {
364 int result, pos = 0, n;
365 static const char * const exception[] = { "invalid", "denormal", "zero",
366 "overflow", "underflow",
367 "precision", NULL };
368 static const int opt_exception[] = { GFC_FPE_INVALID, GFC_FPE_DENORMAL,
369 GFC_FPE_ZERO, GFC_FPE_OVERFLOW,
370 GFC_FPE_UNDERFLOW, GFC_FPE_PRECISION,
371 0 };
372
373 while (*arg)
374 {
375 while (*arg == ',')
376 arg++;
377
378 while (arg[pos] && arg[pos] != ',')
379 pos++;
380
381 result = 0;
382 for (n = 0; exception[n] != NULL; n++)
383 {
384 if (exception[n] && strncmp (exception[n], arg, pos) == 0)
385 {
386 gfc_option.fpe |= opt_exception[n];
387 arg += pos;
388 pos = 0;
389 result = 1;
390 break;
391 }
392 }
393 if (!result)
394 gfc_fatal_error ("Argument to -ffpe-trap is not valid: %s", arg);
395 }
396 }
397
398
399 /* Handle command-line options. Returns 0 if unrecognized, 1 if
400 recognized and handled. */
401
402 int
403 gfc_handle_option (size_t scode, const char *arg, int value)
404 {
405 int result = 1;
406 enum opt_code code = (enum opt_code) scode;
407
408 /* Ignore file names. */
409 if (code == N_OPTS)
410 return 1;
411
412 switch (code)
413 {
414 default:
415 result = 0;
416 break;
417
418 case OPT_Wall:
419 set_Wall (value);
420 break;
421
422 case OPT_Waliasing:
423 gfc_option.warn_aliasing = value;
424 break;
425
426 case OPT_Wampersand:
427 gfc_option.warn_ampersand = value;
428 break;
429
430 case OPT_Wcharacter_truncation:
431 gfc_option.warn_character_truncation = value;
432 break;
433
434 case OPT_Wconversion:
435 gfc_option.warn_conversion = value;
436 break;
437
438 case OPT_Wimplicit_interface:
439 gfc_option.warn_implicit_interface = value;
440 break;
441
442 case OPT_Wline_truncation:
443 gfc_option.warn_line_truncation = value;
444 break;
445
446 case OPT_Wsurprising:
447 gfc_option.warn_surprising = value;
448 break;
449
450 case OPT_Wtabs:
451 gfc_option.warn_tabs = value;
452 break;
453
454 case OPT_Wunderflow:
455 gfc_option.warn_underflow = value;
456 break;
457
458 case OPT_fall_intrinsics:
459 gfc_option.flag_all_intrinsics = 1;
460 break;
461
462 case OPT_fautomatic:
463 gfc_option.flag_automatic = value;
464 break;
465
466 case OPT_fallow_leading_underscore:
467 gfc_option.flag_allow_leading_underscore = value;
468 break;
469
470 case OPT_fbackslash:
471 gfc_option.flag_backslash = value;
472 break;
473
474 case OPT_fbacktrace:
475 gfc_option.flag_backtrace = value;
476 break;
477
478 case OPT_fdump_core:
479 gfc_option.flag_dump_core = value;
480 break;
481
482 case OPT_fcray_pointer:
483 gfc_option.flag_cray_pointer = value;
484 break;
485
486 case OPT_ff2c:
487 gfc_option.flag_f2c = value;
488 break;
489
490 case OPT_fdollar_ok:
491 gfc_option.flag_dollar_ok = value;
492 break;
493
494 case OPT_fexternal_blas:
495 gfc_option.flag_external_blas = value;
496 break;
497
498 case OPT_fblas_matmul_limit_:
499 gfc_option.blas_matmul_limit = value;
500 break;
501
502 case OPT_fd_lines_as_code:
503 gfc_option.flag_d_lines = 1;
504 break;
505
506 case OPT_fd_lines_as_comments:
507 gfc_option.flag_d_lines = 0;
508 break;
509
510 case OPT_fdump_parse_tree:
511 gfc_option.verbose = value;
512 break;
513
514 case OPT_ffixed_form:
515 gfc_option.source_form = FORM_FIXED;
516 break;
517
518 case OPT_ffixed_line_length_none:
519 gfc_option.fixed_line_length = 0;
520 break;
521
522 case OPT_ffixed_line_length_:
523 if (value != 0 && value < 7)
524 gfc_fatal_error ("Fixed line length must be at least seven.");
525 gfc_option.fixed_line_length = value;
526 break;
527
528 case OPT_ffree_form:
529 gfc_option.source_form = FORM_FREE;
530 break;
531
532 case OPT_fopenmp:
533 gfc_option.flag_openmp = value;
534 break;
535
536 case OPT_ffree_line_length_none:
537 gfc_option.free_line_length = 0;
538 break;
539
540 case OPT_ffree_line_length_:
541 if (value != 0 && value < 4)
542 gfc_fatal_error ("Free line length must be at least three.");
543 gfc_option.free_line_length = value;
544 break;
545
546 case OPT_funderscoring:
547 gfc_option.flag_underscoring = value;
548 break;
549
550 case OPT_fsecond_underscore:
551 gfc_option.flag_second_underscore = value;
552 break;
553
554 case OPT_static_libgfortran:
555 #ifndef HAVE_LD_STATIC_DYNAMIC
556 gfc_fatal_error ("-static-libgfortran is not supported in this "
557 "configuration");
558 #endif
559 break;
560
561 case OPT_fimplicit_none:
562 gfc_option.flag_implicit_none = value;
563 break;
564
565 case OPT_fintrinsic_modules_path:
566 gfc_add_include_path (arg, false);
567 gfc_add_intrinsic_modules_path (arg);
568 break;
569
570 case OPT_fmax_errors_:
571 gfc_option.max_errors = value;
572 break;
573
574 case OPT_fmax_stack_var_size_:
575 gfc_option.flag_max_stack_var_size = value;
576 break;
577
578 case OPT_frange_check:
579 gfc_option.flag_range_check = value;
580 break;
581
582 case OPT_fpack_derived:
583 gfc_option.flag_pack_derived = value;
584 break;
585
586 case OPT_frepack_arrays:
587 gfc_option.flag_repack_arrays = value;
588 break;
589
590 case OPT_fpreprocessed:
591 gfc_option.flag_preprocessed = value;
592 break;
593
594 case OPT_fmax_identifier_length_:
595 if (value > GFC_MAX_SYMBOL_LEN)
596 gfc_fatal_error ("Maximum supported identifier length is %d",
597 GFC_MAX_SYMBOL_LEN);
598 gfc_option.max_identifier_length = value;
599 break;
600
601 case OPT_fdefault_integer_8:
602 gfc_option.flag_default_integer = value;
603 break;
604
605 case OPT_fdefault_real_8:
606 gfc_option.flag_default_real = value;
607 break;
608
609 case OPT_fdefault_double_8:
610 gfc_option.flag_default_double = value;
611 break;
612
613 case OPT_I:
614 gfc_add_include_path (arg, true);
615 break;
616
617 case OPT_J:
618 case OPT_M:
619 gfc_handle_module_path_options (arg);
620 break;
621
622 case OPT_fsign_zero:
623 gfc_option.flag_sign_zero = value;
624 break;
625
626 case OPT_ffpe_trap_:
627 gfc_handle_fpe_trap_option (arg);
628 break;
629
630 case OPT_std_f95:
631 gfc_option.allow_std = GFC_STD_F95_OBS | GFC_STD_F95 | GFC_STD_F77;
632 gfc_option.warn_std = GFC_STD_F95_OBS;
633 gfc_option.max_identifier_length = 31;
634 gfc_option.warn_ampersand = 1;
635 gfc_option.warn_tabs = 0;
636 break;
637
638 case OPT_std_f2003:
639 gfc_option.allow_std = GFC_STD_F95_OBS | GFC_STD_F77
640 | GFC_STD_F2003 | GFC_STD_F95;
641 gfc_option.warn_std = GFC_STD_F95_OBS;
642 gfc_option.max_continue_fixed = 255;
643 gfc_option.max_continue_free = 255;
644 gfc_option.max_identifier_length = 63;
645 gfc_option.warn_ampersand = 1;
646 gfc_option.warn_tabs = 0;
647 break;
648
649 case OPT_std_gnu:
650 set_default_std_flags ();
651 break;
652
653 case OPT_std_legacy:
654 set_default_std_flags ();
655 gfc_option.warn_std = 0;
656 break;
657
658 case OPT_Wnonstd_intrinsics:
659 gfc_option.warn_nonstd_intrinsics = value;
660 break;
661
662 case OPT_fshort_enums:
663 gfc_option.fshort_enums = 1;
664 break;
665
666 case OPT_fconvert_little_endian:
667 gfc_option.convert = CONVERT_LITTLE;
668 break;
669
670 case OPT_fconvert_big_endian:
671 gfc_option.convert = CONVERT_BIG;
672 break;
673
674 case OPT_fconvert_native:
675 gfc_option.convert = CONVERT_NATIVE;
676 break;
677
678 case OPT_fconvert_swap:
679 gfc_option.convert = CONVERT_SWAP;
680 break;
681
682 case OPT_frecord_marker_4:
683 gfc_option.record_marker = 4;
684 break;
685
686 case OPT_frecord_marker_8:
687 gfc_option.record_marker = 8;
688 break;
689
690 case OPT_fmax_subrecord_length_:
691 if (value > MAX_SUBRECORD_LENGTH)
692 gfc_fatal_error ("Maximum subrecord length cannot exceed %d",
693 MAX_SUBRECORD_LENGTH);
694
695 gfc_option.max_subrecord_length = value;
696 }
697
698 return result;
699 }