llvm.org GIT mirror llvm / f4fb66a
Support/CommandLine: Add "Did you mean" print for mismatched operands. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@123717 91177308-0d34-0410-b5e6-96231b3b80d8 Daniel Dunbar 8 years ago
1 changed file(s) with 39 addition(s) and 0 deletion(s). Raw diff Collapse all Expand all
177177 Value = Arg.substr(EqualPos+1);
178178 Arg = Arg.substr(0, EqualPos);
179179 return I->second;
180 }
181
182 /// LookupNearestOption - Lookup the closest match to the option specified by
183 /// the specified option on the command line. If there is a value specified
184 /// (after an equal sign) return that as well. This assumes that leading dashes
185 /// have already been stripped.
186 static Option *LookupNearestOption(StringRef Arg,
187 const StringMap &OptionsMap) {
188 // Reject all dashes.
189 if (Arg.empty()) return 0;
190
191 // Split on any equal sign.
192 StringRef LHS = Arg.split('=').first;
193
194 // Find the closest match.
195 Option *Best = 0;
196 unsigned BestDistance = 0;
197 for (StringMap::const_iterator it = OptionsMap.begin(),
198 ie = OptionsMap.end(); it != ie; ++it) {
199 unsigned Distance = StringRef(it->second->ArgStr).edit_distance(
200 Arg, /*AllowReplacements=*/true, /*MaxEditDistance=*/BestDistance);
201 if (!Best || Distance < BestDistance) {
202 Best = it->second;
203 BestDistance = Distance;
204 }
205 }
206
207 return Best;
180208 }
181209
182210 /// CommaSeparateAndAddOccurence - A wrapper around Handler->addOccurence() that
570598 bool DashDashFound = false; // Have we read '--'?
571599 for (int i = 1; i < argc; ++i) {
572600 Option *Handler = 0;
601 Option *NearestHandler = 0;
573602 StringRef Value;
574603 StringRef ArgName = "";
575604
643672 if (Handler == 0)
644673 Handler = HandlePrefixedOrGroupedOption(ArgName, Value,
645674 ErrorParsing, Opts);
675
676 // Otherwise, look for the closest available option to report to the user
677 // in the upcoming error.
678 if (Handler == 0 && SinkOpts.empty())
679 NearestHandler = LookupNearestOption(ArgName, Opts);
646680 }
647681
648682 if (Handler == 0) {
649683 if (SinkOpts.empty()) {
650684 errs() << ProgramName << ": Unknown command line argument '"
651685 << argv[i] << "'. Try: '" << argv[0] << " -help'\n";
686
687 // If we know a near match, report it as well.
688 errs() << ProgramName << ": Did you mean '-"
689 << NearestHandler->ArgStr << "'?\n";
690
652691 ErrorParsing = true;
653692 } else {
654693 for (SmallVectorImpl::iterator I = SinkOpts.begin(),