[gcc r17-2279] cobol: prevent invalid TYPE reference, and define YYLOCATION_PRINT

"James K. Lowden via Gcc-cvs" <[email protected]>
Newsgroups gmane.comp.gcc.cvs
Message-ID <[email protected]>
https://gcc.gnu.org/g:3d8093d7cb7acb98288657ae34a9c99f8fd5a14c

commit r17-2279-g3d8093d7cb7acb98288657ae34a9c99f8fd5a14c
Author: James K. Lowden <[email protected]>
Date:   Thu Jul 9 10:42:42 2026 -0400

    cobol: prevent invalid TYPE reference, and define YYLOCATION_PRINT
    
    gcc/cobol/ChangeLog:
    
            PR cobol/126105
            * parse.y: Remove unnecessary test and improve diagnostic message.
            * parse_ante.h (yylocation_print): New function.
            (YYLOCATION_PRINT): Define, because user-defined cbl_loc_t location.
            * symbols.cc (symbol_typedef): Exclude current field from type search.
            * symbols.h (symbol_typedef): Delete unused overload.

Diff:
---
 gcc/cobol/parse.y      | 16 +++++++---------
 gcc/cobol/parse_ante.h |  9 +++++++++
 gcc/cobol/symbols.cc   |  2 +-
 gcc/cobol/symbols.h    |  1 -
 4 files changed, 17 insertions(+), 11 deletions(-)

diff --git a/gcc/cobol/parse.y b/gcc/cobol/parse.y
index 2d9263d983c5..f7a2be95a0ab 100644
--- a/gcc/cobol/parse.y
+++ b/gcc/cobol/parse.y
@@ -5468,19 +5468,17 @@ sign_separate:  %empty              { $$ = false; }
 type_clause: TYPE to typename
                 {
                   cbl_field_t *field = current_field();
-                  if( $typename ) {
-                    const auto e = symbol_field_same_as(field, $typename);
-		    symbol_field_location( symbol_index(e), @typename );
-                  }
+                  assert( $typename );
+                  const auto e = symbol_field_same_as(field, $typename);
+                  symbol_field_location( symbol_index(e), @typename );
                 }
         |       USAGE is typename
                 {
                   dialect_ok(@typename, MfUsageTypename, "USAGE TYPENAME");
                   cbl_field_t *field = current_field();
-                  if( $typename ) {
-                    const auto e = symbol_field_same_as(field, $typename);
-		    symbol_field_location( symbol_index(e), @typename );
-                  }
+                  assert( $typename );
+                  const auto e = symbol_field_same_as(field, $typename);
+                  symbol_field_location( symbol_index(e), @typename );
                 }
                 ;
 
@@ -7415,7 +7413,7 @@ typename:       NAME
                 {
                   auto e = symbol_typedef(PROGRAM, $NAME);
                   if( ! e ) {
-		    error_msg(@1, "DATA-ITEM '%s' not found", $NAME );
+		    error_msg(@1, "TYPE %qs not found", $NAME );
                     YYERROR;
                   }
                   $$ = cbl_field_of(e);
diff --git a/gcc/cobol/parse_ante.h b/gcc/cobol/parse_ante.h
index 01d546cb85ae..b0b062f80dc6 100644
--- a/gcc/cobol/parse_ante.h
+++ b/gcc/cobol/parse_ante.h
@@ -129,6 +129,15 @@ static bool successful_parse() {
 
 void input_file_status_notify();
 
+static void
+yylocation_print(FILE* file, const cbl_loc_t& loc) {
+  fprintf(file, "%d.%d-%d.%d", 
+          loc.first_line, loc.first_column, 
+          loc.last_line, loc.last_column);
+}
+
+#define YYLOCATION_PRINT(File, Loc) yylocation_print(File, *Loc)
+
 #define YYLLOC_DEFAULT(Current, Rhs, N)                                 \
   do {                                                                  \
       if (N)                                                            \
diff --git a/gcc/cobol/symbols.cc b/gcc/cobol/symbols.cc
index 951eac6a955d..d11bcb25ae97 100644
--- a/gcc/cobol/symbols.cc
+++ b/gcc/cobol/symbols.cc
@@ -2893,7 +2893,7 @@ symbol_typedef( size_t program, const char name[] )
   auto beg = std::reverse_iterator<symbol_elem_t *>(symbols_end());
   auto end = std::reverse_iterator<symbol_elem_t *>(symbols_begin(program));
 
-  auto p = std::find_if( beg, end,
+  auto p = std::find_if( ++beg, end,
                          [name]( const symbol_elem_t& sym ) {
                            if( sym.type == SymField ) {
                              auto f = cbl_field_of(&sym);
diff --git a/gcc/cobol/symbols.h b/gcc/cobol/symbols.h
index 3744b3a7f616..fe167c90df4a 100644
--- a/gcc/cobol/symbols.h
+++ b/gcc/cobol/symbols.h
@@ -2847,7 +2847,6 @@ cbl_namelist_t teed_up_names();
 
 size_t end_of_group( size_t igroup );
 
-symbol_elem_t * symbol_typedef( size_t program, std::list<const char *> names );
 symbol_elem_t * symbol_typedef( size_t program, const char name[] );
 symbol_elem_t * symbol_field( size_t program, size_t parent, const char name[] );
 cbl_label_t *   symbol_label( size_t program, cbl_label_type_t type,
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.