11include (CheckCXXCompilerFlag)
22include (CheckCCompilerFlag)
33
4- # Try to add -Wflag if compiler supports it
4+ # Try to add -Wflag if compiler supports it (GCC/Clang)
55macro (add_warning flag)
66 string (REPLACE "-" "_" underscored_flag ${flag} )
77 string (REPLACE "+" "x" underscored_flag ${underscored_flag} )
@@ -22,6 +22,37 @@ macro (add_warning flag)
2222 endif ()
2323endmacro ()
2424
25+ # MSVC warning management macros
26+ macro (msvc_warning_level level)
27+ if (MSVC )
28+ set (CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /W${level} " )
29+ set (CMAKE_C_FLAGS "${CMAKE_C_FLAGS} /W${level} " )
30+ endif ()
31+ endmacro ()
32+
33+ # Disable specific MSVC warning by code (e.g., 4711)
34+ macro (msvc_disable_warning code)
35+ if (MSVC )
36+ set (CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /wd${code} " )
37+ set (CMAKE_C_FLAGS "${CMAKE_C_FLAGS} /wd${code} " )
38+ endif ()
39+ endmacro ()
40+
41+ # Enable specific MSVC warning by code
42+ macro (msvc_enable_warning code)
43+ if (MSVC )
44+ set (CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /w1${code} " )
45+ set (CMAKE_C_FLAGS "${CMAKE_C_FLAGS} /w1${code} " )
46+ endif ()
47+ endmacro ()
48+
49+ # Disable MSVC warning for specific target
50+ macro (target_msvc_disable_warning target code)
51+ if (MSVC )
52+ target_compile_options (${target} PRIVATE "/wd${code} " )
53+ endif ()
54+ endmacro ()
55+
2556# Try to add -Wno flag if compiler supports it
2657macro (no_warning flag)
2758 add_warning(no -${flag} )
@@ -85,4 +116,57 @@ no_warning(restrict)
85116no_warning(free-nonheap-object)
86117if (CMAKE_CXX_COMPILER_ID MATCHES "GNU" )
87118 no_warning(stringop-overflow)
119+ endif ()
120+
121+ # MSVC-specific warning configuration
122+ if (MSVC )
123+ # Set warning level 3 (default level with reasonable warnings)
124+ msvc_warning_level(3)
125+
126+ # Disable excessive informational warnings that don't indicate bugs
127+ msvc_disable_warning(4711) # Function selected for automatic inline expansion
128+ msvc_disable_warning(4710) # Function not inlined
129+ msvc_disable_warning(4514) # Unreferenced inline function removed
130+ msvc_disable_warning(4820) # Padding added to struct/class
131+
132+ # Disable implicit special member function warnings (often unavoidable in modern C++)
133+ msvc_disable_warning(4626) # Assignment operator implicitly deleted
134+ msvc_disable_warning(4625) # Copy constructor implicitly deleted
135+ msvc_disable_warning(5027) # Move assignment operator implicitly deleted
136+ msvc_disable_warning(5026) # Move constructor implicitly deleted
137+ msvc_disable_warning(4623) # Default constructor implicitly deleted
138+
139+ # Disable other low-value warnings
140+ msvc_disable_warning(5219) # Implicit conversion from T to bool
141+ msvc_disable_warning(5045) # Spectre mitigation
142+ msvc_disable_warning(5246) # Aggregate initialization
143+ msvc_disable_warning(4686) # Template specialization
144+ msvc_disable_warning(5266) # const qualifier discarded
145+ msvc_disable_warning(4800) # Implicit conversion to bool
146+ msvc_disable_warning(4868) # Left-to-right evaluation order
147+ msvc_disable_warning(4371) # Layout change from previous compiler version
148+ msvc_disable_warning(4127) # Conditional expression is constant
149+ msvc_disable_warning(4355) # 'this' used in member initializer list
150+ msvc_disable_warning(4668) # Preprocessor macro not defined
151+ msvc_disable_warning(5039) # Exception specification issue
152+ msvc_disable_warning(4777) # Format string mismatch
153+ msvc_disable_warning(5264) # Variable declared but not used
154+
155+ # KEEP these warnings enabled - they indicate potential bugs:
156+ # C4365: Signed/unsigned mismatch
157+ # C4267: Conversion with possible loss of data
158+ # C4244: Data conversion with possible loss
159+ # C4242: Data conversion with possible loss
160+ # C4458: Declaration hides class member
161+ # C4245: Signed/unsigned mismatch in conversion
162+ # C4389: Signed/unsigned mismatch in equality
163+ # C4457: Declaration hides function parameter
164+ # C4146: Unary minus applied to unsigned type
165+ # C4456: Declaration hides previous local declaration
166+ # C4996: Deprecated function usage
167+ # C4100: Unreferenced formal parameter
168+ # C4101: Unreferenced local variable
169+ # C4061: Switch statement case not handled
170+
171+ message (STATUS "MSVC warning configuration applied - suppressed informational warnings, kept bug-indicating warnings" )
88172endif ()
0 commit comments