1
0
Fork 1
mirror of https://github.com/NixOS/nixpkgs.git synced 2024-11-26 07:31:20 +00:00

Hold state and autoArgs by reference

Switch from convention "appease clang-tidy --checks='*'" to
"References are like non-nullptr pointers".  The clang-tidy check
"google-runtime-references" complains about non-const reference
arguments, but this is not a convention used in Nix.
This commit is contained in:
Chuck 2019-09-16 09:10:29 -07:00 committed by Linus Heckemann
parent c457766a1f
commit c967e3fd3e

View file

@ -104,12 +104,12 @@ Out::Out(Out & o, const std::string & start, const std::string & end, LinePolicy
// Stuff needed for evaluation // Stuff needed for evaluation
struct Context struct Context
{ {
Context(EvalState * state, Bindings * autoArgs, Value options_root, Value config_root) Context(EvalState & state, Bindings & autoArgs, Value options_root, Value config_root)
: state(state), autoArgs(autoArgs), options_root(options_root), config_root(config_root), : state(state), autoArgs(autoArgs), options_root(options_root), config_root(config_root),
underscore_type(state->symbols.create("_type")) underscore_type(state.symbols.create("_type"))
{} {}
EvalState * state; EvalState & state;
Bindings * autoArgs; Bindings & autoArgs;
Value options_root; Value options_root;
Value config_root; Value config_root;
Symbol underscore_type; Symbol underscore_type;
@ -117,12 +117,12 @@ struct Context
Value evaluateValue(Context * ctx, Value * v) Value evaluateValue(Context * ctx, Value * v)
{ {
ctx->state->forceValue(*v); ctx->state.forceValue(*v);
if (ctx->autoArgs->empty()) { if (ctx->autoArgs.empty()) {
return *v; return *v;
} }
Value called{}; Value called{};
ctx->state->autoCallFunction(*ctx->autoArgs, *v, called); ctx->state.autoCallFunction(ctx->autoArgs, *v, called);
return called; return called;
} }
@ -238,7 +238,7 @@ void mapConfigValuesInOption(
{ {
Value * option; Value * option;
try { try {
option = findAlongAttrPath(*ctx->state, path, *ctx->autoArgs, ctx->config_root); option = findAlongAttrPath(ctx->state, path, ctx->autoArgs, ctx->config_root);
} catch (Error &) { } catch (Error &) {
f(path, std::current_exception()); f(path, std::current_exception());
return; return;
@ -246,7 +246,7 @@ void mapConfigValuesInOption(
recurse( recurse(
[f, ctx](const std::string & path, std::variant<Value, std::exception_ptr> v) { [f, ctx](const std::string & path, std::variant<Value, std::exception_ptr> v) {
bool leaf = std::holds_alternative<std::exception_ptr>(v) || std::get<Value>(v).type != tAttrs || bool leaf = std::holds_alternative<std::exception_ptr>(v) || std::get<Value>(v).type != tAttrs ||
ctx->state->isDerivation(std::get<Value>(v)); ctx->state.isDerivation(std::get<Value>(v));
if (!leaf) { if (!leaf) {
return true; // Keep digging return true; // Keep digging
} }
@ -261,20 +261,20 @@ std::string describeError(const Error & e) { return "«error: " + e.msg() + "»"
void describeDerivation(Context * ctx, Out & out, Value v) void describeDerivation(Context * ctx, Out & out, Value v)
{ {
// Copy-pasted from nix/src/nix/repl.cc :( // Copy-pasted from nix/src/nix/repl.cc :(
Bindings::iterator i = v.attrs->find(ctx->state->sDrvPath); Bindings::iterator i = v.attrs->find(ctx->state.sDrvPath);
PathSet pathset; PathSet pathset;
try { try {
Path drvPath = i != v.attrs->end() ? ctx->state->coerceToPath(*i->pos, *i->value, pathset) : "???"; Path drvPath = i != v.attrs->end() ? ctx->state.coerceToPath(*i->pos, *i->value, pathset) : "???";
out << "«derivation " << drvPath << "»"; out << "«derivation " << drvPath << "»";
} catch (Error & e) { } catch (Error & e) {
out << describeError(e); out << describeError(e);
} }
} }
Value parseAndEval(EvalState * state, const std::string & expression, const std::string & path) Value parseAndEval(EvalState & state, const std::string & expression, const std::string & path)
{ {
Value v{}; Value v{};
state->eval(state->parseExprFromString(expression, absPath(path)), v); state.eval(state.parseExprFromString(expression, absPath(path)), v);
return v; return v;
} }
@ -344,7 +344,7 @@ void printValue(Context * ctx, Out & out, std::variant<Value, std::exception_ptr
std::rethrow_exception(*ex); std::rethrow_exception(*ex);
} }
Value v = evaluateValue(ctx, &std::get<Value>(maybe_value)); Value v = evaluateValue(ctx, &std::get<Value>(maybe_value));
if (ctx->state->isDerivation(v)) { if (ctx->state.isDerivation(v)) {
describeDerivation(ctx, out, v); describeDerivation(ctx, out, v);
} else if (v.isList()) { } else if (v.isList()) {
printList(ctx, out, v); printList(ctx, out, v);
@ -353,7 +353,7 @@ void printValue(Context * ctx, Out & out, std::variant<Value, std::exception_ptr
} else if (v.type == tString && std::string(v.string.s).find('\n') != std::string::npos) { } else if (v.type == tString && std::string(v.string.s).find('\n') != std::string::npos) {
printMultiLineString(out, v); printMultiLineString(out, v);
} else { } else {
ctx->state->forceValueDeep(v); ctx->state.forceValueDeep(v);
out << v; out << v;
} }
} catch (ThrownError & e) { } catch (ThrownError & e) {
@ -397,7 +397,7 @@ void printAll(Context * ctx, Out & out)
void printAttr(Context * ctx, Out & out, const std::string & path, Value * root) void printAttr(Context * ctx, Out & out, const std::string & path, Value * root)
{ {
try { try {
printValue(ctx, out, *findAlongAttrPath(*ctx->state, path, *ctx->autoArgs, *root), path); printValue(ctx, out, *findAlongAttrPath(ctx->state, path, ctx->autoArgs, *root), path);
} catch (Error & e) { } catch (Error & e) {
out << describeError(e); out << describeError(e);
} }
@ -444,7 +444,7 @@ void printListing(Out & out, Value * v)
bool optionTypeIs(Context * ctx, Value & v, const std::string & sought_type) bool optionTypeIs(Context * ctx, Value & v, const std::string & sought_type)
{ {
try { try {
const auto & type_lookup = v.attrs->find(ctx->state->sType); const auto & type_lookup = v.attrs->find(ctx->state.sType);
if (type_lookup == v.attrs->end()) { if (type_lookup == v.attrs->end()) {
return false; return false;
} }
@ -452,7 +452,7 @@ bool optionTypeIs(Context * ctx, Value & v, const std::string & sought_type)
if (type.type != tAttrs) { if (type.type != tAttrs) {
return false; return false;
} }
const auto & name_lookup = type.attrs->find(ctx->state->sName); const auto & name_lookup = type.attrs->find(ctx->state.sName);
if (name_lookup == type.attrs->end()) { if (name_lookup == type.attrs->end()) {
return false; return false;
} }
@ -471,14 +471,14 @@ MakeError(OptionPathError, EvalError);
Value getSubOptions(Context * ctx, Value & option) Value getSubOptions(Context * ctx, Value & option)
{ {
Value getSubOptions = Value getSubOptions =
evaluateValue(ctx, findAlongAttrPath(*ctx->state, "type.getSubOptions", *ctx->autoArgs, option)); evaluateValue(ctx, findAlongAttrPath(ctx->state, "type.getSubOptions", ctx->autoArgs, option));
if (getSubOptions.type != tLambda) { if (getSubOptions.type != tLambda) {
throw OptionPathError("Option's type.getSubOptions isn't a function"); throw OptionPathError("Option's type.getSubOptions isn't a function");
} }
Value emptyString{}; Value emptyString{};
nix::mkString(emptyString, ""); nix::mkString(emptyString, "");
Value v; Value v;
ctx->state->callFunction(getSubOptions, emptyString, v, nix::Pos{}); ctx->state.callFunction(getSubOptions, emptyString, v, nix::Pos{});
return v; return v;
} }
@ -506,7 +506,7 @@ Value findAlongOptionPath(Context * ctx, const std::string & path)
} else if (v.type != tAttrs) { } else if (v.type != tAttrs) {
throw OptionPathError("Value is %s while a set was expected", showType(v)); throw OptionPathError("Value is %s while a set was expected", showType(v));
} else { } else {
const auto & next = v.attrs->find(ctx->state->symbols.create(attr)); const auto & next = v.attrs->find(ctx->state.symbols.create(attr));
if (next == v.attrs->end()) { if (next == v.attrs->end()) {
throw OptionPathError("Attribute not found", attr, path); throw OptionPathError("Attribute not found", attr, path);
} }
@ -579,10 +579,10 @@ int main(int argc, char ** argv)
auto store = nix::openStore(); auto store = nix::openStore();
auto state = std::make_unique<EvalState>(myArgs.searchPath, store); auto state = std::make_unique<EvalState>(myArgs.searchPath, store);
Value options_root = parseAndEval(state.get(), options_expr, path); Value options_root = parseAndEval(*state, options_expr, path);
Value config_root = parseAndEval(state.get(), config_expr, path); Value config_root = parseAndEval(*state, config_expr, path);
Context ctx{state.get(), myArgs.getAutoArgs(*state), options_root, config_root}; Context ctx{*state, *myArgs.getAutoArgs(*state), options_root, config_root};
Out out(std::cout); Out out(std::cout);
if (all) { if (all) {
@ -599,7 +599,7 @@ int main(int argc, char ** argv)
} }
} }
ctx.state->printStats(); ctx.state.printStats();
return 0; return 0;
} }