OSDN Git Service

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