Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-8224087

Compile C code for at least C99 Standard compliance

XMLWordPrintable

    • Icon: Enhancement Enhancement
    • Resolution: Fixed
    • Icon: P4 P4
    • 13
    • 13
    • infrastructure
    • None
    • b23

      We've just had a ridiculous situation in 2019 where we had to remove a declaration expression from a for-loop:

      for (int x = 0; x < max; x++) ...

      because unless told otherwise gcc 4.8.5 defaults to supporting -std=gnu90, which is the 1989/1990 C standard with GNU-specific extensions. That changed to -std=gnu11 in gcc 5.1.0 (https://gcc.gnu.org/onlinedocs/gcc-5.1.0/gcc/Standards.html).

      This wasn't noticed initially because our primary compiler is gcc 8.x which supports "gnu11", but gcc 4.8 is still the minimum allowed version for building JDK 13. Now we can either bump that minimum to 5.1 or else tell the compiler explicitly that C99 is allowed.

      By setting an explicit C standard level we will avoid using newer features that might be allowed by the latest compilers. This was sort-of requested in JDK-8175302 but that issue seems a bit confused and was closed as "will not fix". Some of the confusion arose from the fact that we explicitly disabled C99-isms with the Solaris Studio compiler - but that has since been removed (JDK-8215296 - except for some AWT/2D code for some reason).

      History: JDK-4946594 disabled use of C99-isms in Sun Studio Compiler back in 2003 because at the time the Windows C compiler did not support them (and it continued to not support them for a long time!). So to avoid developing shared code on Solaris that then failed to compile on Windows, the Solaris compiler was told not to allow C99 features.

      Jump forward 15 years and C99 (and later) is supported across almost all our compilers and platforms (though not necessarily enabled by default as per this issue), so there is no reason to be forced to write code that will compile only with C89/90.

      The exception in this story is again Windows and the Microsoft Visual Studio compilers. These only support C89/90 with some additional MVSC extensions - and curiously declaration expressions in for-loops are not listed in the documentation as an extension, yet we use them in Windows code and it works! MSVC does not support, and doesn't appear to have any intent of supporting C99 or C11, unless you force the compiler to compile the .c file in C++ mode, in which case you will get those parts of C99 and C11 that were also incorporated in the C++ standard, but it will also be compiled by C++ rules, which may differ from C99/C11 in places.

      https://docs.microsoft.com/en-us/cpp/build/reference/za-ze-disable-language-extensions?view=vs-2019

      So it seems that we're going to be limited to only being able to use C99-isms that are also supported as extensions by MSVC in shared or Windows-only code. (Unless we compiled the remaining JDK library C files as C++ - which is worth investigating.)

      Relatedly we compile for C++ using gnu++98, and there is a separate effort to bring that into the 21st century as well - JDK-8208089.

            dholmes David Holmes
            dholmes David Holmes
            Votes:
            0 Vote for this issue
            Watchers:
            3 Start watching this issue

              Created:
              Updated:
              Resolved: