OSDN Git Service

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