diff --git a/lib/tokenize.cpp b/lib/tokenize.cpp index e65a46b26..59d6f7f18 100644 --- a/lib/tokenize.cpp +++ b/lib/tokenize.cpp @@ -5314,9 +5314,9 @@ bool Tokenizer::simplifyLogicalOperators() ret = true; } // "%var%|) and %var%|(" - else if (Token::Match(tok, "and|or") && - ((Token::Match(tok->previous(), "%var%") || tok->previous()->str() == ")") || - (Token::Match(tok->next(), "%var%") || tok->next()->str() == "("))) + else if (Token::Match(tok->previous(), "%any% and|or %any%") && + ((tok->previous()->isName() || tok->previous()->str() == ")") || + (tok->next()->isName() || tok->next()->str() == "("))) { if (tok->str() == "and") tok->str("&&"); diff --git a/test/testpreprocessor.cpp b/test/testpreprocessor.cpp index 08c6c2ead..2427cd2e9 100644 --- a/test/testpreprocessor.cpp +++ b/test/testpreprocessor.cpp @@ -109,6 +109,7 @@ private: TEST_CASE(if_cond7); TEST_CASE(if_cond8); TEST_CASE(if_cond9); + TEST_CASE(if_cond10); TEST_CASE(if_or); @@ -1050,6 +1051,21 @@ private: ASSERT_EQUALS("\nabc\n\n", actual[""]); } + void if_cond10() + { + const char filedata[] = "#if !defined(a) && !defined(b)\n" + "#if defined(and)\n" + "#endif\n" + "#endif\n"; + + // Preprocess => don't crash.. + std::istringstream istr(filedata); + std::map actual; + Settings settings; + Preprocessor preprocessor(&settings, this); + preprocessor.preprocess(istr, actual, "file.c"); + } + void if_or()