Skip to content

Commit 9a101d2

Browse files
committed
scripted-diff: Replace strprintf(Untranslated) with Untranslated(strprintf)
See PR description for rationale behind this change, but summary is that it makes code more consistent and makes it easier to add compile-time checking to enforce that format strings contain the right specifiers, because it stops using Untranslated() to create the format string, so the Untranslated() function will not need to get involved in formatting. -BEGIN VERIFY SCRIPT- quote='"[^"]+"' quotes="(?:$quote|\\W)*" nonparens="[^()]*" single_level_paren="\($nonparens\)" double_level_paren="\($nonparens\($nonparens\)$nonparens\)" exprs="(?:$double_level_paren|$single_level_paren|$nonparens)*" git grep -l 'Untranslated' | xargs perl -0777 -i -pe "s/strprintf\((\\W*)Untranslated\(($quotes)\)($exprs)(\))/Untranslated(\1strprintf(\2\3))/gs" -END VERIFY SCRIPT-
1 parent 19f49f1 commit 9a101d2

File tree

13 files changed

+53
-53
lines changed

13 files changed

+53
-53
lines changed

src/httpserver.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -229,7 +229,7 @@ static bool InitHTTPAllowList()
229229
const CSubNet subnet{LookupSubNet(strAllow)};
230230
if (!subnet.IsValid()) {
231231
uiInterface.ThreadSafeMessageBox(
232-
strprintf(Untranslated("Invalid -rpcallowip subnet specification: %s. Valid are a single IP (e.g. 1.2.3.4), a network/netmask (e.g. 1.2.3.4/255.255.255.0) or a network/CIDR (e.g. 1.2.3.4/24)."), strAllow),
232+
Untranslated(strprintf("Invalid -rpcallowip subnet specification: %s. Valid are a single IP (e.g. 1.2.3.4), a network/netmask (e.g. 1.2.3.4/255.255.255.0) or a network/CIDR (e.g. 1.2.3.4/24).", strAllow)),
233233
"", CClientUIInterface::MSG_ERROR);
234234
return false;
235235
}

src/index/base.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ bool BaseIndex::Init()
106106
// best chain, we will rewind to the fork point during index sync
107107
const CBlockIndex* locator_index{m_chainstate->m_blockman.LookupBlockIndex(locator.vHave.at(0))};
108108
if (!locator_index) {
109-
return InitError(strprintf(Untranslated("%s: best block of the index not found. Please rebuild the index."), GetName()));
109+
return InitError(Untranslated(strprintf("%s: best block of the index not found. Please rebuild the index.", GetName())));
110110
}
111111
SetBestBlockIndex(locator_index);
112112
}

src/init.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1212,7 +1212,7 @@ static ChainstateLoadResult InitAndLoadChainstate(
12121212
try {
12131213
node.chainman = std::make_unique<ChainstateManager>(*Assert(node.shutdown_signal), chainman_opts, blockman_opts);
12141214
} catch (std::exception& e) {
1215-
return {ChainstateLoadStatus::FAILURE_FATAL, strprintf(Untranslated("Failed to initialize ChainstateManager: %s"), e.what())};
1215+
return {ChainstateLoadStatus::FAILURE_FATAL, Untranslated(strprintf("Failed to initialize ChainstateManager: %s", e.what()))};
12161216
}
12171217
ChainstateManager& chainman = *node.chainman;
12181218
// This is defined and set here instead of inline in validation.h to avoid a hard
@@ -1343,7 +1343,7 @@ bool AppInitMain(NodeContext& node, interfaces::BlockAndHeaderTipInfo* tip_info)
13431343
try {
13441344
ipc->listenAddress(address);
13451345
} catch (const std::exception& e) {
1346-
return InitError(strprintf(Untranslated("Unable to bind to IPC address '%s'. %s"), address, e.what()));
1346+
return InitError(Untranslated(strprintf("Unable to bind to IPC address '%s'. %s", address, e.what())));
13471347
}
13481348
LogPrintf("Listening for IPC requests on address %s\n", address);
13491349
}
@@ -2050,7 +2050,7 @@ bool StartIndexBackgroundSync(NodeContext& node)
20502050
const CBlockIndex* start_block = *indexes_start_block;
20512051
if (!start_block) start_block = chainman.ActiveChain().Genesis();
20522052
if (!chainman.m_blockman.CheckBlockDataAvailability(*index_chain.Tip(), *Assert(start_block))) {
2053-
return InitError(strprintf(Untranslated("%s best block of the index goes beyond pruned data. Please disable the index or reindex (which will download the whole blockchain again)"), older_index_name));
2053+
return InitError(Untranslated(strprintf("%s best block of the index goes beyond pruned data. Please disable the index or reindex (which will download the whole blockchain again)", older_index_name)));
20542054
}
20552055
}
20562056

