Skip to content

Instantly share code, notes, and snippets.

@vladiant
Created August 14, 2021 19:18
Show Gist options
  • Save vladiant/1216c204b303d8255f8704ad489baf3a to your computer and use it in GitHub Desktop.
Save vladiant/1216c204b303d8255f8704ad489baf3a to your computer and use it in GitHub Desktop.
C++ compiler flags
# NOTE: all useful flags for up to GCC 8.1 and Clang 7.0 were added (not included into -Wall -Wextra)
# * https://gcc.gnu.org/onlinedocs/gcc/Warning-Options.html
# * https://gcc.gnu.org/onlinedocs/gcc/C-Dialect-Options.html
# * https://gcc.gnu.org/onlinedocs/gcc/C_002b_002b-Dialect-Options.html
# * https://clang.llvm.org/docs/DiagnosticsReference.html
set(qa_warn)
set(qa_c_warn)
set(qa_cxx_warn)
# WARN: Should be removed after turning on the warnings globally in all project
set(temp_warn)
list(APPEND temp_warn
## WARN
-Wall
-Wno-parentheses
-Wno-unused-variable
-Wno-unused-parameter
-Wno-unknown-pragmas
-Wno-switch
## ERROR
-Werror
-Wno-error=unused-value
)
# FIXED: error: 'void* memset(void*, int, size_t)' clearing an object of non-trivial type; use assignment or value-initialization instead
# <= ${API_BT}/Interfaces/Organizer/Types/SharedMemoryContainer.hpp
if(CMAKE_CXX_COMPILER_ID STREQUAL "GNU" AND CMAKE_CXX_COMPILER_VERSION VERSION_GREATER_EQUAL "8.0.0")
list(APPEND temp_warn -Wno-error=class-memaccess)
endif()
### Main
list(APPEND qa_warn
-Wall # MAYBE: -Wno-unused-variable -Wno-unused-but-set-parameter
-Wextra
-Wpedantic
-Wuninitialized
-Wmissing-include-dirs
-Wshadow
-Wundef
-Winvalid-pch
)
### Extra
list(APPEND qa_warn
## Control flow
-Winit-self
-Wswitch-enum -Wswitch-default
-Wformat=2 -Wformat-nonliteral -Wformat-security -Wformat-y2k
## Arithmetics
-Wdouble-promotion
-Wfloat-equal
-Wpointer-arith
## Cast and conversion
-Wstrict-overflow=5
-Wcast-qual
-Wcast-align
-Wconversion
-Wpacked
## Sanitizing
-Wstrict-aliasing -fstrict-aliasing
-Wredundant-decls
-Wmissing-declarations
-Wmissing-field-initializers
## Security
-Wwrite-strings
# -Wstack-protector -fstack-protector # TEMP:DISABLED: we have questionable usage of arrays < 8 bytes everywhere
# -Wpadded # TEMP:DISABLED: too much noise from our Types, re-enable after fixing them
# -Winline # TEMP:DISABLED: each inherited interface requires creating its own .cpp file with empty ctor, re-enable after removing parasitic interfaces
-Wdisabled-optimization
)
list(APPEND qa_c_warn
-Waggregate-return
-Wbad-function-cast
-Wc++-compat
)
list(APPEND qa_cxx_warn
# -Weffc++ # BAD: requires init std::string(), etc in ctor BUT: some checks are useful
-Wzero-as-null-pointer-constant
-Wctor-dtor-privacy
# OBSOLETE: -Wsign-promo
-Wold-style-cast
-Woverloaded-virtual
)
if(CMAKE_CXX_COMPILER_ID STREQUAL "GNU")
list(APPEND qa_warn
# -pedantic-errors
# -Wchkp -fcheck-pointer-bounds # THINK:RQ: -mmpx
-Wlogical-op
-Wstack-usage=1024 -fstack-usage # -Wframe-larger-than=1024
-Wtrampolines
-Wvector-operation-performance
## Advices
# -Wsuggest-attribute=const # BAD: requires gcc-specific [[gnu::const]] modifier
)
list(APPEND qa_cxx_warn
-Wuseless-cast
-Wnoexcept
-Wstrict-null-sentinel
)
endif()
if(CMAKE_CXX_COMPILER_ID STREQUAL "Clang")
list(APPEND qa_warn
-Werror=option-ignored
-Warc-repeated-use-of-weak
-Wbitfield-enum-conversion
-Wc++11-compat-pedantic
# -Wc++98-c++11-c++14-compat-pedantic # THINK: enable for api-bt only
-Wclass-varargs
-Wconditional-uninitialized
-Wthread-safety
## Mistakes
-Wconsumed
-Wdirect-ivar-access
-Wdisabled-macro-expansion
-Wembedded-directive
-Wexit-time-destructors
-Wexpansion-to-defined
-Wformat-pedantic
-Widiomatic-parentheses
-Winconsistent-missing-destructor-override
-Winfinite-recursion
-Wlocal-type-template-args -Wno-c++98-compat-local-type-template-args # WARN: both must be enabled for api_bt
-Wloop-analysis
-Wmethod-signatures
-Wmismatched-tags
-Wmissing-braces
-Wmissing-prototypes
-Wmissing-variable-declarations
-Wmost
-Wmove
-Wnonportable-system-include-path
-Wnull-pointer-arithmetic
-Wover-aligned
-Woverriding-method-mismatch
-Wpch-date-time
-Wpragmas
-Wreserved-id-macro
-Wreserved-user-defined-literal
-Wretained-language-linkage
-Wsemicolon-before-method-body
-Wsometimes-uninitialized
-Wstring-conversion
-Wsuper-class-method-mismatch
-Wtautological-compare
-Wundefined-reinterpret-cast
-Wunreachable-code
-Wunreachable-code-break
-Wunreachable-code-loop-increment
-Wunreachable-code-return
-Wvector-conversion
## Sanitizing
-Wcomma
-Wduplicate-enum
-Wduplicate-method-arg
-Wduplicate-method-match
-Wdynamic-exception-spec
-Wempty-translation-unit
-Wexplicit-ownership-type
-Wignored-qualifiers
-Wimplicit
-Wkeyword-macro
-Wnewline-eof
-Wredundant-parens
-Wstatic-in-inline
-Wstrict-prototypes
-Wweak-template-vtables
-Wweak-vtables
-Wzero-length-array
## Arrays
-Warray-bounds-pointer-arithmetic
-Wextended-offsetof
-Wflexible-array-extensions
## Arithmetics
-Wfloat-conversion
-Wfloat-overflow-conversion
-Wfloat-zero-conversion
-Wshorten-64-to-32
-Wsign-compare
-Wsign-conversion
## Advices
-Wcomment
-Wdocumentation
-Wdocumentation-pedantic
-Wglobal-constructors
-Wgnu
# -Wheader-hygiene
-Wunneeded-internal-declaration
-Wunneeded-member-function
-Wvla
)
endif()
if((CMAKE_CXX_COMPILER_ID STREQUAL "GNU" AND CMAKE_CXX_COMPILER_VERSION VERSION_GREATER_EQUAL "5.0.0")
OR (CMAKE_CXX_COMPILER_ID STREQUAL "Clang"))
list(APPEND qa_warn
-Wdate-time
)
endif()
if(CMAKE_CXX_COMPILER_ID STREQUAL "GNU" AND CMAKE_CXX_COMPILER_VERSION VERSION_GREATER_EQUAL "5.0.0")
list(APPEND qa_warn
## Advices
-Wsuggest-final-types
-Wsuggest-final-methods
#-Wsuggest-override # DISABLED: wrongly requires "override final" instead of simple "final" in 900+ places. BET:USE: clang build to search for missed "override" instead
)
list(APPEND qa_cxx_warn
-Wconditionally-supported
)
endif()
if((CMAKE_CXX_COMPILER_ID STREQUAL "GNU" AND CMAKE_CXX_COMPILER_VERSION VERSION_GREATER_EQUAL "6.0.0")
OR (CMAKE_CXX_COMPILER_ID STREQUAL "Clang"))
list(APPEND qa_warn
-Wshift-overflow
-Wshift-negative-value
-Wnull-dereference
)
endif()
if(CMAKE_CXX_COMPILER_ID STREQUAL "GNU" AND CMAKE_CXX_COMPILER_VERSION VERSION_GREATER_EQUAL "6.0.0")
list(APPEND qa_warn
-Wshift-overflow=2
-Wduplicated-cond
)
list(APPEND qa_cxx_warn
-Wvirtual-inheritance
## Advices
# -Wtemplates
# -Wmultiple-inheritance
)
endif()
if((CMAKE_CXX_COMPILER_ID STREQUAL "GNU" AND CMAKE_CXX_COMPILER_VERSION VERSION_GREATER_EQUAL "7.0.0")
OR (CMAKE_CXX_COMPILER_ID STREQUAL "Clang"))
list(APPEND qa_warn
-Wunused-macros
)
endif()
if(CMAKE_CXX_COMPILER_ID STREQUAL "GNU" AND CMAKE_CXX_COMPILER_VERSION VERSION_GREATER_EQUAL "7.0.0")
list(APPEND qa_warn
-Wstringop-overflow=4
-Wduplicated-branches
-Walloc-zero
-Walloca
)
endif()
if(CMAKE_CXX_COMPILER_ID STREQUAL "GNU" AND CMAKE_CXX_COMPILER_VERSION VERSION_GREATER_EQUAL "8.0.0")
list(APPEND qa_warn
-Wcast-align=strict
-Wstringop-truncation
)
endif()
if((CMAKE_CXX_COMPILER_ID STREQUAL "GNU" AND CMAKE_CXX_COMPILER_VERSION VERSION_GREATER_EQUAL "8.0.0")
OR (CMAKE_CXX_COMPILER_ID STREQUAL "Clang" AND CMAKE_CXX_COMPILER_VERSION VERSION_GREATER_EQUAL "6.0.0"))
list(APPEND qa_cxx_warn
-Wextra-semi
)
endif()
if(CMAKE_CXX_COMPILER_ID STREQUAL "Clang" AND CMAKE_CXX_COMPILER_VERSION VERSION_GREATER_EQUAL "7.0.0")
list(APPEND qa_cxx_warn
-Wextended-offsetof
)
endif()
# TODO: split on debug/release set by: {performance, aim, applicability}
# i.e. CMAKE_CXX_FLAGS_{DEBUG,RELEASE,RELWITHDEBINFO}
#string(REPLACE ";" " " CMAKE_C_FLAGS "${CMAKE_C_FLAGS};${qa_c_warn};${qa_warn}")
#string(REPLACE ";" " " CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS};${qa_cxx_warn};${qa_warn}")
# WARN: Should be removed after turning on the warnings globally in all project
string(REPLACE ";" " " CMAKE_C_FLAGS "${CMAKE_C_FLAGS};${temp_warn}")
string(REPLACE ";" " " CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS};${temp_warn}")
# NOTE: all useful flags for up to GCC 8.1 and Clang 7.0 were added (not included into -Wall -Wextra)
# * https://gcc.gnu.org/onlinedocs/gcc/Warning-Options.html
# * https://gcc.gnu.org/onlinedocs/gcc/C-Dialect-Options.html
# * https://gcc.gnu.org/onlinedocs/gcc/C_002b_002b-Dialect-Options.html
# * https://clang.llvm.org/docs/DiagnosticsReference.html
set(qa_warn)
set(qa_c_warn)
set(qa_cxx_warn)
### Main
list(APPEND qa_warn
-Wall # MAYBE: -Wno-unused-variable -Wno-unused-but-set-parameter
-Wextra
-Wpedantic
-Wuninitialized
-Wmissing-include-dirs
-Wshadow
-Wundef
-Winvalid-pch
)
### Extra
list(APPEND qa_warn
## Control flow
-Winit-self
-Wswitch-enum -Wswitch-default
-Wformat=2 -Wformat-nonliteral -Wformat-security -Wformat-y2k
## Arithmetics
-Wdouble-promotion
-Wfloat-equal
-Wpointer-arith
## Cast and conversion
-Wstrict-overflow=5
-Wcast-qual
-Wcast-align
-Wconversion
-Wpacked
## Sanitizing
-Wstrict-aliasing -fstrict-aliasing
-Wredundant-decls
-Wmissing-declarations
-Wmissing-field-initializers
## Security
-Wwrite-strings
# -Wstack-protector -fstack-protector # TEMP:DISABLED: we have questionable usage of arrays < 8 bytes everywhere
# -Wpadded # TEMP:DISABLED: too much noise from our Types, re-enable after fixing them
-Winline
-Wdisabled-optimization
)
list(APPEND qa_c_warn
-Waggregate-return
-Wbad-function-cast
-Wc++-compat
)
list(APPEND qa_cxx_warn
# -Weffc++ # BAD: requires init std::string(), etc in ctor BUT: some checks are useful
-Wzero-as-null-pointer-constant
-Wctor-dtor-privacy
# OBSOLETE: -Wsign-promo
-Wold-style-cast
-Woverloaded-virtual
)
if(CMAKE_CXX_COMPILER_ID STREQUAL "GNU")
list(APPEND qa_warn
# -pedantic-errors
# -Wchkp -fcheck-pointer-bounds # THINK:RQ: -mmpx
-Wlogical-op
-Wstack-usage=1024 -fstack-usage # -Wframe-larger-than=1024
-Wtrampolines
-Wvector-operation-performance
## Advices
# -Wsuggest-attribute=const # BAD: requires gcc-specific [[gnu::const]] modifier
)
list(APPEND qa_cxx_warn
-Wuseless-cast
-Wnoexcept
-Wstrict-null-sentinel
)
endif()
if(CMAKE_CXX_COMPILER_ID STREQUAL "Clang")
list(APPEND qa_warn
-Werror=option-ignored
-Warc-repeated-use-of-weak
-Wbitfield-enum-conversion
-Wc++11-compat-pedantic
# -Wc++98-c++11-c++14-compat-pedantic # THINK: enable for api-bt only
-Wclass-varargs
-Wconditional-uninitialized
-Wthread-safety
## Mistakes
-Wconsumed
-Wdirect-ivar-access
-Wdisabled-macro-expansion
-Wembedded-directive
-Wexit-time-destructors
-Wexpansion-to-defined
-Wformat-pedantic
-Widiomatic-parentheses
-Winconsistent-missing-destructor-override
-Winfinite-recursion
-Wlocal-type-template-args
-Wloop-analysis
-Wmethod-signatures
-Wmismatched-tags
-Wmissing-braces
-Wmissing-prototypes
-Wmissing-variable-declarations
-Wmost
-Wmove
-Wnonportable-system-include-path
-Wnull-pointer-arithmetic
-Wover-aligned
-Woverriding-method-mismatch
-Wpch-date-time
-Wpragmas
-Wreserved-id-macro
-Wreserved-user-defined-literal
-Wretained-language-linkage
-Wsemicolon-before-method-body
-Wsometimes-uninitialized
-Wstring-conversion
-Wsuper-class-method-mismatch
-Wtautological-compare
-Wundefined-reinterpret-cast
-Wunreachable-code
-Wunreachable-code-break
-Wunreachable-code-loop-increment
-Wunreachable-code-return
-Wvector-conversion
## Sanitizing
-Wcomma
-Wduplicate-enum
-Wduplicate-method-arg
-Wduplicate-method-match
-Wdynamic-exception-spec
-Wempty-translation-unit
-Wexplicit-ownership-type
-Wignored-qualifiers
-Wimplicit
-Wkeyword-macro
-Wnewline-eof
-Wredundant-parens
-Wstatic-in-inline
-Wstrict-prototypes
-Wweak-template-vtables
-Wweak-vtables
-Wzero-length-array
## Arrays
-Warray-bounds-pointer-arithmetic
-Wextended-offsetof
-Wflexible-array-extensions
## Arithmetics
-Wfloat-conversion
-Wfloat-overflow-conversion
-Wfloat-zero-conversion
-Wshorten-64-to-32
-Wsign-compare
-Wsign-conversion
## Advices
-Wcomment
-Wdocumentation
-Wdocumentation-pedantic
-Wglobal-constructors
-Wgnu
# -Wheader-hygiene
-Wunneeded-internal-declaration
-Wunneeded-member-function
-Wvla
)
endif()
if((CMAKE_CXX_COMPILER_ID STREQUAL "GNU" AND CMAKE_CXX_COMPILER_VERSION VERSION_GREATER_EQUAL "5.0.0")
OR (CMAKE_CXX_COMPILER_ID STREQUAL "Clang"))
list(APPEND qa_warn
-Wdate-time
)
endif()
if(CMAKE_CXX_COMPILER_ID STREQUAL "GNU" AND CMAKE_CXX_COMPILER_VERSION VERSION_GREATER_EQUAL "5.0.0")
list(APPEND qa_warn
## Advices
-Wsuggest-final-types
-Wsuggest-final-methods
-Wsuggest-override
)
list(APPEND qa_cxx_warn
-Wconditionally-supported
)
endif()
if((CMAKE_CXX_COMPILER_ID STREQUAL "GNU" AND CMAKE_CXX_COMPILER_VERSION VERSION_GREATER_EQUAL "6.0.0")
OR (CMAKE_CXX_COMPILER_ID STREQUAL "Clang"))
list(APPEND qa_warn
-Wshift-overflow
-Wshift-negative-value
-Wnull-dereference
)
endif()
if(CMAKE_CXX_COMPILER_ID STREQUAL "GNU" AND CMAKE_CXX_COMPILER_VERSION VERSION_GREATER_EQUAL "6.0.0")
list(APPEND qa_warn
-Wshift-overflow=2
-Wduplicated-cond
)
list(APPEND qa_cxx_warn
-Wvirtual-inheritance
## Advices
# -Wtemplates
# -Wmultiple-inheritance
)
endif()
if((CMAKE_CXX_COMPILER_ID STREQUAL "GNU" AND CMAKE_CXX_COMPILER_VERSION VERSION_GREATER_EQUAL "7.0.0")
OR (CMAKE_CXX_COMPILER_ID STREQUAL "Clang"))
list(APPEND qa_warn
-Wunused-macros
)
endif()
if(CMAKE_CXX_COMPILER_ID STREQUAL "GNU" AND CMAKE_CXX_COMPILER_VERSION VERSION_GREATER_EQUAL "7.0.0")
list(APPEND qa_warn
-Wstringop-overflow=4
-Wduplicated-branches
-Walloc-zero
-Walloca
)
endif()
if(CMAKE_CXX_COMPILER_ID STREQUAL "GNU" AND CMAKE_CXX_COMPILER_VERSION VERSION_GREATER_EQUAL "8.0.0")
list(APPEND qa_warn
-Wcast-align=strict
-Wstringop-truncation
)
endif()
if((CMAKE_CXX_COMPILER_ID STREQUAL "GNU" AND CMAKE_CXX_COMPILER_VERSION VERSION_GREATER_EQUAL "8.0.0")
OR (CMAKE_CXX_COMPILER_ID STREQUAL "Clang" AND CMAKE_CXX_COMPILER_VERSION VERSION_GREATER_EQUAL "6.0.0"))
list(APPEND qa_cxx_warn
-Wextra-semi
)
endif()
# TODO: split on debug/release set by: {performance, aim, applicability}
# i.e. CMAKE_CXX_FLAGS_{DEBUG,RELEASE,RELWITHDEBINFO}
string(REPLACE ";" " " CMAKE_C_FLAGS "${CMAKE_C_FLAGS};${qa_c_warn};${qa_warn}")
string(REPLACE ";" " " CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS};${qa_cxx_warn};${qa_warn}")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment