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

8

IDE Support and Clangd

This chapter is about the Language Server Protocol (LSP) and how you can utilize it to enhance your Integrated Development Environment (IDE). Our primary IDE of choice is Visual Studio Code (VS Code). LLVM has its own implementation of LSP known as Clangd. We will begin by describing LSP and exploring how Clangd leverages it to extend the capabilities provided by the IDE. Finally, we will conclude with examples of how various Clang tools, such as Clang-Tidy and Clang-Format, can be seamlessly integrated into the IDE through Clangd.

We will cover the following topics in this chapter:

  • What is Language Server Protocol (LSP) and how does it improve an IDE’s capabilities?

  • How VS Code and Clangd (the Clang LSP server) can be installed

  • How LSP is used to connect VS Code and Clangd, through an example

  • How Clangd is integrated with other Clang tools

  • Why performance matters for Clangd and what optimizations were made to make Clangd fast

8.1 Technical requirements

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

8.2 Language Server Protocol

An IDE is a software application or platform that provides a comprehensive set of tools and features to assist developers in creating, editing, debugging, and managing software code. An IDE typically includes a code editor with syntax highlighting, debugging capabilities, project management features, version control integration, and, often, plugins or extensions to support various programming languages and frameworks.

Popular examples of IDEs are Visual Studio/VS Code, IntelliJ IDEA, Emacs, and Vim. These tools are designed to streamline the development process, making it easier for developers to write, test, and maintain their code efficiently.

A typical IDE supports multiple languages, and integrating each language can be a challenging task. Each language requires specific support, which can be visualized in Figure 8.1. It’s worth noting that there are many similarities in the development process of different programming languages. For example...

8.3 Environment setup

We will begin our environment setup by building Clangd. Then, we will install VS Code, set up the Clangd extension, and configure Clangd within it.

8.3.1 Clangd build

It’s worth building Clangd in release mode, as we did for LLDB in Section 1.3.3, The LLVM debugger, its build, and usage. This is because performance is crucial in IDEs. For instance, Clangd requires building an AST to provide code navigation functionality. If a user modifies a document, the document should be rebuilt, and the navigation functionality will not be available until the rebuild process is completed. This can result in delays in IDE responses. To prevent IDE slowness in responses, we should ensure that Clangd is built with all the required optimizations. You can use the following project configuration command:

cmake -G Ninja -DCMAKE_BUILD_TYPE=Release -DCMAKE_INSTALL_PREFIX=../install -DLLVM_TARGETS_TO_BUILD="X86" -DLLVM_ENABLE_PROJECTS="clang;clang-tools-extra...

8.4 LSP demo

In this brief LSP demo, we will demonstrate how Clangd opens a file and finds a symbol’s definition. Clangd features a comprehensive logging subsystem that offers valuable insights into its interaction with the IDE. We will use the log subsystem to obtain the necessary information.

8.4.1 Demo description

In our example, we open a test file as shown in the following screenshot and retrieve the definition of the doPrivateWork token:

Figure 8.6: Go-to definition and hover for the doPrivateWork token

Figure 8.6: Go-to definition and hover for the doPrivateWork token

VS Code communicates with Clangd via standard input/output, and we will use Clangd logs to capture the interaction.

This can be achieved by setting up a wrapper shell script instead of using the actual clangd binary in the VS Code settings:

Figure 8.7: Wrapper shell script setup in VS Code

Figure 8.7: Wrapper shell script setup in VS Code

We can use the following script, clangd.sh :

#!/bin/sh 
 
$HOME/clangbook/llvm-project/install/bin/clangd -log verbose -pretty 2> /tmp/clangd...

8.5 Integration with Clang tools

Clangd takes advantage of the LLVM module architecture and has a very strong integration with other Clang tools. In particular, Clangd uses Clang-Format libraries to provide formatting functionality and Clang-Tidy libraries (such as libraries with clang-tidy checks) to support linters in the IDE. The integration is schematically shown in the following figure:

Figure 8.27: VS Code with LSP extension and Clangd server for C++

Figure 8.27: VS Code with LSP extension and Clangd server for C++

The configuration from .clang-format (see Section 7.4.1, Clang-Format configuration and usage examples) is used for formatting, and from .clang-tidy (see Figure 5.12, Clang-Tidy configuration) for linters. Let’s see how the formatting works in Clangd.

8.5.1 Clangd support for code formatting using LSP messages

Clangd provides robust support for code formatting. This feature is essential for developers to maintain consistent code styles and readability in their C and C++ projects. Clangd leverages LSP...

8.6 Performance optimizations

Obtaining a smooth IDE experience with accurate results provided without visible delays is a challenging task. One of the ways to achieve this experience is through compiler performance optimization, as good navigation can be provided with well-parsed source code. Clangd offers excellent examples of performance optimization, which we will explore in some detail. We will start with the optimizations for code modifications.

8.6.1 Optimizations for modified documents

As we saw in Line 4, Open document, navigation support requires the AST as the basic data structure, so we have to use the Clang frontend to obtain it. Additionally, we have to rebuild the AST when there are document modifications. Document modification is a common activity for developers, and we won’t be able to provide a good IDE experience if we always start the build process from scratch.

Source code preamble

To gain insights into the ideas used to speed up AST building for...

8.7 Summary

In this chapter, we acquired knowledge of LSP, a protocol used to provide developer tools integration with IDEs. We explored Clangd, an LSP server that is part of LLVM and can be considered as a prime example of how it integrates various tools discussed in the book. Clangd utilizes the Clang frontend to display compilation errors and leverages the AST as a fundamental data structure that provides information for navigation requests, such as go-to definition requests. Additionally, Clangd is seamlessly integrated with other tools covered in previous chapters, such as Clang-Tidy and Clang-Format. This integration showcases the significant benefits of the LLVM/Clang module structure.

8.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 €14.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