-
Notifications
You must be signed in to change notification settings - Fork 36.5k
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
tinyformat: refactor: increase compile-time checks and don't throw for tfm::format_error #30928
Draft
stickies-v
wants to merge
4
commits into
bitcoin:master
Choose a base branch
from
stickies-v:2024-09/try-format
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
104c805
move-only: ConstevalFormatString tfm::format to tinyformat.h
stickies-v 4ce60d8
tinyformat: remove std::string overload
stickies-v 4cf1221
tinyformat: force compile-time checks for format string literals
stickies-v 49f7307
tinyformat: don't throw format string errors
stickies-v File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,11 +2,14 @@ | |
// Distributed under the MIT software license, see the accompanying | ||
// file COPYING or http://www.opensource.org/licenses/mit-license.php. | ||
|
||
#include <tinyformat.h> | ||
#include <util/string.h> | ||
|
||
#include <boost/test/unit_test.hpp> | ||
#include <test/util/setup_common.h> | ||
|
||
#include <sstream> | ||
|
||
using namespace util; | ||
|
||
BOOST_AUTO_TEST_SUITE(util_string_tests) | ||
|
@@ -82,4 +85,17 @@ BOOST_AUTO_TEST_CASE(ConstevalFormatString_NumSpec) | |
FailFmtWithError<1>("%1$", err_term); | ||
} | ||
|
||
stickies-v marked this conversation as resolved.
Show resolved
Hide resolved
|
||
BOOST_AUTO_TEST_CASE(tinyformat_ConstevalFormatString) | ||
stickies-v marked this conversation as resolved.
Show resolved
Hide resolved
|
||
{ | ||
// Ensure invalid format strings don't throw at run-time, when not in DEBUG | ||
#ifndef DEBUG | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. might as well put the whole test inside, instead of having an empty passing test when DEBUG |
||
BOOST_CHECK_EQUAL(tfm::format("%*c", "dummy"), R"(Error "tinyformat: Cannot convert from argument type to integer for use as variable width or precision" while formatting: %*c)"); | ||
BOOST_CHECK_EQUAL(tfm::format("%2$*3$d", "dummy", "value"), R"(Error "tinyformat: Positional argument out of range" while formatting: %2$*3$d)"); | ||
std::ostringstream oss; | ||
tfm::format(oss, "%.*f", 5); | ||
BOOST_CHECK_EQUAL(oss.str(), R"(Error "tinyformat: Too many conversion specifiers in format string" while formatting: %.*f)"); | ||
#endif | ||
} | ||
|
||
|
||
BOOST_AUTO_TEST_SUITE_END() |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: we could avoid mentioning compile-time twice:
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think this is an improvement, I find it less clear about the distinction between the compile-time format string and the run-time string parameters, so I'm going to leave this as is for now.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not sure why it's less clear than before, but it's just a nit from my part anyway
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Agree the current message is confusing. The distinction between format string and parameters to the format string should ideally be made clearer, as well as the actual string type.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I used
std::string
at first too, but then it was inconsistent with the rest of the documentation, which I think is even more unclear. I'll reconsider updating allstd::string
references in this block if I have to force-push again unless you think this is blocking?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If
Tinyformat handles string parameters.
is too controversial, I think it can be removed in the follow-up that removes the linter.Otherwise, if this is replaced with
std::string
, someone else is going to suggest to mentionstd::string_view
as well.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Changing tinyformat to support
string_view
is definitely out of scope for this PR.I would at least want to make it clearer that format strings are separate from other parameters, like this or with other words to that effect:
Even though I decided to drop into l0rinc's thread here, I independently found it unclear when doing a review-pass on it.
More precise suggestion, but not blocking:
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I agree with that, and I'll incorporate your first suggestion (I don't agree with the translations bit) if I have to force-push, thanks!