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
9 changes: 8 additions & 1 deletion lib/tokenize.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9482,8 +9482,15 @@ 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

When the trailing return type ends in an array declarator, the walk stops at the ) of (*)/(&), which is not the end of the parameter list. isFunctionHead() then takes its ) ;|{|[ branch, skips [3] and finds override instead of try, so this still gives syntaxError (base does too):

struct D : B {
    auto k() -> int(*)[3] override try { return nullptr; } catch (...) { return nullptr; }
};
k.cpp:4:36: error: syntax error: keyword 'try' is not allowed in class/struct/union/enum scope [syntaxError]

-> int(&)[3] override try fails the same way. -> int(*)() override works only because the ) that the walk finds happens to be followed directly by override.

if (par->str() == ">") {
if (const Token* ob = par->findOpeningBracket()) {
par = ob;
continue;
}
}
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
20 changes: 20 additions & 0 deletions test/testtokenize.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2030,6 +2030,26 @@ 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"
"auto l ( ) . W < decltype ( 1 ) > 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"
" auto l() -> W<decltype(1)> override try {} catch (...) {}\n"
"};\n"));
ASSERT_EQUALS("[test.cpp:6:5]: (debug) auto token with no type. [autoNoType]\n"
"[test.cpp:7:5]: (debug) auto token with no type. [autoNoType]\n",
errout_str());
}

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