Skip to content

Commit 22723c8

Browse files
committed
Merge bitcoin/bitcoin#31072: refactor: Clean up messy strformat and bilingual_str usages
0184d33 scripted-diff: Replace strprintf(Untranslated) with Untranslated(strprintf) (Ryan Ofsky) 006e4d1 refactor: Use + instead of strformat to concatenate translated & untranslated strings (Ryan Ofsky) 831d2bf refactor: Don't embed translated string in untranslated string. (Ryan Ofsky) 0580219 refactor: Avoid concatenation of format strings (Ryan Ofsky) Pull request description: This PR cleans up string formatting in the codebase so other PRs adding compile time checking can be simpler and easier to review (specifically #30928, #31061, #31074, and #31149). Currently these PRs are hard to review because in addition to changing formatting APIs, they have to update callers that are using the API's in unusual ways. Clean up these callers now so later PRs can be simpler. Specifically: - Use string literals instead of `std::string` format strings to enable more compile-time checking. - Avoid using untranslated bilingual strings as format strings. Use originals so they can by checked at compile time. - Favor `Untranslated(strprintf(...))` over `strprintf(Untranslated(...), ...)` for consistency and to prevent translated and untranslated strings from being unintentionally combined. ACKs for top commit: maflcko: lgtm ACK 0184d33 🔹 l0rinc: ACK 0184d33 - no overall difference because of the rebase Tree-SHA512: 37eb771187d30977f5f054eddb82af6dd69878ace89cede72032bb389e57200898119f9fe486ce6903ebb00cb24648be215ab3e44842b3d206b35e26038da750
2 parents b1f0f3c + 0184d33 commit 22723c8

File tree

14 files changed

+58
-58
lines changed

14 files changed

+58
-58
lines changed

src/httpserver.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -227,7 +227,7 @@ static bool InitHTTPAllowList()
227227
const CSubNet subnet{LookupSubNet(strAllow)};
228228
if (!subnet.IsValid()) {
229229
uiInterface.ThreadSafeMessageBox(
230-
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),
230+
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)),
231231
"", CClientUIInterface::MSG_ERROR);
232232
return false;
233233
}

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: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -886,7 +886,7 @@ bool AppInitParameterInteraction(const ArgsManager& args)
886886
}
887887
bilingual_str errors;
888888
for (const auto& arg : args.GetUnsuitableSectionOnlyArgs()) {
889-
errors += strprintf(_("Config setting for %s only applied on %s network when in [%s] section.") + Untranslated("\n"), arg, ChainTypeToString(chain), ChainTypeToString(chain));
889+
errors += strprintf(_("Config setting for %s only applied on %s network when in [%s] section."), arg, ChainTypeToString(chain), ChainTypeToString(chain)) + Untranslated("\n");
890890
}
891891

