【问题标题】:use cppcheck without defining all the macros使用 cppcheck 而不定义所有宏
【发布时间】:2016-02-02 02:56:07
【问题描述】:

我在使用 boost 库的项目中使用 cppcheck。这个库中的标头包含大量宏,我什至没有在我的源代码中使用它们。尽管如此,cppcheck 根据这些我认为没用的宏来探索路径。有没有办法告诉 cppcheck 忽略所有宏,除非它是使用 #define 在源代码中定义的?

【问题讨论】:

    标签: c++ cppcheck


    【解决方案1】:

    这是 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找到。

    【讨论】:

      【解决方案2】:

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

      请参阅-D/-U 选项。

      【讨论】:

      • 所以如果我有 10 个宏,并且如果我定义了其中的 3 个(在编译时或在我的源代码中),我是否需要取消其他 7 个?
      • @Brahim:如果你有类似#ifdef UNSET_MACRO1 的东西,可以避免同时检查两条路径。
      【解决方案3】:

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

      // cppcheck-suppress unknownMacro
      

      【讨论】:

        【解决方案4】:

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

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2014-04-05
          • 1970-01-01
          • 2014-08-14
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多