配置 Clang 诊断
使用 Clang-Tidy 和 Clazy 检测 C、C++ 和 Objective-C 程序中的问题。
选择并编辑要执行的 Clang 检查:
- 选择首选项>Analyzer >Clang Tools 。
- Diagnostic configuration 字段显示要执行的检查。选择该字段的值可打开Diagnostic Configurations 对话框,在此可选择和编辑要执行的检查。
要从 Clang-Tidy 配置文件执行检查,请在Clang Tools 选项卡中选择Prefer .clang-tidy file, if present 。
- 选择Copy 可复制所选诊断以供编辑。
- 在Diagnostic configuration name 字段中,为配置命名,然后选择OK 。
- 在Clang Warnings 选项卡中,选择Use diagnostic flags from the build system 复选框,将诊断标记(如警告标记)从构建系统转发到 Clang 代码模型,以便在代码编辑器中显示注释。
- 在Clang-Tidy Checks 选项卡中,选择Select Checks 以选择要执行的检查。要过滤检查,请在Filter by name 字段中输入字符串。
有关可用检查器的更多信息,请参阅Clang Static Analyzer 文档。
- 要以纯文本编辑所选检查,请选择Edit Checks as String 。
- 在Clazy Checks 选项卡中,选择要执行的 Clazy 检查级别。
- 要以纯文本编辑所选检查,请选择Edit Checks as String 。
- 在Filters 字段中,选择主题,以便只查看与Checks 字段中的区域相关的检查。要过滤所选区域中的检查,请在Filter by name 字段中输入字符串。
- 要再次查看所有检查,请选择Reset Topic Filter 。
- 要在线查看有关检查的更多信息,请选择检查旁边的Web Page 链接。
选择 Clazy 检查级别
Clazy 检查分为 0 至 3 级。 0 级检查非常稳定,几乎不会出现误报,而 3 级检查则是试验性的。您可以选择在每个级别执行的检查。要自动包含较低级别的检查,请选择Enable lower levels automatically 复选框。
创建 Clang-Tidy 配置文件
Clang-Tidy 从位于源文件最近的父目录中的 .clang-tidy 文件读取每个源文件的配置。如果任何配置选项都有相应的命令行选项,则命令行选项优先。可以使用-dump-config
查看有效配置。
Qt Creator 根据你选择的检查为你创建配置。要以文件格式存储检查,可创建 .clang-tidy 文件,如下所示:
- 选择Edit Checks as String 并复制字段内容。
- 将
clang-tidy -dump-config
的输出导入名为.clang-tidy
的文件。例如clang-tidy -checks=-*,bugprone-*,cppcoreguidelines-avoid-* -dump-config > .clang-tidy
- 将 .clang-tidy 文件移至源代码的父目录。
以后要使用Qt Creator 添加更多检查,可将 .clang-tidy 文件中的检查复制到Edit Checks as String 字段,选择其他检查,然后将字段内容复制粘贴到 .clang-tidy 文件。
Copyright © The Qt Company Ltd. and other contributors. Documentation contributions included herein are the copyrights of their respective owners. The documentation provided herein is licensed under the terms of the GNU Free Documentation License version 1.3 as published by the Free Software Foundation. Qt and respective logos are trademarks of The Qt Company Ltd in Finland and/or other countries worldwide. All other trademarks are property of their respective owners.