From 2cfcb6f44242c7789d481b57351ca5f9c2dc9b28 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=B6rg=20Thalheim?= Date: Wed, 13 Nov 2024 22:30:42 +0100 Subject: [PATCH] nixos-option: don't throw errors in main https://clang.llvm.org/extra/clang-tidy/checks/bugprone/exception-escape.html --- .../nix/nixos-option/src/nixos-option.cc | 116 +++++++++--------- 1 file changed, 59 insertions(+), 57 deletions(-) diff --git a/pkgs/tools/nix/nixos-option/src/nixos-option.cc b/pkgs/tools/nix/nixos-option/src/nixos-option.cc index 6361a6d414a7..0c8a0be72329 100644 --- a/pkgs/tools/nix/nixos-option/src/nixos-option.cc +++ b/pkgs/tools/nix/nixos-option/src/nixos-option.cc @@ -593,65 +593,67 @@ void printOne(Context & ctx, Out & out, const std::string & path) int main(int argc, char ** argv) { - bool recursive = false; - std::string path = "."; - std::string optionsExpr = "(import {}).options"; - std::string configExpr = "(import {}).config"; - std::vector args; + return nix::handleExceptions(argv[0], [&]() { + bool recursive = false; + std::string path = "."; + std::string optionsExpr = "(import {}).options"; + std::string configExpr = "(import {}).config"; + std::vector args; - struct MyArgs : nix::LegacyArgs, nix::MixEvalArgs - { - using nix::LegacyArgs::LegacyArgs; - }; + struct MyArgs : nix::LegacyArgs, nix::MixEvalArgs + { + using nix::LegacyArgs::LegacyArgs; + }; - MyArgs myArgs(std::string(nix::baseNameOf(argv[0])), [&](Strings::iterator & arg, const Strings::iterator & end) { - if (*arg == "--help") { - nix::showManPage("nixos-option"); - } else if (*arg == "--version") { - nix::printVersion("nixos-option"); - } else if (*arg == "-r" || *arg == "--recursive") { - recursive = true; - } else if (*arg == "--path") { - path = nix::getArg(*arg, arg, end); - } else if (*arg == "--options_expr") { - optionsExpr = nix::getArg(*arg, arg, end); - } else if (*arg == "--config_expr") { - configExpr = nix::getArg(*arg, arg, end); - } else if (!arg->empty() && arg->at(0) == '-') { - return false; - } else { - args.push_back(*arg); + MyArgs myArgs(std::string(nix::baseNameOf(argv[0])), [&](Strings::iterator & arg, const Strings::iterator & end) { + if (*arg == "--help") { + nix::showManPage("nixos-option"); + } else if (*arg == "--version") { + nix::printVersion("nixos-option"); + } else if (*arg == "-r" || *arg == "--recursive") { + recursive = true; + } else if (*arg == "--path") { + path = nix::getArg(*arg, arg, end); + } else if (*arg == "--options_expr") { + optionsExpr = nix::getArg(*arg, arg, end); + } else if (*arg == "--config_expr") { + configExpr = nix::getArg(*arg, arg, end); + } else if (!arg->empty() && arg->at(0) == '-') { + return false; + } else { + args.push_back(*arg); + } + return true; + }); + + myArgs.parseCmdline(nix::argvToStrings(argc, argv)); + + nix::initNix(); + nix::initGC(); + nix::settings.readOnlyMode = true; + auto store = nix::openStore(); + + auto evalStore = myArgs.evalStoreUrl ? nix::openStore(*myArgs.evalStoreUrl) + : nix::openStore(); + auto state = nix::make_ref( + myArgs.lookupPath, evalStore, nix::fetchSettings, nix::evalSettings); + + Value optionsRoot = parseAndEval(*state, optionsExpr, path); + Value configRoot = parseAndEval(*state, configExpr, path); + + Context ctx{*state, *myArgs.getAutoArgs(*state), optionsRoot, configRoot}; + Out out(std::cout); + + auto print = recursive ? printRecursive : printOne; + if (args.empty()) { + print(ctx, out, ""); } - return true; + for (const auto & arg : args) { + print(ctx, out, arg); + } + + ctx.state.maybePrintStats(); + + return 0; }); - - myArgs.parseCmdline(nix::argvToStrings(argc, argv)); - - nix::initNix(); - nix::initGC(); - nix::settings.readOnlyMode = true; - auto store = nix::openStore(); - - auto evalStore = myArgs.evalStoreUrl ? nix::openStore(*myArgs.evalStoreUrl) - : nix::openStore(); - auto state = nix::make_ref( - myArgs.lookupPath, evalStore, nix::fetchSettings, nix::evalSettings); - - Value optionsRoot = parseAndEval(*state, optionsExpr, path); - Value configRoot = parseAndEval(*state, configExpr, path); - - Context ctx{*state, *myArgs.getAutoArgs(*state), optionsRoot, configRoot}; - Out out(std::cout); - - auto print = recursive ? printRecursive : printOne; - if (args.empty()) { - print(ctx, out, ""); - } - for (const auto & arg : args) { - print(ctx, out, arg); - } - - ctx.state.maybePrintStats(); - - return 0; }