]> git.ipfire.org Git - thirdparty/gcc.git/blame - gcc/fortran/options.c
* config/h8300/h8300.md (low byte masking peepholes): Only accept
[thirdparty/gcc.git] / gcc / fortran / options.c
CommitLineData
4ee9c684 1/* Parse and display command line options.
8e8f6434 2 Copyright (C) 2000-2018 Free Software Foundation, Inc.
4ee9c684 3 Contributed by Andy Vaught
4
c84b470d 5This file is part of GCC.
4ee9c684 6
c84b470d 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
bdabe786 9Software Foundation; either version 3, or (at your option) any later
c84b470d 10version.
4ee9c684 11
c84b470d 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.
4ee9c684 16
17You should have received a copy of the GNU General Public License
bdabe786 18along with GCC; see the file COPYING3. If not see
19<http://www.gnu.org/licenses/>. */
4ee9c684 20
4ee9c684 21#include "config.h"
22#include "system.h"
23#include "coretypes.h"
4cba6f60 24#include "target.h"
4ee9c684 25#include "tree.h"
4cba6f60 26#include "gfortran.h"
27#include "diagnostic.h" /* For global_dc. */
4ee9c684 28#include "opts.h"
e3d1ab2b 29#include "toplev.h" /* For save_decoded_options. */
2ecc6bc5 30#include "cpp.h"
f36f9b8b 31#include "langhooks.h"
4ee9c684 32
33gfc_option_t gfc_option;
34
35
83a65a0d 36/* Set flags that control warnings and errors for different
82860817 37 Fortran standards to their default values. Keep in sync with
38 libgfortran/runtime/compile_options.c (init_compile_options). */
83a65a0d 39
40static void
41set_default_std_flags (void)
42{
43 gfc_option.allow_std = GFC_STD_F95_OBS | GFC_STD_F95_DEL
ff4425cf 44 | GFC_STD_F2003 | GFC_STD_F2008 | GFC_STD_F95 | GFC_STD_F77
2e2156cf 45 | GFC_STD_F2008_OBS | GFC_STD_GNU | GFC_STD_LEGACY
003e134b 46 | GFC_STD_F2018 | GFC_STD_F2018_DEL | GFC_STD_F2018_OBS;
9449a7d9 47 gfc_option.warn_std = GFC_STD_F2018_DEL | GFC_STD_F95_DEL | GFC_STD_LEGACY;
83a65a0d 48}
49
82860817 50
cf078427 51/* Set all the DEC extension flags. */
d7cd448a 52
53static void
54set_dec_flags (int value)
55{
a3b89816 56 if (value)
57 {
58 /* Allow legacy code without warnings. */
59 gfc_option.allow_std |= GFC_STD_F95_OBS | GFC_STD_F95_DEL
60 | GFC_STD_GNU | GFC_STD_LEGACY;
61 gfc_option.warn_std &= ~(GFC_STD_LEGACY | GFC_STD_F95_DEL);
62 }
cf078427 63
64 /* Set other DEC compatibility extensions. */
65 flag_dollar_ok |= value;
66 flag_cray_pointer |= value;
67 flag_dec_structure |= value;
68 flag_dec_intrinsic_ints |= value;
69 flag_dec_static |= value;
70 flag_dec_math |= value;
b2cdb112 71 flag_dec_include |= value;
d7cd448a 72}
73
74
e88d34f6 75/* Return language mask for Fortran options. */
76
77unsigned int
78gfc_option_lang_mask (void)
79{
80 return CL_Fortran;
81}
82
f3f006ad 83/* Initialize options structure OPTS. */
84
85void
86gfc_init_options_struct (struct gcc_options *opts)
87{
88 opts->x_flag_errno_math = 0;
a5b628e2 89 opts->frontend_set_flag_errno_math = true;
f3f006ad 90 opts->x_flag_associative_math = -1;
a5b628e2 91 opts->frontend_set_flag_associative_math = true;
f3f006ad 92}
e88d34f6 93
82860817 94/* Get ready for options handling. Keep in sync with
293d72e0 95 libgfortran/runtime/compile_options.c (init_compile_options). */
4ee9c684 96
e88d34f6 97void
98gfc_init_options (unsigned int decoded_options_count,
99 struct cl_decoded_option *decoded_options)
4ee9c684 100{
8594da07 101 gfc_source_file = NULL;
4ee9c684 102 gfc_option.module_dir = NULL;
103 gfc_option.source_form = FORM_UNKNOWN;
766398ac 104 gfc_option.max_continue_fixed = 255;
105 gfc_option.max_continue_free = 255;
4ee9c684 106 gfc_option.max_identifier_length = GFC_MAX_SYMBOL_LEN;
b6be0ac5 107 gfc_option.max_errors = 25;
4ee9c684 108
9e3a9b8b 109 gfc_option.flag_preprocessed = 0;
8594da07 110 gfc_option.flag_d_lines = -1;
a28eb9a8 111 gfc_option.flag_init_integer = GFC_INIT_INTEGER_OFF;
112 gfc_option.flag_init_integer_value = 0;
a28eb9a8 113 gfc_option.flag_init_logical = GFC_INIT_LOGICAL_OFF;
114 gfc_option.flag_init_character = GFC_INIT_CHARACTER_OFF;
115 gfc_option.flag_init_character_value = (char)0;
116
8c84a5de 117 gfc_option.fpe = 0;
553e7cef 118 /* All except GFC_FPE_INEXACT. */
119 gfc_option.fpe_summary = GFC_FPE_INVALID | GFC_FPE_DENORMAL
120 | GFC_FPE_ZERO | GFC_FPE_OVERFLOW
121 | GFC_FPE_UNDERFLOW;
ad8ed98e 122 gfc_option.rtcheck = 0;
8c84a5de 123
45e6f6f0 124 /* ??? Wmissing-include-dirs is disabled by default in C/C++ but
125 enabled by default in Fortran. Ideally, we should express this
126 in .opt, but that is not supported yet. */
127 if (!global_options_set.x_cpp_warn_missing_include_dirs)
7082509e 128 global_options.x_cpp_warn_missing_include_dirs = 1;
45e6f6f0 129
d7cd448a 130 set_dec_flags (0);
131
83a65a0d 132 set_default_std_flags ();
4ee9c684 133
2ecc6bc5 134 /* Initialize cpp-related options. */
e88d34f6 135 gfc_cpp_init_options (decoded_options_count, decoded_options);
dc8078a3 136 gfc_diagnostics_init ();
4ee9c684 137}
138
139
8594da07 140/* Determine the source form from the filename extension. We assume
141 case insensitivity. */
142
143static gfc_source_form
144form_from_filename (const char *filename)
145{
8594da07 146 static const struct
147 {
148 const char *extension;
149 gfc_source_form form;
150 }
151 exttype[] =
152 {
153 {
154 ".f90", FORM_FREE}
155 ,
156 {
157 ".f95", FORM_FREE}
158 ,
159 {
f30a3c31 160 ".f03", FORM_FREE}
161 ,
162 {
ff4425cf 163 ".f08", FORM_FREE}
164 ,
165 {
8594da07 166 ".f", FORM_FIXED}
167 ,
168 {
169 ".for", FORM_FIXED}
170 ,
171 {
ed19cf5f 172 ".ftn", FORM_FIXED}
173 ,
174 {
8594da07 175 "", FORM_UNKNOWN}
176 }; /* sentinel value */
177
178 gfc_source_form f_form;
179 const char *fileext;
180 int i;
181
182 /* Find end of file name. Note, filename is either a NULL pointer or
183 a NUL terminated string. */
184 i = 0;
185 while (filename[i] != '\0')
186 i++;
187
188 /* Find last period. */
189 while (i >= 0 && (filename[i] != '.'))
190 i--;
191
192 /* Did we see a file extension? */
193 if (i < 0)
194 return FORM_UNKNOWN; /* Nope */
195
196 /* Get file extension and compare it to others. */
197 fileext = &(filename[i]);
198
199 i = -1;
200 f_form = FORM_UNKNOWN;
201 do
202 {
203 i++;
204 if (strcasecmp (fileext, exttype[i].extension) == 0)
205 {
206 f_form = exttype[i].form;
207 break;
208 }
209 }
210 while (exttype[i].form != FORM_UNKNOWN);
211
212 return f_form;
213}
214
215
4ee9c684 216/* Finalize commandline options. */
217
218bool
219gfc_post_options (const char **pfilename)
220{
9e3a9b8b 221 const char *filename = *pfilename, *canon_source_file = NULL;
a0d72493 222 char *source_path;
223 int i;
4ee9c684 224
c6418a4e 225 /* Excess precision other than "fast" requires front-end
226 support. */
4267ed07 227 if (flag_excess_precision_cmdline == EXCESS_PRECISION_STANDARD)
c6418a4e 228 sorry ("-fexcess-precision=standard for Fortran");
229 flag_excess_precision_cmdline = EXCESS_PRECISION_FAST;
230
fb063c92 231 /* Fortran allows associative math - but we cannot reassociate if
232 we want traps or signed zeros. Cf. also flag_protect_parens. */
233 if (flag_associative_math == -1)
234 flag_associative_math = (!flag_trapping_math && !flag_signed_zeros);
235
eb106faf 236 if (flag_protect_parens == -1)
237 flag_protect_parens = !optimize_fast;
9d4ede2c 238
b254210f 239 /* -Ofast sets implies -fstack-arrays unless an explicit size is set for
240 stack arrays. */
241 if (flag_stack_arrays == -1 && flag_max_stack_var_size == -2)
eb106faf 242 flag_stack_arrays = optimize_fast;
9e412fd2 243
929c6f45 244 /* By default, disable (re)allocation during assignment for -std=f95,
293d72e0 245 and enable it for F2003/F2008/GNU/Legacy. */
eb106faf 246 if (flag_realloc_lhs == -1)
929c6f45 247 {
248 if (gfc_option.allow_std & GFC_STD_F2003)
eb106faf 249 flag_realloc_lhs = 1;
929c6f45 250 else
eb106faf 251 flag_realloc_lhs = 0;
929c6f45 252 }
253
ad8ed98e 254 /* -fbounds-check is equivalent to -fcheck=bounds */
255 if (flag_bounds_check)
256 gfc_option.rtcheck |= GFC_RTCHECK_BOUNDS;
257
71278019 258 if (flag_compare_debug)
829d7a08 259 flag_dump_fortran_original = 0;
71278019 260
566d7c74 261 /* Make -fmax-errors visible to gfortran's diagnostic machinery. */
262 if (global_options_set.x_flag_max_errors)
263 gfc_option.max_errors = flag_max_errors;
264
4ee9c684 265 /* Verify the input file name. */
266 if (!filename || strcmp (filename, "-") == 0)
267 {
268 filename = "";
269 }
270
9e3a9b8b 271 if (gfc_option.flag_preprocessed)
272 {
273 /* For preprocessed files, if the first tokens are of the form # NUM.
274 handle the directives so we know the original file name. */
275 gfc_source_file = gfc_read_orig_filename (filename, &canon_source_file);
276 if (gfc_source_file == NULL)
277 gfc_source_file = filename;
278 else
279 *pfilename = gfc_source_file;
280 }
281 else
282 gfc_source_file = filename;
283
284 if (canon_source_file == NULL)
285 canon_source_file = gfc_source_file;
8594da07 286
a0d72493 287 /* Adds the path where the source file is to the list of include files. */
288
9e3a9b8b 289 i = strlen (canon_source_file);
290 while (i > 0 && !IS_DIR_SEPARATOR (canon_source_file[i]))
a0d72493 291 i--;
1bcc6eb8 292
a0d72493 293 if (i != 0)
294 {
48d8ad5a 295 source_path = (char *) alloca (i + 1);
9e3a9b8b 296 memcpy (source_path, canon_source_file, i);
a0d72493 297 source_path[i] = 0;
db24420f 298 gfc_add_include_path (source_path, true, true, true);
a0d72493 299 }
300 else
db24420f 301 gfc_add_include_path (".", true, true, true);
a0d72493 302
9e3a9b8b 303 if (canon_source_file != gfc_source_file)
434f0922 304 free (CONST_CAST (char *, canon_source_file));
9e3a9b8b 305
8594da07 306 /* Decide which form the file will be read in as. */
307
308 if (gfc_option.source_form != FORM_UNKNOWN)
309 gfc_current_form = gfc_option.source_form;
310 else
311 {
312 gfc_current_form = form_from_filename (filename);
313
314 if (gfc_current_form == FORM_UNKNOWN)
315 {
316 gfc_current_form = FORM_FREE;
005915c5 317 main_input_filename = filename;
6f521718 318 gfc_warning_now (0, "Reading file %qs as free form",
bf79c656 319 (filename[0] == '\0') ? "<stdin>" : filename);
8594da07 320 }
321 }
322
323 /* If the user specified -fd-lines-as-{code|comments} verify that we're
324 in fixed form. */
325 if (gfc_current_form == FORM_FREE)
326 {
327 if (gfc_option.flag_d_lines == 0)
6f521718 328 gfc_warning_now (0, "%<-fd-lines-as-comments%> has no effect "
399c003f 329 "in free form");
8594da07 330 else if (gfc_option.flag_d_lines == 1)
6f521718 331 gfc_warning_now (0, "%<-fd-lines-as-code%> has no effect in free form");
bf1857ff 332
333 if (warn_line_truncation == -1)
334 warn_line_truncation = 1;
335
336 /* Enable -Werror=line-truncation when -Werror and -Wno-error have
337 not been set. */
338 if (warn_line_truncation && !global_options_set.x_warnings_are_errors
339 && (global_dc->classify_diagnostic[OPT_Wline_truncation] ==
340 DK_UNSPECIFIED))
341 diagnostic_classify_diagnostic (global_dc, OPT_Wline_truncation,
342 DK_ERROR, UNKNOWN_LOCATION);
8594da07 343 }
63256634 344 else
345 {
346 /* With -fdec, set -fd-lines-as-comments by default in fixed form. */
347 if (flag_dec && gfc_option.flag_d_lines == -1)
348 gfc_option.flag_d_lines = 0;
349
350 if (warn_line_truncation == -1)
351 warn_line_truncation = 0;
352 }
4ee9c684 353
675e3934 354 /* If -pedantic, warn about the use of GNU extensions. */
355 if (pedantic && (gfc_option.allow_std & GFC_STD_GNU) != 0)
356 gfc_option.warn_std |= GFC_STD_GNU;
c667e11d 357 /* -std=legacy -pedantic is effectively -std=gnu. */
358 if (pedantic && (gfc_option.allow_std & GFC_STD_LEGACY) != 0)
359 gfc_option.warn_std |= GFC_STD_F95_OBS | GFC_STD_F95_DEL | GFC_STD_LEGACY;
675e3934 360
bdaed7d2 361 /* If the user didn't explicitly specify -f(no)-second-underscore we
362 use it if we're trying to be compatible with f2c, and not
363 otherwise. */
eb106faf 364 if (flag_second_underscore == -1)
829d7a08 365 flag_second_underscore = flag_f2c;
bdaed7d2 366
829d7a08 367 if (!flag_automatic && flag_max_stack_var_size != -2
eb106faf 368 && flag_max_stack_var_size != 0)
6f521718 369 gfc_warning_now (0, "Flag %<-fno-automatic%> overwrites %<-fmax-stack-var-size=%d%>",
eb106faf 370 flag_max_stack_var_size);
829d7a08 371 else if (!flag_automatic && flag_recursive)
6f521718 372 gfc_warning_now (0, "Flag %<-fno-automatic%> overwrites %<-frecursive%>");
829d7a08 373 else if (!flag_automatic && flag_openmp)
6f521718 374 gfc_warning_now (0, "Flag %<-fno-automatic%> overwrites %<-frecursive%> implied by "
eb106faf 375 "%<-fopenmp%>");
829d7a08 376 else if (flag_max_stack_var_size != -2 && flag_recursive)
6f521718 377 gfc_warning_now (0, "Flag %<-frecursive%> overwrites %<-fmax-stack-var-size=%d%>",
eb106faf 378 flag_max_stack_var_size);
829d7a08 379 else if (flag_max_stack_var_size != -2 && flag_openmp)
6f521718 380 gfc_warning_now (0, "Flag %<-fmax-stack-var-size=%d%> overwrites %<-frecursive%> "
eb106faf 381 "implied by %<-fopenmp%>", flag_max_stack_var_size);
14af4f75 382
a7bdd774 383 /* Implement -frecursive as -fmax-stack-var-size=-1. */
829d7a08 384 if (flag_recursive)
eb106faf 385 flag_max_stack_var_size = -1;
14af4f75 386
a7bdd774 387 /* Implied -frecursive; implemented as -fmax-stack-var-size=-1. */
829d7a08 388 if (flag_max_stack_var_size == -2 && flag_openmp && flag_automatic)
a7bdd774 389 {
829d7a08 390 flag_recursive = 1;
eb106faf 391 flag_max_stack_var_size = -1;
a7bdd774 392 }
393
b254210f 394 /* Set flag_stack_arrays correctly. */
395 if (flag_stack_arrays == -1)
396 flag_stack_arrays = 0;
397
14af4f75 398 /* Set default. */
eb106faf 399 if (flag_max_stack_var_size == -2)
400 flag_max_stack_var_size = 32768;
14af4f75 401
7f2e183b 402 /* Implement -fno-automatic as -fmax-stack-var-size=0. */
829d7a08 403 if (!flag_automatic)
eb106faf 404 flag_max_stack_var_size = 0;
e27238e0 405
c23d681b 406 /* If the user did not specify an inline matmul limit, inline up to the BLAS
407 limit or up to 30 if no external BLAS is specified. */
08f351fd 408
c23d681b 409 if (flag_inline_matmul_limit < 0)
410 {
411 if (flag_external_blas)
412 flag_inline_matmul_limit = flag_blas_matmul_limit;
413 else
414 flag_inline_matmul_limit = 30;
415 }
08f351fd 416
10b2bb30 417 /* Optimization implies front end optimization, unless the user
418 specified it directly. */
419
eb106faf 420 if (flag_frontend_optimize == -1)
229c0ef7 421 flag_frontend_optimize = optimize && !optimize_debug;
eb106faf 422
44319903 423 /* Same for front end loop interchange. */
424
425 if (flag_frontend_loop_interchange == -1)
426 flag_frontend_loop_interchange = optimize;
427
829d7a08 428 if (flag_max_array_constructor < 65535)
429 flag_max_array_constructor = 65535;
430
eb106faf 431 if (flag_fixed_line_length != 0 && flag_fixed_line_length < 7)
432 gfc_fatal_error ("Fixed line length must be at least seven");
433
434 if (flag_free_line_length != 0 && flag_free_line_length < 4)
435 gfc_fatal_error ("Free line length must be at least three");
436
437 if (flag_max_subrecord_length > MAX_SUBRECORD_LENGTH)
438 gfc_fatal_error ("Maximum subrecord length cannot exceed %d",
439 MAX_SUBRECORD_LENGTH);
10b2bb30 440
2ecc6bc5 441 gfc_cpp_post_options ();
442
f36f9b8b 443 if (gfc_option.allow_std & GFC_STD_F2008)
444 lang_hooks.name = "GNU Fortran2008";
445 else if (gfc_option.allow_std & GFC_STD_F2003)
446 lang_hooks.name = "GNU Fortran2003";
447
c7a63538 448 return gfc_cpp_preprocess_only ();
4ee9c684 449}
450
451
4ee9c684 452static void
453gfc_handle_module_path_options (const char *arg)
454{
455
456 if (gfc_option.module_dir != NULL)
c4facd47 457 gfc_fatal_error ("gfortran: Only one %<-J%> option allowed");
4ee9c684 458
dfa3fb6a 459 gfc_option.module_dir = XCNEWVEC (char, strlen (arg) + 2);
4ee9c684 460 strcpy (gfc_option.module_dir, arg);
3dc083ce 461
db24420f 462 gfc_add_include_path (gfc_option.module_dir, true, false, true);
e500aeee 463
464 strcat (gfc_option.module_dir, "/");
4ee9c684 465}
466
1bcc6eb8 467
553e7cef 468/* Handle options -ffpe-trap= and -ffpe-summary=. */
469
8c84a5de 470static void
553e7cef 471gfc_handle_fpe_option (const char *arg, bool trap)
8c84a5de 472{
473 int result, pos = 0, n;
98b28ab6 474 /* precision is a backwards compatibility alias for inexact. */
8c84a5de 475 static const char * const exception[] = { "invalid", "denormal", "zero",
1bcc6eb8 476 "overflow", "underflow",
98b28ab6 477 "inexact", "precision", NULL };
8c84a5de 478 static const int opt_exception[] = { GFC_FPE_INVALID, GFC_FPE_DENORMAL,
479 GFC_FPE_ZERO, GFC_FPE_OVERFLOW,
98b28ab6 480 GFC_FPE_UNDERFLOW, GFC_FPE_INEXACT,
481 GFC_FPE_INEXACT,
8c84a5de 482 0 };
553e7cef 483
293d72e0 484 /* As the default for -ffpe-summary= is nonzero, set it to 0. */
553e7cef 485 if (!trap)
486 gfc_option.fpe_summary = 0;
487
8c84a5de 488 while (*arg)
489 {
490 while (*arg == ',')
491 arg++;
1bcc6eb8 492
8c84a5de 493 while (arg[pos] && arg[pos] != ',')
494 pos++;
1bcc6eb8 495
8c84a5de 496 result = 0;
553e7cef 497 if (!trap && strncmp ("none", arg, pos) == 0)
8c84a5de 498 {
553e7cef 499 gfc_option.fpe_summary = 0;
500 arg += pos;
501 pos = 0;
502 continue;
503 }
504 else if (!trap && strncmp ("all", arg, pos) == 0)
505 {
506 gfc_option.fpe_summary = GFC_FPE_INVALID | GFC_FPE_DENORMAL
507 | GFC_FPE_ZERO | GFC_FPE_OVERFLOW
508 | GFC_FPE_UNDERFLOW | GFC_FPE_INEXACT;
509 arg += pos;
510 pos = 0;
511 continue;
512 }
513 else
514 for (n = 0; exception[n] != NULL; n++)
515 {
8c84a5de 516 if (exception[n] && strncmp (exception[n], arg, pos) == 0)
517 {
553e7cef 518 if (trap)
519 gfc_option.fpe |= opt_exception[n];
520 else
521 gfc_option.fpe_summary |= opt_exception[n];
8c84a5de 522 arg += pos;
523 pos = 0;
524 result = 1;
525 break;
526 }
553e7cef 527 }
528 if (!result && !trap)
c4facd47 529 gfc_fatal_error ("Argument to %<-ffpe-trap%> is not valid: %s", arg);
553e7cef 530 else if (!result)
c4facd47 531 gfc_fatal_error ("Argument to %<-ffpe-summary%> is not valid: %s", arg);
553e7cef 532
8c84a5de 533 }
534}
535
1bcc6eb8 536
ad8ed98e 537static void
538gfc_handle_runtime_check_option (const char *arg)
539{
540 int result, pos = 0, n;
541 static const char * const optname[] = { "all", "bounds", "array-temps",
3a60b071 542 "recursion", "do", "pointer",
543 "mem", NULL };
ad8ed98e 544 static const int optmask[] = { GFC_RTCHECK_ALL, GFC_RTCHECK_BOUNDS,
545 GFC_RTCHECK_ARRAY_TEMPS,
7f1bd03f 546 GFC_RTCHECK_RECURSION, GFC_RTCHECK_DO,
3a60b071 547 GFC_RTCHECK_POINTER, GFC_RTCHECK_MEM,
ad8ed98e 548 0 };
549
550 while (*arg)
551 {
552 while (*arg == ',')
553 arg++;
554
555 while (arg[pos] && arg[pos] != ',')
556 pos++;
557
558 result = 0;
559 for (n = 0; optname[n] != NULL; n++)
560 {
561 if (optname[n] && strncmp (optname[n], arg, pos) == 0)
562 {
563 gfc_option.rtcheck |= optmask[n];
564 arg += pos;
565 pos = 0;
566 result = 1;
567 break;
568 }
ea9e8242 569 else if (optname[n] && pos > 3 && gfc_str_startswith (arg, "no-")
e7fbac94 570 && strncmp (optname[n], arg+3, pos-3) == 0)
571 {
572 gfc_option.rtcheck &= ~optmask[n];
573 arg += pos;
574 pos = 0;
575 result = 1;
576 break;
577 }
ad8ed98e 578 }
579 if (!result)
c4facd47 580 gfc_fatal_error ("Argument to %<-fcheck%> is not valid: %s", arg);
ad8ed98e 581 }
582}
583
584
4ee9c684 585/* Handle command-line options. Returns 0 if unrecognized, 1 if
586 recognized and handled. */
1bcc6eb8 587
b78351e5 588bool
8e18705e 589gfc_handle_option (size_t scode, const char *arg, HOST_WIDE_INT value,
3c6c0e40 590 int kind ATTRIBUTE_UNUSED, location_t loc ATTRIBUTE_UNUSED,
b78351e5 591 const struct cl_option_handlers *handlers ATTRIBUTE_UNUSED)
4ee9c684 592{
b78351e5 593 bool result = true;
4ee9c684 594 enum opt_code code = (enum opt_code) scode;
595
2ecc6bc5 596 if (gfc_cpp_handle_option (scode, arg, value) == 1)
b78351e5 597 return true;
2ecc6bc5 598
4ee9c684 599 switch (code)
600 {
601 default:
45e6f6f0 602 if (cl_options[code].flags & gfc_option_lang_mask ())
603 break;
b78351e5 604 result = false;
4ee9c684 605 break;
606
da6ffc6d 607 case OPT_fcheck_array_temporaries:
ad8ed98e 608 gfc_option.rtcheck |= GFC_RTCHECK_ARRAY_TEMPS;
da6ffc6d 609 break;
610
8594da07 611 case OPT_fd_lines_as_code:
612 gfc_option.flag_d_lines = 1;
613 break;
614
615 case OPT_fd_lines_as_comments:
616 gfc_option.flag_d_lines = 0;
617 break;
618
4ee9c684 619 case OPT_ffixed_form:
620 gfc_option.source_form = FORM_FIXED;
621 break;
622
623 case OPT_ffree_form:
624 gfc_option.source_form = FORM_FREE;
625 break;
626
9a61fba1 627 case OPT_static_libgfortran:
628#ifndef HAVE_LD_STATIC_DYNAMIC
c4facd47 629 gfc_fatal_error ("%<-static-libgfortran%> is not supported in this "
9a61fba1 630 "configuration");
631#endif
632 break;
633
99227731 634 case OPT_fintrinsic_modules_path:
7cb1cb52 635 case OPT_fintrinsic_modules_path_:
db24420f 636
637 /* This is needed because omp_lib.h is in a directory together
638 with intrinsic modules. Do no warn because during testing
639 without an installed compiler, we would get lots of bogus
640 warnings for a missing include directory. */
641 gfc_add_include_path (arg, false, false, false);
642
99227731 643 gfc_add_intrinsic_modules_path (arg);
644 break;
645
9e3a9b8b 646 case OPT_fpreprocessed:
647 gfc_option.flag_preprocessed = value;
648 break;
649
4ee9c684 650 case OPT_fmax_identifier_length_:
651 if (value > GFC_MAX_SYMBOL_LEN)
b71883de 652 gfc_fatal_error ("Maximum supported identifier length is %d",
4ee9c684 653 GFC_MAX_SYMBOL_LEN);
654 gfc_option.max_identifier_length = value;
655 break;
656
a28eb9a8 657 case OPT_finit_local_zero:
658 gfc_option.flag_init_integer = GFC_INIT_INTEGER_ON;
659 gfc_option.flag_init_integer_value = 0;
4fe73152 660 flag_init_real = GFC_INIT_REAL_ZERO;
a28eb9a8 661 gfc_option.flag_init_logical = GFC_INIT_LOGICAL_FALSE;
662 gfc_option.flag_init_character = GFC_INIT_CHARACTER_ON;
663 gfc_option.flag_init_character_value = (char)0;
664 break;
665
666 case OPT_finit_logical_:
667 if (!strcasecmp (arg, "false"))
668 gfc_option.flag_init_logical = GFC_INIT_LOGICAL_FALSE;
669 else if (!strcasecmp (arg, "true"))
670 gfc_option.flag_init_logical = GFC_INIT_LOGICAL_TRUE;
671 else
c4facd47 672 gfc_fatal_error ("Unrecognized option to %<-finit-logical%>: %s",
a28eb9a8 673 arg);
674 break;
675
a28eb9a8 676 case OPT_finit_integer_:
677 gfc_option.flag_init_integer = GFC_INIT_INTEGER_ON;
678 gfc_option.flag_init_integer_value = atoi (arg);
679 break;
680
681 case OPT_finit_character_:
682 if (value >= 0 && value <= 127)
683 {
684 gfc_option.flag_init_character = GFC_INIT_CHARACTER_ON;
685 gfc_option.flag_init_character_value = (char)value;
686 }
687 else
c4facd47 688 gfc_fatal_error ("The value of n in %<-finit-character=n%> must be "
a28eb9a8 689 "between 0 and 127");
690 break;
691
4ee9c684 692 case OPT_I:
db24420f 693 gfc_add_include_path (arg, true, false, true);
4ee9c684 694 break;
695
696 case OPT_J:
4ee9c684 697 gfc_handle_module_path_options (arg);
ca9ccc0d 698 break;
db96818c 699
8c84a5de 700 case OPT_ffpe_trap_:
553e7cef 701 gfc_handle_fpe_option (arg, true);
702 break;
703
704 case OPT_ffpe_summary_:
705 gfc_handle_fpe_option (arg, false);
8c84a5de 706 break;
707
4ee9c684 708 case OPT_std_f95:
9449a7d9 709 gfc_option.allow_std = GFC_STD_OPT_F95;
4ee9c684 710 gfc_option.warn_std = GFC_STD_F95_OBS;
766398ac 711 gfc_option.max_continue_fixed = 19;
712 gfc_option.max_continue_free = 39;
4ee9c684 713 gfc_option.max_identifier_length = 31;
8290d53f 714 warn_ampersand = 1;
638664b8 715 warn_tabs = 1;
4ee9c684 716 break;
717
718 case OPT_std_f2003:
9449a7d9 719 gfc_option.allow_std = GFC_STD_OPT_F03;
d6583a5f 720 gfc_option.warn_std = GFC_STD_F95_OBS;
4ee9c684 721 gfc_option.max_identifier_length = 63;
8290d53f 722 warn_ampersand = 1;
638664b8 723 warn_tabs = 1;
4ee9c684 724 break;
725
ff4425cf 726 case OPT_std_f2008:
9449a7d9 727 gfc_option.allow_std = GFC_STD_OPT_F08;
a3b81b0f 728 gfc_option.warn_std = GFC_STD_F95_OBS | GFC_STD_F2008_OBS;
ff4425cf 729 gfc_option.max_identifier_length = 63;
8290d53f 730 warn_ampersand = 1;
638664b8 731 warn_tabs = 1;
ff4425cf 732 break;
733
d976af8e 734 case OPT_std_f2008ts:
003e134b 735 case OPT_std_f2018:
9449a7d9 736 gfc_option.allow_std = GFC_STD_OPT_F18;
003e134b 737 gfc_option.warn_std = GFC_STD_F95_OBS | GFC_STD_F2008_OBS
738 | GFC_STD_F2018_OBS;
739 gfc_option.max_identifier_length = 63;
740 warn_ampersand = 1;
741 warn_tabs = 1;
742 break;
743
4ee9c684 744 case OPT_std_gnu:
83a65a0d 745 set_default_std_flags ();
c667e11d 746 break;
747
748 case OPT_std_legacy:
83a65a0d 749 set_default_std_flags ();
c667e11d 750 gfc_option.warn_std = 0;
d6583a5f 751 break;
752
3b6a4b41 753 case OPT_fshort_enums:
4aafe913 754 /* Handled in language-independent code. */
3b6a4b41 755 break;
15774a8b 756
ad8ed98e 757 case OPT_fcheck_:
758 gfc_handle_runtime_check_option (arg);
759 break;
d7cd448a 760
761 case OPT_fdec:
762 /* Enable all DEC extensions. */
763 set_dec_flags (1);
764 break;
765
766 case OPT_fdec_structure:
cf078427 767 flag_dec_structure = 1;
d7cd448a 768 break;
4ee9c684 769 }
770
d3b7ee7c 771 Fortran_handle_option_auto (&global_options, &global_options_set,
772 scode, arg, value,
773 gfc_option_lang_mask (), kind,
774 loc, handlers, global_dc);
4ee9c684 775 return result;
776}
e3d1ab2b 777
778
779/* Return a string with the options passed to the compiler; used for
780 Fortran's compiler_options() intrinsic. */
781
782char *
783gfc_get_option_string (void)
784{
785 unsigned j;
786 size_t len, pos;
787 char *result;
788
90acbb6e 789 /* Allocate and return a one-character string with '\0'. */
790 if (!save_decoded_options_count)
791 return XCNEWVEC (char, 1);
792
e3d1ab2b 793 /* Determine required string length. */
794
795 len = 0;
796 for (j = 1; j < save_decoded_options_count; j++)
797 {
798 switch (save_decoded_options[j].opt_index)
799 {
800 case OPT_o:
801 case OPT_d:
802 case OPT_dumpbase:
803 case OPT_dumpdir:
804 case OPT_auxbase:
805 case OPT_quiet:
806 case OPT_version:
807 case OPT_fintrinsic_modules_path:
7cb1cb52 808 case OPT_fintrinsic_modules_path_:
e3d1ab2b 809 /* Ignore these. */
810 break;
811 default:
293d72e0 812 /* Ignore file names. */
e3d1ab2b 813 if (save_decoded_options[j].orig_option_with_args_text[0] == '-')
814 len += 1
815 + strlen (save_decoded_options[j].orig_option_with_args_text);
816 }
817 }
818
dfa3fb6a 819 result = XCNEWVEC (char, len);
e3d1ab2b 820
821 pos = 0;
822 for (j = 1; j < save_decoded_options_count; j++)
823 {
824 switch (save_decoded_options[j].opt_index)
825 {
826 case OPT_o:
827 case OPT_d:
828 case OPT_dumpbase:
829 case OPT_dumpdir:
830 case OPT_auxbase:
831 case OPT_quiet:
832 case OPT_version:
833 case OPT_fintrinsic_modules_path:
7cb1cb52 834 case OPT_fintrinsic_modules_path_:
e3d1ab2b 835 /* Ignore these. */
836 continue;
837
838 case OPT_cpp_:
839 /* Use "-cpp" rather than "-cpp=<temporary file>". */
840 len = 4;
841 break;
842
843 default:
293d72e0 844 /* Ignore file names. */
e3d1ab2b 845 if (save_decoded_options[j].orig_option_with_args_text[0] != '-')
846 continue;
847
848 len = strlen (save_decoded_options[j].orig_option_with_args_text);
849 }
850
851 memcpy (&result[pos], save_decoded_options[j].orig_option_with_args_text, len);
852 pos += len;
853 result[pos++] = ' ';
854 }
855
856 result[--pos] = '\0';
857 return result;
858}