Convert uses of loop_over_list to ranged-for loops

This commit is contained in:
Tim Wojtulewicz 2019-07-10 13:04:18 -07:00 committed by Jon Siwek
parent bf70dad395
commit e51f02737b
24 changed files with 292 additions and 345 deletions

View file

@ -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 // rather than just taking over 'a' for ourselves, so that
// the necessary checking gets done. // the necessary checking gets done.
loop_over_list(*a, i) for ( const auto& attr : *a )
AddAttr((*a)[i]); AddAttr(attr);
delete a; delete a;
} }
Attributes::~Attributes() Attributes::~Attributes()
{ {
loop_over_list(*attrs, i) for ( const auto& attr : *attrs )
Unref((*attrs)[i]); Unref(attr);
delete attrs; delete attrs;
@ -188,8 +188,8 @@ void Attributes::AddAttr(Attr* attr)
void Attributes::AddAttrs(Attributes* a) void Attributes::AddAttrs(Attributes* a)
{ {
attr_list* as = a->Attrs(); attr_list* as = a->Attrs();
loop_over_list(*as, i) for ( const auto& attr : *as )
AddAttr((*as)[i]); AddAttr(attr);
Unref(a); Unref(a);
} }
@ -199,9 +199,8 @@ Attr* Attributes::FindAttr(attr_tag t) const
if ( ! attrs ) if ( ! attrs )
return 0; return 0;
loop_over_list(*attrs, i) for ( const auto& a : *attrs )
{ {
Attr* a = (*attrs)[i];
if ( a->Tag() == t ) if ( a->Tag() == t )
return a; return a;
} }
@ -391,9 +390,8 @@ void Attributes::CheckAttr(Attr* a)
int num_expires = 0; int num_expires = 0;
if ( attrs ) if ( attrs )
{ {
loop_over_list(*attrs, i) for ( const auto& a : *attrs )
{ {
Attr* a = (*attrs)[i];
if ( a->Tag() == ATTR_EXPIRE_READ || if ( a->Tag() == ATTR_EXPIRE_READ ||
a->Tag() == ATTR_EXPIRE_WRITE || a->Tag() == ATTR_EXPIRE_WRITE ||
a->Tag() == ATTR_EXPIRE_CREATE ) a->Tag() == ATTR_EXPIRE_CREATE )
@ -505,9 +503,8 @@ bool Attributes::operator==(const Attributes& other) const
if ( ! other.attrs ) if ( ! other.attrs )
return false; return false;
loop_over_list(*attrs, i) for ( const auto& a : *attrs )
{ {
Attr* a = (*attrs)[i];
Attr* o = other.FindAttr(a->Tag()); Attr* o = other.FindAttr(a->Tag());
if ( ! o ) if ( ! o )
@ -517,9 +514,8 @@ bool Attributes::operator==(const Attributes& other) const
return false; return false;
} }
loop_over_list(*other.attrs, j) for ( const auto& o : *other.attrs )
{ {
Attr* o = (*other.attrs)[j];
Attr* a = FindAttr(o->Tag()); Attr* a = FindAttr(o->Tag());
if ( ! a ) if ( ! a )

View file

@ -675,10 +675,10 @@ ListVal* CompositeHash::RecoverVals(const HashKey* k) const
const char* kp = (const char*) k->Key(); const char* kp = (const char*) k->Key();
const char* const k_end = kp + k->Size(); const char* const k_end = kp + k->Size();
loop_over_list(*tl, i) for ( const auto& type : *tl )
{ {
Val* v = nullptr; Val* v = nullptr;
kp = RecoverOneVal(k, kp, k_end, (*tl)[i], v, false); kp = RecoverOneVal(k, kp, k_end, type, v, false);
ASSERT(v); ASSERT(v);
l->Append(v); l->Append(v);
} }

View file

@ -481,8 +481,8 @@ void Connection::ConnectionEvent(EventHandlerPtr f, analyzer::Analyzer* a, val_l
{ {
// This may actually happen if there is no local handler // This may actually happen if there is no local handler
// and a previously existing remote handler went away. // and a previously existing remote handler went away.
loop_over_list(vl, i) for ( const auto& v : vl)
Unref(vl[i]); Unref(v);
return; return;
} }
@ -540,11 +540,10 @@ void Connection::CancelTimers()
// traversing. Thus, we first make a copy of the list which we then // traversing. Thus, we first make a copy of the list which we then
// iterate through. // iterate through.
timer_list tmp(timers.length()); timer_list tmp(timers.length());
loop_over_list(timers, j) std::copy(timers.begin(), timers.end(), std::back_inserter(tmp));
tmp.append(timers[j]);
loop_over_list(tmp, i) for ( const auto& timer : tmp )
GetTimerMgr()->Cancel(tmp[i]); GetTimerMgr()->Cancel(timer);
timers_canceled = 1; timers_canceled = 1;
timers.clear(); timers.clear();

View file

@ -100,9 +100,8 @@ void Dictionary::DeInit()
if ( tbl[i] ) if ( tbl[i] )
{ {
PList<DictEntry>* chain = tbl[i]; PList<DictEntry>* chain = tbl[i];
loop_over_list(*chain, j) for ( const auto& e : *chain )
{ {
DictEntry* e = (*chain)[j];
if ( delete_func ) if ( delete_func )
delete_func(e->value); delete_func(e->value);
delete e; delete e;
@ -120,9 +119,8 @@ void Dictionary::DeInit()
if ( tbl2[i] ) if ( tbl2[i] )
{ {
PList<DictEntry>* chain = tbl2[i]; PList<DictEntry>* chain = tbl2[i];
loop_over_list(*chain, j) for ( const auto& e : *chain )
{ {
DictEntry* e = (*chain)[j];
if ( delete_func ) if ( delete_func )
delete_func(e->value); delete_func(e->value);
delete e; delete e;
@ -249,10 +247,8 @@ void* Dictionary::DoRemove(DictEntry* entry, hash_t h,
order->remove(entry); order->remove(entry);
// Adjust existing cookies. // Adjust existing cookies.
loop_over_list(cookies, i) for ( const auto& c : cookies )
{ {
IterCookie* c = cookies[i];
// Is the affected bucket the current one? // Is the affected bucket the current one?
if ( (unsigned int) c->bucket == h ) 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 // For ongoing iterations: If we already passed the bucket where this
// entry was put, add it to the cookie's list of inserted entries. // 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 ) if ( h < (unsigned int) c->bucket )
c->inserted.append(new_entry); c->inserted.append(new_entry);
} }
@ -606,8 +601,8 @@ unsigned int Dictionary::MemoryAllocation() const
if ( tbl[i] ) if ( tbl[i] )
{ {
PList<DictEntry>* chain = tbl[i]; PList<DictEntry>* chain = tbl[i];
loop_over_list(*chain, j) for ( const auto& c : *chain )
size += padded_sizeof(DictEntry) + pad_size((*chain)[j]->len); size += padded_sizeof(DictEntry) + pad_size(c->len);
size += chain->MemoryAllocation(); size += chain->MemoryAllocation();
} }
@ -622,8 +617,8 @@ unsigned int Dictionary::MemoryAllocation() const
if ( tbl2[i] ) if ( tbl2[i] )
{ {
PList<DictEntry>* chain = tbl2[i]; PList<DictEntry>* chain = tbl2[i];
loop_over_list(*chain, j) for ( const auto& c : *chain )
size += padded_sizeof(DictEntry) + pad_size((*chain)[j]->len); size += padded_sizeof(DictEntry) + pad_size(c->len);
size += chain->MemoryAllocation(); size += chain->MemoryAllocation();
} }

View file

@ -86,8 +86,8 @@ public:
QueueEvent(new Event(h, std::move(vl), src, aid, mgr, obj)); QueueEvent(new Event(h, std::move(vl), src, aid, mgr, obj));
else else
{ {
loop_over_list(vl, i) for ( const auto& v : vl )
Unref(vl[i]); Unref(v);
} }
} }

View file

@ -117,8 +117,8 @@ void EventHandler::Call(val_list* vl, bool no_remote)
Unref(local->Call(vl)); Unref(local->Call(vl));
else else
{ {
loop_over_list(*vl, i) for ( auto v : *vl )
Unref((*vl)[i]); Unref(v);
} }
} }

View file

@ -2113,8 +2113,7 @@ bool AssignExpr::TypeCheck(attr_list* attrs)
if ( attrs ) if ( attrs )
{ {
attr_copy = new attr_list(attrs->length()); attr_copy = new attr_list(attrs->length());
loop_over_list(*attrs, i) std::copy(attrs->begin(), attrs->end(), std::back_inserter(*attr_copy));
attr_copy->append((*attrs)[i]);
} }
bool empty_list_assignment = (op2->AsListExpr()->Exprs().length() == 0); 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(); attr_list* a = sce->Attrs()->Attrs();
attrs = new attr_list(a->length()); attrs = new attr_list(a->length());
loop_over_list(*a, i) std::copy(a->begin(), a->end(), std::back_inserter(*attrs));
attrs->append((*a)[i]);
} }
int errors_before = reporter->Errors(); int errors_before = reporter->Errors();
@ -3029,10 +3027,8 @@ RecordConstructorExpr::RecordConstructorExpr(ListExpr* constructor_list)
const expr_list& exprs = constructor_list->Exprs(); const expr_list& exprs = constructor_list->Exprs();
type_decl_list* record_types = new type_decl_list(exprs.length()); 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 ) if ( e->Tag() != EXPR_FIELD_ASSIGN )
{ {
Error("bad type in record constructor", e); Error("bad type in record constructor", e);
@ -3139,12 +3135,12 @@ TableConstructorExpr::TableConstructorExpr(ListExpr* constructor_list,
const expr_list& cle = constructor_list->Exprs(); const expr_list& cle = constructor_list->Exprs();
// check and promote all index expressions in ctor list // 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; continue;
Expr* idx_expr = cle[i]->AsAssignExpr()->Op1(); Expr* idx_expr = expr->AsAssignExpr()->Op1();
if ( idx_expr->Tag() != EXPR_LIST ) if ( idx_expr->Tag() != EXPR_LIST )
continue; continue;
@ -3178,8 +3174,8 @@ Val* TableConstructorExpr::Eval(Frame* f) const
Val* aggr = new TableVal(Type()->AsTableType(), attrs); Val* aggr = new TableVal(Type()->AsTableType(), attrs);
const expr_list& exprs = op->AsListExpr()->Exprs(); const expr_list& exprs = op->AsListExpr()->Exprs();
loop_over_list(exprs, i) for ( const auto& expr : exprs )
exprs[i]->EvalIntoAggregate(type, aggr, f); expr->EvalIntoAggregate(type, aggr, f);
return aggr; return aggr;
} }
@ -3193,8 +3189,8 @@ Val* TableConstructorExpr::InitVal(const BroType* t, Val* aggr) const
TableVal* tval = aggr ? aggr->AsTableVal() : new TableVal(tt, attrs); TableVal* tval = aggr ? aggr->AsTableVal() : new TableVal(tt, attrs);
const expr_list& exprs = op->AsListExpr()->Exprs(); const expr_list& exprs = op->AsListExpr()->Exprs();
loop_over_list(exprs, i) for ( const auto& expr : exprs )
exprs[i]->EvalIntoAggregate(t, tval, 0); expr->EvalIntoAggregate(t, tval, 0);
return tval; return tval;
} }
@ -3282,9 +3278,9 @@ Val* SetConstructorExpr::Eval(Frame* f) const
TableVal* aggr = new TableVal(type->AsTableType(), attrs); TableVal* aggr = new TableVal(type->AsTableType(), attrs);
const expr_list& exprs = op->AsListExpr()->Exprs(); 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); aggr->Assign(element, 0);
Unref(element); Unref(element);
} }
@ -3302,9 +3298,8 @@ Val* SetConstructorExpr::InitVal(const BroType* t, Val* aggr) const
TableVal* tval = aggr ? aggr->AsTableVal() : new TableVal(tt, attrs); TableVal* tval = aggr ? aggr->AsTableVal() : new TableVal(tt, attrs);
const expr_list& exprs = op->AsListExpr()->Exprs(); 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); Val* element = check_and_promote(e->Eval(0), index_type, 1);
if ( ! element || ! tval->Assign(element, 0) ) if ( ! element || ! tval->Assign(element, 0) )
@ -4417,8 +4412,8 @@ ListExpr::ListExpr(Expr* e) : Expr(EXPR_LIST)
ListExpr::~ListExpr() ListExpr::~ListExpr()
{ {
loop_over_list(exprs, i) for ( const auto& expr: exprs )
Unref(exprs[i]); Unref(expr);
} }
void ListExpr::Append(Expr* e) void ListExpr::Append(Expr* e)
@ -4429,8 +4424,8 @@ void ListExpr::Append(Expr* e)
int ListExpr::IsPure() const int ListExpr::IsPure() const
{ {
loop_over_list(exprs, i) for ( const auto& expr : exprs )
if ( ! exprs[i]->IsPure() ) if ( ! expr->IsPure() )
return 0; return 0;
return 1; return 1;
@ -4438,8 +4433,8 @@ int ListExpr::IsPure() const
int ListExpr::AllConst() const int ListExpr::AllConst() const
{ {
loop_over_list(exprs, i) for ( const auto& expr : exprs )
if ( ! exprs[i]->IsConst() ) if ( ! expr->IsConst() )
return 0; return 0;
return 1; return 1;
@ -4449,9 +4444,9 @@ Val* ListExpr::Eval(Frame* f) const
{ {
ListVal* v = new ListVal(TYPE_ANY); 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 ) if ( ! ev )
{ {
RuntimeError("uninitialized list value"); RuntimeError("uninitialized list value");
@ -4476,12 +4471,12 @@ BroType* ListExpr::InitType() const
if ( exprs[0]->IsRecordElement(0) ) if ( exprs[0]->IsRecordElement(0) )
{ {
type_decl_list* types = new type_decl_list(exprs.length()); 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); 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 td;
delete types; delete types;
return 0; return 0;
@ -4497,9 +4492,8 @@ BroType* ListExpr::InitType() const
else else
{ {
TypeList* tl = new TypeList(); TypeList* tl = new TypeList();
loop_over_list(exprs, i) for ( const auto& e : exprs )
{ {
Expr* e = exprs[i];
BroType* ti = e->Type(); BroType* ti = e->Type();
// Collapse any embedded sets or lists. // 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 // know how to add themselves to a table or record. Another
// possibility is an expression that evaluates itself to a // possibility is an expression that evaluates itself to a
// table, which we can then add to the aggregate. // 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->Tag() == EXPR_ASSIGN || e->Tag() == EXPR_FIELD_ASSIGN )
{ {
if ( ! e->InitVal(t, aggr) ) 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 TableType* tt = tv->Type()->AsTableType();
const TypeList* it = tt->Indices(); const TypeList* it = tt->Indices();
loop_over_list(exprs, i) for ( const auto& expr : exprs )
{ {
Val* element; Val* element;
if ( exprs[i]->Type()->IsSet() ) if ( expr->Type()->IsSet() )
// A set to flatten. // A set to flatten.
element = exprs[i]->Eval(0); element = expr->Eval(0);
else if ( exprs[i]->Type()->Tag() == TYPE_LIST ) else if ( expr->Type()->Tag() == TYPE_LIST )
element = exprs[i]->InitVal(it, 0); element = expr->InitVal(it, 0);
else else
element = exprs[i]->InitVal((*it->Types())[0], 0); element = expr->InitVal((*it->Types())[0], 0);
if ( ! element ) if ( ! element )
return 0; return 0;
@ -4703,7 +4695,7 @@ Val* ListExpr::AddSetInit(const BroType* t, Val* aggr) const
continue; continue;
} }
if ( exprs[i]->Type()->Tag() == TYPE_LIST ) if ( expr->Type()->Tag() == TYPE_LIST )
element = check_and_promote(element, it, 1); element = check_and_promote(element, it, 1);
else else
element = check_and_promote(element, (*it->Types())[0], 1); element = check_and_promote(element, (*it->Types())[0], 1);
@ -4739,8 +4731,8 @@ void ListExpr::ExprDescribe(ODesc* d) const
Expr* ListExpr::MakeLvalue() Expr* ListExpr::MakeLvalue()
{ {
loop_over_list(exprs, i) for ( const auto & expr : exprs )
if ( exprs[i]->Tag() != EXPR_NAME ) if ( expr->Tag() != EXPR_NAME )
ExprError("can only assign to list of identifiers"); ExprError("can only assign to list of identifiers");
return new RefExpr(this); return new RefExpr(this);
@ -4764,9 +4756,9 @@ TraversalCode ListExpr::Traverse(TraversalCallback* cb) const
TraversalCode tc = cb->PreExpr(this); TraversalCode tc = cb->PreExpr(this);
HANDLE_TC_EXPR_PRE(tc); 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); 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) // 2) a string indicating the field name, then (as the next element)
// the value to use for that field. // 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 ) 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)) ) same_type(lhs->FieldType(field), t->FieldType(j)) )
{ {
FieldExpr* fe_lhs = new FieldExpr(record, field_name); 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)); 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(); rf->Ref();
const char* field_name = ""; // rf->FieldName(); 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()); def_elements.insert(def_attr->AttrExpr());
} }
loop_over_list(def_elements, i) for ( const auto& elem : def_elements )
el.append(def_elements[i]->Ref()); el.append(elem->Ref());
} }
TypeList* tl = new TypeList(); TypeList* tl = new TypeList();
@ -5115,18 +5107,22 @@ val_list* eval_list(Frame* f, const ListExpr* l)
const expr_list& e = l->Exprs(); const expr_list& e = l->Exprs();
val_list* v = new val_list(e.length()); 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 ) if ( ! ev )
{
success = false;
break; break;
}
v->append(ev); v->append(ev);
} }
if ( i < e.length() ) if ( ! success )
{ // Failure. { // Failure.
loop_over_list(*v, j) for ( const auto& val : *v )
Unref((*v)[j]); Unref(val);
delete v; delete v;
return 0; return 0;
} }

View file

@ -73,11 +73,11 @@ std::string render_call_stack()
if ( ci.args ) if ( ci.args )
{ {
loop_over_list(*ci.args, i) for ( const auto& arg : *ci.args )
{ {
ODesc d; ODesc d;
d.SetShort(); d.SetShort();
(*ci.args)[i]->Describe(&d); arg->Describe(&d);
if ( ! arg_desc.empty() ) if ( ! arg_desc.empty() )
arg_desc += ", "; arg_desc += ", ";
@ -237,8 +237,8 @@ std::pair<bool, Val*> Func::HandlePluginResult(std::pair<bool, Val*> plugin_resu
} }
} }
loop_over_list(*args, i) for ( const auto& arg : *args )
Unref((*args)[i]); Unref(arg);
return plugin_result; return plugin_result;
} }
@ -300,8 +300,8 @@ Val* BroFunc::Call(val_list* args, Frame* parent) const
{ {
// Can only happen for events and hooks. // Can only happen for events and hooks.
assert(Flavor() == FUNC_FLAVOR_EVENT || Flavor() == FUNC_FLAVOR_HOOK); assert(Flavor() == FUNC_FLAVOR_EVENT || Flavor() == FUNC_FLAVOR_HOOK);
loop_over_list(*args, i) for ( const auto& arg : *args )
Unref((*args)[i]); Unref(arg);
return Flavor() == FUNC_FLAVOR_HOOK ? val_mgr->GetTrue() : 0; 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; stmt_flow_type flow = FLOW_NEXT;
Val* result = 0; Val* result = 0;
for ( size_t i = 0; i < bodies.size(); ++i ) for ( const auto& body : bodies )
{ {
if ( sample_logger ) if ( sample_logger )
sample_logger->LocationSeen( sample_logger->LocationSeen(
bodies[i].stmts->GetLocationInfo()); body.stmts->GetLocationInfo());
Unref(result); Unref(result);
@ -356,7 +356,7 @@ Val* BroFunc::Call(val_list* args, Frame* parent) const
try try
{ {
result = bodies[i].stmts->Exec(f, flow); result = body.stmts->Exec(f, flow);
} }
catch ( InterpreterException& e ) 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 // We have an extra Ref for each argument (so that they don't get
// deleted between bodies), release that. // deleted between bodies), release that.
loop_over_list(*args, k) for ( const auto& arg : *args )
Unref((*args)[k]); Unref(arg);
if ( Flavor() == FUNC_FLAVOR_HOOK ) 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. // For functions, we replace the old body with the new one.
assert(bodies.size() <= 1); assert(bodies.size() <= 1);
for ( unsigned int i = 0; i < bodies.size(); ++i ) for ( const auto& body : bodies )
Unref(bodies[i].stmts); Unref(body.stmts);
bodies.clear(); bodies.clear();
} }
@ -540,8 +540,8 @@ Val* BuiltinFunc::Call(val_list* args, Frame* parent) const
Val* result = func(parent, args); Val* result = func(parent, args);
call_stack.pop_back(); call_stack.pop_back();
loop_over_list(*args, i) for ( const auto& arg : *args )
Unref((*args)[i]); Unref(arg);
// Don't Unref() args, that's the caller's responsibility. // Don't Unref() args, that's the caller's responsibility.
if ( result && g_trace_state.DoTrace() ) if ( result && g_trace_state.DoTrace() )

View file

@ -233,9 +233,8 @@ void MD5Val::digest(val_list& vlist, u_char result[MD5_DIGEST_LENGTH])
{ {
EVP_MD_CTX* h = hash_init(Hash_MD5); 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 ) if ( v->Type()->Tag() == TYPE_STRING )
{ {
const BroString* str = v->AsString(); 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); 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 ) if ( v->Type()->Tag() == TYPE_STRING )
{ {
const BroString* str = v->AsString(); 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); 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 ) if ( v->Type()->Tag() == TYPE_STRING )
{ {
const BroString* str = v->AsString(); const BroString* str = v->AsString();

View file

@ -502,10 +502,7 @@ void Reporter::DoLog(const char* prefix, EventHandlerPtr event, FILE* out,
vl.append(conn->BuildConnVal()); vl.append(conn->BuildConnVal());
if ( addl ) if ( addl )
{ std::copy(addl->begin(), addl->end(), std::back_inserter(vl));
loop_over_list(*addl, i)
vl.append((*addl)[i]);
}
if ( conn ) if ( conn )
conn->ConnectionEventFast(event, 0, std::move(vl)); conn->ConnectionEventFast(event, 0, std::move(vl));
@ -516,8 +513,8 @@ void Reporter::DoLog(const char* prefix, EventHandlerPtr event, FILE* out,
{ {
if ( addl ) if ( addl )
{ {
loop_over_list(*addl, i) for ( const auto& av : *addl )
Unref((*addl)[i]); Unref(av);
} }
} }

View file

@ -13,25 +13,25 @@ Rule::~Rule()
{ {
delete [] id; delete [] id;
loop_over_list(patterns, i) for ( const auto& p : patterns )
{ {
delete [] patterns[i]->pattern; delete [] p->pattern;
delete patterns[i]; delete p;
} }
loop_over_list(hdr_tests, j) for ( const auto& test : hdr_tests )
delete hdr_tests[j]; delete test;
loop_over_list(conditions, k) for ( const auto& cond : conditions )
delete conditions[k]; delete cond;
loop_over_list(actions, l) for ( const auto& action : actions )
delete actions[l]; delete action;
loop_over_list(preconds, m) for ( const auto& prec : preconds )
{ {
delete [] preconds[m]->id; delete [] prec->id;
delete preconds[m]; delete prec;
} }
} }
@ -49,21 +49,20 @@ void Rule::PrintDebug()
{ {
fprintf(stderr, "Rule %s (%d) %s\n", id, idx, active ? "[active]" : "[disabled]"); 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", fprintf(stderr, " %-8s |%s| (%d) \n",
TypeToString(patterns[i]->type), patterns[i]->pattern, TypeToString(p->type), p->pattern, p->id);
patterns[i]->id);
} }
loop_over_list(hdr_tests, j) for ( const auto& h : hdr_tests )
hdr_tests[j]->PrintDebug(); h->PrintDebug();
loop_over_list(conditions, k) for ( const auto& c : conditions )
conditions[k]->PrintDebug(); c->PrintDebug();
loop_over_list(actions, l) for ( const auto& a : actions )
actions[l]->PrintDebug(); a->PrintDebug();
fputs("\n", stderr); fputs("\n", stderr);
} }

View file

@ -83,21 +83,20 @@ RuleHdrTest::RuleHdrTest(RuleHdrTest& h)
comp = h.comp; comp = h.comp;
vals = new maskedvalue_list; vals = new maskedvalue_list;
loop_over_list(*h.vals, i) for ( const auto& val : *h.vals )
vals->append(new MaskedValue(*(*h.vals)[i])); vals->append(new MaskedValue(*val));
prefix_vals = h.prefix_vals; prefix_vals = h.prefix_vals;
for ( int j = 0; j < Rule::TYPES; ++j ) 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; PatternSet* copied_set = new PatternSet;
copied_set->re = 0; copied_set->re = 0;
copied_set->ids = orig_set->ids; copied_set->ids = orig_set->ids;
loop_over_list(orig_set->patterns, l) for ( const auto& pattern : orig_set->patterns )
copied_set->patterns.append(copy_string(orig_set->patterns[l])); copied_set->patterns.append(copy_string(pattern));
delete copied_set; delete copied_set;
// TODO: Why do we create copied_set only to then // TODO: Why do we create copied_set only to then
// never use it? // never use it?
@ -115,14 +114,14 @@ RuleHdrTest::RuleHdrTest(RuleHdrTest& h)
RuleHdrTest::~RuleHdrTest() RuleHdrTest::~RuleHdrTest()
{ {
loop_over_list(*vals, i) for ( auto val : *vals )
delete (*vals)[i]; delete val;
delete vals; delete vals;
for ( int i = 0; i < Rule::TYPES; ++i ) for ( int i = 0; i < Rule::TYPES; ++i )
{ {
loop_over_list(psets[i], j) for ( auto pset : psets[i] )
delete psets[i][j]->re; delete pset->re;
} }
delete ruleset; delete ruleset;
@ -154,12 +153,12 @@ void RuleHdrTest::PrintDebug()
fprintf(stderr, " RuleHdrTest %s[%d:%d] %s", fprintf(stderr, " RuleHdrTest %s[%d:%d] %s",
str_prot[prot], offset, size, str_comp[comp]); str_prot[prot], offset, size, str_comp[comp]);
loop_over_list(*vals, i) for ( const auto& val : *vals )
fprintf(stderr, " 0x%08x/0x%08x", 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 ) for ( const auto& prefix : prefix_vals )
fprintf(stderr, " %s", prefix_vals[i].AsString().c_str()); fprintf(stderr, " %s", prefix.AsString().c_str());
fprintf(stderr, "\n"); fprintf(stderr, "\n");
} }
@ -181,22 +180,22 @@ RuleEndpointState::RuleEndpointState(analyzer::Analyzer* arg_analyzer, bool arg_
RuleEndpointState::~RuleEndpointState() RuleEndpointState::~RuleEndpointState()
{ {
loop_over_list(matchers, i) for ( auto matcher : matchers )
{ {
delete matchers[i]->state; delete matcher->state;
delete matchers[i]; delete matcher;
} }
loop_over_list(matched_text, j) for ( auto text : matched_text )
delete matched_text[j]; delete text;
} }
RuleFileMagicState::~RuleFileMagicState() RuleFileMagicState::~RuleFileMagicState()
{ {
loop_over_list(matchers, i) for ( auto matcher : matchers )
{ {
delete matchers[i]->state; delete matcher->state;
delete matchers[i]; delete matcher;
} }
} }
@ -214,8 +213,8 @@ RuleMatcher::~RuleMatcher()
#endif #endif
Delete(root); Delete(root);
loop_over_list(rules, i) for ( auto rule : rules )
delete rules[i]; delete rule;
} }
void RuleMatcher::Delete(RuleHdrTest* node) void RuleMatcher::Delete(RuleHdrTest* node)
@ -280,13 +279,13 @@ void RuleMatcher::AddRule(Rule* rule)
void RuleMatcher::BuildRulesTree() void RuleMatcher::BuildRulesTree()
{ {
loop_over_list(rules, r) for ( const auto& rule : rules )
{ {
if ( ! rules[r]->Active() ) if ( ! rule->Active() )
continue; continue;
rules[r]->SortHdrTests(); rule->SortHdrTests();
InsertRuleIntoTree(rules[r], 0, root, 0); InsertRuleIntoTree(rule, 0, root, 0);
} }
} }
@ -294,10 +293,8 @@ void RuleMatcher::InsertRuleIntoTree(Rule* r, int testnr,
RuleHdrTest* dest, int level) RuleHdrTest* dest, int level)
{ {
// Initiliaze the preconditions // 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); Rule* pc_rule = rules_by_id.Lookup(pc->id);
if ( ! pc_rule ) 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 each type, get all patterns on this node.
for ( Rule* r = hdr_test->pattern_rules; r; r = r->next ) 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); exprs[p->type].append(p->pattern);
ids[p->type].push_back(p->id); ids[p->type].push_back(p->id);
} }
@ -457,9 +453,10 @@ static inline uint32 getval(const u_char* data, int size)
template <typename FuncT> template <typename FuncT>
static inline bool match_or(const maskedvalue_list& mvals, uint32 v, FuncT comp) 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 true;
} }
return false; return false;
@ -485,9 +482,10 @@ template <typename FuncT>
static inline bool match_not_and(const maskedvalue_list& mvals, uint32 v, static inline bool match_not_and(const maskedvalue_list& mvals, uint32 v,
FuncT comp) 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 false;
} }
return true; return true;
@ -582,9 +580,8 @@ RuleFileMagicState* RuleMatcher::InitFileMagic() const
{ {
RuleFileMagicState* state = new RuleFileMagicState(); 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); assert(set->re);
RuleFileMagicState::Matcher* m = new RuleFileMagicState::Matcher; RuleFileMagicState::Matcher* m = new RuleFileMagicState::Matcher;
m->state = new RE_Match_State(set->re); 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); DBG_LOG(DBG_RULES, "Checking rule: %s", r->id);
// Check whether all patterns of the rule have matched. // 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; return false;
// See if depth is satisfied. // See if depth is satisfied.
if ( matchpos > r->patterns[j]->offset + r->patterns[j]->depth ) if ( matchpos > pattern->offset + pattern->depth )
return false; return false;
// FIXME: How to check for offset ??? ### // FIXME: How to check for offset ??? ###
@ -645,10 +642,8 @@ RuleMatcher::MIME_Matches* RuleMatcher::Match(RuleFileMagicState* state,
bool newmatch = false; 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) ) if ( m->state->Match(data, len, true, false, true) )
newmatch = true; newmatch = true;
} }
@ -660,9 +655,8 @@ RuleMatcher::MIME_Matches* RuleMatcher::Match(RuleFileMagicState* state,
AcceptingMatchSet accepted_matches; 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(); const AcceptingMatchSet& ams = m->state->AcceptedMatches();
accepted_matches.insert(ams.begin(), ams.end()); accepted_matches.insert(ams.begin(), ams.end());
} }
@ -687,10 +681,10 @@ RuleMatcher::MIME_Matches* RuleMatcher::Match(RuleFileMagicState* state,
{ {
Rule* r = *it; Rule* r = *it;
loop_over_list(r->actions, rai) for ( const auto& action : r->actions )
{ {
const RuleActionMIME* ram = const RuleActionMIME* ram =
dynamic_cast<const RuleActionMIME*>(r->actions[rai]); dynamic_cast<const RuleActionMIME*>(action);
if ( ! ram ) if ( ! ram )
continue; continue;
@ -739,11 +733,8 @@ RuleEndpointState* RuleMatcher::InitEndpoint(analyzer::Analyzer* analyzer,
{ {
for ( int i = 0; i < Rule::TYPES; ++i ) 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); assert(set->re);
RuleEndpointState::Matcher* m = RuleEndpointState::Matcher* m =
@ -860,9 +851,8 @@ void RuleMatcher::Match(RuleEndpointState* state, Rule::PatternType type,
} }
// Feed data into all relevant matchers. // 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 && if ( m->type == type &&
m->state->Match((const u_char*) data, data_len, m->state->Match((const u_char*) data, data_len,
bol, eol, clear) ) bol, eol, clear) )
@ -877,9 +867,8 @@ void RuleMatcher::Match(RuleEndpointState* state, Rule::PatternType type,
AcceptingMatchSet accepted_matches; 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(); const AcceptingMatchSet& ams = m->state->AcceptedMatches();
accepted_matches.insert(ams.begin(), ams.end()); 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); 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); DBG_LOG(DBG_RULES, "Checking for accepted rule on HdrTest %d", h->id);
// Skip if rule does not belong to this node. // 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) 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 ) for ( Rule* r = hdr_test->pure_rules; r; r = r->next )
ExecRulePurely(r, 0, state, eos); 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()); DBG_LOG(DBG_RULES, "Evaluating conditions for rule %s", r->ID());
// Check for other rules which have to match first. // 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; RuleEndpointState* pc_state = state;
if ( pc->opposite_dir ) if ( pc->opposite_dir )
@ -1034,8 +1018,8 @@ bool RuleMatcher::EvalRuleConditions(Rule* r, RuleEndpointState* state,
} }
} }
loop_over_list(r->conditions, l) for ( const auto& cond : r->conditions )
if ( ! r->conditions[l]->DoMatch(r, state, data, len) ) if ( ! cond->DoMatch(r, state, data, len) )
return false; return false;
DBG_LOG(DBG_RULES, "Conditions met: MATCH! %s", r->ID()); 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()); state->matched_rules.push_back(r->Index());
loop_over_list(r->actions, i) for ( const auto& action : r->actions )
r->actions[i]->DoAction(r, state, data, len); action->DoAction(r, state, data, len);
// This rule may trigger some other rules; check them. // 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); ExecRule(dep, state, eos);
if ( state->opposite ) if ( state->opposite )
ExecRule(dep, state->opposite, eos); 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()) ) if ( is_member_of(state->matched_rules, rule->Index()) )
return; 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? // Is it on this HdrTest at all?
if ( ! h->ruleset->Contains(rule->Index()) ) if ( ! h->ruleset->Contains(rule->Index()) )
continue; continue;
@ -1104,20 +1085,20 @@ void RuleMatcher::ClearEndpointState(RuleEndpointState* state)
state->payload_size = -1; state->payload_size = -1;
loop_over_list(state->matchers, j) for ( const auto& matcher : state->matchers )
state->matchers[j]->state->Clear(); matcher->state->Clear();
} }
void RuleMatcher::ClearFileMagicState(RuleFileMagicState* state) const void RuleMatcher::ClearFileMagicState(RuleFileMagicState* state) const
{ {
loop_over_list(state->matchers, j) for ( const auto& matcher : state->matchers )
state->matchers[j]->state->Clear(); matcher->state->Clear();
} }
void RuleMatcher::PrintDebug() void RuleMatcher::PrintDebug()
{ {
loop_over_list(rules, i) for ( const auto& rule : rules )
rules[i]->PrintDebug(); rule->PrintDebug();
fprintf(stderr, "\n---------------\n"); fprintf(stderr, "\n---------------\n");
@ -1187,9 +1168,8 @@ void RuleMatcher::GetStats(Stats* stats, RuleHdrTest* hdr_test)
for ( int i = 0; i < Rule::TYPES; ++i ) 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); assert(set->re);
++stats->matchers; ++stats->matchers;
@ -1239,7 +1219,7 @@ void RuleMatcher::DumpStateStats(BroFile* f, RuleHdrTest* hdr_test)
set->re->DFA()->NumStates(), set->re->DFA()->NumStates(),
Rule::TypeToString((Rule::PatternType)i), j)); Rule::TypeToString((Rule::PatternType)i), j));
for ( auto id : set->ids ) for ( const auto& id : set->ids )
{ {
Rule* r = Rule::rule_table[id - 1]; Rule* r = Rule::rule_table[id - 1];
f->Write(fmt("%s ", r->ID())); 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(); ListVal* lv = v->AsTableVal()->ConvertToPureList();
val_list* vals = lv->Vals(); val_list* vals = lv->Vals();
loop_over_list(*vals, i ) for ( const auto& val : *vals )
if ( ! val_to_maskedval((*vals)[i], append_to, prefix_vector) ) if ( ! val_to_maskedval(val, append_to, prefix_vector) )
{ {
Unref(lv); Unref(lv);
return; return;

View file

@ -47,8 +47,8 @@ Scope::~Scope()
if ( attrs ) if ( attrs )
{ {
loop_over_list(*attrs, i) for ( const auto& attr : *attrs )
Unref((*attrs)[i]); Unref(attr);
delete attrs; delete attrs;
} }

View file

@ -123,9 +123,9 @@ ExprListStmt::ExprListStmt(BroStmtTag t, ListExpr* arg_l)
l = arg_l; l = arg_l;
const expr_list& e = l->Exprs(); 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 ) if ( ! t || t->Tag() == TYPE_VOID )
Error("value of type void illegal"); Error("value of type void illegal");
} }
@ -172,9 +172,9 @@ TraversalCode ExprListStmt::Traverse(TraversalCallback* cb) const
HANDLE_TC_STMT_PRE(tc); HANDLE_TC_STMT_PRE(tc);
const expr_list& e = l->Exprs(); 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); HANDLE_TC_STMT_PRE(tc);
} }
@ -430,8 +430,8 @@ Case::~Case()
Unref(expr_cases); Unref(expr_cases);
Unref(s); Unref(s);
loop_over_list((*type_cases), i) for ( const auto& id : *type_cases )
Unref((*type_cases)[i]); Unref(id);
delete type_cases; delete type_cases;
} }
@ -631,9 +631,9 @@ SwitchStmt::SwitchStmt(Expr* index, case_list* arg_cases) :
{ {
have_types = true; 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) ) if ( ! can_cast_value_to_type(e->Type(), ct) )
{ {
@ -641,7 +641,7 @@ SwitchStmt::SwitchStmt(Expr* index, case_list* arg_cases) :
continue; continue;
} }
if ( ! AddCaseLabelTypeMapping((*tl)[j], i) ) if ( ! AddCaseLabelTypeMapping(t, i) )
{ {
c->Error("duplicate case label"); c->Error("duplicate case label");
continue; continue;
@ -665,8 +665,8 @@ SwitchStmt::SwitchStmt(Expr* index, case_list* arg_cases) :
SwitchStmt::~SwitchStmt() SwitchStmt::~SwitchStmt()
{ {
loop_over_list(*cases, i) for ( const auto& c : *cases )
Unref((*cases)[i]); Unref(c);
delete cases; delete cases;
delete comp_hash; delete comp_hash;
@ -793,9 +793,8 @@ int SwitchStmt::IsPure() const
if ( ! e->IsPure() ) if ( ! e->IsPure() )
return 0; return 0;
loop_over_list(*cases, i) for ( const auto& c : *cases )
{ {
Case* c = (*cases)[i];
if ( ! c->ExprCases()->IsPure() || ! c->Body()->IsPure() ) if ( ! c->ExprCases()->IsPure() || ! c->Body()->IsPure() )
return 0; return 0;
} }
@ -812,8 +811,8 @@ void SwitchStmt::Describe(ODesc* d) const
d->PushIndent(); d->PushIndent();
d->AddCount(cases->length()); d->AddCount(cases->length());
loop_over_list(*cases, i) for ( const auto& c : *cases )
(*cases)[i]->Describe(d); c->Describe(d);
d->PopIndent(); d->PopIndent();
if ( ! d->IsBinary() ) if ( ! d->IsBinary() )
@ -830,9 +829,9 @@ TraversalCode SwitchStmt::Traverse(TraversalCallback* cb) const
tc = e->Traverse(cb); tc = e->Traverse(cb);
HANDLE_TC_STMT_PRE(tc); 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); HANDLE_TC_STMT_PRE(tc);
} }
@ -1132,8 +1131,8 @@ ForStmt::ForStmt(id_list* arg_loop_vars, Expr* loop_expr, ID* val_var)
ForStmt::~ForStmt() ForStmt::~ForStmt()
{ {
loop_over_list(*loop_vars, i) for ( const auto& var : *loop_vars )
Unref((*loop_vars)[i]); Unref(var);
delete loop_vars; delete loop_vars;
Unref(value_var); Unref(value_var);
@ -1275,9 +1274,9 @@ TraversalCode ForStmt::Traverse(TraversalCallback* cb) const
TraversalCode tc = cb->PreStmt(this); TraversalCode tc = cb->PreStmt(this);
HANDLE_TC_STMT_PRE(tc); 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); HANDLE_TC_STMT_PRE(tc);
} }
@ -1445,8 +1444,8 @@ StmtList::StmtList() : Stmt(STMT_LIST)
StmtList::~StmtList() StmtList::~StmtList()
{ {
loop_over_list(stmts, i) for ( const auto& stmt : stmts )
Unref(stmts[i]); Unref(stmt);
} }
Val* StmtList::Exec(Frame* f, stmt_flow_type& flow) const 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(); RegisterAccess();
flow = FLOW_NEXT; 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 { // ### 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 { // ### Abort or something
} }
@ -1477,8 +1476,8 @@ Val* StmtList::Exec(Frame* f, stmt_flow_type& flow) const
int StmtList::IsPure() const int StmtList::IsPure() const
{ {
loop_over_list(stmts, i) for ( const auto& stmt : stmts )
if ( ! stmts[i]->IsPure() ) if ( ! stmt->IsPure() )
return 0; return 0;
return 1; return 1;
} }
@ -1502,9 +1501,9 @@ void StmtList::Describe(ODesc* d) const
d->NL(); d->NL();
} }
loop_over_list(stmts, i) for ( const auto& stmt : stmts )
{ {
stmts[i]->Describe(d); stmt->Describe(d);
d->NL(); d->NL();
} }
@ -1518,9 +1517,9 @@ TraversalCode StmtList::Traverse(TraversalCallback* cb) const
TraversalCode tc = cb->PreStmt(this); TraversalCode tc = cb->PreStmt(this);
HANDLE_TC_STMT_PRE(tc); 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); HANDLE_TC_STMT_PRE(tc);
} }
@ -1533,21 +1532,21 @@ Val* EventBodyList::Exec(Frame* f, stmt_flow_type& flow) const
RegisterAccess(); RegisterAccess();
flow = FLOW_NEXT; 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 // Ignore the return value, since there shouldn't be
// any; and ignore the flow, since we still execute // any; and ignore the flow, since we still execute
// all of the event bodies even if one of them does // all of the event bodies even if one of them does
// a FLOW_RETURN. // a FLOW_RETURN.
if ( ! pre_execute_stmt(stmts[i], f) ) if ( ! pre_execute_stmt(stmt, f) )
{ // ### Abort or something { // ### 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 { // ### Abort or something
} }
} }
@ -1563,16 +1562,16 @@ void EventBodyList::Describe(ODesc* d) const
{ {
if ( d->IsReadable() && stmts.length() > 0 ) if ( d->IsReadable() && stmts.length() > 0 )
{ {
loop_over_list(stmts, i) for ( const auto& stmt : stmts )
{ {
if ( ! d->IsBinary() ) if ( ! d->IsBinary() )
{ {
d->Add("{"); d->Add("{");
d->PushIndent(); d->PushIndent();
stmts[i]->AccessStats(d); stmt->AccessStats(d);
} }
stmts[i]->Describe(d); stmt->Describe(d);
if ( ! d->IsBinary() ) if ( ! d->IsBinary() )
{ {
@ -1588,8 +1587,8 @@ void EventBodyList::Describe(ODesc* d) const
InitStmt::~InitStmt() InitStmt::~InitStmt()
{ {
loop_over_list(*inits, i) for ( const auto& init : *inits )
Unref((*inits)[i]); Unref(init);
delete inits; delete inits;
} }
@ -1599,9 +1598,8 @@ Val* InitStmt::Exec(Frame* f, stmt_flow_type& flow) const
RegisterAccess(); RegisterAccess();
flow = FLOW_NEXT; flow = FLOW_NEXT;
loop_over_list(*inits, i) for ( const auto& aggr : *inits )
{ {
ID* aggr = (*inits)[i];
BroType* t = aggr->Type(); BroType* t = aggr->Type();
Val* v = 0; Val* v = 0;
@ -1649,9 +1647,9 @@ TraversalCode InitStmt::Traverse(TraversalCallback* cb) const
TraversalCode tc = cb->PreStmt(this); TraversalCode tc = cb->PreStmt(this);
HANDLE_TC_STMT_PRE(tc); 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); HANDLE_TC_STMT_PRE(tc);
} }

View file

@ -207,16 +207,16 @@ unsigned int BroType::MemoryAllocation() const
TypeList::~TypeList() TypeList::~TypeList()
{ {
loop_over_list(types, i) for ( const auto& type : types )
Unref(types[i]); Unref(type);
Unref(pure_type); Unref(pure_type);
} }
int TypeList::AllMatch(const BroType* t, int is_init) const int TypeList::AllMatch(const BroType* t, int is_init) const
{ {
loop_over_list(types, i) for ( const auto& type : types )
if ( ! same_type(types[i], t, is_init) ) if ( ! same_type(type, t, is_init) )
return 0; return 0;
return 1; return 1;
} }
@ -381,9 +381,8 @@ TableType::TableType(TypeList* ind, BroType* yield)
type_list* tl = indices->Types(); type_list* tl = indices->Types();
loop_over_list(*tl, i) for ( const auto& tli : *tl )
{ {
BroType* tli = (*tl)[i];
InternalTypeTag t = tli->InternalType(); InternalTypeTag t = tli->InternalType();
if ( t == TYPE_INTERNAL_ERROR ) if ( t == TYPE_INTERNAL_ERROR )
@ -704,8 +703,8 @@ RecordType::RecordType(type_decl_list* arg_types) : BroType(TYPE_RECORD)
RecordType* RecordType::ShallowClone() RecordType* RecordType::ShallowClone()
{ {
auto pass = new type_decl_list(); auto pass = new type_decl_list();
loop_over_list(*types, i) for ( const auto& type : *types )
pass->append(new TypeDecl(*(*types)[i])); pass->append(new TypeDecl(*type));
return new RecordType(pass); return new RecordType(pass);
} }
@ -713,8 +712,8 @@ RecordType::~RecordType()
{ {
if ( types ) if ( types )
{ {
loop_over_list(*types, i) for ( auto type : *types )
delete (*types)[i]; delete type;
delete types; delete types;
} }
@ -823,17 +822,15 @@ const char* RecordType::AddFields(type_decl_list* others, attr_list* attr)
if ( 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; log = true;
} }
} }
loop_over_list(*others, i) for ( const auto& td : *others )
{ {
TypeDecl* td = (*others)[i];
if ( ! td->FindAttr(ATTR_DEFAULT) && if ( ! td->FindAttr(ATTR_DEFAULT) &&
! td->FindAttr(ATTR_OPTIONAL) ) ! td->FindAttr(ATTR_OPTIONAL) )
return "extension field must be &optional or have &default"; return "extension field must be &optional or have &default";
@ -883,11 +880,11 @@ void RecordType::DescribeFields(ODesc* d) const
{ {
d->AddCount(0); d->AddCount(0);
d->AddCount(types->length()); 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->SP();
d->Add((*types)[i]->id); d->Add(type->id);
d->SP(); d->SP();
} }
} }

View file

@ -811,8 +811,8 @@ ListVal::ListVal(TypeTag t)
ListVal::~ListVal() ListVal::~ListVal()
{ {
loop_over_list(vals, i) for ( const auto& val : vals )
Unref(vals[i]); Unref(val);
Unref(type); Unref(type);
} }
@ -822,9 +822,9 @@ RE_Matcher* ListVal::BuildRE() const
Internal("non-string list in ListVal::IncludedInString"); Internal("non-string list in ListVal::IncludedInString");
RE_Matcher* re = new RE_Matcher(); 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); re->AddPat(vs);
} }
@ -853,8 +853,8 @@ TableVal* ListVal::ConvertToSet() const
SetType* s = new SetType(set_index, 0); SetType* s = new SetType(set_index, 0);
TableVal* t = new TableVal(s); TableVal* t = new TableVal(s);
loop_over_list(vals, i) for ( const auto& val : vals )
t->Assign(vals[i], 0); t->Assign(val, 0);
Unref(s); Unref(s);
return t; return t;
@ -891,8 +891,8 @@ Val* ListVal::DoClone(CloneState* state)
lv->vals.resize(vals.length()); lv->vals.resize(vals.length());
state->NewClone(this, lv); state->NewClone(this, lv);
loop_over_list(vals, i) for ( const auto& val : vals )
lv->Append(vals[i]->Clone(state)); lv->Append(val->Clone(state));
return lv; return lv;
} }
@ -900,8 +900,8 @@ Val* ListVal::DoClone(CloneState* state)
unsigned int ListVal::MemoryAllocation() const unsigned int ListVal::MemoryAllocation() const
{ {
unsigned int size = 0; unsigned int size = 0;
loop_over_list(vals, i) for ( const auto& val : vals )
size += vals[i]->MemoryAllocation(); size += val->MemoryAllocation();
return size + padded_sizeof(*this) + vals.MemoryAllocation() - padded_sizeof(vals) return size + padded_sizeof(*this) + vals.MemoryAllocation() - padded_sizeof(vals)
+ type->MemoryAllocation(); + type->MemoryAllocation();
@ -1380,8 +1380,8 @@ Val* TableVal::Default(Val* index)
{ {
const val_list* vl0 = index->AsListVal()->Vals(); const val_list* vl0 = index->AsListVal()->Vals();
vl = val_list(vl0->length()); vl = val_list(vl0->length());
loop_over_list(*vl0, i) for ( const auto& v : *vl0 )
vl.append((*vl0)[i]->Ref()); vl.append(v->Ref());
} }
else else
{ {
@ -2284,9 +2284,9 @@ Val* RecordVal::DoClone(CloneState* state)
rv->origin = nullptr; rv->origin = nullptr;
state->NewClone(this, rv); 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); rv->val.val_list_val->append(v);
} }
@ -2296,12 +2296,10 @@ Val* RecordVal::DoClone(CloneState* state)
unsigned int RecordVal::MemoryAllocation() const unsigned int RecordVal::MemoryAllocation() const
{ {
unsigned int size = 0; unsigned int size = 0;
const val_list* vl = AsRecord(); const val_list* vl = AsRecord();
loop_over_list(*vl, i) for ( const auto& v : *vl )
{ {
Val* v = (*vl)[i];
if ( v ) if ( v )
size += v->MemoryAllocation(); size += v->MemoryAllocation();
} }
@ -2679,8 +2677,8 @@ void delete_vals(val_list* vals)
{ {
if ( vals ) if ( vals )
{ {
loop_over_list(*vals, i) for ( const auto& val : *vals )
Unref((*vals)[i]); Unref(val);
delete vals; delete vals;
} }
} }

View file

@ -448,10 +448,8 @@ void end_func(Stmt* body)
if ( attrs ) if ( attrs )
{ {
loop_over_list(*attrs, i) for ( const auto& a : *attrs )
{ {
Attr* a = (*attrs)[i];
if ( a->Tag() == ATTR_DEPRECATED ) if ( a->Tag() == ATTR_DEPRECATED )
continue; continue;

View file

@ -728,11 +728,11 @@ void Analyzer::CancelTimers()
// traversing. Thus, we first make a copy of the list which we then // traversing. Thus, we first make a copy of the list which we then
// iterate through. // iterate through.
timer_list tmp(timers.length()); timer_list tmp(timers.length());
loop_over_list(timers, j) std::copy(timers.begin(), timers.end(), back_inserter(tmp));
tmp.append(timers[j]);
loop_over_list(tmp, i) // TODO: could be a for_each
Conn()->GetTimerMgr()->Cancel(tmp[i]); for ( auto timer : tmp )
Conn()->GetTimerMgr()->Cancel(timer);
timers_canceled = 1; timers_canceled = 1;
timers.clear(); timers.clear();

View file

@ -1054,8 +1054,8 @@ void Manager::ProcessEvent(const broker::topic& topic, broker::zeek::Event ev)
mgr.QueueEventFast(handler, std::move(vl), SOURCE_BROKER); mgr.QueueEventFast(handler, std::move(vl), SOURCE_BROKER);
else else
{ {
loop_over_list(vl, i) for ( const auto& v : vl )
Unref(vl[i]); Unref(v);
} }
} }

View file

@ -1893,8 +1893,8 @@ bool Manager::SendEvent(ReaderFrontend* reader, const string& name, const int nu
if ( convert_error ) if ( convert_error )
{ {
loop_over_list(vl, i) for ( const auto& v : vl )
Unref(vl[i]); Unref(v);
return false; return false;
} }

View file

@ -164,11 +164,8 @@ static type_decl_list* copy_type_decl_list(type_decl_list* tdl)
type_decl_list* rval = new type_decl_list(); type_decl_list* rval = new type_decl_list();
loop_over_list(*tdl, i) for ( const auto& td : *tdl )
{
TypeDecl* td = (*tdl)[i];
rval->append(new TypeDecl(*td)); rval->append(new TypeDecl(*td));
}
return rval; return rval;
} }
@ -180,9 +177,8 @@ static attr_list* copy_attr_list(attr_list* al)
attr_list* rval = new attr_list(); attr_list* rval = new attr_list();
loop_over_list(*al, i) for ( const auto& a : *al )
{ {
Attr* a = (*al)[i];
::Ref(a); ::Ref(a);
rval->append(a); rval->append(a);
} }

View file

@ -61,15 +61,15 @@ function levenshtein_distance%(s1: string, s2: string%): count
function string_cat%(...%): string function string_cat%(...%): string
%{ %{
int n = 0; int n = 0;
loop_over_list(@ARG@, i) for ( const auto& a : @ARG@ )
n += @ARG@[i]->AsString()->Len(); n += a->AsString()->Len();
u_char* b = new u_char[n+1]; u_char* b = new u_char[n+1];
BroString* s = new BroString(1, b, n); 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()); memcpy(b, s->Bytes(), s->Len());
b += s->Len(); b += s->Len();
} }

View file

@ -1001,11 +1001,12 @@ string bro_prefixes()
{ {
string rval; string rval;
loop_over_list(prefixes, j) for ( const auto& prefix : prefixes )
if ( j == 0 ) {
rval.append(prefixes[j]); if ( ! rval.empty() )
else rval.append(":");
rval.append(":").append(prefixes[j]); rval.append(prefix);
}
return rval; return rval;
} }

View file

@ -1566,8 +1566,8 @@ function cat%(...%): string
ODesc d; ODesc d;
d.SetStyle(RAW_STYLE); d.SetStyle(RAW_STYLE);
loop_over_list(@ARG@, i) for ( const auto& a : @ARG@ )
@ARG@[i]->Describe(&d); a->Describe(&d);
BroString* s = new BroString(1, d.TakeBytes(), d.Len()); BroString* s = new BroString(1, d.TakeBytes(), d.Len());
s->SetUseFreeToDelete(true); s->SetUseFreeToDelete(true);