Skip to content

Limit Clang diagnostic suppressions to fmt declarations - #4948

Merged
vitaut merged 1 commit into
fmtlib:mainfrom
yqtian-se:fix-clang-diagnostic-scope
Sep 18, 2026
Merged

vitaut merged 1 commit into
fmtlib:mainfrom
yqtian-se:fix-clang-diagnostic-scope

Conversation

@yqtian-se

Copy link
Copy Markdown
Contributor

Clang diagnostic pragmas remain in effect until another pragma changes or
restores them. Two public fmt headers suppress warnings for fmt declarations
but do not restore the diagnostic state before returning to consumer code:

  • format.h ignores -Wweak-vtables before declaring format_error;
  • std.h ignores -Wbit-int-extension before declaring the bitint and
    ubitint aliases.

Because neither suppression has a matching push and pop, declarations after
the include inherit it. For example, this consumer class normally triggers
-Wweak-vtables, but the warning disappears after including format.h:

#include <fmt/format.h>

class ConsumerClass {
 public:
  virtual void method() {}
};

The same occurs for a consumer _BitInt alias after including std.h:

#include <fmt/std.h>

template <int N>
using consumer_bitint = _BitInt(N);

The requested -Wbit-int-extension diagnostic is suppressed along with fmt's
own aliases. This branch is enabled by default with Clang 15 and later; I used
FMT_USE_BITINT=1 to exercise the same branch with the available Clang 14
compiler.

The matched probes produced the following results:

Consumer declaration Without the fmt header Current main This change
Weak-vtable class with -Wweak-vtables warning no diagnostic warning
_BitInt alias with -Wbit-int-extension warning no diagnostic warning

Thus, including either header currently disables a warning that the consumer
explicitly requested for its own code. In a build that promotes the warning to
an error, the header also changes whether that consumer code is accepted.

This change brackets each suppression with fmt's existing Clang diagnostic
push/pop macros. The warnings remain suppressed for the fmt declarations that
need them, while the consumer's previous diagnostic state is restored
immediately afterward. The macros remain no-ops for non-Clang compilers.

Validation:

  • a Clang 14 build with FMT_PEDANTIC=ON and FMT_WERROR=ON succeeds;
  • the configured test suite passes 26 of 26 tests.

format.h and std.h suppress Clang warnings for declarations provided by
fmt, but do not restore the previous diagnostic state. Including either
header therefore also hides the corresponding warning in unrelated
consumer code.

Use fmt's existing Clang diagnostic push and pop macros to limit
-Wweak-vtables to format_error and -Wbit-int-extension to the two
_BitInt aliases.

Signed-off-by: Yongqiang Tian <yqtian668@gmail.com>
@yqtian-se
yqtian-se requested a review from vitaut as a code owner September 16, 2026 05:20
@vitaut
vitaut merged commit c79d5d2 into fmtlib:main Sep 18, 2026
47 checks passed
@vitaut

vitaut commented Sep 18, 2026

Copy link
Copy Markdown
Contributor

Merged, thanks

@yqtian-se

Copy link
Copy Markdown
Contributor Author

Thanks!

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.

2 participants