Reader small image

You're reading from  Clang Compiler Frontend

Product typeBook
Published inMar 2024
PublisherPackt
ISBN-139781837630981
Edition1st Edition
Right arrow
Author (1)
Ivan Murashko
Ivan Murashko
author image
Ivan Murashko

Ivan V. Murashko is a C++ software engineer: He got his PhD from Peter the Great St.Petersburg Polytechnic University and has over 20 years of C++ programming experience; since 2020 he has worked with LLVM compilers. His area of interest includes clang compiler frontend and clang tools (clang-tidy, clangd).
Read more about Ivan Murashko

Right arrow

5

Clang-Tidy Linter Framework

This chapter introduces Clang-Tidy, the clang-based linter framework that utilizes the Abstract Syntax Tree (AST) to identify anti-patterns in C/C++/Objective-C code. First, we’ll discuss Clang-Tidy’s capabilities, the types of checks it offers, and how to use them. After that, we will delve into the architecture of Clang-Tidy and explore how to create our own custom lint check. In this chapter, we’ll cover the following topics:

  • An overview of Clang-Tidy, including a brief description of the different checks provided by default

  • The internal design of Clang-Tidy

  • How to create a custom Clang-Tidy check

5.1 Technical requirements

The source code for this chapter is located in the chapter5 folder of the book’s GitHub repository: https://github.com/PacktPublishing/Clang-Compiler-Frontend-Packt/tree/main/chapter5.

5.2 Overview of Clang-Tidy and usage examples

Clang-Tidy is a linter and static analysis tool for C and C++ code. It is a part of the Clang and LLVM project. The tool is built on top of the Clang frontend, which means it understands your code in depth, giving it the ability to catch a wide range of issues.

Here are some key points to understand about Clang-Tidy:

  • Checks: Clang-Tidy contains a series of ”checks” that identify various issues or suggest enhancements. These checks range from performance improvements and potential bugs to coding style and modern C++ best practices. For instance, it might suggest using emplace_back instead of push_back for certain cases or identify areas where you might be accidentally using integer overflow.

  • Extensibility: New checks can be added to Clang-Tidy, making it a highly extensible tool. If you have specific coding guidelines or practices you want to enforce, you can write a check for it.

  • Integration: Clang-Tidy is often used within...

5.3 Clang-Tidy’s internal design

Clang-Tidy is built on top of Clang. At its core, Clang-Tidy leverages Clang’s ability to parse and analyze source code into an AST. Each check in Clang-Tidy essentially involves defining patterns or conditions to match against this AST. When a match is found, a diagnostic can be raised, and in many cases, an automatic fix can be suggested. The tool operates on the basis of individual ”checks” that target specific issues or coding styles. Checks are implemented as plugins, making Clang-Tidy extensible. The ASTMatchers library facilitates writing these checks by providing a domain-specific language to query the AST; see Section 3.5, AST matchers and the official documentation [16] for more info. This ensures that checks are both concise and expressive. Clang-Tidy also has support for analyzing the code base using a compilation database, which provides context such as compile flags (see Chapter 9, Appendix 1: Compilation...

5.4 Custom Clang-Tidy check

In this part of the chapter, we will transform our plugin example (see Section 4.6, Clang plugin project) into a Clang-Tidy check. This check will estimate the complexity of a C++ class based on the number of methods it contains. We will define a threshold as a parameter for the check.

Clang-Tidy offers a tool designed to aid in the creation of checks. Let’s begin by creating a skeleton for our check.

5.4.1 Creating a skeleton for the check

Clang-Tidy provides a specific Python script, add_new_check.py , to assist in creating new checks. This script is located in the clang-tools-extra/clang-tidy directory. The script requires two positional parameters:

  • module : This refers to the module directory where the new tidy check will be placed. In our case, this will be misc .

  • check : This is the name of the new tidy check to add. For our purposes, we will name it classchecker .

By running the script in the llvm-project directory (which contains the...

5.5 Summary

In this chapter, we delved into Clang-Tidy, a robust tool for code analysis. We explored its configuration, execution, and internal architecture. Additionally, we developed a custom Clang-Tidy check to assess class complexity. Our check utilized basic AST matchers, akin to regular expressions within the AST. For complexity determination, we employed a simple method. More sophisticated metrics, such as cyclomatic complexity, demand tools such as Control Flow Graphs (CFGs). The adventure continues in the next chapter, where we’ll dive deep into designing intricate checks using CFG.

5.6 Further reading

lock icon
The rest of the chapter is locked
You have been reading a chapter from
Clang Compiler Frontend
Published in: Mar 2024Publisher: PacktISBN-13: 9781837630981
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
undefined
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $15.99/month. Cancel anytime

Author (1)

author image
Ivan Murashko

Ivan V. Murashko is a C++ software engineer: He got his PhD from Peter the Great St.Petersburg Polytechnic University and has over 20 years of C++ programming experience; since 2020 he has worked with LLVM compilers. His area of interest includes clang compiler frontend and clang tools (clang-tidy, clangd).
Read more about Ivan Murashko