OSDN Git Service

* gfortran.h: Add bitmasks for different FPE traps. Add fpe
[pf3gnuchains/gcc-fork.git] / gcc / fortran / options.c
1 /* Parse and display command line options.
2    Copyright (C) 2000, 2001, 2002, 2003, 2004, 2005 Free Software Foundation,
3    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 "tree-inline.h"
33
34 #include "gfortran.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.max_identifier_length = GFC_MAX_SYMBOL_LEN;
50   gfc_option.verbose = 0;
51
52   gfc_option.warn_aliasing = 0;
53   gfc_option.warn_conversion = 0;
54   gfc_option.warn_implicit_interface = 0;
55   gfc_option.warn_line_truncation = 0;
56   gfc_option.warn_underflow = 1;
57   gfc_option.warn_surprising = 0;
58   gfc_option.warn_unused_labels = 0;
59
60   gfc_option.flag_default_double = 0;
61   gfc_option.flag_default_integer = 0;
62   gfc_option.flag_default_real = 0;
63   gfc_option.flag_dollar_ok = 0;
64   gfc_option.flag_underscoring = 1;
65   gfc_option.flag_f2c = 0;
66   gfc_option.flag_second_underscore = -1;
67   gfc_option.flag_implicit_none = 0;
68   gfc_option.flag_max_stack_var_size = 32768;
69   gfc_option.flag_module_access_private = 0;
70   gfc_option.flag_no_backend = 0;
71   gfc_option.flag_pack_derived = 0;
72   gfc_option.flag_repack_arrays = 0;
73   gfc_option.flag_automatic = 1;
74   gfc_option.flag_backslash = 1;
75   gfc_option.flag_d_lines = -1;
76
77   gfc_option.q_kind = gfc_default_double_kind;
78
79   gfc_option.fpe = 0;
80
81   flag_argument_noalias = 2;
82   flag_errno_math = 0;
83
84   gfc_option.allow_std = GFC_STD_F95_OBS | GFC_STD_F95_DEL
85     | GFC_STD_F2003 | GFC_STD_F95 | GFC_STD_F77 | GFC_STD_GNU
86     | GFC_STD_LEGACY;
87   gfc_option.warn_std = GFC_STD_F95_OBS | GFC_STD_F95_DEL
88     | GFC_STD_F2003 | GFC_STD_LEGACY;
89
90   gfc_option.warn_nonstd_intrinsics = 0;
91
92   return CL_Fortran;
93 }
94
95
96 /* Determine the source form from the filename extension.  We assume
97    case insensitivity.  */
98
99 static gfc_source_form
100 form_from_filename (const char *filename)
101 {
102
103   static const struct
104   {
105     const char *extension;
106     gfc_source_form form;
107   }
108   exttype[] =
109   {
110     {
111     ".f90", FORM_FREE}
112     ,
113     {
114     ".f95", FORM_FREE}
115     ,
116     {
117     ".f", FORM_FIXED}
118     ,
119     {
120     ".for", FORM_FIXED}
121     ,
122     {
123     "", FORM_UNKNOWN}
124   };            /* sentinel value */
125
126   gfc_source_form f_form;
127   const char *fileext;
128   int i;
129
130   /* Find end of file name.  Note, filename is either a NULL pointer or
131      a NUL terminated string.  */
132   i = 0;
133   while (filename[i] != '\0')
134     i++;
135
136   /* Find last period.  */
137   while (i >= 0 && (filename[i] != '.'))
138     i--;
139
140   /* Did we see a file extension?  */
141   if (i < 0)
142     return FORM_UNKNOWN; /* Nope  */
143
144   /* Get file extension and compare it to others.  */
145   fileext = &(filename[i]);
146
147   i = -1;
148   f_form = FORM_UNKNOWN;
149   do
150     {
151       i++;
152       if (strcasecmp (fileext, exttype[i].extension) == 0)
153         {
154           f_form = exttype[i].form;
155           break;
156         }
157     }
158   while (exttype[i].form != FORM_UNKNOWN);
159
160   return f_form;
161 }
162
163
164 /* Finalize commandline options.  */
165
166 bool
167 gfc_post_options (const char **pfilename)
168 {
169   const char *filename = *pfilename;
170
171   /* Verify the input file name.  */
172   if (!filename || strcmp (filename, "-") == 0)
173     {
174       filename = "";
175     }
176
177   gfc_source_file = filename;
178
179   /* Decide which form the file will be read in as.  */
180
181   if (gfc_option.source_form != FORM_UNKNOWN)
182     gfc_current_form = gfc_option.source_form;
183   else
184     {
185       gfc_current_form = form_from_filename (filename);
186
187       if (gfc_current_form == FORM_UNKNOWN)
188         {
189           gfc_current_form = FORM_FREE;
190           gfc_warning_now ("Reading file '%s' as free form.", 
191                            (filename[0] == '\0') ? "<stdin>" : filename); 
192         }
193     }
194
195   /* If the user specified -fd-lines-as-{code|comments} verify that we're
196      in fixed form.  */
197   if (gfc_current_form == FORM_FREE)
198     {
199       if (gfc_option.flag_d_lines == 0)
200         gfc_warning_now ("'-fd-lines-as-comments' has no effect "
201                          "in free form.");
202       else if (gfc_option.flag_d_lines == 1)
203         gfc_warning_now ("'-fd-lines-as-code' has no effect "
204                          "in free form.");
205     }
206
207   flag_inline_trees = 1;
208
209   /* Use tree inlining.  */
210   if (!flag_no_inline)
211     flag_no_inline = 1;
212   if (flag_inline_functions)
213     flag_inline_trees = 2;
214
215   /* If -pedantic, warn about the use of GNU extensions.  */
216   if (pedantic && (gfc_option.allow_std & GFC_STD_GNU) != 0)
217     gfc_option.warn_std |= GFC_STD_GNU;
218   /* -std=legacy -pedantic is effectively -std=gnu.  */
219   if (pedantic && (gfc_option.allow_std & GFC_STD_LEGACY) != 0)
220     gfc_option.warn_std |= GFC_STD_F95_OBS | GFC_STD_F95_DEL | GFC_STD_LEGACY;
221
222   /* If the user didn't explicitly specify -f(no)-second-underscore we
223      use it if we're trying to be compatible with f2c, and not
224      otherwise.  */
225   if (gfc_option.flag_second_underscore == -1)
226     gfc_option.flag_second_underscore = gfc_option.flag_f2c;
227
228   /* Implement -fno-automatic as -fmax-stack-var-size=0.  */
229   if (!gfc_option.flag_automatic)
230     gfc_option.flag_max_stack_var_size = 0;
231
232   return false;
233 }
234
235
236 /* Set the options for -Wall.  */
237
238 static void
239 set_Wall (void)
240 {
241
242   gfc_option.warn_aliasing = 1;
243   gfc_option.warn_line_truncation = 1;
244   gfc_option.warn_underflow = 1;
245   gfc_option.warn_surprising = 1;
246   gfc_option.warn_unused_labels = 1;
247   gfc_option.warn_nonstd_intrinsics = 1;
248
249   set_Wunused (1);
250   warn_return_type = 1;
251   warn_switch = 1;
252
253   /* We save the value of warn_uninitialized, since if they put
254      -Wuninitialized on the command line, we need to generate a
255      warning about not using it without also specifying -O.  */
256
257   if (warn_uninitialized != 1)
258     warn_uninitialized = 2;
259 }
260
261
262 static void
263 gfc_handle_module_path_options (const char *arg)
264 {
265
266   if (gfc_option.module_dir != NULL)
267     {
268       gfc_status ("gfortran: Only one -M option allowed\n");
269       exit (3);
270     }
271
272   if (arg == NULL)
273     {
274       gfc_status ("gfortran: Directory required after -M\n");
275       exit (3);
276     }
277
278   gfc_option.module_dir = (char *) gfc_getmem (strlen (arg) + 2);
279   strcpy (gfc_option.module_dir, arg);
280   strcat (gfc_option.module_dir, "/");
281 }
282
283 static void
284 gfc_handle_fpe_trap_option (const char *arg)
285 {
286   int result, pos = 0, n;
287   static const char * const exception[] = { "invalid", "denormal", "zero",
288                                             "overflow", "underflow",
289                                             "precision", NULL };
290   static const int opt_exception[] = { GFC_FPE_INVALID, GFC_FPE_DENORMAL,
291                                        GFC_FPE_ZERO, GFC_FPE_OVERFLOW,
292                                        GFC_FPE_UNDERFLOW, GFC_FPE_PRECISION,
293                                        0 };
294  
295   while (*arg)
296     {
297       while (*arg == ',')
298         arg++;
299       while (arg[pos] && arg[pos] != ',')
300         pos++;
301       result = 0;
302       for (n = 0; exception[n] != NULL; n++)
303         {
304           if (exception[n] && strncmp (exception[n], arg, pos) == 0)
305             {
306               gfc_option.fpe |= opt_exception[n];
307               arg += pos;
308               pos = 0;
309               result = 1;
310               break;
311             }
312         }
313       if (! result)
314         gfc_fatal_error ("Argument to -ffpe-trap is not valid: %s", arg);
315     }
316 }
317
318 /* Handle command-line options.  Returns 0 if unrecognized, 1 if
319    recognized and handled.  */
320 int
321 gfc_handle_option (size_t scode, const char *arg, int value)
322 {
323   int result = 1;
324   enum opt_code code = (enum opt_code) scode;
325
326   /* Ignore file names.  */
327   if (code == N_OPTS)
328     return 1;
329
330   switch (code)
331     {
332     default:
333       result = 0;
334       break;
335
336     case OPT_Wall:
337       set_Wall ();
338       break;
339
340     case OPT_Waliasing:
341       gfc_option.warn_aliasing = value;
342       break;
343
344     case OPT_Wconversion:
345       gfc_option.warn_conversion = value;
346       break;
347
348     case OPT_Wimplicit_interface:
349       gfc_option.warn_implicit_interface = value;
350       break;
351
352     case OPT_Wline_truncation:
353       gfc_option.warn_line_truncation = value;
354       break;
355
356     case OPT_Wunderflow:
357       gfc_option.warn_underflow = value;
358       break;
359
360     case OPT_Wsurprising:
361       gfc_option.warn_surprising = value;
362       break;
363
364     case OPT_Wunused_labels:
365       gfc_option.warn_unused_labels = value;
366       break;
367
368     case OPT_ff2c:
369       gfc_option.flag_f2c = value;
370       break;
371
372     case OPT_fdollar_ok:
373       gfc_option.flag_dollar_ok = value;
374       break;
375
376     case OPT_fautomatic:
377       gfc_option.flag_automatic = value;
378       break;
379
380     case OPT_fbackslash:
381       gfc_option.flag_backslash = value;
382       break;
383
384     case OPT_fd_lines_as_code:
385       gfc_option.flag_d_lines = 1;
386       break;
387
388     case OPT_fd_lines_as_comments:
389       gfc_option.flag_d_lines = 0;
390       break;
391
392     case OPT_fdump_parse_tree:
393       gfc_option.verbose = value;
394       break;
395
396     case OPT_ffixed_form:
397       gfc_option.source_form = FORM_FIXED;
398       break;
399
400     case OPT_ffree_form:
401       gfc_option.source_form = FORM_FREE;
402       break;
403
404     case OPT_funderscoring:
405       gfc_option.flag_underscoring = value;
406       break;
407
408     case OPT_fsecond_underscore:
409       gfc_option.flag_second_underscore = value;
410       break;
411
412     case OPT_fimplicit_none:
413       gfc_option.flag_implicit_none = value;
414       break;
415
416     case OPT_fmax_stack_var_size_:
417       gfc_option.flag_max_stack_var_size = value;
418       break;
419
420     case OPT_fmodule_private:
421       gfc_option.flag_module_access_private = value;
422       break;
423
424     case OPT_fno_backend:
425       gfc_option.flag_no_backend = value;
426       break;
427
428     case OPT_fpack_derived:
429       gfc_option.flag_pack_derived = value;
430       break;
431
432     case OPT_frepack_arrays:
433       gfc_option.flag_repack_arrays = value;
434       break;
435
436     case OPT_ffixed_line_length_none:
437       gfc_option.fixed_line_length = 0;
438       break;
439
440     case OPT_ffixed_line_length_:
441       if (value != 0 && value < 7)
442         gfc_fatal_error ("Fixed line length must be at least seven.");
443       gfc_option.fixed_line_length = value;
444       break;
445
446     case OPT_fmax_identifier_length_:
447       if (value > GFC_MAX_SYMBOL_LEN)
448         gfc_fatal_error ("Maximum supported idenitifier length is %d",
449                          GFC_MAX_SYMBOL_LEN);
450       gfc_option.max_identifier_length = value;
451       break;
452
453     case OPT_qkind_:
454       if (gfc_validate_kind (BT_REAL, value, true) < 0)
455         gfc_fatal_error ("Argument to -fqkind isn't a valid real kind");
456       gfc_option.q_kind = value;
457       break;
458
459     case OPT_fdefault_integer_8:
460       gfc_option.flag_default_integer = value;
461       break;
462
463     case OPT_fdefault_real_8:
464       gfc_option.flag_default_real = value;
465       break;
466
467     case OPT_fdefault_double_8:
468       gfc_option.flag_default_double = value;
469       break;
470
471     case OPT_I:
472       gfc_add_include_path (arg);
473       break;
474
475     case OPT_J:
476     case OPT_M:
477       gfc_handle_module_path_options (arg);
478       break;
479     
480     case OPT_ffpe_trap_:
481       gfc_handle_fpe_trap_option (arg);
482       break;
483
484     case OPT_std_f95:
485       gfc_option.allow_std = GFC_STD_F95_OBS | GFC_STD_F95 | GFC_STD_F77;
486       gfc_option.warn_std = GFC_STD_F95_OBS;
487       gfc_option.max_identifier_length = 31;
488       break;
489
490     case OPT_std_f2003:
491       gfc_option.allow_std = GFC_STD_F95_OBS | GFC_STD_F77 
492         | GFC_STD_F2003 | GFC_STD_F95;
493       gfc_option.warn_std = GFC_STD_F95_OBS;
494       gfc_option.max_identifier_length = 63;
495       break;
496
497     case OPT_std_gnu:
498       gfc_option.allow_std = GFC_STD_F95_OBS | GFC_STD_F95_DEL
499         | GFC_STD_F77 | GFC_STD_F95 | GFC_STD_F2003
500         | GFC_STD_GNU | GFC_STD_LEGACY;
501       gfc_option.warn_std = GFC_STD_F95_OBS | GFC_STD_F95_DEL
502         | GFC_STD_LEGACY;
503       break;
504
505     case OPT_std_legacy:
506       gfc_option.allow_std = GFC_STD_F95_OBS | GFC_STD_F95_DEL
507         | GFC_STD_F77 | GFC_STD_F95 | GFC_STD_F2003
508         | GFC_STD_GNU | GFC_STD_LEGACY;
509       gfc_option.warn_std = 0;
510       break;
511
512     case OPT_Wnonstd_intrinsics:
513       gfc_option.warn_nonstd_intrinsics = 1;
514       break;
515     }
516
517   return result;
518 }