Skip to content

Scope the base_macros.h warning disables to base.h - #1625

Open
Daniel Jump (DanielJump) wants to merge 1 commit into
microsoft:masterfrom
DanielJump:warnings-macro-pragma-leak
Open

Scope the base_macros.h warning disables to base.h#1625
Daniel Jump (DanielJump) wants to merge 1 commit into
microsoft:masterfrom
DanielJump:warnings-macro-pragma-leak

Conversation

@DanielJump

Copy link
Copy Markdown
Member

Fixes #1624.

base_macros.h disables C5046, C4268, C4499 and C4630 with no matching #pragma warning(push) / (pop). 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 <winrt/base.h> - the consumer's own code included.

namespace { struct S { int x; }; }
S f();
int main() { f(); return 0; }

warns C5046 on its own, and goes silent as soon as <winrt/base.h> is included ahead of it, at both /W4 and /Wall.

Change

Open the warning scope immediately before the base_macros.h include in write_base_h and close it at the end of the file, using the existing finish_with / wrap_* idiom so the pop cannot be forgotten. The disables still cover every declaration C++/WinRT makes, which is what they were added for; only their escape into consumer code is removed.

base_macros.h itself keeps the disables, with a comment recording that the enclosing scope belongs to the including file, so the pairing is discoverable from either side.

Validation

Applied the equivalent change to a shipped winrt/base.h and rebuilt with the Windows SDK headers, MSVC x64, /std:c++20 /permissive- /Wall:

  • consumer C5046 in the repro above: suppressed before, reported after
  • no C4193 (warning(pop) with no matching push)
  • a translation unit exercising collections, coroutines, delegates, implements, factories and classic COM reports an identical 166 warnings before and after (C5246 x114, C4365 x21, C4946 x16, C4265 x15), so nothing inside the C++/WinRT headers regressed

Not changed

base_macros.h is also root-included by the generated component .g.h files under WINRT_IMPORT_MODULE and by the global module fragment of each generated .ixx. Those are cppwinrt's own module builds rather than ordinary consumer translation units, and C4499 / C4630 are wanted there, so I left those paths alone. Happy to extend this if you would rather it were uniform.

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 <winrt/base.h> 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.

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟢 Approval recommended

The change safely scopes MSVC warning disables to the generated header and is low risk, with only a minor comment-accuracy nit noted.

Pull request overview

This PR fixes warning-state leakage from <winrt/base.h> by scoping the MSVC warning disables originating in base_macros.h so they apply to C++/WinRT’s own declarations but don’t remain disabled for downstream consumer code.

Changes:

  • Add a generator-emitted #pragma warning(push) immediately before base_macros.h is included in the generated base.h.
  • Ensure a matching #pragma warning(pop) is emitted at the end of the generated base.h via the existing finish_with RAII writer pattern.
  • Document the intended pairing behavior in strings/base_macros.h.
File summaries
File Description
strings/base_macros.h Adds commentary explaining intended warning-disable scoping behavior for MSVC.
cppwinrt/file_writers.h Wraps the base_macros root-include in a push/pop warning scope when generating winrt/base.h.
cppwinrt/code_writers.h Introduces a reusable writer RAII helper that emits #pragma warning(push) and guarantees a matching pop.
Review details
  • Files reviewed: 3/3 changed files
  • Comments generated: 1
  • Review effort level: Lite

💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

Comment thread strings/base_macros.h
Comment on lines +26 to +29
// 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.
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.

base_macros.h disables warnings without push/pop, leaking them into consumer translation units

2 participants