Fixed #6207 ('not' misinterpreted as alternative C token)

This commit is contained in:
Daniel Marjamäki 2016-05-24 13:33:21 +02:00
parent 684966f674
commit 06a594a9e0
2 changed files with 20 additions and 20 deletions

View File

@ -6044,6 +6044,9 @@ namespace {
// xor_eq => ^= // xor_eq => ^=
bool Tokenizer::simplifyCAlternativeTokens() bool Tokenizer::simplifyCAlternativeTokens()
{ {
if (!isC())
return false;
/* For C code: executable scope level */ /* For C code: executable scope level */
unsigned int executableScopeLevel = 0; unsigned int executableScopeLevel = 0;
@ -6066,17 +6069,16 @@ 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 (isC() && !Token::Match(tok->previous(), "%name%|%num%|%char%|)|]|> %name% %name%|%num%|%char%|%op%|(")) if (!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 (isC() && (!Token::Match(tok->next(), "%name%|%op%|(") || if (!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;
} }

View File

@ -5662,22 +5662,20 @@ private:
} }
void simplifyCAlternativeTokens() { void simplifyCAlternativeTokens() {
ASSERT_EQUALS("void f ( ) { if ( a && b ) { ; } }", tokenizeAndStringify("void f() { if (a and b); }")); 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 or b); }")); 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 bitand b); }")); 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 bitor b); }")); 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 xor b); }")); 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 ( ~ b ) { ; } }", tokenizeAndStringify("void f() { if (compl b); }")); 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 (not b); }")); ASSERT_EQUALS("void f ( ) { if ( ! b ) { ; } }", tokenizeAndStringify("void f() { if (not b); }", false, true, Settings::Native, "test.c"));
ASSERT_EQUALS("void f ( ) { if ( a != b ) { ; } }", tokenizeAndStringify("void f() { if (a not_eq b); }")); ASSERT_EQUALS("void f ( ) { if ( a != b ) { ; } }", tokenizeAndStringify("void f() { if (a not_eq b); }", false, true, Settings::Native, "test.c"));
// #6201 // #6201
ASSERT_EQUALS("void f ( ) { if ( ! c || ! memcmp ( a , b , s ) ) { ; } }", tokenizeAndStringify("void f() { if (!c or !memcmp(a, b, s)); }")); 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"));
// #6029
ASSERT_EQUALS("\n" // #6029 ASSERT_EQUALS("void f ( ) { if ( ! b ) { } }", tokenizeAndStringify("void f() { if (not b){} }", false, true, Settings::Native, "test.c"));
"\n" // #6207
"##file 0\n" ASSERT_EQUALS("void f ( ) { if ( not = x ) { } }", tokenizeAndStringify("void f() { if (not=x){} }", false, true, Settings::Native, "test.c"));
"1: void f ( bool b@1 ) { if ( ! b@1 ) { ; } }\n",
tokenizeDebugListing("void f(bool b) { if (not b); }"));
} }
void simplifyCalculations() { void simplifyCalculations() {