Short answer: you can’t make a diagnostic pragma inside code that’s compiled into a Clang module suppress a warning that’s emitted later when that code is instantiated in another TU. This is a known limitation of Clang modules: the diagnostic state from #pragma clang diagnostic … is not preserved/rehonored across module boundaries for things like template instantiations that happen in the importing TU. That’s why your pragma works with textual inclusion but not after the header is compiled into a module.
Practical ways to deal with it:
Mark the problematic header as textual in the module map. Then it’s included textually in importers (so the pragma applies), while the rest of the module remains precompiled. Example module.modulemap: module MyLib { header "MyLib.h" textual header "detail/Equal.h" // contains the pragma-suppressed code export * } Tradeoff: that header is reparsed in each TU (some compile-time cost), but you can limit this to only the few headers that need it.
Mark the module as [system]. You already found this; it suppresses all warnings originating from inside the module for importers. If you only want this for consumers, keep two module maps (one with [system] for installs/consumers, one without for building the library itself), and select the appropriate one via -fmodule-map-file or by installing the consumer one alongside headers.
Keep the header textual via normal inclusion for your own build and only expose it through the module to consumers. You can disable implicit header-to-module mapping during your library build with -fno-implicit-module-maps (or even -fno-implicit-modules) so includes remain textual internally, while consumers still import the module.
Avoid triggering the warning in the first place (e.g., for your float-equal example, compare bit patterns via std::bit_cast/std::memcmp for floating types, or provide specializations), so you don’t need a pragma.
As a last resort, ask users to disable the warning around the instantiation site or pass -Wno-float-equal when importing the module (not ideal).
There isn’t currently a way to “serialize” and reapply the pragma’s diagnostic state from inside the module for template instantiations. If you want per-header selective suppression without making the whole module a system module, marking that header as textual is the supported route.
Was this answer helpful?
version: gpt-5-2025-08-07
Status: UQ Validated
Validated: 7 months ago
Status: Needs Human Verification
Loading model reviews...
Loading reviews...