Dmitri Gribenko | aa58081 | 2012-08-09 00:03:17 +0000 | [diff] [blame] | 1 | //===--- 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 Gribenko | e4330a3 | 2012-09-10 20:32:42 +0000 | [diff] [blame] | 11 | #include "llvm/ADT/STLExtras.h" |
Dmitri Gribenko | aa58081 | 2012-08-09 00:03:17 +0000 | [diff] [blame] | 12 | |
| 13 | namespace clang { |
| 14 | namespace comments { |
| 15 | |
Dmitri Gribenko | e4330a3 | 2012-09-10 20:32:42 +0000 | [diff] [blame] | 16 | #include "clang/AST/CommentCommandInfo.inc" |
Dmitri Gribenko | aa58081 | 2012-08-09 00:03:17 +0000 | [diff] [blame] | 17 | |
Dmitri Gribenko | 6ebf091 | 2013-02-22 14:21:27 +0000 | [diff] [blame] | 18 | CommandTraits::CommandTraits(llvm::BumpPtrAllocator &Allocator, |
| 19 | const CommentOptions &CommentOptions) : |
| 20 | NextID(llvm::array_lengthof(Commands)), Allocator(Allocator) { |
| 21 | registerCommentOptions(CommentOptions); |
| 22 | } |
| 23 | |
| 24 | void 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 Gribenko | aa58081 | 2012-08-09 00:03:17 +0000 | [diff] [blame] | 33 | |
Dmitri Gribenko | e4330a3 | 2012-09-10 20:32:42 +0000 | [diff] [blame] | 34 | const CommandInfo *CommandTraits::getCommandInfoOrNULL(StringRef Name) const { |
| 35 | if (const CommandInfo *Info = getBuiltinCommandInfo(Name)) |
| 36 | return Info; |
| 37 | return getRegisteredCommandInfo(Name); |
| 38 | } |
Dmitri Gribenko | aa58081 | 2012-08-09 00:03:17 +0000 | [diff] [blame] | 39 | |
Dmitri Gribenko | e4330a3 | 2012-09-10 20:32:42 +0000 | [diff] [blame] | 40 | const CommandInfo *CommandTraits::getCommandInfo(unsigned CommandID) const { |
| 41 | if (const CommandInfo *Info = getBuiltinCommandInfo(CommandID)) |
| 42 | return Info; |
| 43 | return getRegisteredCommandInfo(CommandID); |
| 44 | } |
Dmitri Gribenko | aa58081 | 2012-08-09 00:03:17 +0000 | [diff] [blame] | 45 | |
Fariborz Jahanian | ad91e54 | 2013-05-08 22:14:28 +0000 | [diff] [blame^] | 46 | static void |
| 47 | HelperTypoCorrectCommandInfo(SmallVectorImpl<const CommandInfo *> &BestCommand, |
| 48 | StringRef Typo, const CommandInfo *Command) { |
Fariborz Jahanian | 0089bc4 | 2013-05-08 19:21:00 +0000 | [diff] [blame] | 49 | const unsigned MaxEditDistance = 1; |
| 50 | unsigned BestEditDistance = MaxEditDistance + 1; |
Fariborz Jahanian | ad91e54 | 2013-05-08 22:14:28 +0000 | [diff] [blame^] | 51 | 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 | |
| 69 | const CommandInfo * |
| 70 | CommandTraits::getTypoCorrectCommandInfo(StringRef Typo) const { |
Fariborz Jahanian | 0089bc4 | 2013-05-08 19:21:00 +0000 | [diff] [blame] | 71 | SmallVector<const CommandInfo *, 2> BestCommand; |
| 72 | |
| 73 | int NumOfCommands = sizeof(Commands) / sizeof(CommandInfo); |
Fariborz Jahanian | ad91e54 | 2013-05-08 22:14:28 +0000 | [diff] [blame^] | 74 | for (int i = 0; i < NumOfCommands; i++) |
| 75 | HelperTypoCorrectCommandInfo(BestCommand, Typo, &Commands[i]); |
| 76 | |
| 77 | for (unsigned i = 0, e = RegisteredCommands.size(); i != e; ++i) |
| 78 | if (!RegisteredCommands[i]->IsUnknownCommand) |
| 79 | HelperTypoCorrectCommandInfo(BestCommand, Typo, RegisteredCommands[i]); |
| 80 | |
Fariborz Jahanian | 0089bc4 | 2013-05-08 19:21:00 +0000 | [diff] [blame] | 81 | return (BestCommand.size() != 1) ? NULL : BestCommand[0]; |
| 82 | } |
| 83 | |
Dmitri Gribenko | 6ebf091 | 2013-02-22 14:21:27 +0000 | [diff] [blame] | 84 | CommandInfo *CommandTraits::createCommandInfoWithName(StringRef CommandName) { |
Eli Friedman | 116bb09 | 2012-09-11 00:36:26 +0000 | [diff] [blame] | 85 | char *Name = Allocator.Allocate<char>(CommandName.size() + 1); |
Dmitri Gribenko | e4330a3 | 2012-09-10 20:32:42 +0000 | [diff] [blame] | 86 | memcpy(Name, CommandName.data(), CommandName.size()); |
Eli Friedman | 116bb09 | 2012-09-11 00:36:26 +0000 | [diff] [blame] | 87 | Name[CommandName.size()] = '\0'; |
Dmitri Gribenko | aa58081 | 2012-08-09 00:03:17 +0000 | [diff] [blame] | 88 | |
Dmitri Gribenko | e4330a3 | 2012-09-10 20:32:42 +0000 | [diff] [blame] | 89 | // Value-initialize (=zero-initialize in this case) a new CommandInfo. |
| 90 | CommandInfo *Info = new (Allocator) CommandInfo(); |
| 91 | Info->Name = Name; |
| 92 | Info->ID = NextID++; |
| 93 | |
| 94 | RegisteredCommands.push_back(Info); |
| 95 | |
| 96 | return Info; |
| 97 | } |
| 98 | |
Dmitri Gribenko | 6ebf091 | 2013-02-22 14:21:27 +0000 | [diff] [blame] | 99 | const CommandInfo *CommandTraits::registerUnknownCommand( |
| 100 | StringRef CommandName) { |
| 101 | CommandInfo *Info = createCommandInfoWithName(CommandName); |
| 102 | Info->IsUnknownCommand = true; |
| 103 | return Info; |
| 104 | } |
| 105 | |
| 106 | const CommandInfo *CommandTraits::registerBlockCommand(StringRef CommandName) { |
| 107 | CommandInfo *Info = createCommandInfoWithName(CommandName); |
| 108 | Info->IsBlockCommand = true; |
| 109 | return Info; |
| 110 | } |
| 111 | |
Dmitri Gribenko | e4330a3 | 2012-09-10 20:32:42 +0000 | [diff] [blame] | 112 | const CommandInfo *CommandTraits::getBuiltinCommandInfo( |
| 113 | unsigned CommandID) { |
| 114 | if (CommandID < llvm::array_lengthof(Commands)) |
| 115 | return &Commands[CommandID]; |
| 116 | return NULL; |
| 117 | } |
| 118 | |
| 119 | const CommandInfo *CommandTraits::getRegisteredCommandInfo( |
| 120 | StringRef Name) const { |
| 121 | for (unsigned i = 0, e = RegisteredCommands.size(); i != e; ++i) { |
| 122 | if (RegisteredCommands[i]->Name == Name) |
| 123 | return RegisteredCommands[i]; |
Dmitri Gribenko | aa58081 | 2012-08-09 00:03:17 +0000 | [diff] [blame] | 124 | } |
Dmitri Gribenko | e4330a3 | 2012-09-10 20:32:42 +0000 | [diff] [blame] | 125 | return NULL; |
Dmitri Gribenko | aa58081 | 2012-08-09 00:03:17 +0000 | [diff] [blame] | 126 | } |
| 127 | |
Dmitri Gribenko | e4330a3 | 2012-09-10 20:32:42 +0000 | [diff] [blame] | 128 | const CommandInfo *CommandTraits::getRegisteredCommandInfo( |
| 129 | unsigned CommandID) const { |
| 130 | return RegisteredCommands[CommandID - llvm::array_lengthof(Commands)]; |
Dmitri Gribenko | aa58081 | 2012-08-09 00:03:17 +0000 | [diff] [blame] | 131 | } |
| 132 | |
| 133 | } // end namespace comments |
| 134 | } // end namespace clang |
| 135 | |