Skip to content
Open
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
47 changes: 46 additions & 1 deletion lib/templatesimplifier.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,50 @@ static Token *skipRequires(Token *tok)
return tok;
}

// is tok the last token of a trailing return type, as in 'auto f() -> T & requires C<T>'?
static bool isTrailingReturnTypeEnd(const Token *tok)
{
// It is if the tokens from tok backwards form a type that starts right after a '->', and that
// '->' comes after the parameter list (or a specifier following it). A '->' after an object,
// as in 'p->x', is a member access instead.

// walk back over the type: names, '::', '&', '&&', '*', template arguments '<..>' and 'decltype(..)'.
// The loop stops at the first token that cannot be part of the type, the token before the type.
while (Token::Match(tok, "%name%|::|&|&&|*|>|)")) {
if (tok->str() == ">") {
// skip the template arguments ('<' and '>' are not linked yet)
tok = tok->findOpeningBracket();
if (!tok)
return false;
} else if (tok->str() == ")") {
// only 'decltype(..)' ends a type with ')', skip it
if (!Token::simpleMatch(tok->link()->previous(), "decltype"))
return false;
tok = tok->link()->previous();
}
tok = tok->previous();
}
// the type must follow '->' (already simplified to '.')
if (!tok || tok->str() != "." || tok->originalName() != "->")
return false;
// a trailing return type follows a specifier or the parameter list, a member access follows an object
const Token *prev = tok->previous();
if (Token::Match(prev, "const|volatile|noexcept|mutable|&|&&"))
return true;
if (!Token::simpleMatch(prev, ")"))
return false;
// the '(' of a parameter list follows the function name, 'noexcept', template parameters or a lambda
// introducer, the '(' of an object as in '(*p)->x' follows an operator or a keyword like 'return'.
// A call result as in 'f()->x' is not told apart.
const Token *beforeParen = prev->link()->previous();
if (Token::simpleMatch(beforeParen, ">")) {
// the '>' closes template parameters as in '[]<class U>(U u)', or a cast as in 'static_cast<P*>(q)->x'
const Token *opening = beforeParen->findOpeningBracket();
return opening && !Token::Match(opening->previous(), "static_cast|dynamic_cast|reinterpret_cast|const_cast");
}
return Token::Match(beforeParen, "%name%|]") && !Token::Match(beforeParen, "return|throw|co_return|co_yield|co_await");
}

namespace {
class FindToken {
public:
Expand Down Expand Up @@ -3840,7 +3884,8 @@ void TemplateSimplifier::simplifyTemplates(const std::time_t maxtime)
// Remove concepts/requires
// TODO concepts are not removed yet
for (Token *tok = mTokenList.front(); tok; tok = tok->next()) {
if (!Token::Match(tok, ")|>|>>|const|noexcept requires %name%|("))
if (!Token::Match(tok, ")|>|>>|const|noexcept requires %name%|(") &&
!(Token::Match(tok, "%name%|&|&&|* requires %name%|(") && isTrailingReturnTypeEnd(tok)))
continue;
const Token* end = skipRequires(tok->next());
if (end)
Expand Down
41 changes: 41 additions & 0 deletions test/testsimplifytemplate.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -315,6 +315,7 @@ class TestSimplifyTemplate : public TestFixture {
TEST_CASE(requires4);
TEST_CASE(requires5);
TEST_CASE(requires6);
TEST_CASE(requires7);

TEST_CASE(explicitBool1);
TEST_CASE(explicitBool2);
Expand Down Expand Up @@ -6748,6 +6749,46 @@ class TestSimplifyTemplate : public TestFixture {
ASSERT_EQUALS(expected, tok(code));
}

void requires7() { // #15296
const char code[] = "template <class T>\n"
"struct S {\n"
" auto f() -> int requires A<T> || B<T> { return 0; }\n"
" auto g() -> S & requires (A<T> || B<T>) { return *this; }\n"
" auto h() -> std::vector<T> * requires A<T>;\n"
" auto i(T t) -> decltype(t) & requires A<T> || B<T>;\n"
" auto j() noexcept(true) -> int requires A<T> || B<T>;\n"
"};";
const char expected[] = "template < class T > "
"struct S { "
"auto f ( ) . int { return 0 ; } "
"auto g ( ) . S & { return * this ; } "
"auto h ( ) . std :: vector < T > * ; "
"auto i ( T t ) . decltype ( t ) & ; "
"auto j ( ) noexcept ( true ) . int ; "
"} ;";
ASSERT_EQUALS(expected, tok(code));

// requires-expression after a member access
const char code2[] = "template <class T> bool f(P* p, P** a, void* q) {\n"
" return p->x && requires (T t) { t.foo(); } &&\n"
" a[0]->x && requires (T t) { t.foo(); } &&\n"
" (*a)->x && requires (T t) { t.foo(); } &&\n"
" static_cast<P*>(q)->x && requires (T t) { t.foo(); };\n"
"}";
const char expected2[] = "template < class T > bool f ( P * p , P * * a , void * q ) { "
"return p . x && requires ( T t ) { t . foo ( ) ; } && "
"a [ 0 ] . x && requires ( T t ) { t . foo ( ) ; } && "
"( * a ) . x && requires ( T t ) { t . foo ( ) ; } && "
"static_cast < P * > ( q ) . x && requires ( T t ) { t . foo ( ) ; } ; "
"}";
ASSERT_EQUALS(expected2, tok(code2));

// lambda
const char code3[] = "template <class T> void g() { auto l = [](int x) -> int requires A<T> || B<T> { return x; }; }";
const char expected3[] = "template < class T > void g ( ) { auto l ; l = [ ] ( int x ) . int { return x ; } ; }";
ASSERT_EQUALS(expected3, tok(code3));
}

void explicitBool1() {
const char code[] = "class Fred { explicit(true) Fred(int); };\n";
ASSERT_EQUALS("class Fred { explicit Fred ( int ) ; } ;", tok(code));
Expand Down
Loading