On this page

Analyzing code

How to: Analyze

Use integrated code analysis tools to find issues in your code through static analysis, detect memory leaks, profile CPU and memory usage, check code coverage, and visualize trace events. Some tools also analyze architecture, track quality metrics over time, and enforce coding guidelines across a codebase.

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 (Start Debugging of Startup Project) button. In the Debug mode, switch between tools by selecting them in the menu on the debugger toolbar.

Performance Analyzer in Debug mode

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 others require separate installation.

ToolLanguageAvailabilityDescription
Axivion SuiteC, C++, C#Requires installationAnalyze code, architecture, dependencies, metrics, and coding guidelines.
Clang-Tidy and ClazyC, C++, Objective-CBuilt-inFind style violations, interface misuse, and Qt-specific issues with refactoring actions to the diagnostics.
Chrome Trace Format VisualizerAnyBuilt-inView Chrome trace events in trace files, including large files that are difficult to open in chrome://tracing.
CocoC, C++, C#, QML, TclRequires installationFind untested code, redundant tests, and dead code.
CppcheckC, C++Requires installationFind undefined behavior and dangerous coding constructs in C and C++ code without running the application.
HeobAnyRequires installation (Windows)Find memory leaks and buffer overruns on Windows.
Performance AnalyzerAnyBuilt-in (Linux)Analyze the CPU and memory usage of an application on Linux desktop and embedded devices.
QML ProfilerQML, JavaScriptBuilt-inFind causes for typical performance problems in your applications, such as slowness and unresponsiveness.
Static checks for QML and JavaScriptQML, JavaScriptBuilt-inFind common problems in your code, such as syntax errors, missing properties, and type mismatches.
ValgrindAnyRequires 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:

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:

Cppcheck

Cppcheck is a 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:

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:

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:

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.

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 on macOS, Linux, or Windows 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:

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

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.