Fix various compiler/linter warnings in script_opt/CPP code

This commit is contained in:
Jon Siwek 2021-05-03 18:14:41 -07:00 committed by Vern Paxson
parent e200016200
commit 3e85375010
15 changed files with 43 additions and 41 deletions

View file

@ -322,7 +322,7 @@ ScriptFunc::ScriptFunc(std::string _name, FuncTypePtr ft,
auto n = bs.size(); auto n = bs.size();
ASSERT(n == priorities.size()); ASSERT(n == priorities.size());
for ( auto i = 0; i < n; ++i ) for ( auto i = 0u; i < n; ++i )
{ {
Body b; Body b;
b.stmts = std::move(bs[i]); b.stmts = std::move(bs[i]);

View file

@ -86,9 +86,8 @@ void CPPCompile::GenAttrs(const AttributesPtr& attrs)
AddInit(attrs); AddInit(attrs);
for ( auto i = 0; i < avec.size(); ++i ) for ( const auto& attr : avec )
{ {
const auto& attr = avec[i];
const auto& e = attr->GetExpr(); const auto& e = attr->GetExpr();
if ( ! e ) if ( ! e )
@ -171,7 +170,7 @@ const char* CPPCompile::AttrName(const AttrPtr& attr)
case ATTR_IS_ASSIGNED: return "ATTR_IS_ASSIGNED"; case ATTR_IS_ASSIGNED: return "ATTR_IS_ASSIGNED";
case ATTR_IS_USED: return "ATTR_IS_USED"; case ATTR_IS_USED: return "ATTR_IS_USED";
case NUM_ATTRS: return "<busted>"; default: return "<busted>";
} }
} }

View file

@ -4,6 +4,8 @@
%%{ // C segment %%{ // C segment
#include <cinttypes>
#
#include "zeek/Reporter.h" #include "zeek/Reporter.h"
#include "zeek/script_opt/ScriptOpt.h" #include "zeek/script_opt/ScriptOpt.h"
#include "zeek/script_opt/CPP/Func.h" #include "zeek/script_opt/CPP/Func.h"
@ -22,7 +24,7 @@ function load_CPP%(h: count%): bool
if ( cb == detail::standalone_callbacks.end() ) if ( cb == detail::standalone_callbacks.end() )
{ {
reporter->Error("load of non-existing C++ code (%llu)", h); reporter->Error("load of non-existing C++ code (%" PRIu64 ")", h);
return zeek::val_mgr->False(); return zeek::val_mgr->False();
} }

View file

@ -233,14 +233,14 @@ void CPPCompile::AddRecordConstant(const ValPtr& v, string& const_name)
auto r = cast_intrusive<RecordVal>(v); auto r = cast_intrusive<RecordVal>(v);
auto n = r->NumFields(); auto n = r->NumFields();
for ( auto i = 0; i < n; ++i ) for ( auto i = 0u; i < n; ++i )
{ {
const auto& r_i = r->GetField(i); const auto& r_i = r->GetField(i);
if ( r_i ) if ( r_i )
{ {
auto r_i_c = BuildConstant(v, r_i); auto r_i_c = BuildConstant(v, r_i);
AddInit(v, const_name + "->Assign(" + Fmt(i) + AddInit(v, const_name + "->Assign(" + Fmt(static_cast<int>(i)) +
", " + r_i_c + ");"); ", " + r_i_c + ");");
} }
} }
@ -282,7 +282,7 @@ void CPPCompile::AddVectorConstant(const ValPtr& v, string& const_name)
auto vv = cast_intrusive<VectorVal>(v); auto vv = cast_intrusive<VectorVal>(v);
auto n = vv->Size(); auto n = vv->Size();
for ( auto i = 0; i < n; ++i ) for ( auto i = 0u; i < n; ++i )
{ {
const auto& v_i = vv->ValAt(i); const auto& v_i = vv->ValAt(i);
auto v_i_c = BuildConstant(v, v_i); auto v_i_c = BuildConstant(v, v_i);

View file

@ -19,7 +19,7 @@ void CPPCompile::DeclareFunc(const FuncInfo& func)
auto fname = Canonicalize(BodyName(func).c_str()) + "_zf"; auto fname = Canonicalize(BodyName(func).c_str()) + "_zf";
auto pf = func.Profile(); auto pf = func.Profile();
auto f = func.Func(); auto f = func.Func();
auto body = func.Body(); const auto& body = func.Body();
auto priority = func.Priority(); auto priority = func.Priority();
DeclareSubclass(f->GetType(), pf, fname, body, priority, nullptr, DeclareSubclass(f->GetType(), pf, fname, body, priority, nullptr,
@ -133,7 +133,7 @@ void CPPCompile::DeclareSubclass(const FuncTypePtr& ft, const ProfileFunc* pf,
body_hashes[fname] = h; body_hashes[fname] = h;
body_priorities[fname] = priority; body_priorities[fname] = priority;
body_names.emplace(body.get(), fname); body_names.emplace(body.get(), fname);
names_to_bodies.emplace(move(fname), body.get()); names_to_bodies.emplace(fname, body.get());
total_hash = merge_p_hashes(total_hash, h); total_hash = merge_p_hashes(total_hash, h);
} }

View file

@ -63,7 +63,9 @@ void CPPCompile::Compile()
// as a way to identify this compilation. Only germane when doing // as a way to identify this compilation. Only germane when doing
// incremental compilation (particularly of the test suite). // incremental compilation (particularly of the test suite).
char buf[8192]; char buf[8192];
getcwd(buf, sizeof buf); if ( ! getcwd(buf, sizeof buf) )
reporter->FatalError("getcwd failed: %s", strerror(errno));
working_dir = buf; working_dir = buf;
if ( update && addl_tag > 0 && CheckForCollisions() ) if ( update && addl_tag > 0 && CheckForCollisions() )
@ -212,7 +214,7 @@ void CPPCompile::RegisterCompiledBody(const string& f)
// Build up an initializer of the events relevant to the function. // Build up an initializer of the events relevant to the function.
string events; string events;
if ( body_events.count(f) > 0 ) if ( body_events.count(f) > 0 )
for ( auto e : body_events[f] ) for ( const auto& e : body_events[f] )
{ {
if ( events.size() > 0 ) if ( events.size() > 0 )
events += ", "; events += ", ";

View file

@ -638,7 +638,7 @@ string CPPCompile::GenSetConstructorExpr(const Expr* e)
{ {
auto sc = static_cast<const SetConstructorExpr*>(e); auto sc = static_cast<const SetConstructorExpr*>(e);
const auto& t = sc->GetType(); const auto& t = sc->GetType();
auto attrs = sc->GetAttrs(); const auto& attrs = sc->GetAttrs();
string attr_tags; string attr_tags;
string attr_vals; string attr_vals;
@ -654,7 +654,7 @@ string CPPCompile::GenTableConstructorExpr(const Expr* e)
{ {
auto tc = static_cast<const TableConstructorExpr*>(e); auto tc = static_cast<const TableConstructorExpr*>(e);
const auto& t = tc->GetType(); const auto& t = tc->GetType();
auto attrs = tc->GetAttrs(); const auto& attrs = tc->GetAttrs();
string attr_tags; string attr_tags;
string attr_vals; string attr_vals;
@ -1126,7 +1126,7 @@ string CPPCompile::GenIntVector(const vector<int>& vec)
{ {
string res("{ "); string res("{ ");
for ( auto i = 0; i < vec.size(); ++i ) for ( auto i = 0u; i < vec.size(); ++i )
{ {
res += Fmt(vec[i]); res += Fmt(vec[i]);

View file

@ -36,9 +36,8 @@ broker::expected<broker::data> CPPLambdaFunc::SerializeClosure() const
broker::vector body; broker::vector body;
for ( int i = 0; i < vals.size(); ++i ) for ( const auto& val : vals )
{ {
const auto& val = vals[i];
auto expected = Broker::detail::val_to_data(val.get()); auto expected = Broker::detail::val_to_data(val.get());
if ( ! expected ) if ( ! expected )
return broker::ec::invalid_data; return broker::ec::invalid_data;

View file

@ -19,7 +19,7 @@ void CPPCompile::CompileFunc(const FuncInfo& func)
auto fname = Canonicalize(BodyName(func).c_str()) + "_zf"; auto fname = Canonicalize(BodyName(func).c_str()) + "_zf";
auto pf = func.Profile(); auto pf = func.Profile();
auto f = func.Func(); auto f = func.Func();
auto body = func.Body(); const auto& body = func.Body();
DefineBody(f->GetType(), pf, fname, body, nullptr, f->Flavor()); DefineBody(f->GetType(), pf, fname, body, nullptr, f->Flavor());
} }
@ -223,7 +223,7 @@ string CPPCompile::BodyName(const FuncInfo& func)
// Make the name distinct-per-body. // Make the name distinct-per-body.
int i; size_t i;
for ( i = 0; i < bodies.size(); ++i ) for ( i = 0; i < bodies.size(); ++i )
if ( bodies[i].stmts == body ) if ( bodies[i].stmts == body )
break; break;
@ -231,7 +231,7 @@ string CPPCompile::BodyName(const FuncInfo& func)
if ( i >= bodies.size() ) if ( i >= bodies.size() )
reporter->InternalError("can't find body in CPPCompile::BodyName"); reporter->InternalError("can't find body in CPPCompile::BodyName");
return fname + "__" + Fmt(i); return fname + "__" + Fmt(static_cast<int>(i));
} }
string CPPCompile::GenArgs(const RecordTypePtr& params, const Expr* e) string CPPCompile::GenArgs(const RecordTypePtr& params, const Expr* e)
@ -258,7 +258,7 @@ string CPPCompile::GenArgs(const RecordTypePtr& params, const Expr* e)
if ( ! param_any && arg_any ) if ( ! param_any && arg_any )
expr_gen = GenericValPtrToGT(expr_gen, param_t, GEN_NATIVE); expr_gen = GenericValPtrToGT(expr_gen, param_t, GEN_NATIVE);
gen = gen + expr_gen; gen += expr_gen;
if ( i < n - 1 ) if ( i < n - 1 )
gen += ", "; gen += ", ";
} }

View file

@ -161,7 +161,7 @@ void CPPCompile::GenFuncVarInits()
string hashes = "{"; string hashes = "{";
for ( auto b : bodies ) for ( const auto& b : bodies )
{ {
auto body = b.stmts.get(); auto body = b.stmts.get();

View file

@ -31,7 +31,7 @@ ListValPtr index_val__CPP(vector<ValPtr> indices)
// In the future, we could provide N versions of this that // In the future, we could provide N versions of this that
// unroll the loop. // unroll the loop.
for ( auto i : indices ) for ( const auto& i : indices )
ind_v->Append(i); ind_v->Append(i);
return ind_v; return ind_v;
@ -61,7 +61,7 @@ ValPtr index_string__CPP(const StringValPtr& svp, vector<ValPtr> indices)
ValPtr set_event__CPP(IDPtr g, ValPtr v, EventHandlerPtr& gh) ValPtr set_event__CPP(IDPtr g, ValPtr v, EventHandlerPtr& gh)
{ {
g->SetVal(move(v)); g->SetVal(v);
gh = event_registry->Register(g->Name()); gh = event_registry->Register(g->Name());
return v; return v;
} }
@ -116,15 +116,15 @@ ValPtr assign_to_index__CPP(T v1, ValPtr v2, ValPtr v3)
ValPtr assign_to_index__CPP(TableValPtr v1, ValPtr v2, ValPtr v3) ValPtr assign_to_index__CPP(TableValPtr v1, ValPtr v2, ValPtr v3)
{ {
return assign_to_index__CPP<TableValPtr>(v1, v2, v3); return assign_to_index__CPP<TableValPtr>(std::move(v1), std::move(v2), std::move(v3));
} }
ValPtr assign_to_index__CPP(VectorValPtr v1, ValPtr v2, ValPtr v3) ValPtr assign_to_index__CPP(VectorValPtr v1, ValPtr v2, ValPtr v3)
{ {
return assign_to_index__CPP<VectorValPtr>(v1, v2, v3); return assign_to_index__CPP<VectorValPtr>(std::move(v1), std::move(v2), std::move(v3));
} }
ValPtr assign_to_index__CPP(StringValPtr v1, ValPtr v2, ValPtr v3) ValPtr assign_to_index__CPP(StringValPtr v1, ValPtr v2, ValPtr v3)
{ {
return assign_to_index__CPP<StringValPtr>(v1, v2, v3); return assign_to_index__CPP<StringValPtr>(std::move(v1), std::move(v2), std::move(v3));
} }
void add_element__CPP(TableValPtr aggr, ListValPtr indices) void add_element__CPP(TableValPtr aggr, ListValPtr indices)
@ -172,7 +172,7 @@ TableValPtr set_constructor__CPP(vector<ValPtr> elements, TableTypePtr t,
auto attrs = build_attrs__CPP(move(attr_tags), move(attr_vals)); auto attrs = build_attrs__CPP(move(attr_tags), move(attr_vals));
auto aggr = make_intrusive<TableVal>(move(t), move(attrs)); auto aggr = make_intrusive<TableVal>(move(t), move(attrs));
for ( const auto& elem : elements ) for ( auto& elem : elements )
aggr->Assign(move(elem), nullptr); aggr->Assign(move(elem), nullptr);
return aggr; return aggr;
@ -188,7 +188,7 @@ TableValPtr table_constructor__CPP(vector<ValPtr> indices, vector<ValPtr> vals,
auto attrs = build_attrs__CPP(move(attr_tags), move(attr_vals)); auto attrs = build_attrs__CPP(move(attr_tags), move(attr_vals));
auto aggr = make_intrusive<TableVal>(move(t), move(attrs)); auto aggr = make_intrusive<TableVal>(move(t), move(attrs));
for ( auto i = 0; i < n; ++i ) for ( auto i = 0u; i < n; ++i )
{ {
auto v = check_and_promote(vals[i], yt, true); auto v = check_and_promote(vals[i], yt, true);
if ( v ) if ( v )
@ -205,7 +205,7 @@ RecordValPtr record_constructor__CPP(vector<ValPtr> vals, RecordTypePtr t)
rv->Reserve(n); rv->Reserve(n);
for ( auto i = 0; i < n; ++i ) for ( auto i = 0u; i < n; ++i )
rv->Assign(i, vals[i]); rv->Assign(i, vals[i]);
return rv; return rv;
@ -216,7 +216,7 @@ VectorValPtr vector_constructor__CPP(vector<ValPtr> vals, VectorTypePtr t)
auto vv = make_intrusive<VectorVal>(move(t)); auto vv = make_intrusive<VectorVal>(move(t));
auto n = vals.size(); auto n = vals.size();
for ( auto i = 0; i < n; ++i ) for ( auto i = 0u; i < n; ++i )
vv->Assign(i, vals[i]); vv->Assign(i, vals[i]);
return vv; return vv;

View file

@ -140,10 +140,10 @@ void CPPCompile::ExpandTypeVar(const TypePtr& t)
void CPPCompile::ExpandListTypeVar(const TypePtr& t, string& tn) void CPPCompile::ExpandListTypeVar(const TypePtr& t, string& tn)
{ {
auto tl = t->AsTypeList()->GetTypes(); const auto& tl = t->AsTypeList()->GetTypes();
auto t_name = tn + "->AsTypeList()"; auto t_name = tn + "->AsTypeList()";
for ( auto i = 0; i < tl.size(); ++i ) for ( auto i = 0u; i < tl.size(); ++i )
AddInit(t, t_name + "->Append(" + AddInit(t, t_name + "->Append(" +
GenTypeName(tl[i]) + ");"); GenTypeName(tl[i]) + ");");
} }
@ -208,7 +208,7 @@ void CPPCompile::ExpandFuncTypeVar(const TypePtr& t, string& tn)
auto f = t->AsFuncType(); auto f = t->AsFuncType();
auto args_type_accessor = GenTypeName(f->Params()); auto args_type_accessor = GenTypeName(f->Params());
auto yt = f->Yield(); const auto& yt = f->Yield();
string yield_type_accessor; string yield_type_accessor;
@ -447,9 +447,9 @@ void CPPCompile::RegisterType(const TypePtr& tp)
void CPPCompile::RegisterListType(const TypePtr& t) void CPPCompile::RegisterListType(const TypePtr& t)
{ {
auto tl = t->AsTypeList()->GetTypes(); const auto& tl = t->AsTypeList()->GetTypes();
for ( auto i = 0; i < tl.size(); ++i ) for ( auto i = 0u; i < tl.size(); ++i )
{ {
NoteNonRecordInitDependency(t, tl[i]); NoteNonRecordInitDependency(t, tl[i]);
RegisterType(tl[i]); RegisterType(tl[i]);

View file

@ -241,7 +241,7 @@ string CPPCompile::Canonicalize(const char* name) const
if ( c == ':' || c == '-' ) if ( c == ':' || c == '-' )
c = '_'; c = '_';
cname = cname + c; cname += c;
} }
// Add a trailing '_' to avoid conflicts with C++ keywords. // Add a trailing '_' to avoid conflicts with C++ keywords.

View file

@ -483,7 +483,7 @@ void ProfileFuncs::MergeInProfile(ProfileFunc* pf)
(void) HashType(t->AsTypeType()->GetType()); (void) HashType(t->AsTypeType()->GetType());
auto& init_exprs = g->GetInitExprs(); auto& init_exprs = g->GetInitExprs();
for ( auto i_e : init_exprs ) for ( const auto& i_e : init_exprs )
if ( i_e ) if ( i_e )
{ {
pending_exprs.push_back(i_e.get()); pending_exprs.push_back(i_e.get());
@ -550,7 +550,7 @@ void ProfileFuncs::TraverseValue(const ValPtr& v)
auto r = cast_intrusive<RecordVal>(v); auto r = cast_intrusive<RecordVal>(v);
auto n = r->NumFields(); auto n = r->NumFields();
for ( auto i = 0; i < n; ++i ) for ( auto i = 0u; i < n; ++i )
TraverseValue(r->GetField(i)); TraverseValue(r->GetField(i));
} }
break; break;
@ -583,7 +583,7 @@ void ProfileFuncs::TraverseValue(const ValPtr& v)
auto vv = cast_intrusive<VectorVal>(v); auto vv = cast_intrusive<VectorVal>(v);
auto n = vv->Size(); auto n = vv->Size();
for ( auto i = 0; i < n; ++i ) for ( auto i = 0u; i < n; ++i )
TraverseValue(vv->ValAt(i)); TraverseValue(vv->ValAt(i));
} }
break; break;

View file

@ -334,7 +334,7 @@ void analyze_scripts()
printf("\nAdditional C++ script bodies available:\n"); printf("\nAdditional C++ script bodies available:\n");
int addl = 0; int addl = 0;
for ( auto s : compiled_scripts ) for ( const auto& s : compiled_scripts )
if ( already_reported.count(s.first) == 0 ) if ( already_reported.count(s.first) == 0 )
{ {
printf("%s body (hash %llu)\n", printf("%s body (hash %llu)\n",