Fixed #7580 (False positive when using logical operator keywords 'and', 'or')
This commit is contained in:
parent
920b402d35
commit
6e3d5dc0d1
|
@ -6024,8 +6024,7 @@ void Tokenizer::simplifyVariableMultipleAssign()
|
||||||
}
|
}
|
||||||
|
|
||||||
// Binary operators simplification map
|
// Binary operators simplification map
|
||||||
namespace {
|
static const std::map<std::string, std::string> cAlternativeTokens = make_container< std::map<std::string, std::string> >()
|
||||||
const std::map<std::string, std::string> cAlternativeTokens = make_container< std::map<std::string, std::string> >()
|
|
||||||
<< std::make_pair("and", "&&")
|
<< std::make_pair("and", "&&")
|
||||||
<< std::make_pair("and_eq", "&=")
|
<< std::make_pair("and_eq", "&=")
|
||||||
<< std::make_pair("bitand", "&")
|
<< std::make_pair("bitand", "&")
|
||||||
|
@ -6035,7 +6034,6 @@ namespace {
|
||||||
<< std::make_pair("or_eq", "|=")
|
<< std::make_pair("or_eq", "|=")
|
||||||
<< std::make_pair("xor", "^")
|
<< std::make_pair("xor", "^")
|
||||||
<< std::make_pair("xor_eq", "^=") ;
|
<< std::make_pair("xor_eq", "^=") ;
|
||||||
}
|
|
||||||
|
|
||||||
// Simplify the C alternative tokens:
|
// Simplify the C alternative tokens:
|
||||||
// and => &&
|
// and => &&
|
||||||
|
@ -6051,10 +6049,10 @@ namespace {
|
||||||
// xor_eq => ^=
|
// xor_eq => ^=
|
||||||
bool Tokenizer::simplifyCAlternativeTokens()
|
bool Tokenizer::simplifyCAlternativeTokens()
|
||||||
{
|
{
|
||||||
if (!isC())
|
// In C these are optional macros (iso646.h). In C++ these are builtin keywords.
|
||||||
return false;
|
bool cpp = isCPP();
|
||||||
|
|
||||||
/* For C code: executable scope level */
|
/* executable scope level */
|
||||||
unsigned int executableScopeLevel = 0;
|
unsigned int executableScopeLevel = 0;
|
||||||
|
|
||||||
bool ret = false;
|
bool ret = false;
|
||||||
|
@ -6076,15 +6074,15 @@ bool Tokenizer::simplifyCAlternativeTokens()
|
||||||
|
|
||||||
const std::map<std::string, std::string>::const_iterator cOpIt = cAlternativeTokens.find(tok->str());
|
const std::map<std::string, std::string>::const_iterator cOpIt = cAlternativeTokens.find(tok->str());
|
||||||
if (cOpIt != cAlternativeTokens.end()) {
|
if (cOpIt != cAlternativeTokens.end()) {
|
||||||
if (!Token::Match(tok->previous(), "%name%|%num%|%char%|)|]|> %name% %name%|%num%|%char%|%op%|("))
|
if (!cpp && !Token::Match(tok->previous(), "%name%|%num%|%char%|)|]|> %name% %name%|%num%|%char%|%op%|("))
|
||||||
continue;
|
continue;
|
||||||
tok->str(cOpIt->second);
|
tok->str(cOpIt->second);
|
||||||
ret = true;
|
ret = true;
|
||||||
} else if (Token::Match(tok, "not|compl")) {
|
} else if (Token::Match(tok, "not|compl")) {
|
||||||
// Don't simplify 'not p;' (in case 'not' is a type)
|
// Don't simplify 'not p;' (in case 'not' is a type)
|
||||||
if (!Token::Match(tok->next(), "%name%|(") ||
|
if (!cpp && (!Token::Match(tok->next(), "%name%|(") ||
|
||||||
Token::Match(tok->previous(), "[;{}]") ||
|
Token::Match(tok->previous(), "[;{}]") ||
|
||||||
(executableScopeLevel == 0U && tok->strAt(-1) == "("))
|
(executableScopeLevel == 0U && tok->strAt(-1) == "(")))
|
||||||
continue;
|
continue;
|
||||||
tok->str((tok->str() == "not") ? "!" : "~");
|
tok->str((tok->str() == "not") ? "!" : "~");
|
||||||
ret = true;
|
ret = true;
|
||||||
|
|
|
@ -5662,19 +5662,30 @@ private:
|
||||||
|
|
||||||
void simplifyCAlternativeTokens() {
|
void simplifyCAlternativeTokens() {
|
||||||
ASSERT_EQUALS("void f ( ) { if ( a && b ) { ; } }", tokenizeAndStringify("void f() { if (a and b); }", false, true, Settings::Native, "test.c"));
|
ASSERT_EQUALS("void f ( ) { if ( a && b ) { ; } }", tokenizeAndStringify("void f() { if (a and b); }", false, true, Settings::Native, "test.c"));
|
||||||
|
ASSERT_EQUALS("void f ( ) { if ( a && b ) { ; } }", tokenizeAndStringify("void f() { if (a and b); }", false, true, Settings::Native, "test.cpp"));
|
||||||
ASSERT_EQUALS("void f ( ) { if ( a || b ) { ; } }", tokenizeAndStringify("void f() { if (a or b); }", false, true, Settings::Native, "test.c"));
|
ASSERT_EQUALS("void f ( ) { if ( a || b ) { ; } }", tokenizeAndStringify("void f() { if (a or b); }", false, true, Settings::Native, "test.c"));
|
||||||
|
ASSERT_EQUALS("void f ( ) { if ( a || b ) { ; } }", tokenizeAndStringify("void f() { if (a or b); }", false, true, Settings::Native, "test.cpp"));
|
||||||
ASSERT_EQUALS("void f ( ) { if ( a & b ) { ; } }", tokenizeAndStringify("void f() { if (a bitand b); }", false, true, Settings::Native, "test.c"));
|
ASSERT_EQUALS("void f ( ) { if ( a & b ) { ; } }", tokenizeAndStringify("void f() { if (a bitand b); }", false, true, Settings::Native, "test.c"));
|
||||||
|
ASSERT_EQUALS("void f ( ) { if ( a & b ) { ; } }", tokenizeAndStringify("void f() { if (a bitand b); }", false, true, Settings::Native, "test.cpp"));
|
||||||
ASSERT_EQUALS("void f ( ) { if ( a | b ) { ; } }", tokenizeAndStringify("void f() { if (a bitor b); }", false, true, Settings::Native, "test.c"));
|
ASSERT_EQUALS("void f ( ) { if ( a | b ) { ; } }", tokenizeAndStringify("void f() { if (a bitor b); }", false, true, Settings::Native, "test.c"));
|
||||||
|
ASSERT_EQUALS("void f ( ) { if ( a | b ) { ; } }", tokenizeAndStringify("void f() { if (a bitor b); }", false, true, Settings::Native, "test.cpp"));
|
||||||
ASSERT_EQUALS("void f ( ) { if ( a ^ b ) { ; } }", tokenizeAndStringify("void f() { if (a xor b); }", false, true, Settings::Native, "test.c"));
|
ASSERT_EQUALS("void f ( ) { if ( a ^ b ) { ; } }", tokenizeAndStringify("void f() { if (a xor b); }", false, true, Settings::Native, "test.c"));
|
||||||
|
ASSERT_EQUALS("void f ( ) { if ( a ^ b ) { ; } }", tokenizeAndStringify("void f() { if (a xor b); }", false, true, Settings::Native, "test.cpp"));
|
||||||
ASSERT_EQUALS("void f ( ) { if ( ~ b ) { ; } }", tokenizeAndStringify("void f() { if (compl b); }", false, true, Settings::Native, "test.c"));
|
ASSERT_EQUALS("void f ( ) { if ( ~ b ) { ; } }", tokenizeAndStringify("void f() { if (compl b); }", false, true, Settings::Native, "test.c"));
|
||||||
|
ASSERT_EQUALS("void f ( ) { if ( ~ b ) { ; } }", tokenizeAndStringify("void f() { if (compl b); }", false, true, Settings::Native, "test.cpp"));
|
||||||
ASSERT_EQUALS("void f ( ) { if ( ! b ) { ; } }", tokenizeAndStringify("void f() { if (not b); }", false, true, Settings::Native, "test.c"));
|
ASSERT_EQUALS("void f ( ) { if ( ! b ) { ; } }", tokenizeAndStringify("void f() { if (not b); }", false, true, Settings::Native, "test.c"));
|
||||||
|
ASSERT_EQUALS("void f ( ) { if ( ! b ) { ; } }", tokenizeAndStringify("void f() { if (not b); }", false, true, Settings::Native, "test.cpp"));
|
||||||
ASSERT_EQUALS("void f ( ) { if ( a != b ) { ; } }", tokenizeAndStringify("void f() { if (a not_eq b); }", false, true, Settings::Native, "test.c"));
|
ASSERT_EQUALS("void f ( ) { if ( a != b ) { ; } }", tokenizeAndStringify("void f() { if (a not_eq b); }", false, true, Settings::Native, "test.c"));
|
||||||
|
ASSERT_EQUALS("void f ( ) { if ( a != b ) { ; } }", tokenizeAndStringify("void f() { if (a not_eq b); }", false, true, Settings::Native, "test.cpp"));
|
||||||
// #6201
|
// #6201
|
||||||
ASSERT_EQUALS("void f ( ) { if ( ! c || ! memcmp ( a , b , s ) ) { ; } }", tokenizeAndStringify("void f() { if (!c or !memcmp(a, b, s)); }", false, true, Settings::Native, "test.c"));
|
ASSERT_EQUALS("void f ( ) { if ( ! c || ! memcmp ( a , b , s ) ) { ; } }", tokenizeAndStringify("void f() { if (!c or !memcmp(a, b, s)); }", false, true, Settings::Native, "test.c"));
|
||||||
|
ASSERT_EQUALS("void f ( ) { if ( ! c || ! memcmp ( a , b , s ) ) { ; } }", tokenizeAndStringify("void f() { if (!c or !memcmp(a, b, s)); }", false, true, Settings::Native, "test.cpp"));
|
||||||
// #6029
|
// #6029
|
||||||
ASSERT_EQUALS("void f ( ) { if ( ! b ) { } }", tokenizeAndStringify("void f() { if (not b){} }", false, true, Settings::Native, "test.c"));
|
ASSERT_EQUALS("void f ( ) { if ( ! b ) { } }", tokenizeAndStringify("void f() { if (not b){} }", false, true, Settings::Native, "test.c"));
|
||||||
|
ASSERT_EQUALS("void f ( ) { if ( ! b ) { } }", tokenizeAndStringify("void f() { if (not b){} }", false, true, Settings::Native, "test.cpp"));
|
||||||
// #6207
|
// #6207
|
||||||
ASSERT_EQUALS("void f ( ) { if ( not = x ) { } }", tokenizeAndStringify("void f() { if (not=x){} }", false, true, Settings::Native, "test.c"));
|
ASSERT_EQUALS("void f ( ) { if ( not = x ) { } }", tokenizeAndStringify("void f() { if (not=x){} }", false, true, Settings::Native, "test.c"));
|
||||||
|
ASSERT_EQUALS("void f ( ) { if ( not = x ) { } }", tokenizeAndStringify("void f() { if (not=x){} }", false, true, Settings::Native, "test.cpp"));
|
||||||
}
|
}
|
||||||
|
|
||||||
void simplifyCalculations() {
|
void simplifyCalculations() {
|
||||||
|
|
Loading…
Reference in New Issue