2012-08-09 08:03:17 +08:00
|
|
|
//===--- CommentCommandTraits.cpp - Comment command properties --*- C++ -*-===//
|
|
|
|
//
|
2019-01-19 16:50:56 +08:00
|
|
|
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
|
|
|
// See https://llvm.org/LICENSE.txt for license information.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
2012-08-09 08:03:17 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "clang/AST/CommentCommandTraits.h"
|
2012-09-11 04:32:42 +08:00
|
|
|
#include "llvm/ADT/STLExtras.h"
|
2020-03-03 01:24:11 +08:00
|
|
|
#include <cassert>
|
2012-08-09 08:03:17 +08:00
|
|
|
|
|
|
|
namespace clang {
|
|
|
|
namespace comments {
|
|
|
|
|
2012-09-11 04:32:42 +08:00
|
|
|
#include "clang/AST/CommentCommandInfo.inc"
|
2012-08-09 08:03:17 +08:00
|
|
|
|
2013-02-22 22:21:27 +08:00
|
|
|
CommandTraits::CommandTraits(llvm::BumpPtrAllocator &Allocator,
|
|
|
|
const CommentOptions &CommentOptions) :
|
|
|
|
NextID(llvm::array_lengthof(Commands)), Allocator(Allocator) {
|
|
|
|
registerCommentOptions(CommentOptions);
|
|
|
|
}
|
|
|
|
|
|
|
|
void CommandTraits::registerCommentOptions(
|
|
|
|
const CommentOptions &CommentOptions) {
|
|
|
|
for (CommentOptions::BlockCommandNamesTy::const_iterator
|
|
|
|
I = CommentOptions.BlockCommandNames.begin(),
|
|
|
|
E = CommentOptions.BlockCommandNames.end();
|
|
|
|
I != E; I++) {
|
|
|
|
registerBlockCommand(*I);
|
|
|
|
}
|
|
|
|
}
|
2012-08-09 08:03:17 +08:00
|
|
|
|
2012-09-11 04:32:42 +08:00
|
|
|
const CommandInfo *CommandTraits::getCommandInfoOrNULL(StringRef Name) const {
|
|
|
|
if (const CommandInfo *Info = getBuiltinCommandInfo(Name))
|
|
|
|
return Info;
|
|
|
|
return getRegisteredCommandInfo(Name);
|
2012-08-09 08:03:17 +08:00
|
|
|
}
|
|
|
|
|
2012-09-11 04:32:42 +08:00
|
|
|
const CommandInfo *CommandTraits::getCommandInfo(unsigned CommandID) const {
|
|
|
|
if (const CommandInfo *Info = getBuiltinCommandInfo(CommandID))
|
|
|
|
return Info;
|
|
|
|
return getRegisteredCommandInfo(CommandID);
|
|
|
|
}
|
2012-08-09 08:03:17 +08:00
|
|
|
|
2013-05-09 06:14:28 +08:00
|
|
|
const CommandInfo *
|
|
|
|
CommandTraits::getTypoCorrectCommandInfo(StringRef Typo) const {
|
2014-03-07 04:00:05 +08:00
|
|
|
// Single-character command impostures, such as \t or \n, should not go
|
2013-05-10 01:18:52 +08:00
|
|
|
// through the fixit logic.
|
|
|
|
if (Typo.size() <= 1)
|
2014-03-07 04:00:05 +08:00
|
|
|
return nullptr;
|
|
|
|
|
|
|
|
// The maximum edit distance we're prepared to accept.
|
|
|
|
const unsigned MaxEditDistance = 1;
|
|
|
|
|
|
|
|
unsigned BestEditDistance = MaxEditDistance;
|
2013-05-09 03:21:00 +08:00
|
|
|
SmallVector<const CommandInfo *, 2> BestCommand;
|
2014-03-07 04:00:05 +08:00
|
|
|
|
|
|
|
auto ConsiderCorrection = [&](const CommandInfo *Command) {
|
|
|
|
StringRef Name = Command->Name;
|
|
|
|
|
|
|
|
unsigned MinPossibleEditDistance = abs((int)Name.size() - (int)Typo.size());
|
|
|
|
if (MinPossibleEditDistance <= BestEditDistance) {
|
|
|
|
unsigned EditDistance = Typo.edit_distance(Name, true, BestEditDistance);
|
|
|
|
if (EditDistance < BestEditDistance) {
|
|
|
|
BestEditDistance = EditDistance;
|
|
|
|
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;
|
2013-05-09 03:21:00 +08:00
|
|
|
}
|
|
|
|
|
2013-02-22 22:21:27 +08:00
|
|
|
CommandInfo *CommandTraits::createCommandInfoWithName(StringRef CommandName) {
|
2012-09-11 08:36:26 +08:00
|
|
|
char *Name = Allocator.Allocate<char>(CommandName.size() + 1);
|
2012-09-11 04:32:42 +08:00
|
|
|
memcpy(Name, CommandName.data(), CommandName.size());
|
2012-09-11 08:36:26 +08:00
|
|
|
Name[CommandName.size()] = '\0';
|
2012-08-09 08:03:17 +08:00
|
|
|
|
2012-09-11 04:32:42 +08:00
|
|
|
// Value-initialize (=zero-initialize in this case) a new CommandInfo.
|
|
|
|
CommandInfo *Info = new (Allocator) CommandInfo();
|
|
|
|
Info->Name = Name;
|
2014-10-16 00:18:20 +08:00
|
|
|
// We only have a limited number of bits to encode command IDs in the
|
|
|
|
// CommandInfo structure, so the ID numbers can potentially wrap around.
|
|
|
|
assert((NextID < (1 << CommandInfo::NumCommandIDBits))
|
|
|
|
&& "Too many commands. We have limited bits for the command ID.");
|
2012-09-11 04:32:42 +08:00
|
|
|
Info->ID = NextID++;
|
2012-08-09 08:03:17 +08:00
|
|
|
|
2012-09-11 04:32:42 +08:00
|
|
|
RegisteredCommands.push_back(Info);
|
2012-08-09 08:03:17 +08:00
|
|
|
|
2012-09-11 04:32:42 +08:00
|
|
|
return Info;
|
2012-08-09 08:03:17 +08:00
|
|
|
}
|
|
|
|
|
2013-02-22 22:21:27 +08:00
|
|
|
const CommandInfo *CommandTraits::registerUnknownCommand(
|
|
|
|
StringRef CommandName) {
|
|
|
|
CommandInfo *Info = createCommandInfoWithName(CommandName);
|
|
|
|
Info->IsUnknownCommand = true;
|
|
|
|
return Info;
|
|
|
|
}
|
|
|
|
|
|
|
|
const CommandInfo *CommandTraits::registerBlockCommand(StringRef CommandName) {
|
|
|
|
CommandInfo *Info = createCommandInfoWithName(CommandName);
|
|
|
|
Info->IsBlockCommand = true;
|
|
|
|
return Info;
|
|
|
|
}
|
|
|
|
|
2012-09-11 04:32:42 +08:00
|
|
|
const CommandInfo *CommandTraits::getBuiltinCommandInfo(
|
|
|
|
unsigned CommandID) {
|
|
|
|
if (CommandID < llvm::array_lengthof(Commands))
|
|
|
|
return &Commands[CommandID];
|
2014-05-12 13:36:57 +08:00
|
|
|
return nullptr;
|
2012-08-10 02:20:29 +08:00
|
|
|
}
|
|
|
|
|
2012-09-11 04:32:42 +08:00
|
|
|
const CommandInfo *CommandTraits::getRegisteredCommandInfo(
|
|
|
|
StringRef Name) const {
|
|
|
|
for (unsigned i = 0, e = RegisteredCommands.size(); i != e; ++i) {
|
|
|
|
if (RegisteredCommands[i]->Name == Name)
|
|
|
|
return RegisteredCommands[i];
|
|
|
|
}
|
2014-05-12 13:36:57 +08:00
|
|
|
return nullptr;
|
2012-08-09 08:03:17 +08:00
|
|
|
}
|
|
|
|
|
2012-09-11 04:32:42 +08:00
|
|
|
const CommandInfo *CommandTraits::getRegisteredCommandInfo(
|
|
|
|
unsigned CommandID) const {
|
|
|
|
return RegisteredCommands[CommandID - llvm::array_lengthof(Commands)];
|
2012-08-09 08:03:17 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
} // end namespace comments
|
|
|
|
} // end namespace clang
|
|
|
|
|