diff --git a/src/Attr.cc b/src/Attr.cc index 36050d2262..0218956ec2 100644 --- a/src/Attr.cc +++ b/src/Attr.cc @@ -142,16 +142,16 @@ Attributes::Attributes(attr_list* a, BroType* t, bool arg_in_record) // rather than just taking over 'a' for ourselves, so that // the necessary checking gets done. - loop_over_list(*a, i) - AddAttr((*a)[i]); + for ( const auto& attr : *a ) + AddAttr(attr); delete a; } Attributes::~Attributes() { - loop_over_list(*attrs, i) - Unref((*attrs)[i]); + for ( const auto& attr : *attrs ) + Unref(attr); delete attrs; @@ -188,8 +188,8 @@ void Attributes::AddAttr(Attr* attr) void Attributes::AddAttrs(Attributes* a) { attr_list* as = a->Attrs(); - loop_over_list(*as, i) - AddAttr((*as)[i]); + for ( const auto& attr : *as ) + AddAttr(attr); Unref(a); } @@ -199,9 +199,8 @@ Attr* Attributes::FindAttr(attr_tag t) const if ( ! attrs ) return 0; - loop_over_list(*attrs, i) + for ( const auto& a : *attrs ) { - Attr* a = (*attrs)[i]; if ( a->Tag() == t ) return a; } @@ -391,9 +390,8 @@ void Attributes::CheckAttr(Attr* a) int num_expires = 0; if ( attrs ) { - loop_over_list(*attrs, i) + for ( const auto& a : *attrs ) { - Attr* a = (*attrs)[i]; if ( a->Tag() == ATTR_EXPIRE_READ || a->Tag() == ATTR_EXPIRE_WRITE || a->Tag() == ATTR_EXPIRE_CREATE ) @@ -505,9 +503,8 @@ bool Attributes::operator==(const Attributes& other) const if ( ! other.attrs ) return false; - loop_over_list(*attrs, i) + for ( const auto& a : *attrs ) { - Attr* a = (*attrs)[i]; Attr* o = other.FindAttr(a->Tag()); if ( ! o ) @@ -517,9 +514,8 @@ bool Attributes::operator==(const Attributes& other) const return false; } - loop_over_list(*other.attrs, j) + for ( const auto& o : *other.attrs ) { - Attr* o = (*other.attrs)[j]; Attr* a = FindAttr(o->Tag()); if ( ! a ) diff --git a/src/CompHash.cc b/src/CompHash.cc index 4e5366edde..74186f2615 100644 --- a/src/CompHash.cc +++ b/src/CompHash.cc @@ -675,10 +675,10 @@ ListVal* CompositeHash::RecoverVals(const HashKey* k) const const char* kp = (const char*) k->Key(); const char* const k_end = kp + k->Size(); - loop_over_list(*tl, i) + for ( const auto& type : *tl ) { Val* v = nullptr; - kp = RecoverOneVal(k, kp, k_end, (*tl)[i], v, false); + kp = RecoverOneVal(k, kp, k_end, type, v, false); ASSERT(v); l->Append(v); } diff --git a/src/Conn.cc b/src/Conn.cc index dc510038f0..a6247e378a 100644 --- a/src/Conn.cc +++ b/src/Conn.cc @@ -481,8 +481,8 @@ void Connection::ConnectionEvent(EventHandlerPtr f, analyzer::Analyzer* a, val_l { // This may actually happen if there is no local handler // and a previously existing remote handler went away. - loop_over_list(vl, i) - Unref(vl[i]); + for ( const auto& v : vl) + Unref(v); return; } @@ -540,11 +540,10 @@ void Connection::CancelTimers() // traversing. Thus, we first make a copy of the list which we then // iterate through. timer_list tmp(timers.length()); - loop_over_list(timers, j) - tmp.append(timers[j]); + std::copy(timers.begin(), timers.end(), std::back_inserter(tmp)); - loop_over_list(tmp, i) - GetTimerMgr()->Cancel(tmp[i]); + for ( const auto& timer : tmp ) + GetTimerMgr()->Cancel(timer); timers_canceled = 1; timers.clear(); diff --git a/src/Dict.cc b/src/Dict.cc index a179bbea77..d3eea6591f 100644 --- a/src/Dict.cc +++ b/src/Dict.cc @@ -100,9 +100,8 @@ void Dictionary::DeInit() if ( tbl[i] ) { PList* chain = tbl[i]; - loop_over_list(*chain, j) + for ( const auto& e : *chain ) { - DictEntry* e = (*chain)[j]; if ( delete_func ) delete_func(e->value); delete e; @@ -120,9 +119,8 @@ void Dictionary::DeInit() if ( tbl2[i] ) { PList* chain = tbl2[i]; - loop_over_list(*chain, j) + for ( const auto& e : *chain ) { - DictEntry* e = (*chain)[j]; if ( delete_func ) delete_func(e->value); delete e; @@ -249,10 +247,8 @@ void* Dictionary::DoRemove(DictEntry* entry, hash_t h, order->remove(entry); // Adjust existing cookies. - loop_over_list(cookies, i) + for ( const auto& c : cookies ) { - IterCookie* c = cookies[i]; - // Is the affected bucket the current one? if ( (unsigned int) c->bucket == h ) { @@ -482,9 +478,8 @@ void* Dictionary::Insert(DictEntry* new_entry, int copy_key) // For ongoing iterations: If we already passed the bucket where this // entry was put, add it to the cookie's list of inserted entries. - loop_over_list(cookies, i) + for ( const auto& c : cookies ) { - IterCookie* c = cookies[i]; if ( h < (unsigned int) c->bucket ) c->inserted.append(new_entry); } @@ -606,8 +601,8 @@ unsigned int Dictionary::MemoryAllocation() const if ( tbl[i] ) { PList* chain = tbl[i]; - loop_over_list(*chain, j) - size += padded_sizeof(DictEntry) + pad_size((*chain)[j]->len); + for ( const auto& c : *chain ) + size += padded_sizeof(DictEntry) + pad_size(c->len); size += chain->MemoryAllocation(); } @@ -622,8 +617,8 @@ unsigned int Dictionary::MemoryAllocation() const if ( tbl2[i] ) { PList* chain = tbl2[i]; - loop_over_list(*chain, j) - size += padded_sizeof(DictEntry) + pad_size((*chain)[j]->len); + for ( const auto& c : *chain ) + size += padded_sizeof(DictEntry) + pad_size(c->len); size += chain->MemoryAllocation(); } diff --git a/src/Event.h b/src/Event.h index 475dc5f577..1673c9d41f 100644 --- a/src/Event.h +++ b/src/Event.h @@ -86,8 +86,8 @@ public: QueueEvent(new Event(h, std::move(vl), src, aid, mgr, obj)); else { - loop_over_list(vl, i) - Unref(vl[i]); + for ( const auto& v : vl ) + Unref(v); } } diff --git a/src/EventHandler.cc b/src/EventHandler.cc index 0f06ad50ca..8c1b9f28f2 100644 --- a/src/EventHandler.cc +++ b/src/EventHandler.cc @@ -117,8 +117,8 @@ void EventHandler::Call(val_list* vl, bool no_remote) Unref(local->Call(vl)); else { - loop_over_list(*vl, i) - Unref((*vl)[i]); + for ( auto v : *vl ) + Unref(v); } } diff --git a/src/Expr.cc b/src/Expr.cc index 450ac73437..dee29577e7 100644 --- a/src/Expr.cc +++ b/src/Expr.cc @@ -2113,8 +2113,7 @@ bool AssignExpr::TypeCheck(attr_list* attrs) if ( attrs ) { attr_copy = new attr_list(attrs->length()); - loop_over_list(*attrs, i) - attr_copy->append((*attrs)[i]); + std::copy(attrs->begin(), attrs->end(), std::back_inserter(*attr_copy)); } bool empty_list_assignment = (op2->AsListExpr()->Exprs().length() == 0); @@ -2194,8 +2193,7 @@ bool AssignExpr::TypeCheck(attr_list* attrs) { attr_list* a = sce->Attrs()->Attrs(); attrs = new attr_list(a->length()); - loop_over_list(*a, i) - attrs->append((*a)[i]); + std::copy(a->begin(), a->end(), std::back_inserter(*attrs)); } int errors_before = reporter->Errors(); @@ -3029,10 +3027,8 @@ RecordConstructorExpr::RecordConstructorExpr(ListExpr* constructor_list) const expr_list& exprs = constructor_list->Exprs(); type_decl_list* record_types = new type_decl_list(exprs.length()); - loop_over_list(exprs, i) + for ( const auto& e : exprs ) { - Expr* e = exprs[i]; - if ( e->Tag() != EXPR_FIELD_ASSIGN ) { Error("bad type in record constructor", e); @@ -3139,12 +3135,12 @@ TableConstructorExpr::TableConstructorExpr(ListExpr* constructor_list, const expr_list& cle = constructor_list->Exprs(); // check and promote all index expressions in ctor list - loop_over_list(cle, i) + for ( const auto& expr : cle ) { - if ( cle[i]->Tag() != EXPR_ASSIGN ) + if ( expr->Tag() != EXPR_ASSIGN ) continue; - Expr* idx_expr = cle[i]->AsAssignExpr()->Op1(); + Expr* idx_expr = expr->AsAssignExpr()->Op1(); if ( idx_expr->Tag() != EXPR_LIST ) continue; @@ -3178,8 +3174,8 @@ Val* TableConstructorExpr::Eval(Frame* f) const Val* aggr = new TableVal(Type()->AsTableType(), attrs); const expr_list& exprs = op->AsListExpr()->Exprs(); - loop_over_list(exprs, i) - exprs[i]->EvalIntoAggregate(type, aggr, f); + for ( const auto& expr : exprs ) + expr->EvalIntoAggregate(type, aggr, f); return aggr; } @@ -3193,8 +3189,8 @@ Val* TableConstructorExpr::InitVal(const BroType* t, Val* aggr) const TableVal* tval = aggr ? aggr->AsTableVal() : new TableVal(tt, attrs); const expr_list& exprs = op->AsListExpr()->Exprs(); - loop_over_list(exprs, i) - exprs[i]->EvalIntoAggregate(t, tval, 0); + for ( const auto& expr : exprs ) + expr->EvalIntoAggregate(t, tval, 0); return tval; } @@ -3282,9 +3278,9 @@ Val* SetConstructorExpr::Eval(Frame* f) const TableVal* aggr = new TableVal(type->AsTableType(), attrs); const expr_list& exprs = op->AsListExpr()->Exprs(); - loop_over_list(exprs, i) + for ( const auto& expr : exprs ) { - Val* element = exprs[i]->Eval(f); + Val* element = expr->Eval(f); aggr->Assign(element, 0); Unref(element); } @@ -3302,9 +3298,8 @@ Val* SetConstructorExpr::InitVal(const BroType* t, Val* aggr) const TableVal* tval = aggr ? aggr->AsTableVal() : new TableVal(tt, attrs); const expr_list& exprs = op->AsListExpr()->Exprs(); - loop_over_list(exprs, i) + for ( const auto& e : exprs ) { - Expr* e = exprs[i]; Val* element = check_and_promote(e->Eval(0), index_type, 1); if ( ! element || ! tval->Assign(element, 0) ) @@ -4417,8 +4412,8 @@ ListExpr::ListExpr(Expr* e) : Expr(EXPR_LIST) ListExpr::~ListExpr() { - loop_over_list(exprs, i) - Unref(exprs[i]); + for ( const auto& expr: exprs ) + Unref(expr); } void ListExpr::Append(Expr* e) @@ -4429,8 +4424,8 @@ void ListExpr::Append(Expr* e) int ListExpr::IsPure() const { - loop_over_list(exprs, i) - if ( ! exprs[i]->IsPure() ) + for ( const auto& expr : exprs ) + if ( ! expr->IsPure() ) return 0; return 1; @@ -4438,8 +4433,8 @@ int ListExpr::IsPure() const int ListExpr::AllConst() const { - loop_over_list(exprs, i) - if ( ! exprs[i]->IsConst() ) + for ( const auto& expr : exprs ) + if ( ! expr->IsConst() ) return 0; return 1; @@ -4449,9 +4444,9 @@ Val* ListExpr::Eval(Frame* f) const { ListVal* v = new ListVal(TYPE_ANY); - loop_over_list(exprs, i) + for ( const auto& expr : exprs ) { - Val* ev = exprs[i]->Eval(f); + Val* ev = expr->Eval(f); if ( ! ev ) { RuntimeError("uninitialized list value"); @@ -4476,12 +4471,12 @@ BroType* ListExpr::InitType() const if ( exprs[0]->IsRecordElement(0) ) { type_decl_list* types = new type_decl_list(exprs.length()); - loop_over_list(exprs, i) + for ( const auto& expr : exprs ) { TypeDecl* td = new TypeDecl(0, 0); - if ( ! exprs[i]->IsRecordElement(td) ) + if ( ! expr->IsRecordElement(td) ) { - exprs[i]->Error("record element expected"); + expr->Error("record element expected"); delete td; delete types; return 0; @@ -4497,9 +4492,8 @@ BroType* ListExpr::InitType() const else { TypeList* tl = new TypeList(); - loop_over_list(exprs, i) + for ( const auto& e : exprs ) { - Expr* e = exprs[i]; BroType* ti = e->Type(); // Collapse any embedded sets or lists. @@ -4633,10 +4627,8 @@ Val* ListExpr::InitVal(const BroType* t, Val* aggr) const // know how to add themselves to a table or record. Another // possibility is an expression that evaluates itself to a // table, which we can then add to the aggregate. - loop_over_list(exprs, i) + for ( const auto& e : exprs ) { - Expr* e = exprs[i]; - if ( e->Tag() == EXPR_ASSIGN || e->Tag() == EXPR_FIELD_ASSIGN ) { if ( ! e->InitVal(t, aggr) ) @@ -4674,17 +4666,17 @@ Val* ListExpr::AddSetInit(const BroType* t, Val* aggr) const const TableType* tt = tv->Type()->AsTableType(); const TypeList* it = tt->Indices(); - loop_over_list(exprs, i) + for ( const auto& expr : exprs ) { Val* element; - if ( exprs[i]->Type()->IsSet() ) + if ( expr->Type()->IsSet() ) // A set to flatten. - element = exprs[i]->Eval(0); - else if ( exprs[i]->Type()->Tag() == TYPE_LIST ) - element = exprs[i]->InitVal(it, 0); + element = expr->Eval(0); + else if ( expr->Type()->Tag() == TYPE_LIST ) + element = expr->InitVal(it, 0); else - element = exprs[i]->InitVal((*it->Types())[0], 0); + element = expr->InitVal((*it->Types())[0], 0); if ( ! element ) return 0; @@ -4703,7 +4695,7 @@ Val* ListExpr::AddSetInit(const BroType* t, Val* aggr) const continue; } - if ( exprs[i]->Type()->Tag() == TYPE_LIST ) + if ( expr->Type()->Tag() == TYPE_LIST ) element = check_and_promote(element, it, 1); else element = check_and_promote(element, (*it->Types())[0], 1); @@ -4739,8 +4731,8 @@ void ListExpr::ExprDescribe(ODesc* d) const Expr* ListExpr::MakeLvalue() { - loop_over_list(exprs, i) - if ( exprs[i]->Tag() != EXPR_NAME ) + for ( const auto & expr : exprs ) + if ( expr->Tag() != EXPR_NAME ) ExprError("can only assign to list of identifiers"); return new RefExpr(this); @@ -4764,9 +4756,9 @@ TraversalCode ListExpr::Traverse(TraversalCallback* cb) const TraversalCode tc = cb->PreExpr(this); HANDLE_TC_EXPR_PRE(tc); - loop_over_list(exprs, i) + for ( const auto& expr : exprs ) { - tc = exprs[i]->Traverse(cb); + tc = expr->Traverse(cb); HANDLE_TC_EXPR_PRE(tc); } @@ -4785,11 +4777,11 @@ RecordAssignExpr::RecordAssignExpr(Expr* record, Expr* init_list, int is_init) // 2) a string indicating the field name, then (as the next element) // the value to use for that field. - for ( int i = 0; i < inits.length(); ++i ) + for ( const auto& init : inits ) { - if ( inits[i]->Type()->Tag() == TYPE_RECORD ) + if ( init->Type()->Tag() == TYPE_RECORD ) { - RecordType* t = inits[i]->Type()->AsRecordType(); + RecordType* t = init->Type()->AsRecordType(); for ( int j = 0; j < t->NumFields(); ++j ) { @@ -4800,15 +4792,15 @@ RecordAssignExpr::RecordAssignExpr(Expr* record, Expr* init_list, int is_init) same_type(lhs->FieldType(field), t->FieldType(j)) ) { FieldExpr* fe_lhs = new FieldExpr(record, field_name); - FieldExpr* fe_rhs = new FieldExpr(inits[i], field_name); + FieldExpr* fe_rhs = new FieldExpr(init, field_name); Append(get_assign_expr(fe_lhs->Ref(), fe_rhs->Ref(), is_init)); } } } - else if ( inits[i]->Tag() == EXPR_FIELD_ASSIGN ) + else if ( init->Tag() == EXPR_FIELD_ASSIGN ) { - FieldAssignExpr* rf = (FieldAssignExpr*) inits[i]; + FieldAssignExpr* rf = (FieldAssignExpr*) init; rf->Ref(); const char* field_name = ""; // rf->FieldName(); @@ -5072,8 +5064,8 @@ int check_and_promote_args(ListExpr*& args, RecordType* types) def_elements.insert(def_attr->AttrExpr()); } - loop_over_list(def_elements, i) - el.append(def_elements[i]->Ref()); + for ( const auto& elem : def_elements ) + el.append(elem->Ref()); } TypeList* tl = new TypeList(); @@ -5115,18 +5107,22 @@ val_list* eval_list(Frame* f, const ListExpr* l) const expr_list& e = l->Exprs(); val_list* v = new val_list(e.length()); - loop_over_list(e, i) + bool success = true; + for ( const auto& expr : e ) { - Val* ev = e[i]->Eval(f); + Val* ev = expr->Eval(f); if ( ! ev ) + { + success = false; break; + } v->append(ev); } - if ( i < e.length() ) + if ( ! success ) { // Failure. - loop_over_list(*v, j) - Unref((*v)[j]); + for ( const auto& val : *v ) + Unref(val); delete v; return 0; } diff --git a/src/Func.cc b/src/Func.cc index ccd9793291..8f0fa7f5c8 100644 --- a/src/Func.cc +++ b/src/Func.cc @@ -73,11 +73,11 @@ std::string render_call_stack() if ( ci.args ) { - loop_over_list(*ci.args, i) + for ( const auto& arg : *ci.args ) { ODesc d; d.SetShort(); - (*ci.args)[i]->Describe(&d); + arg->Describe(&d); if ( ! arg_desc.empty() ) arg_desc += ", "; @@ -237,8 +237,8 @@ std::pair Func::HandlePluginResult(std::pair plugin_resu } } - loop_over_list(*args, i) - Unref((*args)[i]); + for ( const auto& arg : *args ) + Unref(arg); return plugin_result; } @@ -300,8 +300,8 @@ Val* BroFunc::Call(val_list* args, Frame* parent) const { // Can only happen for events and hooks. assert(Flavor() == FUNC_FLAVOR_EVENT || Flavor() == FUNC_FLAVOR_HOOK); - loop_over_list(*args, i) - Unref((*args)[i]); + for ( const auto& arg : *args ) + Unref(arg); return Flavor() == FUNC_FLAVOR_HOOK ? val_mgr->GetTrue() : 0; } @@ -331,11 +331,11 @@ Val* BroFunc::Call(val_list* args, Frame* parent) const stmt_flow_type flow = FLOW_NEXT; Val* result = 0; - for ( size_t i = 0; i < bodies.size(); ++i ) + for ( const auto& body : bodies ) { if ( sample_logger ) sample_logger->LocationSeen( - bodies[i].stmts->GetLocationInfo()); + body.stmts->GetLocationInfo()); Unref(result); @@ -356,7 +356,7 @@ Val* BroFunc::Call(val_list* args, Frame* parent) const try { - result = bodies[i].stmts->Exec(f, flow); + result = body.stmts->Exec(f, flow); } catch ( InterpreterException& e ) @@ -397,8 +397,8 @@ Val* BroFunc::Call(val_list* args, Frame* parent) const // We have an extra Ref for each argument (so that they don't get // deleted between bodies), release that. - loop_over_list(*args, k) - Unref((*args)[k]); + for ( const auto& arg : *args ) + Unref(arg); if ( Flavor() == FUNC_FLAVOR_HOOK ) { @@ -441,8 +441,8 @@ void BroFunc::AddBody(Stmt* new_body, id_list* new_inits, int new_frame_size, { // For functions, we replace the old body with the new one. assert(bodies.size() <= 1); - for ( unsigned int i = 0; i < bodies.size(); ++i ) - Unref(bodies[i].stmts); + for ( const auto& body : bodies ) + Unref(body.stmts); bodies.clear(); } @@ -540,8 +540,8 @@ Val* BuiltinFunc::Call(val_list* args, Frame* parent) const Val* result = func(parent, args); call_stack.pop_back(); - loop_over_list(*args, i) - Unref((*args)[i]); + for ( const auto& arg : *args ) + Unref(arg); // Don't Unref() args, that's the caller's responsibility. if ( result && g_trace_state.DoTrace() ) diff --git a/src/OpaqueVal.cc b/src/OpaqueVal.cc index d372f525a5..595102e8a2 100644 --- a/src/OpaqueVal.cc +++ b/src/OpaqueVal.cc @@ -233,9 +233,8 @@ void MD5Val::digest(val_list& vlist, u_char result[MD5_DIGEST_LENGTH]) { EVP_MD_CTX* h = hash_init(Hash_MD5); - loop_over_list(vlist, i) + for ( const auto& v : vlist ) { - Val* v = vlist[i]; if ( v->Type()->Tag() == TYPE_STRING ) { const BroString* str = v->AsString(); @@ -385,9 +384,8 @@ void SHA1Val::digest(val_list& vlist, u_char result[SHA_DIGEST_LENGTH]) { EVP_MD_CTX* h = hash_init(Hash_SHA1); - loop_over_list(vlist, i) + for ( const auto& v : vlist ) { - Val* v = vlist[i]; if ( v->Type()->Tag() == TYPE_STRING ) { const BroString* str = v->AsString(); @@ -529,9 +527,8 @@ void SHA256Val::digest(val_list& vlist, u_char result[SHA256_DIGEST_LENGTH]) { EVP_MD_CTX* h = hash_init(Hash_SHA256); - loop_over_list(vlist, i) + for ( const auto& v : vlist ) { - Val* v = vlist[i]; if ( v->Type()->Tag() == TYPE_STRING ) { const BroString* str = v->AsString(); diff --git a/src/Reporter.cc b/src/Reporter.cc index d44eff556b..d4095a4c9f 100644 --- a/src/Reporter.cc +++ b/src/Reporter.cc @@ -502,10 +502,7 @@ void Reporter::DoLog(const char* prefix, EventHandlerPtr event, FILE* out, vl.append(conn->BuildConnVal()); if ( addl ) - { - loop_over_list(*addl, i) - vl.append((*addl)[i]); - } + std::copy(addl->begin(), addl->end(), std::back_inserter(vl)); if ( conn ) conn->ConnectionEventFast(event, 0, std::move(vl)); @@ -516,8 +513,8 @@ void Reporter::DoLog(const char* prefix, EventHandlerPtr event, FILE* out, { if ( addl ) { - loop_over_list(*addl, i) - Unref((*addl)[i]); + for ( const auto& av : *addl ) + Unref(av); } } diff --git a/src/Rule.cc b/src/Rule.cc index 57cb82f65e..c9f3734692 100644 --- a/src/Rule.cc +++ b/src/Rule.cc @@ -13,25 +13,25 @@ Rule::~Rule() { delete [] id; - loop_over_list(patterns, i) + for ( const auto& p : patterns ) { - delete [] patterns[i]->pattern; - delete patterns[i]; + delete [] p->pattern; + delete p; } - loop_over_list(hdr_tests, j) - delete hdr_tests[j]; + for ( const auto& test : hdr_tests ) + delete test; - loop_over_list(conditions, k) - delete conditions[k]; + for ( const auto& cond : conditions ) + delete cond; - loop_over_list(actions, l) - delete actions[l]; + for ( const auto& action : actions ) + delete action; - loop_over_list(preconds, m) + for ( const auto& prec : preconds ) { - delete [] preconds[m]->id; - delete preconds[m]; + delete [] prec->id; + delete prec; } } @@ -49,21 +49,20 @@ void Rule::PrintDebug() { fprintf(stderr, "Rule %s (%d) %s\n", id, idx, active ? "[active]" : "[disabled]"); - loop_over_list(patterns, i) + for ( const auto& p : patterns ) { fprintf(stderr, " %-8s |%s| (%d) \n", - TypeToString(patterns[i]->type), patterns[i]->pattern, - patterns[i]->id); + TypeToString(p->type), p->pattern, p->id); } - loop_over_list(hdr_tests, j) - hdr_tests[j]->PrintDebug(); + for ( const auto& h : hdr_tests ) + h->PrintDebug(); - loop_over_list(conditions, k) - conditions[k]->PrintDebug(); + for ( const auto& c : conditions ) + c->PrintDebug(); - loop_over_list(actions, l) - actions[l]->PrintDebug(); + for ( const auto& a : actions ) + a->PrintDebug(); fputs("\n", stderr); } diff --git a/src/RuleMatcher.cc b/src/RuleMatcher.cc index 50570805ef..93adc8ca6b 100644 --- a/src/RuleMatcher.cc +++ b/src/RuleMatcher.cc @@ -83,21 +83,20 @@ RuleHdrTest::RuleHdrTest(RuleHdrTest& h) comp = h.comp; vals = new maskedvalue_list; - loop_over_list(*h.vals, i) - vals->append(new MaskedValue(*(*h.vals)[i])); + for ( const auto& val : *h.vals ) + vals->append(new MaskedValue(*val)); prefix_vals = h.prefix_vals; for ( int j = 0; j < Rule::TYPES; ++j ) { - loop_over_list(h.psets[j], k) + for ( PatternSet* orig_set : h.psets[j] ) { - PatternSet* orig_set = h.psets[j][k]; PatternSet* copied_set = new PatternSet; copied_set->re = 0; copied_set->ids = orig_set->ids; - loop_over_list(orig_set->patterns, l) - copied_set->patterns.append(copy_string(orig_set->patterns[l])); + for ( const auto& pattern : orig_set->patterns ) + copied_set->patterns.append(copy_string(pattern)); delete copied_set; // TODO: Why do we create copied_set only to then // never use it? @@ -115,14 +114,14 @@ RuleHdrTest::RuleHdrTest(RuleHdrTest& h) RuleHdrTest::~RuleHdrTest() { - loop_over_list(*vals, i) - delete (*vals)[i]; + for ( auto val : *vals ) + delete val; delete vals; for ( int i = 0; i < Rule::TYPES; ++i ) { - loop_over_list(psets[i], j) - delete psets[i][j]->re; + for ( auto pset : psets[i] ) + delete pset->re; } delete ruleset; @@ -154,12 +153,12 @@ void RuleHdrTest::PrintDebug() fprintf(stderr, " RuleHdrTest %s[%d:%d] %s", str_prot[prot], offset, size, str_comp[comp]); - loop_over_list(*vals, i) + for ( const auto& val : *vals ) fprintf(stderr, " 0x%08x/0x%08x", - (*vals)[i]->val, (*vals)[i]->mask); + val->val, val->mask); - for ( size_t i = 0; i < prefix_vals.size(); ++i ) - fprintf(stderr, " %s", prefix_vals[i].AsString().c_str()); + for ( const auto& prefix : prefix_vals ) + fprintf(stderr, " %s", prefix.AsString().c_str()); fprintf(stderr, "\n"); } @@ -181,22 +180,22 @@ RuleEndpointState::RuleEndpointState(analyzer::Analyzer* arg_analyzer, bool arg_ RuleEndpointState::~RuleEndpointState() { - loop_over_list(matchers, i) + for ( auto matcher : matchers ) { - delete matchers[i]->state; - delete matchers[i]; + delete matcher->state; + delete matcher; } - loop_over_list(matched_text, j) - delete matched_text[j]; + for ( auto text : matched_text ) + delete text; } RuleFileMagicState::~RuleFileMagicState() { - loop_over_list(matchers, i) + for ( auto matcher : matchers ) { - delete matchers[i]->state; - delete matchers[i]; + delete matcher->state; + delete matcher; } } @@ -214,8 +213,8 @@ RuleMatcher::~RuleMatcher() #endif Delete(root); - loop_over_list(rules, i) - delete rules[i]; + for ( auto rule : rules ) + delete rule; } void RuleMatcher::Delete(RuleHdrTest* node) @@ -280,13 +279,13 @@ void RuleMatcher::AddRule(Rule* rule) void RuleMatcher::BuildRulesTree() { - loop_over_list(rules, r) + for ( const auto& rule : rules ) { - if ( ! rules[r]->Active() ) + if ( ! rule->Active() ) continue; - rules[r]->SortHdrTests(); - InsertRuleIntoTree(rules[r], 0, root, 0); + rule->SortHdrTests(); + InsertRuleIntoTree(rule, 0, root, 0); } } @@ -294,10 +293,8 @@ void RuleMatcher::InsertRuleIntoTree(Rule* r, int testnr, RuleHdrTest* dest, int level) { // Initiliaze the preconditions - loop_over_list(r->preconds, i) + for ( const auto& pc : r->preconds ) { - Rule::Precond* pc = r->preconds[i]; - Rule* pc_rule = rules_by_id.Lookup(pc->id); if ( ! pc_rule ) { @@ -350,9 +347,8 @@ void RuleMatcher::BuildRegEx(RuleHdrTest* hdr_test, string_list* exprs, // For each type, get all patterns on this node. for ( Rule* r = hdr_test->pattern_rules; r; r = r->next ) { - loop_over_list(r->patterns, j) + for ( const auto& p : r->patterns ) { - Rule::Pattern* p = r->patterns[j]; exprs[p->type].append(p->pattern); ids[p->type].push_back(p->id); } @@ -457,9 +453,10 @@ static inline uint32 getval(const u_char* data, int size) template static inline bool match_or(const maskedvalue_list& mvals, uint32 v, FuncT comp) { - loop_over_list(mvals, i) + // TODO: this could be a find_if + for ( const auto& val : mvals ) { - if ( comp(v & mvals[i]->mask, mvals[i]->val) ) + if ( comp(v & val->mask, val->val) ) return true; } return false; @@ -485,9 +482,10 @@ template static inline bool match_not_and(const maskedvalue_list& mvals, uint32 v, FuncT comp) { - loop_over_list(mvals, i) + // TODO: this could be a find_if + for ( const auto& val : mvals ) { - if ( comp(v & mvals[i]->mask, mvals[i]->val) ) + if ( comp(v & val->mask, val->val) ) return false; } return true; @@ -582,9 +580,8 @@ RuleFileMagicState* RuleMatcher::InitFileMagic() const { RuleFileMagicState* state = new RuleFileMagicState(); - loop_over_list(root->psets[Rule::FILE_MAGIC], i) + for ( const auto& set : root->psets[Rule::FILE_MAGIC] ) { - RuleHdrTest::PatternSet* set = root->psets[Rule::FILE_MAGIC][i]; assert(set->re); RuleFileMagicState::Matcher* m = new RuleFileMagicState::Matcher; m->state = new RE_Match_State(set->re); @@ -602,13 +599,13 @@ bool RuleMatcher::AllRulePatternsMatched(const Rule* r, MatchPos matchpos, DBG_LOG(DBG_RULES, "Checking rule: %s", r->id); // Check whether all patterns of the rule have matched. - loop_over_list(r->patterns, j) + for ( const auto& pattern : r->patterns ) { - if ( ams.find(r->patterns[j]->id) == ams.end() ) + if ( ams.find(pattern->id) == ams.end() ) return false; // See if depth is satisfied. - if ( matchpos > r->patterns[j]->offset + r->patterns[j]->depth ) + if ( matchpos > pattern->offset + pattern->depth ) return false; // FIXME: How to check for offset ??? ### @@ -645,10 +642,8 @@ RuleMatcher::MIME_Matches* RuleMatcher::Match(RuleFileMagicState* state, bool newmatch = false; - loop_over_list(state->matchers, x) + for ( const auto& m : state->matchers ) { - RuleFileMagicState::Matcher* m = state->matchers[x]; - if ( m->state->Match(data, len, true, false, true) ) newmatch = true; } @@ -660,9 +655,8 @@ RuleMatcher::MIME_Matches* RuleMatcher::Match(RuleFileMagicState* state, AcceptingMatchSet accepted_matches; - loop_over_list(state->matchers, y) + for ( const auto& m : state->matchers ) { - RuleFileMagicState::Matcher* m = state->matchers[y]; const AcceptingMatchSet& ams = m->state->AcceptedMatches(); accepted_matches.insert(ams.begin(), ams.end()); } @@ -687,10 +681,10 @@ RuleMatcher::MIME_Matches* RuleMatcher::Match(RuleFileMagicState* state, { Rule* r = *it; - loop_over_list(r->actions, rai) + for ( const auto& action : r->actions ) { const RuleActionMIME* ram = - dynamic_cast(r->actions[rai]); + dynamic_cast(action); if ( ! ram ) continue; @@ -739,11 +733,8 @@ RuleEndpointState* RuleMatcher::InitEndpoint(analyzer::Analyzer* analyzer, { for ( int i = 0; i < Rule::TYPES; ++i ) { - loop_over_list(hdr_test->psets[i], j) + for ( const auto& set : hdr_test->psets[i] ) { - RuleHdrTest::PatternSet* set = - hdr_test->psets[i][j]; - assert(set->re); RuleEndpointState::Matcher* m = @@ -860,9 +851,8 @@ void RuleMatcher::Match(RuleEndpointState* state, Rule::PatternType type, } // Feed data into all relevant matchers. - loop_over_list(state->matchers, x) + for ( const auto& m : state->matchers ) { - RuleEndpointState::Matcher* m = state->matchers[x]; if ( m->type == type && m->state->Match((const u_char*) data, data_len, bol, eol, clear) ) @@ -877,9 +867,8 @@ void RuleMatcher::Match(RuleEndpointState* state, Rule::PatternType type, AcceptingMatchSet accepted_matches; - loop_over_list(state->matchers, y ) + for ( const auto& m : state->matchers ) { - RuleEndpointState::Matcher* m = state->matchers[y]; const AcceptingMatchSet& ams = m->state->AcceptedMatches(); accepted_matches.insert(ams.begin(), ams.end()); } @@ -912,10 +901,8 @@ void RuleMatcher::Match(RuleEndpointState* state, Rule::PatternType type, DBG_LOG(DBG_RULES, "Accepted rule: %s", r->id); - loop_over_list(state->hdr_tests, k) + for ( const auto& h : state->hdr_tests ) { - RuleHdrTest* h = state->hdr_tests[k]; - DBG_LOG(DBG_RULES, "Checking for accepted rule on HdrTest %d", h->id); // Skip if rule does not belong to this node. @@ -965,9 +952,8 @@ void RuleMatcher::FinishEndpoint(RuleEndpointState* state) void RuleMatcher::ExecPureRules(RuleEndpointState* state, bool eos) { - loop_over_list(state->hdr_tests, i) + for ( const auto& hdr_test : state->hdr_tests ) { - RuleHdrTest* hdr_test = state->hdr_tests[i]; for ( Rule* r = hdr_test->pure_rules; r; r = r->next ) ExecRulePurely(r, 0, state, eos); } @@ -1002,10 +988,8 @@ bool RuleMatcher::EvalRuleConditions(Rule* r, RuleEndpointState* state, DBG_LOG(DBG_RULES, "Evaluating conditions for rule %s", r->ID()); // Check for other rules which have to match first. - loop_over_list(r->preconds, i) + for ( const auto& pc : r->preconds ) { - Rule::Precond* pc = r->preconds[i]; - RuleEndpointState* pc_state = state; if ( pc->opposite_dir ) @@ -1034,8 +1018,8 @@ bool RuleMatcher::EvalRuleConditions(Rule* r, RuleEndpointState* state, } } - loop_over_list(r->conditions, l) - if ( ! r->conditions[l]->DoMatch(r, state, data, len) ) + for ( const auto& cond : r->conditions ) + if ( ! cond->DoMatch(r, state, data, len) ) return false; DBG_LOG(DBG_RULES, "Conditions met: MATCH! %s", r->ID()); @@ -1052,13 +1036,12 @@ void RuleMatcher::ExecRuleActions(Rule* r, RuleEndpointState* state, state->matched_rules.push_back(r->Index()); - loop_over_list(r->actions, i) - r->actions[i]->DoAction(r, state, data, len); + for ( const auto& action : r->actions ) + action->DoAction(r, state, data, len); // This rule may trigger some other rules; check them. - loop_over_list(r->dependents, j) + for ( const auto& dep : r->dependents ) { - Rule* dep = (r->dependents)[j]; ExecRule(dep, state, eos); if ( state->opposite ) ExecRule(dep, state->opposite, eos); @@ -1071,10 +1054,8 @@ void RuleMatcher::ExecRule(Rule* rule, RuleEndpointState* state, bool eos) if ( is_member_of(state->matched_rules, rule->Index()) ) return; - loop_over_list(state->hdr_tests, i) + for ( const auto& h : state->hdr_tests ) { - RuleHdrTest* h = state->hdr_tests[i]; - // Is it on this HdrTest at all? if ( ! h->ruleset->Contains(rule->Index()) ) continue; @@ -1104,20 +1085,20 @@ void RuleMatcher::ClearEndpointState(RuleEndpointState* state) state->payload_size = -1; - loop_over_list(state->matchers, j) - state->matchers[j]->state->Clear(); + for ( const auto& matcher : state->matchers ) + matcher->state->Clear(); } void RuleMatcher::ClearFileMagicState(RuleFileMagicState* state) const { - loop_over_list(state->matchers, j) - state->matchers[j]->state->Clear(); + for ( const auto& matcher : state->matchers ) + matcher->state->Clear(); } void RuleMatcher::PrintDebug() { - loop_over_list(rules, i) - rules[i]->PrintDebug(); + for ( const auto& rule : rules ) + rule->PrintDebug(); fprintf(stderr, "\n---------------\n"); @@ -1187,9 +1168,8 @@ void RuleMatcher::GetStats(Stats* stats, RuleHdrTest* hdr_test) for ( int i = 0; i < Rule::TYPES; ++i ) { - loop_over_list(hdr_test->psets[i], j) + for ( const auto& set : hdr_test->psets[i] ) { - RuleHdrTest::PatternSet* set = hdr_test->psets[i][j]; assert(set->re); ++stats->matchers; @@ -1239,7 +1219,7 @@ void RuleMatcher::DumpStateStats(BroFile* f, RuleHdrTest* hdr_test) set->re->DFA()->NumStates(), Rule::TypeToString((Rule::PatternType)i), j)); - for ( auto id : set->ids ) + for ( const auto& id : set->ids ) { Rule* r = Rule::rule_table[id - 1]; f->Write(fmt("%s ", r->ID())); @@ -1350,8 +1330,8 @@ void id_to_maskedvallist(const char* id, maskedvalue_list* append_to, { ListVal* lv = v->AsTableVal()->ConvertToPureList(); val_list* vals = lv->Vals(); - loop_over_list(*vals, i ) - if ( ! val_to_maskedval((*vals)[i], append_to, prefix_vector) ) + for ( const auto& val : *vals ) + if ( ! val_to_maskedval(val, append_to, prefix_vector) ) { Unref(lv); return; diff --git a/src/Scope.cc b/src/Scope.cc index c02b4c2b24..56a9968417 100644 --- a/src/Scope.cc +++ b/src/Scope.cc @@ -47,8 +47,8 @@ Scope::~Scope() if ( attrs ) { - loop_over_list(*attrs, i) - Unref((*attrs)[i]); + for ( const auto& attr : *attrs ) + Unref(attr); delete attrs; } diff --git a/src/Stmt.cc b/src/Stmt.cc index a3c95b4a90..a452f9cd82 100644 --- a/src/Stmt.cc +++ b/src/Stmt.cc @@ -123,9 +123,9 @@ ExprListStmt::ExprListStmt(BroStmtTag t, ListExpr* arg_l) l = arg_l; const expr_list& e = l->Exprs(); - loop_over_list(e, i) + for ( const auto& expr : e ) { - const BroType* t = e[i]->Type(); + const BroType* t = expr->Type(); if ( ! t || t->Tag() == TYPE_VOID ) Error("value of type void illegal"); } @@ -172,9 +172,9 @@ TraversalCode ExprListStmt::Traverse(TraversalCallback* cb) const HANDLE_TC_STMT_PRE(tc); const expr_list& e = l->Exprs(); - loop_over_list(e, i) + for ( const auto& expr : e ) { - tc = e[i]->Traverse(cb); + tc = expr->Traverse(cb); HANDLE_TC_STMT_PRE(tc); } @@ -430,8 +430,8 @@ Case::~Case() Unref(expr_cases); Unref(s); - loop_over_list((*type_cases), i) - Unref((*type_cases)[i]); + for ( const auto& id : *type_cases ) + Unref(id); delete type_cases; } @@ -631,9 +631,9 @@ SwitchStmt::SwitchStmt(Expr* index, case_list* arg_cases) : { have_types = true; - loop_over_list((*tl), j) + for ( const auto& t : *tl ) { - BroType* ct = (*tl)[j]->Type(); + BroType* ct = t->Type(); if ( ! can_cast_value_to_type(e->Type(), ct) ) { @@ -641,7 +641,7 @@ SwitchStmt::SwitchStmt(Expr* index, case_list* arg_cases) : continue; } - if ( ! AddCaseLabelTypeMapping((*tl)[j], i) ) + if ( ! AddCaseLabelTypeMapping(t, i) ) { c->Error("duplicate case label"); continue; @@ -665,8 +665,8 @@ SwitchStmt::SwitchStmt(Expr* index, case_list* arg_cases) : SwitchStmt::~SwitchStmt() { - loop_over_list(*cases, i) - Unref((*cases)[i]); + for ( const auto& c : *cases ) + Unref(c); delete cases; delete comp_hash; @@ -793,9 +793,8 @@ int SwitchStmt::IsPure() const if ( ! e->IsPure() ) return 0; - loop_over_list(*cases, i) + for ( const auto& c : *cases ) { - Case* c = (*cases)[i]; if ( ! c->ExprCases()->IsPure() || ! c->Body()->IsPure() ) return 0; } @@ -812,8 +811,8 @@ void SwitchStmt::Describe(ODesc* d) const d->PushIndent(); d->AddCount(cases->length()); - loop_over_list(*cases, i) - (*cases)[i]->Describe(d); + for ( const auto& c : *cases ) + c->Describe(d); d->PopIndent(); if ( ! d->IsBinary() ) @@ -830,9 +829,9 @@ TraversalCode SwitchStmt::Traverse(TraversalCallback* cb) const tc = e->Traverse(cb); HANDLE_TC_STMT_PRE(tc); - loop_over_list(*cases, i) + for ( const auto& c : *cases ) { - tc = (*cases)[i]->Traverse(cb); + tc = c->Traverse(cb); HANDLE_TC_STMT_PRE(tc); } @@ -1132,8 +1131,8 @@ ForStmt::ForStmt(id_list* arg_loop_vars, Expr* loop_expr, ID* val_var) ForStmt::~ForStmt() { - loop_over_list(*loop_vars, i) - Unref((*loop_vars)[i]); + for ( const auto& var : *loop_vars ) + Unref(var); delete loop_vars; Unref(value_var); @@ -1275,9 +1274,9 @@ TraversalCode ForStmt::Traverse(TraversalCallback* cb) const TraversalCode tc = cb->PreStmt(this); HANDLE_TC_STMT_PRE(tc); - loop_over_list(*loop_vars, i) + for ( const auto& var : *loop_vars ) { - tc = (*loop_vars)[i]->Traverse(cb); + tc = var->Traverse(cb); HANDLE_TC_STMT_PRE(tc); } @@ -1445,8 +1444,8 @@ StmtList::StmtList() : Stmt(STMT_LIST) StmtList::~StmtList() { - loop_over_list(stmts, i) - Unref(stmts[i]); + for ( const auto& stmt : stmts ) + Unref(stmt); } Val* StmtList::Exec(Frame* f, stmt_flow_type& flow) const @@ -1454,17 +1453,17 @@ Val* StmtList::Exec(Frame* f, stmt_flow_type& flow) const RegisterAccess(); flow = FLOW_NEXT; - loop_over_list(stmts, i) + for ( const auto& stmt : stmts ) { - f->SetNextStmt(stmts[i]); + f->SetNextStmt(stmt); - if ( ! pre_execute_stmt(stmts[i], f) ) + if ( ! pre_execute_stmt(stmt, f) ) { // ### Abort or something } - Val* result = stmts[i]->Exec(f, flow); + Val* result = stmt->Exec(f, flow); - if ( ! post_execute_stmt(stmts[i], f, result, &flow) ) + if ( ! post_execute_stmt(stmt, f, result, &flow) ) { // ### Abort or something } @@ -1477,8 +1476,8 @@ Val* StmtList::Exec(Frame* f, stmt_flow_type& flow) const int StmtList::IsPure() const { - loop_over_list(stmts, i) - if ( ! stmts[i]->IsPure() ) + for ( const auto& stmt : stmts ) + if ( ! stmt->IsPure() ) return 0; return 1; } @@ -1502,9 +1501,9 @@ void StmtList::Describe(ODesc* d) const d->NL(); } - loop_over_list(stmts, i) + for ( const auto& stmt : stmts ) { - stmts[i]->Describe(d); + stmt->Describe(d); d->NL(); } @@ -1518,9 +1517,9 @@ TraversalCode StmtList::Traverse(TraversalCallback* cb) const TraversalCode tc = cb->PreStmt(this); HANDLE_TC_STMT_PRE(tc); - loop_over_list(stmts, i) + for ( const auto& stmt : stmts ) { - tc = stmts[i]->Traverse(cb); + tc = stmt->Traverse(cb); HANDLE_TC_STMT_PRE(tc); } @@ -1533,21 +1532,21 @@ Val* EventBodyList::Exec(Frame* f, stmt_flow_type& flow) const RegisterAccess(); flow = FLOW_NEXT; - loop_over_list(stmts, i) + for ( const auto& stmt : stmts ) { - f->SetNextStmt(stmts[i]); + f->SetNextStmt(stmt); // Ignore the return value, since there shouldn't be // any; and ignore the flow, since we still execute // all of the event bodies even if one of them does // a FLOW_RETURN. - if ( ! pre_execute_stmt(stmts[i], f) ) + if ( ! pre_execute_stmt(stmt, f) ) { // ### Abort or something } - Val* result = stmts[i]->Exec(f, flow); + Val* result = stmt->Exec(f, flow); - if ( ! post_execute_stmt(stmts[i], f, result, &flow) ) + if ( ! post_execute_stmt(stmt, f, result, &flow) ) { // ### Abort or something } } @@ -1563,16 +1562,16 @@ void EventBodyList::Describe(ODesc* d) const { if ( d->IsReadable() && stmts.length() > 0 ) { - loop_over_list(stmts, i) + for ( const auto& stmt : stmts ) { if ( ! d->IsBinary() ) { d->Add("{"); d->PushIndent(); - stmts[i]->AccessStats(d); + stmt->AccessStats(d); } - stmts[i]->Describe(d); + stmt->Describe(d); if ( ! d->IsBinary() ) { @@ -1588,8 +1587,8 @@ void EventBodyList::Describe(ODesc* d) const InitStmt::~InitStmt() { - loop_over_list(*inits, i) - Unref((*inits)[i]); + for ( const auto& init : *inits ) + Unref(init); delete inits; } @@ -1599,9 +1598,8 @@ Val* InitStmt::Exec(Frame* f, stmt_flow_type& flow) const RegisterAccess(); flow = FLOW_NEXT; - loop_over_list(*inits, i) + for ( const auto& aggr : *inits ) { - ID* aggr = (*inits)[i]; BroType* t = aggr->Type(); Val* v = 0; @@ -1649,9 +1647,9 @@ TraversalCode InitStmt::Traverse(TraversalCallback* cb) const TraversalCode tc = cb->PreStmt(this); HANDLE_TC_STMT_PRE(tc); - loop_over_list(*inits, i) + for ( const auto& init : *inits ) { - tc = (*inits)[i]->Traverse(cb); + tc = init->Traverse(cb); HANDLE_TC_STMT_PRE(tc); } diff --git a/src/Type.cc b/src/Type.cc index 60461e026f..bc2741a1fb 100644 --- a/src/Type.cc +++ b/src/Type.cc @@ -207,16 +207,16 @@ unsigned int BroType::MemoryAllocation() const TypeList::~TypeList() { - loop_over_list(types, i) - Unref(types[i]); + for ( const auto& type : types ) + Unref(type); Unref(pure_type); } int TypeList::AllMatch(const BroType* t, int is_init) const { - loop_over_list(types, i) - if ( ! same_type(types[i], t, is_init) ) + for ( const auto& type : types ) + if ( ! same_type(type, t, is_init) ) return 0; return 1; } @@ -381,9 +381,8 @@ TableType::TableType(TypeList* ind, BroType* yield) type_list* tl = indices->Types(); - loop_over_list(*tl, i) + for ( const auto& tli : *tl ) { - BroType* tli = (*tl)[i]; InternalTypeTag t = tli->InternalType(); if ( t == TYPE_INTERNAL_ERROR ) @@ -704,8 +703,8 @@ RecordType::RecordType(type_decl_list* arg_types) : BroType(TYPE_RECORD) RecordType* RecordType::ShallowClone() { auto pass = new type_decl_list(); - loop_over_list(*types, i) - pass->append(new TypeDecl(*(*types)[i])); + for ( const auto& type : *types ) + pass->append(new TypeDecl(*type)); return new RecordType(pass); } @@ -713,8 +712,8 @@ RecordType::~RecordType() { if ( types ) { - loop_over_list(*types, i) - delete (*types)[i]; + for ( auto type : *types ) + delete type; delete types; } @@ -823,17 +822,15 @@ const char* RecordType::AddFields(type_decl_list* others, attr_list* attr) if ( attr ) { - loop_over_list(*attr, j) + for ( const auto& at : *attr ) { - if ( (*attr)[j]->Tag() == ATTR_LOG ) + if ( at->Tag() == ATTR_LOG ) log = true; } } - loop_over_list(*others, i) + for ( const auto& td : *others ) { - TypeDecl* td = (*others)[i]; - if ( ! td->FindAttr(ATTR_DEFAULT) && ! td->FindAttr(ATTR_OPTIONAL) ) return "extension field must be &optional or have &default"; @@ -883,11 +880,11 @@ void RecordType::DescribeFields(ODesc* d) const { d->AddCount(0); d->AddCount(types->length()); - loop_over_list(*types, i) + for ( const auto& type : *types ) { - (*types)[i]->type->Describe(d); + type->type->Describe(d); d->SP(); - d->Add((*types)[i]->id); + d->Add(type->id); d->SP(); } } diff --git a/src/Val.cc b/src/Val.cc index c7281a781f..8b53fd0577 100644 --- a/src/Val.cc +++ b/src/Val.cc @@ -811,8 +811,8 @@ ListVal::ListVal(TypeTag t) ListVal::~ListVal() { - loop_over_list(vals, i) - Unref(vals[i]); + for ( const auto& val : vals ) + Unref(val); Unref(type); } @@ -822,9 +822,9 @@ RE_Matcher* ListVal::BuildRE() const Internal("non-string list in ListVal::IncludedInString"); RE_Matcher* re = new RE_Matcher(); - loop_over_list(vals, i) + for ( const auto& val : vals ) { - const char* vs = (const char*) (vals[i]->AsString()->Bytes()); + const char* vs = (const char*) (val->AsString()->Bytes()); re->AddPat(vs); } @@ -853,8 +853,8 @@ TableVal* ListVal::ConvertToSet() const SetType* s = new SetType(set_index, 0); TableVal* t = new TableVal(s); - loop_over_list(vals, i) - t->Assign(vals[i], 0); + for ( const auto& val : vals ) + t->Assign(val, 0); Unref(s); return t; @@ -891,8 +891,8 @@ Val* ListVal::DoClone(CloneState* state) lv->vals.resize(vals.length()); state->NewClone(this, lv); - loop_over_list(vals, i) - lv->Append(vals[i]->Clone(state)); + for ( const auto& val : vals ) + lv->Append(val->Clone(state)); return lv; } @@ -900,8 +900,8 @@ Val* ListVal::DoClone(CloneState* state) unsigned int ListVal::MemoryAllocation() const { unsigned int size = 0; - loop_over_list(vals, i) - size += vals[i]->MemoryAllocation(); + for ( const auto& val : vals ) + size += val->MemoryAllocation(); return size + padded_sizeof(*this) + vals.MemoryAllocation() - padded_sizeof(vals) + type->MemoryAllocation(); @@ -1380,8 +1380,8 @@ Val* TableVal::Default(Val* index) { const val_list* vl0 = index->AsListVal()->Vals(); vl = val_list(vl0->length()); - loop_over_list(*vl0, i) - vl.append((*vl0)[i]->Ref()); + for ( const auto& v : *vl0 ) + vl.append(v->Ref()); } else { @@ -2284,9 +2284,9 @@ Val* RecordVal::DoClone(CloneState* state) rv->origin = nullptr; state->NewClone(this, rv); - loop_over_list(*val.val_list_val, i) + for ( const auto& vlv : *val.val_list_val ) { - Val* v = (*val.val_list_val)[i] ? (*val.val_list_val)[i]->Clone(state) : nullptr; + Val* v = vlv ? vlv->Clone(state) : nullptr; rv->val.val_list_val->append(v); } @@ -2296,12 +2296,10 @@ Val* RecordVal::DoClone(CloneState* state) unsigned int RecordVal::MemoryAllocation() const { unsigned int size = 0; - const val_list* vl = AsRecord(); - loop_over_list(*vl, i) + for ( const auto& v : *vl ) { - Val* v = (*vl)[i]; if ( v ) size += v->MemoryAllocation(); } @@ -2679,8 +2677,8 @@ void delete_vals(val_list* vals) { if ( vals ) { - loop_over_list(*vals, i) - Unref((*vals)[i]); + for ( const auto& val : *vals ) + Unref(val); delete vals; } } diff --git a/src/Var.cc b/src/Var.cc index 74cfee291f..2f67c7a9d0 100644 --- a/src/Var.cc +++ b/src/Var.cc @@ -448,10 +448,8 @@ void end_func(Stmt* body) if ( attrs ) { - loop_over_list(*attrs, i) + for ( const auto& a : *attrs ) { - Attr* a = (*attrs)[i]; - if ( a->Tag() == ATTR_DEPRECATED ) continue; diff --git a/src/analyzer/Analyzer.cc b/src/analyzer/Analyzer.cc index 9977d44e70..95d0322459 100644 --- a/src/analyzer/Analyzer.cc +++ b/src/analyzer/Analyzer.cc @@ -728,11 +728,11 @@ void Analyzer::CancelTimers() // traversing. Thus, we first make a copy of the list which we then // iterate through. timer_list tmp(timers.length()); - loop_over_list(timers, j) - tmp.append(timers[j]); + std::copy(timers.begin(), timers.end(), back_inserter(tmp)); - loop_over_list(tmp, i) - Conn()->GetTimerMgr()->Cancel(tmp[i]); + // TODO: could be a for_each + for ( auto timer : tmp ) + Conn()->GetTimerMgr()->Cancel(timer); timers_canceled = 1; timers.clear(); diff --git a/src/broker/Manager.cc b/src/broker/Manager.cc index 0a6ae8fa65..4f7d7c3aae 100644 --- a/src/broker/Manager.cc +++ b/src/broker/Manager.cc @@ -1054,8 +1054,8 @@ void Manager::ProcessEvent(const broker::topic& topic, broker::zeek::Event ev) mgr.QueueEventFast(handler, std::move(vl), SOURCE_BROKER); else { - loop_over_list(vl, i) - Unref(vl[i]); + for ( const auto& v : vl ) + Unref(v); } } diff --git a/src/input/Manager.cc b/src/input/Manager.cc index c946e4ff10..59d8445aa9 100644 --- a/src/input/Manager.cc +++ b/src/input/Manager.cc @@ -1893,8 +1893,8 @@ bool Manager::SendEvent(ReaderFrontend* reader, const string& name, const int nu if ( convert_error ) { - loop_over_list(vl, i) - Unref(vl[i]); + for ( const auto& v : vl ) + Unref(v); return false; } diff --git a/src/parse.y b/src/parse.y index 820e958a98..4af53cbfdd 100644 --- a/src/parse.y +++ b/src/parse.y @@ -164,11 +164,8 @@ static type_decl_list* copy_type_decl_list(type_decl_list* tdl) type_decl_list* rval = new type_decl_list(); - loop_over_list(*tdl, i) - { - TypeDecl* td = (*tdl)[i]; + for ( const auto& td : *tdl ) rval->append(new TypeDecl(*td)); - } return rval; } @@ -180,9 +177,8 @@ static attr_list* copy_attr_list(attr_list* al) attr_list* rval = new attr_list(); - loop_over_list(*al, i) + for ( const auto& a : *al ) { - Attr* a = (*al)[i]; ::Ref(a); rval->append(a); } diff --git a/src/strings.bif b/src/strings.bif index 8e699c701c..4ab843d505 100644 --- a/src/strings.bif +++ b/src/strings.bif @@ -61,15 +61,15 @@ function levenshtein_distance%(s1: string, s2: string%): count function string_cat%(...%): string %{ int n = 0; - loop_over_list(@ARG@, i) - n += @ARG@[i]->AsString()->Len(); + for ( const auto& a : @ARG@ ) + n += a->AsString()->Len(); u_char* b = new u_char[n+1]; BroString* s = new BroString(1, b, n); - loop_over_list(@ARG@, j) + for ( const auto& a : @ARG@ ) { - const BroString* s = @ARG@[j]->AsString(); + const BroString* s = a->AsString(); memcpy(b, s->Bytes(), s->Len()); b += s->Len(); } diff --git a/src/util.cc b/src/util.cc index 2a6a5c37c4..c2973e6b9e 100644 --- a/src/util.cc +++ b/src/util.cc @@ -1001,11 +1001,12 @@ string bro_prefixes() { string rval; - loop_over_list(prefixes, j) - if ( j == 0 ) - rval.append(prefixes[j]); - else - rval.append(":").append(prefixes[j]); + for ( const auto& prefix : prefixes ) + { + if ( ! rval.empty() ) + rval.append(":"); + rval.append(prefix); + } return rval; } diff --git a/src/zeek.bif b/src/zeek.bif index 55d41948cd..ec098be083 100644 --- a/src/zeek.bif +++ b/src/zeek.bif @@ -1566,8 +1566,8 @@ function cat%(...%): string ODesc d; d.SetStyle(RAW_STYLE); - loop_over_list(@ARG@, i) - @ARG@[i]->Describe(&d); + for ( const auto& a : @ARG@ ) + a->Describe(&d); BroString* s = new BroString(1, d.TakeBytes(), d.Len()); s->SetUseFreeToDelete(true);