blob: 6baf4a9dc6b14e30b3da3b1582f3f31fe05d5669 [file] [log] [blame]
Dmitri Gribenkoaa580812012-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 Gribenkoe4330a32012-09-10 20:32:42 +000011#include "llvm/ADT/STLExtras.h"
Dmitri Gribenkoaa580812012-08-09 00:03:17 +000012
13namespace clang {
14namespace comments {
15
Dmitri Gribenkoe4330a32012-09-10 20:32:42 +000016#include "clang/AST/CommentCommandInfo.inc"
Dmitri Gribenkoaa580812012-08-09 00:03:17 +000017
Dmitri Gribenko6ebf0912013-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 Gribenkoaa580812012-08-09 00:03:17 +000033
Dmitri Gribenkoe4330a32012-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 Gribenkoaa580812012-08-09 00:03:17 +000039
Dmitri Gribenkoe4330a32012-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 Gribenkoaa580812012-08-09 00:03:17 +000045
Fariborz Jahanianad91e542013-05-08 22:14:28 +000046static void
47HelperTypoCorrectCommandInfo(SmallVectorImpl<const CommandInfo *> &BestCommand,
48 StringRef Typo, const CommandInfo *Command) {
Fariborz Jahanian0089bc42013-05-08 19:21:00 +000049 const unsigned MaxEditDistance = 1;
50 unsigned BestEditDistance = MaxEditDistance + 1;
Fariborz Jahanianad91e542013-05-08 22:14:28 +000051 StringRef Name = Command->Name;
52
53 unsigned MinPossibleEditDistance = abs((int)Name.size() - (int)Typo.size());
54 if (MinPossibleEditDistance > 0 &&
55 Typo.size() / MinPossibleEditDistance < 1)
56 return;
57 unsigned EditDistance = Typo.edit_distance(Name, true, MaxEditDistance);
58 if (EditDistance > MaxEditDistance)
59 return;
60 if (EditDistance == BestEditDistance)
61 BestCommand.push_back(Command);
62 else if (EditDistance < BestEditDistance) {
63 BestCommand.clear();
64 BestCommand.push_back(Command);
65 BestEditDistance = EditDistance;
66 }
67}
68
69const CommandInfo *
70CommandTraits::getTypoCorrectCommandInfo(StringRef Typo) const {
Fariborz Jahanianf4030ae2013-05-09 17:18:52 +000071 // single character command impostures, such as \t or \n must not go
72 // through the fixit logic.
73 if (Typo.size() <= 1)
74 return NULL;
75
Fariborz Jahanian0089bc42013-05-08 19:21:00 +000076 SmallVector<const CommandInfo *, 2> BestCommand;
77
78 int NumOfCommands = sizeof(Commands) / sizeof(CommandInfo);
Fariborz Jahanianad91e542013-05-08 22:14:28 +000079 for (int i = 0; i < NumOfCommands; i++)
80 HelperTypoCorrectCommandInfo(BestCommand, Typo, &Commands[i]);
81
82 for (unsigned i = 0, e = RegisteredCommands.size(); i != e; ++i)
83 if (!RegisteredCommands[i]->IsUnknownCommand)
84 HelperTypoCorrectCommandInfo(BestCommand, Typo, RegisteredCommands[i]);
85
Fariborz Jahanian0089bc42013-05-08 19:21:00 +000086 return (BestCommand.size() != 1) ? NULL : BestCommand[0];
87}
88
Dmitri Gribenko6ebf0912013-02-22 14:21:27 +000089CommandInfo *CommandTraits::createCommandInfoWithName(StringRef CommandName) {
Eli Friedman116bb092012-09-11 00:36:26 +000090 char *Name = Allocator.Allocate<char>(CommandName.size() + 1);
Dmitri Gribenkoe4330a32012-09-10 20:32:42 +000091 memcpy(Name, CommandName.data(), CommandName.size());
Eli Friedman116bb092012-09-11 00:36:26 +000092 Name[CommandName.size()] = '\0';
Dmitri Gribenkoaa580812012-08-09 00:03:17 +000093
Dmitri Gribenkoe4330a32012-09-10 20:32:42 +000094 // Value-initialize (=zero-initialize in this case) a new CommandInfo.
95 CommandInfo *Info = new (Allocator) CommandInfo();
96 Info->Name = Name;
97 Info->ID = NextID++;
98
99 RegisteredCommands.push_back(Info);
100
101 return Info;
102}
103
Dmitri Gribenko6ebf0912013-02-22 14:21:27 +0000104const CommandInfo *CommandTraits::registerUnknownCommand(
105 StringRef CommandName) {
106 CommandInfo *Info = createCommandInfoWithName(CommandName);
107 Info->IsUnknownCommand = true;
108 return Info;
109}
110
111const CommandInfo *CommandTraits::registerBlockCommand(StringRef CommandName) {
112 CommandInfo *Info = createCommandInfoWithName(CommandName);
113 Info->IsBlockCommand = true;
114 return Info;
115}
116
Dmitri Gribenkoe4330a32012-09-10 20:32:42 +0000117const CommandInfo *CommandTraits::getBuiltinCommandInfo(
118 unsigned CommandID) {
119 if (CommandID < llvm::array_lengthof(Commands))
120 return &Commands[CommandID];
121 return NULL;
122}
123
124const CommandInfo *CommandTraits::getRegisteredCommandInfo(
125 StringRef Name) const {
126 for (unsigned i = 0, e = RegisteredCommands.size(); i != e; ++i) {
127 if (RegisteredCommands[i]->Name == Name)
128 return RegisteredCommands[i];
Dmitri Gribenkoaa580812012-08-09 00:03:17 +0000129 }
Dmitri Gribenkoe4330a32012-09-10 20:32:42 +0000130 return NULL;
Dmitri Gribenkoaa580812012-08-09 00:03:17 +0000131}
132
Dmitri Gribenkoe4330a32012-09-10 20:32:42 +0000133const CommandInfo *CommandTraits::getRegisteredCommandInfo(
134 unsigned CommandID) const {
135 return RegisteredCommands[CommandID - llvm::array_lengthof(Commands)];
Dmitri Gribenkoaa580812012-08-09 00:03:17 +0000136}
137
138} // end namespace comments
139} // end namespace clang
140