From: Yuka Takahashi Date: Tue, 20 Jun 2017 16:31:31 +0000 (+0000) Subject: [GSoC] Flag value completion for clang X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=f5f37badaf42c9f95d12cedd7d097cf7673c8817;p=clang [GSoC] Flag value completion for clang This is 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. In this patch, Options.td was mainly changed in order to add value class in Options.inc. git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@305805 91177308-0d34-0410-b5e6-96231b3b80d8 --- diff --git a/include/clang/Driver/Options.h b/include/clang/Driver/Options.h index 57e4452f3e..2da3cb4828 100644 --- a/include/clang/Driver/Options.h +++ b/include/clang/Driver/Options.h @@ -39,8 +39,9 @@ enum ClangFlags { enum ID { OPT_INVALID = 0, // This is not an option ID. -#define OPTION(PREFIX, NAME, ID, KIND, GROUP, ALIAS, ALIASARGS, FLAGS, PARAM, \ - HELPTEXT, METAVAR) OPT_##ID, +#define OPTION(PREFIX, NAME, ID, KIND, GROUP, ALIAS, ALIASARGS, FLAGS, PARAM, \ + HELPTEXT, METAVAR, VALUES) \ + OPT_##ID, #include "clang/Driver/Options.inc" LastOption #undef OPTION diff --git a/include/clang/Driver/Options.td b/include/clang/Driver/Options.td index 6c51976e98..bd4d521752 100644 --- a/include/clang/Driver/Options.td +++ b/include/clang/Driver/Options.td @@ -493,7 +493,7 @@ def cl_mad_enable : Flag<["-"], "cl-mad-enable">, Group, Flags<[CC def cl_no_signed_zeros : Flag<["-"], "cl-no-signed-zeros">, Group, Flags<[CC1Option]>, HelpText<"OpenCL only. Allow use of less precise no signed zeros computations in the generated binary.">; def cl_std_EQ : Joined<["-"], "cl-std=">, Group, Flags<[CC1Option]>, - HelpText<"OpenCL language standard to compile for.">; + HelpText<"OpenCL language standard to compile for.">, Values<"cl,CL,cl1.1,CL1.1,cl1.2,CL1.2,cl2.0,CL2.0">; def cl_denorms_are_zero : Flag<["-"], "cl-denorms-are-zero">, Group, Flags<[CC1Option]>, HelpText<"OpenCL only. Allow denormals to be flushed to zero.">; def cl_fp32_correctly_rounded_divide_sqrt : Flag<["-"], "cl-fp32-correctly-rounded-divide-sqrt">, Group, Flags<[CC1Option]>, @@ -804,7 +804,7 @@ def fno_sanitize_coverage : CommaJoined<["-"], "fno-sanitize-coverage=">, Group, Flags<[CoreOption, DriverOption]>, HelpText<"Disable specified features of coverage instrumentation for " - "Sanitizers">; + "Sanitizers">, Values<"func,bb,edge,indirect-calls,trace-bb,trace-cmp,trace-div,trace-gep,8bit-counters,trace-pc,trace-pc-guard,no-prune,inline-8bit-counters">; def fsanitize_memory_track_origins_EQ : Joined<["-"], "fsanitize-memory-track-origins=">, Group, HelpText<"Enable origins tracking in MemorySanitizer">; @@ -923,7 +923,7 @@ def ftrapping_math : Flag<["-"], "ftrapping-math">, Group, Flags<[CC1Op def fno_trapping_math : Flag<["-"], "fno-trapping-math">, Group, Flags<[CC1Option]>; def ffp_contract : Joined<["-"], "ffp-contract=">, Group, Flags<[CC1Option]>, HelpText<"Form fused FP ops (e.g. FMAs): fast (everywhere)" - " | on (according to FP_CONTRACT pragma, default) | off (never fuse)">; + " | on (according to FP_CONTRACT pragma, default) | off (never fuse)">, Values<"fast,on,off">; def ffor_scope : Flag<["-"], "ffor-scope">, Group; def fno_for_scope : Flag<["-"], "fno-for-scope">, Group; @@ -1000,7 +1000,7 @@ def flat__namespace : Flag<["-"], "flat_namespace">; def flax_vector_conversions : Flag<["-"], "flax-vector-conversions">, Group; def flimited_precision_EQ : Joined<["-"], "flimited-precision=">, Group; def flto_EQ : Joined<["-"], "flto=">, Flags<[CoreOption, CC1Option]>, Group, - HelpText<"Set LTO mode to either 'full' or 'thin'">; + HelpText<"Set LTO mode to either 'full' or 'thin'">, Values<"thin,full">; def flto : Flag<["-"], "flto">, Flags<[CoreOption, CC1Option]>, Group, HelpText<"Enable LTO in 'full' mode">; def fno_lto : Flag<["-"], "fno-lto">, Group, @@ -1158,7 +1158,7 @@ def fno_experimental_new_pass_manager : Flag<["-"], "fno-experimental-new-pass-m Group, Flags<[CC1Option]>, HelpText<"Disables an experimental new pass manager in LLVM.">; def fveclib : Joined<["-"], "fveclib=">, Group, Flags<[CC1Option]>, - HelpText<"Use the given vector functions library">; + HelpText<"Use the given vector functions library">, Values<"Accelerate,SVML,none">; def fno_lax_vector_conversions : Flag<["-"], "fno-lax-vector-conversions">, Group, HelpText<"Disallow implicit conversions between vectors with a different number of elements or different element types">, Flags<[CC1Option]>; def fno_merge_all_constants : Flag<["-"], "fno-merge-all-constants">, Group, @@ -1342,7 +1342,7 @@ def fno_short_wchar : Flag<["-"], "fno-short-wchar">, Group, Flags<[CC1 HelpText<"Force wchar_t to be an unsigned int">; def fshow_overloads_EQ : Joined<["-"], "fshow-overloads=">, Group, Flags<[CC1Option]>, HelpText<"Which overload candidates to show when overload resolution fails: " - "best|all; defaults to all">; + "best|all; defaults to all">, Values<"best,all">; def fshow_column : Flag<["-"], "fshow-column">, Group, Flags<[CC1Option]>; def fshow_source_location : Flag<["-"], "fshow-source-location">, Group; def fspell_checking : Flag<["-"], "fspell-checking">, Group; @@ -1456,7 +1456,7 @@ def fuse_init_array : Flag<["-"], "fuse-init-array">, Group, Flags<[CC1 def fno_var_tracking : Flag<["-"], "fno-var-tracking">, Group; def fverbose_asm : Flag<["-"], "fverbose-asm">, Group; def fvisibility_EQ : Joined<["-"], "fvisibility=">, Group, - HelpText<"Set the default symbol visibility for all global declarations">; + HelpText<"Set the default symbol visibility for all global declarations">, Values<"hidden,default">; def fvisibility_inlines_hidden : Flag<["-"], "fvisibility-inlines-hidden">, Group, HelpText<"Give inline C++ member functions default visibility by default">, Flags<[CC1Option]>; @@ -1677,7 +1677,7 @@ def minline_all_stringops : Flag<["-"], "minline-all-stringops">, Group, Group; def malign_double : Flag<["-"], "malign-double">, Group, Flags<[CC1Option]>, HelpText<"Align doubles to two words in structs (x86 only)">; -def mfloat_abi_EQ : Joined<["-"], "mfloat-abi=">, Group; +def mfloat_abi_EQ : Joined<["-"], "mfloat-abi=">, Group, Values<"soft,softfp,hard">; def mfpmath_EQ : Joined<["-"], "mfpmath=">, Group; def mfpu_EQ : Joined<["-"], "mfpu=">, Group; def mhwdiv_EQ : Joined<["-"], "mhwdiv=">, Group; @@ -1709,9 +1709,9 @@ def mstack_alignment : Joined<["-"], "mstack-alignment=">, Group, Flags def mstack_probe_size : Joined<["-"], "mstack-probe-size=">, Group, Flags<[CC1Option]>, HelpText<"Set the stack probe size">; def mthread_model : Separate<["-"], "mthread-model">, Group, Flags<[CC1Option]>, - HelpText<"The thread model to use, e.g. posix, single (posix by default)">; + HelpText<"The thread model to use, e.g. posix, single (posix by default)">, Values<"posix,single">; def meabi : Separate<["-"], "meabi">, Group, Flags<[CC1Option]>, - HelpText<"Set EABI type, e.g. 4, 5 or gnu (default depends on triple)">; + HelpText<"Set EABI type, e.g. 4, 5 or gnu (default depends on triple)">, Values<"default,4,5,gnu">; def mmmx : Flag<["-"], "mmmx">, Group; def mno_3dnowa : Flag<["-"], "mno-3dnowa">, Group; @@ -2205,7 +2205,7 @@ def std_default_EQ : Joined<["-"], "std-default=">; def std_EQ : Joined<["-", "--"], "std=">, Flags<[CC1Option]>, Group, HelpText<"Language standard to compile for">; def stdlib_EQ : Joined<["-", "--"], "stdlib=">, Flags<[CC1Option]>, - HelpText<"C++ standard library to use">; + HelpText<"C++ standard library to use">, Values<"libc++,libstdc++,platform">; def sub__library : JoinedOrSeparate<["-"], "sub_library">; def sub__umbrella : JoinedOrSeparate<["-"], "sub_umbrella">; def system_header_prefix : Joined<["--"], "system-header-prefix=">, diff --git a/lib/Driver/Driver.cpp b/lib/Driver/Driver.cpp index eb504fd4e5..850f6bc9dd 100644 --- a/lib/Driver/Driver.cpp +++ b/lib/Driver/Driver.cpp @@ -1227,7 +1227,25 @@ bool Driver::HandleImmediateArgs(const Compilation &C) { 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'; + StringRef PassedFlags = A->getValue(); + std::vector SuggestedCompletions; + + if (PassedFlags.find(',') == StringRef::npos) { + // If the flag is in the form of "--autocomplete=-foo", + // we were requested to print out all option names that start with "-foo". + // For example, "--autocomplete=-fsyn" is expanded to "-fsyntax-only". + SuggestedCompletions = Opts->findByPrefix(PassedFlags); + } else { + // If the flag is in the form of "--autocomplete=foo,bar", we were + // requested to print out all option values for "-foo" that start with + // "bar". For example, + // "--autocomplete=-stdlib=,l" is expanded to "libc++" and "libstdc++". + StringRef Option, Arg; + std::tie(Option, Arg) = PassedFlags.split(','); + SuggestedCompletions = Opts->suggestValueCompletions(Option, Arg); + } + + llvm::outs() << llvm::join(SuggestedCompletions, " ") << '\n'; return false; } diff --git a/lib/Driver/DriverOptions.cpp b/lib/Driver/DriverOptions.cpp index 6a7410901d..ac63b96cf9 100644 --- a/lib/Driver/DriverOptions.cpp +++ b/lib/Driver/DriverOptions.cpp @@ -21,10 +21,10 @@ using namespace llvm::opt; #undef PREFIX static const OptTable::Info InfoTable[] = { -#define OPTION(PREFIX, NAME, ID, KIND, GROUP, ALIAS, ALIASARGS, FLAGS, PARAM, \ - HELPTEXT, METAVAR) \ - { PREFIX, NAME, HELPTEXT, METAVAR, OPT_##ID, Option::KIND##Class, PARAM, \ - FLAGS, OPT_##GROUP, OPT_##ALIAS, ALIASARGS }, +#define OPTION(PREFIX, NAME, ID, KIND, GROUP, ALIAS, ALIASARGS, FLAGS, PARAM, \ + HELPTEXT, METAVAR, VALUES) \ + {PREFIX, NAME, HELPTEXT, METAVAR, OPT_##ID, Option::KIND##Class, \ + PARAM, FLAGS, OPT_##GROUP, OPT_##ALIAS, ALIASARGS, VALUES}, #include "clang/Driver/Options.inc" #undef OPTION }; diff --git a/test/Driver/autocomplete.c b/test/Driver/autocomplete.c index 94649f2437..78ac4b07cf 100644 --- a/test/Driver/autocomplete.c +++ b/test/Driver/autocomplete.c @@ -4,3 +4,33 @@ // STD: -std={{.*}}-stdlib= // RUN: %clang --autocomplete=foo | not FileCheck %s -check-prefix=NONE // NONE: foo +// RUN: %clang --autocomplete=-stdlib=,l | FileCheck %s -check-prefix=STDLIB +// STDLIB: libc++ libstdc++ +// RUN: %clang --autocomplete=-stdlib=, | FileCheck %s -check-prefix=STDLIBALL +// STDLIBALL: libc++ libstdc++ platform +// RUN: %clang --autocomplete=-meabi,d | FileCheck %s -check-prefix=MEABI +// MEABI: default +// RUN: %clang --autocomplete=-meabi, | FileCheck %s -check-prefix=MEABIALL +// MEABIALL: default 4 5 gnu +// RUN: %clang --autocomplete=-cl-std=,CL2 | FileCheck %s -check-prefix=CLSTD +// CLSTD: CL2.0 +// RUN: %clang --autocomplete=-cl-std=, | FileCheck %s -check-prefix=CLSTDALL +// CLSTDALL: cl CL cl1.1 CL1.1 cl1.2 CL1.2 cl2.0 CL2.0 +// RUN: %clang --autocomplete=-fno-sanitize-coverage=,f | FileCheck %s -check-prefix=FNOSANICOVER +// FNOSANICOVER: func +// RUN: %clang --autocomplete=-fno-sanitize-coverage=, | FileCheck %s -check-prefix=FNOSANICOVERALL +// FNOSANICOVERALL: func bb edge indirect-calls trace-bb trace-cmp trace-div trace-gep 8bit-counters trace-pc trace-pc-guard no-prune inline-8bit-counters +// RUN: %clang --autocomplete=-ffp-contract=, | FileCheck %s -check-prefix=FFPALL +// FFPALL: fast on off +// RUN: %clang --autocomplete=-flto=, | FileCheck %s -check-prefix=FLTOALL +// FLTOALL: thin full +// RUN: %clang --autocomplete=-fveclib=, | FileCheck %s -check-prefix=FVECLIBALL +// FVECLIBALL: Accelerate SVML none +// RUN: %clang --autocomplete=-fshow-overloads=, | FileCheck %s -check-prefix=FSOVERALL +// FSOVERALL: best all +// RUN: %clang --autocomplete=-fvisibility=, | FileCheck %s -check-prefix=FVISIBILITYALL +// FVISIBILITYALL: hidden default +// RUN: %clang --autocomplete=-mfloat-abi=, | FileCheck %s -check-prefix=MFLOATABIALL +// MFLOATABIALL: soft softfp hard +// RUN: %clang --autocomplete=-mthread-model, | FileCheck %s -check-prefix=MTHREADMODELALL +// MTHREADMODELALL: posix single diff --git a/utils/bash-autocomplete.sh b/utils/bash-autocomplete.sh index de4a435b88..3e9f65f10d 100644 --- a/utils/bash-autocomplete.sh +++ b/utils/bash-autocomplete.sh @@ -1,11 +1,35 @@ # Please add "source /path/to/bash-autocomplete.sh" to your .bashrc to use this. _clang() { - local cur prev words cword flags + local cur prev words cword arg _init_completion -n : || return - flags=$( clang --autocomplete="$cur" ) - if [[ "$flags" == "" || "$cur" == "" ]]; then + # bash always separates '=' as a token even if there's no space before/after '='. + # On the other hand, '=' is just a regular character for clang options that + # contain '='. For example, "-stdlib=" is defined as is, instead of "-stdlib" and "=". + # So, we need to partially undo bash tokenization here for integrity. + local w1="${COMP_WORDS[$cword - 1]}" + local w2="${COMP_WORDS[$cword - 2]}" + if [[ "$cur" == -* ]]; then + # -foo + arg="$cur" + elif [[ "$w1" == -* && "$cur" == '=' ]]; then + # -foo= + arg="$w1=," + elif [[ "$w1" == -* ]]; then + # -foo or -foo bar + arg="$w1,$cur" + elif [[ "$w2" == -* && "$w1" == '=' ]]; then + # -foo=bar + arg="$w2=,$cur" + else + _filedir + fi + + local flags=$( clang --autocomplete="$arg" ) + if [[ "$cur" == "=" ]]; then + COMPREPLY=( $( compgen -W "$flags" -- "") ) + elif [[ "$flags" == "" ]]; then _filedir else COMPREPLY=( $( compgen -W "$flags" -- "$cur" ) )