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

7

Refactoring Tools

Clang is renowned for its ability to provide suggestions for code fixes. For instance, if you miss a semicolon, Clang will suggest that you insert it. The ability to modify source code goes beyond the compilation process and is widely used in various tools for code modifications, particularly in refactoring tools. The ability to offer fixes is a powerful feature that extends the capabilities of a linter framework, such as Clang-Tidy, which not only detects issues but also provides suggestions for fixing them.

In this chapter, we will explore refactoring tools. We will begin by discussing the fundamental classes used for code modification, notably clang::Rewriter. We will use Rewriter to build a custom refactoring tool that changes method names within a class. Later in the chapter, we will reimplement the tool using Clang-Tidy and delve into clang::FixItHint, a component of the Clang Diagnostics subsystem that is employed by both Clang-Tidy and the Clang compiler to...

7.1 Technical requirements

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

7.2 Custom code modification tool

We will create a Clang tool that will help us to rename methods for a class that is used for unit testing. We will start with a description for the clang::Rewriter class – the basic class that is used for code modifications.

7.2.1 Code modification support at Clang

clang::Rewriter is a Clang library class that facilitates source code rewriting operations within a translation unit. It provides methods for inserting, removing, and replacing code within theAbstract Syntax Tree (AST) of the source code. Developers can use clang::Rewriter for complex code modifications, such as restructuring or generating new code constructs. It can be applied for both code generation and code refactoring tasks, making it versatile for various code transformation purposes.

The class has several methods for text insertion; for instance, clang::Rewriter ::InsertText inserts the text at the specified source location, and clang ::SourceLocation is used to specify the...

7.3 Clang-Tidy as a code modification tool

We plan to investigate FixItHint, which is a part of the Clang Diagnostics subsystem (see Section 4.4.2, Diagnostics support). FixItHint can be integrated with clang::Rewriter and clang::tooling::Replacement explored previously, providing advanced diagnostics that are used in powerful tools such as Clang-Tidy.

7.3.1 FixItHint

clang::FixItHint is a class in the Clang compiler that significantly enhances its diagnostic capabilities. Its primary role is to provide automated suggestions for correcting code errors or issues that the compiler detects. These suggestions, known as ”fix-its,” are a part of Clang’s diagnostic messages and are intended to guide developers in resolving identified issues in their code.

When Clang encounters a coding error, warning, or stylistic issue, it generates a FixItHint. This hint contains specific recommendations for changes in the source code. For instance, it may suggest replacing a...

7.4 Code modification and Clang-Format

Clang-Format is an essential tool in the Clang/LLVM project, designed for formatting C, C++, Java, JavaScript, Objective-C, or Protobuf code. It plays a crucial role in the Clang tooling ecosystem, offering capabilities for parsing, analyzing, and manipulating source code.

Clang-Format is a part of Clang and has to be installed if we have built and installed the Clang compiler. Let’s look at how it can be used.

7.4.1 Clang-Format configuration and usage examples

Clang-Format uses .clang-format configuration files. The utility will use the closest configuration file; i.e., if the file is located at the folder with the source files we want to format, then the configuration from the folder will be used. The format for configuration files is YAML, which is the same format used for Clang-Tidy configuration files, as shown in Figure 5.12, Clang-Tidy configuration. Let’s create the following simple configuration file:

BasedOnStyle...

7.5 Summary

In this chapter, we investigated the different options provided by Clang for code modifications. We created a specialized Clang Tool that renames a method in a test class. We also rewrote the tool using Clang-Tidy and explored how custom AST matchers can be created. Furthermore, we delved into a variety of different classes provided by Clang for code modifications. One of these classes, clang::FixItHint, is integrated with the Clang diagnostics subsystem and provides a powerful tool for code modification within Clang, as well as in different tools created with Clang. We concluded with Clang-Format, the only tool in the book that does not use the AST but instead utilizes the Clang Lexer to perform code formatting. The next chapter will focus on the integration of different Clang Tools within IDEs.

7.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