Skip to content

Commit

Permalink
[GSoC] Shell autocompletion for clang
Browse files Browse the repository at this point in the history
Summary:
This is a first patch for GSoC project, bash-completion for clang.
To use this on bash, please run `source clang/utils/bash-autocomplete.sh`.
bash-autocomplete.sh is code for bash-completion.

Simple flag completion and path completion is available in this patch.

Reviewers: teemperor, v.g.vassilev, ruiu, Bigcheese, efriedma

Subscribers: llvm-commits

Differential Revision: https://reviews.llvm.org/D33237

git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@303670 91177308-0d34-0410-b5e6-96231b3b80d8
  • Loading branch information
yamaguchi1024 committed May 23, 2017
1 parent 0cb0ba2 commit d7b775c
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 0 deletions.
4 changes: 4 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -359,6 +359,10 @@ if (NOT LLVM_INSTALL_TOOLCHAIN_ONLY)
PATTERN "*.inc"
PATTERN "*.h"
)

install(PROGRAMS utils/bash-autocomplete.sh
DESTINATION share/clang
)
endif()

add_definitions( -D_GNU_SOURCE )
Expand Down
1 change: 1 addition & 0 deletions include/clang/Driver/Options.td
Original file line number Diff line number Diff line change
Expand Up @@ -469,6 +469,7 @@ def arch__errors__fatal : Flag<["-"], "arch_errors_fatal">;
def arch : Separate<["-"], "arch">, Flags<[DriverOption]>;
def arch__only : Separate<["-"], "arch_only">;
def a : Joined<["-"], "a">;
def autocomplete : Joined<["--"], "autocomplete=">;
def bind__at__load : Flag<["-"], "bind_at_load">;
def bundle__loader : Separate<["-"], "bundle_loader">;
def bundle : Flag<["-"], "bundle">;
Expand Down
7 changes: 7 additions & 0 deletions lib/Driver/Driver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1216,6 +1216,13 @@ bool Driver::HandleImmediateArgs(const Compilation &C) {
return false;
}

if (Arg *A = C.getArgs().getLastArg(options::OPT_autocomplete)) {
// Print out all options that start with a given argument. This is used for
// shell autocompletion.
llvm::outs() << llvm::join(Opts->findByPrefix(A->getValue()), " ") << '\n';
return false;
}

if (C.getArgs().hasArg(options::OPT_print_libgcc_file_name)) {
ToolChain::RuntimeLibType RLT = TC.GetRuntimeLibType(C.getArgs());
switch (RLT) {
Expand Down
6 changes: 6 additions & 0 deletions test/Driver/autocomplete.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
// RUN: %clang --autocomplete=-fsyn | FileCheck %s -check-prefix=FSYN
// FSYN: -fsyntax-only
// RUN: %clang --autocomplete=-s | FileCheck %s -check-prefix=STD
// STD: -std={{.*}}-stdlib=
// RUN: %clang --autocomplete=foo | not FileCheck %s -check-prefix=NONE
// NONE: foo
14 changes: 14 additions & 0 deletions utils/bash-autocomplete.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# Please add "source /path/to/bash-autocomplete.sh" to your .bashrc to use this.
_clang()
{
local cur prev words cword flags
_init_completion -n : || return

flags=$( clang --autocomplete="$cur" )
if [[ "$flags" == "" || "$cur" == "" ]]; then
_filedir
else
COMPREPLY=( $( compgen -W "$flags" -- "$cur" ) )
fi
}
complete -F _clang clang

0 comments on commit d7b775c

Please sign in to comment.