blob: 7378a7c3ac0606c8707b5576991750fb8e68812b [file] [log] [blame]
Dmitri Gribenkoca7f80a2012-08-09 00:03:17 +00001//===--- CommentCommandTraits.cpp - Comment command properties --*- C++ -*-===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
10#include "clang/AST/CommentCommandTraits.h"
Dmitri Gribenko7acbf002012-09-10 20:32:42 +000011#include "llvm/ADT/STLExtras.h"
Dmitri Gribenkoca7f80a2012-08-09 00:03:17 +000012
13namespace clang {
14namespace comments {
15
Dmitri Gribenko7acbf002012-09-10 20:32:42 +000016#include "clang/AST/CommentCommandInfo.inc"
Dmitri Gribenkoca7f80a2012-08-09 00:03:17 +000017
Dmitri Gribenkoacf2e782013-02-22 14:21:27 +000018CommandTraits::CommandTraits(llvm::BumpPtrAllocator &Allocator,
19 const CommentOptions &CommentOptions) :
20 NextID(llvm::array_lengthof(Commands)), Allocator(Allocator) {
21 registerCommentOptions(CommentOptions);
22}
23
24void CommandTraits::registerCommentOptions(
25 const CommentOptions &CommentOptions) {
26 for (CommentOptions::BlockCommandNamesTy::const_iterator
27 I = CommentOptions.BlockCommandNames.begin(),
28 E = CommentOptions.BlockCommandNames.end();
29 I != E; I++) {
30 registerBlockCommand(*I);
31 }
32}
Dmitri Gribenkoca7f80a2012-08-09 00:03:17 +000033
Dmitri Gribenko7acbf002012-09-10 20:32:42 +000034const CommandInfo *CommandTraits::getCommandInfoOrNULL(StringRef Name) const {
35 if (const CommandInfo *Info = getBuiltinCommandInfo(Name))
36 return Info;
37 return getRegisteredCommandInfo(Name);
38}
Dmitri Gribenkoca7f80a2012-08-09 00:03:17 +000039
Dmitri Gribenko7acbf002012-09-10 20:32:42 +000040const CommandInfo *CommandTraits::getCommandInfo(unsigned CommandID) const {
41 if (const CommandInfo *Info = getBuiltinCommandInfo(CommandID))
42 return Info;
43 return getRegisteredCommandInfo(CommandID);
44}
Dmitri Gribenkoca7f80a2012-08-09 00:03:17 +000045
Fariborz Jahanianaa9b2802013-05-08 22:14:28 +000046const CommandInfo *
47CommandTraits::getTypoCorrectCommandInfo(StringRef Typo) const {
Richard Smith2d709762014-03-06 20:00:05 +000048 // Single-character command impostures, such as \t or \n, should not go
Fariborz Jahanian0321b8f2013-05-09 17:18:52 +000049 // through the fixit logic.
50 if (Typo.size() <= 1)
Richard Smith2d709762014-03-06 20:00:05 +000051 return nullptr;
52
53 // The maximum edit distance we're prepared to accept.
54 const unsigned MaxEditDistance = 1;
55
56 unsigned BestEditDistance = MaxEditDistance;
Fariborz Jahanian6c7a1662013-05-08 19:21:00 +000057 SmallVector<const CommandInfo *, 2> BestCommand;
Richard Smith2d709762014-03-06 20:00:05 +000058
59 auto ConsiderCorrection = [&](const CommandInfo *Command) {
60 StringRef Name = Command->Name;
61
62 unsigned MinPossibleEditDistance = abs((int)Name.size() - (int)Typo.size());
63 if (MinPossibleEditDistance <= BestEditDistance) {
64 unsigned EditDistance = Typo.edit_distance(Name, true, BestEditDistance);
65 if (EditDistance < BestEditDistance) {
66 BestEditDistance = EditDistance;
67 BestCommand.clear();
68 }
69 if (EditDistance == BestEditDistance)
70 BestCommand.push_back(Command);
71 }
72 };
73
74 for (const auto &Command : Commands)
75 ConsiderCorrection(&Command);
76
77 for (const auto *Command : RegisteredCommands)
78 if (!Command->IsUnknownCommand)
79 ConsiderCorrection(Command);
80
81 return BestCommand.size() == 1 ? BestCommand[0] : nullptr;
Fariborz Jahanian6c7a1662013-05-08 19:21:00 +000082}
83
Dmitri Gribenkoacf2e782013-02-22 14:21:27 +000084CommandInfo *CommandTraits::createCommandInfoWithName(StringRef CommandName) {
Eli Friedmande4f4702012-09-11 00:36:26 +000085 char *Name = Allocator.Allocate<char>(CommandName.size() + 1);
Dmitri Gribenko7acbf002012-09-10 20:32:42 +000086 memcpy(Name, CommandName.data(), CommandName.size());
Eli Friedmande4f4702012-09-11 00:36:26 +000087 Name[CommandName.size()] = '\0';
Dmitri Gribenkoca7f80a2012-08-09 00:03:17 +000088
Dmitri Gribenko7acbf002012-09-10 20:32:42 +000089 // Value-initialize (=zero-initialize in this case) a new CommandInfo.
90 CommandInfo *Info = new (Allocator) CommandInfo();
91 Info->Name = Name;
Dario Domiziolia60f5322014-10-15 16:18:20 +000092 // 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.
94 assert((NextID < (1 << CommandInfo::NumCommandIDBits))
95 && "Too many commands. We have limited bits for the command ID.");
Dmitri Gribenko7acbf002012-09-10 20:32:42 +000096 Info->ID = NextID++;
97
98 RegisteredCommands.push_back(Info);
99
100 return Info;
101}
102
Dmitri Gribenkoacf2e782013-02-22 14:21:27 +0000103const CommandInfo *CommandTraits::registerUnknownCommand(
104 StringRef CommandName) {
105 CommandInfo *Info = createCommandInfoWithName(CommandName);
106 Info->IsUnknownCommand = true;
107 return Info;
108}
109
110const CommandInfo *CommandTraits::registerBlockCommand(StringRef CommandName) {
111 CommandInfo *Info = createCommandInfoWithName(CommandName);
112 Info->IsBlockCommand = true;
113 return Info;
114}
115
Dmitri Gribenko7acbf002012-09-10 20:32:42 +0000116const CommandInfo *CommandTraits::getBuiltinCommandInfo(
117 unsigned CommandID) {
118 if (CommandID < llvm::array_lengthof(Commands))
119 return &Commands[CommandID];
Craig Topper36250ad2014-05-12 05:36:57 +0000120 return nullptr;
Dmitri Gribenko7acbf002012-09-10 20:32:42 +0000121}
122
123const CommandInfo *CommandTraits::getRegisteredCommandInfo(
124 StringRef Name) const {
125 for (unsigned i = 0, e = RegisteredCommands.size(); i != e; ++i) {
126 if (RegisteredCommands[i]->Name == Name)
127 return RegisteredCommands[i];
Dmitri Gribenkoca7f80a2012-08-09 00:03:17 +0000128 }
Craig Topper36250ad2014-05-12 05:36:57 +0000129 return nullptr;
Dmitri Gribenkoca7f80a2012-08-09 00:03:17 +0000130}
131
Dmitri Gribenko7acbf002012-09-10 20:32:42 +0000132const CommandInfo *CommandTraits::getRegisteredCommandInfo(
133 unsigned CommandID) const {
134 return RegisteredCommands[CommandID - llvm::array_lengthof(Commands)];
Dmitri Gribenkoca7f80a2012-08-09 00:03:17 +0000135}
136
137} // end namespace comments
138} // end namespace clang
139