Merge pull request #277 from isildur-g/wip-cppcheck271

phase 1 of addressing cppcheck useStlAlgorithm warnings for fill and copy operations
This commit is contained in:
Konstantinos Margaritis 2024-05-15 10:44:15 +03:00 committed by GitHub
commit a255600773
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
19 changed files with 102 additions and 98 deletions

View File

@ -230,11 +230,12 @@ void checkForMultilineStart(ReportManager &rm, NGHolder &g,
/* we need to interpose a dummy dot vertex between v and accept if /* we need to interpose a dummy dot vertex between v and accept if
* required so that ^ doesn't match trailing \n */ * required so that ^ doesn't match trailing \n */
for (const auto &e : out_edges_range(v, g)) { auto deads = [&g=g](const NFAEdge &e) {
if (target(e, g) == g.accept) { return (target(e, g) == g.accept);
dead.emplace_back(e); };
} 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 */ /* assert has been resolved; clear flag */
g[v].assert_flags &= ~POS_FLAG_MULTILINE_START; g[v].assert_flags &= ~POS_FLAG_MULTILINE_START;
} }

View File

@ -227,11 +227,13 @@ vector<u32> removeClique(CliqueGraph &cg) {
while (!graph_empty(cg)) { while (!graph_empty(cg)) {
const vector<u32> &c = cliquesVec.back(); const vector<u32> &c = cliquesVec.back();
vector<CliqueVertex> dead; vector<CliqueVertex> dead;
for (const auto &v : vertices_range(cg)) {
if (find(c.begin(), c.end(), cg[v].stateId) != c.end()) { auto deads = [&c=c, &cg=cg](const CliqueVertex &v) {
dead.emplace_back(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) { for (const auto &v : dead) {
clear_vertex(v, cg); clear_vertex(v, cg);
remove_vertex(v, cg); remove_vertex(v, cg);

View File

@ -329,11 +329,11 @@ void buildReachMapping(const build_info &args, vector<NFAStateSet> &reach,
// Build a list of vertices with a state index assigned. // Build a list of vertices with a state index assigned.
vector<NFAVertex> verts; vector<NFAVertex> verts;
verts.reserve(args.num_states); verts.reserve(args.num_states);
for (auto v : vertices_range(h)) { auto sidat = [&state_ids=state_ids](const NFAVertex &v) {
if (state_ids.at(v) != NO_STATE) { return (state_ids.at(v) != NO_STATE);
verts.emplace_back(v); };
} 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. // Build a mapping from set-of-states -> reachability.
map<NFAStateSet, CharReach> mapping; map<NFAStateSet, CharReach> mapping;

View File

@ -32,6 +32,8 @@
*/ */
#include "config.h" #include "config.h"
#include <numeric>
#include "tamaramacompile.h" #include "tamaramacompile.h"
@ -127,9 +129,10 @@ buildTamarama(const TamaInfo &tamaInfo, const u32 queue,
sizeof(u32) * subSize + 64; // offsets to subengines in bytecode and sizeof(u32) * subSize + 64; // offsets to subengines in bytecode and
// padding for subengines // padding for subengines
for (const auto &sub : tamaInfo.subengines) { auto subl = [](size_t z, NFA *sub) {
total_size += ROUNDUP_CL(sub->length); 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, // use subSize as a sentinel value for no active subengines,
// so add one to subSize here // so add one to subSize here

View File

@ -92,11 +92,12 @@ static const CharReach CHARREACH_NONWORD_UCP_PRE(CHARREACH_NONWORD);
static static
vector<NFAEdge> getAsserts(const NGHolder &g) { vector<NFAEdge> getAsserts(const NGHolder &g) {
vector<NFAEdge> out; vector<NFAEdge> out;
for (const auto &e : edges_range(g)) { auto assertflags = [&g=g](const NFAEdge &e) {
if (g[e].assert_flags) { return (g[e].assert_flags);
out.emplace_back(e); };
} const auto &er = edges_range(g);
} std::copy_if(begin(er), end(er), std::back_inserter(out), assertflags);
return out; return out;
} }

View File

@ -512,17 +512,17 @@ bool removeSiblingsOfStartDotStar(NGHolder &g) {
* for SOM mode. (see UE-1544) */ * for SOM mode. (see UE-1544) */
bool optimiseVirtualStarts(NGHolder &g) { bool optimiseVirtualStarts(NGHolder &g) {
vector<NFAEdge> dead; 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)) { for (auto v : adjacent_vertices_range(g.startDs, g)) {
u32 flags = g[v].assert_flags; u32 flags = g[v].assert_flags;
if (!(flags & POS_FLAG_VIRTUAL_START)) { if (!(flags & POS_FLAG_VIRTUAL_START)) {
continue; continue;
} }
const auto &e = in_edges_range(v, g);
for (const auto &e : in_edges_range(v, g)) { std::copy_if(begin(e), end(e), std::back_inserter(dead), deads);
if (!is_any_start(source(e, g), g)) {
dead.emplace_back(e);
}
}
} }
if (dead.empty()) { if (dead.empty()) {

View File

@ -571,6 +571,9 @@ bool transformMinLengthToRepeat(NGHolder &g, ReportManager &rm) {
vector<NFAVertex> predcs; vector<NFAVertex> predcs;
vector<NFAEdge> dead; 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)) { for (auto u : inv_adjacent_vertices_range(cyclic, g)) {
DEBUG_PRINTF("pred %zu\n", g[u].index); DEBUG_PRINTF("pred %zu\n", g[u].index);
if (u == cyclic) { 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 // We want to delete the out-edges of each predecessor, but need to
// make sure we don't delete the startDs self loop. // 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) { const auto &e = out_edges_range(u, g);
dead.emplace_back(e); std::copy_if(begin(e), end(e), std::back_inserter(dead), deads);
}
}
} }
remove_edges(dead, g); remove_edges(dead, g);

View 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 */ /* create list of candidates first, to avoid issues of iter invalidation */
DEBUG_PRINTF("attempting to reuse vertices for top starts\n"); DEBUG_PRINTF("attempting to reuse vertices for top starts\n");
vector<NFAVertex> cand_starts; vector<NFAVertex> cand_starts;
for (NFAVertex u : unhandled_succ_tops | map_keys) { auto cands = [&g=g](const NFAVertex &u) {
if (hasSelfLoop(u, g)) { return (hasSelfLoop(u, g));
cand_starts.emplace_back(u); };
} 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) { for (NFAVertex u : cand_starts) {
if (!contains(unhandled_succ_tops, u)) { if (!contains(unhandled_succ_tops, u)) {

View File

@ -488,9 +488,9 @@ vector<LitEdge> add_reverse_edges_and_index(LitGraph &lg) {
const size_t edge_count = num_edges(lg); const size_t edge_count = num_edges(lg);
vector<LitEdge> fwd_edges; vector<LitEdge> fwd_edges;
fwd_edges.reserve(edge_count); 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); vector<LitEdge> rev_map(2 * edge_count);

View File

@ -62,11 +62,13 @@ void pruneUnreachable(NGHolder &g) {
&& edge(g.accept, g.acceptEod, g).second) { && edge(g.accept, g.acceptEod, g).second) {
// Trivial case: there are no in-edges to our accepts (other than // Trivial case: there are no in-edges to our accepts (other than
// accept->acceptEod), so all non-specials are unreachable. // accept->acceptEod), so all non-specials are unreachable.
for (auto v : vertices_range(g)) {
if (!is_special(v, g)) { auto deads = [&g=g](const NFAVertex &v) {
dead.emplace_back(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 { } else {
// Walk a reverse graph from acceptEod with Boost's depth_first_visit // Walk a reverse graph from acceptEod with Boost's depth_first_visit
// call. // call.
@ -199,17 +201,17 @@ void pruneHighlanderAccepts(NGHolder &g, const ReportManager &rm) {
} }
vector<NFAEdge> dead; 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)) { for (auto u : inv_adjacent_vertices_range(g.accept, g)) {
if (is_special(u, g)) { if (is_special(u, g)) {
continue; continue;
} }
// We can prune any out-edges that aren't accepts // We can prune any out-edges that aren't accepts
for (const auto &e : out_edges_range(u, g)) { const auto &er = out_edges_range(u, g);
if (!is_any_accept(target(e, g), g)) { std::copy_if(begin(er), end(er), std::back_inserter(dead), deads);
dead.emplace_back(e);
}
}
} }
if (dead.empty()) { if (dead.empty()) {

View File

@ -58,11 +58,12 @@ vector<DepthMinMax> getDistancesFromSOM(const NGHolder &g_orig) {
cloneHolder(g, g_orig, &vmap); cloneHolder(g, g_orig, &vmap);
vector<NFAVertex> vstarts; vector<NFAVertex> vstarts;
for (auto v : vertices_range(g)) { auto vstart = [&g=g](const NFAVertex &v) {
if (is_virtual_start(v, g)) { return (is_virtual_start(v, g));
vstarts.emplace_back(v); };
} const auto &vr = vertices_range(g);
} std::copy_if(begin(vr), end(vr), std::back_inserter(vstarts), vstart);
vstarts.emplace_back(g.startDs); vstarts.emplace_back(g.startDs);
// wire the successors of every virtual start or startDs to g.start. // wire the successors of every virtual start or startDs to g.start.

View File

@ -407,9 +407,10 @@ void appendLiteral(NGHolder &h, const ue2_literal &s) {
DEBUG_PRINTF("adding '%s' to graph\n", dumpString(s).c_str()); DEBUG_PRINTF("adding '%s' to graph\n", dumpString(s).c_str());
vector<NFAVertex> tail; vector<NFAVertex> tail;
assert(in_degree(h.acceptEod, h) == 1); 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()); assert(!tail.empty());
for (auto v : tail) { for (auto v : tail) {

View File

@ -455,11 +455,10 @@ void cleanupPositions(vector<PositionInfo> &a) {
vector<PositionInfo> out; vector<PositionInfo> out;
out.reserve(a.size()); // output should be close to input in size. out.reserve(a.size()); // output should be close to input in size.
for (const auto &p : a) { auto seens = [&seen=seen](const PositionInfo &p) {
if (seen.emplace(p.pos, p.flags).second) { return (seen.emplace(p.pos, p.flags).second);
out.emplace_back(p); // first encounter };
} std::copy_if(begin(a), end(a), std::back_inserter(out), seens);
}
DEBUG_PRINTF("in %zu; out %zu\n", a.size(), out.size()); DEBUG_PRINTF("in %zu; out %zu\n", a.size(), out.size());
a.swap(out); a.swap(out);

View File

@ -3301,10 +3301,11 @@ void addEodEventProgram(const RoseBuildImpl &build, build_context &bc,
// Collect all edges leading into EOD event literal vertices. // Collect all edges leading into EOD event literal vertices.
vector<RoseEdge> edge_list; vector<RoseEdge> edge_list;
for (const auto &v : lit_info.vertices) { for (const auto &v : lit_info.vertices) {
for (const auto &e : in_edges_range(v, g)) { const auto &er = in_edges_range(v, g);
edge_list.emplace_back(e); std::copy(begin(er), end(er), std::back_inserter(edge_list));
}
} }
// Sort edge list for determinism, prettiness. // Sort edge list for determinism, prettiness.

View File

@ -2432,9 +2432,8 @@ void addPredBlocksAny(map<u32, RoseProgram> &pred_blocks, u32 num_states,
RoseProgram sparse_program; RoseProgram sparse_program;
vector<u32> keys; vector<u32> keys;
for (const u32 &key : pred_blocks | map_keys) { const auto &k = pred_blocks | map_keys;
keys.emplace_back(key); std::copy(begin(k), end(k), std::back_inserter(keys));
}
const RoseInstruction *end_inst = sparse_program.end_instruction(); const RoseInstruction *end_inst = sparse_program.end_instruction();
auto ri = std::make_unique<RoseInstrSparseIterAny>(num_states, keys, end_inst); auto ri = std::make_unique<RoseInstrSparseIterAny>(num_states, keys, end_inst);

View File

@ -1981,11 +1981,10 @@ void filterDiamondCandidates(const RoseGraph &g, CandidateSet &candidates) {
DEBUG_PRINTF("%zu candidates enter\n", candidates.size()); DEBUG_PRINTF("%zu candidates enter\n", candidates.size());
vector<RoseVertex> dead; vector<RoseVertex> dead;
for (const auto &v : candidates) { auto deads = [&g=g](const RoseVertex &v) {
if (hasNoDiamondSiblings(g, v)) { return (hasNoDiamondSiblings(g, v));
dead.emplace_back(v); };
} std::copy_if(begin(candidates), end(candidates), std::back_inserter(dead), deads);
}
for (const auto &v : dead) { for (const auto &v : dead) {
candidates.erase(v); candidates.erase(v);

View File

@ -64,12 +64,11 @@ u32 findMinWidth(const RoseBuildImpl &tbi, enum rose_literal_table table) {
const RoseGraph &g = tbi.g; const RoseGraph &g = tbi.g;
vector<RoseVertex> table_verts; vector<RoseVertex> table_verts;
auto tvs = [&tbi=tbi, &table=table](const RoseVertex &v) {
for (auto v : vertices_range(g)) { return (tbi.hasLiteralInTable(v, table));
if (tbi.hasLiteralInTable(v, table)) { };
table_verts.emplace_back(v); const auto &vr = vertices_range(g);
} std::copy_if(begin(vr), end(vr), std::back_inserter(table_verts), tvs);
}
set<RoseVertex> reachable; set<RoseVertex> reachable;
find_reachable(g, table_verts, &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"); table == ROSE_FLOATING ? "floating" : "anchored");
vector<RoseVertex> table_verts; vector<RoseVertex> table_verts;
auto tvs = [&tbi=tbi, &table=table](const RoseVertex &v) {
for (auto v : vertices_range(g)) { return ((table == ROSE_FLOATING && tbi.isFloating(v))
if ((table == ROSE_FLOATING && tbi.isFloating(v)) || (table == ROSE_ANCHORED && tbi.isAnchored(v)));
|| (table == ROSE_ANCHORED && tbi.isAnchored(v))) { };
table_verts.emplace_back(v); const auto &vr = vertices_range(g);
} std::copy_if(begin(vr), end(vr), std::back_inserter(table_verts), tvs);
}
set<RoseVertex> reachable; set<RoseVertex> reachable;
find_reachable(g, table_verts, &reachable); find_reachable(g, table_verts, &reachable);

View File

@ -89,9 +89,7 @@ public:
/// Set all bits. /// Set all bits.
void setall() { void setall() {
for (auto &e : bits) { std::fill(bits.begin(), bits.end(), all_ones);
e = all_ones;
}
clear_trailer(); clear_trailer();
} }
@ -102,9 +100,7 @@ public:
/// Clear all bits. /// Clear all bits.
void clear() { void clear() {
for (auto &e : bits) { std::fill(bits.begin(), bits.end(), 0);
e = 0;
}
} }
/// Clear all bits (alias for bitset::clear). /// Clear all bits (alias for bitset::clear).

View File

@ -1008,9 +1008,9 @@ int HS_CDECL main(int argc, char *argv[]) {
if (sigSets.empty()) { if (sigSets.empty()) {
SignatureSet sigs; SignatureSet sigs;
sigs.reserve(exprMapTemplate.size()); sigs.reserve(exprMapTemplate.size());
for (auto i : exprMapTemplate | map_keys) { const auto &i = exprMapTemplate | map_keys;
sigs.push_back(i); std::copy(begin(i), end(i), std::back_inserter(sigs));
}
sigSets.emplace_back(exprPath, std::move(sigs)); sigSets.emplace_back(exprPath, std::move(sigs));
} }