OSDN Git Service

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