gcc/gcc/spellcheck.c

301 lines
8.9 KiB
C
Raw Normal View History

/* Find near-matches for strings.
Copyright (C) 2015-2017 Free Software Foundation, Inc.
This file is part of GCC.
GCC is free software; you can redistribute it and/or modify it under
the terms of the GNU General Public License as published by the Free
Software Foundation; either version 3, or (at your option) any later
version.
GCC is distributed in the hope that it will be useful, but WITHOUT ANY
WARRANTY; without even the implied warranty of MERCHANTABILITY or
FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
for more details.
You should have received a copy of the GNU General Public License
along with GCC; see the file COPYING3. If not see
<http://www.gnu.org/licenses/>. */
#include "config.h"
#include "system.h"
#include "coretypes.h"
#include "tm.h"
#include "tree.h"
#include "spellcheck.h"
Selftest framework gcc/ChangeLog: * Makefile.in (OBJS): Add function-tests.o, hash-map-tests.o, hash-set-tests.o, rtl-tests.o, selftest-run-tests.o. (OBJS-libcommon): Add selftest.o. (OBJS-libcommon-target): Add selftest.o. (all.internal): Add "selftest". (all.cross): Likewise. (selftest): New phony target. (s-selftest): New target. (selftest-gdb): New phony target. (COLLECT2_OBJS): Add selftest.o. * bitmap.c: Include "selftest.h". (selftest::test_gc_alloc): New function. (selftest::test_set_range): New function. (selftest::test_clear_bit_in_middle): New function. (selftest::test_copying): New function. (selftest::test_bitmap_single_bit_set_p): New function. (selftest::bitmap_c_tests): New function. * common.opt (fself-test): New. * diagnostic-show-locus.c: Include "selftest.h". (make_range): New function. (test_range_contains_point_for_single_point): New function. (test_range_contains_point_for_single_line): New function. (test_range_contains_point_for_multiple_lines): New function. (assert_eq): New function. (test_get_line_width_without_trailing_whitespace): New function. (selftest::diagnostic_show_locus_c_tests): New function. * et-forest.c: Include "selftest.h". (selftest::test_single_node): New function. (selftest::test_simple_tree): New function. (selftest::test_disconnected_nodes): New function. (selftest::et_forest_c_tests): New function. * fold-const.c: Include "selftest.h". (selftest::assert_binop_folds_to_const): New function. (selftest::assert_binop_folds_to_nonlvalue): New function. (selftest::test_arithmetic_folding): New function. (selftest::fold_const_c_tests): New function. * function-tests.c: New file. * gimple.c: Include "selftest.h". Include "gimple-pretty-print.h". (selftest::verify_gimple_pp): New function. (selftest::test_assign_single): New function. (selftest::test_assign_binop): New function. (selftest::test_nop_stmt): New function. (selftest::test_return_stmt): New function. (selftest::test_return_without_value): New function. (selftest::gimple_c_tests): New function. * hash-map-tests.c: New file. * hash-set-tests.c: New file. * input.c: Include "selftest.h". (selftest::assert_loceq): New function. (selftest::test_accessing_ordinary_linemaps): New function. (selftest::test_unknown_location): New function. (selftest::test_builtins): New function. (selftest::test_reading_source_line): New function. (selftest::input_c_tests): New function. * rtl-tests.c: New file. * selftest-run-tests.c: New file. * selftest.c: New file. * selftest.h: New file. * spellcheck.c: Include "selftest.h". (selftest::levenshtein_distance_unit_test_oneway): New function, adapted from testsuite/gcc.dg/plugin/levenshtein_plugin.c. (selftest::levenshtein_distance_unit_test): Likewise. (selftest::spellcheck_c_tests): Likewise. * toplev.c: Include selftest.h. (toplev::run_self_tests): New. (toplev::main): Handle -fself-test. * toplev.h (toplev::run_self_tests): New. * tree.c: Include "selftest.h". (selftest::test_integer_constants): New function. (selftest::test_identifiers): New function. (selftest::test_labels): New function. (selftest::tree_c_tests): New function. * tree-cfg.c: Include "selftest.h". (selftest::push_fndecl): New function. (selftest::test_linear_chain): New function. (selftest::test_diamond): New function. (selftest::test_fully_connected): New function. (selftest::tree_cfg_c_tests): New function. * vec.c: Include "selftest.h". (selftest::safe_push_range): New function. (selftest::test_quick_push): New function. (selftest::test_safe_push): New function. (selftest::test_truncate): New function. (selftest::test_safe_grow_cleared): New function. (selftest::test_pop): New function. (selftest::test_safe_insert): New function. (selftest::test_ordered_remove): New function. (selftest::test_unordered_remove): New function. (selftest::test_block_remove): New function. (selftest::reverse_cmp): New function. (selftest::test_qsort): New function. (selftest::vec_c_tests): New function.c. * wide-int.cc: Include selftest.h and wide-int-print.h. (selftest::from_int <wide_int>): New function. (selftest::from_int <offset_int>): New function. (selftest::from_int <widest_int>): New function. (selftest::assert_deceq): New function. (selftest::assert_hexeq): New function. (selftest::test_printing <VALUE_TYPE>): New function template. (selftest::test_ops <VALUE_TYPE>): New function template. (selftest::test_comparisons <VALUE_TYPE>): New function template. (selftest::run_all_wide_int_tests <VALUE_TYPE>): New function template. (selftest::wide_int_cc_tests): New function. gcc/testsuite/ChangeLog: * gcc.dg/plugin/levenshtein-test-1.c: Delete. * gcc.dg/plugin/levenshtein_plugin.c: Delete. * gcc.dg/plugin/plugin.exp (plugin_test_list): Remove the above. From-SVN: r237144
2016-06-06 19:11:30 +02:00
#include "selftest.h"
/* The Levenshtein distance is an "edit-distance": the minimal
number of one-character insertions, removals or substitutions
that are needed to change one string into another.
This implementation uses the Wagner-Fischer algorithm. */
edit_distance_t
levenshtein_distance (const char *s, int len_s,
const char *t, int len_t)
{
const bool debug = false;
if (debug)
{
printf ("s: \"%s\" (len_s=%i)\n", s, len_s);
printf ("t: \"%s\" (len_t=%i)\n", t, len_t);
}
if (len_s == 0)
return len_t;
if (len_t == 0)
return len_s;
/* We effectively build a matrix where each (i, j) contains the
Levenshtein distance between the prefix strings s[0:j]
and t[0:i].
Rather than actually build an (len_t + 1) * (len_s + 1) matrix,
we simply keep track of the last row, v0 and a new row, v1,
which avoids an (len_t + 1) * (len_s + 1) allocation and memory accesses
in favor of two (len_s + 1) allocations. These could potentially be
statically-allocated if we impose a maximum length on the
strings of interest. */
edit_distance_t *v0 = new edit_distance_t[len_s + 1];
edit_distance_t *v1 = new edit_distance_t[len_s + 1];
/* The first row is for the case of an empty target string, which
we can reach by deleting every character in the source string. */
for (int i = 0; i < len_s + 1; i++)
v0[i] = i;
/* Build successive rows. */
for (int i = 0; i < len_t; i++)
{
if (debug)
{
printf ("i:%i v0 = ", i);
for (int j = 0; j < len_s + 1; j++)
printf ("%i ", v0[j]);
printf ("\n");
}
/* The initial column is for the case of an empty source string; we
can reach prefixes of the target string of length i
by inserting i characters. */
v1[0] = i + 1;
cgraph.c: Spelling fixes - behaviour -> behavior and neighbour -> neighbor. * cgraph.c: Spelling fixes - behaviour -> behavior and neighbour -> neighbor. * target.def: Likewise. * sel-sched.c: Likewise. * config/mips/mips.c: Likewise. * config/arc/arc.md: Likewise. * config/arm/cortex-a57.md: Likewise. * config/arm/arm.c: Likewise. * config/arm/neon.md: Likewise. * config/arm/arm-c.c: Likewise. * config/vms/vms-c.c: Likewise. * config/s390/s390.c: Likewise. * config/i386/znver1.md: Likewise. * config/i386/i386.c: Likewise. * config/ia64/hpux-unix2003.h: Likewise. * config/msp430/msp430.md: Likewise. * config/rx/rx.c: Likewise. * config/rx/rx.md: Likewise. * config/aarch64/aarch64-simd.md: Likewise. * config/aarch64/aarch64.c: Likewise. * config/nvptx/nvptx.c: Likewise. * config/bfin/bfin.c: Likewise. * config/cris/cris.opt: Likewise. * config/rs6000/rs6000.c: Likewise. * target.h: Likewise. * spellcheck.c: Likewise. * ira-build.c: Likewise. * tree-inline.c: Likewise. * builtins.c: Likewise. * lra-constraints.c: Likewise. * explow.c: Likewise. * hwint.h: Likewise. * targhooks.c: Likewise. * tree-vect-data-refs.c: Likewise. * expr.c: Likewise. * doc/tm.texi: Likewise. * doc/extend.texi: Likewise. * doc/install.texi: Likewise. * doc/md.texi: Likewise. * tree-ssa-tail-merge.c: Likewise. * sched-int.h: Likewise. * match.pd: Likewise. * sched-ebb.c: Likewise. * target.def (omit_struct_return_reg): Likewise. * gimple-ssa-isolate-paths.c: Likewise. (find_implicit_erroneous_behaviour): Renamed to... (find_implicit_erroneous_behavior): ... this. (find_explicit_erroneous_behaviour): Renamed to... (find_explicit_erroneous_behavior): ... this. (gimple_ssa_isolate_erroneous_paths): Adjust caller. gcc/cp/ * error.c: Spelling fixes - behaviour -> behavior and neighbour -> neighbor. * decl.c: Likewise. * typeck.c (cp_build_binary_op): Fix up behavior spelling in diagnostics. * init.c (build_delete): Likewise. gcc/objc/ * objc-act.c: Spelling fixes - behaviour -> behavior and neighbour -> neighbor. * objc-map.h: Likewise. gcc/go/ * gofrontend/lex.cc: Spelling fixes - behaviour -> behavior and neighbour -> neighbor. * gccgo.texi: Likewise. gcc/ada/ * prj-tree.ads: Spelling fixes - behaviour -> behavior and neighbour -> neighbor. * prep.adb: Likewise. * prj.ads: Likewise. * prepcomp.adb: Likewise. * g-socket.ads: Likewise. * s-imgrea.adb: Likewise. * a-calend.adb: Likewise. * exp_disp.adb: Likewise. * doc/gnat_ugn/gnat_utility_programs.rst: Likewise. * g-socket.adb: Likewise. * sem_ch12.adb: Likewise. * terminals.c: Likewise. gcc/testsuite/ * objc.dg/gnu-api-2-method.m: Spelling fixes - behaviour -> behavior and neighbour -> neighbor. * objc.dg/attributes/method-nonnull-1.m: Likewise. * objc.dg/gnu-api-2-class-meta.m: Likewise. * c-c++-common/Wvarargs.c: Likewise. * c-c++-common/goacc/host_data-5.c: Likewise. * obj-c++.dg/gnu-api-2-class-meta.mm: Likewise. * obj-c++.dg/attributes/method-nonnull-1.mm: Likewise. * obj-c++.dg/gnu-api-2-method.mm: Likewise. * gcc.target/aarch64/pr60697.c: Likewise. * gcc.target/aarch64/advsimd-intrinsics/vldX_lane.c: Likewise. * gcc.target/aarch64/advsimd-intrinsics/vqshl.c: Likewise. * gcc.target/aarch64/advsimd-intrinsics/vshuffle.inc: Likewise. * gcc.target/aarch64/advsimd-intrinsics/vrshl.c: Likewise. * gcc.target/aarch64/advsimd-intrinsics/vldX_dup.c: Likewise. * gcc.target/aarch64/advsimd-intrinsics/vstX_lane.c: Likewise. * gcc.target/aarch64/advsimd-intrinsics/vqrshl.c: Likewise. * gcc.target/aarch64/advsimd-intrinsics/vldX.c: Likewise. * gcc.target/aarch64/aapcs64/ice_2.c: Likewise. * gcc.target/aarch64/aapcs64/test_23.c: Likewise. * gcc.target/aarch64/vrnd_f64_1.c: Likewise. * g++.dg/warn/Wconversion-real-integer-3.C: Likewise. * g++.dg/lookup/koenig5.C: Likewise. * g++.dg/ext/no-asm-2.C: Likewise. * gfortran.dg/bounds_check_array_ctor_3.f90: Likewise. * gfortran.dg/bounds_check_array_ctor_7.f90: Likewise. * gfortran.dg/used_types_16.f90: Likewise. * gfortran.dg/assumed_rank_bounds_1.f90: Likewise. * gfortran.dg/bounds_check_array_ctor_1.f90: Likewise. * gfortran.dg/assumed_rank_bounds_2.f90: Likewise. * gfortran.dg/bounds_check_array_ctor_4.f90: Likewise. * gfortran.dg/abstract_type_6.f03: Likewise. * gfortran.dg/bounds_check_array_ctor_5.f90: Likewise. * gfortran.dg/used_types_15.f90: Likewise. * gfortran.dg/bounds_check_array_ctor_8.f90: Likewise. * gfortran.dg/exit_3.f08: Likewise. * gfortran.dg/open_status_2.f90: Likewise. * gfortran.dg/derived_pointer_recursion_2.f90: Likewise. * gfortran.dg/intrinsic_std_1.f90: Likewise. * gfortran.dg/associate_1.f03: Likewise. * gfortran.dg/bounds_check_array_ctor_2.f90: Likewise. * gfortran.dg/intrinsic_std_6.f90: Likewise. * gfortran.dg/bounds_check_array_ctor_6.f90: Likewise. * gcc.dg/builtin-object-size-1.c: Likewise. * gcc.dg/noreturn-6.c: Likewise. * gcc.dg/builtin-stringop-chk-1.c: Likewise. * gcc.dg/globalalias.c: Likewise. * gcc.dg/builtins-config.h: Likewise. * gcc.dg/pr30457.c: Likewise. * gcc.c-torture/compile/volatile-1.c: Likewise. * gcc.c-torture/execute/20101011-1.c: Likewise. * c-c++-common/Waddress-1.c: Likewise. From-SVN: r233358
2016-02-12 00:53:54 +01:00
/* Build the rest of the row by considering neighbors to
the north, west and northwest. */
for (int j = 0; j < len_s; j++)
{
edit_distance_t cost = (s[j] == t[i] ? 0 : 1);
edit_distance_t deletion = v1[j] + 1;
edit_distance_t insertion = v0[j + 1] + 1;
edit_distance_t substitution = v0[j] + cost;
edit_distance_t cheapest = MIN (deletion, insertion);
cheapest = MIN (cheapest, substitution);
v1[j + 1] = cheapest;
}
/* Prepare to move on to next row. */
for (int j = 0; j < len_s + 1; j++)
v0[j] = v1[j];
}
if (debug)
{
printf ("final v1 = ");
for (int j = 0; j < len_s + 1; j++)
printf ("%i ", v1[j]);
printf ("\n");
}
edit_distance_t result = v1[len_s];
delete[] v0;
delete[] v1;
return result;
}
/* Calculate Levenshtein distance between two nil-terminated strings. */
edit_distance_t
levenshtein_distance (const char *s, const char *t)
{
return levenshtein_distance (s, strlen (s), t, strlen (t));
}
PR driver/69265 and 69453: improved suggestions for various misspelled options gcc/ChangeLog: PR driver/69265 PR driver/69453 * gcc.c (driver::driver): Initialize m_option_suggestions. (driver::~driver): Clean up m_option_suggestions. (suggest_option): Convert to... (driver::suggest_option): ...this, and split out into driver::build_option_suggestions and find_closest_string. (driver::build_option_suggestions): New function, from first half of suggest_option. Special-case OPT_fsanitize_ and OPT_fsanitize_recover_, making use of the sanitizer_opts array. For options of enum types, add the various enum values to the candidate strings. (driver::handle_unrecognized_options): Remove "const". * gcc.h (driver::handle_unrecognized_options): Likewise. (driver::build_option_suggestions): New decl. (driver::suggest_option): New decl. (driver::m_option_suggestions): New field. * opts-common.c (add_misspelling_candidates): New function. * opts.c (sanitizer_opts): Remove decl of struct sanitizer_opts_s and make non-static. * opts.h (sanitizer_opts): New array decl. (add_misspelling_candidates): New function decl. * spellcheck.c (find_closest_string): New function. * spellcheck.h (find_closest_string): New function decl. gcc/testsuite/ChangeLog: PR driver/69265 PR driver/69453 * gcc.dg/spellcheck-options-3.c: New test case. * gcc.dg/spellcheck-options-4.c: New test case. * gcc.dg/spellcheck-options-5.c: New test case. * gcc.dg/spellcheck-options-6.c: New test case. * gcc.dg/spellcheck-options-7.c: New test case. * gcc.dg/spellcheck-options-8.c: New test case. * gcc.dg/spellcheck-options-9.c: New test case. * gcc.dg/spellcheck-options-10.c: New test case. From-SVN: r233382
2016-02-12 18:39:27 +01:00
/* Given TARGET, a non-NULL string, and CANDIDATES, a non-NULL ptr to
an autovec of non-NULL strings, determine which element within
CANDIDATES has the lowest edit distance to TARGET. If there are
multiple elements with the same minimal distance, the first in the
vector wins.
If more than half of the letters were misspelled, the suggestion is
likely to be meaningless, so return NULL for this case. */
const char *
find_closest_string (const char *target,
const auto_vec<const char *> *candidates)
{
gcc_assert (target);
gcc_assert (candidates);
int i;
const char *candidate;
best_match<const char *, const char *> bm (target);
PR driver/69265 and 69453: improved suggestions for various misspelled options gcc/ChangeLog: PR driver/69265 PR driver/69453 * gcc.c (driver::driver): Initialize m_option_suggestions. (driver::~driver): Clean up m_option_suggestions. (suggest_option): Convert to... (driver::suggest_option): ...this, and split out into driver::build_option_suggestions and find_closest_string. (driver::build_option_suggestions): New function, from first half of suggest_option. Special-case OPT_fsanitize_ and OPT_fsanitize_recover_, making use of the sanitizer_opts array. For options of enum types, add the various enum values to the candidate strings. (driver::handle_unrecognized_options): Remove "const". * gcc.h (driver::handle_unrecognized_options): Likewise. (driver::build_option_suggestions): New decl. (driver::suggest_option): New decl. (driver::m_option_suggestions): New field. * opts-common.c (add_misspelling_candidates): New function. * opts.c (sanitizer_opts): Remove decl of struct sanitizer_opts_s and make non-static. * opts.h (sanitizer_opts): New array decl. (add_misspelling_candidates): New function decl. * spellcheck.c (find_closest_string): New function. * spellcheck.h (find_closest_string): New function decl. gcc/testsuite/ChangeLog: PR driver/69265 PR driver/69453 * gcc.dg/spellcheck-options-3.c: New test case. * gcc.dg/spellcheck-options-4.c: New test case. * gcc.dg/spellcheck-options-5.c: New test case. * gcc.dg/spellcheck-options-6.c: New test case. * gcc.dg/spellcheck-options-7.c: New test case. * gcc.dg/spellcheck-options-8.c: New test case. * gcc.dg/spellcheck-options-9.c: New test case. * gcc.dg/spellcheck-options-10.c: New test case. From-SVN: r233382
2016-02-12 18:39:27 +01:00
FOR_EACH_VEC_ELT (*candidates, i, candidate)
{
gcc_assert (candidate);
bm.consider (candidate);
PR driver/69265 and 69453: improved suggestions for various misspelled options gcc/ChangeLog: PR driver/69265 PR driver/69453 * gcc.c (driver::driver): Initialize m_option_suggestions. (driver::~driver): Clean up m_option_suggestions. (suggest_option): Convert to... (driver::suggest_option): ...this, and split out into driver::build_option_suggestions and find_closest_string. (driver::build_option_suggestions): New function, from first half of suggest_option. Special-case OPT_fsanitize_ and OPT_fsanitize_recover_, making use of the sanitizer_opts array. For options of enum types, add the various enum values to the candidate strings. (driver::handle_unrecognized_options): Remove "const". * gcc.h (driver::handle_unrecognized_options): Likewise. (driver::build_option_suggestions): New decl. (driver::suggest_option): New decl. (driver::m_option_suggestions): New field. * opts-common.c (add_misspelling_candidates): New function. * opts.c (sanitizer_opts): Remove decl of struct sanitizer_opts_s and make non-static. * opts.h (sanitizer_opts): New array decl. (add_misspelling_candidates): New function decl. * spellcheck.c (find_closest_string): New function. * spellcheck.h (find_closest_string): New function decl. gcc/testsuite/ChangeLog: PR driver/69265 PR driver/69453 * gcc.dg/spellcheck-options-3.c: New test case. * gcc.dg/spellcheck-options-4.c: New test case. * gcc.dg/spellcheck-options-5.c: New test case. * gcc.dg/spellcheck-options-6.c: New test case. * gcc.dg/spellcheck-options-7.c: New test case. * gcc.dg/spellcheck-options-8.c: New test case. * gcc.dg/spellcheck-options-9.c: New test case. * gcc.dg/spellcheck-options-10.c: New test case. From-SVN: r233382
2016-02-12 18:39:27 +01:00
}
return bm.get_best_meaningful_candidate ();
PR driver/69265 and 69453: improved suggestions for various misspelled options gcc/ChangeLog: PR driver/69265 PR driver/69453 * gcc.c (driver::driver): Initialize m_option_suggestions. (driver::~driver): Clean up m_option_suggestions. (suggest_option): Convert to... (driver::suggest_option): ...this, and split out into driver::build_option_suggestions and find_closest_string. (driver::build_option_suggestions): New function, from first half of suggest_option. Special-case OPT_fsanitize_ and OPT_fsanitize_recover_, making use of the sanitizer_opts array. For options of enum types, add the various enum values to the candidate strings. (driver::handle_unrecognized_options): Remove "const". * gcc.h (driver::handle_unrecognized_options): Likewise. (driver::build_option_suggestions): New decl. (driver::suggest_option): New decl. (driver::m_option_suggestions): New field. * opts-common.c (add_misspelling_candidates): New function. * opts.c (sanitizer_opts): Remove decl of struct sanitizer_opts_s and make non-static. * opts.h (sanitizer_opts): New array decl. (add_misspelling_candidates): New function decl. * spellcheck.c (find_closest_string): New function. * spellcheck.h (find_closest_string): New function decl. gcc/testsuite/ChangeLog: PR driver/69265 PR driver/69453 * gcc.dg/spellcheck-options-3.c: New test case. * gcc.dg/spellcheck-options-4.c: New test case. * gcc.dg/spellcheck-options-5.c: New test case. * gcc.dg/spellcheck-options-6.c: New test case. * gcc.dg/spellcheck-options-7.c: New test case. * gcc.dg/spellcheck-options-8.c: New test case. * gcc.dg/spellcheck-options-9.c: New test case. * gcc.dg/spellcheck-options-10.c: New test case. From-SVN: r233382
2016-02-12 18:39:27 +01:00
}
Selftest framework gcc/ChangeLog: * Makefile.in (OBJS): Add function-tests.o, hash-map-tests.o, hash-set-tests.o, rtl-tests.o, selftest-run-tests.o. (OBJS-libcommon): Add selftest.o. (OBJS-libcommon-target): Add selftest.o. (all.internal): Add "selftest". (all.cross): Likewise. (selftest): New phony target. (s-selftest): New target. (selftest-gdb): New phony target. (COLLECT2_OBJS): Add selftest.o. * bitmap.c: Include "selftest.h". (selftest::test_gc_alloc): New function. (selftest::test_set_range): New function. (selftest::test_clear_bit_in_middle): New function. (selftest::test_copying): New function. (selftest::test_bitmap_single_bit_set_p): New function. (selftest::bitmap_c_tests): New function. * common.opt (fself-test): New. * diagnostic-show-locus.c: Include "selftest.h". (make_range): New function. (test_range_contains_point_for_single_point): New function. (test_range_contains_point_for_single_line): New function. (test_range_contains_point_for_multiple_lines): New function. (assert_eq): New function. (test_get_line_width_without_trailing_whitespace): New function. (selftest::diagnostic_show_locus_c_tests): New function. * et-forest.c: Include "selftest.h". (selftest::test_single_node): New function. (selftest::test_simple_tree): New function. (selftest::test_disconnected_nodes): New function. (selftest::et_forest_c_tests): New function. * fold-const.c: Include "selftest.h". (selftest::assert_binop_folds_to_const): New function. (selftest::assert_binop_folds_to_nonlvalue): New function. (selftest::test_arithmetic_folding): New function. (selftest::fold_const_c_tests): New function. * function-tests.c: New file. * gimple.c: Include "selftest.h". Include "gimple-pretty-print.h". (selftest::verify_gimple_pp): New function. (selftest::test_assign_single): New function. (selftest::test_assign_binop): New function. (selftest::test_nop_stmt): New function. (selftest::test_return_stmt): New function. (selftest::test_return_without_value): New function. (selftest::gimple_c_tests): New function. * hash-map-tests.c: New file. * hash-set-tests.c: New file. * input.c: Include "selftest.h". (selftest::assert_loceq): New function. (selftest::test_accessing_ordinary_linemaps): New function. (selftest::test_unknown_location): New function. (selftest::test_builtins): New function. (selftest::test_reading_source_line): New function. (selftest::input_c_tests): New function. * rtl-tests.c: New file. * selftest-run-tests.c: New file. * selftest.c: New file. * selftest.h: New file. * spellcheck.c: Include "selftest.h". (selftest::levenshtein_distance_unit_test_oneway): New function, adapted from testsuite/gcc.dg/plugin/levenshtein_plugin.c. (selftest::levenshtein_distance_unit_test): Likewise. (selftest::spellcheck_c_tests): Likewise. * toplev.c: Include selftest.h. (toplev::run_self_tests): New. (toplev::main): Handle -fself-test. * toplev.h (toplev::run_self_tests): New. * tree.c: Include "selftest.h". (selftest::test_integer_constants): New function. (selftest::test_identifiers): New function. (selftest::test_labels): New function. (selftest::tree_c_tests): New function. * tree-cfg.c: Include "selftest.h". (selftest::push_fndecl): New function. (selftest::test_linear_chain): New function. (selftest::test_diamond): New function. (selftest::test_fully_connected): New function. (selftest::tree_cfg_c_tests): New function. * vec.c: Include "selftest.h". (selftest::safe_push_range): New function. (selftest::test_quick_push): New function. (selftest::test_safe_push): New function. (selftest::test_truncate): New function. (selftest::test_safe_grow_cleared): New function. (selftest::test_pop): New function. (selftest::test_safe_insert): New function. (selftest::test_ordered_remove): New function. (selftest::test_unordered_remove): New function. (selftest::test_block_remove): New function. (selftest::reverse_cmp): New function. (selftest::test_qsort): New function. (selftest::vec_c_tests): New function.c. * wide-int.cc: Include selftest.h and wide-int-print.h. (selftest::from_int <wide_int>): New function. (selftest::from_int <offset_int>): New function. (selftest::from_int <widest_int>): New function. (selftest::assert_deceq): New function. (selftest::assert_hexeq): New function. (selftest::test_printing <VALUE_TYPE>): New function template. (selftest::test_ops <VALUE_TYPE>): New function template. (selftest::test_comparisons <VALUE_TYPE>): New function template. (selftest::run_all_wide_int_tests <VALUE_TYPE>): New function template. (selftest::wide_int_cc_tests): New function. gcc/testsuite/ChangeLog: * gcc.dg/plugin/levenshtein-test-1.c: Delete. * gcc.dg/plugin/levenshtein_plugin.c: Delete. * gcc.dg/plugin/plugin.exp (plugin_test_list): Remove the above. From-SVN: r237144
2016-06-06 19:11:30 +02:00
#if CHECKING_P
namespace selftest {
/* Selftests. */
/* Verify that the levenshtein_distance (A, B) equals the expected
value. */
static void
levenshtein_distance_unit_test_oneway (const char *a, const char *b,
edit_distance_t expected)
{
edit_distance_t actual = levenshtein_distance (a, b);
ASSERT_EQ (actual, expected);
}
/* Verify that both
levenshtein_distance (A, B)
and
levenshtein_distance (B, A)
equal the expected value, to ensure that the function is symmetric. */
static void
levenshtein_distance_unit_test (const char *a, const char *b,
edit_distance_t expected)
{
levenshtein_distance_unit_test_oneway (a, b, expected);
levenshtein_distance_unit_test_oneway (b, a, expected);
}
/* Verify that find_closest_string is sane. */
static void
test_find_closest_string ()
{
auto_vec<const char *> candidates;
/* Verify that it can handle an empty vec. */
ASSERT_EQ (NULL, find_closest_string ("", &candidates));
/* Verify that it works sanely for non-empty vecs. */
candidates.safe_push ("apple");
candidates.safe_push ("banana");
candidates.safe_push ("cherry");
ASSERT_STREQ ("apple", find_closest_string ("app", &candidates));
ASSERT_STREQ ("banana", find_closest_string ("banyan", &candidates));
ASSERT_STREQ ("cherry", find_closest_string ("berry", &candidates));
ASSERT_EQ (NULL, find_closest_string ("not like the others", &candidates));
/* The order of the vec can matter, but it should not matter for these
inputs. */
candidates.truncate (0);
candidates.safe_push ("cherry");
candidates.safe_push ("banana");
candidates.safe_push ("apple");
ASSERT_STREQ ("apple", find_closest_string ("app", &candidates));
ASSERT_STREQ ("banana", find_closest_string ("banyan", &candidates));
ASSERT_STREQ ("cherry", find_closest_string ("berry", &candidates));
ASSERT_EQ (NULL, find_closest_string ("not like the others", &candidates));
/* If the goal string somehow makes it into the candidate list, offering
it as a suggestion will be nonsensical. Verify that we don't offer such
suggestions. */
ASSERT_EQ (NULL, find_closest_string ("banana", &candidates));
}
/* Test data for test_metric_conditions. */
static const char * const test_data[] = {
"",
"foo",
"food",
"boo",
"1234567890123456789012345678901234567890123456789012345678901234567890"
};
/* Verify that levenshtein_distance appears to be a sane distance function,
i.e. the conditions for being a metric. This is done directly for a
small set of examples, using test_data above. This is O(N^3) in the size
of the array, due to the test for the triangle inequality, so we keep the
array small. */
static void
test_metric_conditions ()
{
const int num_test_cases = sizeof (test_data) / sizeof (test_data[0]);
for (int i = 0; i < num_test_cases; i++)
{
for (int j = 0; j < num_test_cases; j++)
{
edit_distance_t dist_ij
= levenshtein_distance (test_data[i], test_data[j]);
/* Identity of indiscernibles: d(i, j) > 0 iff i == j. */
if (i == j)
ASSERT_EQ (dist_ij, 0);
else
ASSERT_TRUE (dist_ij > 0);
/* Symmetry: d(i, j) == d(j, i). */
edit_distance_t dist_ji
= levenshtein_distance (test_data[j], test_data[i]);
ASSERT_EQ (dist_ij, dist_ji);
/* Triangle inequality. */
for (int k = 0; k < num_test_cases; k++)
{
edit_distance_t dist_ik
= levenshtein_distance (test_data[i], test_data[k]);
edit_distance_t dist_jk
= levenshtein_distance (test_data[j], test_data[k]);
ASSERT_TRUE (dist_ik <= dist_ij + dist_jk);
}
}
}
}
Selftest framework gcc/ChangeLog: * Makefile.in (OBJS): Add function-tests.o, hash-map-tests.o, hash-set-tests.o, rtl-tests.o, selftest-run-tests.o. (OBJS-libcommon): Add selftest.o. (OBJS-libcommon-target): Add selftest.o. (all.internal): Add "selftest". (all.cross): Likewise. (selftest): New phony target. (s-selftest): New target. (selftest-gdb): New phony target. (COLLECT2_OBJS): Add selftest.o. * bitmap.c: Include "selftest.h". (selftest::test_gc_alloc): New function. (selftest::test_set_range): New function. (selftest::test_clear_bit_in_middle): New function. (selftest::test_copying): New function. (selftest::test_bitmap_single_bit_set_p): New function. (selftest::bitmap_c_tests): New function. * common.opt (fself-test): New. * diagnostic-show-locus.c: Include "selftest.h". (make_range): New function. (test_range_contains_point_for_single_point): New function. (test_range_contains_point_for_single_line): New function. (test_range_contains_point_for_multiple_lines): New function. (assert_eq): New function. (test_get_line_width_without_trailing_whitespace): New function. (selftest::diagnostic_show_locus_c_tests): New function. * et-forest.c: Include "selftest.h". (selftest::test_single_node): New function. (selftest::test_simple_tree): New function. (selftest::test_disconnected_nodes): New function. (selftest::et_forest_c_tests): New function. * fold-const.c: Include "selftest.h". (selftest::assert_binop_folds_to_const): New function. (selftest::assert_binop_folds_to_nonlvalue): New function. (selftest::test_arithmetic_folding): New function. (selftest::fold_const_c_tests): New function. * function-tests.c: New file. * gimple.c: Include "selftest.h". Include "gimple-pretty-print.h". (selftest::verify_gimple_pp): New function. (selftest::test_assign_single): New function. (selftest::test_assign_binop): New function. (selftest::test_nop_stmt): New function. (selftest::test_return_stmt): New function. (selftest::test_return_without_value): New function. (selftest::gimple_c_tests): New function. * hash-map-tests.c: New file. * hash-set-tests.c: New file. * input.c: Include "selftest.h". (selftest::assert_loceq): New function. (selftest::test_accessing_ordinary_linemaps): New function. (selftest::test_unknown_location): New function. (selftest::test_builtins): New function. (selftest::test_reading_source_line): New function. (selftest::input_c_tests): New function. * rtl-tests.c: New file. * selftest-run-tests.c: New file. * selftest.c: New file. * selftest.h: New file. * spellcheck.c: Include "selftest.h". (selftest::levenshtein_distance_unit_test_oneway): New function, adapted from testsuite/gcc.dg/plugin/levenshtein_plugin.c. (selftest::levenshtein_distance_unit_test): Likewise. (selftest::spellcheck_c_tests): Likewise. * toplev.c: Include selftest.h. (toplev::run_self_tests): New. (toplev::main): Handle -fself-test. * toplev.h (toplev::run_self_tests): New. * tree.c: Include "selftest.h". (selftest::test_integer_constants): New function. (selftest::test_identifiers): New function. (selftest::test_labels): New function. (selftest::tree_c_tests): New function. * tree-cfg.c: Include "selftest.h". (selftest::push_fndecl): New function. (selftest::test_linear_chain): New function. (selftest::test_diamond): New function. (selftest::test_fully_connected): New function. (selftest::tree_cfg_c_tests): New function. * vec.c: Include "selftest.h". (selftest::safe_push_range): New function. (selftest::test_quick_push): New function. (selftest::test_safe_push): New function. (selftest::test_truncate): New function. (selftest::test_safe_grow_cleared): New function. (selftest::test_pop): New function. (selftest::test_safe_insert): New function. (selftest::test_ordered_remove): New function. (selftest::test_unordered_remove): New function. (selftest::test_block_remove): New function. (selftest::reverse_cmp): New function. (selftest::test_qsort): New function. (selftest::vec_c_tests): New function.c. * wide-int.cc: Include selftest.h and wide-int-print.h. (selftest::from_int <wide_int>): New function. (selftest::from_int <offset_int>): New function. (selftest::from_int <widest_int>): New function. (selftest::assert_deceq): New function. (selftest::assert_hexeq): New function. (selftest::test_printing <VALUE_TYPE>): New function template. (selftest::test_ops <VALUE_TYPE>): New function template. (selftest::test_comparisons <VALUE_TYPE>): New function template. (selftest::run_all_wide_int_tests <VALUE_TYPE>): New function template. (selftest::wide_int_cc_tests): New function. gcc/testsuite/ChangeLog: * gcc.dg/plugin/levenshtein-test-1.c: Delete. * gcc.dg/plugin/levenshtein_plugin.c: Delete. * gcc.dg/plugin/plugin.exp (plugin_test_list): Remove the above. From-SVN: r237144
2016-06-06 19:11:30 +02:00
/* Verify levenshtein_distance for a variety of pairs of pre-canned
inputs, comparing against known-good values. */
void
spellcheck_c_tests ()
{
levenshtein_distance_unit_test ("", "nonempty", strlen ("nonempty"));
levenshtein_distance_unit_test ("saturday", "sunday", 3);
levenshtein_distance_unit_test ("foo", "m_foo", 2);
levenshtein_distance_unit_test ("hello_world", "HelloWorld", 3);
levenshtein_distance_unit_test
("the quick brown fox jumps over the lazy dog", "dog", 40);
levenshtein_distance_unit_test
("the quick brown fox jumps over the lazy dog",
"the quick brown dog jumps over the lazy fox",
4);
levenshtein_distance_unit_test
("Lorem ipsum dolor sit amet, consectetur adipiscing elit,",
"All your base are belong to us",
44);
levenshtein_distance_unit_test ("foo", "FOO", 3);
test_find_closest_string ();
test_metric_conditions ();
Selftest framework gcc/ChangeLog: * Makefile.in (OBJS): Add function-tests.o, hash-map-tests.o, hash-set-tests.o, rtl-tests.o, selftest-run-tests.o. (OBJS-libcommon): Add selftest.o. (OBJS-libcommon-target): Add selftest.o. (all.internal): Add "selftest". (all.cross): Likewise. (selftest): New phony target. (s-selftest): New target. (selftest-gdb): New phony target. (COLLECT2_OBJS): Add selftest.o. * bitmap.c: Include "selftest.h". (selftest::test_gc_alloc): New function. (selftest::test_set_range): New function. (selftest::test_clear_bit_in_middle): New function. (selftest::test_copying): New function. (selftest::test_bitmap_single_bit_set_p): New function. (selftest::bitmap_c_tests): New function. * common.opt (fself-test): New. * diagnostic-show-locus.c: Include "selftest.h". (make_range): New function. (test_range_contains_point_for_single_point): New function. (test_range_contains_point_for_single_line): New function. (test_range_contains_point_for_multiple_lines): New function. (assert_eq): New function. (test_get_line_width_without_trailing_whitespace): New function. (selftest::diagnostic_show_locus_c_tests): New function. * et-forest.c: Include "selftest.h". (selftest::test_single_node): New function. (selftest::test_simple_tree): New function. (selftest::test_disconnected_nodes): New function. (selftest::et_forest_c_tests): New function. * fold-const.c: Include "selftest.h". (selftest::assert_binop_folds_to_const): New function. (selftest::assert_binop_folds_to_nonlvalue): New function. (selftest::test_arithmetic_folding): New function. (selftest::fold_const_c_tests): New function. * function-tests.c: New file. * gimple.c: Include "selftest.h". Include "gimple-pretty-print.h". (selftest::verify_gimple_pp): New function. (selftest::test_assign_single): New function. (selftest::test_assign_binop): New function. (selftest::test_nop_stmt): New function. (selftest::test_return_stmt): New function. (selftest::test_return_without_value): New function. (selftest::gimple_c_tests): New function. * hash-map-tests.c: New file. * hash-set-tests.c: New file. * input.c: Include "selftest.h". (selftest::assert_loceq): New function. (selftest::test_accessing_ordinary_linemaps): New function. (selftest::test_unknown_location): New function. (selftest::test_builtins): New function. (selftest::test_reading_source_line): New function. (selftest::input_c_tests): New function. * rtl-tests.c: New file. * selftest-run-tests.c: New file. * selftest.c: New file. * selftest.h: New file. * spellcheck.c: Include "selftest.h". (selftest::levenshtein_distance_unit_test_oneway): New function, adapted from testsuite/gcc.dg/plugin/levenshtein_plugin.c. (selftest::levenshtein_distance_unit_test): Likewise. (selftest::spellcheck_c_tests): Likewise. * toplev.c: Include selftest.h. (toplev::run_self_tests): New. (toplev::main): Handle -fself-test. * toplev.h (toplev::run_self_tests): New. * tree.c: Include "selftest.h". (selftest::test_integer_constants): New function. (selftest::test_identifiers): New function. (selftest::test_labels): New function. (selftest::tree_c_tests): New function. * tree-cfg.c: Include "selftest.h". (selftest::push_fndecl): New function. (selftest::test_linear_chain): New function. (selftest::test_diamond): New function. (selftest::test_fully_connected): New function. (selftest::tree_cfg_c_tests): New function. * vec.c: Include "selftest.h". (selftest::safe_push_range): New function. (selftest::test_quick_push): New function. (selftest::test_safe_push): New function. (selftest::test_truncate): New function. (selftest::test_safe_grow_cleared): New function. (selftest::test_pop): New function. (selftest::test_safe_insert): New function. (selftest::test_ordered_remove): New function. (selftest::test_unordered_remove): New function. (selftest::test_block_remove): New function. (selftest::reverse_cmp): New function. (selftest::test_qsort): New function. (selftest::vec_c_tests): New function.c. * wide-int.cc: Include selftest.h and wide-int-print.h. (selftest::from_int <wide_int>): New function. (selftest::from_int <offset_int>): New function. (selftest::from_int <widest_int>): New function. (selftest::assert_deceq): New function. (selftest::assert_hexeq): New function. (selftest::test_printing <VALUE_TYPE>): New function template. (selftest::test_ops <VALUE_TYPE>): New function template. (selftest::test_comparisons <VALUE_TYPE>): New function template. (selftest::run_all_wide_int_tests <VALUE_TYPE>): New function template. (selftest::wide_int_cc_tests): New function. gcc/testsuite/ChangeLog: * gcc.dg/plugin/levenshtein-test-1.c: Delete. * gcc.dg/plugin/levenshtein_plugin.c: Delete. * gcc.dg/plugin/plugin.exp (plugin_test_list): Remove the above. From-SVN: r237144
2016-06-06 19:11:30 +02:00
}
} // namespace selftest
#endif /* #if CHECKING_P */