[CMake] Add LLVM_ENABLE_WARNING_SUPPRESSIONS to toggle warning suppressions (#183439)

This PR introduces a new opt-in CMake option
`LLVM_ENABLE_WARNING_SUPPRESSIONS` (default `ON`) to toggle warning
suppressions

Previously, several compiler warnings were explicitly disabled using
`-wd` (for MSVC) or `-Wno-...` (for GCC/Clang) flags by default.
However, this causes validation failures with strict compliance scanners
like BinSkim, which require builds to run without any warning
suppressions to meet SDL compliance standards.

This change introduces an opt-in `LLVM_ENABLE_WARNING_SUPPRESSIONS`
option (default ON). When explicitly disabled (OFF), it selectively
filters out the `-wd` flags for MSVC and removes all `-Wno-...` flags
for GCC/Clang in HandleLLVMOptions.cmake. This ensures all compiler
warnings are exposed as intended for static analysis tools while
preserving the default noise-free build experience for regular users.

The option has also been exported to `LLVMConfig.cmake.in` and
documented in `CMake.rst`.

Signed-off-by: Zhu, Shaojie <shaojie.zhu@intel.com>
This commit is contained in:
Shaojie Zhu 2026-03-02 21:44:04 +08:00 committed by GitHub
parent 4af885c0c1
commit 4a907a526d
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 30 additions and 0 deletions

View File

@ -580,6 +580,7 @@ elseif(MINGW OR CYGWIN)
endif()
option(LLVM_ENABLE_WARNINGS "Enable compiler warnings." ON)
option(LLVM_ENABLE_WARNING_SUPPRESSIONS "Suppress compiler warnings." ON)
if( MSVC )
@ -784,6 +785,9 @@ if (MSVC)
# Promoted warnings to errors.
-we4238 # Promote 'nonstandard extension used : class rvalue used as lvalue' to error.
)
if (NOT LLVM_ENABLE_WARNING_SUPPRESSIONS)
list(FILTER msvc_warning_flags EXCLUDE REGEX "^-wd")
endif()
endif(NOT CLANG_CL)
# Enable warnings
@ -809,6 +813,12 @@ if (MSVC)
endforeach(flag)
endif (MSVC)
if (NOT LLVM_ENABLE_WARNING_SUPPRESSIONS AND (LLVM_COMPILER_IS_GCC_COMPATIBLE OR CLANG_CL))
# Keep track of the flags before LLVM appends its default warnings
set(PRE_CMAKE_C_FLAGS "${CMAKE_C_FLAGS}")
set(PRE_CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS}")
endif()
if (LLVM_ENABLE_WARNINGS AND (LLVM_COMPILER_IS_GCC_COMPATIBLE OR CLANG_CL))
# Don't add -Wall for clang-cl, because it maps -Wall to -Weverything for
@ -963,6 +973,19 @@ if (LLVM_COMPILER_IS_GCC_COMPATIBLE AND NOT LLVM_ENABLE_WARNINGS)
append("-w" CMAKE_C_FLAGS CMAKE_CXX_FLAGS)
endif()
if (NOT LLVM_ENABLE_WARNING_SUPPRESSIONS AND (LLVM_COMPILER_IS_GCC_COMPATIBLE OR CLANG_CL))
# Strip the -Wno- flags from the substring of flags added by LLVM in this block
string(LENGTH "${PRE_CMAKE_C_FLAGS}" PRE_CMAKE_C_LEN)
string(SUBSTRING "${CMAKE_C_FLAGS}" ${PRE_CMAKE_C_LEN} -1 ADDED_CMAKE_C_FLAGS)
string(REGEX REPLACE "(^| +)-Wno-[a-zA-Z0-9_-]+" "" ADDED_CMAKE_C_FLAGS "${ADDED_CMAKE_C_FLAGS}")
set(CMAKE_C_FLAGS "${PRE_CMAKE_C_FLAGS}${ADDED_CMAKE_C_FLAGS}")
string(LENGTH "${PRE_CMAKE_CXX_FLAGS}" PRE_CMAKE_CXX_LEN)
string(SUBSTRING "${CMAKE_CXX_FLAGS}" ${PRE_CMAKE_CXX_LEN} -1 ADDED_CMAKE_CXX_FLAGS)
string(REGEX REPLACE "(^| +)-Wno-[a-zA-Z0-9_-]+" "" ADDED_CMAKE_CXX_FLAGS "${ADDED_CMAKE_CXX_FLAGS}")
set(CMAKE_CXX_FLAGS "${PRE_CMAKE_CXX_FLAGS}${ADDED_CMAKE_CXX_FLAGS}")
endif()
macro(append_common_sanitizer_flags)
if (NOT MSVC OR CLANG_CL)
# Append -fno-omit-frame-pointer and turn on debug info to get better

View File

@ -44,6 +44,8 @@ set(LLVM_ABI_BREAKING_CHECKS @LLVM_ABI_BREAKING_CHECKS@)
set(LLVM_ENABLE_WARNINGS @LLVM_ENABLE_WARNINGS@)
set(LLVM_ENABLE_WARNING_SUPPRESSIONS @LLVM_ENABLE_WARNING_SUPPRESSIONS@)
set(LLVM_ENABLE_EXPENSIVE_CHECKS @LLVM_ENABLE_EXPENSIVE_CHECKS@)
set(LLVM_ENABLE_ASSERTIONS @LLVM_ENABLE_ASSERTIONS@)

View File

@ -644,6 +644,11 @@ its enabled sub-projects. Nearly all of these variable names begin with
**LLVM_ENABLE_WARNINGS**:BOOL
Enable all compiler warnings. Defaults to ON.
**LLVM_ENABLE_WARNING_SUPPRESSIONS**:BOOL
Suppress specific compiler warnings. When disabled, this
prevents suppressing warnings with flags such as MSVC's ``-wd`` or GCC/Clang's ``-Wno-...``.
Defaults to ON.
**LLVM_ENABLE_WERROR**:BOOL
Stop and fail the build, if a compiler warning is triggered. Defaults to OFF.