Warnings#
Warnings namely, compiler warnings, static analysis warnings, and runtime analysis warnings are important to identify and fix issues in code. This section will cover the various types of warnings and how to configure and use them in the development process.
Compiling Warnings#
Compiling warnings are the most common type of warnings. They are generated by the compiler when it detects potential errors or potential issues in the compiling process. Normally, these extra warnings are not visible to the developer unless they are enabled explicitly.
Configuration#
The warning flags can be customized to pass the CMake configurable options:
COMPILER_FLAGS_WARNINGS_GNU: Flags for gcc/clang compilers.COMPILER_FLAGS_WARNINGS_MSVC: Flags for msvc compilers.COMPILER_FLAGS_WARNINGS_CUDA: Flags for cuda compilers.CMAKE_COMPILE_WARNING_AS_ERROR: If treat warnings as errors. Default is false.COMPILER_FLAGS_SKIP_TARGETS_REGEXES: List of regexes to skip targets. Default is empty.
Note
All given options will be checked and applied only if the compiler supports them. Otherwise, the options will be ignored.
From now on, we are using the following optinos corresponding to the compilers used:
GCC/Clang#
-pedanticWarn on language extensions-Wall -Wextrareasonable and standard-Wshadowwarn the user if a variable declaration shadows one from a parent context-Wnon-virtual-dtorwarn the user if a class with virtual functions has a non-virtual destructor. This helps catch hard to track down memory errors-Wold-style-castwarn for c-style casts-Wcast-alignwarn for potential performance problem casts-Wunusedwarn on anything being unused-Woverloaded-virtualwarn if you overload (not override) a virtual function-Wpedantic(all versions of GCC, Clang >= 3.2) warn if non-standard C++ is used-Wconversionwarn on type conversions that may lose data-Wsign-conversion(Clang all versions, GCC >= 4.3) warn on sign conversions-Wmisleading-indentation(only in GCC >= 6.0) warn if indentation implies blocks where blocks do not exist-Wduplicated-cond(only in GCC >= 6.0) warn ifif/elsechain has duplicated conditions-Wduplicated-branches(only in GCC >= 7.0) warn ifif/elsebranches have duplicated code-Wlogical-op(only in GCC) warn about logical operations being used where bitwise were probably wanted-Wnull-dereference(only in GCC >= 6.0) warn if a null dereference is detected-Wuseless-cast(only in GCC >= 4.8) warn if you perform a cast to the same type-Wdouble-promotion(GCC >= 4.6, Clang >= 3.8) warn iffloatis implicitly promoted todouble-Wformat=2warn on security issues around functions that format output (i.e.,printf)-Wlifetime(only special branch of Clang currently) shows object lifetime issues-Wimplicit-fallthroughWarns when case statements fall-through. (Included with-Wextrain GCC, not in clang)
MSVC#
/permissive-- Enforces standards conformance./W4All reasonable warnings/w14242‘identfier’: conversion from ‘type1’ to ‘type1’, possible loss of data/w14254‘operator’: conversion from ‘type1:field_bits’ to ‘type2:field_bits’, possible loss of data/w14263‘function’: member function does not override any base class virtual member function/w14265‘classname’: class has virtual functions, but destructor is not virtual instances of this class may not be destructed correctly/w14287‘operator’: unsigned/negative constant mismatch/we4289nonstandard extension used: ‘variable’: loop control variable declared in the for-loop is used outside the for-loop scope/w14296‘operator’: expression is always ‘boolean_value’/w14311‘variable’: pointer truncation from ‘type1’ to ‘type2’/w14545expression before comma evaluates to a function which is missing an argument list/w14546function call before comma missing argument list/w14547‘operator’: operator before comma has no effect; expected operator with side-effect/w14549‘operator’: operator before comma has no effect; did you intend ‘operator’?/w14555expression has no effect; expected expression with side-effect/w14619pragma warning: there is no warning number ‘number’/w14640Enable warning on thread unsafe static member initialization/w14826Conversion from ‘type1’ to ‘type_2’ is sign-extended. This may cause unexpected runtime behavior./w14905wide string literal cast to ‘LPSTR’/w14906string literal cast to ‘LPWSTR’/w14928illegal copy-initialization; more than one user-defined conversion has been implicitly applied
references:
Static Analysis#
Clang-tidy#
The Clang-tidy tool is a clang-based C++ linter tool. It checks for style, performance, and security issues in C++ code.
It can be configured using the following options:
USE_CLANGTIDY: If use clang-tidy. Default is OFF.USE_CLANGTIDY_OPTIONS: Clang-tidy run options. Default is -extra-arg=-Wno-unknown-warning-option;-extra-arg=-Wno-ignored-optimization-argument;-extra-arg=-Wno-unused-command-line-argument;-p;<build_dir>.USE_CLANGTIDY_WARNINGS_AS_ERRORS: Treat clang-tidy warnings as errors. Default is OFF.
Cppcheck#
The Cppcheck tool is a static analysis tool for C/C++ code. It detects various types of bugs and offers a convenient way to suppress false positives.
It can be configured using the following options:
USE_CPPCHECK: If use cppcheck. Default is OFF.USE_CPPCHECK_OPTIONS: cppcheck run options. Default is –enable=style,performance,warning,portability;–inline-suppr;–suppress=cppcheckError;–suppress=internalAstError;–suppress=unmatchedSuppression;–suppress=passedByValue;–suppress=syntaxError;–suppress=preprocessorErrorDirective;–inconclusiveUSE_CPPCHECK_WARNINGS_AS_ERRORS: Treat cppcheck warnings as errors. Default is OFF.
Runtime Analysis#
Sanitizers#
Sanitizers are tools that can detect memory leaks, use-after-free, and other memory errors at runtime. They can be configured using the following options:
USE_SANITIZER: Default isOFF.OFF: Disable sanitizers.Address: Enable AddressSanitizer to detect most issues dealing with memory.Memory: Enable MemorySanitizer to detect uninitialized reads.Undefined: Enable UndefinedBehaviorSanitizer explicitly listed as resulting in undefined behaviour.Thread: Enable ThreadSanitizer to detect data races for multi-threaded code.Leak: Enable LeakSanitizer to detect memory leaks, or issues.CFI: Enable ControlFlowIntegrity to detect control flow undefined behavior.EnableMSVCAnnotations: Enable MSVC annotations. Default is OFF.
USE_SANITIZER_ASAN_FLAGS: /fsanitize=address /Zi;-g -fsanitize=address -fno-omit-frame-pointer;-g -fsanitize=addressUSE_SANITIZER_MSAN_FLAGS: /fsanitize=memory;-g -fsanitize=memory -fsanitize-memory-track-origins;-g -fsanitize=memoryUSE_SANITIZER_USAN_FLAGS: -g -fsanitize=undefinedUSE_SANITIZER_TSAN_FLAGS: -g -fsanitize=threadUSE_SANITIZER_LSAN_FLAGS: -g -fsanitize=leakUSE_SANITIZER_CFI_FLAGS: -g -fsanitize=cfiUSE_SANITIZER_EXTRA_FLAGS: Extra flags to pass to the sanitizer. Default to empty.USE_SANITIZER_BLACKLIST_FILE: Path to a blacklist file for Undefined sanitizer. Default to empty.USE_SANITIZER_SKIP_TARGETS_REGEXES: Regexes to skip targets to sanitize. Default to enable all targets instrumented.
Note
Thread can not work with Address and Leak sanitizers.
Memory can not work with Address, Leak, and Thread sanitizers.
Valgrind#
Valgrind is a tool for detecting memory leaks, errors, and other runtime issues. It can be configured using the following options:
USE_VALGRIND: If use valgrind to check memory issues. Default isOFF.USE_VALGRIND_OPTIONS: Default is –show-leak-kinds=all;–gen-suppressions=all;–track-origins=yes.USE_VALGRIND_SUPPRESSION_FILE: path to valgrind suppress config file.USE_VALGRIND_ENABLE_MEMCHECK: enable memory check with ctest command, e.g. ctest -T memcheck. Default is ON.
Note
Valgrind can not work with sanitizer. You should disable it before run valgrind on testsuit.