CompilerWarnings.cmake 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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(${PROJECT_NAME}_WARNINGS_AS_ERRORS "Treat compiler warnings as errors" TRUE)
  6. set(MSVC_WARNINGS
  7. /W4 # Baseline reasonable warnings
  8. /w14242 # 'identfier': conversion from 'type1' to 'type1', possible loss
  9. # of data
  10. /w14254 # 'operator': conversion from 'type1:field_bits' to
  11. # 'type2:field_bits', possible loss of data
  12. /w14263 # 'function': member function does not override any base class
  13. # virtual member function
  14. /w14265 # 'classname': class has virtual functions, but destructor is not
  15. # virtual instances of this class may not be destructed correctly
  16. /w14287 # 'operator': unsigned/negative constant mismatch
  17. /we4289 # nonstandard extension used: 'variable': loop control variable
  18. # declared in the for-loop is used outside the for-loop scope
  19. /w14296 # 'operator': expression is always 'boolean_value'
  20. /w14311 # 'variable': pointer truncation from 'type1' to 'type2'
  21. /w14545 # expression before comma evaluates to a function which is missing
  22. # an argument list
  23. /w14546 # function call before comma missing argument list
  24. /w14547 # 'operator': operator before comma has no effect; expected
  25. # operator with side-effect
  26. /w14549 # 'operator': operator before comma has no effect; did you intend
  27. # '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 'type_2' is sign-extended. This may
  32. # cause unexpected runtime behavior.
  33. /w14905 # wide string literal cast to 'LPSTR'
  34. /w14906 # string literal cast to 'LPWSTR'
  35. /w14928 # illegal copy-initialization; more than one user-defined
  36. # conversion has been implicitly applied
  37. # Disable the following errors
  38. /wd4101
  39. /wd4201
  40. )
  41. set(CLANG_WARNINGS
  42. -Wall
  43. -Wextra # reasonable and standard
  44. -Wshadow # warn the user if a variable declaration shadows one from a
  45. # parent context
  46. -Wnon-virtual-dtor # warn the user if a class with virtual functions has a
  47. # non-virtual destructor. This helps catch hard to
  48. # track down memory errors
  49. -Wold-style-cast # warn for c-style casts
  50. -Wcast-align # warn for potential performance problem casts
  51. -Wunused # warn on anything being unused
  52. -Woverloaded-virtual # warn if you overload (not override) a virtual
  53. # function
  54. -Wpedantic # warn if non-standard C++ is used
  55. -Wconversion # warn on type conversions that may lose data
  56. -Wsign-conversion # warn on sign conversions
  57. -Wnull-dereference # warn if a null dereference is detected
  58. -Wdouble-promotion # warn if float is implicit promoted to double
  59. -Wformat=2 # warn on security issues around functions that format output
  60. # (ie printf)
  61. -Wno-gnu-zero-variadic-macro-arguments
  62. )
  63. if (${PROJECT_NAME}_WARNINGS_AS_ERRORS)
  64. set(CLANG_WARNINGS ${CLANG_WARNINGS} -Werror)
  65. set(MSVC_WARNINGS ${MSVC_WARNINGS} /WX )
  66. endif()
  67. set(GCC_WARNINGS
  68. ${CLANG_WARNINGS}
  69. -Wmisleading-indentation # warn if identation implies blocks where blocks
  70. # do not exist
  71. -Wduplicated-cond # warn if if / else chain has duplicated conditions
  72. -Wduplicated-branches # warn if if / else branches have duplicated code
  73. -Wlogical-op # warn about logical operations being used where bitwise were
  74. # probably wanted
  75. # -Wuseless-cast # warn if you perform a cast to the same type
  76. -Wno-class-memaccess
  77. -Wno-stringop-overflow
  78. -Wsuggest-override
  79. -Wno-gnu-zero-variadic-macro-arguments
  80. )
  81. if(MSVC)
  82. set(_PROJECT_WARNINGS ${MSVC_WARNINGS})
  83. elseif(CMAKE_CXX_COMPILER_ID STREQUAL "Clang")
  84. set(_PROJECT_WARNINGS ${CLANG_WARNINGS})
  85. else()
  86. set(_PROJECT_WARNINGS ${GCC_WARNINGS})
  87. endif()
  88. get_target_property(type ${project_name} TYPE)
  89. message("--->${type}")
  90. if (${type} STREQUAL "INTERFACE_LIBRARY")
  91. target_compile_options(${project_name} INTERFACE ${_PROJECT_WARNINGS})
  92. else()
  93. target_compile_options(${project_name} PRIVATE ${_PROJECT_WARNINGS})
  94. endif()
  95. endfunction()