在不定义所有宏的情况下使用 cppcheck

use cppcheck without defining all the macros

我在使用 boost 库的项目上使用 cppcheck。这个库中的 headers 包含大量我什至没有在我的源代码中使用的宏。然而,cppcheck 探索路径取决于这些我认为无用的宏。有没有办法告诉 cppcheck 忽略所有宏,除非它是使用 #define 在源代码中定义的?

不完全是你想要的,但你可以指定 define 到 cppcheck 以便它只评估一个分支:

参见 -D/-U 选项。

这是 cppcheck 文档中的必要部分:

-D<ID>               Define preprocessor symbol. Unless --max-configs or
                     --force is used, Cppcheck will only check the given
                     configuration when -D is used.
                     Example: '-DDEBUG=1 -D__cplusplus'.
-U<ID>               Undefine preprocessor symbol. Use -U to explicitly
                     hide certain #ifdef <ID> code paths from checking.
                     Example: '-UDEBUG'

您可以使用这些选项定义 (-D) 或取消定义 (-U) 自定义预处理器符号。

另一个可能有趣的选项是

-f, --force          Force checking of all configurations in files. If used
                     together with '--max-configs=', the last option is the
                     one that is effective.

--max-configs=<limit>
                     Maximum number of configurations to check in a file
                     before skipping it. Default is '12'. If used together
                     with '--force', the last option is the one that is
                     effective.

意思如下:

cppcheck --force <PATH_TO_YOUR_CODE>

Cppcheck 验证预处理器路径的所有组合,这可能会导致大型代码库的检查时间过长。

可以找到相应的文档here

一种选择是通过将此注释放在有问题的行上方来忽略缺少宏的行:

// cppcheck-suppress unknownMacro

使用 --config-exclude= 排除包含无法管理的配置的目录。它只排除头文件。