Index: clang/CMakeLists.txt =================================================================== --- clang/CMakeLists.txt +++ clang/CMakeLists.txt @@ -359,6 +359,10 @@ PATTERN "*.inc" PATTERN "*.h" ) + + install(PROGRAMS utils/bash-autocomplete.sh + DESTINATION share/clang + ) endif() add_definitions( -D_GNU_SOURCE ) Index: clang/include/clang/Driver/Options.td =================================================================== --- clang/include/clang/Driver/Options.td +++ clang/include/clang/Driver/Options.td @@ -469,6 +469,7 @@ 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">; Index: clang/lib/Driver/Driver.cpp =================================================================== --- clang/lib/Driver/Driver.cpp +++ clang/lib/Driver/Driver.cpp @@ -1216,6 +1216,13 @@ 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) { Index: clang/test/Driver/autocomplete.c =================================================================== --- /dev/null +++ clang/test/Driver/autocomplete.c @@ -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 Index: clang/utils/bash-autocomplete.sh =================================================================== --- /dev/null +++ clang/utils/bash-autocomplete.sh @@ -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 Index: llvm/include/llvm/Option/OptTable.h =================================================================== --- llvm/include/llvm/Option/OptTable.h +++ llvm/include/llvm/Option/OptTable.h @@ -113,6 +113,14 @@ return getInfo(id).MetaVar; } + /// Find flags from OptTable which starts with Cur. + /// + /// \param [in] Cur - String prefix that all returned flags need + // to start with. + /// + /// \return The vector of flags which start with Cur. + std::vector findByPrefix(StringRef Cur) const; + /// \brief Parse a single argument; returning the new argument and /// updating Index. /// Index: llvm/lib/Option/OptTable.cpp =================================================================== --- llvm/lib/Option/OptTable.cpp +++ llvm/lib/Option/OptTable.cpp @@ -186,6 +186,20 @@ return 0; } +std::vector OptTable::findByPrefix(StringRef Cur) const { + std::vector Ret; + for (const Info &In : OptionInfos.slice(FirstSearchableIndex)) { + if (!In.Prefixes) + continue; + for (int I = 0; In.Prefixes[I]; I++) { + std::string S = std::string(In.Prefixes[I]) + std::string(In.Name); + if (StringRef(S).startswith(Cur)) + Ret.push_back(S); + } + } + return Ret; +} + Arg *OptTable::ParseOneArg(const ArgList &Args, unsigned &Index, unsigned FlagsToInclude, unsigned FlagsToExclude) const {