Skip to content

Conversation

jonathanschoeller
Copy link

Closes: #19017

In #19015 it's been suggested that we add some new compiler warnings to our build. Some of these, such as -Wunreachable-code-loop-increment, generate warnings. We'll likely want to fix these up if we're going to turn these warnings on.

init.cpp:969:5: warning: loop will run at most once (loop increment never executed) [-Wunreachable-code-loop-increment]
     for (const auto& arg : gArgs.GetUnsuitableSectionOnlyArgs()) {
     ^~~
 1 warning generated.

bitcoin/src/init.cpp

Lines 968 to 972 in aa8d768

// on the command line or in this network's section of the config file.
std::string network = gArgs.GetChainName();
for (const auto& arg : gArgs.GetUnsuitableSectionOnlyArgs()) {
return InitError(strprintf(_("Config setting for %s only applied on %s network when in [%s] section."), arg, network, network));
}

To fix this, collect all errors, and output them in a single error message after the loop completes. This resolves the unreachable code warning, and avoids popup hell that could result from outputting a seperate message for each error or warning one by one.

@practicalswift
Copy link
Contributor

Concept ACK: thanks for fixing this and thereby allowing us to catch more severe issues using -Wunreachable-code-loop-increment going forward!

Great first time contribution and great rationale section in the PR description.

Warm welcome as a contributor @jonathanschoeller! :)

Copy link
Member

@maflcko maflcko left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice. ACK with or without the nit addressed

@maflcko
Copy link
Member

maflcko commented Jun 1, 2020

Oh, it looks like you might have to adjust the tests for the longer error messages.

@hebasto
Copy link
Member

hebasto commented Jun 1, 2020

Concept ACK.

