Rename Library::isargvalid()

This commit is contained in:
Daniel Marjamäki 2018-07-15 23:05:48 +02:00
parent 491ee577c6
commit 7c4820e047
5 changed files with 108 additions and 111 deletions

View File

@ -120,9 +120,9 @@ void CheckFunctions::invalidFunctionUsage()
invalidFunctionArgBoolError(argtok, functionToken->str(), argnr);
// Are the values 0 and 1 valid?
else if (!mSettings->library.isargvalid(functionToken, argnr, static_cast<MathLib::bigint>(0)))
else if (!mSettings->library.isIntArgValid(functionToken, argnr, 0))
invalidFunctionArgError(argtok, functionToken->str(), argnr, nullptr, mSettings->library.validarg(functionToken, argnr));
else if (!mSettings->library.isargvalid(functionToken, argnr, static_cast<MathLib::bigint>(1)))
else if (!mSettings->library.isIntArgValid(functionToken, argnr, 1))
invalidFunctionArgError(argtok, functionToken->str(), argnr, nullptr, mSettings->library.validarg(functionToken, argnr));
}
}

View File

@ -603,19 +603,16 @@ Library::Error Library::loadFunction(const tinyxml2::XMLElement * const node, co
error |= range | (*(p+1) == '.');
range = true;
has_dot = false;
}
else if (*p == '-')
} else if (*p == '-')
error |= (!std::isdigit(*(p+1)));
else if (*p == ',') {
range = false;
error |= *(p+1) == '.';
has_dot = false;
}
else if (*p == '.') {
} else if (*p == '.') {
error |= has_dot | (!std::isdigit(*(p+1)));
has_dot = true;
}
else
} else
error = true;
}
if (error)
@ -726,13 +723,13 @@ Library::Error Library::loadFunction(const tinyxml2::XMLElement * const node, co
return Error(OK);
}
bool Library::isargvalid(const Token *ftok, int argnr, const MathLib::bigint argvalue) const
bool Library::isIntArgValid(const Token *ftok, int argnr, const MathLib::bigint argvalue) const
{
const ArgumentChecks *ac = getarg(ftok, argnr);
if (!ac || ac->valid.empty())
return true;
else if (ac->valid.find('.') != std::string::npos)
return isargvalid(ftok, argnr, static_cast<double>(argvalue));
return isFloatArgValid(ftok, argnr, argvalue);
TokenList tokenList(nullptr);
gettokenlistfromvalid(ac->valid, tokenList);
for (const Token *tok = tokenList.front(); tok; tok = tok->next()) {
@ -748,7 +745,7 @@ bool Library::isargvalid(const Token *ftok, int argnr, const MathLib::bigint arg
return false;
}
bool Library::isargvalid(const Token *ftok, int argnr, double argvalue) const
bool Library::isFloatArgValid(const Token *ftok, int argnr, double argvalue) const
{
const ArgumentChecks *ac = getarg(ftok, argnr);
if (!ac || ac->valid.empty())

View File

@ -300,8 +300,8 @@ public:
return arg && arg->strz;
}
bool isargvalid(const Token *ftok, int argnr, const MathLib::bigint argvalue) const;
bool isargvalid(const Token *ftok, int argnr, double argvalue) const;
bool isIntArgValid(const Token *ftok, int argnr, const MathLib::bigint argvalue) const;
bool isFloatArgValid(const Token *ftok, int argnr, double argvalue) const;
const std::string& validarg(const Token *ftok, int argnr) const {
const ArgumentChecks *arg = getarg(ftok, argnr);

View File

@ -1497,8 +1497,8 @@ const ValueFlow::Value * Token::getInvalidValue(const Token *ftok, unsigned int
const ValueFlow::Value *ret = nullptr;
std::list<ValueFlow::Value>::const_iterator it;
for (it = mValues->begin(); it != mValues->end(); ++it) {
if ((it->isIntValue() && !settings->library.isargvalid(ftok, argnr, it->intvalue)) ||
(it->isFloatValue() && !settings->library.isargvalid(ftok, argnr, it->floatValue))) {
if ((it->isIntValue() && !settings->library.isIntArgValid(ftok, argnr, it->intvalue)) ||
(it->isFloatValue() && !settings->library.isFloatArgValid(ftok, argnr, it->floatValue))) {
if (!ret || ret->isInconclusive() || (ret->condition && !it->isInconclusive()))
ret = &(*it);
if (!ret->isInconclusive() && !ret->condition)

View File

@ -299,120 +299,120 @@ private:
tokenList.front()->next()->astOperand1(tokenList.front());
// 1-
ASSERT_EQUALS(false, library.isargvalid(tokenList.front(), 1, static_cast<MathLib::bigint>(-10)));
ASSERT_EQUALS(false, library.isargvalid(tokenList.front(), 1, -10.0));
ASSERT_EQUALS(false, library.isargvalid(tokenList.front(), 1, static_cast<MathLib::bigint>(0)));
ASSERT_EQUALS(false, library.isargvalid(tokenList.front(), 1, 0.0));
ASSERT_EQUALS(true, library.isargvalid(tokenList.front(), 1, static_cast<MathLib::bigint>(1)));
ASSERT_EQUALS(true, library.isargvalid(tokenList.front(), 1, 1.0));
ASSERT_EQUALS(true, library.isargvalid(tokenList.front(), 1, static_cast<MathLib::bigint>(10)));
ASSERT_EQUALS(true, library.isargvalid(tokenList.front(), 1, 10.0));
ASSERT_EQUALS(false, library.isIntArgValid(tokenList.front(), 1, -10));
ASSERT_EQUALS(false, library.isFloatArgValid(tokenList.front(), 1, -10.0));
ASSERT_EQUALS(false, library.isIntArgValid(tokenList.front(), 1, 0));
ASSERT_EQUALS(false, library.isFloatArgValid(tokenList.front(), 1, 0.0));
ASSERT_EQUALS(true, library.isIntArgValid(tokenList.front(), 1, 1));
ASSERT_EQUALS(true, library.isFloatArgValid(tokenList.front(), 1, 1.0));
ASSERT_EQUALS(true, library.isIntArgValid(tokenList.front(), 1, 10));
ASSERT_EQUALS(true, library.isFloatArgValid(tokenList.front(), 1, 10.0));
// -7-0
ASSERT_EQUALS(false, library.isargvalid(tokenList.front(), 2, static_cast<MathLib::bigint>(-10)));
ASSERT_EQUALS(false, library.isargvalid(tokenList.front(), 2, -10.0));
ASSERT_EQUALS(false, library.isargvalid(tokenList.front(), 2, -7.5));
ASSERT_EQUALS(false, library.isargvalid(tokenList.front(), 2, -7.1));
ASSERT_EQUALS(true, library.isargvalid(tokenList.front(), 2, static_cast<MathLib::bigint>(-7)));
ASSERT_EQUALS(true, library.isargvalid(tokenList.front(), 2, -7.0));
ASSERT_EQUALS(true, library.isargvalid(tokenList.front(), 2, static_cast<MathLib::bigint>(-3)));
ASSERT_EQUALS(true, library.isargvalid(tokenList.front(), 2, -3.0));
ASSERT_EQUALS(true, library.isargvalid(tokenList.front(), 2, -3.5));
ASSERT_EQUALS(true, library.isargvalid(tokenList.front(), 2, static_cast<MathLib::bigint>(0)));
ASSERT_EQUALS(true, library.isargvalid(tokenList.front(), 2, 0.0));
ASSERT_EQUALS(false, library.isargvalid(tokenList.front(), 2, 0.5));
ASSERT_EQUALS(false, library.isargvalid(tokenList.front(), 2, static_cast<MathLib::bigint>(1)));
ASSERT_EQUALS(false, library.isargvalid(tokenList.front(), 2, 1.0));
ASSERT_EQUALS(false, library.isIntArgValid(tokenList.front(), 2, -10));
ASSERT_EQUALS(false, library.isFloatArgValid(tokenList.front(), 2, -10.0));
ASSERT_EQUALS(false, library.isFloatArgValid(tokenList.front(), 2, -7.5));
ASSERT_EQUALS(false, library.isFloatArgValid(tokenList.front(), 2, -7.1));
ASSERT_EQUALS(true, library.isIntArgValid(tokenList.front(), 2, -7));
ASSERT_EQUALS(true, library.isFloatArgValid(tokenList.front(), 2, -7.0));
ASSERT_EQUALS(true, library.isIntArgValid(tokenList.front(), 2, -3));
ASSERT_EQUALS(true, library.isFloatArgValid(tokenList.front(), 2, -3.0));
ASSERT_EQUALS(true, library.isFloatArgValid(tokenList.front(), 2, -3.5));
ASSERT_EQUALS(true, library.isIntArgValid(tokenList.front(), 2, 0));
ASSERT_EQUALS(true, library.isFloatArgValid(tokenList.front(), 2, 0.0));
ASSERT_EQUALS(false, library.isFloatArgValid(tokenList.front(), 2, 0.5));
ASSERT_EQUALS(false, library.isIntArgValid(tokenList.front(), 2, 1));
ASSERT_EQUALS(false, library.isFloatArgValid(tokenList.front(), 2, 1.0));
// 1-5,8
ASSERT_EQUALS(false, library.isargvalid(tokenList.front(), 3, static_cast<MathLib::bigint>(0)));
ASSERT_EQUALS(false, library.isargvalid(tokenList.front(), 3, 0.0));
ASSERT_EQUALS(true, library.isargvalid(tokenList.front(), 3, static_cast<MathLib::bigint>(1)));
ASSERT_EQUALS(true, library.isargvalid(tokenList.front(), 3, 1.0));
ASSERT_EQUALS(true, library.isargvalid(tokenList.front(), 3, static_cast<MathLib::bigint>(3)));
ASSERT_EQUALS(true, library.isargvalid(tokenList.front(), 3, 3.0));
ASSERT_EQUALS(true, library.isargvalid(tokenList.front(), 3, static_cast<MathLib::bigint>(5)));
ASSERT_EQUALS(true, library.isargvalid(tokenList.front(), 3, 5.0));
ASSERT_EQUALS(false, library.isargvalid(tokenList.front(), 3, static_cast<MathLib::bigint>(6)));
ASSERT_EQUALS(false, library.isargvalid(tokenList.front(), 3, 6.0));
ASSERT_EQUALS(false, library.isargvalid(tokenList.front(), 3, static_cast<MathLib::bigint>(7)));
ASSERT_EQUALS(false, library.isargvalid(tokenList.front(), 3, 7.0));
ASSERT_EQUALS(true, library.isargvalid(tokenList.front(), 3, static_cast<MathLib::bigint>(8)));
ASSERT_EQUALS(false, library.isargvalid(tokenList.front(), 3, 8.0));
ASSERT_EQUALS(false, library.isargvalid(tokenList.front(), 3, static_cast<MathLib::bigint>(9)));
ASSERT_EQUALS(false, library.isargvalid(tokenList.front(), 3, 9.0));
ASSERT_EQUALS(false, library.isIntArgValid(tokenList.front(), 3, 0));
ASSERT_EQUALS(false, library.isFloatArgValid(tokenList.front(), 3, 0.0));
ASSERT_EQUALS(true, library.isIntArgValid(tokenList.front(), 3, 1));
ASSERT_EQUALS(true, library.isFloatArgValid(tokenList.front(), 3, 1.0));
ASSERT_EQUALS(true, library.isIntArgValid(tokenList.front(), 3, 3));
ASSERT_EQUALS(true, library.isFloatArgValid(tokenList.front(), 3, 3.0));
ASSERT_EQUALS(true, library.isIntArgValid(tokenList.front(), 3, 5));
ASSERT_EQUALS(true, library.isFloatArgValid(tokenList.front(), 3, 5.0));
ASSERT_EQUALS(false, library.isIntArgValid(tokenList.front(), 3, 6));
ASSERT_EQUALS(false, library.isFloatArgValid(tokenList.front(), 3, 6.0));
ASSERT_EQUALS(false, library.isIntArgValid(tokenList.front(), 3, 7));
ASSERT_EQUALS(false, library.isFloatArgValid(tokenList.front(), 3, 7.0));
ASSERT_EQUALS(true, library.isIntArgValid(tokenList.front(), 3, 8));
ASSERT_EQUALS(false, library.isFloatArgValid(tokenList.front(), 3, 8.0));
ASSERT_EQUALS(false, library.isIntArgValid(tokenList.front(), 3, 9));
ASSERT_EQUALS(false, library.isFloatArgValid(tokenList.front(), 3, 9.0));
// -1,5
ASSERT_EQUALS(false, library.isargvalid(tokenList.front(), 4, static_cast<MathLib::bigint>(-10)));
ASSERT_EQUALS(false, library.isargvalid(tokenList.front(), 4, -10.0));
ASSERT_EQUALS(true, library.isargvalid(tokenList.front(), 4, static_cast<MathLib::bigint>(-1)));
ASSERT_EQUALS(false, library.isargvalid(tokenList.front(), 4, -1.0));
ASSERT_EQUALS(false, library.isargvalid(tokenList.front(), 4, 5.000001));
ASSERT_EQUALS(false, library.isargvalid(tokenList.front(), 4, 5.5));
ASSERT_EQUALS(false, library.isIntArgValid(tokenList.front(), 4, -10));
ASSERT_EQUALS(false, library.isFloatArgValid(tokenList.front(), 4, -10.0));
ASSERT_EQUALS(true, library.isIntArgValid(tokenList.front(), 4, -1));
ASSERT_EQUALS(false, library.isFloatArgValid(tokenList.front(), 4, -1.0));
ASSERT_EQUALS(false, library.isFloatArgValid(tokenList.front(), 4, 5.000001));
ASSERT_EQUALS(false, library.isFloatArgValid(tokenList.front(), 4, 5.5));
// :1,5
ASSERT_EQUALS(true, library.isargvalid(tokenList.front(), 5, static_cast<MathLib::bigint>(-10)));
ASSERT_EQUALS(true, library.isargvalid(tokenList.front(), 5, -10.0));
ASSERT_EQUALS(true, library.isargvalid(tokenList.front(), 5, static_cast<MathLib::bigint>(1)));
ASSERT_EQUALS(true, library.isargvalid(tokenList.front(), 5, 1.0));
ASSERT_EQUALS(false, library.isargvalid(tokenList.front(), 5, static_cast<MathLib::bigint>(2)));
ASSERT_EQUALS(false, library.isargvalid(tokenList.front(), 5, 2.0));
ASSERT_EQUALS(true, library.isIntArgValid(tokenList.front(), 5, -10));
ASSERT_EQUALS(true, library.isFloatArgValid(tokenList.front(), 5, -10.0));
ASSERT_EQUALS(true, library.isIntArgValid(tokenList.front(), 5, 1));
ASSERT_EQUALS(true, library.isFloatArgValid(tokenList.front(), 5, 1.0));
ASSERT_EQUALS(false, library.isIntArgValid(tokenList.front(), 5, 2));
ASSERT_EQUALS(false, library.isFloatArgValid(tokenList.front(), 5, 2.0));
// 1.5:
ASSERT_EQUALS(false, library.isargvalid(tokenList.front(), 6, static_cast<MathLib::bigint>(0)));
ASSERT_EQUALS(false, library.isargvalid(tokenList.front(), 6, 0.0));
ASSERT_EQUALS(false, library.isargvalid(tokenList.front(), 6, static_cast<MathLib::bigint>(1)));
ASSERT_EQUALS(false, library.isargvalid(tokenList.front(), 6, 1.499999));
ASSERT_EQUALS(true, library.isargvalid(tokenList.front(), 6, 1.5));
ASSERT_EQUALS(true, library.isargvalid(tokenList.front(), 6, static_cast<MathLib::bigint>(2)));
ASSERT_EQUALS(true, library.isargvalid(tokenList.front(), 6, static_cast<MathLib::bigint>(10)));
ASSERT_EQUALS(false, library.isIntArgValid(tokenList.front(), 6, 0));
ASSERT_EQUALS(false, library.isFloatArgValid(tokenList.front(), 6, 0.0));
ASSERT_EQUALS(false, library.isIntArgValid(tokenList.front(), 6, 1));
ASSERT_EQUALS(false, library.isFloatArgValid(tokenList.front(), 6, 1.499999));
ASSERT_EQUALS(true, library.isFloatArgValid(tokenList.front(), 6, 1.5));
ASSERT_EQUALS(true, library.isIntArgValid(tokenList.front(), 6, 2));
ASSERT_EQUALS(true, library.isIntArgValid(tokenList.front(), 6, 10));
// -6.7:-5.5,-3.3:-2.7
ASSERT_EQUALS(false, library.isargvalid(tokenList.front(), 7, static_cast<MathLib::bigint>(-7)));
ASSERT_EQUALS(false, library.isargvalid(tokenList.front(), 7, -7.0));
ASSERT_EQUALS(false, library.isargvalid(tokenList.front(), 7, -6.7000001));
ASSERT_EQUALS(true, library.isargvalid(tokenList.front(), 7, -6.7));
ASSERT_EQUALS(true, library.isargvalid(tokenList.front(), 7, static_cast<MathLib::bigint>(-6)));
ASSERT_EQUALS(true, library.isargvalid(tokenList.front(), 7, -6.0));
ASSERT_EQUALS(true, library.isargvalid(tokenList.front(), 7, -5.5));
ASSERT_EQUALS(false, library.isargvalid(tokenList.front(), 7, -5.4999999));
ASSERT_EQUALS(false, library.isargvalid(tokenList.front(), 7, -3.3000001));
ASSERT_EQUALS(true, library.isargvalid(tokenList.front(), 7, -3.3));
ASSERT_EQUALS(true, library.isargvalid(tokenList.front(), 7, static_cast<MathLib::bigint>(-3)));
ASSERT_EQUALS(true, library.isargvalid(tokenList.front(), 7, -3.0));
ASSERT_EQUALS(true, library.isargvalid(tokenList.front(), 7, -2.7));
ASSERT_EQUALS(false, library.isargvalid(tokenList.front(), 7, -2.6999999));
ASSERT_EQUALS(false, library.isargvalid(tokenList.front(), 7, static_cast<MathLib::bigint>(-2)));
ASSERT_EQUALS(false, library.isargvalid(tokenList.front(), 7, -2.0));
ASSERT_EQUALS(false, library.isargvalid(tokenList.front(), 7, static_cast<MathLib::bigint>(0)));
ASSERT_EQUALS(false, library.isargvalid(tokenList.front(), 7, 0.0));
ASSERT_EQUALS(false, library.isargvalid(tokenList.front(), 7, static_cast<MathLib::bigint>(3)));
ASSERT_EQUALS(false, library.isargvalid(tokenList.front(), 7, 3.0));
ASSERT_EQUALS(false, library.isargvalid(tokenList.front(), 7, static_cast<MathLib::bigint>(6)));
ASSERT_EQUALS(false, library.isargvalid(tokenList.front(), 7, 6.0));
ASSERT_EQUALS(false, library.isIntArgValid(tokenList.front(), 7, -7));
ASSERT_EQUALS(false, library.isFloatArgValid(tokenList.front(), 7, -7.0));
ASSERT_EQUALS(false, library.isFloatArgValid(tokenList.front(), 7, -6.7000001));
ASSERT_EQUALS(true, library.isFloatArgValid(tokenList.front(), 7, -6.7));
ASSERT_EQUALS(true, library.isIntArgValid(tokenList.front(), 7, -6));
ASSERT_EQUALS(true, library.isFloatArgValid(tokenList.front(), 7, -6.0));
ASSERT_EQUALS(true, library.isFloatArgValid(tokenList.front(), 7, -5.5));
ASSERT_EQUALS(false, library.isFloatArgValid(tokenList.front(), 7, -5.4999999));
ASSERT_EQUALS(false, library.isFloatArgValid(tokenList.front(), 7, -3.3000001));
ASSERT_EQUALS(true, library.isFloatArgValid(tokenList.front(), 7, -3.3));
ASSERT_EQUALS(true, library.isIntArgValid(tokenList.front(), 7, -3));
ASSERT_EQUALS(true, library.isFloatArgValid(tokenList.front(), 7, -3.0));
ASSERT_EQUALS(true, library.isFloatArgValid(tokenList.front(), 7, -2.7));
ASSERT_EQUALS(false, library.isFloatArgValid(tokenList.front(), 7, -2.6999999));
ASSERT_EQUALS(false, library.isIntArgValid(tokenList.front(), 7, -2));
ASSERT_EQUALS(false, library.isFloatArgValid(tokenList.front(), 7, -2.0));
ASSERT_EQUALS(false, library.isIntArgValid(tokenList.front(), 7, 0));
ASSERT_EQUALS(false, library.isFloatArgValid(tokenList.front(), 7, 0.0));
ASSERT_EQUALS(false, library.isIntArgValid(tokenList.front(), 7, 3));
ASSERT_EQUALS(false, library.isFloatArgValid(tokenList.front(), 7, 3.0));
ASSERT_EQUALS(false, library.isIntArgValid(tokenList.front(), 7, 6));
ASSERT_EQUALS(false, library.isFloatArgValid(tokenList.front(), 7, 6.0));
// 0.0:
ASSERT_EQUALS(false, library.isargvalid(tokenList.front(), 8, static_cast<MathLib::bigint>(-1)));
ASSERT_EQUALS(false, library.isargvalid(tokenList.front(), 8, -1.0));
ASSERT_EQUALS(false, library.isargvalid(tokenList.front(), 8, -0.00000001));
ASSERT_EQUALS(true, library.isargvalid(tokenList.front(), 8, static_cast<MathLib::bigint>(0)));
ASSERT_EQUALS(true, library.isargvalid(tokenList.front(), 8, 0.0));
ASSERT_EQUALS(true, library.isargvalid(tokenList.front(), 8, 0.000000001));
ASSERT_EQUALS(true, library.isargvalid(tokenList.front(), 8, static_cast<MathLib::bigint>(1)));
ASSERT_EQUALS(true, library.isargvalid(tokenList.front(), 8, 1.0));
ASSERT_EQUALS(false, library.isIntArgValid(tokenList.front(), 8, -1));
ASSERT_EQUALS(false, library.isFloatArgValid(tokenList.front(), 8, -1.0));
ASSERT_EQUALS(false, library.isFloatArgValid(tokenList.front(), 8, -0.00000001));
ASSERT_EQUALS(true, library.isIntArgValid(tokenList.front(), 8, 0));
ASSERT_EQUALS(true, library.isFloatArgValid(tokenList.front(), 8, 0.0));
ASSERT_EQUALS(true, library.isFloatArgValid(tokenList.front(), 8, 0.000000001));
ASSERT_EQUALS(true, library.isIntArgValid(tokenList.front(), 8, 1));
ASSERT_EQUALS(true, library.isFloatArgValid(tokenList.front(), 8, 1.0));
// :2.0
ASSERT_EQUALS(true, library.isargvalid(tokenList.front(), 9, static_cast<MathLib::bigint>(-1)));
ASSERT_EQUALS(true, library.isargvalid(tokenList.front(), 9, -1.0));
ASSERT_EQUALS(true, library.isargvalid(tokenList.front(), 9, static_cast<MathLib::bigint>(2)));
ASSERT_EQUALS(true, library.isargvalid(tokenList.front(), 9, 2.0));
ASSERT_EQUALS(false, library.isargvalid(tokenList.front(), 9, 2.00000001));
ASSERT_EQUALS(false, library.isargvalid(tokenList.front(), 9, static_cast<MathLib::bigint>(200)));
ASSERT_EQUALS(false, library.isargvalid(tokenList.front(), 9, 200.0));
ASSERT_EQUALS(true, library.isIntArgValid(tokenList.front(), 9, -1));
ASSERT_EQUALS(true, library.isFloatArgValid(tokenList.front(), 9, -1.0));
ASSERT_EQUALS(true, library.isIntArgValid(tokenList.front(), 9, 2));
ASSERT_EQUALS(true, library.isFloatArgValid(tokenList.front(), 9, 2.0));
ASSERT_EQUALS(false, library.isFloatArgValid(tokenList.front(), 9, 2.00000001));
ASSERT_EQUALS(false, library.isIntArgValid(tokenList.front(), 9, 200));
ASSERT_EQUALS(false, library.isFloatArgValid(tokenList.front(), 9, 200.0));
// 0.0
ASSERT_EQUALS(false, library.isargvalid(tokenList.front(), 10, static_cast<MathLib::bigint>(0)));
ASSERT_EQUALS(false, library.isargvalid(tokenList.front(), 10, 0.0));
ASSERT_EQUALS(false, library.isIntArgValid(tokenList.front(), 10, 0));
ASSERT_EQUALS(false, library.isFloatArgValid(tokenList.front(), 10, 0.0));
}
void function_arg_minsize() const {