Skip to content
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

Fix 12372: C++20: syntaxError with implicit operator and requires clause #6397

Merged
merged 3 commits into from
May 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion lib/tokenize.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ const Token * Tokenizer::isFunctionHead(const Token *tok, const std::string &end
if (Token::Match(tok, "%name% (") && tok->isUpperCaseName())
tok = tok->linkAt(1)->next();
if (tok && tok->originalName() == "->") { // trailing return type
for (tok = tok->next(); tok && !Token::Match(tok, ";|{|override|final"); tok = tok->next())
for (tok = tok->next(); tok && !Token::Match(tok, ";|{|override|final|}|)|]"); tok = tok->next())
if (tok->link() && Token::Match(tok, "<|[|("))
tok = tok->link();
}
Expand All @@ -136,6 +136,14 @@ const Token * Tokenizer::isFunctionHead(const Token *tok, const std::string &end
tok = tok->next();
if (Token::Match(tok, "= 0|default|delete ;"))
tok = tok->tokAt(2);
if (Token::simpleMatch(tok, "requires")) {
for (tok = tok->next(); tok && !Token::Match(tok, ";|{|}|)|]"); tok = tok->next()) {
if (tok->link() && Token::Match(tok, "<|[|("))
tok = tok->link();
if (Token::simpleMatch(tok, "bool {"))
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should this be "%bool% {"? But apparently it's not needed at all...

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, this is for checking when doing explicit conversion to bool as the requires clause doesnt allow implicit conversion to bool so then the user will need to explicitly convert with either static_cast<bool>(...), (bool) ..., bool(...) or bool{...}. However, the latter(ie bool{}) will exit the loop on { so this extra check is there to continue with the explicit conversion.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If it is needed for something, we should have a test for that.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added test.

tok = tok->linkAt(1);
}
}
if (tok && tok->str() == ":" && !Token::Match(tok->next(), "%name%|::"))
return nullptr;
return (tok && endsWith.find(tok->str()) != std::string::npos) ? tok : nullptr;
Expand Down
6 changes: 6 additions & 0 deletions test/testtokenize.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7605,6 +7605,12 @@ class TestTokenizer : public TestFixture {
{
ASSERT_NO_THROW(tokenizeAndStringify("template<class T, class U>\n"
"struct X { X(U) requires true {} };\n"));
ASSERT_NO_THROW(tokenizeAndStringify("template<class T, class U>\n"
"struct X { X(U) requires bool{std::is_integral<T>{}} {} };\n"));
ASSERT_NO_THROW(tokenizeAndStringify("template <typename T>\n"
"struct test { operator int() requires true { return 0; } };\n"));
ASSERT_NO_THROW(tokenizeAndStringify("template <typename T>\n"
"struct test { operator int() requires bool{std::is_integral<T>{}} { return 0; } };\n"));
}

void noCrash1() {
Expand Down
Loading