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

4

Basic Libraries and Tools

LLVM is written in the C++ language and, as of July 2022, it uses the C++17 version of the C++ standard [6]. LLVM actively utilizes functionality provided by the Standard Template Library (STL). On the other hand, LLVM contains numerous internal implementations [13] for fundamental containers, primarily aimed at optimizing performance. For example, llvm::SmallVector has an interface similar to std::vector but features an internally optimized implementation. Hence, familiarity with these extensions is essential for anyone wishing to work with LLVM and Clang.

Additionally, LLVM has introduced other development tools such as TableGen, a domain specific language (DSL) designed for structural data processing, and LIT (LLVM Integrated Tester), the LLVM test framework. More details about these tools are discussed later in this chapter. We’ll cover the following topics in this chapter:

  • LLVM coding style

  • LLVM basic libraries

  • Clang basic libraries

  • LLVM supporting...

4.1 Technical requirements

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

4.2 LLVM coding style

LLVM adheres to specific code-style rules [11]. The primary objective of these rules is to promote proficient C++ practices with a special focus on performance. As previously mentioned, LLVM employs C++17 and prefers using data structures and algorithms from the STL (short for, Standard Template Library). On the other hand, LLVM offers many optimized versions of data structures that mirror those in the STL. For example, llvm::SmallVector<> can be regarded as an optimized version of std::vector<>, especially for small sizes of the vector, a common trait for data structures used in compilers.

Given a choice between an STL object/algorithm and its corresponding LLVM version, the LLVM coding standard advises favoring the LLVM version.

Additional rules pertain to concerns regarding performance limitations. For instance, both run-time type information (RTTI) and C++ exceptions are disallowed. However, there are situations where RTTI could prove beneficial...

4.3 LLVM basic libraries

We are going to start with RTTI replacement in the LLVM code and discuss how it’s implemented. We will then continue with basic containers and smart pointers. We will conclude with some important classes used to represent token locations and how diagnostics are realized in Clang. Later, in Section 4.6, Clang plugin project, we will use some of these classes in our test project.

4.3.1 RTTI replacement and cast operators

As mentioned earlier, LLVM avoids using RTTI due to performance concerns. LLVM has introduced several helper functions that replace RTTI counterparts, allowing for the casting of an object from one type to another. The fundamental ones are as follows:

  • llvm::isa<> is akin to Java’s javainstanceof operator. It returns true or false depending on whether the reference to the tested object belongs to the tested class or not.

  • llvm::cast<>: Use this cast operator when you’re certain that the object is of the specified...

4.4 Clang basic libraries

Clang is a compiler frontend, and its most important operations are related to diagnostics. Diagnostics require precise information about position location in the source code. Let’s explore the basic classes that Clang provides for these operations.

4.4.1 SourceManager and SourceLocation

Clang, as a compiler, operates with text files (programs), and locating a specific place in the program is one of the most frequently requested operations. Let’s look at a typical Clang error report. Consider a program from Chapter 3, Clang AST, as seen in Figure 3.33. Clang produces the following error message for the program:

$ <...>/llvm-project/install/bin/clang -fsyntax-only maxerr.cpp
maxerr.cpp:3:12: error: use of undeclared identifier ’ab’
    return ab;
           ^
 error generated...

4.5 LLVM supporting tools

The LLVM project has its own tooling support. The most important LLVM tools are TableGen and LIT (which stands for LLVM Integrated Tester). We will look into them with examples from the Clang code. These examples should help us understand the purpose of the tooling and how they can be used.

4.5.1 TableGen

TableGen is a domain-specific language (DSL) and associated tool used in the LLVM project for the purpose of describing and generating tables, particularly those that describe a target architecture. This is highly useful for compiler infrastructure, where one frequently needs to describe things such as instruction sets, registers, and various other target-specific attributes in a structured manner.

TableGen is employed in various parts of the Clang compiler. It’s primarily used where there’s a need to generate large amounts of similar code. For instance, it can be used for supporting cast operations that necessitate extensive enum declarations...

4.6 Clang plugin project

The goal of the test project is to create a clang plugin that will estimate class complexity. Specifically, a class is deemed complex if the number of its methods exceeds a certain threshold. We will leverage all the knowledge we have acquired thus far for this project. This will include the use of a recursive visitor and Clang diagnostics. Additionally, we will create a LIT test for our project. Developing the plugin will necessitate a unique build configuration for LLVM, which will be our initial step.

4.6.1 Environment setup

The plugin will be created as a shared object, and our LLVM installation should be built with support for shared libraries (see Section 1.3.1, Configuration with CMake):

cmake -G Ninja -DCMAKE_BUILD_TYPE=Debug -DCMAKE_INSTALL_PREFIX=../install -DLLVM_TARGETS_TO_BUILD="X86" -DLLVM_ENABLE_PROJECTS="clang" -DLLVM_USE_SPLIT_DWARF=ON -DBUILD_SHARED_LIBS=ON ../llvm

Figure 4.31: CMake configuration used for...

4.7 Summary

In this chapter, we became familiar with the basic classes from the LLVM ADT library. We gained knowledge of Clang diagnostics and the test frameworks used in LLVM for various types of testing. Using this knowledge, we created a simple Clang plugin that detects complex classes and issues a warning about their complexity.

The chapter concludes the first part of the book, where we gained basic knowledge of the Clang compiler frontend. We are now prepared to explore various tools built on the foundation of Clang libraries. We will begin with Clang-Tidy, a powerful linter framework used to detect various issues in C++ code.

4.8 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