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
2 changes: 1 addition & 1 deletion lib/tokenize.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9427,7 +9427,7 @@ void Tokenizer::simplifyFunctionTryCatch()
if (!Token::Match(tok, "try {|:"))
continue;
const Token* par = tok->previous();
while (par && par->isKeyword())
while (par && !Token::Match(par, "[);]"))

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

AI review

The walk stops at the first ). When that ) is inside a template argument of the trailing return type, isFunctionHead() sees ) > override try and returns null. So this still gives syntaxError:

struct D : B {
    auto f() -> W<decltype(1)> override try { return {}; } catch (...) { return {}; }
};

A fix could skip linked tokens while walking back: when par is a > or ) with a link, jump to par->link(). It could also keep walking until par->link()->previous() is a name that is not decltype/noexcept/throw. Either way, the walk would reach the ) that closes the parameter list.

par = par->previous();
if (!TokenList::isFunctionHead(par, "try")) // TODO: this is supposed to a list of characters and not strings
continue;
Expand Down
2 changes: 1 addition & 1 deletion lib/tokenlist.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2379,7 +2379,7 @@ const Token * TokenList::isFunctionHead(const Token *tok, const std::string &suf
tok = tok->linkAt(1)->next();
if (Token::Match(tok, "%name% (") && tok->isUpperCaseName())
tok = tok->linkAt(1)->next();
if (tok && tok->originalName() == "->") { // trailing return type
if (tok && (tok->str() == "->" || tok->originalName() == "->")) { // trailing return type
for (tok = tok->next(); tok && !Token::Match(tok, ";|{|override|final|}|)|]"); tok = tok->next())
if (tok->link() && Token::Match(tok, "<|[|("))
tok = tok->link();
Expand Down
16 changes: 16 additions & 0 deletions test/testtokenize.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2028,6 +2028,22 @@ class TestTokenizer : public TestFixture {
"void S::func() const\n"
"try { f(); }\n"
"catch (...) { g(); }\n"));

ASSERT_EQUALS("struct D : B {\n" // #15126
"void f ( ) override { try { } catch ( ... ) { } }\n"
"void g ( ) final { try { } catch ( ... ) { } }\n"
"void i ( ) & override { try { } catch ( ... ) { } }\n"
"void j ( ) const && override { try { } catch ( ... ) { } }\n"
"auto k ( ) . std :: nullptr_t override { try { } catch ( ... ) { } }\n"
"} ;",
tokenizeAndStringify("struct D : B {\n"
" void f() override try {} catch (...) {}\n"
" void g() final try {} catch (...) {}\n"
" void i() & override try {} catch (...) {}\n"
" void j() const && override try {} catch (...) {}\n"
" auto k() -> std::nullptr_t override try {} catch (...) {}\n"
"};\n"));
ASSERT_EQUALS("[test.cpp:6:5]: (debug) auto token with no type. [autoNoType]\n", errout_str());
}

// Simplify "((..))" into "(..)"
Expand Down
Loading