blob: b306fcbb154f3c517a72bfaf6844a834a67cdb08 [file] [log] [blame]
Dmitri Gribenkoca7f80a2012-08-09 00:03:17 +00001//===--- CommentCommandTraits.cpp - Comment command properties --*- C++ -*-===//
2//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// 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
Dmitri Gribenkoca7f80a2012-08-09 00:03:17 +00006//
7//===----------------------------------------------------------------------===//
8
9#include "clang/AST/CommentCommandTraits.h"
Dmitri Gribenko7acbf002012-09-10 20:32:42 +000010#include "llvm/ADT/STLExtras.h"
Dmitri Gribenkoca7f80a2012-08-09 00:03:17 +000011
12namespace clang {
13namespace comments {
14
Dmitri Gribenko7acbf002012-09-10 20:32:42 +000015#include "clang/AST/CommentCommandInfo.inc"
Dmitri Gribenkoca7f80a2012-08-09 00:03:17 +000016
Dmitri Gribenkoacf2e782013-02-22 14:21:27 +000017CommandTraits::CommandTraits(llvm::BumpPtrAllocator &Allocator,
18 const CommentOptions &CommentOptions) :
19 NextID(llvm::array_lengthof(Commands)), Allocator(Allocator) {
20 registerCommentOptions(CommentOptions);
21}
22
23void CommandTraits::registerCommentOptions(
24 const CommentOptions &CommentOptions) {
25 for (CommentOptions::BlockCommandNamesTy::const_iterator
26 I = CommentOptions.BlockCommandNames.begin(),
27 E = CommentOptions.BlockCommandNames.end();
28 I != E; I++) {
29 registerBlockCommand(*I);
30 }
31}
Dmitri Gribenkoca7f80a2012-08-09 00:03:17 +000032
Dmitri Gribenko7acbf002012-09-10 20:32:42 +000033const CommandInfo *CommandTraits::getCommandInfoOrNULL(StringRef Name) const {
34 if (const CommandInfo *Info = getBuiltinCommandInfo(Name))
35 return Info;
36 return getRegisteredCommandInfo(Name);
37}
Dmitri Gribenkoca7f80a2012-08-09 00:03:17 +000038
Dmitri Gribenko7acbf002012-09-10 20:32:42 +000039const CommandInfo *CommandTraits::getCommandInfo(unsigned CommandID) const {
40 if (const CommandInfo *Info = getBuiltinCommandInfo(CommandID))
41 return Info;
42 return getRegisteredCommandInfo(CommandID);
43}
Dmitri Gribenkoca7f80a2012-08-09 00:03:17 +000044
Fariborz Jahanianaa9b2802013-05-08 22:14:28 +000045const CommandInfo *
46CommandTraits::getTypoCorrectCommandInfo(StringRef Typo) const {
Richard Smith2d709762014-03-06 20:00:05 +000047 // Single-character command impostures, such as \t or \n, should not go
Fariborz Jahanian0321b8f2013-05-09 17:18:52 +000048 // through the fixit logic.
49 if (Typo.size() <= 1)
Richard Smith2d709762014-03-06 20:00:05 +000050 return nullptr;
51
52 // The maximum edit distance we're prepared to accept.
53 const unsigned MaxEditDistance = 1;
54
55 unsigned BestEditDistance = MaxEditDistance;
Fariborz Jahanian6c7a1662013-05-08 19:21:00 +000056 SmallVector<const CommandInfo *, 2> BestCommand;
Richard Smith2d709762014-03-06 20:00:05 +000057
58 auto ConsiderCorrection = [&](const CommandInfo *Command) {
59 StringRef Name = Command->Name;
60
61 unsigned MinPossibleEditDistance = abs((int)Name.size() - (int)Typo.size());
62 if (MinPossibleEditDistance <= BestEditDistance) {
63 unsigned EditDistance = Typo.edit_distance(Name, true, BestEditDistance);
64 if (EditDistance < BestEditDistance) {
65 BestEditDistance = EditDistance;
66 BestCommand.clear();
67 }
68 if (EditDistance == BestEditDistance)
69 BestCommand.push_back(Command);
70 }
71 };
72
73 for (const auto &Command : Commands)
74 ConsiderCorrection(&Command);
75
76 for (const auto *Command : RegisteredCommands)
77 if (!Command->IsUnknownCommand)
78 ConsiderCorrection(Command);
79
80 return BestCommand.size() == 1 ? BestCommand[0] : nullptr;
Fariborz Jahanian6c7a1662013-05-08 19:21:00 +000081}
82
Dmitri Gribenkoacf2e782013-02-22 14:21:27 +000083CommandInfo *CommandTraits::createCommandInfoWithName(StringRef CommandName) {
Eli Friedmande4f4702012-09-11 00:36:26 +000084 char *Name = Allocator.Allocate<char>(CommandName.size() + 1);
Dmitri Gribenko7acbf002012-09-10 20:32:42 +000085 memcpy(Name, CommandName.data(), CommandName.size());
Eli Friedmande4f4702012-09-11 00:36:26 +000086 Name[CommandName.size()] = '\0';
Dmitri Gribenkoca7f80a2012-08-09 00:03:17 +000087
Dmitri Gribenko7acbf002012-09-10 20:32:42 +000088 // Value-initialize (=zero-initialize in this case) a new CommandInfo.
89 CommandInfo *Info = new (Allocator) CommandInfo();
90 Info->Name = Name;
Dario Domiziolia60f5322014-10-15 16:18:20 +000091 // We only have a limited number of bits to encode command IDs in the
92 // CommandInfo structure, so the ID numbers can potentially wrap around.
93 assert((NextID < (1 << CommandInfo::NumCommandIDBits))
94 && "Too many commands. We have limited bits for the command ID.");
Dmitri Gribenko7acbf002012-09-10 20:32:42 +000095 Info->ID = NextID++;
96
97 RegisteredCommands.push_back(Info);
98
99 return Info;
100}
101
Dmitri Gribenkoacf2e782013-02-22 14:21:27 +0000102const CommandInfo *CommandTraits::registerUnknownCommand(
103 StringRef CommandName) {
104 CommandInfo *Info = createCommandInfoWithName(CommandName);
105 Info->IsUnknownCommand = true;
106 return Info;
107}
108
109const CommandInfo *CommandTraits::registerBlockCommand(StringRef CommandName) {
110 CommandInfo *Info = createCommandInfoWithName(CommandName);
111 Info->IsBlockCommand = true;
112 return Info;
113}
114
Dmitri Gribenko7acbf002012-09-10 20:32:42 +0000115const CommandInfo *CommandTraits::getBuiltinCommandInfo(
116 unsigned CommandID) {
117 if (CommandID < llvm::array_lengthof(Commands))
118 return &Commands[CommandID];
Craig Topper36250ad2014-05-12 05:36:57 +0000119 return nullptr;
Dmitri Gribenko7acbf002012-09-10 20:32:42 +0000120}
121
122const CommandInfo *CommandTraits::getRegisteredCommandInfo(
123 StringRef Name) const {
124 for (unsigned i = 0, e = RegisteredCommands.size(); i != e; ++i) {
125 if (RegisteredCommands[i]->Name == Name)
126 return RegisteredCommands[i];
Dmitri Gribenkoca7f80a2012-08-09 00:03:17 +0000127 }
Craig Topper36250ad2014-05-12 05:36:57 +0000128 return nullptr;
Dmitri Gribenkoca7f80a2012-08-09 00:03:17 +0000129}
130
Dmitri Gribenko7acbf002012-09-10 20:32:42 +0000131const CommandInfo *CommandTraits::getRegisteredCommandInfo(
132 unsigned CommandID) const {
133 return RegisteredCommands[CommandID - llvm::array_lengthof(Commands)];
Dmitri Gribenkoca7f80a2012-08-09 00:03:17 +0000134}
135
136} // end namespace comments
137} // end namespace clang
138