@@ -75,9 +75,10 @@ void ProgramMemory::setValue(const Token* expr, const ValueFlow::Value& value) {
75
75
if (subexpr)
76
76
(*mValues )[subexpr] = std::move (subvalue);
77
77
}
78
+
78
79
const ValueFlow::Value* ProgramMemory::getValue (nonneg int exprid, bool impossible) const
79
80
{
80
- const auto it = utils::as_const (* mValues ). find (exprid);
81
+ const auto it = find (exprid);
81
82
const bool found = it != mValues ->cend () && (impossible || !it->second .isImpossible ());
82
83
if (found)
83
84
return &it->second ;
@@ -154,18 +155,28 @@ void ProgramMemory::setUnknown(const Token* expr) {
154
155
(*mValues )[expr].valueType = ValueFlow::Value::ValueType::UNINIT;
155
156
}
156
157
157
- bool ProgramMemory::hasValue (nonneg int exprid)
158
+ bool ProgramMemory::hasValue (nonneg int exprid) const
158
159
{
159
- return mValues ->find (exprid) != mValues ->end ();
160
+ const auto it = find (exprid);
161
+ return it != mValues ->cend ();
160
162
}
161
163
162
164
const ValueFlow::Value& ProgramMemory::at (nonneg int exprid) const {
163
- return mValues ->at (exprid);
165
+ const auto it = find (exprid);
166
+ if (it == mValues ->cend ()) {
167
+ throw std::out_of_range (" ProgramMemory::at" );
168
+ }
169
+ return it->second ;
164
170
}
171
+
165
172
ValueFlow::Value& ProgramMemory::at (nonneg int exprid) {
166
173
copyOnWrite ();
167
174
168
- return mValues ->at (exprid);
175
+ const auto it = find (exprid);
176
+ if (it == mValues ->end ()) {
177
+ throw std::out_of_range (" ProgramMemory::at" );
178
+ }
179
+ return it->second ;
169
180
}
170
181
171
182
void ProgramMemory::erase_if (const std::function<bool (const ExprIdToken&)>& pred)
@@ -225,6 +236,21 @@ void ProgramMemory::copyOnWrite()
225
236
mValues = std::make_shared<Map>(*mValues );
226
237
}
227
238
239
+ ProgramMemory::Map::const_iterator ProgramMemory::find (nonneg int exprid) const
240
+ {
241
+ const auto & cvalues = utils::as_const (*mValues );
242
+ return std::find_if (cvalues.cbegin (), cvalues.cend (), [&exprid](const Map::value_type& entry) {
243
+ return entry.first .getExpressionId () == exprid;
244
+ });
245
+ }
246
+
247
+ ProgramMemory::Map::iterator ProgramMemory::find (nonneg int exprid)
248
+ {
249
+ return std::find_if (mValues ->begin (), mValues ->end (), [&exprid](const Map::value_type& entry) {
250
+ return entry.first .getExpressionId () == exprid;
251
+ });
252
+ }
253
+
228
254
static ValueFlow::Value execute (const Token* expr, ProgramMemory& pm, const Settings& settings);
229
255
230
256
static bool evaluateCondition (MathLib::bigint r, const Token* condition, ProgramMemory& pm, const Settings& settings)
@@ -395,7 +421,7 @@ static void fillProgramMemoryFromAssignments(ProgramMemory& pm, const Token* tok
395
421
bool setvar = false ;
396
422
const Token* vartok = tok2->astOperand1 ();
397
423
for (const auto & p:vars) {
398
- if (p.first != vartok->exprId ())
424
+ if (p.first . getExpressionId () != vartok->exprId ())
399
425
continue ;
400
426
if (vartok == tok)
401
427
continue ;
0 commit comments