]> git.ipfire.org Git - thirdparty/gcc.git/blame - gcc/fortran/options.cc
Update copyright years.
[thirdparty/gcc.git] / gcc / fortran / options.cc
CommitLineData
6de9cd9a 1/* Parse and display command line options.
a945c346 2 Copyright (C) 2000-2024 Free Software Foundation, Inc.
6de9cd9a
DN
3 Contributed by Andy Vaught
4
9fc4d79b 5This file is part of GCC.
6de9cd9a 6
9fc4d79b
TS
7GCC is free software; you can redistribute it and/or modify it under
8the terms of the GNU General Public License as published by the Free
d234d788 9Software Foundation; either version 3, or (at your option) any later
9fc4d79b 10version.
6de9cd9a 11
9fc4d79b
TS
12GCC is distributed in the hope that it will be useful, but WITHOUT ANY
13WARRANTY; without even the implied warranty of MERCHANTABILITY or
14FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15for more details.
6de9cd9a
DN
16
17You should have received a copy of the GNU General Public License
d234d788
NC
18along with GCC; see the file COPYING3. If not see
19<http://www.gnu.org/licenses/>. */
6de9cd9a 20
6de9cd9a
DN
21#include "config.h"
22#include "system.h"
23#include "coretypes.h"
2adfab87 24#include "target.h"
6de9cd9a 25#include "tree.h"
2adfab87
AM
26#include "gfortran.h"
27#include "diagnostic.h" /* For global_dc. */
6de9cd9a 28#include "opts.h"
41804a5b 29#include "toplev.h" /* For save_decoded_options. */
670637ee 30#include "cpp.h"
de3aebff 31#include "langhooks.h"
6de9cd9a
DN
32
33gfc_option_t gfc_option;
34
7ed760c7
FR
35#define SET_FLAG(flag, condition, on_value, off_value) \
36 do \
37 { \
38 if (condition) \
39 flag = (on_value); \
40 else \
41 flag = (off_value); \
42 } while (0)
43
44#define SET_BITFLAG2(m) m
45
46#define SET_BITFLAG(flag, condition, value) \
47 SET_BITFLAG2 (SET_FLAG (flag, condition, (flag | (value)), (flag & ~(value))))
48
6de9cd9a 49
a47eec17 50/* Set flags that control warnings and errors for different
5fb41e29
TB
51 Fortran standards to their default values. Keep in sync with
52 libgfortran/runtime/compile_options.c (init_compile_options). */
a47eec17
JD
53
54static void
55set_default_std_flags (void)
56{
57 gfc_option.allow_std = GFC_STD_F95_OBS | GFC_STD_F95_DEL
f489fba1 58 | GFC_STD_F2003 | GFC_STD_F2008 | GFC_STD_F95 | GFC_STD_F77
286f737c 59 | GFC_STD_F2008_OBS | GFC_STD_GNU | GFC_STD_LEGACY
0c2ecfd4
HA
60 | GFC_STD_F2018 | GFC_STD_F2018_DEL | GFC_STD_F2018_OBS | GFC_STD_F2023
61 | GFC_STD_F2023_DEL;
62 gfc_option.warn_std = GFC_STD_F2018_DEL | GFC_STD_F95_DEL | GFC_STD_LEGACY
63 | GFC_STD_F2023_DEL;
a47eec17
JD
64}
65
7ed760c7 66/* Set (or unset) the DEC extension flags. */
f6288c24
FR
67
68static void
69set_dec_flags (int value)
70{
7ed760c7
FR
71 /* Set (or unset) other DEC compatibility extensions. */
72 SET_BITFLAG (flag_dollar_ok, value, value);
73 SET_BITFLAG (flag_cray_pointer, value, value);
74 SET_BITFLAG (flag_dec_structure, value, value);
75 SET_BITFLAG (flag_dec_intrinsic_ints, value, value);
76 SET_BITFLAG (flag_dec_static, value, value);
77 SET_BITFLAG (flag_dec_math, value, value);
78 SET_BITFLAG (flag_dec_include, value, value);
88a8126a 79 SET_BITFLAG (flag_dec_format_defaults, value, value);
17f62b7e 80 SET_BITFLAG (flag_dec_blank_format_item, value, value);
2afeb1ca 81 SET_BITFLAG (flag_dec_char_conversions, value, value);
7ed760c7
FR
82}
83
84/* Finalize DEC flags. */
85
86static void
87post_dec_flags (int value)
88{
89 /* Don't warn for legacy code if -fdec is given; however, setting -fno-dec
90 does not force these warnings. We make one final determination on this
91 at the end because -std= is always set first; thus, we can avoid
92 clobbering the user's desired standard settings in gfc_handle_option
93 e.g. when -fdec and -fno-dec are both given. */
7c23ac0d
FR
94 if (value)
95 {
7c23ac0d 96 gfc_option.allow_std |= GFC_STD_F95_OBS | GFC_STD_F95_DEL
7ed760c7 97 | GFC_STD_GNU | GFC_STD_LEGACY;
7c23ac0d
FR
98 gfc_option.warn_std &= ~(GFC_STD_LEGACY | GFC_STD_F95_DEL);
99 }
f6288c24
FR
100}
101
7ed760c7
FR
102/* Enable (or disable) -finit-local-zero. */
103
104static void
105set_init_local_zero (int value)
106{
107 gfc_option.flag_init_integer_value = 0;
108 gfc_option.flag_init_character_value = (char)0;
109
110 SET_FLAG (gfc_option.flag_init_integer, value, GFC_INIT_INTEGER_ON,
111 GFC_INIT_INTEGER_OFF);
112 SET_FLAG (gfc_option.flag_init_logical, value, GFC_INIT_LOGICAL_FALSE,
113 GFC_INIT_LOGICAL_OFF);
114 SET_FLAG (gfc_option.flag_init_character, value, GFC_INIT_CHARACTER_ON,
115 GFC_INIT_CHARACTER_OFF);
116 SET_FLAG (flag_init_real, value, GFC_INIT_REAL_ZERO, GFC_INIT_REAL_OFF);
117}
f6288c24 118
7a9bf9a4
JM
119/* Return language mask for Fortran options. */
120
121unsigned int
122gfc_option_lang_mask (void)
123{
124 return CL_Fortran;
125}
126
a75bfaa6
JM
127/* Initialize options structure OPTS. */
128
129void
130gfc_init_options_struct (struct gcc_options *opts)
131{
132 opts->x_flag_errno_math = 0;
321bff11 133 opts->frontend_set_flag_errno_math = true;
a75bfaa6 134 opts->x_flag_associative_math = -1;
321bff11 135 opts->frontend_set_flag_associative_math = true;
a75bfaa6 136}
7a9bf9a4 137
5fb41e29 138/* Get ready for options handling. Keep in sync with
1cc0e193 139 libgfortran/runtime/compile_options.c (init_compile_options). */
6de9cd9a 140
7a9bf9a4
JM
141void
142gfc_init_options (unsigned int decoded_options_count,
143 struct cl_decoded_option *decoded_options)
6de9cd9a 144{
e0bcf78c 145 gfc_source_file = NULL;
6de9cd9a
DN
146 gfc_option.module_dir = NULL;
147 gfc_option.source_form = FORM_UNKNOWN;
b9eba3ba
TB
148 /* The following is not quite right as Fortran since 2023 has: "A statement
149 shall not have more than one million characters." This can already be
150 reached by 'just' 100 lines with 10,000 characters each. */
151 gfc_option.max_continue_fixed = 1000000;
152 gfc_option.max_continue_free = 1000000;
6de9cd9a 153 gfc_option.max_identifier_length = GFC_MAX_SYMBOL_LEN;
3f139fcf 154 gfc_option.max_errors = 25;
6de9cd9a 155
2d7c7df6 156 gfc_option.flag_preprocessed = 0;
e0bcf78c 157 gfc_option.flag_d_lines = -1;
c3e799e1 158 set_init_local_zero (0);
51b09ce3 159
944b8b35 160 gfc_option.fpe = 0;
fa86f4f9
TB
161 /* All except GFC_FPE_INEXACT. */
162 gfc_option.fpe_summary = GFC_FPE_INVALID | GFC_FPE_DENORMAL
163 | GFC_FPE_ZERO | GFC_FPE_OVERFLOW
164 | GFC_FPE_UNDERFLOW;
d3d3011f 165 gfc_option.rtcheck = 0;
944b8b35 166
f6288c24 167 set_dec_flags (0);
a47eec17 168 set_default_std_flags ();
6de9cd9a 169
670637ee 170 /* Initialize cpp-related options. */
7a9bf9a4 171 gfc_cpp_init_options (decoded_options_count, decoded_options);
8e54f6d3 172 gfc_diagnostics_init ();
6de9cd9a
DN
173}
174
175
e0bcf78c
TS
176/* Determine the source form from the filename extension. We assume
177 case insensitivity. */
178
179static gfc_source_form
180form_from_filename (const char *filename)
181{
e0bcf78c
TS
182 static const struct
183 {
184 const char *extension;
185 gfc_source_form form;
186 }
187 exttype[] =
188 {
189 {
190 ".f90", FORM_FREE}
191 ,
192 {
193 ".f95", FORM_FREE}
194 ,
195 {
7befd5d2
TB
196 ".f03", FORM_FREE}
197 ,
198 {
f489fba1
FXC
199 ".f08", FORM_FREE}
200 ,
201 {
ba615557
RJ
202 ".fii", FORM_FREE}
203 ,
204 {
205 ".fi", FORM_FIXED}
206 ,
207 {
e0bcf78c
TS
208 ".f", FORM_FIXED}
209 ,
210 {
211 ".for", FORM_FIXED}
212 ,
213 {
1200489c
TB
214 ".ftn", FORM_FIXED}
215 ,
216 {
e0bcf78c
TS
217 "", FORM_UNKNOWN}
218 }; /* sentinel value */
219
220 gfc_source_form f_form;
221 const char *fileext;
222 int i;
223
224 /* Find end of file name. Note, filename is either a NULL pointer or
225 a NUL terminated string. */
226 i = 0;
227 while (filename[i] != '\0')
228 i++;
229
230 /* Find last period. */
231 while (i >= 0 && (filename[i] != '.'))
232 i--;
233
234 /* Did we see a file extension? */
235 if (i < 0)
236 return FORM_UNKNOWN; /* Nope */
237
238 /* Get file extension and compare it to others. */
239 fileext = &(filename[i]);
240
241 i = -1;
242 f_form = FORM_UNKNOWN;
243 do
244 {
245 i++;
246 if (strcasecmp (fileext, exttype[i].extension) == 0)
247 {
248 f_form = exttype[i].form;
249 break;
250 }
251 }
252 while (exttype[i].form != FORM_UNKNOWN);
253
254 return f_form;
255}
256
257
6de9cd9a
DN
258/* Finalize commandline options. */
259
260bool
261gfc_post_options (const char **pfilename)
262{
2d7c7df6 263 const char *filename = *pfilename, *canon_source_file = NULL;
b424a572 264 char *source_path;
83aac698 265 bool verbose_missing_dir_warn;
b424a572 266 int i;
6de9cd9a 267
417ea5c0
TB
268 /* This needs to be after the commandline has been processed.
269 In Fortran, the options is by default enabled, in C/C++
83aac698
TB
270 by default disabled.
271 If not enabled explicitly by the user, only warn for -I
272 and -J, otherwise warn for all include paths. */
273 verbose_missing_dir_warn
00f34291 274 = (OPTION_SET_P (cpp_warn_missing_include_dirs)
83aac698 275 && global_options.x_cpp_warn_missing_include_dirs);
417ea5c0
TB
276 SET_OPTION_IF_UNSET (&global_options, &global_options_set,
277 cpp_warn_missing_include_dirs, 1);
83aac698 278 gfc_check_include_dirs (verbose_missing_dir_warn);
417ea5c0 279
b9eba3ba
TB
280 SET_OPTION_IF_UNSET (&global_options, &global_options_set,
281 flag_free_line_length,
282 (gfc_option.allow_std & GFC_STD_F2023) ? 10000 : 132);
283
c3e799e1
FR
284 /* Finalize DEC flags. */
285 post_dec_flags (flag_dec);
286
8ce94e44
JM
287 /* Excess precision other than "fast" requires front-end
288 support. */
df375b03 289 if (flag_excess_precision == EXCESS_PRECISION_STANDARD)
a3f9f006 290 sorry ("%<-fexcess-precision=standard%> for Fortran");
f19a3270 291 else if (flag_excess_precision == EXCESS_PRECISION_FLOAT16)
292 sorry ("%<-fexcess-precision=16%> for Fortran");
293
df375b03 294 flag_excess_precision = EXCESS_PRECISION_FAST;
8ce94e44 295
feef2551
TB
296 /* Fortran allows associative math - but we cannot reassociate if
297 we want traps or signed zeros. Cf. also flag_protect_parens. */
298 if (flag_associative_math == -1)
299 flag_associative_math = (!flag_trapping_math && !flag_signed_zeros);
300
203c7ebf
TB
301 if (flag_protect_parens == -1)
302 flag_protect_parens = !optimize_fast;
7a994646 303
950a884b
TK
304 /* -Ofast sets implies -fstack-arrays unless an explicit size is set for
305 stack arrays. */
306 if (flag_stack_arrays == -1 && flag_max_stack_var_size == -2)
203c7ebf 307 flag_stack_arrays = optimize_fast;
2d8c9ad5 308
597553ab 309 /* By default, disable (re)allocation during assignment for -std=f95,
1cc0e193 310 and enable it for F2003/F2008/GNU/Legacy. */
203c7ebf 311 if (flag_realloc_lhs == -1)
597553ab
PT
312 {
313 if (gfc_option.allow_std & GFC_STD_F2003)
203c7ebf 314 flag_realloc_lhs = 1;
597553ab 315 else
203c7ebf 316 flag_realloc_lhs = 0;
597553ab
PT
317 }
318
d3d3011f
FXC
319 /* -fbounds-check is equivalent to -fcheck=bounds */
320 if (flag_bounds_check)
321 gfc_option.rtcheck |= GFC_RTCHECK_BOUNDS;
322
2153915d 323 if (flag_compare_debug)
c61819ff 324 flag_dump_fortran_original = 0;
2153915d 325
3a789837 326 /* Make -fmax-errors visible to gfortran's diagnostic machinery. */
00f34291 327 if (OPTION_SET_P (flag_max_errors))
3a789837
NF
328 gfc_option.max_errors = flag_max_errors;
329
6de9cd9a
DN
330 /* Verify the input file name. */
331 if (!filename || strcmp (filename, "-") == 0)
332 {
333 filename = "";
334 }
335
2d7c7df6
JJ
336 if (gfc_option.flag_preprocessed)
337 {
338 /* For preprocessed files, if the first tokens are of the form # NUM.
339 handle the directives so we know the original file name. */
340 gfc_source_file = gfc_read_orig_filename (filename, &canon_source_file);
341 if (gfc_source_file == NULL)
342 gfc_source_file = filename;
343 else
344 *pfilename = gfc_source_file;
345 }
346 else
347 gfc_source_file = filename;
348
349 if (canon_source_file == NULL)
350 canon_source_file = gfc_source_file;
e0bcf78c 351
b424a572
FXC
352 /* Adds the path where the source file is to the list of include files. */
353
2d7c7df6
JJ
354 i = strlen (canon_source_file);
355 while (i > 0 && !IS_DIR_SEPARATOR (canon_source_file[i]))
b424a572 356 i--;
edf1eac2 357
b424a572
FXC
358 if (i != 0)
359 {
ece3f663 360 source_path = (char *) alloca (i + 1);
2d7c7df6 361 memcpy (source_path, canon_source_file, i);
b424a572 362 source_path[i] = 0;
83aac698
TB
363 /* Only warn if the directory is different from the input file as
364 if that one is not found, already an error is shown. */
365 bool warn = gfc_option.flag_preprocessed && gfc_source_file != filename;
366 gfc_add_include_path (source_path, true, true, warn, false);
b424a572
FXC
367 }
368 else
83aac698 369 gfc_add_include_path (".", true, true, false, false);
b424a572 370
2d7c7df6 371 if (canon_source_file != gfc_source_file)
cede9502 372 free (CONST_CAST (char *, canon_source_file));
2d7c7df6 373
e0bcf78c
TS
374 /* Decide which form the file will be read in as. */
375
376 if (gfc_option.source_form != FORM_UNKNOWN)
377 gfc_current_form = gfc_option.source_form;
378 else
379 {
380 gfc_current_form = form_from_filename (filename);
381
382 if (gfc_current_form == FORM_UNKNOWN)
383 {
384 gfc_current_form = FORM_FREE;
4c35d099 385 main_input_filename = filename;
db30e21c 386 gfc_warning_now (0, "Reading file %qs as free form",
4daa149b 387 (filename[0] == '\0') ? "<stdin>" : filename);
e0bcf78c
TS
388 }
389 }
390
391 /* If the user specified -fd-lines-as-{code|comments} verify that we're
392 in fixed form. */
393 if (gfc_current_form == FORM_FREE)
394 {
395 if (gfc_option.flag_d_lines == 0)
db30e21c 396 gfc_warning_now (0, "%<-fd-lines-as-comments%> has no effect "
a56abdcc 397 "in free form");
e0bcf78c 398 else if (gfc_option.flag_d_lines == 1)
db30e21c 399 gfc_warning_now (0, "%<-fd-lines-as-code%> has no effect in free form");
aecfd035
TB
400
401 if (warn_line_truncation == -1)
402 warn_line_truncation = 1;
403
404 /* Enable -Werror=line-truncation when -Werror and -Wno-error have
405 not been set. */
00f34291 406 if (warn_line_truncation && !OPTION_SET_P (warnings_are_errors)
8200cd97 407 && option_unspecified_p (OPT_Wline_truncation))
aecfd035
TB
408 diagnostic_classify_diagnostic (global_dc, OPT_Wline_truncation,
409 DK_ERROR, UNKNOWN_LOCATION);
e0bcf78c 410 }
57e1270f
FR
411 else
412 {
413 /* With -fdec, set -fd-lines-as-comments by default in fixed form. */
414 if (flag_dec && gfc_option.flag_d_lines == -1)
415 gfc_option.flag_d_lines = 0;
416
417 if (warn_line_truncation == -1)
418 warn_line_truncation = 0;
419 }
6de9cd9a 420
fc2d8429
RS
421 /* If -pedantic, warn about the use of GNU extensions. */
422 if (pedantic && (gfc_option.allow_std & GFC_STD_GNU) != 0)
423 gfc_option.warn_std |= GFC_STD_GNU;
c0309c74
RS
424 /* -std=legacy -pedantic is effectively -std=gnu. */
425 if (pedantic && (gfc_option.allow_std & GFC_STD_LEGACY) != 0)
426 gfc_option.warn_std |= GFC_STD_F95_OBS | GFC_STD_F95_DEL | GFC_STD_LEGACY;
fc2d8429 427
973ff4c0
TS
428 /* If the user didn't explicitly specify -f(no)-second-underscore we
429 use it if we're trying to be compatible with f2c, and not
430 otherwise. */
203c7ebf 431 if (flag_second_underscore == -1)
c61819ff 432 flag_second_underscore = flag_f2c;
973ff4c0 433
c61819ff 434 if (!flag_automatic && flag_max_stack_var_size != -2
203c7ebf 435 && flag_max_stack_var_size != 0)
db30e21c 436 gfc_warning_now (0, "Flag %<-fno-automatic%> overwrites %<-fmax-stack-var-size=%d%>",
203c7ebf 437 flag_max_stack_var_size);
c61819ff 438 else if (!flag_automatic && flag_recursive)
91c4891a
ME
439 gfc_warning_now (OPT_Woverwrite_recursive, "Flag %<-fno-automatic%> "
440 "overwrites %<-frecursive%>");
f4e7ea81
TB
441 else if (!flag_automatic && (flag_openmp || flag_openacc))
442 gfc_warning_now (0, "Flag %<-fno-automatic%> overwrites %<-frecursive%> "
443 "implied by %qs", flag_openmp ? "-fopenmp" : "-fopenacc");
c61819ff 444 else if (flag_max_stack_var_size != -2 && flag_recursive)
db30e21c 445 gfc_warning_now (0, "Flag %<-frecursive%> overwrites %<-fmax-stack-var-size=%d%>",
203c7ebf 446 flag_max_stack_var_size);
f4e7ea81
TB
447 else if (flag_max_stack_var_size != -2 && (flag_openmp || flag_openacc))
448 gfc_warning_now (0, "Flag %<-fmax-stack-var-size=%d%> overwrites "
449 "%<-frecursive%> implied by %qs", flag_max_stack_var_size,
450 flag_openmp ? "-fopenmp" : "-fopenacc");
1e7de83b 451
653f6dc5 452 /* Implement -frecursive as -fmax-stack-var-size=-1. */
c61819ff 453 if (flag_recursive)
203c7ebf 454 flag_max_stack_var_size = -1;
1e7de83b 455
653f6dc5 456 /* Implied -frecursive; implemented as -fmax-stack-var-size=-1. */
f4e7ea81
TB
457 if (flag_max_stack_var_size == -2 && flag_automatic
458 && (flag_openmp || flag_openacc))
653f6dc5 459 {
c61819ff 460 flag_recursive = 1;
203c7ebf 461 flag_max_stack_var_size = -1;
653f6dc5
TB
462 }
463
950a884b
TK
464 /* Set flag_stack_arrays correctly. */
465 if (flag_stack_arrays == -1)
466 flag_stack_arrays = 0;
467
1e7de83b 468 /* Set default. */
203c7ebf 469 if (flag_max_stack_var_size == -2)
54320207 470 flag_max_stack_var_size = 65536;
1e7de83b 471
d05d9ac7 472 /* Implement -fno-automatic as -fmax-stack-var-size=0. */
c61819ff 473 if (!flag_automatic)
203c7ebf 474 flag_max_stack_var_size = 0;
3fbab549 475
c7b608a9
TK
476 /* If the user did not specify an inline matmul limit, inline up to the BLAS
477 limit or up to 30 if no external BLAS is specified. */
f1abbf69 478
c7b608a9
TK
479 if (flag_inline_matmul_limit < 0)
480 {
481 if (flag_external_blas)
482 flag_inline_matmul_limit = flag_blas_matmul_limit;
483 else
484 flag_inline_matmul_limit = 30;
485 }
f1abbf69 486
51a30b32
TK
487 /* Optimization implies front end optimization, unless the user
488 specified it directly. */
489
203c7ebf 490 if (flag_frontend_optimize == -1)
bf9197df 491 flag_frontend_optimize = optimize && !optimize_debug;
203c7ebf 492
d88412fc
TK
493 /* Same for front end loop interchange. */
494
495 if (flag_frontend_loop_interchange == -1)
496 flag_frontend_loop_interchange = optimize;
497
95d27703
TK
498 /* Do inline packing by default if optimizing, but not if
499 optimizing for size. */
500 if (flag_inline_arg_packing == -1)
501 flag_inline_arg_packing = optimize && !optimize_size;
502
c61819ff
TB
503 if (flag_max_array_constructor < 65535)
504 flag_max_array_constructor = 65535;
505
203c7ebf
TB
506 if (flag_fixed_line_length != 0 && flag_fixed_line_length < 7)
507 gfc_fatal_error ("Fixed line length must be at least seven");
508
509 if (flag_free_line_length != 0 && flag_free_line_length < 4)
510 gfc_fatal_error ("Free line length must be at least three");
511
512 if (flag_max_subrecord_length > MAX_SUBRECORD_LENGTH)
513 gfc_fatal_error ("Maximum subrecord length cannot exceed %d",
514 MAX_SUBRECORD_LENGTH);
51a30b32 515
83aac698 516 gfc_cpp_post_options (verbose_missing_dir_warn);
670637ee 517
de3aebff
JJ
518 if (gfc_option.allow_std & GFC_STD_F2008)
519 lang_hooks.name = "GNU Fortran2008";
520 else if (gfc_option.allow_std & GFC_STD_F2003)
521 lang_hooks.name = "GNU Fortran2003";
522
566916e6 523 return gfc_cpp_preprocess_only ();
6de9cd9a
DN
524}
525
526
6de9cd9a
DN
527static void
528gfc_handle_module_path_options (const char *arg)
529{
530
531 if (gfc_option.module_dir != NULL)
ddc05d11 532 gfc_fatal_error ("gfortran: Only one %<-J%> option allowed");
6de9cd9a 533
93acb62c 534 gfc_option.module_dir = XCNEWVEC (char, strlen (arg) + 2);
6de9cd9a 535 strcpy (gfc_option.module_dir, arg);
7376d710 536
417ea5c0 537 gfc_add_include_path (gfc_option.module_dir, true, false, true, true);
22b00a9f
TB
538
539 strcat (gfc_option.module_dir, "/");
6de9cd9a
DN
540}
541
edf1eac2 542
fa86f4f9
TB
543/* Handle options -ffpe-trap= and -ffpe-summary=. */
544
944b8b35 545static void
fa86f4f9 546gfc_handle_fpe_option (const char *arg, bool trap)
944b8b35
FXC
547{
548 int result, pos = 0, n;
57b4d355 549 /* precision is a backwards compatibility alias for inexact. */
944b8b35 550 static const char * const exception[] = { "invalid", "denormal", "zero",
edf1eac2 551 "overflow", "underflow",
57b4d355 552 "inexact", "precision", NULL };
944b8b35
FXC
553 static const int opt_exception[] = { GFC_FPE_INVALID, GFC_FPE_DENORMAL,
554 GFC_FPE_ZERO, GFC_FPE_OVERFLOW,
57b4d355
JB
555 GFC_FPE_UNDERFLOW, GFC_FPE_INEXACT,
556 GFC_FPE_INEXACT,
944b8b35 557 0 };
fa86f4f9 558
1cc0e193 559 /* As the default for -ffpe-summary= is nonzero, set it to 0. */
fa86f4f9
TB
560 if (!trap)
561 gfc_option.fpe_summary = 0;
562
944b8b35
FXC
563 while (*arg)
564 {
565 while (*arg == ',')
566 arg++;
edf1eac2 567
944b8b35
FXC
568 while (arg[pos] && arg[pos] != ',')
569 pos++;
edf1eac2 570
944b8b35 571 result = 0;
458c253c 572 if (strncmp ("none", arg, pos) == 0)
944b8b35 573 {
458c253c
HA
574 if (trap)
575 gfc_option.fpe = 0;
576 else
577 gfc_option.fpe_summary = 0;
fa86f4f9
TB
578 arg += pos;
579 pos = 0;
580 continue;
581 }
582 else if (!trap && strncmp ("all", arg, pos) == 0)
583 {
584 gfc_option.fpe_summary = GFC_FPE_INVALID | GFC_FPE_DENORMAL
585 | GFC_FPE_ZERO | GFC_FPE_OVERFLOW
586 | GFC_FPE_UNDERFLOW | GFC_FPE_INEXACT;
587 arg += pos;
588 pos = 0;
589 continue;
590 }
591 else
592 for (n = 0; exception[n] != NULL; n++)
593 {
944b8b35
FXC
594 if (exception[n] && strncmp (exception[n], arg, pos) == 0)
595 {
fa86f4f9
TB
596 if (trap)
597 gfc_option.fpe |= opt_exception[n];
598 else
599 gfc_option.fpe_summary |= opt_exception[n];
944b8b35
FXC
600 arg += pos;
601 pos = 0;
602 result = 1;
603 break;
604 }
fa86f4f9 605 }
458c253c 606 if (!result && trap)
ddc05d11 607 gfc_fatal_error ("Argument to %<-ffpe-trap%> is not valid: %s", arg);
fa86f4f9 608 else if (!result)
ddc05d11 609 gfc_fatal_error ("Argument to %<-ffpe-summary%> is not valid: %s", arg);
fa86f4f9 610
944b8b35
FXC
611 }
612}
613
edf1eac2 614
d3d3011f
FXC
615static void
616gfc_handle_runtime_check_option (const char *arg)
617{
618 int result, pos = 0, n;
619 static const char * const optname[] = { "all", "bounds", "array-temps",
22bdbb0f 620 "recursion", "do", "pointer",
df1afcca 621 "mem", "bits", NULL };
d3d3011f
FXC
622 static const int optmask[] = { GFC_RTCHECK_ALL, GFC_RTCHECK_BOUNDS,
623 GFC_RTCHECK_ARRAY_TEMPS,
33abc845 624 GFC_RTCHECK_RECURSION, GFC_RTCHECK_DO,
22bdbb0f 625 GFC_RTCHECK_POINTER, GFC_RTCHECK_MEM,
df1afcca 626 GFC_RTCHECK_BITS, 0 };
d3d3011f
FXC
627
628 while (*arg)
629 {
630 while (*arg == ',')
631 arg++;
632
633 while (arg[pos] && arg[pos] != ',')
634 pos++;
635
636 result = 0;
637 for (n = 0; optname[n] != NULL; n++)
638 {
639 if (optname[n] && strncmp (optname[n], arg, pos) == 0)
640 {
641 gfc_option.rtcheck |= optmask[n];
642 arg += pos;
643 pos = 0;
644 result = 1;
645 break;
646 }
6ba3079d 647 else if (optname[n] && pos > 3 && startswith (arg, "no-")
a4c54bf2
JD
648 && strncmp (optname[n], arg+3, pos-3) == 0)
649 {
650 gfc_option.rtcheck &= ~optmask[n];
651 arg += pos;
652 pos = 0;
653 result = 1;
654 break;
655 }
d3d3011f
FXC
656 }
657 if (!result)
ddc05d11 658 gfc_fatal_error ("Argument to %<-fcheck%> is not valid: %s", arg);
d3d3011f
FXC
659 }
660}
661
662
6de9cd9a
DN
663/* Handle command-line options. Returns 0 if unrecognized, 1 if
664 recognized and handled. */
edf1eac2 665
5f20c657 666bool
00abf86c 667gfc_handle_option (size_t scode, const char *arg, HOST_WIDE_INT value,
a4d8c676 668 int kind ATTRIBUTE_UNUSED, location_t loc ATTRIBUTE_UNUSED,
5f20c657 669 const struct cl_option_handlers *handlers ATTRIBUTE_UNUSED)
6de9cd9a 670{
5f20c657 671 bool result = true;
6de9cd9a
DN
672 enum opt_code code = (enum opt_code) scode;
673
670637ee 674 if (gfc_cpp_handle_option (scode, arg, value) == 1)
5f20c657 675 return true;
670637ee 676
6de9cd9a
DN
677 switch (code)
678 {
679 default:
7c02f68b
MLI
680 if (cl_options[code].flags & gfc_option_lang_mask ())
681 break;
5f20c657 682 result = false;
6de9cd9a
DN
683 break;
684
0d52899f 685 case OPT_fcheck_array_temporaries:
c3e799e1 686 SET_BITFLAG (gfc_option.rtcheck, value, GFC_RTCHECK_ARRAY_TEMPS);
0d52899f
TB
687 break;
688
e0bcf78c
TS
689 case OPT_fd_lines_as_code:
690 gfc_option.flag_d_lines = 1;
691 break;
692
693 case OPT_fd_lines_as_comments:
694 gfc_option.flag_d_lines = 0;
695 break;
696
6de9cd9a
DN
697 case OPT_ffixed_form:
698 gfc_option.source_form = FORM_FIXED;
699 break;
700
701 case OPT_ffree_form:
702 gfc_option.source_form = FORM_FREE;
703 break;
704
31198773 705 case OPT_fintrinsic_modules_path:
f34dea03 706 case OPT_fintrinsic_modules_path_:
308f961b
TK
707
708 /* This is needed because omp_lib.h is in a directory together
709 with intrinsic modules. Do no warn because during testing
710 without an installed compiler, we would get lots of bogus
711 warnings for a missing include directory. */
417ea5c0 712 gfc_add_include_path (arg, false, false, false, true);
308f961b 713
31198773
FXC
714 gfc_add_intrinsic_modules_path (arg);
715 break;
716
2d7c7df6
JJ
717 case OPT_fpreprocessed:
718 gfc_option.flag_preprocessed = value;
719 break;
720
6de9cd9a
DN
721 case OPT_fmax_identifier_length_:
722 if (value > GFC_MAX_SYMBOL_LEN)
eb6d74fa 723 gfc_fatal_error ("Maximum supported identifier length is %d",
6de9cd9a
DN
724 GFC_MAX_SYMBOL_LEN);
725 gfc_option.max_identifier_length = value;
726 break;
727
51b09ce3 728 case OPT_finit_local_zero:
c3e799e1 729 set_init_local_zero (value);
51b09ce3
AL
730 break;
731
732 case OPT_finit_logical_:
733 if (!strcasecmp (arg, "false"))
734 gfc_option.flag_init_logical = GFC_INIT_LOGICAL_FALSE;
735 else if (!strcasecmp (arg, "true"))
736 gfc_option.flag_init_logical = GFC_INIT_LOGICAL_TRUE;
737 else
ddc05d11 738 gfc_fatal_error ("Unrecognized option to %<-finit-logical%>: %s",
51b09ce3
AL
739 arg);
740 break;
741
51b09ce3
AL
742 case OPT_finit_integer_:
743 gfc_option.flag_init_integer = GFC_INIT_INTEGER_ON;
66086032 744 gfc_option.flag_init_integer_value = strtol (arg, NULL, 10);
51b09ce3
AL
745 break;
746
747 case OPT_finit_character_:
748 if (value >= 0 && value <= 127)
749 {
750 gfc_option.flag_init_character = GFC_INIT_CHARACTER_ON;
751 gfc_option.flag_init_character_value = (char)value;
752 }
753 else
ddc05d11 754 gfc_fatal_error ("The value of n in %<-finit-character=n%> must be "
51b09ce3
AL
755 "between 0 and 127");
756 break;
757
6de9cd9a 758 case OPT_I:
417ea5c0 759 gfc_add_include_path (arg, true, false, true, true);
6de9cd9a
DN
760 break;
761
762 case OPT_J:
6de9cd9a 763 gfc_handle_module_path_options (arg);
2d0b4bcd 764 break;
ccc2ed87 765
944b8b35 766 case OPT_ffpe_trap_:
fa86f4f9
TB
767 gfc_handle_fpe_option (arg, true);
768 break;
769
770 case OPT_ffpe_summary_:
771 gfc_handle_fpe_option (arg, false);
944b8b35
FXC
772 break;
773
6de9cd9a 774 case OPT_std_f95:
67e9518e 775 gfc_option.allow_std = GFC_STD_OPT_F95;
6de9cd9a 776 gfc_option.warn_std = GFC_STD_F95_OBS;
dfbfb630
JD
777 gfc_option.max_continue_fixed = 19;
778 gfc_option.max_continue_free = 39;
6de9cd9a 779 gfc_option.max_identifier_length = 31;
73e42eef 780 warn_ampersand = 1;
16db2a6a 781 warn_tabs = 1;
6de9cd9a
DN
782 break;
783
784 case OPT_std_f2003:
67e9518e 785 gfc_option.allow_std = GFC_STD_OPT_F03;
b7892582 786 gfc_option.warn_std = GFC_STD_F95_OBS;
b9eba3ba
TB
787 gfc_option.max_continue_fixed = 255;
788 gfc_option.max_continue_free = 255;
6de9cd9a 789 gfc_option.max_identifier_length = 63;
73e42eef 790 warn_ampersand = 1;
16db2a6a 791 warn_tabs = 1;
6de9cd9a
DN
792 break;
793
f489fba1 794 case OPT_std_f2008:
67e9518e 795 gfc_option.allow_std = GFC_STD_OPT_F08;
58fc89f6 796 gfc_option.warn_std = GFC_STD_F95_OBS | GFC_STD_F2008_OBS;
b9eba3ba
TB
797 gfc_option.max_continue_free = 255;
798 gfc_option.max_continue_fixed = 255;
f489fba1 799 gfc_option.max_identifier_length = 63;
73e42eef 800 warn_ampersand = 1;
16db2a6a 801 warn_tabs = 1;
f489fba1
FXC
802 break;
803
4650947d 804 case OPT_std_f2008ts:
8179b067 805 case OPT_std_f2018:
67e9518e 806 gfc_option.allow_std = GFC_STD_OPT_F18;
b9eba3ba
TB
807 gfc_option.warn_std = GFC_STD_F95_OBS | GFC_STD_F2008_OBS
808 | GFC_STD_F2018_OBS;
809 gfc_option.max_continue_free = 255;
810 gfc_option.max_continue_fixed = 255;
811 gfc_option.max_identifier_length = 63;
812 warn_ampersand = 1;
813 warn_tabs = 1;
814 break;
815
816 case OPT_std_f2023:
817 gfc_option.allow_std = GFC_STD_OPT_F23;
8179b067
JB
818 gfc_option.warn_std = GFC_STD_F95_OBS | GFC_STD_F2008_OBS
819 | GFC_STD_F2018_OBS;
820 gfc_option.max_identifier_length = 63;
821 warn_ampersand = 1;
822 warn_tabs = 1;
823 break;
824
6de9cd9a 825 case OPT_std_gnu:
a47eec17 826 set_default_std_flags ();
c0309c74
RS
827 break;
828
829 case OPT_std_legacy:
a47eec17 830 set_default_std_flags ();
c0309c74 831 gfc_option.warn_std = 0;
b7892582
JB
832 break;
833
25d8f0a2 834 case OPT_fshort_enums:
5498f011 835 /* Handled in language-independent code. */
25d8f0a2 836 break;
eaa90d25 837
d3d3011f
FXC
838 case OPT_fcheck_:
839 gfc_handle_runtime_check_option (arg);
840 break;
f6288c24
FR
841
842 case OPT_fdec:
c3e799e1
FR
843 /* Set (or unset) the DEC extension flags. */
844 set_dec_flags (value);
f6288c24 845 break;
6de9cd9a
DN
846 }
847
f2bc201f
MLI
848 Fortran_handle_option_auto (&global_options, &global_options_set,
849 scode, arg, value,
850 gfc_option_lang_mask (), kind,
851 loc, handlers, global_dc);
6de9cd9a
DN
852 return result;
853}
41804a5b
TB
854
855
856/* Return a string with the options passed to the compiler; used for
857 Fortran's compiler_options() intrinsic. */
858
859char *
860gfc_get_option_string (void)
861{
862 unsigned j;
863 size_t len, pos;
864 char *result;
865
58b2a358
TB
866 /* Allocate and return a one-character string with '\0'. */
867 if (!save_decoded_options_count)
868 return XCNEWVEC (char, 1);
869
41804a5b
TB
870 /* Determine required string length. */
871
872 len = 0;
873 for (j = 1; j < save_decoded_options_count; j++)
874 {
875 switch (save_decoded_options[j].opt_index)
876 {
877 case OPT_o:
878 case OPT_d:
879 case OPT_dumpbase:
1dedc12d 880 case OPT_dumpbase_ext:
41804a5b 881 case OPT_dumpdir:
41804a5b
TB
882 case OPT_quiet:
883 case OPT_version:
884 case OPT_fintrinsic_modules_path:
f34dea03 885 case OPT_fintrinsic_modules_path_:
41804a5b
TB
886 /* Ignore these. */
887 break;
888 default:
1cc0e193 889 /* Ignore file names. */
41804a5b
TB
890 if (save_decoded_options[j].orig_option_with_args_text[0] == '-')
891 len += 1
892 + strlen (save_decoded_options[j].orig_option_with_args_text);
893 }
894 }
895
93acb62c 896 result = XCNEWVEC (char, len);
41804a5b
TB
897
898 pos = 0;
899 for (j = 1; j < save_decoded_options_count; j++)
900 {
901 switch (save_decoded_options[j].opt_index)
902 {
903 case OPT_o:
904 case OPT_d:
905 case OPT_dumpbase:
1dedc12d 906 case OPT_dumpbase_ext:
41804a5b 907 case OPT_dumpdir:
41804a5b
TB
908 case OPT_quiet:
909 case OPT_version:
910 case OPT_fintrinsic_modules_path:
f34dea03 911 case OPT_fintrinsic_modules_path_:
41804a5b
TB
912 /* Ignore these. */
913 continue;
914
915 case OPT_cpp_:
916 /* Use "-cpp" rather than "-cpp=<temporary file>". */
917 len = 4;
918 break;
919
920 default:
1cc0e193 921 /* Ignore file names. */
41804a5b
TB
922 if (save_decoded_options[j].orig_option_with_args_text[0] != '-')
923 continue;
924
925 len = strlen (save_decoded_options[j].orig_option_with_args_text);
926 }
927
928 memcpy (&result[pos], save_decoded_options[j].orig_option_with_args_text, len);
929 pos += len;
930 result[pos++] = ' ';
931 }
932
933 result[--pos] = '\0';
934 return result;
935}
c3e799e1
FR
936
937#undef SET_BITFLAG
938#undef SET_BITFLAG2
939#undef SET_FLAG