OCD: foreach -> C++11 ranged for

This commit is contained in:
Eelco Dolstra 2015-07-17 19:24:28 +02:00
parent 1511aa9f48
commit 6bd2c7bb38
30 changed files with 849 additions and 874 deletions

View file

@ -367,13 +367,13 @@ LocalStore::LocalStore(bool reserveSpace)
LocalStore::~LocalStore()
{
try {
foreach (RunningSubstituters::iterator, i, runningSubstituters) {
if (i->second.disabled) continue;
i->second.to.close();
i->second.from.close();
i->second.error.close();
if (i->second.pid != -1)
i->second.pid.wait(true);
for (auto & i : runningSubstituters) {
if (i.second.disabled) continue;
i.second.to.close();
i.second.from.close();
i.second.error.close();
if (i.second.pid != -1)
i.second.pid.wait(true);
}
} catch (...) {
ignoreException();
@ -671,19 +671,19 @@ void LocalStore::checkDerivationOutputs(const Path & drvPath, const Derivation &
else {
Derivation drvCopy(drv);
foreach (DerivationOutputs::iterator, i, drvCopy.outputs) {
i->second.path = "";
drvCopy.env[i->first] = "";
for (auto & i : drvCopy.outputs) {
i.second.path = "";
drvCopy.env[i.first] = "";
}
Hash h = hashDerivationModulo(*this, drvCopy);
foreach (DerivationOutputs::const_iterator, i, drv.outputs) {
Path outPath = makeOutputPath(i->first, h, drvName);
StringPairs::const_iterator j = drv.env.find(i->first);
if (i->second.path != outPath || j == drv.env.end() || j->second != outPath)
for (auto & i : drv.outputs) {
Path outPath = makeOutputPath(i.first, h, drvName);
StringPairs::const_iterator j = drv.env.find(i.first);
if (i.second.path != outPath || j == drv.env.end() || j->second != outPath)
throw Error(format("derivation %1% has incorrect output %2%, should be %3%")
% drvPath % i->second.path % outPath);
% drvPath % i.second.path % outPath);
}
}
}
@ -721,11 +721,11 @@ unsigned long long LocalStore::addValidPath(const ValidPathInfo & info, bool che
registration above is undone. */
if (checkOutputs) checkDerivationOutputs(info.path, drv);
foreach (DerivationOutputs::iterator, i, drv.outputs) {
for (auto & i : drv.outputs) {
SQLiteStmtUse use(stmtAddDerivationOutput);
stmtAddDerivationOutput.bind(id);
stmtAddDerivationOutput.bind(i->first);
stmtAddDerivationOutput.bind(i->second.path);
stmtAddDerivationOutput.bind(i.first);
stmtAddDerivationOutput.bind(i.second.path);
if (sqlite3_step(stmtAddDerivationOutput) != SQLITE_DONE)
throwSQLiteError(db, format("adding derivation output for %1% in database") % info.path);
}
@ -796,11 +796,11 @@ void LocalStore::clearFailedPaths(const PathSet & paths)
retry_sqlite {
SQLiteTxn txn(db);
foreach (PathSet::const_iterator, i, paths) {
for (auto & i : paths) {
SQLiteStmtUse use(stmtClearFailedPath);
stmtClearFailedPath.bind(*i);
stmtClearFailedPath.bind(i);
if (sqlite3_step(stmtClearFailedPath) != SQLITE_DONE)
throwSQLiteError(db, format("clearing failed path %1% in database") % *i);
throwSQLiteError(db, format("clearing failed path %1% in database") % i);
}
txn.commit();
@ -923,8 +923,8 @@ PathSet LocalStore::queryValidPaths(const PathSet & paths)
{
retry_sqlite {
PathSet res;
foreach (PathSet::const_iterator, i, paths)
if (isValidPath_(*i)) res.insert(*i);
for (auto & i : paths)
if (isValidPath_(i)) res.insert(i);
return res;
} end_retry_sqlite;
}
@ -1212,14 +1212,14 @@ template<class T> T LocalStore::getIntLineFromSubstituter(RunningSubstituter & r
PathSet LocalStore::querySubstitutablePaths(const PathSet & paths)
{
PathSet res;
foreach (Paths::iterator, i, settings.substituters) {
for (auto & i : settings.substituters) {
if (res.size() == paths.size()) break;
RunningSubstituter & run(runningSubstituters[*i]);
startSubstituter(*i, run);
RunningSubstituter & run(runningSubstituters[i]);
startSubstituter(i, run);
if (run.disabled) continue;
string s = "have ";
foreach (PathSet::const_iterator, j, paths)
if (res.find(*j) == res.end()) { s += *j; s += " "; }
for (auto & j : paths)
if (res.find(j) == res.end()) { s += j; s += " "; }
writeLine(run.to, s);
while (true) {
/* FIXME: we only read stderr when an error occurs, so
@ -1243,8 +1243,8 @@ void LocalStore::querySubstitutablePathInfos(const Path & substituter,
if (run.disabled) return;
string s = "info ";
foreach (PathSet::const_iterator, i, paths)
if (infos.find(*i) == infos.end()) { s += *i; s += " "; }
for (auto & i : paths)
if (infos.find(i) == infos.end()) { s += i; s += " "; }
writeLine(run.to, s);
while (true) {
@ -1272,9 +1272,9 @@ void LocalStore::querySubstitutablePathInfos(const PathSet & paths,
SubstitutablePathInfos & infos)
{
PathSet todo = paths;
foreach (Paths::iterator, i, settings.substituters) {
for (auto & i : settings.substituters) {
if (todo.empty()) break;
querySubstitutablePathInfos(*i, todo, infos);
querySubstitutablePathInfos(i, todo, infos);
}
}
@ -1304,30 +1304,30 @@ void LocalStore::registerValidPaths(const ValidPathInfos & infos)
SQLiteTxn txn(db);
PathSet paths;
foreach (ValidPathInfos::const_iterator, i, infos) {
assert(i->hash.type == htSHA256);
if (isValidPath_(i->path))
updatePathInfo(*i);
for (auto & i : infos) {
assert(i.hash.type == htSHA256);
if (isValidPath_(i.path))
updatePathInfo(i);
else
addValidPath(*i, false);
paths.insert(i->path);
addValidPath(i, false);
paths.insert(i.path);
}
foreach (ValidPathInfos::const_iterator, i, infos) {
unsigned long long referrer = queryValidPathId(i->path);
foreach (PathSet::iterator, j, i->references)
addReference(referrer, queryValidPathId(*j));
for (auto & i : infos) {
unsigned long long referrer = queryValidPathId(i.path);
for (auto & j : i.references)
addReference(referrer, queryValidPathId(j));
}
/* Check that the derivation outputs are correct. We can't do
this in addValidPath() above, because the references might
not be valid yet. */
foreach (ValidPathInfos::const_iterator, i, infos)
if (isDerivation(i->path)) {
for (auto & i : infos)
if (isDerivation(i.path)) {
// FIXME: inefficient; we already loaded the
// derivation in addValidPath().
Derivation drv = readDerivation(i->path);
checkDerivationOutputs(i->path, drv);
Derivation drv = readDerivation(i.path);
checkDerivationOutputs(i.path, drv);
}
/* Do a topological sort of the paths. This will throw an
@ -1761,8 +1761,8 @@ bool LocalStore::verifyStore(bool checkContents, bool repair)
PathSet validPaths2 = queryAllValidPaths(), validPaths, done;
foreach (PathSet::iterator, i, validPaths2)
verifyPath(*i, store, done, validPaths, repair, errors);
for (auto & i : validPaths2)
verifyPath(i, store, done, validPaths, repair, errors);
/* Release the GC lock so that checking content hashes (which can
take ages) doesn't block the GC or builds. */
@ -1774,33 +1774,33 @@ bool LocalStore::verifyStore(bool checkContents, bool repair)
Hash nullHash(htSHA256);
foreach (PathSet::iterator, i, validPaths) {
for (auto & i : validPaths) {
try {
ValidPathInfo info = queryPathInfo(*i);
ValidPathInfo info = queryPathInfo(i);
/* Check the content hash (optionally - slow). */
printMsg(lvlTalkative, format("checking contents of %1%") % *i);
HashResult current = hashPath(info.hash.type, *i);
printMsg(lvlTalkative, format("checking contents of %1%") % i);
HashResult current = hashPath(info.hash.type, i);
if (info.hash != nullHash && info.hash != current.first) {
printMsg(lvlError, format("path %1% was modified! "
"expected hash %2%, got %3%")
% *i % printHash(info.hash) % printHash(current.first));
if (repair) repairPath(*i); else errors = true;
% i % printHash(info.hash) % printHash(current.first));
if (repair) repairPath(i); else errors = true;
} else {
bool update = false;
/* Fill in missing hashes. */
if (info.hash == nullHash) {
printMsg(lvlError, format("fixing missing hash on %1%") % *i);
printMsg(lvlError, format("fixing missing hash on %1%") % i);
info.hash = current.first;
update = true;
}
/* Fill in missing narSize fields (from old stores). */
if (info.narSize == 0) {
printMsg(lvlError, format("updating size field on %1% to %2%") % *i % current.second);
printMsg(lvlError, format("updating size field on %1% to %2%") % i % current.second);
info.narSize = current.second;
update = true;
}
@ -1812,7 +1812,7 @@ bool LocalStore::verifyStore(bool checkContents, bool repair)
} catch (Error & e) {
/* It's possible that the path got GC'ed, so ignore
errors on invalid paths. */
if (isValidPath(*i))
if (isValidPath(i))
printMsg(lvlError, format("error: %1%") % e.msg());
else
printMsg(lvlError, format("warning: %1%") % e.msg());
@ -1844,10 +1844,10 @@ void LocalStore::verifyPath(const Path & path, const PathSet & store,
first, then we can invalidate this path as well. */
bool canInvalidate = true;
PathSet referrers; queryReferrers(path, referrers);
foreach (PathSet::iterator, i, referrers)
if (*i != path) {
verifyPath(*i, store, done, validPaths, repair, errors);
if (validPaths.find(*i) != validPaths.end())
for (auto & i : referrers)
if (i != path) {
verifyPath(i, store, done, validPaths, repair, errors);
if (validPaths.find(i) != validPaths.end())
canInvalidate = false;
}
@ -1925,12 +1925,12 @@ ValidPathInfo LocalStore::queryPathInfoOld(const Path & path)
/* Parse it. */
Strings lines = tokenizeString<Strings>(info, "\n");
foreach (Strings::iterator, i, lines) {
string::size_type p = i->find(':');
for (auto & i : lines) {
string::size_type p = i.find(':');
if (p == string::npos)
throw Error(format("corrupt line in %1%: %2%") % infoFile % *i);
string name(*i, 0, p);
string value(*i, p + 2);
throw Error(format("corrupt line in %1%: %2%") % infoFile % i);
string name(i, 0, p);
string value(i, p + 2);
if (name == "References") {
Strings refs = tokenizeString<Strings>(value, " ");
res.references = PathSet(refs.begin(), refs.end());
@ -1960,18 +1960,18 @@ void LocalStore::upgradeStore6()
SQLiteTxn txn(db);
foreach (PathSet::iterator, i, validPaths) {
addValidPath(queryPathInfoOld(*i), false);
for (auto & i : validPaths) {
addValidPath(queryPathInfoOld(i), false);
std::cerr << ".";
}
std::cerr << "|";
foreach (PathSet::iterator, i, validPaths) {
ValidPathInfo info = queryPathInfoOld(*i);
unsigned long long referrer = queryValidPathId(*i);
foreach (PathSet::iterator, j, info.references)
addReference(referrer, queryValidPathId(*j));
for (auto & i : validPaths) {
ValidPathInfo info = queryPathInfoOld(i);
unsigned long long referrer = queryValidPathId(i);
for (auto & j : info.references)
addReference(referrer, queryValidPathId(j));
std::cerr << ".";
}