From 91ed1621cc082833ada087300b1fb169332cb6c5 Mon Sep 17 00:00:00 2001 From: Bruno Haible Date: Sat, 8 Jul 2023 12:30:47 +0200 Subject: [PATCH] Avoid "gcc -Wunused-parameter" warnings on the generated hash function. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Reported by Vinícius dos Santos Oliveira in . * src/output.cc (Output::output_hash_function): If str is not used, emit a cast from str to void. If len is not used, emit a cast from len to void. * tests/permut2.exp: Update. * tests/permut3.exp: Likewise. * tests/permutc2.exp: Likewise. --- ChangeLog | 12 ++++++++++++ src/output.cc | 29 ++++++++++++++++++++++------- tests/permut2.exp | 1 + tests/permut3.exp | 1 + tests/permutc2.exp | 1 + 5 files changed, 37 insertions(+), 7 deletions(-) diff --git a/ChangeLog b/ChangeLog index 423576a..7e6bbe1 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,15 @@ +2023-07-08 Bruno Haible + + Avoid "gcc -Wunused-parameter" warnings on the generated hash function. + Reported by Vinícius dos Santos Oliveira in + . + * src/output.cc (Output::output_hash_function): If str is not used, emit + a cast from str to void. If len is not used, emit a cast from len to + void. + * tests/permut2.exp: Update. + * tests/permut3.exp: Likewise. + * tests/permutc2.exp: Likewise. + 2023-07-01 Bruno Haible doc: Tweaks. diff --git a/src/output.cc b/src/output.cc index 1e289b3..63cbdac 100644 --- a/src/output.cc +++ b/src/output.cc @@ -1,5 +1,5 @@ /* Output routines. - Copyright (C) 1989-1998, 2000, 2002-2004, 2006-2007, 2009, 2011-2012, 2016, 2018, 2021 Free Software Foundation, Inc. + Copyright (C) 1989-1998, 2000, 2002-2004, 2006-2007, 2009, 2011-2012, 2016, 2018, 2021, 2023 Free Software Foundation, Inc. Written by Douglas C. Schmidt and Bruno Haible . @@ -815,12 +815,15 @@ Output::output_hash_function () const "#endif\n" "#endif\n"); - if (/* The function does not use the 'str' argument? */ - _key_positions.get_size() == 0 - || /* The function uses 'str', but not the 'len' argument? */ - (!_hash_includes_len - && _key_positions[0] < _min_key_len - && _key_positions[_key_positions.get_size() - 1] != Positions::LASTCHAR)) + /* Does the function use the 'str' argument? */ + bool uses_str = (_key_positions.get_size() > 0); + /* Does the function use the 'len' argument? */ + bool uses_len = + (_hash_includes_len + || (_key_positions.get_size() > 0 + && (_key_positions[0] >= _min_key_len + || _key_positions[_key_positions.get_size() - 1] == Positions::LASTCHAR))); + if (!uses_str || !uses_len) /* Pacify lint. */ printf ("/*ARGSUSED*/\n"); @@ -830,6 +833,9 @@ Output::output_hash_function () const if (option[CPLUSPLUS]) printf ("%s::", option.get_class_name ()); printf ("%s ", option.get_hash_name ()); + /* We better not use [[__maybe_unused__]] or __attribute__ ((__unused__)) + because support for these syntaxes in the compilers is constantly + changing. */ printf (option[KRC] ? "(str, len)\n" " %schar *str;\n" @@ -879,6 +885,15 @@ Output::output_hash_function () const " };\n"); } + if (!uses_str) + /* The function does not use the 'str' argument. + Silence "gcc -Wunused-parameter". */ + printf (" (void) str;\n"); + if (!uses_len) + /* The function does not use the 'len' argument. + Silence "gcc -Wunused-parameter". */ + printf (" (void) len;\n"); + if (_key_positions.get_size() == 0) { /* Trivial case: No key positions at all. */ diff --git a/tests/permut2.exp b/tests/permut2.exp index 217b34d..551cef4 100644 --- a/tests/permut2.exp +++ b/tests/permut2.exp @@ -77,6 +77,7 @@ hash (register const char *str, register size_t len) 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4 }; + (void) len; return asso_values[(unsigned char)str[1]+1] + asso_values[(unsigned char)str[0]]; } diff --git a/tests/permut3.exp b/tests/permut3.exp index 0c04c15..e680f34 100644 --- a/tests/permut3.exp +++ b/tests/permut3.exp @@ -77,6 +77,7 @@ hash (register const char *str, register size_t len) 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4 }; + (void) len; return asso_values[(unsigned char)str[1]+1] + asso_values[(unsigned char)str[0]]; } diff --git a/tests/permutc2.exp b/tests/permutc2.exp index 80fbf51..eb2ac35 100644 --- a/tests/permutc2.exp +++ b/tests/permutc2.exp @@ -122,6 +122,7 @@ hash (register const char *str, register size_t len) 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8 }; + (void) len; return asso_values[(unsigned char)str[1]+3] + asso_values[(unsigned char)str[0]]; }