Exemplo n.º 1
0
void CheckAssignIf::comparison()
{
    if (!_settings->isEnabled("style"))
        return;

    for (const Token *tok = _tokenizer->tokens(); tok; tok = tok->next()) {
        if (Token::Match(tok, "&|%or% %num% )| ==|!= %num% &&|%oror%|)")) {
            const MathLib::bigint num1 = MathLib::toLongNumber(tok->strAt(1));
            if (num1 < 0)
                continue;

            const Token *compareToken = tok->tokAt(2);
            if (compareToken->str() == ")") {
                if (!Token::Match(compareToken->link()->previous(), "(|%oror%|&&"))
                    continue;
                compareToken = compareToken->next();
            }

            const MathLib::bigint num2 = MathLib::toLongNumber(compareToken->strAt(1));
            if (num2 < 0)
                continue;

            if ((tok->str() == "&" && (num1 & num2) != num2) ||
                (tok->str() == "|" && (num1 | num2) != num2)) {
                const std::string& op(compareToken->str());
                comparisonError(tok, tok->str(), num1, op, num2, op=="==" ? false : true);
            }
        }
    }
}
Exemplo n.º 2
0
void CheckCondition::comparison()
{
    if (!_settings->isEnabled("style"))
        return;

    for (const Token *tok = _tokenizer->tokens(); tok; tok = tok->next()) {
        if (Token::Match(tok, "==|!=|>|>=|<|<=")) {
            const Token *expr1 = tok->astOperand1();
            const Token *expr2 = tok->astOperand2();
            if (!expr1 || !expr2)
                continue;
            if (expr1->isNumber())
                std::swap(expr1,expr2);
            if (!expr2->isNumber())
                continue;
            const MathLib::bigint num2 = MathLib::toLongNumber(expr2->str());
            if (num2 < 0)
                continue;
            if (!Token::Match(expr1,"[&|]"))
                continue;
            std::list<MathLib::bigint> numbers;
            getnumchildren(expr1, numbers);
            for (std::list<MathLib::bigint>::const_iterator num = numbers.begin(); num != numbers.end(); ++num) {
                const MathLib::bigint num1 = *num;
                if (num1 < 0)
                    continue;
                if (Token::Match(tok, "==|!=|>=|<=")) {
                    if ((expr1->str() == "&" && (num1 & num2) != num2) ||
                        (expr1->str() == "|" && (num1 | num2) != num2)) {
                        const std::string& op(tok->str());
                        comparisonError(expr1, expr1->str(), num1, op, num2, op!="!=" ? false : true);
                    }
                } else if (Token::simpleMatch(tok, ">")) {
                    if ((expr1->str() == "&" && (num1 <= num2))) {
                        const std::string& op(tok->str());
                        comparisonError(expr1, expr1->str(), num1, op, num2, false);
                    }
                } else if (Token::simpleMatch(tok, "<")) {
                    if ((expr1->str() == "|" && (num1 >= num2))) {
                        const std::string& op(tok->str());
                        comparisonError(expr1, expr1->str(), num1, op, num2, false);
                    }
                }
            }
        }
    }
}
Exemplo n.º 3
0
void CheckCondition::comparison()
{
    if (!_settings->isEnabled("style"))
        return;

    for (const Token *tok = _tokenizer->tokens(); tok; tok = tok->next()) {
        if (!tok->isComparisonOp())
            continue;

        const Token *expr1 = tok->astOperand1();
        const Token *expr2 = tok->astOperand2();
        if (!expr1 || !expr2)
            continue;
        if (expr1->isNumber())
            std::swap(expr1,expr2);
        if (!expr2->isNumber())
            continue;
        const MathLib::bigint num2 = MathLib::toLongNumber(expr2->str());
        if (num2 < 0)
            continue;
        if (!Token::Match(expr1,"[&|]"))
            continue;
        std::list<MathLib::bigint> numbers;
        getnumchildren(expr1, numbers);
        for (std::list<MathLib::bigint>::const_iterator num = numbers.begin(); num != numbers.end(); ++num) {
            const MathLib::bigint num1 = *num;
            if (num1 < 0)
                continue;
            if (Token::Match(tok, "==|!=")) {
                if ((expr1->str() == "&" && (num1 & num2) != num2) ||
                    (expr1->str() == "|" && (num1 | num2) != num2)) {
                    const std::string& op(tok->str());
                    comparisonError(expr1, expr1->str(), num1, op, num2, op=="==" ? false : true);
                }
            } else if (expr1->str() == "&") {
                const bool or_equal = Token::Match(tok, ">=|<=");
                const std::string& op(tok->str());
                if ((Token::Match(tok, ">=|<")) && (num1 < num2)) {
                    comparisonError(expr1, expr1->str(), num1, op, num2, or_equal ? false : true);
                } else if ((Token::Match(tok, "<=|>")) && (num1 <= num2)) {
                    comparisonError(expr1, expr1->str(), num1, op, num2, or_equal ? true : false);
                }
            } else if (expr1->str() == "|") {
                if ((expr1->astOperand1()->valueType()) &&
                    (expr1->astOperand1()->valueType()->sign == ValueType::Sign::UNSIGNED)) {
                    const bool or_equal = Token::Match(tok, ">=|<=");
                    const std::string& op(tok->str());
                    if ((Token::Match(tok, ">=|<")) && (num1 >= num2)) {
                        //"(a | 0x07) >= 7U" is always true for unsigned a
                        //"(a | 0x07) < 7U" is always false for unsigned a
                        comparisonError(expr1, expr1->str(), num1, op, num2, or_equal ? true : false);
                    } else if ((Token::Match(tok, "<=|>")) && (num1 > num2)) {
                        //"(a | 0x08) <= 7U" is always false for unsigned a
                        //"(a | 0x07) > 6U" is always true for unsigned a
                        comparisonError(expr1, expr1->str(), num1, op, num2, or_equal ? false : true);
                    }
                }
            }
        }
    }
}