isolate Location specifics to private class variables to enforce correct line number ordering

This commit is contained in:
Vern Paxson 2025-07-03 16:41:14 -07:00 committed by Arne Welzel
parent eb6b4a0c46
commit 5c63133226
30 changed files with 172 additions and 136 deletions

View file

@ -148,7 +148,7 @@ void CPPCompile::DeclareSubclass(const FuncTypePtr& ft, const ProfileFunc* pf, c
stmts = pf->ProfiledExpr();
auto loc = stmts->GetLocationInfo();
auto loc_info = string("\"") + loc->filename + "\", " + Fmt(loc->first_line);
auto loc_info = string("\"") + loc->FileName() + "\", " + Fmt(loc->FirstLine());
Emit("%s_cl(const char* name%s) : CPPStmt(name, %s)%s { }", fname, addl_args, loc_info, inits);
// An additional constructor just used to generate place-holder

View file

@ -193,7 +193,7 @@ bool CPPCompile::AnalyzeFuncBody(FuncInfo& fi, unordered_set<string>& filenames_
const auto& f = fi.Func();
auto& body = fi.Body();
string fn = body->GetLocationInfo()->filename;
string fn = body->GetLocationInfo()->FileName();
if ( ! analysis_options.allow_cond && ! fi.ShouldSkip() ) {
if ( ! analysis_options.only_files.empty() && files_with_conditionals.count(fn) > 0 ) {
@ -358,7 +358,7 @@ void CPPCompile::RegisterCompiledBody(const string& f) {
auto h = body_hashes[f];
auto p = body_priorities[f];
auto loc = body_locs[f];
auto body_info = Fmt(p) + ", " + Fmt(h) + ", \"" + loc->filename + " (C++)\", " + Fmt(loc->first_line);
auto body_info = Fmt(p) + ", " + Fmt(h) + ", \"" + loc->FileName() + " (C++)\", " + Fmt(loc->FirstLine());
Emit("\tCPP_RegisterBody(\"%s\", (void*) %s, %s, %s, std::vector<std::string>(%s)),", f, f, Fmt(type_signature),
body_info, events);

View file

@ -182,8 +182,8 @@ string CPPCompile::BodyName(const FuncInfo& func) {
// Extend name with location information.
auto loc = body->GetLocationInfo();
if ( loc->filename ) {
auto fn = loc->filename;
if ( loc->FileName() ) {
auto fn = loc->FileName();
// Skip leading goop that gets added by search paths.
while ( *fn == '.' || *fn == '/' )

View file

@ -11,7 +11,7 @@ using namespace std;
void CPPCompile::GenStmt(const Stmt* s) {
auto loc = s->GetLocationInfo();
if ( loc != &detail::no_location && s->Tag() != STMT_LIST )
Emit("// %s:%s", loc->filename, to_string(loc->first_line));
Emit("// %s:%d", loc->FileName(), loc->FirstLine());
switch ( s->Tag() ) {
case STMT_INIT: GenInitStmt(s->AsInitStmt()); break;
@ -498,8 +498,8 @@ void CPPCompile::GenAssertStmt(const AssertStmt* a) {
Emit("auto msg_val = zeek::val_mgr->EmptyString();");
auto loc = a->GetLocationInfo();
Emit("static Location loc(\"%s\", %s, %s, %s, %s);", loc->filename, std::to_string(loc->first_line),
std::to_string(loc->last_line), std::to_string(loc->first_column), std::to_string(loc->last_column));
Emit("static Location loc(\"%s\", %s, %s, %s, %s);", loc->FileName(), std::to_string(loc->FirstLine()),
std::to_string(loc->LastLine()), std::to_string(loc->FirstColumn()), std::to_string(loc->LastColumn()));
Emit("report_assert(assert_result, \"%s\", msg_val, &loc);", CPPEscape(a->CondDesc().c_str()).c_str());
EndBlock();

View file

@ -205,7 +205,7 @@ shared_ptr<CPP_InitInfo> CPPCompile::RegisterType(const TypePtr& tp) {
auto tr = tp->AsRecordType();
for ( auto i = tr->NumOrigFields(); i < tr->NumFields(); ++i ) {
auto fd = tr->FieldDecl(i);
if ( filename_matches_opt_files(fd->GetLocationInfo()->filename) ) {
if ( filename_matches_opt_files(fd->GetLocationInfo()->FileName()) ) {
if ( addl_fields == 0 )
addl_fields = i;
}

View file

@ -1450,12 +1450,12 @@ static std::unordered_map<std::string, std::string> filename_module;
void switch_to_module(const char* module_name) {
auto loc = GetCurrentLocation();
if ( loc.first_line != 0 && filename_module.count(loc.filename) == 0 )
filename_module[loc.filename] = module_name;
if ( loc.FirstLine() != 0 && filename_module.count(loc.FileName()) == 0 )
filename_module[loc.FileName()] = module_name;
}
std::string func_name_at_loc(std::string fname, const Location* loc) {
auto find_module = filename_module.find(loc->filename);
auto find_module = filename_module.find(loc->FileName());
if ( find_module == filename_module.end() )
// No associated module.
return fname;
@ -1476,15 +1476,14 @@ std::string func_name_at_loc(std::string fname, const Location* loc) {
TraversalCode SetBlockLineNumbers::PreStmt(const Stmt* s) {
auto loc = const_cast<Location*>(s->GetLocationInfo());
UpdateLocInfo(loc);
block_line_range.emplace_back(loc->first_line, loc->last_line);
block_line_range.emplace_back(loc->FirstLine(), loc->LastLine());
return TC_CONTINUE;
}
TraversalCode SetBlockLineNumbers::PostStmt(const Stmt* s) {
auto loc = const_cast<Location*>(s->GetLocationInfo());
auto r = block_line_range.back();
loc->first_line = r.first;
loc->last_line = r.second;
loc->SetLines(r.first, r.second);
block_line_range.pop_back();
@ -1505,12 +1504,8 @@ TraversalCode SetBlockLineNumbers::PreExpr(const Expr* e) {
}
void SetBlockLineNumbers::UpdateLocInfo(Location* loc) {
// Sometimes locations are generated with inverted line coverage.
if ( loc->first_line > loc->last_line )
std::swap(loc->first_line, loc->last_line);
auto first_line = loc->first_line;
auto last_line = loc->last_line;
auto first_line = loc->FirstLine();
auto last_line = loc->LastLine();
if ( ! block_line_range.empty() ) {
auto& r = block_line_range.back();
@ -1573,7 +1568,7 @@ TraversalCode ASTBlockAnalyzer::PreExpr(const Expr* e) {
}
std::string ASTBlockAnalyzer::BuildExpandedDescription(const Location* loc) {
ASSERT(loc && loc->first_line != 0);
ASSERT(loc && loc->FirstLine() != 0);
auto ls = LocWithFunc(loc);
if ( ! parents.empty() ) {

View file

@ -686,17 +686,17 @@ private:
// Return the key used to associate a Location object with its full
// descriptiion.
std::string LocKey(const Location* loc) const {
return std::string(loc->filename) + ":" + std::to_string(loc->first_line) + "-" +
std::to_string(loc->last_line);
return std::string(loc->FileName()) + ":" + std::to_string(loc->FirstLine()) + "-" +
std::to_string(loc->LastLine());
}
// Return the description of a location including its the function
// in which it's embedded.
std::string LocWithFunc(const Location* loc) const {
auto res = func_name_prefix + std::to_string(loc->first_line);
auto res = func_name_prefix + std::to_string(loc->FirstLine());
if ( loc->first_line != loc->last_line )
res += "-" + std::to_string(loc->last_line);
if ( loc->FirstLine() != loc->LastLine() )
res += "-" + std::to_string(loc->LastLine());
return res;
}

View file

@ -139,7 +139,7 @@ bool filename_matches_opt_files(const char* filename) {
return false;
}
bool obj_matches_opt_files(const Obj* obj) { return filename_matches_opt_files(obj->GetLocationInfo()->filename); }
bool obj_matches_opt_files(const Obj* obj) { return filename_matches_opt_files(obj->GetLocationInfo()->FileName()); }
static bool optimize_AST(ScriptFuncPtr f, std::shared_ptr<ProfileFunc>& pf, std::shared_ptr<Reducer>& rc,
ScopePtr scope, StmtPtr& body) {

View file

@ -22,9 +22,9 @@ ZAMCompiler::ZAMCompiler(ScriptFuncPtr f, std::shared_ptr<ProfileFuncs> _pfs, st
frame_sizeI = 0;
auto loc = body->GetLocationInfo();
ASSERT(loc->first_line != 0 || body->Tag() == STMT_NULL);
auto loc_copy =
std::make_shared<Location>(loc->filename, loc->first_line, loc->last_line, loc->first_column, loc->last_column);
ASSERT(loc->FirstLine() != 0 || body->Tag() == STMT_NULL);
auto loc_copy = std::make_shared<Location>(loc->FileName(), loc->FirstLine(), loc->LastLine(), loc->FirstColumn(),
loc->LastColumn());
ZAM::curr_func = func->GetName();
ZAM::curr_loc = std::make_shared<ZAMLocInfo>(ZAM::curr_func, std::move(loc_copy), nullptr);

View file

@ -39,9 +39,9 @@ std::string ZAMLocInfo::Describe(bool include_lines) const {
desc = func_name;
if ( include_lines ) {
desc += ";" + func_name + ":" + std::to_string(loc->first_line);
if ( loc->last_line > loc->first_line )
desc += "-" + std::to_string(loc->last_line);
desc += ";" + func_name + ":" + std::to_string(loc->FirstLine());
if ( loc->LastLine() > loc->FirstLine() )
desc += "-" + std::to_string(loc->LastLine());
}
}

View file

@ -12,9 +12,9 @@ namespace zeek::detail {
const ZAMStmt ZAMCompiler::CompileStmt(const Stmt* s) {
auto loc = s->GetLocationInfo();
ASSERT(loc->first_line != 0 || s->Tag() == STMT_NULL);
auto loc_copy =
std::make_shared<Location>(loc->filename, loc->first_line, loc->last_line, loc->first_column, loc->last_column);
ASSERT(loc->FirstLine() != 0 || s->Tag() == STMT_NULL);
auto loc_copy = std::make_shared<Location>(loc->FileName(), loc->FirstLine(), loc->LastLine(), loc->FirstColumn(),
loc->LastColumn());
ASSERT(! AST_blocks || s->Tag() == STMT_NULL || AST_blocks->HaveExpDesc(loc_copy.get()));
auto loc_parent = ZAM::curr_loc->Parent();
ZAM::curr_loc = std::make_shared<ZAMLocInfo>(ZAM::curr_func, std::move(loc_copy), ZAM::curr_loc->Parent());