Repository: mesonbuild/meson

Issue:
Feature Request to Add C17/C18/ISO-9899:2018 Support
ISO-9899:2018 (C17/C18) came out last year. Although it's a non-feature corrections release, folks are going to be wanting to specify it in compiler options.

As a first shot at scoping the issue, I simply hacked a local copy of mesonbuild/compilers/c.py to add 'c17', 'c18', 'gnu17', and 'gnu18' to the list of the supported standards in class GnuCCompiler, and added 'c17' and 'gnu17' in class ClangCCompiler. Passing c_std=c17 to project(), my test compiles using GCC 8 and Clang 7 were successful.

There's a lot more to it than that, but maybe it won't be too tough.



Discussion:
Yeah this would be nice. C17 has been the default since GCC 8 so it's a bit sad the best we can get from Meson using its `c_std` feature is C11.

From `gcc(1)`:

```
           c17
           c18
           iso9899:2017
           iso9899:2018
               ISO C17, the 2017 revision of the ISO C standard (expected to
               be published in 2018).  This standard is same as C11 except for
               corrections of defects (all of which are also applied with
               -std=c11) and a new value of "__STDC_VERSION__", and so is
               supported to the same extent as C11.
```
Yes, it would be nice. Since I have control of all the meson.build files I currently use, I can simply include '-std=c17' in the c_args argument to executable() instead of including 'c_std=c17' in the default_options argument to project(), as I did in the past, or passing it to add_global_arguments() or add_project_arguments(), which I've never tried.

But it will trip up someone before too long if it hasn't already.

I have a PR prepared and uploaded to my jrl/64/meson/ fork. Since I'm a github novice, I'm waiting to see if my procedure on a different issue #5033 is correct. Once I think I know what I'm doing, I'll send a PR for this one.

Resolving patch:
diff --git a/mesonbuild/compilers/c.py b/mesonbuild/compilers/c.py
--- a/mesonbuild/compilers/c.py
+++ b/mesonbuild/compilers/c.py
@@ -1217,9 +1217,17 @@ def __init__(self, exelist, version, compiler_type, is_cross, exe_wrapper=None,
 
     def get_options(self):
         opts = CCompiler.get_options(self)
+        c_stds = ['c89', 'c99', 'c11']
+        g_stds = ['gnu89', 'gnu99', 'gnu11']
+        if self.compiler_type is CompilerType.CLANG_OSX:
+            v = '>=10.0.0'
+        else:
+            v = '>=7.0.0'
+        if version_compare(self.version, v):
+            c_stds += ['c17']
+            g_stds += ['gnu17']
         opts.update({'c_std': coredata.UserComboOption('c_std', 'C language standard to use',
-                                                       ['none', 'c89', 'c99', 'c11',
-                                                        'gnu89', 'gnu99', 'gnu11'],
+                                                       ['none'] + c_stds + g_stds,
                                                        'none')})
         return opts
 
@@ -1281,9 +1289,14 @@ def __init__(self, exelist, version, compiler_type, is_cross, exe_wrapper=None,
 
     def get_options(self):
         opts = CCompiler.get_options(self)
+        c_stds = ['c89', 'c99', 'c11']
+        g_stds = ['gnu89', 'gnu99', 'gnu11']
+        v = '>=8.0.0'
+        if version_compare(self.version, v):
+            c_stds += ['c17', 'c18']
+            g_stds += ['gnu17', 'gnu18']
         opts.update({'c_std': coredata.UserComboOption('c_std', 'C language standard to use',
-                                                       ['none', 'c89', 'c99', 'c11',
-                                                        'gnu89', 'gnu99', 'gnu11'],
+                                                       ['none'] + c_stds + g_stds,
                                                        'none')})
         if self.compiler_type.is_windows_compiler:
             opts.update({


Test patch: