Fixed #2892 (false positive: (portability) Assigning an address value to the integer (int/long/etc) type is not portable)

This commit is contained in:
Daniel Marjamäki 2011-07-07 15:14:33 +02:00
parent 4055b0ec5f
commit 66d145ba1c
5 changed files with 28 additions and 1 deletions

View File

@ -61,7 +61,7 @@ void Check64BitPortability::pointerassignment()
if (isaddr(var1) && isint(var2))
assignmentIntegerToAddressError(tok->next());
else if (isint(var1) && isaddr(var2))
else if (isint(var1) && isaddr(var2) && !tok->tokAt(3)->isPointerCompare())
assignmentAddressToIntegerError(tok->next());
}
}

View File

@ -36,6 +36,7 @@ Token::Token(Token **t) :
_isBoolean(false),
_isUnsigned(false),
_isSigned(false),
_isPointerCompare(false),
_isLong(false),
_isUnused(false),
_varId(0),
@ -112,6 +113,7 @@ void Token::deleteThis()
_isBoolean = _next->_isBoolean;
_isUnsigned = _next->_isUnsigned;
_isSigned = _next->_isSigned;
_isPointerCompare = _next->_isPointerCompare;
_isLong = _next->_isLong;
_isUnused = _next->_isUnused;
_varId = _next->_varId;

View File

@ -219,6 +219,14 @@ public:
{
_isSigned = sign;
}
bool isPointerCompare() const
{
return _isPointerCompare;
}
void isPointerCompare(bool b)
{
_isPointerCompare = b;
}
bool isLong() const
{
return _isLong;
@ -447,6 +455,7 @@ private:
bool _isBoolean;
bool _isUnsigned;
bool _isSigned;
bool _isPointerCompare;
bool _isLong;
bool _isUnused;
unsigned int _varId;

View File

@ -260,6 +260,7 @@ void Tokenizer::insertTokens(Token *dest, const Token *src, unsigned int n)
dest->isBoolean(src->isBoolean());
dest->isUnsigned(src->isUnsigned());
dest->isSigned(src->isSigned());
dest->isPointerCompare(src->isPointerCompare());
dest->isLong(src->isLong());
dest->isUnused(src->isUnused());
src = src->next();
@ -284,6 +285,7 @@ Token *Tokenizer::copyTokens(Token *dest, const Token *first, const Token *last)
tok2->isBoolean(tok->isBoolean());
tok2->isUnsigned(tok->isUnsigned());
tok2->isSigned(tok->isSigned());
tok2->isPointerCompare(tok->isPointerCompare());
tok2->isLong(tok->isLong());
tok2->isUnused(tok->isUnused());
tok2->varId(tok->varId());
@ -6477,17 +6479,21 @@ void Tokenizer::simplifyIfNotNull()
Token::Match(tok, "0 != %var%"))
{
deleteFrom = tok->previous();
if (tok->tokAt(2))
tok->tokAt(2)->isPointerCompare(true);
}
else if (Token::Match(tok, "%var% != 0"))
{
deleteFrom = tok;
tok->isPointerCompare(true);
}
else if (Token::Match(tok, "%var% .|:: %var% != 0"))
{
tok = tok->tokAt(2);
deleteFrom = tok;
tok->isPointerCompare(true);
}
}

View File

@ -38,6 +38,7 @@ private:
TEST_CASE(novardecl);
TEST_CASE(functionpar);
TEST_CASE(structmember);
TEST_CASE(ptrcompare);
}
void check(const char code[])
@ -101,6 +102,15 @@ private:
"}\n");
TODO_ASSERT_EQUALS("[test.cpp:3]: (portability) Assigning an address value to the integer (int/long/etc) type is not portable\n", "", errout.str());
}
void ptrcompare()
{
// Ticket #2892
check("void foo(int *p) {\n"
" int a = (p != NULL);\n"
"}\n");
ASSERT_EQUALS("", errout.str());
}
};
REGISTER_TEST(Test64BitPortability)