Reformat the world

This commit is contained in:
Tim Wojtulewicz 2021-09-16 15:35:39 -07:00
parent 194cb24547
commit b2f171ec69
714 changed files with 35149 additions and 35203 deletions

View file

@ -1,21 +1,20 @@
// See the file "COPYING" in the main distribution directory for copyright.
#include "zeek/Expr.h"
#include "zeek/Scope.h"
#include "zeek/Reporter.h"
#include "zeek/Desc.h"
#include "zeek/script_opt/GenIDDefs.h"
#include "zeek/script_opt/ScriptOpt.h"
#include "zeek/Desc.h"
#include "zeek/Expr.h"
#include "zeek/Reporter.h"
#include "zeek/Scope.h"
#include "zeek/script_opt/ExprOptInfo.h"
#include "zeek/script_opt/ScriptOpt.h"
#include "zeek/script_opt/StmtOptInfo.h"
namespace zeek::detail
{
namespace zeek::detail {
GenIDDefs::GenIDDefs(std::shared_ptr<ProfileFunc> _pf, const Func* f,
ScopePtr scope, StmtPtr body)
: pf(std::move(_pf))
GenIDDefs::GenIDDefs(std::shared_ptr<ProfileFunc> _pf, const Func* f, ScopePtr scope, StmtPtr body)
: pf(std::move(_pf))
{
TraverseFunction(f, scope, body);
}
@ -52,7 +51,7 @@ void GenIDDefs::TraverseFunction(const Func* f, ScopePtr scope, StmtPtr body)
TrackID(a);
}
stmt_num = 0; // 0 = "before the first statement"
stmt_num = 0; // 0 = "before the first statement"
body->Traverse(this);
}
@ -65,201 +64,203 @@ TraversalCode GenIDDefs::PreStmt(const Stmt* s)
si->stmt_num = ++stmt_num;
si->block_level = confluence_blocks.size() + 1;
switch ( s->Tag() ) {
case STMT_CATCH_RETURN:
switch ( s->Tag() )
{
auto cr = s->AsCatchReturnStmt();
auto block = cr->Block();
StartConfluenceBlock(s);
block->Traverse(this);
EndConfluenceBlock();
auto retvar = cr->RetVar();
if ( retvar )
TrackID(retvar->Id());
return TC_ABORTSTMT;
}
case STMT_IF:
{
auto i = s->AsIfStmt();
auto cond = i->StmtExpr();
auto t_branch = i->TrueBranch();
auto f_branch = i->FalseBranch();
cond->Traverse(this);
StartConfluenceBlock(s);
t_branch->Traverse(this);
if ( ! t_branch->NoFlowAfter(false) )
BranchBeyond(curr_stmt, s, true);
f_branch->Traverse(this);
if ( ! f_branch->NoFlowAfter(false) )
BranchBeyond(curr_stmt, s, true);
EndConfluenceBlock(true);
return TC_ABORTSTMT;
}
case STMT_SWITCH:
{
auto sw = s->AsSwitchStmt();
auto e = sw->StmtExpr();
e->Traverse(this);
StartConfluenceBlock(sw);
for ( const auto& c : *sw->Cases() )
{
auto body = c->Body();
auto exprs = c->ExprCases();
if ( exprs )
exprs->Traverse(this);
auto type_ids = c->TypeCases();
if ( type_ids )
case STMT_CATCH_RETURN:
{
for ( const auto& id : *type_ids )
if ( id->Name() )
TrackID(id);
auto cr = s->AsCatchReturnStmt();
auto block = cr->Block();
StartConfluenceBlock(s);
block->Traverse(this);
EndConfluenceBlock();
auto retvar = cr->RetVar();
if ( retvar )
TrackID(retvar->Id());
return TC_ABORTSTMT;
}
body->Traverse(this);
}
case STMT_IF:
{
auto i = s->AsIfStmt();
auto cond = i->StmtExpr();
auto t_branch = i->TrueBranch();
auto f_branch = i->FalseBranch();
EndConfluenceBlock(sw->HasDefault());
cond->Traverse(this);
return TC_ABORTSTMT;
StartConfluenceBlock(s);
t_branch->Traverse(this);
if ( ! t_branch->NoFlowAfter(false) )
BranchBeyond(curr_stmt, s, true);
f_branch->Traverse(this);
if ( ! f_branch->NoFlowAfter(false) )
BranchBeyond(curr_stmt, s, true);
EndConfluenceBlock(true);
return TC_ABORTSTMT;
}
case STMT_SWITCH:
{
auto sw = s->AsSwitchStmt();
auto e = sw->StmtExpr();
e->Traverse(this);
StartConfluenceBlock(sw);
for ( const auto& c : *sw->Cases() )
{
auto body = c->Body();
auto exprs = c->ExprCases();
if ( exprs )
exprs->Traverse(this);
auto type_ids = c->TypeCases();
if ( type_ids )
{
for ( const auto& id : *type_ids )
if ( id->Name() )
TrackID(id);
}
body->Traverse(this);
}
EndConfluenceBlock(sw->HasDefault());
return TC_ABORTSTMT;
}
case STMT_FOR:
{
auto f = s->AsForStmt();
auto ids = f->LoopVars();
auto e = f->LoopExpr();
auto body = f->LoopBody();
auto val_var = f->ValueVar();
e->Traverse(this);
for ( const auto& id : *ids )
TrackID(id);
if ( val_var )
TrackID(val_var);
StartConfluenceBlock(s);
body->Traverse(this);
if ( ! body->NoFlowAfter(false) )
BranchBackTo(curr_stmt, s, true);
EndConfluenceBlock();
return TC_ABORTSTMT;
}
case STMT_WHILE:
{
auto w = s->AsWhileStmt();
StartConfluenceBlock(s);
auto cond_pred_stmt = w->CondPredStmt();
if ( cond_pred_stmt )
cond_pred_stmt->Traverse(this);
// Important to traverse the condition in its version
// interpreted as a statement, so that when evaluating
// its variable usage, that's done in the context of
// *after* cond_pred_stmt executes, rather than as
// part of that execution.
auto cond_stmt = w->ConditionAsStmt();
cond_stmt->Traverse(this);
auto body = w->Body();
body->Traverse(this);
if ( ! body->NoFlowAfter(false) )
BranchBackTo(curr_stmt, s, true);
EndConfluenceBlock();
return TC_ABORTSTMT;
}
case STMT_WHEN:
{
// ### punt on these for now, need to reflect on bindings.
return TC_ABORTSTMT;
}
default:
return TC_CONTINUE;
}
case STMT_FOR:
{
auto f = s->AsForStmt();
auto ids = f->LoopVars();
auto e = f->LoopExpr();
auto body = f->LoopBody();
auto val_var = f->ValueVar();
e->Traverse(this);
for ( const auto& id : *ids )
TrackID(id);
if ( val_var )
TrackID(val_var);
StartConfluenceBlock(s);
body->Traverse(this);
if ( ! body->NoFlowAfter(false) )
BranchBackTo(curr_stmt, s, true);
EndConfluenceBlock();
return TC_ABORTSTMT;
}
case STMT_WHILE:
{
auto w = s->AsWhileStmt();
StartConfluenceBlock(s);
auto cond_pred_stmt = w->CondPredStmt();
if ( cond_pred_stmt )
cond_pred_stmt->Traverse(this);
// Important to traverse the condition in its version
// interpreted as a statement, so that when evaluating
// its variable usage, that's done in the context of
// *after* cond_pred_stmt executes, rather than as
// part of that execution.
auto cond_stmt = w->ConditionAsStmt();
cond_stmt->Traverse(this);
auto body = w->Body();
body->Traverse(this);
if ( ! body->NoFlowAfter(false) )
BranchBackTo(curr_stmt, s, true);
EndConfluenceBlock();
return TC_ABORTSTMT;
}
case STMT_WHEN:
{
// ### punt on these for now, need to reflect on bindings.
return TC_ABORTSTMT;
}
default:
return TC_CONTINUE;
}
}
TraversalCode GenIDDefs::PostStmt(const Stmt* s)
{
switch ( s->Tag() ) {
case STMT_INIT:
switch ( s->Tag() )
{
auto init = s->AsInitStmt();
auto& inits = init->Inits();
case STMT_INIT:
{
auto init = s->AsInitStmt();
auto& inits = init->Inits();
for ( const auto& id : inits )
{
auto id_t = id->GetType();
for ( const auto& id : inits )
{
auto id_t = id->GetType();
// Only aggregates get initialized.
if ( zeek::IsAggr(id->GetType()->Tag()) )
TrackID(id);
}
// Only aggregates get initialized.
if ( zeek::IsAggr(id->GetType()->Tag()) )
TrackID(id);
}
break;
}
break;
}
case STMT_RETURN:
ReturnAt(s);
break;
case STMT_NEXT:
BranchBackTo(curr_stmt, FindLoop(), false);
break;
case STMT_BREAK:
{
auto target = FindBreakTarget();
if ( target )
BranchBeyond(s, target, false);
else
{
ASSERT(func_flavor == FUNC_FLAVOR_HOOK);
case STMT_RETURN:
ReturnAt(s);
}
break;
break;
case STMT_NEXT:
BranchBackTo(curr_stmt, FindLoop(), false);
break;
case STMT_BREAK:
{
auto target = FindBreakTarget();
if ( target )
BranchBeyond(s, target, false);
else
{
ASSERT(func_flavor == FUNC_FLAVOR_HOOK);
ReturnAt(s);
}
break;
}
case STMT_FALLTHROUGH:
// No need to do anything, the work all occurs
// with NoFlowAfter.
break;
default:
break;
}
case STMT_FALLTHROUGH:
// No need to do anything, the work all occurs
// with NoFlowAfter.
break;
default:
break;
}
return TC_CONTINUE;
}
@ -267,68 +268,68 @@ TraversalCode GenIDDefs::PreExpr(const Expr* e)
{
e->GetOptInfo()->stmt_num = stmt_num;
switch ( e->Tag() ) {
case EXPR_NAME:
CheckVarUsage(e, e->AsNameExpr()->Id());
break;
case EXPR_ASSIGN:
switch ( e->Tag() )
{
auto lhs = e->GetOp1();
auto op2 = e->GetOp2();
case EXPR_NAME:
CheckVarUsage(e, e->AsNameExpr()->Id());
break;
if ( lhs->Tag() == EXPR_LIST &&
op2->GetType()->Tag() != TYPE_ANY )
{
// This combination occurs only for assignments used
// to initialize table entries. Treat it as references
// to both the lhs and the rhs, not as an assignment.
return TC_CONTINUE;
}
case EXPR_ASSIGN:
{
auto lhs = e->GetOp1();
auto op2 = e->GetOp2();
op2->Traverse(this);
if ( lhs->Tag() == EXPR_LIST && op2->GetType()->Tag() != TYPE_ANY )
{
// This combination occurs only for assignments used
// to initialize table entries. Treat it as references
// to both the lhs and the rhs, not as an assignment.
return TC_CONTINUE;
}
if ( ! CheckLHS(lhs, op2) )
// Not a simple assignment (or group of assignments),
// so analyze the accesses to check for use of
// possibly undefined values.
lhs->Traverse(this);
op2->Traverse(this);
return TC_ABORTSTMT;
if ( ! CheckLHS(lhs, op2) )
// Not a simple assignment (or group of assignments),
// so analyze the accesses to check for use of
// possibly undefined values.
lhs->Traverse(this);
return TC_ABORTSTMT;
}
case EXPR_COND:
// Special hack. We turn off checking for usage issues
// inside conditionals. This is because we use them heavily
// to deconstruct logical expressions for which the actual
// operand access is safe (guaranteed not to access a value
// that hasn't been undefined), but the flow analysis has
// trouble determining that.
++suppress_usage;
e->GetOp1()->Traverse(this);
e->GetOp2()->Traverse(this);
e->GetOp3()->Traverse(this);
--suppress_usage;
return TC_ABORTSTMT;
case EXPR_LAMBDA:
{
auto l = static_cast<const LambdaExpr*>(e);
const auto& ids = l->OuterIDs();
for ( auto& id : ids )
CheckVarUsage(e, id);
// Don't descend into the lambda body - we'll analyze and
// optimize it separately, as its own function.
return TC_ABORTSTMT;
}
default:
break;
}
case EXPR_COND:
// Special hack. We turn off checking for usage issues
// inside conditionals. This is because we use them heavily
// to deconstruct logical expressions for which the actual
// operand access is safe (guaranteed not to access a value
// that hasn't been undefined), but the flow analysis has
// trouble determining that.
++suppress_usage;
e->GetOp1()->Traverse(this);
e->GetOp2()->Traverse(this);
e->GetOp3()->Traverse(this);
--suppress_usage;
return TC_ABORTSTMT;
case EXPR_LAMBDA:
{
auto l = static_cast<const LambdaExpr*>(e);
const auto& ids = l->OuterIDs();
for ( auto& id : ids )
CheckVarUsage(e, id);
// Don't descend into the lambda body - we'll analyze and
// optimize it separately, as its own function.
return TC_ABORTSTMT;
}
default:
break;
}
return TC_CONTINUE;
}
@ -341,12 +342,11 @@ TraversalCode GenIDDefs::PostExpr(const Expr* e)
// of their operands).
auto t = e->Tag();
if ( t == EXPR_INCR || t == EXPR_DECR ||
t == EXPR_ADD_TO || t == EXPR_REMOVE_FROM )
if ( t == EXPR_INCR || t == EXPR_DECR || t == EXPR_ADD_TO || t == EXPR_REMOVE_FROM )
{
auto op = e->GetOp1();
if ( ! IsAggr(op) )
(void) CheckLHS(op);
(void)CheckLHS(op);
}
return TC_CONTINUE;
@ -354,47 +354,48 @@ TraversalCode GenIDDefs::PostExpr(const Expr* e)
bool GenIDDefs::CheckLHS(const ExprPtr& lhs, const ExprPtr& rhs)
{
switch ( lhs->Tag() ) {
case EXPR_REF:
return CheckLHS(lhs->GetOp1(), rhs);
case EXPR_NAME:
switch ( lhs->Tag() )
{
auto n = lhs->AsNameExpr();
TrackID(n->Id(), rhs);
return true;
case EXPR_REF:
return CheckLHS(lhs->GetOp1(), rhs);
case EXPR_NAME:
{
auto n = lhs->AsNameExpr();
TrackID(n->Id(), rhs);
return true;
}
case EXPR_LIST:
{ // look for [a, b, c] = any_val
auto l = lhs->AsListExpr();
for ( const auto& expr : l->Exprs() )
{
if ( expr->Tag() != EXPR_NAME )
// This will happen for table initializers,
// for example.
return false;
auto n = expr->AsNameExpr();
TrackID(n->Id());
}
return true;
}
case EXPR_FIELD:
// If we want to track record field initializations,
// we'd handle that here.
return false;
case EXPR_INDEX:
// If we wanted to track potential alterations of
// aggregates, we'd do that here.
return false;
default:
reporter->InternalError("bad tag in GenIDDefs::CheckLHS");
}
case EXPR_LIST:
{ // look for [a, b, c] = any_val
auto l = lhs->AsListExpr();
for ( const auto& expr : l->Exprs() )
{
if ( expr->Tag() != EXPR_NAME )
// This will happen for table initializers,
// for example.
return false;
auto n = expr->AsNameExpr();
TrackID(n->Id());
}
return true;
}
case EXPR_FIELD:
// If we want to track record field initializations,
// we'd handle that here.
return false;
case EXPR_INDEX:
// If we wanted to track potential alterations of
// aggregates, we'd do that here.
return false;
default:
reporter->InternalError("bad tag in GenIDDefs::CheckLHS");
}
}
bool GenIDDefs::IsAggr(const Expr* e) const
@ -411,8 +412,7 @@ bool GenIDDefs::IsAggr(const Expr* e) const
void GenIDDefs::CheckVarUsage(const Expr* e, const ID* id)
{
if ( analysis_options.usage_issues != 1 || id->IsGlobal() ||
suppress_usage > 0 )
if ( analysis_options.usage_issues != 1 || id->IsGlobal() || suppress_usage > 0 )
return;
auto oi = id->GetOptInfo();
@ -515,19 +515,18 @@ void GenIDDefs::TrackID(const ID* id, const ExprPtr& e)
auto oi = id->GetOptInfo();
ASSERT(! barrier_blocks.empty());
oi->DefinedAfter(curr_stmt, e,
confluence_blocks, barrier_blocks.back());
oi->DefinedAfter(curr_stmt, e, confluence_blocks, barrier_blocks.back());
// Ensure we track this identifier across all relevant
// confluence regions.
for ( auto i = barrier_blocks.back(); i < confluence_blocks.size(); ++i )
// Add one because modified_IDs includes outer non-confluence
// block.
modified_IDs[i+1].insert(id);
modified_IDs[i + 1].insert(id);
if ( confluence_blocks.empty() )
// This is a definition at the outermost level.
modified_IDs[0].insert(id);
}
} // zeek::detail
} // zeek::detail