Qt Creator integrates the following Clang tools for finding problems in C, C++, and Objective-C source code by using static analysis:
注意: The Clang static analyzer checks are a part of Clang-Tidy. To use the checks you must create a custom configuration for the Clang tools and enable them for Clang-Tidy.
Clang tools are delivered and installed with Qt Creator, and therefore you do not need to set them up separately.
In addition to running the tools to collect diagnostics, you can select
to load diagnostics from
YAML
files that you exported using the
-export fixes
选项。
To run the Clang tools to analyze the currently open file:
(
Analyze File
) button on the editor toolbar.
To run the Clang tools to analyze an open project:
The found issues are displayed in the Clang-Tidy and Clazy view:
注意:
If you select
Debug
in the mode selector to open the
Debug
mode and then select
Clang-Tidy and Clazy
, you must select the
(
Start
) button to open the
Files to Analyze
对话框。
Double-click an issue to move to the location where the issue appears in the code editor.
If a fixit exists for an issue, you can select the check box next to the issue to schedule it for fixing. Select the Select Fixits check box to select all fixits. You can see the status of an issue by hovering the mouse pointer over the icon next to the check box.
To see more information about an issue that is marked with the
icon, hover the mouse pointer over the line.
Select the
button to customize Clang diagnostics for the current project.
To restore the global settings, select Restore Global Settings . To view and modify the global settings, select Open Global Settings . To open the Clang static analyzer, select Go to Analyzer .
To configure Clang diagnostics globally for Clang tools:
For more information about the available checkers, see Clang Static Analyzer documentation .
To suppress diagnostics, select Suppress This Diagnostic in the context menu. To view the suppression list for a project and to remove diagnostics from it, select Projects > Project Settings > Clang Tools .
The Clazy checks are divided into levels from 0 to 3. The checks at level 0 are very stable and provide hardly any false positives, while the checks at level 3 can be considered experimental. You can select the checks to perform at each level. To include the checks from the lower levels automatically, select the Enable lower levels automatically check box.
Clang-Tidy reads the configuration for each source file from a .clang-tidy file located in the closest parent directory of the source file. If any configuration options have a corresponding command-line option, the command-line option takes precedence. The effective configuration can be inspected using
-dump-config
.
Qt Creator creates the configuration for you based on the checks you select. To store the checks in file format, you can create a .clang-tidy file, as follows:
clang-tidy -dump-config
into a file named
.clang-tidy
。例如:
clang-tidy -checks=-*,bugprone-*,cppcoreguidelines-avoid-* -dump-config > .clang-tidy
To add more checks using Qt Creator later on, copy the checks from your .clang-tidy file into the Edit Checks as String field, select additional checks, and copy-paste the contents of the field to the .clang-tidy file.