1e5dd7070Spatrick //===--- CommentCommandTraits.cpp - Comment command properties --*- C++ -*-===//
2e5dd7070Spatrick //
3e5dd7070Spatrick // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4e5dd7070Spatrick // See https://llvm.org/LICENSE.txt for license information.
5e5dd7070Spatrick // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6e5dd7070Spatrick //
7e5dd7070Spatrick //===----------------------------------------------------------------------===//
8e5dd7070Spatrick
9e5dd7070Spatrick #include "clang/AST/CommentCommandTraits.h"
10e5dd7070Spatrick #include "llvm/ADT/STLExtras.h"
11ec727ea7Spatrick #include <cassert>
12e5dd7070Spatrick
13e5dd7070Spatrick namespace clang {
14e5dd7070Spatrick namespace comments {
15e5dd7070Spatrick
16e5dd7070Spatrick #include "clang/AST/CommentCommandInfo.inc"
17e5dd7070Spatrick
CommandTraits(llvm::BumpPtrAllocator & Allocator,const CommentOptions & CommentOptions)18e5dd7070Spatrick CommandTraits::CommandTraits(llvm::BumpPtrAllocator &Allocator,
19*12c85518Srobert const CommentOptions &CommentOptions)
20*12c85518Srobert : NextID(std::size(Commands)), Allocator(Allocator) {
21e5dd7070Spatrick registerCommentOptions(CommentOptions);
22e5dd7070Spatrick }
23e5dd7070Spatrick
registerCommentOptions(const CommentOptions & CommentOptions)24e5dd7070Spatrick void CommandTraits::registerCommentOptions(
25e5dd7070Spatrick const CommentOptions &CommentOptions) {
26e5dd7070Spatrick for (CommentOptions::BlockCommandNamesTy::const_iterator
27e5dd7070Spatrick I = CommentOptions.BlockCommandNames.begin(),
28e5dd7070Spatrick E = CommentOptions.BlockCommandNames.end();
29e5dd7070Spatrick I != E; I++) {
30e5dd7070Spatrick registerBlockCommand(*I);
31e5dd7070Spatrick }
32e5dd7070Spatrick }
33e5dd7070Spatrick
getCommandInfoOrNULL(StringRef Name) const34e5dd7070Spatrick const CommandInfo *CommandTraits::getCommandInfoOrNULL(StringRef Name) const {
35e5dd7070Spatrick if (const CommandInfo *Info = getBuiltinCommandInfo(Name))
36e5dd7070Spatrick return Info;
37e5dd7070Spatrick return getRegisteredCommandInfo(Name);
38e5dd7070Spatrick }
39e5dd7070Spatrick
getCommandInfo(unsigned CommandID) const40e5dd7070Spatrick const CommandInfo *CommandTraits::getCommandInfo(unsigned CommandID) const {
41e5dd7070Spatrick if (const CommandInfo *Info = getBuiltinCommandInfo(CommandID))
42e5dd7070Spatrick return Info;
43e5dd7070Spatrick return getRegisteredCommandInfo(CommandID);
44e5dd7070Spatrick }
45e5dd7070Spatrick
46e5dd7070Spatrick const CommandInfo *
getTypoCorrectCommandInfo(StringRef Typo) const47e5dd7070Spatrick CommandTraits::getTypoCorrectCommandInfo(StringRef Typo) const {
48e5dd7070Spatrick // Single-character command impostures, such as \t or \n, should not go
49e5dd7070Spatrick // through the fixit logic.
50e5dd7070Spatrick if (Typo.size() <= 1)
51e5dd7070Spatrick return nullptr;
52e5dd7070Spatrick
53e5dd7070Spatrick // The maximum edit distance we're prepared to accept.
54e5dd7070Spatrick const unsigned MaxEditDistance = 1;
55e5dd7070Spatrick
56e5dd7070Spatrick unsigned BestEditDistance = MaxEditDistance;
57e5dd7070Spatrick SmallVector<const CommandInfo *, 2> BestCommand;
58e5dd7070Spatrick
59e5dd7070Spatrick auto ConsiderCorrection = [&](const CommandInfo *Command) {
60e5dd7070Spatrick StringRef Name = Command->Name;
61e5dd7070Spatrick
62e5dd7070Spatrick unsigned MinPossibleEditDistance = abs((int)Name.size() - (int)Typo.size());
63e5dd7070Spatrick if (MinPossibleEditDistance <= BestEditDistance) {
64e5dd7070Spatrick unsigned EditDistance = Typo.edit_distance(Name, true, BestEditDistance);
65e5dd7070Spatrick if (EditDistance < BestEditDistance) {
66e5dd7070Spatrick BestEditDistance = EditDistance;
67e5dd7070Spatrick BestCommand.clear();
68e5dd7070Spatrick }
69e5dd7070Spatrick if (EditDistance == BestEditDistance)
70e5dd7070Spatrick BestCommand.push_back(Command);
71e5dd7070Spatrick }
72e5dd7070Spatrick };
73e5dd7070Spatrick
74e5dd7070Spatrick for (const auto &Command : Commands)
75e5dd7070Spatrick ConsiderCorrection(&Command);
76e5dd7070Spatrick
77e5dd7070Spatrick for (const auto *Command : RegisteredCommands)
78e5dd7070Spatrick if (!Command->IsUnknownCommand)
79e5dd7070Spatrick ConsiderCorrection(Command);
80e5dd7070Spatrick
81e5dd7070Spatrick return BestCommand.size() == 1 ? BestCommand[0] : nullptr;
82e5dd7070Spatrick }
83e5dd7070Spatrick
createCommandInfoWithName(StringRef CommandName)84e5dd7070Spatrick CommandInfo *CommandTraits::createCommandInfoWithName(StringRef CommandName) {
85e5dd7070Spatrick char *Name = Allocator.Allocate<char>(CommandName.size() + 1);
86e5dd7070Spatrick memcpy(Name, CommandName.data(), CommandName.size());
87e5dd7070Spatrick Name[CommandName.size()] = '\0';
88e5dd7070Spatrick
89e5dd7070Spatrick // Value-initialize (=zero-initialize in this case) a new CommandInfo.
90e5dd7070Spatrick CommandInfo *Info = new (Allocator) CommandInfo();
91e5dd7070Spatrick Info->Name = Name;
92e5dd7070Spatrick // We only have a limited number of bits to encode command IDs in the
93e5dd7070Spatrick // CommandInfo structure, so the ID numbers can potentially wrap around.
94e5dd7070Spatrick assert((NextID < (1 << CommandInfo::NumCommandIDBits))
95e5dd7070Spatrick && "Too many commands. We have limited bits for the command ID.");
96e5dd7070Spatrick Info->ID = NextID++;
97e5dd7070Spatrick
98e5dd7070Spatrick RegisteredCommands.push_back(Info);
99e5dd7070Spatrick
100e5dd7070Spatrick return Info;
101e5dd7070Spatrick }
102e5dd7070Spatrick
registerUnknownCommand(StringRef CommandName)103e5dd7070Spatrick const CommandInfo *CommandTraits::registerUnknownCommand(
104e5dd7070Spatrick StringRef CommandName) {
105e5dd7070Spatrick CommandInfo *Info = createCommandInfoWithName(CommandName);
106e5dd7070Spatrick Info->IsUnknownCommand = true;
107e5dd7070Spatrick return Info;
108e5dd7070Spatrick }
109e5dd7070Spatrick
registerBlockCommand(StringRef CommandName)110e5dd7070Spatrick const CommandInfo *CommandTraits::registerBlockCommand(StringRef CommandName) {
111e5dd7070Spatrick CommandInfo *Info = createCommandInfoWithName(CommandName);
112e5dd7070Spatrick Info->IsBlockCommand = true;
113e5dd7070Spatrick return Info;
114e5dd7070Spatrick }
115e5dd7070Spatrick
getBuiltinCommandInfo(unsigned CommandID)116e5dd7070Spatrick const CommandInfo *CommandTraits::getBuiltinCommandInfo(
117e5dd7070Spatrick unsigned CommandID) {
118*12c85518Srobert if (CommandID < std::size(Commands))
119e5dd7070Spatrick return &Commands[CommandID];
120e5dd7070Spatrick return nullptr;
121e5dd7070Spatrick }
122e5dd7070Spatrick
getRegisteredCommandInfo(StringRef Name) const123e5dd7070Spatrick const CommandInfo *CommandTraits::getRegisteredCommandInfo(
124e5dd7070Spatrick StringRef Name) const {
125e5dd7070Spatrick for (unsigned i = 0, e = RegisteredCommands.size(); i != e; ++i) {
126e5dd7070Spatrick if (RegisteredCommands[i]->Name == Name)
127e5dd7070Spatrick return RegisteredCommands[i];
128e5dd7070Spatrick }
129e5dd7070Spatrick return nullptr;
130e5dd7070Spatrick }
131e5dd7070Spatrick
getRegisteredCommandInfo(unsigned CommandID) const132e5dd7070Spatrick const CommandInfo *CommandTraits::getRegisteredCommandInfo(
133e5dd7070Spatrick unsigned CommandID) const {
134*12c85518Srobert return RegisteredCommands[CommandID - std::size(Commands)];
135e5dd7070Spatrick }
136e5dd7070Spatrick
137e5dd7070Spatrick } // end namespace comments
138e5dd7070Spatrick } // end namespace clang
139e5dd7070Spatrick
140