# ================================================================================== # adapted from # https://github.com/zhivkopetrov/cmake_helpers.git # MIT License Copyright (c) 2020-2023 Zhivko Petrov # # some compiler warnings from # https://lefticus.gitbooks.io/cpp-best-practices/content/02-Use_the_Tools_Available.html # # ================================================================================== # target_enable_warnings function(target_enable_warnings target) if(CMAKE_CXX_COMPILER_ID MATCHES MSVC) target_compile_options( ${target} PRIVATE /W4 /WX ) endif() if((CMAKE_CXX_COMPILER_ID MATCHES GNU) OR (CMAKE_CXX_COMPILER_ID MATCHES Clang)) target_compile_options( ${target} PRIVATE # compiler warnings # generic -Wall -Wextra # reasonable and standard -Wpedantic # non-standard cpp is used -Wmisleading-indentation -Wlogical-op # logical op being used instead of bitwise -Wundef -Wuninitialized -Weffc++ # disabled because it would require skeleton changes # variables -Wshadow # variable declaration shadows one from parent context -Wunused # anything being unused # casts -Werror=old-style-cast # C-style casts -Wcast-align # performance problem casts -Wcast-qual -Wuseless-cast # cast to same type # conversion #-Wconversion # type conversion may lose data -Wsign-conversion # sign conversion -Wdouble-promotion # implicit float to double # functions -Wunused-parameter -Werror=missing-declarations # missing function declaration in header files -Werror=redundant-decls -Werror=return-type -Werror=overloaded-virtual # inheritance -Wnon-virtual-dtor # class with virtual functions has non-virt dtor -Woverloaded-virtual # overloaded (not override) virtual function # branches -Wduplicated-cond # if/else chain has duplicated conditions -Wduplicated-branches # if/else chain has duplicated code # pointers -Wpointer-arith -Wnull-dereference # style -Wformat=2 # security issues around printf -Werror=date-time # usage of __DATE__ and __TIME__ macros -Werror=missing-field-initializers # logic -Wlogical-op -Wimplicit-fallthrough -Wduplicated-cond -Wduplicated-branches # other flags -ffunction-sections # place each function into its own section # better ASLR but larger executables -fstack-protector-all # emit check for buffer overflows on all functions -fstack-clash-protection # emit check for stack clash attacks ) endif() # add -Werror to release builds if((${CMAKE_BUILD_TYPE} MATCHES Release) OR (${CMAKE_BUILD_TYPE} MATCHES MinSizeRel)) # currently disabled #target_compile_options(${target} PRIVATE -Werror) endif() endfunction()