CompilerWarnings.cmake 4.0 KB

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