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 @@ -3778,7 +3778,7 @@ void Tokenizer::concatenateNegativeNumberAndAnyPositive()
if (tok->findOpeningBracket())
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), "[+-*!~]")))

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

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.

syntaxError(tok);

while ((tok->isArithmeticalOp() || tok->isComparisonOp()) && tok->str() != ">" && tok->next() && tok->strAt(1) == "+" &&
Expand Down
11 changes: 11 additions & 0 deletions test/testtokenize.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8195,6 +8195,17 @@ class TestTokenizer : public TestFixture {

ASSERT_EQUALS(";", tokenizeAndStringify("typedef std::size_t size_t;\n")); // #14809
ASSERT_EQUALS("struct S { int i { 0 } ; } ;", tokenizeAndStringify("struct S { int i : 1{0}; };\n")); // #15070

ASSERT_EQUALS( // #15183
"int f ( int i ) {\n"
"if ( i > 0 ) {\n"
"return - ! ! i ; }\n"
"return - ~ i ;\n"
"}", tokenizeAndStringify("int f(int i) {\n"
" if (i > 0)\n"
" return -!!i;\n"
" return -~i;\n"
"}\n"));
}


Expand Down
Loading