Created
May 14, 2017 18:55
-
-
Save yamaguchi1024/4374ed2d13d6620b45aa888d530ce2de 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/Driver.h b/clang/include/clang/Driver/Driver.h | |
index 1009754..a856046 100644 | |
--- a/clang/include/clang/Driver/Driver.h | |
+++ b/clang/include/clang/Driver/Driver.h | |
@@ -398,6 +398,9 @@ public: | |
/// PrintActions - Print the list of actions. | |
void PrintActions(const Compilation &C) const; | |
+ /// PrintAutocomplete - Print the autocompletion for clang. | |
+ void PrintAutocomplete(StringRef Cur) const; | |
+ | |
/// PrintHelp - Print the help text. | |
/// | |
/// \param ShowHidden - Show hidden options. | |
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..a6e3b79 100644 | |
--- a/clang/lib/Driver/Driver.cpp | |
+++ b/clang/lib/Driver/Driver.cpp | |
@@ -76,6 +76,7 @@ | |
#endif | |
using namespace clang::driver; | |
+using namespace clang::driver::options; | |
using namespace clang; | |
using namespace llvm::opt; | |
@@ -1081,6 +1082,15 @@ int Driver::ExecuteCompilation( | |
return 0; | |
} | |
+void Driver::PrintAutocomplete(StringRef Cur) const { | |
+ // 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(Cur)) | |
+ llvm::outs() << S << " "; | |
+ | |
+ return; | |
+} | |
+ | |
void Driver::PrintHelp(bool ShowHidden) const { | |
unsigned IncludedFlagsBitmask; | |
unsigned ExcludedFlagsBitmask; | |
@@ -1216,6 +1226,11 @@ bool Driver::HandleImmediateArgs(const Compilation &C) { | |
return false; | |
} | |
+ if (Arg *A = C.getArgs().getLastArg(options::OPT_autocomplete)) { | |
+ PrintAutocomplete(A->getValue()); | |
+ 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..945e5fb 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. | |
/// | |
@@ -153,6 +155,8 @@ public: | |
unsigned &MissingArgCount, unsigned FlagsToInclude = 0, | |
unsigned FlagsToExclude = 0) const; | |
+ InputArgList MatchFlags(StringRef Cur) const; | |
+ | |
/// \brief Render the help text for an option table. | |
/// | |
/// \param OS - The stream to write the help text to. | |
diff --git a/llvm/lib/Option/OptTable.cpp b/llvm/lib/Option/OptTable.cpp | |
index 7eafb00..6b1b2f3 100644 | |
--- a/llvm/lib/Option/OptTable.cpp | |
+++ b/llvm/lib/Option/OptTable.cpp | |
@@ -186,6 +186,25 @@ 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) { | |
+ // for (OptTable::Info Opt : *this::Info) { | |
+ 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