Improved char literal handling. In the 'normal' tokenlist these should not be simplified to integer literals.
This commit is contained in:
parent
e0f9171127
commit
d09a8dde57
|
@ -166,7 +166,7 @@ void CheckString::checkSuspiciousStringCompare()
|
||||||
const Token* litTok = tok->astOperand2();
|
const Token* litTok = tok->astOperand2();
|
||||||
if (!varTok || !litTok) // <- failed to create AST for comparison
|
if (!varTok || !litTok) // <- failed to create AST for comparison
|
||||||
continue;
|
continue;
|
||||||
if (varTok->tokType() == Token::eString || varTok->tokType() == Token::eNumber)
|
if (Token::Match(varTok, "%char%|%num%|%str%"))
|
||||||
std::swap(varTok, litTok);
|
std::swap(varTok, litTok);
|
||||||
else if (!Token::Match(litTok, "%char%|%num%|%str%"))
|
else if (!Token::Match(litTok, "%char%|%num%|%str%"))
|
||||||
continue;
|
continue;
|
||||||
|
@ -200,9 +200,7 @@ void CheckString::checkSuspiciousStringCompare()
|
||||||
varTok = varTok->astParent();
|
varTok = varTok->astParent();
|
||||||
const std::string varname = varTok->expressionString();
|
const std::string varname = varTok->expressionString();
|
||||||
|
|
||||||
const bool ischar(litTok->tokType() == Token::eChar ||
|
const bool ischar(litTok->tokType() == Token::eChar);
|
||||||
(!litTok->originalName().empty() &&
|
|
||||||
litTok->originalName().front() == '\''));
|
|
||||||
if (litTok->tokType() == Token::eString) {
|
if (litTok->tokType() == Token::eString) {
|
||||||
if (_tokenizer->isC() || (var && var->isArrayOrPointer()))
|
if (_tokenizer->isC() || (var && var->isArrayOrPointer()))
|
||||||
suspiciousStringCompareError(tok, varname);
|
suspiciousStringCompareError(tok, varname);
|
||||||
|
|
|
@ -1894,7 +1894,7 @@ void Tokenizer::simplifyNull()
|
||||||
for (Token *tok = list.front(); tok; tok = tok->next()) {
|
for (Token *tok = list.front(); tok; tok = tok->next()) {
|
||||||
if (tok->str() == "NULL" && (!Token::Match(tok->previous(), "[(,] NULL [,)]") || tok->strAt(-2) == "="))
|
if (tok->str() == "NULL" && (!Token::Match(tok->previous(), "[(,] NULL [,)]") || tok->strAt(-2) == "="))
|
||||||
tok->str("0");
|
tok->str("0");
|
||||||
else if (tok->str() == "__null" || tok->str() == "'\\0'" || tok->str() == "'\\x0'") {
|
else if (tok->str() == "__null") {
|
||||||
tok->originalName(tok->str());
|
tok->originalName(tok->str());
|
||||||
tok->str("0");
|
tok->str("0");
|
||||||
}
|
}
|
||||||
|
|
|
@ -341,7 +341,7 @@ private:
|
||||||
ASSERT_EQUALS("; x [ ++ y ] = x [ y ] + 1 ;", tok("; x[++y] += 1;"));
|
ASSERT_EQUALS("; x [ ++ y ] = x [ y ] + 1 ;", tok("; x[++y] += 1;"));
|
||||||
|
|
||||||
ASSERT_EQUALS(";", tok(";x += 0;"));
|
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 |= 0;"));
|
ASSERT_EQUALS(";", tok(";x |= 0;"));
|
||||||
ASSERT_EQUALS(";", tok(";x *= 1;"));
|
ASSERT_EQUALS(";", tok(";x *= 1;"));
|
||||||
|
|
|
@ -3698,15 +3698,13 @@ private:
|
||||||
void vardecl8() {
|
void vardecl8() {
|
||||||
// ticket #696
|
// ticket #696
|
||||||
const char code[] = "char a[10]={'\\0'}, b[10]={'\\0'};";
|
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));
|
ASSERT_EQUALS(res, tokenizeAndStringify(code));
|
||||||
}
|
}
|
||||||
|
|
||||||
void vardecl9() {
|
void vardecl9() {
|
||||||
const char code[] = "char a[2] = {'A', '\\0'}, b[2] = {'B', '\\0'};";
|
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));
|
ASSERT_EQUALS(res, tokenizeAndStringify(code));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue