Skip to content
Merged
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
3 changes: 2 additions & 1 deletion lib/astutils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1614,7 +1614,8 @@ static bool isUnchanged(const Token *startToken, const Token *endToken, const st
struct FwdAnalysis::Result FwdAnalysis::checkRecursive(const Token *expr, const Token *startToken, const Token *endToken, const std::set<int> &exprVarIds, bool local, bool inInnerClass)
{
// Parse the given tokens
for (const Token *tok = startToken; tok != endToken; tok = tok->next()) {

for (const Token* tok = startToken; precedes(tok, endToken); tok = tok->next()) {
if (Token::simpleMatch(tok, "try {")) {
// TODO: handle try
return Result(Result::Type::BAILOUT);
Expand Down
54 changes: 43 additions & 11 deletions lib/valueflow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1841,6 +1841,16 @@ static void valueFlowGlobalStaticVar(TokenList *tokenList, const Settings *setti
}
}

static void valueFlowForward(Token* startToken,
const Token* endToken,
const Token* exprTok,
std::list<ValueFlow::Value> values,
const bool constValue,
const bool subFunction,
TokenList* const tokenlist,
ErrorLogger* const errorLogger,
const Settings* settings);

static void valueFlowReverse(TokenList *tokenlist,
Token *tok,
const Token * const varToken,
Expand All @@ -1865,8 +1875,30 @@ static void valueFlowReverse(TokenList *tokenlist,
}

if (tok2->varId() == varid) {
if (tok2->hasKnownValue())
break;
// bailout: assignment
if (Token::Match(tok2->previous(), "!!* %name% =")) {
Token* assignTok = const_cast<Token*>(tok2->next()->astOperand2());
if (!assignTok->hasKnownValue()) {
std::list<ValueFlow::Value> values = {val};
setTokenValue(assignTok, val, settings);
if (val2.condition) {
setTokenValue(assignTok, val2, settings);
values.push_back(val2);
}
const Token* startForwardToken = nextAfterAstRightmostLeaf(tok2->next());
const Token* endForwardToken = tok->scope() ? tok->scope()->bodyEnd : tok;
valueFlowForward(const_cast<Token*>(startForwardToken),
endForwardToken,
assignTok,
values,
false,
false,
tokenlist,
errorLogger,
settings);
}
if (settings->debugwarnings)
bailout(tokenlist, errorLogger, tok2, "assignment of " + tok2->str());
break;
Expand Down Expand Up @@ -2062,6 +2094,17 @@ static void valueFlowReverse(TokenList *tokenlist,
}
}

static bool isConditionKnown(const Token* tok, bool then)
{
const char* op = "||";
if (then)
op = "&&";
const Token* parent = tok->astParent();
while (parent && parent->str() == op)
parent = parent->astParent();
return (parent && parent->str() == "(");
}

static void valueFlowBeforeCondition(TokenList *tokenlist, SymbolDatabase *symboldatabase, ErrorLogger *errorLogger, const Settings *settings)
{
for (const Scope * scope : symboldatabase->functionScopes) {
Expand Down Expand Up @@ -4338,17 +4381,6 @@ static void valueFlowAfterAssign(TokenList *tokenlist, SymbolDatabase* symboldat
}
}

static bool isConditionKnown(const Token* tok, bool then)
{
const char * op = "||";
if (then)
op = "&&";
const Token* parent = tok->astParent();
while (parent && parent->str() == op)
parent = parent->astParent();
return (parent && parent->str() == "(");
}

static void valueFlowSetConditionToKnown(const Token* tok, std::list<ValueFlow::Value>& values, bool then)
{
if (values.empty())
Expand Down
20 changes: 20 additions & 0 deletions test/testvalueflow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ class TestValueFlow : public TestFixture {
TEST_CASE(valueFlowBeforeConditionSizeof);
TEST_CASE(valueFlowBeforeConditionSwitch);
TEST_CASE(valueFlowBeforeConditionTernaryOp);
TEST_CASE(valueFlowBeforeConditionForward);

TEST_CASE(valueFlowAfterAssign);
TEST_CASE(valueFlowAfterCondition);
Expand Down Expand Up @@ -1410,6 +1411,25 @@ class TestValueFlow : public TestFixture {
errout.str());
}

void valueFlowBeforeConditionForward()
{
const char* code;

code = "void f(int a) {\n"
" int x = a;\n"
" if (a == 123) {}\n"
" int b = x;\n"
"}";
ASSERT_EQUALS(true, testValueOfX(code, 4U, 123));

code = "void f(int a) {\n"
" int x = a;\n"
" if (a != 123) {}\n"
" int b = x;\n"
"}";
ASSERT_EQUALS(true, testValueOfX(code, 4U, 123));
}

void valueFlowAfterAssign() {
const char *code;

Expand Down