[llvm] 4a907a5 - [CMake] Add LLVM_ENABLE_WARNING_SUPPRESSIONS to toggle warning suppressions (#183439)

via llvm-commits llvm-commits at lists.llvm.org
Mon Mar 2 05:44:08 PST 2026


Author: Shaojie Zhu
Date: 2026-03-02T14:44:04+01:00
New Revision: 4a907a526dd735acf7433be2cbddb30d48e96095

URL: https://github.com/llvm/llvm-project/commit/4a907a526dd735acf7433be2cbddb30d48e96095
DIFF: https://github.com/llvm/llvm-project/commit/4a907a526dd735acf7433be2cbddb30d48e96095.diff

LOG: [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 at intel.com>

Added: 
    

Modified: 
    llvm/cmake/modules/HandleLLVMOptions.cmake
    llvm/cmake/modules/LLVMConfig.cmake.in
    llvm/docs/CMake.rst

Removed: 
    


################################################################################
diff  --git a/llvm/cmake/modules/HandleLLVMOptions.cmake b/llvm/cmake/modules/HandleLLVMOptions.cmake
index 4ee3d42ed762a..58c3b28ed5e87 100644
--- a/llvm/cmake/modules/HandleLLVMOptions.cmake
+++ b/llvm/cmake/modules/HandleLLVMOptions.cmake
@@ -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

diff  --git a/llvm/cmake/modules/LLVMConfig.cmake.in b/llvm/cmake/modules/LLVMConfig.cmake.in
index 70c807abea98a..0a7ae97bd7549 100644
--- a/llvm/cmake/modules/LLVMConfig.cmake.in
+++ b/llvm/cmake/modules/LLVMConfig.cmake.in
@@ -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@)

diff  --git a/llvm/docs/CMake.rst b/llvm/docs/CMake.rst
index ec181999aec57..6a70a82a6e438 100644
--- a/llvm/docs/CMake.rst
+++ b/llvm/docs/CMake.rst
@@ -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.
 


        


More information about the llvm-commits mailing list