HomePhabricator

[clang-cl] Accept `#pragma warning(disable : N)` for some N

Authored by thakis on Sep 28 2021, 4:33 PM.

Description

[clang-cl] Accept #pragma warning(disable : N) for some N

clang-cl maps /wdNNNN to -Wno-flags for a few warnings that map
cleanly from cl.exe concepts to clang concepts.

This patch adds support for the same numbers to
#pragma warning(disable : NNNN). It also lets
#pragma warning(push) and #pragma warning(pop) have an effect,
since these are used together with warning(disable).

The optional numeric argument to warning(push) is ignored,
as are the other non-disable pragma warning() arguments.
(Supporting error would be easy, but we also don't support
/we, and those should probably be added together.)

The motivating example is that a bunch of code (including in LLVM)
uses this idiom to locally disable warnings about calls to deprecated
functions in Windows-only code, and 4996 maps nicely to
-Wno-deprecated-declarations:

#pragma warning(push)
#pragma warning(disable: 4996)
  f();
#pragma warning(pop)

Implementation-wise:

  • Move /wd flag handling from Options.td to actual Driver-level code
  • Extract the function mapping cl.exe IDs to warning groups to the new file clang/lib/Basic/CLWarnings.cpp
  • Create a diag::Group enum so that CLWarnings.cpp can refer to existing groups by ID (and give DllexportExplicitInstantiationDecl a named group), and add a function to map a diag::Group to the spelling of it's associated commandline flag
  • Call that new function from PragmaWarningHandler

Differential Revision: https://reviews.llvm.org/D110668