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
17 changes: 15 additions & 2 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
49 changes: 49 additions & 0 deletions test/testtokenize.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,7 @@ class TestTokenizer : public TestFixture {
TEST_CASE(vardecl34);
TEST_CASE(vardecl35);
TEST_CASE(vardecl36);
TEST_CASE(vardecl37);
TEST_CASE(vardecl_stl_1);
TEST_CASE(vardecl_stl_2);
TEST_CASE(vardecl_stl_3);
Expand Down Expand Up @@ -3069,6 +3070,54 @@ 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 volatile_variables() {
{
const char code[] = "volatile int a=0;\n"
Expand Down
Loading