]> git.ipfire.org Git - thirdparty/vectorscan.git/commitdiff
phase 1 of addressing cppcheck useStlAlgorithm warnings, 277/head
authorG.E <gregory.economou@vectorcamp.gr>
Tue, 14 May 2024 14:37:38 +0000 (17:37 +0300)
committerG.E <gregory.economou@vectorcamp.gr>
Tue, 14 May 2024 14:37:38 +0000 (17:37 +0300)
this set only includes fill and copy operations.

19 files changed:
src/compiler/asserts.cpp
src/nfa/castlecompile.cpp
src/nfa/limex_compile.cpp
src/nfa/tamaramacompile.cpp
src/nfagraph/ng_asserts.cpp
src/nfagraph/ng_edge_redundancy.cpp
src/nfagraph/ng_extparam.cpp
src/nfagraph/ng_limex.cpp
src/nfagraph/ng_literal_analysis.cpp
src/nfagraph/ng_prune.cpp
src/nfagraph/ng_som_util.cpp
src/nfagraph/ng_util.cpp
src/parser/buildstate.cpp
src/rose/rose_build_bytecode.cpp
src/rose/rose_build_program.cpp
src/rose/rose_build_role_aliasing.cpp
src/rose/rose_build_width.cpp
src/util/bitfield.h
tools/hsbench/main.cpp

index 51a052b04f4435e897f0518b35c61689c6092ef1..60a32e1c0fc6469b7eb380a7e8e9a1f488b7078b 100644 (file)
@@ -229,11 +229,12 @@ void checkForMultilineStart(ReportManager &rm, NGHolder &g,
 
         /* we need to interpose a dummy dot vertex between v and accept if
          * required so that ^ doesn't match trailing \n */
-         for (const auto &e : out_edges_range(v, g)) {
-            if (target(e, g) == g.accept) {
-                dead.emplace_back(e);
-            }
-        }
+        auto deads = [&g=g](const NFAEdge &e) {
+            return (target(e, g) == g.accept);
+        };
+        const auto &er = out_edges_range(v, g);
+        std::copy_if(begin(er), end(er),  std::back_inserter(dead), deads);
+
         /* assert has been resolved; clear flag */
         g[v].assert_flags &= ~POS_FLAG_MULTILINE_START;
     }