892892
if (!errors.empty()) {
@@ -901,7 +901,7 @@ bool AppInitParameterInteraction(const ArgsManager& args)
901901
// Warn if unrecognized section name are present in the config file.
902902
bilingual_str warnings;
903903
for (const auto& section : args.GetUnrecognizedSections()) {
904-
warnings += strprintf(Untranslated("%s:%i ") + _("Section [%s] is not recognized.") + Untranslated("\n"), section.m_file, section.m_line, section.m_name);
904+
warnings += Untranslated(strprintf("%s:%i ", section.m_file, section.m_line)) + strprintf(_("Section [%s] is not recognized."), section.m_name) + Untranslated("\n");
905905
}
906906

907907
if (!warnings.empty()) {
@@ -1208,7 +1208,7 @@ static ChainstateLoadResult InitAndLoadChainstate(
12081208
try {
12091209
node.chainman = std::make_unique<ChainstateManager>(*Assert(node.shutdown_signal), chainman_opts, blockman_opts);
12101210
} catch (std::exception& e) {
1211-
return {ChainstateLoadStatus::FAILURE_FATAL, strprintf(Untranslated("Failed to initialize ChainstateManager: %s"), e.what())};
1211+
return {ChainstateLoadStatus::FAILURE_FATAL, Untranslated(strprintf("Failed to initialize ChainstateManager: %s", e.what()))};
12121212
}
12131213
ChainstateManager& chainman = *node.chainman;
12141214
// This is defined and set here instead of inline in validation.h to avoid a hard
@@ -1339,7 +1339,7 @@ bool AppInitMain(NodeContext& node, interfaces::BlockAndHeaderTipInfo* tip_info)
13391339
try {
13401340
ipc->listenAddress(address);
13411341
} catch (const std::exception& e) {
1342-
return InitError(strprintf(Untranslated("Unable to bind to IPC address '%s'. %s"), address, e.what()));
1342+
return InitError(Untranslated(strprintf("Unable to bind to IPC address '%s'. %s", address, e.what())));
13431343
}
13441344
LogPrintf("Listening for IPC requests on address %s\n", address);
13451345
}
@@ -2044,7 +2044,7 @@ bool StartIndexBackgroundSync(NodeContext& node)
20442044
const CBlockIndex* start_block = *indexes_start_block;
20452045
if (!start_block) start_block = chainman.ActiveChain().Genesis();
20462046
if (!chainman.m_blockman.CheckBlockDataAvailability(*index_chain.Tip(), *Assert(start_block))) {
2047-
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));
2047+
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)));
20482048
}
20492049
}
20502050

src/init/common.cpp

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

119119
if (!LogInstance().m_log_timestamps)

src/net.cpp

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

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

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

@@ -3082,14 +3082,14 @@ bool CConnman::BindListenPort(const CService& addrBind, bilingual_str& strError,
30823082
if (addrBind.IsIPv6()) {
30833083
#ifdef IPV6_V6ONLY
30843084
if (sock->SetSockOpt(IPPROTO_IPV6, IPV6_V6ONLY, (sockopt_arg_type)&nOne, sizeof(int)) == SOCKET_ERROR) {
3085-
strError = strprintf(Untranslated("Error setting IPV6_V6ONLY on socket: %s, continuing anyway"), NetworkErrorString(WSAGetLastError()));
3085+
strError = Untranslated(strprintf("Error setting IPV6_V6ONLY on socket: %s, continuing anyway", NetworkErrorString(WSAGetLastError())));
30863086
LogPrintf("%s\n", strError.original);
30873087
}
30883088
#endif
30893089
#ifdef WIN32
30903090
int nProtLevel = PROTECTION_LEVEL_UNRESTRICTED;
30913091
if (sock->SetSockOpt(IPPROTO_IPV6, IPV6_PROTECTION_LEVEL, (const char*)&nProtLevel, sizeof(int)) == SOCKET_ERROR) {
3092-
strError = strprintf(Untranslated("Error setting IPV6_PROTECTION_LEVEL on socket: %s, continuing anyway"), NetworkErrorString(WSAGetLastError()));
3092+
strError = Untranslated(strprintf("Error setting IPV6_PROTECTION_LEVEL on socket: %s, continuing anyway", NetworkErrorString(WSAGetLastError())));
30933093
LogPrintf("%s\n", strError.original);
30943094
}
30953095
#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/interface_ui.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ bool InitError(const bilingual_str& str, const std::vector<std::string>& details
7474
// functions which provide error details are ones that run during early init
7575
// before the GUI uiInterface is registered, so there's no point passing
7676
// main messages and details separately to uiInterface yet.
77-
return InitError(details.empty() ? str : strprintf(Untranslated("%s:\n%s"), str, MakeUnorderedList(details)));
77+
return InitError(details.empty() ? str : str + Untranslated(strprintf(":\n%s", MakeUnorderedList(details))));
7878
}
7979

8080
void InitWarning(const bilingual_str& str)

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.persist_v1_dat = argsman.GetBoolArg("-persistmempoolv1", mempool_opts.persist_v1_dat);

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, CLIENT_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)

0 commit comments

Comments
 (0)