OSDN Git Service

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