Created
May 14, 2017 19:07
-
-
Save yamaguchi1024/2000c4bfea2664fc8271910f96568467 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
diff --git a/clang/include/clang/Driver/Options.td b/clang/include/clang/Driver/Options.td | |
index deec33b..3000d90 100644 | |
--- a/clang/include/clang/Driver/Options.td | |
+++ b/clang/include/clang/Driver/Options.td | |
@@ -469,6 +469,8 @@ 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=">, Flags<[DriverOption]>, Group<Action_Group>, | |
+ HelpText<"This is a flag for clang-autocompletion">; | |
def bind__at__load : Flag<["-"], "bind_at_load">; | |
def bundle__loader : Separate<["-"], "bundle_loader">; | |
def bundle : Flag<["-"], "bundle">; | |
diff --git a/clang/lib/Driver/Driver.cpp b/clang/lib/Driver/Driver.cpp | |
index f36deff..f7c4cae 100644 | |
--- a/clang/lib/Driver/Driver.cpp | |
+++ b/clang/lib/Driver/Driver.cpp | |
@@ -1216,6 +1216,15 @@ bool Driver::HandleImmediateArgs(const Compilation &C) { | |
return false; | |
} | |
+ if (Arg *A = C.getArgs().getLastArg(options::OPT_autocomplete)) { | |
+ // Call function in OptTable with argument Cur. This function is supposed to | |
+ // return all flags with start with Cur, in InputArglist. | |
+ for (std::string &S : Opts->matchFlags(A->getValue())) | |
+ llvm::outs() << S << " "; | |
+ llvm::outs() << "\n"; | |
+ return false; | |
+ } | |
+ | |
if (C.getArgs().hasArg(options::OPT_print_libgcc_file_name)) { | |
ToolChain::RuntimeLibType RLT = TC.GetRuntimeLibType(C.getArgs()); | |
switch (RLT) { | |
diff --git a/clang/test/Driver/autocomplete.c b/clang/test/Driver/autocomplete.c | |
new file mode 100644 | |
index 0000000..fb7e409 | |
--- /dev/null | |
+++ b/clang/test/Driver/autocomplete.c | |
@@ -0,0 +1,6 @@ | |
+// RUN: %clang -autocomplete=-fsyn > %t | |
+// grep "-fsyntax-only" %t | |
+// RUN: %clang -autocomplete=-fno-sanitize-t > %t | |
+// grep "-fno-sanitize-thread-atomics -fno-sanitize-thread-func-entry-exit -fno-sanitize-thread-memory-access -fno-sanitize-trap=" %t | |
+// RUN: %clang -autocomplete=foo > %t | |
+// not grep "*" %t | |
diff --git a/llvm/include/llvm/Option/OptTable.h b/llvm/include/llvm/Option/OptTable.h | |
index 390e527..0b1ea6c 100644 | |
--- a/llvm/include/llvm/Option/OptTable.h | |
+++ b/llvm/include/llvm/Option/OptTable.h | |
@@ -113,6 +113,8 @@ public: | |
return getInfo(id).MetaVar; | |
} | |
+ std::vector<std::string> matchFlags(StringRef Cur) const; | |
+ | |
/// \brief Parse a single argument; returning the new argument and | |
/// updating Index. | |
/// | |
diff --git a/llvm/lib/Option/OptTable.cpp b/llvm/lib/Option/OptTable.cpp | |
index 7eafb00..c23ce57 100644 | |
--- a/llvm/lib/Option/OptTable.cpp | |
+++ b/llvm/lib/Option/OptTable.cpp | |
@@ -186,6 +186,24 @@ static unsigned matchOption(const OptTable::Info *I, StringRef Str, | |
return 0; | |
} | |
+std::vector<std::string> OptTable::matchFlags(StringRef Cur) const { | |
+ std::vector<std::string> Ret; | |
+ const Info *Start = OptionInfos.begin() + FirstSearchableIndex; | |
+ const Info *End = OptionInfos.end(); | |
+ for (; Start != End; ++Start) { | |
+ if (Start->Prefixes) { | |
+ for (int i = 0; Start->Prefixes[i]; i++) { | |
+ std::string s = | |
+ (std::string(Start->Prefixes[i]) + std::string(Start->Name)); | |
+ StringRef CurFlag = s; | |
+ if (CurFlag.startswith(Cur)) | |
+ Ret.push_back(s); | |
+ } | |
+ } | |
+ } | |
+ return Ret; | |
+} | |
+ | |
Arg *OptTable::ParseOneArg(const ArgList &Args, unsigned &Index, | |
unsigned FlagsToInclude, | |
unsigned FlagsToExclude) const { |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment