Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion .selfcheck_suppressions
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,6 @@ functionStatic:*/gui/ui_*.h
# --debug-warnings suppressions
valueFlowBailout
valueFlowBailoutIncompleteVar
valueFlowMaxIterations:gui/resultstree.cpp
autoNoType:externals/simplecpp/simplecpp.cpp
autoNoType:cli/*.cpp
autoNoType:lib/*.cpp
Expand Down
10 changes: 5 additions & 5 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -499,7 +499,7 @@ $(libcppdir)/addoninfo.o: lib/addoninfo.cpp externals/picojson/picojson.h lib/ad
$(libcppdir)/analyzerinfo.o: lib/analyzerinfo.cpp externals/tinyxml2/tinyxml2.h lib/analyzerinfo.h lib/config.h lib/errorlogger.h lib/errortypes.h lib/filesettings.h lib/mathlib.h lib/path.h lib/platform.h lib/refthunk.h lib/standards.h lib/utils.h lib/xml.h
$(CXX) ${INCLUDE_FOR_LIB} $(CPPFLAGS) $(CXXFLAGS) -c -o $@ $(libcppdir)/analyzerinfo.cpp

$(libcppdir)/astutils.o: lib/astutils.cpp lib/astutils.h lib/check.h lib/checkclass.h lib/checkers.h lib/checkimpl.h lib/config.h lib/errortypes.h lib/findtoken.h lib/infer.h lib/library.h lib/mathlib.h lib/platform.h lib/refthunk.h lib/settings.h lib/smallvector.h lib/sourcelocation.h lib/standards.h lib/symboldatabase.h lib/templatesimplifier.h lib/token.h lib/utils.h lib/valueflow.h lib/valueptr.h lib/vfvalue.h
$(libcppdir)/astutils.o: lib/astutils.cpp lib/astutils.h lib/check.h lib/checkclass.h lib/checkers.h lib/checkimpl.h lib/config.h lib/errortypes.h lib/findtoken.h lib/infer.h lib/library.h lib/mathlib.h lib/platform.h lib/refthunk.h lib/settings.h lib/smallvector.h lib/sourcelocation.h lib/standards.h lib/symboldatabase.h lib/templatesimplifier.h lib/token.h lib/utils.h lib/valueflow.h lib/valueptr.h lib/vf_common.h lib/vfvalue.h
$(CXX) ${INCLUDE_FOR_LIB} $(CPPFLAGS) $(CXXFLAGS) -c -o $@ $(libcppdir)/astutils.cpp

$(libcppdir)/check64bit.o: lib/check64bit.cpp lib/check.h lib/check64bit.h lib/checkers.h lib/checkimpl.h lib/config.h lib/errortypes.h lib/library.h lib/mathlib.h lib/platform.h lib/refthunk.h lib/settings.h lib/smallvector.h lib/sourcelocation.h lib/standards.h lib/symboldatabase.h lib/templatesimplifier.h lib/token.h lib/tokenize.h lib/tokenlist.h lib/utils.h lib/vfvalue.h
Expand All @@ -520,7 +520,7 @@ $(libcppdir)/checkbufferoverrun.o: lib/checkbufferoverrun.cpp externals/tinyxml2
$(libcppdir)/checkclass.o: lib/checkclass.cpp externals/tinyxml2/tinyxml2.h lib/astutils.h lib/check.h lib/checkclass.h lib/checkers.h lib/checkimpl.h lib/config.h lib/errorlogger.h lib/errortypes.h lib/library.h lib/mathlib.h lib/path.h lib/platform.h lib/refthunk.h lib/settings.h lib/smallvector.h lib/sourcelocation.h lib/standards.h lib/symboldatabase.h lib/templatesimplifier.h lib/token.h lib/tokenize.h lib/tokenlist.h lib/utils.h lib/vfvalue.h lib/xml.h
$(CXX) ${INCLUDE_FOR_LIB} $(CPPFLAGS) $(CXXFLAGS) -c -o $@ $(libcppdir)/checkclass.cpp

$(libcppdir)/checkcondition.o: lib/checkcondition.cpp lib/astutils.h lib/check.h lib/checkcondition.h lib/checkers.h lib/checkimpl.h lib/checkother.h lib/config.h lib/errortypes.h lib/library.h lib/mathlib.h lib/platform.h lib/refthunk.h lib/settings.h lib/smallvector.h lib/sourcelocation.h lib/standards.h lib/symboldatabase.h lib/templatesimplifier.h lib/token.h lib/tokenize.h lib/tokenlist.h lib/utils.h lib/vfvalue.h
$(libcppdir)/checkcondition.o: lib/checkcondition.cpp lib/astutils.h lib/check.h lib/checkcondition.h lib/checkers.h lib/checkimpl.h lib/checkother.h lib/config.h lib/errortypes.h lib/library.h lib/mathlib.h lib/platform.h lib/refthunk.h lib/settings.h lib/smallvector.h lib/sourcelocation.h lib/standards.h lib/symboldatabase.h lib/templatesimplifier.h lib/token.h lib/tokenize.h lib/tokenlist.h lib/utils.h lib/vf_common.h lib/vfvalue.h
$(CXX) ${INCLUDE_FOR_LIB} $(CPPFLAGS) $(CXXFLAGS) -c -o $@ $(libcppdir)/checkcondition.cpp

$(libcppdir)/checkers.o: lib/checkers.cpp lib/checkers.h lib/config.h
Expand Down Expand Up @@ -688,10 +688,10 @@ $(libcppdir)/utils.o: lib/utils.cpp lib/config.h lib/utils.h
$(libcppdir)/vf_analyzers.o: lib/vf_analyzers.cpp lib/analyzer.h lib/astutils.h lib/calculate.h lib/checkers.h lib/config.h lib/errortypes.h lib/library.h lib/mathlib.h lib/platform.h lib/programmemory.h lib/refthunk.h lib/settings.h lib/smallvector.h lib/sourcelocation.h lib/standards.h lib/symboldatabase.h lib/templatesimplifier.h lib/token.h lib/utils.h lib/valueflow.h lib/valueptr.h lib/vf_analyzers.h lib/vf_common.h lib/vf_settokenvalue.h lib/vfvalue.h
$(CXX) ${INCLUDE_FOR_LIB} $(CPPFLAGS) $(CXXFLAGS) -c -o $@ $(libcppdir)/vf_analyzers.cpp

$(libcppdir)/vf_common.o: lib/vf_common.cpp lib/astutils.h lib/checkers.h lib/config.h lib/errortypes.h lib/library.h lib/mathlib.h lib/path.h lib/platform.h lib/refthunk.h lib/settings.h lib/smallvector.h lib/sourcelocation.h lib/standards.h lib/symboldatabase.h lib/templatesimplifier.h lib/token.h lib/utils.h lib/vf_common.h lib/vf_settokenvalue.h lib/vfvalue.h
$(libcppdir)/vf_common.o: lib/vf_common.cpp lib/astutils.h lib/checkers.h lib/config.h lib/errortypes.h lib/infer.h lib/library.h lib/mathlib.h lib/path.h lib/platform.h lib/refthunk.h lib/settings.h lib/smallvector.h lib/sourcelocation.h lib/standards.h lib/symboldatabase.h lib/templatesimplifier.h lib/token.h lib/utils.h lib/vf_common.h lib/vf_settokenvalue.h lib/vfvalue.h
$(CXX) ${INCLUDE_FOR_LIB} $(CPPFLAGS) $(CXXFLAGS) -c -o $@ $(libcppdir)/vf_common.cpp

$(libcppdir)/vf_settokenvalue.o: lib/vf_settokenvalue.cpp lib/astutils.h lib/calculate.h lib/checkers.h lib/config.h lib/errortypes.h lib/library.h lib/mathlib.h lib/platform.h lib/refthunk.h lib/settings.h lib/smallvector.h lib/sourcelocation.h lib/standards.h lib/symboldatabase.h lib/templatesimplifier.h lib/token.h lib/utils.h lib/valueflow.h lib/vf_common.h lib/vf_settokenvalue.h lib/vfvalue.h
$(libcppdir)/vf_settokenvalue.o: lib/vf_settokenvalue.cpp lib/astutils.h lib/calculate.h lib/checkers.h lib/config.h lib/errortypes.h lib/infer.h lib/library.h lib/mathlib.h lib/platform.h lib/refthunk.h lib/settings.h lib/smallvector.h lib/sourcelocation.h lib/standards.h lib/symboldatabase.h lib/templatesimplifier.h lib/token.h lib/utils.h lib/valueflow.h lib/valueptr.h lib/vf_common.h lib/vf_settokenvalue.h lib/vfvalue.h
$(CXX) ${INCLUDE_FOR_LIB} $(CPPFLAGS) $(CXXFLAGS) -c -o $@ $(libcppdir)/vf_settokenvalue.cpp

$(libcppdir)/vfvalue.o: lib/vfvalue.cpp lib/config.h lib/errortypes.h lib/mathlib.h lib/smallvector.h lib/templatesimplifier.h lib/token.h lib/utils.h lib/vfvalue.h
Expand Down Expand Up @@ -961,7 +961,7 @@ test/testutils.o: test/testutils.cpp lib/check.h lib/checkers.h lib/color.h lib/
test/testvaarg.o: test/testvaarg.cpp lib/check.h lib/checkers.h lib/checkimpl.h lib/checkvaarg.h lib/color.h lib/config.h lib/errorlogger.h lib/errortypes.h lib/library.h lib/mathlib.h lib/path.h lib/platform.h lib/refthunk.h lib/settings.h lib/standards.h lib/tokenize.h lib/tokenlist.h lib/utils.h test/fixture.h test/helpers.h
$(CXX) ${INCLUDE_FOR_TEST} ${CFLAGS_FOR_TEST} $(CPPFLAGS) $(CXXFLAGS) -c -o $@ test/testvaarg.cpp

test/testvalueflow.o: test/testvalueflow.cpp lib/check.h lib/checkers.h lib/color.h lib/config.h lib/errorlogger.h lib/errortypes.h lib/library.h lib/mathlib.h lib/path.h lib/platform.h lib/refthunk.h lib/settings.h lib/smallvector.h lib/standards.h lib/templatesimplifier.h lib/token.h lib/tokenize.h lib/tokenlist.h lib/utils.h lib/vfvalue.h test/fixture.h test/helpers.h
test/testvalueflow.o: test/testvalueflow.cpp lib/check.h lib/checkers.h lib/color.h lib/config.h lib/errorlogger.h lib/errortypes.h lib/infer.h lib/library.h lib/mathlib.h lib/path.h lib/platform.h lib/refthunk.h lib/settings.h lib/smallvector.h lib/sourcelocation.h lib/standards.h lib/symboldatabase.h lib/templatesimplifier.h lib/token.h lib/tokenize.h lib/tokenlist.h lib/utils.h lib/valueptr.h lib/vf_common.h lib/vfvalue.h test/fixture.h test/helpers.h
$(CXX) ${INCLUDE_FOR_TEST} ${CFLAGS_FOR_TEST} $(CPPFLAGS) $(CXXFLAGS) -c -o $@ test/testvalueflow.cpp

test/testvarid.o: test/testvarid.cpp lib/check.h lib/checkers.h lib/color.h lib/config.h lib/errorlogger.h lib/errortypes.h lib/library.h lib/mathlib.h lib/path.h lib/platform.h lib/refthunk.h lib/settings.h lib/smallvector.h lib/standards.h lib/templatesimplifier.h lib/token.h lib/tokenize.h lib/tokenlist.h lib/utils.h lib/vfvalue.h test/fixture.h test/helpers.h
Expand Down
135 changes: 97 additions & 38 deletions lib/astutils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
#include "utils.h"
#include "valueflow.h"
#include "valueptr.h"
#include "vf_common.h"
#include "vfvalue.h"

#include "checkclass.h"
Expand All @@ -42,6 +43,7 @@
#include <functional>
#include <initializer_list>
#include <iterator>
#include <limits>
#include <list>
#include <set>
#include <type_traits>
Expand Down Expand Up @@ -835,6 +837,17 @@ bool astIsRHS(const Token* tok)
return parent->astOperand2() == tok;
}

std::string invertOperatorForOperandSwap(std::string op)
{
if (op.empty())
return op;
if (op[0] == '<')
op[0] = '>';
else if (op[0] == '>')
op[0] = '<';
return op;
}

template<class T, REQUIRES("T must be a Token class", std::is_convertible<T*, const Token*> )>
static T* getCondTokImpl(T* tok)
{
Expand Down Expand Up @@ -952,56 +965,66 @@ const Token *findNextTokenFromBreak(const Token *breakToken)
return nullptr;
}

bool extractForLoopValues(const Token *forToken,
nonneg int &varid,
bool &knownInitValue,
MathLib::bigint &initValue,
bool &partialCond,
MathLib::bigint &stepValue,
MathLib::bigint &lastValue)
ForLoopValues extractForLoopValues(const Token *forToken)
{
ForLoopValues values;
if (!Token::simpleMatch(forToken, "for (") || !Token::simpleMatch(forToken->next()->astOperand2(), ";"))
return false;
return values;
const Token *initExpr = forToken->next()->astOperand2()->astOperand1();
const Token *condExpr = forToken->next()->astOperand2()->astOperand2()->astOperand1();
const Token *incExpr = forToken->next()->astOperand2()->astOperand2()->astOperand2();
if (!initExpr || !initExpr->isBinaryOp() || initExpr->str() != "=" || !Token::Match(initExpr->astOperand1(), "%var%"))
return false;
std::vector<MathLib::bigint> minInitValue =
getMinValue(makeIntegralInferModel(), initExpr->astOperand2()->values());
if (minInitValue.empty()) {
const ValueFlow::Value* v = initExpr->astOperand2()->getMinValue(true);
if (v)
minInitValue.push_back(v->intvalue);
return values;
const Token* const initToken = initExpr->astOperand2();
const std::vector<MathLib::bigint> minInitValue =
getMinValue(makeIntegralInferModel(), initToken->values());
values.initValueSource = getMinValueSource(makeIntegralInferModel(), initToken->values());
values.hasMinimum = !minInitValue.empty();
if (values.hasMinimum) {
values.knownInitValue = initToken->hasKnownIntValue();
values.initValue = minInitValue.front();
if (values.knownInitValue && values.initValueSource &&
values.initValueSource->bound != ValueFlow::Value::Bound::Point) {
const auto it = std::find_if(initToken->values().cbegin(),
initToken->values().cend(),
[&](const ValueFlow::Value& value) {
return value.isIntValue() && !value.isImpossible() && !value.isInconclusive() &&
value.bound == ValueFlow::Value::Bound::Point && value.intvalue == values.initValue;
});
if (it != initToken->values().cend())
values.initValueSource = &*it;
}
}
if (minInitValue.empty())
return false;
varid = initExpr->astOperand1()->varId();
knownInitValue = initExpr->astOperand2()->hasKnownIntValue();
initValue = minInitValue.front();
partialCond = Token::Match(condExpr, "%oror%|&&");
visitAstNodes(condExpr, [varid, &condExpr](const Token *tok) {
values.varid = initExpr->astOperand1()->varId();
values.partialCond = Token::Match(condExpr, "%oror%|&&");
visitAstNodes(condExpr, [&](const Token *tok) {
if (Token::Match(tok, "%oror%|&&"))
return ChildrenToVisit::op1_and_op2;
if (Token::Match(tok, "<|<=") && tok->isBinaryOp() && tok->astOperand1()->varId() == varid && tok->astOperand2()->hasKnownIntValue()) {
if (Token::Match(tok, "<|<=") && tok->isBinaryOp() && tok->astOperand1()->varId() == values.varid &&
tok->astOperand2()->hasKnownIntValue()) {
if (Token::Match(condExpr, "%oror%|&&") || tok->astOperand2()->getKnownIntValue() < condExpr->astOperand2()->getKnownIntValue())
condExpr = tok;
}
return ChildrenToVisit::none;
});
if (!Token::Match(condExpr, "<|<=") || !condExpr->isBinaryOp() || condExpr->astOperand1()->varId() != varid || !condExpr->astOperand2()->hasKnownIntValue())
return false;
if (!incExpr || !incExpr->isUnaryOp("++") || incExpr->astOperand1()->varId() != varid)
return false;
stepValue = 1;
if (condExpr->str() == "<")
lastValue = condExpr->astOperand2()->getKnownIntValue() - 1;
else
lastValue = condExpr->astOperand2()->getKnownIntValue();
return true;
if (!Token::Match(condExpr, "<|<=") || !condExpr->isBinaryOp() || condExpr->astOperand1()->varId() != values.varid ||
!condExpr->astOperand2()->hasKnownIntValue())
return values;
if (!incExpr || !incExpr->isUnaryOp("++") || incExpr->astOperand1()->varId() != values.varid)
return values;
values.stepValue = 1;
if (condExpr->str() == "<") {
const MathLib::bigint limit = condExpr->astOperand2()->getKnownIntValue();
if (limit == std::numeric_limits<MathLib::bigint>::min())
return values;
values.lastValue = limit - 1;
} else {
values.lastValue = condExpr->astOperand2()->getKnownIntValue();
}
values.valid = true;
return values;
}


static const Token * getVariableInitExpression(const Variable * var)
{
if (!var)
Expand Down Expand Up @@ -1476,6 +1499,22 @@ static inline bool isDifferentKnownValues(const Token * const tok1, const Token
});
}

// Like isDifferentKnownValues(), but the two constants are read as they compare against 'exprTok'.
static bool isDifferentKnownValues(const Token* tok1, const Token* tok2, const Token* exprTok, const Settings& settings)
{
return compareKnownValue(tok1, tok2, [&](const ValueFlow::Value& v1, const ValueFlow::Value& v2, bool sameLifetime) {
if (!v1.isIntValue() || !v2.isIntValue()) {
bool equal = v1.equalValue(v2);
if (v1.isIteratorValue())
equal &= sameLifetime;
return !equal;
}
const MathLib::bigint value1 = ValueFlow::castConstantToOperandType(tok1, exprTok, v1.intvalue, settings);
const MathLib::bigint value2 = ValueFlow::castConstantToOperandType(tok2, exprTok, v2.intvalue, settings);
return value1 != value2;
});
}

static inline bool isSameConstantValue(bool macro, const Token* tok1, const Token* tok2)
{
if (tok1 == nullptr || tok2 == nullptr)
Expand Down Expand Up @@ -1903,9 +1942,9 @@ bool isOppositeCond(bool isNot, const Token * const cond1, const Token * const c
if (!isNot) {
if (cond1->str() == "==" && cond2->str() == "==") {
if (isSameExpression(true, cond1->astOperand1(), cond2->astOperand1(), settings, pure, followVar, errors))
return isDifferentKnownValues(cond1->astOperand2(), cond2->astOperand2());
return isDifferentKnownValues(cond1->astOperand2(), cond2->astOperand2(), cond1->astOperand1(), settings);
if (isSameExpression(true, cond1->astOperand2(), cond2->astOperand2(), settings, pure, followVar, errors))
return isDifferentKnownValues(cond1->astOperand1(), cond2->astOperand1());
return isDifferentKnownValues(cond1->astOperand1(), cond2->astOperand1(), cond1->astOperand2(), settings);
}

if (Library::isContainerYield(cond1, Library::Container::Yield::EMPTY, "empty") &&
Expand Down Expand Up @@ -2011,10 +2050,16 @@ bool isOppositeCond(bool isNot, const Token * const cond1, const Token * const c
const ValueFlow::Value &rhsValue1 = value1->values().front();
const ValueFlow::Value &rhsValue2 = value2->values().front();

// The operands are compared in their common type. When one side is an unsigned constant and the
// other a signed expression, the comparison happens in the unsigned domain, so the constant must be
// read as that expression's type would see it: 0xFFFFFFFFu is -1 for an 'int' operand.
const MathLib::bigint bound1 = ValueFlow::castConstantToOperandType(value1, expr1, rhsValue1.intvalue, settings);
const MathLib::bigint bound2 = ValueFlow::castConstantToOperandType(value2, expr2, rhsValue2.intvalue, settings);

if (op1 == "<" || op1 == "<=")
return (op2 == "==" || op2 == ">" || op2 == ">=") && (rhsValue1.intvalue < rhsValue2.intvalue);
return (op2 == "==" || op2 == ">" || op2 == ">=") && (bound1 < bound2);
if (op1 == ">=" || op1 == ">")
return (op2 == "==" || op2 == "<" || op2 == "<=") && (rhsValue1.intvalue > rhsValue2.intvalue);
return (op2 == "==" || op2 == "<" || op2 == "<=") && (bound1 > bound2);

return false;
}
Expand Down Expand Up @@ -3934,6 +3979,20 @@ const Token *skipUnreachableBranch(const Token *tok)
return tok;
}

bool isInUnreachableBranch(const Token *tok)
{
for (const Scope *scope = tok->scope(); scope; scope = scope->nestedIn) {
if (scope->type != ScopeType::eIf && scope->type != ScopeType::eElse)
continue;
const Token *body = scope->bodyStart;
if (!body || body->str() != "{" || scope->bodyEnd != body->link())
continue;
if (skipUnreachableBranch(body) != body)
return true;
}
return false;
}

bool isEscapeKeyword(const Token *tok, const Settings &settings)
{
if (!tok)
Expand Down
29 changes: 21 additions & 8 deletions lib/astutils.h
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,9 @@ std::vector<ValueType> getParentValueTypes(const Token* tok,
bool astIsLHS(const Token* tok);
bool astIsRHS(const Token* tok);

/** The comparison operator that tests the left operand, i.e. the one with the operands exchanged. */
std::string invertOperatorForOperandSwap(std::string op);

