@@ -85,6 +85,15 @@ const ValueFlow::Value* ProgramMemory::getValue(nonneg int exprid, bool impossib
85
85
return nullptr ;
86
86
}
87
87
88
+ ValueFlow::Value* ProgramMemory::getValue (nonneg int exprid)
89
+ {
90
+ const auto it = find (exprid);
91
+ const bool found = it != mValues ->cend ();
92
+ if (found)
93
+ return &it->second ;
94
+ return nullptr ;
95
+ }
96
+
88
97
// cppcheck-suppress unusedFunction
89
98
bool ProgramMemory::getIntValue (nonneg int exprid, MathLib::bigint& result) const
90
99
{
@@ -161,24 +170,6 @@ bool ProgramMemory::hasValue(nonneg int exprid) const
161
170
return it != mValues ->cend ();
162
171
}
163
172
164
- const ValueFlow::Value& ProgramMemory::at (nonneg int exprid) const {
165
- const auto it = find (exprid);
166
- if (it == mValues ->cend ()) {
167
- throw std::out_of_range (" ProgramMemory::at" );
168
- }
169
- return it->second ;
170
- }
171
-
172
- ValueFlow::Value& ProgramMemory::at (nonneg int exprid) {
173
- copyOnWrite ();
174
-
175
- const auto it = find (exprid);
176
- if (it == mValues ->end ()) {
177
- throw std::out_of_range (" ProgramMemory::at" );
178
- }
179
- return it->second ;
180
- }
181
-
182
173
void ProgramMemory::erase_if (const std::function<bool (const ExprIdToken&)>& pred)
183
174
{
184
175
if (mValues ->empty ())
@@ -1343,10 +1334,9 @@ namespace {
1343
1334
1344
1335
ValueFlow::Value executeMultiCondition (bool b, const Token* expr)
1345
1336
{
1346
- if (pm->hasValue (expr->exprId ())) {
1347
- const ValueFlow::Value& v = utils::as_const (*pm).at (expr->exprId ());
1348
- if (v.isIntValue ())
1349
- return v;
1337
+ if (const ValueFlow::Value* v = utils::as_const (*pm).getValue (expr->exprId ())) {
1338
+ if (v->isIntValue ())
1339
+ return *v;
1350
1340
}
1351
1341
1352
1342
// Evaluate recursively if there are no exprids
@@ -1475,18 +1465,18 @@ namespace {
1475
1465
if (rhs.isUninitValue ())
1476
1466
return unknown ();
1477
1467
if (expr->str () != " =" ) {
1478
- if (!pm->hasValue (expr->astOperand1 ()->exprId ()))
1468
+ ValueFlow::Value* lhs = pm->getValue (expr->astOperand1 ()->exprId ());
1469
+ if (!lhs)
1479
1470
return unknown ();
1480
- ValueFlow::Value& lhs = pm->at (expr->astOperand1 ()->exprId ());
1481
- rhs = evaluate (removeAssign (expr->str ()), lhs, rhs);
1482
- if (lhs.isIntValue ())
1483
- ValueFlow::Value::visitValue (rhs, std::bind (assign{}, std::ref (lhs.intvalue ), std::placeholders::_1));
1484
- else if (lhs.isFloatValue ())
1471
+ rhs = evaluate (removeAssign (expr->str ()), *lhs, rhs);
1472
+ if (lhs->isIntValue ())
1473
+ ValueFlow::Value::visitValue (rhs, std::bind (assign{}, std::ref (lhs->intvalue ), std::placeholders::_1));
1474
+ else if (lhs->isFloatValue ())
1485
1475
ValueFlow::Value::visitValue (rhs,
1486
- std::bind (assign{}, std::ref (lhs. floatValue ), std::placeholders::_1));
1476
+ std::bind (assign{}, std::ref (lhs-> floatValue ), std::placeholders::_1));
1487
1477
else
1488
1478
return unknown ();
1489
- return lhs;
1479
+ return * lhs;
1490
1480
}
1491
1481
pm->setValue (expr->astOperand1 (), rhs);
1492
1482
return rhs;
@@ -1498,20 +1488,20 @@ namespace {
1498
1488
execute (expr->astOperand1 ());
1499
1489
return execute (expr->astOperand2 ());
1500
1490
} else if (expr->tokType () == Token::eIncDecOp && expr->astOperand1 () && expr->astOperand1 ()->exprId () != 0 ) {
1501
- if (!pm->hasValue (expr->astOperand1 ()->exprId ()))
1491
+ ValueFlow::Value* lhs = pm->getValue (expr->astOperand1 ()->exprId ());
1492
+ if (!lhs)
1502
1493
return ValueFlow::Value::unknown ();
1503
- ValueFlow::Value& lhs = pm->at (expr->astOperand1 ()->exprId ());
1504
- if (!lhs.isIntValue ())
1494
+ if (!lhs->isIntValue ())
1505
1495
return unknown ();
1506
1496
// overflow
1507
- if (!lhs. isImpossible () && lhs. intvalue == 0 && expr->str () == " --" && astIsUnsigned (expr->astOperand1 ()))
1497
+ if (!lhs-> isImpossible () && lhs-> intvalue == 0 && expr->str () == " --" && astIsUnsigned (expr->astOperand1 ()))
1508
1498
return unknown ();
1509
1499
1510
1500
if (expr->str () == " ++" )
1511
- lhs. intvalue ++;
1501
+ lhs-> intvalue ++;
1512
1502
else
1513
- lhs. intvalue --;
1514
- return lhs;
1503
+ lhs-> intvalue --;
1504
+ return * lhs;
1515
1505
} else if (expr->str () == " [" && expr->astOperand1 () && expr->astOperand2 ()) {
1516
1506
const Token* tokvalue = nullptr ;
1517
1507
if (!pm->getTokValue (expr->astOperand1 ()->exprId (), tokvalue)) {
@@ -1602,13 +1592,16 @@ namespace {
1602
1592
}
1603
1593
return execute (expr->astOperand1 ());
1604
1594
}
1605
- if (expr->exprId () > 0 && pm->hasValue (expr->exprId ())) {
1606
- ValueFlow::Value result = utils::as_const (*pm).at (expr->exprId ());
1607
- if (result.isImpossible () && result.isIntValue () && result.intvalue == 0 && isUsedAsBool (expr, settings)) {
1608
- result.intvalue = !result.intvalue ;
1609
- result.setKnown ();
1595
+ if (expr->exprId () > 0 ) {
1596
+ if (const ValueFlow::Value* v = utils::as_const (*pm).getValue (expr->exprId ()))
1597
+ {
1598
+ ValueFlow::Value result = *v;
1599
+ if (result.isImpossible () && result.isIntValue () && result.intvalue == 0 && isUsedAsBool (expr, settings)) {
1600
+ result.intvalue = !result.intvalue ;
1601
+ result.setKnown ();
1602
+ }
1603
+ return result;
1610
1604
}
1611
- return result;
1612
1605
}
1613
1606
1614
1607
if (Token::Match (expr->previous (), " >|%name% {|(" )) {
@@ -1658,14 +1651,16 @@ namespace {
1658
1651
}
1659
1652
// Check if function modifies argument
1660
1653
visitAstNodes (expr->astOperand2 (), [&](const Token* child) {
1661
- if (child->exprId () > 0 && pm->hasValue (child->exprId ())) {
1662
- ValueFlow::Value& v = pm->at (child->exprId ());
1663
- if (v.valueType == ValueFlow::Value::ValueType::CONTAINER_SIZE) {
1664
- if (ValueFlow::isContainerSizeChanged (child, v.indirect , settings))
1665
- v = unknown ();
1666
- } else if (v.valueType != ValueFlow::Value::ValueType::UNINIT) {
1667
- if (isVariableChanged (child, v.indirect , settings))
1668
- v = unknown ();
1654
+ if (child->exprId () > 0 ) {
1655
+ if (ValueFlow::Value* v = pm->getValue (child->exprId ()))
1656
+ {
1657
+ if (v->valueType == ValueFlow::Value::ValueType::CONTAINER_SIZE) {
1658
+ if (ValueFlow::isContainerSizeChanged (child, v->indirect , settings))
1659
+ *v = unknown ();
1660
+ } else if (v->valueType != ValueFlow::Value::ValueType::UNINIT) {
1661
+ if (isVariableChanged (child, v->indirect , settings))
1662
+ *v = unknown ();
1663
+ }
1669
1664
}
1670
1665
}
1671
1666
return ChildrenToVisit::op1_and_op2;
@@ -1717,22 +1712,27 @@ namespace {
1717
1712
return v;
1718
1713
if (!expr)
1719
1714
return v;
1720
- if (expr->exprId () > 0 && pm->hasValue (expr->exprId ())) {
1721
- if (updateValue (v, utils::as_const (*pm).at (expr->exprId ())))
1722
- return v;
1715
+ if (expr->exprId () > 0 ) {
1716
+ if (const ValueFlow::Value* val = utils::as_const (*pm).getValue (expr->exprId ()))
1717
+ {
1718
+ if (updateValue (v, *val))
1719
+ return v;
1720
+ }
1723
1721
}
1724
1722
// Find symbolic values
1725
1723
for (const ValueFlow::Value& value : expr->values ()) {
1726
1724
if (!value.isSymbolicValue ())
1727
1725
continue ;
1728
1726
if (!value.isKnown ())
1729
1727
continue ;
1730
- if (value.tokvalue ->exprId () > 0 && !pm->hasValue (value.tokvalue ->exprId ()))
1728
+ if (value.tokvalue ->exprId () == 0 )
1729
+ continue ;
1730
+ const ValueFlow::Value* v_p = utils::as_const (*pm).getValue (value.tokvalue ->exprId ());
1731
+ if (!v_p)
1731
1732
continue ;
1732
- const ValueFlow::Value& v_ref = utils::as_const (*pm).at (value.tokvalue ->exprId ());
1733
- if (!v_ref.isIntValue () && value.intvalue != 0 )
1733
+ if (!v_p->isIntValue () && value.intvalue != 0 )
1734
1734
continue ;
1735
- ValueFlow::Value v2 = v_ref ;
1735
+ ValueFlow::Value v2 = *v_p ;
1736
1736
v2.intvalue += value.intvalue ;
1737
1737
return v2;
1738
1738
}
0 commit comments