OSDN Git Service

0bea67d7eeac8c4887e4184faa1ed600721111e8
[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 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_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;
106
107   gfc_option.fpe = 0;
108
109   /* Argument pointers cannot point to anything but their argument.  */
110   flag_argument_noalias = 3;
111
112   flag_errno_math = 0;
113
114   set_default_std_flags ();
115
116   gfc_option.warn_nonstd_intrinsics = 0;
117
118   /* -fshort-enums can be default on some targets.  */
119   gfc_option.fshort_enums = targetm.default_short_enums ();
120
121   return CL_Fortran;
122 }
123
124
125 /* Determine the source form from the filename extension.  We assume
126    case insensitivity.  */
127
128 static gfc_source_form
129 form_from_filename (const char *filename)
130 {
131   static const struct
132   {
133     const char *extension;
134     gfc_source_form form;
135   }
136   exttype[] =
137   {
138     {
139     ".f90", FORM_FREE}
140     ,
141     {
142     ".f95", FORM_FREE}
143     ,
144     {
145     ".f03", FORM_FREE}
146     ,
147     {
148     ".f", FORM_FIXED}
149     ,
150     {
151     ".for", FORM_FIXED}
152     ,
153     {
154     "", FORM_UNKNOWN}
155   };            /* sentinel value */
156
157   gfc_source_form f_form;
158   const char *fileext;
159   int i;
160
161   /* Find end of file name.  Note, filename is either a NULL pointer or
162      a NUL terminated string.  */
163   i = 0;
164   while (filename[i] != '\0')
165     i++;
166
167   /* Find last period.  */
168   while (i >= 0 && (filename[i] != '.'))
169     i--;
170
171   /* Did we see a file extension?  */
172   if (i < 0)
173     return FORM_UNKNOWN; /* Nope  */
174
175   /* Get file extension and compare it to others.  */
176   fileext = &(filename[i]);
177
178   i = -1;
179   f_form = FORM_UNKNOWN;
180   do
181     {
182       i++;
183       if (strcasecmp (fileext, exttype[i].extension) == 0)
184         {
185           f_form = exttype[i].form;
186           break;
187         }
188     }
189   while (exttype[i].form != FORM_UNKNOWN);
190
191   return f_form;
192 }
193
194
195 /* Finalize commandline options.  */
196
197 bool
198 gfc_post_options (const char **pfilename)
199 {
200   const char *filename = *pfilename, *canon_source_file = NULL;
201   char *source_path;
202   int i;
203
204   /* Verify the input file name.  */
205   if (!filename || strcmp (filename, "-") == 0)
206     {
207       filename = "";
208     }
209
210   if (gfc_option.flag_preprocessed)
211     {
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;
217       else
218         *pfilename = gfc_source_file;
219     }
220   else
221     gfc_source_file = filename;
222
223   if (canon_source_file == NULL)
224     canon_source_file = gfc_source_file;
225
226   /* Adds the path where the source file is to the list of include files.  */
227
228   i = strlen (canon_source_file);
229   while (i > 0 && !IS_DIR_SEPARATOR (canon_source_file[i]))
230     i--;
231
232   if (i != 0)
233     {
234       source_path = alloca (i + 1);
235       memcpy (source_path, canon_source_file, i);
236       source_path[i] = 0;
237       gfc_add_include_path (source_path, true);
238     }
239   else
240     gfc_add_include_path (".", true);
241
242   if (canon_source_file != gfc_source_file)
243     gfc_free (CONST_CAST (canon_source_file));
244
245   /* Decide which form the file will be read in as.  */
246
247   if (gfc_option.source_form != FORM_UNKNOWN)
248     gfc_current_form = gfc_option.source_form;
249   else
250     {
251       gfc_current_form = form_from_filename (filename);
252
253       if (gfc_current_form == FORM_UNKNOWN)
254         {
255           gfc_current_form = FORM_FREE;
256           gfc_warning_now ("Reading file '%s' as free form", 
257                            (filename[0] == '\0') ? "<stdin>" : filename);
258         }
259     }
260
261   /* If the user specified -fd-lines-as-{code|comments} verify that we're
262      in fixed form.  */
263   if (gfc_current_form == FORM_FREE)
264     {
265       if (gfc_option.flag_d_lines == 0)
266         gfc_warning_now ("'-fd-lines-as-comments' has no effect "
267                          "in free form");
268       else if (gfc_option.flag_d_lines == 1)
269         gfc_warning_now ("'-fd-lines-as-code' has no effect in free form");
270     }
271
272   flag_inline_trees = 1;
273
274   /* Use tree inlining.  */
275   if (!flag_no_inline)
276     flag_no_inline = 1;
277   if (flag_inline_functions)
278     flag_inline_trees = 2;
279
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;
286
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
289      otherwise.  */
290   if (gfc_option.flag_second_underscore == -1)
291     gfc_option.flag_second_underscore = gfc_option.flag_f2c;
292
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;
296   
297   if (pedantic)
298     { 
299       gfc_option.warn_ampersand = 1;
300       gfc_option.warn_tabs = 0;
301     }
302
303   if (gfc_option.flag_all_intrinsics)
304     gfc_option.warn_nonstd_intrinsics = 0;
305
306   return false;
307 }
308
309
310 /* Set the options for -Wall.  */
311
312 static void
313 set_Wall (int setting)
314 {
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;
323
324   set_Wunused (setting);
325   warn_return_type = setting;
326   warn_switch = setting;
327
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.  */
331   if (setting == 0)
332     warn_uninitialized = 0;
333   else if (warn_uninitialized != 1)
334     warn_uninitialized = 2;
335 }
336
337
338 static void
339 gfc_handle_module_path_options (const char *arg)
340 {
341
342   if (gfc_option.module_dir != NULL)
343     {
344       gfc_status ("gfortran: Only one -M option allowed\n");
345       exit (3);
346     }
347
348   if (arg == NULL)
349     {
350       gfc_status ("gfortran: Directory required after -M\n");
351       exit (3);
352     }
353
354   gfc_option.module_dir = (char *) gfc_getmem (strlen (arg) + 2);
355   strcpy (gfc_option.module_dir, arg);
356   strcat (gfc_option.module_dir, "/");
357
358   gfc_add_include_path (gfc_option.module_dir, true);
359 }
360
361
362 static void
363 gfc_handle_fpe_trap_option (const char *arg)
364 {
365   int result, pos = 0, n;
366   static const char * const exception[] = { "invalid", "denormal", "zero",
367                                             "overflow", "underflow",
368                                             "precision", NULL };
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,
372                                        0 };
373  
374   while (*arg)
375     {
376       while (*arg == ',')
377         arg++;
378
379       while (arg[pos] && arg[pos] != ',')
380         pos++;
381
382       result = 0;
383       for (n = 0; exception[n] != NULL; n++)
384         {
385           if (exception[n] && strncmp (exception[n], arg, pos) == 0)
386             {
387               gfc_option.fpe |= opt_exception[n];
388               arg += pos;
389               pos = 0;
390               result = 1;
391               break;
392             }
393         }
394       if (!result)
395         gfc_fatal_error ("Argument to -ffpe-trap is not valid: %s", arg);
396     }
397 }
398
399
400 /* Handle command-line options.  Returns 0 if unrecognized, 1 if
401    recognized and handled.  */
402
403 int
404 gfc_handle_option (size_t scode, const char *arg, int value)
405 {
406   int result = 1;
407   enum opt_code code = (enum opt_code) scode;
408
409   /* Ignore file names.  */
410   if (code == N_OPTS)
411     return 1;
412
413   switch (code)
414     {
415     default:
416       result = 0;
417       break;
418
419     case OPT_Wall:
420       set_Wall (value);
421       break;
422
423     case OPT_Waliasing:
424       gfc_option.warn_aliasing = value;
425       break;
426
427     case OPT_Wampersand:
428       gfc_option.warn_ampersand = value;
429       break;
430
431     case OPT_Wcharacter_truncation:
432       gfc_option.warn_character_truncation = value;
433       break;
434
435     case OPT_Wconversion:
436       gfc_option.warn_conversion = value;
437       break;
438
439     case OPT_Wimplicit_interface:
440       gfc_option.warn_implicit_interface = value;
441       break;
442
443     case OPT_Wline_truncation:
444       gfc_option.warn_line_truncation = value;
445       break;
446
447     case OPT_Wsurprising:
448       gfc_option.warn_surprising = value;
449       break;
450
451     case OPT_Wtabs:
452       gfc_option.warn_tabs = value;
453       break;
454
455     case OPT_Wunderflow:
456       gfc_option.warn_underflow = value;
457       break;
458
459     case OPT_fall_intrinsics:
460       gfc_option.flag_all_intrinsics = 1;
461       break;
462
463     case OPT_fautomatic:
464       gfc_option.flag_automatic = value;
465       break;
466
467     case OPT_fallow_leading_underscore:
468       gfc_option.flag_allow_leading_underscore = value;
469       break;
470       
471     case OPT_fbackslash:
472       gfc_option.flag_backslash = value;
473       break;
474       
475     case OPT_fbacktrace:
476       gfc_option.flag_backtrace = value;
477       break;
478       
479     case OPT_fdump_core:
480       gfc_option.flag_dump_core = value;
481       break;
482
483     case OPT_fcray_pointer:
484       gfc_option.flag_cray_pointer = value;
485       break;
486
487     case OPT_ff2c:
488       gfc_option.flag_f2c = value;
489       break;
490
491     case OPT_fdollar_ok:
492       gfc_option.flag_dollar_ok = value;
493       break;
494
495     case OPT_fexternal_blas:
496       gfc_option.flag_external_blas = value;
497       break;
498
499     case OPT_fblas_matmul_limit_:
500       gfc_option.blas_matmul_limit = value;
501       break;
502
503     case OPT_fd_lines_as_code:
504       gfc_option.flag_d_lines = 1;
505       break;
506
507     case OPT_fd_lines_as_comments:
508       gfc_option.flag_d_lines = 0;
509       break;
510
511     case OPT_fdump_parse_tree:
512       gfc_option.verbose = value;
513       break;
514
515     case OPT_ffixed_form:
516       gfc_option.source_form = FORM_FIXED;
517       break;
518
519     case OPT_ffixed_line_length_none:
520       gfc_option.fixed_line_length = 0;
521       break;
522
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;
527       break;
528
529     case OPT_ffree_form:
530       gfc_option.source_form = FORM_FREE;
531       break;
532
533     case OPT_fopenmp:
534       gfc_option.flag_openmp = value;
535       break;
536
537     case OPT_ffree_line_length_none:
538       gfc_option.free_line_length = 0;
539       break;
540
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;
545       break;
546
547     case OPT_funderscoring:
548       gfc_option.flag_underscoring = value;
549       break;
550
551     case OPT_fsecond_underscore:
552       gfc_option.flag_second_underscore = value;
553       break;
554
555     case OPT_static_libgfortran:
556 #ifndef HAVE_LD_STATIC_DYNAMIC
557       gfc_fatal_error ("-static-libgfortran is not supported in this "
558                        "configuration");
559 #endif
560       break;
561
562     case OPT_fimplicit_none:
563       gfc_option.flag_implicit_none = value;
564       break;
565
566     case OPT_fintrinsic_modules_path:
567       gfc_add_include_path (arg, false);
568       gfc_add_intrinsic_modules_path (arg);
569       break;
570
571     case OPT_fmax_errors_:
572       gfc_option.max_errors = value;
573       break;
574
575     case OPT_fmax_stack_var_size_:
576       gfc_option.flag_max_stack_var_size = value;
577       break;
578
579     case OPT_fmodule_private:
580       gfc_option.flag_module_private = value;
581       break;
582       
583     case OPT_frange_check:
584       gfc_option.flag_range_check = value;
585       break;
586
587     case OPT_fpack_derived:
588       gfc_option.flag_pack_derived = value;
589       break;
590
591     case OPT_frepack_arrays:
592       gfc_option.flag_repack_arrays = value;
593       break;
594
595     case OPT_fpreprocessed:
596       gfc_option.flag_preprocessed = value;
597       break;
598
599     case OPT_fmax_identifier_length_:
600       if (value > GFC_MAX_SYMBOL_LEN)
601         gfc_fatal_error ("Maximum supported identifier length is %d",
602                          GFC_MAX_SYMBOL_LEN);
603       gfc_option.max_identifier_length = value;
604       break;
605
606     case OPT_fdefault_integer_8:
607       gfc_option.flag_default_integer = value;
608       break;
609
610     case OPT_fdefault_real_8:
611       gfc_option.flag_default_real = value;
612       break;
613
614     case OPT_fdefault_double_8:
615       gfc_option.flag_default_double = value;
616       break;
617
618     case OPT_I:
619       gfc_add_include_path (arg, true);
620       break;
621
622     case OPT_J:
623     case OPT_M:
624       gfc_handle_module_path_options (arg);
625       break;
626     
627     case OPT_fsign_zero:
628       gfc_option.flag_sign_zero = value;
629       break;
630     
631     case OPT_ffpe_trap_:
632       gfc_handle_fpe_trap_option (arg);
633       break;
634
635     case OPT_std_f95:
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;
641       break;
642
643     case OPT_std_f2003:
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;
652       break;
653
654     case OPT_std_gnu:
655       set_default_std_flags ();
656       break;
657
658     case OPT_std_legacy:
659       set_default_std_flags ();
660       gfc_option.warn_std = 0;
661       break;
662
663     case OPT_Wnonstd_intrinsics:
664       gfc_option.warn_nonstd_intrinsics = value;
665       break;
666
667     case OPT_fshort_enums:
668       gfc_option.fshort_enums = 1;
669       break;
670
671     case OPT_fconvert_little_endian:
672       gfc_option.convert = CONVERT_LITTLE;
673       break;
674
675     case OPT_fconvert_big_endian:
676       gfc_option.convert = CONVERT_BIG;
677       break;
678
679     case OPT_fconvert_native:
680       gfc_option.convert = CONVERT_NATIVE;
681       break;
682
683     case OPT_fconvert_swap:
684       gfc_option.convert = CONVERT_SWAP;
685       break;
686
687     case OPT_frecord_marker_4:
688       gfc_option.record_marker = 4;
689       break;
690
691     case OPT_frecord_marker_8:
692       gfc_option.record_marker = 8;
693       break;
694
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);
699
700       gfc_option.max_subrecord_length = value;
701     }
702
703   return result;
704 }