summaryrefslogtreecommitdiff
path: root/cmake/CompilerWarnings.cmake
blob: 895a7e88823bba1f953179a1c35e5dc27fcc261b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# compiler warnings based on: https://lefticus.gitbooks.io/cpp-best-practices/content/02-Use_the_Tools_Available.html

# only enable warnings for debug builds
if (NOT ${CMAKE_BUILD_TYPE} STREQUAL Debug)
    return()
endif ()

set(BASE_WARNINGS
    -Wall -Wextra # reasonable and standard
    -Wpedantic # warn if non-standard c++ is used
    -Werror=shadow # warn the user if a variable shadows one from a parent context
    -Wunused # warn on anything being unused

    -Werror=non-virtual-dtor # warn the user if a class with virtual functions has a non-virtual dtor
    -Werror=overloaded-virtual # warn if you overload (not override) a virtual function

    -Werror=old-style-cast # warn for c-style casts
    -Wcast-align # warn for potential performance problem casts

    -Wconversion # warn on type conversions that may lose data
    -Wsign-conversion # warn on sign conversions
    -Wdouble-promotion # warn if float is implicit promoted to double

    -Wnull-dereference # warn if a null dereference is detected
    -Wformat=2 # warn on security issues around functions that format output (ie printf)
)

set(GCC_WARNINGS
    -Wmisleading-indentation # (only in GCC >= 6.0) warn if indentation implies blocks where blocks do not exist
    -Wduplicated-cond # (only in GCC >= 6.0) warn if if / else chain has duplicated conditions
    -Wduplicated-branches # (only in GCC >= 7.0) warn if if / else branches have duplicated code
    -Wlogical-op # (only in GCC) warn about logical operations being used where bitwise were probably wanted
    -Wnull-dereference # (only in GCC >= 6.0) warn if a null dereference is detected
    -Wuseless-cast # (only in GCC >= 4.8) warn if you perform a cast to the same type
)

if (${CMAKE_CXX_COMPILER_ID} MATCHES Clang)
    message(STATUS "Adding more ${CMAKE_CXX_COMPILER_ID} compiler warnings")
    add_compile_options(${BASE_WARNINGS})
elseif (${CMAKE_CXX_COMPILER_ID} MATCHES GNU)
    message(STATUS "Adding more ${CMAKE_CXX_COMPILER_ID} compiler warnings")
    add_compile_options(${BASE_WARNINGS} ${GCC_WARNINGS})
endif ()