|
33 | 33 | #include "utils.h" |
34 | 34 | #include "valueflow.h" |
35 | 35 | #include "valueptr.h" |
| 36 | +#include "vf_common.h" |
36 | 37 | #include "vfvalue.h" |
37 | 38 |
|
38 | 39 | #include "checkclass.h" |
|
42 | 43 | #include <functional> |
43 | 44 | #include <initializer_list> |
44 | 45 | #include <iterator> |
| 46 | +#include <limits> |
45 | 47 | #include <list> |
46 | 48 | #include <set> |
47 | 49 | #include <type_traits> |
@@ -835,6 +837,17 @@ bool astIsRHS(const Token* tok) |
835 | 837 | return parent->astOperand2() == tok; |
836 | 838 | } |
837 | 839 |
|
| 840 | +std::string invertOperatorForOperandSwap(std::string op) |
| 841 | +{ |
| 842 | + if (op.empty()) |
| 843 | + return op; |
| 844 | + if (op[0] == '<') |
| 845 | + op[0] = '>'; |
| 846 | + else if (op[0] == '>') |
| 847 | + op[0] = '<'; |
| 848 | + return op; |
| 849 | +} |
| 850 | + |
838 | 851 | template<class T, REQUIRES("T must be a Token class", std::is_convertible<T*, const Token*> )> |
839 | 852 | static T* getCondTokImpl(T* tok) |
840 | 853 | { |
@@ -952,56 +965,66 @@ const Token *findNextTokenFromBreak(const Token *breakToken) |
952 | 965 | return nullptr; |
953 | 966 | } |
954 | 967 |
|
955 | | -bool extractForLoopValues(const Token *forToken, |
956 | | - nonneg int &varid, |
957 | | - bool &knownInitValue, |
958 | | - MathLib::bigint &initValue, |
959 | | - bool &partialCond, |
960 | | - MathLib::bigint &stepValue, |
961 | | - MathLib::bigint &lastValue) |
| 968 | +ForLoopValues extractForLoopValues(const Token *forToken) |
962 | 969 | { |
| 970 | + ForLoopValues values; |
963 | 971 | if (!Token::simpleMatch(forToken, "for (") || !Token::simpleMatch(forToken->next()->astOperand2(), ";")) |
964 | | - return false; |
| 972 | + return values; |
965 | 973 | const Token *initExpr = forToken->next()->astOperand2()->astOperand1(); |
966 | 974 | const Token *condExpr = forToken->next()->astOperand2()->astOperand2()->astOperand1(); |
967 | 975 | const Token *incExpr = forToken->next()->astOperand2()->astOperand2()->astOperand2(); |
968 | 976 | if (!initExpr || !initExpr->isBinaryOp() || initExpr->str() != "=" || !Token::Match(initExpr->astOperand1(), "%var%")) |
969 | | - return false; |
970 | | - std::vector<MathLib::bigint> minInitValue = |
971 | | - getMinValue(makeIntegralInferModel(), initExpr->astOperand2()->values()); |
972 | | - if (minInitValue.empty()) { |
973 | | - const ValueFlow::Value* v = initExpr->astOperand2()->getMinValue(true); |
974 | | - if (v) |
975 | | - minInitValue.push_back(v->intvalue); |
| 977 | + return values; |
| 978 | + const Token* const initToken = initExpr->astOperand2(); |
| 979 | + const std::vector<MathLib::bigint> minInitValue = |
| 980 | + getMinValue(makeIntegralInferModel(), initToken->values()); |
| 981 | + values.initValueSource = getMinValueSource(makeIntegralInferModel(), initToken->values()); |
| 982 | + values.hasMinimum = !minInitValue.empty(); |
| 983 | + if (values.hasMinimum) { |
| 984 | + values.knownInitValue = initToken->hasKnownIntValue(); |
| 985 | + values.initValue = minInitValue.front(); |
| 986 | + if (values.knownInitValue && values.initValueSource && |
| 987 | + values.initValueSource->bound != ValueFlow::Value::Bound::Point) { |
| 988 | + const auto it = std::find_if(initToken->values().cbegin(), |
| 989 | + initToken->values().cend(), |
| 990 | + [&](const ValueFlow::Value& value) { |
| 991 | + return value.isIntValue() && !value.isImpossible() && !value.isInconclusive() && |
| 992 | + value.bound == ValueFlow::Value::Bound::Point && value.intvalue == values.initValue; |
| 993 | + }); |
| 994 | + if (it != initToken->values().cend()) |
| 995 | + values.initValueSource = &*it; |
| 996 | + } |
976 | 997 | } |
977 | | - if (minInitValue.empty()) |
978 | | - return false; |
979 | | - varid = initExpr->astOperand1()->varId(); |
980 | | - knownInitValue = initExpr->astOperand2()->hasKnownIntValue(); |
981 | | - initValue = minInitValue.front(); |
982 | | - partialCond = Token::Match(condExpr, "%oror%|&&"); |
983 | | - visitAstNodes(condExpr, [varid, &condExpr](const Token *tok) { |
| 998 | + values.varid = initExpr->astOperand1()->varId(); |
| 999 | + values.partialCond = Token::Match(condExpr, "%oror%|&&"); |
| 1000 | + visitAstNodes(condExpr, [&](const Token *tok) { |
984 | 1001 | if (Token::Match(tok, "%oror%|&&")) |
985 | 1002 | return ChildrenToVisit::op1_and_op2; |
986 | | - if (Token::Match(tok, "<|<=") && tok->isBinaryOp() && tok->astOperand1()->varId() == varid && tok->astOperand2()->hasKnownIntValue()) { |
| 1003 | + if (Token::Match(tok, "<|<=") && tok->isBinaryOp() && tok->astOperand1()->varId() == values.varid && |
| 1004 | + tok->astOperand2()->hasKnownIntValue()) { |
987 | 1005 | if (Token::Match(condExpr, "%oror%|&&") || tok->astOperand2()->getKnownIntValue() < condExpr->astOperand2()->getKnownIntValue()) |
988 | 1006 | condExpr = tok; |
989 | 1007 | } |
990 | 1008 | return ChildrenToVisit::none; |
991 | 1009 | }); |
992 | | - if (!Token::Match(condExpr, "<|<=") || !condExpr->isBinaryOp() || condExpr->astOperand1()->varId() != varid || !condExpr->astOperand2()->hasKnownIntValue()) |
993 | | - return false; |
994 | | - if (!incExpr || !incExpr->isUnaryOp("++") || incExpr->astOperand1()->varId() != varid) |
995 | | - return false; |
996 | | - stepValue = 1; |
997 | | - if (condExpr->str() == "<") |
998 | | - lastValue = condExpr->astOperand2()->getKnownIntValue() - 1; |
999 | | - else |
1000 | | - lastValue = condExpr->astOperand2()->getKnownIntValue(); |
1001 | | - return true; |
| 1010 | + if (!Token::Match(condExpr, "<|<=") || !condExpr->isBinaryOp() || condExpr->astOperand1()->varId() != values.varid || |
| 1011 | + !condExpr->astOperand2()->hasKnownIntValue()) |
| 1012 | + return values; |
| 1013 | + if (!incExpr || !incExpr->isUnaryOp("++") || incExpr->astOperand1()->varId() != values.varid) |
| 1014 | + return values; |
| 1015 | + values.stepValue = 1; |
| 1016 | + if (condExpr->str() == "<") { |
| 1017 | + const MathLib::bigint limit = condExpr->astOperand2()->getKnownIntValue(); |
| 1018 | + if (limit == std::numeric_limits<MathLib::bigint>::min()) |
| 1019 | + return values; |
| 1020 | + values.lastValue = limit - 1; |
| 1021 | + } else { |
| 1022 | + values.lastValue = condExpr->astOperand2()->getKnownIntValue(); |
| 1023 | + } |
| 1024 | + values.valid = true; |
| 1025 | + return values; |
1002 | 1026 | } |
1003 | 1027 |
|
1004 | | - |
1005 | 1028 | static const Token * getVariableInitExpression(const Variable * var) |
1006 | 1029 | { |
1007 | 1030 | if (!var) |
@@ -1476,6 +1499,22 @@ static inline bool isDifferentKnownValues(const Token * const tok1, const Token |
1476 | 1499 | }); |
1477 | 1500 | } |
1478 | 1501 |
|
| 1502 | +// Like isDifferentKnownValues(), but the two constants are read as they compare against 'exprTok'. |
| 1503 | +static bool isDifferentKnownValues(const Token* tok1, const Token* tok2, const Token* exprTok, const Settings& settings) |
| 1504 | +{ |
| 1505 | + return compareKnownValue(tok1, tok2, [&](const ValueFlow::Value& v1, const ValueFlow::Value& v2, bool sameLifetime) { |
| 1506 | + if (!v1.isIntValue() || !v2.isIntValue()) { |
| 1507 | + bool equal = v1.equalValue(v2); |
| 1508 | + if (v1.isIteratorValue()) |
| 1509 | + equal &= sameLifetime; |
| 1510 | + return !equal; |
| 1511 | + } |
| 1512 | + const MathLib::bigint value1 = ValueFlow::castConstantToOperandType(tok1, exprTok, v1.intvalue, settings); |
| 1513 | + const MathLib::bigint value2 = ValueFlow::castConstantToOperandType(tok2, exprTok, v2.intvalue, settings); |
| 1514 | + return value1 != value2; |
| 1515 | + }); |
| 1516 | +} |
| 1517 | + |
1479 | 1518 | static inline bool isSameConstantValue(bool macro, const Token* tok1, const Token* tok2) |
1480 | 1519 | { |
1481 | 1520 | if (tok1 == nullptr || tok2 == nullptr) |
@@ -1903,9 +1942,9 @@ bool isOppositeCond(bool isNot, const Token * const cond1, const Token * const c |
1903 | 1942 | if (!isNot) { |
1904 | 1943 | if (cond1->str() == "==" && cond2->str() == "==") { |
1905 | 1944 | if (isSameExpression(true, cond1->astOperand1(), cond2->astOperand1(), settings, pure, followVar, errors)) |
1906 | | - return isDifferentKnownValues(cond1->astOperand2(), cond2->astOperand2()); |
| 1945 | + return isDifferentKnownValues(cond1->astOperand2(), cond2->astOperand2(), cond1->astOperand1(), settings); |
1907 | 1946 | if (isSameExpression(true, cond1->astOperand2(), cond2->astOperand2(), settings, pure, followVar, errors)) |
1908 | | - return isDifferentKnownValues(cond1->astOperand1(), cond2->astOperand1()); |
| 1947 | + return isDifferentKnownValues(cond1->astOperand1(), cond2->astOperand1(), cond1->astOperand2(), settings); |
1909 | 1948 | } |
1910 | 1949 |
|
1911 | 1950 | if (Library::isContainerYield(cond1, Library::Container::Yield::EMPTY, "empty") && |
@@ -2011,10 +2050,16 @@ bool isOppositeCond(bool isNot, const Token * const cond1, const Token * const c |
2011 | 2050 | const ValueFlow::Value &rhsValue1 = value1->values().front(); |
2012 | 2051 | const ValueFlow::Value &rhsValue2 = value2->values().front(); |
2013 | 2052 |
|
| 2053 | + // The operands are compared in their common type. When one side is an unsigned constant and the |
| 2054 | + // other a signed expression, the comparison happens in the unsigned domain, so the constant must be |
| 2055 | + // read as that expression's type would see it: 0xFFFFFFFFu is -1 for an 'int' operand. |
| 2056 | + const MathLib::bigint bound1 = ValueFlow::castConstantToOperandType(value1, expr1, rhsValue1.intvalue, settings); |
| 2057 | + const MathLib::bigint bound2 = ValueFlow::castConstantToOperandType(value2, expr2, rhsValue2.intvalue, settings); |
| 2058 | + |
2014 | 2059 | if (op1 == "<" || op1 == "<=") |
2015 | | - return (op2 == "==" || op2 == ">" || op2 == ">=") && (rhsValue1.intvalue < rhsValue2.intvalue); |
| 2060 | + return (op2 == "==" || op2 == ">" || op2 == ">=") && (bound1 < bound2); |
2016 | 2061 | if (op1 == ">=" || op1 == ">") |
2017 | | - return (op2 == "==" || op2 == "<" || op2 == "<=") && (rhsValue1.intvalue > rhsValue2.intvalue); |
| 2062 | + return (op2 == "==" || op2 == "<" || op2 == "<=") && (bound1 > bound2); |
2018 | 2063 |
|
2019 | 2064 | return false; |
2020 | 2065 | } |
@@ -3934,6 +3979,20 @@ const Token *skipUnreachableBranch(const Token *tok) |
3934 | 3979 | return tok; |
3935 | 3980 | } |
3936 | 3981 |
|
| 3982 | +bool isInUnreachableBranch(const Token *tok) |
| 3983 | +{ |
| 3984 | + for (const Scope *scope = tok->scope(); scope; scope = scope->nestedIn) { |
| 3985 | + if (scope->type != ScopeType::eIf && scope->type != ScopeType::eElse) |
| 3986 | + continue; |
| 3987 | + const Token *body = scope->bodyStart; |
| 3988 | + if (!body || body->str() != "{" || scope->bodyEnd != body->link()) |
| 3989 | + continue; |
| 3990 | + if (skipUnreachableBranch(body) != body) |
| 3991 | + return true; |
| 3992 | + } |
| 3993 | + return false; |
| 3994 | +} |
| 3995 | + |
3937 | 3996 | bool isEscapeKeyword(const Token *tok, const Settings &settings) |
3938 | 3997 | { |
3939 | 3998 | if (!tok) |
|
0 commit comments