Use std::find_if when searching m_StoredStates.

This commit is contained in:
Frederich Munch 2017-02-02 12:46:35 -05:00 committed by sftnight
parent 4dd1ce8e56
commit a91b83632e
2 changed files with 35 additions and 29 deletions

View File

@ -499,23 +499,19 @@ namespace cling {
m_StoredStates.push_back(state); m_StoredStates.push_back(state);
} }
void Interpreter::compareInterpreterState(const std::string& name) const { void Interpreter::compareInterpreterState(const std::string &Name) const {
short foundAtPos = -1; const auto Itr = std::find_if(
for (short i = 0, e = m_StoredStates.size(); i != e; ++i) { m_StoredStates.begin(), m_StoredStates.end(),
if (m_StoredStates[i]->getName() == name) { [&Name](const ClangInternalState *S) { return S->getName() == Name; });
foundAtPos = i; if (Itr == m_StoredStates.end()) {
break; cling::errs() << "The store point name " << Name
} << " does not exist."
} "Unbalanced store / compare\n";
if (foundAtPos < 0) {
cling::errs() << "The store point name " << name << " does not exist."
"Unbalanced store / compare\n";
return; return;
} }
// This may induce deserialization // This may induce deserialization
PushTransactionRAII RAII(this); PushTransactionRAII RAII(this);
m_StoredStates[foundAtPos]->compare(name, m_Opts.Verbose()); (*Itr)->compare(Name, m_Opts.Verbose());
} }
void Interpreter::printIncludedFiles(llvm::raw_ostream& Out) const { void Interpreter::printIncludedFiles(llvm::raw_ostream& Out) const {
@ -1184,23 +1180,21 @@ namespace cling {
// Clear any stored states that reference the llvm::Module. // Clear any stored states that reference the llvm::Module.
// Do it first in case // Do it first in case
if (!m_StoredStates.empty()) { if (!m_StoredStates.empty()) {
const llvm::Module* const module = T.getModule(); const llvm::Module *const Module = T.getModule();
std::vector<unsigned> badStates; const auto Predicate = [&Module](const ClangInternalState *S) {
for (unsigned i = 0, N = m_StoredStates.size(); i < N; ++i) { return S->getModule() == Module;
if (m_StoredStates[i]->getModule() == module) { };
badStates.push_back(i); auto Itr =
if (m_Opts.Verbose()) { std::find_if(m_StoredStates.begin(), m_StoredStates.end(), Predicate);
cling::errs() << "Unloading Transaction forced state '" while (Itr != m_StoredStates.end()) {
<< m_StoredStates[i]->getName() if (m_Opts.Verbose()) {
<< "' to be destroyed\n"; cling::errs() << "Unloading Transaction forced state '"
} << (*Itr)->getName() << "' to be destroyed\n";
} }
} m_StoredStates.erase(Itr);
// Pop off the back so as not to recompute index.
// Should the user be warned in verbose mode? Itr = std::find_if(m_StoredStates.begin(), m_StoredStates.end(),
for (std::vector<unsigned>::reverse_iterator it = badStates.rbegin(), Predicate);
e = badStates.rend(); it != e; ++it) {
m_StoredStates.erase(m_StoredStates.begin()+(*it));
} }
} }

View File

@ -10,9 +10,21 @@
int i = 0; int i = 0;
.storeState "A" .storeState "A"
.storeState "B"
.storeState "C"
.undo .undo
// CHECK: Unloading Transaction forced state 'A' to be destroyed // CHECK: Unloading Transaction forced state 'A' to be destroyed
// CHECK-NEXT: Unloading Transaction forced state 'B' to be destroyed
// CHECK-NEXT: Unloading Transaction forced state 'C' to be destroyed
.compareState "D"
.compareState "E"
.compareState "F"
// CHECK-NEXT: The store point name D does not exist.Unbalanced store / compare
// CHECK-NEXT: The store point name E does not exist.Unbalanced store / compare
// CHECK-NEXT: The store point name F does not exist.Unbalanced store / compare
// expected-no-diagnostics // expected-no-diagnostics