Skip to content

Fix #15126 FP syntaxError (function try block with override in struct) - #8955

Open
chrchr-github wants to merge 2 commits into
cppcheck-opensource:mainfrom
chrchr-github:chr_15126
Open

chrchr-github wants to merge 2 commits into
cppcheck-opensource:mainfrom
chrchr-github:chr_15126

Conversation

@chrchr-github

Copy link
Copy Markdown
Collaborator

No description provided.

@dmcppcheck dmcppcheck left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

AI review

The fix works for the common forms, void f() [const|noexcept|noexcept(true)] override|final try {...} catch(...) {...}, and I found no regressions. It only handles a single override or final directly after ) or after a keyword, though. Two forms with override still give the same syntaxError:

  • a ref-qualifier: void i() & override try, void i() && override try, void i() const & override try
  • a trailing return type: auto k() -> void override try

The base version fails on these forms without override as well (void i() & try, auto k() -> void try), so they are a gap that already existed rather than a regression. It is still a gap in the same ticket area. When final or override is used as a variable name before a try inside a function body, the PR gives no new false positive.

Testing

I built cppcheck and testrunner in pr/. ./testrunner TestTokenizer TestGarbage TestSymbolDatabase TestSimplifyTokens TestClass passed with 0 failures.

Handled correctly, no errors:

  • const override
  • noexcept override
  • noexcept(true) override
  • override final
  • int final = 0; try {...} inside a function body
  • int override[2]; ... try {...} inside a function body

Still syntaxError, ./cppcheck -q x.cpp in each case:

struct E : B {
    void i() & override try {} catch (...) {}
};
error: syntax error: keyword 'try' is not allowed in class/struct/union/enum scope [syntaxError]

These forms give the same error:

  • void i() && override try {} catch (...) {}
  • void i() const & override try {} catch (...) {}
  • auto k() -> void override try {} catch (...) {}

Without override, the base version also rejects void i() & try {} catch (...) {} and auto k() -> void try {} catch (...) {}, so this limitation existed before the PR.


Automated review of aa91b3a by Claude. The pull request was built and tested locally.

Comment thread lib/tokenize.cpp
continue;
const Token* par = tok->previous();
while (par && par->isKeyword())
while (par && (par->isKeyword() || Token::Match(par, "final|override")))

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

AI review

This loop still misses override or final after a ref-qualifier or a trailing return type. These still produce syntaxError:

struct E : B { void i() & override try {} catch (...) {} };
struct F : B { auto k() -> void override try {} catch (...) {} };

&/&& and -> type before try already fail in base without override, so this is a gap that existed before, not a regression. It may still be worth covering here, or noting in the ticket.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants