about summary refs log tree commit diff
path: root/third_party/nix/src/nix
diff options
context:
space:
mode:
authorVincent Ambo <tazjin@google.com>2020-05-20T21·58+0100
committerVincent Ambo <tazjin@google.com>2020-05-20T21·58+0100
commit43677021e3c285c2ced2075b918da947e13fcb00 (patch)
tree261d2ecbc6a9492d6410f9f4e8fd6629f20b49e6 /third_party/nix/src/nix
parent689ef502f5b0655c9923ed77da2ae3504630f473 (diff)
refactor(3p/nix): Apply clang-tidy's performance-* fixes r/789
This applies the performance fixes listed here:

https://clang.llvm.org/extra/clang-tidy/checks/list.html
Diffstat (limited to '')
-rw-r--r--third_party/nix/src/nix-build/nix-build.cc2
-rw-r--r--third_party/nix/src/nix-collect-garbage/nix-collect-garbage.cc2
-rw-r--r--third_party/nix/src/nix-daemon/nix-daemon.cc6
-rw-r--r--third_party/nix/src/nix-env/nix-env.cc4
-rw-r--r--third_party/nix/src/nix-store/dotgraph.cc2
-rw-r--r--third_party/nix/src/nix-store/dotgraph.hh2
-rw-r--r--third_party/nix/src/nix-store/graphml.cc2
-rw-r--r--third_party/nix/src/nix-store/graphml.hh2
-rw-r--r--third_party/nix/src/nix-store/nix-store.cc3
-rw-r--r--third_party/nix/src/nix/cat.cc2
-rw-r--r--third_party/nix/src/nix/command.hh22
-rw-r--r--third_party/nix/src/nix/doctor.cc2
-rw-r--r--third_party/nix/src/nix/hash.cc4
-rw-r--r--third_party/nix/src/nix/installables.cc29
-rw-r--r--third_party/nix/src/nix/ls.cc2
-rw-r--r--third_party/nix/src/nix/repl.cc15
-rw-r--r--third_party/nix/src/nix/run.cc4
-rw-r--r--third_party/nix/src/nix/search.cc8
-rw-r--r--third_party/nix/src/nix/upgrade-nix.cc4
-rw-r--r--third_party/nix/src/nix/verify.cc4
20 files changed, 65 insertions, 56 deletions
diff --git a/third_party/nix/src/nix-build/nix-build.cc b/third_party/nix/src/nix-build/nix-build.cc
index 5090cbe51370..921fc2ba22d1 100644
--- a/third_party/nix/src/nix-build/nix-build.cc
+++ b/third_party/nix/src/nix-build/nix-build.cc
@@ -299,7 +299,7 @@ static void _main(int argc, char** argv) {
   if (readStdin) {
     exprs = {state->parseStdin()};
   } else {
-    for (auto i : left) {
+    for (const auto& i : left) {
       if (fromArgs) {
         exprs.push_back(state->parseExprFromString(i, absPath(".")));
       } else {
diff --git a/third_party/nix/src/nix-collect-garbage/nix-collect-garbage.cc b/third_party/nix/src/nix-collect-garbage/nix-collect-garbage.cc
index c3ff068887b5..0050bdda15fb 100644
--- a/third_party/nix/src/nix-collect-garbage/nix-collect-garbage.cc
+++ b/third_party/nix/src/nix-collect-garbage/nix-collect-garbage.cc
@@ -18,7 +18,7 @@ bool dryRun = false;
  * Of course, this makes rollbacks to before this point in time
  * impossible. */
 
-void removeOldGenerations(std::string dir) {
+void removeOldGenerations(const std::string& dir) {
   if (access(dir.c_str(), R_OK) != 0) {
     return;
   }
diff --git a/third_party/nix/src/nix-daemon/nix-daemon.cc b/third_party/nix/src/nix-daemon/nix-daemon.cc
index 060cc10497b9..9f5e8b7cbedb 100644
--- a/third_party/nix/src/nix-daemon/nix-daemon.cc
+++ b/third_party/nix/src/nix-daemon/nix-daemon.cc
@@ -194,9 +194,9 @@ struct RetrieveRegularNARSink : ParseSink {
   }
 };
 
-static void performOp(TunnelLogger* logger, ref<Store> store, bool trusted,
-                      unsigned int clientVersion, Source& from, Sink& to,
-                      unsigned int op) {
+static void performOp(TunnelLogger* logger, const ref<Store>& store,
+                      bool trusted, unsigned int clientVersion, Source& from,
+                      Sink& to, unsigned int op) {
   switch (op) {
     case wopIsValidPath: {
       /* 'readStorePath' could raise an error leading to the connection
diff --git a/third_party/nix/src/nix-env/nix-env.cc b/third_party/nix/src/nix-env/nix-env.cc
index ec2da8f83502..95a395a96fcc 100644
--- a/third_party/nix/src/nix-env/nix-env.cc
+++ b/third_party/nix/src/nix-env/nix-env.cc
@@ -171,8 +171,8 @@ static void loadSourceExpr(EvalState& state, const Path& path, Value& v) {
   }
 }
 
-static void loadDerivations(EvalState& state, Path nixExprPath,
-                            string systemFilter, Bindings& autoArgs,
+static void loadDerivations(EvalState& state, const Path& nixExprPath,
+                            const string& systemFilter, Bindings& autoArgs,
                             const string& pathPrefix, DrvInfos& elems) {
   Value vRoot;
   loadSourceExpr(state, nixExprPath, vRoot);
diff --git a/third_party/nix/src/nix-store/dotgraph.cc b/third_party/nix/src/nix-store/dotgraph.cc
index 259ecbd74a5e..0186a7b22ebd 100644
--- a/third_party/nix/src/nix-store/dotgraph.cc
+++ b/third_party/nix/src/nix-store/dotgraph.cc
@@ -79,7 +79,7 @@ void printClosure(const Path & nePath, const StoreExpr & fs)
 }
 #endif
 
-void printDotGraph(ref<Store> store, const PathSet& roots) {
+void printDotGraph(const ref<Store>& store, const PathSet& roots) {
   PathSet workList(roots);
   PathSet doneSet;
 
diff --git a/third_party/nix/src/nix-store/dotgraph.hh b/third_party/nix/src/nix-store/dotgraph.hh
index c7069451e114..20fc357b1bd4 100644
--- a/third_party/nix/src/nix-store/dotgraph.hh
+++ b/third_party/nix/src/nix-store/dotgraph.hh
@@ -6,6 +6,6 @@ namespace nix {
 
 class Store;
 
-void printDotGraph(ref<Store> store, const PathSet& roots);
+void printDotGraph(const ref<Store>& store, const PathSet& roots);
 
 }  // namespace nix
diff --git a/third_party/nix/src/nix-store/graphml.cc b/third_party/nix/src/nix-store/graphml.cc
index 6510b328810d..a1a16cc8f28a 100644
--- a/third_party/nix/src/nix-store/graphml.cc
+++ b/third_party/nix/src/nix-store/graphml.cc
@@ -37,7 +37,7 @@ static string makeNode(const ValidPathInfo& info) {
       (isDerivation(info.path) ? "derivation" : "output-path"));
 }
 
-void printGraphML(ref<Store> store, const PathSet& roots) {
+void printGraphML(const ref<Store>& store, const PathSet& roots) {
   PathSet workList(roots);
   PathSet doneSet;
   std::pair<PathSet::iterator, bool> ret;
diff --git a/third_party/nix/src/nix-store/graphml.hh b/third_party/nix/src/nix-store/graphml.hh
index c330dc6abc43..199421acb264 100644
--- a/third_party/nix/src/nix-store/graphml.hh
+++ b/third_party/nix/src/nix-store/graphml.hh
@@ -6,6 +6,6 @@ namespace nix {
 
 class Store;
 
-void printGraphML(ref<Store> store, const PathSet& roots);
+void printGraphML(const ref<Store>& store, const PathSet& roots);
 
 }  // namespace nix
diff --git a/third_party/nix/src/nix-store/nix-store.cc b/third_party/nix/src/nix-store/nix-store.cc
index 802ebd420792..40d6f01cd96c 100644
--- a/third_party/nix/src/nix-store/nix-store.cc
+++ b/third_party/nix/src/nix-store/nix-store.cc
@@ -28,6 +28,9 @@ using namespace nix;
 using std::cin;
 using std::cout;
 
+// TODO(tazjin): clang-tidy's performance lints don't like this, but
+// the automatic fixes fail (it seems that some of the ops want to own
+// the args for whatever reason)
 using Operation = void (*)(Strings, Strings);
 
 static Path gcRoot;
diff --git a/third_party/nix/src/nix/cat.cc b/third_party/nix/src/nix/cat.cc
index 9b2da929ac33..2d662a453f54 100644
--- a/third_party/nix/src/nix/cat.cc
+++ b/third_party/nix/src/nix/cat.cc
@@ -8,7 +8,7 @@ using namespace nix;
 struct MixCat : virtual Args {
   std::string path;
 
-  void cat(ref<FSAccessor> accessor) {
+  void cat(const ref<FSAccessor>& accessor) {
     auto st = accessor->stat(path);
     if (st.type == FSAccessor::Type::tMissing) {
       throw Error(format("path '%1%' does not exist") % path);
diff --git a/third_party/nix/src/nix/command.hh b/third_party/nix/src/nix/command.hh
index 829ce080d3a2..4122bdd0e58f 100644
--- a/third_party/nix/src/nix/command.hh
+++ b/third_party/nix/src/nix/command.hh
@@ -171,21 +171,23 @@ struct RegisterCommand {
 };
 
 std::shared_ptr<Installable> parseInstallable(SourceExprCommand& cmd,
-                                              ref<Store> store,
+                                              const ref<Store>& store,
                                               const std::string& installable,
                                               bool useDefaultInstallables);
 
-Buildables build(ref<Store> store, RealiseMode mode,
-                 std::vector<std::shared_ptr<Installable>> installables);
+Buildables build(const ref<Store>& store, RealiseMode mode,
+                 const std::vector<std::shared_ptr<Installable>>& installables);
 
-PathSet toStorePaths(ref<Store> store, RealiseMode mode,
-                     std::vector<std::shared_ptr<Installable>> installables);
+PathSet toStorePaths(
+    const ref<Store>& store, RealiseMode mode,
+    const std::vector<std::shared_ptr<Installable>>& installables);
 
-Path toStorePath(ref<Store> store, RealiseMode mode,
-                 std::shared_ptr<Installable> installable);
+Path toStorePath(const ref<Store>& store, RealiseMode mode,
+                 const std::shared_ptr<Installable>& installable);
 
-PathSet toDerivations(ref<Store> store,
-                      std::vector<std::shared_ptr<Installable>> installables,
-                      bool useDeriver = false);
+PathSet toDerivations(
+    const ref<Store>& store,
+    const std::vector<std::shared_ptr<Installable>>& installables,
+    bool useDeriver = false);
 
 }  // namespace nix
diff --git a/third_party/nix/src/nix/doctor.cc b/third_party/nix/src/nix/doctor.cc
index 95bf8a922e26..44b4bd605d6c 100644
--- a/third_party/nix/src/nix/doctor.cc
+++ b/third_party/nix/src/nix/doctor.cc
@@ -64,7 +64,7 @@ struct CmdDoctor : StoreCommand {
     return true;
   }
 
-  static bool checkProfileRoots(ref<Store> store) {
+  static bool checkProfileRoots(const ref<Store>& store) {
     PathSet dirs;
 
     for (auto& dir : tokenizeString<Strings>(getEnv("PATH"), ":")) {
diff --git a/third_party/nix/src/nix/hash.cc b/third_party/nix/src/nix/hash.cc
index da4b64fe757c..74ff45d0d84e 100644
--- a/third_party/nix/src/nix/hash.cc
+++ b/third_party/nix/src/nix/hash.cc
@@ -34,7 +34,7 @@ struct CmdHash : Command {
   }
 
   void run() override {
-    for (auto path : paths) {
+    for (const auto& path : paths) {
       Hash h = mode == mFile ? hashFile(ht, path) : hashPath(ht, path).first;
       if (truncate && h.hashSize > 20) {
         h = compressHash(h, 20);
@@ -73,7 +73,7 @@ struct CmdToBase : Command {
   }
 
   void run() override {
-    for (auto s : args) {
+    for (const auto& s : args) {
       std::cout << fmt("%s\n", Hash(s, ht).to_string(base, base == SRI));
     }
   }
diff --git a/third_party/nix/src/nix/installables.cc b/third_party/nix/src/nix/installables.cc
index f6cba6d3b636..8d2f93256814 100644
--- a/third_party/nix/src/nix/installables.cc
+++ b/third_party/nix/src/nix/installables.cc
@@ -198,8 +198,8 @@ std::string attrRegex = R"([A-Za-z_][A-Za-z0-9-_+]*)";
 static std::regex attrPathRegex(fmt(R"(%1%(\.%1%)*)", attrRegex));
 
 static std::vector<std::shared_ptr<Installable>> parseInstallables(
-    SourceExprCommand& cmd, ref<Store> store, std::vector<std::string> ss,
-    bool useDefaultInstallables) {
+    SourceExprCommand& cmd, const ref<Store>& store,
+    std::vector<std::string> ss, bool useDefaultInstallables) {
   std::vector<std::shared_ptr<Installable>> result;
 
   if (ss.empty() && useDefaultInstallables) {
@@ -213,7 +213,7 @@ static std::vector<std::shared_ptr<Installable>> parseInstallables(
     if (s.compare(0, 1, "(") == 0) {
       result.push_back(std::make_shared<InstallableExpr>(cmd, s));
 
-    } else if (s.find("/") != std::string::npos) {
+    } else if (s.find('/') != std::string::npos) {
       auto path = store->toStorePath(store->followLinksToStore(s));
 
       if (store->isStorePath(path)) {
@@ -233,7 +233,7 @@ static std::vector<std::shared_ptr<Installable>> parseInstallables(
 }
 
 std::shared_ptr<Installable> parseInstallable(SourceExprCommand& cmd,
-                                              ref<Store> store,
+                                              const ref<Store>& store,
                                               const std::string& installable,
                                               bool useDefaultInstallables) {
   auto installables = parseInstallables(cmd, store, {installable}, false);
@@ -241,8 +241,9 @@ std::shared_ptr<Installable> parseInstallable(SourceExprCommand& cmd,
   return installables.front();
 }
 
-Buildables build(ref<Store> store, RealiseMode mode,
-                 std::vector<std::shared_ptr<Installable>> installables) {
+Buildables build(
+    const ref<Store>& store, RealiseMode mode,
+    const std::vector<std::shared_ptr<Installable>>& installables) {
   if (mode != Build) {
     settings.readOnlyMode = true;
   }
@@ -278,8 +279,9 @@ Buildables build(ref<Store> store, RealiseMode mode,
   return buildables;
 }
 
-PathSet toStorePaths(ref<Store> store, RealiseMode mode,
-                     std::vector<std::shared_ptr<Installable>> installables) {
+PathSet toStorePaths(
+    const ref<Store>& store, RealiseMode mode,
+    const std::vector<std::shared_ptr<Installable>>& installables) {
   PathSet outPaths;
 
   for (auto& b : build(store, mode, installables)) {
@@ -291,8 +293,8 @@ PathSet toStorePaths(ref<Store> store, RealiseMode mode,
   return outPaths;
 }
 
-Path toStorePath(ref<Store> store, RealiseMode mode,
-                 std::shared_ptr<Installable> installable) {
+Path toStorePath(const ref<Store>& store, RealiseMode mode,
+                 const std::shared_ptr<Installable>& installable) {
   auto paths = toStorePaths(store, mode, {installable});
 
   if (paths.size() != 1) {
@@ -303,9 +305,10 @@ Path toStorePath(ref<Store> store, RealiseMode mode,
   return *paths.begin();
 }
 
-PathSet toDerivations(ref<Store> store,
-                      std::vector<std::shared_ptr<Installable>> installables,
-                      bool useDeriver) {
+PathSet toDerivations(
+    const ref<Store>& store,
+    const std::vector<std::shared_ptr<Installable>>& installables,
+    bool useDeriver) {
   PathSet drvPaths;
 
   for (auto& i : installables) {
diff --git a/third_party/nix/src/nix/ls.cc b/third_party/nix/src/nix/ls.cc
index 13b7bbbbb9bd..5b052f8fcc7c 100644
--- a/third_party/nix/src/nix/ls.cc
+++ b/third_party/nix/src/nix/ls.cc
@@ -74,7 +74,7 @@ struct MixLs : virtual Args, MixJSON {
            showDirectory);
   }
 
-  void list(ref<FSAccessor> accessor) {
+  void list(const ref<FSAccessor>& accessor) {
     if (path == "/") {
       path = "";
     }
diff --git a/third_party/nix/src/nix/repl.cc b/third_party/nix/src/nix/repl.cc
index 81e940dd3aaf..78991645ac07 100644
--- a/third_party/nix/src/nix/repl.cc
+++ b/third_party/nix/src/nix/repl.cc
@@ -3,6 +3,7 @@
 #include <cstdlib>
 #include <cstring>
 #include <iostream>
+#include <utility>
 
 #include <glog/logging.h>
 
@@ -58,10 +59,10 @@ struct NixRepl {
 
   const Path historyFile;
 
-  NixRepl(const Strings& searchPath, nix::ref<Store> store);
+  NixRepl(const Strings& searchPath, const nix::ref<Store>& store);
   ~NixRepl();
   void mainLoop(const std::vector<std::string>& files);
-  StringSet completePrefix(string prefix);
+  StringSet completePrefix(const string& prefix);
   static bool getLine(string& input, const std::string& prompt);
   Path getDerivationPath(Value& v);
   bool processLine(string line);
@@ -70,7 +71,7 @@ struct NixRepl {
   void reloadFiles();
   void addAttrsToScope(Value& attrs);
   void addVarToScope(const Symbol& name, Value& v);
-  Expr* parseString(string s);
+  Expr* parseString(const string& s);
   void evalString(string s, Value& v);
 
   using ValuesSeen = set<Value*>;
@@ -129,7 +130,7 @@ string removeWhitespace(string s) {
   return s;
 }
 
-NixRepl::NixRepl(const Strings& searchPath, nix::ref<Store> store)
+NixRepl::NixRepl(const Strings& searchPath, const nix::ref<Store>& store)
     : state(searchPath, store),
       staticEnv(false, &state.staticBaseEnv),
       historyFile(getDataDir() + "/nix/repl-history") {
@@ -332,7 +333,7 @@ bool NixRepl::getLine(string& input, const std::string& prompt) {
   return true;
 }
 
-StringSet NixRepl::completePrefix(string prefix) {
+StringSet NixRepl::completePrefix(const string& prefix) {
   StringSet completions;
 
   size_t start = prefix.find_last_of(" \n\r\t(){}[]");
@@ -641,13 +642,13 @@ void NixRepl::addVarToScope(const Symbol& name, Value& v) {
   varNames.insert((string)name);
 }
 
-Expr* NixRepl::parseString(string s) {
+Expr* NixRepl::parseString(const string& s) {
   Expr* e = state.parseExprFromString(s, curDir, staticEnv);
   return e;
 }
 
 void NixRepl::evalString(string s, Value& v) {
-  Expr* e = parseString(s);
+  Expr* e = parseString(std::move(s));
   e->eval(state, *env, v);
   state.forceValue(v);
 }
diff --git a/third_party/nix/src/nix/run.cc b/third_party/nix/src/nix/run.cc
index 9827360f0aa9..fa471bd542a1 100644
--- a/third_party/nix/src/nix/run.cc
+++ b/third_party/nix/src/nix/run.cc
@@ -30,7 +30,7 @@ struct CmdRun : InstallablesCommand {
         .description("command and arguments to be executed; defaults to 'bash'")
         .labels({"command", "args"})
         .arity(ArityAny)
-        .handler([&](std::vector<std::string> ss) {
+        .handler([&](const std::vector<std::string>& ss) {
           if (ss.empty()) {
             throw UsageError("--command requires at least one argument");
           }
@@ -227,7 +227,7 @@ void chrootHelper(int argc, char** argv) {
       throw SysError("mounting '%s' on '%s'", realStoreDir, storeDir);
     }
 
-    for (auto entry : readDirectory("/")) {
+    for (const auto& entry : readDirectory("/")) {
       auto src = "/" + entry.name;
       auto st = lstat(src);
       if (!S_ISDIR(st.st_mode)) {
diff --git a/third_party/nix/src/nix/search.cc b/third_party/nix/src/nix/search.cc
index 5f6def220291..7fe81adb6990 100644
--- a/third_party/nix/src/nix/search.cc
+++ b/third_party/nix/src/nix/search.cc
@@ -16,12 +16,12 @@
 
 using namespace nix;
 
-std::string wrap(std::string prefix, std::string s) {
+std::string wrap(const std::string& prefix, const std::string& s) {
   return prefix + s + ANSI_NORMAL;
 }
 
 std::string hilite(const std::string& s, const std::smatch& m,
-                   std::string postfix) {
+                   const std::string& postfix) {
   return m.empty() ? s
                    : std::string(m.prefix()) + ANSI_RED + std::string(m.str()) +
                          postfix + std::string(m.suffix());
@@ -99,7 +99,7 @@ struct CmdSearch : SourceExprCommand, MixJSON {
 
     std::function<void(Value*, std::string, bool, JSONObject*)> doExpr;
 
-    doExpr = [&](Value* v, std::string attrPath, bool toplevel,
+    doExpr = [&](Value* v, const std::string& attrPath, bool toplevel,
                  JSONObject* cache) {
       DLOG(INFO) << "at attribute '" << attrPath << "'";
 
@@ -262,7 +262,7 @@ struct CmdSearch : SourceExprCommand, MixJSON {
     }
 
     RunPager pager;
-    for (auto el : results) {
+    for (const auto& el : results) {
       std::cout << el.second << "\n";
     }
   }
diff --git a/third_party/nix/src/nix/upgrade-nix.cc b/third_party/nix/src/nix/upgrade-nix.cc
index b225aac2d725..9b060a7f195a 100644
--- a/third_party/nix/src/nix/upgrade-nix.cc
+++ b/third_party/nix/src/nix/upgrade-nix.cc
@@ -97,7 +97,7 @@ struct CmdUpgradeNix : MixDryRun, StoreCommand {
   }
 
   /* Return the profile in which Nix is installed. */
-  static Path getProfileDir(ref<Store> store) {
+  static Path getProfileDir(const ref<Store>& store) {
     Path where;
 
     for (auto& dir : tokenizeString<Strings>(getEnv("PATH"), ":")) {
@@ -143,7 +143,7 @@ struct CmdUpgradeNix : MixDryRun, StoreCommand {
   }
 
   /* Return the store path of the latest stable Nix. */
-  Path getLatestNix(ref<Store> store) {
+  Path getLatestNix(const ref<Store>& store) {
     // FIXME: use nixos.org?
     auto req = DownloadRequest(storePathsUrl);
     auto res = getDownloader()->download(req);
diff --git a/third_party/nix/src/nix/verify.cc b/third_party/nix/src/nix/verify.cc
index 63dd181e074c..a773b3a2c8de 100644
--- a/third_party/nix/src/nix/verify.cc
+++ b/third_party/nix/src/nix/verify.cc
@@ -102,8 +102,8 @@ struct CmdVerify : StorePathsCommand {
             size_t actualSigsNeeded = std::max(sigsNeeded, (size_t)1);
             size_t validSigs = 0;
 
-            auto doSigs = [&](StringSet sigs) {
-              for (auto sig : sigs) {
+            auto doSigs = [&](const StringSet& sigs) {
+              for (const auto& sig : sigs) {
                 if (sigsSeen.count(sig) != 0u) {
                   continue;
                 }