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
20 changes: 17 additions & 3 deletions lib/tokenize.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7511,6 +7511,7 @@ void Tokenizer::simplifyVarDecl(const bool only_k_r_fpar)
simplifyVarDecl(list.front(), nullptr, only_k_r_fpar);
}

// Returns the closing parenthesis of the parameter list if tok ends a trailing return type
static Token* isTrailingReturnType(Token* tok)
{
while (Token::Match(tok, "%name%|::|>")) {
Expand All @@ -7519,8 +7520,20 @@ static Token* isTrailingReturnType(Token* tok)
else
tok = tok->tokAt(-1);
}
if (tok && Token::simpleMatch(tok->tokAt(-1), ") ."))
return tok->tokAt(-1);
if (!Token::simpleMatch(tok, "."))
return nullptr;
// Skip the specifiers between the parameter list and the trailing return type
tok = tok->previous();
while (tok) {
if (Token::Match(tok, "const|volatile|&|&&|noexcept|mutable|constexpr|consteval|static"))
tok = tok->previous();
else if (tok->str() == ")" && Token::Match(tok->link()->previous(), "noexcept|throw"))
tok = tok->link()->tokAt(-2);
else
break;
}
if (Token::simpleMatch(tok, ")"))
return tok;
return nullptr;
}

Expand Down Expand Up @@ -7581,7 +7594,8 @@ void Tokenizer::simplifyVarDecl(Token * tokBegin, const Token * const tokEnd, co
if (only_k_r_fpar && finishedwithkr) {
if (Token::Match(tok, "(|[|{")) {
tok = tok->link();
if (tok->next() && Token::Match(tok, ") !!{"))
// Don't step onto tokEnd: the loop increment would then step past it
if (tok->next() && tok->next() != tokEnd && Token::Match(tok, ") !!{"))
tok = tok->next();
else
continue;
Expand Down
55 changes: 55 additions & 0 deletions test/testtokenize.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,8 @@ class TestTokenizer : public TestFixture {
TEST_CASE(vardecl34);
TEST_CASE(vardecl35);
TEST_CASE(vardecl36);
TEST_CASE(vardecl37);
TEST_CASE(vardecl38);
TEST_CASE(vardecl_stl_1);
TEST_CASE(vardecl_stl_2);
TEST_CASE(vardecl_stl_3);
Expand Down Expand Up @@ -3069,6 +3071,59 @@ class TestTokenizer : public TestFixture {
ASSERT_EQUALS(expected, tokenizeAndStringify(code));
}

void vardecl37() { // #15244 specifiers before a trailing return type
{
const char code[] = "struct S {\n"
" auto f() const -> int;\n"
" auto g() volatile & -> int;\n"
" auto h() && -> int;\n"
" auto i() const noexcept -> int;\n"
"};\n"
"auto S::f() const -> int { int a = 1, b = 2; return a + b; }\n"
"auto S::g() volatile & -> int { int a = 1, b = 2; return a + b; }\n"
"auto S::h() && -> int { int a = 1, b = 2; return a + b; }\n"
"auto S::i() const noexcept -> int { int a = 1, b = 2; return a + b; }\n"
"auto j() noexcept -> int { int a = 1, b = 2; return a + b; }\n"
"auto k() noexcept(true) -> int { int a = 1, b = 2; return a + b; }\n";
const char expected[] = "struct S {\n"
"auto f ( ) const . int ;\n"
"auto g ( ) volatile & . int ;\n"
"auto h ( ) && . int ;\n"
"auto i ( ) const noexcept . int ;\n"
"} ;\n"
"auto S :: f ( ) const . int { int a ; a = 1 ; int b ; b = 2 ; return a + b ; }\n"
"auto S :: g ( ) volatile & . int { int a ; a = 1 ; int b ; b = 2 ; return a + b ; }\n"
"auto S :: h ( ) && . int { int a ; a = 1 ; int b ; b = 2 ; return a + b ; }\n"
"auto S :: i ( ) const noexcept . int { int a ; a = 1 ; int b ; b = 2 ; return a + b ; }\n"
"auto j ( ) noexcept . int { int a ; a = 1 ; int b ; b = 2 ; return a + b ; }\n"
"auto k ( ) noexcept ( true ) . int { int a ; a = 1 ; int b ; b = 2 ; return a + b ; }";
ASSERT_EQUALS(expected, tokenizeAndStringify(code));
ignore_errout();
}
{ // a structured binding is not a lambda
const char code[] = "struct R { int a; int b; };\n"
"R f();\n"
"auto g() noexcept -> int { auto [p, c]{f()}; return p; }\n";
ASSERT_EQUALS("struct R { int a ; int b ; } ;\n"
"R f ( ) ;\n"
"auto g ( ) noexcept . int { auto [ p , c ] { f ( ) } ; return p ; }",
tokenizeAndStringify(code));
ignore_errout();
}
{ // lambdas
const char code[] = "auto l = [](int x) mutable -> int { int a = 1, b = 2; return a + b + x; };\n"
"auto m = [](int x) noexcept(true) -> int { int a = 1, b = 2; return a + b + x; };\n";
ASSERT_EQUALS("auto l ; l = [ ] ( int x ) mutable . int { int a ; a = 1 ; int b ; b = 2 ; return a + b + x ; } ;\n"
"auto m ; m = [ ] ( int x ) noexcept ( true ) . int { int a ; a = 1 ; int b ; b = 2 ; return a + b + x ; } ;",
tokenizeAndStringify(code));
}
}

void vardecl38() { // #15245 range ending in ')' before a braced initializer's '}'
ASSERT_EQUALS("int * q { new int [ 2 ] { ( 1 ) } } ;",
tokenizeAndStringify("int* q{new int[2]{(1)}};"));
}

void volatile_variables() {
{
const char code[] = "volatile int a=0;\n"
Expand Down