Skip to content

build: /WX on every MSVC target, and no more C4127 - #389

Merged
charlesnicholson merged 1 commit into
mainfrom
fix/msvc-c4127-werror
Sep 1, 2026
Merged

charlesnicholson merged 1 commit into
mainfrom
fix/msvc-c4127-werror

Conversation

@charlesnicholson

Copy link
Copy Markdown
Owner

build.bat under Visual Studio 2022 stops on error C2220 partway through the conformance suite. 32 lines in tests/conformance.c each raise C4127, "conditional expression is constant", and that build has carried /WX for a long time.

CI never saw it: the windows-latest runner is on Visual Studio 2026 (v18.9.1), whose front end no longer reports C4127 for these. So main is green on the runner and broken on a 2022 desktop.

The warnings

Two macros were the whole of it. Each wrapped its cases in a runtime if over a sizeof comparison the compiler folds away:

if (sizeof(type) <= sizeof(int)) { ... }                    /* NPF_TEST_WPROMO */
if (sizeof(int_least8_t) == sizeof(signed char)) { ... }    /* NPF_TEST_WSAME  */

C has no if constexpr to mark that as deliberate, so the guards move to the preprocessor, which answers the same questions from the type maxima <stdint.h> and <limits.h> already publish:

  • a signed type is no wider than int exactly when its maximum fits in INT_MAX
  • two integer types of one signedness hold the same values exactly when their maxima match — which is the property these cases actually rest on, more directly than sizeof equality does

Grouping cases under #if is what the rest of that section already does. Both macros lose a parameter and a nesting level as a result.

The policy

/WX is MSVC's -Werror, and three of the four compile-only targets — npf_static, use_npf_directly, wrap_npf — were building at the default warning level, so nothing they said could fail the build. They now carry the same /W4 /WX set as the unit and conformance builds, hoisted into one _CL_WARN_FLAGS so the policy lives in a single place.

All three were already clean at /W4. A survey run with /WX off across the whole matrix found C4127 to be the only warning the entire Windows build emits — 6208 of them, all from those 32 lines.

Verification

VS 2022 19.44.35228, full 2532-combo matrix, no shard:

config before after
Release / x64 error C2220, exit 1 clean, exit 0
Release / x86 — clean, exit 0
Debug / x64 — clean, exit 0

All three report PASSED: 3642306 assertions across 5064 objects — the same count the green VS 2026 CI run reports, so the #if guards select exactly what the runtime guards used to and no coverage was traded away. clang also compiles the file clean under the Makefile's -Wall -Wextra -Wundef -Werror in both C17 and C++20; -Wundef confirms every macro in the new #if lines is actually defined.

nanoprintf.h is untouched, so there is no code-size impact — cortex-m0/m4 -Os output is unchanged by construction.

🤖 Generated with Claude Code

build.bat under Visual Studio 2022 stops on error C2220 partway through the
conformance suite. 32 lines in tests/conformance.c each raise C4127,
"conditional expression is constant", and that build has carried /WX for a long
time. CI never saw it: the windows-latest runner is on Visual Studio 2026, whose
front end no longer reports C4127 for these, so main is green on the runner and
broken on a 2022 desktop.

Two macros were the whole of it. Each wrapped its cases in a runtime `if` over a
sizeof comparison the compiler folds away:

    if (sizeof(type) <= sizeof(int)) { ... }
    if (sizeof(int_least8_t) == sizeof(signed char)) { ... }

C has no `if constexpr` to mark that as deliberate, so the guards move to the
preprocessor, which answers the same questions from the type maxima <stdint.h>
and <limits.h> already publish: a signed type is no wider than int exactly when
its maximum fits in INT_MAX, and two integer types of one signedness hold the
same values exactly when their maxima match -- which is the property these cases
actually rest on. Grouping cases under #if is what the rest of the section does.

The other half is the policy itself. /WX is MSVC's -Werror, and three of the four
compile-only targets -- npf_static, use_npf_directly, wrap_npf -- were building
at the default warning level, so nothing they said could fail the build. They now
carry the same /W4 /WX set as the unit and conformance builds, hoisted into one
_CL_WARN_FLAGS. All three were already clean at /W4: a survey run with /WX off
found C4127 to be the only warning the entire Windows build emits, 6208 of them,
all from those 32 lines.

Verified on VS 2022 19.44.35228 across Release/x64, Release/x86 and Debug/x64:
zero warnings, and "PASSED: 3642306 assertions across 5064 objects" -- the same
count the green VS 2026 CI run reports, so the #if guards select exactly what the
runtime guards used to. clang also compiles the file clean under the Makefile's
-Wall -Wextra -Wundef -Werror in both C17 and C++20.

nanoprintf.h is untouched, so there is no code-size impact.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
@charlesnicholson
charlesnicholson merged commit a6a62e4 into main Sep 1, 2026
147 of 148 checks passed
@charlesnicholson
charlesnicholson deleted the fix/msvc-c4127-werror branch September 1, 2026 15:39
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.

1 participant