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