Clang Compiler Frontend

Book description

Boost your productivity with a variety of compiler tools that integrate seamlessly into your IDE

Key Features

  • Expand your understanding of the C++ programming language by learning about how the C++ compiler works and how to utilize its advanced features
  • Explore techniques for static code analysis and use them to create lint checks
  • Enhance your IDE to support advanced compiler tools
  • Purchase of the print or Kindle book includes a free PDF eBook

Book Description

Discover the power of Clang, a versatile compiler known for its compilation speed and insightful error and warning messages. This book will get you acquainted with the capabilities of Clang, helping you harness its features for performance improvements and modularity by creating custom compiler tools.

While focused on Clang compiler frontend, this book also covers other parts of LLVM, essential to understanding Clang's functionality, to keep up with the constantly evolving LLVM project.

Starting with LLVM fundamentals, from installation procedures to development tools, this book walks you through Clang's internal architecture and its integral role within LLVM. As you progress, you’ll also tackle optimizing compilation performance through features such as C++ modules and header maps.

The later chapters cover tools developed using the Clang/LLVM, including clang-tidy for linting, refactoring tools, and IDE support, and feature many examples to illustrate the material.

By the end of this book, you’ll have a solid understanding of Clang, different Clang Tools, and how to use them to their fullest potential.

What you will learn

  • Get to grips with compiler architecture
  • Gain an understanding of the inner workings of Clang
  • Familiarize yourself with features specific to Clang
  • Investigate various techniques for static code analysis
  • Acquire knowledge on how to use AST matchers
  • Create custom code modification and refactoring tools
  • Explore tools for integrating compiler tools with IDEs

Who this book is for

This book is for experienced C++ software engineers who have no prior experience with compiler design but want to gain the knoweldge they need to get up and running. Engineers who want to learn about how Clang works and familiarize themselves with its specific features will also benefit from this book.