index 56b12700f32f683cef04c6707b879a67932f0774..c5485e32a146c03e5a835ba5cad9838af9e78516 100644 (file)
@@ -227,11 +227,13 @@ vector<u32> removeClique(CliqueGraph &cg) {
     while (!graph_empty(cg)) {
         const vector<u32> &c = cliquesVec.back();
         vector<CliqueVertex> dead;
-        for (const auto &v : vertices_range(cg)) {
-            if (find(c.begin(), c.end(), cg[v].stateId) != c.end()) {
-                dead.emplace_back(v);
-            }
-        }
+
+        auto deads = [&c=c, &cg=cg](const CliqueVertex &v) {
+            return (find(c.begin(), c.end(), cg[v].stateId) != c.end());
+        };
+        const auto &vr = vertices_range(cg);
+        std::copy_if(begin(vr), end(vr),  std::back_inserter(dead), deads);
+
         for (const auto &v : dead) {
             clear_vertex(v, cg);
             remove_vertex(v, cg);
index f84cdc32fccd7d998a5ba7d501a33bd079145e03..e92c2781cb993cbefef1aa9ff4e92832939a69b0 100644 (file)
@@ -329,11 +329,11 @@ void buildReachMapping(const build_info &args, vector<NFAStateSet> &reach,
     // Build a list of vertices with a state index assigned.
     vector<NFAVertex> verts;
     verts.reserve(args.num_states);
-    for (auto v : vertices_range(h)) {
-        if (state_ids.at(v) != NO_STATE) {
-            verts.emplace_back(v);
-        }
-    }
+    auto sidat = [&state_ids=state_ids](const NFAVertex &v) {
+        return (state_ids.at(v) != NO_STATE);
+    };
+    const auto &vr = vertices_range(h);
+    std::copy_if(begin(vr), end(vr),  std::back_inserter(verts), sidat);
 
     // Build a mapping from set-of-states -> reachability.
     map<NFAStateSet, CharReach> mapping;
index 6f8c3dbe470bde891e860774dfebafe706c66688..57164908ffbc1d47a3638ed4a177f15a1d31c6a5 100644 (file)
@@ -32,6 +32,8 @@
  */
 
 #include "config.h"
+#include <numeric>
+
 
 #include "tamaramacompile.h"
 
@@ -127,9 +129,10 @@ buildTamarama(const TamaInfo &tamaInfo, const u32 queue,
         sizeof(u32) * subSize + 64; // offsets to subengines in bytecode and
                                     // padding for subengines
 
-    for (const auto &sub : tamaInfo.subengines) {
-        total_size += ROUNDUP_CL(sub->length);
-    }
+    auto subl = [](size_t z, NFA *sub) {
+        return z + (size_t)(ROUNDUP_CL(sub->length));
+    };
+    total_size += std::accumulate(tamaInfo.subengines.begin(), tamaInfo.subengines.end(), 0, subl);
 
     // use subSize as a sentinel value for no active subengines,
     // so add one to subSize here
index 764ebed1b964c3f1b2afad369ac625f0d7ecede8..d98493623538d94c01b9167e67fec43eaaef081c 100644 (file)
@@ -92,11 +92,12 @@ static const CharReach CHARREACH_NONWORD_UCP_PRE(CHARREACH_NONWORD);
 static
 vector<NFAEdge> getAsserts(const NGHolder &g) {
     vector<NFAEdge> out;
-    for (const auto &e : edges_range(g)) {
-        if (g[e].assert_flags) {
-            out.emplace_back(e);
-        }
-    }
+    auto assertflags = [&g=g](const NFAEdge &e) {
+        return (g[e].assert_flags);
+    };
+    const auto &er = edges_range(g);
+    std::copy_if(begin(er), end(er),  std::back_inserter(out), assertflags);
+
     return out;
 }
 
index d6e9895b7aa4d9f5cf0725d43f90552154290ae5..d4b5b0612ab4b2afe3e7c22f89708ad21f0772fc 100644 (file)
@@ -512,17 +512,17 @@ bool removeSiblingsOfStartDotStar(NGHolder &g) {
  * for SOM mode. (see UE-1544) */
 bool optimiseVirtualStarts(NGHolder &g) {
     vector<NFAEdge> dead;
+    auto deads = [&g=g](const NFAEdge &e) {
+        return (!is_any_start(source(e, g), g));
+    };
+
     for (auto v : adjacent_vertices_range(g.startDs, g)) {
         u32 flags = g[v].assert_flags;
         if (!(flags & POS_FLAG_VIRTUAL_START)) {
             continue;
         }
-
-        for (const auto &e : in_edges_range(v, g)) {
-            if (!is_any_start(source(e, g), g)) {
-                dead.emplace_back(e);
-            }
-        }
+        const auto &e = in_edges_range(v, g);
+        std::copy_if(begin(e), end(e),  std::back_inserter(dead), deads);
     }
 
     if (dead.empty()) {
index 65e30a14059b6e68cb0aba71ee4db8eaece74c65..7f2065d5e5e66147ebadd89dec0f192c1007cad0 100644 (file)
@@ -571,6 +571,9 @@ bool transformMinLengthToRepeat(NGHolder &g, ReportManager &rm) {
 
     vector<NFAVertex> preds;
     vector<NFAEdge> dead;
+    auto deads = [&g=g](const NFAEdge &e) {
+        return (target(e, g) != g.startDs);
+    };
     for (auto u : inv_adjacent_vertices_range(cyclic, g)) {
         DEBUG_PRINTF("pred %zu\n", g[u].index);
         if (u == cyclic) {
@@ -580,11 +583,9 @@ bool transformMinLengthToRepeat(NGHolder &g, ReportManager &rm) {
 
         // We want to delete the out-edges of each predecessor, but need to
         // make sure we don't delete the startDs self loop.
-        for (const auto &e : out_edges_range(u, g)) {
-            if (target(e, g) != g.startDs) {
-                dead.emplace_back(e);
-            }
-        }
+
+        const auto &e = out_edges_range(u, g);
+        std::copy_if(begin(e), end(e),  std::back_inserter(dead), deads);
     }
 
     remove_edges(dead, g);
index 27d8c5244add7ae4bf360caf8aa5716d3c1bd657..f78b4144f40c3647168dc6cca2127addfc5d7fb4 100644 (file)
@@ -389,11 +389,11 @@ void reusePredsAsStarts(const NGHolder &g, const map<u32, CharReach> &top_reach,
     /* create list of candidates first, to avoid issues of iter invalidation */
     DEBUG_PRINTF("attempting to reuse vertices for top starts\n");
     vector<NFAVertex> cand_starts;
-    for (NFAVertex u : unhandled_succ_tops | map_keys) {
-        if (hasSelfLoop(u, g)) {
-            cand_starts.emplace_back(u);
-        }
-    }
+    auto cands = [&g=g](const NFAVertex &u) {
+        return (hasSelfLoop(u, g));
+    };
+    const auto &u = unhandled_succ_tops | map_keys;
+    std::copy_if(begin(u), end(u),  std::back_inserter(cand_starts), cands);
 
     for (NFAVertex u : cand_starts) {
         if (!contains(unhandled_succ_tops, u)) {
index 77964b8128521216389713e087bfac194825c5cd..6d2030f7a709145cbebc013dbe05685969cacabd 100644 (file)
@@ -488,9 +488,9 @@ vector<LitEdge> add_reverse_edges_and_index(LitGraph &lg) {
     const size_t edge_count = num_edges(lg);
     vector<LitEdge> fwd_edges;
     fwd_edges.reserve(edge_count);
-    for (const auto &e : edges_range(lg)) {
-        fwd_edges.push_back(e);
-    }
+
+    const auto &e = edges_range(lg);
+    std::copy(begin(e), end(e),  std::back_inserter(fwd_edges));
 
     vector<LitEdge> rev_map(2 * edge_count);
 
index 73d7e64b27f8a25d0d0fbb35069e26cecc9a569c..9fb9d77b45a95362485e82e38097cf2ff71a7fe2 100644 (file)
@@ -62,11 +62,13 @@ void pruneUnreachable(NGHolder &g) {
         && edge(g.accept, g.acceptEod, g).second) {
         // Trivial case: there are no in-edges to our accepts (other than
         // accept->acceptEod), so all non-specials are unreachable.
-        for (auto v : vertices_range(g)) {
-            if (!is_special(v, g)) {
-                dead.emplace_back(v);
-            }
-        }
+
+        auto deads = [&g=g](const NFAVertex &v) {
+            return (!is_special(v, g));
+        };
+        const auto &vr = vertices_range(g);
+        std::copy_if(begin(vr), end(vr),  std::back_inserter(dead), deads);
+
     } else {
         // Walk a reverse graph from acceptEod with Boost's depth_first_visit
         // call.
@@ -199,17 +201,17 @@ void pruneHighlanderAccepts(NGHolder &g, const ReportManager &rm) {
     }
 
     vector<NFAEdge> dead;
+    auto deads = [&g=g](const NFAEdge &e) {
+        return (!is_any_accept(target(e, g), g));
+    };
     for (auto u : inv_adjacent_vertices_range(g.accept, g)) {
         if (is_special(u, g)) {
             continue;
         }
 
         // We can prune any out-edges that aren't accepts
-        for (const auto &e : out_edges_range(u, g)) {
-            if (!is_any_accept(target(e, g), g)) {
-                dead.emplace_back(e);
-            }
-        }
+        const auto &er = out_edges_range(u, g);
+        std::copy_if(begin(er), end(er),  std::back_inserter(dead), deads);
     }
 
     if (dead.empty()) {
index 82277c061fe18fe8b4cce5ab73783e17f6d9941b..160b2e4f61c199f7f1c050fe89bae1189733fc45 100644 (file)
@@ -58,11 +58,12 @@ vector<DepthMinMax> getDistancesFromSOM(const NGHolder &g_orig) {
     cloneHolder(g, g_orig, &vmap);
 
     vector<NFAVertex> vstarts;
-    for (auto v : vertices_range(g)) {
-        if (is_virtual_start(v, g)) {
-            vstarts.emplace_back(v);
-        }
-    }
+    auto vstart = [&g=g](const NFAVertex &v) {
+        return (is_virtual_start(v, g));
+    };
+    const auto &vr = vertices_range(g);
+    std::copy_if(begin(vr), end(vr),  std::back_inserter(vstarts), vstart);
+
     vstarts.emplace_back(g.startDs);
 
     // wire the successors of every virtual start or startDs to g.start.
index b1d39d2e3a6d5019a7cdba600edb0578fa3ff879..906eba132150894e85e205ef6dae48650bcb6cdd 100644 (file)
@@ -405,9 +405,10 @@ void appendLiteral(NGHolder &h, const ue2_literal &s) {
     DEBUG_PRINTF("adding '%s' to graph\n", dumpString(s).c_str());
     vector<NFAVertex> tail;
     assert(in_degree(h.acceptEod, h) == 1);
-    for (auto v : inv_adjacent_vertices_range(h.accept, h)) {
-        tail.emplace_back(v);
-    }
+
+    const auto &vr = inv_adjacent_vertices_range(h.accept, h);
+    std::copy(begin(vr), end(vr),  std::back_inserter(tail));
+
     assert(!tail.empty());
 
     for (auto v : tail) {
index 96f91cb6cd5865b4aa561ddf03bd9bb6d2b2192a..cd611d9c1b91909c0796a2e15600121602f2ced0 100644 (file)
@@ -455,11 +455,10 @@ void cleanupPositions(vector<PositionInfo> &a) {
     vector<PositionInfo> out;
     out.reserve(a.size()); // output should be close to input in size.
 
-    for (const auto &p : a) {
-        if (seen.emplace(p.pos, p.flags).second) {
-            out.emplace_back(p); // first encounter
-        }
-    }
+    auto seens = [&seen=seen](const PositionInfo &p) {
+        return (seen.emplace(p.pos, p.flags).second);
+    };
+    std::copy_if(begin(a), end(a),  std::back_inserter(out), seens);
 
     DEBUG_PRINTF("in %zu; out %zu\n", a.size(), out.size());
     a.swap(out);
index 06f36582b38c714f43f0982de0a0ba1ac9de68fe..b386e124a6f5eea6aab7ca7fac6011625c5b5c60 100644 (file)
@@ -3308,10 +3308,11 @@ void addEodEventProgram(const RoseBuildImpl &build, build_context &bc,
 
     // Collect all edges leading into EOD event literal vertices.
     vector<RoseEdge> edge_list;
+
+
     for (const auto &v : lit_info.vertices) {
-        for (const auto &e : in_edges_range(v, g)) {
-            edge_list.emplace_back(e);
-        }
+        const auto &er = in_edges_range(v, g);
+        std::copy(begin(er), end(er),  std::back_inserter(edge_list));
     }
 
     // Sort edge list for determinism, prettiness.
index 861855b54eeeaeebf82ea67cd467de4d71b5a018..9e544e5eac3cc665acbf3575f0db47ac4e8a45a2 100644 (file)
@@ -2432,9 +2432,8 @@ void addPredBlocksAny(map<u32, RoseProgram> &pred_blocks, u32 num_states,
     RoseProgram sparse_program;
 
     vector<u32> keys;
-    for (const u32 &key : pred_blocks | map_keys) {
-        keys.emplace_back(key);
-    }
+    const auto &k = pred_blocks | map_keys;
+    std::copy(begin(k), end(k),  std::back_inserter(keys));
 
     const RoseInstruction *end_inst = sparse_program.end_instruction();
     auto ri = std::make_unique<RoseInstrSparseIterAny>(num_states, keys, end_inst);
index 2888b9a0f0969a5a8f3ed5c656a71920b9344f8e..acf8bd57d9bbdf5ad3498eebf5409f61d693ff24 100644 (file)
@@ -1976,11 +1976,10 @@ void filterDiamondCandidates(RoseGraph &g, CandidateSet &candidates) {
     DEBUG_PRINTF("%zu candidates enter\n", candidates.size());
 
     vector<RoseVertex> dead;
-    for (const auto &v : candidates) {
-        if (hasNoDiamondSiblings(g, v)) {
-            dead.emplace_back(v);
-        }
-    }
+    auto deads = [&g=g](const RoseVertex &v) {
+        return (hasNoDiamondSiblings(g, v));
+    };
+    std::copy_if(begin(candidates), end(candidates), std::back_inserter(dead), deads);
 
     for (const auto &v : dead) {
         candidates.erase(v);
index 327911eacde345731dc4db1c95f07ab160d54731..7c509ba797b60b1a48cdd5898402fe455e419a75 100644 (file)
@@ -64,12 +64,11 @@ u32 findMinWidth(const RoseBuildImpl &tbi, enum rose_literal_table table) {
     const RoseGraph &g = tbi.g;
 
     vector<RoseVertex> table_verts;
-
-    for (auto v : vertices_range(g)) {
-        if (tbi.hasLiteralInTable(v, table)) {
-            table_verts.emplace_back(v);
-        }
-    }
+    auto tvs = [&tbi=tbi, &table=table](const RoseVertex &v) {
+        return (tbi.hasLiteralInTable(v, table));
+    };
+    const auto &vr = vertices_range(g);
+    std::copy_if(begin(vr), end(vr),  std::back_inserter(table_verts), tvs);
 
     set<RoseVertex> reachable;
     find_reachable(g, table_verts, &reachable);
@@ -189,13 +188,12 @@ u32 findMaxBAWidth(const RoseBuildImpl &tbi, enum rose_literal_table table) {
                   table == ROSE_FLOATING ? "floating" : "anchored");
 
     vector<RoseVertex> table_verts;
-
-    for (auto v : vertices_range(g)) {
-        if ((table == ROSE_FLOATING && tbi.isFloating(v))
-            || (table == ROSE_ANCHORED && tbi.isAnchored(v))) {
-            table_verts.emplace_back(v);
-        }
-    }
+    auto tvs = [&tbi=tbi, &table=table](const RoseVertex &v) {
+        return ((table == ROSE_FLOATING && tbi.isFloating(v))
+               || (table == ROSE_ANCHORED && tbi.isAnchored(v)));
+    };
+    const auto &vr = vertices_range(g);
+    std::copy_if(begin(vr), end(vr),  std::back_inserter(table_verts), tvs);
 
     set<RoseVertex> reachable;
     find_reachable(g, table_verts, &reachable);
index 4a3fbd6ed50aeb352309daa5e3bf7584f9bb03a3..b34e5e89a55feb317a023d54502f3ca88a5c4e55 100644 (file)
@@ -89,9 +89,7 @@ public:
 
     /// Set all bits.
     void setall() {
-        for (auto &e : bits) {
-            e = all_ones;
-        }
+        std::fill(bits.begin(), bits.end(), all_ones);
         clear_trailer();
     }
 
@@ -102,9 +100,7 @@ public:
 
     /// Clear all bits.
     void clear() {
-        for (auto &e : bits) {
-            e = 0;
-        }
+        std::fill(bits.begin(), bits.end(), 0);
     }
 
     /// Clear all bits (alias for bitset::clear).
index 1a19d510fe86d668b8ae3ddce4ea8335479caee1..98e3e4f24e695258d06444980c7df1b1ec0b4501 100644 (file)
@@ -1008,9 +1008,9 @@ int HS_CDECL main(int argc, char *argv[]) {
     if (sigSets.empty()) {
         SignatureSet sigs;
         sigs.reserve(exprMapTemplate.size());
-        for (auto i : exprMapTemplate | map_keys) {
-            sigs.push_back(i);
-        }
+        const auto &i = exprMapTemplate | map_keys;
+        std::copy(begin(i), end(i),  std::back_inserter(sigs));
+
         sigSets.emplace_back(exprPath, std::move(sigs));
     }