ci, iwyu: Enforce warning-free `src/common`
What changed, and why it matters
This is a routine code cleanup commit. It adjusts which C++ header files are included in the `src/common` part of Bitcoin Core so that an automated tool called Include What You Use (IWYU) reports no warnings. It also tells the continuous integration system to treat any future IWYU warnings in this area as errors. There is no change to program behavior, no bug fix, and no security-relevant change.
No security action required. Treat as normal build/CI hygiene.
Security signals we found
No strong security signals were identified.
Evidence from the diff
The commit modifies 22 files under src/common plus CI scripts and one test file. Changes are limited to adding, removing, or reordering #include directives to satisfy IWYU, and updating the CI regex (FILES_WITH_ENFORCED_IWYU) to cover all of src/common/*.cpp. It also swaps some system headers for project-specific replacements (e.g., <util/check.h> for <cassert>, <util/time.h> for <chrono>). No functional code logic is altered.
Changed components
src/common/*.cppsrc/common/*.hci/test/03_test_script.shInspect captured patch +75 / −38
diff --git a/ci/test/03_test_script.sh b/ci/test/03_test_script.sh
index 7650ab7d..834473b1 100755
--- a/ci/test/03_test_script.sh
+++ b/ci/test/03_test_script.sh
@@ -234,7 +234,7 @@ fi
if [[ "${RUN_IWYU}" == true ]]; then
# TODO: Consider enforcing IWYU across the entire codebase.
- FILES_WITH_ENFORCED_IWYU="/src/(((bench|crypto|index|kernel|primitives|script|univalue/(lib|test)|util|zmq)/.*|common/license_info|node/(blockstorage|interfaces|miner|mining_args|utxo_snapshot)|rpc/mining|clientversion|core_io|signet|init)\\.cpp)"
+ FILES_WITH_ENFORCED_IWYU='/src/((bench|common|crypto|index|kernel|primitives|script|univalue/(lib|test)|util|zmq)/.*|node/(blockstorage|interfaces|miner|mining_args|utxo_snapshot)|rpc/mining|clientversion|core_io|signet|init)\.cpp'
jq --arg patterns "$FILES_WITH_ENFORCED_IWYU" 'map(select(.file | test($patterns)))' "${BASE_BUILD_DIR}/compile_commands.json" > "${BASE_BUILD_DIR}/compile_commands_iwyu_errors.json"
jq --arg patterns "$FILES_WITH_ENFORCED_IWYU" 'map(select(.file | test($patterns) | not))' "${BASE_BUILD_DIR}/compile_commands.json" > "${BASE_BUILD_DIR}/compile_commands_iwyu_warnings.json"
@@ -247,7 +247,8 @@ if [[ "${RUN_IWYU}" == true ]]; then
-p "${BASE_BUILD_DIR}" "${MAKEJOBS}" \
-- -Xiwyu --cxx17ns -Xiwyu --mapping_file="${BASE_ROOT_DIR}/contrib/devtools/iwyu/bitcoin.core.imp" \
-Xiwyu --max_line_length=160 \
- -Xiwyu --check_also="*/primitives/*.h" \
+ -Xiwyu --check_also='*/common/types\.h' \
+ -Xiwyu --check_also='*/primitives/transaction_identifier\.h' \
2>&1 || true
} | tee /tmp/iwyu_ci.out
python3 "/include-what-you-use/fix_includes.py" --nosafe_headers < /tmp/iwyu_ci.out
diff --git a/src/common/args.cpp b/src/common/args.cpp
index cfd36e1f..97b37eb4 100644
--- a/src/common/args.cpp
+++ b/src/common/args.cpp
@@ -23,8 +23,6 @@
#endif
#include <algorithm>
-#include <cassert>
-#include <cstdint>
#include <cstdlib>
#include <cstring>
#include <map>
diff --git a/src/common/args.h b/src/common/args.h
index 0a3195f8..0a83e143 100644
--- a/src/common/args.h
+++ b/src/common/args.h
@@ -6,7 +6,6 @@
#define BITCOIN_COMMON_ARGS_H
#include <common/settings.h>
-#include <compat/compat.h>
#include <sync.h>
#include <util/chaintype.h>
#include <util/fs.h>
diff --git a/src/common/bloom.cpp b/src/common/bloom.cpp
index 3ee78994..c15c8f78 100644
--- a/src/common/bloom.cpp
+++ b/src/common/bloom.cpp
@@ -16,8 +16,7 @@
#include <algorithm>
#include <cmath>
-#include <cstdlib>
-#include <limits>
+#include <compare>
#include <vector>
static constexpr double LN2SQUARED = 0.4804530139182014246671025263266649717305529515945455;
diff --git a/src/common/bloom.h b/src/common/bloom.h
index 97007e1f..c9ed89f8 100644
--- a/src/common/bloom.h
+++ b/src/common/bloom.h
@@ -6,8 +6,9 @@
#define BITCOIN_COMMON_BLOOM_H
#include <serialize.h>
-#include <span.h>
+#include <cstdint>
+#include <span>
#include <vector>
class COutPoint;
diff --git a/src/common/config.cpp b/src/common/config.cpp
index cc7ffd59..8cf324ca 100644
--- a/src/common/config.cpp
+++ b/src/common/config.cpp
@@ -2,28 +2,25 @@
// Distributed under the MIT software license, see the accompanying
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
-#include <common/args.h>
+#include <common/args.h> // IWYU pragma: associated
#include <common/settings.h>
#include <sync.h>
#include <tinyformat.h>
#include <univalue.h>
-#include <util/chaintype.h>
+#include <util/check.h>
#include <util/fs.h>
#include <util/log.h>
#include <util/string.h>
#include <algorithm>
-#include <cassert>
#include <cstdlib>
-#include <filesystem>
#include <fstream>
#include <iostream>
-#include <sstream>
#include <list>
#include <map>
-#include <memory>
#include <optional>
+#include <sstream>
#include <string>
#include <string_view>
#include <utility>
diff --git a/src/common/init.cpp b/src/common/init.cpp
index 5c9742be..091b8d81 100644
--- a/src/common/init.cpp
+++ b/src/common/init.cpp
@@ -2,15 +2,15 @@
// Distributed under the MIT software license, see the accompanying
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
+#include <common/init.h>
+
#include <chainparams.h>
#include <common/args.h>
-#include <common/init.h>
#include <tinyformat.h>
#include <util/fs.h>
#include <util/log.h>
#include <util/translation.h>
-#include <algorithm>
#include <exception>
#include <optional>
diff --git a/src/common/interfaces.cpp b/src/common/interfaces.cpp
index b501493d..3873ca2e 100644
--- a/src/common/interfaces.cpp
+++ b/src/common/interfaces.cpp
@@ -2,8 +2,9 @@
// Distributed under the MIT software license, see the accompanying
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
-#include <interfaces/echo.h>
-#include <interfaces/handler.h>
+#include <interfaces/echo.h> // IWYU pragma: associated
+#include <interfaces/handler.h> // IWYU pragma: associated
+
#include <util/btcsignals.h>
#include <memory>
diff --git a/src/common/messages.cpp b/src/common/messages.cpp
index 82ad310b..e558442f 100644
--- a/src/common/messages.cpp
+++ b/src/common/messages.cpp
@@ -4,16 +4,17 @@
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
#include <common/messages.h>
+
#include <common/types.h>
#include <node/types.h>
#include <policy/fees/block_policy_estimator.h>
#include <tinyformat.h>
+#include <util/check.h>
#include <util/fees.h>
#include <util/strencodings.h>
#include <util/string.h>
#include <util/translation.h>
-#include <cassert>
#include <map>
#include <string>
#include <string_view>
diff --git a/src/common/messages.h b/src/common/messages.h
index 60fdaa18..6dc3212f 100644
--- a/src/common/messages.h
+++ b/src/common/messages.h
@@ -13,17 +13,19 @@
#include <string>
#include <string_view>
+#include <utility>
struct bilingual_str;
-
enum class FeeEstimateMode;
enum class FeeReason;
+
namespace node {
enum class TransactionError;
} // namespace node
namespace common {
enum class PSBTError;
+
bool FeeModeFromString(std::string_view mode_string, FeeEstimateMode& fee_estimate_mode);
std::string StringForFeeReason(FeeReason reason);
std::string FeeModes(const std::string& delimiter);
diff --git a/src/common/netif.cpp b/src/common/netif.cpp
index 997db7d5..1120ee60 100644
--- a/src/common/netif.cpp
+++ b/src/common/netif.cpp
@@ -6,12 +6,22 @@
#include <common/netif.h>
+#include <compat/compat.h>
#include <netbase.h>
#include <util/check.h>
#include <util/log.h>
#include <util/sock.h>
+#include <cerrno>
+#include <cstdint>
+#include <cstring>
+#include <functional>
+#include <memory>
+#include <string>
+#include <type_traits>
+
#if defined(__linux__)
+#include <linux/netlink.h>
#include <linux/rtnetlink.h>
#elif defined(__FreeBSD__)
#include <netlink/netlink.h>
@@ -27,8 +37,6 @@
#include <ifaddrs.h>
#endif
-#include <type_traits>
-
namespace {
//! Return CNetAddr for the specified OS-level network address.
diff --git a/src/common/netif.h b/src/common/netif.h
index 769bcbcc..84df00a2 100644
--- a/src/common/netif.h
+++ b/src/common/netif.h
@@ -8,6 +8,7 @@
#include <netaddress.h>
#include <optional>
+#include <vector>
//! Query the OS for the default gateway for `network`. This only makes sense for NET_IPV4 and NET_IPV6.
//! Returns std::nullopt if it cannot be found, or there is no support for this OS.
diff --git a/src/common/pcp.cpp b/src/common/pcp.cpp
index 7b22e82e..96a85821 100644
--- a/src/common/pcp.cpp
+++ b/src/common/pcp.cpp
@@ -4,19 +4,30 @@
#include <common/pcp.h>
-#include <atomic>
-#include <common/netif.h>
+#include <compat/compat.h>
#include <crypto/common.h>
+#include <crypto/hex_base.h>
#include <netaddress.h>
#include <netbase.h>
-#include <random.h>
-#include <span.h>
+#include <tinyformat.h>
#include <util/check.h>
#include <util/log.h>
-#include <util/readwritefile.h>
#include <util/sock.h>
-#include <util/strencodings.h>
+#include <util/string.h>
#include <util/threadinterrupt.h>
+#include <util/time.h>
+
+#include <algorithm>
+#include <atomic>
+#include <compare>
+#include <cstring>
+#include <functional>
+#include <map>
+#include <memory>
+#include <optional>
+#include <span>
+#include <utility>
+#include <vector>
namespace {
diff --git a/src/common/pcp.h b/src/common/pcp.h
index 121349b0..c48317f7 100644
--- a/src/common/pcp.h
+++ b/src/common/pcp.h
@@ -6,10 +6,16 @@
#define BITCOIN_COMMON_PCP_H
#include <netaddress.h>
-#include <util/threadinterrupt.h>
+#include <util/time.h>
+#include <array>
+#include <cstddef>
+#include <cstdint>
+#include <string>
#include <variant>
+class CThreadInterrupt;
+
// RFC6886 NAT-PMP and RFC6887 Port Control Protocol (PCP) implementation.
// NAT-PMP and PCP use network byte order (big-endian).
diff --git a/src/common/run_command.cpp b/src/common/run_command.cpp
index 86f89e17..8c57e53b 100644
--- a/src/common/run_command.cpp
+++ b/src/common/run_command.cpp
@@ -14,6 +14,10 @@
#include <util/subprocess.h>
#endif // ENABLE_EXTERNAL_SIGNER
+#include <sstream>
+#include <stdexcept>
+#include <utility>
+
UniValue RunCommandParseJSON(const std::vector<std::string>& cmd_args, const std::string& str_std_in)
{
#ifdef ENABLE_EXTERNAL_SIGNER
diff --git a/src/common/settings.cpp b/src/common/settings.cpp
index 7d511b57..eca29bec 100644
--- a/src/common/settings.cpp
+++ b/src/common/settings.cpp
@@ -2,15 +2,14 @@
// Distributed under the MIT software license, see the accompanying
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
-#include <common/settings.h>
-
#include <bitcoin-build-config.h> // IWYU pragma: keep
+#include <common/settings.h>
+
#include <tinyformat.h>
#include <univalue.h>
#include <util/fs.h>
-#include <algorithm>
#include <fstream>
#include <iterator>
#include <map>
diff --git a/src/common/settings.h b/src/common/settings.h
index bc7b89a9..6f2579b1 100644
--- a/src/common/settings.h
+++ b/src/common/settings.h
@@ -12,7 +12,9 @@
#include <string>
#include <vector>
-class UniValue;
+// Users of this header need to explicitly #include <univalue.h>
+// IWYU pragma: no_include <univalue.h>
+class UniValue; // IWYU pragma: keep
namespace common {
diff --git a/src/common/signmessage.cpp b/src/common/signmessage.cpp
index 0f9e1f5e..b45951f6 100644
--- a/src/common/signmessage.cpp
+++ b/src/common/signmessage.cpp
@@ -4,15 +4,18 @@
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
#include <common/signmessage.h>
+
+#include <addresstype.h>
#include <hash.h>
#include <key.h>
#include <key_io.h>
#include <pubkey.h>
#include <uint256.h>
+#include <util/check.h>
#include <util/strencodings.h>
-#include <cassert>
#include <optional>
+#include <span>
#include <string>
#include <variant>
#include <vector>
diff --git a/src/common/system.cpp b/src/common/system.cpp
index ca7b857d..c33e3564 100644
--- a/src/common/system.cpp
+++ b/src/common/system.cpp
@@ -12,12 +12,13 @@
#include <util/time.h>
#ifdef WIN32
-#include <cassert>
-#include <codecvt>
#include <compat/compat.h>
+#include <util/check.h>
+#include <codecvt>
#include <windows.h>
#else
#include <sys/stat.h>
+#include <sys/types.h>
#include <unistd.h>
#endif
@@ -29,6 +30,7 @@
#include <cstddef>
#include <cstdint>
#include <cstdlib>
+#include <limits>
#include <locale>
#include <optional>
#include <stdexcept>
diff --git a/src/common/system.h b/src/common/system.h
index a3100fec..6fa6bd44 100644
--- a/src/common/system.h
+++ b/src/common/system.h
@@ -7,10 +7,10 @@
#define BITCOIN_COMMON_SYSTEM_H
#include <bitcoin-build-config.h> // IWYU pragma: keep
+
#include <util/time.h>
-#include <chrono>
-#include <cstdint>
+#include <cstddef>
#include <optional>
#include <string>
diff --git a/src/common/url.cpp b/src/common/url.cpp
index 19db4e99..a186c512 100644
--- a/src/common/url.cpp
+++ b/src/common/url.cpp
@@ -5,6 +5,7 @@
#include <common/url.h>
#include <charconv>
+#include <cstddef>
#include <string>
#include <string_view>
#include <system_error>
diff --git a/src/test/util_tests.cpp b/src/test/util_tests.cpp
index 4dbe9b45..90510e3d 100644
--- a/src/test/util_tests.cpp
+++ b/src/test/util_tests.cpp
@@ -4,6 +4,7 @@
#include <clientversion.h>
#include <common/signmessage.h>
+#include <compat/compat.h>
#include <hash.h>
#include <key.h>
#include <script/parsing.h>
Why this scored 15/100
Community notes
Notes can correct, qualify, or add evidence to the AI analysis. Every note shown here has been validated by a human moderator.
The AI analysis stands alone for now. Submit a note if you can add evidence or important context.