Improved char literal handling. In the 'normal' tokenlist these should not be simplified to integer literals.

This commit is contained in:
Daniel Marjamäki 2016-10-12 10:20:24 +02:00
parent e0f9171127
commit d09a8dde57
4 changed files with 6 additions and 10 deletions

View File

@ -166,7 +166,7 @@ void CheckString::checkSuspiciousStringCompare()
const Token* litTok = tok->astOperand2();
if (!varTok || !litTok) // <- failed to create AST for comparison
continue;
if (varTok->tokType() == Token::eString || varTok->tokType() == Token::eNumber)
if (Token::Match(varTok, "%char%|%num%|%str%"))
std::swap(varTok, litTok);
else if (!Token::Match(litTok, "%char%|%num%|%str%"))
continue;
@ -200,9 +200,7 @@ void CheckString::checkSuspiciousStringCompare()
varTok = varTok->astParent();
const std::string varname = varTok->expressionString();
const bool ischar(litTok->tokType() == Token::eChar ||
(!litTok->originalName().empty() &&
litTok->originalName().front() == '\''));
const bool ischar(litTok->tokType() == Token::eChar);
if (litTok->tokType() == Token::eString) {
if (_tokenizer->isC() || (var && var->isArrayOrPointer()))
suspiciousStringCompareError(tok, varname);

View File

@ -1894,7 +1894,7 @@ void Tokenizer::simplifyNull()
for (Token *tok = list.front(); tok; tok = tok->next()) {
if (tok->str() == "NULL" && (!Token::Match(tok->previous(), "[(,] NULL [,)]") || tok->strAt(-2) == "="))
tok->str("0");
else if (tok->str() == "__null" || tok->str() == "'\\0'" || tok->str() == "'\\x0'") {
else if (tok->str() == "__null") {
tok->originalName(tok->str());
tok->str("0");
}

View File

@ -341,7 +341,7 @@ private:
ASSERT_EQUALS("; x [ ++ y ] = x [ y ] + 1 ;", tok("; x[++y] += 1;"));
ASSERT_EQUALS(";", tok(";x += 0;"));
ASSERT_EQUALS(";", tok(";x += '\\0';"));
TODO_ASSERT_EQUALS(";", "; x = x + '\\0' ;", tok("; x += '\\0'; "));
ASSERT_EQUALS(";", tok(";x -= 0;"));
ASSERT_EQUALS(";", tok(";x |= 0;"));
ASSERT_EQUALS(";", tok(";x *= 1;"));

View File

@ -3698,15 +3698,13 @@ private:
void vardecl8() {
// ticket #696
const char code[] = "char a[10]={'\\0'}, b[10]={'\\0'};";
const char res[] = "char a [ 10 ] = { 0 } ; char b [ 10 ] = { 0 } ;";
const char res[] = "char a [ 10 ] = { '\\0' } ; char b [ 10 ] = { '\\0' } ;";
ASSERT_EQUALS(res, tokenizeAndStringify(code));
}
void vardecl9() {
const char code[] = "char a[2] = {'A', '\\0'}, b[2] = {'B', '\\0'};";
const char res[] = "char a [ 2 ] = { 'A' , 0 } ; char b [ 2 ] = { 'B' , 0 } ;";
const char res[] = "char a [ 2 ] = { 'A' , '\\0' } ; char b [ 2 ] = { 'B' , '\\0' } ;";
ASSERT_EQUALS(res, tokenizeAndStringify(code));
}