- Qt Creator Documentation
- How to
- Analyzing code
- Qt Creator 20.0.0

View type hierarchy ◦ Analyze CPU usage

# Analyzing code

> How to: Analyze

Use integrated code analysis tools to perform static code analysis, detect memory leaks, profile CPU and memory usage, check code coverage, and visualize trace events.

## Using code analysis tools

To use a code analysis tool, select it in the Analyze menu or in the pull-down menu of the (Start Debugging of Startup Project) button. In the Debug mode, switch between tools by selecting them in the menu on the debugger toolbar.

Drag the views in the Debug mode to new positions on the screen. The size and position of views are saved for future sessions. Select View > Views > Reset to Default Layout to reset the views to their original sizes and positions.

## Overview of the code analysis tools

The following table describes code analysis tools you can use in Qt Creator to analyze your code. Some tools are built in to Qt Creator, while you can install others separately on the computer or run them from a server.

Tool | Language | Availability | Description
--- | --- | --- | ---
Axivion Suite | C, C++, C# | Requires Axivion server | Analyze code, architecture, dependencies, metrics, and coding guidelines.
Clang-Tidy and Clazy | C, C++, Objective-C | Built-in | Find diagnostics of style violations, interface misuse, and Qt-specific issues, and apply fixits.
Chrome Trace Format Visualizer | Any | Built-in | Explore traces generated by third-party tools in Chrome Trace Format.
Coco | C, C++, C#, QML, Tcl | Requires installation | Find untested code, redundant tests, and dead code.
Cppcheck | C, C++ | Requires installation | Find undefined behavior and dangerous coding constructs in C and C++ code without running the application.
Heob | Any | Requires installation (Windows) | Find memory leaks and buffer overruns on Windows.
Performance Analyzer | Any | Built-in (Linux) | Analyze the CPU and memory usage of an application on Linux desktop and embedded devices.
QML Profiler | QML, JavaScript | Built-in | Find causes for typical performance problems in your applications, such as slowness and unresponsiveness.
Static checks for QML and JavaScript | QML, JavaScript | Built-in | Find common problems in your code, such as syntax errors, missing properties, and type mismatches.
Valgrind | Any | Requires installation (Linux, macOS 10.9-11.0, Intel only) | Detect memory leaks, memory errors, and threading issues.

## Static analysis for C and C++

### Axivion Suite

Axivion Suite is an enterprise-grade software quality suite that tracks architectural consistency, enforces team-wide coding guidelines, and measures quality trends over successive builds. It supports C, C++, and C#.

With Axivion Suite, you can:

- Check the source code for potential runtime errors.
- Use metrics to generate quantitative information about the internal quality of the source code.
- Run style checks to achieve compliance with coding guidelines.
- Detect both duplicates and similar pieces of code in the source code.
- Recognize cyclical dependencies at different levels.
- Detect unreachable code.

Install the Axivion Suite and enable the Axivion plugin in Qt Creator to view the analysis results, including violations, metrics, and inline annotations.

For more information, see:

- Connect to Axivion dashboard servers
- Local analysis with Axivion
- View Axivion static code analysis results

### Clang-Tidy and Clazy

Qt Creator includes built-in support for Clang-Tidy and Clazy, which are complementary static analysis tools for C, C++, and Objective-C code. By default, diagnostics are shown in real time for the open file as you edit. This lets you maintain code quality and consistency without waiting for a separate build step.

Clang-Tidy checks for typical programming errors such as style violations, incorrect use of interfaces, and patterns that are likely to be bugs. It also offers automatic fixes for many of the issues it detects, including modernization of older C++ code.

Clazy adds Qt-specific checks on top of the Clang diagnostics. These range from unnecessary memory allocation to misuse of Qt API. Clazy also provides refactoring actions to fix some of the issues it finds.

For more information, see:

- Analyze code with Clang-Tidy and Clazy
- Configure Clang diagnostics
- Set global preferences for Clang tools
- Specify Clang tools settings

### Cppcheck

Cppcheck is a lightweight static analysis tool focused on finding undefined behavior and dangerous coding constructs in C and C++ code. It analyzes the source code without building or running the application.

Cppcheck looks for a wide range of issues, including out-of-bounds accesses, null pointer dereferences, use of uninitialized variables, resource leaks, and suspicious integer arithmetic. For the full list of checks, see Cppcheck: List of checks.

