llvm.org GIT mirror llvm / 9b1f688
Re-push "[Option] Fix PR37006 prefix choice in findNearest" Summary: Original changeset (https://reviews.llvm.org/D46776) by @modocache. It was reverted after the PS4 bot failed. The issue has been determined to be with the way the PS4 SDK handles this particular option. https://reviews.llvm.org/D50410 removes this test, so we can push this again. Patch by Arnaud Coomans! Reviewers: cfe-commits, modocache Reviewed By: modocache Differential Revision: https://reviews.llvm.org/D50515 git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@341329 91177308-0d34-0410-b5e6-96231b3b80d8 Brian Gesiak 11 months ago
3 changed file(s) with 30 addition(s) and 25 deletion(s). Raw diff Collapse all Expand all
251251 unsigned MinimumLength) const {
252252 assert(!Option.empty());
253253
254 // Consider each option as a candidate, finding the closest match.
254 // Consider each [option prefix + option name] pair as a candidate, finding
255 // the closest match.
255256 unsigned BestDistance = UINT_MAX;
256257 for (const Info &CandidateInfo :
257258 ArrayRef(OptionInfos).drop_front(FirstSearchableIndex)) {
258259 StringRef CandidateName = CandidateInfo.Name;
259260
260 // Ignore option candidates with empty names, such as "--", or names
261 // that do not meet the minimum length.
261 // We can eliminate some option prefix/name pairs as candidates right away:
262 // * Ignore option candidates with empty names, such as "--", or names
263 // that do not meet the minimum length.
262264 if (CandidateName.empty() || CandidateName.size() < MinimumLength)
263265 continue;
264266
265 // If FlagsToInclude were specified, ignore options that don't include
266 // those flags.
267 // * If FlagsToInclude were specified, ignore options that don't include
268 // those flags.
267269 if (FlagsToInclude && !(CandidateInfo.Flags & FlagsToInclude))
268270 continue;
269 // Ignore options that contain the FlagsToExclude.
271 // * Ignore options that contain the FlagsToExclude.
270272 if (CandidateInfo.Flags & FlagsToExclude)
271273 continue;
272274
273 // Ignore positional argument option candidates (which do not
274 // have prefixes).
275 // * Ignore positional argument option candidates (which do not
276 // have prefixes).
275277 if (!CandidateInfo.Prefixes)
276278 continue;
277 // Find the most appropriate prefix. For example, if a user asks for
278 // "--helm", suggest "--help" over "-help".
279 StringRef Prefix = CandidateInfo.Prefixes[0];
280 for (int P = 1; CandidateInfo.Prefixes[P]; P++) {
281 if (Option.startswith(CandidateInfo.Prefixes[P]))
282 Prefix = CandidateInfo.Prefixes[P];
283 }
284
285 // Check if the candidate ends with a character commonly used when
279
280 // Now check if the candidate ends with a character commonly used when
286281 // delimiting an option from its value, such as '=' or ':'. If it does,
287282 // attempt to split the given option based on that delimiter.
288283 std::string Delimiter = "";
296291 else
297292 std::tie(LHS, RHS) = Option.split(Last);
298293
299 std::string NormalizedName =
300 (LHS.drop_front(Prefix.size()) + Delimiter).str();
301 unsigned Distance =
302 CandidateName.edit_distance(NormalizedName, /*AllowReplacements=*/true,
303 /*MaxEditDistance=*/BestDistance);
304 if (Distance < BestDistance) {
305 BestDistance = Distance;
306 NearestString = (Prefix + CandidateName + RHS).str();
294 // Consider each possible prefix for each candidate to find the most
295 // appropriate one. For example, if a user asks for "--helm", suggest
296 // "--help" over "-help".
297 for (int P = 0; const char *const CandidatePrefix = CandidateInfo.Prefixes[P]; P++) {
298 std::string NormalizedName = (LHS + Delimiter).str();
299 StringRef Candidate = (CandidatePrefix + CandidateName).str();
300 unsigned Distance =
301 Candidate.edit_distance(NormalizedName, /*AllowReplacements=*/true,
302 /*MaxEditDistance=*/BestDistance);
303 if (Distance < BestDistance) {
304 BestDistance = Distance;
305 NearestString = (Candidate + RHS).str();
306 }
307307 }
308308 }
309309 return BestDistance;
282282 EXPECT_EQ(Nearest, "-blorp");
283283 EXPECT_EQ(1U, T.findNearest("--blorm", Nearest));
284284 EXPECT_EQ(Nearest, "--blorp");
285 EXPECT_EQ(1U, T.findNearest("-blarg", Nearest));
286 EXPECT_EQ(Nearest, "-blarn");
287 EXPECT_EQ(1U, T.findNearest("--blarm", Nearest));
288 EXPECT_EQ(Nearest, "--blarn");
285289 EXPECT_EQ(1U, T.findNearest("-fjormp", Nearest));
286290 EXPECT_EQ(Nearest, "--fjormp");
287291
2929 def SlurpJoined : Option<["-"], "slurpjoined", KIND_REMAINING_ARGS_JOINED>;
3030
3131 def Blorp : Flag<["-", "--"], "blorp">, HelpText<"The blorp option">, Flags<[OptFlag1]>;
32 def Blarn : Flag<["--", "-"], "blarn">, HelpText<"The blarn option">, Flags<[OptFlag1]>;
3233 def Cramb : Joined<["/"], "cramb:">, HelpText<"The cramb option">, MetaVarName<"CRAMB">, Flags<[OptFlag1]>;
3334 def Doopf1 : Flag<["-"], "doopf1">, HelpText<"The doopf1 option">, Flags<[OptFlag1]>;
3435 def Doopf2 : Flag<["-"], "doopf2">, HelpText<"The doopf2 option">, Flags<[OptFlag2]>;