[PATCH] D41873: [Option] For typo '-foo', suggest '--foo'
Brian Gesiak via Phabricator via llvm-commits
llvm-commits at lists.llvm.org
Tue Jan 9 10:50:46 PST 2018
modocache created this revision.
modocache added reviewers: v.g.vassilev, teemperor, ruiu, jroelofs, yamaguchi.
https://reviews.llvm.org/rL321877 introduced the `OptTable::findNearest`
method, to find the closest edit distance option for a given string.
However, the implementation contained a bug: for a typo `-foo` with an
edit distance of 1 away from a valid option `--foo`, `findNearest`
would suggest a nearby option of `foo`. That is, the result would not
include the `--` prefix, and so was not a valid option.
Fix the bug by ensuring that the prefix string is initialized to one of
the valid prefixes for the option.
Test Plan: `check-llvm-unit`
https://reviews.llvm.org/D41873
Files:
lib/Option/OptTable.cpp
unittests/Option/OptionParsingTest.cpp
unittests/Option/Opts.td
Index: unittests/Option/Opts.td
===================================================================
--- unittests/Option/Opts.td
+++ unittests/Option/Opts.td
@@ -34,4 +34,5 @@
def Doopf1 : Flag<["-"], "doopf1">, HelpText<"The doopf1 option">, Flags<[OptFlag1]>;
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 DashDash : Option<["--"], "", KIND_REMAINING_ARGS>;
Index: unittests/Option/OptionParsingTest.cpp
===================================================================
--- unittests/Option/OptionParsingTest.cpp
+++ unittests/Option/OptionParsingTest.cpp
@@ -283,6 +283,8 @@
EXPECT_EQ(Nearest, "-blorp");
EXPECT_EQ(1U, T.findNearest("--blorm", Nearest));
EXPECT_EQ(Nearest, "--blorp");
+ EXPECT_EQ(1U, T.findNearest("-fjormp", Nearest));
+ EXPECT_EQ(Nearest, "--fjormp");
// The nearest candidate respects the prefix and value delimiter
// of the original string.
Index: lib/Option/OptTable.cpp
===================================================================
--- lib/Option/OptTable.cpp
+++ lib/Option/OptTable.cpp
@@ -277,8 +277,8 @@
continue;
// Find the most appropriate prefix. For example, if a user asks for
// "--helm", suggest "--help" over "-help".
- StringRef Prefix;
- for (int P = 0; CandidateInfo.Prefixes[P]; P++) {
+ StringRef Prefix = CandidateInfo.Prefixes[0];
+ for (int P = 1; CandidateInfo.Prefixes[P]; P++) {
if (Option.startswith(CandidateInfo.Prefixes[P]))
Prefix = CandidateInfo.Prefixes[P];
}
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D41873.129126.patch
Type: text/x-patch
Size: 1740 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20180109/86d109c9/attachment.bin>
More information about the llvm-commits
mailing list