From 4cefba5cedb1562da29e153d25e5f3e4d94db08f Mon Sep 17 00:00:00 2001 From: "G.E" Date: Tue, 14 May 2024 17:37:38 +0300 Subject: [PATCH] phase 1 of addressing cppcheck useStlAlgorithm warnings, this set only includes fill and copy operations. --- src/compiler/asserts.cpp | 11 ++++++----- src/nfa/castlecompile.cpp | 12 +++++++----- src/nfa/limex_compile.cpp | 10 +++++----- src/nfa/tamaramacompile.cpp | 9 ++++++--- src/nfagraph/ng_asserts.cpp | 11 ++++++----- src/nfagraph/ng_edge_redundancy.cpp | 12 ++++++------ src/nfagraph/ng_extparam.cpp | 11 ++++++----- src/nfagraph/ng_limex.cpp | 10 +++++----- src/nfagraph/ng_literal_analysis.cpp | 6 +++--- src/nfagraph/ng_prune.cpp | 22 ++++++++++++---------- src/nfagraph/ng_som_util.cpp | 11 ++++++----- src/nfagraph/ng_util.cpp | 7 ++++--- src/parser/buildstate.cpp | 9 ++++----- src/rose/rose_build_bytecode.cpp | 7 ++++--- src/rose/rose_build_program.cpp | 5 ++--- src/rose/rose_build_role_aliasing.cpp | 9 ++++----- src/rose/rose_build_width.cpp | 24 +++++++++++------------- src/util/bitfield.h | 8 ++------ tools/hsbench/main.cpp | 6 +++--- 19 files changed, 102 insertions(+), 98 deletions(-) diff --git a/src/compiler/asserts.cpp b/src/compiler/asserts.cpp index 51a052b0..60a32e1c 100644 --- a/src/compiler/asserts.cpp +++ b/src/compiler/asserts.cpp @@ -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; } diff --git a/src/nfa/castlecompile.cpp b/src/nfa/castlecompile.cpp index 56b12700..c5485e32 100644 --- a/src/nfa/castlecompile.cpp +++ b/src/nfa/castlecompile.cpp @@ -227,11 +227,13 @@ vector removeClique(CliqueGraph &cg) { while (!graph_empty(cg)) { const vector &c = cliquesVec.back(); vector 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); diff --git a/src/nfa/limex_compile.cpp b/src/nfa/limex_compile.cpp index f84cdc32..e92c2781 100644 --- a/src/nfa/limex_compile.cpp +++ b/src/nfa/limex_compile.cpp @@ -329,11 +329,11 @@ void buildReachMapping(const build_info &args, vector &reach, // Build a list of vertices with a state index assigned. vector 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 mapping; diff --git a/src/nfa/tamaramacompile.cpp b/src/nfa/tamaramacompile.cpp index 6f8c3dbe..57164908 100644 --- a/src/nfa/tamaramacompile.cpp +++ b/src/nfa/tamaramacompile.cpp @@ -32,6 +32,8 @@ */ #include "config.h" +#include + #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 diff --git a/src/nfagraph/ng_asserts.cpp b/src/nfagraph/ng_asserts.cpp index 764ebed1..d9849362 100644 --- a/src/nfagraph/ng_asserts.cpp +++ b/src/nfagraph/ng_asserts.cpp @@ -92,11 +92,12 @@ static const CharReach CHARREACH_NONWORD_UCP_PRE(CHARREACH_NONWORD); static vector getAsserts(const NGHolder &g) { vector 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; } diff --git a/src/nfagraph/ng_edge_redundancy.cpp b/src/nfagraph/ng_edge_redundancy.cpp index d6e9895b..d4b5b061 100644 --- a/src/nfagraph/ng_edge_redundancy.cpp +++ b/src/nfagraph/ng_edge_redundancy.cpp @@ -512,17 +512,17 @@ bool removeSiblingsOfStartDotStar(NGHolder &g) { * for SOM mode. (see UE-1544) */ bool optimiseVirtualStarts(NGHolder &g) { vector 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()) { diff --git a/src/nfagraph/ng_extparam.cpp b/src/nfagraph/ng_extparam.cpp index 65e30a14..7f2065d5 100644 --- a/src/nfagraph/ng_extparam.cpp +++ b/src/nfagraph/ng_extparam.cpp @@ -571,6 +571,9 @@ bool transformMinLengthToRepeat(NGHolder &g, ReportManager &rm) { vector preds; vector 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); diff --git a/src/nfagraph/ng_limex.cpp b/src/nfagraph/ng_limex.cpp index 27d8c524..f78b4144 100644 --- a/src/nfagraph/ng_limex.cpp +++ b/src/nfagraph/ng_limex.cpp @@ -389,11 +389,11 @@ void reusePredsAsStarts(const NGHolder &g, const map &top_reach, /* create list of candidates first, to avoid issues of iter invalidation */ DEBUG_PRINTF("attempting to reuse vertices for top starts\n"); vector 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)) { diff --git a/src/nfagraph/ng_literal_analysis.cpp b/src/nfagraph/ng_literal_analysis.cpp index 77964b81..6d2030f7 100644 --- a/src/nfagraph/ng_literal_analysis.cpp +++ b/src/nfagraph/ng_literal_analysis.cpp @@ -488,9 +488,9 @@ vector add_reverse_edges_and_index(LitGraph &lg) { const size_t edge_count = num_edges(lg); vector 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 rev_map(2 * edge_count); diff --git a/src/nfagraph/ng_prune.cpp b/src/nfagraph/ng_prune.cpp index 73d7e64b..9fb9d77b 100644 --- a/src/nfagraph/ng_prune.cpp +++ b/src/nfagraph/ng_prune.cpp @@ -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 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()) { diff --git a/src/nfagraph/ng_som_util.cpp b/src/nfagraph/ng_som_util.cpp index 82277c06..160b2e4f 100644 --- a/src/nfagraph/ng_som_util.cpp +++ b/src/nfagraph/ng_som_util.cpp @@ -58,11 +58,12 @@ vector getDistancesFromSOM(const NGHolder &g_orig) { cloneHolder(g, g_orig, &vmap); vector 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. diff --git a/src/nfagraph/ng_util.cpp b/src/nfagraph/ng_util.cpp index b1d39d2e..906eba13 100644 --- a/src/nfagraph/ng_util.cpp +++ b/src/nfagraph/ng_util.cpp @@ -405,9 +405,10 @@ void appendLiteral(NGHolder &h, const ue2_literal &s) { DEBUG_PRINTF("adding '%s' to graph\n", dumpString(s).c_str()); vector 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) { diff --git a/src/parser/buildstate.cpp b/src/parser/buildstate.cpp index 96f91cb6..cd611d9c 100644 --- a/src/parser/buildstate.cpp +++ b/src/parser/buildstate.cpp @@ -455,11 +455,10 @@ void cleanupPositions(vector &a) { vector 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); diff --git a/src/rose/rose_build_bytecode.cpp b/src/rose/rose_build_bytecode.cpp index 06f36582..b386e124 100644 --- a/src/rose/rose_build_bytecode.cpp +++ b/src/rose/rose_build_bytecode.cpp @@ -3308,10 +3308,11 @@ void addEodEventProgram(const RoseBuildImpl &build, build_context &bc, // Collect all edges leading into EOD event literal vertices. vector 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. diff --git a/src/rose/rose_build_program.cpp b/src/rose/rose_build_program.cpp index 861855b5..9e544e5e 100644 --- a/src/rose/rose_build_program.cpp +++ b/src/rose/rose_build_program.cpp @@ -2432,9 +2432,8 @@ void addPredBlocksAny(map &pred_blocks, u32 num_states, RoseProgram sparse_program; vector 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(num_states, keys, end_inst); diff --git a/src/rose/rose_build_role_aliasing.cpp b/src/rose/rose_build_role_aliasing.cpp index 2888b9a0..acf8bd57 100644 --- a/src/rose/rose_build_role_aliasing.cpp +++ b/src/rose/rose_build_role_aliasing.cpp @@ -1976,11 +1976,10 @@ void filterDiamondCandidates(RoseGraph &g, CandidateSet &candidates) { DEBUG_PRINTF("%zu candidates enter\n", candidates.size()); vector 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); diff --git a/src/rose/rose_build_width.cpp b/src/rose/rose_build_width.cpp index 327911ea..7c509ba7 100644 --- a/src/rose/rose_build_width.cpp +++ b/src/rose/rose_build_width.cpp @@ -64,12 +64,11 @@ u32 findMinWidth(const RoseBuildImpl &tbi, enum rose_literal_table table) { const RoseGraph &g = tbi.g; vector 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 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 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 reachable; find_reachable(g, table_verts, &reachable); diff --git a/src/util/bitfield.h b/src/util/bitfield.h index 4a3fbd6e..b34e5e89 100644 --- a/src/util/bitfield.h +++ b/src/util/bitfield.h @@ -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). diff --git a/tools/hsbench/main.cpp b/tools/hsbench/main.cpp index 1a19d510..98e3e4f2 100644 --- a/tools/hsbench/main.cpp +++ b/tools/hsbench/main.cpp @@ -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)); }