Debian Codegraph @

gcc/fortran/options.cc

raw
1 /* Parse and display command line options.
2 Copyright (C) 2000-2025 Free Software Foundation, Inc.
3 Contributed by Andy Vaught
4
5 This file is part of GCC.
6
7 GCC is free software; you can redistribute it and/or modify it under
8 the terms of the GNU General Public License as published by the Free
9 Software Foundation; either version 3, or (at your option) any later
10 version.
11
12 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15 for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING3. If not see
19 <http://www.gnu.org/licenses/>. */
20
21 #include "config.h"
22 #include "system.h"
23 #include "coretypes.h"
24 #include "target.h"
25 #include "tree.h"
26 #include "gfortran.h"
27 #include "diagnostic.h" /* For global_dc. */
28 #include "opts.h"
29 #include "toplev.h" /* For save_decoded_options. */
30 #include "cpp.h"
31 #include "langhooks.h"
32
33 gfc_option_t gfc_option;
34
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
49
50 /* Set flags that control warnings and errors for different
51 Fortran standards to their default values. Keep in sync with
52 libgfortran/runtime/compile_options.c (init_compile_options). */
53
54 static void
55 set_default_std_flags (void)
56 {
57 gfc_option.allow_std = GFC_STD_F95_OBS | GFC_STD_F95_DEL
58 | GFC_STD_F2003 | GFC_STD_F2008 | GFC_STD_F95 | GFC_STD_F77
59 | GFC_STD_F2008_OBS | GFC_STD_GNU | GFC_STD_LEGACY
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;
64 }
65
66 /* Set (or unset) the DEC extension flags. */
67
68 static void
69 set_dec_flags (int value)
70 {
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);
79 SET_BITFLAG (flag_dec_format_defaults, value, value);
80 SET_BITFLAG (flag_dec_blank_format_item, value, value);
81 SET_BITFLAG (flag_dec_char_conversions, value, value);
82 }
83
84 /* Finalize DEC flags. */
85
86 static void
87 post_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. */
94 if (value)
95 {
96 gfc_option.allow_std |= GFC_STD_F95_OBS | GFC_STD_F95_DEL
97 | GFC_STD_GNU | GFC_STD_LEGACY;
98 gfc_option.warn_std &= ~(GFC_STD_LEGACY | GFC_STD_F95_DEL);
99 }
100 }
101
102 /* Enable (or disable) -finit-local-zero. */
103
104 static void
105 set_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 }
118
119 /* Return language mask for Fortran options. */
120
121 unsigned int
122 gfc_option_lang_mask (void)
123 {
124 return CL_Fortran;
125 }
126
127 /* Initialize options structure OPTS. */
128
129 void
130 gfc_init_options_struct (struct gcc_options *opts)
131 {
132 opts->x_flag_errno_math = 0;
133 opts->frontend_set_flag_errno_math = true;
134 opts->x_flag_associative_math = -1;
135 opts->frontend_set_flag_associative_math = true;
136 }
137
138 /* Get ready for options handling. Keep in sync with
139 libgfortran/runtime/compile_options.c (init_compile_options). */
140
141 void
142 gfc_init_options (unsigned int decoded_options_count,
143 struct cl_decoded_option *decoded_options)
144 {
145 gfc_source_file = NULL;
146 gfc_option.module_dir = NULL;
147 gfc_option.source_form = FORM_UNKNOWN;
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;
153 gfc_option.max_identifier_length = GFC_MAX_SYMBOL_LEN;
154 gfc_option.max_errors = 25;
155
156 gfc_option.flag_preprocessed = 0;
157 gfc_option.flag_d_lines = -1;
158 set_init_local_zero (0);
159
160 gfc_option.fpe = 0;
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;
165 gfc_option.rtcheck = 0;
166
167 set_dec_flags (0);
168 set_default_std_flags ();
169
170 /* Initialize cpp-related options. */
171 gfc_cpp_init_options (decoded_options_count, decoded_options);
172 gfc_diagnostics_init ();
173 }
174
175
176 /* Determine the source form from the filename extension. We assume
177 case insensitivity. */
178
179 static gfc_source_form
180 form_from_filename (const char *filename)
181 {
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 {
196 ".f03", FORM_FREE}
197 ,
198 {
199 ".f08", FORM_FREE}
200 ,
201 {
202 ".fii", FORM_FREE}
203 ,
204 {
205 ".fi", FORM_FIXED}
206 ,
207 {
208 ".f", FORM_FIXED}
209 ,
210 {
211 ".for", FORM_FIXED}
212 ,
213 {
214 ".ftn", FORM_FIXED}
215 ,
216 {
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
258 /* Finalize commandline options. */
259
260 bool
261 gfc_post_options (const char **pfilename)
262 {
263 const char *filename = *pfilename, *canon_source_file = NULL;
264 char *source_path;
265 bool verbose_missing_dir_warn;
266 int i;
267
268 /* This needs to be after the commandline has been processed.
269 In Fortran, the options is by default enabled, in C/C++
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
274 = (OPTION_SET_P (cpp_warn_missing_include_dirs)
275 && global_options.x_cpp_warn_missing_include_dirs);
276 SET_OPTION_IF_UNSET (&global_options, &global_options_set,
277 cpp_warn_missing_include_dirs, 1);
278 gfc_check_include_dirs (verbose_missing_dir_warn);
279
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
284 /* Finalize DEC flags. */
285 post_dec_flags (flag_dec);
286
287 /* Excess precision other than "fast" requires front-end
288 support. */
289 if (flag_excess_precision == EXCESS_PRECISION_STANDARD)
290 sorry ("%<-fexcess-precision=standard%> for Fortran");
291 else if (flag_excess_precision == EXCESS_PRECISION_FLOAT16)
292 sorry ("%<-fexcess-precision=16%> for Fortran");
293
294 flag_excess_precision = EXCESS_PRECISION_FAST;
295
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
301 if (flag_protect_parens == -1)
302 flag_protect_parens = !optimize_fast;
303
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)
307 flag_stack_arrays = optimize_fast;
308
309 /* By default, disable (re)allocation during assignment for -std=f95,
310 and enable it for F2003/F2008/GNU/Legacy. */
311 if (flag_realloc_lhs == -1)
312 {
313 if (gfc_option.allow_std & GFC_STD_F2003)
314 flag_realloc_lhs = 1;
315 else
316 flag_realloc_lhs = 0;
317 }
318
319 /* -fbounds-check is equivalent to -fcheck=bounds */
320 if (flag_bounds_check)
321 gfc_option.rtcheck |= GFC_RTCHECK_BOUNDS;
322
323 if (flag_compare_debug)
324 flag_dump_fortran_original = 0;
325
326 /* Make -fmax-errors visible to gfortran's diagnostic machinery. */
327 if (OPTION_SET_P (flag_max_errors))
328 gfc_option.max_errors = flag_max_errors;
329
330 /* Verify the input file name. */
331 if (!filename || strcmp (filename, "-") == 0)
332 {
333 filename = "";
334 }
335
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;
351
352 /* Adds the path where the source file is to the list of include files. */
353
354 i = strlen (canon_source_file);
355 while (i > 0 && !IS_DIR_SEPARATOR (canon_source_file[i]))
356 i--;
357
358 if (i != 0)
359 {
360 source_path = (char *) alloca (i + 1);
361 memcpy (source_path, canon_source_file, i);
362 source_path[i] = 0;
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);
367 }
368 else
369 gfc_add_include_path (".", true, true, false, false);
370
371 if (canon_source_file != gfc_source_file)
372 free (CONST_CAST (char *, canon_source_file));
373
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;
385 main_input_filename = filename;
386 gfc_warning_now (0, "Reading file %qs as free form",
387 (filename[0] == '\0') ? "<stdin>" : filename);
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)
396 gfc_warning_now (0, "%<-fd-lines-as-comments%> has no effect "
397 "in free form");
398 else if (gfc_option.flag_d_lines == 1)
399 gfc_warning_now (0, "%<-fd-lines-as-code%> has no effect in free form");
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. */
406 if (warn_line_truncation && !OPTION_SET_P (warnings_are_errors)
407 && option_unspecified_p (OPT_Wline_truncation))
408 diagnostic_classify_diagnostic (global_dc, OPT_Wline_truncation,
409 DK_ERROR, UNKNOWN_LOCATION);
410 }
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 }
420
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;
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;
427
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. */
431 if (flag_second_underscore == -1)
432 flag_second_underscore = flag_f2c;
433
434 if (!flag_automatic && flag_max_stack_var_size != -2
435 && flag_max_stack_var_size != 0)
436 gfc_warning_now (0, "Flag %<-fno-automatic%> overwrites %<-fmax-stack-var-size=%d%>",
437 flag_max_stack_var_size);
438 else if (!flag_automatic && flag_recursive)
439 gfc_warning_now (OPT_Woverwrite_recursive, "Flag %<-fno-automatic%> "
440 "overwrites %<-frecursive%>");
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");
444 else if (flag_max_stack_var_size != -2 && flag_recursive)
445 gfc_warning_now (0, "Flag %<-frecursive%> overwrites %<-fmax-stack-var-size=%d%>",
446 flag_max_stack_var_size);
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");
451
452 /* Implement -frecursive as -fmax-stack-var-size=-1. */
453 if (flag_recursive)
454 flag_max_stack_var_size = -1;
455
456 /* Implied -frecursive; implemented as -fmax-stack-var-size=-1. */
457 if (flag_max_stack_var_size == -2 && flag_automatic
458 && (flag_openmp || flag_openacc))
459 {
460 flag_recursive = 1;
461 flag_max_stack_var_size = -1;
462 }
463
464 /* Set flag_stack_arrays correctly. */
465 if (flag_stack_arrays == -1)
466 flag_stack_arrays = 0;
467
468 /* Set default. */
469 if (flag_max_stack_var_size == -2)
470 flag_max_stack_var_size = 65536;
471
472 /* Implement -fno-automatic as -fmax-stack-var-size=0. */
473 if (!flag_automatic)
474 flag_max_stack_var_size = 0;
475
476 /* Decide inlining preference depending on optimization if nothing was
477 specified on the command line. */
478 if ((flag_inline_intrinsics & GFC_FLAG_INLINE_INTRINSIC_MAXLOC)
479 == GFC_FLAG_INLINE_INTRINSIC_MAXLOC_UNSET)
480 {
481 if (optimize == 0 || optimize_size != 0)
482 flag_inline_intrinsics &= ~GFC_FLAG_INLINE_INTRINSIC_MAXLOC;
483 else
484 flag_inline_intrinsics |= GFC_FLAG_INLINE_INTRINSIC_MAXLOC;
485 }
486 if ((flag_inline_intrinsics & GFC_FLAG_INLINE_INTRINSIC_MINLOC)
487 == GFC_FLAG_INLINE_INTRINSIC_MINLOC_UNSET)
488 {
489 if (optimize == 0 || optimize_size != 0)
490 flag_inline_intrinsics &= ~GFC_FLAG_INLINE_INTRINSIC_MINLOC;
491 else
492 flag_inline_intrinsics |= GFC_FLAG_INLINE_INTRINSIC_MINLOC;
493 }
494
495 /* If the user did not specify an inline matmul limit, inline up to the BLAS
496 limit or up to 30 if no external BLAS is specified. */
497
498 if (flag_inline_matmul_limit < 0)
499 {
500 if (flag_external_blas)
501 flag_inline_matmul_limit = flag_blas_matmul_limit;
502 else
503 flag_inline_matmul_limit = 30;
504 }
505
506 /* Optimization implies front end optimization, unless the user
507 specified it directly. */
508
509 if (flag_frontend_optimize == -1)
510 flag_frontend_optimize = optimize && !optimize_debug;
511
512 /* Same for front end loop interchange. */
513
514 if (flag_frontend_loop_interchange == -1)
515 flag_frontend_loop_interchange = optimize;
516
517 /* Do inline packing by default if optimizing, but not if
518 optimizing for size. */
519 if (flag_inline_arg_packing == -1)
520 flag_inline_arg_packing = optimize && !optimize_size;
521
522 if (flag_max_array_constructor < 65535)
523 flag_max_array_constructor = 65535;
524
525 if (flag_fixed_line_length != 0 && flag_fixed_line_length < 7)
526 gfc_fatal_error ("Fixed line length must be at least seven");
527
528 if (flag_free_line_length != 0 && flag_free_line_length < 4)
529 gfc_fatal_error ("Free line length must be at least three");
530
531 if (flag_max_subrecord_length > MAX_SUBRECORD_LENGTH)
532 gfc_fatal_error ("Maximum subrecord length cannot exceed %d",
533 MAX_SUBRECORD_LENGTH);
534
535 gfc_cpp_post_options (verbose_missing_dir_warn);
536
537 if (gfc_option.allow_std & GFC_STD_F2008)
538 lang_hooks.name = "GNU Fortran2008";
539 else if (gfc_option.allow_std & GFC_STD_F2003)
540 lang_hooks.name = "GNU Fortran2003";
541
542 /* Set the unsigned "standard". */
543 if (flag_unsigned)
544 gfc_option.allow_std |= GFC_STD_UNSIGNED;
545
546 return gfc_cpp_preprocess_only ();
547 }
548
549
550 static void
551 gfc_handle_module_path_options (const char *arg)
552 {
553
554 if (gfc_option.module_dir != NULL)
555 gfc_fatal_error ("gfortran: Only one %<-J%> option allowed");
556
557 gfc_option.module_dir = XCNEWVEC (char, strlen (arg) + 2);
558 strcpy (gfc_option.module_dir, arg);
559
560 gfc_add_include_path (gfc_option.module_dir, true, false, true, true);
561
562 strcat (gfc_option.module_dir, "/");
563 }
564
565
566 /* Handle options -ffpe-trap= and -ffpe-summary=. */
567
568 static void
569 gfc_handle_fpe_option (const char *arg, bool trap)
570 {
571 int result, pos = 0, n;
572 /* precision is a backwards compatibility alias for inexact. */
573 static const char * const exception[] = { "invalid", "denormal", "zero",
574 "overflow", "underflow",
575 "inexact", "precision", NULL };
576 static const int opt_exception[] = { GFC_FPE_INVALID, GFC_FPE_DENORMAL,
577 GFC_FPE_ZERO, GFC_FPE_OVERFLOW,
578 GFC_FPE_UNDERFLOW, GFC_FPE_INEXACT,
579 GFC_FPE_INEXACT,
580 0 };
581
582 /* As the default for -ffpe-summary= is nonzero, set it to 0. */
583 if (!trap)
584 gfc_option.fpe_summary = 0;
585
586 while (*arg)
587 {
588 while (*arg == ',')
589 arg++;
590
591 while (arg[pos] && arg[pos] != ',')
592 pos++;
593
594 result = 0;
595 if (strncmp ("none", arg, pos) == 0)
596 {
597 if (trap)
598 gfc_option.fpe = 0;
599 else
600 gfc_option.fpe_summary = 0;
601 arg += pos;
602 pos = 0;
603 continue;
604 }
605 else if (!trap && strncmp ("all", arg, pos) == 0)
606 {
607 gfc_option.fpe_summary = GFC_FPE_INVALID | GFC_FPE_DENORMAL
608 | GFC_FPE_ZERO | GFC_FPE_OVERFLOW
609 | GFC_FPE_UNDERFLOW | GFC_FPE_INEXACT;
610 arg += pos;
611 pos = 0;
612 continue;
613 }
614 else
615 for (n = 0; exception[n] != NULL; n++)
616 {
617 if (exception[n] && strncmp (exception[n], arg, pos) == 0)
618 {
619 if (trap)
620 gfc_option.fpe |= opt_exception[n];
621 else
622 gfc_option.fpe_summary |= opt_exception[n];
623 arg += pos;
624 pos = 0;
625 result = 1;
626 break;
627 }
628 }
629 if (!result && trap)
630 gfc_fatal_error ("Argument to %<-ffpe-trap%> is not valid: %s", arg);
631 else if (!result)
632 gfc_fatal_error ("Argument to %<-ffpe-summary%> is not valid: %s", arg);
633
634 }
635 }
636
637
638 static void
639 gfc_handle_runtime_check_option (const char *arg)
640 {
641 int result, pos = 0, n;
642 static const char * const optname[] = { "all", "bounds", "array-temps",
643 "recursion", "do", "pointer",
644 "mem", "bits", NULL };
645 static const int optmask[] = { GFC_RTCHECK_ALL, GFC_RTCHECK_BOUNDS,
646 GFC_RTCHECK_ARRAY_TEMPS,
647 GFC_RTCHECK_RECURSION, GFC_RTCHECK_DO,
648 GFC_RTCHECK_POINTER, GFC_RTCHECK_MEM,
649 GFC_RTCHECK_BITS, 0 };
650
651 while (*arg)
652 {
653 while (*arg == ',')
654 arg++;
655
656 while (arg[pos] && arg[pos] != ',')
657 pos++;
658
659 result = 0;
660 for (n = 0; optname[n] != NULL; n++)
661 {
662 if (optname[n] && strncmp (optname[n], arg, pos) == 0)
663 {
664 gfc_option.rtcheck |= optmask[n];
665 arg += pos;
666 pos = 0;
667 result = 1;
668 break;
669 }
670 else if (optname[n] && pos > 3 && startswith (arg, "no-")
671 && strncmp (optname[n], arg+3, pos-3) == 0)
672 {
673 gfc_option.rtcheck &= ~optmask[n];
674 arg += pos;
675 pos = 0;
676 result = 1;
677 break;
678 }
679 }
680 if (!result)
681 gfc_fatal_error ("Argument to %<-fcheck%> is not valid: %s", arg);
682 }
683 }
684
685
686 /* Handle command-line options. Returns 0 if unrecognized, 1 if
687 recognized and handled. */
688
689 bool
690 gfc_handle_option (size_t scode, const char *arg, HOST_WIDE_INT value,
691 int kind ATTRIBUTE_UNUSED, location_t loc ATTRIBUTE_UNUSED,
692 const struct cl_option_handlers *handlers ATTRIBUTE_UNUSED)
693 {
694 bool result = true;
695 enum opt_code code = (enum opt_code) scode;
696
697 if (gfc_cpp_handle_option (scode, arg, value) == 1)
698 return true;
699
700 switch (code)
701 {
702 default:
703 if (cl_options[code].flags & gfc_option_lang_mask ())
704 break;
705 result = false;
706 break;
707
708 case OPT_fcheck_array_temporaries:
709 SET_BITFLAG (gfc_option.rtcheck, value, GFC_RTCHECK_ARRAY_TEMPS);
710 break;
711
712 case OPT_fd_lines_as_code:
713 gfc_option.flag_d_lines = 1;
714 break;
715
716 case OPT_fd_lines_as_comments:
717 gfc_option.flag_d_lines = 0;
718 break;
719
720 case OPT_ffixed_form:
721 gfc_option.source_form = FORM_FIXED;
722 break;
723
724 case OPT_ffree_form:
725 gfc_option.source_form = FORM_FREE;
726 break;
727
728 case OPT_fintrinsic_modules_path:
729 case OPT_fintrinsic_modules_path_:
730
731 /* This is needed because omp_lib.h is in a directory together
732 with intrinsic modules. Do no warn because during testing
733 without an installed compiler, we would get lots of bogus
734 warnings for a missing include directory. */
735 gfc_add_include_path (arg, false, false, false, true);
736
737 gfc_add_intrinsic_modules_path (arg);
738 break;
739
740 case OPT_fpreprocessed:
741 gfc_option.flag_preprocessed = value;
742 break;
743
744 case OPT_fmax_identifier_length_:
745 if (value > GFC_MAX_SYMBOL_LEN)
746 gfc_fatal_error ("Maximum supported identifier length is %d",
747 GFC_MAX_SYMBOL_LEN);
748 gfc_option.max_identifier_length = value;
749 break;
750
751 case OPT_finit_local_zero:
752 set_init_local_zero (value);
753 break;
754
755 case OPT_finit_logical_:
756 if (!strcasecmp (arg, "false"))
757 gfc_option.flag_init_logical = GFC_INIT_LOGICAL_FALSE;
758 else if (!strcasecmp (arg, "true"))
759 gfc_option.flag_init_logical = GFC_INIT_LOGICAL_TRUE;
760 else
761 gfc_fatal_error ("Unrecognized option to %<-finit-logical%>: %s",
762 arg);
763 break;
764
765 case OPT_finit_integer_:
766 gfc_option.flag_init_integer = GFC_INIT_INTEGER_ON;
767 gfc_option.flag_init_integer_value = strtol (arg, NULL, 10);
768 break;
769
770 case OPT_finit_character_:
771 if (value >= 0 && value <= 127)
772 {
773 gfc_option.flag_init_character = GFC_INIT_CHARACTER_ON;
774 gfc_option.flag_init_character_value = (char)value;
775 }
776 else
777 gfc_fatal_error ("The value of n in %<-finit-character=n%> must be "
778 "between 0 and 127");
779 break;
780
781 case OPT_I:
782 gfc_add_include_path (arg, true, false, true, true);
783 break;
784
785 case OPT_J:
786 gfc_handle_module_path_options (arg);
787 break;
788
789 case OPT_ffpe_trap_:
790 gfc_handle_fpe_option (arg, true);
791 break;
792
793 case OPT_ffpe_summary_:
794 gfc_handle_fpe_option (arg, false);
795 break;
796
797 case OPT_std_f95:
798 gfc_option.allow_std = GFC_STD_OPT_F95;
799 gfc_option.warn_std = GFC_STD_F95_OBS;
800 gfc_option.max_continue_fixed = 19;
801 gfc_option.max_continue_free = 39;
802 gfc_option.max_identifier_length = 31;
803 warn_ampersand = 1;
804 warn_tabs = 1;
805 break;
806
807 case OPT_std_f2003:
808 gfc_option.allow_std = GFC_STD_OPT_F03;
809 gfc_option.warn_std = GFC_STD_F95_OBS;
810 gfc_option.max_continue_fixed = 255;
811 gfc_option.max_continue_free = 255;
812 gfc_option.max_identifier_length = 63;
813 warn_ampersand = 1;
814 warn_tabs = 1;
815 break;
816
817 case OPT_std_f2008:
818 gfc_option.allow_std = GFC_STD_OPT_F08;
819 gfc_option.warn_std = GFC_STD_F95_OBS | GFC_STD_F2008_OBS;
820 gfc_option.max_continue_free = 255;
821 gfc_option.max_continue_fixed = 255;
822 gfc_option.max_identifier_length = 63;
823 warn_ampersand = 1;
824 warn_tabs = 1;
825 break;
826
827 case OPT_std_f2008ts:
828 case OPT_std_f2018:
829 gfc_option.allow_std = GFC_STD_OPT_F18;
830 gfc_option.warn_std = GFC_STD_F95_OBS | GFC_STD_F2008_OBS
831 | GFC_STD_F2018_OBS;
832 gfc_option.max_continue_free = 255;
833 gfc_option.max_continue_fixed = 255;
834 gfc_option.max_identifier_length = 63;
835 warn_ampersand = 1;
836 warn_tabs = 1;
837 break;
838
839 case OPT_std_f2023:
840 gfc_option.allow_std = GFC_STD_OPT_F23;
841 gfc_option.warn_std = GFC_STD_F95_OBS | GFC_STD_F2008_OBS
842 | GFC_STD_F2018_OBS;
843 gfc_option.max_identifier_length = 63;
844 warn_ampersand = 1;
845 warn_tabs = 1;
846 break;
847
848 case OPT_std_f202y:
849 gfc_option.allow_std = GFC_STD_OPT_F23 | GFC_STD_F202Y;
850 gfc_option.warn_std = GFC_STD_F95_OBS | GFC_STD_F2008_OBS
851 | GFC_STD_F2018_OBS;
852 gfc_option.max_identifier_length = 63;
853 warn_ampersand = 1;
854 warn_tabs = 1;
855 break;
856
857 case OPT_std_gnu:
858 set_default_std_flags ();
859 break;
860
861 case OPT_std_legacy:
862 set_default_std_flags ();
863 gfc_option.warn_std = 0;
864 break;
865
866 case OPT_fshort_enums:
867 /* Handled in language-independent code. */
868 break;
869
870 case OPT_fcheck_:
871 gfc_handle_runtime_check_option (arg);
872 break;
873
874 case OPT_fdec:
875 /* Set (or unset) the DEC extension flags. */
876 set_dec_flags (value);
877 break;
878
879 case OPT_fbuiltin_:
880 /* We only handle -fno-builtin-omp_is_initial_device
881 and -fno-builtin-acc_on_device. */
882 if (value)
883 return false; /* Not supported. */
884 if (!strcmp ("omp_is_initial_device", arg))
885 gfc_option.disable_omp_is_initial_device = true;
886 else if (!strcmp ("acc_on_device", arg))
887 gfc_option.disable_acc_on_device = true;
888 else
889 warning (0, "command-line option %<-fno-builtin-%s%> is not valid for "
890 "Fortran", arg);
891 break;
892
893 }
894
895 Fortran_handle_option_auto (&global_options, &global_options_set,
896 scode, arg, value,
897 gfc_option_lang_mask (), kind,
898 loc, handlers, global_dc);
899 return result;
900 }
901
902
903 /* Return a string with the options passed to the compiler; used for
904 Fortran's compiler_options() intrinsic. */
905
906 char *
907 gfc_get_option_string (void)
908 {
909 unsigned j;
910 size_t len, pos;
911 char *result;
912
913 /* Allocate and return a one-character string with '\0'. */
914 if (!save_decoded_options_count)
915 return XCNEWVEC (char, 1);
916
917 /* Determine required string length. */
918
919 len = 0;
920 for (j = 1; j < save_decoded_options_count; j++)
921 {
922 switch (save_decoded_options[j].opt_index)
923 {
924 case OPT_o:
925 case OPT_d:
926 case OPT_dumpbase:
927 case OPT_dumpbase_ext:
928 case OPT_dumpdir:
929 case OPT_quiet:
930 case OPT_version:
931 case OPT_fintrinsic_modules_path:
932 case OPT_fintrinsic_modules_path_:
933 /* Ignore these. */
934 break;
935 default:
936 /* Ignore file names. */
937 if (save_decoded_options[j].orig_option_with_args_text[0] == '-')
938 len += 1
939 + strlen (save_decoded_options[j].orig_option_with_args_text);
940 }
941 }
942
943 result = XCNEWVEC (char, len);
944
945 pos = 0;
946 for (j = 1; j < save_decoded_options_count; j++)
947 {
948 switch (save_decoded_options[j].opt_index)
949 {
950 case OPT_o:
951 case OPT_d:
952 case OPT_dumpbase:
953 case OPT_dumpbase_ext:
954 case OPT_dumpdir:
955 case OPT_quiet:
956 case OPT_version:
957 case OPT_fintrinsic_modules_path:
958 case OPT_fintrinsic_modules_path_:
959 /* Ignore these. */
960 continue;
961
962 case OPT_cpp_:
963 /* Use "-cpp" rather than "-cpp=<temporary file>". */
964 len = 4;
965 break;
966
967 default:
968 /* Ignore file names. */
969 if (save_decoded_options[j].orig_option_with_args_text[0] != '-')
970 continue;
971
972 len = strlen (save_decoded_options[j].orig_option_with_args_text);
973 }
974
975 memcpy (&result[pos], save_decoded_options[j].orig_option_with_args_text, len);
976 pos += len;
977 result[pos++] = ' ';
978 }
979
980 result[--pos] = '\0';
981 return result;
982 }
983
984 #undef SET_BITFLAG
985 #undef SET_BITFLAG2
986 #undef SET_FLAG