* fold-const.c (c_getstr): Clamp STRING_LENGTH to STRING_SIZE.
[official-gcc.git] / gcc / params.c
blob623296ce49b6c8cf98da7be08a3fb01bc2c21a93
1 /* params.c - Run-time parameters.
2 Copyright (C) 2001-2018 Free Software Foundation, Inc.
3 Written by Mark Mitchell <mark@codesourcery.com>.
5 This file is part of GCC.
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.
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.
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/>. */
21 #include "config.h"
22 #include "system.h"
23 #include "coretypes.h"
24 #include "common/common-target.h"
25 #include "params.h"
26 #include "params-enum.h"
27 #include "diagnostic-core.h"
28 #include "spellcheck.h"
30 /* An array containing the compiler parameters and their current
31 values. */
33 param_info *compiler_params;
35 /* The number of entries in the table. */
36 static size_t num_compiler_params;
38 /* Whether the parameters have all been initialized and had their
39 default values determined. */
40 static bool params_finished;
42 #define DEFPARAM(ENUM, OPTION, HELP, DEFAULT, MIN, MAX)
43 #define DEFPARAMENUM5(ENUM, OPTION, HELP, DEFAULT, V0, V1, V2, V3, V4) \
44 static const char *values_ ## ENUM [] = { #V0, #V1, #V2, #V3, #V4, NULL };
45 #include "params.def"
46 #undef DEFPARAMENUM5
47 #undef DEFPARAM
49 static const param_info lang_independent_params[] = {
50 #define DEFPARAM(ENUM, OPTION, HELP, DEFAULT, MIN, MAX) \
51 { OPTION, DEFAULT, MIN, MAX, HELP, NULL },
52 #define DEFPARAMENUM5(ENUM, OPTION, HELP, DEFAULT, \
53 V0, V1, V2, V3, V4) \
54 { OPTION, (int)ENUM ## _KIND_ ## DEFAULT, 0, 4, HELP, values_ ## ENUM },
55 #include "params.def"
56 #undef DEFPARAM
57 #undef DEFPARAMENUM5
58 { NULL, 0, 0, 0, NULL, NULL }
61 /* Add the N PARAMS to the current list of compiler parameters. */
63 void
64 add_params (const param_info params[], size_t n)
66 gcc_assert (!params_finished);
68 /* Allocate enough space for the new parameters. */
69 compiler_params = XRESIZEVEC (param_info, compiler_params,
70 num_compiler_params + n);
71 /* Copy them into the table. */
72 memcpy (compiler_params + num_compiler_params,
73 params,
74 n * sizeof (param_info));
75 /* Keep track of how many parameters we have. */
76 num_compiler_params += n;
79 /* Add all parameters and default values that can be set in both the
80 driver and the compiler proper. */
82 void
83 global_init_params (void)
85 gcc_assert (!params_finished);
87 add_params (lang_independent_params, LAST_PARAM);
88 targetm_common.option_default_params ();
91 /* Note that all parameters have been added and all default values
92 set. */
94 void
95 finish_params (void)
97 params_finished = true;
100 /* Reset all state within params.c so that we can rerun the compiler
101 within the same process. For use by toplev::finalize. */
103 void
104 params_c_finalize (void)
106 XDELETEVEC (compiler_params);
107 compiler_params = NULL;
108 num_compiler_params = 0;
109 params_finished = false;
112 /* Set the value of the parameter given by NUM to VALUE in PARAMS and
113 PARAMS_SET. If EXPLICIT_P, this is being set by the user;
114 otherwise it is being set implicitly by the compiler. */
116 static void
117 set_param_value_internal (compiler_param num, int value,
118 int *params, int *params_set,
119 bool explicit_p)
121 size_t i = (size_t) num;
123 gcc_assert (params_finished);
125 params[i] = value;
126 if (explicit_p)
127 params_set[i] = true;
130 /* Return true if it can find the matching entry for NAME in the parameter
131 table, and assign the entry index to INDEX. Return false otherwise. */
133 bool
134 find_param (const char *name, enum compiler_param *index)
136 for (size_t i = 0; i < num_compiler_params; ++i)
137 if (strcmp (compiler_params[i].option, name) == 0)
139 *index = (enum compiler_param) i;
140 return true;
143 return false;
146 /* Look for the closest match for NAME in the parameter table, returning it
147 if it is a reasonable suggestion for a misspelling. Return NULL
148 otherwise. */
150 const char *
151 find_param_fuzzy (const char *name)
153 best_match <const char *, const char *> bm (name);
154 for (size_t i = 0; i < num_compiler_params; ++i)
155 bm.consider (compiler_params[i].option);
156 return bm.get_best_meaningful_candidate ();
159 /* Return true if param with entry index INDEX should be defined using strings.
160 If so, return the value corresponding to VALUE_NAME in *VALUE_P. */
162 bool
163 param_string_value_p (enum compiler_param index, const char *value_name,
164 int *value_p)
166 param_info *entry = &compiler_params[(int) index];
167 if (entry->value_names == NULL)
168 return false;
170 *value_p = -1;
172 for (int i = 0; entry->value_names[i] != NULL; ++i)
173 if (strcmp (entry->value_names[i], value_name) == 0)
175 *value_p = i;
176 return true;
179 return true;
182 /* Set the VALUE associated with the parameter given by NAME in PARAMS
183 and PARAMS_SET. */
185 void
186 set_param_value (const char *name, int value,
187 int *params, int *params_set)
189 size_t i;
191 /* Make sure nobody tries to set a parameter to an invalid value. */
192 gcc_assert (value != INVALID_PARAM_VAL);
194 enum compiler_param index;
195 if (!find_param (name, &index))
197 /* If we didn't find this parameter, issue an error message. */
198 error ("invalid parameter %qs", name);
199 return;
201 i = (size_t)index;
203 if (value < compiler_params[i].min_value)
204 error ("minimum value of parameter %qs is %u",
205 compiler_params[i].option,
206 compiler_params[i].min_value);
207 else if (compiler_params[i].max_value > compiler_params[i].min_value
208 && value > compiler_params[i].max_value)
209 error ("maximum value of parameter %qs is %u",
210 compiler_params[i].option,
211 compiler_params[i].max_value);
212 else
213 set_param_value_internal ((compiler_param) i, value,
214 params, params_set, true);
217 /* Set the value of the parameter given by NUM to VALUE in PARAMS and
218 PARAMS_SET, implicitly, if it has not been set explicitly by the
219 user. */
221 void
222 maybe_set_param_value (compiler_param num, int value,
223 int *params, int *params_set)
225 if (!params_set[(int) num])
226 set_param_value_internal (num, value, params, params_set, false);
229 /* Set the default value of a parameter given by NUM to VALUE, before
230 option processing. */
232 void
233 set_default_param_value (compiler_param num, int value)
235 gcc_assert (!params_finished);
237 compiler_params[(int) num].default_value = value;
240 /* Return the default value of parameter NUM. */
243 default_param_value (compiler_param num)
245 return compiler_params[(int) num].default_value;
248 /* Initialize an array PARAMS with default values of the
249 parameters. */
251 void
252 init_param_values (int *params)
254 size_t i;
256 gcc_assert (params_finished);
258 for (i = 0; i < num_compiler_params; i++)
259 params[i] = compiler_params[i].default_value;
262 /* Return the current value of num_compiler_params, for the benefit of
263 plugins that use parameters as features. */
265 size_t
266 get_num_compiler_params (void)
268 return num_compiler_params;