src/init/common.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -111,8 +111,8 @@ bool StartLogging(const ArgsManager& args)
111111
}
112112
}
113113
if (!LogInstance().StartLogging()) {
114-
return InitError(strprintf(Untranslated("Could not open debug log file %s"),
115-
fs::PathToString(LogInstance().m_file_path)));
114+
return InitError(Untranslated(strprintf("Could not open debug log file %s",
115+
fs::PathToString(LogInstance().m_file_path))));
116116
}
117117

118118
if (!LogInstance().m_log_timestamps)

src/net.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3054,22 +3054,22 @@ bool CConnman::BindListenPort(const CService& addrBind, bilingual_str& strError,
30543054
socklen_t len = sizeof(sockaddr);
30553055
if (!addrBind.GetSockAddr((struct sockaddr*)&sockaddr, &len))
30563056
{
3057-
strError = strprintf(Untranslated("Bind address family for %s not supported"), addrBind.ToStringAddrPort());
3057+
strError = Untranslated(strprintf("Bind address family for %s not supported", addrBind.ToStringAddrPort()));
30583058
LogPrintLevel(BCLog::NET, BCLog::Level::Error, "%s\n", strError.original);
30593059
return false;
30603060
}
30613061

30623062
std::unique_ptr<Sock> sock = CreateSock(addrBind.GetSAFamily(), SOCK_STREAM, IPPROTO_TCP);
30633063
if (!sock) {
3064-
strError = strprintf(Untranslated("Couldn't open socket for incoming connections (socket returned error %s)"), NetworkErrorString(WSAGetLastError()));
3064+
strError = Untranslated(strprintf("Couldn't open socket for incoming connections (socket returned error %s)", NetworkErrorString(WSAGetLastError())));
30653065
LogPrintLevel(BCLog::NET, BCLog::Level::Error, "%s\n", strError.original);
30663066
return false;
30673067
}
30683068

30693069
// Allow binding if the port is still in TIME_WAIT state after
30703070
// the program was closed and restarted.
30713071
if (sock->SetSockOpt(SOL_SOCKET, SO_REUSEADDR, (sockopt_arg_type)&nOne, sizeof(int)) == SOCKET_ERROR) {
3072-
strError = strprintf(Untranslated("Error setting SO_REUSEADDR on socket: %s, continuing anyway"), NetworkErrorString(WSAGetLastError()));
3072+
strError = Untranslated(strprintf("Error setting SO_REUSEADDR on socket: %s, continuing anyway", NetworkErrorString(WSAGetLastError())));
30733073
LogPrintf("%s\n", strError.original);
30743074
}
30753075

@@ -3078,14 +3078,14 @@ bool CConnman::BindListenPort(const CService& addrBind, bilingual_str& strError,
30783078
if (addrBind.IsIPv6()) {
30793079
#ifdef IPV6_V6ONLY
30803080
if (sock->SetSockOpt(IPPROTO_IPV6, IPV6_V6ONLY, (sockopt_arg_type)&nOne, sizeof(int)) == SOCKET_ERROR) {
3081-
strError = strprintf(Untranslated("Error setting IPV6_V6ONLY on socket: %s, continuing anyway"), NetworkErrorString(WSAGetLastError()));
3081+
strError = Untranslated(strprintf("Error setting IPV6_V6ONLY on socket: %s, continuing anyway", NetworkErrorString(WSAGetLastError())));
30823082
LogPrintf("%s\n", strError.original);
30833083
}
30843084
#endif
30853085
#ifdef WIN32
30863086
int nProtLevel = PROTECTION_LEVEL_UNRESTRICTED;
30873087
if (sock->SetSockOpt(IPPROTO_IPV6, IPV6_PROTECTION_LEVEL, (const char*)&nProtLevel, sizeof(int)) == SOCKET_ERROR) {
3088-
strError = strprintf(Untranslated("Error setting IPV6_PROTECTION_LEVEL on socket: %s, continuing anyway"), NetworkErrorString(WSAGetLastError()));
3088+
strError = Untranslated(strprintf("Error setting IPV6_PROTECTION_LEVEL on socket: %s, continuing anyway", NetworkErrorString(WSAGetLastError())));
30893089
LogPrintf("%s\n", strError.original);
30903090
}
30913091
#endif

src/node/chainstatemanager_args.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,15 +35,15 @@ util::Result<void> ApplyArgsManOptions(const ArgsManager& args, ChainstateManage
3535
if (auto min_work{uint256::FromUserHex(*value)}) {
3636
opts.minimum_chain_work = UintToArith256(*min_work);
3737
} else {
38-
return util::Error{strprintf(Untranslated("Invalid minimum work specified (%s), must be up to %d hex digits"), *value, uint256::size() * 2)};
38+
return util::Error{Untranslated(strprintf("Invalid minimum work specified (%s), must be up to %d hex digits", *value, uint256::size() * 2))};
3939
}
4040
}
4141

4242
if (auto value{args.GetArg("-assumevalid")}) {
4343
if (auto block_hash{uint256::FromUserHex(*value)}) {
4444
opts.assumed_valid_block = *block_hash;
4545
} else {
46-
return util::Error{strprintf(Untranslated("Invalid assumevalid block hash specified (%s), must be up to %d hex digits (or 0 to disable)"), *value, uint256::size() * 2)};
46+
return util::Error{Untranslated(strprintf("Invalid assumevalid block hash specified (%s), must be up to %d hex digits (or 0 to disable)", *value, uint256::size() * 2))};
4747
}
4848
}
4949

src/node/mempool_args.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ util::Result<void> ApplyArgsManOptions(const ArgsManager& argsman, const CChainP
8989

9090
mempool_opts.require_standard = !argsman.GetBoolArg("-acceptnonstdtxn", DEFAULT_ACCEPT_NON_STD_TXN);
9191
if (!chainparams.IsTestChain() && !mempool_opts.require_standard) {
92-
return util::Error{strprintf(Untranslated("acceptnonstdtxn is not currently supported for %s chain"), chainparams.GetChainTypeString())};
92+
return util::Error{Untranslated(strprintf("acceptnonstdtxn is not currently supported for %s chain", chainparams.GetChainTypeString()))};
9393
}
9494

9595
mempool_opts.full_rbf = argsman.GetBoolArg("-mempoolfullrbf", mempool_opts.full_rbf);

src/qt/bitcoin.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -529,7 +529,7 @@ int GuiMain(int argc, char* argv[])
529529
SetupUIArgs(gArgs);
530530
std::string error;
531531
if (!gArgs.ParseParameters(argc, argv, error)) {
532-
InitError(strprintf(Untranslated("Error parsing command line arguments: %s"), error));
532+
InitError(Untranslated(strprintf("Error parsing command line arguments: %s", error)));
533533
// Create a message box, because the gui has neither been created nor has subscribed to core signals
534534
QMessageBox::critical(nullptr, PACKAGE_NAME,
535535
// message cannot be translated because translations have not been initialized

src/test/result_tests.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ util::Result<int> IntFn(int i, bool success)
4242
util::Result<bilingual_str> StrFn(bilingual_str s, bool success)
4343
{
4444
if (success) return s;
45-
return util::Error{strprintf(Untranslated("str %s error."), s.original)};
45+
return util::Error{Untranslated(strprintf("str %s error.", s.original))};
4646
}
4747

4848
util::Result<NoCopy> NoCopyFn(int i, bool success)

src/validation.cpp

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -5667,20 +5667,20 @@ util::Result<CBlockIndex*> ChainstateManager::ActivateSnapshot(
56675667
if (!GetParams().AssumeutxoForBlockhash(base_blockhash).has_value()) {
56685668
auto available_heights = GetParams().GetAvailableSnapshotHeights();
56695669
std::string heights_formatted = util::Join(available_heights, ", ", [&](const auto& i) { return util::ToString(i); });
5670-
return util::Error{strprintf(Untranslated("assumeutxo block hash in snapshot metadata not recognized (hash: %s). The following snapshot heights are available: %s"),
5670+
return util::Error{Untranslated(strprintf("assumeutxo block hash in snapshot metadata not recognized (hash: %s). The following snapshot heights are available: %s",
56715671
base_blockhash.ToString(),
5672-
heights_formatted)};
5672+
heights_formatted))};
56735673
}
56745674

56755675
snapshot_start_block = m_blockman.LookupBlockIndex(base_blockhash);
56765676
if (!snapshot_start_block) {
5677-
return util::Error{strprintf(Untranslated("The base block header (%s) must appear in the headers chain. Make sure all headers are syncing, and call loadtxoutset again"),
5678-
base_blockhash.ToString())};
5677+
return util::Error{Untranslated(strprintf("The base block header (%s) must appear in the headers chain. Make sure all headers are syncing, and call loadtxoutset again",
5678+
base_blockhash.ToString()))};
56795679
}
56805680

56815681
bool start_block_invalid = snapshot_start_block->nStatus & BLOCK_FAILED_MASK;
56825682
if (start_block_invalid) {
5683-
return util::Error{strprintf(Untranslated("The base block header (%s) is part of an invalid chain"), base_blockhash.ToString())};
5683+
return util::Error{Untranslated(strprintf("The base block header (%s) is part of an invalid chain", base_blockhash.ToString()))};
56845684
}
56855685

56865686
if (!m_best_header || m_best_header->GetAncestor(snapshot_start_block->nHeight) != snapshot_start_block) {
@@ -5840,16 +5840,16 @@ util::Result<void> ChainstateManager::PopulateAndValidateSnapshot(
58405840
if (!snapshot_start_block) {
58415841
// Needed for ComputeUTXOStats to determine the
58425842
// height and to avoid a crash when base_blockhash.IsNull()
5843-
return util::Error{strprintf(Untranslated("Did not find snapshot start blockheader %s"),
5844-
base_blockhash.ToString())};
5843+
return util::Error{Untranslated(strprintf("Did not find snapshot start blockheader %s",
5844+
base_blockhash.ToString()))};
58455845
}
58465846

58475847
int base_height = snapshot_start_block->nHeight;
58485848
const auto& maybe_au_data = GetParams().AssumeutxoForHeight(base_height);
58495849

58505850
if (!maybe_au_data) {
5851-
return util::Error{strprintf(Untranslated("Assumeutxo height in snapshot metadata not recognized "
5852-
"(%d) - refusing to load snapshot"), base_height)};
5851+
return util::Error{Untranslated(strprintf("Assumeutxo height in snapshot metadata not recognized "
5852+
"(%d) - refusing to load snapshot", base_height))};
58535853
}
58545854

58555855
const AssumeutxoData& au_data = *maybe_au_data;
@@ -5887,12 +5887,12 @@ util::Result<void> ChainstateManager::PopulateAndValidateSnapshot(
58875887
if (coin.nHeight > base_height ||
58885888
outpoint.n >= std::numeric_limits<decltype(outpoint.n)>::max() // Avoid integer wrap-around in coinstats.cpp:ApplyHash
58895889
) {
5890-
return util::Error{strprintf(Untranslated("Bad snapshot data after deserializing %d coins"),
5891-
coins_count - coins_left)};
5890+
return util::Error{Untranslated(strprintf("Bad snapshot data after deserializing %d coins",
5891+
coins_count - coins_left))};
58925892
}
58935893
if (!MoneyRange(coin.out.nValue)) {
5894-
return util::Error{strprintf(Untranslated("Bad snapshot data after deserializing %d coins - bad tx out value"),
5895-
coins_count - coins_left)};
5894+
return util::Error{Untranslated(strprintf("Bad snapshot data after deserializing %d coins - bad tx out value",
5895+
coins_count - coins_left))};
58965896
}
58975897
coins_cache.EmplaceCoinInternalDANGER(std::move(outpoint), std::move(coin));
58985898

@@ -5930,8 +5930,8 @@ util::Result<void> ChainstateManager::PopulateAndValidateSnapshot(
59305930
}
59315931
}
59325932
} catch (const std::ios_base::failure&) {
5933-
return util::Error{strprintf(Untranslated("Bad snapshot format or truncated snapshot after deserializing %d coins"),
5934-
coins_processed)};
5933+
return util::Error{Untranslated(strprintf("Bad snapshot format or truncated snapshot after deserializing %d coins",
5934+
coins_processed))};
59355935
}
59365936
}
59375937

@@ -5951,8 +5951,8 @@ util::Result<void> ChainstateManager::PopulateAndValidateSnapshot(
59515951
out_of_coins = true;
59525952
}
59535953
if (!out_of_coins) {
5954-
return util::Error{strprintf(Untranslated("Bad snapshot - coins left over after deserializing %d coins"),
5955-
coins_count)};
5954+
return util::Error{Untranslated(strprintf("Bad snapshot - coins left over after deserializing %d coins",
5955+
coins_count))};
59565956
}
59575957

59585958
LogPrintf("[snapshot] loaded %d (%.2f MB) coins from snapshot %s\n",
@@ -5983,8 +5983,8 @@ util::Result<void> ChainstateManager::PopulateAndValidateSnapshot(
59835983

59845984
// Assert that the deserialized chainstate contents match the expected assumeutxo value.
59855985
if (AssumeutxoHash{maybe_stats->hashSerialized} != au_data.hash_serialized) {
5986-
return util::Error{strprintf(Untranslated("Bad snapshot content hash: expected %s, got %s"),
5987-
au_data.hash_serialized.ToString(), maybe_stats->hashSerialized.ToString())};
5986+
return util::Error{Untranslated(strprintf("Bad snapshot content hash: expected %s, got %s",
5987+
au_data.hash_serialized.ToString(), maybe_stats->hashSerialized.ToString()))};
59885988
}
59895989

59905990
snapshot_chainstate.m_chain.SetTip(*snapshot_start_block);

0 commit comments

Comments
 (0)