include (CheckCXXCompilerFlag) include (CheckCCompilerFlag) # Try to add -Wflag if compiler supports it macro (add_warning flag) string(REPLACE "-" "_" underscored_flag ${flag}) string(REPLACE "+" "x" underscored_flag ${underscored_flag}) check_cxx_compiler_flag("-W${flag}" SUPPORTS_CXXFLAG_${underscored_flag}) check_c_compiler_flag("-W${flag}" SUPPORTS_CFLAG_${underscored_flag}) if (SUPPORTS_CXXFLAG_${underscored_flag}) set (CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -W${flag}") else() message (STATUS "Flag -W${flag} is unsupported") endif() if (SUPPORTS_CFLAG_${underscored_flag}) set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -W${flag}") else() message(STATUS "Flag -W${flag} is unsupported") endif() endmacro() # Try to add -Wno flag if compiler supports it macro (no_warning flag) add_warning(no-${flag}) endmacro () # The same but only for specified target. macro (target_add_warning target flag) string (REPLACE "-" "_" underscored_flag ${flag}) string (REPLACE "+" "x" underscored_flag ${underscored_flag}) check_cxx_compiler_flag("-W${flag}" SUPPORTS_CXXFLAG_${underscored_flag}) if (SUPPORTS_CXXFLAG_${underscored_flag}) target_compile_options (${target} PRIVATE "-W${flag}") else () message (STATUS "Flag -W${flag} is unsupported") endif () endmacro () macro (target_no_warning target flag) target_add_warning(${target} no-${flag}) endmacro () add_warning(all) add_warning(extra) add_warning(error) # treat all warnings as errors if(CMAKE_CXX_COMPILER_ID MATCHES "Clang") add_warning(strict-overflow=2) add_warning(suggest-override) add_warning(suggest-destructor-override) add_warning(unused) add_warning(unreachable-code) # TODO: these warnings are not enabled by default, but we consider them as useful and good to enable in the future no_warning(implicit-int-conversion) no_warning(implicit-float-conversion) no_warning(unused-member-function) no_warning(old-style-cast) no_warning(non-virtual-dtor) no_warning(float-conversion) no_warning(sign-conversion) no_warning(shorten-64-to-32) no_warning(padded) no_warning(missing-noreturn) elseif(CMAKE_CXX_COMPILER_ID MATCHES "GNU") add_warning(pedantic) # Add compiler options only to c++ compiler function(add_cxx_compile_options option) add_compile_options("$<$,CXX>:${option}>") endfunction() add_cxx_compile_options(-Wstrict-overflow=1) add_cxx_compile_options(-Wdelete-incomplete) add_cxx_compile_options(-Wduplicated-cond) add_cxx_compile_options(-Wdisabled-optimization) add_cxx_compile_options(-Winit-self) add_cxx_compile_options(-Wbool-compare) add_cxx_compile_options(-Wlogical-not-parentheses) add_cxx_compile_options(-Wlogical-op) add_cxx_compile_options(-Wmaybe-uninitialized) add_cxx_compile_options(-Wmisleading-indentation) add_cxx_compile_options(-Wno-return-local-addr) add_cxx_compile_options(-Wodr) add_cxx_compile_options(-Wpointer-arith) add_cxx_compile_options(-Wredundant-decls) add_cxx_compile_options(-Wreorder) add_cxx_compile_options(-Wshift-negative-value) add_cxx_compile_options(-Wsizeof-array-argument) add_cxx_compile_options(-Wswitch-bool) add_cxx_compile_options(-Wtautological-compare) add_cxx_compile_options(-Wtrampolines) endif()