diff --git a/lib/programmemory.cpp b/lib/programmemory.cpp index af86ef983183..95b6c1bc479c 100644 --- a/lib/programmemory.cpp +++ b/lib/programmemory.cpp @@ -173,10 +173,39 @@ void ProgramMemory::erase_if(const std::function& pred if (mValues->empty()) return; - // TODO: how to delay until we actuallly modify? - copyOnWrite(); + // TODO: the std::find_if() call is causing ValueFlow::Value::Value(ValueFlow::Value const&) invocations + /*auto it = std::find_if(mValues->cbegin(), mValues->cend(), [&pred](const std::pair& entry) { + return pred(entry.first); + });*/ + + auto it = mValues->cbegin(); + for (; it != mValues->cend(); ++it) { + if (pred(it->first)) + break; + } + if (it == mValues->cend()) + return; + + if (mValues.use_count() == 1) + { + it = mValues->erase(it); + } + else + { + const auto& exprIdTok = it->first; + + copyOnWrite(); + + it = mValues->cbegin(); + for (; it != mValues->cend(); ++it) { + if (it->first == exprIdTok) { + it = mValues->erase(it); + break; + } + } + } - for (auto it = mValues->begin(); it != mValues->end();) { + for (; it != mValues->cend();) { if (pred(it->first)) it = mValues->erase(it); else