Skip to content

Commit 712cab3

Browse files
committed
Merge bitcoin/bitcoin#31061: refactor: Check translatable format strings at compile-time
fa3efb5 refactor: Introduce struct to hold a runtime format string (MarcoFalke) fa6adb0 lint: Remove unused and broken format string linter (MarcoFalke) fadc6b9 refactor: Check translatable format strings at compile-time (MarcoFalke) fa1d5ac refactor: Use TranslateFn type consistently (MarcoFalke) eeee6cf refactor: Delay translation of _() literals (MarcoFalke) Pull request description: All translatable format strings are fixed. This change surfaces errors in them at compile-time. The implementation achieves this by allowing to delay the translation (or `std::string` construction) that previously happened in `_()` by returning a new type from this function. The new type can be converted to `bilingual_str` where needed. This can be tested by adding a format string error in an original string literal and observing a new compile-time failure. Fixes bitcoin/bitcoin#30530 ACKs for top commit: stickies-v: re-ACK fa3efb5 ryanofsky: Code review ACK fa3efb5. Since last review added TranslateFn commit, clarified FormatStringCheck documentation, dropped redundant `inline` keyword Tree-SHA512: 28fa1db11e85935d998031347bd519675d75c171c8323b0ed6cdd0b628c95250bb86b30876946cc48840ded541e95b8a152696f9f2b13a5f28f5673228ee0509
2 parents e7c4794 + fa3efb5 commit 712cab3

23 files changed

+122
-468
lines changed

