2021-04-19 10:20:43 +08:00
# from here:
#
# https://github.com/lefticus/cppbestpractices/blob/master/02-Use_the_Tools_Available.md
# Helper function to enable compiler warnings for a specific set of files
function ( set_file_warnings )
2021-12-19 05:21:52 +08:00
option ( WARNINGS_AS_ERRORS "Treat compiler warnings as errors" TRUE )
2021-04-19 10:20:43 +08:00
set ( MSVC_WARNINGS
/ W 4 # Baseline reasonable warnings
/ w 1 4 2 4 2 # 'identifier': conversion from 'type1' to 'type1', possible loss of data
/ w 1 4 2 5 4 # 'operator': conversion from 'type1:field_bits' to 'type2:field_bits', possible loss of data
/ w 1 4 2 6 3 # 'function': member function does not override any base class virtual member function
/ w 1 4 2 6 5 # 'classname': class has virtual functions, but destructor is not virtual instances of this class may not be destructed correctly
/ w 1 4 2 8 7 # 'operator': unsigned/negative constant mismatch
/ w e 4 2 8 9 # nonstandard extension used: 'variable': loop control variable declared in the for-loop is used outside the for-loop scope
/ w 1 4 2 9 6 # 'operator': expression is always 'boolean_value'
/ w 1 4 3 1 1 # 'variable': pointer truncation from 'type1' to 'type2'
/ w 1 4 5 4 5 # expression before comma evaluates to a function which is missing an argument list
/ w 1 4 5 4 6 # function call before comma missing argument list
/ w 1 4 5 4 7 # 'operator': operator before comma has no effect; expected operator with side-effect
/ w 1 4 5 4 9 # 'operator': operator before comma has no effect; did you intend 'operator'?
/ w 1 4 5 5 5 # expression has no effect; expected expression with side- effect
/ w 1 4 6 1 9 # pragma warning: there is no warning number 'number'
/ w 1 4 6 4 0 # Enable warning on thread un-safe static member initialization
/ w 1 4 8 2 6 # Conversion from 'type1' to 'type_2' is sign-extended. This may cause unexpected runtime behavior.
/ w 1 4 9 0 5 # wide string literal cast to 'LPSTR'
/ w 1 4 9 0 6 # string literal cast to 'LPWSTR'
/ w 1 4 9 2 8 # illegal copy-initialization; more than one user-defined conversion has been implicitly applied
# /permissive- # standards conformance mode for MSVC compiler. Disabled until all out-of-the-box Windows SDKs can successfully build with it.
# Disables, remove when appropriate
/ w d 4 9 9 6 # disable warnings about deprecated functions
2021-11-19 09:15:57 +08:00
/ w d 4 0 6 8 # disable warnings about unknown pragmas (e.g. #pragma GCC)
/ w d 4 5 0 5 # disable warnings about unused functions that might be platform-specific
/ w d 4 8 0 0 # disable warnings regarding implicit conversions to bool
2021-04-19 10:20:43 +08:00
)
2021-11-19 09:15:57 +08:00
# some warnings are not supported on older NDK versions used for CI
if ( ANDROID )
set ( NON_ANDROID_CLANG_AND_GCC_WARNINGS "" )
set ( NON_ANDROID_GCC_WARNINGS "" )
else ( )
set ( NON_ANDROID_CLANG_AND_GCC_WARNINGS
- W n u l l - d e r e f e r e n c e # warn if a null dereference is detected
- W o l d - s t y l e - c a s t # warn for c-style casts
- W p e d a n t i c # warn if non-standard C++ is used
)
set ( NON_ANDROID_GCC_WARNINGS
- W m i s l e a d i n g - i n d e n t a t i o n # warn if indentation implies blocks where blocks do not exist
- W d u p l i c a t e d - c o n d # warn if if / else chain has duplicated conditions
)
endif ( )
set ( CLANG_AND_GCC_WARNINGS
2021-04-19 10:20:43 +08:00
- W a l l
- W e x t r a # reasonable and standard
- W s h a d o w # warn the user if a variable declaration shadows one from a parent context
- W n o n - v i r t u a l - d t o r # warn the user if a class with virtual functions has a non-virtual destructor. This helps catch hard to track down memory errors
- W c a s t - a l i g n # warn for potential performance problem casts
- W u n u s e d # warn on anything being unused
- W o v e r l o a d e d - v i r t u a l # warn if you overload (not override) a virtual function
- W c o n v e r s i o n # warn on type conversions that may lose data
- W s i g n - c o n v e r s i o n # warn on sign conversions
- W d o u b l e - p r o m o t i o n # warn if float is implicit promoted to double
- W f o r m a t = 2 # warn on security issues around functions that format output (ie printf)
2021-11-19 09:15:57 +08:00
# -Wimplicit-fallthrough # warn when a missing break causes control flow to continue at the next case in a switch statement (disabled until better compiler support for explicit fallthrough is available)
$ { N O N _ A N D R O I D _ C L A N G _ A N D _ G C C _ W A R N I N G S }
)
2021-12-16 16:54:15 +08:00
if ( WARNINGS_AS_ERRORS )
set ( CLANG_AND_GCC_WARNINGS ${ CLANG_AND_GCC_WARNINGS } -Werror )
set ( MSVC_WARNINGS ${ MSVC_WARNINGS } /WX )
endif ( )
2021-11-19 09:15:57 +08:00
set ( CLANG_WARNINGS
$ { C L A N G _ A N D _ G C C _ W A R N I N G S }
- W n o - u n k n o w n - w a r n i n g - o p t i o n # do not warn on GCC-specific warning diagnostic pragmas
2021-04-19 10:20:43 +08:00
)
set ( GCC_WARNINGS
2021-11-19 09:15:57 +08:00
$ { C L A N G _ A N D _ G C C _ W A R N I N G S }
$ { N O N _ A N D R O I D _ G C C _ W A R N I N G S }
2021-04-19 10:20:43 +08:00
- W l o g i c a l - o p # warn about logical operations being used where bitwise were probably wanted
2021-11-19 09:15:57 +08:00
# -Wuseless-cast # warn if you perform a cast to the same type (disabled because it is not portable as some typedefs might vary between platforms)
2021-04-19 10:20:43 +08:00
)
# Don't enable -Wduplicated-branches for GCC < 8.1 since it will lead to false positives
# https://github.com/gcc-mirror/gcc/commit/6bebae75035889a4844eb4d32a695bebf412bcd7
if ( CMAKE_CXX_COMPILER_VERSION VERSION_GREATER_EQUAL 8.1 )
set ( GCC_WARNINGS
$ { G C C _ W A R N I N G S }
- W d u p l i c a t e d - b r a n c h e s # warn if if / else branches have duplicated code
)
endif ( )
if ( MSVC )
set ( FILE_WARNINGS ${ MSVC_WARNINGS } )
elseif ( CMAKE_CXX_COMPILER_ID MATCHES ".*Clang" )
set ( FILE_WARNINGS ${ CLANG_WARNINGS } )
elseif ( CMAKE_CXX_COMPILER_ID STREQUAL "GNU" )
set ( FILE_WARNINGS ${ GCC_WARNINGS } )
else ( )
message ( AUTHOR_WARNING "No compiler warnings set for '${CMAKE_CXX_COMPILER_ID}' compiler." )
endif ( )
foreach ( WARNING ${ FILE_WARNINGS } )
set_property ( SOURCE ${ ARGV } APPEND_STRING PROPERTY COMPILE_FLAGS " ${WARNING}" )
endforeach ( )
endfunction ( )