Static Analyzers

Fun with Static Analyzers!

Anyone who writes code, either professionally or as a hobby, has encountered the pain of debugging an issue that later proves to be caused by a well known, common programming mistake. Some of us thrive on the thrill of hunting down and squashing these bugs but most of us would prefer to prevent these problems from occurring in the first place.

What if there were tools that could help us identify and fix these common bugs before they became a thorn in our side? What if there were tools to make the code more readable for other humans out there? Static-analysis tools are here to the rescue and one of them has caught our attention lately. Clang-tidy is part of the clang-based static-analysis tool suite, along with the more powerful Clang Static Analyzer and Clang Format tools. Although clang-tidy is presented as a C++ linter tool, it is definitely more than that, providing a framework for running robust analysis on C or C++ code. The errors reported by the tool can help you solve various bugs or fix coding-style issues.

“How can I use the clang-tidy on my code?” “Where can I get the tool?” “Where can I go to get more information?” Keep reading and your questions will be answered.

Using clang-tidy

Clang-tidy is currently available as a command-line tool. In order to run basic checks on source code files, you can:

  • Instruct clang-tidy what file to analyze.
  • Select which checks to run with the –checks option.
  • Specify, at the end of the command line, the compilation options that are used to build the code (for example, include directories or reprocessor macro definitions).

A simple example of running checks in the clang-analyzer category:
To list all available checks, use the –list-checks option.

When running checks, clang-tidy generates warnings for your code. These warnings identify the places in your code where the check rule matched.

An output example reporting a place where the C++ auto specifier should be used:

As we mentioned earlier, you have the option to automate the process of fixing the potential problems that are detected by adding the –fix option to the command line.

The power of clang-tidy stands in its ability to be easily extended and adapted to detect new issues that may interest you. For that, custom checks can be added to clang-tidy by following their tutorial. The framework facilitates adding new checks through helper python scripts that create the skeleton source files with classes where the checks will be implemented. The interface that clang presents in these extension modules gives you access to the Abstract Syntax Tree (AST) level and thus offers the opportunity to implement powerful checks on your code.

Where to Get and Build It

Clang-tidy is part of the clang release and pre-built binaries are available.
Source code can be found in the extra Clang tools (clang-tools-extra) LLVM repository, git mirror. After cloning the tree, the tool can be built as a standalone target following their instructions.

Give it a Try

To conclude, clang-tidy brings a series of benefits to the development process such as: identifying and fixing suspicious coding constructs or even bugs, improving quality through code style checks, and the opportunity to expand the checking features to custom requirements. Give it a try on your MPLAB® XC application code and tell us what you think in the Microchip forums! Also, which other static analyzers have you tried?

© 2024 Microchip Technology, Inc.
Notice: ARM and Cortex are the registered trademarks of ARM Limited in the EU and other countries.
Information contained on this site regarding device applications and the like is provided only for your convenience and may be superseded by updates. It is your responsibility to ensure that your application meets with your specifications. MICROCHIP MAKES NO REPRESENTATIONS OR WARRANTIES OF ANY KIND WHETHER EXPRESS OR IMPLIED, WRITTEN OR ORAL, STATUTORY OR OTHERWISE, RELATED TO THE INFORMATION, INCLUDING BUT NOT LIMITED TO ITS CONDITION, QUALITY, PERFORMANCE, MERCHANTABILITY OR FITNESS FOR PURPOSE. Microchip disclaims all liability arising from this information and its use. Use of Microchip devices in life support and/or safety applications is entirely at the buyer's risk, and the buyer agrees to defend, indemnify and hold harmless Microchip from any and all damages, claims, suits, or expenses resulting from such use. No licenses are conveyed, implicitly or otherwise, under any Microchip intellectual property rights.