@@ -75,9 +75,10 @@ void ProgramMemory::setValue(const Token* expr, const ValueFlow::Value& value) {
7575 if (subexpr)
7676 (*mValues )[subexpr] = std::move (subvalue);
7777}
78+
7879const ValueFlow::Value* ProgramMemory::getValue (nonneg int exprid, bool impossible) const
7980{
80- const auto it = utils::as_const (* mValues ). find (exprid);
81+ const auto it = find (exprid);
8182 const bool found = it != mValues ->cend () && (impossible || !it->second .isImpossible ());
8283 if (found)
8384 return &it->second ;
@@ -154,18 +155,28 @@ void ProgramMemory::setUnknown(const Token* expr) {
154155 (*mValues )[expr].valueType = ValueFlow::Value::ValueType::UNINIT;
155156}
156157
157- bool ProgramMemory::hasValue (nonneg int exprid)
158+ bool ProgramMemory::hasValue (nonneg int exprid) const
158159{
159- return mValues ->find (exprid) != mValues ->end ();
160+ const auto it = find (exprid);
161+ return it != mValues ->cend ();
160162}
161163
162164const 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 ;
164170}
171+
165172ValueFlow::Value& ProgramMemory::at (nonneg int exprid) {
166173 copyOnWrite ();
167174
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 ;
169180}
170181
171182void ProgramMemory::erase_if (const std::function<bool (const ExprIdToken&)>& pred)
@@ -225,6 +236,21 @@ void ProgramMemory::copyOnWrite()
225236 mValues = std::make_shared<Map>(*mValues );
226237}
227238
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+
228254static ValueFlow::Value execute (const Token* expr, ProgramMemory& pm, const Settings& settings);
229255
230256static 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
395421 bool setvar = false ;
396422 const Token* vartok = tok2->astOperand1 ();
397423 for (const auto & p:vars) {
398- if (p.first != vartok->exprId ())
424+ if (p.first . getExpressionId () != vartok->exprId ())
399425 continue ;
400426 if (vartok == tok)
401427 continue ;
0 commit comments