https://gcc.gnu.org/g:3e7dd06860bf0589bc743e458b16bd1d33d73835

commit r16-2192-g3e7dd06860bf0589bc743e458b16bd1d33d73835
Author: Rainer Orth <r...@cebitec.uni-bielefeld.de>
Date:   Fri Jul 11 09:56:18 2025 +0200

    cobol: Fix build on 32-bit Darwin [PR120621]
    
    Bootstrapping trunk with 32-bit-default on Mac OS X 10.11
    (i386-apple-darwin15) fails:
    
    /vol/gcc/src/hg/master/local/gcc/cobol/lexio.cc: In static member function 
'static void cdftext::process_file(filespan_t, int, bool)':
    /vol/gcc/src/hg/master/local/gcc/cobol/lexio.cc:1859:14: error: format '%u' 
expects argument of type 'unsigned int', but argument 4 has type 'size_t' {aka 
'long unsigned int'} [-Werror=format=]
     1859 |       dbgmsg("%s:%d: line " HOST_SIZE_T_PRINT_UNSIGNED ", opening 
%s on fd %d",
          |              
^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
     1860 |              __func__, __LINE__,mfile.lineno(),
          |                                 ~~~~~~~~~~~~~~
          |                                             |
          |                                             size_t {aka long 
unsigned int}
    In file included from /vol/gcc/src/hg/master/local/gcc/system.h:1244,
                     from 
/vol/gcc/src/hg/master/local/gcc/cobol/cobol-system.h:61,
                     from /vol/gcc/src/hg/master/local/gcc/cobol/lexio.cc:33:
    /vol/gcc/src/hg/master/local/gcc/hwint.h:135:51: note: format string is 
defined here
      135 | #define HOST_SIZE_T_PRINT_UNSIGNED "%" GCC_PRISZ "u"
          |                                     ~~~~~~~~~~~~~~^
          |                                                   |
          |                                                   unsigned int
          |                                     %" GCC_PRISZ "lu
    
    On Darwin, size_t is always long unsigned int.  However, unsigned int
    and long unsigned int are both 32-bit, so hwint.h selects %u for the
    format.  As documented there, the arg needs to be cast to fmt_size_t to
    avoid the error.
    
    This isn't an issue on other 32-bit platforms like Solaris/i386 or
    Linux/i686 since they use unsigned int for size_t.
    
    /vol/gcc/src/hg/master/local/gcc/cobol/parse.y: In function 'int yyparse()':
    /vol/gcc/src/hg/master/local/gcc/cobol/parse.y:10215:36: error: format 
'%zu' expects argument of type 'size_t', but argument 4 has type 'int' 
[-Werror=format=]
    10215 |                     error_msg(loc, "FUNCTION %qs has "
          |                                    ^~~~~~~~~~~~~~~~~~~
    10216 |                               "inconsistent parameter type %zu 
(%qs)",
          |                               
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    10217 |                               keyword_str($1), p - args.data(), 
name_of(p->field) );
          |                                                                
~~~~~~~~~~~~~~~
          |                                                                  |
          |                                                                  int
    
    The arg (p - args.data())) is ptrdiff_t (int on 32-bit Darwin), while
    the %zu format expect size_t (long unsigned int).  The patch therefore
    casts the ptrdiff_t arg to long and prints it as such.
    
    There are two more instances of the same problem:
    
    /vol/gcc/src/hg/master/local/gcc/cobol/util.cc: In member function 'void 
cbl_field_t::report_invalid_initial_value(const YYLTYPE&) const':
    /vol/gcc/src/hg/master/local/gcc/cobol/util.cc:905:80: error: format '%zu' 
expects argument of type 'size_t', but argument 6 has type 'int' 
[-Werror=format=]
      905 |                 error_msg(loc, "%s cannot represent VALUE %qs 
exactly (max %c%zu)",
          |                                                                     
         ~~^
          |                                                                     
           |
          |                                                                     
           long unsigned int
          |                                                                     
         %u
      906 |                           name, data.initial, '.', pend - p);
          |                                                    ~~~~~~~~
          |                                                         |
          |                                                         int
    
    In file included from /vol/gcc/src/hg/master/local/gcc/cobol/scan.l:48:
    /vol/gcc/src/hg/master/local/gcc/cobol/scan_ante.h: In function 'int 
numstr_of(const char*, radix_t)':
    /vol/gcc/src/hg/master/local/gcc/cobol/scan_ante.h:152:25: error: format 
