From 7fd34ac8a2962c02e6dede2c3e656353e8534fbd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Marjam=C3=A4ki?= Date: Mon, 11 Mar 2019 13:11:17 +0100 Subject: [PATCH] Manual: Add chapter about speeding up Cppcheck analysis --- man/manual.md | 81 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 81 insertions(+) diff --git a/man/manual.md b/man/manual.md index cc64df9b8..192175ed0 100644 --- a/man/manual.md +++ b/man/manual.md @@ -593,6 +593,87 @@ Newline Carriage return +## Speeding up Cppcheck + +It is possible to speed up Cppcheck analysis in a few different ways. + +### Preprocessor configurations + +Imagine this source code: + + void foo() + { + int x; + #ifdef __GNUC__ + x = 0; + #endif + #ifdef __MSCVER + x = 1; + #endif + return x; + } + +By default Cppcheck will try to check all the configurations. There are 3 important configurations here: + * Neither `__GNUC__` nor `__MSCVER` is defined + * `__GNUC__` is defined + * `__MSCVER` is defined + +When you run Cppcheck, the output will be something like: + + $ cppcheck test.c + Checking test.c ... + [test.c:10]: (error) Uninitialized variable: x + Checking test.c: __GNUC__... + Checking test.c: __MSCVER... + +Now if you want you can limit the analysis. You probably know what the target compiler is. If `-D` is supplied and you do not specify `--force` then Cppcheck will only check the configuration you give. + + $ cppcheck -D __GNUC__ test.c + Checking test.c ... + Checking test.c: __GNUC__=1... + + +### Unused templates + +If you think Cppcheck is slow and you are using templates, then you should try how it works to remove unused templates. + +Imagine this code: + + template struct Foo { + T x = 100; + }; + + template struct Bar { + T x = 200 / 0; + }; + + int main() { + Foo foo; + return 0; + } + +Cppcheck says: + + $ cppcheck test.cpp + Checking test.cpp ... + [test.cpp:7]: (error) Division by zero. + +It complains about division by zero in `Bar` even though `Bar` is not instantiated. + +You can use the option `--remove-unused-templates` to remove unused templates from Cppcheck analysis. + +Example: + + $ cppcheck --remove-unused-templates test.cpp + Checking test.cpp ... + +This lost message is in theory not critical, since `Bar` is not instantiated the division by zero should not occur in your real program. + +The speedup you get can be remarkable. + +### Check headers + +TBD ## Misra