10
Stack Exchange
votes

How to ignore warnings in headers that are part of a clang C++ module?

Posted on 8/11/2016
UQ ID:394
Technology
Stack Overflow
Original

We're using clang with -fmodule and -fcxx-module to enable module support as documented at http://clang.llvm.org/docs/Modules.html. We're already seeing a significant improvement in build times by defining module maps for our core libraries.

However, we have some library headers that use pragmas to disable warnings for certain lines, for example:

template <typename TFloat>
static bool exactlyEqual(TFloat lhs, TFloat rhs)
{
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wfloat-equal"
    return lhs == rhs;
#pragma clang diagnostic pop
}

When this header is pulled in as a precompiled module, it seems clang's internal representation does not preserve the pragma information and the warning is still emitted. Since we treat warnings as errors this causes compilation to fail. Some might argue to just disable float-equal entirely, but we have a bunch of other cases with different warnings which we don't want to globally disable.

We're already using -Wno-system-headers and -isystem so that clients of libraries generally don't see warnings like this anyway (even without the pragma), but this doesn't seem to work when the header is imported as a module. In addition we still hit these warnings for code internal to the library which includes the header as a non-system header (i.e. without using -isystem / using double quotes), since module precompilation and importing also occurs here.

I've tried using _Pragma(...) instead of #pragma which didn't have any effect.

Is there some other way to conditionally ignore warnings in headers that come from precompiled clang modules?

UPDATE: I've put a sample project up on https://github.com/MikeWeller/ClangModuleWarnings which reproduces the problem

UPDATE: Seems the [system] module attribute will suppress all warnings. However this suppresses even warnings we want to see when building the library itself, and we don't want to make all our modules system modules. If we find a way to not use the library's module map when building the library itself this may be acceptable but we'd still like to pragma out certain warnings for non-system modules..

Keywords & Tags
c++
clang
clang++
llvm-clang
Model Answers (9)
Submitted answers from various AI models
Unsolved
No reviews yet

K2-Think

Pending UQ Validation
Needs Human Verification
MBZUAI-IFM
Qwen2.5-32B
Sep 18, 2025, 01:50 PM

To address the issue of ignoring specific warnings in headers that are part of Clang C++ modules, follow these steps:

Problem Analysis

Clang modules precompile headers into a binary form (Precompiled Module, PCM) to reduce parsing time. However, diagnostic pragmas (like #pragma clang diagnostic ignored) in these headers are processed during module compilation but not propagated to the impor...

0 model reviews
0 human reviews
No reviews yet

GPT-5

UQ Validated
Needs Human Verification
OpenAI
Sep 2, 2025, 06:56 AM

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 importin...

0 model reviews
0 human reviews
No reviews yet

Gemini 2.5 Pro

Failed UQ Validation
Needs Human Verification
Google
Aug 1, 2025, 12:00 AM

This is an excellent and subtle question that gets to the heart of how Clang modules work and their current limitations. You've correctly identified the core issue: diagnostic pragmas are not serialized as part of the precompiled module's Abstract Syntax Tree (AST), so they have no effect when the module is imported.

The warning for exactlyEqual is triggered not at the point of its definition (...

0 model reviews
0 human reviews
No reviews yet

Claude Opus 4

Failed UQ Validation
Needs Human Verification
Anthropic
Aug 1, 2025, 12:00 AM

This is a known limitation with Clang modules where pragma-based warning suppressions don't always work correctly when headers are compiled into modules. Here are several approaches to address this issue:

Solution 1: Use Module Attributes

The most straightforward solution is to use the [system] attribute in your module map, but with a more targeted approach:

module MyLibrary [...
0 model reviews
0 human reviews
No reviews yet

DeepSeek R1

Failed UQ Validation
Needs Human Verification
DeepSeek
deepseek-ai/DeepSeek-R1
Aug 1, 2025, 12:00 AM
<think> We are facing an issue where warning suppression pragmas inside headers are not respected when those headers are part of a precompiled module in Clang. The problem occurs because the module's internal representation does not preserve the pragma information. We have tried using `_Pragma` and `-Wno-system-headers` with `-isystem`, but these do not work for modules.

The sample project provid...

0 model reviews
0 human reviews
No reviews yet

o3 Pro

Failed UQ Validation
Needs Human Verification
OpenAI
Aug 1, 2025, 12:00 AM

Short version
For a “normal” (non-textual, non-system) module Clang does not record the effect of #pragma clang diagnostic … when it writes the .pcm file.
When the same code is later imported the compiler never looks at the source again, it only reads the pre-compiled AST, and the diagnostic state that was in force while the header was being parsed is not restored.
Because of that the wa...

0 model reviews
0 human reviews
No reviews yet

Claude 3.7 Sonnet

Failed UQ Validation
Needs Human Verification
Anthropic
Aug 1, 2025, 12:00 AM

Handling Warnings in Clang C++ Modules

This is a known limitation in how Clang's module system handles pragmas. When headers are compiled into modules, certain preprocessor-level constructs like warning pragmas aren't preserved in the way you'd expect.

Solutions and Workarounds

1. Use Module Map Attributes

The most direct solution for module-wide warning control is to use attributes i...

0 model reviews
0 human reviews
No reviews yet

o4 mini

Failed UQ Validation
Needs Human Verification
OpenAI
Aug 1, 2025, 12:00 AM

Clang’s modules are implemented as pre‐parsed AST files and, unfortunately, any diagnostic pragmas you sprinkle inside your headers are thrown away when the module is built. That means

#pragma clang diagnostic push
#pragma clang diagnostic ignored “-Wfloat-equal”
// …
#pragma clang diagnostic pop

will work just fine if you #include that header normally, but as soon as it lives...

0 model reviews
0 human reviews
No reviews yet

o3

Failed UQ Validation
Needs Human Verification
OpenAI
Aug 1, 2025, 12:00 AM

What you are running into is a current limitation of Clang’s modules implementation, not a mistake in the way you are using the diagnostic pragmas.

• When the header is read while the module is being built, the #pragma clang diagnostic push / ignored / pop sequences are honoured and no diagnostic is produced.

• The information that those pragmas set up (the “diagnostic state” that says “-W...

0 model reviews
0 human reviews
Submit Your Model's Answer
Your submission will be validated by our LLM validator and then by human experts

You need to be logged in to submit an answer