Skip to content

Commit 5f21d9d

Browse files
committed
Fixed #8975 (Syntax error for valid C code)
1 parent 7c5015d commit 5f21d9d

2 files changed

Lines changed: 16 additions & 0 deletions

File tree

lib/tokenize.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7338,6 +7338,11 @@ bool Tokenizer::simplifyCAlternativeTokens()
73387338
const std::map<std::string, std::string>::const_iterator cOpIt = cAlternativeTokens.find(tok->str());
73397339
if (cOpIt != cAlternativeTokens.end()) {
73407340
alt.push_back(tok);
7341+
7342+
// Is this a variable declaration..
7343+
if (isC() && Token::Match(tok->previous(), "%type%|* %name% [;,=]"))
7344+
return false;
7345+
73417346
if (!Token::Match(tok->previous(), "%name%|%num%|%char%|)|]|> %name% %name%|%num%|%char%|%op%|("))
73427347
continue;
73437348
if (Token::Match(tok->next(), "%assign%|%or%|%oror%|&&|*|/|%|^") && !Token::Match(tok->previous(), "%num%|%char%|) %name% *"))

test/testtokenize.cpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6307,6 +6307,17 @@ class TestTokenizer : public TestFixture {
63076307
tokenizeAndStringify("void f(const char *str) { while (*str=='!' or *str=='['){} }"));
63086308
// #9920
63096309
ASSERT_EQUALS("result = ch != s . end ( ) && * ch == ':' ;", tokenizeAndStringify("result = ch != s.end() and *ch == ':';", false, true, Settings::Native, "test.c"));
6310+
6311+
// #8975
6312+
ASSERT_EQUALS("void foo ( ) {\n"
6313+
"char * or ;\n"
6314+
"while ( ( * or != 0 ) && ( * or != '|' ) ) { or ++ ; }\n"
6315+
"}",
6316+
tokenizeAndStringify(
6317+
"void foo() {\n"
6318+
" char *or;\n"
6319+
" while ((*or != 0) && (*or != '|')) or++;\n"
6320+
"}", false, true, Settings::Native, "test.c"));
63106321
}
63116322

63126323
void simplifyCalculations() {

0 commit comments

Comments
 (0)