Skip to content

Commit 6dfb41e

Browse files
fanquakePastaPastaPasta
authored andcommitted
Merge bitcoin#30235: build: warn on self-assignment
15796d4 build: warn on self-assignment (Cory Fields) 53372f2 refactor: disable self-assign warning for tests (Cory Fields) Pull request description: Belt-and suspenders after bitcoin#30234. Self-assignment should be safe _and_ discouraged. We used to opt out of this warning because something deep in our serialization/byteswapping code could self-assign, but that doesn't appear to be the case anymore. ACKs for top commit: maflcko: ACK 15796d4 fanquake: ACK 15796d4 - not a huge fan of inline pragma usage, but this seems fine, given it's to work around an already-fixed compiler bug, and we'll only be carrying it for a shortish time in any case. Tree-SHA512: 1f95f7c730b974ad1da55ebd381040bac312f2f380fff9d569ebab91d7c1963592a84d1613d81d96238c6f5a66aa40deebba68a76f6b24b02150d0a77c769654
1 parent 325c8f0 commit 6dfb41e

File tree

3 files changed

+32
-1
lines changed

3 files changed

+32
-1
lines changed

configure.ac

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -513,12 +513,12 @@ AX_CHECK_COMPILE_FLAG([-Wsuggest-override], [WARN_CXXFLAGS="$WARN_CXXFLAGS -Wsug
513513
AX_CHECK_COMPILE_FLAG([-Wimplicit-fallthrough], [WARN_CXXFLAGS="$WARN_CXXFLAGS -Wimplicit-fallthrough"], [], [$CXXFLAG_WERROR])
514514
AX_CHECK_COMPILE_FLAG([-Wunreachable-code], [WARN_CXXFLAGS="$WARN_CXXFLAGS -Wunreachable-code"], [], [$CXXFLAG_WERROR])
515515
AX_CHECK_COMPILE_FLAG([-Wdocumentation], [WARN_CXXFLAGS="$WARN_CXXFLAGS -Wdocumentation"], [], [$CXXFLAG_WERROR])
516+
AX_CHECK_COMPILE_FLAG([-Wself-assign], [WARN_CXXFLAGS="$WARN_CXXFLAGS -Wself-assign"], [], [$CXXFLAG_WERROR])
516517

517518
dnl Some compilers (gcc) ignore unknown -Wno-* options, but warn about all
518519
dnl unknown options if any other warning is produced. Test the -Wfoo case, and
519520
dnl set the -Wno-foo case if it works.
520521
AX_CHECK_COMPILE_FLAG([-Wunused-parameter], [NOWARN_CXXFLAGS="$NOWARN_CXXFLAGS -Wno-unused-parameter"], [], [$CXXFLAG_WERROR])
521-
AX_CHECK_COMPILE_FLAG([-Wself-assign], [NOWARN_CXXFLAGS="$NOWARN_CXXFLAGS -Wno-self-assign"], [], [$CXXFLAG_WERROR])
522522

523523
dnl Don't allow extended (non-ASCII) symbols in identifiers. This is easier for code review.
524524
AX_CHECK_COMPILE_FLAG([-fno-extended-identifiers], [CORE_CXXFLAGS="$CORE_CXXFLAGS -fno-extended-identifiers"], [], [$CXXFLAG_WERROR])

src/test/fuzz/muhash.cpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,19 @@ FUZZ_TARGET(muhash)
4343
},
4444
[&] {
4545
// Test that dividing a MuHash by itself brings it back to it's initial state
46+
47+
// See note about clang + self-assignment in test/uint256_tests.cpp
48+
#if defined(__clang__)
49+
# pragma clang diagnostic push
50+
# pragma clang diagnostic ignored "-Wself-assign-overloaded"
51+
#endif
52+
4653
muhash /= muhash;
54+
55+
#if defined(__clang__)
56+
# pragma clang diagnostic pop
57+
#endif
58+
4759
muhash.Finalize(out);
4860
out2 = uint256S(initial_state_hash);
4961
},

src/test/uint256_tests.cpp

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -268,6 +268,22 @@ BOOST_AUTO_TEST_CASE( conversion )
268268

269269
BOOST_AUTO_TEST_CASE( operator_with_self )
270270
{
271+
272+
/* Clang 16 and earlier detects v -= v and v /= v as self-assignments
273+
to 0 and 1 respectively.
274+
See: https://github.com/llvm/llvm-project/issues/42469
275+
and the fix in commit c5302325b2a62d77cf13dd16cd5c19141862fed0 .
276+
277+
This makes some sense for arithmetic classes, but could be considered a bug
278+
elsewhere. Disable the warning here so that the code can be tested, but the
279+
warning should remain on as there will likely always be a better way to
280+
express this.
281+
*/
282+
283+
#if defined(__clang__)
284+
# pragma clang diagnostic push
285+
# pragma clang diagnostic ignored "-Wself-assign-overloaded"
286+
#endif
271287
arith_uint256 v = UintToArith256(uint256S("02"));
272288
v *= v;
273289
BOOST_CHECK(v == UintToArith256(uint256S("04")));
@@ -277,6 +293,9 @@ BOOST_AUTO_TEST_CASE( operator_with_self )
277293
BOOST_CHECK(v == UintToArith256(uint256S("02")));
278294
v -= v;
279295
BOOST_CHECK(v == UintToArith256(uint256S("0")));
296+
#if defined(__clang__)
297+
# pragma clang diagnostic pop
298+
#endif
280299
}
281300

282301
BOOST_AUTO_TEST_CASE( check_ONE )

0 commit comments

Comments
 (0)