Repository navigation
Fix #15183 Syntax error with -!! - #9005
chrchr-github wants to merge 2 commits into
Conversation
dmcppcheck
left a comment
There was a problem hiding this comment.
AI review
The fix works for ! and ~ after a unary +/-. It is incomplete, though: the same check in concatenateNegativeNumberAndAnyPositive() still reports a false syntaxError on valid code when another prefix operator follows the sign. Examples are return - --i;, return - ++i; and return +&x;. They are the same kind of bug as #15183 and fail the same way on base and on the PR. I found no regressions: invalid code such as return -!; is still rejected, now as internalAstError. return -~; is now accepted silently, but return ~; and return x + ~; are already accepted on base, so that is not new.
Testing
- I built
cppcheckandtestrunnerin pr/, andcppcheckin base/. ./testrunner TestTokenizer: 392 tests, 0 failures.- I compared base and PR on small C files of the form
int x; int f(int i) { <stmt> }, run withcppcheck -q --enable=all. - Fixed by the PR. Base reported
syntaxErroron each of these and the PR reports nothing wrong:return -!!i;return -~i;return +!i;return x * -!i;return x == -!i;return x < -~i;return i ? -!i : -~i;int a[] = {-!i, +~i};case -!0:return sizeof -!i;
- Still a false syntax error on valid code, the same on base and PR:
int x; int f(int i) { return - --i; }
int x; int f(int i) { return - ++i; }
int x; int f(int i) { return +&x; }$ ./cppcheck -q u.c
u.c:1:23: error: syntax error [syntaxError]
- Invalid code:
return -!;now givesinternalAstError; base gavesyntaxError.return -~;is now accepted without any error. Base also acceptsreturn ~;andreturn x + ~;, so this is not a new kind of gap.
Automated review of a05990f by Claude. The pull request was built and tested locally.
| continue; | ||
|
|
||
| if (!tok->tokAt(2) || (tok->tokAt(2)->isOp() && !Token::Match(tok->tokAt(2), "[+-*]"))) | ||
| if (!tok->tokAt(2) || (tok->tokAt(2)->isOp() && !Token::Match(tok->tokAt(2), "[+-*!~]"))) |
There was a problem hiding this comment.
AI review
Valid code with other prefix operators after a unary +/- still gets a false syntaxError here, for the same reason as #15183:
int x; int f(int i) { return - --i; }
int x; int f(int i) { return - ++i; }
int x; int f(int i) { return +&x; }All three still give error: syntax error [syntaxError] with this PR. ++/-- are inc/dec op tokens, and & is an address-of operator here. Consider also allowing ++|--|& after the sign, or only rejecting operators that can't be unary.
No description provided.