]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
cobol: Fix build on 32-bit Darwin [PR120621]
authorRainer Orth <ro@CeBiTec.Uni-Bielefeld.DE>
Fri, 11 Jul 2025 07:56:18 +0000 (09:56 +0200)
committerRainer Orth <ro@CeBiTec.Uni-Bielefeld.DE>
Fri, 11 Jul 2025 07:56:18 +0000 (09:56 +0200)
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  <ro@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.

gcc/cobol/lexio.cc
gcc/cobol/parse.y
gcc/cobol/scan_ante.h
gcc/cobol/util.cc

index 0aebe52ada63d181127f75a9ed5560c4cf940b7c..dc632c2ecd8fc1a2083f6b1748ad43d0e641f7b1 100644 (file)
@@ -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 );
index 83bffdfccc5cb6cbf2328b705a6cc20fd02b53da..7bcbf7467e9acfb5a6f3d8d0bda379a72f35fd8e 100644 (file)
@@ -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)?
index 696073913972b23366cadfe3017cd0767f994c80..88a8e8c3609cf71985a9f6667bb3e9e8f0263112 100644 (file)
@@ -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;
     }
 
index c85b4cbea6f23cf01a78242c1d49e0d4202818ca..854bd7fb644153c6dde3dc6469b015c3348bca1e 100644 (file)
@@ -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));
               }
             }
           }