blob: e4cc84afb78edbc130dc60440f81c871f7f865d4 [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 Jahanian0089bc42013-05-08 19:21:00 +000046const CommandInfo *
47CommandTraits::getTypoCorrectCommandInfo(StringRef Typo) const {
48 const unsigned MaxEditDistance = 1;
49 unsigned BestEditDistance = MaxEditDistance + 1;
50 SmallVector<const CommandInfo *, 2> BestCommand;
51
52 int NumOfCommands = sizeof(Commands) / sizeof(CommandInfo);
53 for (int i = 0; i < NumOfCommands; i++) {
54 StringRef Name = Commands[i].Name;
55 unsigned MinPossibleEditDistance = abs((int)Name.size() - (int)Typo.size());
56 if (MinPossibleEditDistance > 0 &&
57 Typo.size() / MinPossibleEditDistance < 1)
58 continue;
59 unsigned EditDistance = Typo.edit_distance(Name, true, MaxEditDistance);
60 if (EditDistance > MaxEditDistance)
61 continue;
62 if (EditDistance == BestEditDistance)
63 BestCommand.push_back(&Commands[i]);
64 else if (EditDistance < BestEditDistance) {
65 BestCommand.clear();
66 BestCommand.push_back(&Commands[i]);
67 BestEditDistance = EditDistance;
68 }
69 }
70 return (BestCommand.size() != 1) ? NULL : BestCommand[0];
71}
72
Dmitri Gribenko6ebf0912013-02-22 14:21:27 +000073CommandInfo *CommandTraits::createCommandInfoWithName(StringRef CommandName) {
Eli Friedman116bb092012-09-11 00:36:26 +000074 char *Name = Allocator.Allocate<char>(CommandName.size() + 1);
Dmitri Gribenkoe4330a32012-09-10 20:32:42 +000075 memcpy(Name, CommandName.data(), CommandName.size());
Eli Friedman116bb092012-09-11 00:36:26 +000076 Name[CommandName.size()] = '\0';
Dmitri Gribenkoaa580812012-08-09 00:03:17 +000077
Dmitri Gribenkoe4330a32012-09-10 20:32:42 +000078 // Value-initialize (=zero-initialize in this case) a new CommandInfo.
79 CommandInfo *Info = new (Allocator) CommandInfo();
80 Info->Name = Name;
81 Info->ID = NextID++;
82
83 RegisteredCommands.push_back(Info);
84
85 return Info;
86}
87
Dmitri Gribenko6ebf0912013-02-22 14:21:27 +000088const CommandInfo *CommandTraits::registerUnknownCommand(
89 StringRef CommandName) {
90 CommandInfo *Info = createCommandInfoWithName(CommandName);
91 Info->IsUnknownCommand = true;
92 return Info;
93}
94
95const CommandInfo *CommandTraits::registerBlockCommand(StringRef CommandName) {
96 CommandInfo *Info = createCommandInfoWithName(CommandName);
97 Info->IsBlockCommand = true;
98 return Info;
99}
100
Dmitri Gribenkoe4330a32012-09-10 20:32:42 +0000101const CommandInfo *CommandTraits::getBuiltinCommandInfo(
102 unsigned CommandID) {
103 if (CommandID < llvm::array_lengthof(Commands))
104 return &Commands[CommandID];
105 return NULL;
106}
107
108const CommandInfo *CommandTraits::getRegisteredCommandInfo(
109 StringRef Name) const {
110 for (unsigned i = 0, e = RegisteredCommands.size(); i != e; ++i) {
111 if (RegisteredCommands[i]->Name == Name)
112 return RegisteredCommands[i];
Dmitri Gribenkoaa580812012-08-09 00:03:17 +0000113 }
Dmitri Gribenkoe4330a32012-09-10 20:32:42 +0000114 return NULL;
Dmitri Gribenkoaa580812012-08-09 00:03:17 +0000115}
116
Dmitri Gribenkoe4330a32012-09-10 20:32:42 +0000117const CommandInfo *CommandTraits::getRegisteredCommandInfo(
118 unsigned CommandID) const {
119 return RegisteredCommands[CommandID - llvm::array_lengthof(Commands)];
Dmitri Gribenkoaa580812012-08-09 00:03:17 +0000120}
121
122} // end namespace comments
123} // end namespace clang
124