Global code completion and architecture of clangd Ilya Biryukov - - PowerPoint PPT Presentation

global code completion and architecture of clangd
SMART_READER_LITE
LIVE PREVIEW

Global code completion and architecture of clangd Ilya Biryukov - - PowerPoint PPT Presentation

Global code completion and architecture of clangd Ilya Biryukov Google EuroLLVM April 16, 2018 Overview 1. Language Server Protocol 2. Architecture of clangd 3. Code completion 4. Current state and future work Language Server Protocol


slide-1
SLIDE 1

Global code completion and architecture of clangd

Ilya Biryukov Google

EuroLLVM April 16, 2018

slide-2
SLIDE 2

Overview

1. Language Server Protocol 2. Architecture of clangd 3. Code completion 4. Current state and future work

slide-3
SLIDE 3

Language Server Protocol

slide-4
SLIDE 4

C++ Java C# Vim C++ Vim plugin Java Vim plugin C# Vim plugin Emacs C++ Emacs plugin Java Emacs plugin C# Emacs plugin Atom C++ Atom plugin Java Atom plugin C# Atom plugin Sublime Text C++ Sublime plugin Java Sublime plugin C# Sublime plugin

Editor integration problem

slide-5
SLIDE 5

Editor integration problem

vim Emacs irony-mode clang_complete vim-clang … rtags … libclang

slide-6
SLIDE 6

Language Server Protocol (LSP)

Editors (native or via plugins) vim Emacs Sublime Atom VSCode … Language Servers clangd(C++) cquery (C++) rlc (Rust) TypeScript Eclipse jdt.ls (Java) … LSP

slide-7
SLIDE 7

YouCompleteMe

Editors (via ycm plugins) vim Emacs Sublime Atom VSCode … Completers HTTP YCM Server C++ completer C# completer Java completer …

slide-8
SLIDE 8

clangd

slide-9
SLIDE 9

clangd

Language Server based on clang.

  • Developed as part of LLVM in clang-tools-extra
  • Supported features

○ Diagnostics ○ Code completion ○ Go to definition ○ Documentation ○ Local rename ○ Formatting ○ …

slide-10
SLIDE 10

Architecture of clangd

clangd clang Frontend AST Cache Index LSP features completion goto definition … signature help

slide-11
SLIDE 11

Index

  • Provides project-wide information
  • AST Decl → Index Symbol
  • Example:

namespace llvm { class StringRef { /* ... */ }; } int dropFirst(llvm::StringRef S, size_t N);

Name: StringRef Scope: llvm Kind: class ID: llvm::StringRef … Name: dropFirst Scope: <global-namespace> Kind: function ID: dropFirst(llvm::StringRef, size_t) …

slide-12
SLIDE 12

Index

Models of your program

AST

  • Single file
  • Up-to-date

Dynamic

  • Active files
  • ~1 minute old

Static

  • All files
  • ~1 day old
  • external?
slide-13
SLIDE 13

Code completion

slide-14
SLIDE 14

Clang completion of class members.

slide-15
SLIDE 15

Clang completion inside namespaces.

slide-16
SLIDE 16

Clang completion inside namespaces.

slide-17
SLIDE 17

Idea of global completion

slide-18
SLIDE 18

Idea of global completion

slide-19
SLIDE 19

Idea of global completion

slide-20
SLIDE 20

Global completion

  • Completion returns results

from all headers

○ adds #include if needed

  • General case is hard

std::vector<bool> vec_bool.flip(); // ok std::vector<int> vec_int; vec_int.flip(); // error

slide-21
SLIDE 21

Completion items: AST or Index?

namespace llvm { int getAsUnsignedInteger(const char *); class StringRef { const char *Ptr; public: StringRef substr(size_t N) { const char *NewPtr = Ptr + N; | } llvm::Optional llvm::DenseSet llvm::ArrayRef …

slide-22
SLIDE 22

Code completion flow

AST completions scopes index results clang Frontend PCH

slide-23
SLIDE 23

Index.fuzzyFind(/*Scopes*/ {"::", "std::"}, /*Query*/ "vec");

Scopes

void my_class::do_work() { vec| } #include "my_class.h" using namespace std;

slide-24
SLIDE 24

Ranking

  • Query match score
  • Symbol signals

○ Uses ○ Type information ○ Proximity

slide-25
SLIDE 25

Ranking example

Query: “uniq”

Match 0.9 0.9 0.5 0.5 Symbol unique unique_ptr make_unique MakeUnique Uses 45 1504 403 607 Uses 0.3 1.0 0.6 0.7 Total 0.27 0.9 0.30 0.35

× =

slide-26
SLIDE 26

Ranking example

Query: “uniq”

Symbol MakeUnique unique_ptr make_unique unique Total 0.35 0.9 0.30 0.27

slide-27
SLIDE 27

Current state and future work

slide-28
SLIDE 28

Current state

  • Basic LSP support

○ completion, diagnostics, fix-its, signature help, local rename, go to definition, formatting

  • Global completion (experimental)
  • Project-wide index (experimental)
slide-29
SLIDE 29

Embedding in a Web IDE

  • Used by internal Web IDE at Google
  • Runs in Cloud
  • Implementation challenges:

○ Virtual File System ○ Buildsystem integration ○ Tracing requests

slide-30
SLIDE 30

cquery

  • Language Server based on libclang
  • More features than clangd
  • Highly optimized in-memory index
  • Designed to run locally
slide-31
SLIDE 31

Future plans

  • More LSP features
  • clang-tidy
  • index-while-build
  • Better build system integration
slide-32
SLIDE 32

Thanks to all contributors!

  • Ben Jackson
  • Benjamin Kramer
  • Eric Liu
  • Haojian Wu
  • Krasimir Georgiev
  • Marc-André Laperle
  • Raoul Wols
  • Sam McCall
  • Simon Marchi
  • Stanislav Ionascu
  • William Enright
slide-33
SLIDE 33
  • Feedback welcome!

○ https://clang.llvm.org/extra/clangd.html

  • Contributions welcome!

○ https://reviews.llvm.org/source/CTE/browse/clang-tools-extra/trunk/clangd/ ○ https://clang.llvm.org/extra/clangd.html#getting-involved

  • Reach us at “cfe-dev”