'%zu' expects argument of type 'size_t', but argument 4 has type 'int' 
[-Werror=format=]
      152 |       error_msg(yylloc, "significand of %s has more than 36 digits 
(%zu)", input, nx);
          |                         
^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~         ~~
          |                                                                     
              |
          |                                                                     
              int
    
    Fixed in the same way.
    
    Bootstrapped without regressions on i386-apple-darwin15,
    x86_64-apple-darwin, i386-pc-solaris2.11, amd64-pc-solaris2.11,
    i686-pc-linux-gnu, and x86_64-pc-linux-gnu.
    
    2025-06-23  Rainer Orth  <r...@cebitec.uni-bielefeld.de>
    
            gcc/cobol:
            PR cobol/120621
            * lexio.cc (parse_replace_pairs): Cast mfile.lineno() to fmt_size_t.
            * parse.y (intrinsic): Print ptrdiff_t using %ld, cast arg to long.
            * scan_ante.h (numstr_of): Print nx using %ld, cast arg to long.
            * util.cc (cbl_field_t::report_invalid_initial_value): Print
            ptrdiff_t using %ld, cast arg to long.

Diff:
---
 gcc/cobol/lexio.cc    | 2 +-
 gcc/cobol/parse.y     | 4 ++--
 gcc/cobol/scan_ante.h | 2 +-
 gcc/cobol/util.cc     | 4 ++--
 4 files changed, 6 insertions(+), 6 deletions(-)

diff --git a/gcc/cobol/lexio.cc b/gcc/cobol/lexio.cc
index 0aebe52ada63..dc632c2ecd8f 100644
--- a/gcc/cobol/lexio.cc
+++ b/gcc/cobol/lexio.cc
@@ -1904,7 +1904,7 @@ cdftext::process_file( filespan_t mfile, int output, bool 
second_pass ) {
                    []( char ch ) { return ch == '\n'; } );
       struct { int in, out; filespan_t mfile; } copy;
       dbgmsg("%s:%d: line " HOST_SIZE_T_PRINT_UNSIGNED ", opening %s on fd %d",
-             __func__, __LINE__,mfile.lineno(),
+             __func__, __LINE__, (fmt_size_t)mfile.lineno(),
              copybook.source(), copybook.current()->fd);
       copy.in = copybook.current()->fd;
       copy.mfile = free_form_reference_format( copy.in );
diff --git a/gcc/cobol/parse.y b/gcc/cobol/parse.y
index 83bffdfccc5c..7bcbf7467e9a 100644
--- a/gcc/cobol/parse.y
+++ b/gcc/cobol/parse.y
@@ -10316,8 +10316,8 @@ intrinsic:      function_udf
                   if( p != NULL ) {
                    auto loc = symbol_field_location(field_index(p->field));
                     error_msg(loc, "FUNCTION %qs has "
-                              "inconsistent parameter type %zu (%qs)",
-                              keyword_str($1), p - args.data(), 
name_of(p->field) );
+                              "inconsistent parameter type %ld (%qs)",
+                              keyword_str($1), (long)(p - args.data()), 
name_of(p->field) );
                     YYERROR;
                   }
                   $$ = is_numeric(args[0].field)?
diff --git a/gcc/cobol/scan_ante.h b/gcc/cobol/scan_ante.h
index 696073913972..88a8e8c3609c 100644
--- a/gcc/cobol/scan_ante.h
+++ b/gcc/cobol/scan_ante.h
@@ -149,7 +149,7 @@ numstr_of( const char string[], radix_t radix = decimal_e ) 
{
     }
     auto nx = std::count_if(input, p, fisdigit);
     if( 36 < nx ) {
-      error_msg(yylloc, "significand of %s has more than 36 digits (%zu)", 
input, nx);
+      error_msg(yylloc, "significand of %s has more than 36 digits (%ld)", 
input, (long)nx);
       return NO_CONDITION;
     }
 
diff --git a/gcc/cobol/util.cc b/gcc/cobol/util.cc
index c85b4cbea6f2..854bd7fb6441 100644
--- a/gcc/cobol/util.cc
+++ b/gcc/cobol/util.cc
@@ -1058,8 +1058,8 @@ cbl_field_t::report_invalid_initial_value(const YYLTYPE& 
loc) const {
                                                  return TOUPPER(ch) == 'E';
                                                } );
               if( !has_exponent && data.precision() < pend - p ) {
-                error_msg(loc, "%s cannot represent VALUE %qs exactly (max 
%c%zu)",
-                          name, data.initial, '.', pend - p);
+                error_msg(loc, "%s cannot represent VALUE %qs exactly (max 
%c%ld)",
+                          name, data.initial, '.', (long)(pend - p));
               }
             }
           }

Reply via email to