From 4e438bc41d29e96fcd8851f0cae21543d7e27414 Mon Sep 17 00:00:00 2001
From: "James K. Lowden" <jklowden@symas.com>
Date: Sun, 26 Jan 2025 14:36:44 -0500
Subject: [PATCH] call parser_enter/leave_file for each file

---
 gcc/cobol/scan.l     | 23 ++++++++++++++---------
 gcc/cobol/symbols.cc | 36 ------------------------------------
 gcc/cobol/symbols.h  |  3 ---
 gcc/cobol/util.cc    |  7 ++++---
 4 files changed, 18 insertions(+), 51 deletions(-)

diff --git a/gcc/cobol/scan.l b/gcc/cobol/scan.l
index 23d5b7520e9c..50691509aa93 100644
--- a/gcc/cobol/scan.l
+++ b/gcc/cobol/scan.l
@@ -2089,7 +2089,7 @@ BASIS		{ yy_push_state(basis); return BASIS; }
 }
 
 <*>{
-  {PUSH_FILE} {
+  {PUSH_FILE}	{
 			yy_set_bol(true);
 			auto top_file = cobol_lineno_save();
 			if( top_file ) {
@@ -2097,21 +2097,26 @@ BASIS		{ yy_push_state(basis); return BASIS; }
 						     yylineno, top_file);
 			}
 			// "\f#file push <name>": name starts at offset 13.
-			char *name = xstrdup(yytext);
-			name[yyleng - 1] = '\0'; // kill the trailing formfeed
-			name += 12;
+			char *filename = xstrdup(yytext);
+			filename[yyleng - 1] = '\0'; // kill the trailing formfeed
+			filename += 12;
 			if( yytext[0] != '\f' ) {
-			  dbgmsg("logic warning: name was adjusted to %s", --name);
+			  dbgmsg("logic warning: filename was adjusted to %s", --filename);
 			}
-			cobol_filename(name, 0);
+			cobol_filename(filename, 0);
 
 			if( yy_flex_debug ) dbgmsg("starting line %4d of %s",
-						  yylineno, name); }
-  {POP_FILE} {
+						   yylineno, filename);
+			parser_enter_file(filename);
+		}
+
+  {POP_FILE} 	{
 			yy_set_bol(true);
 			auto name = cobol_filename_restore(true);
 	   		if( yy_flex_debug ) dbgmsg("resuming line %4d of %s",
-						   yylineno, name? name : "<none>"); }
+						   yylineno, name? name : "<none>");
+			parser_leave_file();
+		}
 
   {LINE_DIRECTIVE} {	cobol_fileline_set(yytext); }
 }
diff --git a/gcc/cobol/symbols.cc b/gcc/cobol/symbols.cc
index d180efa86bea..557b7c3fb7d9 100644
--- a/gcc/cobol/symbols.cc
+++ b/gcc/cobol/symbols.cc
@@ -2612,42 +2612,6 @@ symbol_typedef_add( size_t program, struct cbl_field_t *field ) {
   return e;
 }
 
-struct symbol_range_t {
-  std::string filename;
-  size_t first, last;
-  symbol_range_t(const char *filename)
-    : filename(filename)
-  {
-    first = last = symbols.nelem - 1;
-  }
-  bool defines( size_t isym ) const {
-    return first <= isym && isym <= last;
-  }
-};
-
-static std::vector<symbol_range_t> cobol_filenames;
-
-void
-symbol_cobol_filename_begin( const char filename[] ) {
-  if( ! cobol_filenames.empty() ) {
-    cobol_filenames.back().last = symbols.nelem - 1;
-  }
-  symbol_range_t range(filename);
-  cobol_filenames.push_back(range);
-}
-
-#if 0
-const char *
-symbol_cobol_filename_of( size_t isym ) {
-  auto p = std::find_if( cobol_filenames.begin(), cobol_filenames.end(),
-                         [isym]( const auto& range ) {
-                           return range.defines(isym);
-                         } );
-  assert(p != cobol_filenames.end());
-  return p->filename.c_str();
-}
-#endif
-
 typedef std::map <std::string, size_t > namemap_t;
 static std::map <size_t, namemap_t > numeric_constants;
 
diff --git a/gcc/cobol/symbols.h b/gcc/cobol/symbols.h
index f51d24cf8e21..3965ea40a867 100644
--- a/gcc/cobol/symbols.h
+++ b/gcc/cobol/symbols.h
@@ -2127,9 +2127,6 @@ bool symbol_label_section_exists( size_t program );
 
 size_t symbol_field_capacity( const cbl_field_t *field );
 
-void         symbol_cobol_filename_begin( const char filename[] );
-const char * symbol_cobol_filename_of( size_t isym );
-
 size_t file_status_register();
 size_t return_code_register();
 size_t very_true_register();
diff --git a/gcc/cobol/util.cc b/gcc/cobol/util.cc
index 30191c4c9561..390868327444 100644
--- a/gcc/cobol/util.cc
+++ b/gcc/cobol/util.cc
@@ -1926,6 +1926,7 @@ class unique_stack : public std::stack<input_file_t>
   }
 };
 
+static const char *input_filename_vestige;
 static unique_stack input_filenames;
 static std::map<std::string, ino_t> old_filenames;
 static const unsigned int sysp = 0;  // not a C header file, cf. line-map.h
@@ -1953,13 +1954,13 @@ bool cobol_filename( const char *name, ino_t inode ) {
     assert(inode != 0);
     linemap_add(line_table, LC_ENTER, sysp, name, 1);
   }
+  input_filename_vestige = name;
   bool pushed = input_filenames.push( input_file_t(name, inode, 1, lines) );
   input_filenames.top().lineno = yylineno = 1;
   if( getenv(__func__) ) {
     dbgmsg("   saving %s with lineno as %d",
           input_filenames.top().name, input_filenames.top().lineno);
   }
-  symbol_cobol_filename_begin(name);
   return pushed;
 }
 
@@ -1976,7 +1977,7 @@ cobol_lineno_save() {
 
 const char *
 cobol_filename() {
-  return input_filenames.empty()? "" : input_filenames.top().name;
+  return input_filenames.empty()? input_filename_vestige : input_filenames.top().name;
 }
 
 const char *
@@ -1984,6 +1985,7 @@ cobol_filename_restore( bool scanning ) {
   assert(!input_filenames.empty());
   const input_file_t& top( input_filenames.top() );
   old_filenames[top.name] = top.inode;
+  input_filename_vestige = top.name;
   
   input_filenames.pop();
   if( input_filenames.empty() ) return NULL;
@@ -1997,7 +1999,6 @@ cobol_filename_restore( bool scanning ) {
   if( getenv("cobol_filename") ) {
     dbgmsg("restoring %s with lineno to %d", input.name, input.lineno);
   }
-  symbol_cobol_filename_begin(input.name);
   return input.name;
 }
 
-- 
GitLab