Skip to content

Commit

Permalink
Fix #13419 (Tokenizer: invalid compound statement simplification in f…
Browse files Browse the repository at this point in the history
…or loop) (#7101)
  • Loading branch information
danmar authored Dec 15, 2024
1 parent ee7fd29 commit c194451
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 6 deletions.
8 changes: 5 additions & 3 deletions lib/tokenize.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3665,7 +3665,7 @@ void Tokenizer::simplifyExternC()
}
}

void Tokenizer::simplifyRoundCurlyParentheses()
void Tokenizer::simplifyCompoundStatements()
{
for (Token *tok = list.front(); tok; tok = tok->next()) {
while (Token::Match(tok, "[;{}:] ( {") &&
Expand All @@ -3683,6 +3683,8 @@ void Tokenizer::simplifyRoundCurlyParentheses()
tok->deleteThis();
tok->deleteNext(3);
}
else if (tok->str() == "(")
tok = tok->link();
}
}

Expand Down Expand Up @@ -5615,8 +5617,8 @@ bool Tokenizer::simplifyTokenList1(const char FileName[])
if (isCPP())
simplifyExternC();

// simplify weird but legal code: "[;{}] ( { code; } ) ;"->"[;{}] code;"
simplifyRoundCurlyParentheses();
// simplify compound statements: "[;{}] ( { code; } ) ;"->"[;{}] code;"
simplifyCompoundStatements();

// check for simple syntax errors..
for (const Token *tok = list.front(); tok; tok = tok->next()) {
Expand Down
2 changes: 1 addition & 1 deletion lib/tokenize.h
Original file line number Diff line number Diff line change
Expand Up @@ -311,7 +311,7 @@ class CPPCHECKLIB Tokenizer {

void simplifyExternC();

void simplifyRoundCurlyParentheses();
void simplifyCompoundStatements();

void simplifyTypeIntrinsics();

Expand Down
7 changes: 5 additions & 2 deletions test/testtokenize.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -310,7 +310,7 @@ class TestTokenizer : public TestFixture {
TEST_CASE(simplifyCAlternativeTokens);

// x = ({ 123; }); => { x = 123; }
TEST_CASE(simplifyRoundCurlyParentheses);
TEST_CASE(simplifyCompoundStatements);

TEST_CASE(simplifyOperatorName1);
TEST_CASE(simplifyOperatorName2);
Expand Down Expand Up @@ -5059,9 +5059,12 @@ class TestTokenizer : public TestFixture {
ignore_errout();
}

void simplifyRoundCurlyParentheses() {
void simplifyCompoundStatements() {
ASSERT_EQUALS("; x = 123 ;", tokenizeAndStringify(";x=({123;});"));
ASSERT_EQUALS("; x = y ;", tokenizeAndStringify(";x=({y;});"));
// #13419: Do not simplify compound statements in for loop
ASSERT_EQUALS("void foo ( int x ) { for ( ; ( { { } ; x < 1 ; } ) ; ) }",
tokenizeAndStringify("void foo(int x) { for (;({ {}; x<1; });) }"));
}

void simplifyOperatorName1() {
Expand Down

0 comments on commit c194451

Please sign in to comment.