diff --git a/lib/templatesimplifier.cpp b/lib/templatesimplifier.cpp index d28b350ea8b..ecb02b2d427 100644 --- a/lib/templatesimplifier.cpp +++ b/lib/templatesimplifier.cpp @@ -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'? +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 '[](U u)', or a cast as in 'static_cast(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: @@ -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) diff --git a/test/testsimplifytemplate.cpp b/test/testsimplifytemplate.cpp index 407649e0c7f..73c435d51c4 100644 --- a/test/testsimplifytemplate.cpp +++ b/test/testsimplifytemplate.cpp @@ -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); @@ -6748,6 +6749,46 @@ class TestSimplifyTemplate : public TestFixture { ASSERT_EQUALS(expected, tok(code)); } + void requires7() { // #15296 + const char code[] = "template \n" + "struct S {\n" + " auto f() -> int requires A || B { return 0; }\n" + " auto g() -> S & requires (A || B) { return *this; }\n" + " auto h() -> std::vector * requires A;\n" + " auto i(T t) -> decltype(t) & requires A || B;\n" + " auto j() noexcept(true) -> int requires A || B;\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 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(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 void g() { auto l = [](int x) -> int requires A || B { 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));