@jonathanschoeller jonathanschoeller force-pushed the fix-Wunreachable-code-loop-increment branch from 34c4fc9 to b546870 Compare June 1, 2020 10:55
src/init.cpp Outdated
for (const auto& section : gArgs.GetUnrecognizedSections()) {
InitWarning(strprintf(Untranslated("%s:%i ") + _("Section [%s] is not recognized."), section.m_file, section.m_line, section.m_name));
warnings += strprintf(Untranslated("%s:%i ") + _("Section [%s] is not recognized.\n"), section.m_file, section.m_line, section.m_name);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please don't change the translation string _("Section [%s] is not recognized.") and keep the special character \n untranslated (like above)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Slow typing from my side :)

Copy link
Member

@hebasto hebasto left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ACK b546870, tested on Linux Mint 19.3 (x86_64).

I suggest to add a commit to modify configure.ac as well:

--- a/configure.ac
+++ b/configure.ac
@@ -391,6 +391,7 @@ if test "x$enable_werror" = "xyes"; then
   dnl https://gcc.gnu.org/bugzilla/show_bug.cgi?id=78010
   AX_CHECK_COMPILE_FLAG([-Werror=suggest-override],[ERROR_CXXFLAGS="$ERROR_CXXFLAGS -Werror=suggest-override"],,[[$CXXFLAG_WERROR]],
                         [AC_LANG_SOURCE([[struct A { virtual void f(); }; struct B : A { void f() final; };]])])
+  AX_CHECK_COMPILE_FLAG([-Werror=unreachable-code-loop-increment],[ERROR_CXXFLAGS="$ERROR_CXXFLAGS -Werror=unreachable-code-loop-increment"],,[[$CXXFLAG_WERROR]])
 fi
 
 if test "x$CXXFLAGS_overridden" = "xno"; then
@@ -410,6 +411,7 @@ if test "x$CXXFLAGS_overridden" = "xno"; then
   AX_CHECK_COMPILE_FLAG([-Wsign-compare],[WARN_CXXFLAGS="$WARN_CXXFLAGS -Wsign-compare"],,[[$CXXFLAG_WERROR]])
   AX_CHECK_COMPILE_FLAG([-Wsuggest-override],[WARN_CXXFLAGS="$WARN_CXXFLAGS -Wsuggest-override"],,[[$CXXFLAG_WERROR]],
                         [AC_LANG_SOURCE([[struct A { virtual void f(); }; struct B : A { void f() final; };]])])
+  AX_CHECK_COMPILE_FLAG([-Wunreachable-code-loop-increment],[WARN_CXXFLAGS="$WARN_CXXFLAGS -Wunreachable-code-loop-increment"],,[[$CXXFLAG_WERROR]])
 
   dnl Some compilers (gcc) ignore unknown -Wno-* options, but warn about all
   dnl unknown options if any other warning is produced. Test the -Wfoo case, and

src/init.cpp Outdated
for (const auto& section : gArgs.GetUnrecognizedSections()) {
InitWarning(strprintf(Untranslated("%s:%i ") + _("Section [%s] is not recognized."), section.m_file, section.m_line, section.m_name));
warnings += strprintf(Untranslated("%s:%i ") + _("Section [%s] is not recognized.\n"), section.m_file, section.m_line, section.m_name);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
warnings += strprintf(Untranslated("%s:%i ") + _("Section [%s] is not recognized.\n"), section.m_file, section.m_line, section.m_name);
warnings += strprintf(Untranslated("%s:%i ") + _("Section [%s] is not recognized.") + Untranslated("\n"), section.m_file, section.m_line, section.m_name);

Building with -Wunreachable-code-loop-increment causes a warning
due to always returning on the first iteration of the loop that
outputs errors on invalid args.

Collect all errors, and output them in a single error message
after the loop completes, resolving the warning and avoiding
popup hell by outputting a seperate message for each error.
Enable unreachable-code-loop-increment and treat as error.
refs: bitcoin#19015
@jonathanschoeller jonathanschoeller force-pushed the fix-Wunreachable-code-loop-increment branch from b546870 to eea8114 Compare June 1, 2020 20:36
@jonathanschoeller
Copy link
Author

Thanks @practicalswift , @MarcoFalke , and @hebasto for welcoming me and taking the time to help me with this PR.

@DrahtBot
Copy link
Contributor

DrahtBot commented Jun 2, 2020

The following sections might be updated with supplementary metadata relevant to reviewers and maintainers.

Conflicts

Reviewers, this pull request conflicts with the following ones:

If you consider this pull request important, please also help to review the conflicting pull requests. Ideally, start with the one that should be merged first.

Copy link
Member

@hebasto hebasto left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

re-ACK eea8114, only suggested changes applied since the previous review.

@laanwj
Copy link
Member

laanwj commented Jun 4, 2020

Code review ACK eea8114

@laanwj laanwj merged commit b46fb5c into bitcoin:master Jun 4, 2020
@jonathanschoeller jonathanschoeller deleted the fix-Wunreachable-code-loop-increment branch June 4, 2020 21:06
sidhujag pushed a commit to syscoin/syscoin that referenced this pull request Jun 4, 2020
eea8114 build: Enable unreachable-code-loop-increment (Jonathan Schoeller)
d15db4b refactor: Fix unreachable code in init arg checks (Jonathan Schoeller)

Pull request description:

  Closes: bitcoin#19017

  In bitcoin#19015 it's been suggested that we add some new compiler warnings to our build. Some of these, such as `-Wunreachable-code-loop-increment`, generate warnings. We'll likely want to fix these up if we're going to turn these warnings on.

  ```shell
  init.cpp:969:5: warning: loop will run at most once (loop increment never executed) [-Wunreachable-code-loop-increment]
       for (const auto& arg : gArgs.GetUnsuitableSectionOnlyArgs()) {
       ^~~
   1 warning generated.
   ```
   https://github.com/bitcoin/bitcoin/blob/aa8d76806c74a51ec66e5004394fe9ea8ff0fac4/src/init.cpp#L968-L972

  To fix this, collect all errors, and output them in a single error message after the loop completes. This resolves the unreachable code warning, and avoids popup hell that could result from outputting a seperate message for each error or warning one by one.

ACKs for top commit:
  laanwj:
    Code review ACK eea8114
  hebasto:
    re-ACK eea8114, only suggested changes applied since the [previous](bitcoin#19131 (review)) review.

Tree-SHA512: 2aa3ceb7fab581b6ba2580900668388d8eba1c3001c8ff9c11c1f4a9a10fbc37f30e590249862676858446e3f4950140a252953ba1643ba3bfd772f8eae20583
deadalnix pushed a commit to Bitcoin-ABC/bitcoin-abc that referenced this pull request Dec 10, 2020
Summary: Backport of core [[bitcoin/bitcoin#19131 | PR19131]].

Test Plan:
  ninja all check-all

Reviewers: #bitcoin_abc, majcosta

Reviewed By: #bitcoin_abc, majcosta

Differential Revision: https://reviews.bitcoinabc.org/D8648
deadalnix pushed a commit to Bitcoin-ABC/bitcoin-abc that referenced this pull request Dec 11, 2020
Summary:
Follow-up for rABC6c84fca7a26eee3f5f031f02610d4528f7ef8276
which lost the changes for an unknown reason.

Backport of core [[bitcoin/bitcoin#19131 | PR19131]].

Test Plan:
With Werror enabled:
  ninja all check-all

Reviewers: #bitcoin_abc, jasonbcox

Reviewed By: #bitcoin_abc, jasonbcox

Differential Revision: https://reviews.bitcoinabc.org/D8658
@bitcoin bitcoin locked as resolved and limited conversation to collaborators Feb 15, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Fix -Wunreachable-code-loop-increment warning
8 participants