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