From a3eeef792ee832445e8ca0b3ce02fa7c1e44d3ca Mon Sep 17 00:00:00 2001 From: Dan Jump Date: Tue, 1 Sep 2026 13:55:43 -0700 Subject: [PATCH 1/3] Scope the base_macros.h warning disables to base.h base_macros.h disables C5046, C4268, C4499 and C4630 without a matching #pragma warning(push)/(pop) pair. base.h includes it near the top and never restores the warning state, so those four warnings stay disabled for the rest of every translation unit that includes base.h, not just for the C++/WinRT declarations they were added for. Consumers silently lose the warnings in their own code: namespace { struct S { int x; }; } S f(); int main() { f(); } That warns C5046 on its own, and stops warning as soon as is included ahead of it, at both /W4 and /Wall. Open the scope before the include and close it at the end of base.h. The disables still cover everything C++/WinRT declares, and the warning state is handed back to the consumer unchanged. The set of warnings reported from within the C++/WinRT headers is unaffected. --- cppwinrt/code_writers.h | 27 +++++++++++++++++++++++++++ cppwinrt/file_writers.h | 1 + strings/base_macros.h | 5 +++++ 3 files changed, 33 insertions(+) diff --git a/cppwinrt/code_writers.h b/cppwinrt/code_writers.h index a9aa5bc78..470ab6162 100644 --- a/cppwinrt/code_writers.h +++ b/cppwinrt/code_writers.h @@ -117,6 +117,33 @@ namespace cppwinrt return { w, write_close_file_guard }; } + static void write_macro_warning_pop(writer& w) + { + auto format = R"(#ifdef _MSC_VER +#pragma warning(pop) +#endif // _MSC_VER +)"; + + w.write(format); + } + + // base_macros.h disables a handful of compiler warnings that C++/WinRT's own + // declarations would otherwise trigger. Those disables have to stay in effect + // for the rest of the file that includes it, so the enclosing scope is opened + // here rather than inside base_macros.h itself. Without the matching pop the + // disables would remain active for everything the consumer writes after the + // include, silently turning the warnings off for their code as well. + [[nodiscard]] static finish_with wrap_macro_warning_scope(writer& w) + { + auto format = R"(#ifdef _MSC_VER +#pragma warning(push) +#endif // _MSC_VER +)"; + + w.write(format); + return { w, write_macro_warning_pop }; + } + [[nodiscard]] static finish_with wrap_lean_and_mean(writer& w, bool is_lean_and_mean = true) { if (is_lean_and_mean) diff --git a/cppwinrt/file_writers.h b/cppwinrt/file_writers.h index fd9833cb1..365567c80 100644 --- a/cppwinrt/file_writers.h +++ b/cppwinrt/file_writers.h @@ -17,6 +17,7 @@ namespace cppwinrt w.write(strings::base_detect_numerics); w.write(strings::base_include_numerics); } + auto wrap_macro_warnings = wrap_macro_warning_scope(w); w.write_root_include("base_macros"); w.write(strings::base_source_location); w.write(strings::base_types); diff --git a/strings/base_macros.h b/strings/base_macros.h index 850d0c627..9991fac3d 100644 --- a/strings/base_macros.h +++ b/strings/base_macros.h @@ -23,6 +23,11 @@ #define WINRT_IMPL_SHIM(...) (*(abi_t<__VA_ARGS__>**)&static_cast<__VA_ARGS__ const&>(static_cast(*this))) #ifdef _MSC_VER +// These disables deliberately apply to the remainder of the file that includes +// this header, because they cover declarations made throughout C++/WinRT. The +// including file opens a #pragma warning(push) beforehand and pops it at the +// end, which keeps them from escaping into consumer code. + // Note: this is a workaround for a false-positive warning produced by the Visual C++ 15.9 compiler. #pragma warning(disable : 5046) From bd4b70c40e85e28cf09b4954e9d58802ce9b0cfc Mon Sep 17 00:00:00 2001 From: Daniel Jump Date: Mon, 7 Sep 2026 08:47:10 -0700 Subject: [PATCH 2/3] Clean up comments in base_macros.h Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com> --- strings/base_macros.h | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/strings/base_macros.h b/strings/base_macros.h index 9991fac3d..da702708a 100644 --- a/strings/base_macros.h +++ b/strings/base_macros.h @@ -24,9 +24,10 @@ #ifdef _MSC_VER // These disables deliberately apply to the remainder of the file that includes -// this header, because they cover declarations made throughout C++/WinRT. The -// including file opens a #pragma warning(push) beforehand and pops it at the -// end, which keeps them from escaping into consumer code. +// this header, because they cover declarations made throughout C++/WinRT. +// When included by , that file opens a #pragma warning(push) +// before including base_macros.h and pops it at the end to avoid leaking +// the disables into consumer code. // Note: this is a workaround for a false-positive warning produced by the Visual C++ 15.9 compiler. #pragma warning(disable : 5046) From 640e8d4e338c94c05be333b86b3ef223b5214d9f Mon Sep 17 00:00:00 2001 From: Dan Jump Date: Mon, 7 Sep 2026 09:57:54 -0700 Subject: [PATCH 3/3] Remove obsolete base macro warning disables The C5046 and C4268 disables targeted Visual C++ 15.9 and 16.3, while current v143 and v145 builds no longer emit them. The v145 module build also no longer emits C4499 or C4630. Delete the pragmas instead of adding push/pop plumbing so the warning-state leak is removed at its source. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> Copilot-Session: a5731c31-939d-42bb-bf1d-45099cd5dd53 --- cppwinrt/code_writers.h | 27 --------------------------- cppwinrt/file_writers.h | 1 - strings/base_macros.h | 18 ------------------ 3 files changed, 46 deletions(-) diff --git a/cppwinrt/code_writers.h b/cppwinrt/code_writers.h index 470ab6162..a9aa5bc78 100644 --- a/cppwinrt/code_writers.h +++ b/cppwinrt/code_writers.h @@ -117,33 +117,6 @@ namespace cppwinrt return { w, write_close_file_guard }; } - static void write_macro_warning_pop(writer& w) - { - auto format = R"(#ifdef _MSC_VER -#pragma warning(pop) -#endif // _MSC_VER -)"; - - w.write(format); - } - - // base_macros.h disables a handful of compiler warnings that C++/WinRT's own - // declarations would otherwise trigger. Those disables have to stay in effect - // for the rest of the file that includes it, so the enclosing scope is opened - // here rather than inside base_macros.h itself. Without the matching pop the - // disables would remain active for everything the consumer writes after the - // include, silently turning the warnings off for their code as well. - [[nodiscard]] static finish_with wrap_macro_warning_scope(writer& w) - { - auto format = R"(#ifdef _MSC_VER -#pragma warning(push) -#endif // _MSC_VER -)"; - - w.write(format); - return { w, write_macro_warning_pop }; - } - [[nodiscard]] static finish_with wrap_lean_and_mean(writer& w, bool is_lean_and_mean = true) { if (is_lean_and_mean) diff --git a/cppwinrt/file_writers.h b/cppwinrt/file_writers.h index 365567c80..fd9833cb1 100644 --- a/cppwinrt/file_writers.h +++ b/cppwinrt/file_writers.h @@ -17,7 +17,6 @@ namespace cppwinrt w.write(strings::base_detect_numerics); w.write(strings::base_include_numerics); } - auto wrap_macro_warnings = wrap_macro_warning_scope(w); w.write_root_include("base_macros"); w.write(strings::base_source_location); w.write(strings::base_types); diff --git a/strings/base_macros.h b/strings/base_macros.h index da702708a..8f05cc98f 100644 --- a/strings/base_macros.h +++ b/strings/base_macros.h @@ -22,24 +22,6 @@ #define WINRT_IMPL_SHIM(...) (*(abi_t<__VA_ARGS__>**)&static_cast<__VA_ARGS__ const&>(static_cast(*this))) -#ifdef _MSC_VER -// These disables deliberately apply to the remainder of the file that includes -// this header, because they cover declarations made throughout C++/WinRT. -// When included by , that file opens a #pragma warning(push) -// before including base_macros.h and pops it at the end to avoid leaking -// the disables into consumer code. - -// Note: this is a workaround for a false-positive warning produced by the Visual C++ 15.9 compiler. -#pragma warning(disable : 5046) - -// Note: this is a workaround for a false-positive warning produced by the Visual C++ 16.3 compiler. -#pragma warning(disable : 4268) - -// C++ module warnings by /W4 -#pragma warning(disable : 4499) -#pragma warning(disable : 4630) -#endif // _MSC_VER - #ifndef WINRT_EXPORT #ifdef WINRT_IMPL_BUILD_MODULE #define WINRT_EXPORT export extern "C++"