[PATCH] D41732: [Option] Add 'findNearest' method to catch typos

Jonathan Roelofs via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Fri Jan 5 04:36:29 PST 2018

jroelofs added inline comments.

Comment at: lib/Option/OptTable.cpp:280
+    // "--helm", suggest "--help" over "-help".
+    StringRef Prefix;
+    for (int P = 0; CandidateInfo.Prefixes[P]; P++) {
modocache wrote:
> jroelofs wrote:
> > ruiu wrote:
> > > I wonder why you chose to use the prefix matching instead of edit distance. Conveniently, StringRef has edit_distance function, so you can use that if you want.
> > +1
> Ah, my apologies, I think this code may not be clear.
> Just to make sure we're on the same page: this loop isn't matching input string `--hel` to valid option `--help`, it's matching each valid option's set of prefixes (for most LLVM programs' options, those are `-`, `--`, or both) to `--hel`.
> The goal here is to find the prefix that matches the one the user provided. The reason I'm doing this is because, below when we compare using `StringRef::edit_distance`, we get an edit distance of `1` between `--hel` and both `--help` (add one `p`) and `-help` (remove one `-`).
> But I think it would be nice, if the user provided the input `--hel`, for LLVM to suggest an option with the same prefix, so I'd like to deterministically return `--help`. That is, I do *not* want to return `-help`.
> Maybe that's not as desirable behavior as I think it is. But, in order to implement it, I believe I'm unable to use `StringRef::edit_distance` here. I need to check whether a prefix (of any length, although conventionally they're one or two characters in most LLVM programs) exists at the front of both a valid option and the option string being passed in. `StringRef::startswith` seems like a natural fit for this, as opposed to `StringRef::edit_distance`.
Ohh I see, you are using edit_distance below for sorting the actual option name matches, and this is just a heuristic tweak to give preference to options with the same lead characters as what the user wrote.


More information about the llvm-commits mailing list