unsigned division: don't warn about 'unsigned char' because it is promoted to int.

This commit is contained in:
Daniel Marjamäki 2013-01-30 16:50:12 +01:00
parent 509dd9a989
commit bc6d31c7c7
3 changed files with 14 additions and 3 deletions

View File

@ -1808,11 +1808,11 @@ void CheckOther::unreachableCodeError(const Token *tok, bool inconclusive)
//--------------------------------------------------------------------------- //---------------------------------------------------------------------------
// Check for unsigned divisions // 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()); return(var && !var->typeStartToken()->isUnsigned() && Token::Match(var->typeEndToken(), "int|char|short|long") && !var->isPointer() && !var->isArray());
} }

View File

@ -294,6 +294,9 @@ public:
void checkVarFuncNullUB(); void checkVarFuncNullUB();
private: private:
bool isUnsigned(const Variable *var) const;
bool isSigned(const Variable *var) const;
// Error messages.. // Error messages..
void oppositeInnerConditionError(const Token *tok); void oppositeInnerConditionError(const Token *tok);
void clarifyCalculationError(const Token *tok, const std::string &op); void clarifyCalculationError(const Token *tok, const std::string &op);

View File

@ -63,6 +63,7 @@ private:
TEST_CASE(division8); TEST_CASE(division8);
TEST_CASE(division9); TEST_CASE(division9);
TEST_CASE(division10); TEST_CASE(division10);
TEST_CASE(division11); // no error when using "unsigned char" (it is promoted)
} }
void division1() { void division1() {
@ -197,6 +198,13 @@ private:
check("i / i", true); check("i / i", true);
ASSERT_EQUALS("", errout.str()); 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) REGISTER_TEST(TestDivision)