Install Cppcheck separately and enable the Cppcheck plugin in Qt Creator to integrate the results. Cppcheck is automatically run on open files and you can also run it manually on selected files from Analyze > Cppcheck.

For more information, see Detect errors in C++ code with Cppcheck.

## QML and JavaScript

### Static checks for QML and JavaScript

With the built-in static analyzer, you can run static checks on the QML and JavaScript code in your project to find common problems, similarly to using JSLint.

The checks catch syntax errors, missing or misspelled properties, type mismatches, and other common mistakes without having to run the application.

For more information, see JavaScript and QML checks.

### QML Profiler

Qt Creator comes with QML Profiler for inspecting binding evaluations and signal handling when running QML code. Use it to identify performance bottlenecks that are difficult to spot through code inspection alone.

QML Profiler records events, such as signal handling, binding evaluations, and JavaScript execution, and visualizes them in a timeline view. This is useful for diagnosing slowness and unresponsiveness.

For more information, see:

- Profiling QML applications
- Profile QML applications

## Memory analysis

### Valgrind

Install Memcheck and Callgrind from Valgrind's Tool Suite and enable the Valgrind plugin in Qt Creator to detect memory errors and profile function execution on Linux and macOS (10.9-11.0, Intel only). You can run the tools on a remote Linux machine or device from any development host.

Memcheck finds memory management issues such as reads or writes to freed memory, use of uninitialized memory, memory leaks, and incorrect use of allocation functions. Callgrind records the call history of functions and the cache usage statistics.

You can run the Valgrind tools either locally on the development host or remotely on another host. You can use them to analyze both applications for which you set up a project in Qt Creator and applications for which you do not have a project.

To run the Valgrind tools to analyze an application for which you have a project, open the project in Qt Creator and select the kit to run the project. The kit specifies whether the Valgrind tools are run locally or remotely.

To set preferences for the Valgrind tools, select Preferences > Analyzer > Valgrind. You can override the general settings for each project in the Run Settings for the project.

For more information, see:

- Valgrind Memcheck
- Valgrind Callgrind
- Detect memory leaks with Memcheck
- Run Valgrind tools on external applications
- Specify Valgrind settings for a project

### Heob

On Windows, install the Heob heap observer to detect buffer overruns and memory leaks.

Heob raises an access violation on buffer overruns and records stack traces of the offending instruction and buffer allocation. You can see the results after Heob exits normally.

For more information, see:

- Heob sources
- Heob
- Detect memory leaks with Heob

## Profiling and tracing

### Performance Analyzer

On Linux, use Performance Analyzer to analyze the CPU and memory usage of an application on Linux desktop and embedded devices.

Performance Analyzer uses the Perf tool bundled with the Linux kernel to take periodic snapshots of the call chain of an application and visualizes them in a timeline view or as a flame graph.

For more information, see Performance Analyzer.

### Chrome Trace Format Visualizer

Use the Chrome Trace Format Visualizer to view Chrome trace events. This is especially useful when viewing large trace files that are difficult to visualize using the built-in trace-viewer (`chrome://tracing`).

Several tracing tools can generate information about Chrome trace events in Chrome Trace Format beyond Chrome-based tools. With the visualizer, you can navigate, zoom, and inspect events in the trace.

For more information, see:

- Chrome Trace Format Visualizer
- KDAB: Full Stack Tracing Part 1

## Code coverage

### Coco

Install the Coco code coverage toolchain and enable the Coco plugin in Qt Creator for C, C++, C#, QML, and Tcl programs. Use it to analyze the way an application runs as part of a test suite, for example. Use the results to make the tests more efficient and complete.

You can:

- Find untested code sections.
- Find redundant tests which can then be eliminated. Coco can identify portions of the source code that are covered by a test. It can detect whether a new test covers lines in the source code that the existing tests do not cover.
- Find dead code by displaying code that is never executed.
- Calculate the optimum test execution order so as to maximize test coverage for each run. This is particularly useful for manual testing.
- Analyze two separate versions of an application and compare the differences. This makes it possible to see which tests are affected by source code modifications and also to get some measure of the test coverage of a patch or hot fix.
- Measure the execution time of applications and tests.

For more information, see:

- Set up code coverage from Coco
- View code coverage reports from Coco
- Coco Manual

See also Activate kits for a project, How to: Analyze, Analyzers, and Enable and disable plugins.