使用 Clang-Tidy 和 Clazy 分析代码

注意: Clang 静态分析器检查是 Clang-Tidy 的一部分。要使用这些检查,必须为 Clang 工具创建自定义配置,并为 Clang-Tidy 启用它们。

分析当前文件

运行 Clang-Tidy 或 Clazy 分析当前打开的文件:

  • 选择编辑器工具栏上的分析文件 (Analyze File) ,然后选择工具:Clang-TidyClazy
  • 选择Tools >C++ >Analyze Current File with Clang-TidyAnalyze Current File with Clazy

分析打开的项目

要运行 Clang-Tidy 或 Clazy 来分析一个打开的项目,请执行下列操作

  1. 选择Analyze >Clang-TidyClazy
  2. 选择要应用检查的文件。

    文件分析对话框

  3. 选择Analyze 开始检查。

查看诊断

Clang-TidyClazy 视图显示问题:

Clang-Tidy 视图

Clang-Tidy 视图中的诊断。

注意: 如果在模式选择器中选择Debug 以打开Debug 模式,然后选择Clang-TidyClazy ,则必须选择分析项目 (Analyze Project) 按钮才能打开Files to Analyze 对话框。

双击问题可移动到问题出现在代码编辑器中的位置。

如果某个问题存在 fixit,您可以选择问题旁边的复选框来安排修复。选择Select Fixits 复选框可选择所有修复程序。要查看问题的状态,请将鼠标指针悬停在复选框旁边的图标上。

要查看标有灯泡图标 图标的问题的更多信息,请将鼠标指针悬停在该行上。

选择设置 按钮,自定义当前项目的诊断。

过滤诊断

过滤诊断:

  1. 选择过滤器诊断 打开Filter Diagnostics 对话框。

    过滤器诊断对话框

  2. 选择要查看的诊断程序。
  3. 选择OK

要查看所有诊断,请选择Select All 。要查看具有修复程序的诊断程序,请选择Select All with Fixits

要隐藏所有诊断程序,请选择Clear Selection

要查看特定类型的诊断,请右键单击Diagnostics 中的条目,然后在上下文菜单中选择Filter for This Diagnostic Kind 。要隐藏该类诊断,请选择Filter out This Diagnostic Kind

禁止诊断

要抑制诊断结果,请在右键菜单中选择Suppress This DiagnosticSuppress This Diagnostic Inline

要查看项目的抑制列表并删除其中的诊断程序,请选择Projects >Project Settings >Clang Tools

禁用检查

要在全局或针对特定项目禁用特定类型的检查,请在右键菜单中选择Disable This CheckDisable These Checks

从 YAML 文件加载诊断程序

除了运行工具收集诊断结果外,还可以选择从使用"-export-fixes "导出的 YAML 文件中加载诊断程序 ,从使用-export fixes 选项导出的YAML文件中加载诊断结果。

另请参阅 检查代码语法配置 Clang 诊断如何:分析指定 Clang 工具设置分析器Clang 工具

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.