|
| 1 | +# from here: |
| 2 | +# |
| 3 | +# https://github.com/lefticus/cppbestpractices/blob/master/02-Use_the_Tools_Available.md |
| 4 | + |
| 5 | +function( |
| 6 | + set_compiler_warnings |
| 7 | + target_name |
| 8 | + MSVC_WARNINGS |
| 9 | + CLANG_WARNINGS |
| 10 | + INTEL_WARNINGS) |
| 11 | + if("${MSVC_WARNINGS}" STREQUAL "") |
| 12 | + set(MSVC_WARNINGS |
| 13 | + /W4 # Baseline reasonable warnings |
| 14 | + /w14242 # 'identifier': conversion from 'type1' to 'type2', possible loss of data |
| 15 | + /w14254 # 'operator': conversion from 'type1:field_bits' to 'type2:field_bits', possible loss of data |
| 16 | + /w14263 # 'function': member function does not override any base class virtual member function |
| 17 | + /w14265 # 'classname': class has virtual functions, but destructor is not virtual instances of this class may not |
| 18 | + # be destructed correctly |
| 19 | + /w14287 # 'operator': unsigned/negative constant mismatch |
| 20 | + /we4289 # nonstandard extension used: 'variable': loop control variable declared in the for-loop is used outside |
| 21 | + # the for-loop scope |
| 22 | + /w14296 # 'operator': expression is always 'boolean_value' |
| 23 | + /w14311 # 'variable': pointer truncation from 'type1' to 'type2' |
| 24 | + /w14545 # expression before comma evaluates to a function which is missing an argument list |
| 25 | + /w14546 # function call before comma missing argument list |
| 26 | + /w14547 # 'operator': operator before comma has no effect; expected operator with side-effect |
| 27 | + /w14549 # 'operator': operator before comma has no effect; did you intend 'operator'? |
| 28 | + /w14555 # expression has no effect; expected expression with side- effect |
| 29 | + /w14619 # pragma warning: there is no warning number 'number' |
| 30 | + /w14640 # Enable warning on thread un-safe static member initialization |
| 31 | + /w14826 # Conversion from 'type1' to 'type2' is sign-extended. This may cause unexpected runtime behavior. |
| 32 | + /w14905 # wide string literal cast to 'LPSTR' |
| 33 | + /w14906 # string literal cast to 'LPWSTR' |
| 34 | + /w14928 # illegal copy-initialization; more than one user-defined conversion has been implicitly applied |
| 35 | + /permissive- # standards conformance mode for MSVC compiler. |
| 36 | + /WX # Treat warnings as errors |
| 37 | + ) |
| 38 | + endif() |
| 39 | + |
| 40 | + if("${CLANG_WARNINGS}" STREQUAL "") |
| 41 | + set(CLANG_WARNINGS |
| 42 | + -Wall |
| 43 | + -Wextra # reasonable and standard |
| 44 | + -Wshadow # warn the user if a variable declaration shadows one from a parent context |
| 45 | + -Wnon-virtual-dtor # warn the user if a class with virtual functions has a non-virtual destructor. This helps |
| 46 | + # catch hard to track down memory errors |
| 47 | + -Wold-style-cast # warn for c-style casts |
| 48 | + -Wcast-align # warn for potential performance problem casts |
| 49 | + -Wunused # warn on anything being unused |
| 50 | + -Woverloaded-virtual # warn if you overload (not override) a virtual function |
| 51 | + -Wpedantic # warn if non-standard C++ is used |
| 52 | + -Wconversion # warn on type conversions that may lose data |
| 53 | + -Wsign-conversion # warn on sign conversions |
| 54 | + -Wnull-dereference # warn if a null dereference is detected |
| 55 | + -Wdouble-promotion # warn if float is implicit promoted to double |
| 56 | + -Wformat=2 # warn on security issues around functions that format output (ie printf) |
| 57 | + -Wimplicit-fallthrough # warn on statements that fallthrough without an explicit annotation |
| 58 | + -Werror # treat warnings as errors |
| 59 | + ) |
| 60 | + endif() |
| 61 | + |
| 62 | + if("${GCC_WARNINGS}" STREQUAL "") |
| 63 | + set(GCC_WARNINGS |
| 64 | + ${CLANG_WARNINGS} |
| 65 | + -Wmisleading-indentation # warn if indentation implies blocks where blocks do not exist |
| 66 | + -Wduplicated-cond # warn if if / else chain has duplicated conditions |
| 67 | + -Wduplicated-branches # warn if if / else branches have duplicated code |
| 68 | + -Wlogical-op # warn about logical operations being used where bitwise were probably wanted |
| 69 | + -Wuseless-cast # warn if you perform a cast to the same type |
| 70 | + -Wsuggest-override # warn if an overridden member function is not marked 'override' or 'final' |
| 71 | + ) |
| 72 | + endif() |
| 73 | + |
| 74 | + if("${INTEL_WARNINGS}" STREQUAL "") |
| 75 | + if(WIN32) |
| 76 | + set(INTEL_WARNINGS |
| 77 | + /Wall |
| 78 | + /Wshadow # warn when a variable declaration hides a previous declaration |
| 79 | + /Wunused-variable # warn if a local or non-constant static variable is unused |
| 80 | + /Wunused-function # warn if a declared function is not used |
| 81 | + /Wformat # whether argument checking is enabled for calls to printf, scanf, and so forth |
| 82 | + /Werror-all # Treat all warnings as errors |
| 83 | + ) |
| 84 | + else() |
| 85 | + set(INTEL_WARNINGS |
| 86 | + -Wall |
| 87 | + -Wshadow # warn when a variable declaration hides a previous declaration |
| 88 | + -Wunused-variable # warn if a local or non-constant static variable is unused |
| 89 | + -Wunused-function # warn if a declared function is not used |
| 90 | + -Woverloaded-virtual # warn if you overload (not override) a virtual function |
| 91 | + -Wformat # whether argument checking is enabled for calls to printf, scanf, and so forth |
| 92 | + -Werror-all # Treat all warnings as errors |
| 93 | + ) |
| 94 | + endif() |
| 95 | + endif() |
| 96 | + |
| 97 | + if(MSVC) |
| 98 | + set(PROJECT_WARNINGS_CXX ${MSVC_WARNINGS}) |
| 99 | + elseif(CMAKE_CXX_COMPILER_ID MATCHES ".*Clang") |
| 100 | + set(PROJECT_WARNINGS_CXX ${CLANG_WARNINGS}) |
| 101 | + elseif(CMAKE_CXX_COMPILER_ID STREQUAL "GNU") |
| 102 | + set(PROJECT_WARNINGS_CXX ${GCC_WARNINGS}) |
| 103 | + elseif(CMAKE_CXX_COMPILER_ID STREQUAL "IntelLLVM") |
| 104 | + set(PROJECT_WARNINGS_CXX ${INTEL_WARNINGS}) |
| 105 | + else() |
| 106 | + message(AUTHOR_WARNING "No compiler warnings set for CXX compiler: '${CMAKE_CXX_COMPILER_ID}'") |
| 107 | + endif() |
| 108 | + |
| 109 | + target_compile_options( |
| 110 | + ${target_name} |
| 111 | + INTERFACE |
| 112 | + $<$<COMPILE_LANGUAGE:CXX>:${PROJECT_WARNINGS_CXX}> |
| 113 | + ) |
| 114 | +endfunction() |
| 115 | + |
| 116 | +add_library(compiler_warnings_settings INTERFACE) |
| 117 | +set_compiler_warnings( |
| 118 | + compiler_warnings_settings |
| 119 | + "" |
| 120 | + "" |
| 121 | + "" |
| 122 | +) |
0 commit comments