Fix dead store and simplify. No functionality change (although the code is now

correct if MaxEditDistance were increased to something greater than 1).

llvm-svn: 203153
This commit is contained in:
Richard Smith 2014-03-06 20:00:05 +00:00
parent c68b0f7797
commit 2d70976fc0
1 changed files with 31 additions and 36 deletions

View File

@ -43,47 +43,42 @@ const CommandInfo *CommandTraits::getCommandInfo(unsigned CommandID) const {
return getRegisteredCommandInfo(CommandID); return getRegisteredCommandInfo(CommandID);
} }
static void
HelperTypoCorrectCommandInfo(SmallVectorImpl<const CommandInfo *> &BestCommand,
StringRef Typo, const CommandInfo *Command) {
const unsigned MaxEditDistance = 1;
unsigned BestEditDistance = MaxEditDistance + 1;
StringRef Name = Command->Name;
unsigned MinPossibleEditDistance = abs((int)Name.size() - (int)Typo.size());
if (MinPossibleEditDistance > 0 &&
Typo.size() / MinPossibleEditDistance < 1)
return;
unsigned EditDistance = Typo.edit_distance(Name, true, MaxEditDistance);
if (EditDistance > MaxEditDistance)
return;
if (EditDistance == BestEditDistance)
BestCommand.push_back(Command);
else if (EditDistance < BestEditDistance) {
BestCommand.clear();
BestCommand.push_back(Command);
BestEditDistance = EditDistance;
}
}
const CommandInfo * const CommandInfo *
CommandTraits::getTypoCorrectCommandInfo(StringRef Typo) const { CommandTraits::getTypoCorrectCommandInfo(StringRef Typo) const {
// single character command impostures, such as \t or \n must not go // Single-character command impostures, such as \t or \n, should not go
// through the fixit logic. // through the fixit logic.
if (Typo.size() <= 1) if (Typo.size() <= 1)
return NULL; return nullptr;
// The maximum edit distance we're prepared to accept.
const unsigned MaxEditDistance = 1;
unsigned BestEditDistance = MaxEditDistance;
SmallVector<const CommandInfo *, 2> BestCommand; SmallVector<const CommandInfo *, 2> BestCommand;
const int NumOfCommands = llvm::array_lengthof(Commands); auto ConsiderCorrection = [&](const CommandInfo *Command) {
for (int i = 0; i < NumOfCommands; i++) StringRef Name = Command->Name;
HelperTypoCorrectCommandInfo(BestCommand, Typo, &Commands[i]);
unsigned MinPossibleEditDistance = abs((int)Name.size() - (int)Typo.size());
for (unsigned i = 0, e = RegisteredCommands.size(); i != e; ++i) if (MinPossibleEditDistance <= BestEditDistance) {
if (!RegisteredCommands[i]->IsUnknownCommand) unsigned EditDistance = Typo.edit_distance(Name, true, BestEditDistance);
HelperTypoCorrectCommandInfo(BestCommand, Typo, RegisteredCommands[i]); if (EditDistance < BestEditDistance) {
BestEditDistance = EditDistance;
return (BestCommand.size() != 1) ? NULL : BestCommand[0]; BestCommand.clear();
}
if (EditDistance == BestEditDistance)
BestCommand.push_back(Command);
}
};
for (const auto &Command : Commands)
ConsiderCorrection(&Command);
for (const auto *Command : RegisteredCommands)
if (!Command->IsUnknownCommand)
ConsiderCorrection(Command);
return BestCommand.size() == 1 ? BestCommand[0] : nullptr;
} }
CommandInfo *CommandTraits::createCommandInfoWithName(StringRef CommandName) { CommandInfo *CommandTraits::createCommandInfoWithName(StringRef CommandName) {