llvm.org GIT mirror llvm / 28a57e3
Fix OptTable::findNearest() adding delimiter for free 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. Differential Revision: https://reviews.llvm.org/D61373 git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@359685 91177308-0d34-0410-b5e6-96231b3b80d8 Nico Weber 3 months ago
3 changed file(s) with 16 addition(s) and 9 deletion(s). Raw diff Collapse all Expand all
279279 // Now check if the candidate ends with a character commonly used when
280280 // delimiting an option from its value, such as '=' or ':'. If it does,
281281 // attempt to split the given option based on that delimiter.
282 std::string Delimiter = "";
282 StringRef LHS, RHS;
283283 char Last = CandidateName.back();
284 if (Last == '=' || Last == ':')
285 Delimiter = std::string(1, Last);
286
287 StringRef LHS, RHS;
288 if (Delimiter.empty())
289 LHS = Option;
290 else
284 bool CandidateHasDelimiter = Last == '=' || Last == ':';
285 std::string NormalizedName = Option;
286 if (CandidateHasDelimiter) {
291287 std::tie(LHS, RHS) = Option.split(Last);
288 NormalizedName = LHS;
289 if (Option.find(Last) == LHS.size())
290 NormalizedName += Last;
291 }
292292
293293 // Consider each possible prefix for each candidate to find the most
294294 // appropriate one. For example, if a user asks for "--helm", suggest
295295 // "--help" over "-help".
296296 for (int P = 0;
297297 const char *const CandidatePrefix = CandidateInfo.Prefixes[P]; P++) {
298 std::string NormalizedName = (LHS + Delimiter).str();
299298 std::string Candidate = (CandidatePrefix + CandidateName).str();
300299 StringRef CandidateRef = Candidate;
301300 unsigned Distance =
297297 EXPECT_EQ(1U, T.findNearest("/framb:foo", Nearest));
298298 EXPECT_EQ(Nearest, "/cramb:foo");
299299
300 // `--glormp` should have an editing distance of 1 to `--glormp=`.
301 EXPECT_EQ(1U, T.findNearest("--glormp", Nearest));
302 EXPECT_EQ(Nearest, "--glormp=");
303 EXPECT_EQ(0U, T.findNearest("--glormp=foo", Nearest));
304
300305 // Flags should be included and excluded as specified.
301306 EXPECT_EQ(1U, T.findNearest("-doopf", Nearest, /*FlagsToInclude=*/OptFlag2));
302307 EXPECT_EQ(Nearest, "-doopf2");
3535 def Doopf2 : Flag<["-"], "doopf2">, HelpText<"The doopf2 option">, Flags<[OptFlag2]>;
3636 def Ermgh : Joined<["--"], "ermgh">, HelpText<"The ermgh option">, MetaVarName<"ERMGH">, Flags<[OptFlag1]>;
3737 def Fjormp : Flag<["--"], "fjormp">, HelpText<"The fjormp option">, Flags<[OptFlag1]>;
38
39 def Glormp_eq : Flag<["--"], "glormp=">;
40
3841 def DashDash : Option<["--"], "", KIND_REMAINING_ARGS>;