src/banman.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ void BanMan::LoadBanlist()
3030
{
3131
LOCK(m_banned_mutex);
3232

33-
if (m_client_interface) m_client_interface->InitMessage(_("Loading banlist…").translated);
33+
if (m_client_interface) m_client_interface->InitMessage(_("Loading banlist…"));
3434

3535
const auto start{SteadyClock::now()};
3636
if (m_ban_db.Read(m_banned)) {

src/bitcoin-cli.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ using util::ToString;
5050
// just use a plain system_clock.
5151
using CliClock = std::chrono::system_clock;
5252

53-
const std::function<std::string(const char*)> G_TRANSLATION_FUN = nullptr;
53+
const TranslateFn G_TRANSLATION_FUN{nullptr};
5454

5555
static const char DEFAULT_RPCCONNECT[] = "127.0.0.1";
5656
static const int DEFAULT_HTTP_CLIENT_TIMEOUT=900;

src/bitcoin-tx.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ static bool fCreateBlank;
4141
static std::map<std::string,UniValue> registers;
4242
static const int CONTINUE_EXECUTION=-1;
4343

44-
const std::function<std::string(const char*)> G_TRANSLATION_FUN = nullptr;
44+
const TranslateFn G_TRANSLATION_FUN{nullptr};
4545

4646
static void SetupBitcoinTxArgs(ArgsManager &argsman)
4747
{

src/bitcoin-util.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626

2727
static const int CONTINUE_EXECUTION=-1;
2828

29-
const std::function<std::string(const char*)> G_TRANSLATION_FUN = nullptr;
29+
const TranslateFn G_TRANSLATION_FUN{nullptr};
3030

3131
static void SetupBitcoinUtilArgs(ArgsManager &argsman)
3232
{

src/bitcoin-wallet.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626

2727
using util::Join;
2828

29-
const std::function<std::string(const char*)> G_TRANSLATION_FUN = nullptr;
29+
const TranslateFn G_TRANSLATION_FUN{nullptr};
3030

3131
static void SetupWalletToolArgs(ArgsManager& argsman)
3232
{

src/bitcoind.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@
3434

3535
using node::NodeContext;
3636

37-
const std::function<std::string(const char*)> G_TRANSLATION_FUN = nullptr;
37+
const TranslateFn G_TRANSLATION_FUN{nullptr};
3838

3939
#if HAVE_DECL_FORK
4040

src/clientversion.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ std::string LicenseInfo()
9595
strprintf(_("The source code is available from %s."), URL_SOURCE_CODE).translated +
9696
"\n" +
9797
"\n" +
98-
_("This is experimental software.").translated + "\n" +
98+
_("This is experimental software.") + "\n" +
9999
strprintf(_("Distributed under the MIT software license, see the accompanying file %s or %s"), "COPYING", "<https://opensource.org/licenses/MIT>").translated +
100100
"\n";
101101
}

src/init.cpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1244,8 +1244,8 @@ static ChainstateLoadResult InitAndLoadChainstate(
12441244
_("Error reading from database, shutting down."),
12451245
"", CClientUIInterface::MSG_ERROR);
12461246
};
1247-
uiInterface.InitMessage(_("Loading block index…").translated);
1248-
auto catch_exceptions = [](auto&& f) {
1247+
uiInterface.InitMessage(_("Loading block index…"));
1248+
auto catch_exceptions = [](auto&& f) -> ChainstateLoadResult {
12491249
try {
12501250
return f();
12511251
} catch (const std::exception& e) {
@@ -1255,7 +1255,7 @@ static ChainstateLoadResult InitAndLoadChainstate(
12551255
};
12561256
auto [status, error] = catch_exceptions([&] { return LoadChainstate(chainman, cache_sizes, options); });
12571257
if (status == node::ChainstateLoadStatus::SUCCESS) {
1258-
uiInterface.InitMessage(_("Verifying blocks…").translated);
1258+
uiInterface.InitMessage(_("Verifying blocks…"));
12591259
if (chainman.m_blockman.m_have_pruned && options.check_blocks > MIN_BLOCKS_TO_KEEP) {
12601260
LogWarning("pruned datadir may not have more than %d blocks; only checking available blocks\n",
12611261
MIN_BLOCKS_TO_KEEP);
@@ -1418,7 +1418,7 @@ bool AppInitMain(NodeContext& node, interfaces::BlockAndHeaderTipInfo* tip_info)
14181418

14191419
// Initialize addrman
14201420
assert(!node.addrman);
1421-
uiInterface.InitMessage(_("Loading P2P addresses…").translated);
1421+
uiInterface.InitMessage(_("Loading P2P addresses…"));
14221422
auto addrman{LoadAddrman(*node.netgroupman, args)};
14231423
if (!addrman) return InitError(util::ErrorString(addrman));
14241424
node.addrman = std::move(*addrman);
@@ -1703,7 +1703,7 @@ bool AppInitMain(NodeContext& node, interfaces::BlockAndHeaderTipInfo* tip_info)
17031703
if (chainman.m_blockman.m_blockfiles_indexed) {
17041704
LOCK(cs_main);
17051705
for (Chainstate* chainstate : chainman.GetAll()) {
1706-
uiInterface.InitMessage(_("Pruning blockstore…").translated);
1706+
uiInterface.InitMessage(_("Pruning blockstore…"));
17071707
chainstate->PruneAndFlush();
17081708
}
17091709
}
@@ -1999,7 +1999,7 @@ bool AppInitMain(NodeContext& node, interfaces::BlockAndHeaderTipInfo* tip_info)
19991999
// ChainstateManager's active tip.
20002000
SetRPCWarmupFinished();
20012001

2002-
uiInterface.InitMessage(_("Done loading").translated);
2002+
uiInterface.InitMessage(_("Done loading"));
20032003

20042004
for (const auto& client : node.chain_clients) {
20052005
client->start(scheduler);

src/kernel/bitcoinkernel.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
// Copyright (c) 2022 The Bitcoin Core developers
22
// Distributed under the MIT software license, see the accompanying
33
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
4+
#include <util/translation.h>
45

56
#include <functional>
67
#include <string>
78

89
// Define G_TRANSLATION_FUN symbol in libbitcoinkernel library so users of the
910
// library aren't required to export this symbol
10-
extern const std::function<std::string(const char*)> G_TRANSLATION_FUN = nullptr;
11+
extern const TranslateFn G_TRANSLATION_FUN{nullptr};

src/net.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3296,7 +3296,7 @@ bool CConnman::Start(CScheduler& scheduler, const Options& connOptions)
32963296
}
32973297

32983298
if (m_client_interface) {
3299-
m_client_interface->InitMessage(_("Starting network threads…").translated);
3299+
m_client_interface->InitMessage(_("Starting network threads…"));
33003300
}
33013301

33023302
fAddressesInitialized = true;

0 commit comments

Comments
 (0)