Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

build: Enable -Wshadow #29485

Closed
wants to merge 40 commits into from
Closed

build: Enable -Wshadow #29485

wants to merge 40 commits into from

Conversation

Empact
Copy link
Member

@Empact Empact commented Feb 26, 2024

This is an attempt to demonstrate what is necessary to get -Wshadow building under -Werror on clang, and to see what other existing pull requests that might conflict with.

This was prompted by an example of shadowing I spotted in review. We currently warn against shadowing in the docs but do not detect it, so instead rely on review to identify these issues.

Note:

The question is: have the circumstances changed in the past 9 years? Is a partial fix beneficial?

One option would be to merge the low-risk changes, re. tests, etc. and exclude
sensitive files from this warning to get it applied to regular builds.

Related:

node/chainstate.cpp:234:15: error: declaration shadows a structured binding [-Werror,-Wshadow]
        auto [init_status, init_error] = CompleteChainstateInitialization(chainman, cache_sizes, options);
              ^
node/chainstate.cpp:198:11: note: previous declaration is here
    auto [init_status, init_error] = CompleteChainstateInitialization(chainman, cache_sizes, options);
          ^
node/chainstate.cpp:234:28: error: declaration shadows a structured binding [-Werror,-Wshadow]
        auto [init_status, init_error] = CompleteChainstateInitialization(chainman, cache_sizes, options);
                           ^
node/chainstate.cpp:198:24: note: previous declaration is here
    auto [init_status, init_error] = CompleteChainstateInitialization(chainman, cache_sizes, options);
                       ^
rpc/rawtransaction.cpp:1316:22: error: declaration shadows a local variable [-Werror,-Wshadow]
            UniValue proprietary(UniValue::VARR);
                     ^
rpc/rawtransaction.cpp:1095:14: note: previous declaration is here
    UniValue proprietary(UniValue::VARR);
             ^
rpc/rawtransaction.cpp:1411:22: error: declaration shadows a local variable [-Werror,-Wshadow]
            UniValue proprietary(UniValue::VARR);
                     ^
rpc/rawtransaction.cpp:1095:14: note: previous declaration is here
    UniValue proprietary(UniValue::VARR);
             ^