Table of contents

  1. Preface
    1. Who this book is for
    2. What this book covers
    3. To get the most out of this book
    4. Download the example code files
    5. Conventions used
    6. Get in touch
    7. Share your thoughts
    8. Download a free PDF copy of this book
  2. Part I: Clang Setup and Architecture
  3. Chapter 1: Environment Setup
    1. 1.1 Technical requirements
      1. 1.1.1 CMake as project configuration tool
      2. 1.1.2 Ninja as build tool
    2. 1.2 Getting to know LLVM
      1. 1.2.1 Short LLVM history
      2. 1.2.2 OS support
      3. 1.2.3 LLVM/Clang project structure
    3. 1.3 Source code compilation
      1. 1.3.1 Configuration with CMake
      2. 1.3.2 Build
      3. 1.3.3 The LLVM debugger, its build, and usage
    4. 1.4 Test project – syntax check with a Clang tool
    5. 1.5 Summary
    6. 1.6 Further reading
  4. Chapter 2: Clang Architecture
    1. 2.1 Technical requirements
    2. 2.2 Getting started with compilers
      1. 2.2.1 Exploring the compiler workflow
      2. 2.2.2 Frontend
    3. 2.3 Clang driver overview
      1. 2.3.1 Example program
      2. 2.3.2 Compilation phases
      3. 2.3.3 Tool execution
      4. 2.3.4 Combining it all together
      5. 2.3.5 Debugging Clang
    4. 2.4 Clang frontend overview
      1. 2.4.1 Frontend action
      2. 2.4.2 Preprocessor
      3. 2.4.3 Parser and sema
    5. 2.5 Summary
    6. 2.6 Further reading
  5. Chapter 3: Clang AST
    1. 3.1 Technical requirements
    2. 3.2 AST
      1. 3.2.1 Statements
      2. 3.2.2 Declarations
      3. 3.2.3 Types
    3. 3.3 AST traversal
      1. 3.3.1 DeclVisitor test tool
      2. 3.3.2 Visitor implementation
    4. 3.4 Recursive AST visitor
    5. 3.5 AST matchers
    6. 3.6 Explore Clang AST with clang-query
    7. 3.7 Processing AST in the case of errors
    8. 3.8 Summary
    9. 3.9 Further reading
  6. Chapter 4: Basic Libraries and Tools
    1. 4.1 Technical requirements
    2. 4.2 LLVM coding style
    3. 4.3 LLVM basic libraries
      1. 4.3.1 RTTI replacement and cast operators
      2. 4.3.2 Containers
      3. 4.3.3 Smart pointers
    4. 4.4 Clang basic libraries
      1. 4.4.1 SourceManager and SourceLocation
      2. 4.4.2 Diagnostics support
    5. 4.5 LLVM supporting tools
      1. 4.5.1 TableGen
      2. 4.5.2 LLVM test framework
    6. 4.6 Clang plugin project
      1. 4.6.1 Environment setup
      2. 4.6.2 CMake build configuration for plugin
      3. 4.6.3 Recursive visitor class
      4. 4.6.4 Plugin AST consumer class
      5. 4.6.5 Plugin AST action class
      6. 4.6.6 Plugin code
      7. 4.6.7 Building and running plugin code
      8. 4.6.8 LIT tests for clang plugin
    7. 4.7 Summary
    8. 4.8 Further reading
  7. Part II: Clang Tools
  8. Chapter 5: Clang-Tidy Linter Framework
    1. 5.1 Technical requirements
    2. 5.2 Overview of Clang-Tidy and usage examples
      1. 5.2.1 Building and testing Clang-Tidy
      2. 5.2.2 Clang-Tidy usage
      3. 5.2.3 Clang-Tidy checks
    3. 5.3 Clang-Tidy’s internal design
      1. 5.3.1 Internal organization
      2. 5.3.2 Configuration and integration
    4. 5.4 Custom Clang-Tidy check
      1. 5.4.1 Creating a skeleton for the check
      2. 5.4.2 Clang-Tidy check implementation
      3. 5.4.3 LIT test
      4. 5.4.4 Results in the case of compilation errors
      5. 5.4.5 Compilation errors as edge cases
    5. 5.5 Summary
    6. 5.6 Further reading
  9. Chapter 6: Advanced Code Analysis
    1. 6.1 Technical requirements
    2. 6.2 Static analysis
    3. 6.3 CFG
    4. 6.4 Custom CFG check
      1. 6.4.1 Creating the project skeleton
      2. 6.4.2 Check implementation
      3. 6.4.3 Building and testing the cyclomatic complexity check
    5. 6.5 CFG on Clang
      1. 6.5.1 CFG construction by example
      2. 6.5.2 CFG construction implementation details
    6. 6.6 Brief description of Clang analysis tools
    7. 6.7 Knowing the limitations of analysis
    8. 6.8 Summary
    9. 6.9 Future reading
  10. Chapter 7: Refactoring Tools
    1. 7.1 Technical requirements
    2. 7.2 Custom code modification tool
      1. 7.2.1 Code modification support at Clang
      2. 7.2.2 Test class
      3. 7.2.3 Visitor class implementation
      4. 7.2.4 Consumer class implementation
      5. 7.2.5 Build configuration and main function
      6. 7.2.6 Running the code modification tool
    3. 7.3 Clang-Tidy as a code modification tool
      1. 7.3.1 FixItHint
      2. 7.3.2 Creating project skeleton
      3. 7.3.3 Check implementation
      4. 7.3.4 Build and run the check
    4. 7.4 Code modification and Clang-Format
      1. 7.4.1 Clang-Format configuration and usage examples
      2. 7.4.2 Design considerations
      3. 7.4.3 Clang-Tidy and Clang-Format
    5. 7.5 Summary
    6. 7.6 Further reading
  11. Chapter 8: IDE Support and Clangd
    1. 8.1 Technical requirements
    2. 8.2 Language Server Protocol
    3. 8.3 Environment setup
      1. 8.3.1 Clangd build
      2. 8.3.2 VS Code installation and setup
    4. 8.4 LSP demo
      1. 8.4.1 Demo description
      2. 8.4.2 LSP session
    5. 8.5 Integration with Clang tools
      1. 8.5.1 Clangd support for code formatting using LSP messages
      2. 8.5.2 Clang-Tidy
    6. 8.6 Performance optimizations
      1. 8.6.1 Optimizations for modified documents
      2. 8.6.2 Building preamble optimization
    7. 8.7 Summary
    8. 8.8 Further reading
  12. Part III: Appendix
  13. Appendix 1: Compilation Database
    1. 9.1 Compilation database definition
    2. 9.2 CDB creation
    3. 9.3 Clang tools and a CDB
    4. 9.4 Further reading
  14. Appendix 2: Build Speed Optimization
    1. 10.1 Technical requirements
    2. 10.2 Precompiled headers
    3. 10.3 Clang modules
    4. 10.4 Further reading
  15. Bibliography
  16. Index
    1. Why subscribe?
  17. Other Books You Might Enjoy
    1. Packt is searching for authors like you
    2. Share your thoughts
    3. Download a free PDF copy of this book

Product information

  • Title: Clang Compiler Frontend
  • Author(s): Ivan Murashko
  • Release date: March 2024
  • Publisher(s): Packt Publishing
  • ISBN: 9781837630981