From 66d145ba1c338bfa3d36d4f602b02772ddd9dc1d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Marjam=C3=A4ki?= Date: Thu, 7 Jul 2011 15:14:33 +0200 Subject: [PATCH] Fixed #2892 (false positive: (portability) Assigning an address value to the integer (int/long/etc) type is not portable) --- lib/check64bit.cpp | 2 +- lib/token.cpp | 2 ++ lib/token.h | 9 +++++++++ lib/tokenize.cpp | 6 ++++++ test/test64bit.cpp | 10 ++++++++++ 5 files changed, 28 insertions(+), 1 deletion(-) diff --git a/lib/check64bit.cpp b/lib/check64bit.cpp index a11954325..3eb6d65ef 100644 --- a/lib/check64bit.cpp +++ b/lib/check64bit.cpp @@ -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()); } } diff --git a/lib/token.cpp b/lib/token.cpp index 6e89de54c..486d7f0e3 100644 --- a/lib/token.cpp +++ b/lib/token.cpp @@ -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; diff --git a/lib/token.h b/lib/token.h index 841753c38..9f42be11e 100644 --- a/lib/token.h +++ b/lib/token.h @@ -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; diff --git a/lib/tokenize.cpp b/lib/tokenize.cpp index a8cf03f48..2235771fd 100644 --- a/lib/tokenize.cpp +++ b/lib/tokenize.cpp @@ -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); } } diff --git a/test/test64bit.cpp b/test/test64bit.cpp index 5d39ecb8c..0699aaa5b 100644 --- a/test/test64bit.cpp +++ b/test/test64bit.cpp @@ -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)