[PATCH] D61373: Fix OptTable::findNearest() adding delimiter for free

Nico Weber via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Wed May 1 05:52:08 PDT 2019


thakis created this revision.
thakis added reviewers: modocache, MaskRay.
Herald added a subscriber: hiraditya.
Herald added a project: LLVM.

Prior to this, OptTable::findNearest() thought that the input `--foo` had an editing distance of 0 from an existing flag `--foo=`, which made it suggest flags with delimiters more often than flags without one. After this, it correctly assigns this case an editing distance of 1.


https://reviews.llvm.org/D61373

Files:
  llvm/lib/Option/OptTable.cpp
  llvm/unittests/Option/OptionParsingTest.cpp
  llvm/unittests/Option/Opts.td


Index: llvm/unittests/Option/Opts.td
===================================================================
--- llvm/unittests/Option/Opts.td
+++ llvm/unittests/Option/Opts.td
@@ -36,4 +36,7 @@
 def Doopf2 : Flag<["-"], "doopf2">, HelpText<"The doopf2 option">, Flags<[OptFlag2]>;
 def Ermgh : Joined<["--"], "ermgh">, HelpText<"The ermgh option">, MetaVarName<"ERMGH">, Flags<[OptFlag1]>;
 def Fjormp : Flag<["--"], "fjormp">, HelpText<"The fjormp option">, Flags<[OptFlag1]>;
+
+def Glormp_eq : Flag<["--"], "glormp=">;
+
 def DashDash : Option<["--"], "", KIND_REMAINING_ARGS>;
Index: llvm/unittests/Option/OptionParsingTest.cpp
===================================================================
--- llvm/unittests/Option/OptionParsingTest.cpp
+++ llvm/unittests/Option/OptionParsingTest.cpp
@@ -298,6 +298,11 @@
   EXPECT_EQ(1U, T.findNearest("/framb:foo", Nearest));
   EXPECT_EQ(Nearest, "/cramb:foo");
 
+  // `-glormp` should have an editing distance of 1 to `-glormp=`.
+  EXPECT_EQ(1U, T.findNearest("--glormp", Nearest));
+  EXPECT_EQ(Nearest, "--glormp=");
+  EXPECT_EQ(0U, T.findNearest("--glormp=foo", Nearest));
+
   // Flags should be included and excluded as specified.
   EXPECT_EQ(1U, T.findNearest("-doopf", Nearest, /*FlagsToInclude=*/OptFlag2));
   EXPECT_EQ(Nearest, "-doopf2");
Index: llvm/lib/Option/OptTable.cpp
===================================================================
--- llvm/lib/Option/OptTable.cpp
+++ llvm/lib/Option/OptTable.cpp
@@ -291,11 +291,14 @@
     else
       std::tie(LHS, RHS) = Option.split(Last);
 
+    std::string NormalizedName = LHS;
+    if (Option.find(Delimiter) == LHS.size())
+      NormalizedName += Delimiter;
+
     // Consider each possible prefix for each candidate to find the most
     // appropriate one. For example, if a user asks for "--helm", suggest
     // "--help" over "-help".
     for (int P = 0; const char *const CandidatePrefix = CandidateInfo.Prefixes[P]; P++) {
-      std::string NormalizedName = (LHS + Delimiter).str();
       std::string Candidate = (CandidatePrefix + CandidateName).str();
       StringRef CandidateRef = Candidate;
       unsigned Distance =


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D61373.197531.patch
Type: text/x-patch
Size: 2150 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20190501/e460d154/attachment.bin>


More information about the llvm-commits mailing list