1 /* Provide option suggestion for --complete option and a misspelled
3 Copyright (C) 2016-2018 Free Software Foundation, Inc.
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
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
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/>. */
23 #include "coretypes.h"
27 #include "spellcheck.h"
28 #include "opt-suggestions.h"
29 #include "common/common-target.h"
32 option_proposer::~option_proposer ()
34 delete m_option_suggestions
;
38 option_proposer::suggest_option (const char *bad_opt
)
40 /* Lazily populate m_option_suggestions. */
41 if (!m_option_suggestions
)
42 build_option_suggestions (NULL
);
43 gcc_assert (m_option_suggestions
);
45 /* "m_option_suggestions" is now populated. Use it. */
46 return find_closest_string
48 (auto_vec
<const char *> *) m_option_suggestions
);
51 /* Populate RESULTS with valid completions of options that begin
52 with OPTION_PREFIX. */
55 option_proposer::get_completions (const char *option_prefix
,
56 auto_string_vec
&results
)
58 /* Bail out for an invalid input. */
59 if (option_prefix
== NULL
|| option_prefix
[0] == '\0')
62 /* Option suggestions are built without first leading dash character. */
63 if (option_prefix
[0] == '-')
66 size_t length
= strlen (option_prefix
);
68 /* Handle OPTION_PREFIX starting with "-param". */
69 const char *prefix
= "-param";
70 if (length
>= strlen (prefix
)
71 && strstr (option_prefix
, prefix
) == option_prefix
)
73 /* We support both '-param-xyz=123' and '-param xyz=123' */
74 option_prefix
+= strlen (prefix
);
75 char separator
= option_prefix
[0];
77 if (separator
== ' ' || separator
== '=')
78 find_param_completions (separator
, option_prefix
, results
);
82 /* Lazily populate m_option_suggestions. */
83 if (!m_option_suggestions
)
84 build_option_suggestions (option_prefix
);
85 gcc_assert (m_option_suggestions
);
87 for (unsigned i
= 0; i
< m_option_suggestions
->length (); i
++)
89 char *candidate
= (*m_option_suggestions
)[i
];
90 if (strlen (candidate
) >= length
91 && strstr (candidate
, option_prefix
) == candidate
)
92 results
.safe_push (concat ("-", candidate
, NULL
));
97 /* Print on stdout a list of valid options that begin with OPTION_PREFIX,
98 one per line, suitable for use by Bash completion.
100 Implementation of the "-completion=" option. */
103 option_proposer::suggest_completion (const char *option_prefix
)
105 auto_string_vec results
;
106 get_completions (option_prefix
, results
);
107 for (unsigned i
= 0; i
< results
.length (); i
++)
108 printf ("%s\n", results
[i
]);
112 option_proposer::build_option_suggestions (const char *prefix
)
114 gcc_assert (m_option_suggestions
== NULL
);
115 m_option_suggestions
= new auto_string_vec ();
117 /* We build a vec of m_option_suggestions, using add_misspelling_candidates
118 to add copies of strings, without a leading dash. */
120 for (unsigned int i
= 0; i
< cl_options_count
; i
++)
122 const struct cl_option
*option
= &cl_options
[i
];
123 const char *opt_text
= option
->opt_text
;
127 if (option
->var_type
== CLVC_ENUM
)
129 const struct cl_enum
*e
= &cl_enums
[option
->var_enum
];
130 for (unsigned j
= 0; e
->values
[j
].arg
!= NULL
; j
++)
132 char *with_arg
= concat (opt_text
, e
->values
[j
].arg
, NULL
);
133 add_misspelling_candidates (m_option_suggestions
, option
,
140 if (option
->flags
& CL_TARGET
)
142 vec
<const char *> option_values
143 = targetm_common
.get_valid_option_values (i
, prefix
);
144 if (!option_values
.is_empty ())
146 for (unsigned j
= 0; j
< option_values
.length (); j
++)
148 char *with_arg
= concat (opt_text
, option_values
[j
],
150 add_misspelling_candidates (m_option_suggestions
, option
,
157 add_misspelling_candidates (m_option_suggestions
, option
,
163 case OPT_fsanitize_recover_
:
164 /* -fsanitize= and -fsanitize-recover= can take
165 a comma-separated list of arguments. Given that combinations
166 are supported, we can't add all potential candidates to the
167 vec, but if we at least add them individually without commas,
168 we should do a better job e.g. correcting
172 rather than to "-Wframe-address" (PR driver/69265). */
174 for (int j
= 0; sanitizer_opts
[j
].name
!= NULL
; ++j
)
176 struct cl_option optb
;
177 /* -fsanitize=all is not valid, only -fno-sanitize=all.
178 So don't register the positive misspelling candidates
180 if (sanitizer_opts
[j
].flag
== ~0U && i
== OPT_fsanitize_
)
183 optb
.opt_text
= opt_text
= "-fno-sanitize=";
184 optb
.cl_reject_negative
= true;
187 /* Get one arg at a time e.g. "-fsanitize=address". */
188 char *with_arg
= concat (opt_text
,
189 sanitizer_opts
[j
].name
,
191 /* Add with_arg and all of its variant spellings e.g.
192 "-fno-sanitize=address" to candidates (albeit without
194 add_misspelling_candidates (m_option_suggestions
, option
,
204 /* Find parameter completions for --param format with SEPARATOR.
205 Again, save the completions into results. */
208 option_proposer::find_param_completions (const char separator
,
209 const char *param_prefix
,
210 auto_string_vec
&results
)
212 char separator_str
[] = {separator
, '\0'};
213 size_t length
= strlen (param_prefix
);
214 for (unsigned i
= 0; i
< get_num_compiler_params (); ++i
)
216 const char *candidate
= compiler_params
[i
].option
;
217 if (strlen (candidate
) >= length
218 && strstr (candidate
, param_prefix
) == candidate
)
219 results
.safe_push (concat ("--param", separator_str
, candidate
, NULL
));
227 /* Verify that PROPOSER generates sane auto-completion suggestions
228 for OPTION_PREFIX. */
231 verify_autocompletions (option_proposer
&proposer
, const char *option_prefix
)
233 auto_string_vec suggestions
;
234 proposer
.get_completions (option_prefix
, suggestions
);
236 /* There must be at least one suggestion, and every suggestion must
237 indeed begin with OPTION_PREFIX. */
239 ASSERT_GT (suggestions
.length (), 0);
241 for (unsigned i
= 0; i
< suggestions
.length (); i
++)
242 ASSERT_STR_STARTSWITH (suggestions
[i
], option_prefix
);
245 /* Verify that valid options are auto-completed correctly. */
248 test_completion_valid_options (option_proposer
&proposer
)
250 const char *option_prefixes
[] =
252 "-fno-var-tracking-assignments-toggle",
253 "-fpredictive-commoning",
254 "--param=stack-clash-protection-guard-size",
255 "--param=max-predicted-iterations",
256 "-ftree-loop-distribute-patterns",
259 "--param=ipa-cp-value-list-size",
262 "--param=tracer-dynamic-coverage-feedback",
263 "-Wno-format-contains-nul",
265 "-fisolate-erroneous-paths-attribute",
268 "--param=asan-globals",
271 "-Wformat-zero-length",
272 "-Wstringop-truncation",
275 "-Warray-temporaries",
277 "-Wreturn-local-addr",
278 "--param=sms-dfa-history",
279 "--param=asan-instrument-reads",
283 "--param=max-fields-for-field-sensitive",
284 "-fisolate-erroneous-paths-dereference",
286 "-Wcast-align=strict",
288 "-Wpacked-not-aligned",
292 "--param=max-iterations-computation-cost",
293 "-Wmultiple-inheritance",
294 "-fno-sel-sched-reschedule-pipelined",
295 "-Wassign-intercept",
296 "-Wno-format-security",
297 "-fno-sched-stalled-insns",
298 "-fbtr-bb-exclusive",
299 "-fno-tree-tail-merge",
301 "-Wno-unused-but-set-parameter",
305 for (const char **ptr
= option_prefixes
; *ptr
!= NULL
; ptr
++)
306 verify_autocompletions (proposer
, *ptr
);
309 /* Verify that valid parameters are auto-completed correctly,
310 both with the "--param=PARAM" form and the "--param PARAM" form. */
313 test_completion_valid_params (option_proposer
&proposer
)
315 const char *option_prefixes
[] =
317 "--param=sched-state-edge-prob-cutoff",
318 "--param=iv-consider-all-candidates-bound",
319 "--param=align-threshold",
320 "--param=prefetch-min-insn-to-mem-ratio",
321 "--param=max-unrolled-insns",
322 "--param=max-early-inliner-iterations",
323 "--param=max-vartrack-reverse-op-size",
324 "--param=ipa-cp-loop-hint-bonus",
325 "--param=tracer-min-branch-ratio",
326 "--param=graphite-max-arrays-per-scop",
327 "--param=sink-frequency-threshold",
328 "--param=max-cse-path-length",
329 "--param=sra-max-scalarization-size-Osize",
330 "--param=prefetch-latency",
331 "--param=dse-max-object-size",
332 "--param=asan-globals",
333 "--param=max-vartrack-size",
334 "--param=case-values-threshold",
335 "--param=max-slsr-cand-scan",
336 "--param=min-insn-to-prefetch-ratio",
337 "--param=tracer-min-branch-probability",
338 "--param sink-frequency-threshold",
339 "--param max-cse-path-length",
340 "--param sra-max-scalarization-size-Osize",
341 "--param prefetch-latency",
342 "--param dse-max-object-size",
343 "--param asan-globals",
344 "--param max-vartrack-size",
348 for (const char **ptr
= option_prefixes
; *ptr
!= NULL
; ptr
++)
349 verify_autocompletions (proposer
, *ptr
);
352 /* Return true when EXPECTED is one of completions for OPTION_PREFIX string. */
355 in_completion_p (option_proposer
&proposer
, const char *option_prefix
,
356 const char *expected
)
358 auto_string_vec suggestions
;
359 proposer
.get_completions (option_prefix
, suggestions
);
361 for (unsigned i
= 0; i
< suggestions
.length (); i
++)
363 char *r
= suggestions
[i
];
364 if (strcmp (r
, expected
) == 0)
371 /* Return true when PROPOSER does not find any partial completion
372 for OPTION_PREFIX. */
375 empty_completion_p (option_proposer
&proposer
, const char *option_prefix
)
377 auto_string_vec suggestions
;
378 proposer
.get_completions (option_prefix
, suggestions
);
379 return suggestions
.is_empty ();
382 /* Verify autocompletions of partially-complete options. */
385 test_completion_partial_match (option_proposer
&proposer
)
387 ASSERT_TRUE (in_completion_p (proposer
, "-fsani", "-fsanitize=address"));
388 ASSERT_TRUE (in_completion_p (proposer
, "-fsani",
389 "-fsanitize-address-use-after-scope"));
390 ASSERT_TRUE (in_completion_p (proposer
, "-fipa-icf", "-fipa-icf-functions"));
391 ASSERT_TRUE (in_completion_p (proposer
, "-fipa-icf", "-fipa-icf"));
392 ASSERT_TRUE (in_completion_p (proposer
, "--param=",
393 "--param=max-vartrack-reverse-op-size"));
394 ASSERT_TRUE (in_completion_p (proposer
, "--param ",
395 "--param max-vartrack-reverse-op-size"));
397 ASSERT_FALSE (in_completion_p (proposer
, "-fipa-icf", "-fipa"));
398 ASSERT_FALSE (in_completion_p (proposer
, "-fipa-icf-functions", "-fipa-icf"));
400 ASSERT_FALSE (empty_completion_p (proposer
, "-"));
401 ASSERT_FALSE (empty_completion_p (proposer
, "-fipa"));
402 ASSERT_FALSE (empty_completion_p (proposer
, "--par"));
405 /* Verify that autocompletion does not return any match for garbage inputs. */
408 test_completion_garbage (option_proposer
&proposer
)
410 ASSERT_TRUE (empty_completion_p (proposer
, NULL
));
411 ASSERT_TRUE (empty_completion_p (proposer
, ""));
412 ASSERT_TRUE (empty_completion_p (proposer
, "- "));
413 ASSERT_TRUE (empty_completion_p (proposer
, "123456789"));
414 ASSERT_TRUE (empty_completion_p (proposer
, "---------"));
415 ASSERT_TRUE (empty_completion_p (proposer
, "#########"));
416 ASSERT_TRUE (empty_completion_p (proposer
, "- - - - - -"));
417 ASSERT_TRUE (empty_completion_p (proposer
, "-fsanitize=address2"));
420 /* Run all of the selftests within this file. */
423 opt_proposer_c_tests ()
425 option_proposer proposer
;
427 test_completion_valid_options (proposer
);
428 test_completion_valid_params (proposer
);
429 test_completion_partial_match (proposer
);
430 test_completion_garbage (proposer
);
433 } // namespace selftest
435 #endif /* #if CHECKING_P */