From 5f21d9d97b9b9518b08ac7cbab0ae465af88d91a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Marjam=C3=A4ki?= Date: Tue, 12 Jan 2021 21:26:32 +0100 Subject: [PATCH] Fixed #8975 (Syntax error for valid C code) --- lib/tokenize.cpp | 5 +++++ test/testtokenize.cpp | 11 +++++++++++ 2 files changed, 16 insertions(+) diff --git a/lib/tokenize.cpp b/lib/tokenize.cpp index 721cc4243..d3f8732a5 100644 --- a/lib/tokenize.cpp +++ b/lib/tokenize.cpp @@ -7338,6 +7338,11 @@ bool Tokenizer::simplifyCAlternativeTokens() const std::map::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% *")) diff --git a/test/testtokenize.cpp b/test/testtokenize.cpp index 346b59a1d..98ef5f06d 100644 --- a/test/testtokenize.cpp +++ b/test/testtokenize.cpp @@ -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() {