Skip to content

Conversation

@pganssle
Copy link
Owner

@pganssle pganssle commented Jun 2, 2020

This uses a macro to use the "disable this warning on GCC" pragmas only on GCC, to avoid "unknown pragma" warnings on other compilers.

setup.py Outdated
"backports.zoneinfo._czoneinfo", sources=["lib/zoneinfo_module.c"],
"backports.zoneinfo._czoneinfo",
sources=["lib/zoneinfo_module.c"],
extra_compile_args=["-std=c99"],
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not quite sure how to do this in a cross-platform way, because this works for gcc and clang but not for MSVC, which warns about an unknown argument (though the command still succeeds).

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here's some prior art: python/cpython@0b7829e#diff-2e01a9f45ceaa02248341ccd350bc7c0

MSVC is primarily meant to be a C++ compiler so it only has c++ mode switches, the standard of C it implements is basically C99 I think. So just leaving it without a flag should be fine.

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does that work if someone is using cygwin or mingw or something?

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good question, apparently mingw overrides sys.platform to be mingw when using gcc so it should work there:
https://github.com/msys2/MINGW-packages/blob/abd06ca92d876b9db05dd65f27d71c4ebe2673a9/mingw-w64-python2/0410-MINGW-build-extensions-with-GCC.patch#L54

Not sure about cygwin though I'd imagine it also overrides sys.platform for this reason.

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hm, looks like mingw and cygwin return mingw and cygwin, respectively. I'm not familiar enough with Windows to know if there is a gcc for that platform, but I guess it's pretty safe to do it this way now, I suppose.

I guess the real question is how important is this. It seems like the worst thing that happens in the common case is that it raises a warning on Windows, but if someone does have a Windows system out there running gcc, I think this turns it into a hard failure.

I guess I could do the sys.platform check and then as a fallback put the disabling pragma under a check against __STDC_VERSION__ >= 199901L (since that's the only part that requires C99.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

While there's no way to run gcc without mingw or cygwin, clang can actually be built natively and made available available on Windows. Though even there they provide a utility to make the toolchain compatible with MSVC: https://clang.llvm.org/docs/MSVCCompatibility.html#msvc-compatibility

I think this might not be worth handling with the preprocessor directive, may as well wait to see if anyone brings it up as a real problem.

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The -std=c99 is really only needed on gcc because the only C99 feature we're using is _Pragma, which is only in the GCC branch of the conditional compile, so I suppose we're safe.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

clang sets __GNUC__ since it implements gcc compiler extensions.

@pganssle
Copy link
Owner Author

pganssle commented Jun 2, 2020

@ammaraskar What do you think of this?

@ammaraskar
Copy link

Aah I see what you mean about the verbosity, this seems good. It might make a good addition to pyport.h but I don't think suppressing diagnostics is very common in other parts of CPython.

pganssle added 3 commits June 3, 2020 10:11
GCC-specific pragmas will raise unknown pragma warnings on MSVC; rather
than adding #ifdefs around the individual pragmas, this defines a macro
that uses the pragmas on GCC but is a no-op on other compilers.

This requires using at least c99, which is when _Pragma was introduced.
This is no longer necessary, since we are now conditionally using
pragmas to disable warnings on a per-compiler basis.
@pganssle pganssle force-pushed the refactor_error_disabling branch from 97370e1 to 7129b1a Compare June 3, 2020 14:11
@pganssle pganssle merged commit 47d28a1 into master Jun 3, 2020
@pganssle pganssle deleted the refactor_error_disabling branch June 18, 2020 18:35
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants