Skip to content

Commit

Permalink
OCD: foreach -> C++11 ranged for
Browse files Browse the repository at this point in the history
  • Loading branch information
edolstra committed Jul 17, 2015
1 parent 1511aa9 commit 6bd2c7b
Show file tree
Hide file tree
Showing 30 changed files with 849 additions and 874 deletions.
8 changes: 4 additions & 4 deletions src/download-via-ssh/download-via-ssh.cc
Expand Up @@ -64,8 +64,8 @@ static void query(std::pair<FdSink, FdSource> & pipes)
writeStrings(tokenized, pipes.first);
pipes.first.flush();
PathSet paths = readStrings<PathSet>(pipes.second);
foreach (PathSet::iterator, i, paths)
std::cout << *i << std::endl;
for (auto & i : paths)
std::cout << i << std::endl;
} else if (cmd == "info") {
writeInt(cmdQueryPathInfos, pipes.first);
writeStrings(tokenized, pipes.first);
Expand All @@ -80,8 +80,8 @@ static void query(std::pair<FdSink, FdSource> & pipes)
std::cout << deriver << std::endl;
PathSet references = readStorePaths<PathSet>(pipes.second);
std::cout << references.size() << std::endl;
foreach (PathSet::iterator, i, references)
std::cout << *i << std::endl;
for (auto & i : references)
std::cout << i << std::endl;
std::cout << readLongLong(pipes.second) << std::endl;
std::cout << readLongLong(pipes.second) << std::endl;
}
Expand Down
5 changes: 2 additions & 3 deletions src/libexpr/attr-path.cc
Expand Up @@ -42,11 +42,10 @@ Value * findAlongAttrPath(EvalState & state, const string & attrPath,

Value * v = &vIn;

foreach (Strings::iterator, i, tokens) {
for (auto & attr : tokens) {

/* Is *i an index (integer) or a normal attribute name? */
/* Is i an index (integer) or a normal attribute name? */
enum { apAttr, apIndex } apType = apAttr;
string attr = *i;
unsigned int attrIndex;
if (string2Int(attr, attrIndex)) apType = apIndex;

Expand Down
90 changes: 45 additions & 45 deletions src/libexpr/eval.cc
Expand Up @@ -98,8 +98,8 @@ static void printValue(std::ostream & str, std::set<const Value *> & active, con
str << "{ ";
typedef std::map<string, Value *> Sorted;
Sorted sorted;
foreach (Bindings::iterator, i, *v.attrs)
sorted[i->name] = i->value;
for (auto & i : *v.attrs)
sorted[i.name] = i.value;
for (auto & i : sorted) {
str << i.first << " = ";
printValue(str, active, *i.second);
Expand Down Expand Up @@ -442,8 +442,8 @@ void mkString(Value & v, const string & s, const PathSet & context)
unsigned int n = 0;
v.string.context = (const char * *)
allocBytes((context.size() + 1) * sizeof(char *));
foreach (PathSet::const_iterator, i, context)
v.string.context[n++] = dupString(i->c_str());
for (auto & i : context)
v.string.context[n++] = dupString(i.c_str());
v.string.context[n] = 0;
}
}
Expand Down Expand Up @@ -723,15 +723,15 @@ void ExprAttrs::eval(EvalState & state, Env & env, Value & v)
environment, while the inherited attributes are evaluated
in the original environment. */
unsigned int displ = 0;
foreach (AttrDefs::iterator, i, attrs) {
for (auto & i : attrs) {
Value * vAttr;
if (hasOverrides && !i->second.inherited) {
if (hasOverrides && !i.second.inherited) {
vAttr = state.allocValue();
mkThunk(*vAttr, env2, i->second.e);
mkThunk(*vAttr, env2, i.second.e);
} else
vAttr = i->second.e->maybeThunk(state, i->second.inherited ? env : env2);
vAttr = i.second.e->maybeThunk(state, i.second.inherited ? env : env2);
env2.values[displ++] = vAttr;
v.attrs->push_back(Attr(i->first, vAttr, &i->second.pos));
v.attrs->push_back(Attr(i.first, vAttr, &i.second.pos));
}

/* If the rec contains an attribute called `__overrides', then
Expand Down Expand Up @@ -762,25 +762,25 @@ void ExprAttrs::eval(EvalState & state, Env & env, Value & v)
}

else
foreach (AttrDefs::iterator, i, attrs)
v.attrs->push_back(Attr(i->first, i->second.e->maybeThunk(state, env), &i->second.pos));
for (auto & i : attrs)
v.attrs->push_back(Attr(i.first, i.second.e->maybeThunk(state, env), &i.second.pos));

/* Dynamic attrs apply *after* rec and __overrides. */
foreach (DynamicAttrDefs::iterator, i, dynamicAttrs) {
for (auto & i : dynamicAttrs) {
Value nameVal;
i->nameExpr->eval(state, *dynamicEnv, nameVal);
i.nameExpr->eval(state, *dynamicEnv, nameVal);
state.forceValue(nameVal);
if (nameVal.type == tNull)
continue;
state.forceStringNoCtx(nameVal);
Symbol nameSym = state.symbols.create(nameVal.string.s);
Bindings::iterator j = v.attrs->find(nameSym);
if (j != v.attrs->end())
throwEvalError("dynamic attribute ‘%1%’ at %2% already defined at %3%", nameSym, i->pos, *j->pos);
throwEvalError("dynamic attribute ‘%1%’ at %2% already defined at %3%", nameSym, i.pos, *j->pos);

i->valueExpr->setName(nameSym);
i.valueExpr->setName(nameSym);
/* Keep sorted order so find can catch duplicates */
v.attrs->push_back(Attr(nameSym, i->valueExpr->maybeThunk(state, *dynamicEnv), &i->pos));
v.attrs->push_back(Attr(nameSym, i.valueExpr->maybeThunk(state, *dynamicEnv), &i.pos));
v.attrs->sort(); // FIXME: inefficient
}
}
Expand All @@ -797,8 +797,8 @@ void ExprLet::eval(EvalState & state, Env & env, Value & v)
while the inherited attributes are evaluated in the original
environment. */
unsigned int displ = 0;
foreach (ExprAttrs::AttrDefs::iterator, i, attrs->attrs)
env2.values[displ++] = i->second.e->maybeThunk(state, i->second.inherited ? env : env2);
for (auto & i : attrs->attrs)
env2.values[displ++] = i.second.e->maybeThunk(state, i.second.inherited ? env : env2);

body->eval(state, env2, v);
}
Expand Down Expand Up @@ -849,10 +849,10 @@ void ExprSelect::eval(EvalState & state, Env & env, Value & v)

try {

foreach (AttrPath::const_iterator, i, attrPath) {
for (auto & i : attrPath) {
nrLookups++;
Bindings::iterator j;
Symbol name = getName(*i, state, env);
Symbol name = getName(i, state, env);
if (def) {
state.forceValue(*vAttrs);
if (vAttrs->type != tAttrs ||
Expand Down Expand Up @@ -891,10 +891,10 @@ void ExprOpHasAttr::eval(EvalState & state, Env & env, Value & v)

e->eval(state, env, vTmp);

foreach (AttrPath::const_iterator, i, attrPath) {
for (auto & i : attrPath) {
state.forceValue(*vAttrs);
Bindings::iterator j;
Symbol name = getName(*i, state, env);
Symbol name = getName(i, state, env);
if (vAttrs->type != tAttrs ||
(j = vAttrs->attrs->find(name)) == vAttrs->attrs->end())
{
Expand Down Expand Up @@ -1007,12 +1007,12 @@ void EvalState::callFunction(Value & fun, Value & arg, Value & v, const Pos & po
there is no matching actual argument but the formal
argument has a default, use the default. */
unsigned int attrsUsed = 0;
foreach (Formals::Formals_::iterator, i, lambda.formals->formals) {
Bindings::iterator j = arg.attrs->find(i->name);
for (auto & i : lambda.formals->formals) {
Bindings::iterator j = arg.attrs->find(i.name);
if (j == arg.attrs->end()) {
if (!i->def) throwTypeError("%1% called without required argument ‘%2%’, at %3%",
lambda, i->name, pos);
env2.values[displ++] = i->def->maybeThunk(*this, env2);
if (!i.def) throwTypeError("%1% called without required argument ‘%2%’, at %3%",
lambda, i.name, pos);
env2.values[displ++] = i.def->maybeThunk(*this, env2);
} else {
attrsUsed++;
env2.values[displ++] = j->value;
Expand All @@ -1024,9 +1024,9 @@ void EvalState::callFunction(Value & fun, Value & arg, Value & v, const Pos & po
if (!lambda.formals->ellipsis && attrsUsed != arg.attrs->size()) {
/* Nope, so show the first unexpected argument to the
user. */
foreach (Bindings::iterator, i, *arg.attrs)
if (lambda.formals->argNames.find(i->name) == lambda.formals->argNames.end())
throwTypeError("%1% called with unexpected argument ‘%2%’, at %3%", lambda, i->name, pos);
for (auto & i : *arg.attrs)
if (lambda.formals->argNames.find(i.name) == lambda.formals->argNames.end())
throwTypeError("%1% called with unexpected argument ‘%2%’, at %3%", lambda, i.name, pos);
abort(); // can't happen
}
}
Expand Down Expand Up @@ -1068,12 +1068,12 @@ void EvalState::autoCallFunction(Bindings & args, Value & fun, Value & res)
Value * actualArgs = allocValue();
mkAttrs(*actualArgs, fun.lambda.fun->formals->formals.size());

foreach (Formals::Formals_::iterator, i, fun.lambda.fun->formals->formals) {
Bindings::iterator j = args.find(i->name);
for (auto & i : fun.lambda.fun->formals->formals) {
Bindings::iterator j = args.find(i.name);
if (j != args.end())
actualArgs->attrs->push_back(*j);
else if (!i->def)
throwTypeError("cannot auto-call a function that has an argument without a default value (‘%1%’)", i->name);
else if (!i.def)
throwTypeError("cannot auto-call a function that has an argument without a default value (‘%1%’)", i.name);
}

actualArgs->attrs->sort();
Expand Down Expand Up @@ -1229,9 +1229,9 @@ void ExprConcatStrings::eval(EvalState & state, Env & env, Value & v)
bool first = !forceString;
ValueType firstType = tString;

foreach (vector<Expr *>::iterator, i, *es) {
for (auto & i : *es) {
Value vTmp;
(*i)->eval(state, env, vTmp);
i->eval(state, env, vTmp);

/* If the first element is a path, then the result will also
be a path, we don't copy anything (yet - that's done later,
Expand Down Expand Up @@ -1583,25 +1583,25 @@ void EvalState::printStats()
printMsg(v, format("calls to %1% primops:") % primOpCalls.size());
typedef std::multimap<unsigned int, Symbol> PrimOpCalls_;
PrimOpCalls_ primOpCalls_;
foreach (PrimOpCalls::iterator, i, primOpCalls)
primOpCalls_.insert(std::pair<unsigned int, Symbol>(i->second, i->first));
foreach_reverse (PrimOpCalls_::reverse_iterator, i, primOpCalls_)
for (auto & i : primOpCalls)
primOpCalls_.insert(std::pair<unsigned int, Symbol>(i.second, i.first));
for (auto i = primOpCalls_.rbegin(); i != primOpCalls_.rend(); ++i)
printMsg(v, format("%1$10d %2%") % i->first % i->second);

printMsg(v, format("calls to %1% functions:") % functionCalls.size());
typedef std::multimap<unsigned int, ExprLambda *> FunctionCalls_;
FunctionCalls_ functionCalls_;
foreach (FunctionCalls::iterator, i, functionCalls)
functionCalls_.insert(std::pair<unsigned int, ExprLambda *>(i->second, i->first));
foreach_reverse (FunctionCalls_::reverse_iterator, i, functionCalls_)
for (auto & i : functionCalls)
functionCalls_.insert(std::pair<unsigned int, ExprLambda *>(i.second, i.first));
for (auto i = functionCalls_.rbegin(); i != functionCalls_.rend(); ++i)
printMsg(v, format("%1$10d %2%") % i->first % i->second->showNamePos());

printMsg(v, format("evaluations of %1% attributes:") % attrSelects.size());
typedef std::multimap<unsigned int, Pos> AttrSelects_;
AttrSelects_ attrSelects_;
foreach (AttrSelects::iterator, i, attrSelects)
attrSelects_.insert(std::pair<unsigned int, Pos>(i->second, i->first));
foreach_reverse (AttrSelects_::reverse_iterator, i, attrSelects_)
for (auto & i : attrSelects)
attrSelects_.insert(std::pair<unsigned int, Pos>(i.second, i.first));
for (auto i = attrSelects_.rbegin(); i != attrSelects_.rend(); ++i)
printMsg(v, format("%1$10d %2%") % i->first % i->second);

}
Expand Down
20 changes: 10 additions & 10 deletions src/libexpr/get-drvs.cc
Expand Up @@ -85,8 +85,8 @@ StringSet DrvInfo::queryMetaNames()
{
StringSet res;
if (!getMeta()) return res;
foreach (Bindings::iterator, i, *meta)
res.insert(i->name);
for (auto & i : *meta)
res.insert(i.name);
return res;
}

Expand All @@ -102,8 +102,8 @@ bool DrvInfo::checkMeta(Value & v)
else if (v.type == tAttrs) {
Bindings::iterator i = v.attrs->find(state->sOutPath);
if (i != v.attrs->end()) return false;
foreach (Bindings::iterator, i, *v.attrs)
if (!checkMeta(*i->value)) return false;
for (auto & i : *v.attrs)
if (!checkMeta(*i.value)) return false;
return true;
}
else return v.type == tInt || v.type == tBool || v.type == tString;
Expand Down Expand Up @@ -255,13 +255,13 @@ static void getDerivations(EvalState & state, Value & vIn,
precedence). */
typedef std::map<string, Symbol> SortedSymbols;
SortedSymbols attrs;
foreach (Bindings::iterator, i, *v.attrs)
attrs.insert(std::pair<string, Symbol>(i->name, i->name));
for (auto & i : *v.attrs)
attrs.insert(std::pair<string, Symbol>(i.name, i.name));

foreach (SortedSymbols::iterator, i, attrs) {
startNest(nest, lvlDebug, format("evaluating attribute ‘%1%’") % i->first);
string pathPrefix2 = addToPath(pathPrefix, i->first);
Value & v2(*v.attrs->find(i->second)->value);
for (auto & i : attrs) {
startNest(nest, lvlDebug, format("evaluating attribute ‘%1%’") % i.first);
string pathPrefix2 = addToPath(pathPrefix, i.first);
Value & v2(*v.attrs->find(i.second)->value);
if (combineChannels)
getDerivations(state, v2, pathPrefix2, autoArgs, drvs, done, ignoreAssertionFailures);
else if (getDerivation(state, v2, pathPrefix2, drvs, done, ignoreAssertionFailures)) {
Expand Down
4 changes: 2 additions & 2 deletions src/libexpr/names.cc
Expand Up @@ -98,8 +98,8 @@ int compareVersions(const string & v1, const string & v2)
DrvNames drvNamesFromArgs(const Strings & opArgs)
{
DrvNames result;
foreach (Strings::const_iterator, i, opArgs)
result.push_back(DrvName(*i));
for (auto & i : opArgs)
result.push_back(DrvName(i));
return result;
}

Expand Down

0 comments on commit 6bd2c7b

Please sign in to comment.