OSDN Git Service

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