Fixed #8975 (Syntax error for valid C code)

This commit is contained in:
Daniel Marjamäki 2021-01-12 21:26:32 +01:00
parent 7c5015d54e
commit 5f21d9d97b
2 changed files with 16 additions and 0 deletions

View File

@ -7338,6 +7338,11 @@ bool Tokenizer::simplifyCAlternativeTokens()
const std::map<std::string, std::string>::const_iterator cOpIt = cAlternativeTokens.find(tok->str());
if (cOpIt != cAlternativeTokens.end()) {
alt.push_back(tok);
// Is this a variable declaration..
if (isC() && Token::Match(tok->previous(), "%type%|* %name% [;,=]"))
return false;
if (!Token::Match(tok->previous(), "%name%|%num%|%char%|)|]|> %name% %name%|%num%|%char%|%op%|("))
continue;
if (Token::Match(tok->next(), "%assign%|%or%|%oror%|&&|*|/|%|^") && !Token::Match(tok->previous(), "%num%|%char%|) %name% *"))

View File

@ -6307,6 +6307,17 @@ private:
tokenizeAndStringify("void f(const char *str) { while (*str=='!' or *str=='['){} }"));
// #9920
ASSERT_EQUALS("result = ch != s . end ( ) && * ch == ':' ;", tokenizeAndStringify("result = ch != s.end() and *ch == ':';", false, true, Settings::Native, "test.c"));
// #8975
ASSERT_EQUALS("void foo ( ) {\n"
"char * or ;\n"
"while ( ( * or != 0 ) && ( * or != '|' ) ) { or ++ ; }\n"
"}",
tokenizeAndStringify(
"void foo() {\n"
" char *or;\n"
" while ((*or != 0) && (*or != '|')) or++;\n"
"}", false, true, Settings::Native, "test.c"));
}
void simplifyCalculations() {