Learn the importance and practical applications of the CodeChecker tool for static code analysis in C. The article describes how CodeChecker assists in identifying and fixing potential code defects, motivating developers to adhere to best practices and security standards. We'll go by the hand through the installation, configuration and use of this tool in projects using Clang Tidy, Clang Static Analyzer and Cppcheck. In addition, the article guides you through the process of integrating CodeChecker into a Zephyr project and illustrates how the tool can improve code quality by analyzing trends and running on a remote server.
The C language made its debut in 1972 and has since gained enormous popularity. It is difficult to estimate how many programs have been developed in this language. More than 50 years is long enough to know its strengths and weaknesses. In the case of the C language, this also means seeing potential flaws and pitfalls. Such cases have been collected in the form of guidelines and standards that programmers should follow. To help them do this, static analysis tools have been developed. These tools detect code structures that are considered bad practice or dangerous from a security perspective. They can analyze each file individually or take into account the broader context of the project and its dependencies to assess the code's compliance with the guidelines or standards.
Static analysis has a 44% success rate in removing defects when used alone! Why is this the case? It seems that the key factor here is the speed of information transfer. When coding, we can catch any violation right away when we type a semicolon (sorry Python enthusiasts :wink:). We are then in the context of the violation, so there is a good chance that we will correct it right away. If not, we'll remember the error or store it in a database accessible to the whole team.
By storing the results of the analysis in a database, we can easily visualize the quality of the project, using the number of violations as a measure. Most tools also offer trend analysis, showing historical code quality and its evolution. Based on this data, necessary corrective actions can be taken or more attention can be paid to areas in need of improvement.
CodeChecker
CodeChecker is an infrastructure that supports the process of static code analysis. It runs on a variety of backends, including:
- Clang Tidy
- Clang Static Analyzer
- Cppcheck
The system runs a server that stores analysis results and can display trends. Zephyr integrates seamlessly with CodeChecker. This article will guide you through the installation, activation and configuration of CodeChecker. In addition, it will show you how to navigate the CodeChecker dashboard and how to perform analysis both with and without Zephyrro on legacy code.
Installation
CodeChecker is a Python package that can be easily installed using pip:
pip install codechecker
In this article, we will focus on the clang-tidy backend in CodeChecker. Understanding this aspect will simplify the configuration process. If you don't have clang-tidy installed, you can do this depending on your operating system. For example, on Ubuntu, use the following command:
sudo apt install clang-tidy
If the Zephyr configuration is not set up on your local workstation, you need to set up two items for this tutorial. The first requirement is the installation of the SDK, necessary for building projects natively without the need for a motherboard. Refer to the official Zephyr documentation for guidance on installing the SDK: Getting Started Guide - Zephyr Project Documentation [1].
First build
Let's turn to the analysis to show how simple it can be. This article focuses on the repository available at [2].
After cloning the repository, initialize and update the workspace by running the following commands:
west init -l app && west update
If this is your first encounter with Zephyr on your current computer, it is recommended to export Zephyr definitions for CMake. You can do this by running the command:
west zephyr-export
Checkpoint! Make sure the following command executes successfully before proceeding further:
west build -b qemu_cortex_m3 app --pristine && west build -t run
Running static analysis with Zephyr
To successfully run CodeChecker, you must compile the project with the specified option enabled. Run the following command:
west build -b qemu_cortex_m3 app --pristine -- -DZEPHYR_SCA_VARIANT=codechecker
After running this command, you might have encountered a problem when the terminal ran out of space, making it difficult to see the initial part of the command. This is because CodeChecker inherently performs static analysis using tools such as clang-tidy, clangsa and cppcheck. It also analyzes all files compiled within the project, which in this case is about 84 files. However, we will focus on two specific translation units: main.cpp and worker.cpp.