From bc6d31c7c75f48ed7199eb1dea3c88f69865cfeb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Marjam=C3=A4ki?= Date: Wed, 30 Jan 2013 16:50:12 +0100 Subject: [PATCH] unsigned division: don't warn about 'unsigned char' because it is promoted to int. --- lib/checkother.cpp | 6 +++--- lib/checkother.h | 3 +++ test/testdivision.cpp | 8 ++++++++ 3 files changed, 14 insertions(+), 3 deletions(-) diff --git a/lib/checkother.cpp b/lib/checkother.cpp index 81280841c..c6644218a 100644 --- a/lib/checkother.cpp +++ b/lib/checkother.cpp @@ -1808,11 +1808,11 @@ void CheckOther::unreachableCodeError(const Token *tok, bool inconclusive) //--------------------------------------------------------------------------- // Check for unsigned divisions //--------------------------------------------------------------------------- -static bool isUnsigned(const Variable* var) +bool CheckOther::isUnsigned(const Variable* var) const { - return(var && var->typeStartToken()->isUnsigned() && !var->isPointer() && !var->isArray()); + return(var && var->typeStartToken()->isUnsigned() && !var->isPointer() && !var->isArray() && _tokenizer->sizeOfType(var->typeStartToken()) >= _settings->sizeof_int); } -static bool isSigned(const Variable* var) +bool CheckOther::isSigned(const Variable* var) const { return(var && !var->typeStartToken()->isUnsigned() && Token::Match(var->typeEndToken(), "int|char|short|long") && !var->isPointer() && !var->isArray()); } diff --git a/lib/checkother.h b/lib/checkother.h index a09e5a242..c8ed424d1 100644 --- a/lib/checkother.h +++ b/lib/checkother.h @@ -294,6 +294,9 @@ public: void checkVarFuncNullUB(); private: + bool isUnsigned(const Variable *var) const; + bool isSigned(const Variable *var) const; + // Error messages.. void oppositeInnerConditionError(const Token *tok); void clarifyCalculationError(const Token *tok, const std::string &op); diff --git a/test/testdivision.cpp b/test/testdivision.cpp index fb372656b..32669cc0b 100644 --- a/test/testdivision.cpp +++ b/test/testdivision.cpp @@ -63,6 +63,7 @@ private: TEST_CASE(division8); TEST_CASE(division9); TEST_CASE(division10); + TEST_CASE(division11); // no error when using "unsigned char" (it is promoted) } void division1() { @@ -197,6 +198,13 @@ private: check("i / i", true); ASSERT_EQUALS("", errout.str()); } + + void division11() { + check("void f(int x, unsigned char y) {\n" + " int z = x / y;\n" // no error, y is promoted + "}", true); + ASSERT_EQUALS("", errout.str()); + } }; REGISTER_TEST(TestDivision)