A better shell command-line autocompletion for Clang Yuka Takahashi - - PowerPoint PPT Presentation

a better shell command line autocompletion for clang
SMART_READER_LITE
LIVE PREVIEW

A better shell command-line autocompletion for Clang Yuka Takahashi - - PowerPoint PPT Presentation

A better shell command-line autocompletion for Clang Yuka Takahashi - Univ. of Tokyo Google Summer of Code 2017 Mentors: Raphael Isemann and Vassil Vassilev Normal shell completion (1/2) Collect a list of flags and write a custom shell


slide-1
SLIDE 1

Yuka Takahashi - Univ. of Tokyo

Google Summer of Code 2017 Mentors: Raphael Isemann and Vassil Vassilev

A better shell command-line autocompletion for Clang

slide-2
SLIDE 2

➢ Collect a list of flags and write a custom shell script. ➢ It is a lot of work to collect flags, and portability is low.

1

Yuka Takahashi, LLVM dev meeting 2017

case $prev in

  • -help|-v|--version|-F|--separator) return ;;
  • m|--magic-file|-f|--files-from)

_filedir return ;; ... Example: `file` command bash autocompletion

Normal shell completion (1/2)

slide-3
SLIDE 3

Normal shell completion (2/2)

Yuka Takahashi, LLVM dev meeting 2017

Example: `gcc` command bash autocompletion COMPREPLY=( $( compgen -W "$( $cc --help 2>/dev/null | tr '\t' ' ' |\ command sed -e '/^ *-/!d' -e 's/ *-\([^][ <>]*\).*/-\1/' )" -- "$cur" ) ) [[ $COMPREPLY == *= ]] && compopt -o nospace 2

Normal shell completion (2/2) Normal shell completion (2/2)

➢ Script has to parse `help -v`. ➢ They can’t autocomplete flag’s values because help doesn’t show them.

slide-4
SLIDE 4

Clang

  • -autocomplete=<query>

Result Bash

Yuka Takahashi, LLVM dev meeting 2017

3

In this project (1/3) bash-completion in Clang (1/3)

➢ We made an API in Clang which dynamically queries available flags.

slide-5
SLIDE 5

In this project (2/3)

$ build/bin/clang --autocomplete=-tr

  • traditional-cpp Enable some traditional CPP emulation
  • trigraphs Process trigraph sequences

Yuka Takahashi, LLVM dev meeting 2017

4

In this project (2/3) bash-completion in Clang (2/3)

➢ You can always complete one flag at a time. ➢ Just pass this flag to the --autocomplete flag in the selected clang binary.

Example: Clang API for autocompletion

slide-6
SLIDE 6

$ build/bin/clang --autocomplete=-stdlib=,l libc++ libstdc++

Yuka Takahashi, LLVM dev meeting 2017

5

In this project (3/3) bash-completion in Clang (3/3)

➢ The API also supports completing the values of flags. ➢ Shell should provide an incomplete value behind the flag separated by a comma.

Example: Clang API for value autocompletion

slide-7
SLIDE 7

Yuka Takahashi, LLVM dev meeting 2017

if [[ "$cur" == -* ]]; then arg="$arg$cur" elif [[ "$prev" == -* && "$cur" == '=' ]]; then arg="$arg$prev=," …. flags=$("$path" --autocomplete="$arg" 2>/dev/null | sed -e 's/\t.*//') 6

Shell Implementation Shell implementation

➢ For `clang -std=[tab]`, shell has to parse the command and execute `clang --autocomplete=-std=,`

Example: Implementation for bash

slide-8
SLIDE 8

Yuka Takahashi, LLVM dev meeting 2017

QString origArg = arg; if (arg == "") arg = "#"; if (arg.endsWith("=")) { arg += ","; completeValue = true; } else if (arg.contains("=")) { .... params << QString("--autocomplete=" + arg); pingProcess.execute(exec, params);

7

IDE Implementation IDE implementation

Example: Implementation for Qt

slide-9
SLIDE 9

Yuka Takahashi, LLVM dev meeting 2017

8

Advantages Advantages

➢ It can support every future version of Clang, starting from Clang 5.0. ➢ With this API, autocompletion can be built on any shell and has high portability.

slide-10
SLIDE 10

Yuka Takahashi, LLVM dev meeting 2017

Further information Further Information

➢ LLVM blog:

blog.llvm.org/2017/09/clang-bash-better-auto-completion-is.html

➢ GSoC final report:

summerofcode.withgoogle.com/projects/#6620432915496960 9

slide-11
SLIDE 11

Thanks for your attention!