Token* getCondTok(Token* tok);
const Token* getCondTok(const Token* tok);

Expand All @@ -230,15 +233,22 @@ const Token* getCondTokFromEnd(const Token* endBlock);
const Token *findNextTokenFromBreak(const Token *breakToken);

/**
* Extract for loop values: loopvar varid, init value, step value, last value (inclusive)
* The values of a for loop that can be extracted: loop variable id, init value, step value,
* last value (inclusive). `initValueSource` identifies the fact supplying the minimum.
*/
bool extractForLoopValues(const Token *forToken,
nonneg int &varid,
bool &knownInitValue,
MathLib::bigint &initValue,
bool &partialCond,
MathLib::bigint &stepValue,
MathLib::bigint &lastValue);
struct ForLoopValues {

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: the members should be reordered to avoid padding

bool valid = false;
bool hasMinimum = false;
nonneg int varid = 0;
bool knownInitValue = false;
MathLib::bigint initValue = 0;
const ValueFlow::Value* initValueSource = nullptr;
bool partialCond = false;
MathLib::bigint stepValue = 0;
MathLib::bigint lastValue = 0;
};

ForLoopValues extractForLoopValues(const Token *forToken);

bool precedes(const Token * tok1, const Token * tok2);
bool succeeds(const Token* tok1, const Token* tok2);
Expand Down Expand Up @@ -466,6 +476,9 @@ bool isUnreachableOperand(const Token *tok);

const Token *skipUnreachableBranch(const Token *tok);

/** Is 'tok' inside a branch that skipUnreachableBranch() proves unreachable? */
bool isInUnreachableBranch(const Token *tok);

bool isEscapeKeyword(const Token *tok, const Settings &settings);

#endif // astutilsH
Loading
Loading