common/init.cpp:73:31: error: declaration shadows a local variable [-Werror,-Wshadow]
            const std::string error = strprintf(
                              ^
common/init.cpp:37:21: note: previous declaration is here
        std::string error;
                    ^
wallet/rpc/backup.cpp:792:24: error: declaration shadows a local variable [-Werror,-Wshadow]
            const auto it{spk_man.mapKeyMetadata.find(keyid)};
                       ^
wallet/rpc/backup.cpp:784:67: note: previous declaration is here
    for (std::vector<std::pair<int64_t, CKeyID> >::const_iterator it = vKeyBirth.begin(); it != vKeyBirth.end(); it++) {
                                                                  ^
univalue/lib/univalue.cpp:104:35: error: declaration shadows a field of 'UniValue' [-Werror,-Wshadow]
void UniValue::push_back(UniValue val)
                                  ^
./univalue/include/univalue.h:103:17: note: previous declaration is here
    std::string val;                       // numbers are stored as C++ strings
                ^
univalue/lib/univalue.cpp:118:52: error: declaration shadows a field of 'UniValue' [-Werror,-Wshadow]
void UniValue::pushKVEnd(std::string key, UniValue val)
                                                   ^
./univalue/include/univalue.h:103:17: note: previous declaration is here
    std::string val;                       // numbers are stored as C++ strings
                ^
univalue/lib/univalue.cpp:126:49: error: declaration shadows a field of 'UniValue' [-Werror,-Wshadow]
void UniValue::pushKV(std::string key, UniValue val)
                                                ^
./univalue/include/univalue.h:103:17: note: previous declaration is here
    std::string val;                       // numbers are stored as C++ strings
                ^
bitcoind.cpp:122:14: error: declaration shadows a local variable [-Werror,-Wshadow]
    if (auto error = common::InitConfig(args)) {
             ^
bitcoind.cpp:117:17: note: previous declaration is here
    std::string error;
                ^
external_signer.cpp:49:36: error: declaration shadows a local variable [-Werror,-Wshadow]
        for (const ExternalSigner& signer : signers) {
                                   ^
external_signer.cpp:32:26: note: previous declaration is here
    for (const UniValue& signer : result.getValues()) {
                         ^
test/fuzz/bitdeque.cpp:353:33: error: declaration shadows a local variable [-Werror,-Wshadow]
                    const auto& cdeq = deq;
                                ^
test/fuzz/bitdeque.cpp:352:22: note: variable 'cdeq' is captured here
                if (!cdeq.empty()) {
                     ^
test/fuzz/bitdeque.cpp:45:17: note: previous declaration is here
    const auto& cdeq = deq;
                ^
net.cpp:817:20: error: declaration shadows a field of 'V1Transport' [-Werror,-Wshadow]
    CMessageHeader hdr(m_magic_bytes, msg.m_type.c_str(), msg.data.size());
                   ^
./net.h:381:20: note: previous declaration is here
    CMessageHeader hdr GUARDED_BY(m_recv_mutex); // complete header
                   ^
rpc/rawtransaction.cpp:1330:22: error: declaration shadows a local variable [-Werror,-Wshadow]
            UniValue unknowns(UniValue::VOBJ);
                     ^
rpc/rawtransaction.cpp:1107:14: note: previous declaration is here
    UniValue unknowns(UniValue::VOBJ);
             ^
rpc/rawtransaction.cpp:1425:22: error: declaration shadows a local variable [-Werror,-Wshadow]
            UniValue unknowns(UniValue::VOBJ);
                     ^
rpc/rawtransaction.cpp:1107:14: note: previous declaration is here
    UniValue unknowns(UniValue::VOBJ);
             ^
wallet/rpc/wallet.cpp:737:21: error: declaration shadows a local variable [-Werror,-Wshadow]
        for (size_t i = 0; i < mtx.vout.size(); ++i) {
                    ^
wallet/rpc/wallet.cpp:699:17: note: previous declaration is here
    for (size_t i = 0; i < txs.size(); ++i) {
                ^
wallet/transaction.cpp:48:22: error: declaration shadows a local variable [-Werror,-Wshadow]
    } else if (auto* conf = state<TxStateConflicted>()) {
                     ^
wallet/transaction.cpp:46:15: note: previous declaration is here
    if (auto* conf = state<TxStateConfirmed>()) {
              ^
wallet/spend.cpp:1313:14: error: declaration shadows a local variable [-Werror,-Wshadow]
        auto result = wallet.chain().checkChainLimits(tx);
             ^
wallet/spend.cpp:1138:28: note: previous declaration is here
    const SelectionResult& result = *select_coins_res;
                           ^
netbase.cpp:294:28: error: declaration shadows a local variable [-Werror,-Wshadow]
                const auto timeout = std::min(remaining, std::chrono::milliseconds{MAX_WAIT_FOR_IO});
                           ^
netbase.cpp:277:93: note: previous declaration is here
static IntrRecvError InterruptibleRecv(uint8_t* data, size_t len, std::chrono::milliseconds timeout, const Sock& sock)
                                                                                            ^
void CDBBatch::WriteImpl(Span<const std::byte> key, DataStream& ssValue)
                                                                ^
./dbwrapper.h:83:16: note: previous declaration is here
    DataStream ssValue{};
               ^
@Empact Empact marked this pull request as ready for review February 26, 2024 21:48
@DrahtBot
Copy link
Contributor

DrahtBot commented Feb 26, 2024

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

Code Coverage

For detailed information about the code coverage, see the test coverage report.

Reviews

See the guideline for information on the review process.
A summary of reviews will appear here.

Conflicts

Reviewers, this pull request conflicts with the following ones:

  • #29432 (Stratum v2 Template Provider (take 3) by Sjors)
  • #29015 (kernel: Streamline util library by ryanofsky)
  • #28678 (miniscript: convert non-critical asserts to Assumes by sipa)
  • #28031 (Package Relay 1/3: Introduce TxDownloadManager and improve orphan-handling by glozow)
  • #27865 (wallet: Track no-longer-spendable TXOs separately by achow101)
  • #27307 (wallet: track mempool conflicts with wallet transactions by ishaanam)
  • #26022 (Add util::ResultPtr class by ryanofsky)
  • #25972 (build: no-longer disable WARN_CXXFLAGS when CXXFLAGS is set by fanquake)
  • #25722 (refactor: Use util::Result class for wallet loading by ryanofsky)
  • #25665 (refactor: Add util::Result failure values, multiple error and warning messages by ryanofsky)

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.

@Empact Empact marked this pull request as draft February 26, 2024 23:38
@DrahtBot
Copy link
Contributor

🚧 At least one of the CI tasks failed. Make sure to run all tests locally, according to the
documentation.

Possibly this is due to a silent merge conflict (the changes in this pull request being
incompatible with the current code in the target branch). If so, make sure to rebase on the latest
commit of the target branch.

Leave a comment here, if you need help tracking down a confusing failure.

Debug: https://github.com/bitcoin/bitcoin/runs/22004282753

rpc/util.cpp:536:33: error: declaration shadows a local variable [-Werror,-Wshadow]
        for (const std::string& name : names) {
                                ^
rpc/util.cpp:519:36: note: previous declaration is here
RPCHelpMan::RPCHelpMan(std::string name, std::string description, std::vector<RPCArg> args, RPCResults results, RPCExamples examples, RPCMethodImpl fun)
                                   ^
script/descriptor.cpp:1933:42: error: declaration shadows a local variable [-Werror,-Wshadow]
                for (const auto& [depth, script, leaf_ver] : *tree) {
                                         ^
script/descriptor.cpp:1847:60: note: previous declaration is here
std::unique_ptr<DescriptorImpl> InferScript(const CScript& script, ParseScriptContext ctx, const SigningProvider& provider)
                                                           ^
validation.cpp:1616:31: error: declaration shadows a local variable [-Werror,-Wshadow]
        } else if (const auto it{individual_results_nonfinal.find(wtxid)}; it != individual_results_nonfinal.end()) {
                              ^
validation.cpp:1602:31: note: previous declaration is here
        } else if (const auto it{results_final.find(wtxid)}; it != results_final.end()) {
                              ^
net_processing.cpp:2658:35: error: declaration shadows a local variable [-Werror,-Wshadow]
                for (const auto& [peer, stat] : m_headers_presync_stats) {
                                  ^
net_processing.cpp:2605:64: note: previous declaration is here
bool PeerManagerImpl::IsContinuationOfLowWorkHeadersSync(Peer& peer, CNode& pfrom, std::vector<CBlockHeader>& headers)
                                                               ^
net_processing.cpp:3042:28: error: declaration shadows a local variable [-Werror,-Wshadow]
    while (CTransactionRef porphanTx = m_orphanage.GetTxToReconsider(peer.m_id)) {
                           ^
net_processing.cpp:3040:21: note: previous declaration is here
    CTransactionRef porphanTx = nullptr;
                    ^
wallet/wallet.cpp:901:48: error: declaration shadows a typedef in 'wallet::CWallet' [-Werror,-Wshadow]
    typedef std::multimap<int64_t, CWalletTx*> TxItems;
                                               ^
./wallet/wallet.h:483:48: note: previous declaration is here
    typedef std::multimap<int64_t, CWalletTx*> TxItems;
                                               ^
wallet/wallet.cpp:2787:22: error: declaration shadows a local variable [-Werror,-Wshadow]
    } else if (auto* conf = wtx.state<TxStateConflicted>()) {
                     ^
wallet/wallet.cpp:2785:15: note: previous declaration is here
    if (auto* conf = wtx.state<TxStateConfirmed>()) {
              ^
wallet/wallet.cpp:3377:22: error: declaration shadows a local variable [-Werror,-Wshadow]
    } else if (auto* conf = wtx.state<TxStateConflicted>()) {
                     ^
wallet/wallet.cpp:3374:15: note: previous declaration is here
    if (auto* conf = wtx.state<TxStateConfirmed>()) {
              ^
wallet/wallet.cpp:4441:24: error: declaration shadows a local variable [-Werror,-Wshadow]
        DatabaseStatus status;
                       ^
wallet/wallet.cpp:4305:20: note: previous declaration is here
    DatabaseStatus status;
                   ^
wallet/wallet.cpp:4442:36: error: declaration shadows a local variable [-Werror,-Wshadow]
        std::vector<bilingual_str> warnings;
                                   ^
wallet/wallet.cpp:4287:32: note: previous declaration is here
    std::vector<bilingual_str> warnings;
                               ^
./script/miniscript.h:649:64: error: declaration shadows a field of 'Node<Key>' [-Werror,-Wshadow]
            [&upfn](DummyState, const Node& node, Span<Result> subs) {
                                                               ^
./script/miniscript.h:509:39: note: previous declaration is here
    mutable std::vector<NodeRef<Key>> subs;
                                      ^
./script/miniscript.h:663:67: error: declaration shadows a field of 'Node<Key>' [-Werror,-Wshadow]
            [&upfn](State&& state, const Node& node, Span<Result> subs) {
                                                                  ^
./script/miniscript.h:509:39: note: previous declaration is here
    mutable std::vector<NodeRef<Key>> subs;
                                      ^
./script/miniscript.h:678:64: error: declaration shadows a field of 'Node<Key>' [-Werror,-Wshadow]
            [&upfn](DummyState, const Node& node, Span<Result> subs) {
                                                               ^
./script/miniscript.h:509:39: note: previous declaration is here
    mutable std::vector<NodeRef<Key>> subs;
                                      ^
./script/miniscript.h:741:87: error: declaration shadows a field of 'Node<Key>' [-Werror,-Wshadow]
        auto upfn = [&ctx, is_tapscript](bool verify, const Node& node, Span<CScript> subs) -> CScript {
                                                                                      ^
./script/miniscript.h:509:39: note: previous declaration is here
    mutable std::vector<NodeRef<Key>> subs;
                                      ^
./script/miniscript.h:819:92: error: declaration shadows a field of 'Node<Key>' [-Werror,-Wshadow]
        auto upfn = [&ctx, is_tapscript](bool wrapped, const Node& node, Span<std::string> subs) -> std::optional<std::string> {
                                                                                           ^
./script/miniscript.h:509:39: note: previous declaration is here
    mutable std::vector<NodeRef<Key>> subs;
                                      ^
./script/miniscript.h:1430:58: error: declaration shadows a field of 'Node<Key>' [-Werror,-Wshadow]
        auto upfn = [&ctx](const Node& node, Span<state> subs) -> state {
                                                         ^
./script/miniscript.h:509:39: note: previous declaration is here
    mutable std::vector<NodeRef<Key>> subs;
                                      ^
./script/miniscript.h:1538:77: error: declaration shadows a field of 'Node<Key>' [-Werror,-Wshadow]
        return TreeEval<const Node*>([](const Node& node, Span<const Node*> subs) -> const Node* {
                                                                            ^
./script/miniscript.h:509:39: note: previous declaration is here
    mutable std::vector<NodeRef<Key>> subs;
                                      ^
./script/miniscript.h:1551:64: error: declaration shadows a field of 'Node<Key>' [-Werror,-Wshadow]
        return TreeEval<int>([&fn](const Node& node, Span<int> subs) -> bool {
                                                               ^
./script/miniscript.h:509:39: note: previous declaration is here
    mutable std::vector<NodeRef<Key>> subs;
                                      ^
util/sock.cpp:75:27: error: declaration shadows a static data member of 'Sock' [-Werror,-Wshadow]
     static constexpr auto ERR = SOCKET_ERROR;
                           ^
./util/sock.h:154:28: note: previous declaration is here
     static constexpr Event ERR = 0b100;
                            ^
crypto/sha256.cpp:554:23: error: declaration shadows a local variable [-Werror,-Wshadow]
        unsigned char out[64];
                      ^
crypto/sha256.cpp:548:19: note: previous declaration is here
    unsigned char out[32];
                  ^
crypto/sha256.cpp:561:23: error: declaration shadows a local variable [-Werror,-Wshadow]
        unsigned char out[128];
                      ^
crypto/sha256.cpp:548:19: note: previous declaration is here
    unsigned char out[32];
                  ^
crypto/sha256.cpp:568:23: error: declaration shadows a local variable [-Werror,-Wshadow]
        unsigned char out[256];
                      ^
crypto/sha256.cpp:548:19: note: previous declaration is here
    unsigned char out[32];
                  ^
test/key_tests.cpp:184:21: error: declaration shadows a local variable [-Werror,-Wshadow]
        std::string msg = "A message to be signed" + ToString(i);
                    ^
test/key_tests.cpp:161:17: note: previous declaration is here
    std::string msg = "A message to be signed";
                ^
test/key_tests.cpp:340:18: error: declaration shadows a local variable [-Werror,-Wshadow]
            bool ok = key.SignSchnorr(msg256, sig64, &merkle_root, aux256);
                 ^
test/key_tests.cpp:324:14: note: previous declaration is here
        bool ok = key.SignSchnorr(msg256, sig64, nullptr, aux256);
             ^
test/miniminer_tests.cpp:182:21: error: declaration shadows a local variable [-Werror,-Wshadow]
        const auto& entry{*Assert(pool.GetEntry(tx->GetHash()))};
                    ^
test/miniminer_tests.cpp:113:28: note: previous declaration is here
    TestMemPoolEntryHelper entry;
                           ^
test/net_tests.cpp:888:30: error: declaration shadows a local variable [-Werror,-Wshadow]
            for (const auto& addr : addresses) {
                             ^
test/net_tests.cpp:878:57: note: previous declaration is here
    CaptureMessage = [&sent, &expected](const CAddress& addr,
                                                        ^
test/fuzz/utxo_total_supply.cpp:71:28: error: declaration shadows a local variable [-Werror,-Wshadow]
            const uint32_t i = tx.vout.size() - 2;
                           ^
test/fuzz/utxo_total_supply.cpp:66:24: note: previous declaration is here
        const uint32_t i = tx.vout.size() - 1;
                       ^
test/minisketch_tests.cpp:47:23: error: declaration shadows a local variable [-Werror,-Wshadow]
        for (uint32_t i = 0; i < a_not_b; ++i) BOOST_CHECK_EQUAL(sols[i], start_a + i);
                      ^
test/minisketch_tests.cpp:21:14: note: previous declaration is here
    for (int i = 0; i < 100; ++i) {
             ^
test/minisketch_tests.cpp:48:23: error: declaration shadows a local variable [-Werror,-Wshadow]
        for (uint32_t i = 0; i < b_not_a; ++i) BOOST_CHECK_EQUAL(sols[i + a_not_b], start_b + both + i);
                      ^
test/minisketch_tests.cpp:21:14: note: previous declaration is here
    for (int i = 0; i < 100; ++i) {
             ^
By including the line number in the counter variable name

Here I use __LINE__ rather than UNIQUE_NAME's __COUNTER__ to generate a stable name
for the repeated references.

test/fuzz/txorphan.cpp:82:9: error: declaration shadows a local variable [-Werror,-Wshadow]
        LIMITED_WHILE(fuzzed_data_provider.ConsumeBool(), 10 * DEFAULT_MAX_ORPHAN_TRANSACTIONS)
        ^
./test/fuzz/fuzz.h:24:19: note: expanded from macro 'LIMITED_WHILE'
    for (unsigned _count{limit}; (condition) && _count; --_count)
                  ^
test/fuzz/txorphan.cpp:48:5: note: previous declaration is here
    LIMITED_WHILE(outpoints.size() < 200'000 && fuzzed_data_provider.ConsumeBool(), 10 * DEFAULT_MAX_ORPHAN_TRANSACTIONS)
    ^
./test/fuzz/fuzz.h:24:19: note: expanded from macro 'LIMITED_WHILE'
    for (unsigned _count{limit}; (condition) && _count; --_count)
                  ^
@Empact Empact marked this pull request as ready for review February 27, 2024 04:20
@maflcko
Copy link
Member

maflcko commented Feb 27, 2024

The question is: have the circumstances changed in the past 9 years? Is a partial fix beneficial?

I don't think they have. The problem is still that system-dependent stuff (system headers, compilers, compiler versions, lib versions) will mutate the result, so I am sure the frustration will come back.

Just looking at the diff:

  • sometimes std::string error; is allowed, other times it is not, depending on the location. In any case this is fixed by log: Nuke error(...) #29236?
  • There aren't any bugs found by this (except maybe for the one you mentioned in a draft pull request?), so the benefit seems questionable.
  • There are a bunch of conflicts, and the diff isn't trivial to review.

@fanquake
Copy link
Member

I think the commits removing what should be dead code should be split out, but I'm not quite convinced otherwise.

@Empact
Copy link
Member Author

Empact commented Feb 27, 2024

Ok sounds reasonable to me, particularly on the conflicts point, I'll split out that subset for now.

FYI @maflcko AFAIK this is unrelated to #29236, the error in question are local string variables, rather than the log function.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants