[PATCH] D37249: [Bash-autocomplete] Refactor autocomplete code into own function

2017-08-29 Thread Yuka Takahashi via Phabricator via cfe-commits
This revision was automatically updated to reflect the committed changes.
Closed by commit rL312018: [Bash-autocomplete] Refactor autocomplete code into 
own function (authored by yamaguchi).

Changed prior to commit:
  https://reviews.llvm.org/D37249?vs=113027=113118#toc

Repository:
  rL LLVM

https://reviews.llvm.org/D37249

Files:
  cfe/trunk/include/clang/Driver/Driver.h
  cfe/trunk/lib/Driver/Driver.cpp

Index: cfe/trunk/lib/Driver/Driver.cpp
===
--- cfe/trunk/lib/Driver/Driver.cpp
+++ cfe/trunk/lib/Driver/Driver.cpp
@@ -1156,6 +1156,56 @@
 OS << i << ',' << DiagnosticIDs::getCategoryNameFromID(i) << '\n';
 }
 
+void Driver::handleAutocompletions(StringRef PassedFlags) const {
+  // Print out all options that start with a given argument. This is used for
+  // shell autocompletion.
+  std::vector SuggestedCompletions;
+
+  unsigned short DisableFlags =
+  options::NoDriverOption | options::Unsupported | options::Ignored;
+  // We want to show cc1-only options only when clang is invoked as "clang
+  // -cc1".
+  // When clang is invoked as "clang -cc1", we add "#" to the beginning of an
+  // --autocomplete
+  // option so that the clang driver can distinguish whether it is requested to
+  // show cc1-only options or not.
+  if (PassedFlags[0] == '#') {
+DisableFlags &= ~options::NoDriverOption;
+PassedFlags = PassedFlags.substr(1);
+  }
+
+  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, DisableFlags);
+
+// We have to query the -W flags manually as they're not in the OptTable.
+// TODO: Find a good way to add them to OptTable instead and them remove
+// this code.
+for (StringRef S : DiagnosticIDs::getDiagnosticFlags())
+  if (S.startswith(PassedFlags))
+SuggestedCompletions.push_back(S);
+  } 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);
+  }
+
+  // Sort the autocomplete candidates so that shells print them out in a
+  // deterministic order. We could sort in any way, but we chose
+  // case-insensitive sorting for consistency with the -help option
+  // which prints out options in the case-insensitive alphabetical order.
+  std::sort(SuggestedCompletions.begin(), SuggestedCompletions.end(),
+[](StringRef A, StringRef B) { return A.compare_lower(B) < 0; });
+
+  llvm::outs() << llvm::join(SuggestedCompletions, "\n") << '\n';
+}
+
 bool Driver::HandleImmediateArgs(const Compilation ) {
   // The order these options are handled in gcc is all over the place, but we
   // don't expect inconsistencies w.r.t. that to matter in practice.
@@ -1249,50 +1299,8 @@
   }
 
   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.
 StringRef PassedFlags = A->getValue();
-std::vector SuggestedCompletions;
-
-unsigned short DisableFlags = options::NoDriverOption | options::Unsupported | options::Ignored;
-// We want to show cc1-only options only when clang is invoked as "clang -cc1".
-// When clang is invoked as "clang -cc1", we add "#" to the beginning of an --autocomplete
-// option so that the clang driver can distinguish whether it is requested to show cc1-only options or not.
-if (PassedFlags[0] == '#') {
-  DisableFlags &= ~options::NoDriverOption;
-  PassedFlags = PassedFlags.substr(1);
-}
-
-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, DisableFlags);
-
-  // We have to query the -W flags manually as they're not in the OptTable.
-  // TODO: Find a good way to add them to OptTable instead and them remove
-  // this code.
-  for (StringRef S : DiagnosticIDs::getDiagnosticFlags())
-if (S.startswith(PassedFlags))
-  SuggestedCompletions.push_back(S);
-} 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 

[PATCH] D37249: [Bash-autocomplete] Refactor autocomplete code into own function

2017-08-29 Thread Raphael Isemann via Phabricator via cfe-commits
teemperor accepted this revision.
teemperor added a comment.
This revision is now accepted and ready to land.

LGTM!


https://reviews.llvm.org/D37249



___
cfe-commits mailing list
cfe-commits@lists.llvm.org
http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D37249: [Bash-autocomplete] Refactor autocomplete code into own function

2017-08-29 Thread Yuka Takahashi via Phabricator via cfe-commits
yamaguchi updated this revision to Diff 113027.
yamaguchi marked an inline comment as done.
yamaguchi added a comment.

Update diff. I agree that this is more generic.


https://reviews.llvm.org/D37249

Files:
  clang/include/clang/Driver/Driver.h
  clang/lib/Driver/Driver.cpp

Index: clang/lib/Driver/Driver.cpp
===
--- clang/lib/Driver/Driver.cpp
+++ clang/lib/Driver/Driver.cpp
@@ -1157,6 +1157,56 @@
 OS << i << ',' << DiagnosticIDs::getCategoryNameFromID(i) << '\n';
 }
 
+void Driver::handleAutocompletions(StringRef PassedFlags) const {
+  // Print out all options that start with a given argument. This is used for
+  // shell autocompletion.
+  std::vector SuggestedCompletions;
+
+  unsigned short DisableFlags =
+  options::NoDriverOption | options::Unsupported | options::Ignored;
+  // We want to show cc1-only options only when clang is invoked as "clang
+  // -cc1".
+  // When clang is invoked as "clang -cc1", we add "#" to the beginning of an
+  // --autocomplete
+  // option so that the clang driver can distinguish whether it is requested to
+  // show cc1-only options or not.
+  if (PassedFlags[0] == '#') {
+DisableFlags &= ~options::NoDriverOption;
+PassedFlags = PassedFlags.substr(1);
+  }
+
+  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, DisableFlags);
+
+// We have to query the -W flags manually as they're not in the OptTable.
+// TODO: Find a good way to add them to OptTable instead and them remove
+// this code.
+for (StringRef S : DiagnosticIDs::getDiagnosticFlags())
+  if (S.startswith(PassedFlags))
+SuggestedCompletions.push_back(S);
+  } 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);
+  }
+
+  // Sort the autocomplete candidates so that shells print them out in a
+  // deterministic order. We could sort in any way, but we chose
+  // case-insensitive sorting for consistency with the -help option
+  // which prints out options in the case-insensitive alphabetical order.
+  std::sort(SuggestedCompletions.begin(), SuggestedCompletions.end(),
+[](StringRef A, StringRef B) { return A.compare_lower(B) < 0; });
+
+  llvm::outs() << llvm::join(SuggestedCompletions, "\n") << '\n';
+}
+
 bool Driver::HandleImmediateArgs(const Compilation ) {
   // The order these options are handled in gcc is all over the place, but we
   // don't expect inconsistencies w.r.t. that to matter in practice.
@@ -1250,50 +1300,8 @@
   }
 
   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.
 StringRef PassedFlags = A->getValue();
-std::vector SuggestedCompletions;
-
-unsigned short DisableFlags = options::NoDriverOption | options::Unsupported | options::Ignored;
-// We want to show cc1-only options only when clang is invoked as "clang -cc1".
-// When clang is invoked as "clang -cc1", we add "#" to the beginning of an --autocomplete
-// option so that the clang driver can distinguish whether it is requested to show cc1-only options or not.
-if (PassedFlags[0] == '#') {
-  DisableFlags &= ~options::NoDriverOption;
-  PassedFlags = PassedFlags.substr(1);
-}
-
-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, DisableFlags);
-
-  // We have to query the -W flags manually as they're not in the OptTable.
-  // TODO: Find a good way to add them to OptTable instead and them remove
-  // this code.
-  for (StringRef S : DiagnosticIDs::getDiagnosticFlags())
-if (S.startswith(PassedFlags))
-  SuggestedCompletions.push_back(S);
-} 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(',');
-  

[PATCH] D37249: [Bash-autocomplete] Refactor autocomplete code into own function

2017-08-28 Thread Raphael Isemann via Phabricator via cfe-commits
teemperor added inline comments.



Comment at: clang/lib/Driver/Driver.cpp:1160
 
+void Driver::handleAutocompletions(const llvm::opt::Arg *A) const {
+  // Print out all options that start with a given argument. This is used for

I think a more generic interface would be `void 
Driver::handleAutocompletions(StringRef PassedFlags) const {` as we don't 
really require that the user of this function actually has an `Arg` with a 
value before calling this function.


https://reviews.llvm.org/D37249



___
cfe-commits mailing list
cfe-commits@lists.llvm.org
http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D37249: [Bash-autocomplete] Refactor autocomplete code into own function

2017-08-28 Thread Yuka Takahashi via Phabricator via cfe-commits
yamaguchi created this revision.

We wrote many codes in HandleImediateArgs, so I've refactored it into
handleAutocompletions.


https://reviews.llvm.org/D37249

Files:
  clang/include/clang/Driver/Driver.h
  clang/lib/Driver/Driver.cpp

Index: clang/lib/Driver/Driver.cpp
===
--- clang/lib/Driver/Driver.cpp
+++ clang/lib/Driver/Driver.cpp
@@ -1157,6 +1157,57 @@
 OS << i << ',' << DiagnosticIDs::getCategoryNameFromID(i) << '\n';
 }
 
+void Driver::handleAutocompletions(const llvm::opt::Arg *A) const {
+  // Print out all options that start with a given argument. This is used for
+  // shell autocompletion.
+  StringRef PassedFlags = A->getValue();
+  std::vector SuggestedCompletions;
+
+  unsigned short DisableFlags =
+  options::NoDriverOption | options::Unsupported | options::Ignored;
+  // We want to show cc1-only options only when clang is invoked as "clang
+  // -cc1".
+  // When clang is invoked as "clang -cc1", we add "#" to the beginning of an
+  // --autocomplete
+  // option so that the clang driver can distinguish whether it is requested to
+  // show cc1-only options or not.
+  if (PassedFlags[0] == '#') {
+DisableFlags &= ~options::NoDriverOption;
+PassedFlags = PassedFlags.substr(1);
+  }
+
+  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, DisableFlags);
+
+// We have to query the -W flags manually as they're not in the OptTable.
+// TODO: Find a good way to add them to OptTable instead and them remove
+// this code.
+for (StringRef S : DiagnosticIDs::getDiagnosticFlags())
+  if (S.startswith(PassedFlags))
+SuggestedCompletions.push_back(S);
+  } 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);
+  }
+
+  // Sort the autocomplete candidates so that shells print them out in a
+  // deterministic order. We could sort in any way, but we chose
+  // case-insensitive sorting for consistency with the -help option
+  // which prints out options in the case-insensitive alphabetical order.
+  std::sort(SuggestedCompletions.begin(), SuggestedCompletions.end(),
+[](StringRef A, StringRef B) { return A.compare_lower(B) < 0; });
+
+  llvm::outs() << llvm::join(SuggestedCompletions, "\n") << '\n';
+}
+
 bool Driver::HandleImmediateArgs(const Compilation ) {
   // The order these options are handled in gcc is all over the place, but we
   // don't expect inconsistencies w.r.t. that to matter in practice.
@@ -1250,50 +1301,7 @@
   }
 
   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.
-StringRef PassedFlags = A->getValue();
-std::vector SuggestedCompletions;
-
-unsigned short DisableFlags = options::NoDriverOption | options::Unsupported | options::Ignored;
-// We want to show cc1-only options only when clang is invoked as "clang -cc1".
-// When clang is invoked as "clang -cc1", we add "#" to the beginning of an --autocomplete
-// option so that the clang driver can distinguish whether it is requested to show cc1-only options or not.
-if (PassedFlags[0] == '#') {
-  DisableFlags &= ~options::NoDriverOption;
-  PassedFlags = PassedFlags.substr(1);
-}
-
-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, DisableFlags);
-
-  // We have to query the -W flags manually as they're not in the OptTable.
-  // TODO: Find a good way to add them to OptTable instead and them remove
-  // this code.
-  for (StringRef S : DiagnosticIDs::getDiagnosticFlags())
-if (S.startswith(PassedFlags))
-  SuggestedCompletions.push_back(S);
-} 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(',');
-