https://gcc.gnu.org/bugzilla/show_bug.cgi?id=125704
Bug ID: 125704
Summary: -Wglobal-module cannot be suppressed in-source:
#pragma GCC diagnostic ignored "-Wglobal-module" in
the global module fragment is itself diagnosed by
-Wglobal-module
Product: gcc
Version: 16.1.0
Status: UNCONFIRMED
Severity: normal
Priority: P3
Component: c++
Assignee: unassigned at gcc dot gnu.org
Reporter: gershnik at hotmail dot com
Target Milestone: ---
-Wglobal-module warns about declarations in the global module fragment that did
not arrive via preprocessor inclusion. The problem is that it has no in-source
off switch. The standard mechanism for silencing a warning over a region is:
#pragma GCC diagnostic ignored "-Wglobal-module"
but that pragma is *itself* flagged by -Wglobal-module when placed in the
global module fragment, evidently because the pragma is non-#include content in
that region. So the author of a module interface unit has no way to suppress
the warning from within the source. The only escape is -Wno-global-module on
the consumer's command line, which a shipped library cannot apply on its
consumers' behalf.
Minimal reproducer (repro.cppm):
module;
#pragma GCC diagnostic ignored "-Wglobal-module" // intended to suppress;
itself warns
struct iovec; // warning is suppressed
export module repro;
Compiled with:
g++ -std=c++26 -fmodules-ts -Wall -c repro.cppm
Output:
warning: global module fragment contents must be from preprocessor inclusion
[-Wglobal-module]
2 | #pragma GCC diagnostic ignored "-Wglobal-module" // intended to
suppress; itself warns
| ^~~
Expected:
No warnings. The suppression pragma itself is not diagnosed by the very warning
it disables.
Impact:
There are legitimate use cases where non-include declarations are necessary in
the global module fragment. For example, they might be needed to provide
fallbacks for system-defined structs or typedefs on a system where the relevant
#include-s are not available. There _should_ be a way for the module author to
silence the warning for such intentional use cases.