nemequ / hedley

A C/C++ header to help move #ifdefs out of your code
https://nemequ.github.io/hedley/
Creative Commons Zero v1.0 Universal
774 stars 51 forks source link

Disable feature checking macros on IBM XL C/C++ #44

Open nemequ opened 3 years ago

nemequ commented 3 years ago

Newer versions of XL C/C++ implement __has_attribute, __has_builtin, etc., but the results are untrustworthy. They routinely return true to indicate that the feature is supported, but if you try to actually use it you'll end up with a compile-time warning or error informing you that the feature isn't supported.

For example:

typedef enum {
  FOO_BAR,
  FOO_BAZ
}
#if defined(__has_attribute)
  #if __has_attribute(flag_enum)
    __attribute__((__flag_enum__))
  #endif
#endif
Foo;

Will result in something like:

error: 1540-2990 The attribute " __attribute__((flag_enum))" is not supported.  The attribute is ignored.

  __attribute__((__flag_enum__))

                 ^

1 error generated.

This seems to be the norm, not the exception (at least some of the bitreverse builtins do the same thing, unless they've fixed that in more recent versions). Anything that the version of clang that release of xlc is based on will return true, but IBM regularly omits the implementation and generates a diagnostic when you try to actually use it. This means that unless people are explicitly testing xlc (which is unlikely), code directly relying on HEDLEY_HAS_* (which wrap the __has_* macros) is likely broken.

I think the best thing to do would be to just have HEDLEY_HAS_ATTRIBUTE, HEDLEY_HAS_BUILTIN, etc., just unconditionally return 0 on xlc (like it does for compilers like gcc, msvc, etc., which don't support the feature testing macros at all).

Internally this isn't really a problem since; we already have version number based checks for most features since __has_* is a fairly recent feature in xlc and most of the attributes we have macros for are supported in earlier versions which Hedley also tries to support.

This could be a problem for projects using HEDLEY_HAS_*… there is a good chance that they are broken on xlc anyways, but it's also possible that people are relying on it for attributes which actually are supported by xlc.

If IBM fixes their feature detection macros in a future version of xlc we can always re-enable HEDLEY_HAS_* for newer versions but keep it disabled for older versions.

If anyone is relying on HEDLEY_HAS_* macros for feature testing on XL C/C++, please let me know. If this would break a lot of projects we can try to find another solution (maybe a HEDLEY_SUPPORTS_* group of macros, and deprecating HEDLEY_HAS_*?). If nobody speaks up I'll probably go ahead and commit this change soon and it will be in the next release.