migrated some raw pointers to smart pointers

This commit is contained in:
Vern Paxson 2023-07-07 18:29:45 -07:00
parent c23ee30542
commit 1b2cd0c767
14 changed files with 74 additions and 113 deletions

View file

@ -205,7 +205,7 @@ void get_first_statement(Stmt* list, Stmt*& first, Location& loc)
while ( first->Tag() == STMT_LIST ) while ( first->Tag() == STMT_LIST )
{ {
if ( first->AsStmtList()->Stmts()[0] ) if ( first->AsStmtList()->Stmts()[0] )
first = first->AsStmtList()->Stmts()[0]; first = first->AsStmtList()->Stmts()[0].get();
else else
break; break;
} }

View file

@ -777,8 +777,8 @@ StmtPtr ScriptFunc::AddInits(StmtPtr body, const std::vector<IDPtr>& inits)
return body; return body;
auto stmt_series = make_intrusive<StmtList>(); auto stmt_series = make_intrusive<StmtList>();
stmt_series->Stmts().push_back(new InitStmt(inits)); stmt_series->Stmts().push_back(make_intrusive<InitStmt>(inits));
stmt_series->Stmts().push_back(body.release()); stmt_series->Stmts().push_back(std::move(body));
return stmt_series; return stmt_series;
} }

View file

@ -576,12 +576,12 @@ static StmtTag get_last_stmt_tag(const Stmt* stmt)
return stmt->Tag(); return stmt->Tag();
const StmtList* stmts = stmt->AsStmtList(); const StmtList* stmts = stmt->AsStmtList();
int len = stmts->Stmts().length(); auto len = stmts->Stmts().size();
if ( len == 0 ) if ( len == 0 )
return STMT_LIST; return STMT_LIST;
return get_last_stmt_tag(stmts->Stmts()[len - 1]); return get_last_stmt_tag(stmts->Stmts()[len - 1].get());
} }
class FallthroughFinder : public TraversalCallback class FallthroughFinder : public TraversalCallback
@ -1668,26 +1668,19 @@ void ReturnStmt::StmtDescribe(ODesc* d) const
DescribeDone(d); DescribeDone(d);
} }
StmtList::StmtList() : Stmt(STMT_LIST) StmtList::StmtList() : Stmt(STMT_LIST) { }
{
stmts = new StmtPList;
}
StmtList::~StmtList() StmtList::~StmtList() { }
{
for ( const auto& stmt : Stmts() )
Unref(stmt);
delete stmts;
}
ValPtr StmtList::Exec(Frame* f, StmtFlowType& flow) ValPtr StmtList::Exec(Frame* f, StmtFlowType& flow)
{ {
RegisterAccess(); RegisterAccess();
flow = FLOW_NEXT; flow = FLOW_NEXT;
for ( const auto& stmt : Stmts() ) for ( const auto& stmt_ptr : Stmts() )
{ {
auto stmt = stmt_ptr.get();
f->SetNextStmt(stmt); f->SetNextStmt(stmt);
if ( ! pre_execute_stmt(stmt, f) ) if ( ! pre_execute_stmt(stmt, f) )
@ -1720,10 +1713,10 @@ void StmtList::StmtDescribe(ODesc* d) const
if ( ! d->IsReadable() ) if ( ! d->IsReadable() )
{ {
AddTag(d); AddTag(d);
d->AddCount(stmts->length()); d->AddCount(stmts.size());
} }
if ( stmts->length() == 0 ) if ( stmts.empty() )
DescribeDone(d); DescribeDone(d);
else else
@ -2209,7 +2202,7 @@ void WhenInfo::BuildInvokeElems()
invoke_timeout = make_intrusive<ListExpr>(three_const); invoke_timeout = make_intrusive<ListExpr>(three_const);
} }
WhenStmt::WhenStmt(WhenInfo* arg_wi) : Stmt(STMT_WHEN), wi(arg_wi) WhenStmt::WhenStmt(std::shared_ptr<WhenInfo> arg_wi) : Stmt(STMT_WHEN), wi(std::move(arg_wi))
{ {
wi->Build(ThisPtr()); wi->Build(ThisPtr());
@ -2231,11 +2224,6 @@ WhenStmt::WhenStmt(WhenInfo* arg_wi) : Stmt(STMT_WHEN), wi(arg_wi)
} }
} }
WhenStmt::~WhenStmt()
{
delete wi;
}
ValPtr WhenStmt::Exec(Frame* f, StmtFlowType& flow) ValPtr WhenStmt::Exec(Frame* f, StmtFlowType& flow)
{ {
RegisterAccess(); RegisterAccess();

View file

@ -464,8 +464,8 @@ public:
ValPtr Exec(Frame* f, StmtFlowType& flow) override; ValPtr Exec(Frame* f, StmtFlowType& flow) override;
const StmtPList& Stmts() const { return *stmts; } const auto& Stmts() const { return stmts; }
StmtPList& Stmts() { return *stmts; } auto& Stmts() { return stmts; }
void StmtDescribe(ODesc* d) const override; void StmtDescribe(ODesc* d) const override;
@ -481,23 +481,18 @@ public:
bool NoFlowAfter(bool ignore_break) const override; bool NoFlowAfter(bool ignore_break) const override;
// Idioms commonly used in reduction. // Idioms commonly used in reduction.
StmtList(StmtPtr s1, Stmt* s2);
StmtList(StmtPtr s1, StmtPtr s2); StmtList(StmtPtr s1, StmtPtr s2);
StmtList(StmtPtr s1, StmtPtr s2, StmtPtr s3); StmtList(StmtPtr s1, StmtPtr s2, StmtPtr s3);
protected: protected:
bool IsPure() const override; bool IsPure() const override;
StmtPList* stmts; std::vector<StmtPtr> stmts;
// Optimization-related: // Optimization-related:
bool ReduceStmt(int& s_i, StmtPList* f_stmts, Reducer* c); bool ReduceStmt(int& s_i, std::vector<StmtPtr>& f_stmts, Reducer* c);
void ResetStmts(StmtPList* new_stmts) void ResetStmts(std::vector<StmtPtr> new_stmts) { stmts = std::move(new_stmts); }
{
delete stmts;
stmts = new_stmts;
}
}; };
class InitStmt final : public Stmt class InitStmt final : public Stmt
@ -690,9 +685,7 @@ private:
class WhenStmt final : public Stmt class WhenStmt final : public Stmt
{ {
public: public:
// The constructor takes ownership of the WhenInfo object. WhenStmt(std::shared_ptr<WhenInfo> wi);
WhenStmt(WhenInfo* wi);
~WhenStmt() override;
ValPtr Exec(Frame* f, StmtFlowType& flow) override; ValPtr Exec(Frame* f, StmtFlowType& flow) override;
bool IsPure() const override; bool IsPure() const override;
@ -703,7 +696,7 @@ public:
StmtPtr TimeoutBody() const { return wi->TimeoutStmt(); } StmtPtr TimeoutBody() const { return wi->TimeoutStmt(); }
bool IsReturn() const { return wi->IsReturn(); } bool IsReturn() const { return wi->IsReturn(); }
WhenInfo* Info() const { return wi; } auto Info() const { return wi; }
void StmtDescribe(ODesc* d) const override; void StmtDescribe(ODesc* d) const override;
@ -716,7 +709,7 @@ public:
StmtPtr DoReduce(Reducer* c) override; StmtPtr DoReduce(Reducer* c) override;
private: private:
WhenInfo* wi; std::shared_ptr<WhenInfo> wi;
}; };
// Internal statement used for inlining. Executes a block and stops // Internal statement used for inlining. Executes a block and stops

View file

@ -99,7 +99,7 @@ protected:
double time; double time;
}; };
Trigger::Trigger(WhenInfo* wi, double timeout, const IDSet& _globals, Trigger::Trigger(std::shared_ptr<WhenInfo> wi, double timeout, const IDSet& _globals,
std::vector<ValPtr> _local_aggrs, Frame* f, const Location* loc) std::vector<ValPtr> _local_aggrs, Frame* f, const Location* loc)
{ {
timeout_value = timeout; timeout_value = timeout;

View file

@ -48,8 +48,8 @@ public:
// statements are executed immediately and the object is deleted // statements are executed immediately and the object is deleted
// right away. // right away.
Trigger(WhenInfo* wi, double timeout, const IDSet& globals, std::vector<ValPtr> local_aggrs, Trigger(std::shared_ptr<WhenInfo> wi, double timeout, const IDSet& globals,
Frame* f, const Location* loc); std::vector<ValPtr> local_aggrs, Frame* f, const Location* loc);
~Trigger() override; ~Trigger() override;

View file

@ -23,7 +23,6 @@ class Type;
using ValPList = PList<Val>; using ValPList = PList<Val>;
using ExprPList = PList<detail::Expr>; using ExprPList = PList<detail::Expr>;
using IDPList = PList<detail::ID>; using IDPList = PList<detail::ID>;
using StmtPList = PList<detail::Stmt>;
using TypePList = PList<Type>; using TypePList = PList<Type>;
using AttrPList = PList<detail::Attr>; using AttrPList = PList<detail::Attr>;
using TimerPList = PList<detail::Timer, ListOrder::UNORDERED>; using TimerPList = PList<detail::Timer, ListOrder::UNORDERED>;

View file

@ -395,7 +395,7 @@ zeek:
stmt_list stmt_list
{ {
if ( stmts ) if ( stmts )
stmts->AsStmtList()->Stmts().push_back($3); stmts->AsStmtList()->Stmts().push_back({AdoptRef{}, $3});
else else
stmts = $3; stmts = $3;
@ -1931,7 +1931,8 @@ stmt:
| when_clause | when_clause
{ {
$$ = new WhenStmt($1); std::shared_ptr<WhenInfo> wi($1);
$$ = new WhenStmt(wi);
script_coverage_mgr.AddStmt($$); script_coverage_mgr.AddStmt($$);
} }
@ -1968,7 +1969,7 @@ stmt_list:
stmt_list stmt stmt_list stmt
{ {
set_location(@1, @2); set_location(@1, @2);
$1->AsStmtList()->Stmts().push_back($2); $1->AsStmtList()->Stmts().push_back({AdoptRef{}, $2});
$1->UpdateLocationEndInfo(@2); $1->UpdateLocationEndInfo(@2);
} }
| |

View file

@ -654,7 +654,7 @@ ExprPtr Reducer::UpdateExpr(ExprPtr e)
return make_intrusive<ConstExpr>(c->ValuePtr()); return make_intrusive<ConstExpr>(c->ValuePtr());
} }
StmtPtr Reducer::MergeStmts(const NameExpr* lhs, ExprPtr rhs, Stmt* succ_stmt) StmtPtr Reducer::MergeStmts(const NameExpr* lhs, ExprPtr rhs, const StmtPtr& succ_stmt)
{ {
// First check for tmp=rhs. // First check for tmp=rhs.
auto lhs_id = lhs->Id(); auto lhs_id = lhs->Id();

View file

@ -137,7 +137,7 @@ public:
// Given an lhs=rhs statement followed by succ_stmt, returns // Given an lhs=rhs statement followed by succ_stmt, returns
// a (new) merge of the two if they're of the form tmp=rhs, var=tmp; // a (new) merge of the two if they're of the form tmp=rhs, var=tmp;
// otherwise, nil. // otherwise, nil.
StmtPtr MergeStmts(const NameExpr* lhs, ExprPtr rhs, Stmt* succ_stmt); StmtPtr MergeStmts(const NameExpr* lhs, ExprPtr rhs, const StmtPtr& succ_stmt);
// Update expressions with optimized versions. They are distinct // Update expressions with optimized versions. They are distinct
// because the first two (meant for calls in a Stmt reduction // because the first two (meant for calls in a Stmt reduction

View file

@ -91,7 +91,7 @@ StmtPtr ExprListStmt::DoReduce(Reducer* c)
new_l->Append(red_e); new_l->Append(red_e);
if ( red_e_stmt ) if ( red_e_stmt )
s->Stmts().push_back(red_e_stmt.release()); s->Stmts().push_back(red_e_stmt);
} }
} }
@ -103,7 +103,7 @@ StmtPtr ExprListStmt::DoReduce(Reducer* c)
else else
{ {
s->Stmts().push_back(DoSubclassReduce(new_l, c).release()); s->Stmts().push_back(DoSubclassReduce(new_l, c));
return s->Reduce(c); return s->Reduce(c);
} }
} }
@ -296,7 +296,7 @@ StmtPtr IfStmt::DoReduce(Reducer* c)
} }
if ( red_e_stmt ) if ( red_e_stmt )
return TransformMe(make_intrusive<StmtList>(red_e_stmt, this), c); return TransformMe(make_intrusive<StmtList>(red_e_stmt, ThisPtr()), c);
return ThisPtr(); return ThisPtr();
} }
@ -382,7 +382,14 @@ StmtPtr SwitchStmt::DoReduce(Reducer* rc)
// Note, the compiler checks for constant switch expressions. // Note, the compiler checks for constant switch expressions.
if ( red_e_stmt ) if ( red_e_stmt )
s->Stmts().push_back(red_e_stmt.release()); s->Stmts().push_back(red_e_stmt);
// Update type cases.
for ( auto& i : case_label_type_list )
{
IDPtr idp = {NewRef{}, i.first};
i.first = rc->UpdateID(idp).release();
}
for ( const auto& c : *cases ) for ( const auto& c : *cases )
{ {
@ -393,7 +400,7 @@ StmtPtr SwitchStmt::DoReduce(Reducer* rc)
auto red_cases = c_e->Reduce(rc, c_e_stmt); auto red_cases = c_e->Reduce(rc, c_e_stmt);
if ( c_e_stmt ) if ( c_e_stmt )
s->Stmts().push_back(c_e_stmt.release()); s->Stmts().push_back(c_e_stmt);
} }
auto c_t = c->TypeCases(); auto c_t = c->TypeCases();
@ -403,14 +410,7 @@ StmtPtr SwitchStmt::DoReduce(Reducer* rc)
c->UpdateBody(c->Body()->Reduce(rc)); c->UpdateBody(c->Body()->Reduce(rc));
} }
// Update type cases. if ( ! s->Stmts().empty() )
for ( auto& i : case_label_type_list )
{
IDPtr idp = {NewRef{}, i.first};
i.first = rc->UpdateID(idp).release();
}
if ( s->Stmts().length() > 0 )
{ {
StmtPtr me = ThisPtr(); StmtPtr me = ThisPtr();
auto pre_and_me = make_intrusive<StmtList>(s, me); auto pre_and_me = make_intrusive<StmtList>(s, me);
@ -634,7 +634,7 @@ StmtPtr ForStmt::DoReduce(Reducer* c)
Warn("empty \"for\" body leaves loop variables in indeterminate state"); Warn("empty \"for\" body leaves loop variables in indeterminate state");
if ( red_e_stmt ) if ( red_e_stmt )
return TransformMe(make_intrusive<StmtList>(red_e_stmt, this), c); return TransformMe(make_intrusive<StmtList>(red_e_stmt, ThisPtr()), c);
return ThisPtr(); return ThisPtr();
} }
@ -677,33 +677,22 @@ StmtPtr ReturnStmt::DoReduce(Reducer* c)
return ThisPtr(); return ThisPtr();
} }
StmtList::StmtList(StmtPtr s1, Stmt* s2) : Stmt(STMT_LIST)
{
stmts = new StmtPList;
if ( s1 )
stmts->append(s1.release());
if ( s2 )
stmts->append(s2);
}
StmtList::StmtList(StmtPtr s1, StmtPtr s2) : Stmt(STMT_LIST) StmtList::StmtList(StmtPtr s1, StmtPtr s2) : Stmt(STMT_LIST)
{ {
stmts = new StmtPList;
if ( s1 ) if ( s1 )
stmts->append(s1.release()); stmts.push_back(std::move(s1));
if ( s2 ) if ( s2 )
stmts->append(s2.release()); stmts.push_back(std::move(s2));
} }
StmtList::StmtList(StmtPtr s1, StmtPtr s2, StmtPtr s3) : Stmt(STMT_LIST) StmtList::StmtList(StmtPtr s1, StmtPtr s2, StmtPtr s3) : Stmt(STMT_LIST)
{ {
stmts = new StmtPList;
if ( s1 ) if ( s1 )
stmts->append(s1.release()); stmts.push_back(std::move(s1));
if ( s2 ) if ( s2 )
stmts->append(s2.release()); stmts.push_back(std::move(s2));
if ( s3 ) if ( s3 )
stmts->append(s3.release()); stmts.push_back(std::move(s3));
} }
StmtPtr StmtList::Duplicate() StmtPtr StmtList::Duplicate()
@ -711,7 +700,7 @@ StmtPtr StmtList::Duplicate()
auto new_sl = new StmtList(); auto new_sl = new StmtList();
for ( auto& stmt : Stmts() ) for ( auto& stmt : Stmts() )
new_sl->Stmts().push_back(stmt->Duplicate().release()); new_sl->Stmts().push_back(stmt->Duplicate());
return SetSucc(new_sl); return SetSucc(new_sl);
} }
@ -724,7 +713,7 @@ void StmtList::Inline(Inliner* inl)
bool StmtList::IsReduced(Reducer* c) const bool StmtList::IsReduced(Reducer* c) const
{ {
int n = Stmts().length(); auto n = Stmts().size();
for ( auto i = 0; i < n; ++i ) for ( auto i = 0; i < n; ++i )
{ {
@ -741,10 +730,10 @@ bool StmtList::IsReduced(Reducer* c) const
StmtPtr StmtList::DoReduce(Reducer* c) StmtPtr StmtList::DoReduce(Reducer* c)
{ {
StmtPList* f_stmts = new StmtPList{}; std::vector<StmtPtr> f_stmts;
bool did_change = false; bool did_change = false;
int n = Stmts().length(); auto n = Stmts().size();
for ( auto i = 0; i < n; ++i ) for ( auto i = 0; i < n; ++i )
{ {
@ -761,31 +750,25 @@ StmtPtr StmtList::DoReduce(Reducer* c)
return ThisPtr(); return ThisPtr();
} }
if ( f_stmts->length() == 0 ) if ( f_stmts.empty() )
{
delete f_stmts;
return TransformMe(make_intrusive<NullStmt>(), c); return TransformMe(make_intrusive<NullStmt>(), c);
}
if ( f_stmts->length() == 1 ) if ( f_stmts.size() == 1 )
return (*f_stmts)[0]->Reduce(c); return f_stmts[0]->Reduce(c);
if ( did_change ) if ( did_change )
{ {
ResetStmts(f_stmts); ResetStmts(std::move(f_stmts));
return Reduce(c); return Reduce(c);
} }
else
delete f_stmts;
return ThisPtr(); return ThisPtr();
} }
bool StmtList::ReduceStmt(int& s_i, StmtPList* f_stmts, Reducer* c) bool StmtList::ReduceStmt(int& s_i, std::vector<StmtPtr>& f_stmts, Reducer* c)
{ {
bool did_change = false; bool did_change = false;
auto stmt = Stmts()[s_i]->ThisPtr(); auto stmt = Stmts()[s_i];
auto old_stmt = stmt; auto old_stmt = stmt;
stmt = stmt->Reduce(c); stmt = stmt->Reduce(c);
@ -814,7 +797,7 @@ bool StmtList::ReduceStmt(int& s_i, StmtPList* f_stmts, Reducer* c)
if ( e->Tag() != EXPR_ASSIGN ) if ( e->Tag() != EXPR_ASSIGN )
{ {
f_stmts->append(stmt.release()); f_stmts.push_back(std::move(stmt));
return false; return false;
} }
@ -823,14 +806,14 @@ bool StmtList::ReduceStmt(int& s_i, StmtPList* f_stmts, Reducer* c)
if ( lhs->Tag() != EXPR_NAME ) if ( lhs->Tag() != EXPR_NAME )
{ {
f_stmts->append(stmt.release()); f_stmts.push_back(std::move(stmt));
return false; return false;
} }
auto var = lhs->AsNameExpr(); auto var = lhs->AsNameExpr();
auto rhs = a->GetOp2(); auto rhs = a->GetOp2();
if ( s_i < Stmts().length() - 1 ) if ( s_i < Stmts().size() - 1 )
{ {
// See if we can compress an assignment chain. // See if we can compress an assignment chain.
auto& s_i_succ = Stmts()[s_i + 1]; auto& s_i_succ = Stmts()[s_i + 1];
@ -842,7 +825,7 @@ bool StmtList::ReduceStmt(int& s_i, StmtPList* f_stmts, Reducer* c)
auto merge = c->MergeStmts(var, rhs, s_i_succ); auto merge = c->MergeStmts(var, rhs, s_i_succ);
if ( merge ) if ( merge )
{ {
f_stmts->append(merge.release()); f_stmts.push_back(std::move(merge));
// Skip both this statement and the next, // Skip both this statement and the next,
// now that we've substituted the merge. // now that we've substituted the merge.
@ -864,7 +847,7 @@ bool StmtList::ReduceStmt(int& s_i, StmtPList* f_stmts, Reducer* c)
auto sl = stmt->AsStmtList(); auto sl = stmt->AsStmtList();
for ( auto& sub_stmt : sl->Stmts() ) for ( auto& sub_stmt : sl->Stmts() )
f_stmts->append(sub_stmt->Ref()); f_stmts.push_back(sub_stmt);
did_change = true; did_change = true;
} }
@ -874,10 +857,7 @@ bool StmtList::ReduceStmt(int& s_i, StmtPList* f_stmts, Reducer* c)
did_change = true; did_change = true;
else else
// No need to Ref() because the StmtPList destructor f_stmts.push_back(std::move(stmt));
// doesn't Unref(), only the explicit list-walking
// in the ~StmtList destructor.
f_stmts->append(stmt.release());
return did_change; return did_change;
} }
@ -971,7 +951,7 @@ void WhenInfo::UpdateIDs(Reducer* c)
StmtPtr WhenStmt::Duplicate() StmtPtr WhenStmt::Duplicate()
{ {
return SetSucc(new WhenStmt(new WhenInfo(wi))); return SetSucc(new WhenStmt(wi));
} }
bool WhenStmt::IsReduced(Reducer* c) const bool WhenStmt::IsReduced(Reducer* c) const

View file

@ -197,20 +197,20 @@ UDs UseDefs::PropagateUDs(const Stmt* s, UDs succ_UDs, const Stmt* succ_stmt, bo
auto sl = s->AsStmtList(); auto sl = s->AsStmtList();
const auto& stmts = sl->Stmts(); const auto& stmts = sl->Stmts();
for ( int i = stmts.length(); --i >= 0; ) for ( int i = stmts.size(); --i >= 0; )
{ {
auto s_i = stmts[i]; auto s_i = stmts[i].get();
const Stmt* succ; const Stmt* succ;
if ( i == stmts.length() - 1 ) if ( i == stmts.size() - 1 )
{ // Very last statement. { // Very last statement.
succ = succ_stmt; succ = succ_stmt;
if ( successor2.find(s) != successor2.end() ) if ( successor2.find(s) != successor2.end() )
successor2[s_i] = successor2[s]; successor2[s_i] = successor2[s];
} }
else else
succ = stmts[i + 1]; succ = stmts[i + 1].get();
succ_UDs = PropagateUDs(s_i, succ_UDs, succ, second_pass); succ_UDs = PropagateUDs(s_i, succ_UDs, succ, second_pass);
} }

View file

@ -193,7 +193,8 @@ const Stmt* ZAMCompiler::LastStmt(const Stmt* s) const
if ( s->Tag() == STMT_LIST ) if ( s->Tag() == STMT_LIST )
{ {
auto sl = s->AsStmtList()->Stmts(); auto sl = s->AsStmtList()->Stmts();
return sl[sl.length() - 1]; ASSERT(! sl.empty());
return sl.back().get();
} }
else else

View file

@ -437,9 +437,8 @@ public:
// ... and its name. // ... and its name.
std::string lambda_name; std::string lambda_name;
// For "when" statements. Needs to be non-const so we can // For "when" statements.
// Instantiate() it as needed. std::shared_ptr<WhenInfo> wi;
WhenInfo* wi;
// A parallel array for the cat() built-in replacement. // A parallel array for the cat() built-in replacement.
std::unique_ptr<CatArg>* cat_args = nullptr; std::unique_ptr<CatArg>* cat_args = nullptr;