https://gcc.gnu.org/g:70add872be783070deab8285ebb3a175d42256e3

commit r17-2632-g70add872be783070deab8285ebb3a175d42256e3
Author: James K. Lowden <[email protected]>
Date:   Wed Jul 22 17:50:47 2026 -0400

    cobol: Improve "sameness" test when deciding whether a CDF token was used.
    
    In establishing whether or not the CDF parser discarded a lookahead
    token, compare not just the last scanned token's type, but also its
    location.
    
    gcc/cobol/ChangeLog:
    
            * cdf.y (cdf::location): New function.
            * scan_post.h (same_end): New function to match token ending
            locations.
            (same_token): New function to match two tokens.

Diff:
---
 gcc/cobol/cdf.y       |  7 +++++++
 gcc/cobol/scan_post.h | 26 ++++++++++++++++++++++++--
 2 files changed, 31 insertions(+), 2 deletions(-)

diff --git a/gcc/cobol/cdf.y b/gcc/cobol/cdf.y
index f239e60ae602..9196a545b6a5 100644
--- a/gcc/cobol/cdf.y
+++ b/gcc/cobol/cdf.y
@@ -932,6 +932,13 @@ location_set( const cbl_loc_t& loc ) {
   return cdf_location = loc;
 }
 
+namespace cdf {
+    cbl_loc_t
+    location() {
+        return ::cdf_location;
+    }
+}
+
 bool // used by cobol1.cc
 defined_cmd( const char arg[] )
 {
diff --git a/gcc/cobol/scan_post.h b/gcc/cobol/scan_post.h
index 838882b44cb4..0ad0f95fcdb2 100644
--- a/gcc/cobol/scan_post.h
+++ b/gcc/cobol/scan_post.h
@@ -251,11 +251,31 @@ struct recent_token_t {
   cbl_loc_t loc;
   recent_token_t( int token, YYSTYPE value, cbl_loc_t loc )
     : token(token), value(value), loc(loc) {}
+  // for matching the current token against the token queue
+  recent_token_t( int token, cbl_loc_t loc )
+    : token(token), loc(loc) {}
 };
 #define RECENT(T) recent_token_t( (T), yylval )
 
 namespace cdf {
   int used_token();
+  cbl_loc_t location();
+}
+
+static bool
+same_end(const cbl_loc_t& a, const cbl_loc_t& b ) {
+  return a.last_line == b.last_line
+    &&   a.last_column == b.last_column;
+}
+
+/*
+ * Two tokens are one and the same if they have the type and end at the same
+ * place.  The scanner's idea of the current location might embrace more
+ * characters.
+ */
+static bool
+same_token( const recent_token_t& a, const recent_token_t& b ) {
+  return a.token == b.token && same_end(a.loc, b.loc);
 }
 
 /*
@@ -299,8 +319,9 @@ static struct recent_tokens_t : protected 
std::queue<recent_token_t>
      * the lookahead token.  Because it was the last one returned by lexer(),
      * the location is accurate.  Else return the next token.
      */
-    if( ! empty() && front().token == end_token ) pop();
-    if( ! empty() && back().token == end_token ) c.clear();
+    recent_token_t ending(end_token, cdf::location());
+    if( ! empty() && same_token(front(), ending) ) pop();
+    if( ! empty() && same_token(back(), ending) ) c.clear();
 
     end_token = 0;
 
@@ -312,6 +333,7 @@ static struct recent_tokens_t : protected 
std::queue<recent_token_t>
       }
     }
 
+    dbgmsg("%s:%d: returning %s", __func__, __LINE__, keyword_str(end_token));
     return end_token;
   }
 } recent_tokens;

Reply via email to